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 this patch only deals with symbols of binary functions
(BinaryFunction::updateOutputValues). The technique described above
turned out to be too expensive for basic block symbols so those are
handled differently in D155604.
Depends on D155604
Background: we've discussed offline that this change causes a noticeable BOLT runtime regression.
The alternative approach for mapping input to output addresses used for address translation, will be to generate a map in a section that we can read and discard later (i.e. skip writing to a file). The map/section contents will be an ordered list of pairs <InputAddress, OutputAddress>. For output address we will emit the temporary symbol to the section. I believe BOLTLinker should be able to process it.
With the approach above, we can continue to emit all internal symbols as temps and avoid the regression.