From patchwork Thu Nov 13 17:58:46 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 3714 Received: (qmail 1174 invoked by alias); 13 Nov 2014 17:58:51 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 1145 invoked by uid 89); 13 Nov 2014 17:58:50 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.2 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mail-oi0-f42.google.com MIME-Version: 1.0 X-Received: by 10.182.108.229 with SMTP id hn5mr3083496obb.51.1415901527075; Thu, 13 Nov 2014 09:58:47 -0800 (PST) In-Reply-To: References: Date: Thu, 13 Nov 2014 09:58:46 -0800 Message-ID: Subject: Re: What is R_X86_64_GOTPLT64 used for? From: "H.J. Lu" To: Michael Matz Cc: "x86-64-abi@googlegroups.com" , GCC Development , Binutils , GNU C Library On Thu, Nov 13, 2014 at 9:03 AM, H.J. Lu wrote: > On Thu, Nov 13, 2014 at 8:33 AM, Michael Matz wrote: >> Hi, >> >> On Thu, 13 Nov 2014, H.J. Lu wrote: >> >>> x86-64 psABI has >>> >>> name@GOT: specifies the offset to the GOT entry for the symbol name >>> from the base of the GOT. >>> >>> name@GOTPLT: specifies the offset to the GOT entry for the symbol name >>> from the base of the GOT, implying that there is a corresponding PLT entry. >>> >>> But GCC never generates name@GOTPLT and assembler fails to assemble >>> it: >> >> I've added the implementation for the large model, but only dimly remember >> how it got added to the ABI in the first place. The additional effect of >> using that reloc was supposed to be that the GOT slot was to be placed >> into .got.plt, and this might hint at the reasoning for this reloc: >> >> If you take the address of a function and call it, you need both a GOT >> slot and a PLT entry (where the existence of GOT slot is implied by the > > That is correct. > >> PLT of course). Now, if you use the normal @GOT64 reloc for the >> address-taking operation that would create a slot in .got. For the call >> instruction you'd use @PLT (or variants thereof, like PLTOFF), which >> creates the PLT slot _and_ a slot in .got.plt. So, now we've ended up >> with two GOT slots for the same symbol, where one should be enough (the >> address taking operation can just as well use the slot in .got.plt). So >> if the compiler would emit @GOTPLT64 instead of @GOT64 for all address >> references to symbols where it knows that it's a function it could save >> one GOT slot. > > @GOTPLT will create a PLT entry, but it doesn't mean PLT entry will > be used. Only @PLTOFF will use PLT entry. Linker should be smart > enough to use only one GOT slot, regardless if @GOTPLT or @GOT > is used to take function address and call via PLT. However, if > @GOTPLT is used without @PLT, a PLT entry will be created and unused. > > I'd like to propose > > 1. Update psABI to remove R_X86_64_GOTPLT64. > 2. Fix assembler to take @GOTPLT for backward compatibility, > 3. Make sure that linker uses one GOT slot for @GOT and @PLTOFF. > Linker does: case R_X86_64_GOT64: case R_X86_64_GOTPLT64: base_got = htab->elf.sgot; if (htab->elf.sgot == NULL) abort (); if (h != NULL) { bfd_boolean dyn; off = h->got.offset; if (h->needs_plt && h->plt.offset != (bfd_vma)-1 && off == (bfd_vma)-1) { /* We can't use h->got.offset here to save state, or even just remember the offset, as finish_dynamic_symbol would use that as offset into .got. */ bfd_vma plt_index = h->plt.offset / plt_entry_size - 1; off = (plt_index + 3) * GOT_ENTRY_SIZE; base_got = htab->elf.sgotplt; } So if a symbol is accessed by both @GOT and @PLTOFF, its needs_plt will be true and its got.plt entry will be used for both @GOT and @GOTPLT. @GOTPLT has no advantage over @GOT, but potentially wastes a PLT entry. Here is a patch to mark relocation 30 (R_X86_64_GOTPLT64) as reserved. I pushed updated x86-64 psABI changes to https://github.com/hjl-tools/x86-64-psABI/tree/hjl/master I will update linker to keep accepting relocation 30 and treat it the same as R_X86_64_GOT64. diff --git a/low-level-sys-info.tex b/low-level-sys-info.tex index 7f636fc..981390b 100644 --- a/low-level-sys-info.tex +++ b/low-level-sys-info.tex @@ -1242,9 +1242,6 @@ examples and discussion. They are: \begin{itemize} \item \code{name@GOT}: specifies the offset to the GOT entry for the symbol \code{name} from the base of the GOT. -\item \code{name@GOTPLT}: specifies the offset to the GOT entry for - the symbol \code{name} from the base of the GOT, implying that - there is a corresponding PLT entry. \item \code{name@GOTOFF}: specifies the offset to the location of the symbol \code{name} from the base of the GOT. \item \code{name@GOTPCREL}: specifies the offset to the GOT entry diff --git a/object-files.tex b/object-files.tex index 4705e96..c0698dc 100644 --- a/object-files.tex +++ b/object-files.tex @@ -611,7 +611,7 @@ Name & Value & Field & Calculati on \\ \hline \code{R_X86_64_GOTPC64} & 29 & word64 & \code{GOT - P + A} \\ \hline gnu-6:pts/18[114]> cat /tmp/x diff --git a/low-level-sys-info.tex b/low-level-sys-info.tex index 7f636fc..981390b 100644 --- a/low-level-sys-info.tex +++ b/low-level-sys-info.tex @@ -1242,9 +1242,6 @@ examples and discussion. They are: \begin{itemize} \item \code{name@GOT}: specifies the offset to the GOT entry for the symbol \code{name} from the base of the GOT. -\item \code{name@GOTPLT}: specifies the offset to the GOT entry for - the symbol \code{name} from the base of the GOT, implying that - there is a corresponding PLT entry. \item \code{name@GOTOFF}: specifies the offset to the location of the symbol \code{name} from the base of the GOT. \item \code{name@GOTPCREL}: specifies the offset to the GOT entry diff --git a/object-files.tex b/object-files.tex index 4705e96..c0698dc 100644 --- a/object-files.tex +++ b/object-files.tex @@ -611,7 +611,7 @@ Name & Value & Field & Calculation \\ \hline \code{R_X86_64_GOTPC64} & 29 & word64 & \code{GOT - P + A} \\ \hline -\code{R_X86_64_GOTPLT64} & 30 & word64 & \code{G + A} \\ +\code{Reserved} & 30 & & \\ \hline \code{R_X86_64_PLTOFF64} & 31 & word64 & \code{L - GOT + A} \\