Skip to content

Commit 38d6202

Browse files
committed
Revert "[lld-macho] Overhaul map file code"
This reverts commit 213dbdb. This patch series breaks lld:map-file.s on arm v7 linux buildbots. e.g https://lab.llvm.org/buildbot/#/builders/178/builds/3190
1 parent ac3096e commit 38d6202

File tree

2 files changed

+95
-126
lines changed

2 files changed

+95
-126
lines changed

lld/MachO/MapFile.cpp

Lines changed: 65 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This file implements the -map option, which maps address ranges to their
10-
// respective contents, plus the input file these contents were originally from.
11-
// The contents (typically symbols) are listed in address order. Dead-stripped
12-
// contents are included as well.
9+
// This file implements the -map option. It shows lists in order and
10+
// hierarchically the outputFile, arch, input files, output sections and
11+
// symbols:
1312
//
1413
// # Path: test
1514
// # Arch: x86_84
@@ -29,16 +28,15 @@
2928
//===----------------------------------------------------------------------===//
3029

3130
#include "MapFile.h"
32-
#include "ConcatOutputSection.h"
3331
#include "Config.h"
3432
#include "InputFiles.h"
3533
#include "InputSection.h"
34+
#include "OutputSection.h"
3635
#include "OutputSegment.h"
3736
#include "Symbols.h"
3837
#include "SyntheticSections.h"
3938
#include "Target.h"
4039
#include "lld/Common/ErrorHandler.h"
41-
#include "llvm/ADT/DenseMap.h"
4240
#include "llvm/Support/Parallel.h"
4341
#include "llvm/Support/TimeProfiler.h"
4442

@@ -47,77 +45,71 @@ using namespace llvm::sys;
4745
using namespace lld;
4846
using namespace lld::macho;
4947

50-
struct CStringInfo {
51-
uint32_t fileIndex;
52-
StringRef str;
53-
};
54-
5548
struct MapInfo {
5649
SmallVector<InputFile *> files;
50+
SmallVector<Defined *> liveSymbols;
5751
SmallVector<Defined *> deadSymbols;
58-
DenseMap<const OutputSection *,
59-
SmallVector<std::pair<uint64_t /*addr*/, CStringInfo>>>
60-
liveCStringsForSection;
61-
SmallVector<CStringInfo> deadCStrings;
6252
};
6353

