[dsymutil] Copy symbol table regardless of LINKEDIT segment
authorJonas Devlieghere <jonas@devlieghere.com>
Fri, 25 Feb 2022 19:37:03 +0000 (11:37 -0800)
committerJonas Devlieghere <jonas@devlieghere.com>
Fri, 25 Feb 2022 21:24:39 +0000 (13:24 -0800)
Ensure we copy the symbol table for MH_PRELOAD Mach-Os, which don't have
a LINKEDIT segment, but (can) have a symbol table.

rdar://88919473

Differential revision: https://reviews.llvm.org/D120583

llvm/test/tools/dsymutil/ARM/preload.test [new file with mode: 0644]
llvm/test/tools/dsymutil/Inputs/private/tmp/preload/foo [new file with mode: 0755]
llvm/test/tools/dsymutil/Inputs/private/tmp/preload/foo.o [new file with mode: 0644]
llvm/tools/dsymutil/MachOUtils.cpp

diff --git a/llvm/test/tools/dsymutil/ARM/preload.test b/llvm/test/tools/dsymutil/ARM/preload.test
new file mode 100644 (file)
index 0000000..3caa4a2
--- /dev/null
@@ -0,0 +1,10 @@
+$ cat foo.c
+void start(void) asm("start");
+void start(void) {}
+$ xcrun clang -c -o foo.o foo.c -g3
+$ xcrun clang -o foo foo.o -g3 -Wl,-preload -nodefaultlibs
+
+RUN: dsymutil -oso-prepend-path %p/../Inputs %p/../Inputs/private/tmp/preload/foo -o %t.dSYM
+RUN: llvm-nm %p/../Inputs/private/tmp/preload/foo | FileCheck %s
+RUN: llvm-nm %t.dSYM/Contents/Resources/DWARF/foo | FileCheck %s
+CHECK: start
diff --git a/llvm/test/tools/dsymutil/Inputs/private/tmp/preload/foo b/llvm/test/tools/dsymutil/Inputs/private/tmp/preload/foo
new file mode 100755 (executable)
index 0000000..db45a8e
Binary files /dev/null and b/llvm/test/tools/dsymutil/Inputs/private/tmp/preload/foo differ
diff --git a/llvm/test/tools/dsymutil/Inputs/private/tmp/preload/foo.o b/llvm/test/tools/dsymutil/Inputs/private/tmp/preload/foo.o
new file mode 100644 (file)
index 0000000..2b2df02
Binary files /dev/null and b/llvm/test/tools/dsymutil/Inputs/private/tmp/preload/foo.o differ
index 26a2786..b4f3f33 100644 (file)
@@ -336,15 +336,6 @@ static bool isExecutable(const object::MachOObjectFile &Obj) {
     return Obj.getHeader().filetype != MachO::MH_OBJECT;
 }
 
-static bool hasLinkEditSegment(const object::MachOObjectFile &Obj) {
-  bool HasLinkEditSegment = false;
-  iterateOnSegments(Obj, [&](const MachO::segment_command_64 &Segment) {
-    if (StringRef("__LINKEDIT") == Segment.segname)
-      HasLinkEditSegment = true;
-  });
-  return HasLinkEditSegment;
-}
-
 static unsigned segmentLoadCommandSize(bool Is64Bit, unsigned NumSections) {
   if (Is64Bit)
     return sizeof(MachO::segment_command_64) +
@@ -396,7 +387,9 @@ bool generateDsymCompanion(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
   unsigned LoadCommandSize = 0;
   unsigned NumLoadCommands = 0;
 
-  // Get LC_UUID and LC_BUILD_VERSION.
+  bool HasSymtab = false;
+
+  // Check LC_SYMTAB and get LC_UUID and LC_BUILD_VERSION.
   MachO::uuid_command UUIDCmd;
   SmallVector<MachO::build_version_command, 2> BuildVersionCmd;
   memset(&UUIDCmd, 0, sizeof(UUIDCmd));
@@ -420,14 +413,16 @@ bool generateDsymCompanion(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
       BuildVersionCmd.push_back(Cmd);
       break;
     }
+    case MachO::LC_SYMTAB:
+      HasSymtab = true;
+      break;
     default:
       break;
     }
   }
 
   // If we have a valid symtab to copy, do it.
-  bool ShouldEmitSymtab =
-      isExecutable(InputBinary) && hasLinkEditSegment(InputBinary);
+  bool ShouldEmitSymtab = HasSymtab && isExecutable(InputBinary);
   if (ShouldEmitSymtab) {
     LoadCommandSize += sizeof(MachO::symtab_command);
     ++NumLoadCommands;