blob: 9cee0d71e5cf10560806cde7b00a70f388ccf2d6 [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"
Shankar Easwaranbbf9ddd2012-12-27 01:40:08 +000028#include "llvm/Support/Path.h"
Sid Manning1a601412012-07-25 16:27:21 +000029#include "llvm/Support/ELF.h"
30#include "llvm/Support/Endian.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000031#include "llvm/Support/ErrorHandling.h"
Sid Manning1a601412012-07-25 16:27:21 +000032#include "llvm/Support/MathExtras.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000033#include "llvm/Support/Memory.h"
34#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer6344b322012-12-20 00:37:10 +000035#include "llvm/Support/Path.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000036#include "llvm/Support/raw_ostream.h"
37#include "llvm/Support/system_error.h"
Shankar Easwaranbbf9ddd2012-12-27 01:40:08 +000038#include "AtomsELF.h"
Sid Manning1a601412012-07-25 16:27:21 +000039
Nick Kledzikabb69812012-05-31 22:34:00 +000040#include <map>
41#include <vector>
42
Sid Manning1a601412012-07-25 16:27:21 +000043using namespace lld;
Michael J. Spencer6344b322012-12-20 00:37:10 +000044using llvm::support::endianness;
Michael J. Spencera2c97272013-01-04 21:09:21 +000045using namespace llvm::object;
Sid Manning1a601412012-07-25 16:27:21 +000046
Michael J. Spencer6344b322012-12-20 00:37:10 +000047namespace {
Michael J. Spencer6344b322012-12-20 00:37:10 +000048// \brief Read a binary, find out based on the symbol table contents what kind
49// of symbol it is and create corresponding atoms for it
Michael J. Spencera2c97272013-01-04 21:09:21 +000050template<endianness target_endianness, std::size_t max_align, bool is64Bits>
Sid Manning1a601412012-07-25 16:27:21 +000051class FileELF: public File {
Michael J. Spencera2c97272013-01-04 21:09:21 +000052 typedef Elf_Sym_Impl<target_endianness, max_align, is64Bits> Elf_Sym;
53 typedef Elf_Shdr_Impl<target_endianness, max_align, is64Bits> Elf_Shdr;
54 typedef Elf_Rel_Impl<target_endianness, max_align, is64Bits, false> Elf_Rel;
55 typedef Elf_Rel_Impl<target_endianness, max_align, is64Bits, 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)
59 : 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. Spencera2c97272013-01-04 21:09:21 +000066 _objFile.reset(llvm::dyn_cast<ELFObjectFile<target_endianness, max_align,
Sid Manning8caf4de2012-09-17 12:49:38 +000067 is64Bits> >(binaryFile.get()));
Sid Manning1a601412012-07-25 16:27:21 +000068
Sid Manning8caf4de2012-09-17 12:49:38 +000069 if (!_objFile) {
Michael J. Spencera2c97272013-01-04 21:09:21 +000070 EC = make_error_code(object_error::invalid_file_type);
Sid Manning1a601412012-07-25 16:27:21 +000071 return;
72 }
73
Sid Manning8caf4de2012-09-17 12:49:38 +000074 binaryFile.take();
Sid Manning1a601412012-07-25 16:27:21 +000075
Sid Manning8caf4de2012-09-17 12:49:38 +000076 std::map< const Elf_Shdr *, std::vector<const Elf_Sym *>> sectionSymbols;
Sid Manning1a601412012-07-25 16:27:21 +000077
Michael J. Spencer6344b322012-12-20 00:37:10 +000078 // Handle: SHT_REL and SHT_RELA sections:
79 // Increment over the sections, when REL/RELA section types are found add
80 // the contents to the RelocationReferences map.
Michael J. Spencera2c97272013-01-04 21:09:21 +000081 section_iterator sit(_objFile->begin_sections());
82 section_iterator sie(_objFile->end_sections());
Sid Manning8caf4de2012-09-17 12:49:38 +000083 for (; sit != sie; sit.increment(EC)) {
84 if (EC)
85 return;
86
87 const Elf_Shdr *section = _objFile->getElfSection(sit);
88
89 if (section->sh_type == llvm::ELF::SHT_RELA) {
90 llvm::StringRef sectionName;
91 if ((EC = _objFile->getSectionName(section, sectionName)))
92 return;
93 // Get rid of the leading .rela so Atoms can use their own section
94 // name to find the relocs.
95 sectionName = sectionName.drop_front(5);
96
97 auto rai(_objFile->beginELFRela(section));
98 auto rae(_objFile->endELFRela(section));
99
100 auto &Ref = _relocationAddendRefences[sectionName];
101 for (; rai != rae; rai++) {
102 Ref.push_back(&*rai);
103 }
104 }
105
106 if (section->sh_type == llvm::ELF::SHT_REL) {
107 llvm::StringRef sectionName;
108 if ((EC = _objFile->getSectionName(section, sectionName)))
109 return;
110 // Get rid of the leading .rel so Atoms can use their own section
111 // name to find the relocs.
112 sectionName = sectionName.drop_front(4);
113
114 auto ri(_objFile->beginELFRel(section));
115 auto re(_objFile->endELFRel(section));
116
117 auto &Ref = _relocationReferences[sectionName];
118 for (; ri != re; ri++) {
119 Ref.push_back(&*ri);
120 }
121 }
122 }
123
Michael J. Spencer6344b322012-12-20 00:37:10 +0000124 // Increment over all the symbols collecting atoms and symbol names for
125 // later use.
Michael J. Spencera2c97272013-01-04 21:09:21 +0000126 symbol_iterator it(_objFile->begin_symbols());
127 symbol_iterator ie(_objFile->end_symbols());
Sid Manning1a601412012-07-25 16:27:21 +0000128
Sid Manning429a4bc2012-07-27 14:52:18 +0000129 for (; it != ie; it.increment(EC)) {
Sid Manning1a601412012-07-25 16:27:21 +0000130 if (EC)
Sid Manning429a4bc2012-07-27 14:52:18 +0000131 return;
Sid Manning1a601412012-07-25 16:27:21 +0000132
Sid Manning8caf4de2012-09-17 12:49:38 +0000133 if ((EC = it->getSection(sit)))
Sid Manning429a4bc2012-07-27 14:52:18 +0000134 return;
Sid Manning1a601412012-07-25 16:27:21 +0000135
Sid Manning8caf4de2012-09-17 12:49:38 +0000136 const Elf_Shdr *section = _objFile->getElfSection(sit);
137 const Elf_Sym *symbol = _objFile->getElfSymbol(it);
Sid Manning1a601412012-07-25 16:27:21 +0000138
Sid Manning8caf4de2012-09-17 12:49:38 +0000139 llvm::StringRef symbolName;
140 if ((EC = _objFile->getSymbolName(section, symbol, symbolName)))
Sid Manning429a4bc2012-07-27 14:52:18 +0000141 return;
Sid Manning1a601412012-07-25 16:27:21 +0000142
Sid Manning8caf4de2012-09-17 12:49:38 +0000143 if (symbol->st_shndx == llvm::ELF::SHN_ABS) {
Sid Manning1a601412012-07-25 16:27:21 +0000144 // Create an absolute atom.
Michael J. Spencera2c97272013-01-04 21:09:21 +0000145 auto *newAtom = new (_readerStorage.Allocate<
146 ELFAbsoluteAtom<target_endianness, max_align, is64Bits> > ())
147 ELFAbsoluteAtom<target_endianness, max_align, is64Bits>(
148 *this, symbolName, symbol, symbol->st_value);
Sid Manning1a601412012-07-25 16:27:21 +0000149
Sid Manning8caf4de2012-09-17 12:49:38 +0000150 _absoluteAtoms._atoms.push_back(newAtom);
151 _symbolToAtomMapping.insert(std::make_pair(symbol, newAtom));
Sid Manning8caf4de2012-09-17 12:49:38 +0000152 } else if (symbol->st_shndx == llvm::ELF::SHN_UNDEF) {
Sid Manning1a601412012-07-25 16:27:21 +0000153 // Create an undefined atom.
Michael J. Spencera2c97272013-01-04 21:09:21 +0000154 auto *newAtom = new (_readerStorage.Allocate<
155 ELFUndefinedAtom<target_endianness, max_align, is64Bits> > ())
156 ELFUndefinedAtom<target_endianness, max_align, is64Bits>(
157 *this, symbolName, symbol);
Sid Manning8caf4de2012-09-17 12:49:38 +0000158
159 _undefinedAtoms._atoms.push_back(newAtom);
160 _symbolToAtomMapping.insert(std::make_pair(symbol, newAtom));
Sid Manning1a601412012-07-25 16:27:21 +0000161 } else {
162 // This is actually a defined symbol. Add it to its section's list of
163 // symbols.
Sid Manning8caf4de2012-09-17 12:49:38 +0000164 if (symbol->getType() == llvm::ELF::STT_NOTYPE
165 || symbol->getType() == llvm::ELF::STT_OBJECT
166 || symbol->getType() == llvm::ELF::STT_FUNC
167 || symbol->getType() == llvm::ELF::STT_SECTION
168 || symbol->getType() == llvm::ELF::STT_FILE
169 || symbol->getType() == llvm::ELF::STT_TLS
170 || symbol->getType() == llvm::ELF::STT_COMMON
171 || symbol->st_shndx == llvm::ELF::SHN_COMMON) {
172 sectionSymbols[section].push_back(symbol);
Michael J. Spencer6344b322012-12-20 00:37:10 +0000173 } else {
Sid Manning8caf4de2012-09-17 12:49:38 +0000174 llvm::errs() << "Unable to create atom for: " << symbolName << "\n";
Michael J. Spencera2c97272013-01-04 21:09:21 +0000175 EC = object_error::parse_failed;
Sid Manning1a601412012-07-25 16:27:21 +0000176 return;
177 }
178 }
179 }
180
Sid Manning8caf4de2012-09-17 12:49:38 +0000181 for (auto &i : sectionSymbols) {
182 auto &symbols = i.second;
183 llvm::StringRef symbolName;
184 llvm::StringRef sectionName;
Sid Manning1a601412012-07-25 16:27:21 +0000185 // Sort symbols by position.
Sid Manning8caf4de2012-09-17 12:49:38 +0000186 std::stable_sort(symbols.begin(), symbols.end(),
Michael J. Spencer6344b322012-12-20 00:37:10 +0000187 [](const Elf_Sym *A, const Elf_Sym *B) {
Sid Manning1a601412012-07-25 16:27:21 +0000188 return A->st_value < B->st_value;
Michael J. Spencer6344b322012-12-20 00:37:10 +0000189 });
Sid Manning1a601412012-07-25 16:27:21 +0000190
191 // i.first is the section the symbol lives in
Sid Manning8caf4de2012-09-17 12:49:38 +0000192 for (auto si = symbols.begin(), se = symbols.end(); si != se; ++si) {
Sid Manning1a601412012-07-25 16:27:21 +0000193 StringRef symbolContents;
Sid Manning8caf4de2012-09-17 12:49:38 +0000194 if ((EC = _objFile->getSectionContents(i.first, symbolContents)))
Sid Manning429a4bc2012-07-27 14:52:18 +0000195 return;
Sid Manning1a601412012-07-25 16:27:21 +0000196
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
Sid Manning8caf4de2012-09-17 12:49:38 +0000200 if ((EC = _objFile->getSectionName(i.first, sectionName)))
Sid Manning429a4bc2012-07-27 14:52:18 +0000201 return;
Sid Manning1a601412012-07-25 16:27:21 +0000202
Sid Manning8caf4de2012-09-17 12:49:38 +0000203 bool isCommon = false;
Sid Manning1a601412012-07-25 16:27:21 +0000204 if (((*si)->getType() == llvm::ELF::STT_COMMON)
Sid Manning8caf4de2012-09-17 12:49:38 +0000205 || (*si)->st_shndx == llvm::ELF::SHN_COMMON)
206 isCommon = true;
Sid Manning1a601412012-07-25 16:27:21 +0000207
208 // Get the symbol's content:
Sid Manning8caf4de2012-09-17 12:49:38 +0000209 llvm::ArrayRef<uint8_t> symbolData;
210 uint64_t contentSize;
Sid Manning1a601412012-07-25 16:27:21 +0000211 if (si + 1 == se) {
212 // if this is the last symbol, take up the remaining data.
Michael J. Spencer6344b322012-12-20 00:37:10 +0000213 contentSize = (isCommon) ? 0
Sid Manning8caf4de2012-09-17 12:49:38 +0000214 : ((i.first)->sh_size - (*si)->st_value);
Sid Manning1a601412012-07-25 16:27:21 +0000215 }
216 else {
Michael J. Spencer6344b322012-12-20 00:37:10 +0000217 contentSize = (isCommon) ? 0
Sid Manning8caf4de2012-09-17 12:49:38 +0000218 : (*(si + 1))->st_value - (*si)->st_value;
Sid Manning1a601412012-07-25 16:27:21 +0000219 }
220
Sid Manning8caf4de2012-09-17 12:49:38 +0000221 symbolData = llvm::ArrayRef<uint8_t>((uint8_t *)symbolContents.data()
222 + (*si)->st_value, contentSize);
223
Sid Manning8caf4de2012-09-17 12:49:38 +0000224 unsigned int referenceStart = _references.size();
225
Michael J. Spencer6344b322012-12-20 00:37:10 +0000226 // Only relocations that are inside the domain of the atom are added.
Sid Manning8caf4de2012-09-17 12:49:38 +0000227
228 // Add Rela (those with r_addend) references:
229 for (auto &rai : _relocationAddendRefences[sectionName]) {
230 if ((rai->r_offset >= (*si)->st_value) &&
231 (rai->r_offset < (*si)->st_value+contentSize)) {
Michael J. Spencera2c97272013-01-04 21:09:21 +0000232 auto *ERef = new (_readerStorage.Allocate<
233 ELFReference<target_endianness, max_align, is64Bits> > ())
234 ELFReference<target_endianness, max_align, is64Bits> (
235 rai, rai->r_offset-(*si)->st_value, nullptr);
Sid Manning8caf4de2012-09-17 12:49:38 +0000236
237 _references.push_back(ERef);
238 }
239 }
240
Michael J. Spencer6344b322012-12-20 00:37:10 +0000241 // Add Rel references.
Sid Manning8caf4de2012-09-17 12:49:38 +0000242 for (auto &ri : _relocationReferences[sectionName]) {
243 if (((ri)->r_offset >= (*si)->st_value) &&
244 ((ri)->r_offset < (*si)->st_value+contentSize)) {
Michael J. Spencera2c97272013-01-04 21:09:21 +0000245 auto *ERef = new (_readerStorage.Allocate<
246 ELFReference<target_endianness, max_align, is64Bits> > ())
247 ELFReference<target_endianness, max_align, is64Bits> (
248 (ri), (ri)->r_offset-(*si)->st_value, nullptr);
Sid Manning8caf4de2012-09-17 12:49:38 +0000249
250 _references.push_back(ERef);
251 }
252 }
253
254 // Create the DefinedAtom and add it to the list of DefinedAtoms.
Michael J. Spencera2c97272013-01-04 21:09:21 +0000255 auto *newAtom = new (_readerStorage.Allocate<
256 ELFDefinedAtom<target_endianness, max_align, is64Bits> > ())
257 ELFDefinedAtom<target_endianness, max_align, is64Bits>(
258 *this, symbolName, sectionName, *si, i.first, symbolData,
259 referenceStart, _references.size(), _references);
Sid Manning8caf4de2012-09-17 12:49:38 +0000260
261 _definedAtoms._atoms.push_back(newAtom);
262 _symbolToAtomMapping.insert(std::make_pair((*si), newAtom));
Sid Manning1a601412012-07-25 16:27:21 +0000263 }
264 }
Sid Manning8caf4de2012-09-17 12:49:38 +0000265
Michael J. Spencer6344b322012-12-20 00:37:10 +0000266 // All the Atoms and References are created. Now update each Reference's
267 // target with the Atom pointer it refers to.
Sid Manning8caf4de2012-09-17 12:49:38 +0000268 for (auto &ri : _references) {
269 const Elf_Sym *Symbol = _objFile->getElfSymbol(ri->targetSymbolIndex());
270 ri->setTarget(findAtom (Symbol));
271 }
Sid Manning1a601412012-07-25 16:27:21 +0000272 }
273
274 virtual void addAtom(const Atom&) {
275 llvm_unreachable("cannot add atoms to native .o files");
276 }
277
278 virtual const atom_collection<DefinedAtom> &defined() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000279 return _definedAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000280 }
281
282 virtual const atom_collection<UndefinedAtom> &undefined() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000283 return _undefinedAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000284 }
285
286 virtual const atom_collection<SharedLibraryAtom> &sharedLibrary() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000287 return _sharedLibraryAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000288 }
289
290 virtual const atom_collection<AbsoluteAtom> &absolute() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000291 return _absoluteAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000292 }
293
Sid Manning8caf4de2012-09-17 12:49:38 +0000294 Atom *findAtom(const Elf_Sym *symbol) {
295 return (_symbolToAtomMapping.lookup(symbol));
296 }
297
Sid Manning1a601412012-07-25 16:27:21 +0000298private:
Michael J. Spencera2c97272013-01-04 21:09:21 +0000299 std::unique_ptr<ELFObjectFile<target_endianness, max_align, is64Bits> >
Sid Manning8caf4de2012-09-17 12:49:38 +0000300 _objFile;
301 atom_collection_vector<DefinedAtom> _definedAtoms;
302 atom_collection_vector<UndefinedAtom> _undefinedAtoms;
303 atom_collection_vector<SharedLibraryAtom> _sharedLibraryAtoms;
304 atom_collection_vector<AbsoluteAtom> _absoluteAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000305
Michael J. Spencer6344b322012-12-20 00:37:10 +0000306 /// \brief _relocationAddendRefences and _relocationReferences contain the
307 /// list of relocations references. In ELF, if a section named, ".text" has
308 /// relocations will also have a section named ".rel.text" or ".rela.text"
309 /// which will hold the entries. -- .rel or .rela is prepended to create
310 /// the SHT_REL(A) section name.
Sid Manning8caf4de2012-09-17 12:49:38 +0000311 std::map<llvm::StringRef, std::vector<const Elf_Rela *> >
312 _relocationAddendRefences;
313 std::map<llvm::StringRef, std::vector<const Elf_Rel *> >
314 _relocationReferences;
315
Michael J. Spencera2c97272013-01-04 21:09:21 +0000316 std::vector<ELFReference<target_endianness, max_align, is64Bits> *>
317 _references;
Sid Manning8caf4de2012-09-17 12:49:38 +0000318 llvm::DenseMap<const Elf_Sym *, Atom *> _symbolToAtomMapping;
319
320 llvm::BumpPtrAllocator _readerStorage;
Sid Manning1a601412012-07-25 16:27:21 +0000321};
322
Michael J. Spencer6344b322012-12-20 00:37:10 +0000323// \brief A reader object that will instantiate correct FileELF by examining the
324// memory buffer for ELF class and bitwidth
Sid Manning1a601412012-07-25 16:27:21 +0000325class ReaderELF: public Reader {
326public:
Michael J. Spencera5d22812012-11-13 19:58:58 +0000327 ReaderELF(const ReaderOptionsELF &,
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000328 ReaderOptionsArchive &readerOptionsArchive)
Michael J. Spencera5d22812012-11-13 19:58:58 +0000329 : _readerOptionsArchive(readerOptionsArchive)
Michael J. Spencer6344b322012-12-20 00:37:10 +0000330 , _readerArchive(_readerOptionsArchive) {
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000331 _readerOptionsArchive.setReader(this);
332 }
333
Sid Manning1a601412012-07-25 16:27:21 +0000334 error_code parseFile(std::unique_ptr<MemoryBuffer> mb, std::vector<
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000335 std::unique_ptr<File> > &result) {
Michael J. Spencer6344b322012-12-20 00:37:10 +0000336 llvm::error_code ec;
Sid Manning1a601412012-07-25 16:27:21 +0000337 std::unique_ptr<File> f;
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000338 std::pair<unsigned char, unsigned char> Ident;
Sid Manning1a601412012-07-25 16:27:21 +0000339
Michael J. Spencer6344b322012-12-20 00:37:10 +0000340 llvm::sys::LLVMFileType fileType =
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000341 llvm::sys::IdentifyFileType(mb->getBufferStart(),
342 static_cast<unsigned>(mb->getBufferSize()));
Michael J. Spencera2c97272013-01-04 21:09:21 +0000343
344 std::size_t MaxAlignment =
345 1ULL << llvm::CountTrailingZeros_64(uintptr_t(mb->getBufferStart()));
346
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000347 switch (fileType) {
Michael J. Spencera5d22812012-11-13 19:58:58 +0000348 case llvm::sys::ELF_Relocatable_FileType:
Michael J. Spencera2c97272013-01-04 21:09:21 +0000349 Ident = getElfArchType(&*mb);
Michael J. Spencer6344b322012-12-20 00:37:10 +0000350 // Instantiate the correct FileELF template instance based on the Ident
351 // pair. Once the File is created we push the file to the vector of files
352 // already created during parser's life.
Michael J. Spencera5d22812012-11-13 19:58:58 +0000353 if (Ident.first == llvm::ELF::ELFCLASS32 && Ident.second
354 == llvm::ELF::ELFDATA2LSB) {
Michael J. Spencera2c97272013-01-04 21:09:21 +0000355 if (MaxAlignment >= 4)
356 f.reset(
357 new FileELF<llvm::support::little, 4, false>(std::move(mb), ec));
358 else if (MaxAlignment >= 2)
359 f.reset(
360 new FileELF<llvm::support::little, 2, false>(std::move(mb), ec));
361 else
362 llvm_unreachable("Invalid alignment for ELF file!");
Michael J. Spencera5d22812012-11-13 19:58:58 +0000363 } else if (Ident.first == llvm::ELF::ELFCLASS32 && Ident.second
364 == llvm::ELF::ELFDATA2MSB) {
Michael J. Spencera2c97272013-01-04 21:09:21 +0000365 if (MaxAlignment >= 4)
366 f.reset(
367 new FileELF<llvm::support::big, 4, false>(std::move(mb), ec));
368 else if (MaxAlignment >= 2)
369 f.reset(
370 new FileELF<llvm::support::big, 2, false>(std::move(mb), ec));
371 else
372 llvm_unreachable("Invalid alignment for ELF file!");
Michael J. Spencera5d22812012-11-13 19:58:58 +0000373 } else if (Ident.first == llvm::ELF::ELFCLASS64 && Ident.second
374 == llvm::ELF::ELFDATA2MSB) {
Michael J. Spencera2c97272013-01-04 21:09:21 +0000375 if (MaxAlignment >= 8)
376 f.reset(
377 new FileELF<llvm::support::big, 8, true>(std::move(mb), ec));
378 else if (MaxAlignment >= 2)
379 f.reset(
380 new FileELF<llvm::support::big, 2, true>(std::move(mb), ec));
381 else
382 llvm_unreachable("Invalid alignment for ELF file!");
Michael J. Spencera5d22812012-11-13 19:58:58 +0000383 } else if (Ident.first == llvm::ELF::ELFCLASS64 && Ident.second
384 == llvm::ELF::ELFDATA2LSB) {
Michael J. Spencera2c97272013-01-04 21:09:21 +0000385 if (MaxAlignment >= 8)
386 f.reset(
387 new FileELF<llvm::support::little, 8, true>(std::move(mb), ec));
388 else if (MaxAlignment >= 2)
389 f.reset(
390 new FileELF<llvm::support::little, 2, true>(std::move(mb), ec));
391 else
392 llvm_unreachable("Invalid alignment for ELF file!");
Michael J. Spencera5d22812012-11-13 19:58:58 +0000393 }
394 if (!ec)
395 result.push_back(std::move(f));
396 break;
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000397
Michael J. Spencera5d22812012-11-13 19:58:58 +0000398 case llvm::sys::Archive_FileType:
399 ec = _readerArchive.parseFile(std::move(mb), result);
400 break;
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000401
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 {
Nick Kledzikabb69812012-05-31 22:34:00 +0000420ReaderOptionsELF::ReaderOptionsELF() {
421}
422
423ReaderOptionsELF::~ReaderOptionsELF() {
424}
425
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000426Reader *createReaderELF(const ReaderOptionsELF &options,
427 ReaderOptionsArchive &optionsArchive) {
428 return new ReaderELF(options, optionsArchive);
Nick Kledzikabb69812012-05-31 22:34:00 +0000429}
Michael J. Spencer6344b322012-12-20 00:37:10 +0000430} // end namespace lld