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
137 lines
4.3 KiB
C++
137 lines
4.3 KiB
C++
//===- MapFile.cpp --------------------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file implements the -map option. It shows lists in order and
|
|
// hierarchically the outputFile, arch, input files, output sections and
|
|
// symbol:
|
|
//
|
|
// # Path: test
|
|
// # Arch: x86_84
|
|
// # Object files:
|
|
// [ 0] linker synthesized
|
|
// [ 1] a.o
|
|
// # Sections:
|
|
// # Address Size Segment Section
|
|
// 0x1000005C0 0x0000004C __TEXT __text
|
|
// # Symbols:
|
|
// # Address File Name
|
|
// 0x1000005C0 [ 1] _main
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "MapFile.h"
|
|
#include "Config.h"
|
|
#include "InputFiles.h"
|
|
#include "InputSection.h"
|
|
#include "OutputSection.h"
|
|
#include "OutputSegment.h"
|
|
#include "Symbols.h"
|
|
#include "Target.h"
|
|
#include "llvm/Support/Parallel.h"
|
|
#include "llvm/Support/TimeProfiler.h"
|
|
|
|
using namespace llvm;
|
|
using namespace llvm::sys;
|
|
using namespace lld;
|
|
using namespace lld::macho;
|
|
|
|
// Returns a list of all symbols that we want to print out.
|
|
static std::vector<Defined *> getSymbols() {
|
|
std::vector<Defined *> v;
|
|
for (InputFile *file : inputFiles)
|
|
if (isa<ObjFile>(file))
|
|
for (Symbol *sym : file->symbols)
|
|
if (auto *d = dyn_cast_or_null<Defined>(sym))
|
|
if (d->isLive() && d->isec && d->getFile() == file) {
|
|
assert(!shouldOmitFromOutput(d->isec));
|
|
v.push_back(d);
|
|
}
|
|
return v;
|
|
}
|
|
|
|
// Construct a map from symbols to their stringified representations.
|
|
// Demangling symbols (which is what toString() does) is slow, so
|
|
// we do that in batch using parallel-for.
|
|
static DenseMap<Symbol *, std::string>
|
|
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]);
|
|
});
|
|
|
|
DenseMap<Symbol *, std::string> ret;
|
|
for (size_t i = 0, e = syms.size(); i < e; ++i)
|
|
ret[syms[i]] = std::move(str[i]);
|
|
return ret;
|
|
}
|
|
|
|
void macho::writeMapFile() {
|
|
if (config->mapFile.empty())
|
|
return;
|
|
|
|
TimeTraceScope timeScope("Write map file");
|
|
|
|
// Open a map file for writing.
|
|
std::error_code ec;
|
|
raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None);
|
|
if (ec) {
|
|
error("cannot open " + config->mapFile + ": " + ec.message());
|
|
return;
|
|
}
|
|
|
|
// Dump output path.
|
|
os << format("# Path: %s\n", config->outputFile.str().c_str());
|
|
|
|
// Dump output architecture.
|
|
os << format("# Arch: %s\n",
|
|
getArchitectureName(config->arch()).str().c_str());
|
|
|
|
// Dump table of object files.
|
|
os << "# Object files:\n";
|
|
os << format("[%3u] %s\n", 0, (const char *)"linker synthesized");
|
|
uint32_t fileIndex = 1;
|
|
DenseMap<lld::macho::InputFile *, uint32_t> readerToFileOrdinal;
|
|
for (InputFile *file : inputFiles) {
|
|
if (isa<ObjFile>(file)) {
|
|
os << format("[%3u] %s\n", fileIndex, file->getName().str().c_str());
|
|
readerToFileOrdinal[file] = fileIndex++;
|
|
}
|
|
}
|
|
|
|
// Collect symbol info that we want to print out.
|
|
std::vector<Defined *> syms = getSymbols();
|
|
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);
|
|
|
|
// Dump table of sections
|
|
os << "# Sections:\n";
|
|
os << "# Address\tSize \tSegment\tSection\n";
|
|
for (OutputSegment *seg : outputSegments)
|
|
for (OutputSection *osec : seg->getSections()) {
|
|
if (osec->isHidden())
|
|
continue;
|
|
|
|
os << format("0x%08llX\t0x%08llX\t%s\t%s\n", osec->addr, osec->getSize(),
|
|
seg->name.str().c_str(), osec->name.str().c_str());
|
|
}
|
|
|
|
// Dump table of symbols
|
|
os << "# Symbols:\n";
|
|
os << "# Address\t File Name\n";
|
|
for (Symbol *sym : syms) {
|
|
os << format("0x%08llX\t[%3u] %s\n", sym->getVA(),
|
|
readerToFileOrdinal[sym->getFile()], symStr[sym].c_str());
|
|
}
|
|
|
|
// TODO: when we implement -dead_strip, we should dump dead stripped symbols
|
|
}
|