public inbox for linuxppc-dev@ozlabs.org 
 help / color / mirror / Atom feed
From: Alan Modra <amodra@bigpond•net.au>
To: Jonathan Bartlett <johnnyb@eskimo•com>
Cc: linuxppc-dev@ozlabs•org
Subject: Re: Why the "opd" section?
Date: Tue, 25 Jul 2006 23:36:34 +0930	[thread overview]
Message-ID: <20060725140634.GH6872@bubble.grove.modra.org> (raw)
In-Reply-To: <Pine.SUN.4.58.0607250612510.14690@eskimo.com>

On Tue, Jul 25, 2006 at 06:23:30AM -0700, Jonathan Bartlett wrote:
> > OPD is an array of function pointers.  Function pointers on powerpc64
> > are not just simple pointers to some code;  They specify the code entry
> > point, the TOC pointer, and the static chain pointer (unused by C).
> > To call a function, you need to know all these values because functions
> > do not initialise their own TOC pointer.  This allows for more efficient
> > code.  The compiler/linker can omit the TOC pointer load when both
> > caller and callee are known to share the same TOC.  (In many ways, the
> > TOC is like the powerpc32 GOT.  powerpc32 -fpic/PIC code initialises the
> > GOT pointer on entry to every function, even when caller and callee are
> > known to have the same GOT pointer.)
> 
> So, why is it only in the 64-bit ELF?  Is it just because it's a newer
> idea?

The idea isn't exactly new.  It's more the case that the powerpc32 ABI
is so old.

> Also, I tried compiling this piece of code in its own file:
> 
> int addtwo(int a)
> {
>         int b = addone(a);
>         return addone(b);
> }
> 
> I compiled it with "gcc -m64 -shared -fPIC -O3 -S tmp.c"
> 
> The resulting code had the following as it's compilation:
> 
>         mflr 0
>         std 0,16(1)
>         stdu 1,-112(1)
>         bl addone
>         nop
>         bl addone
>         nop
>         addi 1,1,112
>         ld 0,16(1)
>         mtlr 0
>         blr
> 
> It seems to be branching _directly_ to the opd entry, instead of the
> address pointed to by the opd entry.  Also, the TOC pointer is never used,
> despite the fact that these were separately compiled.  Or is this taken
> care of by the linker?  Under what conditions would the TOC pointer be
> different?

Yes, you're right.  It does look to be branching directly to the opd
entry at the assembly level.  It of course won't do that because
powerpc64-ld is clever enough to realise that doing so would never make
any sense.  Instead, ld does the OPD lookup and modifies the "bl" insns
to go directly to the function's code entry if the TOC vallue of caller
and callee is identical, or to go via a linker generated stub if they
are different.

A number of different stubs might be used.  For a large statically
linked program where TOC size exceeds 64k, ld groups functions such that
each group uses a TOC of less than 64k, and inserts r2 adjusting stubs
between calls from one group to another.  For calls to shared library
functions, ld inserts plt call stubs that load an opd entry from the
plt.  See binutils bfd/elf64-ppc.c if you want all the gory details on
stubs.

-- 
Alan Modra
IBM OzLabs - Linux Technology Centre

  reply	other threads:[~2006-07-25 14:06 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-24  4:01 Why the "opd" section? Jonathan Bartlett
2006-07-25  2:15 ` Alan Modra
2006-07-25 13:23   ` Jonathan Bartlett
2006-07-25 14:06     ` Alan Modra [this message]
2006-07-28 18:19       ` Linas Vepstas
2006-08-09 13:15       ` .tc entries question Jonathan Bartlett
2006-08-09 13:55         ` Alan Modra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060725140634.GH6872@bubble.grove.modra.org \
    --to=amodra@bigpond$(echo .)net.au \
    --cc=johnnyb@eskimo$(echo .)com \
    --cc=linuxppc-dev@ozlabs$(echo .)org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox