[lld] 5b21395 - [lld-macho] Print archive names in linker map

Jez Ng via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 10 22:40:26 PST 2023


Author: Jez Ng
Date: 2023-03-11T01:40:14-05:00
New Revision: 5b21395cc2422d735c632afefcb95f08d8ed4b88

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

LOG: [lld-macho] Print archive names in linker map

If a symbol is pulled in from an archive, we should include the archive
name in the map file output. This is what ld64 does.

Note that we aren't using `toString(InputFile*)` here because it
includes the install name for dylibs in its output, and ld64's map file
does not contain those.

Reviewed By: #lld-macho, smeenai

Differential Revision: https://reviews.llvm.org/D145623

Added: 
    

Modified: 
    lld/MachO/MapFile.cpp
    lld/test/MachO/map-file.ll
    lld/test/MachO/map-file.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/MapFile.cpp b/lld/MachO/MapFile.cpp
index c16e046dcd1f7..0c1b4f1254d23 100644
--- a/lld/MachO/MapFile.cpp
+++ b/lld/MachO/MapFile.cpp
@@ -122,8 +122,17 @@ static MapInfo gatherMapInfo() {
   return info;
 }
 
+// We use this instead of `toString(const InputFile *)` as we don't want to
+// include the dylib install name in our output.
+static void printFileName(raw_fd_ostream &os, const InputFile *f) {
+  if (f->archiveName.empty())
+    os << f->getName();
+  else
+    os << f->archiveName << "(" << path::filename(f->getName()) + ")";
+}
+
 // For printing the contents of the __stubs and __la_symbol_ptr sections.
