[PATCH] D70570: [PowerPC] Only use PLT annotations if using PIC relocation model

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 10 10:55:33 PST 2019


MaskRay added a comment.

In D70570#1777712 <https://reviews.llvm.org/D70570#1777712>, @sfertile wrote:

> Hi Justin,
>
> I'd like to help reviewing this patch. I know how calls work in the 2 64-bit ELF abis pretty well but am not familiar with 32-bit PowerPC at all so I have a handful of questions to start:
>
> 1. I've been using this ABI DOC <http://refspecs.linux-foundation.org/elf/elfspec_ppc.pdf>. Is this the 32-bit ABI supported in LLVM? Are there other 32-bit ELF PowerPC Abis I need to be aware of?
> 2. What `secure-plt` reference or references should I read to get up to speed?


`Power Architecture® 32-bit Application Binary Interface Supplement 1.0 - Linux® & Embedded` (https://www.polyomino.org.uk/publications/2011/Power-Arch-32-bit-ABI-supp-1.0-Unified.pdf) is the newest version I can find. You can find secure PLT information there. Unfortunately I think a lot of details are undocumented. They are essentially implementation details in gcc and binutils, so we have to observe gcc/as/ld output or read their source code.

@jhibbits This patch needs a rebase after D70126 <https://reviews.llvm.org/D70126>.

> Finally I'd like to just verify my base understanding of the difference between `bl sym` and `bl sym at PLT`.
> 
> `bl sym` will produce a call instruction  with an `R_PPC_REL24` relocation, which will tell the linker to fill in the instruction with the offset from the program counter at the call instruction to `sym`.

Matches my observations.

> `bl sym at PLT` will produce a call instruction with an `R_PPC_PLTREL24` which will instruct the linker to create a PLT, allocate a PLT entry for `sym`, and fill in the instruction with the offset from the program counter to the PLT entry for 'sym'.

This is like R_X86_64_PLT32. A PLT entry may be optimized out if it is not necessary (the symbol is non-preemptible). See

  // lld/ELF/Relocations.cpp
  static RelExpr fromPlt(RelExpr expr) {
    // We decided not to use a plt. Optimize a reference to the plt to a
    // reference to the symbol itself.
    switch (expr) {
    case R_PLT_PC:
    case R_PPC32_PLTREL:
      return R_PC;

The tricky part is that the addend may be 0 (BSS-PLT or Secure-PLT -fpic/-fpie) or 0x8000 (Secure-PLT -fPIC/-fPIE).

As an aside: https://reviews.llvm.org/D70937 was committed to fix PPC64 problems, but we need to think how to make the code more general (less PPC32 specific hack).

> I'm used to having 1 relocation type for both calls, and the linker can determine based on the properties of 'sym' and whether its linking a shared object or not whether the call needs to indirect through the PLT.
> 
> - If you emit a call instruction with the PLTREL relocation but the symbol ends up module local, does the linker convert the call to a local one?

It does.

> - Is it an error to not use a PLTREL relocation for a call that needs to be indirected through the PLT or is the linker smart enough to add a PLT entry for the symbol and branch to there instead?

For Secure PLT which uses 0x8000 as the addend of an R_PPC_PLTREL24 relocation, R_PPC_PLTREL24 must be used. It is an error in GNU ld and lld to use R_PPC_REL24.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D70570/new/

https://reviews.llvm.org/D70570





More information about the llvm-commits mailing list