1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
//===- 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 in the same format as link.exe
// (based on observations)
//
// Header (program name, timestamp info, preferred load address)
//
// Section list (Start = Section index:Base address):
// Start Length Name Class
// 0001:00001000 00000015H .text CODE
//
// Symbols list:
// Address Publics by Value Rva + Base Lib:Object
// 0001:00001000 main 0000000140001000 main.obj
// 0001:00001300 ?__scrt_common_main@@YAHXZ 0000000140001300 libcmt:exe_main.obj
//
// entry point at 0001:00000360
//
// Static symbols
//
// 0000:00000000 __guard_fids__ 0000000140000000 libcmt : exe_main.obj
//===----------------------------------------------------------------------===//
#include "MapFile.h"
#include "COFFLinkerContext.h"
#include "SymbolTable.h"
#include "Symbols.h"
#include "Writer.h"
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/Timer.h"
#include "llvm/Support/Parallel.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using namespace llvm::object;
using namespace lld;
using namespace lld::coff;
// Print out the first two columns of a line.
static void writeHeader(raw_ostream &os, uint32_t sec, uint64_t addr) {
os << format(" %04x:%08llx", sec, addr);
}
// Write the time stamp with the format used by link.exe
// It seems identical to strftime with "%c" on msvc build, but we need a
// locale-agnostic version.
static void writeFormattedTimestamp(raw_ostream &os, time_t tds) {
constexpr const char *const days[7] = {"Sun", "Mon", "Tue", "Wed",
"Thu", "Fri", "Sat"};
constexpr const char *const months[12] = {"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"};
tm *time = localtime(&tds);
os << format("%s %s %2d %02d:%02d:%02d %d", days[time->tm_wday],
months[time->tm_mon], time->tm_mday, time->tm_hour, time->tm_min,
time->tm_sec, time->tm_year + 1900);
}
static void sortUniqueSymbols(std::vector<Defined *> &syms,
uint64_t imageBase) {
// Build helper vector
using SortEntry = std::pair<Defined *, size_t>;
std::vector<SortEntry> v;
v.resize(syms.size());
for (size_t i = 0, e = syms.size(); i < e; ++i)
v[i] = SortEntry(syms[i], i);
// Remove duplicate symbol pointers
parallelSort(v, std::less<SortEntry>());
auto end = std::unique(v.begin(), v.end(),
[](const SortEntry &a, const SortEntry &b) {
return a.first == b.first;
});
v.erase(end, v.end());
// Sort by RVA then original order
parallelSort(v, [imageBase](const SortEntry &a, const SortEntry &b) {
// Add config.imageBase to avoid comparing "negative" RVAs.
// This can happen with symbols of Absolute kind
uint64_t rvaa = imageBase + a.first->getRVA();
uint64_t rvab = imageBase + b.first->getRVA();
return rvaa < rvab || (rvaa == rvab && a.second < b.second);
});
syms.resize(v.size());
for (size_t i = 0, e = v.size(); i < e; ++i)
syms[i] = v[i].first;
}
// Returns the lists of all symbols that we want to print out.
static void getSymbols(const COFFLinkerContext &ctx,
std::vector<Defined *> &syms,
std::vector<Defined *> &staticSyms) {
for (ObjFile *file : ctx.objFileInstances)
for (Symbol *b : file->getSymbols()) {
if (!b || !b->isLive())
continue;
if (auto *sym = dyn_cast<DefinedCOFF>(b)) {
COFFSymbolRef symRef = sym->getCOFFSymbol();
if (!symRef.isSectionDefinition() &&
symRef.getStorageClass() != COFF::IMAGE_SYM_CLASS_LABEL) {
if (symRef.getStorageClass() == COFF::IMAGE_SYM_CLASS_STATIC)
staticSyms.push_back(sym);
else
syms.push_back(sym);
}
} else if (auto *sym = dyn_cast<Defined>(b)) {
syms.push_back(sym);
}
}
for (ImportFile *file : ctx.importFileInstances) {
if (!file->live)
continue;
if (!file->thunkSym)
continue;
if (!file->thunkLive)
continue;
if (auto *thunkSym = dyn_cast<Defined>(file->thunkSym))
syms.push_back(thunkSym);
if (auto *impSym = dyn_cast_or_null<Defined>(file->impSym))
syms.push_back(impSym);
}
sortUniqueSymbols(syms, ctx.config.imageBase);
sortUniqueSymbols(staticSyms, ctx.config.imageBase);
}
// Construct a map from symbols to their stringified representations.
static DenseMap<Defined *, std::string>
getSymbolStrings(const COFFLinkerContext &ctx, ArrayRef<Defined *> syms) {
std::vector<std::string> str(syms.size());
parallelFor((size_t)0, syms.size(), [&](size_t i) {
raw_string_ostream os(str[i]);
Defined *sym = syms[i];
uint16_t sectionIdx = 0;
uint64_t address = 0;
SmallString<128> fileDescr;
if (auto *absSym = dyn_cast<DefinedAbsolute>(sym)) {
address = absSym->getVA();
fileDescr = "<absolute>";
} else if (isa<DefinedSynthetic>(sym)) {
fileDescr = "<linker-defined>";
} else if (isa<DefinedCommon>(sym)) {
fileDescr = "<common>";
} else if (Chunk *chunk = sym->getChunk()) {
address = sym->getRVA();
if (OutputSection *sec = ctx.getOutputSection(chunk))
address -= sec->header.VirtualAddress;
sectionIdx = chunk->getOutputSectionIdx();
InputFile *file;
if (auto *impSym = dyn_cast<DefinedImportData>(sym))
file = impSym->file;
else if (auto *thunkSym = dyn_cast<DefinedImportThunk>(sym))
file = thunkSym->wrappedSym->file;
else
file = sym->getFile();
if (file) {
if (!file->parentName.empty()) {
fileDescr = sys::path::filename(file->parentName);
sys::path::replace_extension(fileDescr, "");
fileDescr += ":";
}
fileDescr += sys::path::filename(file->getName());
}
}
writeHeader(os, sectionIdx, address);
os << " ";
os << left_justify(sym->getName(), 26);
os << " ";
os << format_hex_no_prefix((ctx.config.imageBase + sym->getRVA()), 16);
if (!fileDescr.empty()) {
os << " "; // FIXME : Handle "f" and "i" flags sometimes generated
// by link.exe in those spaces
os << fileDescr;
}
});
DenseMap<Defined *, std::string> ret;
for (size_t i = 0, e = syms.size(); i < e; ++i)
ret[syms[i]] = std::move(str[i]);
return ret;
}
void lld::coff::writeMapFile(COFFLinkerContext &ctx) {
if (ctx.config.mapFile.empty())
return;
std::error_code ec;
raw_fd_ostream os(ctx.config.mapFile, ec, sys::fs::OF_None);
if (ec)
fatal("cannot open " + ctx.config.mapFile + ": " + ec.message());
ScopedTimer t1(ctx.totalMapTimer);
// Collect symbol info that we want to print out.
ScopedTimer t2(ctx.symbolGatherTimer);
std::vector<Defined *> syms;
std::vector<Defined *> staticSyms;
getSymbols(ctx, syms, staticSyms);
t2.stop();
ScopedTimer t3(ctx.symbolStringsTimer);
DenseMap<Defined *, std::string> symStr = getSymbolStrings(ctx, syms);
DenseMap<Defined *, std::string> staticSymStr =
getSymbolStrings(ctx, staticSyms);
t3.stop();
ScopedTimer t4(ctx.writeTimer);
SmallString<128> AppName = sys::path::filename(ctx.config.outputFile);
sys::path::replace_extension(AppName, "");
// Print out the file header
os << " " << AppName << "\n";
os << "\n";
os << " Timestamp is " << format_hex_no_prefix(ctx.config.timestamp, 8)
<< " (";
if (ctx.config.repro) {
os << "Repro mode";
} else {
writeFormattedTimestamp(os, ctx.config.timestamp);
}
os << ")\n";
os << "\n";
os << " Preferred load address is "
<< format_hex_no_prefix(ctx.config.imageBase, 16) << "\n";
os << "\n";
// Print out section table.
os << " Start Length Name Class\n";
for (OutputSection *sec : ctx.outputSections) {
// Merge display of chunks with same sectionName
std::vector<std::pair<SectionChunk *, SectionChunk *>> ChunkRanges;
for (Chunk *c : sec->chunks) {
auto *sc = dyn_cast<SectionChunk>(c);
if (!sc)
continue;
if (ChunkRanges.empty() ||
c->getSectionName() != ChunkRanges.back().first->getSectionName()) {
ChunkRanges.emplace_back(sc, sc);
} else {
ChunkRanges.back().second = sc;
}
}
const bool isCodeSection =
(sec->header.Characteristics & COFF::IMAGE_SCN_CNT_CODE) &&
(sec->header.Characteristics & COFF::IMAGE_SCN_MEM_READ) &&
(sec->header.Characteristics & COFF::IMAGE_SCN_MEM_EXECUTE);
StringRef SectionClass = (isCodeSection ? "CODE" : "DATA");
for (auto &cr : ChunkRanges) {
size_t size =
cr.second->getRVA() + cr.second->getSize() - cr.first->getRVA();
auto address = cr.first->getRVA() - sec->header.VirtualAddress;
writeHeader(os, sec->sectionIndex, address);
os << " " << format_hex_no_prefix(size, 8) << "H";
os << " " << left_justify(cr.first->getSectionName(), 23);
os << " " << SectionClass;
os << '\n';
}
}
// Print out the symbols table (without static symbols)
os << "\n";
os << " Address Publics by Value Rva+Base"
" Lib:Object\n";
os << "\n";
for (Defined *sym : syms)
os << symStr[sym] << '\n';
// Print out the entry point.
os << "\n";
uint16_t entrySecIndex = 0;
uint64_t entryAddress = 0;
if (!ctx.config.noEntry) {
Defined *entry = dyn_cast_or_null<Defined>(ctx.config.entry);
if (entry) {
Chunk *chunk = entry->getChunk();
entrySecIndex = chunk->getOutputSectionIdx();
entryAddress =
entry->getRVA() - ctx.getOutputSection(chunk)->header.VirtualAddress;
}
}
os << " entry point at ";
os << format("%04x:%08llx", entrySecIndex, entryAddress);
os << "\n";
// Print out the static symbols
os << "\n";
os << " Static symbols\n";
os << "\n";
for (Defined *sym : staticSyms)
os << staticSymStr[sym] << '\n';
// Print out the exported functions
if (ctx.config.mapInfo) {
os << "\n";
os << " Exports\n";
os << "\n";
os << " ordinal name\n\n";
for (Export &e : ctx.config.exports) {
os << format(" %7d", e.ordinal) << " " << e.name << "\n";
if (!e.extName.empty() && e.extName != e.name)
os << " exported name: " << e.extName << "\n";
}
}
t4.stop();
t1.stop();
}
|