From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=pass header.i=@linaro.org header.s=google header.b=xh9cOvmV; spf=pass (domain: linaro.org, ip: 209.85.128.67, mailfrom: leif.lindholm@linaro.org) Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by groups.io with SMTP; Wed, 04 Sep 2019 08:38:00 -0700 Received: by mail-wm1-f67.google.com with SMTP id r195so4352928wme.2 for ; Wed, 04 Sep 2019 08:37:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=eZSz9b57NY1CMTWVlvyHhJQBsEUJpMMav1p/NRnWUwA=; b=xh9cOvmVcaa+WbOIdhwZGEys42O3d88PRCmIhGeUiCKjAqpMXh90oUv6ABsNNZXoXF 8jkh4LPttut3TrZNSGawbk5TI6v+khc1s/3VDK+AhwnvNf719fxLB4GxfUO8TU/nnf3Q 92qlNu1dGBlJJtHGdpUYiCLXjxVF7DxS4A5Gc9uDNbRWzzWc1X7cm6BPSgBv0YGxL7O3 OCWBtH8qrqCM9xlzGImvQF/kQ624SrRVZdCTPFC5D8hgJBMNURKhh2jTTfC807KoIpa0 K52wwrQParPDU8v0rSrprsyy+dy88gvYLKftnFQU4MBrcUNTsLXWoFY8ZNihm40dGjbS FMxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=eZSz9b57NY1CMTWVlvyHhJQBsEUJpMMav1p/NRnWUwA=; b=OsVQw7e2yHPQAXVH5Hd+Y9kvpoCsN8jtLYUDne1CLrPO/X/ra/dsi801TMiMYBjnJ6 47HmxWJV3Jk03Qu5uz+7DtB+u14sZR3mSntqghU7oa/IRnXnGsavFu8muqb22zp4CYjL qTnrE1OmVXptasAE6vZtTgyV283CG101G3eFOQ8UKB8NLmNssUGnzUmlIkO6t8ze/Z+x ClomWPZUoMTteQT8/J6All5Th1Iz+lfr+KlXun/XnjgZIYBa/SZhU1lbh48VpOeMXCRY OufeE2yZ9ao9Fk66gBujLTOH9qFggPa5rH4JxQJb3JkMic5EbZ6TkyutNSeRVqtLpma+ IIHQ== X-Gm-Message-State: APjAAAWMlM+kNbCNR9s7mfm798QQGzaA91QEGFERO7RAUE2XJSLZDe44 vHIDgcWgxOqVAzwamOiLeoTT/A== X-Google-Smtp-Source: APXvYqyVCWLrg+QfWqLy/yLQLd5vcCdTM7lPwQJvLglC17ZaUB7KlSCgP7YyHPVoYEsfoNpRW62Ukg== X-Received: by 2002:a1c:ed05:: with SMTP id l5mr4903209wmh.21.1567611478458; Wed, 04 Sep 2019 08:37:58 -0700 (PDT) Return-Path: Received: from bivouac.eciton.net (bivouac.eciton.net. [2a00:1098:0:86:1000:23:0:2]) by smtp.gmail.com with ESMTPSA id v8sm43099470wra.79.2019.09.04.08.37.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Sep 2019 08:37:57 -0700 (PDT) Date: Wed, 4 Sep 2019 16:37:56 +0100 From: "Leif Lindholm" To: Ard Biesheuvel Cc: edk2-devel-groups-io , "Gao, Liming" Subject: Re: [PATCH] BaseTools/GenFw AARCH64: fix up GOT based relative relocations Message-ID: <20190904153756.GM29255@bivouac.eciton.net> References: <20190904041733.12741-1-ard.biesheuvel@linaro.org> <20190904114933.GG29255@bivouac.eciton.net> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Sep 04, 2019 at 05:01:58AM -0700, Ard Biesheuvel wrote: > On Wed, 4 Sep 2019 at 04:49, Leif Lindholm wrote: > > > > On Tue, Sep 03, 2019 at 09:17:33PM -0700, Ard Biesheuvel wrote: > > > We take great care to avoid GOT based relocations in EDK2 executables, > > > primarily because they are pointless - we don't care about things like > > > the CoW footprint or relocations that target read-only sections, and so > > > GOT entries only bloat the binary. > > > > > > However, in some cases (e.g., when building the relocatable PrePi SEC > > > module in ArmVirtPkg with the CLANG38 toolchain), we may end up with > > > some GOT based relocations nonetheless, which break the build since > > > GenFw does not know how to deal with them. > > > > > > The relocations emitted in this case are ADRP/LDR instruction pairs > > > that are annotated as GOT based, which means that it is the linker's > > > job to emit the GOT entry and tag it with an appropriate dynamic > > > relocation that ensures that the correct absolute value is stored into > > > the GOT entry when the executable is loaded. This dynamic relocation > > > not visible to GenFw, and so populating the PE/COFF relocation section > > > for these entries is non-trivial. > > > > > > Since each ADRP/LDR pair refers to a single symbol that is local to the > > > binary (given that shared libraries are not supported), we can actually > > > convert the ADRP/LDR pair into an ADRP/ADD pair that produces the symbol > > > address directly rather than loading it from memory. This leaves the > > > GOT entry in the binary, but since it is now unused, it is no longer > > > necessary to emit a PE/COFF relocation entry for it. > > > > > > Signed-off-by: Ard Biesheuvel > > > > This is a very neat fix. My only concern is that I am not able to > > reproduce the issue on my local Buster with clang7 (default). Is it > > reproducible with clang8? > > > > I managed to reproduce it on Ubuntu Bionic with clang 6. It may also > be related to the version of ld.gold or the LLVM gold plugin. > > You should be able to test this patch for correctness by stripping the > no-pie/no-pic options from the GCC5 command line, and checking any > produced .dll with readelf -r to see whether any GOT based relocations > were emitted, and whether the resulting binary still runs. I will do > the same locally. By removing the -no-pic/-no-pie flags, I get the GCC5 profile to display the issue. And by adding this patch, the build issue goes away. The image remains bootable with -kernel. I won't claim to have double checked the arithmetic/encodings, but apart from that: Reviewed-by: Leif Lindholm Thanks! > > > --- > > > BaseTools/Source/C/GenFw/Elf64Convert.c | 28 +++++++++++++++++++- > > > 1 file changed, 27 insertions(+), 1 deletion(-) > > > > > > diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C/GenFw/Elf64Convert.c > > > index 3d6319c821e9..d574300ac4fe 100644 > > > --- a/BaseTools/Source/C/GenFw/Elf64Convert.c > > > +++ b/BaseTools/Source/C/GenFw/Elf64Convert.c > > > @@ -1017,6 +1017,31 @@ WriteSections64 ( > > > } else if (mEhdr->e_machine == EM_AARCH64) { > > > > > > switch (ELF_R_TYPE(Rel->r_info)) { > > > + INT64 Offset; > > > + > > > + case R_AARCH64_LD64_GOT_LO12_NC: > > > + // > > > + // Convert into an ADD instruction - see R_AARCH64_ADR_GOT_PAGE below. > > > + // > > > + *(UINT32 *)Targ &= 0x3ff; > > > + *(UINT32 *)Targ |= 0x91000000 | ((Sym->st_value & 0xfff) << 10); > > > + break; > > > + > > > + case R_AARCH64_ADR_GOT_PAGE: > > > + // > > > + // This relocation points to the GOT entry that contains the absolute > > > + // address of the symbol we are referring to. Since EDK2 only uses > > > + // fully linked binaries, we can avoid the indirection, and simply > > > + // refer to the symbol directly. This implies having to patch the > > > + // subsequent LDR instruction (covered by a R_AARCH64_LD64_GOT_LO12_NC > > > + // relocation) into an ADD instruction - this is handled above. > > > + // > > > + Offset = (Sym->st_value - (Rel->r_offset & ~0xfff)) >> 12; > > > + > > > + *(UINT32 *)Targ &= 0x9000001f; > > > + *(UINT32 *)Targ |= ((Offset & 0x1ffffc) << (5 - 2)) | ((Offset & 0x3) << 29); > > > + > > > + /* fall through */ > > > > > > case R_AARCH64_ADR_PREL_PG_HI21: > > > // > > > @@ -1037,7 +1062,6 @@ WriteSections64 ( > > > // Attempt to convert the ADRP into an ADR instruction. > > > // This is only possible if the symbol is within +/- 1 MB. > > > // > > > - INT64 Offset; > > > > > > // Decode the ADRP instruction > > > Offset = (INT32)((*(UINT32 *)Targ & 0xffffe0) << 8); > > > @@ -1212,6 +1236,8 @@ WriteRelocations64 ( > > > case R_AARCH64_LDST32_ABS_LO12_NC: > > > case R_AARCH64_LDST64_ABS_LO12_NC: > > > case R_AARCH64_LDST128_ABS_LO12_NC: > > > + case R_AARCH64_ADR_GOT_PAGE: > > > + case R_AARCH64_LD64_GOT_LO12_NC: > > > // > > > // No fixups are required for relative relocations, provided that > > > // the relative offsets between sections have been preserved in > > > -- > > > 2.17.1 > > >