6454
static MapInfo gatherMapInfo() {
6555
MapInfo info;
6656
for (InputFile *file : inputFiles)
6757
if (isa<ObjFile>(file) || isa<BitcodeFile>(file)) {
68-
uint32_t fileIndex = info.files.size() + 1;
69-
bool isReferencedFile = false;
70-
71-
// Gather the dead symbols. We don't have to bother with the live ones
72-
// because we will pick them up as we iterate over the OutputSections
73-
// later.
58+
bool hasEmittedSymbol = false;
7459
for (Symbol *sym : file->symbols) {
7560
if (auto *d = dyn_cast_or_null<Defined>(sym))
76-
// Only emit the prevailing definition of a symbol. Also, don't emit
77-
// the symbol if it is part of a cstring section (we use the literal
78-
// value instead, similar to ld64)
79-
if (d->isec && d->getFile() == file &&
80-
!isa<CStringInputSection>(d->isec)) {
81-
isReferencedFile = true;
82-
if (!d->isLive())
61+
if (d->isec && d->getFile() == file) {
62+
if (d->isLive()) {
63+
assert(!shouldOmitFromOutput(d->isec));
64+
info.liveSymbols.push_back(d);
65+
} else {
8366
info.deadSymbols.push_back(d);
84-
}
85-
}
86-
87-
// Gather all the cstrings (both live and dead). A CString(Output)Section
88-
// doesn't provide us a way of figuring out which InputSections its
89-
// cstring contents came from, so we need to build up that mapping here.
90-
for (const Section *sec : file->sections) {
91-
for (const Subsection &subsec : sec->subsections) {
92-
if (auto isec = dyn_cast<CStringInputSection>(subsec.isec)) {
93-
auto &liveCStrings = info.liveCStringsForSection[isec->parent];
94-
for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
95-
if (piece.live)
96-
liveCStrings.push_back({isec->parent->addr + piece.outSecOff,
97-
{fileIndex, isec->getStringRef(i)}});
98-
else
99-
info.deadCStrings.push_back({fileIndex, isec->getStringRef(i)});
100-
isReferencedFile = true;
10167
}
102-
} else {
103-
break;
68+
hasEmittedSymbol = true;
10469
}
105-
}
10670
}
107-
108-
if (isReferencedFile)
71+
if (hasEmittedSymbol)
10972
info.files.push_back(file);
11073
}
111-
112-
// cstrings are not stored in sorted order in their OutputSections, so we sort
113-
// them here.
114-
for (auto &liveCStrings : info.liveCStringsForSection)
115-
parallelSort(liveCStrings.second, [](const auto &p1, const auto &p2) {
116-
return p1.first < p2.first;
117-
});
74+
parallelSort(info.liveSymbols.begin(), info.liveSymbols.end(),
75+
[](Defined *a, Defined *b) { return a->getVA() < b->getVA(); });
11876
return info;
11977
}
12078

79+
// Construct a map from symbols to their stringified representations.
80+
// Demangling symbols (which is what toString() does) is slow, so
81+
// we do that in batch using parallel-for.
82+
static DenseMap<Symbol *, std::string>
83+
getSymbolStrings(ArrayRef<Defined *> syms) {
84+
std::vector<std::string> str(syms.size());
85+
parallelFor(0, syms.size(), [&](size_t i) {
86+
raw_string_ostream os(str[i]);
87+
Defined *sym = syms[i];
88+
89+
switch (sym->isec->kind()) {
90+
case InputSection::CStringLiteralKind: {
91+
// Output "literal string: <string literal>"
92+
const auto *isec = cast<CStringInputSection>(sym->isec);
93+
const StringPiece &piece = isec->getStringPiece(sym->value);
94+
assert(
95+
sym->value == piece.inSecOff &&
96+
"We expect symbols to always point to the start of a StringPiece.");
97+
StringRef str = isec->getStringRef(&piece - &(*isec->pieces.begin()));
98+
(os << "literal string: ").write_escaped(str);
99+
break;
100+
}
101+
case InputSection::ConcatKind:
102+
case InputSection::WordLiteralKind:
103+
os << toString(*sym);
104+
}
105+
});
106+
107+
DenseMap<Symbol *, std::string> ret;
108+
for (size_t i = 0, e = syms.size(); i < e; ++i)
109+
ret[syms[i]] = std::move(str[i]);
110+
return ret;
111+
}
112+
121113
void macho::writeMapFile() {
122114
if (config->mapFile.empty())
123115
return;
@@ -132,12 +124,16 @@ void macho::writeMapFile() {
132124
return;
133125
}
134126

127+
// Dump output path.
135128
os << format("# Path: %s\n", config->outputFile.str().c_str());
129+
130+
// Dump output architecture.
136131
os << format("# Arch: %s\n",
137132
getArchitectureName(config->arch()).str().c_str());
138133

139134
MapInfo info = gatherMapInfo();
140135

136+
// Dump table of object files.
141137
os << "# Object files:\n";
142138
os << format("[%3u] %s\n", 0, (const char *)"linker synthesized");
143139
uint32_t fileIndex = 1;
@@ -147,6 +143,7 @@ void macho::writeMapFile() {
147143
readerToFileOrdinal[file] = fileIndex++;
148144
}
149145

146+
// Dump table of sections
150147
os << "# Sections:\n";
151148
os << "# Address\tSize \tSegment\tSection\n";
152149
for (OutputSegment *seg : outputSegments)
@@ -158,48 +155,28 @@ void macho::writeMapFile() {
158155
seg->name.str().c_str(), osec->name.str().c_str());
159156
}
160157

158+
// Dump table of symbols
159+
DenseMap<Symbol *, std::string> liveSymbolStrings =
160+
getSymbolStrings(info.liveSymbols);
161161
os << "# Symbols:\n";
162162
os << "# Address\tSize \tFile Name\n";
163-
for (const OutputSegment *seg : outputSegments) {
164-
for (const OutputSection *osec : seg->getSections()) {
165-
if (auto *concatOsec = dyn_cast<ConcatOutputSection>(osec)) {
166-
for (const InputSection *isec : concatOsec->inputs) {
167-
for (Defined *sym : isec->symbols)
168-
os << format("0x%08llX\t0x%08llX\t[%3u] %s\n", sym->getVA(),
169-
sym->size, readerToFileOrdinal[sym->getFile()],
170-
sym->getName().str().data());
171-
}
172-
} else if (osec == in.cStringSection || osec == in.objcMethnameSection) {
173-
const auto &liveCStrings = info.liveCStringsForSection.lookup(osec);
174-
uint64_t lastAddr = 0; // strings will never start at address 0, so this
175-
// is a sentinel value
176-
for (const auto &[addr, info] : liveCStrings) {
177-
uint64_t size = 0;
178-
if (addr != lastAddr)
179-
size = info.str.size() + 1; // include null terminator
180-
lastAddr = addr;
181-
os << format("0x%08llX\t0x%08llX\t[%3u] literal string: ", addr, size,
182-
info.fileIndex);
183-
os.write_escaped(info.str) << "\n";
184-
}
185-
}
186-
// TODO print other synthetic sections
187-
}
163+
for (Defined *sym : info.liveSymbols) {
164+
assert(sym->isLive());
165+
os << format("0x%08llX\t0x%08llX\t[%3u] %s\n", sym->getVA(), sym->size,
166+
readerToFileOrdinal[sym->getFile()],
167+
liveSymbolStrings[sym].c_str());
188168
}
189169

190170
if (config->deadStrip) {
171+
DenseMap<Symbol *, std::string> deadSymbolStrings =
172+
getSymbolStrings(info.deadSymbols);
191173
os << "# Dead Stripped Symbols:\n";
192174
os << "# \tSize \tFile Name\n";
193175
for (Defined *sym : info.deadSymbols) {
194176
assert(!sym->isLive());
195177
os << format("<<dead>>\t0x%08llX\t[%3u] %s\n", sym->size,
196178
readerToFileOrdinal[sym->getFile()],
197-
sym->getName().str().data());
198-
}
199-
for (CStringInfo &cstrInfo : info.deadCStrings) {
200-
os << format("<<dead>>\t0x%08llX\t[%3u] literal string: ",
201-
cstrInfo.str.size() + 1, cstrInfo.fileIndex);
202-
os.write_escaped(cstrInfo.str) << "\n";
179+
deadSymbolStrings[sym].c_str());
203180
}
204181
}
205182
}

