[lld][macho] Stop grouping symbols by sections in mapfile.

As per [Bug 50689](https://bugs.llvm.org/show_bug.cgi?id=50689),

```
2. getSectionSyms() puts all the symbols into a map of section -> symbols, but this seems unnecessary. This was likely copied from the ELF port, which prints a section header before the list of symbols it contains. But the Mach-O map file doesn't print these headers.
```

This diff removes `getSectionSyms()` and keeps all symbols in a flat vector.

What does ld64's mapfile look like?
```
$ llvm-mc -filetype=obj -triple=x86_64-apple-darwin test.s -o test.o
$ llvm-mc -filetype=obj -triple=x86_64-apple-darwin foo.s -o foo.o
$ ld -map map test.o foo.o -o out -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lSystem
```

```
[  0] linker synthesized
[  1] test.o
[  2] foo.o
0x100003FB7     0x00000001      __TEXT  __text
0x100003FB8     0x00000000      __TEXT  obj
0x100003FB8     0x00000048      __TEXT  __unwind_info
0x100004000     0x00000001      __DATA  __common
0x100003FB7     0x00000001      [  1] _main
0x100003FB8     0x00000000      [  2] _foo
0x100003FB8     0x00000048      [  0] compact unwind info
0x100004000     0x00000001      [  1] _number
```

Perf numbers when linking chromium framework on a 16-Core Intel Xeon W Mac Pro:
```
base           diff           difference (95% CI)
sys_time   1.406 ± 0.020  1.388 ± 0.019  [  -1.9% ..   -0.6%]
user_time  5.557 ± 0.023  5.914 ± 0.020  [  +6.2% ..   +6.6%]
wall_time  4.455 ± 0.041  4.436 ± 0.035  [  -0.8% ..   -0.0%]
samples    35             35
```

Reviewed By: #lld-macho, int3

Differential Revision: https://reviews.llvm.org/D114735
This commit is contained in:
Roger Kim 2022-01-20 12:13:04 -08:00 committed by Jez Ng
parent 82452be5cb
commit f84023a812
2 changed files with 8 additions and 31 deletions

View File

@ -40,26 +40,6 @@ using namespace llvm::sys;
using namespace lld; using namespace lld;
using namespace lld::macho; using namespace lld::macho;
using SymbolMapTy = DenseMap<const InputSection *, SmallVector<Defined *, 4>>;
// Returns a map from sections to their symbols.
static SymbolMapTy getSectionSyms(ArrayRef<Defined *> syms) {
SymbolMapTy ret;
for (Defined *dr : syms)
ret[dr->isec].push_back(dr);
// Sort symbols by address. We want to print out symbols in the order they
// appear in the output file rather than the order they appeared in the input
// files.
for (auto &it : ret)
parallelSort(
it.second.begin(), it.second.end(), [](Defined *a, Defined *b) {
return a->getVA() != b->getVA() ? a->getVA() < b->getVA()
: a->getName() < b->getName();
});
return ret;
}
// Returns a list of all symbols that we want to print out. // Returns a list of all symbols that we want to print out.
static std::vector<Defined *> getSymbols() { static std::vector<Defined *> getSymbols() {
std::vector<Defined *> v; std::vector<Defined *> v;
@ -126,7 +106,10 @@ void macho::writeMapFile() {
// Collect symbol info that we want to print out. // Collect symbol info that we want to print out.
std::vector<Defined *> syms = getSymbols(); std::vector<Defined *> syms = getSymbols();
SymbolMapTy sectionSyms = getSectionSyms(syms); parallelSort(syms.begin(), syms.end(), [](Defined *a, Defined *b) {
return a->getVA() != b->getVA() ? a->getVA() < b->getVA()
: a->getName() < b->getName();
});
DenseMap<Symbol *, std::string> symStr = getSymbolStrings(syms); DenseMap<Symbol *, std::string> symStr = getSymbolStrings(syms);
// Dump table of sections // Dump table of sections
@ -144,16 +127,10 @@ void macho::writeMapFile() {
// Dump table of symbols // Dump table of symbols
os << "# Symbols:\n"; os << "# Symbols:\n";
os << "# Address\t File Name\n"; os << "# Address\t File Name\n";
for (InputSection *isec : inputSections) { for (Symbol *sym : syms) {
auto symsIt = sectionSyms.find(isec);
assert(!shouldOmitFromOutput(isec) || (symsIt == sectionSyms.end()));
if (symsIt == sectionSyms.end())
continue;
for (Symbol *sym : symsIt->second) {
os << format("0x%08llX\t[%3u] %s\n", sym->getVA(), os << format("0x%08llX\t[%3u] %s\n", sym->getVA(),
readerToFileOrdinal[sym->getFile()], symStr[sym].c_str()); readerToFileOrdinal[sym->getFile()], symStr[sym].c_str());
} }
}
// TODO: when we implement -dead_strip, we should dump dead stripped symbols // TODO: when we implement -dead_strip, we should dump dead stripped symbols
} }

View File

@ -47,8 +47,8 @@ _main:
# CHECK-NEXT: # Symbols: # CHECK-NEXT: # Symbols:
# CHECK-NEXT: # Address File Name # CHECK-NEXT: # Address File Name
# CHECK-NEXT: 0x[[#NUMBER]] [ 1] _number
# CHECK-NEXT: 0x[[#MAIN]] [ 1] _main # CHECK-NEXT: 0x[[#MAIN]] [ 1] _main
# CHECK-NEXT: 0x[[#FOO]] [ 2] _foo # CHECK-NEXT: 0x[[#FOO]] [ 2] _foo
# CHECK-NEXT: 0x[[#NUMBER]] [ 1] _number
# MAPFILE: "name":"Total Write map file" # MAPFILE: "name":"Total Write map file"