From nobody Sun May 19 00:17:26 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+92596+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+92596+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1661091425; cv=none; d=zohomail.com; s=zohoarc; b=KGejAxR0eYaExlt8mlr9EKMZ93sZZ99xL9EPSPciPGiLYjAzrEjy/+Hrxiks9Bc5o1rUAHz5Te1t7yOlEewYpY1kaciuYjWoB6etzsNS4COmzqs3wE6UfNdaMd4dIB/fewOjJnWnjKQfkxuTteq6eiINdJ7x1IQUbMt9ZoEc7as= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1661091425; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=fSB5y8RHp4SOyXdsPLr6zrtOfNamozYvSCSkFjqZ29Y=; b=XE5s9gpRMcSnoX8R3j+T3XgRC2HEXXQBNdSYDsAs6wcDMpU/sXCGeHFUd0wgqJX/DhOFspZyl9dIQfsw/1/IC9Qac2S+DfRW3eroEW5oHUbYDm30HgGmoPbBGonEAj2kqUyxtFP3Ask3NeuBu0oZygNQzktsqbXUVvGjBzvJVUk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+92596+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1661091425225648.653789676779; Sun, 21 Aug 2022 07:17:05 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id hVctYY1788612xs5nJFXHjVp; Sun, 21 Aug 2022 07:17:03 -0700 X-Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web12.7104.1661091422999018997 for ; Sun, 21 Aug 2022 07:17:03 -0700 X-Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 326A060A67; Sun, 21 Aug 2022 14:17:02 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE598C433D6; Sun, 21 Aug 2022 14:16:59 +0000 (UTC) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Rebecca Cran , Bob Feng , Liming Gao , "Kinney, Michael D" , Leif Lindholm Subject: [edk2-devel] [PATCH] BaseTools/GenFw AARCH64: Convert more types of explicit GOT references Date: Sun, 21 Aug 2022 16:16:37 +0200 Message-Id: <20220821141637.2531871-1-ardb@kernel.org> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ardb@kernel.org X-Gm-Message-State: 9cNKDbXXgdqnaHbzxJvDYjMZx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1661091423; bh=2ObIzpMJppq11SM5jCPWzAxBlVrpWyX3ZokarO61mAQ=; h=Cc:Date:From:Reply-To:Subject:To; b=KqJd89cvKCowkpoU0zyxI0Y3aMolTcI1+d1pTaeZt60qySxwXbUD34psw5RjYuLBua3 rGS6tuvb2FWkjFilVrhwkLEMqtDXTfWqqHy37MKSYb8N5+wp78nV7o+37HJnNZr3GMDJ3 Gfnk/AnaRoJBzTX3dOU3BM2PidE5bWpu8hI= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1661091427241100003 Content-Type: text/plain; charset="utf-8" Rebecca reports that builds of AArch64 DSCs that involve PIE linking when using ELF based toolchains are failing in some cases, resulting in an error message like bad definition for symbol '_GLOBAL_OFFSET_TABLE_'@0x72d8 or unsupported symbol type. For example, absolute and undefined symbols are not supported. The reason turns out to be that, while GenFw does carry some logic to convert GOT based symbol references into direct ones (which is always possible given that our ELF to PE/COFF conversion only supports fully linked executables), it does not support all possible combinations of relocations that the linker may emit to load symbol addresses from the GOT. In particular, when performing a non-LTO link on object code built with GCC using -fpie, we may end up with GOT based references such as the one below, where the address of the GOT itself is taken, and the ofset of the symbol in the GOT is reflected in the immediate offset of the subsequent LDR instruction. 838: adrp x0, 16000 838: R_AARCH64_ADR_PREL_PG_HI21 _GLOBAL_OFFSET_TABLE_ 83c: ldr x0, [x0, #2536] 83c: R_AARCH64_LD64_GOTPAGE_LO15 _gPcd_BinaryPatch_PcdFdBaseAddress The reason that we omit GOT based symbol references when performing ELF to PE/COFF conversion is that the GOT is not described by static ELF relocations, which means that the ELF file lacks the metadata to generate the PE/COFF relocations covering the GOT table in the PE/COFF executable. Given that none of the usual motivations for using a GOT (copy on write footprint, shared libraries) apply to EFI executables in the first place, the easiest way around this is to convert all GOT based symbol address loads to PC relative ADR/ADRP instructions. So implement this handling for R_AARCH64_LD64_GOTPAGE_LO15 and R_AARCH64_LD64_GOTOFF_LO15 relocations as well, and turn the LDR instructions in question into ADR instructions that generate the address immediately. This leaves the reference to _GLOBAL_OFFSET_TABLE_ itself, which is what generated the error to begin with. Considering that this symbol is never referenced (i.e., it doesn't appear anywhere in the code) and is only meaningful in combination with R_*_GOT_* based relocations that follow it, we can just disregard any references to it entirely, given that we convert all of those followup relocations into direct references. Cc: Rebecca Cran Cc: Bob Feng Cc: Liming Gao Cc: "Kinney, Michael D" Cc: Leif Lindholm Signed-off-by: Ard Biesheuvel Acked-by: Bob Feng Acked-by: Leif Lindholm Tested-by: Rebecca Cran --- This patch can be tested using the following method: - add the following lines to ArmVirtPkg/PrePi/ArmVirtPrePiUniCoreRelocatabl= e.inf [BuildOptions] GCC:*_*_AARCH64_CC_FLAGS =3D -fpie GCC:*_*_AARCH64_DLINK_FLAGS =3D -Wl,-Bsymbolic,-pie - build ArmVirtPkg/ArmVirtQemuKernel.dsc in DEBUG mode using GCC49 (other combos might work as well) and observe the build failure - apply this patch and observe that the build failure is gone - boot the resulting image in QEMU using the -kernel ../QEMU_EFI.fd command line option and observe that the image boots as usual BaseTools/Source/C/GenFw/Elf64Convert.c | 35 ++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C/G= enFw/Elf64Convert.c index 35e96dd05bc2..3173ca9280f4 100644 --- a/BaseTools/Source/C/GenFw/Elf64Convert.c +++ b/BaseTools/Source/C/GenFw/Elf64Convert.c @@ -1305,6 +1305,22 @@ WriteSections64 ( Elf_Shdr *SymShdr; UINT8 *Targ; =20 + // + // The _GLOBAL_OFFSET_TABLE_ symbol is not actually an absolute sy= mbol, + // but carries the SHN_ABS section index for historical reasons. + // It must be accompanied by a R_*_GOT_* type relocation on a + // subsequent instruction, which we handle below, specifically to = avoid + // the GOT indirection, and to refer to the symbol directly. This = means + // we can simply disregard direct references to the GOT symbol its= elf, + // as the resulting value will never be used. + // + if (Sym->st_shndx =3D=3D SHN_ABS) { + const UINT8 *SymName =3D GetSymName (Sym); + if (strcmp ((CHAR8 *)SymName, "_GLOBAL_OFFSET_TABLE_") =3D=3D 0)= { + continue; + } + } + // // Check section header index found in symbol table and get the se= ction // header location. @@ -1448,6 +1464,23 @@ WriteSections64 ( switch (ELF_R_TYPE(Rel->r_info)) { INT64 Offset; =20 + case R_AARCH64_LD64_GOTOFF_LO15: + case R_AARCH64_LD64_GOTPAGE_LO15: + // + // Convert into an ADR instruction that refers to the symbol d= irectly. + // + Offset =3D Sym->st_value - Rel->r_offset; + + *(UINT32 *)Targ &=3D 0x1000001f; + *(UINT32 *)Targ |=3D ((Offset & 0x1ffffc) << (5 - 2)) | ((Offs= et & 0x3) << 29); + + if (Offset < -0x100000 || Offset > 0xfffff) { + Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s fail= ed to relax GOT based symbol reference - image is too big (>1 MiB).", + mInImageName); + break; + } + break; + case R_AARCH64_LD64_GOT_LO12_NC: // // Convert into an ADD instruction - see R_AARCH64_ADR_GOT_PAG= E below. @@ -1686,6 +1719,8 @@ WriteRelocations64 ( case R_AARCH64_LDST128_ABS_LO12_NC: case R_AARCH64_ADR_GOT_PAGE: case R_AARCH64_LD64_GOT_LO12_NC: + case R_AARCH64_LD64_GOTOFF_LO15: + case R_AARCH64_LD64_GOTPAGE_LO15: // // No fixups are required for relative relocations, provided= that // the relative offsets between sections have been preserved= in --=20 2.35.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#92596): https://edk2.groups.io/g/devel/message/92596 Mute This Topic: https://groups.io/mt/93162057/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-