[PATCH] D154604: [BOLT] Calculate output values using BOLTLinker

Job Noorman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 6 06:32:42 PDT 2023


jobnoorman created this revision.
jobnoorman added reviewers: rafauler, maksfb, yota9, Amir.
Herald added subscribers: asb, treapster, pmatos, ayermolo, luismarques, pengfei, s.egerton, PkmX, simoncook, arichardson.
Herald added a project: All.
jobnoorman requested review of this revision.
Herald added subscribers: llvm-commits, wangpc.
Herald added a project: LLVM.

BOLT uses `MCAsmLayout` to calculate the output values of functions and
basic blocks. This means output values are calculated based on a
pre-linking state and any changes to symbol values during linking will
cause incorrect values to be used.

This issue can be triggered by enabling linker relaxation on RISC-V.
Since linker relaxation can remove instructions, symbol values may
change. This causes, among other things, the symbol table created by
BOLT in the output executable to be incorrect.

This patch solves this issue by using `BOLTLinker` to get symbol values
instead of `MCAsmLayout`. This way, output values are calculated based
on a post-linking state. To make sure the linker can update all
necessary symbols, this patch also makes sure all these symbols are not
marked as temporary so that they end-up in the object file's symbol
table.

Note that I tested this using llvm-bolt-wrapper.py and everything seems
to match. There is one test that fails with this patch, however:
X86/MachO/emit_new_binary_with_external_symbol.test

I am pretty sure this test was incorrect before, though: the last 4
instructions in the ORIGINAL test seem to be junk that is leftover from
the input binary, for example. I'm not entirely sure what happened there
and why it seems fixed by this patch so it would be appreciated if
someone could take an extra look at this.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154604

Files:
  bolt/include/bolt/Core/BinaryBasicBlock.h
  bolt/include/bolt/Core/BinaryFunction.h
  bolt/include/bolt/Rewrite/DWARFRewriter.h
  bolt/include/bolt/Rewrite/RewriteInstance.h
  bolt/lib/Core/BinaryBasicBlock.cpp
  bolt/lib/Core/BinaryContext.cpp
  bolt/lib/Core/BinaryEmitter.cpp
  bolt/lib/Core/BinaryFunction.cpp
  bolt/lib/Passes/IdenticalCodeFolding.cpp
  bolt/lib/Rewrite/DWARFRewriter.cpp
  bolt/lib/Rewrite/MachORewriteInstance.cpp
  bolt/lib/Rewrite/RewriteInstance.cpp
  bolt/test/X86/MachO/emit_new_binary_with_external_symbol.test

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154604.537693.patch
Type: text/x-patch
Size: 19183 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230706/28972d33/attachment.bin>


More information about the llvm-commits mailing list