This is an archive of the discontinued LLVM Phabricator instance.

RISCV: adjust relocation emission
ClosedPublic

Authored by compnerd on Aug 19 2022, 2:36 PM.

Details

Summary

Simplify and make the pair-wise relocation more precise. If either of
the symbol references are textual, the relocation must be delayed. If
the difference is across sections, delay it as well which partially
matches the behaviour of gas. We unfortunately do not handle the case
where the difference references a symbol that is not yet defined. In
such a case, we simply fail to resolve the difference, which should
hopefully not be too onerous (particularly since no other target
supports cross-section references and it is not clear if this was
intentional on the part of RISCV).

Diff Detail

Event Timeline

compnerd created this revision.Aug 19 2022, 2:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 19 2022, 2:36 PM
compnerd requested review of this revision.Aug 19 2022, 2:36 PM
asb added a comment.Sep 1 2022, 5:58 AM

Is this patch ready for review? Is there any more background on the need for this change?

@asb no, this patch wasn't ready, I didn't want to loose the work on it though. This is dependent on D133456 though. I think that I have a variant of this which is nearly ready.

compnerd updated this revision to Diff 458789.Sep 8 2022, 10:20 AM
compnerd edited the summary of this revision. (Show Details)
compnerd added a reviewer: MaskRay.
compnerd updated this revision to Diff 462282.Sep 22 2022, 1:42 PM

Stylistic changes suggested by @MaskRay offline.

MaskRay accepted this revision.Sep 22 2022, 3:47 PM
MaskRay added inline comments.
llvm/test/MC/RISCV/riscv64-64b-pcrel.s
1

You can guard negative tests with macros so that the positive tests can use llvm-mc return code of 0.

llvm-mc supports --defsym. I have added some tests using --defsym ERR=1.

This revision is now accepted and ready to land.Sep 22 2022, 3:47 PM
This revision was automatically updated to reflect the committed changes.

It would've been nice if you asked for review once this was ready... as far as we're concerned it was still parked and not ready for review

MaskRay added a comment.EditedJun 8 2023, 10:23 AM

The description says

We unfortunately do not handle the case where the difference references a symbol that is not yet defined. In such a case, we simply fail to resolve the difference, which should hopefully not be too onerous ...

Android riscv64 nterp: Fix "oat" code size calculation. is an instance to work around suppressed R_RISCV_ADD32/R_RISCV_SUB32. It reduces to:

# Both end and begin are not defined yet. We decide ADD/SUB relocations upfront and don't know they will be needed.
.4byte end-begin

begin:
  call foo
end:

I have elaborated this in my notes (https://maskray.me/blog/2021-03-14-the-dark-side-of-riscv-linker-relaxation "Label differences related to text section symbols") and will think about improvements in my free time.

D145474: Ideally we should delay the decision of PC-relative ...

evandro removed a subscriber: evandro.Jun 8 2023, 10:35 AM