[PATCH] D145687: [BOLT] Add minimal RISC-V 64-bit support

Job Noorman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 9 06:01:51 PST 2023


jobnoorman created this revision.
jobnoorman added reviewers: yota9, maksfb, rafauler, asb.
Herald added subscribers: luke, treapster, pmatos, ayermolo, VincentWu, vkmr, frasercrmck, luismarques, apazos, sameer.abuasal, s.egerton, Jim, jocewei, PkmX, the_o, brucehoult, MartinMosbeck, rogfer01, edward-jones, zzheng, jrtc27, shiva0217, kito-cheng, niosHD, sabuasal, simoncook, johnrusso, rbar, arichardson.
Herald added a reviewer: Amir.
Herald added a project: All.
jobnoorman requested review of this revision.
Herald added subscribers: llvm-commits, pcwang-thead, MaskRay.
Herald added a project: LLVM.

Just enough features are implemented to process a simple "hello world"
executable and produce something that still runs (including libc calls).
This was mainly a matter of implementing support for various
relocations. Currently, the following are handled:

- R_RISCV_JAL
- R_RISCV_CALL
- R_RISCV_CALL_PLT
- R_RISCV_BRANCH
- R_RISCV_RVC_BRANCH
- R_RISCV_RVC_JUMP
- R_RISCV_GOT_HI20
- R_RISCV_PCREL_HI20
- R_RISCV_PCREL_LO12_I
- R_RISCV_RELAX
- R_RISCV_NONE

Note that in order to implement some of these relocations, this patch
relies on another patch for RuntimeDyld (D145686 <https://reviews.llvm.org/D145686>). However, it has been pointed
out in the past (D98560 <https://reviews.llvm.org/D98560>) that it might be better to switch to JITLink.
That change would be a lot more involved, though, so using RuntimeDyld
for the time being seems to be easiest way forward for getting RISC-V
support in BOLT.

Executables linked with linker relaxation will probably fail to be
processed. BOLT relocates .text to a high address while leaving .plt at
its original (low) address. This causes PC-relative PLT calls that were
relaxed to a JAL to not fit their offset in an I-immediate anymore. This
is something that will be addressed in a later patch.

Changes to the BOLT core are relatively minor. Two things were tricky to
implement and needed slightly larger changes. I'll explain those below.

The R_RISCV_CALL(_PLT) relocation is put on the first instruction of a
AUIPC/JALR pair, the second does not get any relocation (unlike other
PCREL pairs). This causes issues with the combinations of the way BOLT
processes binaries and the RISC-V MC-layer handles relocations:

- BOLT reassembles instructions one by one and since the JALR doesn't have a relocation, it simply gets copied without modification;
- Even though the MC-layer handles R_RISCV_CALL properly (adjusts both the AUIPC and the JALR), it assumes the immediates of both instructions are 0 (to be able to or-in a new value). This will most likely not be the case for the JALR that got copied over.

To handle this difficulty without resorting to RISC-V-specific hacks in
the BOLT core, a new postProcessFunction() method was added to
MCPlusBuilder. This method allows backends to perform any kind fixing
needed after binary functions have been created. For RISC-V, this
methods searches for AUIPC/JALR pairs and zeroes-out the immediate of
the JALR.

A second difficulty was supporting ABS symbols. As far as I can tell,
ABS symbols were not handled at all, causing __global_pointer$ to break.
RewriteInstance::analyzeRelocation was updated to handle these
generically.

Tests are provided for all supported relocations. Note that in order to
test the correct handling of PLT entries, an ELF file produced by GCC
had to be used. While I tried to strip the YAML representation, it's
still quite large. Any suggestions on how to improve this would be
appreciated.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D145687

Files:
  bolt/include/bolt/Core/BinaryContext.h
  bolt/include/bolt/Core/BinaryFunction.h
  bolt/include/bolt/Core/MCPlusBuilder.h
  bolt/include/bolt/Rewrite/RewriteInstance.h
  bolt/lib/Core/BinaryContext.cpp
  bolt/lib/Core/BinaryFunction.cpp
  bolt/lib/Core/Relocation.cpp
  bolt/lib/Rewrite/CMakeLists.txt
  bolt/lib/Rewrite/RewriteInstance.cpp
  bolt/lib/Target/CMakeLists.txt
  bolt/lib/Target/RISCV/CMakeLists.txt
  bolt/lib/Target/RISCV/RISCVMCPlusBuilder.cpp
  bolt/test/RISCV/Inputs/plt-gnu-ld.yaml
  bolt/test/RISCV/lit.local.cfg
  bolt/test/RISCV/plt-gnu-ld.test
  bolt/test/RISCV/reloc-branch.s
  bolt/test/RISCV/reloc-call.s
  bolt/test/RISCV/reloc-got.s
  bolt/test/RISCV/reloc-jal.s
  bolt/test/RISCV/reloc-pcrel.s
  bolt/test/RISCV/reloc-rvc-branch.s
  bolt/test/RISCV/reloc-rvc-jump.s

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D145687.503743.patch
Type: text/x-patch
Size: 67559 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230309/9a69a592/attachment.bin>


More information about the llvm-commits mailing list