@samitouri / QOSamiQemu / commits / 156e536a7b

linux-user: Fix AT_PHDR when program headers are relocated into their own segment

When a binary is patched or relocated such that the program header table is moved into a separate PT_LOAD segment (rather than sitting at the start of the first loadable segment), QEMU's AT_PHDR auxv entry becomes incorrect. The loader was computing AT_PHDR as load_addr + e_phoff, which assumes the headers are mapped 1:1 from file offset 0. This breaks when the headers are elsewhere. The Linux kernel instead locates the PT_LOAD segment that contains e_phoff, then computes the in-memory address as p_vaddr + (e_phoff - p_offset). This correctly handles relocated headers. Fix by: 1. Add phdr_addr field to image_info to cache the resolved address. 2. Initialize to load_addr + e_phoff (fallback for headers outside any PT_LOAD). 3. In the PT_LOAD mapping loop, detect if the segment contains e_phoff and override with the segment-relative address. 4. Use info->phdr_addr for AT_PHDR instead of the incorrect formula. Signed-off-by: Akshit Yadav <valium7171@gmail.com> Reviewed-by: Helge Deller <deller@gmx.de>

Akshit Yadav committed Jun 13, 2026 at 20:51 UTC 156e536a7b9700018aaa2437072c14e3376340a7
2 files changed +21 -1
linux-user/elfload.c
+20 -1
@@ -699,7 +699,7 @@ static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
699 /* There must be exactly DLINFO_ITEMS entries here, or the assert
700 * on info->auxv_len will trigger.
701 */
702 - NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
702 + NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->phdr_addr));
703 NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
704 NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
705 NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
@@ -1469,6 +1469,12 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
1469 info->data_offset = load_bias;
1470 info->load_addr = load_addr;
1471 info->entry = ehdr->e_entry + load_bias;
1472 + /*
1473 + * Fallback for AT_PHDR if the program headers do not fall within
1474 + * any PT_LOAD segment (see the loop below, which overrides this with
1475 + * the correct in-memory address when a containing segment is found).
1476 + */
1477 + info->phdr_addr = load_addr + ehdr->e_phoff;
1478 info->start_code = -1;
1479 info->end_code = 0;
1480 info->start_data = -1;
@@ -1523,6 +1529,19 @@ static void load_elf_image(const char *image_name, const ImageSource *src,
1529 vaddr_ef = vaddr + eppnt->p_filesz;
1530 vaddr_em = vaddr + eppnt->p_memsz;
1531
1532 + /*
1533 + * If this segment contains the program headers, record their
1534 + * in-memory address for AT_PHDR. This matches the kernel, which
1535 + * locates the headers via the containing PT_LOAD rather than
1536 + * assuming load_addr + e_phoff (false when the phdrs are not
1537 + * mapped 1:1 from file offset 0, e.g. relocated into their own
1538 + * segment by a binary patcher).
1539 + */
1540 + if (eppnt->p_offset <= ehdr->e_phoff &&
1541 + ehdr->e_phoff < eppnt->p_offset + eppnt->p_filesz) {
1542 + info->phdr_addr = vaddr + (ehdr->e_phoff - eppnt->p_offset);
1543 + }
1544 +
1545 /*
1546 * Some segments may be completely empty, with a non-zero p_memsz
1547 * but no backing file segment.
linux-user/qemu.h
+1
@@ -26,6 +26,7 @@
26 struct image_info {
27 abi_ulong load_bias;
28 abi_ulong load_addr;
29 + abi_ulong phdr_addr;
30 abi_ulong start_code;
31 abi_ulong end_code;
32 abi_ulong start_data;