blob: 2b117140749b4fbe690ebf5afb68d01206ef8e2b [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- InputFiles.cpp -----------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "InputFiles.h"
11#include "Chunks.h"
12#include "Symbols.h"
13#include "Driver.h"
14#include "llvm/ADT/STLExtras.h"
15
16using namespace llvm::ELF;
17
18using namespace lld;
19using namespace lld::elf2;
20
Rafael Espindola3c9cb4b2015-08-05 12:03:34 +000021template <class ELFT>
22bool ObjectFile<ELFT>::isCompatibleWith(const ObjectFileBase &Other) const {
23 if (kind() != Other.kind())
24 return false;
25 return getObj()->getHeader()->e_machine ==
26 cast<ObjectFile<ELFT>>(Other).getObj()->getHeader()->e_machine;
27}
28
Michael J. Spencer84487f12015-07-24 21:03:07 +000029template <class ELFT> void elf2::ObjectFile<ELFT>::parse() {
30 // Parse a memory buffer as a ELF file.
31 std::error_code EC;
32 ELFObj = llvm::make_unique<ELFFile<ELFT>>(MB.getBuffer(), EC);
33 error(EC);
34
35 // Read section and symbol tables.
36 initializeChunks();
37 initializeSymbols();
38}
39
40template <class ELFT> void elf2::ObjectFile<ELFT>::initializeChunks() {
41 uint64_t Size = ELFObj->getNumSections();
42 Chunks.reserve(Size);
43 for (const Elf_Shdr &Sec : ELFObj->sections()) {
44 if (Sec.sh_flags & SHF_ALLOC) {
45 auto *C = new (Alloc) SectionChunk<ELFT>(this->getObj(), &Sec);
46 Chunks.push_back(C);
47 }
48 }
49}
50
51template <class ELFT> void elf2::ObjectFile<ELFT>::initializeSymbols() {
52 const Elf_Shdr *Symtab = ELFObj->getDotSymtabSec();
53 ErrorOr<StringRef> StringTableOrErr =
54 ELFObj->getStringTableForSymtab(*Symtab);
55 error(StringTableOrErr.getError());
56 StringRef StringTable = *StringTableOrErr;
57
58 Elf_Sym_Range Syms = ELFObj->symbols();
59 Syms = Elf_Sym_Range(Syms.begin() + 1, Syms.end());
60 auto NumSymbols = std::distance(Syms.begin(), Syms.end());
61 SymbolBodies.reserve(NumSymbols);
Rafael Espindola30318512015-08-04 14:00:56 +000062 for (const Elf_Sym &Sym : Syms)
63 SymbolBodies.push_back(createSymbolBody(StringTable, &Sym));
Michael J. Spencer84487f12015-07-24 21:03:07 +000064}
65
66template <class ELFT>
67SymbolBody *elf2::ObjectFile<ELFT>::createSymbolBody(StringRef StringTable,
68 const Elf_Sym *Sym) {
69 ErrorOr<StringRef> NameOrErr = Sym->getName(StringTable);
70 error(NameOrErr.getError());
71 StringRef Name = *NameOrErr;
72 if (Sym->isUndefined())
73 return new (Alloc) Undefined(Name);
Michael J. Spencercdae0a42015-07-28 22:58:25 +000074 return new (Alloc) DefinedRegular<ELFT>(this, Sym);
Michael J. Spencer84487f12015-07-24 21:03:07 +000075}
76
77namespace lld {
78namespace elf2 {
79template class elf2::ObjectFile<llvm::object::ELF32LE>;
80template class elf2::ObjectFile<llvm::object::ELF32BE>;
81template class elf2::ObjectFile<llvm::object::ELF64LE>;
82template class elf2::ObjectFile<llvm::object::ELF64BE>;
83}
84}