Page MenuHomePhabricator

[PowerPC] Support PCRelative Callees for R_PPC64_REL24 Relocation

Authored by stefanp on Jul 1 2020, 4:58 AM.



The R_PPC64_REL24 is used in function calls when the caller requires a
valid TOC pointer. If the callee shares the same TOC or does not clobber
the TOC pointer then a direct call can be made. If the callee does not
share the TOC a thunk must be added to save the TOC pointer for the caller.

Up until PC Relative was introduced all local calls on medium and large code
models were assumed to share a TOC. This is no longer the case because
if the caller requires a TOC and the callee is PC Relative then the callee
can clobber the TOC even if it is in the same DSO.

This patch is to add support for a TOC caller calling a PC Relative callee that
clobbers the TOC.

Diff Detail

Event Timeline

stefanp created this revision.Jul 1 2020, 4:58 AM
stefanp updated this revision to Diff 274883.Jul 1 2020, 11:52 AM

Updated initial test case to clean up some of the instruction.
Added a second test to check that an error is reported when the nop after the
call is missing.

anil9 added a subscriber: anil9.Jul 2 2020, 6:42 PM
anil9 added inline comments.

guaranteee - > guarantee


need the save -> need to save


This stub does: -> This stub:


Branch -> Branches/Jumps

sfertile added inline comments.Jul 3 2020, 7:20 AM

Alternatively: 2) Tails calls the callee


What happens if offset doesn't fit within 26 bits?


This is being named the same as a branch extending thunk (ie a trampoline for when the call is too far to represent with a single call instruction). The name we create shoud represent the thunk type, it makes reading disassembly much easier. How about "__toc_save_` instead?

37 ↗(On Diff #274883)

nit: remove the compiler generated comments.


nit: remove extra blank line.

stefanp updated this revision to Diff 276164.Jul 7 2020, 11:47 AM
stefanp marked an inline comment as done.

Fixed some nits in comments.
Added a fatal error and a test case case to cover the situation where the offset
for the branch is more than 26 bits.


Good point.
Right now we just drop the extra bits from the offset and jump to an incorrect offset.

I'll add an error to fail at this point if the offset does not fit in the 26 bits.


The reason I had used this name was because GCC used long_branch.callee as the name as well so I figured I would do the same thing. However, I do like the idea of calling it __toc_save better so I'm going to use that instead.

MaskRay added inline comments.Jul 7 2020, 4:02 PM

Drop trailing period.

"R2 save stub branch offset is too large: " + Twine(offset)

15 ↗(On Diff #276164)

Delete addi 3, 3, 5 and extsw. They are irrelevant.

25 ↗(On Diff #276164)

Delete irrelevant mr and add

stefanp updated this revision to Diff 276281.Jul 7 2020, 5:45 PM

Updated text for error message.
Test cleanup be deleting lines that are not needed.

stefanp updated this revision to Diff 276284.Jul 7 2020, 5:49 PM

Cleaned up third test as well.

MaskRay added inline comments.Jul 7 2020, 7:01 PM

This needs a comment.

7 ↗(On Diff #276284)

Use ## for comments. Newer tests conform to this rule.

29 ↗(On Diff #276284)

add 3, 4, 3 is irrelevant and can be deleted.

35 ↗(On Diff #276284)

.size can be deleted

stefanp updated this revision to Diff 276373.Jul 8 2020, 4:28 AM
stefanp marked an inline comment as done.

Fixed a couple of conditions and added comments for them.
Added a test for the new condition.
Cleaned up existing tests.


I've added a comment.
Adding the comment forced me to re-assess the condition statement so I've also changed the condition to be just:

if ((s.stOther >> 5) == 1)

There was no reason to exclude R_PPC64_REL14 from this condition.
I have also added a test for R_PPC64_REL14.

sfertile accepted this revision.Jul 8 2020, 8:52 AM
sfertile marked an inline comment as done.




This revision is now accepted and ready to land.Jul 8 2020, 8:52 AM
This revision was automatically updated to reflect the committed changes.