[lld-macho] Implement branch-range-extension thunks
authorGreg McGary <gkm@fb.com>
Tue, 30 Mar 2021 00:33:48 +0000 (17:33 -0700)
committerGreg McGary <gkm@fb.com>
Wed, 12 May 2021 16:44:58 +0000 (09:44 -0700)
commit93c8559baf551a7a30ab17654569ac5ac92986f4
treef50d689deb0d44fd269137f0d49f9d6c5a7f2e1b
parent9934571eab9c6b3be22c4c8857bd3f4280b77843
[lld-macho] Implement branch-range-extension thunks

Extend the range of calls beyond an architecture's limited branch range by first calling a thunk, which loads the far address into a scratch register (x16 on ARM64) and branches through it.

Other ports (COFF, ELF) use multiple passes with successively-refined guesses regarding the expansion of text-space imposed by thunk-space overhead. This MachO algorithm places thunks during MergedOutputSection::finalize() in a single pass using exact thunk-space overheads. Thunks are kept in a separate vector to avoid the overhead of inserting into the `inputs` vector of `MergedOutputSection`.

FIXME:
* arm64-stubs.s test is broken
* add thunk tests
* Handle thunks to DylibSymbol in MergedOutputSection::finalize()

Differential Revision: https://reviews.llvm.org/D100818
15 files changed:
lld/MachO/Arch/ARM64.cpp
lld/MachO/Driver.cpp
lld/MachO/InputSection.cpp
lld/MachO/InputSection.h
lld/MachO/MergedOutputSection.cpp
lld/MachO/MergedOutputSection.h
lld/MachO/Options.td
lld/MachO/Symbols.cpp
lld/MachO/Symbols.h
lld/MachO/SyntheticSections.cpp
lld/MachO/SyntheticSections.h
lld/MachO/Target.h
lld/MachO/Writer.cpp
lld/test/MachO/arm64-thunks.s [new file with mode: 0644]
lld/test/MachO/tools/generate-thunkable-program.py [new file with mode: 0755]