[llvm] c278668 - [JITLink][MachO] Add support for non-subsections-via-symbols objects.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 17 16:00:05 PDT 2022


Author: Lang Hames
Date: 2022-08-17T15:55:42-07:00
New Revision: c2786683caa9d5011eda7429fcbe46e5987c8cd0

URL: https://github.com/llvm/llvm-project/commit/c2786683caa9d5011eda7429fcbe46e5987c8cd0
DIFF: https://github.com/llvm/llvm-project/commit/c2786683caa9d5011eda7429fcbe46e5987c8cd0.diff

LOG: [JITLink][MachO] Add support for non-subsections-via-symbols objects.

This patch updates MachOLinkGraphBuilder to honor the MH_SUBSECTIONS_VIA_SYMBOLS
flag. Prior to this patch we assumed MH_SUBSECTIONS_VIA_SYMBOLS, but never
checked the flag.

If MH_SUBSECTIONS_VIA_SYMBOLS is set (the default for MachO output on modern
compilers) then MachOLinkGraphBuilder will break MachO section content into
jitlink::Blocks on symbol boundaries. (This is how JITLink has always handled
MachO sections previously).

If MH_SUBSECTIONS_VIA_SYMBOLS is not set then MachOLinkGraphBuilder will create
a single jitlink::Block for each MachO section.

Existing hand-written testcases that were _not_ using the
.subsections_via_symbols directive are updated to use it. A new testcase for
non-subsections-via-symbols behavior is included.

Added: 
    llvm/test/ExecutionEngine/JITLink/X86/MachO_non_subsections_via_symbols.s

