1 //===- MapFile.cpp --------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
17 // [ 0] linker synthesized
20 // # Address Size Segment Section
21 // 0x1000005C0 0x0000004C __TEXT __text
23 // # Address Size File Name
24 // 0x1000005C0 0x00000001 [ 1] _main
25 // # Dead Stripped Symbols:
27 // <<dead>> 0x00000001 [ 1] _foo
29 //===----------------------------------------------------------------------===//
32 #include "ConcatOutputSection.h"
34 #include "InputFiles.h"
35 #include "InputSection.h"
36 #include "OutputSegment.h"
38 #include "SyntheticSections.h"
40 #include "lld/Common/ErrorHandler.h"
41 #include "llvm/ADT/DenseMap.h"
42 #include "llvm/Support/Parallel.h"
43 #include "llvm/Support/TimeProfiler.h"
46 using namespace llvm::sys
;
48 using namespace lld::macho
;
56 SmallVector
<InputFile
*> files
;
57 SmallVector
<Defined
*> deadSymbols
;
58 DenseMap
<const OutputSection
*,
59 SmallVector
<std::pair
<uint64_t /*addr*/, CStringInfo
>>>
60 liveCStringsForSection
;
61 SmallVector
<CStringInfo
> deadCStrings
;
64 static MapInfo
gatherMapInfo() {
66 for (InputFile
*file
: inputFiles
) {
67 bool isReferencedFile
= false;
69 if (isa
<ObjFile
>(file
) || isa
<BitcodeFile
>(file
)) {
70 uint32_t fileIndex
= info
.files
.size() + 1;
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
75 for (Symbol
*sym
: file
->symbols
) {
76 if (auto *d
= dyn_cast_or_null
<Defined
>(sym
))
77 // 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)
80 if (d
->isec
&& d
->getFile() == file
&&
81 !isa
<CStringInputSection
>(d
->isec
)) {
82 isReferencedFile
= true;
84 info
.deadSymbols
.push_back(d
);
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
)) {
97 liveCStrings
.push_back({isec
->parent
->addr
+ piece
.outSecOff
,
98 {fileIndex
, isec
->getStringRef(i
)}});
100 info
.deadCStrings
.push_back({fileIndex
, isec
->getStringRef(i
)});
101 isReferencedFile
= true;
108 } else if (const auto *dylibFile
= dyn_cast
<DylibFile
>(file
)) {
109 isReferencedFile
= dylibFile
->isReferenced();
112 if (isReferencedFile
)
113 info
.files
.push_back(file
);
116 // cstrings are not stored in sorted order in their OutputSections, so we sort
118 for (auto &liveCStrings
: info
.liveCStringsForSection
)
119 parallelSort(liveCStrings
.second
, [](const auto &p1
, const auto &p2
) {
120 return p1
.first
< p2
.first
;
125 // We use this instead of `toString(const InputFile *)` as we don't want to
126 // include the dylib install name in our output.
127 static void printFileName(raw_fd_ostream
&os
, const InputFile
*f
) {
128 if (f
->archiveName
.empty())
131 os
<< f
->archiveName
<< "(" << path::filename(f
->getName()) + ")";
134 // For printing the contents of the __stubs and __la_symbol_ptr sections.
135 static void printStubsEntries(
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());
146 static void printNonLazyPointerSection(raw_fd_ostream
&os
,
147 NonLazyPointerSectionBase
*osec
) {
148 // 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
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());
158 void macho::writeMapFile() {
159 if (config
->mapFile
.empty())
162 TimeTraceScope
timeScope("Write map file");
164 // Open a map file for writing.
166 raw_fd_ostream
os(config
->mapFile
, ec
, sys::fs::OF_None
);
168 error("cannot open " + config
->mapFile
+ ": " + ec
.message());
172 os
<< format("# Path: %s\n", config
->outputFile
.str().c_str());
173 os
<< format("# Arch: %s\n",
174 getArchitectureName(config
->arch()).str().c_str());
176 MapInfo info
= gatherMapInfo();
178 os
<< "# Object files:\n";
179 os
<< format("[%3u] %s\n", 0, (const char *)"linker synthesized");
180 uint32_t fileIndex
= 1;
181 DenseMap
<lld::macho::InputFile
*, uint32_t> readerToFileOrdinal
;
182 for (InputFile
*file
: info
.files
) {
183 os
<< format("[%3u] ", fileIndex
);
184 printFileName(os
, file
);
186 readerToFileOrdinal
[file
] = fileIndex
++;
189 os
<< "# Sections:\n";
190 os
<< "# Address\tSize \tSegment\tSection\n";
191 for (OutputSegment
*seg
: outputSegments
)
192 for (OutputSection
*osec
: seg
->getSections()) {
193 if (osec
->isHidden())
196 os
<< format("0x%08llX\t0x%08llX\t%s\t%s\n", osec
->addr
, osec
->getSize(),
197 seg
->name
.str().c_str(), osec
->name
.str().c_str());
200 os
<< "# Symbols:\n";
201 os
<< "# Address\tSize \tFile Name\n";
202 for (const OutputSegment
*seg
: outputSegments
) {
203 for (const OutputSection
*osec
: seg
->getSections()) {
204 if (auto *concatOsec
= dyn_cast
<ConcatOutputSection
>(osec
)) {
205 for (const InputSection
*isec
: concatOsec
->inputs
) {
206 for (Defined
*sym
: isec
->symbols
)
207 if (!(isPrivateLabel(sym
->getName()) && sym
->size
== 0))
208 os
<< format("0x%08llX\t0x%08llX\t[%3u] %s\n", sym
->getVA(),
209 sym
->size
, readerToFileOrdinal
[sym
->getFile()],
210 sym
->getName().str().data());
212 } else if (osec
== in
.cStringSection
|| osec
== in
.objcMethnameSection
) {
213 const auto &liveCStrings
= info
.liveCStringsForSection
.lookup(osec
);
214 uint64_t lastAddr
= 0; // strings will never start at address 0, so this
215 // is a sentinel value
216 for (const auto &[addr
, info
] : liveCStrings
) {
218 if (addr
!= lastAddr
)
219 size
= info
.str
.size() + 1; // include null terminator
221 os
<< format("0x%08llX\t0x%08llX\t[%3u] literal string: ", addr
, size
,
223 os
.write_escaped(info
.str
) << "\n";
225 } else if (osec
== (void *)in
.unwindInfo
) {
226 os
<< format("0x%08llX\t0x%08llX\t[ 0] compact unwind info\n",
227 osec
->addr
, osec
->getSize());
228 } else if (osec
== in
.stubs
) {
229 printStubsEntries(os
, readerToFileOrdinal
, osec
, target
->stubSize
);
230 } else if (osec
== in
.lazyPointers
) {
231 printStubsEntries(os
, readerToFileOrdinal
, osec
, target
->wordSize
);
232 } else if (osec
== in
.stubHelper
) {
233 // yes, ld64 calls it "helper helper"...
234 os
<< format("0x%08llX\t0x%08llX\t[ 0] helper helper\n", osec
->addr
,
236 } else if (osec
== in
.got
) {
237 printNonLazyPointerSection(os
, in
.got
);
238 } else if (osec
== in
.tlvPointers
) {
239 printNonLazyPointerSection(os
, in
.tlvPointers
);
241 // TODO print other synthetic sections
245 if (config
->deadStrip
) {
246 os
<< "# Dead Stripped Symbols:\n";
247 os
<< "# \tSize \tFile Name\n";
248 for (Defined
*sym
: info
.deadSymbols
) {
249 assert(!sym
->isLive());
250 os
<< format("<<dead>>\t0x%08llX\t[%3u] %s\n", sym
->size
,
251 readerToFileOrdinal
[sym
->getFile()],
252 sym
->getName().str().data());
254 for (CStringInfo
&cstrInfo
: info
.deadCStrings
) {
255 os
<< format("<<dead>>\t0x%08zX\t[%3u] literal string: ",
256 cstrInfo
.str
.size() + 1, cstrInfo
.fileIndex
);
257 os
.write_escaped(cstrInfo
.str
) << "\n";