This is an archive of the discontinued LLVM Phabricator instance.

[ELF][AArch64] Refine and fix the condition when BTI/PAC PLT needs bti c
ClosedPublic

Authored by MaskRay on Sep 21 2021, 8:15 PM.

Details

Summary

(As I mentioned in https://reviews.llvm.org/D62609#1534158 ,
the condition for using bti c for executable can be loosened.)

In two cases the address of a PLT may escape:

  • canonical PLT entry for a STT_FUNC
  • non-preemptible STT_GNU_IFUNC which is converted to STT_FUNC

The first case can be detected with needsPltAddr.

The second case is not straightforward to detect because for the Relocations.cpp
created directSym, it's difficult to know whether the associated sym has
exercised the !needsPlt(expr) code path. Just use the conservative isInIplt
condition. A non-preemptible ifunc not referenced by non-GOT-generating
non-PLT-generating relocations will have an unneeded bti c, but the cost is acceptable.

The second case fixes a bug as well: a -shared link may have non-preemptible ifunc.
Before the patch we did not emit bti c and could be wrong if the PLT address escaped.
GNU ld doesn't handle the case: relocation R_AARCH64_ADR_PREL_PG_HI21 against STT_GNU_IFUNC symbol 'ifunc2' isn't handled by elf64_aarch64_final_link_relocate (https://sourceware.org/bugzilla/show_bug.cgi?id=28370)

For -shared, if BTI is enabled but PAC is disabled, the PLT entry size increases
from 16 to 24 because we have to select the PLT scheme early, but the cost is
acceptable.

Diff Detail

Event Timeline

MaskRay created this revision.Sep 21 2021, 8:15 PM
MaskRay requested review of this revision.Sep 21 2021, 8:15 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 21 2021, 8:15 PM
MaskRay edited the summary of this revision. (Show Details)Sep 21 2021, 8:22 PM
MaskRay edited the summary of this revision. (Show Details)Sep 21 2021, 9:18 PM

Will check the test cases later tonight, but at first glance this looks good to me.

peter.smith accepted this revision.Sep 22 2021, 11:11 AM

LGTM thanks for implementing the optimisation.

This revision is now accepted and ready to land.Sep 22 2021, 11:11 AM
abrachet added inline comments.
lld/test/ELF/aarch64-ifunc-bti.s
10

I've just noticed this while doing something unrelated to this patch, but I think that this should be %t.so? Looks like it'll just work with that change

Herald added a project: Restricted Project. · View Herald TranscriptJul 14 2022, 1:42 PM
Herald added a subscriber: StephenFan. · View Herald Transcript
MaskRay added inline comments.Jul 14 2022, 1:44 PM
lld/test/ELF/aarch64-ifunc-bti.s
10

Thanks for spotting this! I'll fix it.