blob: fba6076187fa3e7674603f48907ffc1c924ba638 [file] [log] [blame]
Michael J. Spencer6344b322012-12-20 00:37:10 +00001//===- lib/ReaderWriter/ELF/ReaderELF.cpp ---------------------------------===//
Nick Kledzikabb69812012-05-31 22:34:00 +00002//
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//===----------------------------------------------------------------------===//
Michael J. Spencer6344b322012-12-20 00:37:10 +00009///
10/// \file
11/// \brief Defines the ELF Reader and all helper sub classes to consume an ELF
12/// file and produces atoms out of it.
13///
Sid Manning1a601412012-07-25 16:27:21 +000014//===----------------------------------------------------------------------===//
Michael J. Spencer6344b322012-12-20 00:37:10 +000015
Nick Kledzikabb69812012-05-31 22:34:00 +000016#include "lld/ReaderWriter/ReaderELF.h"
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +000017#include "lld/ReaderWriter/ReaderArchive.h"
Michael J. Spencer842885e2013-01-15 21:12:45 +000018#include "lld/Core/ErrorOr.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000019#include "lld/Core/File.h"
Sid Manning8caf4de2012-09-17 12:49:38 +000020#include "lld/Core/Reference.h"
Michael J. Spencer6344b322012-12-20 00:37:10 +000021
Sid Manning1a601412012-07-25 16:27:21 +000022#include "llvm/ADT/ArrayRef.h"
Sid Manning8caf4de2012-09-17 12:49:38 +000023#include "llvm/ADT/SmallString.h"
Sid Manning1a601412012-07-25 16:27:21 +000024#include "llvm/ADT/StringRef.h"
25#include "llvm/Object/ELF.h"
26#include "llvm/Object/ObjectFile.h"
27#include "llvm/Support/Allocator.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000028#include "llvm/Support/Casting.h"
Shankar Easwaranbbf9ddd2012-12-27 01:40:08 +000029#include "llvm/Support/Path.h"
Sid Manning1a601412012-07-25 16:27:21 +000030#include "llvm/Support/ELF.h"
31#include "llvm/Support/Endian.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000032#include "llvm/Support/ErrorHandling.h"
Sid Manning1a601412012-07-25 16:27:21 +000033#include "llvm/Support/MathExtras.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000034#include "llvm/Support/Memory.h"
35#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer6344b322012-12-20 00:37:10 +000036#include "llvm/Support/Path.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000037#include "llvm/Support/raw_ostream.h"
38#include "llvm/Support/system_error.h"
Shankar Easwaranbbf9ddd2012-12-27 01:40:08 +000039#include "AtomsELF.h"
Sid Manning1a601412012-07-25 16:27:21 +000040
Nick Kledzikabb69812012-05-31 22:34:00 +000041#include <map>
42#include <vector>
43
Sid Manning1a601412012-07-25 16:27:21 +000044using namespace lld;
Michael J. Spencer6344b322012-12-20 00:37:10 +000045using llvm::support::endianness;
Michael J. Spencera2c97272013-01-04 21:09:21 +000046using namespace llvm::object;
Sid Manning1a601412012-07-25 16:27:21 +000047
Michael J. Spencer6344b322012-12-20 00:37:10 +000048namespace {
Michael J. Spencer84d3b012013-01-16 23:34:32 +000049/// \brief Read a binary, find out based on the symbol table contents what kind
50/// of symbol it is and create corresponding atoms for it
51template <class ELFT> class FileELF : public File {
Michael J. Spencerb03f6c42013-01-15 07:53:22 +000052 typedef Elf_Sym_Impl<ELFT> Elf_Sym;
53 typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
54 typedef Elf_Rel_Impl<ELFT, false> Elf_Rel;
55 typedef Elf_Rel_Impl<ELFT, true> Elf_Rela;
Sid Manning1a601412012-07-25 16:27:21 +000056
57public:
Michael J. Spencer6344b322012-12-20 00:37:10 +000058 FileELF(std::unique_ptr<llvm::MemoryBuffer> MB, llvm::error_code &EC)
Michael J. Spencer84d3b012013-01-16 23:34:32 +000059 : File(MB->getBufferIdentifier()) {
Michael J. Spencera2c97272013-01-04 21:09:21 +000060 llvm::OwningPtr<Binary> binaryFile;
61 EC = createBinary(MB.release(), binaryFile);
Sid Manning1a601412012-07-25 16:27:21 +000062 if (EC)
63 return;
64
65 // Point Obj to correct class and bitwidth ELF object
Michael J. Spencerb03f6c42013-01-15 07:53:22 +000066 _objFile.reset(llvm::dyn_cast<ELFObjectFile<ELFT>>(binaryFile.get()));
Sid Manning1a601412012-07-25 16:27:21 +000067
Sid Manning8caf4de2012-09-17 12:49:38 +000068 if (!_objFile) {
Michael J. Spencera2c97272013-01-04 21:09:21 +000069 EC = make_error_code(object_error::invalid_file_type);
Sid Manning1a601412012-07-25 16:27:21 +000070 return;
71 }
72
Sid Manning8caf4de2012-09-17 12:49:38 +000073 binaryFile.take();
Sid Manning1a601412012-07-25 16:27:21 +000074
Michael J. Spencer84d3b012013-01-16 23:34:32 +000075 std::map<const Elf_Shdr *, std::vector<const Elf_Sym *>> sectionSymbols;
Sid Manning1a601412012-07-25 16:27:21 +000076
Michael J. Spencer6344b322012-12-20 00:37:10 +000077 // Handle: SHT_REL and SHT_RELA sections:
78 // Increment over the sections, when REL/RELA section types are found add
79 // the contents to the RelocationReferences map.
Michael J. Spencera2c97272013-01-04 21:09:21 +000080 section_iterator sit(_objFile->begin_sections());
81 section_iterator sie(_objFile->end_sections());
Sid Manning8caf4de2012-09-17 12:49:38 +000082 for (; sit != sie; sit.increment(EC)) {
83 if (EC)
84 return;
85
86 const Elf_Shdr *section = _objFile->getElfSection(sit);
87
88 if (section->sh_type == llvm::ELF::SHT_RELA) {
89 llvm::StringRef sectionName;
90 if ((EC = _objFile->getSectionName(section, sectionName)))
91 return;
92 // Get rid of the leading .rela so Atoms can use their own section
93 // name to find the relocs.
94 sectionName = sectionName.drop_front(5);
95
96 auto rai(_objFile->beginELFRela(section));
97 auto rae(_objFile->endELFRela(section));
98
99 auto &Ref = _relocationAddendRefences[sectionName];
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000100 for (; rai != rae; ++rai) {
Sid Manning8caf4de2012-09-17 12:49:38 +0000101 Ref.push_back(&*rai);
102 }
103 }
104
105 if (section->sh_type == llvm::ELF::SHT_REL) {
106 llvm::StringRef sectionName;
107 if ((EC = _objFile->getSectionName(section, sectionName)))
108 return;
109 // Get rid of the leading .rel so Atoms can use their own section
110 // name to find the relocs.
111 sectionName = sectionName.drop_front(4);
112
113 auto ri(_objFile->beginELFRel(section));
114 auto re(_objFile->endELFRel(section));
115
116 auto &Ref = _relocationReferences[sectionName];
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000117 for (; ri != re; ++ri) {
Sid Manning8caf4de2012-09-17 12:49:38 +0000118 Ref.push_back(&*ri);
119 }
120 }
121 }
122
Michael J. Spencer6344b322012-12-20 00:37:10 +0000123 // Increment over all the symbols collecting atoms and symbol names for
124 // later use.
Michael J. Spencera2c97272013-01-04 21:09:21 +0000125 symbol_iterator it(_objFile->begin_symbols());
126 symbol_iterator ie(_objFile->end_symbols());
Sid Manning1a601412012-07-25 16:27:21 +0000127
Sid Manning429a4bc2012-07-27 14:52:18 +0000128 for (; it != ie; it.increment(EC)) {
Sid Manning1a601412012-07-25 16:27:21 +0000129 if (EC)
Sid Manning429a4bc2012-07-27 14:52:18 +0000130 return;
Sid Manning1a601412012-07-25 16:27:21 +0000131
Sid Manning8caf4de2012-09-17 12:49:38 +0000132 if ((EC = it->getSection(sit)))
Sid Manning429a4bc2012-07-27 14:52:18 +0000133 return;
Sid Manning1a601412012-07-25 16:27:21 +0000134
Sid Manning8caf4de2012-09-17 12:49:38 +0000135 const Elf_Shdr *section = _objFile->getElfSection(sit);
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000136 const Elf_Sym *symbol = _objFile->getElfSymbol(it);
Sid Manning1a601412012-07-25 16:27:21 +0000137
Sid Manning8caf4de2012-09-17 12:49:38 +0000138 llvm::StringRef symbolName;
139 if ((EC = _objFile->getSymbolName(section, symbol, symbolName)))
Sid Manning429a4bc2012-07-27 14:52:18 +0000140 return;
Sid Manning1a601412012-07-25 16:27:21 +0000141
Sid Manning8caf4de2012-09-17 12:49:38 +0000142 if (symbol->st_shndx == llvm::ELF::SHN_ABS) {
Sid Manning1a601412012-07-25 16:27:21 +0000143 // Create an absolute atom.
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000144 auto *newAtom = new (_readerStorage.Allocate<ELFAbsoluteAtom<ELFT>>())
145 ELFAbsoluteAtom<ELFT>(*this, symbolName, symbol,
146 symbol->st_value);
Sid Manning1a601412012-07-25 16:27:21 +0000147
Sid Manning8caf4de2012-09-17 12:49:38 +0000148 _absoluteAtoms._atoms.push_back(newAtom);
149 _symbolToAtomMapping.insert(std::make_pair(symbol, newAtom));
Sid Manning8caf4de2012-09-17 12:49:38 +0000150 } else if (symbol->st_shndx == llvm::ELF::SHN_UNDEF) {
Sid Manning1a601412012-07-25 16:27:21 +0000151 // Create an undefined atom.
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000152 auto *newAtom = new (_readerStorage.Allocate<ELFUndefinedAtom<ELFT>>())
153 ELFUndefinedAtom<ELFT>(*this, symbolName, symbol);
Sid Manning8caf4de2012-09-17 12:49:38 +0000154
155 _undefinedAtoms._atoms.push_back(newAtom);
156 _symbolToAtomMapping.insert(std::make_pair(symbol, newAtom));
Sid Manning1a601412012-07-25 16:27:21 +0000157 } else {
158 // This is actually a defined symbol. Add it to its section's list of
159 // symbols.
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000160 if (symbol->getType() == llvm::ELF::STT_NOTYPE ||
161 symbol->getType() == llvm::ELF::STT_OBJECT ||
162 symbol->getType() == llvm::ELF::STT_FUNC ||
163 symbol->getType() == llvm::ELF::STT_GNU_IFUNC ||
164 symbol->getType() == llvm::ELF::STT_SECTION ||
165 symbol->getType() == llvm::ELF::STT_FILE ||
166 symbol->getType() == llvm::ELF::STT_TLS ||
167 symbol->getType() == llvm::ELF::STT_COMMON ||
168 symbol->st_shndx == llvm::ELF::SHN_COMMON) {
Sid Manning8caf4de2012-09-17 12:49:38 +0000169 sectionSymbols[section].push_back(symbol);
Michael J. Spencer6344b322012-12-20 00:37:10 +0000170 } else {
Sid Manning8caf4de2012-09-17 12:49:38 +0000171 llvm::errs() << "Unable to create atom for: " << symbolName << "\n";
Michael J. Spencera2c97272013-01-04 21:09:21 +0000172 EC = object_error::parse_failed;
Sid Manning1a601412012-07-25 16:27:21 +0000173 return;
174 }
175 }
176 }
177
Sid Manning8caf4de2012-09-17 12:49:38 +0000178 for (auto &i : sectionSymbols) {
179 auto &symbols = i.second;
Sid Manning1a601412012-07-25 16:27:21 +0000180 // Sort symbols by position.
Sid Manning8caf4de2012-09-17 12:49:38 +0000181 std::stable_sort(symbols.begin(), symbols.end(),
Michael J. Spencer6344b322012-12-20 00:37:10 +0000182 [](const Elf_Sym *A, const Elf_Sym *B) {
Sid Manning1a601412012-07-25 16:27:21 +0000183 return A->st_value < B->st_value;
Michael J. Spencer6344b322012-12-20 00:37:10 +0000184 });
Sid Manning1a601412012-07-25 16:27:21 +0000185
Michael J. Spencer842885e2013-01-15 21:12:45 +0000186 StringRef sectionContents;
187 if ((EC = _objFile->getSectionContents(i.first, sectionContents)))
188 return;
189
190 llvm::StringRef sectionName;
191 if ((EC = _objFile->getSectionName(i.first, sectionName)))
192 return;
193
Sid Manning1a601412012-07-25 16:27:21 +0000194 // i.first is the section the symbol lives in
Sid Manning8caf4de2012-09-17 12:49:38 +0000195 for (auto si = symbols.begin(), se = symbols.end(); si != se; ++si) {
Michael J. Spencer842885e2013-01-15 21:12:45 +0000196 llvm::StringRef symbolName;
Sid Manning8caf4de2012-09-17 12:49:38 +0000197 if ((EC = _objFile->getSymbolName(i.first, *si, symbolName)))
Sid Manning429a4bc2012-07-27 14:52:18 +0000198 return;
Sid Manning1a601412012-07-25 16:27:21 +0000199
Michael J. Spencer842885e2013-01-15 21:12:45 +0000200 bool isCommon = (*si)->getType() == llvm::ELF::STT_COMMON ||
201 (*si)->st_shndx == llvm::ELF::SHN_COMMON;
Sid Manning1a601412012-07-25 16:27:21 +0000202
203 // Get the symbol's content:
Sid Manning8caf4de2012-09-17 12:49:38 +0000204 uint64_t contentSize;
Sid Manning1a601412012-07-25 16:27:21 +0000205 if (si + 1 == se) {
206 // if this is the last symbol, take up the remaining data.
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000207 contentSize = isCommon ? 0
208 : i.first->sh_size - (*si)->st_value;
Michael J. Spencer842885e2013-01-15 21:12:45 +0000209 } else {
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000210 contentSize = isCommon ? 0
211 : (*(si + 1))->st_value - (*si)->st_value;
Sid Manning1a601412012-07-25 16:27:21 +0000212 }
213
Michael J. Spencer8b3898a2013-01-15 21:13:02 +0000214 // Don't allocate content to a weak symbol, as they may be merged away.
215 // Create an anonymous atom to hold the data.
216 ELFDefinedAtom<ELFT> *anonAtom = nullptr;
217 if ((*si)->getBinding() == llvm::ELF::STB_WEAK && contentSize != 0) {
218 // Create a new non-weak ELF symbol.
219 auto sym = new (_readerStorage.Allocate<Elf_Sym>()) Elf_Sym;
220 *sym = **si;
221 sym->setBinding(llvm::ELF::STB_GLOBAL);
222 anonAtom = createDefinedAtomAndAssignRelocations(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000223 "", sectionName, sym, i.first,
224 ArrayRef<uint8_t>((uint8_t *)sectionContents.data() +
225 (*si)->st_value, contentSize));
Michael J. Spencer8b3898a2013-01-15 21:13:02 +0000226 contentSize = 0;
227 }
228
Michael J. Spencer842885e2013-01-15 21:12:45 +0000229 ArrayRef<uint8_t> symbolData = ArrayRef<uint8_t>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000230 (uint8_t *)sectionContents.data() +
231 (*si)->st_value, contentSize);
Sid Manning8caf4de2012-09-17 12:49:38 +0000232
Michael J. Spencer842885e2013-01-15 21:12:45 +0000233 auto newAtom = createDefinedAtomAndAssignRelocations(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000234 symbolName, sectionName, *si, i.first, symbolData);
Sid Manning8caf4de2012-09-17 12:49:38 +0000235
236 _definedAtoms._atoms.push_back(newAtom);
237 _symbolToAtomMapping.insert(std::make_pair((*si), newAtom));
Michael J. Spencer8b3898a2013-01-15 21:13:02 +0000238 if (anonAtom)
239 _definedAtoms._atoms.push_back(anonAtom);
Sid Manning1a601412012-07-25 16:27:21 +0000240 }
241 }
Sid Manning8caf4de2012-09-17 12:49:38 +0000242
Michael J. Spencer6344b322012-12-20 00:37:10 +0000243 // All the Atoms and References are created. Now update each Reference's
244 // target with the Atom pointer it refers to.
Sid Manning8caf4de2012-09-17 12:49:38 +0000245 for (auto &ri : _references) {
Michael J. Spencer842885e2013-01-15 21:12:45 +0000246 const Elf_Sym *Symbol = _objFile->getElfSymbol(ri->targetSymbolIndex());
247 ri->setTarget(findAtom(Symbol));
Sid Manning8caf4de2012-09-17 12:49:38 +0000248 }
Sid Manning1a601412012-07-25 16:27:21 +0000249 }
250
Sid Manning1a601412012-07-25 16:27:21 +0000251 virtual const atom_collection<DefinedAtom> &defined() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000252 return _definedAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000253 }
254
255 virtual const atom_collection<UndefinedAtom> &undefined() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000256 return _undefinedAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000257 }
258
259 virtual const atom_collection<SharedLibraryAtom> &sharedLibrary() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000260 return _sharedLibraryAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000261 }
262
263 virtual const atom_collection<AbsoluteAtom> &absolute() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000264 return _absoluteAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000265 }
266
Michael J. Spencer842885e2013-01-15 21:12:45 +0000267 Atom *findAtom(const Elf_Sym *symbol) {
268 return _symbolToAtomMapping.lookup(symbol);
Sid Manning8caf4de2012-09-17 12:49:38 +0000269 }
270
Sid Manning1a601412012-07-25 16:27:21 +0000271private:
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000272 ELFDefinedAtom<ELFT> *createDefinedAtomAndAssignRelocations(
273 StringRef symbolName, StringRef sectionName, const Elf_Sym *symbol,
274 const Elf_Shdr *section, ArrayRef<uint8_t> content) {
Michael J. Spencer842885e2013-01-15 21:12:45 +0000275 unsigned int referenceStart = _references.size();
276
277 // Only relocations that are inside the domain of the atom are added.
278
279 // Add Rela (those with r_addend) references:
280 for (auto &rai : _relocationAddendRefences[sectionName]) {
281 if (!((rai->r_offset >= symbol->st_value) &&
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000282 (rai->r_offset < symbol->st_value + content.size())))
Michael J. Spencer842885e2013-01-15 21:12:45 +0000283 continue;
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000284 auto *ERef = new (_readerStorage.Allocate<ELFReference<ELFT>>())
285 ELFReference<ELFT>(rai, rai->r_offset - symbol->st_value,
286 nullptr);
Michael J. Spencer842885e2013-01-15 21:12:45 +0000287 _references.push_back(ERef);
288 }
289
290 // Add Rel references.
291 for (auto &ri : _relocationReferences[sectionName]) {
292 if ((ri->r_offset >= symbol->st_value) &&
293 (ri->r_offset < symbol->st_value + content.size())) {
294 auto *ERef = new (_readerStorage.Allocate<ELFReference<ELFT>>())
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000295 ELFReference<ELFT>(ri, ri->r_offset - symbol->st_value,
296 nullptr);
Michael J. Spencer842885e2013-01-15 21:12:45 +0000297 _references.push_back(ERef);
298 }
299 }
300
301 // Create the DefinedAtom and add it to the list of DefinedAtoms.
302 return new (_readerStorage.Allocate<ELFDefinedAtom<ELFT>>())
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000303 ELFDefinedAtom<ELFT>(*this, symbolName, sectionName, symbol, section,
304 content, referenceStart, _references.size(),
305 _references);
Michael J. Spencer842885e2013-01-15 21:12:45 +0000306 }
307
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000308 std::unique_ptr<ELFObjectFile<ELFT>> _objFile;
309 atom_collection_vector<DefinedAtom> _definedAtoms;
310 atom_collection_vector<UndefinedAtom> _undefinedAtoms;
Sid Manning8caf4de2012-09-17 12:49:38 +0000311 atom_collection_vector<SharedLibraryAtom> _sharedLibraryAtoms;
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000312 atom_collection_vector<AbsoluteAtom> _absoluteAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000313
Michael J. Spencer6344b322012-12-20 00:37:10 +0000314 /// \brief _relocationAddendRefences and _relocationReferences contain the
315 /// list of relocations references. In ELF, if a section named, ".text" has
316 /// relocations will also have a section named ".rel.text" or ".rela.text"
317 /// which will hold the entries. -- .rel or .rela is prepended to create
318 /// the SHT_REL(A) section name.
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000319 std::map<llvm::StringRef,
320 std::vector<const Elf_Rela *>> _relocationAddendRefences;
321 std::map<llvm::StringRef,
322 std::vector<const Elf_Rel *>> _relocationReferences;
Michael J. Spencer842885e2013-01-15 21:12:45 +0000323 std::vector<ELFReference<ELFT> *> _references;
Sid Manning8caf4de2012-09-17 12:49:38 +0000324 llvm::DenseMap<const Elf_Sym *, Atom *> _symbolToAtomMapping;
Sid Manning8caf4de2012-09-17 12:49:38 +0000325 llvm::BumpPtrAllocator _readerStorage;
Sid Manning1a601412012-07-25 16:27:21 +0000326};
327
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000328/// \brief A reader object that will instantiate correct FileELF by examining the
329/// memory buffer for ELF class and bit width
330class ReaderELF : public Reader {
Sid Manning1a601412012-07-25 16:27:21 +0000331public:
Michael J. Spencera5d22812012-11-13 19:58:58 +0000332 ReaderELF(const ReaderOptionsELF &,
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000333 ReaderOptionsArchive &readerOptionsArchive)
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000334 : _readerOptionsArchive(readerOptionsArchive),
335 _readerArchive(_readerOptionsArchive) {
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000336 _readerOptionsArchive.setReader(this);
337 }
338
Michael J. Spencer842885e2013-01-15 21:12:45 +0000339 error_code parseFile(std::unique_ptr<MemoryBuffer> mb,
340 std::vector<std::unique_ptr<File>> &result) {
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000341 using llvm::object::ELFType;
Michael J. Spencer6344b322012-12-20 00:37:10 +0000342 llvm::sys::LLVMFileType fileType =
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000343 llvm::sys::IdentifyFileType(mb->getBufferStart(),
344 static_cast<unsigned>(mb->getBufferSize()));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000345
346 std::size_t MaxAlignment =
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000347 1ULL << llvm::CountTrailingZeros_64(uintptr_t(mb->getBufferStart()));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000348
Michael J. Spencer842885e2013-01-15 21:12:45 +0000349 llvm::error_code ec;
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000350 switch (fileType) {
Michael J. Spencer842885e2013-01-15 21:12:45 +0000351 case llvm::sys::ELF_Relocatable_FileType: {
352 std::pair<unsigned char, unsigned char> Ident = getElfArchType(&*mb);
353 std::unique_ptr<File> f;
Michael J. Spencer6344b322012-12-20 00:37:10 +0000354 // Instantiate the correct FileELF template instance based on the Ident
355 // pair. Once the File is created we push the file to the vector of files
356 // already created during parser's life.
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000357 if (Ident.first == llvm::ELF::ELFCLASS32 &&
358 Ident.second == llvm::ELF::ELFDATA2LSB) {
Michael J. Spencera2c97272013-01-04 21:09:21 +0000359 if (MaxAlignment >= 4)
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000360 f.reset(new FileELF<ELFType<llvm::support::little, 4, false>>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000361 std::move(mb), ec));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000362 else if (MaxAlignment >= 2)
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000363 f.reset(new FileELF<ELFType<llvm::support::little, 2, false>>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000364 std::move(mb), ec));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000365 else
366 llvm_unreachable("Invalid alignment for ELF file!");
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000367 } else if (Ident.first == llvm::ELF::ELFCLASS32 &&
368 Ident.second == llvm::ELF::ELFDATA2MSB) {
Michael J. Spencera2c97272013-01-04 21:09:21 +0000369 if (MaxAlignment >= 4)
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000370 f.reset(new FileELF<ELFType<llvm::support::big, 4, false>>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000371 std::move(mb), ec));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000372 else if (MaxAlignment >= 2)
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000373 f.reset(new FileELF<ELFType<llvm::support::big, 2, false>>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000374 std::move(mb), ec));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000375 else
376 llvm_unreachable("Invalid alignment for ELF file!");
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000377 } else if (Ident.first == llvm::ELF::ELFCLASS64 &&
378 Ident.second == llvm::ELF::ELFDATA2MSB) {
Michael J. Spencera2c97272013-01-04 21:09:21 +0000379 if (MaxAlignment >= 8)
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000380 f.reset(new FileELF<ELFType<llvm::support::big, 8, true>>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000381 std::move(mb), ec));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000382 else if (MaxAlignment >= 2)
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000383 f.reset(new FileELF<ELFType<llvm::support::big, 2, true>>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000384 std::move(mb), ec));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000385 else
386 llvm_unreachable("Invalid alignment for ELF file!");
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000387 } else if (Ident.first == llvm::ELF::ELFCLASS64 &&
388 Ident.second == llvm::ELF::ELFDATA2LSB) {
Michael J. Spencera2c97272013-01-04 21:09:21 +0000389 if (MaxAlignment >= 8)
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000390 f.reset(new FileELF<ELFType<llvm::support::little, 8, true>>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000391 std::move(mb), ec));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000392 else if (MaxAlignment >= 2)
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000393 f.reset(new FileELF<ELFType<llvm::support::little, 2, true>>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000394 std::move(mb), ec));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000395 else
396 llvm_unreachable("Invalid alignment for ELF file!");
Michael J. Spencera5d22812012-11-13 19:58:58 +0000397 }
398 if (!ec)
399 result.push_back(std::move(f));
400 break;
Michael J. Spencer842885e2013-01-15 21:12:45 +0000401 }
Michael J. Spencera5d22812012-11-13 19:58:58 +0000402 case llvm::sys::Archive_FileType:
403 ec = _readerArchive.parseFile(std::move(mb), result);
404 break;
Michael J. Spencera5d22812012-11-13 19:58:58 +0000405 default:
406 llvm_unreachable("not supported format");
407 break;
Sid Manning1a601412012-07-25 16:27:21 +0000408 }
409
410 if (ec)
411 return ec;
412
Sid Manning1a601412012-07-25 16:27:21 +0000413 return error_code::success();
414 }
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000415
416private:
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000417 ReaderOptionsArchive &_readerOptionsArchive;
418 ReaderArchive _readerArchive;
Sid Manning1a601412012-07-25 16:27:21 +0000419};
Michael J. Spencer6344b322012-12-20 00:37:10 +0000420} // end anon namespace.
Nick Kledzikabb69812012-05-31 22:34:00 +0000421
422namespace lld {
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000423ReaderOptionsELF::ReaderOptionsELF() {}
Nick Kledzikabb69812012-05-31 22:34:00 +0000424
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000425ReaderOptionsELF::~ReaderOptionsELF() {}
Nick Kledzikabb69812012-05-31 22:34:00 +0000426
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000427Reader *createReaderELF(const ReaderOptionsELF &options,
428 ReaderOptionsArchive &optionsArchive) {
429 return new ReaderELF(options, optionsArchive);
Nick Kledzikabb69812012-05-31 22:34:00 +0000430}
Michael J. Spencer6344b322012-12-20 00:37:10 +0000431} // end namespace lld