Modified: 
    llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp
    llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.h
    llvm/test/ExecutionEngine/JITLink/X86/Inputs/x86-64_self_relocation.s
    llvm/test/ExecutionEngine/JITLink/X86/MachO-duplicate-local.test
    llvm/test/ExecutionEngine/JITLink/X86/MachO_archive_load_hidden_expect_success.s
    llvm/test/ExecutionEngine/JITLink/X86/MachO_archive_support.s
    llvm/test/ExecutionEngine/JITLink/X86/MachO_weak_local.s

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp b/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp
index 1bf12f438be07..1315654f23124 100644
--- a/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp
+++ b/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp
@@ -51,7 +51,10 @@ MachOLinkGraphBuilder::MachOLinkGraphBuilder(
     : Obj(Obj),
       G(std::make_unique<LinkGraph>(
           std::string(Obj.getFileName()), std::move(TT), getPointerSize(Obj),
-          getEndianness(Obj), std::move(GetEdgeKindName))) {}
+          getEndianness(Obj), std::move(GetEdgeKindName))) {
+  auto &MachHeader = Obj.getHeader64();
+  SubsectionsViaSymbols = MachHeader.flags & MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
+}
 
 void MachOLinkGraphBuilder::addCustomSectionParser(
     StringRef SectionName, SectionParserFunction Parser) {
@@ -485,15 +488,24 @@ Error MachOLinkGraphBuilder::graphifyRegularSymbols() {
     }
 
     // Visit section symbols in order by popping off the reverse-sorted stack,
-    // building blocks for each alt-entry chain and creating symbols as we go.
+    // building graph symbols as we go.
+    //
+    // If MH_SUBSECTIONS_VIA_SYMBOLS is set we'll build a block for each
+    // alt-entry chain.
+    //
+    // If MH_SUBSECTIONS_VIA_SYMBOLS is not set then we'll just build one block
+    // for the whole section.
     while (!SecNSymStack.empty()) {
       SmallVector<NormalizedSymbol *, 8> BlockSyms;
 
+      // Get the symbols in this alt-entry chain, or the whole section (if
+      // !SubsectionsViaSymbols).
       BlockSyms.push_back(SecNSymStack.back());
       SecNSymStack.pop_back();
       while (!SecNSymStack.empty() &&
              (isAltEntry(*SecNSymStack.back()) ||
-              SecNSymStack.back()->Value == BlockSyms.back()->Value)) {
+              SecNSymStack.back()->Value == BlockSyms.back()->Value ||
+             !SubsectionsViaSymbols)) {
         BlockSyms.push_back(SecNSymStack.back());
         SecNSymStack.pop_back();
       }

diff  --git a/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.h b/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.h
index 2951a8533098c..7cbc9f33b182c 100644
--- a/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.h
+++ b/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.h
@@ -226,6 +226,7 @@ class MachOLinkGraphBuilder {
   const object::MachOObjectFile &Obj;
   std::unique_ptr<LinkGraph> G;
 
+  bool SubsectionsViaSymbols = false;
   DenseMap<unsigned, NormalizedSection> IndexToSection;
   Section *CommonSection = nullptr;
 

diff  --git a/llvm/test/ExecutionEngine/JITLink/X86/Inputs/x86-64_self_relocation.s b/llvm/test/ExecutionEngine/JITLink/X86/Inputs/x86-64_self_relocation.s
index 37384408c73b2..8afb7e2edddf2 100644
--- a/llvm/test/ExecutionEngine/JITLink/X86/Inputs/x86-64_self_relocation.s
+++ b/llvm/test/ExecutionEngine/JITLink/X86/Inputs/x86-64_self_relocation.s
@@ -32,4 +32,6 @@ _main:
   movzbl  %al, %eax
   addq  $32, %rsp
   popq  %rbp
-  retq
\ No newline at end of file
+  retq
+
+  .subsections_via_symbols

diff  --git a/llvm/test/ExecutionEngine/JITLink/X86/MachO-duplicate-local.test b/llvm/test/ExecutionEngine/JITLink/X86/MachO-duplicate-local.test
index 25def26e3b7ca..d2895e448a3c0 100644
--- a/llvm/test/ExecutionEngine/JITLink/X86/MachO-duplicate-local.test
+++ b/llvm/test/ExecutionEngine/JITLink/X86/MachO-duplicate-local.test
@@ -19,7 +19,7 @@ FileHeader:
   filetype:        0x1
   ncmds:           4
   sizeofcmds:      280
-  flags:           0x0
+  flags:           0x2000
   reserved:        0x0
 LoadCommands:
   - cmd:             LC_SEGMENT_64

diff  --git a/llvm/test/ExecutionEngine/JITLink/X86/MachO_archive_load_hidden_expect_success.s b/llvm/test/ExecutionEngine/JITLink/X86/MachO_archive_load_hidden_expect_success.s
index e52f905628a35..497dd30d80714 100644
--- a/llvm/test/ExecutionEngine/JITLink/X86/MachO_archive_load_hidden_expect_success.s
+++ b/llvm/test/ExecutionEngine/JITLink/X86/MachO_archive_load_hidden_expect_success.s
@@ -25,3 +25,5 @@ _main:
 	.p2align	3
 ExtraDefRef:
 	.quad	ExtraDef
+
+        .subsections_via_symbols

diff  --git a/llvm/test/ExecutionEngine/JITLink/X86/MachO_archive_support.s b/llvm/test/ExecutionEngine/JITLink/X86/MachO_archive_support.s
index 77f396075c4eb..800bf66734ec9 100644
--- a/llvm/test/ExecutionEngine/JITLink/X86/MachO_archive_support.s
+++ b/llvm/test/ExecutionEngine/JITLink/X86/MachO_archive_support.s
@@ -21,3 +21,5 @@ _main:
 	.p2align	3
 ExtraDefRef:
 	.quad	ExtraDef
+
+        .subsections_via_symbols

diff  --git a/llvm/test/ExecutionEngine/JITLink/X86/MachO_non_subsections_via_symbols.s b/llvm/test/ExecutionEngine/JITLink/X86/MachO_non_subsections_via_symbols.s
new file mode 100644
index 0000000000000..e1adb3bc75a1f
--- /dev/null
+++ b/llvm/test/ExecutionEngine/JITLink/X86/MachO_non_subsections_via_symbols.s
@@ -0,0 +1,21 @@
+# The assembly below does NOT include the usual .subsections_via_symbols
+# directive. Check that when the directive is absent we only create one block
+# to cover the whole data section.
+#
+# REQUIRES: asserts
+# RUN: llvm-mc -triple=x86_64-apple-macosx10.9 -filetype=obj -o %t %s
+# RUN: llvm-jitlink -debug-only=jitlink -noexec %t 2>&1 | FileCheck %s
+
+# CHECK:        Creating graph symbols...
+# CHECK:          Graphifying regular section __DATA,__data...
+# CHECK-NEXT:       Creating block {{.*}} with 2 symbol(s)...
+# CHECK-NEXT:         0x0000000000000004 -- 0x0000000000000008: _b
+# CHECK-NEXT:         0x0000000000000000 -- 0x0000000000000008: _main
+
+	.section	__DATA,__data
+	.p2align	2
+        .globl  _main
+_main:
+	.long	1
+_b:
+        .long   2

diff  --git a/llvm/test/ExecutionEngine/JITLink/X86/MachO_weak_local.s b/llvm/test/ExecutionEngine/JITLink/X86/MachO_weak_local.s
index 41990b1312244..f227411908a4b 100644
--- a/llvm/test/ExecutionEngine/JITLink/X86/MachO_weak_local.s
+++ b/llvm/test/ExecutionEngine/JITLink/X86/MachO_weak_local.s
@@ -14,3 +14,5 @@ _foo_weak:
   .p2align  4, 0x90
 _main:
   jmp _foo_weak
+
+  .subsections_via_symbols


        


More information about the llvm-commits mailing list