This is an archive of the discontinued LLVM Phabricator instance.

[LLD] Emit DT_PPC64_OPT into the dynamic section
ClosedPublic

Authored by stefanp on May 15 2023, 6:52 PM.

Details

Summary

As per section 4.2.2 of the PowerPC ELFv2 ABI, this value tells the dynamic linker which optimizations it is allowed to do.
Specifically, the higher order bit of the two tells the dynamic linker that there may be multiple TOC pointers in the binary.

When we resolve any NOTOC relocations during linking, we need to set this value because we may be calling
TOC functions from NOTOC functions when the NOTOC function already clobbered the TOC pointer.

In practice, this ensures that the PLT resolver always resolves the call to the GEP (global entry point) of
the TOC function (which will set up the TOC for the TOC function).

Original patch by nemanjai

Diff Detail

Unit TestsFailed

Event Timeline

stefanp created this revision.May 15 2023, 6:52 PM
Herald added a project: Restricted Project. · View Herald Transcript
stefanp requested review of this revision.May 15 2023, 6:52 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 15 2023, 6:52 PM
MaskRay added inline comments.May 15 2023, 8:01 PM
lld/ELF/SyntheticSections.cpp
1536

Since dynamicSectionOptFlags >= 0 for PPC64, the condition is unneeded.

1537

DT_PPC64_OPT

stefanp updated this revision to Diff 522734.May 16 2023, 12:04 PM

Removed guard that was not required.
Added DT_PPC64_OPT.

MaskRay added inline comments.May 16 2023, 12:47 PM
lld/ELF/SyntheticSections.cpp
1534

This comment is incomplete and repeats what's said elsewhere. I'd just remove this comment. Users know to find dynamicSectionOptFlags.

1535

Does GNU ld emit this dynamic tag when it is 0? An alternative is to omit this entry if 0.

lld/ELF/Target.h
147

You can initialize this to 0 and omit the assignment in PPC64. Other targets don't use this.

You can name this ppc64DynamicSectionOpt

As per section 4.2.2, ...

"section 4.2.2" of what?

stefanp edited the summary of this revision. (Show Details)May 17 2023, 11:14 AM
stefanp updated this revision to Diff 523156.May 17 2023, 1:29 PM

Renamed the dynamicSectionOptFlags to PPC specific name.
Removed redundant comment.

lld/ELF/SyntheticSections.cpp
1535

I checked this with GNU ld (GNU Binutils) 2.37.20220122 on a PowerPC machine.

It looks like that version of ld does generate the dynamic tag even for 0.
I used the test test/ELF/ppc64-plt-stub.s which generates 0x0000000070000003 (PPC64_OPT) 0x0 with both this patch and ld.

MaskRay accepted this revision.May 24 2023, 12:13 PM

Looks great!

This revision is now accepted and ready to land.May 24 2023, 12:13 PM
This revision was landed with ongoing or failed builds.Jun 5 2023, 9:18 AM
This revision was automatically updated to reflect the committed changes.