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"
|
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"
|
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;
|
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)) {
|
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
|
|
|
|
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;
|
|
|
|
if (auto EC = COFFObj->getSection(SectionNumber, Sec))
|
|
|
|
fatal("getSection failed: #" + Twine(SectionNumber) + ": " + EC.message());
|
2019-01-15 03:05:21 +08:00
|
|
|
|
|
|
|
StringRef Name;
|
2017-11-28 09:30:07 +08:00
|
|
|
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
|
|
|
|
2018-08-24 01:44:42 +08:00
|
|
|
if (Name == ".llvm_addrsig") {
|
|
|
|
AddrsigSec = 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);
|
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-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);
|
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) {
|
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-01-23 10:07:10 +08:00
|
|
|
|
2019-01-26 08:14:52 +08:00
|
|
|
auto Diag = [&]() {
|
2019-01-23 10:07:10 +08:00
|
|
|
StringRef Name, ParentName;
|
|
|
|
COFFObj->getSymbolName(Sym, Name);
|
2017-11-28 09:30:07 +08:00
|
|
|
|
2019-01-23 10:07:10 +08:00
|
|
|
const coff_section *ParentSec;
|
|
|
|
COFFObj->getSection(ParentIndex, ParentSec);
|
|
|
|
COFFObj->getSectionName(ParentSec, ParentName);
|
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.
|
|
|
|
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 09:30:07 +08: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-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);
|
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);
|
|
|
|
|
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);
|
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));
|
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) {
|
|
|
|
StringRef Name;
|
|
|
|
COFFObj->getSymbolName(Sym, Name);
|
|
|
|
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
|
|
|
}
|
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,
|
2018-08-07 05:26:09 +08:00
|
|
|
std::vector<const coff_aux_section_definition *> &ComdatDefs,
|
|
|
|
bool &Prevailing) {
|
|
|
|
Prevailing = false;
|
2018-07-25 06:52:11 +08:00
|
|
|
auto GetName = [&]() {
|
|
|
|
StringRef S;
|
|
|
|
COFFObj->getSymbolName(Sym, S);
|
|
|
|
return S;
|
|
|
|
};
|
|
|
|
|
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();
|
|
|
|
|
2015-06-09 03:43:59 +08:00
|
|
|
// 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;
|
|
|
|
}
|
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
|
|
|
|
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;
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
2018-10-19 14:39:36 +08:00
|
|
|
// If this was a duplicate, we logged an error but may continue;
|
|
|
|
// in this case, ImpSym is nullptr.
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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) {
|
2018-07-21 07:06:34 +08:00
|
|
|
return sys::path::filename(Path, sys::path::Style::windows);
|
2016-12-08 07:17:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|