[PATCH] D118077: Print C-string literals in mapfile

Roger Kim via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Feb 5 12:39:47 PST 2022


Roger created this revision.
Herald added a project: lld-macho.
Herald added a reviewer: lld-macho.
Roger retitled this revision from "Print C-string literals in symbol table." to "Print C-string literals in mapfile".
Roger edited the summary of this revision.
Herald added a subscriber: pengfei.
Roger updated this revision to Diff 406203.
Roger added a comment.
Roger edited the summary of this revision.
Roger published this revision for review.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Ready for review


This diff has the C-string literals printed into the mapfile in the symbol table like how ld64 does.

Here is what ld64's mapfile looks like with C-string literals:

  # Path: out
  # Arch: x86_64
  # Object files:
  [  0] linker synthesized
  [  1] foo.o
  # Sections:
  # Address       Size            Segment Section
  0x100003F7D     0x0000001D      __TEXT  __text
  0x100003F9A     0x0000001E      __TEXT  __cstring
  0x100003FB8     0x00000048      __TEXT  __unwind_info
  # Symbols:
  # Address       Size            File  Name
  0x100003F7D     0x0000001D      [  1] _main
  0x100003F9A     0x0000000E      [  1] literal string: Hello world!\n
  0x100003FA8     0x00000010      [  1] literal string: Hello, it's me\n
  0x100003FB8     0x00000048      [  0] compact unwind info

Here is what the new lld's Mach-O mapfile looks like:

  # Path: /Users/rgr/local/llvm-project/build/Debug/tools/lld/test/MachO/Output/map-file.s.tmp/c-string-liter
  al-out
  # Arch: x86_64
  # Object files:
  [  0] linker synthesized
  [  1] /Users/rgr/local/llvm-project/build/Debug/tools/lld/test/MachO/Output/map-file.s.tmp/c-string-literal
  .o
  # Sections:
  # Address       Size            Segment Section
  0x1000002E0     0x0000001D      __TEXT  __text
  0x1000002FD     0x0000001D      __TEXT  __cstring
  # Symbols:
  # Address           File  Name
  0x1000002E0     [  1] _main
  0x1000002FD     [  1] literal string: Hello world!\n
  0x10000030B     [  1] literal string: Hello, it's me\n


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D118077

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


Index: lld/test/MachO/map-file.s
===================================================================
--- lld/test/MachO/map-file.s
+++ lld/test/MachO/map-file.s
@@ -2,6 +2,7 @@
 # RUN: rm -rf %t; split-file %s %t
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/foo.s -o %t/foo.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/c-string-literal.s -o %t/c-string-literal.o
 
 # RUN: %lld -map %t/map %t/test.o %t/foo.o --time-trace -o %t/test-map
 # RUN: llvm-objdump --syms --section-headers %t/test-map > %t/objdump
@@ -51,4 +52,33 @@
 # CHECK-NEXT: 0x[[#FOO]]       [  2]  _foo
 # CHECK-NEXT: 0x[[#NUMBER]]    [  1]  _number
 
+#--- c-string-literal.s
+.section __TEXT,__cstring
+.globl _hello_world, _hello_its_me, _main
+
+_hello_world:
+.asciz "Hello world!\n"
+
+_hello_its_me:
+.asciz "Hello, it's me"
+
+.text
+_main:
+  movl $0x2000004, %eax # write() syscall
+  mov $1, %rdi # stdout
+  leaq _hello_world(%rip), %rsi
+  mov $13, %rdx # length of str
+  syscall
+  ret
+
+# RUN: %lld -map %t/c-string-literal-map %t/c-string-literal.o -o %t/c-string-literal-out
+# RUN: cat %t/c-string-literal-map
+# RUN: FileCheck --check-prefix=CSTRING %s < %t/c-string-literal-map
+
+## C-string literals should be printed as "literal string: <C string literal>"
+# CSTRING-LABEL: Symbols:
+# CSTRING-DAG: _main
+# CSTRING-DAG: literal string: Hello world!\n
+# CSTRING-DAG: literal string: Hello, it's me
+
 # MAPFILE: "name":"Total Write map file"
Index: lld/MachO/MapFile.cpp
===================================================================
--- lld/MachO/MapFile.cpp
+++ lld/MachO/MapFile.cpp
@@ -31,6 +31,7 @@
 #include "OutputSection.h"
 #include "OutputSegment.h"
 #include "Symbols.h"
+#include "SyntheticSections.h"
 #include "Target.h"
 #include "llvm/Support/Parallel.h"
 #include "llvm/Support/TimeProfiler.h"
@@ -76,7 +77,28 @@
   std::vector<std::string> str(syms.size());
   parallelForEachN(0, syms.size(), [&](size_t i) {
     raw_string_ostream os(str[i]);
-    os << toString(*syms[i]);
+    auto *sym = syms[i];
+
+    switch (sym->isec->kind()) {
+    case InputSection::CStringLiteralKind: {
+      // Output "literal string: <string literal>"
+      auto *isec = static_cast<CStringInputSection *>(sym->isec);
+      assert(isec != nullptr);
+      auto &piece = isec->getStringPiece(sym->value);
+      auto offset = sym->value - piece.outSecOff;
+      auto pieceIndex = &piece - &(*isec->pieces.begin());
+      auto str = isec->getStringRef(pieceIndex);
+      assert(str.back() == '\000');
+      (os << "literal string: ")
+          .write_escaped(str.substr(offset,
+                                    // Remove null sequence at the end
+                                    str.size() - offset - 1));
+      break;
+    }
+    case InputSection::ConcatKind:
+    case InputSection::WordLiteralKind:
+      os << toString(*sym);
+    }
   });
 
   DenseMap<Symbol *, std::string> ret;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118077.406203.patch
Type: text/x-patch
Size: 3020 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220205/4c440c90/attachment.bin>


More information about the llvm-commits mailing list