2015-05-29 03:09:30 +08:00
|
|
|
//===- InputFiles.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
|
2015-05-29 03:09:30 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-12-18 22:06:06 +08:00
|
|
|
#include "InputFiles.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "Chunks.h"
|
2016-04-22 01:14:10 +08:00
|
|
|
#include "Config.h"
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
#include "DebugTypes.h"
|
2016-07-26 10:00:42 +08:00
|
|
|
#include "Driver.h"
|
2016-12-10 05:55:24 +08:00
|
|
|
#include "SymbolTable.h"
|
2015-08-06 03:51:28 +08:00
|
|
|
#include "Symbols.h"
|
2019-11-15 06:16:21 +08:00
|
|
|
#include "lld/Common/DWARF.h"
|
[lld] unified COFF and ELF error handling on new Common/ErrorHandler
Summary:
The COFF linker and the ELF linker have long had similar but separate
Error.h and Error.cpp files to implement error handling. This change
introduces new error handling code in Common/ErrorHandler.h, changes the
COFF and ELF linkers to use it, and removes the old, separate
implementations.
Reviewers: ruiu
Reviewed By: ruiu
Subscribers: smeenai, jyknight, emaste, sdardis, nemanjai, nhaehnle, mgorny, javed.absar, kbarton, fedor.sergeev, llvm-commits
Differential Revision: https://reviews.llvm.org/D39259
llvm-svn: 316624
2017-10-26 06:28:38 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2016-12-18 22:06:06 +08:00
|
|
|
#include "llvm-c/lto.h"
|
2016-04-22 01:14:10 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Triple.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/COFF.h"
|
2019-02-23 09:46:18 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
|
|
|
|
#include "llvm/DebugInfo/CodeView/SymbolDeserializer.h"
|
|
|
|
#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
|
2020-05-09 21:58:15 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
|
2019-11-15 05:46:00 +08:00
|
|
|
#include "llvm/LTO/LTO.h"
|
2016-04-22 01:14:10 +08:00
|
|
|
#include "llvm/Object/Binary.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "llvm/Object/COFF.h"
|
2016-04-22 01:14:10 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2016-04-22 01:14:10 +08:00
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
2018-07-21 07:06:34 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2016-04-22 01:14:10 +08:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include <cstring>
|
|
|
|
#include <system_error>
|
|
|
|
#include <utility>
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2016-09-16 06:24:51 +08:00
|
|
|
using namespace llvm;
|
2015-07-09 04:22:50 +08:00
|
|
|
using namespace llvm::COFF;
|
2019-02-23 09:46:18 +08:00
|
|
|
using namespace llvm::codeview;
|
2015-05-29 03:09:30 +08:00
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::support::endian;
|
2020-02-20 09:05:42 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::coff;
|
2016-04-22 01:14:10 +08:00
|
|
|
|
2015-07-10 03:54:13 +08:00
|
|
|
using llvm::Triple;
|
2015-07-25 07:51:14 +08:00
|
|
|
using llvm::support::ulittle32_t;
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2019-10-10 19:27:58 +08:00
|
|
|
// Returns the last element of a path, which is supposed to be a filename.
|
|
|
|
static StringRef getBasename(StringRef path) {
|
|
|
|
return sys::path::filename(path, sys::path::Style::windows);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
|
2020-02-20 09:05:42 +08:00
|
|
|
std::string lld::toString(const coff::InputFile *file) {
|
2019-10-10 19:27:58 +08:00
|
|
|
if (!file)
|
|
|
|
return "<internal>";
|
|
|
|
if (file->parentName.empty() || file->kind() == coff::InputFile::ImportKind)
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(file->getName());
|
2019-10-10 19:27:58 +08:00
|
|
|
|
|
|
|
return (getBasename(file->parentName) + "(" + getBasename(file->getName()) +
|
|
|
|
")")
|
|
|
|
.str();
|
|
|
|
}
|
|
|
|
|
2017-07-27 08:45:26 +08:00
|
|
|
std::vector<ObjFile *> ObjFile::instances;
|
2020-05-09 21:58:15 +08:00
|
|
|
std::map<std::string, PDBInputFile *> PDBInputFile::instances;
|
2017-07-27 08:45:26 +08:00
|
|
|
std::vector<ImportFile *> ImportFile::instances;
|
|
|
|
std::vector<BitcodeFile *> BitcodeFile::instances;
|
|
|
|
|
2017-02-03 07:58:14 +08:00
|
|
|
/// Checks that Source is compatible with being a weak alias to Target.
|
|
|
|
/// If Source is Undefined and has no weak alias set, makes it a weak
|
|
|
|
/// alias to Target.
|
|
|
|
static void checkAndSetWeakAlias(SymbolTable *symtab, InputFile *f,
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *source, Symbol *target) {
|
2017-05-25 01:12:10 +08:00
|
|
|
if (auto *u = dyn_cast<Undefined>(source)) {
|
2018-10-04 02:31:53 +08:00
|
|
|
if (u->weakAlias && u->weakAlias != target) {
|
|
|
|
// Weak aliases as produced by GCC are named in the form
|
|
|
|
// .weak.<weaksymbol>.<othersymbol>, where <othersymbol> is the name
|
|
|
|
// of another symbol emitted near the weak symbol.
|
|
|
|
// Just use the definition from the first object file that defined
|
|
|
|
// this weak symbol.
|
|
|
|
if (config->mingw)
|
|
|
|
return;
|
2017-11-01 00:10:24 +08:00
|
|
|
symtab->reportDuplicate(source, f);
|
2018-10-04 02:31:53 +08:00
|
|
|
}
|
2017-02-03 07:58:14 +08:00
|
|
|
u->weakAlias = target;
|
2017-05-25 01:12:10 +08:00
|
|
|
}
|
2017-02-03 07:58:14 +08:00
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2019-09-04 04:32:16 +08:00
|
|
|
static bool ignoredSymbolName(StringRef name) {
|
|
|
|
return name == "@feat.00" || name == "@comp.id";
|
|
|
|
}
|
|
|
|
|
2016-09-16 06:24:51 +08:00
|
|
|
ArchiveFile::ArchiveFile(MemoryBufferRef m) : InputFile(ArchiveKind, m) {}
|
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
void ArchiveFile::parse() {
|
2015-06-01 05:04:56 +08:00
|
|
|
// Parse a MemoryBufferRef as an archive file.
|
2017-12-07 11:24:57 +08:00
|
|
|
file = CHECK(Archive::create(mb), this);
|
2015-05-29 03:09:30 +08:00
|
|
|
|
2016-12-10 05:55:24 +08:00
|
|
|
// Read the symbol table to construct Lazy objects.
|
|
|
|
for (const Archive::Symbol &sym : file->symbols())
|
2019-09-04 04:32:16 +08:00
|
|
|
symtab->addLazyArchive(this, sym);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a buffer pointing to a member file containing a given symbol.
|
2019-07-19 21:29:10 +08:00
|
|
|
void ArchiveFile::addMember(const Archive::Symbol &sym) {
|
2019-07-24 03:00:01 +08:00
|
|
|
const Archive::Child &c =
|
|
|
|
CHECK(sym.getMember(),
|
|
|
|
"could not get the member for symbol " + toCOFFString(sym));
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
// Return an empty buffer if we have already returned the same buffer.
|
2016-12-12 11:16:14 +08:00
|
|
|
if (!seen.insert(c.getChildOffset()).second)
|
2016-12-15 12:02:23 +08:00
|
|
|
return;
|
2016-07-26 10:00:42 +08:00
|
|
|
|
2019-07-19 21:29:10 +08:00
|
|
|
driver->enqueueArchiveMember(c, sym, getName());
|
2016-12-10 05:55:24 +08:00
|
|
|
}
|
2016-09-16 06:24:51 +08:00
|
|
|
|
2020-02-20 09:05:42 +08:00
|
|
|
std::vector<MemoryBufferRef> lld::coff::getArchiveMembers(Archive *file) {
|
2017-08-31 04:55:18 +08:00
|
|
|
std::vector<MemoryBufferRef> v;
|
|
|
|
Error err = Error::success();
|
2020-01-02 07:28:48 +08:00
|
|
|
for (const Archive::Child &c : file->children(err)) {
|
2017-08-31 04:55:18 +08:00
|
|
|
MemoryBufferRef mbref =
|
2017-12-07 06:08:17 +08:00
|
|
|
CHECK(c.getMemoryBufferRef(),
|
2017-08-31 04:55:18 +08:00
|
|
|
file->getFileName() +
|
|
|
|
": could not get the buffer for a child of the archive");
|
|
|
|
v.push_back(mbref);
|
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
fatal(file->getFileName() +
|
|
|
|
": Archive::children failed: " + toString(std::move(err)));
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2019-09-04 04:32:16 +08:00
|
|
|
void LazyObjFile::fetch() {
|
|
|
|
if (mb.getBuffer().empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
InputFile *file;
|
|
|
|
if (isBitcode(mb))
|
|
|
|
file = make<BitcodeFile>(mb, "", 0, std::move(symbols));
|
|
|
|
else
|
|
|
|
file = make<ObjFile>(mb, std::move(symbols));
|
|
|
|
mb = {};
|
|
|
|
symtab->addFile(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LazyObjFile::parse() {
|
|
|
|
if (isBitcode(this->mb)) {
|
|
|
|
// Bitcode file.
|
|
|
|
std::unique_ptr<lto::InputFile> obj =
|
|
|
|
CHECK(lto::InputFile::create(this->mb), this);
|
|
|
|
for (const lto::InputFile::Symbol &sym : obj->symbols()) {
|
|
|
|
if (!sym.isUndefined())
|
|
|
|
symtab->addLazyObject(this, sym.getName());
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Native object file.
|
|
|
|
std::unique_ptr<Binary> coffObjPtr = CHECK(createBinary(mb), this);
|
|
|
|
COFFObjectFile *coffObj = cast<COFFObjectFile>(coffObjPtr.get());
|
|
|
|
uint32_t numSymbols = coffObj->getNumberOfSymbols();
|
|
|
|
for (uint32_t i = 0; i < numSymbols; ++i) {
|
|
|
|
COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
|
|
|
|
if (coffSym.isUndefined() || !coffSym.isExternal() ||
|
|
|
|
coffSym.isWeakExternal())
|
|
|
|
continue;
|
2020-05-09 01:41:05 +08:00
|
|
|
StringRef name = check(coffObj->getSymbolName(coffSym));
|
2019-09-04 04:32:16 +08:00
|
|
|
if (coffSym.isAbsolute() && ignoredSymbolName(name))
|
|
|
|
continue;
|
|
|
|
symtab->addLazyObject(this, name);
|
|
|
|
i += coffSym.getNumberOfAuxSymbols();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-27 07:05:24 +08:00
|
|
|
void ObjFile::parse() {
|
2015-05-29 03:09:30 +08:00
|
|
|
// Parse a memory buffer as a COFF file.
|
2017-12-07 11:24:57 +08:00
|
|
|
std::unique_ptr<Binary> bin = CHECK(createBinary(mb), this);
|
2016-12-09 02:49:04 +08:00
|
|
|
|
|
|
|
if (auto *obj = dyn_cast<COFFObjectFile>(bin.get())) {
|
|
|
|
bin.release();
|
|
|
|
coffObj.reset(obj);
|
|
|
|
} else {
|
|
|
|
fatal(toString(this) + " is not a COFF file");
|
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
// Read section and symbol tables.
|
2015-08-06 22:58:50 +08:00
|
|
|
initializeChunks();
|
|
|
|
initializeSymbols();
|
2019-02-23 09:46:18 +08:00
|
|
|
initializeFlags();
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
initializeDependencies();
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2020-05-09 01:41:05 +08:00
|
|
|
const coff_section *ObjFile::getSection(uint32_t i) {
|
|
|
|
auto sec = coffObj->getSection(i);
|
|
|
|
if (!sec)
|
|
|
|
fatal("getSection failed: #" + Twine(i) + ": " + toString(sec.takeError()));
|
|
|
|
return *sec;
|
2019-01-30 10:17:27 +08:00
|
|
|
}
|
|
|
|
|
2017-11-28 09:30:07 +08:00
|
|
|
// We set SectionChunk pointers in the SparseChunks vector to this value
|
|
|
|
// temporarily to mark comdat sections as having an unknown resolution. As we
|
|
|
|
// walk the object file's symbol table, once we visit either a leader symbol or
|
|
|
|
// an associative section definition together with the parent comdat's leader,
|
|
|
|
// we set the pointer to either nullptr (to mark the section as discarded) or a
|
|
|
|
// valid SectionChunk for that section.
|
|
|
|
static SectionChunk *const pendingComdat = reinterpret_cast<SectionChunk *>(1);
|
|
|
|
|
2017-07-27 07:05:24 +08:00
|
|
|
void ObjFile::initializeChunks() {
|
2015-05-29 03:09:30 +08:00
|
|
|
uint32_t numSections = coffObj->getNumberOfSections();
|
|
|
|
sparseChunks.resize(numSections + 1);
|
|
|
|
for (uint32_t i = 1; i < numSections + 1; ++i) {
|
2019-01-30 10:17:27 +08:00
|
|
|
const coff_section *sec = getSection(i);
|
2017-11-28 09:30:07 +08:00
|
|
|
if (sec->Characteristics & IMAGE_SCN_LNK_COMDAT)
|
|
|
|
sparseChunks[i] = pendingComdat;
|
2017-06-21 01:14:09 +08:00
|
|
|
else
|
2018-03-16 05:14:02 +08:00
|
|
|
sparseChunks[i] = readSection(i, nullptr, "");
|
2017-11-28 09:30:07 +08:00
|
|
|
}
|
|
|
|
}
|
2017-06-21 01:14:09 +08:00
|
|
|
|
2017-11-28 09:30:07 +08:00
|
|
|
SectionChunk *ObjFile::readSection(uint32_t sectionNumber,
|
2018-03-16 05:14:02 +08:00
|
|
|
const coff_aux_section_definition *def,
|
|
|
|
StringRef leaderName) {
|
2019-01-30 10:17:27 +08:00
|
|
|
const coff_section *sec = getSection(sectionNumber);
|
2019-01-15 03:05:21 +08:00
|
|
|
|
|
|
|
StringRef name;
|
2019-05-02 18:32:03 +08:00
|
|
|
if (Expected<StringRef> e = coffObj->getSectionName(sec))
|
|
|
|
name = *e;
|
|
|
|
else
|
2017-11-28 09:30:07 +08:00
|
|
|
fatal("getSectionName failed: #" + Twine(sectionNumber) + ": " +
|
2019-05-02 18:32:03 +08:00
|
|
|
toString(e.takeError()));
|
2018-02-06 09:58:26 +08:00
|
|
|
|
2017-11-28 09:30:07 +08:00
|
|
|
if (name == ".drectve") {
|
|
|
|
ArrayRef<uint8_t> data;
|
2019-05-14 12:22:51 +08:00
|
|
|
cantFail(coffObj->getSectionContents(sec, data));
|
2019-03-30 05:00:22 +08:00
|
|
|
directives = StringRef((const char *)data.data(), data.size());
|
2017-11-28 09:30:07 +08:00
|
|
|
return nullptr;
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
2017-11-28 09:30:07 +08:00
|
|
|
|
2018-08-24 01:44:42 +08:00
|
|
|
if (name == ".llvm_addrsig") {
|
|
|
|
addrsigSec = sec;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-07-22 04:46:11 +08:00
|
|
|
if (name == ".llvm.call-graph-profile") {
|
|
|
|
callgraphSec = sec;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-11-28 09:30:07 +08:00
|
|
|
// Object files may have DWARF debug info or MS CodeView debug info
|
|
|
|
// (or both).
|
|
|
|
//
|
|
|
|
// DWARF sections don't need any special handling from the perspective
|
|
|
|
// of the linker; they are just a data section containing relocations.
|
|
|
|
// We can just link them to complete debug info.
|
|
|
|
//
|
2019-01-15 03:05:21 +08:00
|
|
|
// CodeView needs linker support. We need to interpret debug info,
|
|
|
|
// and then write it to a separate .pdb file.
|
2017-11-28 09:30:07 +08:00
|
|
|
|
2018-04-18 07:32:33 +08:00
|
|
|
// Ignore DWARF debug info unless /debug is given.
|
|
|
|
if (!config->debug && name.startswith(".debug_"))
|
2017-11-28 09:30:07 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
|
|
|
|
return nullptr;
|
|
|
|
auto *c = make<SectionChunk>(this, sec);
|
|
|
|
if (def)
|
|
|
|
c->checksum = def->CheckSum;
|
|
|
|
|
|
|
|
// CodeView sections are stored to a different vector because they are not
|
|
|
|
// linked in the regular manner.
|
|
|
|
if (c->isCodeView())
|
|
|
|
debugChunks.push_back(c);
|
2019-04-25 04:38:37 +08:00
|
|
|
else if (name == ".gfids$y")
|
2018-02-06 09:58:26 +08:00
|
|
|
guardFidChunks.push_back(c);
|
2020-11-18 10:02:13 +08:00
|
|
|
else if (name == ".giats$y")
|
|
|
|
guardIATChunks.push_back(c);
|
2019-04-25 04:38:37 +08:00
|
|
|
else if (name == ".gljmp$y")
|
2018-02-14 04:32:53 +08:00
|
|
|
guardLJmpChunks.push_back(c);
|
2018-02-06 09:58:26 +08:00
|
|
|
else if (name == ".sxdata")
|
2020-05-15 02:21:53 +08:00
|
|
|
sxDataChunks.push_back(c);
|
2018-05-12 06:21:36 +08:00
|
|
|
else if (config->tailMerge && sec->NumberOfRelocations == 0 &&
|
|
|
|
name == ".rdata" && leaderName.startswith("??_C@"))
|
2018-03-16 05:14:02 +08:00
|
|
|
// COFF sections that look like string literal sections (i.e. no
|
|
|
|
// relocations, in .rdata, leader symbol name matches the MSVC name mangling
|
|
|
|
// for string literals) are subject to string tail merging.
|
|
|
|
MergeChunk::addSection(c);
|
2019-08-30 14:56:33 +08:00
|
|
|
else if (name == ".rsrc" || name.startswith(".rsrc$"))
|
|
|
|
resourceChunks.push_back(c);
|
2017-11-28 09:30:07 +08:00
|
|
|
else
|
|
|
|
chunks.push_back(c);
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2019-08-30 14:56:33 +08:00
|
|
|
void ObjFile::includeResourceChunks() {
|
|
|
|
chunks.insert(chunks.end(), resourceChunks.begin(), resourceChunks.end());
|
|
|
|
}
|
|
|
|
|
2017-11-28 09:30:07 +08:00
|
|
|
void ObjFile::readAssociativeDefinition(
|
|
|
|
COFFSymbolRef sym, const coff_aux_section_definition *def) {
|
2018-08-07 05:26:09 +08:00
|
|
|
readAssociativeDefinition(sym, def, def->getNumber(sym.isBigObj()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjFile::readAssociativeDefinition(COFFSymbolRef sym,
|
|
|
|
const coff_aux_section_definition *def,
|
2019-01-23 10:07:10 +08:00
|
|
|
uint32_t parentIndex) {
|
|
|
|
SectionChunk *parent = sparseChunks[parentIndex];
|
2019-01-29 05:16:15 +08:00
|
|
|
int32_t sectionNumber = sym.getSectionNumber();
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2019-01-26 08:14:52 +08:00
|
|
|
auto diag = [&]() {
|
2020-05-09 01:41:05 +08:00
|
|
|
StringRef name = check(coffObj->getSymbolName(sym));
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2020-05-09 01:41:05 +08:00
|
|
|
StringRef parentName;
|
2019-01-30 10:17:27 +08:00
|
|
|
const coff_section *parentSec = getSection(parentIndex);
|
2019-05-02 18:32:03 +08:00
|
|
|
if (Expected<StringRef> e = coffObj->getSectionName(parentSec))
|
|
|
|
parentName = *e;
|
2019-01-29 05:16:15 +08:00
|
|
|
error(toString(this) + ": associative comdat " + name + " (sec " +
|
|
|
|
Twine(sectionNumber) + ") has invalid reference to section " +
|
|
|
|
parentName + " (sec " + Twine(parentIndex) + ")");
|
2019-01-26 08:14:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (parent == pendingComdat) {
|
|
|
|
// This can happen if an associative comdat refers to another associative
|
|
|
|
// comdat that appears after it (invalid per COFF spec) or to a section
|
|
|
|
// without any symbols.
|
|
|
|
diag();
|
2017-11-28 09:30:07 +08:00
|
|
|
return;
|
2019-01-23 10:07:10 +08:00
|
|
|
}
|
2017-11-28 09:30:07 +08:00
|
|
|
|
|
|
|
// Check whether the parent is prevailing. If it is, so are we, and we read
|
|
|
|
// the section; otherwise mark it as discarded.
|
|
|
|
if (parent) {
|
2019-01-26 08:14:52 +08:00
|
|
|
SectionChunk *c = readSection(sectionNumber, def, "");
|
|
|
|
sparseChunks[sectionNumber] = c;
|
2019-01-29 23:50:31 +08:00
|
|
|
if (c) {
|
|
|
|
c->selection = IMAGE_COMDAT_SELECT_ASSOCIATIVE;
|
|
|
|
parent->addAssociative(c);
|
|
|
|
}
|
2017-11-28 09:30:07 +08:00
|
|
|
} else {
|
|
|
|
sparseChunks[sectionNumber] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 05:26:09 +08:00
|
|
|
void ObjFile::recordPrevailingSymbolForMingw(
|
|
|
|
COFFSymbolRef sym, DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
|
|
|
|
// For comdat symbols in executable sections, where this is the copy
|
|
|
|
// of the section chunk we actually include instead of discarding it,
|
|
|
|
// add the symbol to a map to allow using it for implicitly
|
|
|
|
// associating .[px]data$<func> sections to it.
|
2020-07-25 17:25:19 +08:00
|
|
|
// Use the suffix from the .text$<func> instead of the leader symbol
|
|
|
|
// name, for cases where the names differ (i386 mangling/decorations,
|
|
|
|
// cases where the leader is a weak symbol named .weak.func.default*).
|
2018-08-07 05:26:09 +08:00
|
|
|
int32_t sectionNumber = sym.getSectionNumber();
|
|
|
|
SectionChunk *sc = sparseChunks[sectionNumber];
|
|
|
|
if (sc && sc->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) {
|
2020-07-25 17:25:19 +08:00
|
|
|
StringRef name = sc->getSectionName().split('$').second;
|
2018-08-07 05:26:09 +08:00
|
|
|
prevailingSectionMap[name] = sectionNumber;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjFile::maybeAssociateSEHForMingw(
|
|
|
|
COFFSymbolRef sym, const coff_aux_section_definition *def,
|
|
|
|
const DenseMap<StringRef, uint32_t> &prevailingSectionMap) {
|
2020-05-09 01:41:05 +08:00
|
|
|
StringRef name = check(coffObj->getSymbolName(sym));
|
2019-06-15 05:02:04 +08:00
|
|
|
if (name.consume_front(".pdata$") || name.consume_front(".xdata$") ||
|
|
|
|
name.consume_front(".eh_frame$")) {
|
|
|
|
// For MinGW, treat .[px]data$<func> and .eh_frame$<func> as implicitly
|
|
|
|
// associative to the symbol <func>.
|
2018-08-07 05:26:09 +08:00
|
|
|
auto parentSym = prevailingSectionMap.find(name);
|
|
|
|
if (parentSym != prevailingSectionMap.end())
|
|
|
|
readAssociativeDefinition(sym, def, parentSym->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 09:30:07 +08:00
|
|
|
Symbol *ObjFile::createRegular(COFFSymbolRef sym) {
|
|
|
|
SectionChunk *sc = sparseChunks[sym.getSectionNumber()];
|
|
|
|
if (sym.isExternal()) {
|
2020-05-09 01:41:05 +08:00
|
|
|
StringRef name = check(coffObj->getSymbolName(sym));
|
2017-11-28 09:30:07 +08:00
|
|
|
if (sc)
|
2019-10-18 18:43:15 +08:00
|
|
|
return symtab->addRegular(this, name, sym.getGeneric(), sc,
|
|
|
|
sym.getValue());
|
2018-10-06 03:43:16 +08:00
|
|
|
// For MinGW symbols named .weak.* that point to a discarded section,
|
|
|
|
// don't create an Undefined symbol. If nothing ever refers to the symbol,
|
|
|
|
// everything should be fine. If something actually refers to the symbol
|
|
|
|
// (e.g. the undefined weak alias), linking will fail due to undefined
|
|
|
|
// references at the end.
|
|
|
|
if (config->mingw && name.startswith(".weak."))
|
|
|
|
return nullptr;
|
2017-11-28 09:30:07 +08:00
|
|
|
return symtab->addUndefined(name, this, false);
|
|
|
|
}
|
|
|
|
if (sc)
|
2019-01-15 03:05:21 +08:00
|
|
|
return make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
|
2017-11-28 09:30:07 +08:00
|
|
|
/*IsExternal*/ false, sym.getGeneric(), sc);
|
|
|
|
return nullptr;
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2017-07-27 07:05:24 +08:00
|
|
|
void ObjFile::initializeSymbols() {
|
2015-05-29 03:09:30 +08:00
|
|
|
uint32_t numSymbols = coffObj->getNumberOfSymbols();
|
2017-11-21 02:52:53 +08:00
|
|
|
symbols.resize(numSymbols);
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
SmallVector<std::pair<Symbol *, uint32_t>, 8> weakAliases;
|
2017-11-28 09:30:07 +08:00
|
|
|
std::vector<uint32_t> pendingIndexes;
|
|
|
|
pendingIndexes.reserve(numSymbols);
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2018-08-07 05:26:09 +08:00
|
|
|
DenseMap<StringRef, uint32_t> prevailingSectionMap;
|
2017-11-28 09:30:07 +08:00
|
|
|
std::vector<const coff_aux_section_definition *> comdatDefs(
|
|
|
|
coffObj->getNumberOfSections() + 1);
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
for (uint32_t i = 0; i < numSymbols; ++i) {
|
2017-11-04 06:49:02 +08:00
|
|
|
COFFSymbolRef coffSym = check(coffObj->getSymbol(i));
|
2018-08-07 05:26:09 +08:00
|
|
|
bool prevailingComdat;
|
2017-11-04 06:49:02 +08:00
|
|
|
if (coffSym.isUndefined()) {
|
2017-11-28 09:30:07 +08:00
|
|
|
symbols[i] = createUndefined(coffSym);
|
2017-11-04 06:49:02 +08:00
|
|
|
} else if (coffSym.isWeakExternal()) {
|
2017-11-28 09:30:07 +08:00
|
|
|
symbols[i] = createUndefined(coffSym);
|
|
|
|
uint32_t tagIndex = coffSym.getAux<coff_aux_weak_external>()->TagIndex;
|
|
|
|
weakAliases.emplace_back(symbols[i], tagIndex);
|
2018-08-07 05:26:09 +08:00
|
|
|
} else if (Optional<Symbol *> optSym =
|
|
|
|
createDefined(coffSym, comdatDefs, prevailingComdat)) {
|
2017-11-28 09:30:07 +08:00
|
|
|
symbols[i] = *optSym;
|
2018-08-07 05:26:09 +08:00
|
|
|
if (config->mingw && prevailingComdat)
|
|
|
|
recordPrevailingSymbolForMingw(coffSym, prevailingSectionMap);
|
2015-06-30 06:16:21 +08:00
|
|
|
} else {
|
2017-11-28 09:30:07 +08:00
|
|
|
// createDefined() returns None if a symbol belongs to a section that
|
|
|
|
// was pending at the point when the symbol was read. This can happen in
|
|
|
|
// two cases:
|
|
|
|
// 1) section definition symbol for a comdat leader;
|
2019-01-23 10:07:10 +08:00
|
|
|
// 2) symbol belongs to a comdat section associated with another section.
|
2017-11-28 09:30:07 +08:00
|
|
|
// In both of these cases, we can expect the section to be resolved by
|
|
|
|
// the time we finish visiting the remaining symbols in the symbol
|
|
|
|
// table. So we postpone the handling of this symbol until that time.
|
|
|
|
pendingIndexes.push_back(i);
|
2015-06-30 06:16:21 +08:00
|
|
|
}
|
2017-11-04 06:49:02 +08:00
|
|
|
i += coffSym.getNumberOfAuxSymbols();
|
2017-11-28 09:30:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i : pendingIndexes) {
|
|
|
|
COFFSymbolRef sym = check(coffObj->getSymbol(i));
|
2019-01-15 03:05:21 +08:00
|
|
|
if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
|
2017-11-28 09:30:07 +08:00
|
|
|
if (def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
|
|
|
|
readAssociativeDefinition(sym, def);
|
2018-08-07 05:26:09 +08:00
|
|
|
else if (config->mingw)
|
|
|
|
maybeAssociateSEHForMingw(sym, def, prevailingSectionMap);
|
|
|
|
}
|
2018-07-27 04:14:50 +08:00
|
|
|
if (sparseChunks[sym.getSectionNumber()] == pendingComdat) {
|
2020-05-09 01:41:05 +08:00
|
|
|
StringRef name = check(coffObj->getSymbolName(sym));
|
2018-07-27 04:14:50 +08:00
|
|
|
log("comdat section " + name +
|
|
|
|
" without leader and unassociated, discarding");
|
|
|
|
continue;
|
|
|
|
}
|
2017-11-28 09:30:07 +08:00
|
|
|
symbols[i] = createRegular(sym);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
2017-05-25 01:12:32 +08:00
|
|
|
|
|
|
|
for (auto &kv : weakAliases) {
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *sym = kv.first;
|
2017-05-25 01:12:32 +08:00
|
|
|
uint32_t idx = kv.second;
|
2017-11-21 02:52:53 +08:00
|
|
|
checkAndSetWeakAlias(symtab, this, sym, symbols[idx]);
|
2017-05-25 01:12:32 +08:00
|
|
|
}
|
2020-06-02 09:46:51 +08:00
|
|
|
|
|
|
|
// Free the memory used by sparseChunks now that symbol loading is finished.
|
|
|
|
decltype(sparseChunks)().swap(sparseChunks);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *ObjFile::createUndefined(COFFSymbolRef sym) {
|
2020-05-09 01:41:05 +08:00
|
|
|
StringRef name = check(coffObj->getSymbolName(sym));
|
2017-11-01 00:10:24 +08:00
|
|
|
return symtab->addUndefined(name, this, sym.isWeakExternal());
|
2015-06-30 06:16:21 +08:00
|
|
|
}
|
|
|
|
|
2020-08-26 20:47:04 +08:00
|
|
|
static const coff_aux_section_definition *findSectionDef(COFFObjectFile *obj,
|
|
|
|
int32_t section) {
|
|
|
|
uint32_t numSymbols = obj->getNumberOfSymbols();
|
|
|
|
for (uint32_t i = 0; i < numSymbols; ++i) {
|
|
|
|
COFFSymbolRef sym = check(obj->getSymbol(i));
|
|
|
|
if (sym.getSectionNumber() != section)
|
|
|
|
continue;
|
|
|
|
if (const coff_aux_section_definition *def = sym.getSectionDefinition())
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjFile::handleComdatSelection(
|
|
|
|
COFFSymbolRef sym, COMDATType &selection, bool &prevailing,
|
|
|
|
DefinedRegular *leader,
|
|
|
|
const llvm::object::coff_aux_section_definition *def) {
|
2019-02-14 11:16:44 +08:00
|
|
|
if (prevailing)
|
|
|
|
return;
|
|
|
|
// There's already an existing comdat for this symbol: `Leader`.
|
|
|
|
// Use the comdats's selection field to determine if the new
|
|
|
|
// symbol in `Sym` should be discarded, produce a duplicate symbol
|
|
|
|
// error, etc.
|
|
|
|
|
|
|
|
SectionChunk *leaderChunk = nullptr;
|
|
|
|
COMDATType leaderSelection = IMAGE_COMDAT_SELECT_ANY;
|
|
|
|
|
|
|
|
if (leader->data) {
|
|
|
|
leaderChunk = leader->getChunk();
|
|
|
|
leaderSelection = leaderChunk->selection;
|
|
|
|
} else {
|
|
|
|
// FIXME: comdats from LTO files don't know their selection; treat them
|
|
|
|
// as "any".
|
|
|
|
selection = leaderSelection;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((selection == IMAGE_COMDAT_SELECT_ANY &&
|
|
|
|
leaderSelection == IMAGE_COMDAT_SELECT_LARGEST) ||
|
|
|
|
(selection == IMAGE_COMDAT_SELECT_LARGEST &&
|
|
|
|
leaderSelection == IMAGE_COMDAT_SELECT_ANY)) {
|
|
|
|
// cl.exe picks "any" for vftables when building with /GR- and
|
|
|
|
// "largest" when building with /GR. To be able to link object files
|
|
|
|
// compiled with each flag, "any" and "largest" are merged as "largest".
|
|
|
|
leaderSelection = selection = IMAGE_COMDAT_SELECT_LARGEST;
|
|
|
|
}
|
|
|
|
|
2020-01-23 16:43:56 +08:00
|
|
|
// GCCs __declspec(selectany) doesn't actually pick "any" but "same size as".
|
|
|
|
// Clang on the other hand picks "any". To be able to link two object files
|
|
|
|
// with a __declspec(selectany) declaration, one compiled with gcc and the
|
|
|
|
// other with clang, we merge them as proper "same size as"
|
|
|
|
if (config->mingw && ((selection == IMAGE_COMDAT_SELECT_ANY &&
|
|
|
|
leaderSelection == IMAGE_COMDAT_SELECT_SAME_SIZE) ||
|
|
|
|
(selection == IMAGE_COMDAT_SELECT_SAME_SIZE &&
|
|
|
|
leaderSelection == IMAGE_COMDAT_SELECT_ANY))) {
|
|
|
|
leaderSelection = selection = IMAGE_COMDAT_SELECT_SAME_SIZE;
|
|
|
|
}
|
|
|
|
|
2019-02-14 11:16:44 +08:00
|
|
|
// Other than that, comdat selections must match. This is a bit more
|
|
|
|
// strict than link.exe which allows merging "any" and "largest" if "any"
|
|
|
|
// is the first symbol the linker sees, and it allows merging "largest"
|
|
|
|
// with everything (!) if "largest" is the first symbol the linker sees.
|
|
|
|
// Making this symmetric independent of which selection is seen first
|
|
|
|
// seems better though.
|
|
|
|
// (This behavior matches ModuleLinker::getComdatResult().)
|
|
|
|
if (selection != leaderSelection) {
|
|
|
|
log(("conflicting comdat type for " + toString(*leader) + ": " +
|
|
|
|
Twine((int)leaderSelection) + " in " + toString(leader->getFile()) +
|
|
|
|
" and " + Twine((int)selection) + " in " + toString(this))
|
|
|
|
.str());
|
|
|
|
symtab->reportDuplicate(leader, this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (selection) {
|
|
|
|
case IMAGE_COMDAT_SELECT_NODUPLICATES:
|
|
|
|
symtab->reportDuplicate(leader, this);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IMAGE_COMDAT_SELECT_ANY:
|
|
|
|
// Nothing to do.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IMAGE_COMDAT_SELECT_SAME_SIZE:
|
2020-08-26 20:47:04 +08:00
|
|
|
if (leaderChunk->getSize() != getSection(sym)->SizeOfRawData) {
|
|
|
|
if (!config->mingw) {
|
|
|
|
symtab->reportDuplicate(leader, this);
|
|
|
|
} else {
|
|
|
|
const coff_aux_section_definition *leaderDef = findSectionDef(
|
|
|
|
leaderChunk->file->getCOFFObj(), leaderChunk->getSectionNumber());
|
|
|
|
if (!leaderDef || leaderDef->Length != def->Length)
|
|
|
|
symtab->reportDuplicate(leader, this);
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 11:16:44 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IMAGE_COMDAT_SELECT_EXACT_MATCH: {
|
|
|
|
SectionChunk newChunk(this, getSection(sym));
|
|
|
|
// link.exe only compares section contents here and doesn't complain
|
|
|
|
// if the two comdat sections have e.g. different alignment.
|
|
|
|
// Match that.
|
|
|
|
if (leaderChunk->getContents() != newChunk.getContents())
|
2019-10-18 18:43:15 +08:00
|
|
|
symtab->reportDuplicate(leader, this, &newChunk, sym.getValue());
|
2019-02-14 11:16:44 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IMAGE_COMDAT_SELECT_ASSOCIATIVE:
|
|
|
|
// createDefined() is never called for IMAGE_COMDAT_SELECT_ASSOCIATIVE.
|
|
|
|
// (This means lld-link doesn't produce duplicate symbol errors for
|
|
|
|
// associative comdats while link.exe does, but associate comdats
|
|
|
|
// are never extern in practice.)
|
|
|
|
llvm_unreachable("createDefined not called for associative comdats");
|
|
|
|
|
|
|
|
case IMAGE_COMDAT_SELECT_LARGEST:
|
|
|
|
if (leaderChunk->getSize() < getSection(sym)->SizeOfRawData) {
|
|
|
|
// Replace the existing comdat symbol with the new one.
|
2020-05-09 01:41:05 +08:00
|
|
|
StringRef name = check(coffObj->getSymbolName(sym));
|
2019-02-14 11:16:44 +08:00
|
|
|
// FIXME: This is incorrect: With /opt:noref, the previous sections
|
|
|
|
// make it into the final executable as well. Correct handling would
|
|
|
|
// be to undo reading of the whole old section that's being replaced,
|
|
|
|
// or doing one pass that determines what the final largest comdat
|
|
|
|
// is for all IMAGE_COMDAT_SELECT_LARGEST comdats and then reading
|
|
|
|
// only the largest one.
|
|
|
|
replaceSymbol<DefinedRegular>(leader, this, name, /*IsCOMDAT*/ true,
|
|
|
|
/*IsExternal*/ true, sym.getGeneric(),
|
|
|
|
nullptr);
|
|
|
|
prevailing = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IMAGE_COMDAT_SELECT_NEWEST:
|
|
|
|
llvm_unreachable("should have been rejected earlier");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 09:30:07 +08:00
|
|
|
Optional<Symbol *> ObjFile::createDefined(
|
|
|
|
COFFSymbolRef sym,
|
2018-08-07 05:26:09 +08:00
|
|
|
std::vector<const coff_aux_section_definition *> &comdatDefs,
|
|
|
|
bool &prevailing) {
|
|
|
|
prevailing = false;
|
2020-05-09 01:41:05 +08:00
|
|
|
auto getName = [&]() { return check(coffObj->getSymbolName(sym)); };
|
2018-07-25 06:52:11 +08:00
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
if (sym.isCommon()) {
|
2017-05-19 01:03:49 +08:00
|
|
|
auto *c = make<CommonChunk>(sym);
|
2015-05-29 03:09:30 +08:00
|
|
|
chunks.push_back(c);
|
2018-07-25 06:52:11 +08:00
|
|
|
return symtab->addCommon(this, getName(), sym.getValue(), sym.getGeneric(),
|
|
|
|
c);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
2018-07-25 06:52:11 +08:00
|
|
|
|
2015-06-09 03:43:59 +08:00
|
|
|
if (sym.isAbsolute()) {
|
2018-07-25 06:52:11 +08:00
|
|
|
StringRef name = getName();
|
|
|
|
|
2019-09-04 04:32:16 +08:00
|
|
|
if (name == "@feat.00")
|
2019-08-31 07:24:41 +08:00
|
|
|
feat00Flags = sym.getValue();
|
2019-09-04 04:32:16 +08:00
|
|
|
// Skip special symbols.
|
|
|
|
if (ignoredSymbolName(name))
|
2019-08-31 07:24:41 +08:00
|
|
|
return nullptr;
|
2018-07-25 06:52:11 +08:00
|
|
|
|
2016-12-10 05:55:24 +08:00
|
|
|
if (sym.isExternal())
|
2017-11-01 00:10:24 +08:00
|
|
|
return symtab->addAbsolute(name, sym);
|
2018-07-25 06:52:11 +08:00
|
|
|
return make<DefinedAbsolute>(name, sym);
|
2015-06-09 03:43:59 +08:00
|
|
|
}
|
2018-07-25 06:52:11 +08:00
|
|
|
|
2016-03-16 00:47:28 +08:00
|
|
|
int32_t sectionNumber = sym.getSectionNumber();
|
|
|
|
if (sectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
|
2015-06-24 08:05:50 +08:00
|
|
|
return nullptr;
|
2015-06-30 05:32:37 +08:00
|
|
|
|
2016-03-16 00:47:28 +08:00
|
|
|
if (llvm::COFF::isReservedSectionNumber(sectionNumber))
|
2018-07-25 06:52:11 +08:00
|
|
|
fatal(toString(this) + ": " + getName() +
|
2018-04-26 07:33:19 +08:00
|
|
|
" should not refer to special section " + Twine(sectionNumber));
|
2016-03-16 00:47:28 +08:00
|
|
|
|
|
|
|
if ((uint32_t)sectionNumber >= sparseChunks.size())
|
2018-07-25 06:52:11 +08:00
|
|
|
fatal(toString(this) + ": " + getName() +
|
2018-04-26 07:33:19 +08:00
|
|
|
" should not refer to non-existent section " + Twine(sectionNumber));
|
2016-03-16 00:47:28 +08:00
|
|
|
|
2019-01-30 10:17:27 +08:00
|
|
|
// Comdat handling.
|
|
|
|
// A comdat symbol consists of two symbol table entries.
|
|
|
|
// The first symbol entry has the name of the section (e.g. .text), fixed
|
2019-10-10 10:04:56 +08:00
|
|
|
// values for the other fields, and one auxiliary record.
|
2019-01-30 10:17:27 +08:00
|
|
|
// The second symbol entry has the name of the comdat symbol, called the
|
|
|
|
// "comdat leader".
|
|
|
|
// When this function is called for the first symbol entry of a comdat,
|
2019-07-16 16:26:38 +08:00
|
|
|
// it sets comdatDefs and returns None, and when it's called for the second
|
|
|
|
// symbol entry it reads comdatDefs and then sets it back to nullptr.
|
2019-01-30 10:17:27 +08:00
|
|
|
|
|
|
|
// Handle comdat leader.
|
2017-11-28 09:30:07 +08:00
|
|
|
if (const coff_aux_section_definition *def = comdatDefs[sectionNumber]) {
|
|
|
|
comdatDefs[sectionNumber] = nullptr;
|
2019-01-30 10:17:27 +08:00
|
|
|
DefinedRegular *leader;
|
|
|
|
|
2017-11-28 09:30:07 +08:00
|
|
|
if (sym.isExternal()) {
|
|
|
|
std::tie(leader, prevailing) =
|
2018-07-25 06:52:11 +08:00
|
|
|
symtab->addComdat(this, getName(), sym.getGeneric());
|
2017-11-28 09:30:07 +08:00
|
|
|
} else {
|
2019-01-15 03:05:21 +08:00
|
|
|
leader = make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
|
2017-11-28 09:30:07 +08:00
|
|
|
/*IsExternal*/ false, sym.getGeneric());
|
|
|
|
prevailing = true;
|
|
|
|
}
|
2018-07-25 06:52:11 +08:00
|
|
|
|
2019-01-30 10:17:27 +08:00
|
|
|
if (def->Selection < (int)IMAGE_COMDAT_SELECT_NODUPLICATES ||
|
|
|
|
// Intentionally ends at IMAGE_COMDAT_SELECT_LARGEST: link.exe
|
|
|
|
// doesn't understand IMAGE_COMDAT_SELECT_NEWEST either.
|
|
|
|
def->Selection > (int)IMAGE_COMDAT_SELECT_LARGEST) {
|
|
|
|
fatal("unknown comdat type " + std::to_string((int)def->Selection) +
|
|
|
|
" for " + getName() + " in " + toString(this));
|
|
|
|
}
|
|
|
|
COMDATType selection = (COMDATType)def->Selection;
|
|
|
|
|
2019-07-10 17:10:01 +08:00
|
|
|
if (leader->isCOMDAT)
|
2020-08-26 20:47:04 +08:00
|
|
|
handleComdatSelection(sym, selection, prevailing, leader, def);
|
2019-01-30 10:17:27 +08:00
|
|
|
|
2017-11-28 09:30:07 +08:00
|
|
|
if (prevailing) {
|
2018-07-25 06:52:11 +08:00
|
|
|
SectionChunk *c = readSection(sectionNumber, def, getName());
|
2017-11-28 09:30:07 +08:00
|
|
|
sparseChunks[sectionNumber] = c;
|
|
|
|
c->sym = cast<DefinedRegular>(leader);
|
2019-01-30 10:17:27 +08:00
|
|
|
c->selection = selection;
|
2017-11-28 09:30:07 +08:00
|
|
|
cast<DefinedRegular>(leader)->data = &c->repl;
|
|
|
|
} else {
|
|
|
|
sparseChunks[sectionNumber] = nullptr;
|
|
|
|
}
|
|
|
|
return leader;
|
|
|
|
}
|
2015-06-30 05:32:37 +08:00
|
|
|
|
2019-01-23 10:07:10 +08:00
|
|
|
// Prepare to handle the comdat leader symbol by setting the section's
|
|
|
|
// ComdatDefs pointer if we encounter a non-associative comdat.
|
2017-11-28 09:30:07 +08:00
|
|
|
if (sparseChunks[sectionNumber] == pendingComdat) {
|
2019-01-15 03:05:21 +08:00
|
|
|
if (const coff_aux_section_definition *def = sym.getSectionDefinition()) {
|
2019-01-23 10:07:10 +08:00
|
|
|
if (def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE)
|
2017-11-28 09:30:07 +08:00
|
|
|
comdatDefs[sectionNumber] = def;
|
|
|
|
}
|
|
|
|
return None;
|
2019-01-23 10:07:10 +08:00
|
|
|
}
|
2019-01-15 03:05:21 +08:00
|
|
|
|
2017-11-28 09:30:07 +08:00
|
|
|
return createRegular(sym);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2017-07-27 07:05:24 +08:00
|
|
|
MachineTypes ObjFile::getMachineType() {
|
2015-07-10 03:54:13 +08:00
|
|
|
if (coffObj)
|
|
|
|
return static_cast<MachineTypes>(coffObj->getMachine());
|
|
|
|
return IMAGE_FILE_MACHINE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2019-02-23 09:46:18 +08:00
|
|
|
ArrayRef<uint8_t> ObjFile::getDebugSection(StringRef secName) {
|
|
|
|
if (SectionChunk *sec = SectionChunk::findByName(debugChunks, secName))
|
|
|
|
return sec->consumeDebugMagic();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-10-10 10:04:56 +08:00
|
|
|
// OBJ files systematically store critical information in a .debug$S stream,
|
2019-02-23 09:46:18 +08:00
|
|
|
// even if the TU was compiled with no debug info. At least two records are
|
|
|
|
// always there. S_OBJNAME stores a 32-bit signature, which is loaded into the
|
|
|
|
// PCHSignature member. S_COMPILE3 stores compile-time cmd-line flags. This is
|
2019-07-16 16:26:38 +08:00
|
|
|
// currently used to initialize the hotPatchable member.
|
2019-02-23 09:46:18 +08:00
|
|
|
void ObjFile::initializeFlags() {
|
|
|
|
ArrayRef<uint8_t> data = getDebugSection(".debug$S");
|
|
|
|
if (data.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
DebugSubsectionArray subsections;
|
|
|
|
|
|
|
|
BinaryStreamReader reader(data, support::little);
|
|
|
|
ExitOnError exitOnErr;
|
|
|
|
exitOnErr(reader.readArray(subsections, data.size()));
|
|
|
|
|
|
|
|
for (const DebugSubsectionRecord &ss : subsections) {
|
|
|
|
if (ss.kind() != DebugSubsectionKind::Symbols)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned offset = 0;
|
|
|
|
|
|
|
|
// Only parse the first two records. We are only looking for S_OBJNAME
|
|
|
|
// and S_COMPILE3, and they usually appear at the beginning of the
|
|
|
|
// stream.
|
|
|
|
for (unsigned i = 0; i < 2; ++i) {
|
|
|
|
Expected<CVSymbol> sym = readSymbolFromStream(ss.getRecordData(), offset);
|
|
|
|
if (!sym) {
|
|
|
|
consumeError(sym.takeError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sym->kind() == SymbolKind::S_COMPILE3) {
|
|
|
|
auto cs =
|
|
|
|
cantFail(SymbolDeserializer::deserializeAs<Compile3Sym>(sym.get()));
|
|
|
|
hotPatchable =
|
|
|
|
(cs.Flags & CompileSym3Flags::HotPatch) != CompileSym3Flags::None;
|
|
|
|
}
|
|
|
|
if (sym->kind() == SymbolKind::S_OBJNAME) {
|
|
|
|
auto objName = cantFail(SymbolDeserializer::deserializeAs<ObjNameSym>(
|
|
|
|
sym.get()));
|
|
|
|
pchSignature = objName.Signature;
|
|
|
|
}
|
|
|
|
offset += sym->length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
// Depending on the compilation flags, OBJs can refer to external files,
|
|
|
|
// necessary to merge this OBJ into the final PDB. We currently support two
|
|
|
|
// types of external files: Precomp/PCH OBJs, when compiling with /Yc and /Yu.
|
|
|
|
// And PDB type servers, when compiling with /Zi. This function extracts these
|
|
|
|
// dependencies and makes them available as a TpiSource interface (see
|
lld-link: Reject more than one resource .obj file
Users are exepcted to pass all .res files to the linker, which then
merges all the resource in all .res files into a tree structure and then
converts the final tree structure to a .obj file with .rsrc$01 and
.rsrc$02 sections and then links that.
If the user instead passes several .obj files containing such resources,
the correct thing to do would be to have custom code to merge the trees
in the resource sections instead of doing normal section merging -- but
link.exe rejects if multiple resource obj files are passed in with
LNK4078, so let lld-link do that too instead of silently writing broken
.rsrc sections in that case.
The only real way to run into this is if users manually convert .res
files to .obj files by running cvtres and then handing the resulting
.obj files to lld-link instead, which in practice likely never happens.
(lld-link is slightly stricter than link.exe now: If link.exe is passed
one .obj file created by cvtres, and a .res file, for some reason it
just emits a warning instead of an error and outputs strange looking
data. lld-link now errors out on mixed input like this.)
One way users could accidentally run into this is the following
scenario: If a .res file is passed to lib.exe, then lib.exe calls
cvtres.exe on the .res file before putting it in the output .lib.
(llvm-lib currently doesn't do this.)
link.exe's /wholearchive seems to only add obj files referenced from the
static library index, but lld-link current really adds all files in the
archive. So if lld-link /wholearchive is used with .lib files produced
by lib.exe and .res files were among the files handed to lib.exe, we
previously silently produced invalid output, but now we error out.
link.exe's /wholearchive semantics on the other hand mean that it
wouldn't load the resource object files from the .lib file at all.
Since this scenario is probably still an unlikely corner case,
the difference in behavior here seems fine -- and lld-link might have to
change to use link.exe's /wholearchive semantics in the future anyways.
Vaguely related to PR42180.
Differential Revision: https://reviews.llvm.org/D63109
llvm-svn: 363078
2019-06-11 23:22:28 +08:00
|
|
|
// DebugTypes.h). Both cases only happen with cl.exe: clang-cl produces regular
|
|
|
|
// output even with /Yc and /Yu and with /Zi.
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
void ObjFile::initializeDependencies() {
|
|
|
|
if (!config->debug)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool isPCH = false;
|
|
|
|
|
|
|
|
ArrayRef<uint8_t> data = getDebugSection(".debug$P");
|
|
|
|
if (!data.empty())
|
|
|
|
isPCH = true;
|
|
|
|
else
|
|
|
|
data = getDebugSection(".debug$T");
|
|
|
|
|
2020-06-04 09:08:55 +08:00
|
|
|
// Don't make a TpiSource for objects with no debug info. If the object has
|
|
|
|
// symbols but no types, make a plain, empty TpiSource anyway, because it
|
|
|
|
// simplifies adding the symbols later.
|
|
|
|
if (data.empty()) {
|
|
|
|
if (!debugChunks.empty())
|
|
|
|
debugTypesObj = makeTpiSource(this);
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
return;
|
2020-06-04 09:08:55 +08:00
|
|
|
}
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
|
2020-05-09 21:58:15 +08:00
|
|
|
// Get the first type record. It will indicate if this object uses a type
|
|
|
|
// server (/Zi) or a PCH file (/Yu).
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
CVTypeArray types;
|
|
|
|
BinaryStreamReader reader(data, support::little);
|
|
|
|
cantFail(reader.readArray(types, reader.getLength()));
|
|
|
|
CVTypeArray::Iterator firstType = types.begin();
|
|
|
|
if (firstType == types.end())
|
|
|
|
return;
|
|
|
|
|
2019-11-15 06:27:48 +08:00
|
|
|
// Remember the .debug$T or .debug$P section.
|
|
|
|
debugTypes = data;
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
|
2020-05-09 21:58:15 +08:00
|
|
|
// This object file is a PCH file that others will depend on.
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
if (isPCH) {
|
|
|
|
debugTypesObj = makePrecompSource(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:58:15 +08:00
|
|
|
// This object file was compiled with /Zi. Enqueue the PDB dependency.
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
if (firstType->kind() == LF_TYPESERVER2) {
|
|
|
|
TypeServer2Record ts = cantFail(
|
|
|
|
TypeDeserializer::deserializeAs<TypeServer2Record>(firstType->data()));
|
2020-05-09 21:58:15 +08:00
|
|
|
debugTypesObj = makeUseTypeServerSource(this, ts);
|
|
|
|
PDBInputFile::enqueue(ts.getName(), this);
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:58:15 +08:00
|
|
|
// This object was compiled with /Yu. It uses types from another object file
|
|
|
|
// with a matching signature.
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
if (firstType->kind() == LF_PRECOMP) {
|
|
|
|
PrecompRecord precomp = cantFail(
|
|
|
|
TypeDeserializer::deserializeAs<PrecompRecord>(firstType->data()));
|
2020-05-09 21:58:15 +08:00
|
|
|
debugTypesObj = makeUsePrecompSource(this, precomp);
|
2020-09-16 09:50:34 +08:00
|
|
|
// Drop the LF_PRECOMP record from the input stream.
|
|
|
|
debugTypes = debugTypes.drop_front(firstType->RecordData.size());
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:58:15 +08:00
|
|
|
// This is a plain old object file.
|
[LLD][COFF] Early dependency detection
We introduce a new class hierarchy for debug types merging (in DebugTypes.h). The end-goal is to parallelize the type merging - please see the plan in D59226.
Previously, dependency discovery was done on the fly, much later, during the type merging loop. Unfortunately, parallelizing the type merging requires the dependencies to be merged in first, before any dependent ObjFile, thus this early discovery.
The overall intention for this path is to discover debug information dependencies at a much earlier stage, when processing input files. Currently, two types of dependency are supported: PDB type servers (when compiling with MSVC /Zi) and precompiled headers OBJs (when compiling with MSVC /Yc and /Yu). Once discovered, an explicit link is added into the dependent ObjFile, through the new debug types class hierarchy introduced in DebugTypes.h.
Differential Revision: https://reviews.llvm.org/D59053
llvm-svn: 357383
2019-04-01 21:36:59 +08:00
|
|
|
debugTypesObj = makeTpiSource(this);
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:58:15 +08:00
|
|
|
// Make a PDB path assuming the PDB is in the same folder as the OBJ
|
|
|
|
static std::string getPdbBaseName(ObjFile *file, StringRef tSPath) {
|
|
|
|
StringRef localPath =
|
|
|
|
!file->parentName.empty() ? file->parentName : file->getName();
|
|
|
|
SmallString<128> path = sys::path::parent_path(localPath);
|
|
|
|
|
|
|
|
// Currently, type server PDBs are only created by MSVC cl, which only runs
|
|
|
|
// on Windows, so we can assume type server paths are Windows style.
|
|
|
|
sys::path::append(path,
|
|
|
|
sys::path::filename(tSPath, sys::path::Style::windows));
|
|
|
|
return std::string(path.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// The casing of the PDB path stamped in the OBJ can differ from the actual path
|
|
|
|
// on disk. With this, we ensure to always use lowercase as a key for the
|
|
|
|
// PDBInputFile::instances map, at least on Windows.
|
|
|
|
static std::string normalizePdbPath(StringRef path) {
|
|
|
|
#if defined(_WIN32)
|
|
|
|
return path.lower();
|
|
|
|
#else // LINUX
|
|
|
|
return std::string(path);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// If existing, return the actual PDB path on disk.
|
|
|
|
static Optional<std::string> findPdbPath(StringRef pdbPath,
|
|
|
|
ObjFile *dependentFile) {
|
|
|
|
// Ensure the file exists before anything else. In some cases, if the path
|
|
|
|
// points to a removable device, Driver::enqueuePath() would fail with an
|
|
|
|
// error (EAGAIN, "resource unavailable try again") which we want to skip
|
|
|
|
// silently.
|
|
|
|
if (llvm::sys::fs::exists(pdbPath))
|
|
|
|
return normalizePdbPath(pdbPath);
|
|
|
|
std::string ret = getPdbBaseName(dependentFile, pdbPath);
|
|
|
|
if (llvm::sys::fs::exists(ret))
|
|
|
|
return normalizePdbPath(ret);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
PDBInputFile::PDBInputFile(MemoryBufferRef m) : InputFile(PDBKind, m) {}
|
|
|
|
|
|
|
|
PDBInputFile::~PDBInputFile() = default;
|
|
|
|
|
|
|
|
PDBInputFile *PDBInputFile::findFromRecordPath(StringRef path,
|
|
|
|
ObjFile *fromFile) {
|
|
|
|
auto p = findPdbPath(path.str(), fromFile);
|
|
|
|
if (!p)
|
|
|
|
return nullptr;
|
|
|
|
auto it = PDBInputFile::instances.find(*p);
|
|
|
|
if (it != PDBInputFile::instances.end())
|
|
|
|
return it->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PDBInputFile::enqueue(StringRef path, ObjFile *fromFile) {
|
|
|
|
auto p = findPdbPath(path.str(), fromFile);
|
|
|
|
if (!p)
|
|
|
|
return;
|
|
|
|
auto it = PDBInputFile::instances.emplace(*p, nullptr);
|
|
|
|
if (!it.second)
|
|
|
|
return; // already scheduled for load
|
|
|
|
driver->enqueuePDB(*p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PDBInputFile::parse() {
|
|
|
|
PDBInputFile::instances[mb.getBufferIdentifier().str()] = this;
|
|
|
|
|
|
|
|
std::unique_ptr<pdb::IPDBSession> thisSession;
|
|
|
|
loadErr.emplace(pdb::NativeSession::createFromPdb(
|
|
|
|
MemoryBuffer::getMemBuffer(mb, false), thisSession));
|
|
|
|
if (*loadErr)
|
|
|
|
return; // fail silently at this point - the error will be handled later,
|
|
|
|
// when merging the debug type stream
|
|
|
|
|
|
|
|
session.reset(static_cast<pdb::NativeSession *>(thisSession.release()));
|
|
|
|
|
|
|
|
pdb::PDBFile &pdbFile = session->getPDBFile();
|
|
|
|
auto expectedInfo = pdbFile.getPDBInfoStream();
|
|
|
|
// All PDB Files should have an Info stream.
|
|
|
|
if (!expectedInfo) {
|
|
|
|
loadErr.emplace(expectedInfo.takeError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
debugTypesObj = makeTypeServerSource(this);
|
|
|
|
}
|
|
|
|
|
2019-10-18 18:43:15 +08:00
|
|
|
// Used only for DWARF debug info, which is not common (except in MinGW
|
|
|
|
// environments). This returns an optional pair of file name and line
|
|
|
|
// number for where the variable was defined.
|
|
|
|
Optional<std::pair<StringRef, uint32_t>>
|
|
|
|
ObjFile::getVariableLocation(StringRef var) {
|
|
|
|
if (!dwarf) {
|
2019-10-21 16:01:52 +08:00
|
|
|
dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
|
2019-10-18 18:43:15 +08:00
|
|
|
if (!dwarf)
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
if (config->machine == I386)
|
|
|
|
var.consume_front("_");
|
2019-10-21 16:01:52 +08:00
|
|
|
Optional<std::pair<std::string, unsigned>> ret = dwarf->getVariableLoc(var);
|
|
|
|
if (!ret)
|
2019-10-18 18:43:15 +08:00
|
|
|
return None;
|
2019-10-21 16:01:52 +08:00
|
|
|
return std::make_pair(saver.save(ret->first), ret->second);
|
2019-10-18 18:43:15 +08:00
|
|
|
}
|
|
|
|
|
2019-10-21 16:01:59 +08:00
|
|
|
// Used only for DWARF debug info, which is not common (except in MinGW
|
|
|
|
// environments).
|
|
|
|
Optional<DILineInfo> ObjFile::getDILineInfo(uint32_t offset,
|
|
|
|
uint32_t sectionIndex) {
|
|
|
|
if (!dwarf) {
|
|
|
|
dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
|
|
|
|
if (!dwarf)
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dwarf->getDILineInfo(offset, sectionIndex);
|
|
|
|
}
|
|
|
|
|
2020-04-03 18:45:18 +08:00
|
|
|
static StringRef ltrim1(StringRef s, const char *chars) {
|
2015-07-10 04:22:41 +08:00
|
|
|
if (!s.empty() && strchr(chars, s[0]))
|
|
|
|
return s.substr(1);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
void ImportFile::parse() {
|
2015-06-01 05:04:56 +08:00
|
|
|
const char *buf = mb.getBufferStart();
|
2015-05-29 03:09:30 +08:00
|
|
|
const auto *hdr = reinterpret_cast<const coff_import_header *>(buf);
|
|
|
|
|
|
|
|
// Check if the total size is valid.
|
2019-06-14 22:03:08 +08:00
|
|
|
if (mb.getBufferSize() != sizeof(*hdr) + hdr->SizeOfData)
|
2016-07-15 07:37:14 +08:00
|
|
|
fatal("broken import library");
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
// Read names and create an __imp_ symbol.
|
2017-05-19 01:03:49 +08:00
|
|
|
StringRef name = saver.save(StringRef(buf + sizeof(*hdr)));
|
|
|
|
StringRef impName = saver.save("__imp_" + name);
|
2015-08-17 16:30:31 +08:00
|
|
|
const char *nameStart = buf + sizeof(coff_import_header) + name.size() + 1;
|
2020-01-29 03:23:46 +08:00
|
|
|
dllName = std::string(StringRef(nameStart));
|
2015-07-09 04:22:50 +08:00
|
|
|
StringRef extName;
|
|
|
|
switch (hdr->getNameType()) {
|
|
|
|
case IMPORT_ORDINAL:
|
|
|
|
extName = "";
|
|
|
|
break;
|
|
|
|
case IMPORT_NAME:
|
|
|
|
extName = name;
|
|
|
|
break;
|
|
|
|
case IMPORT_NAME_NOPREFIX:
|
2015-07-10 04:22:41 +08:00
|
|
|
extName = ltrim1(name, "?@_");
|
2015-07-09 04:22:50 +08:00
|
|
|
break;
|
|
|
|
case IMPORT_NAME_UNDECORATE:
|
2015-07-10 04:22:41 +08:00
|
|
|
extName = ltrim1(name, "?@_");
|
2015-07-09 04:22:50 +08:00
|
|
|
extName = extName.substr(0, extName.find('@'));
|
|
|
|
break;
|
|
|
|
}
|
2016-12-10 05:55:24 +08:00
|
|
|
|
|
|
|
this->hdr = hdr;
|
|
|
|
externalName = extName;
|
|
|
|
|
2017-09-02 07:35:43 +08:00
|
|
|
impSym = symtab->addImportData(impName, this);
|
2018-10-19 14:39:36 +08:00
|
|
|
// If this was a duplicate, we logged an error but may continue;
|
2019-07-16 16:26:38 +08:00
|
|
|
// in this case, impSym is nullptr.
|
2018-10-19 14:39:36 +08:00
|
|
|
if (!impSym)
|
|
|
|
return;
|
2017-09-02 06:12:10 +08:00
|
|
|
|
2017-05-22 14:01:37 +08:00
|
|
|
if (hdr->getType() == llvm::COFF::IMPORT_CONST)
|
2017-09-02 07:35:43 +08:00
|
|
|
static_cast<void>(symtab->addImportData(name, this));
|
2015-05-29 03:09:30 +08:00
|
|
|
|
|
|
|
// If type is function, we need to create a thunk which jump to an
|
|
|
|
// address pointed by the __imp_ symbol. (This allows you to call
|
|
|
|
// DLL functions just like regular non-DLL functions.)
|
2017-09-02 06:12:10 +08:00
|
|
|
if (hdr->getType() == llvm::COFF::IMPORT_CODE)
|
2018-07-10 18:40:11 +08:00
|
|
|
thunkSym = symtab->addImportThunk(
|
|
|
|
name, cast_or_null<DefinedImportData>(impSym), hdr->Machine);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2019-11-15 05:46:00 +08:00
|
|
|
BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
|
|
|
|
uint64_t offsetInArchive)
|
|
|
|
: BitcodeFile(mb, archiveName, offsetInArchive, {}) {}
|
|
|
|
|
2019-04-16 03:48:32 +08:00
|
|
|
BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
|
2019-09-04 04:32:16 +08:00
|
|
|
uint64_t offsetInArchive,
|
|
|
|
std::vector<Symbol *> &&symbols)
|
|
|
|
: InputFile(BitcodeKind, mb), symbols(std::move(symbols)) {
|
2019-04-16 03:48:32 +08:00
|
|
|
std::string path = mb.getBufferIdentifier().str();
|
2019-07-12 02:48:58 +08:00
|
|
|
if (config->thinLTOIndexOnly)
|
|
|
|
path = replaceThinLTOSuffix(mb.getBufferIdentifier());
|
2019-04-16 03:48:32 +08:00
|
|
|
|
|
|
|
// ThinLTO assumes that all MemoryBufferRefs given to it have a unique
|
|
|
|
// name. If two archives define two members with the same name, this
|
|
|
|
// causes a collision which result in only one of the objects being taken
|
|
|
|
// into consideration at LTO time (which very likely causes undefined
|
|
|
|
// symbols later in the link stage). So we append file offset to make
|
|
|
|
// filename unique.
|
|
|
|
MemoryBufferRef mbref(
|
|
|
|
mb.getBuffer(),
|
2020-03-04 17:46:31 +08:00
|
|
|
saver.save(archiveName.empty() ? path
|
|
|
|
: archiveName + sys::path::filename(path) +
|
|
|
|
utostr(offsetInArchive)));
|
2019-04-16 03:48:32 +08:00
|
|
|
|
|
|
|
obj = check(lto::InputFile::create(mbref));
|
|
|
|
}
|
|
|
|
|
2019-11-15 05:46:00 +08:00
|
|
|
BitcodeFile::~BitcodeFile() = default;
|
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
void BitcodeFile::parse() {
|
2017-11-28 09:30:07 +08:00
|
|
|
std::vector<std::pair<Symbol *, bool>> comdat(obj->getComdatTable().size());
|
|
|
|
for (size_t i = 0; i != obj->getComdatTable().size(); ++i)
|
2019-01-30 10:17:27 +08:00
|
|
|
// FIXME: lto::InputFile doesn't keep enough data to do correct comdat
|
|
|
|
// selection handling.
|
2017-11-28 09:30:07 +08:00
|
|
|
comdat[i] = symtab->addComdat(this, saver.save(obj->getComdatTable()[i]));
|
2017-02-03 07:58:14 +08:00
|
|
|
for (const lto::InputFile::Symbol &objSym : obj->symbols()) {
|
|
|
|
StringRef symName = saver.save(objSym.getName());
|
2017-11-28 09:30:07 +08:00
|
|
|
int comdatIndex = objSym.getComdatIndex();
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *sym;
|
2017-03-29 06:31:35 +08:00
|
|
|
if (objSym.isUndefined()) {
|
2017-02-03 07:58:14 +08:00
|
|
|
sym = symtab->addUndefined(symName, this, false);
|
2017-03-29 06:31:35 +08:00
|
|
|
} else if (objSym.isCommon()) {
|
2017-02-03 07:58:14 +08:00
|
|
|
sym = symtab->addCommon(this, symName, objSym.getCommonSize());
|
2017-03-29 06:31:35 +08:00
|
|
|
} else if (objSym.isWeak() && objSym.isIndirect()) {
|
2017-02-03 07:58:14 +08:00
|
|
|
// Weak external.
|
|
|
|
sym = symtab->addUndefined(symName, this, true);
|
2020-01-29 03:23:46 +08:00
|
|
|
std::string fallback = std::string(objSym.getCOFFWeakExternalFallback());
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *alias = symtab->addUndefined(saver.save(fallback));
|
2017-11-01 00:10:24 +08:00
|
|
|
checkAndSetWeakAlias(symtab, this, sym, alias);
|
2017-11-28 09:30:07 +08:00
|
|
|
} else if (comdatIndex != -1) {
|
|
|
|
if (symName == obj->getComdatTable()[comdatIndex])
|
|
|
|
sym = comdat[comdatIndex].first;
|
|
|
|
else if (comdat[comdatIndex].second)
|
|
|
|
sym = symtab->addRegular(this, symName);
|
|
|
|
else
|
|
|
|
sym = symtab->addUndefined(symName, this, false);
|
2015-06-02 04:10:10 +08:00
|
|
|
} else {
|
2017-11-28 09:30:07 +08:00
|
|
|
sym = symtab->addRegular(this, symName);
|
2015-06-06 10:00:45 +08:00
|
|
|
}
|
2018-04-12 03:52:53 +08:00
|
|
|
symbols.push_back(sym);
|
2019-02-20 08:26:01 +08:00
|
|
|
if (objSym.isUsed())
|
2019-07-12 14:12:27 +08:00
|
|
|
config->gcroot.push_back(sym);
|
2015-06-06 10:00:45 +08:00
|
|
|
}
|
2017-03-31 12:47:07 +08:00
|
|
|
directives = obj->getCOFFLinkerOpts();
|
2015-06-02 04:10:10 +08:00
|
|
|
}
|
2015-06-02 05:19:43 +08:00
|
|
|
|
2015-07-10 03:54:13 +08:00
|
|
|
MachineTypes BitcodeFile::getMachineType() {
|
2017-04-14 10:55:06 +08:00
|
|
|
switch (Triple(obj->getTargetTriple()).getArch()) {
|
2015-07-10 03:54:13 +08:00
|
|
|
case Triple::x86_64:
|
2015-07-26 05:54:50 +08:00
|
|
|
return AMD64;
|
2015-07-10 03:54:13 +08:00
|
|
|
case Triple::x86:
|
2015-07-26 05:54:50 +08:00
|
|
|
return I386;
|
2015-07-10 03:54:13 +08:00
|
|
|
case Triple::arm:
|
2015-07-26 05:54:50 +08:00
|
|
|
return ARMNT;
|
2017-07-02 04:29:27 +08:00
|
|
|
case Triple::aarch64:
|
|
|
|
return ARM64;
|
2015-07-10 03:54:13 +08:00
|
|
|
default:
|
|
|
|
return IMAGE_FILE_MACHINE_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 02:48:58 +08:00
|
|
|
|
2020-02-20 09:05:42 +08:00
|
|
|
std::string lld::coff::replaceThinLTOSuffix(StringRef path) {
|
2019-07-12 02:48:58 +08:00
|
|
|
StringRef suffix = config->thinLTOObjectSuffixReplace.first;
|
|
|
|
StringRef repl = config->thinLTOObjectSuffixReplace.second;
|
|
|
|
|
|
|
|
if (path.consume_back(suffix))
|
|
|
|
return (path + repl).str();
|
2020-01-29 03:23:46 +08:00
|
|
|
return std::string(path);
|
2019-07-12 02:48:58 +08:00
|
|
|
}
|