-void printStubsEntries(
+static void printStubsEntries(
     raw_fd_ostream &os,
     const DenseMap<lld::macho::InputFile *, uint32_t> &readerToFileOrdinal,
     const OutputSection *osec, size_t entrySize) {
@@ -134,8 +143,8 @@ void printStubsEntries(
                  sym->getName().str().data());
 }
 
-void printNonLazyPointerSection(raw_fd_ostream &os,
-                                NonLazyPointerSectionBase *osec) {
+static void printNonLazyPointerSection(raw_fd_ostream &os,
+                                       NonLazyPointerSectionBase *osec) {
   // ld64 considers stubs to belong to particular files, but considers GOT
   // entries to be linker-synthesized. Not sure why they made that decision, but
   // I think we can follow suit unless there's demand for better symbol-to-file
@@ -171,7 +180,9 @@ void macho::writeMapFile() {
   uint32_t fileIndex = 1;
   DenseMap<lld::macho::InputFile *, uint32_t> readerToFileOrdinal;
   for (InputFile *file : info.files) {
-    os << format("[%3u] %s\n", fileIndex, file->getName().str().c_str());
+    os << format("[%3u] ", fileIndex);
+    printFileName(os, file);
+    os << "\n";
     readerToFileOrdinal[file] = fileIndex++;
   }
 

diff  --git a/lld/test/MachO/map-file.ll b/lld/test/MachO/map-file.ll
index 2f8332a2a3732..c8ab444370b3e 100644
--- a/lld/test/MachO/map-file.ll
+++ b/lld/test/MachO/map-file.ll
@@ -27,9 +27,9 @@
 ; FOOBAR-NEXT: # Arch: x86_64
 ; FOOBAR-NEXT: # Object files:
 ; FOOBAR-NEXT: [  0] linker synthesized
-; FOOBAR-NEXT: [  1] {{.*}}{{/|\\}}usr/lib{{/|\\}}libSystem.tbd
-; FOOBAR-NEXT: [  2] {{.*}}{{/|\\}}map-file.ll.tmp/foo.thinlto.o
-; FOOBAR-NEXT: [  3] {{.*}}{{/|\\}}map-file.ll.tmp/bar.thinlto.o
+; FOOBAR-NEXT: [  1] {{.*}}{{/|\\}}usr/lib{{/|\\}}libSystem.tbd{{$}}
+; FOOBAR-NEXT: [  2] {{.*}}{{/|\\}}map-file.ll.tmp/foo.thinlto.o{{$}}
+; FOOBAR-NEXT: [  3] {{.*}}{{/|\\}}map-file.ll.tmp/bar.thinlto.o{{$}}
 ; FOOBAR-NEXT: # Sections:
 ; FOOBAR:      # Symbols:
 ; FOOBAR-NEXT: # Address        Size             File  Name

diff  --git a/lld/test/MachO/map-file.s b/lld/test/MachO/map-file.s
index 64c66830b21ef..f6b4e10260906 100644
--- a/lld/test/MachO/map-file.s
+++ b/lld/test/MachO/map-file.s
@@ -5,9 +5,11 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/c-string-literal.s -o %t/c-string-literal.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/baz.s -o %t/baz.o
 
+# RUN: llvm-ar --format=darwin crs %t/libfoo.a %t/foo.o
 # RUN: %lld -dylib %t/baz.o -o %t/libbaz.dylib
-# RUN: %lld -demangle -map %t/map %t/test.o %t/foo.o %t/c-string-literal.o \
-# RUN:   %t/libbaz.dylib --time-trace -o %t/test --no-deduplicate-strings
+# RUN: %lld -demangle -map %t/map %t/test.o -force_load %t/libfoo.a \
+# RUN:   %t/c-string-literal.o %t/libbaz.dylib --time-trace -o %t/test \
+# RUN:   --no-deduplicate-strings
 # RUN: llvm-objdump --syms --section-headers %t/test > %t/objdump
 ## Check that symbols in cstring sections aren't emitted
 ## Also check that we don't have redundant EH_Frame symbols (regression test)
@@ -42,11 +44,11 @@
 # CHECK-NEXT: # Arch: x86_64
 # CHECK-NEXT: # Object files:
 # CHECK-NEXT: [  0] linker synthesized
-# CHECK-NEXT: [  1] {{.*}}{{/|\\}}usr/lib{{/|\\}}libSystem.tbd
-# CHECK-NEXT: [  2] {{.*}}{{/|\\}}map-file.s.tmp/test.o
-# CHECK-NEXT: [  3] {{.*}}{{/|\\}}map-file.s.tmp/foo.o
-# CHECK-NEXT: [  4] {{.*}}{{/|\\}}map-file.s.tmp/c-string-literal.o
-# CHECK-NEXT: [  5] {{.*}}{{/|\\}}map-file.s.tmp/libbaz.dylib
+# CHECK-NEXT: [  1] {{.*}}{{/|\\}}usr/lib{{/|\\}}libSystem.tbd{{$}}
+# CHECK-NEXT: [  2] {{.*}}{{/|\\}}map-file.s.tmp/test.o{{$}}
+# CHECK-NEXT: [  3] {{.*}}{{/|\\}}map-file.s.tmp/libfoo.a(foo.o){{$}}
+# CHECK-NEXT: [  4] {{.*}}{{/|\\}}map-file.s.tmp/c-string-literal.o{{$}}
+# CHECK-NEXT: [  5] {{.*}}{{/|\\}}map-file.s.tmp/libbaz.dylib{{$}}
 
 # CHECK-NEXT: # Sections:
 # CHECK-NEXT: # Address           Size            Segment  Section
@@ -87,8 +89,8 @@
 
 # MAPFILE: "name":"Total Write map file"
 
-# RUN: %lld -demangle -dead_strip -map %t/stripped-map %t/test.o %t/foo.o \
-# RUN:   %t/c-string-literal.o %t/libbaz.dylib -o %t/stripped
+# RUN: %lld -demangle -dead_strip -map %t/stripped-map %t/test.o -force_load \
+# RUN:   %t/libfoo.a %t/c-string-literal.o %t/libbaz.dylib -o %t/stripped
 # RUN: FileCheck --check-prefix=STRIPPED %s < %t/stripped-map
 
 # STRIPPED-LABEL: Dead Stripped Symbols:
@@ -98,8 +100,8 @@
 # STRIPPED-DAG:   <<dead>>  0x0000000F  [  4] literal string: Hello, it's me
 # STRIPPED-DAG:   <<dead>>  0x0000000E  [  4] literal string: Hello world!\n
 
-# RUN: %lld --icf=all -map %t/icf-map %t/test.o %t/foo.o %t/c-string-literal.o \
-# RUN:   %t/libbaz.dylib -o /dev/null
+# RUN: %lld --icf=all -map %t/icf-map %t/test.o -force_load %t/libfoo.a \
+# RUN:   %t/c-string-literal.o %t/libbaz.dylib -o /dev/null
 # RUN: FileCheck --check-prefix=ICF %s < %t/icf-map
 
 ## Verify that folded symbols and cstrings have size zero. Note that ld64 prints


        


More information about the llvm-commits mailing list