2015-07-25 05:03:07 +08:00
|
|
|
//===- InputFiles.cpp -----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "InputFiles.h"
|
2015-09-22 08:01:39 +08:00
|
|
|
#include "InputSection.h"
|
2015-08-06 23:08:23 +08:00
|
|
|
#include "Error.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "Symbols.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
using namespace llvm;
|
2015-07-25 05:03:07 +08:00
|
|
|
using namespace llvm::ELF;
|
2015-09-04 04:03:54 +08:00
|
|
|
using namespace llvm::object;
|
2015-10-01 01:06:09 +08:00
|
|
|
using namespace llvm::sys::fs;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf2;
|
|
|
|
|
2015-09-04 04:03:54 +08:00
|
|
|
template <class ELFT> static uint16_t getEMachine(const ELFFileBase &B) {
|
|
|
|
bool IsShared = isa<SharedFileBase>(B);
|
|
|
|
if (IsShared)
|
|
|
|
return cast<SharedFile<ELFT>>(B).getEMachine();
|
|
|
|
return cast<ObjectFile<ELFT>>(B).getEMachine();
|
2015-08-05 20:03:34 +08:00
|
|
|
}
|
|
|
|
|
2015-09-23 00:53:55 +08:00
|
|
|
uint16_t ELFFileBase::getEMachine() const {
|
|
|
|
switch (EKind) {
|
2015-09-04 04:03:54 +08:00
|
|
|
case ELF32BEKind:
|
2015-09-23 00:53:55 +08:00
|
|
|
return ::getEMachine<ELF32BE>(*this);
|
2015-09-04 04:03:54 +08:00
|
|
|
case ELF32LEKind:
|
2015-09-23 00:53:55 +08:00
|
|
|
return ::getEMachine<ELF32LE>(*this);
|
2015-09-04 04:03:54 +08:00
|
|
|
case ELF64BEKind:
|
2015-09-23 00:53:55 +08:00
|
|
|
return ::getEMachine<ELF64BE>(*this);
|
2015-09-04 04:03:54 +08:00
|
|
|
case ELF64LEKind:
|
2015-09-23 00:53:55 +08:00
|
|
|
return ::getEMachine<ELF64LE>(*this);
|
2015-09-04 04:03:54 +08:00
|
|
|
}
|
2015-09-04 06:25:11 +08:00
|
|
|
llvm_unreachable("Invalid kind");
|
2015-09-04 04:03:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ELFFileBase::isCompatibleWith(const ELFFileBase &Other) const {
|
|
|
|
return getELFKind() == Other.getELFKind() &&
|
2015-09-23 00:53:55 +08:00
|
|
|
getEMachine() == Other.getEMachine();
|
2015-09-04 04:03:54 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
namespace {
|
|
|
|
class ECRAII {
|
2015-07-25 05:03:07 +08:00
|
|
|
std::error_code EC;
|
2015-09-24 23:11:50 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
std::error_code &getEC() { return EC; }
|
|
|
|
~ECRAII() { error(EC); }
|
|
|
|
};
|
2015-09-04 04:03:54 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
template <class ELFT>
|
|
|
|
ELFData<ELFT>::ELFData(MemoryBufferRef MB)
|
|
|
|
: ELFObj(MB.getBuffer(), ECRAII().getEC()) {}
|
|
|
|
|
2015-09-08 23:50:05 +08:00
|
|
|
template <class ELFT>
|
2015-09-17 04:45:57 +08:00
|
|
|
typename ELFData<ELFT>::Elf_Sym_Range
|
|
|
|
ELFData<ELFT>::getSymbolsHelper(bool Local) {
|
2015-09-08 23:50:05 +08:00
|
|
|
if (!Symtab)
|
|
|
|
return Elf_Sym_Range(nullptr, nullptr);
|
2015-09-24 23:11:50 +08:00
|
|
|
Elf_Sym_Range Syms = ELFObj.symbols(Symtab);
|
2015-09-17 04:45:57 +08:00
|
|
|
uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
|
|
|
|
uint32_t FirstNonLocal = Symtab->sh_info;
|
|
|
|
if (FirstNonLocal > NumSymbols)
|
|
|
|
error("Invalid sh_info in symbol table");
|
|
|
|
if (!Local)
|
2015-09-30 10:37:51 +08:00
|
|
|
return make_range(Syms.begin() + FirstNonLocal, Syms.end());
|
|
|
|
// +1 to skip over dummy symbol.
|
|
|
|
return make_range(Syms.begin() + 1, Syms.begin() + FirstNonLocal);
|
2015-09-17 04:45:57 +08:00
|
|
|
}
|
2015-09-08 23:50:05 +08:00
|
|
|
|
2015-09-17 04:45:57 +08:00
|
|
|
template <class ELFT>
|
|
|
|
typename ELFData<ELFT>::Elf_Sym_Range ELFData<ELFT>::getNonLocalSymbols() {
|
|
|
|
if (!Symtab)
|
|
|
|
return Elf_Sym_Range(nullptr, nullptr);
|
2015-09-24 23:11:50 +08:00
|
|
|
ErrorOr<StringRef> StringTableOrErr = ELFObj.getStringTableForSymtab(*Symtab);
|
2015-09-08 23:50:05 +08:00
|
|
|
error(StringTableOrErr.getError());
|
|
|
|
StringTable = *StringTableOrErr;
|
2015-09-17 04:45:57 +08:00
|
|
|
return getSymbolsHelper(false);
|
|
|
|
}
|
2015-09-08 23:50:05 +08:00
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
template <class ELFT>
|
|
|
|
ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
|
|
|
|
: ObjectFileBase(getStaticELFKind<ELFT>(), M), ELFData<ELFT>(M) {}
|
|
|
|
|
2015-09-17 04:45:57 +08:00
|
|
|
template <class ELFT>
|
|
|
|
typename ObjectFile<ELFT>::Elf_Sym_Range ObjectFile<ELFT>::getLocalSymbols() {
|
|
|
|
return this->getSymbolsHelper(true);
|
2015-09-08 23:50:05 +08:00
|
|
|
}
|
|
|
|
|
2015-09-04 04:03:54 +08:00
|
|
|
template <class ELFT> void elf2::ObjectFile<ELFT>::parse() {
|
2015-07-25 05:03:07 +08:00
|
|
|
// Read section and symbol tables.
|
2015-09-22 08:16:19 +08:00
|
|
|
initializeSections();
|
2015-07-25 05:03:07 +08:00
|
|
|
initializeSymbols();
|
|
|
|
}
|
|
|
|
|
2015-09-22 08:16:19 +08:00
|
|
|
template <class ELFT> void elf2::ObjectFile<ELFT>::initializeSections() {
|
2015-09-24 23:11:50 +08:00
|
|
|
uint64_t Size = this->ELFObj.getNumSections();
|
2015-09-22 08:16:19 +08:00
|
|
|
Sections.resize(Size);
|
2015-08-25 04:06:32 +08:00
|
|
|
unsigned I = 0;
|
2015-09-24 23:11:50 +08:00
|
|
|
for (const Elf_Shdr &Sec : this->ELFObj.sections()) {
|
2015-08-13 22:45:44 +08:00
|
|
|
switch (Sec.sh_type) {
|
|
|
|
case SHT_SYMTAB:
|
2015-09-08 23:50:05 +08:00
|
|
|
this->Symtab = &Sec;
|
2015-08-13 22:45:44 +08:00
|
|
|
break;
|
2015-08-25 05:43:25 +08:00
|
|
|
case SHT_SYMTAB_SHNDX: {
|
2015-09-04 04:03:54 +08:00
|
|
|
ErrorOr<ArrayRef<Elf_Word>> ErrorOrTable =
|
2015-09-24 23:11:50 +08:00
|
|
|
this->ELFObj.getSHNDXTable(Sec);
|
2015-08-25 05:43:25 +08:00
|
|
|
error(ErrorOrTable);
|
|
|
|
SymtabSHNDX = *ErrorOrTable;
|
|
|
|
break;
|
|
|
|
}
|
2015-08-13 22:45:44 +08:00
|
|
|
case SHT_STRTAB:
|
|
|
|
case SHT_NULL:
|
2015-08-28 07:15:56 +08:00
|
|
|
break;
|
2015-08-13 22:45:44 +08:00
|
|
|
case SHT_RELA:
|
2015-08-28 07:15:56 +08:00
|
|
|
case SHT_REL: {
|
|
|
|
uint32_t RelocatedSectionIndex = Sec.sh_info;
|
|
|
|
if (RelocatedSectionIndex >= Size)
|
|
|
|
error("Invalid relocated section index");
|
2015-09-22 08:16:19 +08:00
|
|
|
InputSection<ELFT> *RelocatedSection = Sections[RelocatedSectionIndex];
|
2015-08-28 07:15:56 +08:00
|
|
|
if (!RelocatedSection)
|
|
|
|
error("Unsupported relocation reference");
|
|
|
|
RelocatedSection->RelocSections.push_back(&Sec);
|
2015-08-13 22:45:44 +08:00
|
|
|
break;
|
2015-08-28 07:15:56 +08:00
|
|
|
}
|
2015-08-13 22:45:44 +08:00
|
|
|
default:
|
2015-09-22 08:16:19 +08:00
|
|
|
Sections[I] = new (Alloc) InputSection<ELFT>(this, &Sec);
|
2015-08-13 22:45:44 +08:00
|
|
|
break;
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
2015-08-25 04:06:32 +08:00
|
|
|
++I;
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void elf2::ObjectFile<ELFT>::initializeSymbols() {
|
2015-09-08 23:50:05 +08:00
|
|
|
Elf_Sym_Range Syms = this->getNonLocalSymbols();
|
2015-08-12 04:06:51 +08:00
|
|
|
uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
|
2015-09-08 23:50:05 +08:00
|
|
|
SymbolBodies.reserve(NumSymbols);
|
2015-08-04 22:00:56 +08:00
|
|
|
for (const Elf_Sym &Sym : Syms)
|
2015-09-08 23:50:05 +08:00
|
|
|
SymbolBodies.push_back(createSymbolBody(this->StringTable, &Sym));
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
SymbolBody *elf2::ObjectFile<ELFT>::createSymbolBody(StringRef StringTable,
|
|
|
|
const Elf_Sym *Sym) {
|
|
|
|
ErrorOr<StringRef> NameOrErr = Sym->getName(StringTable);
|
|
|
|
error(NameOrErr.getError());
|
|
|
|
StringRef Name = *NameOrErr;
|
2015-08-25 05:43:25 +08:00
|
|
|
|
|
|
|
uint32_t SecIndex = Sym->st_shndx;
|
2015-08-29 05:26:51 +08:00
|
|
|
switch (SecIndex) {
|
|
|
|
case SHN_ABS:
|
2015-08-27 20:40:06 +08:00
|
|
|
return new (Alloc) DefinedAbsolute<ELFT>(Name, *Sym);
|
2015-08-29 05:26:51 +08:00
|
|
|
case SHN_UNDEF:
|
|
|
|
return new (Alloc) Undefined<ELFT>(Name, *Sym);
|
|
|
|
case SHN_COMMON:
|
|
|
|
return new (Alloc) DefinedCommon<ELFT>(Name, *Sym);
|
|
|
|
case SHN_XINDEX:
|
2015-09-24 23:11:50 +08:00
|
|
|
SecIndex = this->ELFObj.getExtendedSymbolTableIndex(Sym, this->Symtab,
|
|
|
|
SymtabSHNDX);
|
2015-08-29 05:26:51 +08:00
|
|
|
break;
|
|
|
|
}
|
2015-08-25 05:43:25 +08:00
|
|
|
|
2015-09-22 08:16:19 +08:00
|
|
|
if (SecIndex >= Sections.size() || (SecIndex != 0 && !Sections[SecIndex]))
|
2015-08-25 06:00:25 +08:00
|
|
|
error("Invalid section index");
|
|
|
|
|
2015-08-12 01:33:02 +08:00
|
|
|
switch (Sym->getBinding()) {
|
|
|
|
default:
|
|
|
|
error("unexpected binding");
|
|
|
|
case STB_GLOBAL:
|
2015-08-29 04:19:34 +08:00
|
|
|
case STB_WEAK:
|
2015-09-22 08:16:19 +08:00
|
|
|
return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, *Sections[SecIndex]);
|
2015-08-12 01:33:02 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
void ArchiveFile::parse() {
|
2015-09-30 10:42:27 +08:00
|
|
|
ErrorOr<std::unique_ptr<Archive>> ArchiveOrErr = Archive::create(MB);
|
2015-09-05 06:28:10 +08:00
|
|
|
error(ArchiveOrErr, "Failed to parse archive");
|
|
|
|
File = std::move(*ArchiveOrErr);
|
|
|
|
|
|
|
|
// Allocate a buffer for Lazy objects.
|
|
|
|
size_t NumSyms = File->getNumberOfSymbols();
|
|
|
|
LazySymbols.reserve(NumSyms);
|
|
|
|
|
|
|
|
// Read the symbol table to construct Lazy objects.
|
|
|
|
for (const Archive::Symbol &Sym : File->symbols())
|
|
|
|
LazySymbols.emplace_back(this, Sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a buffer pointing to a member file containing a given symbol.
|
|
|
|
MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
|
|
|
|
ErrorOr<Archive::child_iterator> ItOrErr = Sym->getMember();
|
|
|
|
error(ItOrErr,
|
|
|
|
Twine("Could not get the member for symbol ") + Sym->getName());
|
|
|
|
Archive::child_iterator It = *ItOrErr;
|
|
|
|
|
2015-09-09 04:36:20 +08:00
|
|
|
if (!Seen.insert(It->getChildOffset()).second)
|
2015-09-05 06:28:10 +08:00
|
|
|
return MemoryBufferRef();
|
2015-09-09 04:36:20 +08:00
|
|
|
|
2015-09-05 06:28:10 +08:00
|
|
|
ErrorOr<MemoryBufferRef> Ret = It->getMemoryBufferRef();
|
|
|
|
error(Ret, Twine("Could not get the buffer for the member defining symbol ") +
|
|
|
|
Sym->getName());
|
|
|
|
return *Ret;
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
template <class ELFT>
|
|
|
|
SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
|
|
|
|
: SharedFileBase(getStaticELFKind<ELFT>(), M), ELFData<ELFT>(M) {}
|
2015-09-08 23:50:05 +08:00
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
template <class ELFT> void SharedFile<ELFT>::parse() {
|
|
|
|
for (const Elf_Shdr &Sec : this->ELFObj.sections()) {
|
2015-09-08 23:50:05 +08:00
|
|
|
if (Sec.sh_type == SHT_DYNSYM) {
|
|
|
|
this->Symtab = &Sec;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Elf_Sym_Range Syms = this->getNonLocalSymbols();
|
|
|
|
uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
|
|
|
|
SymbolBodies.reserve(NumSymbols);
|
|
|
|
for (const Elf_Sym &Sym : Syms) {
|
|
|
|
if (Sym.isUndefined())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ErrorOr<StringRef> NameOrErr = Sym.getName(this->StringTable);
|
|
|
|
error(NameOrErr.getError());
|
|
|
|
StringRef Name = *NameOrErr;
|
|
|
|
|
|
|
|
SymbolBodies.emplace_back(Name, Sym);
|
|
|
|
}
|
|
|
|
}
|
2015-09-04 04:03:54 +08:00
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace elf2 {
|
2015-09-17 04:45:57 +08:00
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
template class elf2::ObjectFile<llvm::object::ELF32LE>;
|
|
|
|
template class elf2::ObjectFile<llvm::object::ELF32BE>;
|
|
|
|
template class elf2::ObjectFile<llvm::object::ELF64LE>;
|
|
|
|
template class elf2::ObjectFile<llvm::object::ELF64BE>;
|
2015-09-04 04:03:54 +08:00
|
|
|
|
|
|
|
template class elf2::SharedFile<llvm::object::ELF32LE>;
|
|
|
|
template class elf2::SharedFile<llvm::object::ELF32BE>;
|
|
|
|
template class elf2::SharedFile<llvm::object::ELF64LE>;
|
|
|
|
template class elf2::SharedFile<llvm::object::ELF64BE>;
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
}
|