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-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>
|
2015-10-13 09:17:02 +08:00
|
|
|
ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef M)
|
|
|
|
: InputFile(K, M), ELFObj(MB.getBuffer(), ECRAII().getEC()) {}
|
2015-09-24 23:11:50 +08:00
|
|
|
|
2015-09-08 23:50:05 +08:00
|
|
|
template <class ELFT>
|
2015-10-12 09:55:32 +08:00
|
|
|
typename ELFFileBase<ELFT>::Elf_Sym_Range
|
|
|
|
ELFFileBase<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-11-03 22:13:40 +08:00
|
|
|
template <class ELFT>
|
|
|
|
uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
|
|
|
|
uint32_t Index = Sym.st_shndx;
|
|
|
|
if (Index == ELF::SHN_XINDEX)
|
|
|
|
Index = this->ELFObj.getExtendedSymbolTableIndex(&Sym, this->Symtab,
|
|
|
|
SymtabSHNDX);
|
|
|
|
else if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!Index)
|
|
|
|
error("Invalid section index");
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
2015-10-12 09:55:32 +08:00
|
|
|
template <class ELFT> void ELFFileBase<ELFT>::initStringTable() {
|
2015-10-02 04:26:37 +08:00
|
|
|
if (!Symtab)
|
|
|
|
return;
|
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-10-02 03:52:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2015-10-12 09:55:32 +08:00
|
|
|
typename ELFFileBase<ELFT>::Elf_Sym_Range
|
|
|
|
ELFFileBase<ELFT>::getNonLocalSymbols() {
|
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)
|
2015-10-13 09:17:02 +08:00
|
|
|
: ELFFileBase<ELFT>(Base::ObjectKind, M) {}
|
2015-09-24 23:11:50 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
ELF2: Implement --gc-sections.
Section garbage collection is a feature to remove unused sections
from outputs. Unused sections are sections that cannot be reachable
from known GC-root symbols or sections. Naturally the feature is
implemented as a mark-sweep garbage collector.
In this patch, I added Live bit to InputSectionBase. If and only
if Live bit is on, the section will be written to the output.
Starting from GC-root symbols or sections, a new function, markLive(),
visits all reachable sections and sets their Live bits. Writer then
ignores sections whose Live bit is off, so that such sections are
excluded from the output.
This change has small negative impact on performance if you use
the feature because making sections means more work. The time to
link Clang changes from 0.356s to 0.386s, or +8%.
It reduces Clang size from 57,764,984 bytes to 55,296,600 bytes.
That is 4.3% reduction.
http://reviews.llvm.org/D13950
llvm-svn: 251043
2015-10-23 02:49:53 +08:00
|
|
|
template <class ELFT>
|
|
|
|
const typename ObjectFile<ELFT>::Elf_Sym *
|
|
|
|
ObjectFile<ELFT>::getLocalSymbol(uintX_t SymIndex) {
|
|
|
|
uint32_t FirstNonLocal = this->Symtab->sh_info;
|
|
|
|
if (SymIndex >= FirstNonLocal)
|
|
|
|
return nullptr;
|
|
|
|
Elf_Sym_Range Syms = this->ELFObj.symbols(this->Symtab);
|
|
|
|
return Syms.begin() + SymIndex;
|
|
|
|
}
|
|
|
|
|
2015-10-10 03:25:07 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void elf2::ObjectFile<ELFT>::parse(DenseSet<StringRef> &Comdats) {
|
2015-07-25 05:03:07 +08:00
|
|
|
// Read section and symbol tables.
|
2015-10-10 03:25:07 +08:00
|
|
|
initializeSections(Comdats);
|
2015-07-25 05:03:07 +08:00
|
|
|
initializeSymbols();
|
|
|
|
}
|
|
|
|
|
2015-10-10 03:25:07 +08:00
|
|
|
template <class ELFT>
|
|
|
|
StringRef ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) {
|
|
|
|
const ELFFile<ELFT> &Obj = this->ELFObj;
|
|
|
|
uint32_t SymtabdSectionIndex = Sec.sh_link;
|
|
|
|
ErrorOr<const Elf_Shdr *> SecOrErr = Obj.getSection(SymtabdSectionIndex);
|
|
|
|
error(SecOrErr);
|
|
|
|
const Elf_Shdr *SymtabSec = *SecOrErr;
|
|
|
|
uint32_t SymIndex = Sec.sh_info;
|
|
|
|
const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex);
|
|
|
|
ErrorOr<StringRef> StringTableOrErr = Obj.getStringTableForSymtab(*SymtabSec);
|
|
|
|
error(StringTableOrErr);
|
|
|
|
ErrorOr<StringRef> SignatureOrErr = Sym->getName(*StringTableOrErr);
|
|
|
|
error(SignatureOrErr);
|
|
|
|
return *SignatureOrErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
ArrayRef<typename ObjectFile<ELFT>::GroupEntryType>
|
|
|
|
ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
|
|
|
|
const ELFFile<ELFT> &Obj = this->ELFObj;
|
|
|
|
ErrorOr<ArrayRef<GroupEntryType>> EntriesOrErr =
|
|
|
|
Obj.template getSectionContentsAsArray<GroupEntryType>(&Sec);
|
|
|
|
error(EntriesOrErr.getError());
|
|
|
|
ArrayRef<GroupEntryType> Entries = *EntriesOrErr;
|
|
|
|
if (Entries.empty() || Entries[0] != GRP_COMDAT)
|
|
|
|
error("Unsupported SHT_GROUP format");
|
|
|
|
return Entries.slice(1);
|
|
|
|
}
|
|
|
|
|
2015-10-25 06:51:01 +08:00
|
|
|
template <class ELFT>
|
|
|
|
static bool shouldMerge(const typename ELFFile<ELFT>::Elf_Shdr &Sec) {
|
|
|
|
typedef typename ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
uintX_t Flags = Sec.sh_flags;
|
|
|
|
if (!(Flags & SHF_MERGE))
|
|
|
|
return false;
|
|
|
|
if (Flags & SHF_WRITE)
|
|
|
|
error("Writable SHF_MERGE sections are not supported");
|
|
|
|
uintX_t EntSize = Sec.sh_entsize;
|
2015-11-09 16:40:44 +08:00
|
|
|
if (!EntSize || Sec.sh_size % EntSize)
|
2015-10-25 06:51:01 +08:00
|
|
|
error("SHF_MERGE section size must be a multiple of sh_entsize");
|
|
|
|
|
|
|
|
// Don't try to merge if the aligment is larger than the sh_entsize.
|
|
|
|
//
|
|
|
|
// If this is not a SHF_STRINGS, we would need to pad after every entity. It
|
|
|
|
// would be equivalent for the producer of the .o to just set a larger
|
|
|
|
// sh_entsize.
|
|
|
|
//
|
|
|
|
// If this is a SHF_STRINGS, the larger alignment makes sense. Unfortunately
|
|
|
|
// it would complicate tail merging. This doesn't seem that common to
|
|
|
|
// justify the effort.
|
|
|
|
if (Sec.sh_addralign > EntSize)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-10 03:25:07 +08:00
|
|
|
template <class ELFT>
|
|
|
|
void elf2::ObjectFile<ELFT>::initializeSections(DenseSet<StringRef> &Comdats) {
|
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-10-10 03:25:07 +08:00
|
|
|
unsigned I = -1;
|
2015-10-08 20:02:38 +08:00
|
|
|
const ELFFile<ELFT> &Obj = this->ELFObj;
|
|
|
|
for (const Elf_Shdr &Sec : Obj.sections()) {
|
2015-10-10 03:25:07 +08:00
|
|
|
++I;
|
|
|
|
if (Sections[I] == &InputSection<ELFT>::Discarded)
|
|
|
|
continue;
|
|
|
|
|
2015-08-13 22:45:44 +08:00
|
|
|
switch (Sec.sh_type) {
|
2015-10-10 03:25:07 +08:00
|
|
|
case SHT_GROUP:
|
|
|
|
Sections[I] = &InputSection<ELFT>::Discarded;
|
|
|
|
if (Comdats.insert(getShtGroupSignature(Sec)).second)
|
|
|
|
continue;
|
|
|
|
for (GroupEntryType E : getShtGroupEntries(Sec)) {
|
|
|
|
uint32_t SecIndex = E;
|
|
|
|
if (SecIndex >= Size)
|
|
|
|
error("Invalid section index in group");
|
|
|
|
Sections[SecIndex] = &InputSection<ELFT>::Discarded;
|
|
|
|
}
|
|
|
|
break;
|
2015-08-13 22:45:44 +08:00
|
|
|
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-10-08 20:02:38 +08:00
|
|
|
ErrorOr<ArrayRef<Elf_Word>> ErrorOrTable = Obj.getSHNDXTable(Sec);
|
2015-08-25 05:43:25 +08:00
|
|
|
error(ErrorOrTable);
|
2015-11-03 22:13:40 +08:00
|
|
|
this->SymtabSHNDX = *ErrorOrTable;
|
2015-08-25 05:43:25 +08:00
|
|
|
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-10-20 05:00:02 +08:00
|
|
|
InputSectionBase<ELFT> *RelocatedSection =
|
|
|
|
Sections[RelocatedSectionIndex];
|
2015-08-28 07:15:56 +08:00
|
|
|
if (!RelocatedSection)
|
|
|
|
error("Unsupported relocation reference");
|
2015-11-12 03:54:14 +08:00
|
|
|
if (auto *S = dyn_cast<InputSection<ELFT>>(RelocatedSection)) {
|
2015-10-20 05:00:02 +08:00
|
|
|
S->RelocSections.push_back(&Sec);
|
2015-11-12 03:54:14 +08:00
|
|
|
} else if (auto *S = dyn_cast<EHInputSection<ELFT>>(RelocatedSection)) {
|
|
|
|
if (S->RelocSection)
|
|
|
|
error("Multiple relocation sections to .eh_frame are not supported");
|
|
|
|
S->RelocSection = &Sec;
|
|
|
|
} else {
|
2015-10-20 05:00:02 +08:00
|
|
|
error("Relocations pointing to SHF_MERGE are not supported");
|
2015-11-12 03:54:14 +08:00
|
|
|
}
|
2015-08-13 22:45:44 +08:00
|
|
|
break;
|
2015-08-28 07:15:56 +08:00
|
|
|
}
|
2015-11-12 03:54:14 +08:00
|
|
|
default: {
|
|
|
|
ErrorOr<StringRef> NameOrErr = this->ELFObj.getSectionName(&Sec);
|
|
|
|
error(NameOrErr);
|
2015-11-15 04:56:08 +08:00
|
|
|
if (*NameOrErr == ".note.GNU-stack")
|
|
|
|
Sections[I] = &InputSection<ELFT>::Discarded;
|
|
|
|
else if (*NameOrErr == ".eh_frame")
|
2015-11-12 03:54:14 +08:00
|
|
|
Sections[I] = new (this->Alloc) EHInputSection<ELFT>(this, &Sec);
|
|
|
|
else if (shouldMerge<ELFT>(Sec))
|
2015-10-20 05:00:02 +08:00
|
|
|
Sections[I] = new (this->Alloc) MergeInputSection<ELFT>(this, &Sec);
|
2015-10-25 06:51:01 +08:00
|
|
|
else
|
2015-10-20 05:00:02 +08:00
|
|
|
Sections[I] = new (this->Alloc) InputSection<ELFT>(this, &Sec);
|
2015-08-13 22:45:44 +08:00
|
|
|
break;
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
2015-11-12 03:54:14 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void elf2::ObjectFile<ELFT>::initializeSymbols() {
|
2015-10-02 03:52:48 +08:00
|
|
|
this->initStringTable();
|
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-10-12 09:55:32 +08:00
|
|
|
this->SymbolBodies.reserve(NumSymbols);
|
2015-08-04 22:00:56 +08:00
|
|
|
for (const Elf_Sym &Sym : Syms)
|
2015-10-12 09:55:32 +08:00
|
|
|
this->SymbolBodies.push_back(createSymbolBody(this->StringTable, &Sym));
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-10-16 23:29:48 +08:00
|
|
|
template <class ELFT>
|
2015-10-20 05:00:02 +08:00
|
|
|
InputSectionBase<ELFT> *
|
2015-10-16 23:29:48 +08:00
|
|
|
elf2::ObjectFile<ELFT>::getSection(const Elf_Sym &Sym) const {
|
2015-11-03 22:13:40 +08:00
|
|
|
uint32_t Index = this->getSectionIndex(Sym);
|
|
|
|
if (Index == 0)
|
2015-10-16 23:29:48 +08:00
|
|
|
return nullptr;
|
2015-11-03 22:13:40 +08:00
|
|
|
if (Index >= Sections.size() || !Sections[Index])
|
2015-10-16 23:29:48 +08:00
|
|
|
error("Invalid section index");
|
|
|
|
return Sections[Index];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-10-16 23:29:48 +08:00
|
|
|
switch (Sym->st_shndx) {
|
2015-08-29 05:26:51 +08:00
|
|
|
case SHN_ABS:
|
2015-10-12 09:55:32 +08:00
|
|
|
return new (this->Alloc) DefinedAbsolute<ELFT>(Name, *Sym);
|
2015-08-29 05:26:51 +08:00
|
|
|
case SHN_UNDEF:
|
2015-10-12 09:55:32 +08:00
|
|
|
return new (this->Alloc) Undefined<ELFT>(Name, *Sym);
|
2015-08-29 05:26:51 +08:00
|
|
|
case SHN_COMMON:
|
2015-10-12 09:55:32 +08:00
|
|
|
return new (this->Alloc) DefinedCommon<ELFT>(Name, *Sym);
|
2015-08-29 05:26:51 +08:00
|
|
|
}
|
2015-08-25 05:43:25 +08:00
|
|
|
|
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-10-10 03:25:07 +08:00
|
|
|
case STB_GNU_UNIQUE: {
|
2015-10-20 05:00:02 +08:00
|
|
|
InputSectionBase<ELFT> *Sec = getSection(*Sym);
|
2015-10-10 03:25:07 +08:00
|
|
|
if (Sec == &InputSection<ELFT>::Discarded)
|
2015-10-12 09:55:32 +08:00
|
|
|
return new (this->Alloc) Undefined<ELFT>(Name, *Sym);
|
|
|
|
return new (this->Alloc) DefinedRegular<ELFT>(Name, *Sym, *Sec);
|
2015-10-10 03:25:07 +08:00
|
|
|
}
|
2015-08-12 01:33:02 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-10-02 02:02:21 +08:00
|
|
|
static std::unique_ptr<Archive> openArchive(MemoryBufferRef MB) {
|
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");
|
2015-10-02 02:02:21 +08:00
|
|
|
return std::move(*ArchiveOrErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArchiveFile::parse() {
|
|
|
|
File = openArchive(MB);
|
2015-09-05 06:28:10 +08:00
|
|
|
|
|
|
|
// 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) {
|
2015-11-05 22:40:28 +08:00
|
|
|
ErrorOr<Archive::Child> COrErr = Sym->getMember();
|
|
|
|
error(COrErr, "Could not get the member for symbol " + Sym->getName());
|
|
|
|
const Archive::Child &C = *COrErr;
|
2015-09-05 06:28:10 +08:00
|
|
|
|
2015-11-05 22:40:28 +08:00
|
|
|
if (!Seen.insert(C.getChildOffset()).second)
|
2015-09-05 06:28:10 +08:00
|
|
|
return MemoryBufferRef();
|
2015-09-09 04:36:20 +08:00
|
|
|
|
2015-11-05 22:40:28 +08:00
|
|
|
ErrorOr<MemoryBufferRef> Ret = C.getMemoryBufferRef();
|
2015-10-12 23:49:06 +08:00
|
|
|
error(Ret, "Could not get the buffer for the member defining symbol " +
|
2015-09-05 06:28:10 +08:00
|
|
|
Sym->getName());
|
|
|
|
return *Ret;
|
|
|
|
}
|
|
|
|
|
2015-10-02 02:02:21 +08:00
|
|
|
std::vector<MemoryBufferRef> ArchiveFile::getMembers() {
|
|
|
|
File = openArchive(MB);
|
|
|
|
|
|
|
|
std::vector<MemoryBufferRef> Result;
|
2015-11-06 03:25:47 +08:00
|
|
|
for (auto &ChildOrErr : File->children()) {
|
|
|
|
error(ChildOrErr,
|
|
|
|
"Could not get the child of the archive " + File->getFileName());
|
|
|
|
const Archive::Child Child(*ChildOrErr);
|
2015-10-02 02:02:21 +08:00
|
|
|
ErrorOr<MemoryBufferRef> MbOrErr = Child.getMemoryBufferRef();
|
2015-10-12 23:49:06 +08:00
|
|
|
error(MbOrErr, "Could not get the buffer for a child of the archive " +
|
|
|
|
File->getFileName());
|
2015-10-02 02:02:21 +08:00
|
|
|
Result.push_back(MbOrErr.get());
|
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
template <class ELFT>
|
|
|
|
SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
|
2015-10-13 09:17:02 +08:00
|
|
|
: ELFFileBase<ELFT>(Base::SharedKind, M) {
|
2015-10-12 10:22:58 +08:00
|
|
|
AsNeeded = Config->AsNeeded;
|
|
|
|
}
|
2015-09-08 23:50:05 +08:00
|
|
|
|
2015-11-03 22:13:40 +08:00
|
|
|
template <class ELFT>
|
|
|
|
const typename ELFFile<ELFT>::Elf_Shdr *
|
|
|
|
SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
|
|
|
|
uint32_t Index = this->getSectionIndex(Sym);
|
|
|
|
if (Index == 0)
|
|
|
|
return nullptr;
|
|
|
|
ErrorOr<const Elf_Shdr *> Ret = this->ELFObj.getSection(Index);
|
|
|
|
error(Ret);
|
|
|
|
return *Ret;
|
|
|
|
}
|
|
|
|
|
2015-10-02 03:52:48 +08:00
|
|
|
template <class ELFT> void SharedFile<ELFT>::parseSoName() {
|
2015-10-01 23:47:50 +08:00
|
|
|
typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
|
|
|
|
typedef typename ELFFile<ELFT>::uintX_t uintX_t;
|
|
|
|
const Elf_Shdr *DynamicSec = nullptr;
|
|
|
|
|
|
|
|
const ELFFile<ELFT> Obj = this->ELFObj;
|
|
|
|
for (const Elf_Shdr &Sec : Obj.sections()) {
|
2015-11-03 22:13:40 +08:00
|
|
|
switch (Sec.sh_type) {
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
case SHT_DYNSYM:
|
2015-09-08 23:50:05 +08:00
|
|
|
this->Symtab = &Sec;
|
2015-11-03 22:13:40 +08:00
|
|
|
break;
|
|
|
|
case SHT_DYNAMIC:
|
2015-10-01 23:47:50 +08:00
|
|
|
DynamicSec = &Sec;
|
2015-11-03 22:13:40 +08:00
|
|
|
break;
|
|
|
|
case SHT_SYMTAB_SHNDX: {
|
|
|
|
ErrorOr<ArrayRef<Elf_Word>> ErrorOrTable = Obj.getSHNDXTable(Sec);
|
|
|
|
error(ErrorOrTable);
|
|
|
|
this->SymtabSHNDX = *ErrorOrTable;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 23:50:05 +08:00
|
|
|
}
|
|
|
|
|
2015-10-02 03:52:48 +08:00
|
|
|
this->initStringTable();
|
2015-10-12 09:55:32 +08:00
|
|
|
this->SoName = this->getName();
|
2015-10-01 23:47:50 +08:00
|
|
|
|
2015-10-12 23:49:02 +08:00
|
|
|
if (!DynamicSec)
|
|
|
|
return;
|
|
|
|
auto *Begin =
|
|
|
|
reinterpret_cast<const Elf_Dyn *>(Obj.base() + DynamicSec->sh_offset);
|
|
|
|
const Elf_Dyn *End = Begin + DynamicSec->sh_size / sizeof(Elf_Dyn);
|
|
|
|
|
|
|
|
for (const Elf_Dyn &Dyn : make_range(Begin, End)) {
|
|
|
|
if (Dyn.d_tag == DT_SONAME) {
|
|
|
|
uintX_t Val = Dyn.getVal();
|
|
|
|
if (Val >= this->StringTable.size())
|
|
|
|
error("Invalid DT_SONAME entry");
|
|
|
|
this->SoName = StringRef(this->StringTable.data() + Val);
|
|
|
|
return;
|
2015-10-01 23:47:50 +08:00
|
|
|
}
|
|
|
|
}
|
2015-10-02 03:52:48 +08:00
|
|
|
}
|
2015-10-01 23:47:50 +08:00
|
|
|
|
2015-10-02 03:52:48 +08:00
|
|
|
template <class ELFT> void SharedFile<ELFT>::parse() {
|
|
|
|
Elf_Sym_Range Syms = this->getNonLocalSymbols();
|
2015-09-08 23:50:05 +08:00
|
|
|
uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
|
|
|
|
SymbolBodies.reserve(NumSymbols);
|
|
|
|
for (const Elf_Sym &Sym : Syms) {
|
|
|
|
ErrorOr<StringRef> NameOrErr = Sym.getName(this->StringTable);
|
|
|
|
error(NameOrErr.getError());
|
|
|
|
StringRef Name = *NameOrErr;
|
|
|
|
|
2015-10-14 00:34:14 +08:00
|
|
|
if (Sym.isUndefined())
|
|
|
|
Undefs.push_back(Name);
|
|
|
|
else
|
|
|
|
SymbolBodies.emplace_back(this, Name, Sym);
|
2015-09-08 23:50:05 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-04 04:03:54 +08:00
|
|
|
|
2015-10-12 23:31:09 +08:00
|
|
|
template <typename T>
|
|
|
|
static std::unique_ptr<InputFile> createELFFileAux(MemoryBufferRef MB) {
|
|
|
|
std::unique_ptr<T> Ret = llvm::make_unique<T>(MB);
|
|
|
|
|
|
|
|
if (!Config->FirstElf)
|
|
|
|
Config->FirstElf = Ret.get();
|
|
|
|
|
2015-10-14 00:20:50 +08:00
|
|
|
if (Config->EKind == ELFNoneKind) {
|
|
|
|
Config->EKind = Ret->getELFKind();
|
2015-10-12 23:31:09 +08:00
|
|
|
Config->EMachine = Ret->getEMachine();
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(Ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <template <class> class T>
|
|
|
|
std::unique_ptr<InputFile> lld::elf2::createELFFile(MemoryBufferRef MB) {
|
|
|
|
std::pair<unsigned char, unsigned char> Type =
|
|
|
|
object::getElfArchType(MB.getBuffer());
|
|
|
|
if (Type.second != ELF::ELFDATA2LSB && Type.second != ELF::ELFDATA2MSB)
|
|
|
|
error("Invalid data encoding: " + MB.getBufferIdentifier());
|
|
|
|
|
|
|
|
if (Type.first == ELF::ELFCLASS32) {
|
|
|
|
if (Type.second == ELF::ELFDATA2LSB)
|
|
|
|
return createELFFileAux<T<object::ELF32LE>>(MB);
|
|
|
|
return createELFFileAux<T<object::ELF32BE>>(MB);
|
|
|
|
}
|
|
|
|
if (Type.first == ELF::ELFCLASS64) {
|
|
|
|
if (Type.second == ELF::ELFDATA2LSB)
|
|
|
|
return createELFFileAux<T<object::ELF64LE>>(MB);
|
|
|
|
return createELFFileAux<T<object::ELF64BE>>(MB);
|
|
|
|
}
|
|
|
|
error("Invalid file class: " + MB.getBufferIdentifier());
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace elf2 {
|
2015-10-12 09:55:32 +08:00
|
|
|
template class ELFFileBase<llvm::object::ELF32LE>;
|
|
|
|
template class ELFFileBase<llvm::object::ELF32BE>;
|
|
|
|
template class ELFFileBase<llvm::object::ELF64LE>;
|
|
|
|
template class ELFFileBase<llvm::object::ELF64BE>;
|
2015-09-17 04:45:57 +08:00
|
|
|
|
2015-10-12 23:27:09 +08:00
|
|
|
template class ObjectFile<llvm::object::ELF32LE>;
|
|
|
|
template class ObjectFile<llvm::object::ELF32BE>;
|
|
|
|
template class ObjectFile<llvm::object::ELF64LE>;
|
|
|
|
template class ObjectFile<llvm::object::ELF64BE>;
|
|
|
|
|
|
|
|
template class SharedFile<llvm::object::ELF32LE>;
|
|
|
|
template class SharedFile<llvm::object::ELF32BE>;
|
|
|
|
template class SharedFile<llvm::object::ELF64LE>;
|
|
|
|
template class SharedFile<llvm::object::ELF64BE>;
|
2015-10-12 23:31:09 +08:00
|
|
|
|
|
|
|
template std::unique_ptr<InputFile> createELFFile<ObjectFile>(MemoryBufferRef);
|
|
|
|
template std::unique_ptr<InputFile> createELFFile<SharedFile>(MemoryBufferRef);
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
}
|