2017-01-14 05:05:46 +08:00
|
|
|
//===- MapFile.cpp --------------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2017-01-14 05:05:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the -Map option. It shows lists in order and
|
|
|
|
// hierarchically the output sections, input sections, input files and
|
|
|
|
// symbol:
|
|
|
|
//
|
2017-04-29 04:38:27 +08:00
|
|
|
// Address Size Align Out In Symbol
|
|
|
|
// 00201000 00000015 4 .text
|
2017-05-01 04:58:20 +08:00
|
|
|
// 00201000 0000000e 4 test.o:(.text)
|
2017-04-29 04:38:27 +08:00
|
|
|
// 0020100e 00000000 0 local
|
|
|
|
// 00201005 00000000 0 f(int)
|
2017-01-14 05:05:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MapFile.h"
|
|
|
|
#include "InputFiles.h"
|
2017-05-19 05:30:14 +08:00
|
|
|
#include "LinkerScript.h"
|
2017-05-19 01:26:00 +08:00
|
|
|
#include "OutputSections.h"
|
2017-04-29 01:19:13 +08:00
|
|
|
#include "SymbolTable.h"
|
2017-12-10 00:56:18 +08:00
|
|
|
#include "Symbols.h"
|
2017-08-11 19:34:04 +08:00
|
|
|
#include "SyntheticSections.h"
|
2018-03-01 01:38:19 +08:00
|
|
|
#include "lld/Common/Strings.h"
|
2018-03-15 04:29:45 +08:00
|
|
|
#include "llvm/ADT/MapVector.h"
|
|
|
|
#include "llvm/ADT/SetVector.h"
|
[Support] Move LLD's parallel algorithm wrappers to support
Essentially takes the lld/Common/Threads.h wrappers and moves them to
the llvm/Support/Paralle.h algorithm header.
The changes are:
- Remove policy parameter, since all clients use `par`.
- Rename the methods to `parallelSort` etc to match LLVM style, since
they are no longer C++17 pstl compatible.
- Move algorithms from llvm::parallel:: to llvm::, since they have
"parallel" in the name and are no longer overloads of the regular
algorithms.
- Add range overloads
- Use the sequential algorithm directly when 1 thread is requested
(skips task grouping)
- Fix the index type of parallelForEachN to size_t. Nobody in LLVM was
using any other parameter, and it made overload resolution hard for
for_each_n(par, 0, foo.size(), ...) because 0 is int, not size_t.
Remove Threads.h and update LLD for that.
This is a prerequisite for parallel public symbol processing in the PDB
library, which is in LLVM.
Reviewed By: MaskRay, aganea
Differential Revision: https://reviews.llvm.org/D79390
2020-05-05 11:03:19 +08:00
|
|
|
#include "llvm/Support/Parallel.h"
|
2020-11-03 22:41:09 +08:00
|
|
|
#include "llvm/Support/TimeProfiler.h"
|
2017-01-15 08:41:21 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-01-14 05:05:46 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::object;
|
2020-05-15 13:18:58 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
2017-01-14 05:05:46 +08:00
|
|
|
|
2019-04-01 08:11:24 +08:00
|
|
|
using SymbolMapTy = DenseMap<const SectionBase *, SmallVector<Defined *, 4>>;
|
2017-04-29 01:19:13 +08:00
|
|
|
|
2019-08-23 03:43:27 +08:00
|
|
|
static constexpr char indent8[] = " "; // 8 spaces
|
|
|
|
static constexpr char indent16[] = " "; // 16 spaces
|
2018-03-07 06:48:46 +08:00
|
|
|
|
2017-04-29 01:19:13 +08:00
|
|
|
// Print out the first three columns of a line.
|
2018-04-05 18:51:06 +08:00
|
|
|
static void writeHeader(raw_ostream &os, uint64_t vma, uint64_t lma,
|
|
|
|
uint64_t size, uint64_t align) {
|
2018-04-06 00:45:37 +08:00
|
|
|
if (config->is64)
|
2018-04-06 01:20:18 +08:00
|
|
|
os << format("%16llx %16llx %8llx %5lld ", vma, lma, size, align);
|
2018-04-06 00:45:37 +08:00
|
|
|
else
|
2018-04-06 01:20:18 +08:00
|
|
|
os << format("%8llx %8llx %8llx %5lld ", vma, lma, size, align);
|
2017-01-14 05:05:46 +08:00
|
|
|
}
|
|
|
|
|
2017-04-29 07:29:15 +08:00
|
|
|
// Returns a list of all symbols that we want to print out.
|
2018-04-27 01:58:58 +08:00
|
|
|
static std::vector<Defined *> getSymbols() {
|
|
|
|
std::vector<Defined *> v;
|
|
|
|
for (InputFile *file : objectFiles)
|
|
|
|
for (Symbol *b : file->getSymbols())
|
2017-11-06 12:35:31 +08:00
|
|
|
if (auto *dr = dyn_cast<Defined>(b))
|
2019-05-29 11:55:20 +08:00
|
|
|
if (!dr->isSection() && dr->section && dr->section->isLive() &&
|
2018-04-27 01:58:58 +08:00
|
|
|
(dr->file == file || dr->needsPltAddr || dr->section->bss))
|
2017-08-05 02:42:04 +08:00
|
|
|
v.push_back(dr);
|
2017-04-29 07:29:15 +08:00
|
|
|
return v;
|
|
|
|
}
|
2017-04-29 01:19:13 +08:00
|
|
|
|
2017-04-29 07:29:15 +08:00
|
|
|
// Returns a map from sections to their symbols.
|
2018-04-27 01:58:58 +08:00
|
|
|
static SymbolMapTy getSectionSyms(ArrayRef<Defined *> syms) {
|
2017-04-29 07:29:15 +08:00
|
|
|
SymbolMapTy ret;
|
2018-04-27 01:58:58 +08:00
|
|
|
for (Defined *dr : syms)
|
|
|
|
ret[dr->section].push_back(dr);
|
2017-04-29 01:19:13 +08:00
|
|
|
|
|
|
|
// Sort symbols by address. We want to print out symbols in the
|
|
|
|
// order in the output file rather than the order they appeared
|
|
|
|
// in the input files.
|
2019-04-23 10:42:06 +08:00
|
|
|
for (auto &it : ret)
|
|
|
|
llvm::stable_sort(it.second, [](Defined *a, Defined *b) {
|
2018-04-24 17:55:39 +08:00
|
|
|
return a->getVA() < b->getVA();
|
|
|
|
});
|
2017-04-29 07:29:15 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2017-01-14 05:05:46 +08:00
|
|
|
|
2017-04-29 07:29:15 +08:00
|
|
|
// 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.
|
2017-12-05 22:23:18 +08:00
|
|
|
static DenseMap<Symbol *, std::string>
|
2018-04-27 01:58:58 +08:00
|
|
|
getSymbolStrings(ArrayRef<Defined *> syms) {
|
2017-04-29 01:19:13 +08:00
|
|
|
std::vector<std::string> str(syms.size());
|
2017-05-11 04:02:19 +08:00
|
|
|
parallelForEachN(0, syms.size(), [&](size_t i) {
|
2017-04-29 01:19:13 +08:00
|
|
|
raw_string_ostream os(str[i]);
|
2018-04-05 18:51:06 +08:00
|
|
|
OutputSection *osec = syms[i]->getOutputSection();
|
|
|
|
uint64_t vma = syms[i]->getVA();
|
|
|
|
uint64_t lma = osec ? osec->getLMA() + vma - osec->getVA(0) : 0;
|
|
|
|
writeHeader(os, vma, lma, syms[i]->getSize(), 1);
|
2018-03-08 01:15:15 +08:00
|
|
|
os << indent16 << toString(*syms[i]);
|
2017-04-29 01:19:13 +08:00
|
|
|
});
|
2017-01-14 08:37:28 +08:00
|
|
|
|
2017-12-05 22:23:18 +08:00
|
|
|
DenseMap<Symbol *, std::string> ret;
|
2017-04-29 07:29:15 +08:00
|
|
|
for (size_t i = 0, e = syms.size(); i < e; ++i)
|
|
|
|
ret[syms[i]] = std::move(str[i]);
|
|
|
|
return ret;
|
2017-01-14 05:05:46 +08:00
|
|
|
}
|
|
|
|
|
2018-03-15 05:18:18 +08:00
|
|
|
// Print .eh_frame contents. Since the section consists of EhSectionPieces,
|
|
|
|
// we need a specialized printer for that section.
|
|
|
|
//
|
|
|
|
// .eh_frame tend to contain a lot of section pieces that are contiguous
|
|
|
|
// both in input file and output file. Such pieces are squashed before
|
|
|
|
// being displayed to make output compact.
|
2019-05-24 05:30:30 +08:00
|
|
|
static void printEhFrame(raw_ostream &os, const EhFrameSection *sec) {
|
2018-03-15 05:18:18 +08:00
|
|
|
std::vector<EhSectionPiece> pieces;
|
|
|
|
|
|
|
|
auto add = [&](const EhSectionPiece &p) {
|
|
|
|
// If P is adjacent to Last, squash the two.
|
|
|
|
if (!pieces.empty()) {
|
|
|
|
EhSectionPiece &last = pieces.back();
|
|
|
|
if (last.sec == p.sec && last.inputOff + last.size == p.inputOff &&
|
|
|
|
last.outputOff + last.size == p.outputOff) {
|
|
|
|
last.size += p.size;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pieces.push_back(p);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Gather section pieces.
|
2019-05-24 05:30:30 +08:00
|
|
|
for (const CieRecord *rec : sec->getCieRecords()) {
|
2018-03-15 05:18:18 +08:00
|
|
|
add(*rec->cie);
|
|
|
|
for (const EhSectionPiece *fde : rec->fdes)
|
|
|
|
add(*fde);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print out section pieces.
|
2019-05-24 05:30:30 +08:00
|
|
|
const OutputSection *osec = sec->getOutputSection();
|
2018-03-15 05:18:18 +08:00
|
|
|
for (EhSectionPiece &p : pieces) {
|
2018-04-05 18:51:06 +08:00
|
|
|
writeHeader(os, osec->addr + p.outputOff, osec->getLMA() + p.outputOff,
|
|
|
|
p.size, 1);
|
2018-03-15 05:18:18 +08:00
|
|
|
os << indent8 << toString(p.sec->file) << ":(" << p.sec->name << "+0x"
|
|
|
|
<< Twine::utohexstr(p.inputOff) + ")\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
void elf::writeMapFile() {
|
2017-01-15 08:41:21 +08:00
|
|
|
if (config->mapFile.empty())
|
2017-01-14 05:05:46 +08:00
|
|
|
return;
|
|
|
|
|
2020-11-03 22:41:09 +08:00
|
|
|
llvm::TimeTraceScope timeScope("Write map file");
|
|
|
|
|
2017-04-29 07:29:15 +08:00
|
|
|
// Open a map file for writing.
|
2017-01-15 08:41:21 +08:00
|
|
|
std::error_code ec;
|
2019-08-05 13:43:48 +08:00
|
|
|
raw_fd_ostream os(config->mapFile, ec, sys::fs::OF_None);
|
2017-04-29 07:29:15 +08:00
|
|
|
if (ec) {
|
2017-01-16 09:07:19 +08:00
|
|
|
error("cannot open " + config->mapFile + ": " + ec.message());
|
2017-04-29 07:29:15 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect symbol info that we want to print out.
|
2018-04-27 01:58:58 +08:00
|
|
|
std::vector<Defined *> syms = getSymbols();
|
2017-07-28 19:13:21 +08:00
|
|
|
SymbolMapTy sectionSyms = getSectionSyms(syms);
|
2017-12-05 22:23:18 +08:00
|
|
|
DenseMap<Symbol *, std::string> symStr = getSymbolStrings(syms);
|
2017-04-29 07:29:15 +08:00
|
|
|
|
|
|
|
// Print out the header line.
|
2017-10-29 04:15:56 +08:00
|
|
|
int w = config->is64 ? 16 : 8;
|
2018-04-06 01:20:18 +08:00
|
|
|
os << right_justify("VMA", w) << ' ' << right_justify("LMA", w)
|
|
|
|
<< " Size Align Out In Symbol\n";
|
2017-04-29 07:29:15 +08:00
|
|
|
|
2018-12-06 17:04:52 +08:00
|
|
|
OutputSection* osec = nullptr;
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *cmd : script->sectionCommands) {
|
|
|
|
if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
|
|
|
|
if (assign->provide && !assign->sym)
|
2018-04-05 19:25:58 +08:00
|
|
|
continue;
|
2021-11-26 12:24:23 +08:00
|
|
|
uint64_t lma = osec ? osec->getLMA() + assign->addr - osec->getVA(0) : 0;
|
|
|
|
writeHeader(os, assign->addr, lma, assign->size, 1);
|
|
|
|
os << assign->commandString << '\n';
|
2018-04-05 19:25:58 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-26 12:24:23 +08:00
|
|
|
osec = cast<OutputSection>(cmd);
|
2018-04-05 18:51:06 +08:00
|
|
|
writeHeader(os, osec->addr, osec->getLMA(), osec->size, osec->alignment);
|
2017-04-29 07:29:15 +08:00
|
|
|
os << osec->name << '\n';
|
|
|
|
|
|
|
|
// Dump symbols for each input section.
|
2021-11-26 12:24:23 +08:00
|
|
|
for (SectionCommand *subCmd : osec->commands) {
|
|
|
|
if (auto *isd = dyn_cast<InputSectionDescription>(subCmd)) {
|
2018-03-15 17:16:40 +08:00
|
|
|
for (InputSection *isec : isd->sections) {
|
2019-05-24 05:30:30 +08:00
|
|
|
if (auto *ehSec = dyn_cast<EhFrameSection>(isec)) {
|
|
|
|
printEhFrame(os, ehSec);
|
2018-03-15 17:16:40 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:51:06 +08:00
|
|
|
writeHeader(os, isec->getVA(0), osec->getLMA() + isec->getOffset(0),
|
|
|
|
isec->getSize(), isec->alignment);
|
2018-03-15 17:16:40 +08:00
|
|
|
os << indent8 << toString(isec) << '\n';
|
|
|
|
for (Symbol *sym : sectionSyms[isec])
|
|
|
|
os << symStr[sym] << '\n';
|
|
|
|
}
|
2018-03-15 05:18:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-26 12:24:23 +08:00
|
|
|
if (auto *data = dyn_cast<ByteCommand>(subCmd)) {
|
|
|
|
writeHeader(os, osec->addr + data->offset,
|
|
|
|
osec->getLMA() + data->offset, data->size, 1);
|
|
|
|
os << indent8 << data->commandString << '\n';
|
2018-03-15 17:16:40 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-26 12:24:23 +08:00
|
|
|
if (auto *assign = dyn_cast<SymbolAssignment>(subCmd)) {
|
|
|
|
if (assign->provide && !assign->sym)
|
2018-04-05 19:25:58 +08:00
|
|
|
continue;
|
2021-11-26 12:24:23 +08:00
|
|
|
writeHeader(os, assign->addr,
|
|
|
|
osec->getLMA() + assign->addr - osec->getVA(0),
|
|
|
|
assign->size, 1);
|
|
|
|
os << indent8 << assign->commandString << '\n';
|
2018-03-15 17:16:40 +08:00
|
|
|
continue;
|
|
|
|
}
|
2017-04-29 07:29:15 +08:00
|
|
|
}
|
|
|
|
}
|
2017-01-14 05:05:46 +08:00
|
|
|
}
|
2018-03-15 04:29:45 +08:00
|
|
|
|
2021-09-21 00:52:30 +08:00
|
|
|
void elf::writeWhyExtract() {
|
|
|
|
if (config->whyExtract.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::error_code ec;
|
|
|
|
raw_fd_ostream os(config->whyExtract, ec, sys::fs::OF_None);
|
|
|
|
if (ec) {
|
|
|
|
error("cannot open --why-extract= file " + config->whyExtract + ": " +
|
|
|
|
ec.message());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
os << "reference\textracted\tsymbol\n";
|
|
|
|
for (auto &entry : whyExtract) {
|
|
|
|
os << std::get<0>(entry) << '\t' << toString(std::get<1>(entry)) << '\t'
|
|
|
|
<< toString(std::get<2>(entry)) << '\n';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 04:29:45 +08:00
|
|
|
static void print(StringRef a, StringRef b) {
|
Make it possible to redirect not only errs() but also outs()
This change is for those who use lld as a library. Context:
https://reviews.llvm.org/D70287
This patch adds a new parmeter to lld::*::link() so that we can pass
an raw_ostream object representing stdout. Previously, lld::*::link()
took only an stderr object.
Justification for making stdoutOS and stderrOS mandatory: I wanted to
make link() functions to take stdout and stderr in that order.
However, if we change the function signature from
bool link(ArrayRef<const char *> args, bool canExitEarly,
raw_ostream &stderrOS = llvm::errs());
to
bool link(ArrayRef<const char *> args, bool canExitEarly,
raw_ostream &stdoutOS = llvm::outs(),
raw_ostream &stderrOS = llvm::errs());
, then the meaning of existing code that passes stderrOS silently
changes (stderrOS would be interpreted as stdoutOS). So, I chose to
make existing code not to compile, so that developers can fix their
code.
Differential Revision: https://reviews.llvm.org/D70292
2019-11-15 13:06:57 +08:00
|
|
|
lld::outs() << left_justify(a, 49) << " " << b << "\n";
|
2018-03-15 04:29:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Output a cross reference table to stdout. This is for --cref.
|
|
|
|
//
|
|
|
|
// For each global symbol, we print out a file that defines the symbol
|
|
|
|
// followed by files that uses that symbol. Here is an example.
|
|
|
|
//
|
|
|
|
// strlen /lib/x86_64-linux-gnu/libc.so.6
|
|
|
|
// tools/lld/tools/lld/CMakeFiles/lld.dir/lld.cpp.o
|
|
|
|
// lib/libLLVMSupport.a(PrettyStackTrace.cpp.o)
|
|
|
|
//
|
|
|
|
// In this case, strlen is defined by libc.so.6 and used by other two
|
|
|
|
// files.
|
2020-05-15 13:18:58 +08:00
|
|
|
void elf::writeCrossReferenceTable() {
|
2018-03-15 04:29:45 +08:00
|
|
|
if (!config->cref)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Collect symbols and files.
|
|
|
|
MapVector<Symbol *, SetVector<InputFile *>> map;
|
|
|
|
for (InputFile *file : objectFiles) {
|
|
|
|
for (Symbol *sym : file->getSymbols()) {
|
|
|
|
if (isa<SharedSymbol>(sym))
|
|
|
|
map[sym].insert(file);
|
|
|
|
if (auto *d = dyn_cast<Defined>(sym))
|
2019-05-29 11:55:20 +08:00
|
|
|
if (!d->isLocal() && (!d->section || d->section->isLive()))
|
2018-03-15 04:29:45 +08:00
|
|
|
map[d].insert(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print out a header.
|
Make it possible to redirect not only errs() but also outs()
This change is for those who use lld as a library. Context:
https://reviews.llvm.org/D70287
This patch adds a new parmeter to lld::*::link() so that we can pass
an raw_ostream object representing stdout. Previously, lld::*::link()
took only an stderr object.
Justification for making stdoutOS and stderrOS mandatory: I wanted to
make link() functions to take stdout and stderr in that order.
However, if we change the function signature from
bool link(ArrayRef<const char *> args, bool canExitEarly,
raw_ostream &stderrOS = llvm::errs());
to
bool link(ArrayRef<const char *> args, bool canExitEarly,
raw_ostream &stdoutOS = llvm::outs(),
raw_ostream &stderrOS = llvm::errs());
, then the meaning of existing code that passes stderrOS silently
changes (stderrOS would be interpreted as stdoutOS). So, I chose to
make existing code not to compile, so that developers can fix their
code.
Differential Revision: https://reviews.llvm.org/D70292
2019-11-15 13:06:57 +08:00
|
|
|
lld::outs() << "Cross Reference Table\n\n";
|
2018-03-15 04:29:45 +08:00
|
|
|
print("Symbol", "File");
|
|
|
|
|
|
|
|
// Print out a table.
|
|
|
|
for (auto kv : map) {
|
|
|
|
Symbol *sym = kv.first;
|
|
|
|
SetVector<InputFile *> &files = kv.second;
|
|
|
|
|
|
|
|
print(toString(*sym), toString(sym->file));
|
|
|
|
for (InputFile *file : files)
|
|
|
|
if (file != sym->file)
|
|
|
|
print("", toString(file));
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 16:31:18 +08:00
|
|
|
|
2020-05-15 13:18:58 +08:00
|
|
|
void elf::writeArchiveStats() {
|
2020-04-28 10:30:09 +08:00
|
|
|
if (config->printArchiveStats.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::error_code ec;
|
|
|
|
raw_fd_ostream os(config->printArchiveStats, ec, sys::fs::OF_None);
|
|
|
|
if (ec) {
|
|
|
|
error("--print-archive-stats=: cannot open " + config->printArchiveStats +
|
|
|
|
": " + ec.message());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
os << "members\tfetched\tarchive\n";
|
|
|
|
for (const ArchiveFile *f : archiveFiles)
|
|
|
|
os << f->getMemberCount() << '\t' << f->getFetchedMemberCount() << '\t'
|
|
|
|
<< f->getName() << '\n';
|
|
|
|
}
|