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"
|
2016-04-29 03:30:41 +08:00
|
|
|
#include "Driver.h"
|
2015-08-06 23:08:23 +08:00
|
|
|
#include "Error.h"
|
2016-02-11 23:24:48 +08:00
|
|
|
#include "InputSection.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
#include "Symbols.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-04-22 05:44:25 +08:00
|
|
|
#include "llvm/CodeGen/Analysis.h"
|
2016-02-13 04:54:57 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2016-03-02 23:43:50 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2016-02-13 04:54:57 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-07-25 05:03:07 +08:00
|
|
|
|
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;
|
2016-02-28 08:25:54 +08:00
|
|
|
using namespace lld::elf;
|
2015-07-25 05:03:07 +08:00
|
|
|
|
2016-03-03 14:22:29 +08:00
|
|
|
template <class ELFT>
|
|
|
|
static ELFFile<ELFT> createELFObj(MemoryBufferRef MB) {
|
2015-07-25 05:03:07 +08:00
|
|
|
std::error_code EC;
|
2016-03-03 14:22:29 +08:00
|
|
|
ELFFile<ELFT> F(MB.getBuffer(), EC);
|
2016-03-04 06:24:39 +08:00
|
|
|
check(EC);
|
2016-03-03 14:22:29 +08:00
|
|
|
return F;
|
2015-09-04 04:03:54 +08:00
|
|
|
}
|
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
template <class ELFT>
|
2016-03-03 14:22:29 +08:00
|
|
|
ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB)
|
|
|
|
: InputFile(K, MB), ELFObj(createELFObj<ELFT>(MB)) {}
|
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>
|
2016-03-15 07:16:09 +08:00
|
|
|
typename ELFT::SymRange ELFFileBase<ELFT>::getElfSymbols(bool OnlyGlobals) {
|
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)
|
2016-03-12 16:31:34 +08:00
|
|
|
fatal("invalid sh_info in symbol table");
|
2016-03-11 20:06:30 +08:00
|
|
|
|
|
|
|
if (OnlyGlobals)
|
2016-04-05 22:47:28 +08:00
|
|
|
return makeArrayRef(Syms.begin() + FirstNonLocal, Syms.end());
|
|
|
|
return makeArrayRef(Syms.begin(), Syms.end());
|
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);
|
2016-03-09 22:31:18 +08:00
|
|
|
if (I >= ELF::SHN_LORESERVE)
|
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;
|
2016-03-04 06:24:39 +08:00
|
|
|
StringTable = check(ELFObj.getStringTableForSymtab(*Symtab));
|
2015-10-02 03:52:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2016-03-11 20:06:30 +08:00
|
|
|
elf::ObjectFile<ELFT>::ObjectFile(MemoryBufferRef M)
|
|
|
|
: ELFFileBase<ELFT>(Base::ObjectKind, M) {}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getNonLocalSymbols() {
|
|
|
|
if (!this->Symtab)
|
|
|
|
return this->SymbolBodies;
|
|
|
|
uint32_t FirstNonLocal = this->Symtab->sh_info;
|
|
|
|
return makeArrayRef(this->SymbolBodies).slice(FirstNonLocal);
|
2015-09-17 04:45:57 +08:00
|
|
|
}
|
2015-09-08 23:50:05 +08:00
|
|
|
|
2015-09-24 23:11:50 +08:00
|
|
|
template <class ELFT>
|
2016-03-11 20:06:30 +08:00
|
|
|
ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getLocalSymbols() {
|
|
|
|
if (!this->Symtab)
|
|
|
|
return this->SymbolBodies;
|
|
|
|
uint32_t FirstNonLocal = this->Symtab->sh_info;
|
|
|
|
return makeArrayRef(this->SymbolBodies).slice(1, FirstNonLocal - 1);
|
|
|
|
}
|
2015-09-24 23:11:50 +08:00
|
|
|
|
2015-09-17 04:45:57 +08:00
|
|
|
template <class ELFT>
|
2016-03-11 20:06:30 +08:00
|
|
|
ArrayRef<SymbolBody *> elf::ObjectFile<ELFT>::getSymbols() {
|
|
|
|
if (!this->Symtab)
|
|
|
|
return this->SymbolBodies;
|
|
|
|
return makeArrayRef(this->SymbolBodies).slice(1);
|
2015-09-08 23:50:05 +08:00
|
|
|
}
|
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
template <class ELFT> uint32_t elf::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
|
|
|
}
|
|
|
|
|
2015-10-10 03:25:07 +08:00
|
|
|
template <class ELFT>
|
2016-02-28 08:25:54 +08:00
|
|
|
void elf::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>
|
2016-02-28 08:25:54 +08:00
|
|
|
StringRef elf::ObjectFile<ELFT>::getShtGroupSignature(const Elf_Shdr &Sec) {
|
2015-10-10 03:25:07 +08:00
|
|
|
const ELFFile<ELFT> &Obj = this->ELFObj;
|
|
|
|
uint32_t SymtabdSectionIndex = Sec.sh_link;
|
2016-03-04 06:24:39 +08:00
|
|
|
const Elf_Shdr *SymtabSec = check(Obj.getSection(SymtabdSectionIndex));
|
2015-10-10 03:25:07 +08:00
|
|
|
uint32_t SymIndex = Sec.sh_info;
|
|
|
|
const Elf_Sym *Sym = Obj.getSymbol(SymtabSec, SymIndex);
|
2016-03-04 06:24:39 +08:00
|
|
|
StringRef StringTable = check(Obj.getStringTableForSymtab(*SymtabSec));
|
|
|
|
return check(Sym->getName(StringTable));
|
2015-10-10 03:25:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
2016-03-14 06:02:04 +08:00
|
|
|
ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
|
2016-02-28 08:25:54 +08:00
|
|
|
elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
|
2015-10-10 03:25:07 +08:00
|
|
|
const ELFFile<ELFT> &Obj = this->ELFObj;
|
2016-03-14 06:02:04 +08:00
|
|
|
ArrayRef<Elf_Word> Entries =
|
|
|
|
check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec));
|
2015-10-10 03:25:07 +08:00
|
|
|
if (Entries.empty() || Entries[0] != GRP_COMDAT)
|
2016-03-12 16:31:34 +08:00
|
|
|
fatal("unsupported SHT_GROUP format");
|
2015-10-10 03:25:07 +08:00
|
|
|
return Entries.slice(1);
|
|
|
|
}
|
|
|
|
|
2016-03-15 07:16:09 +08:00
|
|
|
template <class ELFT> static bool shouldMerge(const typename ELFT::Shdr &Sec) {
|
|
|
|
typedef typename ELFT::uint uintX_t;
|
2016-04-30 00:12:29 +08:00
|
|
|
|
|
|
|
// We don't merge sections if -O0 (default is -O1). This makes sometimes
|
|
|
|
// the linker significantly faster, although the output will be bigger.
|
|
|
|
if (Config->Optimize == 0)
|
|
|
|
return false;
|
|
|
|
|
2015-10-25 06:51:01 +08:00
|
|
|
uintX_t Flags = Sec.sh_flags;
|
|
|
|
if (!(Flags & SHF_MERGE))
|
|
|
|
return false;
|
|
|
|
if (Flags & SHF_WRITE)
|
2016-03-12 16:31:34 +08:00
|
|
|
fatal("writable SHF_MERGE sections are not supported");
|
2015-10-25 06:51:01 +08:00
|
|
|
uintX_t EntSize = Sec.sh_entsize;
|
2016-03-21 22:57:20 +08:00
|
|
|
if (!EntSize || Sec.sh_size % EntSize)
|
ELF: Rename error -> fatal and redefine error as a non-noreturn function.
In many situations, we don't want to exit at the first error even in the
process model. For example, it is better to report all undefined symbols
rather than reporting the first one that the linker picked up randomly.
In order to handle such errors, we don't need to wrap everything with
ErrorOr (thanks for David Blaikie for pointing this out!) Instead, we
can set a flag to record the fact that we found an error and keep it
going until it reaches a reasonable checkpoint.
This idea should be applicable to other places. For example, we can
ignore broken relocations and check for errors after visiting all relocs.
In this patch, I rename error to fatal, and introduce another version of
error which doesn't call exit. That function instead sets HasError to true.
Once HasError becomes true, it stays true, so that we know that there
was an error if it is true.
I think introducing a non-noreturn error reporting function is by itself
a good idea, and it looks to me that this also provides a gradual path
towards lld-as-a-library (or at least embed-lld-to-your-program) without
sacrificing code readability with lots of ErrorOr's.
http://reviews.llvm.org/D16641
llvm-svn: 259069
2016-01-29 02:40:06 +08:00
|
|
|
fatal("SHF_MERGE section size must be a multiple of sh_entsize");
|
2015-10-25 06:51:01 +08:00
|
|
|
|
2016-02-19 22:17:40 +08:00
|
|
|
// Don't try to merge if the aligment is larger than the sh_entsize and this
|
|
|
|
// is not SHF_STRINGS.
|
2015-10-25 06:51:01 +08:00
|
|
|
//
|
2016-02-19 22:17:40 +08:00
|
|
|
// Since 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
|
2015-10-25 06:51:01 +08:00
|
|
|
// sh_entsize.
|
2016-02-19 22:17:40 +08:00
|
|
|
if (Flags & SHF_STRINGS)
|
|
|
|
return true;
|
|
|
|
|
2015-10-25 06:51:01 +08:00
|
|
|
if (Sec.sh_addralign > EntSize)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-10 03:25:07 +08:00
|
|
|
template <class ELFT>
|
2016-02-28 08:25:54 +08:00
|
|
|
void elf::ObjectFile<ELFT>::initializeSections(
|
2016-02-13 05:17:10 +08:00
|
|
|
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;
|
2016-04-04 22:04:16 +08:00
|
|
|
if (Sections[I] == &InputSection<ELFT>::Discarded)
|
2015-10-10 03:25:07 +08:00
|
|
|
continue;
|
|
|
|
|
2015-08-13 22:45:44 +08:00
|
|
|
switch (Sec.sh_type) {
|
2015-10-10 03:25:07 +08:00
|
|
|
case SHT_GROUP:
|
2016-04-04 22:04:16 +08:00
|
|
|
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)
|
2016-03-12 16:31:34 +08:00
|
|
|
fatal("invalid section index in group");
|
2016-04-04 22:04:16 +08:00
|
|
|
Sections[SecIndex] = &InputSection<ELFT>::Discarded;
|
2015-10-10 03:25:07 +08:00
|
|
|
}
|
|
|
|
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;
|
2016-03-04 00:21:44 +08:00
|
|
|
case SHT_SYMTAB_SHNDX:
|
2016-03-04 06:24:39 +08:00
|
|
|
this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
|
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: {
|
2016-03-14 05:52:57 +08:00
|
|
|
// This section contains relocation information.
|
|
|
|
// If -r is given, we do not interpret or apply relocation
|
|
|
|
// but just copy relocation sections to output.
|
2016-02-25 16:23:37 +08:00
|
|
|
if (Config->Relocatable) {
|
2016-04-06 09:11:10 +08:00
|
|
|
Sections[I] = new (IAlloc.Allocate()) InputSection<ELFT>(this, &Sec);
|
2016-03-14 05:52:57 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the relocation target section and associate this
|
|
|
|
// section with it.
|
|
|
|
InputSectionBase<ELFT> *Target = getRelocTarget(Sec);
|
|
|
|
if (!Target)
|
|
|
|
break;
|
|
|
|
if (auto *S = dyn_cast<InputSection<ELFT>>(Target)) {
|
2015-10-20 05:00:02 +08:00
|
|
|
S->RelocSections.push_back(&Sec);
|
2016-03-14 05:52:57 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (auto *S = dyn_cast<EHInputSection<ELFT>>(Target)) {
|
2015-11-12 03:54:14 +08:00
|
|
|
if (S->RelocSection)
|
2016-03-12 16:31:34 +08:00
|
|
|
fatal("multiple relocation sections to .eh_frame are not supported");
|
2015-11-12 03:54:14 +08:00
|
|
|
S->RelocSection = &Sec;
|
2016-03-14 05:52:57 +08:00
|
|
|
break;
|
2015-11-12 03:54:14 +08:00
|
|
|
}
|
2016-03-14 05:52:57 +08:00
|
|
|
fatal("relocations pointing to SHF_MERGE are not supported");
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 05:52:57 +08:00
|
|
|
template <class ELFT>
|
|
|
|
InputSectionBase<ELFT> *
|
|
|
|
elf::ObjectFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) {
|
|
|
|
uint32_t Idx = Sec.sh_info;
|
|
|
|
if (Idx >= Sections.size())
|
|
|
|
fatal("invalid relocated section index");
|
|
|
|
InputSectionBase<ELFT> *Target = Sections[Idx];
|
|
|
|
|
|
|
|
// Strictly speaking, a relocation section must be included in the
|
|
|
|
// group of the section it relocates. However, LLVM 3.3 and earlier
|
|
|
|
// would fail to do so, so we gracefully handle that case.
|
2016-04-04 22:04:16 +08:00
|
|
|
if (Target == &InputSection<ELFT>::Discarded)
|
2016-03-14 05:52:57 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (!Target)
|
|
|
|
fatal("unsupported relocation reference");
|
|
|
|
return Target;
|
|
|
|
}
|
|
|
|
|
2016-02-13 05:17:10 +08:00
|
|
|
template <class ELFT>
|
|
|
|
InputSectionBase<ELFT> *
|
2016-02-28 08:25:54 +08:00
|
|
|
elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec) {
|
2016-03-04 06:24:39 +08:00
|
|
|
StringRef Name = check(this->ELFObj.getSectionName(&Sec));
|
2015-12-24 16:41:12 +08:00
|
|
|
|
|
|
|
// .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")
|
2016-04-04 22:04:16 +08:00
|
|
|
return &InputSection<ELFT>::Discarded;
|
2015-12-24 16:41:12 +08:00
|
|
|
|
2016-04-08 05:04:51 +08:00
|
|
|
if (Name == ".note.GNU-split-stack") {
|
2016-03-12 16:31:34 +08:00
|
|
|
error("objects using splitstacks are not supported");
|
2016-04-08 05:04:51 +08:00
|
|
|
return &InputSection<ELFT>::Discarded;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Config->StripDebug && Name.startswith(".debug"))
|
|
|
|
return &InputSection<ELFT>::Discarded;
|
2016-03-10 02:01:45 +08:00
|
|
|
|
2015-12-24 16:41:12 +08:00
|
|
|
// 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-04-06 10:52:47 +08:00
|
|
|
MipsReginfo.reset(new MipsReginfoInputSection<ELFT>(this, &Sec));
|
|
|
|
return MipsReginfo.get();
|
2015-12-25 21:02:13 +08:00
|
|
|
}
|
2015-12-24 16:41:12 +08:00
|
|
|
|
2016-03-03 15:49:35 +08:00
|
|
|
// We dont need special handling of .eh_frame sections if relocatable
|
|
|
|
// output was choosen. Proccess them as usual input sections.
|
|
|
|
if (!Config->Relocatable && 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);
|
2016-04-06 09:11:10 +08:00
|
|
|
return new (IAlloc.Allocate()) InputSection<ELFT>(this, &Sec);
|
2015-12-24 16:41:12 +08:00
|
|
|
}
|
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
template <class ELFT> void elf::ObjectFile<ELFT>::initializeSymbols() {
|
2015-10-02 03:52:48 +08:00
|
|
|
this->initStringTable();
|
2016-03-11 20:06:30 +08:00
|
|
|
Elf_Sym_Range Syms = this->getElfSymbols(false);
|
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-21 10:10:12 +08:00
|
|
|
SymbolBodies.push_back(createSymbolBody(&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-02-28 08:25:54 +08:00
|
|
|
elf::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])
|
2016-03-12 16:31:34 +08:00
|
|
|
fatal("invalid section index");
|
2016-02-26 02:43:51 +08:00
|
|
|
InputSectionBase<ELFT> *S = Sections[Index];
|
2016-04-04 22:04:16 +08:00
|
|
|
if (S == &InputSectionBase<ELFT>::Discarded)
|
2016-02-26 02:43:51 +08:00
|
|
|
return S;
|
|
|
|
return S->Repl;
|
2015-10-16 23:29:48 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
template <class ELFT>
|
2016-02-28 08:25:54 +08:00
|
|
|
SymbolBody *elf::ObjectFile<ELFT>::createSymbolBody(const Elf_Sym *Sym) {
|
2016-04-06 01:47:29 +08:00
|
|
|
unsigned char Binding = Sym->getBinding();
|
2016-03-11 22:21:37 +08:00
|
|
|
InputSectionBase<ELFT> *Sec = getSection(*Sym);
|
|
|
|
if (Binding == STB_LOCAL) {
|
2016-04-04 22:04:16 +08:00
|
|
|
if (Sym->st_shndx == SHN_UNDEF)
|
2016-04-28 08:26:54 +08:00
|
|
|
return new (Alloc) Undefined(Sym->st_name, Sym->st_other, Sym->getType());
|
2016-04-05 19:47:46 +08:00
|
|
|
return new (Alloc) DefinedRegular<ELFT>(*Sym, Sec);
|
2016-03-11 22:21:37 +08:00
|
|
|
}
|
2016-03-11 20:06:30 +08:00
|
|
|
|
2016-03-04 06:24:39 +08:00
|
|
|
StringRef Name = check(Sym->getName(this->StringTable));
|
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-04-28 08:26:54 +08:00
|
|
|
return new (Alloc) Undefined(Name, Binding, Sym->st_other, Sym->getType(),
|
|
|
|
/*IsBitcode*/ false);
|
2015-08-29 05:26:51 +08:00
|
|
|
case SHN_COMMON:
|
2016-04-04 22:04:16 +08:00
|
|
|
return new (Alloc) DefinedCommon(Name, Sym->st_size, Sym->st_value, Binding,
|
|
|
|
Sym->st_other, Sym->getType());
|
2015-08-29 05:26:51 +08:00
|
|
|
}
|
2015-08-25 05:43:25 +08:00
|
|
|
|
2016-03-11 20:06:30 +08:00
|
|
|
switch (Binding) {
|
2015-08-12 01:33:02 +08:00
|
|
|
default:
|
2016-03-11 22:43:02 +08:00
|
|
|
fatal("unexpected binding");
|
2015-08-12 01:33:02 +08:00
|
|
|
case STB_GLOBAL:
|
2015-08-29 04:19:34 +08:00
|
|
|
case STB_WEAK:
|
2016-03-11 22:21:37 +08:00
|
|
|
case STB_GNU_UNIQUE:
|
2016-04-04 22:04:16 +08:00
|
|
|
if (Sec == &InputSection<ELFT>::Discarded)
|
2016-04-28 08:26:54 +08:00
|
|
|
return new (Alloc) Undefined(Name, Binding, Sym->st_other, Sym->getType(),
|
|
|
|
/*IsBitcode*/ false);
|
2016-01-06 09:14:11 +08:00
|
|
|
return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, Sec);
|
2015-10-10 03:25:07 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
2015-10-02 02:02:21 +08:00
|
|
|
void ArchiveFile::parse() {
|
2016-03-15 05:31:07 +08:00
|
|
|
File = check(Archive::create(MB), "failed to parse archive");
|
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) {
|
2016-03-04 00:21:44 +08:00
|
|
|
Archive::Child C =
|
2016-03-04 06:24:39 +08:00
|
|
|
check(Sym->getMember(),
|
2016-03-15 05:31:07 +08:00
|
|
|
"could not get the member for symbol " + Sym->getName());
|
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
|
|
|
|
2016-03-04 06:24:39 +08:00
|
|
|
return check(C.getMemoryBufferRef(),
|
2016-03-15 05:31:07 +08:00
|
|
|
"could not get the buffer for the member defining symbol " +
|
2016-03-04 00:21:44 +08:00
|
|
|
Sym->getName());
|
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>
|
2016-03-15 07:16:09 +08:00
|
|
|
const typename ELFT::Shdr *
|
2015-11-03 22:13:40 +08:00
|
|
|
SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
|
|
|
|
uint32_t Index = this->getSectionIndex(Sym);
|
|
|
|
if (Index == 0)
|
|
|
|
return nullptr;
|
2016-03-04 06:24:39 +08:00
|
|
|
return check(this->ELFObj.getSection(Index));
|
2015-11-03 22:13:40 +08:00
|
|
|
}
|
|
|
|
|
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() {
|
2016-03-15 07:16:09 +08:00
|
|
|
typedef typename ELFT::Dyn Elf_Dyn;
|
|
|
|
typedef typename ELFT::uint uintX_t;
|
2015-10-01 23:47:50 +08:00
|
|
|
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;
|
2016-03-04 00:21:44 +08:00
|
|
|
case SHT_SYMTAB_SHNDX:
|
2016-03-04 06:24:39 +08:00
|
|
|
this->SymtabSHNDX = check(Obj.getSHNDXTable(Sec));
|
2015-11-03 22:13:40 +08:00
|
|
|
break;
|
2016-04-28 04:22:31 +08:00
|
|
|
case SHT_GNU_versym:
|
|
|
|
this->VersymSec = &Sec;
|
|
|
|
break;
|
|
|
|
case SHT_GNU_verdef:
|
|
|
|
this->VerdefSec = &Sec;
|
|
|
|
break;
|
2015-11-03 22:13:40 +08:00
|
|
|
}
|
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())
|
2016-03-12 16:31:34 +08:00
|
|
|
fatal("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-04-28 04:22:31 +08:00
|
|
|
// Parse the version definitions in the object file if present. Returns a vector
|
|
|
|
// whose nth element contains a pointer to the Elf_Verdef for version identifier
|
|
|
|
// n. Version identifiers that are not definitions map to nullptr. The array
|
|
|
|
// always has at least length 1.
|
|
|
|
template <class ELFT>
|
|
|
|
std::vector<const typename ELFT::Verdef *>
|
|
|
|
SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
|
|
|
|
std::vector<const Elf_Verdef *> Verdefs(1);
|
|
|
|
// We only need to process symbol versions for this DSO if it has both a
|
|
|
|
// versym and a verdef section, which indicates that the DSO contains symbol
|
|
|
|
// version definitions.
|
|
|
|
if (!VersymSec || !VerdefSec)
|
|
|
|
return Verdefs;
|
|
|
|
|
|
|
|
// The location of the first global versym entry.
|
|
|
|
Versym = reinterpret_cast<const Elf_Versym *>(this->ELFObj.base() +
|
|
|
|
VersymSec->sh_offset) +
|
|
|
|
this->Symtab->sh_info;
|
|
|
|
|
|
|
|
// We cannot determine the largest verdef identifier without inspecting
|
|
|
|
// every Elf_Verdef, but both bfd and gold assign verdef identifiers
|
|
|
|
// sequentially starting from 1, so we predict that the largest identifier
|
|
|
|
// will be VerdefCount.
|
|
|
|
unsigned VerdefCount = VerdefSec->sh_info;
|
|
|
|
Verdefs.resize(VerdefCount + 1);
|
|
|
|
|
|
|
|
// Build the Verdefs array by following the chain of Elf_Verdef objects
|
|
|
|
// from the start of the .gnu.version_d section.
|
|
|
|
const uint8_t *Verdef = this->ELFObj.base() + VerdefSec->sh_offset;
|
|
|
|
for (unsigned I = 0; I != VerdefCount; ++I) {
|
|
|
|
auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
|
|
|
|
Verdef += CurVerdef->vd_next;
|
|
|
|
unsigned VerdefIndex = CurVerdef->vd_ndx;
|
|
|
|
if (Verdefs.size() <= VerdefIndex)
|
|
|
|
Verdefs.resize(VerdefIndex + 1);
|
|
|
|
Verdefs[VerdefIndex] = CurVerdef;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Verdefs;
|
|
|
|
}
|
|
|
|
|
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() {
|
2016-04-28 04:22:31 +08:00
|
|
|
// Create mapping from version identifiers to Elf_Verdef entries.
|
|
|
|
const Elf_Versym *Versym = nullptr;
|
|
|
|
std::vector<const Elf_Verdef *> Verdefs = parseVerdefs(Versym);
|
|
|
|
|
2016-03-11 20:06:30 +08:00
|
|
|
Elf_Sym_Range Syms = this->getElfSymbols(true);
|
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) {
|
2016-04-30 00:23:31 +08:00
|
|
|
StringRef Name = check(Sym.getName(this->StringTable));
|
|
|
|
if (Sym.isUndefined()) {
|
|
|
|
Undefs.push_back(Name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-04-28 04:22:31 +08:00
|
|
|
unsigned VersymIndex = 0;
|
|
|
|
if (Versym) {
|
|
|
|
VersymIndex = Versym->vs_index;
|
|
|
|
++Versym;
|
|
|
|
// Ignore local symbols and non-default versions.
|
|
|
|
if (VersymIndex == 0 || (VersymIndex & VERSYM_HIDDEN))
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-30 00:23:31 +08:00
|
|
|
SymbolBodies.emplace_back(this, Name, Sym, Verdefs[VersymIndex]);
|
2015-09-08 23:50:05 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-04 04:03:54 +08:00
|
|
|
|
2016-02-13 04:54:57 +08:00
|
|
|
BitcodeFile::BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
|
|
|
|
|
|
|
|
bool BitcodeFile::classof(const InputFile *F) {
|
|
|
|
return F->kind() == BitcodeKind;
|
|
|
|
}
|
|
|
|
|
2016-03-07 08:54:17 +08:00
|
|
|
static uint8_t getGvVisibility(const GlobalValue *GV) {
|
|
|
|
switch (GV->getVisibility()) {
|
2016-03-08 03:06:14 +08:00
|
|
|
case GlobalValue::DefaultVisibility:
|
|
|
|
return STV_DEFAULT;
|
2016-03-07 08:54:17 +08:00
|
|
|
case GlobalValue::HiddenVisibility:
|
|
|
|
return STV_HIDDEN;
|
|
|
|
case GlobalValue::ProtectedVisibility:
|
|
|
|
return STV_PROTECTED;
|
|
|
|
}
|
2016-03-12 16:31:34 +08:00
|
|
|
llvm_unreachable("unknown visibility");
|
2016-03-12 02:46:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SymbolBody *
|
2016-04-23 02:26:33 +08:00
|
|
|
BitcodeFile::createBody(const DenseSet<const Comdat *> &KeptComdats,
|
2016-03-12 02:46:51 +08:00
|
|
|
const IRObjectFile &Obj,
|
2016-04-23 02:26:33 +08:00
|
|
|
const BasicSymbolRef &Sym,
|
|
|
|
const GlobalValue *GV) {
|
|
|
|
SmallString<64> Name;
|
|
|
|
raw_svector_ostream OS(Name);
|
|
|
|
Sym.printName(OS);
|
|
|
|
StringRef NameRef = Saver.save(StringRef(Name));
|
|
|
|
SymbolBody *Body;
|
2016-03-12 02:46:51 +08:00
|
|
|
|
2016-04-12 00:39:43 +08:00
|
|
|
uint32_t Flags = Sym.getFlags();
|
2016-04-23 02:26:33 +08:00
|
|
|
bool IsWeak = Flags & BasicSymbolRef::SF_Weak;
|
2016-04-27 08:05:06 +08:00
|
|
|
uint32_t Binding = IsWeak ? STB_WEAK : STB_GLOBAL;
|
2016-04-23 02:26:33 +08:00
|
|
|
|
2016-04-12 00:39:43 +08:00
|
|
|
uint8_t Visibility;
|
|
|
|
if (GV)
|
|
|
|
Visibility = getGvVisibility(GV);
|
|
|
|
else
|
|
|
|
// FIXME: Set SF_Hidden flag correctly for module asm symbols, and expose
|
|
|
|
// protected visibility.
|
|
|
|
Visibility = STV_DEFAULT;
|
2016-03-12 02:46:51 +08:00
|
|
|
|
2016-04-23 02:26:33 +08:00
|
|
|
if (GV)
|
|
|
|
if (const Comdat *C = GV->getComdat())
|
|
|
|
if (!KeptComdats.count(C)) {
|
2016-04-27 08:05:06 +08:00
|
|
|
Body = new (Alloc) Undefined(NameRef, Binding, Visibility, /*Type*/ 0,
|
2016-04-28 08:26:54 +08:00
|
|
|
/*IsBitcode*/ true);
|
2016-04-23 02:26:33 +08:00
|
|
|
return Body;
|
|
|
|
}
|
2016-03-12 02:46:51 +08:00
|
|
|
|
|
|
|
const Module &M = Obj.getModule();
|
2016-04-23 02:26:33 +08:00
|
|
|
if (Flags & BasicSymbolRef::SF_Undefined)
|
2016-04-27 08:05:06 +08:00
|
|
|
return new (Alloc) Undefined(NameRef, Binding, Visibility, /*Type*/ 0,
|
2016-04-28 08:26:54 +08:00
|
|
|
/*IsBitcode*/ true);
|
2016-04-23 02:26:33 +08:00
|
|
|
if (Flags & BasicSymbolRef::SF_Common) {
|
2016-04-12 00:39:43 +08:00
|
|
|
// FIXME: Set SF_Common flag correctly for module asm symbols, and expose
|
|
|
|
// size and alignment.
|
|
|
|
assert(GV);
|
2016-03-12 02:46:51 +08:00
|
|
|
const DataLayout &DL = M.getDataLayout();
|
|
|
|
uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
|
2016-04-27 08:05:06 +08:00
|
|
|
return new (Alloc) DefinedCommon(NameRef, Size, GV->getAlignment(), Binding,
|
|
|
|
Visibility, /*Type*/ 0);
|
2016-03-12 02:46:51 +08:00
|
|
|
}
|
2016-04-23 02:26:33 +08:00
|
|
|
return new (Alloc) DefinedBitcode(NameRef, IsWeak, Visibility);
|
|
|
|
}
|
|
|
|
|
|
|
|
SymbolBody *
|
|
|
|
BitcodeFile::createSymbolBody(const DenseSet<const Comdat *> &KeptComdats,
|
|
|
|
const IRObjectFile &Obj,
|
|
|
|
const BasicSymbolRef &Sym) {
|
|
|
|
const GlobalValue *GV = Obj.getSymbolGV(Sym.getRawDataRefImpl());
|
|
|
|
SymbolBody *Body = createBody(KeptComdats, Obj, Sym, GV);
|
|
|
|
|
2016-04-12 00:39:43 +08:00
|
|
|
// FIXME: Expose a thread-local flag for module asm symbols.
|
2016-04-22 05:44:25 +08:00
|
|
|
if (GV) {
|
|
|
|
if (GV->isThreadLocal())
|
|
|
|
Body->Type = STT_TLS;
|
|
|
|
Body->CanOmitFromDynSym = canBeOmittedFromSymbolTable(GV);
|
|
|
|
}
|
2016-03-12 02:46:51 +08:00
|
|
|
return Body;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BitcodeFile::shouldSkip(const BasicSymbolRef &Sym) {
|
|
|
|
uint32_t Flags = Sym.getFlags();
|
|
|
|
if (!(Flags & BasicSymbolRef::SF_Global))
|
|
|
|
return true;
|
|
|
|
if (Flags & BasicSymbolRef::SF_FormatSpecific)
|
|
|
|
return true;
|
|
|
|
return false;
|
2016-03-12 00:11:47 +08:00
|
|
|
}
|
|
|
|
|
2016-03-02 23:43:50 +08:00
|
|
|
void BitcodeFile::parse(DenseSet<StringRef> &ComdatGroups) {
|
2016-04-29 03:30:41 +08:00
|
|
|
Obj = check(IRObjectFile::create(MB, Driver->Context));
|
2016-03-04 00:21:44 +08:00
|
|
|
const Module &M = Obj->getModule();
|
2016-03-02 23:43:50 +08:00
|
|
|
|
|
|
|
DenseSet<const Comdat *> KeptComdats;
|
|
|
|
for (const auto &P : M.getComdatSymbolTable()) {
|
|
|
|
StringRef N = Saver.save(P.first());
|
|
|
|
if (ComdatGroups.insert(N).second)
|
|
|
|
KeptComdats.insert(&P.second);
|
|
|
|
}
|
|
|
|
|
2016-03-12 02:46:51 +08:00
|
|
|
for (const BasicSymbolRef &Sym : Obj->symbols())
|
|
|
|
if (!shouldSkip(Sym))
|
|
|
|
SymbolBodies.push_back(createSymbolBody(KeptComdats, *Obj, Sym));
|
2016-02-13 04:54:57 +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();
|
2016-04-29 18:39:17 +08:00
|
|
|
if (Config->EMachine == EM_MIPS && Config->EKind == ELF64LEKind)
|
|
|
|
Config->Mips64EL = true;
|
2015-10-12 23:31:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2016-04-08 08:18:25 +08:00
|
|
|
unsigned char Size;
|
|
|
|
unsigned char Endian;
|
|
|
|
std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
|
|
|
|
if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
|
2016-03-12 16:31:34 +08:00
|
|
|
fatal("invalid data encoding: " + MB.getBufferIdentifier());
|
2015-10-12 23:31:09 +08:00
|
|
|
|
2016-04-08 08:18:25 +08:00
|
|
|
if (Size == ELFCLASS32) {
|
|
|
|
if (Endian == 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
|
|
|
}
|
2016-04-08 08:18:25 +08:00
|
|
|
if (Size == ELFCLASS64) {
|
|
|
|
if (Endian == 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
|
|
|
}
|
2016-03-12 16:31:34 +08:00
|
|
|
fatal("invalid file class: " + MB.getBufferIdentifier());
|
2015-10-12 23:31:09 +08:00
|
|
|
}
|
|
|
|
|
2016-04-08 08:14:55 +08:00
|
|
|
static bool isBitcode(MemoryBufferRef MB) {
|
|
|
|
using namespace sys::fs;
|
|
|
|
return identify_magic(MB.getBuffer()) == file_magic::bitcode;
|
|
|
|
}
|
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
std::unique_ptr<InputFile> elf::createObjectFile(MemoryBufferRef MB,
|
|
|
|
StringRef ArchiveName) {
|
2016-02-24 02:17:11 +08:00
|
|
|
std::unique_ptr<InputFile> F;
|
2016-04-08 08:14:55 +08:00
|
|
|
if (isBitcode(MB))
|
2016-02-24 02:17:11 +08:00
|
|
|
F.reset(new BitcodeFile(MB));
|
|
|
|
else
|
|
|
|
F = createELFFile<ObjectFile>(MB);
|
2016-02-02 16:22:41 +08:00
|
|
|
F->ArchiveName = ArchiveName;
|
|
|
|
return F;
|
2016-01-06 08:09:43 +08:00
|
|
|
}
|
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
std::unique_ptr<InputFile> elf::createSharedFile(MemoryBufferRef MB) {
|
2016-01-06 08:09:43 +08:00
|
|
|
return createELFFile<SharedFile>(MB);
|
|
|
|
}
|
|
|
|
|
2016-04-08 03:24:51 +08:00
|
|
|
void LazyObjectFile::parse() {
|
|
|
|
for (StringRef Sym : getSymbols())
|
|
|
|
LazySymbols.emplace_back(Sym, this->MB);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> std::vector<StringRef> LazyObjectFile::getElfSymbols() {
|
|
|
|
typedef typename ELFT::Shdr Elf_Shdr;
|
|
|
|
typedef typename ELFT::Sym Elf_Sym;
|
|
|
|
typedef typename ELFT::SymRange Elf_Sym_Range;
|
|
|
|
|
|
|
|
const ELFFile<ELFT> Obj = createELFObj<ELFT>(this->MB);
|
|
|
|
for (const Elf_Shdr &Sec : Obj.sections()) {
|
|
|
|
if (Sec.sh_type != SHT_SYMTAB)
|
|
|
|
continue;
|
|
|
|
Elf_Sym_Range Syms = Obj.symbols(&Sec);
|
|
|
|
uint32_t FirstNonLocal = Sec.sh_info;
|
|
|
|
StringRef StringTable = check(Obj.getStringTableForSymtab(Sec));
|
|
|
|
std::vector<StringRef> V;
|
|
|
|
for (const Elf_Sym &Sym : Syms.slice(FirstNonLocal))
|
2016-04-09 04:49:31 +08:00
|
|
|
if (Sym.st_shndx != SHN_UNDEF)
|
|
|
|
V.push_back(check(Sym.getName(StringTable)));
|
2016-04-08 03:24:51 +08:00
|
|
|
return V;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<StringRef> LazyObjectFile::getBitcodeSymbols() {
|
|
|
|
LLVMContext Context;
|
|
|
|
std::unique_ptr<IRObjectFile> Obj =
|
|
|
|
check(IRObjectFile::create(this->MB, Context));
|
|
|
|
std::vector<StringRef> V;
|
|
|
|
for (const BasicSymbolRef &Sym : Obj->symbols()) {
|
|
|
|
if (BitcodeFile::shouldSkip(Sym))
|
|
|
|
continue;
|
2016-04-09 05:38:22 +08:00
|
|
|
if (Sym.getFlags() & BasicSymbolRef::SF_Undefined)
|
2016-04-09 04:49:31 +08:00
|
|
|
continue;
|
2016-04-08 03:24:51 +08:00
|
|
|
SmallString<64> Name;
|
|
|
|
raw_svector_ostream OS(Name);
|
|
|
|
Sym.printName(OS);
|
|
|
|
V.push_back(Saver.save(StringRef(Name)));
|
|
|
|
}
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2016-04-09 04:49:31 +08:00
|
|
|
// Returns a vector of globally-visible defined symbol names.
|
2016-04-08 03:24:51 +08:00
|
|
|
std::vector<StringRef> LazyObjectFile::getSymbols() {
|
2016-04-08 08:14:55 +08:00
|
|
|
if (isBitcode(this->MB))
|
2016-04-08 03:24:51 +08:00
|
|
|
return getBitcodeSymbols();
|
|
|
|
|
2016-04-08 08:14:55 +08:00
|
|
|
unsigned char Size;
|
|
|
|
unsigned char Endian;
|
|
|
|
std::tie(Size, Endian) = getElfArchType(this->MB.getBuffer());
|
|
|
|
if (Size == ELFCLASS32) {
|
|
|
|
if (Endian == ELFDATA2LSB)
|
2016-04-08 03:24:51 +08:00
|
|
|
return getElfSymbols<ELF32LE>();
|
|
|
|
return getElfSymbols<ELF32BE>();
|
|
|
|
}
|
2016-04-08 08:14:55 +08:00
|
|
|
if (Endian == ELFDATA2LSB)
|
2016-04-08 03:24:51 +08:00
|
|
|
return getElfSymbols<ELF64LE>();
|
|
|
|
return getElfSymbols<ELF64BE>();
|
|
|
|
}
|
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
template class elf::ELFFileBase<ELF32LE>;
|
|
|
|
template class elf::ELFFileBase<ELF32BE>;
|
|
|
|
template class elf::ELFFileBase<ELF64LE>;
|
|
|
|
template class elf::ELFFileBase<ELF64BE>;
|
2015-11-20 10:19:36 +08:00
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
template class elf::ObjectFile<ELF32LE>;
|
|
|
|
template class elf::ObjectFile<ELF32BE>;
|
|
|
|
template class elf::ObjectFile<ELF64LE>;
|
|
|
|
template class elf::ObjectFile<ELF64BE>;
|
2015-11-20 10:19:36 +08:00
|
|
|
|
2016-02-28 08:25:54 +08:00
|
|
|
template class elf::SharedFile<ELF32LE>;
|
|
|
|
template class elf::SharedFile<ELF32BE>;
|
|
|
|
template class elf::SharedFile<ELF64LE>;
|
|
|
|
template class elf::SharedFile<ELF64BE>;
|