lld/test/MachO/map-file.s

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@
44
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/test.s -o %t/test.o
55
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %t/c-string-literal.s -o %t/c-string-literal.o
66

7-
# RUN: %lld -demangle -map %t/map %t/test.o %t/foo.o %t/c-string-literal.o \
8-
# RUN: --time-trace -o %t/test
7+
# RUN: %lld -map %t/map %t/test.o %t/foo.o %t/c-string-literal.o --time-trace -o %t/test
98
# RUN: llvm-objdump --syms --section-headers %t/test > %t/objdump
10-
## Check that symbols in cstring sections aren't emitted
11-
# RUN: cat %t/objdump %t/map | FileCheck %s --implicit-check-not _hello_world
9+
# RUN: cat %t/objdump %t/map > %t/out
10+
# RUN: FileCheck %s < %t/out
1211
# RUN: FileCheck %s --check-prefix=MAPFILE < %t/test.time-trace
1312

1413
# CHECK: Sections:
15-
# CHECK-NEXT: Idx Name Size VMA Type
16-
# CHECK-NEXT: 0 __text {{[0-9a-f]+}} [[#%x,TEXT:]] TEXT
17-
# CHECK-NEXT: 1 __cstring {{[0-9a-f]+}} [[#%x,CSTR:]] DATA
18-
# CHECK-NEXT: 2 __common {{[0-9a-f]+}} [[#%x,BSS:]] BSS
14+
# CHECK-NEXT: Idx Name Size VMA Type
15+
# CHECK-NEXT: 0 __text {{[0-9a-f]+}} [[#%x,TEXT:]] TEXT
16+
# CHECK-NEXT: 1 obj {{[0-9a-f]+}} [[#%x,DATA:]] TEXT
17+
# CHECK-NEXT: 2 __cstring {{[0-9a-f]+}} [[#%x,CSTR:]] DATA
18+
# CHECK-NEXT: 3 __common {{[0-9a-f]+}} [[#%x,BSS:]] BSS
1919

