Skip to content

Commit 4f2c46c

Browse files
Roger Kimint3
authored andcommitted
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
1 parent 6759cdd commit 4f2c46c

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

lld/MachO/MapFile.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "OutputSection.h"
3232
#include "OutputSegment.h"
3333
#include "Symbols.h"
34+
#include "SyntheticSections.h"
3435
#include "Target.h"
3536
#include "llvm/Support/Parallel.h"
3637
#include "llvm/Support/TimeProfiler.h"
@@ -76,7 +77,27 @@ getSymbolStrings(ArrayRef<Defined *> syms) {
7677
std::vector<std::string> str(syms.size());
7778
parallelForEachN(0, syms.size(), [&](size_t i) {
7879
raw_string_ostream os(str[i]);
79-
os << toString(*syms[i]);
80+
Defined *sym = syms[i];
81+
82+
switch (sym->isec->kind()) {
83+
case InputSection::CStringLiteralKind: {
84+
// Output "literal string: <string literal>"
85+
const auto *isec = cast<CStringInputSection>(sym->isec);
86+
const StringPiece &piece = isec->getStringPiece(sym->value);
87+
assert(
88+
sym->value == piece.inSecOff &&
89+
"We expect symbols to always point to the start of a StringPiece.");
90+
StringRef str = isec->getStringRef(&piece - &(*isec->pieces.begin()));
91+
assert(str.back() == '\000');
92+
(os << "literal string: ")
93+
// Remove null sequence at the end
94+
.write_escaped(str.substr(0, str.size() - 1));
95+
break;
96+
}
97+
case InputSection::ConcatKind:
98+
case InputSection::WordLiteralKind:
99+
os << toString(*sym);
100+
}
80101
});
81102

82103
DenseMap<Symbol *, std::string> ret;

lld/test/MachO/map-file.s

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# RUN: rm -rf %t; split-file %s %t
33
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/foo.s -o %t/foo.o
44
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
5+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/c-string-literal.s -o %t/c-string-literal.o
56

67
# RUN: %lld -map %t/map %t/test.o %t/foo.o --time-trace -o %t/test-map
78
# RUN: llvm-objdump --syms --section-headers %t/test-map > %t/objdump
@@ -51,4 +52,42 @@ _main:
5152
# CHECK-NEXT: 0x[[#FOO]] [ 2] _foo
5253
# CHECK-NEXT: 0x[[#NUMBER]] [ 1] _number
5354

55+
#--- c-string-literal.s
56+
.section __TEXT,__cstring
57+
.globl _hello_world, _hello_its_me, _main
58+
59+
_hello_world:
60+
.asciz "Hello world!\n"
61+
62+
_hello_its_me:
63+
.asciz "Hello, it's me"
64+
65+
.text
66+
_main:
67+
movl $0x2000004, %eax # write() syscall
68+
mov $1, %rdi # stdout
69+
leaq _hello_world(%rip), %rsi
70+
mov $13, %rdx # length of str
71+
syscall
72+
ret
73+
74+
# RUN: %lld -map %t/c-string-literal-map %t/c-string-literal.o -o %t/c-string-literal-out
75+
# RUN: FileCheck --check-prefix=CSTRING %s < %t/c-string-literal-map
76+
77+
## C-string literals should be printed as "literal string: <C string literal>"
78+
# CSTRING-LABEL: Symbols:
79+
# CSTRING-DAG: _main
80+
# CSTRING-DAG: literal string: Hello world!\n
81+
# CSTRING-DAG: literal string: Hello, it's me
82+
83+
# RUN: %lld -dead_strip -map %t/dead-c-string-literal-map %t/c-string-literal.o -o %t/dead-c-string-literal-out
84+
# RUN: FileCheck --check-prefix=DEADCSTRING %s < %t/dead-c-string-literal-map
85+
86+
## C-string literals should be printed as "literal string: <C string literal>"
87+
# DEADCSTRING-LABEL: Symbols:
88+
# DEADCSTRING-DAG: _main
89+
# DEADCSTRING-DAG: literal string: Hello world!\n
90+
# DEADCSTRING-LABEL: Dead Stripped Symbols:
91+
# DEADCSTRING-DAG: literal string: Hello, it's me
92+
5493
# MAPFILE: "name":"Total Write map file"

0 commit comments

Comments
 (0)