diff --git a/lld/MachO/MapFile.cpp b/lld/MachO/MapFile.cpp --- 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,24 @@ std::vector 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: " + const auto *isec = &cast(*sym->isec); + const StringPiece &piece = isec->getStringPiece(sym->value); + 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 ret; diff --git a/lld/test/MachO/map-file.s b/lld/test/MachO/map-file.s --- 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 @@ # 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: " +# 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: " +# 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"