2015-05-28 19:09:30 +00:00
|
|
|
//===- InputFiles.cpp -----------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00: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-28 19:09:30 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-12-18 14:06:06 +00:00
|
|
|
#include "InputFiles.h"
|
2015-05-28 19:09:30 +00:00
|
|
|
#include "Chunks.h"
|
2016-04-21 17:14:10 +00: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 13:36:59 +00:00
|
|
|
#include "DebugTypes.h"
|
2016-07-26 02:00:42 +00:00
|
|
|
#include "Driver.h"
|
2016-12-09 21:55:24 +00:00
|
|
|
#include "SymbolTable.h"
|
2015-08-05 19:51:28 +00:00
|
|
|
#include "Symbols.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-25 22:28:38 +00:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2017-11-28 20:39:17 +00:00
|
|
|
#include "lld/Common/Memory.h"
|
2016-12-18 14:06:06 +00:00
|
|
|
#include "llvm-c/lto.h"
|
2016-04-21 17:14:10 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Triple.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2017-06-07 03:48:56 +00:00
|
|
|
#include "llvm/BinaryFormat/COFF.h"
|
2019-02-23 01:46:18 +00: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 13:36:59 +00:00
|
|
|
#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
|
2016-04-21 17:14:10 +00:00
|
|
|
#include "llvm/Object/Binary.h"
|
2015-05-28 19:09:30 +00:00
|
|
|
#include "llvm/Object/COFF.h"
|
2016-04-21 17:14:10 +00:00
|
|
|
#include "llvm/Support/Casting.h"
|
2015-05-28 19:09:30 +00:00
|
|
|
#include "llvm/Support/Endian.h"
|
2016-04-21 17:14:10 +00:00
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
2018-07-20 23:06:34 +00:00
|
|
|
#include "llvm/Support/Path.h"
|
2016-04-21 17:14:10 +00:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
|
|
|
#include <cstring>
|
|
|
|
#include <system_error>
|
|
|
|
#include <utility>
|
2015-05-28 19:09:30 +00:00
|
|
|
|
2016-09-15 22:24:51 +00:00
|
|
|
using namespace llvm;
|
2015-07-08 20:22:50 +00:00
|
|
|
using namespace llvm::COFF;
|
2019-02-23 01:46:18 +00:00
|
|
|
using namespace llvm::codeview;
|
2015-05-28 19:09:30 +00:00
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::support::endian;
|
2016-04-21 17:14:10 +00:00
|
|
|
|
2015-07-09 19:54:13 +00:00
|
|
|
using llvm::Triple;
|
2015-07-24 23:51:14 +00:00
|
|
|
using llvm::support::ulittle32_t;
|
2015-05-28 19:09:30 +00:00
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
|
|
|
|
2017-07-27 00:45:26 +00:00
|
|
|
std::vector<ObjFile *> ObjFile::Instances;
|
|
|
|
std::vector<ImportFile *> ImportFile::Instances;
|
|
|
|
std::vector<BitcodeFile *> BitcodeFile::Instances;
|
|
|
|
|
2017-02-02 23:58:14 +00: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-03 21:21:47 +00:00
|
|
|
Symbol *Source, Symbol *Target) {
|
2017-05-24 17:12:10 +00:00
|
|
|
if (auto *U = dyn_cast<Undefined>(Source)) {
|
2018-10-03 18:31:53 +00: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-10-31 16:10:24 +00:00
|
|
|
Symtab->reportDuplicate(Source, F);
|
2018-10-03 18:31:53 +00:00
|
|
|
}
|
2017-02-02 23:58:14 +00:00
|
|
|
U->WeakAlias = Target;
|
2017-05-24 17:12:10 +00:00
|
|
|
}
|
2017-02-02 23:58:14 +00:00
|
|
|
}
|
2015-05-28 19:09:30 +00:00
|
|
|
|
2016-09-15 22:24:51 +00:00
|
|
|
ArchiveFile::ArchiveFile(MemoryBufferRef M) : InputFile(ArchiveKind, M) {}
|
|
|
|
|
2015-08-06 14:58:50 +00:00
|
|
|
void ArchiveFile::parse() {
|
2015-05-31 21:04:56 +00:00
|
|
|
// Parse a MemoryBufferRef as an archive file.
|
2017-12-07 03:24:57 +00:00
|
|
|
File = CHECK(Archive::create(MB), this);
|
2015-05-28 19:09:30 +00:00
|
|
|
|
2016-12-09 21:55:24 +00:00
|
|
|
// Read the symbol table to construct Lazy objects.
|
|
|
|
for (const Archive::Symbol &Sym : File->symbols())
|
|
|
|
Symtab->addLazy(this, Sym);
|
2015-05-28 19:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a buffer pointing to a member file containing a given symbol.
|
2016-12-15 04:02:23 +00:00
|
|
|
void ArchiveFile::addMember(const Archive::Symbol *Sym) {
|
2016-07-15 01:06:38 +00:00
|
|
|
const Archive::Child &C =
|
2017-12-06 22:08:17 +00:00
|
|
|
CHECK(Sym->getMember(),
|
2016-07-15 01:12:24 +00:00
|
|
|
"could not get the member for symbol " + Sym->getName());
|
2015-05-28 19:09:30 +00:00
|
|
|
|
|
|
|
// Return an empty buffer if we have already returned the same buffer.
|
2016-12-12 03:16:14 +00:00
|
|
|
if (!Seen.insert(C.getChildOffset()).second)
|
2016-12-15 04:02:23 +00:00
|
|
|
return;
|
2016-07-26 02:00:42 +00:00
|
|
|
|
2016-12-15 04:02:23 +00:00
|
|
|
Driver->enqueueArchiveMember(C, Sym->getName(), getName());
|
2016-12-09 21:55:24 +00:00
|
|
|
}
|
2016-09-15 22:24:51 +00:00
|
|
|
|
2017-08-30 20:55:18 +00:00
|
|
|
std::vector<MemoryBufferRef> getArchiveMembers(Archive *File) {
|
|
|
|
std::vector<MemoryBufferRef> V;
|
|
|
|
Error Err = Error::success();
|
|
|
|
for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) {
|
|
|
|
Archive::Child C =
|
2017-12-06 22:08:17 +00:00
|
|
|
CHECK(COrErr,
|
2017-08-30 20:55:18 +00:00
|
|
|
File->getFileName() + ": could not get the child of the archive");
|
|
|
|
MemoryBufferRef MBRef =
|
2017-12-06 22:08:17 +00:00
|
|
|
CHECK(C.getMemoryBufferRef(),
|
2017-08-30 20:55:18 +00: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;
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:05:24 +00:00
|
|
|
void ObjFile::parse() {
|
2015-05-28 19:09:30 +00:00
|
|
|
// Parse a memory buffer as a COFF file.
|
2017-12-07 03:24:57 +00:00
|
|
|
std::unique_ptr<Binary> Bin = CHECK(createBinary(MB), this);
|
2016-12-08 18:49:04 +00: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-28 19:09:30 +00:00
|
|
|
|
|
|
|
// Read section and symbol tables.
|
2015-08-06 14:58:50 +00:00
|
|
|
initializeChunks();
|
|
|
|
initializeSymbols();
|
2019-02-23 01:46:18 +00: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 13:36:59 +00:00
|
|
|
initializeDependencies();
|
2015-05-28 19:09:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 02:17:27 +00:00
|
|
|
const coff_section* ObjFile::getSection(uint32_t I) {
|
|
|
|
const coff_section *Sec;
|
|
|
|
if (auto EC = COFFObj->getSection(I, Sec))
|
|
|
|
fatal("getSection failed: #" + Twine(I) + ": " + EC.message());
|
|
|
|
return Sec;
|
|
|
|
}
|
|
|
|
|
2017-11-28 01:30:07 +00: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-26 23:05:24 +00:00
|
|
|
void ObjFile::initializeChunks() {
|
2015-05-28 19:09:30 +00:00
|
|
|
uint32_t NumSections = COFFObj->getNumberOfSections();
|
|
|
|
Chunks.reserve(NumSections);
|
|
|
|
SparseChunks.resize(NumSections + 1);
|
|
|
|
for (uint32_t I = 1; I < NumSections + 1; ++I) {
|
2019-01-30 02:17:27 +00:00
|
|
|
const coff_section *Sec = getSection(I);
|
2017-11-28 01:30:07 +00:00
|
|
|
if (Sec->Characteristics & IMAGE_SCN_LNK_COMDAT)
|
|
|
|
SparseChunks[I] = PendingComdat;
|
2017-06-20 17:14:09 +00:00
|
|
|
else
|
2018-03-15 21:14:02 +00:00
|
|
|
SparseChunks[I] = readSection(I, nullptr, "");
|
2017-11-28 01:30:07 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-20 17:14:09 +00:00
|
|
|
|
2017-11-28 01:30:07 +00:00
|
|
|
SectionChunk *ObjFile::readSection(uint32_t SectionNumber,
|
2018-03-15 21:14:02 +00:00
|
|
|
const coff_aux_section_definition *Def,
|
|
|
|
StringRef LeaderName) {
|
2019-01-30 02:17:27 +00:00
|
|
|
const coff_section *Sec = getSection(SectionNumber);
|
2019-01-14 19:05:21 +00:00
|
|
|
|
|
|
|
StringRef Name;
|
2017-11-28 01:30:07 +00:00
|
|
|
if (auto EC = COFFObj->getSectionName(Sec, Name))
|
|
|
|
fatal("getSectionName failed: #" + Twine(SectionNumber) + ": " +
|
|
|
|
EC.message());
|
2018-02-06 01:58:26 +00:00
|
|
|
|
2017-11-28 01:30:07 +00:00
|
|
|
if (Name == ".drectve") {
|
|
|
|
ArrayRef<uint8_t> Data;
|
|
|
|
COFFObj->getSectionContents(Sec, Data);
|
2019-03-29 21:00:22 +00:00
|
|
|
Directives = StringRef((const char *)Data.data(), Data.size());
|
2017-11-28 01:30:07 +00:00
|
|
|
return nullptr;
|
2015-05-28 19:09:30 +00:00
|
|
|
}
|
2017-11-28 01:30:07 +00:00
|
|
|
|
2018-08-23 17:44:42 +00:00
|
|
|
if (Name == ".llvm_addrsig") {
|
|
|
|
AddrsigSec = Sec;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-11-28 01:30:07 +00: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-14 19:05:21 +00:00
|
|
|
// CodeView needs linker support. We need to interpret debug info,
|
|
|
|
// and then write it to a separate .pdb file.
|
2017-11-28 01:30:07 +00:00
|
|
|
|
2018-04-17 23:32:33 +00:00
|
|
|
// Ignore DWARF debug info unless /debug is given.
|
|
|
|
if (!Config->Debug && Name.startswith(".debug_"))
|
2017-11-28 01:30:07 +00: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);
|
2018-02-13 20:32:53 +00:00
|
|
|
else if (Config->GuardCF != GuardCFLevel::Off && Name == ".gfids$y")
|
2018-02-06 01:58:26 +00:00
|
|
|
GuardFidChunks.push_back(C);
|
2018-02-13 20:32:53 +00:00
|
|
|
else if (Config->GuardCF != GuardCFLevel::Off && Name == ".gljmp$y")
|
|
|
|
GuardLJmpChunks.push_back(C);
|
2018-02-06 01:58:26 +00:00
|
|
|
else if (Name == ".sxdata")
|
|
|
|
SXDataChunks.push_back(C);
|
2018-05-11 22:21:36 +00:00
|
|
|
else if (Config->TailMerge && Sec->NumberOfRelocations == 0 &&
|
|
|
|
Name == ".rdata" && LeaderName.startswith("??_C@"))
|
2018-03-15 21:14:02 +00: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);
|
2017-11-28 01:30:07 +00:00
|
|
|
else
|
|
|
|
Chunks.push_back(C);
|
|
|
|
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjFile::readAssociativeDefinition(
|
|
|
|
COFFSymbolRef Sym, const coff_aux_section_definition *Def) {
|
2018-08-06 21:26:09 +00:00
|
|
|
readAssociativeDefinition(Sym, Def, Def->getNumber(Sym.isBigObj()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjFile::readAssociativeDefinition(COFFSymbolRef Sym,
|
|
|
|
const coff_aux_section_definition *Def,
|
2019-01-23 02:07:10 +00:00
|
|
|
uint32_t ParentIndex) {
|
|
|
|
SectionChunk *Parent = SparseChunks[ParentIndex];
|
2019-01-28 21:16:15 +00:00
|
|
|
int32_t SectionNumber = Sym.getSectionNumber();
|
2019-01-23 02:07:10 +00:00
|
|
|
|
2019-01-26 00:14:52 +00:00
|
|
|
auto Diag = [&]() {
|
2019-01-23 02:07:10 +00:00
|
|
|
StringRef Name, ParentName;
|
|
|
|
COFFObj->getSymbolName(Sym, Name);
|
2017-11-28 01:30:07 +00:00
|
|
|
|
2019-01-30 02:17:27 +00:00
|
|
|
const coff_section *ParentSec = getSection(ParentIndex);
|
2019-01-23 02:07:10 +00:00
|
|
|
COFFObj->getSectionName(ParentSec, ParentName);
|
2019-01-28 21:16:15 +00:00
|
|
|
error(toString(this) + ": associative comdat " + Name + " (sec " +
|
|
|
|
Twine(SectionNumber) + ") has invalid reference to section " +
|
|
|
|
ParentName + " (sec " + Twine(ParentIndex) + ")");
|
2019-01-26 00:14:52 +00: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 01:30:07 +00:00
|
|
|
return;
|
2019-01-23 02:07:10 +00:00
|
|
|
}
|
2017-11-28 01:30:07 +00: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 00:14:52 +00:00
|
|
|
SectionChunk *C = readSection(SectionNumber, Def, "");
|
|
|
|
SparseChunks[SectionNumber] = C;
|
2019-01-29 15:50:31 +00:00
|
|
|
if (C) {
|
|
|
|
C->Selection = IMAGE_COMDAT_SELECT_ASSOCIATIVE;
|
|
|
|
Parent->addAssociative(C);
|
|
|
|
}
|
2017-11-28 01:30:07 +00:00
|
|
|
} else {
|
|
|
|
SparseChunks[SectionNumber] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 21:26:09 +00: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.
|
|
|
|
int32_t SectionNumber = Sym.getSectionNumber();
|
|
|
|
SectionChunk *SC = SparseChunks[SectionNumber];
|
|
|
|
if (SC && SC->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) {
|
|
|
|
StringRef Name;
|
|
|
|
COFFObj->getSymbolName(Sym, Name);
|
|
|
|
PrevailingSectionMap[Name] = SectionNumber;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjFile::maybeAssociateSEHForMingw(
|
|
|
|
COFFSymbolRef Sym, const coff_aux_section_definition *Def,
|
|
|
|
const DenseMap<StringRef, uint32_t> &PrevailingSectionMap) {
|
|
|
|
StringRef Name;
|
|
|
|
COFFObj->getSymbolName(Sym, Name);
|
|
|
|
if (Name.consume_front(".pdata$") || Name.consume_front(".xdata$")) {
|
|
|
|
// For MinGW, treat .[px]data$<func> as implicitly associative to
|
|
|
|
// the symbol <func>.
|
|
|
|
auto ParentSym = PrevailingSectionMap.find(Name);
|
|
|
|
if (ParentSym != PrevailingSectionMap.end())
|
|
|
|
readAssociativeDefinition(Sym, Def, ParentSym->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-28 01:30:07 +00:00
|
|
|
Symbol *ObjFile::createRegular(COFFSymbolRef Sym) {
|
|
|
|
SectionChunk *SC = SparseChunks[Sym.getSectionNumber()];
|
|
|
|
if (Sym.isExternal()) {
|
|
|
|
StringRef Name;
|
|
|
|
COFFObj->getSymbolName(Sym, Name);
|
|
|
|
if (SC)
|
|
|
|
return Symtab->addRegular(this, Name, Sym.getGeneric(), SC);
|
2018-10-05 19:43:16 +00: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 01:30:07 +00:00
|
|
|
return Symtab->addUndefined(Name, this, false);
|
|
|
|
}
|
|
|
|
if (SC)
|
2019-01-14 19:05:21 +00:00
|
|
|
return make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
|
2017-11-28 01:30:07 +00:00
|
|
|
/*IsExternal*/ false, Sym.getGeneric(), SC);
|
|
|
|
return nullptr;
|
2015-05-28 19:09:30 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:05:24 +00:00
|
|
|
void ObjFile::initializeSymbols() {
|
2015-05-28 19:09:30 +00:00
|
|
|
uint32_t NumSymbols = COFFObj->getNumberOfSymbols();
|
2017-11-20 18:52:53 +00:00
|
|
|
Symbols.resize(NumSymbols);
|
2017-05-24 17:12:32 +00:00
|
|
|
|
2017-11-03 21:21:47 +00:00
|
|
|
SmallVector<std::pair<Symbol *, uint32_t>, 8> WeakAliases;
|
2017-11-28 01:30:07 +00:00
|
|
|
std::vector<uint32_t> PendingIndexes;
|
|
|
|
PendingIndexes.reserve(NumSymbols);
|
|
|
|
|
2018-08-06 21:26:09 +00:00
|
|
|
DenseMap<StringRef, uint32_t> PrevailingSectionMap;
|
2017-11-28 01:30:07 +00:00
|
|
|
std::vector<const coff_aux_section_definition *> ComdatDefs(
|
|
|
|
COFFObj->getNumberOfSections() + 1);
|
2017-05-24 17:12:32 +00:00
|
|
|
|
2015-05-28 19:09:30 +00:00
|
|
|
for (uint32_t I = 0; I < NumSymbols; ++I) {
|
2017-11-03 22:49:02 +00:00
|
|
|
COFFSymbolRef COFFSym = check(COFFObj->getSymbol(I));
|
2018-08-06 21:26:09 +00:00
|
|
|
bool PrevailingComdat;
|
2017-11-03 22:49:02 +00:00
|
|
|
if (COFFSym.isUndefined()) {
|
2017-11-28 01:30:07 +00:00
|
|
|
Symbols[I] = createUndefined(COFFSym);
|
2017-11-03 22:49:02 +00:00
|
|
|
} else if (COFFSym.isWeakExternal()) {
|
2017-11-28 01:30:07 +00:00
|
|
|
Symbols[I] = createUndefined(COFFSym);
|
|
|
|
uint32_t TagIndex = COFFSym.getAux<coff_aux_weak_external>()->TagIndex;
|
|
|
|
WeakAliases.emplace_back(Symbols[I], TagIndex);
|
2018-08-06 21:26:09 +00:00
|
|
|
} else if (Optional<Symbol *> OptSym =
|
|
|
|
createDefined(COFFSym, ComdatDefs, PrevailingComdat)) {
|
2017-11-28 01:30:07 +00:00
|
|
|
Symbols[I] = *OptSym;
|
2018-08-06 21:26:09 +00:00
|
|
|
if (Config->MinGW && PrevailingComdat)
|
|
|
|
recordPrevailingSymbolForMingw(COFFSym, PrevailingSectionMap);
|
2015-06-29 22:16:21 +00:00
|
|
|
} else {
|
2017-11-28 01:30:07 +00: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 02:07:10 +00:00
|
|
|
// 2) symbol belongs to a comdat section associated with another section.
|
2017-11-28 01:30:07 +00: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-29 22:16:21 +00:00
|
|
|
}
|
2017-11-03 22:49:02 +00:00
|
|
|
I += COFFSym.getNumberOfAuxSymbols();
|
2017-11-28 01:30:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t I : PendingIndexes) {
|
|
|
|
COFFSymbolRef Sym = check(COFFObj->getSymbol(I));
|
2019-01-14 19:05:21 +00:00
|
|
|
if (const coff_aux_section_definition *Def = Sym.getSectionDefinition()) {
|
2017-11-28 01:30:07 +00:00
|
|
|
if (Def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
|
|
|
|
readAssociativeDefinition(Sym, Def);
|
2018-08-06 21:26:09 +00:00
|
|
|
else if (Config->MinGW)
|
|
|
|
maybeAssociateSEHForMingw(Sym, Def, PrevailingSectionMap);
|
|
|
|
}
|
2018-07-26 20:14:50 +00:00
|
|
|
if (SparseChunks[Sym.getSectionNumber()] == PendingComdat) {
|
|
|
|
StringRef Name;
|
|
|
|
COFFObj->getSymbolName(Sym, Name);
|
|
|
|
log("comdat section " + Name +
|
|
|
|
" without leader and unassociated, discarding");
|
|
|
|
continue;
|
|
|
|
}
|
2017-11-28 01:30:07 +00:00
|
|
|
Symbols[I] = createRegular(Sym);
|
2015-05-28 19:09:30 +00:00
|
|
|
}
|
2017-05-24 17:12:32 +00:00
|
|
|
|
|
|
|
for (auto &KV : WeakAliases) {
|
2017-11-03 21:21:47 +00:00
|
|
|
Symbol *Sym = KV.first;
|
2017-05-24 17:12:32 +00:00
|
|
|
uint32_t Idx = KV.second;
|
2017-11-20 18:52:53 +00:00
|
|
|
checkAndSetWeakAlias(Symtab, this, Sym, Symbols[Idx]);
|
2017-05-24 17:12:32 +00:00
|
|
|
}
|
2015-05-28 19:09:30 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 21:21:47 +00:00
|
|
|
Symbol *ObjFile::createUndefined(COFFSymbolRef Sym) {
|
2015-06-29 22:16:21 +00:00
|
|
|
StringRef Name;
|
|
|
|
COFFObj->getSymbolName(Sym, Name);
|
2017-10-31 16:10:24 +00:00
|
|
|
return Symtab->addUndefined(Name, this, Sym.isWeakExternal());
|
2015-06-29 22:16:21 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 03:16:44 +00:00
|
|
|
void ObjFile::handleComdatSelection(COFFSymbolRef Sym, COMDATType &Selection,
|
|
|
|
bool &Prevailing, DefinedRegular *Leader) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
if (LeaderChunk->getSize() != getSection(Sym)->SizeOfRawData)
|
|
|
|
Symtab->reportDuplicate(Leader, this);
|
|
|
|
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())
|
|
|
|
Symtab->reportDuplicate(Leader, this);
|
|
|
|
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.
|
|
|
|
StringRef Name;
|
|
|
|
COFFObj->getSymbolName(Sym, Name);
|
|
|
|
// 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 01:30:07 +00:00
|
|
|
Optional<Symbol *> ObjFile::createDefined(
|
|
|
|
COFFSymbolRef Sym,
|
2018-08-06 21:26:09 +00:00
|
|
|
std::vector<const coff_aux_section_definition *> &ComdatDefs,
|
|
|
|
bool &Prevailing) {
|
|
|
|
Prevailing = false;
|
2018-07-24 22:52:11 +00:00
|
|
|
auto GetName = [&]() {
|
|
|
|
StringRef S;
|
|
|
|
COFFObj->getSymbolName(Sym, S);
|
|
|
|
return S;
|
|
|
|
};
|
|
|
|
|
2015-05-28 19:09:30 +00:00
|
|
|
if (Sym.isCommon()) {
|
2017-05-18 17:03:49 +00:00
|
|
|
auto *C = make<CommonChunk>(Sym);
|
2015-05-28 19:09:30 +00:00
|
|
|
Chunks.push_back(C);
|
2018-07-24 22:52:11 +00:00
|
|
|
return Symtab->addCommon(this, GetName(), Sym.getValue(), Sym.getGeneric(),
|
|
|
|
C);
|
2015-05-28 19:09:30 +00:00
|
|
|
}
|
2018-07-24 22:52:11 +00:00
|
|
|
|
2015-06-08 19:43:59 +00:00
|
|
|
if (Sym.isAbsolute()) {
|
2018-07-24 22:52:11 +00:00
|
|
|
StringRef Name = GetName();
|
|
|
|
|
2015-06-08 19:43:59 +00:00
|
|
|
// Skip special symbols.
|
2015-07-24 23:51:14 +00:00
|
|
|
if (Name == "@comp.id")
|
2015-06-08 19:43:59 +00:00
|
|
|
return nullptr;
|
2015-07-24 23:51:14 +00:00
|
|
|
if (Name == "@feat.00") {
|
2018-02-06 01:58:26 +00:00
|
|
|
Feat00Flags = Sym.getValue();
|
2015-07-24 23:51:14 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-24 22:52:11 +00:00
|
|
|
|
2016-12-09 21:55:24 +00:00
|
|
|
if (Sym.isExternal())
|
2017-10-31 16:10:24 +00:00
|
|
|
return Symtab->addAbsolute(Name, Sym);
|
2018-07-24 22:52:11 +00:00
|
|
|
return make<DefinedAbsolute>(Name, Sym);
|
2015-06-08 19:43:59 +00:00
|
|
|
}
|
2018-07-24 22:52:11 +00:00
|
|
|
|
2016-03-15 16:47:28 +00:00
|
|
|
int32_t SectionNumber = Sym.getSectionNumber();
|
|
|
|
if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
|
2015-06-24 00:05:50 +00:00
|
|
|
return nullptr;
|
2015-06-29 21:32:37 +00:00
|
|
|
|
2016-03-15 16:47:28 +00:00
|
|
|
if (llvm::COFF::isReservedSectionNumber(SectionNumber))
|
2018-07-24 22:52:11 +00:00
|
|
|
fatal(toString(this) + ": " + GetName() +
|
2018-04-25 23:33:19 +00:00
|
|
|
" should not refer to special section " + Twine(SectionNumber));
|
2016-03-15 16:47:28 +00:00
|
|
|
|
|
|
|
if ((uint32_t)SectionNumber >= SparseChunks.size())
|
2018-07-24 22:52:11 +00:00
|
|
|
fatal(toString(this) + ": " + GetName() +
|
2018-04-25 23:33:19 +00:00
|
|
|
" should not refer to non-existent section " + Twine(SectionNumber));
|
2016-03-15 16:47:28 +00:00
|
|
|
|
2019-01-30 02:17:27 +00: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
|
|
|
|
// values for the other fields, and one auxilliary record.
|
|
|
|
// 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,
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Handle comdat leader.
|
2017-11-28 01:30:07 +00:00
|
|
|
if (const coff_aux_section_definition *Def = ComdatDefs[SectionNumber]) {
|
|
|
|
ComdatDefs[SectionNumber] = nullptr;
|
2019-01-30 02:17:27 +00:00
|
|
|
DefinedRegular *Leader;
|
|
|
|
|
2017-11-28 01:30:07 +00:00
|
|
|
if (Sym.isExternal()) {
|
|
|
|
std::tie(Leader, Prevailing) =
|
2018-07-24 22:52:11 +00:00
|
|
|
Symtab->addComdat(this, GetName(), Sym.getGeneric());
|
2017-11-28 01:30:07 +00:00
|
|
|
} else {
|
2019-01-14 19:05:21 +00:00
|
|
|
Leader = make<DefinedRegular>(this, /*Name*/ "", /*IsCOMDAT*/ false,
|
2017-11-28 01:30:07 +00:00
|
|
|
/*IsExternal*/ false, Sym.getGeneric());
|
|
|
|
Prevailing = true;
|
|
|
|
}
|
2018-07-24 22:52:11 +00:00
|
|
|
|
2019-01-30 02:17:27 +00: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-02-14 03:16:44 +00:00
|
|
|
if (Leader->isCOMDAT())
|
|
|
|
handleComdatSelection(Sym, Selection, Prevailing, Leader);
|
2019-01-30 02:17:27 +00:00
|
|
|
|
2017-11-28 01:30:07 +00:00
|
|
|
if (Prevailing) {
|
2018-07-24 22:52:11 +00:00
|
|
|
SectionChunk *C = readSection(SectionNumber, Def, GetName());
|
2017-11-28 01:30:07 +00:00
|
|
|
SparseChunks[SectionNumber] = C;
|
|
|
|
C->Sym = cast<DefinedRegular>(Leader);
|
2019-01-30 02:17:27 +00:00
|
|
|
C->Selection = Selection;
|
2017-11-28 01:30:07 +00:00
|
|
|
cast<DefinedRegular>(Leader)->Data = &C->Repl;
|
|
|
|
} else {
|
|
|
|
SparseChunks[SectionNumber] = nullptr;
|
|
|
|
}
|
|
|
|
return Leader;
|
|
|
|
}
|
2015-06-29 21:32:37 +00:00
|
|
|
|
2019-01-23 02:07:10 +00: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 01:30:07 +00:00
|
|
|
if (SparseChunks[SectionNumber] == PendingComdat) {
|
2019-01-14 19:05:21 +00:00
|
|
|
if (const coff_aux_section_definition *Def = Sym.getSectionDefinition()) {
|
2019-01-23 02:07:10 +00:00
|
|
|
if (Def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE)
|
2017-11-28 01:30:07 +00:00
|
|
|
ComdatDefs[SectionNumber] = Def;
|
|
|
|
}
|
|
|
|
return None;
|
2019-01-23 02:07:10 +00:00
|
|
|
}
|
2019-01-14 19:05:21 +00:00
|
|
|
|
2017-11-28 01:30:07 +00:00
|
|
|
return createRegular(Sym);
|
2015-05-28 19:09:30 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:05:24 +00:00
|
|
|
MachineTypes ObjFile::getMachineType() {
|
2015-07-09 19:54:13 +00:00
|
|
|
if (COFFObj)
|
|
|
|
return static_cast<MachineTypes>(COFFObj->getMachine());
|
|
|
|
return IMAGE_FILE_MACHINE_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2019-02-23 01:46:18 +00:00
|
|
|
ArrayRef<uint8_t> ObjFile::getDebugSection(StringRef SecName) {
|
|
|
|
if (SectionChunk *Sec = SectionChunk::findByName(DebugChunks, SecName))
|
|
|
|
return Sec->consumeDebugMagic();
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// OBJ files systematically store critical informations in a .debug$S stream,
|
|
|
|
// 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
|
|
|
|
// currently used to initialize the HotPatchable member.
|
|
|
|
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 13:36:59 +00: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
|
|
|
|
// DebugTypes.h).
|
|
|
|
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");
|
|
|
|
|
|
|
|
if (Data.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CVTypeArray Types;
|
|
|
|
BinaryStreamReader Reader(Data, support::little);
|
|
|
|
cantFail(Reader.readArray(Types, Reader.getLength()));
|
|
|
|
|
|
|
|
CVTypeArray::Iterator FirstType = Types.begin();
|
|
|
|
if (FirstType == Types.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
DebugTypes.emplace(Types);
|
|
|
|
|
|
|
|
if (IsPCH) {
|
|
|
|
DebugTypesObj = makePrecompSource(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FirstType->kind() == LF_TYPESERVER2) {
|
|
|
|
TypeServer2Record TS = cantFail(
|
|
|
|
TypeDeserializer::deserializeAs<TypeServer2Record>(FirstType->data()));
|
|
|
|
DebugTypesObj = makeUseTypeServerSource(this, &TS);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FirstType->kind() == LF_PRECOMP) {
|
|
|
|
PrecompRecord Precomp = cantFail(
|
|
|
|
TypeDeserializer::deserializeAs<PrecompRecord>(FirstType->data()));
|
|
|
|
DebugTypesObj = makeUsePrecompSource(this, &Precomp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DebugTypesObj = makeTpiSource(this);
|
|
|
|
}
|
|
|
|
|
2015-07-09 20:22:41 +00:00
|
|
|
StringRef ltrim1(StringRef S, const char *Chars) {
|
|
|
|
if (!S.empty() && strchr(Chars, S[0]))
|
|
|
|
return S.substr(1);
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2015-08-06 14:58:50 +00:00
|
|
|
void ImportFile::parse() {
|
2015-05-31 21:04:56 +00:00
|
|
|
const char *Buf = MB.getBufferStart();
|
|
|
|
const char *End = MB.getBufferEnd();
|
2015-05-28 19:09:30 +00:00
|
|
|
const auto *Hdr = reinterpret_cast<const coff_import_header *>(Buf);
|
|
|
|
|
|
|
|
// Check if the total size is valid.
|
2015-08-06 14:58:50 +00:00
|
|
|
if ((size_t)(End - Buf) != (sizeof(*Hdr) + Hdr->SizeOfData))
|
2016-07-14 23:37:14 +00:00
|
|
|
fatal("broken import library");
|
2015-05-28 19:09:30 +00:00
|
|
|
|
|
|
|
// Read names and create an __imp_ symbol.
|
2017-05-18 17:03:49 +00:00
|
|
|
StringRef Name = Saver.save(StringRef(Buf + sizeof(*Hdr)));
|
|
|
|
StringRef ImpName = Saver.save("__imp_" + Name);
|
2015-08-17 08:30:31 +00:00
|
|
|
const char *NameStart = Buf + sizeof(coff_import_header) + Name.size() + 1;
|
2015-09-02 07:27:31 +00:00
|
|
|
DLLName = StringRef(NameStart);
|
2015-07-08 20:22:50 +00:00
|
|
|
StringRef ExtName;
|
|
|
|
switch (Hdr->getNameType()) {
|
|
|
|
case IMPORT_ORDINAL:
|
|
|
|
ExtName = "";
|
|
|
|
break;
|
|
|
|
case IMPORT_NAME:
|
|
|
|
ExtName = Name;
|
|
|
|
break;
|
|
|
|
case IMPORT_NAME_NOPREFIX:
|
2015-07-09 20:22:41 +00:00
|
|
|
ExtName = ltrim1(Name, "?@_");
|
2015-07-08 20:22:50 +00:00
|
|
|
break;
|
|
|
|
case IMPORT_NAME_UNDECORATE:
|
2015-07-09 20:22:41 +00:00
|
|
|
ExtName = ltrim1(Name, "?@_");
|
2015-07-08 20:22:50 +00:00
|
|
|
ExtName = ExtName.substr(0, ExtName.find('@'));
|
|
|
|
break;
|
|
|
|
}
|
2016-12-09 21:55:24 +00:00
|
|
|
|
|
|
|
this->Hdr = Hdr;
|
|
|
|
ExternalName = ExtName;
|
|
|
|
|
2017-09-01 23:35:43 +00:00
|
|
|
ImpSym = Symtab->addImportData(ImpName, this);
|
2018-10-19 06:39:36 +00:00
|
|
|
// If this was a duplicate, we logged an error but may continue;
|
|
|
|
// in this case, ImpSym is nullptr.
|
|
|
|
if (!ImpSym)
|
|
|
|
return;
|
2017-09-01 22:12:10 +00:00
|
|
|
|
2017-05-22 06:01:37 +00:00
|
|
|
if (Hdr->getType() == llvm::COFF::IMPORT_CONST)
|
2017-09-01 23:35:43 +00:00
|
|
|
static_cast<void>(Symtab->addImportData(Name, this));
|
2015-05-28 19:09:30 +00: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-01 22:12:10 +00:00
|
|
|
if (Hdr->getType() == llvm::COFF::IMPORT_CODE)
|
2018-07-10 10:40:11 +00:00
|
|
|
ThunkSym = Symtab->addImportThunk(
|
|
|
|
Name, cast_or_null<DefinedImportData>(ImpSym), Hdr->Machine);
|
2015-05-28 19:09:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 14:58:50 +00:00
|
|
|
void BitcodeFile::parse() {
|
2017-03-28 21:20:06 +00:00
|
|
|
Obj = check(lto::InputFile::create(MemoryBufferRef(
|
|
|
|
MB.getBuffer(), Saver.save(ParentName + MB.getBufferIdentifier()))));
|
2017-11-28 01:30:07 +00:00
|
|
|
std::vector<std::pair<Symbol *, bool>> Comdat(Obj->getComdatTable().size());
|
|
|
|
for (size_t I = 0; I != Obj->getComdatTable().size(); ++I)
|
2019-01-30 02:17:27 +00:00
|
|
|
// FIXME: lto::InputFile doesn't keep enough data to do correct comdat
|
|
|
|
// selection handling.
|
2017-11-28 01:30:07 +00:00
|
|
|
Comdat[I] = Symtab->addComdat(this, Saver.save(Obj->getComdatTable()[I]));
|
2017-02-02 23:58:14 +00:00
|
|
|
for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) {
|
|
|
|
StringRef SymName = Saver.save(ObjSym.getName());
|
2017-11-28 01:30:07 +00:00
|
|
|
int ComdatIndex = ObjSym.getComdatIndex();
|
2017-11-03 21:21:47 +00:00
|
|
|
Symbol *Sym;
|
2017-03-28 22:31:35 +00:00
|
|
|
if (ObjSym.isUndefined()) {
|
2017-02-02 23:58:14 +00:00
|
|
|
Sym = Symtab->addUndefined(SymName, this, false);
|
2017-03-28 22:31:35 +00:00
|
|
|
} else if (ObjSym.isCommon()) {
|
2017-02-02 23:58:14 +00:00
|
|
|
Sym = Symtab->addCommon(this, SymName, ObjSym.getCommonSize());
|
2017-03-28 22:31:35 +00:00
|
|
|
} else if (ObjSym.isWeak() && ObjSym.isIndirect()) {
|
2017-02-02 23:58:14 +00:00
|
|
|
// Weak external.
|
|
|
|
Sym = Symtab->addUndefined(SymName, this, true);
|
|
|
|
std::string Fallback = ObjSym.getCOFFWeakExternalFallback();
|
2017-11-03 21:21:47 +00:00
|
|
|
Symbol *Alias = Symtab->addUndefined(Saver.save(Fallback));
|
2017-10-31 16:10:24 +00:00
|
|
|
checkAndSetWeakAlias(Symtab, this, Sym, Alias);
|
2017-11-28 01:30:07 +00: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-01 20:10:10 +00:00
|
|
|
} else {
|
2017-11-28 01:30:07 +00:00
|
|
|
Sym = Symtab->addRegular(this, SymName);
|
2015-06-06 02:00:45 +00:00
|
|
|
}
|
2018-04-11 19:52:53 +00:00
|
|
|
Symbols.push_back(Sym);
|
2019-02-20 00:26:01 +00:00
|
|
|
if (ObjSym.isUsed())
|
|
|
|
Config->GCRoot.push_back(Sym);
|
2015-06-06 02:00:45 +00:00
|
|
|
}
|
2017-03-31 04:47:07 +00:00
|
|
|
Directives = Obj->getCOFFLinkerOpts();
|
2015-06-01 20:10:10 +00:00
|
|
|
}
|
2015-06-01 21:19:43 +00:00
|
|
|
|
2015-07-09 19:54:13 +00:00
|
|
|
MachineTypes BitcodeFile::getMachineType() {
|
2017-04-14 02:55:06 +00:00
|
|
|
switch (Triple(Obj->getTargetTriple()).getArch()) {
|
2015-07-09 19:54:13 +00:00
|
|
|
case Triple::x86_64:
|
2015-07-25 21:54:50 +00:00
|
|
|
return AMD64;
|
2015-07-09 19:54:13 +00:00
|
|
|
case Triple::x86:
|
2015-07-25 21:54:50 +00:00
|
|
|
return I386;
|
2015-07-09 19:54:13 +00:00
|
|
|
case Triple::arm:
|
2015-07-25 21:54:50 +00:00
|
|
|
return ARMNT;
|
2017-07-01 20:29:27 +00:00
|
|
|
case Triple::aarch64:
|
|
|
|
return ARM64;
|
2015-07-09 19:54:13 +00:00
|
|
|
default:
|
|
|
|
return IMAGE_FILE_MACHINE_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 10:04:08 +00:00
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|
2015-07-09 19:54:13 +00:00
|
|
|
|
2016-12-07 23:17:02 +00:00
|
|
|
// Returns the last element of a path, which is supposed to be a filename.
|
|
|
|
static StringRef getBasename(StringRef Path) {
|
2018-07-20 23:06:34 +00:00
|
|
|
return sys::path::filename(Path, sys::path::Style::windows);
|
2016-12-07 23:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
|
2017-12-05 16:50:46 +00:00
|
|
|
std::string lld::toString(const coff::InputFile *File) {
|
2016-12-07 23:17:02 +00:00
|
|
|
if (!File)
|
2017-11-27 22:49:16 +00:00
|
|
|
return "<internal>";
|
2019-03-29 20:25:34 +00:00
|
|
|
if (File->ParentName.empty() || File->kind() == coff::InputFile::ImportKind)
|
2017-12-07 01:21:27 +00:00
|
|
|
return File->getName();
|
2016-12-07 23:17:02 +00:00
|
|
|
|
2017-12-07 01:21:27 +00:00
|
|
|
return (getBasename(File->ParentName) + "(" + getBasename(File->getName()) +
|
|
|
|
")")
|
|
|
|
.str();
|
2016-12-07 23:17:02 +00:00
|
|
|
}
|