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-11-20 10:10:52 +08:00
|
|
|
template <class ELFT>
|
|
|
|
ELFKind ELFFileBase<ELFT>::getELFKind() {
|
2016-01-06 08:09:41 +08:00
|
|
|
if (ELFT::TargetEndianness == support::little)
|
|
|
|
return ELFT::Is64Bits ? ELF64LEKind : ELF32LEKind;
|
|
|
|
return ELFT::Is64Bits ? ELF64BEKind : ELF32BEKind;
|
2015-11-20 10:10:52 +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 {
|
2015-12-24 16:36:56 +08:00
|
|
|
uint32_t I = Sym.st_shndx;
|
|
|
|
if (I == ELF::SHN_XINDEX)
|
2016-01-06 09:14:11 +08:00
|
|
|
return ELFObj.getExtendedSymbolTableIndex(&Sym, Symtab, SymtabSHNDX);
|
2015-12-24 22:22:24 +08:00
|
|
|
if (I >= ELF::SHN_LORESERVE || I == ELF::SHN_ABS)
|
2015-11-03 22:13:40 +08:00
|
|
|
return 0;
|
2015-12-24 16:36:56 +08:00
|
|
|
return I;
|
2015-11-03 22:13:40 +08:00
|
|
|
}
|
|
|
|
|
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);
|
2016-01-06 08:09:41 +08:00
|
|
|
error(StringTableOrErr);
|
2015-09-08 23:50:05 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-12-25 21:02:13 +08:00
|
|
|
template <class ELFT> uint32_t ObjectFile<ELFT>::getMipsGp0() const {
|
2016-01-07 06:42:43 +08:00
|
|
|
if (MipsReginfo)
|
|
|
|
return MipsReginfo->Reginfo->ri_gp_value;
|
|
|
|
return 0;
|
2015-12-25 21:02:13 +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>
|
2016-01-06 10:06:33 +08:00
|
|
|
void ObjectFile<ELFT>::parse(DenseSet<StringRef> &ComdatGroups) {
|
2015-07-25 05:03:07 +08:00
|
|
|
// Read section and symbol tables.
|
2016-01-06 10:06:33 +08:00
|
|
|
initializeSections(ComdatGroups);
|
2015-07-25 05:03:07 +08:00
|
|
|
initializeSymbols();
|
|
|
|
}
|
|
|
|
|
2015-12-24 16:41:12 +08:00
|
|
|
// Sections with SHT_GROUP and comdat bits define comdat section groups.
|
|
|
|
// They are identified and deduplicated by group name. This function
|
|
|
|
// returns a group name.
|
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>
|
2016-01-07 04:30:02 +08:00
|
|
|
ArrayRef<typename ObjectFile<ELFT>::uint32_X>
|
2015-10-10 03:25:07 +08:00
|
|
|
ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
|
|
|
|
const ELFFile<ELFT> &Obj = this->ELFObj;
|
2016-01-07 04:30:02 +08:00
|
|
|
ErrorOr<ArrayRef<uint32_X>> EntriesOrErr =
|
|
|
|
Obj.template getSectionContentsAsArray<uint32_X>(&Sec);
|
2016-01-06 08:09:41 +08:00
|
|
|
error(EntriesOrErr);
|
2016-01-07 04:30:02 +08:00
|
|
|
ArrayRef<uint32_X> Entries = *EntriesOrErr;
|
2015-10-10 03:25:07 +08:00
|
|
|
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>
|
2016-01-06 10:06:33 +08:00
|
|
|
void ObjectFile<ELFT>::initializeSections(DenseSet<StringRef> &ComdatGroups) {
|
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;
|
2016-01-06 10:06:33 +08:00
|
|
|
if (ComdatGroups.insert(getShtGroupSignature(Sec)).second)
|
2015-10-10 03:25:07 +08:00
|
|
|
continue;
|
2016-01-07 04:30:02 +08:00
|
|
|
for (uint32_t SecIndex : getShtGroupEntries(Sec)) {
|
2015-10-10 03:25:07 +08:00
|
|
|
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-22 06:19:32 +08:00
|
|
|
default:
|
2015-12-24 16:41:12 +08:00
|
|
|
Sections[I] = createInputSection(Sec);
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-24 16:41:12 +08:00
|
|
|
template <class ELFT> InputSectionBase<ELFT> *
|
2016-01-06 08:09:41 +08:00
|
|
|
ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
|
2015-12-24 16:41:12 +08:00
|
|
|
ErrorOr<StringRef> NameOrErr = this->ELFObj.getSectionName(&Sec);
|
|
|
|
error(NameOrErr);
|
|
|
|
StringRef Name = *NameOrErr;
|
|
|
|
|
|
|
|
// .note.GNU-stack is a marker section to control the presence of
|
|
|
|
// PT_GNU_STACK segment in outputs. Since the presence of the segment
|
|
|
|
// is controlled only by the command line option (-z execstack) in LLD,
|
|
|
|
// .note.GNU-stack is ignored.
|
|
|
|
if (Name == ".note.GNU-stack")
|
|
|
|
return &InputSection<ELFT>::Discarded;
|
|
|
|
|
|
|
|
// A MIPS object file has a special section that contains register
|
|
|
|
// usage info, which needs to be handled by the linker specially.
|
2015-12-25 21:02:13 +08:00
|
|
|
if (Config->EMachine == EM_MIPS && Name == ".reginfo") {
|
2016-01-06 09:14:11 +08:00
|
|
|
MipsReginfo = new (Alloc) MipsReginfoInputSection<ELFT>(this, &Sec);
|
2015-12-25 21:02:13 +08:00
|
|
|
return MipsReginfo;
|
|
|
|
}
|
2015-12-24 16:41:12 +08:00
|
|
|
|
|
|
|
if (Name == ".eh_frame")
|
2016-01-06 09:14:11 +08:00
|
|
|
return new (EHAlloc.Allocate()) EHInputSection<ELFT>(this, &Sec);
|
2015-12-24 16:41:12 +08:00
|
|
|
if (shouldMerge<ELFT>(Sec))
|
2016-01-06 09:14:11 +08:00
|
|
|
return new (MAlloc.Allocate()) MergeInputSection<ELFT>(this, &Sec);
|
|
|
|
return new (Alloc) InputSection<ELFT>(this, &Sec);
|
2015-12-24 16:41:12 +08:00
|
|
|
}
|
|
|
|
|
2016-01-06 08:09:41 +08:00
|
|
|
template <class ELFT> void 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());
|
2016-01-06 09:14:11 +08:00
|
|
|
SymbolBodies.reserve(NumSymbols);
|
2015-08-04 22:00:56 +08:00
|
|
|
for (const Elf_Sym &Sym : Syms)
|
2016-01-06 09:14:11 +08:00
|
|
|
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> *
|
2016-01-06 08:09:41 +08:00
|
|
|
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>
|
2016-01-06 08:09:41 +08:00
|
|
|
SymbolBody *ObjectFile<ELFT>::createSymbolBody(StringRef StringTable,
|
2016-01-21 08:45:39 +08:00
|
|
|
const Elf_Sym *Sym) {
|
2015-07-25 05:03:07 +08:00
|
|
|
ErrorOr<StringRef> NameOrErr = Sym->getName(StringTable);
|
2016-01-06 08:09:41 +08:00
|
|
|
error(NameOrErr);
|
2015-07-25 05:03:07 +08:00
|
|
|
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_UNDEF:
|
2016-01-06 09:14:11 +08:00
|
|
|
return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
|
2015-08-29 05:26:51 +08:00
|
|
|
case SHN_COMMON:
|
2016-01-06 09:14:11 +08:00
|
|
|
return new (Alloc) DefinedCommon(Name, Sym->st_size, Sym->st_value,
|
|
|
|
Sym->getBinding() == llvm::ELF::STB_WEAK,
|
|
|
|
Sym->getVisibility());
|
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)
|
2016-01-06 09:14:11 +08:00
|
|
|
return new (Alloc) UndefinedElf<ELFT>(Name, *Sym);
|
|
|
|
return new (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
|
|
|
void ArchiveFile::parse() {
|
2016-01-06 08:51:35 +08:00
|
|
|
ErrorOr<std::unique_ptr<Archive>> FileOrErr = Archive::create(MB);
|
|
|
|
error(FileOrErr, "Failed to parse archive");
|
|
|
|
File = std::move(*FileOrErr);
|
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-12-17 09:51:23 +08:00
|
|
|
ErrorOr<MemoryBufferRef> RefOrErr = C.getMemoryBufferRef();
|
|
|
|
if (!RefOrErr)
|
|
|
|
error(RefOrErr, "Could not get the buffer for the member defining symbol " +
|
|
|
|
Sym->getName());
|
|
|
|
return *RefOrErr;
|
2015-09-05 06:28:10 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
template <class ELFT>
|
|
|
|
SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
|
2016-01-06 08:09:41 +08:00
|
|
|
: ELFFileBase<ELFT>(Base::SharedKind, M), 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;
|
|
|
|
}
|
|
|
|
|
2016-01-06 09:56:36 +08:00
|
|
|
// Partially parse the shared object file so that we can call
|
|
|
|
// getSoName on this object.
|
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();
|
2016-01-06 09:14:11 +08:00
|
|
|
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");
|
2016-01-06 09:14:11 +08:00
|
|
|
SoName = StringRef(this->StringTable.data() + Val);
|
2015-10-12 23:49:02 +08:00
|
|
|
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
|
|
|
|
2016-01-06 09:56:36 +08:00
|
|
|
// Fully parse the shared object file. This must be called after parseSoName().
|
|
|
|
template <class ELFT> void SharedFile<ELFT>::parseRest() {
|
2015-10-02 03:52:48 +08:00
|
|
|
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>
|
2016-01-06 08:09:43 +08:00
|
|
|
static std::unique_ptr<InputFile> createELFFile(MemoryBufferRef MB) {
|
2015-11-20 10:19:36 +08:00
|
|
|
std::pair<unsigned char, unsigned char> Type = getElfArchType(MB.getBuffer());
|
2015-10-12 23:31:09 +08:00
|
|
|
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)
|
2015-11-20 10:19:36 +08:00
|
|
|
return createELFFileAux<T<ELF32LE>>(MB);
|
|
|
|
return createELFFileAux<T<ELF32BE>>(MB);
|
2015-10-12 23:31:09 +08:00
|
|
|
}
|
|
|
|
if (Type.first == ELF::ELFCLASS64) {
|
|
|
|
if (Type.second == ELF::ELFDATA2LSB)
|
2015-11-20 10:19:36 +08:00
|
|
|
return createELFFileAux<T<ELF64LE>>(MB);
|
|
|
|
return createELFFileAux<T<ELF64BE>>(MB);
|
2015-10-12 23:31:09 +08:00
|
|
|
}
|
|
|
|
error("Invalid file class: " + MB.getBufferIdentifier());
|
|
|
|
}
|
|
|
|
|
2016-01-06 08:09:43 +08:00
|
|
|
std::unique_ptr<InputFile> elf2::createObjectFile(MemoryBufferRef MB) {
|
|
|
|
return createELFFile<ObjectFile>(MB);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<InputFile> elf2::createSharedFile(MemoryBufferRef MB) {
|
|
|
|
return createELFFile<SharedFile>(MB);
|
|
|
|
}
|
|
|
|
|
2015-11-20 10:19:36 +08:00
|
|
|
template class elf2::ELFFileBase<ELF32LE>;
|
|
|
|
template class elf2::ELFFileBase<ELF32BE>;
|
|
|
|
template class elf2::ELFFileBase<ELF64LE>;
|
|
|
|
template class elf2::ELFFileBase<ELF64BE>;
|
|
|
|
|
|
|
|
template class elf2::ObjectFile<ELF32LE>;
|
|
|
|
template class elf2::ObjectFile<ELF32BE>;
|
|
|
|
template class elf2::ObjectFile<ELF64LE>;
|
|
|
|
template class elf2::ObjectFile<ELF64BE>;
|
|
|
|
|
|
|
|
template class elf2::SharedFile<ELF32LE>;
|
|
|
|
template class elf2::SharedFile<ELF32BE>;
|
|
|
|
template class elf2::SharedFile<ELF64LE>;
|
|
|
|
template class elf2::SharedFile<ELF64BE>;
|