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"
|
|
|
|
#include "Chunks.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"
|
|
|
|
|
|
|
|
using namespace llvm::ELF;
|
|
|
|
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf2;
|
|
|
|
|
2015-08-05 20:03:34 +08:00
|
|
|
template <class ELFT>
|
|
|
|
bool ObjectFile<ELFT>::isCompatibleWith(const ObjectFileBase &Other) const {
|
|
|
|
if (kind() != Other.kind())
|
|
|
|
return false;
|
|
|
|
return getObj()->getHeader()->e_machine ==
|
|
|
|
cast<ObjectFile<ELFT>>(Other).getObj()->getHeader()->e_machine;
|
|
|
|
}
|
|
|
|
|
2015-07-25 05:03:07 +08:00
|
|
|
template <class ELFT> void elf2::ObjectFile<ELFT>::parse() {
|
|
|
|
// Parse a memory buffer as a ELF file.
|
|
|
|
std::error_code EC;
|
|
|
|
ELFObj = llvm::make_unique<ELFFile<ELFT>>(MB.getBuffer(), EC);
|
|
|
|
error(EC);
|
|
|
|
|
|
|
|
// Read section and symbol tables.
|
|
|
|
initializeChunks();
|
|
|
|
initializeSymbols();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> void elf2::ObjectFile<ELFT>::initializeChunks() {
|
|
|
|
uint64_t Size = ELFObj->getNumSections();
|
2015-08-25 04:06:32 +08:00
|
|
|
Chunks.resize(Size);
|
|
|
|
unsigned I = 0;
|
2015-07-25 05:03:07 +08:00
|
|
|
for (const Elf_Shdr &Sec : ELFObj->sections()) {
|
2015-08-13 22:45:44 +08:00
|
|
|
switch (Sec.sh_type) {
|
|
|
|
case SHT_SYMTAB:
|
2015-08-10 23:12:17 +08:00
|
|
|
Symtab = &Sec;
|
2015-08-13 22:45:44 +08:00
|
|
|
break;
|
2015-08-25 05:43:25 +08:00
|
|
|
case SHT_SYMTAB_SHNDX: {
|
|
|
|
ErrorOr<ArrayRef<Elf_Word>> ErrorOrTable = ELFObj->getSHNDXTable(Sec);
|
|
|
|
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");
|
|
|
|
SectionChunk<ELFT> *RelocatedSection = Chunks[RelocatedSectionIndex];
|
|
|
|
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-08-28 07:15:56 +08:00
|
|
|
Chunks[I] = new (Alloc) SectionChunk<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() {
|
|
|
|
ErrorOr<StringRef> StringTableOrErr =
|
|
|
|
ELFObj->getStringTableForSymtab(*Symtab);
|
|
|
|
error(StringTableOrErr.getError());
|
|
|
|
StringRef StringTable = *StringTableOrErr;
|
|
|
|
|
2015-08-08 01:16:28 +08:00
|
|
|
Elf_Sym_Range Syms = ELFObj->symbols(Symtab);
|
2015-08-12 04:06:51 +08:00
|
|
|
uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
|
2015-08-12 00:30:34 +08:00
|
|
|
uint32_t FirstNonLocal = Symtab->sh_info;
|
|
|
|
if (FirstNonLocal > NumSymbols)
|
|
|
|
error("Invalid sh_info in symbol table");
|
|
|
|
Syms = llvm::make_range(Syms.begin() + FirstNonLocal, Syms.end());
|
2015-07-25 05:03:07 +08:00
|
|
|
SymbolBodies.reserve(NumSymbols);
|
2015-08-04 22:00:56 +08:00
|
|
|
for (const Elf_Sym &Sym : Syms)
|
|
|
|
SymbolBodies.push_back(createSymbolBody(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-27 20:40:06 +08:00
|
|
|
if (SecIndex == SHN_ABS)
|
|
|
|
return new (Alloc) DefinedAbsolute<ELFT>(Name, *Sym);
|
|
|
|
|
2015-08-25 05:43:25 +08:00
|
|
|
if (SecIndex == SHN_XINDEX)
|
|
|
|
SecIndex = ELFObj->getExtendedSymbolTableIndex(Sym, Symtab, SymtabSHNDX);
|
|
|
|
|
2015-08-25 06:00:25 +08:00
|
|
|
if (SecIndex >= Chunks.size() ||
|
|
|
|
(SecIndex != 0 && !Chunks[SecIndex]))
|
|
|
|
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-08-12 01:33:02 +08:00
|
|
|
if (Sym->isUndefined())
|
2015-08-15 00:46:28 +08:00
|
|
|
return new (Alloc) Undefined<ELFT>(Name, *Sym);
|
2015-08-25 04:06:32 +08:00
|
|
|
return new (Alloc) DefinedRegular<ELFT>(Name, *Sym, *Chunks[SecIndex]);
|
2015-08-12 01:33:02 +08:00
|
|
|
}
|
2015-07-25 05:03:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace elf2 {
|
|
|
|
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>;
|
|
|
|
}
|
|
|
|
}
|