1344a3780SDimitry Andric //===- MapFile.cpp --------------------------------------------------------===//
2344a3780SDimitry Andric //
3344a3780SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4344a3780SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5344a3780SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6344a3780SDimitry Andric //
7344a3780SDimitry Andric //===----------------------------------------------------------------------===//
8344a3780SDimitry Andric //
9e3b55780SDimitry Andric // This file implements the -map option, which maps address ranges to their
10e3b55780SDimitry Andric // respective contents, plus the input file these contents were originally from.
11e3b55780SDimitry Andric // The contents (typically symbols) are listed in address order. Dead-stripped
12e3b55780SDimitry Andric // contents are included as well.
13344a3780SDimitry Andric //
14344a3780SDimitry Andric // # Path: test
15344a3780SDimitry Andric // # Arch: x86_84
16344a3780SDimitry Andric // # Object files:
17344a3780SDimitry Andric // [ 0] linker synthesized
18344a3780SDimitry Andric // [ 1] a.o
19344a3780SDimitry Andric // # Sections:
20344a3780SDimitry Andric // # Address Size Segment Section
21344a3780SDimitry Andric // 0x1000005C0 0x0000004C __TEXT __text
22344a3780SDimitry Andric // # Symbols:
23e3b55780SDimitry Andric // # Address Size File Name
24e3b55780SDimitry Andric // 0x1000005C0 0x00000001 [ 1] _main
25e3b55780SDimitry Andric // # Dead Stripped Symbols:
26e3b55780SDimitry Andric // # Size File Name
27e3b55780SDimitry Andric // <<dead>> 0x00000001 [ 1] _foo
28344a3780SDimitry Andric //
29344a3780SDimitry Andric //===----------------------------------------------------------------------===//
30344a3780SDimitry Andric
31344a3780SDimitry Andric #include "MapFile.h"
32e3b55780SDimitry Andric #include "ConcatOutputSection.h"
33344a3780SDimitry Andric #include "Config.h"
34344a3780SDimitry Andric #include "InputFiles.h"
35344a3780SDimitry Andric #include "InputSection.h"
36344a3780SDimitry Andric #include "OutputSegment.h"
37344a3780SDimitry Andric #include "Symbols.h"
38145449b1SDimitry Andric #include "SyntheticSections.h"
39344a3780SDimitry Andric #include "Target.h"
40e3b55780SDimitry Andric #include "lld/Common/ErrorHandler.h"
41e3b55780SDimitry Andric #include "llvm/ADT/DenseMap.h"
42344a3780SDimitry Andric #include "llvm/Support/Parallel.h"
43344a3780SDimitry Andric #include "llvm/Support/TimeProfiler.h"
44344a3780SDimitry Andric
45344a3780SDimitry Andric using namespace llvm;
46344a3780SDimitry Andric using namespace llvm::sys;
47344a3780SDimitry Andric using namespace lld;
48344a3780SDimitry Andric using namespace lld::macho;
49344a3780SDimitry Andric
50e3b55780SDimitry Andric struct CStringInfo {
51e3b55780SDimitry Andric uint32_t fileIndex;
52e3b55780SDimitry Andric StringRef str;
53e3b55780SDimitry Andric };
54e3b55780SDimitry Andric
55e3b55780SDimitry Andric struct MapInfo {
56e3b55780SDimitry Andric SmallVector<InputFile *> files;
57e3b55780SDimitry Andric SmallVector<Defined *> deadSymbols;
58e3b55780SDimitry Andric DenseMap<const OutputSection *,
59e3b55780SDimitry Andric SmallVector<std::pair<uint64_t /*addr*/, CStringInfo>>>
60e3b55780SDimitry Andric liveCStringsForSection;
61e3b55780SDimitry Andric SmallVector<CStringInfo> deadCStrings;
62e3b55780SDimitry Andric };
63e3b55780SDimitry Andric
gatherMapInfo()64e3b55780SDimitry Andric static MapInfo gatherMapInfo() {
65e3b55780SDimitry Andric MapInfo info;
66e3b55780SDimitry Andric for (InputFile *file : inputFiles) {
67e3b55780SDimitry Andric bool isReferencedFile = false;
68e3b55780SDimitry Andric
69e3b55780SDimitry Andric if (isa<ObjFile>(file) || isa<BitcodeFile>(file)) {
70e3b55780SDimitry Andric uint32_t fileIndex = info.files.size() + 1;
71e3b55780SDimitry Andric
72e3b55780SDimitry Andric // Gather the dead symbols. We don't have to bother with the live ones
73e3b55780SDimitry Andric // because we will pick them up as we iterate over the OutputSections
74e3b55780SDimitry Andric // later.
75e3b55780SDimitry Andric for (Symbol *sym : file->symbols) {
76344a3780SDimitry Andric if (auto *d = dyn_cast_or_null<Defined>(sym))
77e3b55780SDimitry Andric // Only emit the prevailing definition of a symbol. Also, don't emit
78e3b55780SDimitry Andric // the symbol if it is part of a cstring section (we use the literal
79e3b55780SDimitry Andric // value instead, similar to ld64)
80ac9a064cSDimitry Andric if (d->isec() && d->getFile() == file &&
81ac9a064cSDimitry Andric !isa<CStringInputSection>(d->isec())) {
82e3b55780SDimitry Andric isReferencedFile = true;
83e3b55780SDimitry Andric if (!d->isLive())
84e3b55780SDimitry Andric info.deadSymbols.push_back(d);
85e3b55780SDimitry Andric }
86e3b55780SDimitry Andric }
87e3b55780SDimitry Andric
88e3b55780SDimitry Andric // Gather all the cstrings (both live and dead). A CString(Output)Section
89e3b55780SDimitry Andric // doesn't provide us a way of figuring out which InputSections its
90e3b55780SDimitry Andric // cstring contents came from, so we need to build up that mapping here.
91e3b55780SDimitry Andric for (const Section *sec : file->sections) {
92e3b55780SDimitry Andric for (const Subsection &subsec : sec->subsections) {
93e3b55780SDimitry Andric if (auto isec = dyn_cast<CStringInputSection>(subsec.isec)) {
94e3b55780SDimitry Andric auto &liveCStrings = info.liveCStringsForSection[isec->parent];
95e3b55780SDimitry Andric for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
96e3b55780SDimitry Andric if (piece.live)
97e3b55780SDimitry Andric liveCStrings.push_back({isec->parent->addr + piece.outSecOff,
98e3b55780SDimitry Andric {fileIndex, isec->getStringRef(i)}});
99e3b55780SDimitry Andric else
100e3b55780SDimitry Andric info.deadCStrings.push_back({fileIndex, isec->getStringRef(i)});
101e3b55780SDimitry Andric isReferencedFile = true;
102e3b55780SDimitry Andric }
103ecbca9f5SDimitry Andric } else {
104145449b1SDimitry Andric break;
105145449b1SDimitry Andric }
106145449b1SDimitry Andric }
107e3b55780SDimitry Andric }
108e3b55780SDimitry Andric } else if (const auto *dylibFile = dyn_cast<DylibFile>(file)) {
109e3b55780SDimitry Andric isReferencedFile = dylibFile->isReferenced();
110e3b55780SDimitry Andric }
111344a3780SDimitry Andric
112e3b55780SDimitry Andric if (isReferencedFile)
113e3b55780SDimitry Andric info.files.push_back(file);
114e3b55780SDimitry Andric }
115e3b55780SDimitry Andric
116e3b55780SDimitry Andric // cstrings are not stored in sorted order in their OutputSections, so we sort
117e3b55780SDimitry Andric // them here.
118e3b55780SDimitry Andric for (auto &liveCStrings : info.liveCStringsForSection)
119e3b55780SDimitry Andric parallelSort(liveCStrings.second, [](const auto &p1, const auto &p2) {
120e3b55780SDimitry Andric return p1.first < p2.first;
121e3b55780SDimitry Andric });
122e3b55780SDimitry Andric return info;
123e3b55780SDimitry Andric }
124e3b55780SDimitry Andric
1257fa27ce4SDimitry Andric // We use this instead of `toString(const InputFile *)` as we don't want to
1267fa27ce4SDimitry Andric // include the dylib install name in our output.
printFileName(raw_fd_ostream & os,const InputFile * f)1277fa27ce4SDimitry Andric static void printFileName(raw_fd_ostream &os, const InputFile *f) {
1287fa27ce4SDimitry Andric if (f->archiveName.empty())
1297fa27ce4SDimitry Andric os << f->getName();
1307fa27ce4SDimitry Andric else
1317fa27ce4SDimitry Andric os << f->archiveName << "(" << path::filename(f->getName()) + ")";
1327fa27ce4SDimitry Andric }
1337fa27ce4SDimitry Andric
134e3b55780SDimitry Andric // For printing the contents of the __stubs and __la_symbol_ptr sections.
printStubsEntries(raw_fd_ostream & os,const DenseMap<lld::macho::InputFile *,uint32_t> & readerToFileOrdinal,const OutputSection * osec,size_t entrySize)1357fa27ce4SDimitry Andric static void printStubsEntries(
136e3b55780SDimitry Andric raw_fd_ostream &os,
137e3b55780SDimitry Andric const DenseMap<lld::macho::InputFile *, uint32_t> &readerToFileOrdinal,
138e3b55780SDimitry Andric const OutputSection *osec, size_t entrySize) {
139e3b55780SDimitry Andric for (const Symbol *sym : in.stubs->getEntries())
140e3b55780SDimitry Andric os << format("0x%08llX\t0x%08zX\t[%3u] %s\n",
141e3b55780SDimitry Andric osec->addr + sym->stubsIndex * entrySize, entrySize,
142e3b55780SDimitry Andric readerToFileOrdinal.lookup(sym->getFile()),
143e3b55780SDimitry Andric sym->getName().str().data());
144e3b55780SDimitry Andric }
145e3b55780SDimitry Andric
printNonLazyPointerSection(raw_fd_ostream & os,NonLazyPointerSectionBase * osec)1467fa27ce4SDimitry Andric static void printNonLazyPointerSection(raw_fd_ostream &os,
147e3b55780SDimitry Andric NonLazyPointerSectionBase *osec) {
148e3b55780SDimitry Andric // ld64 considers stubs to belong to particular files, but considers GOT
149e3b55780SDimitry Andric // entries to be linker-synthesized. Not sure why they made that decision, but
150e3b55780SDimitry Andric // I think we can follow suit unless there's demand for better symbol-to-file
151e3b55780SDimitry Andric // associations.
152e3b55780SDimitry Andric for (const Symbol *sym : osec->getEntries())
153e3b55780SDimitry Andric os << format("0x%08llX\t0x%08zX\t[ 0] non-lazy-pointer-to-local: %s\n",
154e3b55780SDimitry Andric osec->addr + sym->gotIndex * target->wordSize,
155e3b55780SDimitry Andric target->wordSize, sym->getName().str().data());
156344a3780SDimitry Andric }
157344a3780SDimitry Andric
getSymSizeForMap(Defined * sym)158ac9a064cSDimitry Andric static uint64_t getSymSizeForMap(Defined *sym) {
159ac9a064cSDimitry Andric if (sym->wasIdenticalCodeFolded)
160ac9a064cSDimitry Andric return 0;
161ac9a064cSDimitry Andric return sym->size;
162ac9a064cSDimitry Andric }
163ac9a064cSDimitry Andric
writeMapFile()164344a3780SDimitry Andric void macho::writeMapFile() {
165344a3780SDimitry Andric if (config->mapFile.empty())
166344a3780SDimitry Andric return;
167344a3780SDimitry Andric
168344a3780SDimitry Andric TimeTraceScope timeScope("Write map file");
169344a3780SDimitry Andric
170344a3780SDimitry Andric // Open a map file for writing.
171344a3780SDimitry Andric std::error_code ec;
172344a3780SDimitry Andric raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None);
173344a3780SDimitry Andric if (ec) {
174344a3780SDimitry Andric error("cannot open " + config->mapFile + ": " + ec.message());
175344a3780SDimitry Andric return;
176344a3780SDimitry Andric }
177344a3780SDimitry Andric
178344a3780SDimitry Andric os << format("# Path: %s\n", config->outputFile.str().c_str());
179344a3780SDimitry Andric os << format("# Arch: %s\n",
180344a3780SDimitry Andric getArchitectureName(config->arch()).str().c_str());
181344a3780SDimitry Andric
182e3b55780SDimitry Andric MapInfo info = gatherMapInfo();
183e3b55780SDimitry Andric
184344a3780SDimitry Andric os << "# Object files:\n";
185344a3780SDimitry Andric os << format("[%3u] %s\n", 0, (const char *)"linker synthesized");
186344a3780SDimitry Andric uint32_t fileIndex = 1;
187344a3780SDimitry Andric DenseMap<lld::macho::InputFile *, uint32_t> readerToFileOrdinal;
188e3b55780SDimitry Andric for (InputFile *file : info.files) {
1897fa27ce4SDimitry Andric os << format("[%3u] ", fileIndex);
1907fa27ce4SDimitry Andric printFileName(os, file);
1917fa27ce4SDimitry Andric os << "\n";
192344a3780SDimitry Andric readerToFileOrdinal[file] = fileIndex++;
193344a3780SDimitry Andric }
194344a3780SDimitry Andric
195344a3780SDimitry Andric os << "# Sections:\n";
196344a3780SDimitry Andric os << "# Address\tSize \tSegment\tSection\n";
197344a3780SDimitry Andric for (OutputSegment *seg : outputSegments)
198344a3780SDimitry Andric for (OutputSection *osec : seg->getSections()) {
199344a3780SDimitry Andric if (osec->isHidden())
200344a3780SDimitry Andric continue;
201344a3780SDimitry Andric
202344a3780SDimitry Andric os << format("0x%08llX\t0x%08llX\t%s\t%s\n", osec->addr, osec->getSize(),
203344a3780SDimitry Andric seg->name.str().c_str(), osec->name.str().c_str());
204344a3780SDimitry Andric }
205344a3780SDimitry Andric
206ac9a064cSDimitry Andric // Shared function to print an array of symbols.
207ac9a064cSDimitry Andric auto printIsecArrSyms = [&](const std::vector<ConcatInputSection *> &arr) {
208ac9a064cSDimitry Andric for (const ConcatInputSection *isec : arr) {
209ac9a064cSDimitry Andric for (Defined *sym : isec->symbols) {
210ac9a064cSDimitry Andric if (!(isPrivateLabel(sym->getName()) && getSymSizeForMap(sym) == 0))
211ac9a064cSDimitry Andric os << format("0x%08llX\t0x%08llX\t[%3u] %s\n", sym->getVA(),
212ac9a064cSDimitry Andric getSymSizeForMap(sym),
213ac9a064cSDimitry Andric readerToFileOrdinal[sym->getFile()],
214ac9a064cSDimitry Andric sym->getName().str().data());
215ac9a064cSDimitry Andric }
216ac9a064cSDimitry Andric }
217ac9a064cSDimitry Andric };
218ac9a064cSDimitry Andric
219344a3780SDimitry Andric os << "# Symbols:\n";
220e3b55780SDimitry Andric os << "# Address\tSize \tFile Name\n";
221e3b55780SDimitry Andric for (const OutputSegment *seg : outputSegments) {
222e3b55780SDimitry Andric for (const OutputSection *osec : seg->getSections()) {
223e3b55780SDimitry Andric if (auto *concatOsec = dyn_cast<ConcatOutputSection>(osec)) {
224ac9a064cSDimitry Andric printIsecArrSyms(concatOsec->inputs);
225e3b55780SDimitry Andric } else if (osec == in.cStringSection || osec == in.objcMethnameSection) {
226e3b55780SDimitry Andric const auto &liveCStrings = info.liveCStringsForSection.lookup(osec);
227e3b55780SDimitry Andric uint64_t lastAddr = 0; // strings will never start at address 0, so this
228e3b55780SDimitry Andric // is a sentinel value
229e3b55780SDimitry Andric for (const auto &[addr, info] : liveCStrings) {
230e3b55780SDimitry Andric uint64_t size = 0;
231e3b55780SDimitry Andric if (addr != lastAddr)
232e3b55780SDimitry Andric size = info.str.size() + 1; // include null terminator
233e3b55780SDimitry Andric lastAddr = addr;
234e3b55780SDimitry Andric os << format("0x%08llX\t0x%08llX\t[%3u] literal string: ", addr, size,
235e3b55780SDimitry Andric info.fileIndex);
236e3b55780SDimitry Andric os.write_escaped(info.str) << "\n";
237e3b55780SDimitry Andric }
238e3b55780SDimitry Andric } else if (osec == (void *)in.unwindInfo) {
239e3b55780SDimitry Andric os << format("0x%08llX\t0x%08llX\t[ 0] compact unwind info\n",
240e3b55780SDimitry Andric osec->addr, osec->getSize());
241e3b55780SDimitry Andric } else if (osec == in.stubs) {
242e3b55780SDimitry Andric printStubsEntries(os, readerToFileOrdinal, osec, target->stubSize);
243e3b55780SDimitry Andric } else if (osec == in.lazyPointers) {
244e3b55780SDimitry Andric printStubsEntries(os, readerToFileOrdinal, osec, target->wordSize);
245e3b55780SDimitry Andric } else if (osec == in.stubHelper) {
246e3b55780SDimitry Andric // yes, ld64 calls it "helper helper"...
247e3b55780SDimitry Andric os << format("0x%08llX\t0x%08llX\t[ 0] helper helper\n", osec->addr,
248e3b55780SDimitry Andric osec->getSize());
249e3b55780SDimitry Andric } else if (osec == in.got) {
250e3b55780SDimitry Andric printNonLazyPointerSection(os, in.got);
251e3b55780SDimitry Andric } else if (osec == in.tlvPointers) {
252e3b55780SDimitry Andric printNonLazyPointerSection(os, in.tlvPointers);
253ac9a064cSDimitry Andric } else if (osec == in.objcMethList) {
254ac9a064cSDimitry Andric printIsecArrSyms(in.objcMethList->getInputs());
255e3b55780SDimitry Andric }
256e3b55780SDimitry Andric // TODO print other synthetic sections
257e3b55780SDimitry Andric }
258344a3780SDimitry Andric }
259344a3780SDimitry Andric
260ecbca9f5SDimitry Andric if (config->deadStrip) {
261ecbca9f5SDimitry Andric os << "# Dead Stripped Symbols:\n";
262e3b55780SDimitry Andric os << "# \tSize \tFile Name\n";
263e3b55780SDimitry Andric for (Defined *sym : info.deadSymbols) {
264ecbca9f5SDimitry Andric assert(!sym->isLive());
265ac9a064cSDimitry Andric os << format("<<dead>>\t0x%08llX\t[%3u] %s\n", getSymSizeForMap(sym),
266e3b55780SDimitry Andric readerToFileOrdinal[sym->getFile()],
267e3b55780SDimitry Andric sym->getName().str().data());
268e3b55780SDimitry Andric }
269e3b55780SDimitry Andric for (CStringInfo &cstrInfo : info.deadCStrings) {
270e3b55780SDimitry Andric os << format("<<dead>>\t0x%08zX\t[%3u] literal string: ",
271e3b55780SDimitry Andric cstrInfo.str.size() + 1, cstrInfo.fileIndex);
272e3b55780SDimitry Andric os.write_escaped(cstrInfo.str) << "\n";
273ecbca9f5SDimitry Andric }
274ecbca9f5SDimitry Andric }
275344a3780SDimitry Andric }
276