From nobody Sun Jul 26 13:27:02 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1781366170; cv=none; d=zohomail.com; s=zohoarc; b=MeAbOvMCktFPmc+aTlFHwrvHlua4dMI4kq+eWYRBajGqlqaiW0a+GCUai8jIz6ERrFIZaCWELeJ+1LEuv68rveA2EP2315QTBFBofEbccklNNS9lzDYuy3H0pVCiCVxQcX4qzOpIloTmgnAFIKQ5xjb35PNi3Gw1fXPadF/gmSI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1781366170; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=mMXTU2ZXIuCA5mgFvk7PVjZZ7pXKY2stODnzkrc6ruc=; b=L3cynC1J6z0SOsxUPKyv1HIvkQof5Ule9+MJRT30H7XQusdMGWiP2UjIqx+CD2WzY6SqfILvBS1WeK2dUNPj/15TOQ3nxOljMNhG+DqWups9E3Livkg6B1wufCXAMVlieP1OGFzccSPDDFl3GxHLaXSWbyKBA/UC+evd6S8ngKA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1781366170774414.2475507938291; Sat, 13 Jun 2026 08:56:10 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wYQhs-0008Vx-5K; Sat, 13 Jun 2026 11:55:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wYQCK-0007SL-Li for qemu-devel@nongnu.org; Sat, 13 Jun 2026 11:23:00 -0400 Received: from mail-pj1-x1043.google.com ([2607:f8b0:4864:20::1043]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1wYQCI-0002fz-VO for qemu-devel@nongnu.org; Sat, 13 Jun 2026 11:23:00 -0400 Received: by mail-pj1-x1043.google.com with SMTP id 98e67ed59e1d1-36b7b7b7a80so1383917a91.1 for ; Sat, 13 Jun 2026 08:22:58 -0700 (PDT) Received: from DESKTOP-O31H9PS.localdomain ([157.49.119.134]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2c433078264sm52643775ad.65.2026.06.13.08.22.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 Jun 2026 08:22:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781364177; x=1781968977; darn=nongnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=mMXTU2ZXIuCA5mgFvk7PVjZZ7pXKY2stODnzkrc6ruc=; b=AbWHVOsT9gFE75utbZQk0o57Ydqjz20ORDGoO6bxaXfeCTeHy1bz/Zn/JdAT/6xBG3 Jl1ym02keI9F4IcT1e2/JUoFe5lreQfgR1KQuZJcI6vB+m+7DwATrPbRqBNiDDqQVn0i pakMv9L5jY09+840OxfQxDO4P67wMuYBBaZHUWAmytVIzDu0GA3G4i1/Y/k17kGtzoB/ 7xXBsWwkHIFvFPWCZj8Sj+ZvH7bM+Ge/CoI8/Xqx9X5se4MJUdFl9YqvE3j3RSOPGlE0 ZRFIgtsKxNU4P9bQqCk+87lfZT3jbLCwGAKE0nNg8qnRbN5RoP8FfW/Orsc9Iw/zpBIo BP2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781364177; x=1781968977; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=mMXTU2ZXIuCA5mgFvk7PVjZZ7pXKY2stODnzkrc6ruc=; b=qFfWlpMzgl8+06daNBPnsEffxrWFxPgNPFCqzewpO8f46poPUKMjBUZ2Lb6e2TRCdQ 47UzOMhzzFZDyPfSEUrdBbNGwAurFXoDtQYGS1J1l2dPBhT6QoDORr5z1Fo5DsoRyjyn x1L3qlYMP02SIX4C6CDrj7ulY/2yU2Fv4D8Ii5Y2AU9fMhI93LiPq6WVAq2ZqdvPtpRc 8JReKSfaalwVGJFOzEx6IV+tdd5vMbOROm4XU+A77Cm5fEEcHEtwus6WYE1zoOqtxBDg OAVxj8z/hCVDUjqVU3cBcqaYQ3XxpbiBjaRPF67h8IGaVE4PdXFIMv3NZcPeI3+m/NiS xAaQ== X-Gm-Message-State: AOJu0YxvOl+4x4uiFnUOrq5X97wtK6pRMK59af7qL3rWX6ExE6CIUhfM I/I8STo2F/N2euP2LeAwmJapQlq7K13W0IjlijF669RNj+Nwahrf3w8xUy1Wq/if/ZE= X-Gm-Gg: Acq92OEONl5oVUJ60ZuUGXejEgmELMpSfXjH/hBGSecqLw26vLbNXGiP1qi42Sw5R52 IdvSBtPt+h8gXNU6ELc2vL1QyvPcuKLNKrFZrzxaMdjLB0jqdaZaDWfKkP//jGIszZS5vlcgZGV cjnJly7hnw96qqR4qjDHMgxuyJoOBjh8fw1LIXkOzeCdAyW72P3AhL/V5E4nB5BHUyPRFJwpOTs Twc2IFE4a0UlrBFrZ8eFv1/vBqJv8nskkgvSc+JHlX/x32ItJSTGEvnbPLAjpiVaQrx9780JQVp rvEG/Z1I0CTeoRqQKnpc5r5jSFTZb3WURbRp172uLohk203Fhi0i7z9JPk6r7ExeA7FzhvFSXZN T1TMLOTXF79jxEBUdP1ppiFeKcSP0R3O+hRzwg/W/cmQjMacmQY+FSV+Q8YOIx+OHsKvSNiW+xL aQXxlXF0tQEiVRvcF9OHlVNx5449kP8jRqPJ6wpLZos5zlRDiC4w== X-Received: by 2002:a17:903:944:b0:2c0:af09:f3d4 with SMTP id d9443c01a7336-2c3fd9ebc4cmr64975115ad.27.1781364176814; Sat, 13 Jun 2026 08:22:56 -0700 (PDT) From: valium007 To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, deller@gmx.de, pierrick.bouvier@oss.qualcomm.com, valium007 Subject: [PATCH] linux-user: Fix AT_PHDR when program headers are relocated into their own segment Date: Sat, 13 Jun 2026 20:51:56 +0530 Message-ID: <20260613152156.41147-1-valium7171@gmail.com> X-Mailer: git-send-email 2.54.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1043; envelope-from=valium7171@gmail.com; helo=mail-pj1-x1043.google.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Sat, 13 Jun 2026 11:55:34 -0400 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1781366174299158500 Content-Type: text/plain; charset="utf-8" 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 head= ers are mapped 1:1 from file offset 0. This breaks when the headers are elsewhe= re. 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_L= OAD). 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: valium007 --- linux-user/elfload.c | 21 ++++++++++++++++++++- linux-user/qemu.h | 1 + 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index b05b8b0..8049c8a 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -699,7 +699,7 @@ static abi_ulong create_elf_tables(abi_ulong p, int arg= c, int envc, /* There must be exactly DLINFO_ITEMS entries here, or the assert * on info->auxv_len will trigger. */ - NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff)); + NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->phdr_addr)); NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr))); NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum)); NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE)); @@ -1469,6 +1469,12 @@ static void load_elf_image(const char *image_name, c= onst ImageSource *src, info->data_offset =3D load_bias; info->load_addr =3D load_addr; info->entry =3D ehdr->e_entry + load_bias; + /* + * Fallback for AT_PHDR if the program headers do not fall within + * any PT_LOAD segment (see the loop below, which overrides this with + * the correct in-memory address when a containing segment is found). + */ + info->phdr_addr =3D load_addr + ehdr->e_phoff; info->start_code =3D -1; info->end_code =3D 0; info->start_data =3D -1; @@ -1523,6 +1529,19 @@ static void load_elf_image(const char *image_name, c= onst ImageSource *src, vaddr_ef =3D vaddr + eppnt->p_filesz; vaddr_em =3D vaddr + eppnt->p_memsz; =20 + /* + * If this segment contains the program headers, record their + * in-memory address for AT_PHDR. This matches the kernel, whi= ch + * locates the headers via the containing PT_LOAD rather than + * assuming load_addr + e_phoff (false when the phdrs are not + * mapped 1:1 from file offset 0, e.g. relocated into their own + * segment by a binary patcher). + */ + if (eppnt->p_offset <=3D ehdr->e_phoff && + ehdr->e_phoff < eppnt->p_offset + eppnt->p_filesz) { + info->phdr_addr =3D vaddr + (ehdr->e_phoff - eppnt->p_offs= et); + } + /* * Some segments may be completely empty, with a non-zero p_me= msz * but no backing file segment. diff --git a/linux-user/qemu.h b/linux-user/qemu.h index 07fe801..2268493 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -26,6 +26,7 @@ struct image_info { abi_ulong load_bias; abi_ulong load_addr; + abi_ulong phdr_addr; abi_ulong start_code; abi_ulong end_code; abi_ulong start_data; --=20 2.54.0