[JITLink][MachO] Re-apply b64afadf306, MachO linker-private support, with fixes.
authorLang Hames <lhames@gmail.com>
Sat, 14 Mar 2020 23:53:05 +0000 (16:53 -0700)
committerLang Hames <lhames@gmail.com>
Sun, 15 Mar 2020 01:36:15 +0000 (18:36 -0700)
Global symbols with linker-private prefixes should be resolvable across object
boundaries, but internal symbols with linker-private prefixes should not.

llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.cpp
llvm/lib/ExecutionEngine/JITLink/MachO_arm64.cpp
llvm/lib/ExecutionEngine/Orc/Mangling.cpp
llvm/test/ExecutionEngine/JITLink/X86/Inputs/MachO_global_linker_private_def.s [new file with mode: 0644]
llvm/test/ExecutionEngine/JITLink/X86/Inputs/MachO_internal_linker_private_def.s [new file with mode: 0644]
llvm/test/ExecutionEngine/JITLink/X86/MachO_linker_private_symbols.s [new file with mode: 0644]

index eea3a72..bb96116 100644 (file)
@@ -64,12 +64,14 @@ Linkage MachOLinkGraphBuilder::getLinkage(uint16_t Desc) {
 }
 
 Scope MachOLinkGraphBuilder::getScope(StringRef Name, uint8_t Type) {
-  if (Name.startswith("l"))
-    return Scope::Local;
   if (Type & MachO::N_PEXT)
     return Scope::Hidden;
-  if (Type & MachO::N_EXT)
-    return Scope::Default;
+  if (Type & MachO::N_EXT) {
+    if (Name.startswith("l"))
+      return Scope::Hidden;
+    else
+      return Scope::Default;
+  }
   return Scope::Local;
 }
 
index 9447674..cc39403 100644 (file)
@@ -560,7 +560,8 @@ private:
       *(ulittle32_t *)FixupPtr = Value;
       break;
     }
-    case Pointer64: {
+    case Pointer64:
+    case Pointer64Anon: {
       uint64_t Value = E.getTarget().getAddress() + E.getAddend();
       *(ulittle64_t *)FixupPtr = Value;
       break;
index b21a0d7..6baf186 100644 (file)
@@ -89,6 +89,8 @@ getObjectSymbolInfo(ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
   if (!Obj)
     return Obj.takeError();
 
+  bool IsMachO = isa<object::MachOObjectFile>(Obj->get());
+
   SymbolFlagsMap SymbolFlags;
   for (auto &Sym : (*Obj)->symbols()) {
     // Skip symbols not defined in this object file.
@@ -113,14 +115,20 @@ getObjectSymbolInfo(ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
     auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
     if (!SymFlags)
       return SymFlags.takeError();
+
+    // Strip the 'exported' flag from MachO linker-private symbols.
+    if (IsMachO && Name->startswith("l"))
+      *SymFlags &= ~JITSymbolFlags::Exported;
+
     SymbolFlags[InternedName] = std::move(*SymFlags);
   }
 
   SymbolStringPtr InitSymbol;
 
-  if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(Obj->get())) {
-    for (auto &Sec : MachOObj->sections()) {
-      auto SecType = MachOObj->getSectionType(Sec);
+  if (IsMachO) {
+    auto &MachOObj = cast<object::MachOObjectFile>(*Obj->get());
+    for (auto &Sec : MachOObj.sections()) {
+      auto SecType = MachOObj.getSectionType(Sec);
       if ((SecType & MachO::SECTION_TYPE) == MachO::S_MOD_INIT_FUNC_POINTERS) {
         std::string InitSymString;
         raw_string_ostream(InitSymString)
diff --git a/llvm/test/ExecutionEngine/JITLink/X86/Inputs/MachO_global_linker_private_def.s b/llvm/test/ExecutionEngine/JITLink/X86/Inputs/MachO_global_linker_private_def.s
new file mode 100644 (file)
index 0000000..ad285a6
--- /dev/null
@@ -0,0 +1,12 @@
+# Supplies a global definition, l_foo, with a linker-private prefix. Since this
+# definition is marked as global it should be resolvable outside the object.
+
+       .section        __TEXT,__text,regular,pure_instructions
+       .macosx_version_min 10, 14
+        .globl          l_foo
+       .p2align        4, 0x90
+l_foo:
+       xorl    %eax, %eax
+       retq
+
+.subsections_via_symbols
diff --git a/llvm/test/ExecutionEngine/JITLink/X86/Inputs/MachO_internal_linker_private_def.s b/llvm/test/ExecutionEngine/JITLink/X86/Inputs/MachO_internal_linker_private_def.s
new file mode 100644 (file)
index 0000000..bda54ad
--- /dev/null
@@ -0,0 +1,12 @@
+# Supplies an internal definition, l_foo, with a linker-private prefix. Since
+# this definition is not marked as global it should not be resolvable outside
+# the object.
+
+       .section        __TEXT,__text,regular,pure_instructions
+       .macosx_version_min 10, 14
+       .p2align        4, 0x90
+l_foo:
+       xorl    %eax, %eax
+       retq
+
+.subsections_via_symbols
diff --git a/llvm/test/ExecutionEngine/JITLink/X86/MachO_linker_private_symbols.s b/llvm/test/ExecutionEngine/JITLink/X86/MachO_linker_private_symbols.s
new file mode 100644 (file)
index 0000000..21c19cb
--- /dev/null
@@ -0,0 +1,22 @@
+# RUN: rm -rf %t && mkdir -p %t
+# RUN: llvm-mc -triple=x86_64-apple-macosx10.9 -filetype=obj \
+# RUN:   -o %t/global_lp_def.o %S/Inputs/MachO_global_linker_private_def.s
+# RUN: llvm-mc -triple=x86_64-apple-macosx10.9 -filetype=obj \
+# RUN:   -o %t/internal_lp_def.o %S/Inputs/MachO_internal_linker_private_def.s
+# RUN: llvm-mc -triple=x86_64-apple-macosx10.9 -filetype=obj \
+# RUN:   -o %t/macho_lp_test.o %s
+# RUN: llvm-jitlink -noexec %t/global_lp_def.o %t/macho_lp_test.o
+# RUN: not llvm-jitlink -noexec %t/internal_lp_def.o %t/macho_lp_test.o
+#
+# Check that we can resolve global symbols whose names start with the
+# linker-private prefix 'l' across object boundaries, and that we can't resolve
+# internals with the linker-private prefix across object boundaries.
+
+       .section        __TEXT,__text,regular,pure_instructions
+       .macosx_version_min 10, 14
+       .globl  _main
+       .p2align        4, 0x90
+_main:
+       jmp     l_foo
+
+.subsections_via_symbols