blob: 16e53f221c68f141186771b02554f407ca5d9d08 [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"
Nick Kledzikabb69812012-05-31 22:34:00 +000018#include "lld/Core/File.h"
Sid Manning8caf4de2012-09-17 12:49:38 +000019#include "lld/Core/Reference.h"
Michael J. Spencer6344b322012-12-20 00:37:10 +000020
Sid Manning1a601412012-07-25 16:27:21 +000021#include "llvm/ADT/ArrayRef.h"
Sid Manning8caf4de2012-09-17 12:49:38 +000022#include "llvm/ADT/SmallString.h"
Sid Manning1a601412012-07-25 16:27:21 +000023#include "llvm/ADT/StringRef.h"
24#include "llvm/Object/ELF.h"
25#include "llvm/Object/ObjectFile.h"
26#include "llvm/Support/Allocator.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000027#include "llvm/Support/Casting.h"
Michael J. Spencer8af48f32013-01-20 21:27:05 +000028#include "llvm/Support/ErrorOr.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. Spencer71c00f42013-01-16 23:34:45 +0000144 auto *newAtom = new (_readerStorage)
145 ELFAbsoluteAtom<ELFT>(*this, symbolName, symbol, symbol->st_value);
Sid Manning1a601412012-07-25 16:27:21 +0000146
Sid Manning8caf4de2012-09-17 12:49:38 +0000147 _absoluteAtoms._atoms.push_back(newAtom);
148 _symbolToAtomMapping.insert(std::make_pair(symbol, newAtom));
Sid Manning8caf4de2012-09-17 12:49:38 +0000149 } else if (symbol->st_shndx == llvm::ELF::SHN_UNDEF) {
Sid Manning1a601412012-07-25 16:27:21 +0000150 // Create an undefined atom.
Michael J. Spencer71c00f42013-01-16 23:34:45 +0000151 auto *newAtom = new (_readerStorage)
152 ELFUndefinedAtom<ELFT>(*this, symbolName, symbol);
Sid Manning8caf4de2012-09-17 12:49:38 +0000153
154 _undefinedAtoms._atoms.push_back(newAtom);
155 _symbolToAtomMapping.insert(std::make_pair(symbol, newAtom));
Sid Manning1a601412012-07-25 16:27:21 +0000156 } else {
157 // This is actually a defined symbol. Add it to its section's list of
158 // symbols.
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000159 if (symbol->getType() == llvm::ELF::STT_NOTYPE ||
160 symbol->getType() == llvm::ELF::STT_OBJECT ||
161 symbol->getType() == llvm::ELF::STT_FUNC ||
162 symbol->getType() == llvm::ELF::STT_GNU_IFUNC ||
163 symbol->getType() == llvm::ELF::STT_SECTION ||
164 symbol->getType() == llvm::ELF::STT_FILE ||
165 symbol->getType() == llvm::ELF::STT_TLS ||
166 symbol->getType() == llvm::ELF::STT_COMMON ||
167 symbol->st_shndx == llvm::ELF::SHN_COMMON) {
Sid Manning8caf4de2012-09-17 12:49:38 +0000168 sectionSymbols[section].push_back(symbol);
Michael J. Spencer6344b322012-12-20 00:37:10 +0000169 } else {
Sid Manning8caf4de2012-09-17 12:49:38 +0000170 llvm::errs() << "Unable to create atom for: " << symbolName << "\n";
Michael J. Spencera2c97272013-01-04 21:09:21 +0000171 EC = object_error::parse_failed;
Sid Manning1a601412012-07-25 16:27:21 +0000172 return;
173 }
174 }
175 }
176
Sid Manning8caf4de2012-09-17 12:49:38 +0000177 for (auto &i : sectionSymbols) {
178 auto &symbols = i.second;
Sid Manning1a601412012-07-25 16:27:21 +0000179 // Sort symbols by position.
Sid Manning8caf4de2012-09-17 12:49:38 +0000180 std::stable_sort(symbols.begin(), symbols.end(),
Michael J. Spencer6344b322012-12-20 00:37:10 +0000181 [](const Elf_Sym *A, const Elf_Sym *B) {
Sid Manning1a601412012-07-25 16:27:21 +0000182 return A->st_value < B->st_value;
Michael J. Spencer6344b322012-12-20 00:37:10 +0000183 });
Sid Manning1a601412012-07-25 16:27:21 +0000184
Michael J. Spencer842885e2013-01-15 21:12:45 +0000185 StringRef sectionContents;
186 if ((EC = _objFile->getSectionContents(i.first, sectionContents)))
187 return;
188
189 llvm::StringRef sectionName;
190 if ((EC = _objFile->getSectionName(i.first, sectionName)))
191 return;
192
Sid Manning1a601412012-07-25 16:27:21 +0000193 // i.first is the section the symbol lives in
Sid Manning8caf4de2012-09-17 12:49:38 +0000194 for (auto si = symbols.begin(), se = symbols.end(); si != se; ++si) {
Michael J. Spencer842885e2013-01-15 21:12:45 +0000195 llvm::StringRef symbolName;
Sid Manning8caf4de2012-09-17 12:49:38 +0000196 if ((EC = _objFile->getSymbolName(i.first, *si, symbolName)))
Sid Manning429a4bc2012-07-27 14:52:18 +0000197 return;
Sid Manning1a601412012-07-25 16:27:21 +0000198
Michael J. Spencer842885e2013-01-15 21:12:45 +0000199 bool isCommon = (*si)->getType() == llvm::ELF::STT_COMMON ||
200 (*si)->st_shndx == llvm::ELF::SHN_COMMON;
Sid Manning1a601412012-07-25 16:27:21 +0000201
202 // Get the symbol's content:
Sid Manning8caf4de2012-09-17 12:49:38 +0000203 uint64_t contentSize;
Sid Manning1a601412012-07-25 16:27:21 +0000204 if (si + 1 == se) {
205 // if this is the last symbol, take up the remaining data.
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000206 contentSize = isCommon ? 0
207 : i.first->sh_size - (*si)->st_value;
Michael J. Spencer842885e2013-01-15 21:12:45 +0000208 } else {
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000209 contentSize = isCommon ? 0
210 : (*(si + 1))->st_value - (*si)->st_value;
Sid Manning1a601412012-07-25 16:27:21 +0000211 }
212
Michael J. Spencer8b3898a2013-01-15 21:13:02 +0000213 // Don't allocate content to a weak symbol, as they may be merged away.
214 // Create an anonymous atom to hold the data.
215 ELFDefinedAtom<ELFT> *anonAtom = nullptr;
216 if ((*si)->getBinding() == llvm::ELF::STB_WEAK && contentSize != 0) {
217 // Create a new non-weak ELF symbol.
Michael J. Spencer71c00f42013-01-16 23:34:45 +0000218 auto sym = new (_readerStorage) Elf_Sym;
Michael J. Spencer8b3898a2013-01-15 21:13:02 +0000219 *sym = **si;
220 sym->setBinding(llvm::ELF::STB_GLOBAL);
221 anonAtom = createDefinedAtomAndAssignRelocations(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000222 "", sectionName, sym, i.first,
223 ArrayRef<uint8_t>((uint8_t *)sectionContents.data() +
224 (*si)->st_value, contentSize));
Michael J. Spencer8b3898a2013-01-15 21:13:02 +0000225 contentSize = 0;
226 }
227
Michael J. Spencer842885e2013-01-15 21:12:45 +0000228 ArrayRef<uint8_t> symbolData = ArrayRef<uint8_t>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000229 (uint8_t *)sectionContents.data() +
230 (*si)->st_value, contentSize);
Sid Manning8caf4de2012-09-17 12:49:38 +0000231
Michael J. Spencer842885e2013-01-15 21:12:45 +0000232 auto newAtom = createDefinedAtomAndAssignRelocations(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000233 symbolName, sectionName, *si, i.first, symbolData);
Sid Manning8caf4de2012-09-17 12:49:38 +0000234
235 _definedAtoms._atoms.push_back(newAtom);
236 _symbolToAtomMapping.insert(std::make_pair((*si), newAtom));
Michael J. Spencer8b3898a2013-01-15 21:13:02 +0000237 if (anonAtom)
238 _definedAtoms._atoms.push_back(anonAtom);
Sid Manning1a601412012-07-25 16:27:21 +0000239 }
240 }
Sid Manning8caf4de2012-09-17 12:49:38 +0000241
Michael J. Spencer6344b322012-12-20 00:37:10 +0000242 // All the Atoms and References are created. Now update each Reference's
243 // target with the Atom pointer it refers to.
Sid Manning8caf4de2012-09-17 12:49:38 +0000244 for (auto &ri : _references) {
Michael J. Spencer842885e2013-01-15 21:12:45 +0000245 const Elf_Sym *Symbol = _objFile->getElfSymbol(ri->targetSymbolIndex());
246 ri->setTarget(findAtom(Symbol));
Sid Manning8caf4de2012-09-17 12:49:38 +0000247 }
Sid Manning1a601412012-07-25 16:27:21 +0000248 }
249
Sid Manning1a601412012-07-25 16:27:21 +0000250 virtual const atom_collection<DefinedAtom> &defined() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000251 return _definedAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000252 }
253
254 virtual const atom_collection<UndefinedAtom> &undefined() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000255 return _undefinedAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000256 }
257
258 virtual const atom_collection<SharedLibraryAtom> &sharedLibrary() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000259 return _sharedLibraryAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000260 }
261
262 virtual const atom_collection<AbsoluteAtom> &absolute() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000263 return _absoluteAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000264 }
265
Michael J. Spencer842885e2013-01-15 21:12:45 +0000266 Atom *findAtom(const Elf_Sym *symbol) {
267 return _symbolToAtomMapping.lookup(symbol);
Sid Manning8caf4de2012-09-17 12:49:38 +0000268 }
269
Sid Manning1a601412012-07-25 16:27:21 +0000270private:
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000271 ELFDefinedAtom<ELFT> *createDefinedAtomAndAssignRelocations(
272 StringRef symbolName, StringRef sectionName, const Elf_Sym *symbol,
273 const Elf_Shdr *section, ArrayRef<uint8_t> content) {
Michael J. Spencer842885e2013-01-15 21:12:45 +0000274 unsigned int referenceStart = _references.size();
275
276 // Only relocations that are inside the domain of the atom are added.
277
278 // Add Rela (those with r_addend) references:
279 for (auto &rai : _relocationAddendRefences[sectionName]) {
280 if (!((rai->r_offset >= symbol->st_value) &&
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000281 (rai->r_offset < symbol->st_value + content.size())))
Michael J. Spencer842885e2013-01-15 21:12:45 +0000282 continue;
Michael J. Spencer71c00f42013-01-16 23:34:45 +0000283 auto *ERef = new (_readerStorage)
284 ELFReference<ELFT>(rai, rai->r_offset - symbol->st_value, nullptr);
Michael J. Spencer842885e2013-01-15 21:12:45 +0000285 _references.push_back(ERef);
286 }
287
288 // Add Rel references.
289 for (auto &ri : _relocationReferences[sectionName]) {
290 if ((ri->r_offset >= symbol->st_value) &&
291 (ri->r_offset < symbol->st_value + content.size())) {
Michael J. Spencer71c00f42013-01-16 23:34:45 +0000292 auto *ERef = new (_readerStorage)
293 ELFReference<ELFT>(ri, ri->r_offset - symbol->st_value, nullptr);
Michael J. Spencer842885e2013-01-15 21:12:45 +0000294 _references.push_back(ERef);
295 }
296 }
297
298 // Create the DefinedAtom and add it to the list of DefinedAtoms.
Michael J. Spencer71c00f42013-01-16 23:34:45 +0000299 return new (_readerStorage)
300 ELFDefinedAtom<ELFT>(*this, symbolName, sectionName, symbol, section,
301 content, referenceStart, _references.size(),
302 _references);
Michael J. Spencer842885e2013-01-15 21:12:45 +0000303 }
304
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000305 std::unique_ptr<ELFObjectFile<ELFT>> _objFile;
306 atom_collection_vector<DefinedAtom> _definedAtoms;
307 atom_collection_vector<UndefinedAtom> _undefinedAtoms;
Sid Manning8caf4de2012-09-17 12:49:38 +0000308 atom_collection_vector<SharedLibraryAtom> _sharedLibraryAtoms;
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000309 atom_collection_vector<AbsoluteAtom> _absoluteAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000310
Michael J. Spencer6344b322012-12-20 00:37:10 +0000311 /// \brief _relocationAddendRefences and _relocationReferences contain the
312 /// list of relocations references. In ELF, if a section named, ".text" has
313 /// relocations will also have a section named ".rel.text" or ".rela.text"
314 /// which will hold the entries. -- .rel or .rela is prepended to create
315 /// the SHT_REL(A) section name.
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000316 std::map<llvm::StringRef,
317 std::vector<const Elf_Rela *>> _relocationAddendRefences;
318 std::map<llvm::StringRef,
319 std::vector<const Elf_Rel *>> _relocationReferences;
Michael J. Spencer842885e2013-01-15 21:12:45 +0000320 std::vector<ELFReference<ELFT> *> _references;
Sid Manning8caf4de2012-09-17 12:49:38 +0000321 llvm::DenseMap<const Elf_Sym *, Atom *> _symbolToAtomMapping;
Sid Manning8caf4de2012-09-17 12:49:38 +0000322 llvm::BumpPtrAllocator _readerStorage;
Sid Manning1a601412012-07-25 16:27:21 +0000323};
324
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000325/// \brief A reader object that will instantiate correct FileELF by examining the
326/// memory buffer for ELF class and bit width
327class ReaderELF : public Reader {
Sid Manning1a601412012-07-25 16:27:21 +0000328public:
Michael J. Spencera5d22812012-11-13 19:58:58 +0000329 ReaderELF(const ReaderOptionsELF &,
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000330 ReaderOptionsArchive &readerOptionsArchive)
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000331 : _readerOptionsArchive(readerOptionsArchive),
332 _readerArchive(_readerOptionsArchive) {
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000333 _readerOptionsArchive.setReader(this);
334 }
335
Michael J. Spencer842885e2013-01-15 21:12:45 +0000336 error_code parseFile(std::unique_ptr<MemoryBuffer> mb,
337 std::vector<std::unique_ptr<File>> &result) {
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000338 using llvm::object::ELFType;
Michael J. Spencer6344b322012-12-20 00:37:10 +0000339 llvm::sys::LLVMFileType fileType =
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000340 llvm::sys::IdentifyFileType(mb->getBufferStart(),
341 static_cast<unsigned>(mb->getBufferSize()));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000342
343 std::size_t MaxAlignment =
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000344 1ULL << llvm::CountTrailingZeros_64(uintptr_t(mb->getBufferStart()));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000345
Michael J. Spencer842885e2013-01-15 21:12:45 +0000346 llvm::error_code ec;
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000347 switch (fileType) {
Michael J. Spencer842885e2013-01-15 21:12:45 +0000348 case llvm::sys::ELF_Relocatable_FileType: {
349 std::pair<unsigned char, unsigned char> Ident = getElfArchType(&*mb);
350 std::unique_ptr<File> f;
Michael J. Spencer6344b322012-12-20 00:37:10 +0000351 // Instantiate the correct FileELF template instance based on the Ident
352 // pair. Once the File is created we push the file to the vector of files
353 // already created during parser's life.
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000354 if (Ident.first == llvm::ELF::ELFCLASS32 &&
355 Ident.second == llvm::ELF::ELFDATA2LSB) {
Michael J. Spencera2c97272013-01-04 21:09:21 +0000356 if (MaxAlignment >= 4)
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000357 f.reset(new FileELF<ELFType<llvm::support::little, 4, false>>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000358 std::move(mb), ec));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000359 else if (MaxAlignment >= 2)
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000360 f.reset(new FileELF<ELFType<llvm::support::little, 2, false>>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000361 std::move(mb), ec));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000362 else
363 llvm_unreachable("Invalid alignment for ELF file!");
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000364 } else if (Ident.first == llvm::ELF::ELFCLASS32 &&
365 Ident.second == llvm::ELF::ELFDATA2MSB) {
Michael J. Spencera2c97272013-01-04 21:09:21 +0000366 if (MaxAlignment >= 4)
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000367 f.reset(new FileELF<ELFType<llvm::support::big, 4, false>>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000368 std::move(mb), ec));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000369 else if (MaxAlignment >= 2)
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000370 f.reset(new FileELF<ELFType<llvm::support::big, 2, false>>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000371 std::move(mb), ec));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000372 else
373 llvm_unreachable("Invalid alignment for ELF file!");
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000374 } else if (Ident.first == llvm::ELF::ELFCLASS64 &&
375 Ident.second == llvm::ELF::ELFDATA2MSB) {
Michael J. Spencera2c97272013-01-04 21:09:21 +0000376 if (MaxAlignment >= 8)
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000377 f.reset(new FileELF<ELFType<llvm::support::big, 8, true>>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000378 std::move(mb), ec));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000379 else if (MaxAlignment >= 2)
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000380 f.reset(new FileELF<ELFType<llvm::support::big, 2, true>>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000381 std::move(mb), ec));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000382 else
383 llvm_unreachable("Invalid alignment for ELF file!");
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000384 } else if (Ident.first == llvm::ELF::ELFCLASS64 &&
385 Ident.second == llvm::ELF::ELFDATA2LSB) {
Michael J. Spencera2c97272013-01-04 21:09:21 +0000386 if (MaxAlignment >= 8)
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000387 f.reset(new FileELF<ELFType<llvm::support::little, 8, true>>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000388 std::move(mb), ec));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000389 else if (MaxAlignment >= 2)
Michael J. Spencerb03f6c42013-01-15 07:53:22 +0000390 f.reset(new FileELF<ELFType<llvm::support::little, 2, true>>(
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000391 std::move(mb), ec));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000392 else
393 llvm_unreachable("Invalid alignment for ELF file!");
Michael J. Spencera5d22812012-11-13 19:58:58 +0000394 }
395 if (!ec)
396 result.push_back(std::move(f));
397 break;
Michael J. Spencer842885e2013-01-15 21:12:45 +0000398 }
Michael J. Spencera5d22812012-11-13 19:58:58 +0000399 case llvm::sys::Archive_FileType:
400 ec = _readerArchive.parseFile(std::move(mb), result);
401 break;
Michael J. Spencera5d22812012-11-13 19:58:58 +0000402 default:
403 llvm_unreachable("not supported format");
404 break;
Sid Manning1a601412012-07-25 16:27:21 +0000405 }
406
407 if (ec)
408 return ec;
409
Sid Manning1a601412012-07-25 16:27:21 +0000410 return error_code::success();
411 }
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000412
413private:
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000414 ReaderOptionsArchive &_readerOptionsArchive;
415 ReaderArchive _readerArchive;
Sid Manning1a601412012-07-25 16:27:21 +0000416};
Michael J. Spencer6344b322012-12-20 00:37:10 +0000417} // end anon namespace.
Nick Kledzikabb69812012-05-31 22:34:00 +0000418
419namespace lld {
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000420ReaderOptionsELF::ReaderOptionsELF() {}
Nick Kledzikabb69812012-05-31 22:34:00 +0000421
Michael J. Spencer84d3b012013-01-16 23:34:32 +0000422ReaderOptionsELF::~ReaderOptionsELF() {}
Nick Kledzikabb69812012-05-31 22:34:00 +0000423
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000424Reader *createReaderELF(const ReaderOptionsELF &options,
425 ReaderOptionsArchive &optionsArchive) {
426 return new ReaderELF(options, optionsArchive);
Nick Kledzikabb69812012-05-31 22:34:00 +0000427}
Michael J. Spencer6344b322012-12-20 00:37:10 +0000428} // end namespace lld