2020
# CHECK: SYMBOL TABLE:
2121
# CHECK-DAG: [[#%x,MAIN:]] g F __TEXT,__text _main
2222
# CHECK-DAG: [[#%x,NUMBER:]] g O __DATA,__common _number
23-
# CHECK-DAG: [[#%x,BAR:]] g F __TEXT,__text _bar
24-
# CHECK-DAG: [[#%x,FOO:]] g F __TEXT,__text __ZTIN3foo3bar4MethE
23+
# CHECK-DAG: [[#%x,FOO:]] g F __TEXT,obj _foo
2524
# CHECK-DAG: [[#%x,HIWORLD:]] g O __TEXT,__cstring _hello_world
2625
# CHECK-DAG: [[#%x,HIITSME:]] g O __TEXT,__cstring _hello_its_me
2726

@@ -36,61 +35,56 @@
3635
# CHECK-NEXT: # Sections:
3736
# CHECK-NEXT: # Address Size Segment Section
3837
# CHECK-NEXT: 0x[[#%X,TEXT]] 0x{{[0-9A-F]+}} __TEXT __text
38+
# CHECK-NEXT: 0x[[#%X,DATA]] 0x{{[0-9A-F]+}} __TEXT obj
3939
# CHECK-NEXT: 0x[[#%X,CSTR]] 0x{{[0-9A-F]+}} __TEXT __cstring
4040
# CHECK-NEXT: 0x[[#%X,BSS]] 0x{{[0-9A-F]+}} __DATA __common
4141

4242
# CHECK-NEXT: # Symbols:
43-
# CHECK-NEXT: # Address Size File Name
44-
# CHECK-DAG: 0x[[#%X,MAIN]] 0x00000001 [ 1] _main
45-
# CHECK-DAG: 0x[[#%X,BAR]] 0x00000001 [ 1] _bar
46-
# CHECK-DAG: 0x[[#%X,FOO]] 0x00000001 [ 2] __ZTIN3foo3bar4MethE
47-
# CHECK-DAG: 0x[[#%X,HIWORLD]] 0x0000000E [ 3] literal string: Hello world!\n
48-
# CHECK-DAG: 0x[[#%X,HIITSME]] 0x0000000F [ 3] literal string: Hello, it's me
49-
# CHECK-DAG: 0x[[#%X,HIITSME + 0xf]] 0x0000000E [ 3] literal string: Hello world!\n
50-
# CHECK-DAG: 0x[[#%X,NUMBER]] 0x00000001 [ 1] _number
43+
# CHECK-NEXT: # Address Size File Name
44+
# CHECK-DAG: 0x[[#%X,MAIN]] 0x00000001 [ 1] _main
45+
# CHECK-DAG: 0x[[#%X,FOO]] 0x00000001 [ 2] _foo
46+
# CHECK-DAG: 0x[[#%X,HIWORLD]] 0x0000000E [ 3] literal string: Hello world!\n
47+
# CHECK-DAG: 0x[[#%X,HIITSME]] 0x0000000F [ 3] literal string: Hello, it's me
48+
# CHECK-DAG: 0x[[#%X,NUMBER]] 0x00000001 [ 1] _number
5149

