[lld] 4f2c46c - Print C-string literals in mapfile
Jez Ng via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 11 16:42:31 PST 2022
Author: Roger Kim
Date: 2022-02-11T19:42:20-05:00
New Revision: 4f2c46c35ccd17ef726c4b4e8e9500d7972e757a
URL: https://github.com/llvm/llvm-project/commit/4f2c46c35ccd17ef726c4b4e8e9500d7972e757a
DIFF: https://github.com/llvm/llvm-project/commit/4f2c46c35ccd17ef726c4b4e8e9500d7972e757a.diff
LOG: Print C-string literals in mapfile
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
```
Reviewed By: #lld-macho, int3
Differential Revision: https://reviews.llvm.org/D118077
Added:
Modified:
lld/MachO/MapFile.cpp
lld/test/MachO/map-file.s
Removed:
################################################################################
diff --git a/lld/MachO/MapFile.cpp b/lld/MachO/MapFile.cpp
index 8f9381ff0d792..a4a0065c28165 100644
--- a/lld/MachO/MapFile.cpp
+++ b/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,27 @@ getSymbolStrings(ArrayRef<Defined *> syms) {
std::vector<std::string> str(syms.size());
parallelForEachN(0, syms.size(), [&](size_t i) {
raw_string_ostream os(str[i]);
- os << toString(*syms[i]);
+ Defined *sym = syms[i];
+
+ switch (sym->isec->kind()) {
+ case InputSection::CStringLiteralKind: {
+ // Output "literal string: <string literal>"
+ const auto *isec = cast<CStringInputSection>(sym->isec);
+ const StringPiece &piece = isec->getStringPiece(sym->value);
+ assert(
+ sym->value == piece.inSecOff &&
+ "We expect symbols to always point to the start of a StringPiece.");
+ StringRef str = isec->getStringRef(&piece - &(*isec->pieces.begin()));
+ assert(str.back() == '\000');
+ (os << "literal string: ")
+ // Remove null sequence at the end
+ .write_escaped(str.substr(0, str.size() - 1));
+ break;
+ }
+ case InputSection::ConcatKind:
+ case InputSection::WordLiteralKind:
+ os << toString(*sym);
+ }
});
DenseMap<Symbol *, std::string> ret;
diff --git a/lld/test/MachO/map-file.s b/lld/test/MachO/map-file.s
index 85c23e763e9ef..aa0bc6a8caf1b 100644
--- a/lld/test/MachO/map-file.s
+++ b/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,42 @@ _main:
# 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: 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
+
+# RUN: %lld -dead_strip -map %t/dead-c-string-literal-map %t/c-string-literal.o -o %t/dead-c-string-literal-out
+# RUN: FileCheck --check-prefix=DEADCSTRING %s < %t/dead-c-string-literal-map
+
+## C-string literals should be printed as "literal string: <C string literal>"
+# DEADCSTRING-LABEL: Symbols:
+# DEADCSTRING-DAG: _main
+# DEADCSTRING-DAG: literal string: Hello world!\n
+# DEADCSTRING-LABEL: Dead Stripped Symbols:
+# DEADCSTRING-DAG: literal string: Hello, it's me
+
# MAPFILE: "name":"Total Write map file"
More information about the llvm-commits
mailing list