From nobody Sun Dec 22 10:17:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail(p=quarantine dis=none) header.from=users.sourceforge.net Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1528375789432979.2172144855808; Thu, 7 Jun 2018 05:49:49 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 932A52097213A; Thu, 7 Jun 2018 05:49:48 -0700 (PDT) Received: from sonic306-20.consmr.mail.ne1.yahoo.com (sonic306-20.consmr.mail.ne1.yahoo.com [66.163.189.82]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 8763E2119705D for ; Thu, 7 Jun 2018 05:49:47 -0700 (PDT) Received: from sonic.gate.mail.ne1.yahoo.com by sonic306.consmr.mail.ne1.yahoo.com with HTTP; Thu, 7 Jun 2018 12:49:44 +0000 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=66.163.189.82; helo=sonic306-20.consmr.mail.ne1.yahoo.com; envelope-from=zenith432@users.sourceforge.net; receiver=edk2-devel@lists.01.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1528375784; bh=KFFKDRrLJoda1bCGV0Bi8U8X+za5F1NBmBOJSAMrMug=; h=Date:From:Reply-To:To:Cc:Subject:References:From:Subject; b=TyzLIa0EOn9KVS45wolQDXhCdWdvq5dU2mZl0peS66jv2ZcOKj92UAWebS/Qzzp21Zg5injw1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wrkZJ9VK..DkY9I_aHKl0iHMNnLAGJN 99pc72G9osJgomkfafzqSJ01Pl2Hvzru7pYXpvaRafexoNwI1VcLZegE7s_oANmpKql08A01O3Zt tvFVVSfbKcZsaF6agaYTYv8_Cc7AtAC3Kgi1T_azcxtJehoaFBPbJ5USIphk7TXKL.NJmYNY_2Q- - Date: Thu, 7 Jun 2018 12:49:41 +0000 (UTC) From: Zenith432 To: "edk2-devel@lists.01.org" Message-ID: <1917777633.1686290.1528375781778@mail.yahoo.com> MIME-Version: 1.0 References: <1917777633.1686290.1528375781778.ref@mail.yahoo.com> X-Mailer: WebService/1.1.11950 YahooMailBasic Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0 Subject: [edk2] [PATCH v2] BaseTools/GenFw: Add X64 GOTPCREL Support to GenFw X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Zenith432 Cc: LimingGao Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Adds support for the following X64 ELF relocations to GenFw R_X86_64_GOTPCREL R_X86_64_GOTPCRELX R_X86_64_REX_GOTPCRELX CC: Shi Steven CC: Yonghong Zhu CC: Liming Gao Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Zenith432 --- BaseTools/Source/C/GenFw/Elf64Convert.c | 166 +++++++++++++++++++++++- BaseTools/Source/C/GenFw/elf_common.h | 23 ++++ 2 files changed, 188 insertions(+), 1 deletion(-) diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C/G= enFw/Elf64Convert.c index c39bdff0..1518c395 100644 --- a/BaseTools/Source/C/GenFw/Elf64Convert.c +++ b/BaseTools/Source/C/GenFw/Elf64Convert.c @@ -94,6 +94,15 @@ STATIC Elf_Ehdr *mEhdr; STATIC Elf_Shdr *mShdrBase; STATIC Elf_Phdr *mPhdrBase; =20 +// +// GOT information +// +STATIC Elf_Shdr *mGOTShdr =3D NULL; +STATIC UINT32 mGOTShindex =3D 0; +STATIC UINT32 *mGOTCoffEntries =3D NULL; +STATIC UINT32 mGOTMaxCoffEntries =3D 0; +STATIC UINT32 mGOTNumCoffEntries =3D 0; + // // Coff information // @@ -322,6 +331,117 @@ GetSymName ( return StrtabContents + Sym->st_name; } =20 +// +// Find the ELF section hosting the GOT from an ELF Rva +// of a single GOT entry. Normally, GOT is placed in +// ELF .text section, so assume once we find in which +// section the GOT is, all GOT entries are there, and +// just verify this. +// +STATIC +VOID +FindElfGOTSectionFromGOTEntryElfRva ( + Elf64_Addr GOTEntryElfRva + ) +{ + UINT32 i; + if (mGOTShdr !=3D NULL) { + if (GOTEntryElfRva >=3D mGOTShdr->sh_addr && + GOTEntryElfRva < mGOTShdr->sh_addr + mGOTShdr->sh_size) + return; + Error (NULL, 0, 3000, "Unsupported", "FindElfGOTSectionFromGOTEntryElf= Rva: GOT entries found in multiple sections."); + exit(EXIT_FAILURE); + } + for (i =3D 0; i < mEhdr->e_shnum; i++) { + Elf_Shdr *shdr =3D GetShdrByIndex(i); + if (GOTEntryElfRva >=3D shdr->sh_addr && + GOTEntryElfRva < shdr->sh_addr + shdr->sh_size) { + mGOTShdr =3D shdr; + mGOTShindex =3D i; + return; + } + } + Error (NULL, 0, 3000, "Invalid", "FindElfGOTSectionFromGOTEntryElfRva: E= lfRva 0x%016LX for GOT entry not found in any section.", GOTEntryElfRva); + exit(EXIT_FAILURE); +} + +// +// Stores locations of GOT entries in COFF image. +// Returns TRUE if GOT entry is new. +// Simple implementation as number of GOT +// entries is expected to be low. +// + +STATIC +BOOLEAN +AccumulateCoffGOTEntries ( + UINT32 GOTCoffEntry + ) +{ + UINT32 i; + if (mGOTCoffEntries !=3D NULL) { + for (i =3D 0; i < mGOTNumCoffEntries; i++) + if (mGOTCoffEntries[i] =3D=3D GOTCoffEntry) + return FALSE; + } + if (mGOTCoffEntries =3D=3D NULL) { + mGOTCoffEntries =3D (UINT32*)malloc(5 * sizeof *mGOTCoffEntries); + if (mGOTCoffEntries =3D=3D NULL) { + Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!"); + } + assert (mGOTCoffEntries !=3D NULL); + mGOTMaxCoffEntries =3D 5; + mGOTNumCoffEntries =3D 0; + } else if (mGOTNumCoffEntries =3D=3D mGOTMaxCoffEntries) { + mGOTCoffEntries =3D (UINT32*)realloc(mGOTCoffEntries, 2 * mGOTMaxCoffE= ntries * sizeof *mGOTCoffEntries); + if (mGOTCoffEntries =3D=3D NULL) { + Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!"); + } + assert (mGOTCoffEntries !=3D NULL); + mGOTMaxCoffEntries +=3D mGOTMaxCoffEntries; + } + mGOTCoffEntries[mGOTNumCoffEntries++] =3D GOTCoffEntry; + return TRUE; +} + +STATIC +int +__comparator ( + const void* lhs, + const void* rhs + ) +{ + if (*(const UINT32*)lhs < *(const UINT32*)rhs) + return -1; + return *(const UINT32*)lhs > *(const UINT32*)rhs; +} + +STATIC +VOID +EmitGOTRelocations ( + VOID + ) +{ + UINT32 i; + if (mGOTCoffEntries =3D=3D NULL) + return; + qsort( + mGOTCoffEntries, + mGOTNumCoffEntries, + sizeof *mGOTCoffEntries, + __comparator); + for (i =3D 0; i < mGOTNumCoffEntries; i++) { + VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X", mGOTCoffEntrie= s[i]); + CoffAddFixup( + mGOTCoffEntries[i], + EFI_IMAGE_REL_BASED_DIR64); + } + free(mGOTCoffEntries); + mGOTCoffEntries =3D NULL; + mGOTMaxCoffEntries =3D 0; + mGOTNumCoffEntries =3D 0; +} + // // Elf functions interface implementation // @@ -698,7 +818,7 @@ WriteSections64 ( // section that applies to the entire binary, and which will have its = section // index set to #0 (which is a NULL section with the SHF_ALLOC bit cle= ared). // - // In the absence of GOT based relocations (which we currently don't s= upport), + // In the absence of GOT based relocations, // this RELA section will contain redundant R_xxx_RELATIVE relocations= , one // for every R_xxx_xx64 relocation appearing in the per-section RELA s= ections. // (i.e., .rela.text and .rela.data) @@ -780,6 +900,7 @@ WriteSections64 ( // Determine how to handle each relocation type based on the machi= ne type. // if (mEhdr->e_machine =3D=3D EM_X86_64) { + Elf64_Addr GOTEntryRva; switch (ELF_R_TYPE(Rel->r_info)) { case R_X86_64_NONE: break; @@ -834,6 +955,32 @@ WriteSections64 ( - (SecOffset - SecShdr->sh_addr)); VerboseMsg ("Relocation: 0x%08X", *(UINT32 *)Targ); break; + case R_X86_64_GOTPCREL: + case R_X86_64_GOTPCRELX: + case R_X86_64_REX_GOTPCRELX: + VerboseMsg ("R_X86_64_GOTPCREL family"); + VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X", + (UINT32)(SecOffset + (Rel->r_offset - SecShdr->sh_addr)), + *(UINT32 *)Targ); + GOTEntryRva =3D Rel->r_offset - Rel->r_addend + *(INT32 *)Targ; + FindElfGOTSectionFromGOTEntryElfRva(GOTEntryRva); + *(UINT32 *)Targ =3D (UINT32) (*(UINT32 *)Targ + + (mCoffSectionsOffset[mGOTShindex] - mGOTShdr->sh_addr) + - (SecOffset - SecShdr->sh_addr)); + VerboseMsg ("Relocation: 0x%08X", *(UINT32 *)Targ); + GOTEntryRva +=3D (mCoffSectionsOffset[mGOTShindex] - mGOTShdr-= >sh_addr); // ELF Rva -> COFF Rva + if (AccumulateCoffGOTEntries((UINT32)GOTEntryRva)) { + // + // Relocate GOT entry if it's the first time we run into it + // + Targ =3D mCoffFile + GOTEntryRva; + VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX", + (UINT32)GOTEntryRva, + *(UINT64 *)Targ); + *(UINT64 *)Targ =3D *(UINT64 *)Targ - SymShdr->sh_addr + mCo= ffSectionsOffset[Sym->st_shndx]; + VerboseMsg ("Relocation: 0x%016LX", *(UINT64*)Targ); + } + break; default: Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64= relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info)); } @@ -972,6 +1119,9 @@ WriteRelocations64 ( case R_X86_64_NONE: case R_X86_64_PC32: case R_X86_64_PLT32: + case R_X86_64_GOTPCREL: + case R_X86_64_GOTPCRELX: + case R_X86_64_REX_GOTPCRELX: break; case R_X86_64_64: VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",=20 @@ -1040,10 +1190,24 @@ WriteRelocations64 ( Error (NULL, 0, 3000, "Not Supported", "This tool does not sup= port relocations for ELF with e_machine %u (processor type).", (unsigned) m= Ehdr->e_machine); } } + if (mEhdr->e_machine =3D=3D EM_X86_64 && RelShdr->sh_info =3D=3D m= GOTShindex) { + // + // Tack relocations for GOT entries after other relocations for + // the section the GOT is in, as it's usually found at the end + // of the section. + // + EmitGOTRelocations(); + } } } } =20 + if (mEhdr->e_machine =3D=3D EM_X86_64) { + // + // Just in case GOT is in a section with no other relocations + // + EmitGOTRelocations(); + } // // Pad by adding empty entries. // diff --git a/BaseTools/Source/C/GenFw/elf_common.h b/BaseTools/Source/C/Gen= Fw/elf_common.h index 766d0e42..50b4e1f2 100644 --- a/BaseTools/Source/C/GenFw/elf_common.h +++ b/BaseTools/Source/C/GenFw/elf_common.h @@ -544,6 +544,12 @@ typedef struct { #define R_386_TLS_DTPMOD32 35 /* GOT entry containing TLS index */ #define R_386_TLS_DTPOFF32 36 /* GOT entry containing TLS offset */ #define R_386_TLS_TPOFF32 37 /* GOT entry of -ve static TLS offset */ +#define R_386_SIZE32 38 /* 32-bit symbol size */ +#define R_386_TLS_GOTDESC 39 /* GOT offset for TLS descriptor. */ +#define R_386_TLS_DESC_CALL 40 /* Marker of call through TLS descriptor fo= r relaxation. */ +#define R_386_TLS_DESC 41 /* TLS descriptor containing pointer to code an= d to argument, returning the TLS offset for the symbol. */ +#define R_386_IRELATIVE 42 /* Adjust indirectly by program base */ +#define R_386_GOT32X 43 /* Load from 32 bit GOT entry, relaxable. */ =20 /* Null relocation */ #define R_AARCH64_NONE 256 /* No relocation */ @@ -1052,6 +1058,23 @@ typedef struct { #define R_X86_64_DTPOFF32 21 /* Offset in TLS block */ #define R_X86_64_GOTTPOFF 22 /* PC relative offset to IE GOT entry */ #define R_X86_64_TPOFF32 23 /* Offset in static TLS block */ +#define R_X86_64_PC64 24 /* PC relative 64 bit */ +#define R_X86_64_GOTOFF64 25 /* 64 bit offset to GOT */ +#define R_X86_64_GOTPC32 26 /* 32 bit signed pc relative offset to GOT */ +#define R_X86_64_GOT64 27 /* 64-bit GOT entry offset */ +#define R_X86_64_GOTPCREL64 28 /* 64-bit PC relative offset to GOT entry */ +#define R_X86_64_GOTPC64 29 /* 64-bit PC relative offset to GOT */ +#define R_X86_64_GOTPLT64 30 /* like GOT64, says PLT entry needed */ +#define R_X86_64_PLTOFF64 31 /* 64-bit GOT relative offset to PLT entry */ +#define R_X86_64_SIZE32 32 /* Size of symbol plus 32-bit addend */ +#define R_X86_64_SIZE64 33 /* Size of symbol plus 64-bit addend */ +#define R_X86_64_GOTPC32_TLSDESC 34 /* GOT offset for TLS descriptor. */ +#define R_X86_64_TLSDESC_CALL 35 /* Marker for call through TLS descriptor= . */ +#define R_X86_64_TLSDESC 36 /* TLS descriptor. */ +#define R_X86_64_IRELATIVE 37 /* Adjust indirectly by program base */ +#define R_X86_64_RELATIVE64 38 /* 64-bit adjust by program base */ +#define R_X86_64_GOTPCRELX 41 /* Load from 32 bit signed pc relative offse= t to GOT entry without REX prefix, relaxable. */ +#define R_X86_64_REX_GOTPCRELX 42 /* Load from 32 bit signed pc relative o= ffset to GOT entry with REX prefix, relaxable. */ =20 =20 #endif /* !_SYS_ELF_COMMON_H_ */ --=20 2.17.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel