This is an archive of the discontinued LLVM Phabricator instance.

[LLD][ELF][RISCV] Add auipc->lui relaxation.
Needs RevisionPublic

Authored by dybv-sc on May 3 2023, 8:06 AM.

Details

Reviewers
MaskRay
Summary

This relaxation enables use of weak undefined symbols in
programms written with medany code model in mind if code
section happens to be placed far from zero address(>2GiB).

It performs following transormation:

auipc + add -> lui + add

It does it if symbol address is out of range for pc-relative
addressing mode (>2GiB far from pc) and within range for
zero-relative one.

Diff Detail

Event Timeline

dybv-sc created this revision.May 3 2023, 8:06 AM
Herald added a project: Restricted Project. · View Herald Transcript
dybv-sc requested review of this revision.May 3 2023, 8:06 AM
MaskRay requested changes to this revision.EditedMay 3 2023, 10:14 AM

This relaxation enables use of weak undefined symbols in programms written with medany code model in mind if code section happens to be placed far from zero address(>2GiB).

I think the motivation is not right. For such weak references, the compiler should generate GOT-generating relocations, not relying on linker relaxation (which is an optional feature) for correctness. https://maskray.me/blog/2021-04-25-weak-symbol

https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/201 "Not doing so is deprecated and a future version of this specification will require using the GOT, not just advise."

This revision now requires changes to proceed.May 3 2023, 10:14 AM

This relaxation enables use of weak undefined symbols in programms written with medany code model in mind if code section happens to be placed far from zero address(>2GiB).

I think the motivation is not right. For such weak references, the compiler should generate GOT-generating relocations, not relying on linker relaxation (which is an optional feature) for correctness.

https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/201 "Not doing so is deprecated and a future version of this specification will require using the GOT, not just advise."

I need to get back to my LLVM patch to do that :( if someone wants to commandeer the series that's fine by me otherwise I'll try and do it once I'm finished with what I'm currently preoccupied with

This comment was removed by jrtc27.

This relaxation enables use of weak undefined symbols in programms written with medany code model in mind if code section happens to be placed far from zero address(>2GiB).

I think the motivation is not right. For such weak references, the compiler should generate GOT-generating relocations, not relying on linker relaxation (which is an optional feature) for correctness. https://maskray.me/blog/2021-04-25-weak-symbol

https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/201 "Not doing so is deprecated and a future version of this specification will require using the GOT, not just advise."

I understand that, but why not to do that linker relaxation too? That makes lld more compatible with gnu.ld which does that already. Besides, it enables linking with libraries that were compiled using old abi, so that change enhance backwards compatibility.

This relaxation enables use of weak undefined symbols in programms written with medany code model in mind if code section happens to be placed far from zero address(>2GiB).

I think the motivation is not right. For such weak references, the compiler should generate GOT-generating relocations, not relying on linker relaxation (which is an optional feature) for correctness. https://maskray.me/blog/2021-04-25-weak-symbol

https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/201 "Not doing so is deprecated and a future version of this specification will require using the GOT, not just advise."

I understand that, but why not to do that linker relaxation too? That makes lld more compatible with gnu.ld which does that already. Besides, it enables linking with libraries that were compiled using old abi, so that change enhance backwards compatibility.

This relaxation is not appropriate and it will be removed from a future psABI. Code that relies on the brittle assumption is super rare and if present, should be fixed, not relying on the linker to work around the user error.
I don't think the need is significant enough to justify the to-be-removed relaxation.

asb added a comment.May 16 2023, 6:37 AM

Do recent GCC toolchains still produce code requiring this relaxation (as opposed to using the GOT)?

Outside of R_RISCV_ALIGN, is this the only case that requires relaxation implementation for correctness? I agree with @MaskRay that it is better for the RISC-V ecosystem if the set of required relaxation transformations is kept minimal (not least as it makes it easier to bring up a new linker). But I'm also wary about incompatibilities between lld and GNU ld leading to issues.

evandro removed a subscriber: evandro.May 17 2023, 3:55 PM