This is an archive of the discontinued LLVM Phabricator instance.

[LLD][AArch64] Fix resolution of R_PLT_PAGE RelExpr
ClosedPublic

Authored by peter.smith on Nov 13 2018, 7:19 AM.

Details

Summary

The R_AARCH64_ADR_PREL_PG_HI21 relocation type is given the R_PAGE_PC RelExpr. This can be transformed to R_PLT_PAGE_PC via toPlt(). Unfortunately the resolution is identical to R_PAGE_PC so instead of getting the address of the PLT entry we get the address of the symbol which may not be correct in the case of static ifuncs (See comments in D54314 and example in D54145). The fix is simple. Just use getPltVA() + A rather than getVA() to get the address of the PLT entry.

It looks like the only way that R_PLT_PAGE_PC will be generated from compiled code is via the address of an ifunc in the same executable with non-pic code. I thought that it might be possible to generate an example by taking the address of a function in a DSO but this does not go through the toPlt() route, it uses processRelocAux() // This handles a non PIC program call to function in a shared library.

The patch is rebased on top of D54314 and D54145, it is most likely independent of it, but makes sense to be committed afterwards.

Diff Detail

Repository
rL LLVM

Event Timeline

peter.smith created this revision.Nov 13 2018, 7:19 AM

It looks correct. Minor nits/suggestions.

ELF/InputSection.cpp
683 ↗(On Diff #173847)

Perhaps better to split them into the different 'case's then?
I would remove Dest and start doing early returns too.

May be something like the following?

case R_PAGE_PC:    
  uint64_t Val = Sym.isUndefWeak() ? A : Sym.getVA(A);
  return getAArch64Page(Val)  - getAArch64Page(P);

case R_PLT_PAGE_PC:
  uint64_t Val = Sym.isUndefWeak() ? A : (Sym.getVA(A) + A);
  return getAArch64Page(Val) - getAArch64Page(P);
test/ELF/aarch64-gnu-ifunc3.s
6 ↗(On Diff #173847)

Please an empty line before .text

Thanks, for the comments. I've updated the diff to incorporate them.

grimar accepted this revision.Nov 13 2018, 9:50 AM

LGTM. We can rename the R_PLT_PAGE_PC/R_PAGE_PC expressions to use an AARCH64 suffix (like
https://reviews.llvm.org/rLLD346749 did), but that is unrelated to this patch.

This revision is now accepted and ready to land.Nov 13 2018, 9:50 AM
ruiu accepted this revision.Nov 13 2018, 9:43 PM

LGTM

This revision was automatically updated to reflect the committed changes.