diff --git a/runtime/libia2/CMakeLists.txt b/runtime/libia2/CMakeLists.txt index 18d5efb0d..5674442b5 100644 --- a/runtime/libia2/CMakeLists.txt +++ b/runtime/libia2/CMakeLists.txt @@ -13,6 +13,10 @@ if(LIBIA2_DEBUG) target_compile_definitions(libia2 PUBLIC LIBIA2_DEBUG=1) endif() +if(LIBIA2_DEBUG_LOG) + target_compile_definitions(libia2 PRIVATE LIBIA2_DEBUG_LOG=1) +endif() + target_link_options(libia2 INTERFACE "-pthread" diff --git a/runtime/libia2/ia2.c b/runtime/libia2/ia2.c index b86e16468..104596819 100644 --- a/runtime/libia2/ia2.c +++ b/runtime/libia2/ia2.c @@ -351,7 +351,9 @@ int protect_pages(struct dl_phdr_info *info, size_t size, void *data) { search_args->found_library_count++; } - // printf("protecting library: %s\n", basename(info->dlpi_name)); +#if LIBIA2_DEBUG_LOG + printf("protecting library: %s\n", basename(info->dlpi_name)); +#endif struct AddressRange shared_ranges[NUM_SHARED_RANGES] = {0}; size_t shared_range_count = 0; @@ -379,6 +381,9 @@ int protect_pages(struct dl_phdr_info *info, size_t size, void *data) { search_args->shared_sections[i].end); exit(-1); } +#if LIBIA2_DEBUG_LOG + printf("Shared range %zu: 0x%" PRIx64 "-0x%" PRIx64 "\n", shared_range_count, cur_range->start, cur_range->end); +#endif shared_range_count++; } @@ -430,6 +435,9 @@ int protect_pages(struct dl_phdr_info *info, size_t size, void *data) { Elf64_Addr start = (info->dlpi_addr + phdr.p_vaddr) & ~0xFFFUL; Elf64_Addr seg_end = (start + phdr.p_memsz + 0xFFFUL) & ~0xFFFUL; +#if LIBIA2_DEBUG_LOG + printf("Segment %zu: 0x%" PRIx64 "-0x%" PRIx64 "\n", i, start, seg_end); +#endif while (start < seg_end) { Elf64_Addr cur_end = seg_end; @@ -440,6 +448,9 @@ int protect_pages(struct dl_phdr_info *info, size_t size, void *data) { // and remove the overlapping portion. if (shared_ranges[j].start <= start && shared_ranges[j].end > start) { start = shared_ranges[j].end; +#if LIBIA2_DEBUG_LOG + printf("Shared range %zu overlaps start of segment %zu, adjusting start to 0x%" PRIx64 "\n", j, i, start); +#endif } // Look for a shared range overlapping any of the rest of the current @@ -448,6 +459,9 @@ int protect_pages(struct dl_phdr_info *info, size_t size, void *data) { if (shared_ranges[j].start > start && shared_ranges[j].start < cur_end) { cur_end = shared_ranges[j].start; +#if LIBIA2_DEBUG_LOG + printf("Shared range %zu overlaps end of segment %zu, adjusting end to 0x%" PRIx64 "\n", j, i, cur_end); +#endif } } diff --git a/runtime/libia2/include/ia2_internal.h b/runtime/libia2/include/ia2_internal.h index a04ad2cd1..2be035f35 100644 --- a/runtime/libia2/include/ia2_internal.h +++ b/runtime/libia2/include/ia2_internal.h @@ -300,9 +300,16 @@ works as a reasonable signpost no-op. */ #if defined(__aarch64__) int ia2_mprotect_with_tag(void *addr, size_t len, int prot, int tag); #elif defined(__x86_64__) +#if LIBIA2_DEBUG_LOG +static int ia2_mprotect_with_tag(void *addr, size_t len, int prot, int tag) { + printf("ia2_mprotect_with_tag(addr=%p, len=%zu, prot=%d, tag=%d)\n", addr, len, prot, tag); + return pkey_mprotect(addr, len, prot, tag); +} +#else /* We can't use an alias attribute since this points to a function outside the translation unit */ #define ia2_mprotect_with_tag pkey_mprotect #endif +#endif char *allocate_stack(int i); void verify_tls_padding(void); void ensure_pkeys_allocated(int *n_to_alloc);