This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: What is R_X86_64_GOTPLT64 used for?
- From: "H.J. Lu" <hjl dot tools at gmail dot com>
- To: Michael Matz <matz at suse dot de>
- Cc: "x86-64-abi at googlegroups dot com" <x86-64-abi at googlegroups dot com>, GCC Development <gcc at gcc dot gnu dot org>, Binutils <binutils at sourceware dot org>, GNU C Library <libc-alpha at sourceware dot org>
- Date: Mon, 17 Nov 2014 12:35:59 -0800
- Subject: Re: What is R_X86_64_GOTPLT64 used for?
- Authentication-results: sourceware.org; auth=none
- References: <CAMe9rOqb0g2asAe6UZ0hxh8jFf-+eBiaez0pLrPjd0oqVdP0Rg at mail dot gmail dot com> <alpine dot LNX dot 2 dot 00 dot 1411131717220 dot 405 at wotan dot suse dot de> <CAMe9rOrTg=YtVZ1EqN7ha8qUPSXzms20eMU51txVAmL3+cUsQQ at mail dot gmail dot com> <CAMe9rOrnQRo3XXowAEcd_h=i_i5v04=i=kLWjm2ANduv8MwhYQ at mail dot gmail dot com> <alpine dot LNX dot 2 dot 00 dot 1411171425020 dot 405 at wotan dot suse dot de>
On Mon, Nov 17, 2014 at 6:14 AM, Michael Matz <matz@suse.de> wrote:
> Hi,
>
> On Thu, 13 Nov 2014, H.J. Lu wrote:
>
>> Linker does:
>>
>> ... code that looks like it might create just one GOT slot ...
>>
>> 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.
>
> The above is not correct. Had you tried you'd see this:
>
> % cat x.c
> extern void foo (void);
> void main (void)
> {
> void (*f)(void) = foo;
> f();
> foo();
> }
> % gcc -fPIE -mcmodel=large -S x.c; cat x.s
> ...
> movabsq $foo@GOT, %rax
> ...
> movabsq $foo@PLTOFF, %rax
> ...
>
> So, foo is access via @GOT offset and @PLTOFF. Then,
>
> % cat y.c
> void foo (void) {}
> % gcc -o liby.so -shared -fPIC y.c
> % gcc -fPIE -mcmodel=large x.s liby.so
> % readelf -r a.out
> ...
> 000000600ff8 000400000006 R_X86_64_GLOB_DAT 0000000000000000 foo + 0
> ...
> 000000601028 000400000007 R_X86_64_JUMP_SLO 0000000000000000 foo + 0
> ...
>
> The first one (to 600ff8) is the normal GOT slot, the second one the GOT
> slot for the PLT entry. Both are actually used:
>
> 00000000004005f0 <foo@plt>:
> 4005f0: ff 25 32 0a 20 00 jmpq *0x200a32(%rip) # 601028 <_GLOBAL_OFFSET_TABLE_+0x28>
>
> That uses the second GOT slot, and:
>
> 00000000004006ec <main>:
> 4006ec: 55 push %rbp
> 4006ed: 48 89 e5 mov %rsp,%rbp
> 4006f0: 53 push %rbx
> 4006f1: 48 83 ec 18 sub $0x18,%rsp
> 4006f5: 48 8d 1d f9 ff ff ff lea -0x7(%rip),%rbx # 4006f5 <main+0x9>
> 4006fc: 49 bb 0b 09 20 00 00 movabs $0x20090b,%r11
> 400703: 00 00 00
> 400706: 4c 01 db add %r11,%rbx
> 400709: 48 b8 f8 ff ff ff ff movabs $0xfffffffffffffff8,%rax
> 400710: ff ff ff
> 400713: 48 8b 04 03 mov (%rbx,%rax,1),%rax
>
> This uses the first slot at 0x600ff8.
>
> So, no, currently GOT and GOTPLT (at least how it's supposed to be
> implemented) are not equivalent.
It has nothing to do with large model. The same thing
happens to small model. We may be to able optimize
it, independent of GOTPLT.
In any case, -mcmodel=large shouldn't change program behavior.
--
H.J.