5250
# MAPFILE: "name":"Total Write map file"
5351

54-
# RUN: %lld -demangle -dead_strip -map %t/stripped-map %t/test.o %t/foo.o %t/c-string-literal.o -o %t/stripped
52+
# RUN: %lld -dead_strip -map %t/stripped-map %t/test.o %t/foo.o %t/c-string-literal.o -o %t/stripped
5553
# RUN: FileCheck --check-prefix=STRIPPED %s < %t/stripped-map
5654

5755
## C-string literals should be printed as "literal string: <C string literal>"
5856
# STRIPPED-LABEL: Dead Stripped Symbols:
59-
# STRIPPED-DAG: <<dead>> 0x00000001 [ 1] _bar
60-
# STRIPPED-DAG: <<dead>> 0x00000001 [ 1] _number
61-
# STRIPPED-DAG: <<dead>> 0x00000001 [ 2] __ZTIN3foo3bar4MethE
62-
# STRIPPED-DAG: <<dead>> 0x0000000E [ 3] literal string: Hello world!\n
63-
# STRIPPED-DAG: <<dead>> 0x0000000F [ 3] literal string: Hello, it's me
64-
# STRIPPED-DAG: <<dead>> 0x0000000E [ 3] literal string: Hello world!\n
57+
# STRIPPED-DAG: <<dead>> 0x00000001 [ 2] _foo
58+
# STRIPPED-DAG: <<dead>> 0x0000000E [ 3] literal string: Hello world!\n
59+
# STRIPPED-DAG: <<dead>> 0x0000000F [ 3] literal string: Hello, it's me
60+
# STRIPPED-DAG: <<dead>> 0x00000001 [ 1] _number
6561

6662
# RUN: %lld --icf=all -map %t/icf-map %t/test.o %t/foo.o %t/c-string-literal.o -o %t/icf
6763
# RUN: FileCheck --check-prefix=ICF %s < %t/icf-map
6864

69-
## Verify that folded symbols and cstrings have size zero. Note that ld64 prints
70-
## folded symbols but not folded cstrings; we print both.
71-
7265
# ICF: Symbols:
73-
# ICF-DAG: 0x[[#%X,FOO:]] 0x00000000 [ 2] __ZTIN3foo3bar4MethE
74-
# ICF-DAG: 0x[[#FOO]] 0x00000001 [ 1] _bar
75-
# ICF-DAG: 0x[[#%X,HIWORLD:]] 0x0000000E [ 3] literal string: Hello world!\n
76-
# ICF-DAG: 0x[[#%X,HIWORLD]] 0x00000000 [ 3] literal string: Hello world!\n
66+
# ICF-DAG: 0x[[#%X,FOO:]] 0x00000000 [ 2] _foo
67+
# ICF-DAG: 0x[[#FOO]] 0x00000001 [ 1] _bar
7768

7869
#--- foo.s
79-
.globl __ZTIN3foo3bar4MethE
80-
## This C++ symbol makes it clear that we do not print the demangled name in
81-
## the map file, even if `-demangle` is passed.
82-
__ZTIN3foo3bar4MethE:
70+
## ICF will only fold sections marked as pure_instructions
71+
.section __TEXT,obj,regular,pure_instructions
72+
.globl _foo
73+
.alt_entry _alt_foo
74+
_foo:
8375
nop
8476

8577
.subsections_via_symbols
8678

8779
#--- test.s
8880
.comm _number, 1
8981
.globl _main, _bar
82+
.alt_entry _alt_bar
9083

9184
_main:
9285
ret
9386

87+
.section __TEXT,obj,regular,pure_instructions
9488
_bar:
9589
nop
9690

@@ -107,6 +101,4 @@ _hello_world:
107101
_hello_its_me:
108102
.asciz "Hello, it's me"
109103

110-
.asciz "Hello world!\n"
111-
112104
.subsections_via_symbols

0 commit comments

Comments
 (0)