[PATCH] D129936: [JITLink][COFF][x86_64] Reimplement ADDR32NB/REL32.

Sunho Kim via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 16 11:22:04 PDT 2022


sunho created this revision.
sunho added reviewers: lhames, sgraenitz, v.g.vassilev.
Herald added subscribers: jsji, StephenFan, pengfei, hiraditya.
Herald added a project: All.
sunho requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Reimplements ADDR32NB/REL32 relocations to properly deal with out-of-reach targets.

1. ADDR32NB

This relocation requires to set the delta of target from the "imagebase" of module in native environment. We will request the client or orc runtime to make imagebase symbol available to jitdylib that will point to the start address of jitdylib. This way orc runtime has a good access to image base location and properly register the function table so that windows kernel can locate rewind table properly. The value that this relocation will set shouldn't exceed the 32bit address space.

2. REL32

This relocation is used for any RIP relative addressing instructions and the relocation offset is set to the middle of instruction. The value that this relocation will set shouldn't exceed the 32bit address space.

Because of 32bit range limitation we need to create a PLT stub when target it out of reach, but we have two requirements. First, stub address needs to be unique since it's pointed by unwind/exception table. If we make duplicate stub address it will not recognize the function in backtrace for example. Second, stub cannot be used for some rel32 relocation. As rel32 relocation is used for basically any kind of RIP relative instructions including lea/mov it's invalid to generate stubs for this kind of data instructions.

As x86 instruction bytes are not safe to disassemble backwards (it will be fundamentally broken for our case as well because of existance of REX prefix), we're not allowed to patch the instruction bytes -- notice relocation offset points to the middle of instruction that we need to go back to find the start. The compiler will not generate rel32 relocation of data instruction to real external symbol such as the symbols in external shared library, so if symbol definitions are given correctly rel32 relocation of data instruction will not be out-of-reach unless JITLink memory manager failed its duty.

To deal with these complications while maintaing the external orc interface (e.g. not adding custom DynamicSearchDefinitionGenerator), we first generate stub for every potential out-of-reach symbols. And then, we clean stub up later in optimization pass. We try to thoroughly validate in optimization pass to not break the unique address of stub address and 32bit in-range limitation as well as adhering to the requirement to clean up stub for data rel32 instructions.

As this process is very COFF specific, COFF specific relocation edges were added extending the x86_64 edge. These edges will be lowered to generic x86_64 relocation edges at the end of pipeline.


https://reviews.llvm.org/D129936

Files:
  llvm/include/llvm/ExecutionEngine/JITLink/x86_64.h
  llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.cpp
  llvm/lib/ExecutionEngine/JITLink/COFF_x86_64.cpp
  llvm/test/ExecutionEngine/JITLink/X86/COFF_abs.s
  llvm/test/ExecutionEngine/JITLink/X86/COFF_addr32nb_reloc.test
  llvm/test/ExecutionEngine/JITLink/X86/COFF_basic.s
  llvm/test/ExecutionEngine/JITLink/X86/COFF_comdat_any.test
  llvm/test/ExecutionEngine/JITLink/X86/COFF_comdat_associative.test
  llvm/test/ExecutionEngine/JITLink/X86/COFF_comdat_associative_dead_strip.test
  llvm/test/ExecutionEngine/JITLink/X86/COFF_comdat_associative_no_dead_strip.test
  llvm/test/ExecutionEngine/JITLink/X86/COFF_comdat_exact_match.test
  llvm/test/ExecutionEngine/JITLink/X86/COFF_comdat_intervene.test
  llvm/test/ExecutionEngine/JITLink/X86/COFF_comdat_largest.test
  llvm/test/ExecutionEngine/JITLink/X86/COFF_comdat_noduplicate.test
  llvm/test/ExecutionEngine/JITLink/X86/COFF_comdat_same_size.test
  llvm/test/ExecutionEngine/JITLink/X86/COFF_comdat_weak.s
  llvm/test/ExecutionEngine/JITLink/X86/COFF_comdat_weak_duplicate.s
  llvm/test/ExecutionEngine/JITLink/X86/COFF_comdat_weak_plus_strong.s
  llvm/test/ExecutionEngine/JITLink/X86/COFF_common_symbol.s
  llvm/test/ExecutionEngine/JITLink/X86/COFF_external_func.s
  llvm/test/ExecutionEngine/JITLink/X86/COFF_external_var.s
  llvm/test/ExecutionEngine/JITLink/X86/COFF_file_debug.s
  llvm/test/ExecutionEngine/JITLink/X86/COFF_label.test
  llvm/test/ExecutionEngine/JITLink/X86/COFF_static_var.s
  llvm/test/ExecutionEngine/JITLink/X86/COFF_strong_duplicate.s
  llvm/test/ExecutionEngine/JITLink/X86/COFF_weak_external.s
  llvm/test/ExecutionEngine/JITLink/X86/COFF_x86-64_small_pic_relocations.s

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129936.445242.patch
Type: text/x-patch
Size: 34268 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220716/1351c1f2/attachment.bin>


More information about the llvm-commits mailing list