blob: 12417df8cecb8c66e778c5e1cfee5a57e64a1371 [file] [log] [blame]
caoming.royed8bff12021-03-18 14:38:301//===- MapFile.cpp --------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
Jez Ng7ca32bd2022-12-01 05:57:169// 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.
caoming.royed8bff12021-03-18 14:38:3013//
14// # Path: test
15// # Arch: x86_84
16// # Object files:
17// [ 0] linker synthesized
18// [ 1] a.o
19// # Sections:
Jez Ngda374d12022-10-22 02:39:5720// # Address Size Segment Section
21// 0x1000005C0 0x0000004C __TEXT __text
caoming.royed8bff12021-03-18 14:38:3022// # Symbols:
Jez Ngda374d12022-10-22 02:39:5723// # Address Size File Name
24// 0x1000005C0 0x00000001 [ 1] _main
25// # Dead Stripped Symbols:
26// # Size File Name
27// <<dead>> 0x00000001 [ 1] _foo
caoming.royed8bff12021-03-18 14:38:3028//
29//===----------------------------------------------------------------------===//
30
31#include "MapFile.h"
Jez Ng7ca32bd2022-12-01 05:57:1632#include "ConcatOutputSection.h"
caoming.royed8bff12021-03-18 14:38:3033#include "Config.h"
34#include "InputFiles.h"
35#include "InputSection.h"
caoming.royed8bff12021-03-18 14:38:3036#include "OutputSegment.h"
37#include "Symbols.h"
Roger Kim4f2c46c2022-02-12 00:33:2338#include "SyntheticSections.h"
caoming.royed8bff12021-03-18 14:38:3039#include "Target.h"
Vy Nguyenfc7a7182022-10-19 16:45:4940#include "lld/Common/ErrorHandler.h"
Jez Ng7ca32bd2022-12-01 05:57:1641#include "llvm/ADT/DenseMap.h"
caoming.royed8bff12021-03-18 14:38:3042#include "llvm/Support/Parallel.h"
Jez Ng4bcaafe2021-03-25 18:39:4443#include "llvm/Support/TimeProfiler.h"
caoming.royed8bff12021-03-18 14:38:3044
45using namespace llvm;
46using namespace llvm::sys;
47using namespace lld;
48using namespace lld::macho;
49
Jez Ng7ca32bd2022-12-01 05:57:1650struct CStringInfo {
51 uint32_t fileIndex;
52 StringRef str;
53};
54
Jez Ngb9457332022-10-22 02:48:2555struct MapInfo {
56 SmallVector<InputFile *> files;
Jez Ngb9457332022-10-22 02:48:2557 SmallVector<Defined *> deadSymbols;
Jez Ng7ca32bd2022-12-01 05:57:1658 DenseMap<const OutputSection *,
59 SmallVector<std::pair<uint64_t /*addr*/, CStringInfo>>>
60 liveCStringsForSection;
61 SmallVector<CStringInfo> deadCStrings;
Jez Ngb9457332022-10-22 02:48:2562};
63
64static MapInfo gatherMapInfo() {
65 MapInfo info;
Jez Ngaa288fd2022-12-21 22:26:0266 for (InputFile *file : inputFiles) {
67 bool isReferencedFile = false;
68
Jez Ngb9457332022-10-22 02:48:2569 if (isa<ObjFile>(file) || isa<BitcodeFile>(file)) {
Jez Ng7ca32bd2022-12-01 05:57:1670 uint32_t fileIndex = info.files.size() + 1;
Jez Ng7ca32bd2022-12-01 05:57:1671
72 // Gather the dead symbols. We don't have to bother with the live ones
73 // because we will pick them up as we iterate over the OutputSections
74 // later.
Jez Ngb9457332022-10-22 02:48:2575 for (Symbol *sym : file->symbols) {
Nico Webera5645512021-05-07 21:10:0576 if (auto *d = dyn_cast_or_null<Defined>(sym))
Jez Ng7ca32bd2022-12-01 05:57:1677 // Only emit the prevailing definition of a symbol. Also, don't emit
78 // the symbol if it is part of a cstring section (we use the literal
79 // value instead, similar to ld64)
alx322a3a79c2024-04-18 18:42:2280 if (d->isec() && d->getFile() == file &&
81 !isa<CStringInputSection>(d->isec())) {
Jez Ng7ca32bd2022-12-01 05:57:1682 isReferencedFile = true;
83 if (!d->isLive())
Jez Ngb9457332022-10-22 02:48:2584 info.deadSymbols.push_back(d);
Jez Ng213dbdb2022-11-08 21:33:2285 }
Jez Ng213dbdb2022-11-08 21:33:2286 }
Jez Ng7ca32bd2022-12-01 05:57:1687
88 // Gather all the cstrings (both live and dead). A CString(Output)Section
89 // doesn't provide us a way of figuring out which InputSections its
90 // cstring contents came from, so we need to build up that mapping here.
91 for (const Section *sec : file->sections) {
92 for (const Subsection &subsec : sec->subsections) {
93 if (auto isec = dyn_cast<CStringInputSection>(subsec.isec)) {
94 auto &liveCStrings = info.liveCStringsForSection[isec->parent];
95 for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) {
96 if (piece.live)
97 liveCStrings.push_back({isec->parent->addr + piece.outSecOff,
98 {fileIndex, isec->getStringRef(i)}});
99 else
100 info.deadCStrings.push_back({fileIndex, isec->getStringRef(i)});
101 isReferencedFile = true;
102 }
103 } else {
104 break;
105 }
106 }
107 }
Jez Ngaa288fd2022-12-21 22:26:02108 } else if (const auto *dylibFile = dyn_cast<DylibFile>(file)) {
109 isReferencedFile = dylibFile->isReferenced();
Jez Ngb9457332022-10-22 02:48:25110 }
Jez Ng7ca32bd2022-12-01 05:57:16111
Jez Ngaa288fd2022-12-21 22:26:02112 if (isReferencedFile)
113 info.files.push_back(file);
114 }
115
Jez Ng7ca32bd2022-12-01 05:57:16116 // cstrings are not stored in sorted order in their OutputSections, so we sort
117 // them here.
118 for (auto &liveCStrings : info.liveCStringsForSection)
119 parallelSort(liveCStrings.second, [](const auto &p1, const auto &p2) {
120 return p1.first < p2.first;
121 });
Jez Ngb9457332022-10-22 02:48:25122 return info;
caoming.royed8bff12021-03-18 14:38:30123}
124
Jez Ng5b213952023-03-11 03:29:14125// We use this instead of `toString(const InputFile *)` as we don't want to
126// include the dylib install name in our output.
127static void printFileName(raw_fd_ostream &os, const InputFile *f) {
128 if (f->archiveName.empty())
129 os << f->getName();
130 else
131 os << f->archiveName << "(" << path::filename(f->getName()) + ")";
132}
133
Jez Ngaa288fd2022-12-21 22:26:02134// For printing the contents of the __stubs and __la_symbol_ptr sections.
Jez Ng5b213952023-03-11 03:29:14135static void printStubsEntries(
Jez Ngaa288fd2022-12-21 22:26:02136 raw_fd_ostream &os,
137 const DenseMap<lld::macho::InputFile *, uint32_t> &readerToFileOrdinal,
138 const OutputSection *osec, size_t entrySize) {
139 for (const Symbol *sym : in.stubs->getEntries())
140 os << format("0x%08llX\t0x%08zX\t[%3u] %s\n",
141 osec->addr + sym->stubsIndex * entrySize, entrySize,
142 readerToFileOrdinal.lookup(sym->getFile()),
143 sym->getName().str().data());
144}
145
Jez Ng5b213952023-03-11 03:29:14146static void printNonLazyPointerSection(raw_fd_ostream &os,
147 NonLazyPointerSectionBase *osec) {
Jez Ngaa288fd2022-12-21 22:26:02148 // ld64 considers stubs to belong to particular files, but considers GOT
149 // entries to be linker-synthesized. Not sure why they made that decision, but
150 // I think we can follow suit unless there's demand for better symbol-to-file
151 // associations.
152 for (const Symbol *sym : osec->getEntries())
153 os << format("0x%08llX\t0x%08zX\t[ 0] non-lazy-pointer-to-local: %s\n",
154 osec->addr + sym->gotIndex * target->wordSize,
155 target->wordSize, sym->getName().str().data());
156}
157
alx322a3a79c2024-04-18 18:42:22158static uint64_t getSymSizeForMap(Defined *sym) {
alx32d1756162024-09-05 23:36:21159 if (sym->identicalCodeFoldingKind == Symbol::ICFFoldKind::Body)
alx322a3a79c2024-04-18 18:42:22160 return 0;
161 return sym->size;
162}
163
alx32162814a2025-01-08 05:07:51164// Merges two vectors of input sections in order of their outSecOff values.
165// This approach creates a new (temporary) vector which is not ideal but the
166// ideal approach leads to a lot of code duplication.
167static std::vector<ConcatInputSection *>
168mergeOrderedInputs(ArrayRef<ConcatInputSection *> inputs1,
169 ArrayRef<ConcatInputSection *> inputs2) {
170 std::vector<ConcatInputSection *> vec(inputs1.size() + inputs2.size());
171 std::merge(inputs1.begin(), inputs1.end(), inputs2.begin(), inputs2.end(),
172 vec.begin(), [](ConcatInputSection *a, ConcatInputSection *b) {
173 return a->outSecOff < b->outSecOff;
174 });
175 return vec;
176}
177
caoming.royed8bff12021-03-18 14:38:30178void macho::writeMapFile() {
179 if (config->mapFile.empty())
180 return;
181
Jez Ng4bcaafe2021-03-25 18:39:44182 TimeTraceScope timeScope("Write map file");
183
caoming.royed8bff12021-03-18 14:38:30184 // Open a map file for writing.
185 std::error_code ec;
186 raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None);
187 if (ec) {
188 error("cannot open " + config->mapFile + ": " + ec.message());
189 return;
190 }
191
caoming.royed8bff12021-03-18 14:38:30192 os << format("# Path: %s\n", config->outputFile.str().c_str());
Jez Nged4a4e32021-04-21 19:43:38193 os << format("# Arch: %s\n",
194 getArchitectureName(config->arch()).str().c_str());
caoming.royed8bff12021-03-18 14:38:30195
Jez Ngb9457332022-10-22 02:48:25196 MapInfo info = gatherMapInfo();
197
caoming.royed8bff12021-03-18 14:38:30198 os << "# Object files:\n";
199 os << format("[%3u] %s\n", 0, (const char *)"linker synthesized");
200 uint32_t fileIndex = 1;
201 DenseMap<lld::macho::InputFile *, uint32_t> readerToFileOrdinal;
Jez Ngb9457332022-10-22 02:48:25202 for (InputFile *file : info.files) {
Jez Ng5b213952023-03-11 03:29:14203 os << format("[%3u] ", fileIndex);
204 printFileName(os, file);
205 os << "\n";
Jez Ngb9457332022-10-22 02:48:25206 readerToFileOrdinal[file] = fileIndex++;
caoming.royed8bff12021-03-18 14:38:30207 }
208
caoming.royed8bff12021-03-18 14:38:30209 os << "# Sections:\n";
210 os << "# Address\tSize \tSegment\tSection\n";
211 for (OutputSegment *seg : outputSegments)
212 for (OutputSection *osec : seg->getSections()) {
213 if (osec->isHidden())
214 continue;
215
216 os << format("0x%08llX\t0x%08llX\t%s\t%s\n", osec->addr, osec->getSize(),
217 seg->name.str().c_str(), osec->name.str().c_str());
218 }
219
alx32742a82a2024-03-27 21:34:27220 // Shared function to print an array of symbols.
221 auto printIsecArrSyms = [&](const std::vector<ConcatInputSection *> &arr) {
222 for (const ConcatInputSection *isec : arr) {
223 for (Defined *sym : isec->symbols) {
alx322a3a79c2024-04-18 18:42:22224 if (!(isPrivateLabel(sym->getName()) && getSymSizeForMap(sym) == 0))
alx32742a82a2024-03-27 21:34:27225 os << format("0x%08llX\t0x%08llX\t[%3u] %s\n", sym->getVA(),
alx322a3a79c2024-04-18 18:42:22226 getSymSizeForMap(sym),
227 readerToFileOrdinal[sym->getFile()],
alx32742a82a2024-03-27 21:34:27228 sym->getName().str().data());
229 }
230 }
231 };
232
caoming.royed8bff12021-03-18 14:38:30233 os << "# Symbols:\n";
Jez Ngbdd0cec2022-10-13 20:44:29234 os << "# Address\tSize \tFile Name\n";
Jez Ng7ca32bd2022-12-01 05:57:16235 for (const OutputSegment *seg : outputSegments) {
236 for (const OutputSection *osec : seg->getSections()) {
alx32162814a2025-01-08 05:07:51237 if (auto *textOsec = dyn_cast<TextOutputSection>(osec)) {
238 auto inputsAndThunks =
239 mergeOrderedInputs(textOsec->inputs, textOsec->getThunks());
240 printIsecArrSyms(inputsAndThunks);
241 } else if (auto *concatOsec = dyn_cast<ConcatOutputSection>(osec)) {
alx32742a82a2024-03-27 21:34:27242 printIsecArrSyms(concatOsec->inputs);
Jez Ng7ca32bd2022-12-01 05:57:16243 } else if (osec == in.cStringSection || osec == in.objcMethnameSection) {
244 const auto &liveCStrings = info.liveCStringsForSection.lookup(osec);
245 uint64_t lastAddr = 0; // strings will never start at address 0, so this
246 // is a sentinel value
247 for (const auto &[addr, info] : liveCStrings) {
248 uint64_t size = 0;
249 if (addr != lastAddr)
250 size = info.str.size() + 1; // include null terminator
251 lastAddr = addr;
252 os << format("0x%08llX\t0x%08llX\t[%3u] literal string: ", addr, size,
253 info.fileIndex);
254 os.write_escaped(info.str) << "\n";
255 }
Jez Ng41f90e92022-12-01 06:20:54256 } else if (osec == (void *)in.unwindInfo) {
257 os << format("0x%08llX\t0x%08llX\t[ 0] compact unwind info\n",
258 osec->addr, osec->getSize());
Jez Ngaa288fd2022-12-21 22:26:02259 } else if (osec == in.stubs) {
260 printStubsEntries(os, readerToFileOrdinal, osec, target->stubSize);
261 } else if (osec == in.lazyPointers) {
262 printStubsEntries(os, readerToFileOrdinal, osec, target->wordSize);
263 } else if (osec == in.stubHelper) {
264 // yes, ld64 calls it "helper helper"...
265 os << format("0x%08llX\t0x%08llX\t[ 0] helper helper\n", osec->addr,
266 osec->getSize());
267 } else if (osec == in.got) {
268 printNonLazyPointerSection(os, in.got);
269 } else if (osec == in.tlvPointers) {
270 printNonLazyPointerSection(os, in.tlvPointers);
alx32742a82a2024-03-27 21:34:27271 } else if (osec == in.objcMethList) {
272 printIsecArrSyms(in.objcMethList->getInputs());
Jez Ng7ca32bd2022-12-01 05:57:16273 }
274 // TODO print other synthetic sections
275 }
caoming.royed8bff12021-03-18 14:38:30276 }
277
Roger Kim42208432022-01-28 18:51:27278 if (config->deadStrip) {
Roger Kim42208432022-01-28 18:51:27279 os << "# Dead Stripped Symbols:\n";
Jez Ngbdd0cec2022-10-13 20:44:29280 os << "# \tSize \tFile Name\n";
Jez Ngb9457332022-10-22 02:48:25281 for (Defined *sym : info.deadSymbols) {
Roger Kim42208432022-01-28 18:51:27282 assert(!sym->isLive());
alx322a3a79c2024-04-18 18:42:22283 os << format("<<dead>>\t0x%08llX\t[%3u] %s\n", getSymSizeForMap(sym),
Jez Ngbdd0cec2022-10-13 20:44:29284 readerToFileOrdinal[sym->getFile()],
Jez Ng7ca32bd2022-12-01 05:57:16285 sym->getName().str().data());
286 }
287 for (CStringInfo &cstrInfo : info.deadCStrings) {
David Spickett7c7e39d2022-12-06 10:30:38288 os << format("<<dead>>\t0x%08zX\t[%3u] literal string: ",
Jez Ng7ca32bd2022-12-01 05:57:16289 cstrInfo.str.size() + 1, cstrInfo.fileIndex);
290 os.write_escaped(cstrInfo.str) << "\n";
Roger Kim42208432022-01-28 18:51:27291 }
292 }
caoming.royed8bff12021-03-18 14:38:30293}