2015-05-29 03:09:30 +08:00
|
|
|
//===- InputFiles.cpp -----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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"
|
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"
|
[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"
|
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"
|
|
|
|
#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;
|
2015-05-29 03:09:30 +08:00
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::support::endian;
|
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
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
|
|
|
|
2017-07-27 08:45:26 +08:00
|
|
|
std::vector<ObjFile *> ObjFile::Instances;
|
|
|
|
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)) {
|
|
|
|
if (U->WeakAlias && U->WeakAlias != Target)
|
2017-11-01 00:10:24 +08:00
|
|
|
Symtab->reportDuplicate(Source, F);
|
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
|
|
|
|
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())
|
|
|
|
Symtab->addLazy(this, Sym);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a buffer pointing to a member file containing a given symbol.
|
2016-12-15 12:02:23 +08:00
|
|
|
void ArchiveFile::addMember(const Archive::Symbol *Sym) {
|
2016-07-15 09:06:38 +08:00
|
|
|
const Archive::Child &C =
|
2017-12-07 06:08:17 +08:00
|
|
|
CHECK(Sym->getMember(),
|
2016-07-15 09:12:24 +08:00
|
|
|
"could not get the member for symbol " + Sym->getName());
|
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
|
|
|
|
2016-12-15 12:02:23 +08:00
|
|
|
Driver->enqueueArchiveMember(C, Sym->getName(), getName());
|
2016-12-10 05:55:24 +08:00
|
|
|
}
|
2016-09-16 06:24:51 +08:00
|
|
|
|
2017-08-31 04:55:18 +08: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-07 06:08:17 +08:00
|
|
|
CHECK(COrErr,
|
2017-08-31 04:55:18 +08:00
|
|
|
File->getFileName() + ": could not get the child of the archive");
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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();
|
2015-05-29 03:09:30 +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();
|
|
|
|
Chunks.reserve(NumSections);
|
|
|
|
SparseChunks.resize(NumSections + 1);
|
|
|
|
for (uint32_t I = 1; I < NumSections + 1; ++I) {
|
|
|
|
const coff_section *Sec;
|
2016-07-15 08:40:46 +08:00
|
|
|
if (auto EC = COFFObj->getSection(I, Sec))
|
[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
|
|
|
fatal("getSection failed: #" + Twine(I) + ": " + EC.message());
|
2016-11-22 01:22:35 +08:00
|
|
|
|
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) {
|
2017-11-28 09:30:07 +08:00
|
|
|
const coff_section *Sec;
|
|
|
|
StringRef Name;
|
|
|
|
if (auto EC = COFFObj->getSection(SectionNumber, Sec))
|
|
|
|
fatal("getSection failed: #" + Twine(SectionNumber) + ": " + EC.message());
|
|
|
|
if (auto EC = COFFObj->getSectionName(Sec, Name))
|
|
|
|
fatal("getSectionName failed: #" + Twine(SectionNumber) + ": " +
|
|
|
|
EC.message());
|
2018-02-06 09:58:26 +08:00
|
|
|
|
2017-11-28 09:30:07 +08:00
|
|
|
if (Name == ".drectve") {
|
|
|
|
ArrayRef<uint8_t> Data;
|
|
|
|
COFFObj->getSectionContents(Sec, Data);
|
|
|
|
Directives = std::string((const char *)Data.data(), Data.size());
|
|
|
|
return nullptr;
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
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.
|
|
|
|
//
|
|
|
|
// CodeView needs a linker support. We need to interpret and debug
|
|
|
|
// info, and then write it to a separate .pdb file.
|
|
|
|
|
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);
|
2018-02-14 04:32:53 +08:00
|
|
|
else if (Config->GuardCF != GuardCFLevel::Off && Name == ".gfids$y")
|
2018-02-06 09:58:26 +08:00
|
|
|
GuardFidChunks.push_back(C);
|
2018-02-14 04:32:53 +08:00
|
|
|
else if (Config->GuardCF != GuardCFLevel::Off && Name == ".gljmp$y")
|
|
|
|
GuardLJmpChunks.push_back(C);
|
2018-02-06 09:58:26 +08:00
|
|
|
else if (Name == ".sxdata")
|
|
|
|
SXDataChunks.push_back(C);
|
2018-03-16 05:14:02 +08:00
|
|
|
else if (Config->DoICF && Sec->NumberOfRelocations == 0 && Name == ".rdata" &&
|
|
|
|
LeaderName.startswith("??_C@"))
|
|
|
|
// 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 09:30:07 +08:00
|
|
|
else
|
|
|
|
Chunks.push_back(C);
|
|
|
|
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ObjFile::readAssociativeDefinition(
|
|
|
|
COFFSymbolRef Sym, const coff_aux_section_definition *Def) {
|
|
|
|
SectionChunk *Parent = SparseChunks[Def->getNumber(Sym.isBigObj())];
|
|
|
|
|
|
|
|
// If the parent is pending, it probably means that its section definition
|
|
|
|
// appears after us in the symbol table. Leave the associated section as
|
|
|
|
// pending; we will handle it during the second pass in initializeSymbols().
|
|
|
|
if (Parent == PendingComdat)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Check whether the parent is prevailing. If it is, so are we, and we read
|
|
|
|
// the section; otherwise mark it as discarded.
|
|
|
|
int32_t SectionNumber = Sym.getSectionNumber();
|
|
|
|
if (Parent) {
|
2018-03-16 05:14:02 +08:00
|
|
|
SparseChunks[SectionNumber] = readSection(SectionNumber, Def, "");
|
2017-11-28 09:30:07 +08:00
|
|
|
if (SparseChunks[SectionNumber])
|
|
|
|
Parent->addAssociative(SparseChunks[SectionNumber]);
|
|
|
|
} else {
|
|
|
|
SparseChunks[SectionNumber] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
return Symtab->addUndefined(Name, this, false);
|
|
|
|
}
|
|
|
|
if (SC)
|
|
|
|
return make<DefinedRegular>(this, /*Name*/ "", false,
|
|
|
|
/*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);
|
2017-05-25 01:12:32 +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);
|
|
|
|
|
|
|
|
std::vector<const coff_aux_section_definition *> ComdatDefs(
|
|
|
|
COFFObj->getNumberOfSections() + 1);
|
2017-05-25 01:12:32 +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));
|
|
|
|
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);
|
|
|
|
} else if (Optional<Symbol *> OptSym = createDefined(COFFSym, ComdatDefs)) {
|
|
|
|
Symbols[I] = *OptSym;
|
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;
|
|
|
|
// 2) symbol belongs to a comdat section associated with a section whose
|
|
|
|
// section definition symbol appears later in the symbol table.
|
|
|
|
// 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));
|
|
|
|
if (auto *Def = Sym.getSectionDefinition())
|
|
|
|
if (Def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
|
|
|
|
readAssociativeDefinition(Sym, Def);
|
|
|
|
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
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *ObjFile::createUndefined(COFFSymbolRef Sym) {
|
2015-06-30 06:16:21 +08:00
|
|
|
StringRef Name;
|
|
|
|
COFFObj->getSymbolName(Sym, Name);
|
2017-11-01 00:10:24 +08:00
|
|
|
return Symtab->addUndefined(Name, this, Sym.isWeakExternal());
|
2015-06-30 06:16:21 +08:00
|
|
|
}
|
|
|
|
|
2017-11-28 09:30:07 +08:00
|
|
|
Optional<Symbol *> ObjFile::createDefined(
|
|
|
|
COFFSymbolRef Sym,
|
|
|
|
std::vector<const coff_aux_section_definition *> &ComdatDefs) {
|
2015-06-09 03:43:59 +08:00
|
|
|
StringRef Name;
|
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);
|
2017-02-03 07:58:14 +08:00
|
|
|
COFFObj->getSymbolName(Sym, Name);
|
2017-11-04 05:21:47 +08:00
|
|
|
Symbol *S =
|
2017-02-03 07:58:14 +08:00
|
|
|
Symtab->addCommon(this, Name, Sym.getValue(), Sym.getGeneric(), C);
|
2017-11-01 00:10:24 +08:00
|
|
|
return S;
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
2015-06-09 03:43:59 +08:00
|
|
|
if (Sym.isAbsolute()) {
|
|
|
|
COFFObj->getSymbolName(Sym, Name);
|
|
|
|
// Skip special symbols.
|
2015-07-25 07:51:14 +08:00
|
|
|
if (Name == "@comp.id")
|
2015-06-09 03:43:59 +08:00
|
|
|
return nullptr;
|
2015-07-25 07:51:14 +08:00
|
|
|
if (Name == "@feat.00") {
|
2018-02-06 09:58:26 +08:00
|
|
|
Feat00Flags = Sym.getValue();
|
2015-07-25 07:51:14 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-12-10 05:55:24 +08:00
|
|
|
if (Sym.isExternal())
|
2017-11-01 00:10:24 +08:00
|
|
|
return Symtab->addAbsolute(Name, Sym);
|
2016-12-10 05:55:24 +08:00
|
|
|
else
|
2017-05-19 01:03:49 +08:00
|
|
|
return make<DefinedAbsolute>(Name, Sym);
|
2015-06-09 03:43:59 +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
|
|
|
// Reserved sections numbers don't have contents.
|
|
|
|
if (llvm::COFF::isReservedSectionNumber(SectionNumber))
|
2016-12-08 07:17:02 +08:00
|
|
|
fatal("broken object file: " + toString(this));
|
2016-03-16 00:47:28 +08:00
|
|
|
|
|
|
|
// This symbol references a section which is not present in the section
|
|
|
|
// header.
|
|
|
|
if ((uint32_t)SectionNumber >= SparseChunks.size())
|
2016-12-08 07:17:02 +08:00
|
|
|
fatal("broken object file: " + toString(this));
|
2016-03-16 00:47:28 +08:00
|
|
|
|
2017-11-28 09:30:07 +08:00
|
|
|
// Handle comdat leader symbols.
|
|
|
|
if (const coff_aux_section_definition *Def = ComdatDefs[SectionNumber]) {
|
|
|
|
ComdatDefs[SectionNumber] = nullptr;
|
|
|
|
Symbol *Leader;
|
|
|
|
bool Prevailing;
|
|
|
|
if (Sym.isExternal()) {
|
|
|
|
COFFObj->getSymbolName(Sym, Name);
|
|
|
|
std::tie(Leader, Prevailing) =
|
|
|
|
Symtab->addComdat(this, Name, Sym.getGeneric());
|
|
|
|
} else {
|
|
|
|
Leader = make<DefinedRegular>(this, /*Name*/ "", false,
|
|
|
|
/*IsExternal*/ false, Sym.getGeneric());
|
|
|
|
Prevailing = true;
|
|
|
|
}
|
|
|
|
if (Prevailing) {
|
2018-03-16 05:14:02 +08:00
|
|
|
SectionChunk *C = readSection(SectionNumber, Def, Name);
|
2017-11-28 09:30:07 +08:00
|
|
|
SparseChunks[SectionNumber] = C;
|
|
|
|
C->Sym = cast<DefinedRegular>(Leader);
|
|
|
|
cast<DefinedRegular>(Leader)->Data = &C->Repl;
|
|
|
|
} else {
|
|
|
|
SparseChunks[SectionNumber] = nullptr;
|
|
|
|
}
|
|
|
|
return Leader;
|
|
|
|
}
|
2015-06-30 05:32:37 +08:00
|
|
|
|
2017-11-28 09:30:07 +08:00
|
|
|
// Read associative section definitions and prepare to handle the comdat
|
|
|
|
// leader symbol by setting the section's ComdatDefs pointer if we encounter a
|
|
|
|
// non-associative comdat.
|
|
|
|
if (SparseChunks[SectionNumber] == PendingComdat) {
|
|
|
|
if (auto *Def = Sym.getSectionDefinition()) {
|
|
|
|
if (Def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
|
|
|
|
readAssociativeDefinition(Sym, Def);
|
|
|
|
else
|
|
|
|
ComdatDefs[SectionNumber] = Def;
|
|
|
|
}
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
2015-06-30 05:32:37 +08:00
|
|
|
|
2017-11-28 09:30:07 +08:00
|
|
|
if (SparseChunks[SectionNumber] == PendingComdat)
|
|
|
|
return None;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-07-10 04:22:41 +08:00
|
|
|
StringRef ltrim1(StringRef S, const char *Chars) {
|
|
|
|
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();
|
|
|
|
const char *End = MB.getBufferEnd();
|
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.
|
2015-08-06 22:58:50 +08:00
|
|
|
if ((size_t)(End - Buf) != (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;
|
2015-09-02 15:27:31 +08:00
|
|
|
DLLName = 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);
|
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)
|
2017-09-02 07:35:43 +08:00
|
|
|
ThunkSym = Symtab->addImportThunk(Name, ImpSym, Hdr->Machine);
|
2015-05-29 03:09:30 +08:00
|
|
|
}
|
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
void BitcodeFile::parse() {
|
2017-03-29 05:20:06 +08:00
|
|
|
Obj = check(lto::InputFile::create(MemoryBufferRef(
|
|
|
|
MB.getBuffer(), Saver.save(ParentName + MB.getBufferIdentifier()))));
|
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)
|
|
|
|
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);
|
|
|
|
std::string Fallback = 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);
|
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;
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 18:04:08 +08:00
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|
2015-07-10 03:54:13 +08:00
|
|
|
|
2016-12-08 07:17:02 +08:00
|
|
|
// Returns the last element of a path, which is supposed to be a filename.
|
|
|
|
static StringRef getBasename(StringRef Path) {
|
|
|
|
size_t Pos = Path.find_last_of("\\/");
|
|
|
|
if (Pos == StringRef::npos)
|
|
|
|
return Path;
|
|
|
|
return Path.substr(Pos + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
|
2017-12-06 00:50:46 +08:00
|
|
|
std::string lld::toString(const coff::InputFile *File) {
|
2016-12-08 07:17:02 +08:00
|
|
|
if (!File)
|
2017-11-28 06:49:16 +08:00
|
|
|
return "<internal>";
|
2016-12-08 07:17:02 +08:00
|
|
|
if (File->ParentName.empty())
|
2017-12-07 09:21:27 +08:00
|
|
|
return File->getName();
|
2016-12-08 07:17:02 +08:00
|
|
|
|
2017-12-07 09:21:27 +08:00
|
|
|
return (getBasename(File->ParentName) + "(" + getBasename(File->getName()) +
|
|
|
|
")")
|
|
|
|
.str();
|
2016-12-08 07:17:02 +08:00
|
|
|
}
|