blob: e239fd77910b04dad8bf6675856d2202917045f5 [file] [log] [blame]
Nick Kledzikabb69812012-05-31 22:34:00 +00001//===- lib/ReaderWriter/ELF/ReaderELF.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//===----------------------------------------------------------------------===//
Sid Manning1a601412012-07-25 16:27:21 +00009//
10// This file contains the ELF Reader and all helper sub classes
11// to consume an ELF file and produces atoms out of it.
12//
13//===----------------------------------------------------------------------===//
Nick Kledzikabb69812012-05-31 22:34:00 +000014#include "lld/ReaderWriter/ReaderELF.h"
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +000015#include "lld/ReaderWriter/ReaderArchive.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000016#include "lld/Core/File.h"
Sid Manning8caf4de2012-09-17 12:49:38 +000017#include "lld/Core/Reference.h"
Sid Manning1a601412012-07-25 16:27:21 +000018#include "llvm/ADT/ArrayRef.h"
Sid Manning8caf4de2012-09-17 12:49:38 +000019#include "llvm/ADT/SmallString.h"
Sid Manning1a601412012-07-25 16:27:21 +000020#include "llvm/ADT/StringRef.h"
21#include "llvm/Object/ELF.h"
22#include "llvm/Object/ObjectFile.h"
23#include "llvm/Support/Allocator.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000024#include "llvm/Support/Casting.h"
Sid Manning1a601412012-07-25 16:27:21 +000025#include "llvm/Support/ELF.h"
26#include "llvm/Support/Endian.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000027#include "llvm/Support/ErrorHandling.h"
Sid Manning1a601412012-07-25 16:27:21 +000028#include "llvm/Support/MathExtras.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000029#include "llvm/Support/Memory.h"
30#include "llvm/Support/MemoryBuffer.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/Support/system_error.h"
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +000033#include "llvm/Support/Path.h"
Nick Kledzikabb69812012-05-31 22:34:00 +000034
Sid Manning1a601412012-07-25 16:27:21 +000035
Nick Kledzikabb69812012-05-31 22:34:00 +000036#include <map>
37#include <vector>
38
Sid Manning1a601412012-07-25 16:27:21 +000039using llvm::object::Elf_Sym_Impl;
40using namespace lld;
41
42namespace { // anonymous
43
Sid Manning1a601412012-07-25 16:27:21 +000044
Sid Manning8caf4de2012-09-17 12:49:38 +000045/// \brief Relocation References: Defined Atoms may contain
46/// references that will need to be patched before
47/// the executable is written.
48template <llvm::support::endianness target_endianness, bool is64Bits>
49class ELFReference final : public Reference {
50
51 typedef llvm::object::Elf_Rel_Impl
52 <target_endianness, is64Bits, false> Elf_Rel;
53 typedef llvm::object::Elf_Rel_Impl
54 <target_endianness, is64Bits, true> Elf_Rela;
Sid Manning1a601412012-07-25 16:27:21 +000055public:
Sid Manning1a601412012-07-25 16:27:21 +000056
Sid Manning8caf4de2012-09-17 12:49:38 +000057 ELFReference(const Elf_Rela *rela, uint64_t offset, const Atom *target)
58 : _target(target)
59 , _targetSymbolIndex(rela->getSymbol())
60 , _offsetInAtom(offset)
61 , _addend(rela->r_addend)
62 , _kind(rela->getType()) {}
63
64 ELFReference(const Elf_Rel *rel, uint64_t offset, const Atom *target)
65 : _target(target)
66 , _targetSymbolIndex(rel->getSymbol())
67 , _offsetInAtom(offset)
68 , _addend(0)
69 , _kind(rel->getType()) {}
70
71
72 virtual uint64_t offsetInAtom() const {
73 return _offsetInAtom;
Sid Manning1a601412012-07-25 16:27:21 +000074 }
75
Sid Manning8caf4de2012-09-17 12:49:38 +000076 virtual Kind kind() const {
77 return _kind;
Sid Manning1a601412012-07-25 16:27:21 +000078 }
79
Sid Manning8caf4de2012-09-17 12:49:38 +000080 virtual void setKind(Kind kind) {
81 _kind = kind;
Sid Manning1a601412012-07-25 16:27:21 +000082 }
83
Sid Manning8caf4de2012-09-17 12:49:38 +000084 virtual const Atom *target() const {
85 return _target;
86 }
87
88/// \brief targetSymbolIndex: This is the symbol table index that contains
89/// the target reference.
90 uint64_t targetSymbolIndex() const {
91 return _targetSymbolIndex;
92 }
93
94 virtual Addend addend() const {
95 return _addend;
96 }
97
98 virtual void setAddend(Addend A) {
99 _addend = A;
100 }
101
102 virtual void setTarget(const Atom *newAtom) {
103 _target = newAtom;
104 }
Sid Manning1a601412012-07-25 16:27:21 +0000105private:
Sid Manning8caf4de2012-09-17 12:49:38 +0000106 const Atom *_target;
107 uint64_t _targetSymbolIndex;
108 uint64_t _offsetInAtom;
109 Addend _addend;
110 Kind _kind;
Sid Manning1a601412012-07-25 16:27:21 +0000111};
112
113
Sid Manning8caf4de2012-09-17 12:49:38 +0000114/// \brief ELFAbsoluteAtom: These atoms store symbols that are fixed to a
115/// particular address. This atom has no content its address will be used by
116/// the writer to fixup references that point to it.
Sid Manning1a601412012-07-25 16:27:21 +0000117template<llvm::support::endianness target_endianness, bool is64Bits>
Sid Manning2a590242012-10-18 17:16:19 +0000118class ELFAbsoluteAtom final : public AbsoluteAtom {
119
120 typedef llvm::object::Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
Sid Manning8caf4de2012-09-17 12:49:38 +0000121
122public:
123 ELFAbsoluteAtom(const File &file,
124 llvm::StringRef name,
Sid Manning2a590242012-10-18 17:16:19 +0000125 const Elf_Sym *symbol,
Sid Manning8caf4de2012-09-17 12:49:38 +0000126 uint64_t value)
127 : _owningFile(file)
128 , _name(name)
Sid Manning2a590242012-10-18 17:16:19 +0000129 , _symbol(symbol)
Sid Manning8caf4de2012-09-17 12:49:38 +0000130 , _value(value)
131 {}
132
133 virtual const class File &file() const {
134 return _owningFile;
135 }
136
Sid Manning2a590242012-10-18 17:16:19 +0000137 virtual Scope scope() const {
138 if (_symbol->st_other == llvm::ELF::STV_HIDDEN)
139 return scopeLinkageUnit;
140 if (_symbol->getBinding() == llvm::ELF::STB_LOCAL)
141 return scopeTranslationUnit;
142 else
143 return scopeGlobal;
144 }
145
Sid Manning8caf4de2012-09-17 12:49:38 +0000146 virtual llvm::StringRef name() const {
147 return _name;
148 }
149
150 virtual uint64_t value() const {
151 return _value;
152 }
153
154private:
155 const File &_owningFile;
156 llvm::StringRef _name;
Sid Manning2a590242012-10-18 17:16:19 +0000157 const Elf_Sym *_symbol;
Sid Manning8caf4de2012-09-17 12:49:38 +0000158 uint64_t _value;
159};
160
161
162/// \brief ELFUndefinedAtom: These atoms store undefined symbols and are
163/// place holders that will be replaced by defined atoms later in the
164/// linking process.
165template<llvm::support::endianness target_endianness, bool is64Bits>
166class ELFUndefinedAtom final: public UndefinedAtom {
Sid Manning1a601412012-07-25 16:27:21 +0000167
168 typedef llvm::object::Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
169
170public:
Sid Manning8caf4de2012-09-17 12:49:38 +0000171 ELFUndefinedAtom(const File &file,
172 llvm::StringRef name,
173 const Elf_Sym *symbol)
174 : _owningFile(file)
175 , _name(name)
176 , _symbol(symbol)
Sid Manning1a601412012-07-25 16:27:21 +0000177 {}
178
179 virtual const class File &file() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000180 return _owningFile;
Sid Manning1a601412012-07-25 16:27:21 +0000181 }
182
183 virtual llvm::StringRef name() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000184 return _name;
Sid Manning1a601412012-07-25 16:27:21 +0000185 }
186
187 // FIXME What distinguishes a symbol in ELF that can help
188 // decide if the symbol is undefined only during build and not
189 // runtime? This will make us choose canBeNullAtBuildtime and
190 // canBeNullAtRuntime
191 //
192 virtual CanBeNull canBeNull() const {
193
Sid Manning8caf4de2012-09-17 12:49:38 +0000194 if (_symbol->getBinding() == llvm::ELF::STB_WEAK)
Sid Manning1a601412012-07-25 16:27:21 +0000195 return CanBeNull::canBeNullAtBuildtime;
196 else
197 return CanBeNull::canBeNullNever;
198 }
199
200private:
Sid Manning8caf4de2012-09-17 12:49:38 +0000201 const File &_owningFile;
202 llvm::StringRef _name;
203 const Elf_Sym *_symbol;
Sid Manning1a601412012-07-25 16:27:21 +0000204};
205
206
Sid Manning8caf4de2012-09-17 12:49:38 +0000207/// \brief ELFDefinedAtom: This atom stores defined symbols and will contain
208/// either data or code.
Sid Manning1a601412012-07-25 16:27:21 +0000209template<llvm::support::endianness target_endianness, bool is64Bits>
Sid Manning8caf4de2012-09-17 12:49:38 +0000210class ELFDefinedAtom final: public DefinedAtom {
Sid Manning1a601412012-07-25 16:27:21 +0000211
212 typedef llvm::object::Elf_Sym_Impl<target_endianness, is64Bits> Elf_Sym;
213 typedef llvm::object::Elf_Shdr_Impl<target_endianness, is64Bits> Elf_Shdr;
214
215public:
Sid Manning8caf4de2012-09-17 12:49:38 +0000216 ELFDefinedAtom(const File &file,
217 llvm::StringRef symbolName,
218 llvm::StringRef sectionName,
219 const Elf_Sym *symbol,
220 const Elf_Shdr *section,
221 llvm::ArrayRef<uint8_t> contentData,
222 unsigned int referenceStart,
223 unsigned int referenceEnd,
224 std::vector<ELFReference
225 <target_endianness, is64Bits> *> &referenceList)
226
227 : _owningFile(file)
228 , _symbolName(symbolName)
229 , _sectionName(sectionName)
230 , _symbol(symbol)
231 , _section(section)
232 , _contentData(contentData)
233 , _referenceStartIndex(referenceStart)
234 , _referenceEndIndex(referenceEnd)
235 , _referenceList(referenceList) {
236 static uint64_t orderNumber = 0;
237 _ordinal = ++orderNumber;
Sid Manning1a601412012-07-25 16:27:21 +0000238 }
239
240 virtual const class File &file() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000241 return _owningFile;
Sid Manning1a601412012-07-25 16:27:21 +0000242 }
243
244 virtual llvm::StringRef name() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000245 return _symbolName;
Sid Manning1a601412012-07-25 16:27:21 +0000246 }
247
248 virtual uint64_t ordinal() const {
249 return _ordinal;
250 }
251
252 virtual uint64_t size() const {
253
Sid Manning05c82a42012-10-03 21:46:48 +0000254 // Common symbols are not allocated in object files,
255 // so use st_size to tell how many bytes are required.
Sid Manning8caf4de2012-09-17 12:49:38 +0000256 if ((_symbol->getType() == llvm::ELF::STT_COMMON)
257 || _symbol->st_shndx == llvm::ELF::SHN_COMMON)
Sid Manning05c82a42012-10-03 21:46:48 +0000258 return (uint64_t)_symbol->st_size;
Sid Manning1a601412012-07-25 16:27:21 +0000259
Sid Manning8caf4de2012-09-17 12:49:38 +0000260 return _contentData.size();
Sid Manning1a601412012-07-25 16:27:21 +0000261
262 }
263
264 virtual Scope scope() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000265 if (_symbol->st_other == llvm::ELF::STV_HIDDEN)
Sid Manning1a601412012-07-25 16:27:21 +0000266 return scopeLinkageUnit;
Sid Manning8caf4de2012-09-17 12:49:38 +0000267 else if (_symbol->getBinding() != llvm::ELF::STB_LOCAL)
Sid Manning1a601412012-07-25 16:27:21 +0000268 return scopeGlobal;
269 else
270 return scopeTranslationUnit;
271 }
272
273 // FIXME Need to revisit this in future.
274
275 virtual Interposable interposable() const {
276 return interposeNo;
277 }
278
279 // FIXME What ways can we determine this in ELF?
280
281 virtual Merge merge() const {
282
Sid Manning8caf4de2012-09-17 12:49:38 +0000283 if (_symbol->getBinding() == llvm::ELF::STB_WEAK)
Sid Manning1a601412012-07-25 16:27:21 +0000284 return mergeAsWeak;
285
Sid Manning8caf4de2012-09-17 12:49:38 +0000286 if ((_symbol->getType() == llvm::ELF::STT_COMMON)
287 || _symbol->st_shndx == llvm::ELF::SHN_COMMON)
Sid Manning1a601412012-07-25 16:27:21 +0000288 return mergeAsTentative;
289
290 return mergeNo;
291 }
292
293 virtual ContentType contentType() const {
294
Sid Manninge861d432012-10-01 23:23:05 +0000295 ContentType ret = typeUnknown;
Sid Manning1a601412012-07-25 16:27:21 +0000296
Sid Manning1a601412012-07-25 16:27:21 +0000297
Sid Manninge861d432012-10-01 23:23:05 +0000298 switch (_section->sh_type) {
299 case llvm::ELF::SHT_PROGBITS:
300 case llvm::ELF::SHT_DYNAMIC:
301 switch (_section->sh_flags) {
302 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR):
303 ret = typeCode;
304 break;
305 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE):
306 ret = typeData;
307 break;
308 case llvm::ELF::SHF_ALLOC:
309 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_MERGE):
310 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_MERGE |
311 llvm::ELF::SHF_STRINGS):
312 ret = typeConstant;
313 break;
314 }
315 break;
316 case llvm::ELF::SHT_NOBITS:
317 ret = typeZeroFill;
318 break;
319 case llvm::ELF::SHT_NULL:
320 if ((_symbol->getType() == llvm::ELF::STT_COMMON)
321 || _symbol->st_shndx == llvm::ELF::SHN_COMMON)
322 ret = typeZeroFill;
323 break;
324 }
Sid Manning2a590242012-10-18 17:16:19 +0000325
Sid Manninge861d432012-10-01 23:23:05 +0000326 return ret;
Sid Manning1a601412012-07-25 16:27:21 +0000327 }
328
329 virtual Alignment alignment() const {
330
331 // Unallocated common symbols specify their alignment
332 // constraints in st_value.
Sid Manning8caf4de2012-09-17 12:49:38 +0000333 if ((_symbol->getType() == llvm::ELF::STT_COMMON)
334 || _symbol->st_shndx == llvm::ELF::SHN_COMMON) {
Sid Manning05c82a42012-10-03 21:46:48 +0000335 return Alignment(llvm::Log2_64(_symbol->st_value));
Sid Manning1a601412012-07-25 16:27:21 +0000336 }
Sid Manning8caf4de2012-09-17 12:49:38 +0000337 return Alignment(llvm::Log2_64(_section->sh_addralign));
Sid Manning1a601412012-07-25 16:27:21 +0000338 }
339
340 // Do we have a choice for ELF? All symbols
341 // live in explicit sections.
342 virtual SectionChoice sectionChoice() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000343 if (_symbol->st_shndx > llvm::ELF::SHN_LORESERVE)
Sid Manning1a601412012-07-25 16:27:21 +0000344 return sectionBasedOnContent;
345
346 return sectionCustomRequired;
347 }
348
349 virtual llvm::StringRef customSectionName() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000350 return _sectionName;
Sid Manning1a601412012-07-25 16:27:21 +0000351 }
352
353 // It isn't clear that __attribute__((used)) is transmitted to
354 // the ELF object file.
355 virtual DeadStripKind deadStrip() const {
356 return deadStripNormal;
357 }
358
359 virtual ContentPermissions permissions() const {
360
Sid Manning8caf4de2012-09-17 12:49:38 +0000361 switch (_section->sh_type) {
Sid Manning1a601412012-07-25 16:27:21 +0000362 // permRW_L is for sections modified by the runtime
363 // loader.
364 case llvm::ELF::SHT_REL:
365 case llvm::ELF::SHT_RELA:
366 return permRW_L;
367
368 case llvm::ELF::SHT_DYNAMIC:
369 case llvm::ELF::SHT_PROGBITS:
Sid Manning8caf4de2012-09-17 12:49:38 +0000370 switch (_section->sh_flags) {
Sid Manning1a601412012-07-25 16:27:21 +0000371
372 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR):
373 return permR_X;
374
375 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE):
376 return permRW_;
377
378 case llvm::ELF::SHF_ALLOC:
379 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_MERGE):
380 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_MERGE
381 | llvm::ELF::SHF_STRINGS):
382 return permR__;
383 }
384 default:
385 return perm___;
386 }
387 }
388
389 // Many non ARM architectures use ELF file format
390 // This not really a place to put a architecture
391 // specific method in an atom. A better approach is
392 // needed.
393 //
394 virtual bool isThumb() const {
395 return false;
396 }
397
398 // FIXME Not Sure if ELF supports alias atoms. Find out more.
399 virtual bool isAlias() const {
400 return false;
401 }
402
403 virtual llvm::ArrayRef<uint8_t> rawContent() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000404 return _contentData;
Sid Manning1a601412012-07-25 16:27:21 +0000405 }
406
Sid Manning8caf4de2012-09-17 12:49:38 +0000407 DefinedAtom::reference_iterator begin() const {
408 uintptr_t index = _referenceStartIndex;
409 const void *it = reinterpret_cast<const void*>(index);
410 return reference_iterator(*this, it);
Sid Manning1a601412012-07-25 16:27:21 +0000411 }
412
Sid Manning8caf4de2012-09-17 12:49:38 +0000413 DefinedAtom::reference_iterator end() const {
414 uintptr_t index = _referenceEndIndex;
415 const void *it = reinterpret_cast<const void*>(index);
416 return reference_iterator(*this, it);
417 }
418
419 const Reference *derefIterator(const void *It) const {
420 uintptr_t index = reinterpret_cast<uintptr_t>(It);
421 assert(index >= _referenceStartIndex);
422 assert(index < _referenceEndIndex);
423 return ((_referenceList)[index]);
424 }
425
426 void incrementIterator(const void*& It) const {
427 uintptr_t index = reinterpret_cast<uintptr_t>(It);
428 ++index;
429 It = reinterpret_cast<const void*>(index);
Sid Manning1a601412012-07-25 16:27:21 +0000430 }
431
432private:
Sid Manning1a601412012-07-25 16:27:21 +0000433
Sid Manning8caf4de2012-09-17 12:49:38 +0000434 const File &_owningFile;
435 llvm::StringRef _symbolName;
436 llvm::StringRef _sectionName;
437 const Elf_Sym *_symbol;
438 const Elf_Shdr *_section;
Sid Manning8caf4de2012-09-17 12:49:38 +0000439 // _contentData will hold the bits that make up the atom.
440 llvm::ArrayRef<uint8_t> _contentData;
Sid Manning1a601412012-07-25 16:27:21 +0000441
442 uint64_t _ordinal;
Sid Manning8caf4de2012-09-17 12:49:38 +0000443 unsigned int _referenceStartIndex;
444 unsigned int _referenceEndIndex;
445 std::vector<ELFReference<target_endianness, is64Bits> *> &_referenceList;
Sid Manning1a601412012-07-25 16:27:21 +0000446};
447
448
449// FileELF will read a binary, find out based on the symbol table contents
450// what kind of symbol it is and create corresponding atoms for it
451
452template<llvm::support::endianness target_endianness, bool is64Bits>
453class FileELF: public File {
454
Sid Manning8caf4de2012-09-17 12:49:38 +0000455 typedef llvm::object::Elf_Sym_Impl
456 <target_endianness, is64Bits> Elf_Sym;
457 typedef llvm::object::Elf_Shdr_Impl
458 <target_endianness, is64Bits> Elf_Shdr;
459 typedef llvm::object::Elf_Rel_Impl
460 <target_endianness, is64Bits, false> Elf_Rel;
461 typedef llvm::object::Elf_Rel_Impl
462 <target_endianness, is64Bits, true> Elf_Rela;
Sid Manning1a601412012-07-25 16:27:21 +0000463
464public:
465 FileELF(std::unique_ptr<llvm::MemoryBuffer> MB, llvm::error_code &EC) :
466 File(MB->getBufferIdentifier()) {
467
Sid Manning8caf4de2012-09-17 12:49:38 +0000468 llvm::OwningPtr<llvm::object::Binary> binaryFile;
469 EC = llvm::object::createBinary(MB.release(), binaryFile);
Sid Manning1a601412012-07-25 16:27:21 +0000470 if (EC)
471 return;
472
473 // Point Obj to correct class and bitwidth ELF object
Sid Manning8caf4de2012-09-17 12:49:38 +0000474 _objFile.reset(llvm::dyn_cast<llvm::object::ELFObjectFile<target_endianness,
475 is64Bits> >(binaryFile.get()));
Sid Manning1a601412012-07-25 16:27:21 +0000476
Sid Manning8caf4de2012-09-17 12:49:38 +0000477 if (!_objFile) {
Sid Manning1a601412012-07-25 16:27:21 +0000478 EC = make_error_code(llvm::object::object_error::invalid_file_type);
479 return;
480 }
481
Sid Manning8caf4de2012-09-17 12:49:38 +0000482 binaryFile.take();
Sid Manning1a601412012-07-25 16:27:21 +0000483
Sid Manning8caf4de2012-09-17 12:49:38 +0000484 std::map< const Elf_Shdr *, std::vector<const Elf_Sym *>> sectionSymbols;
Sid Manning1a601412012-07-25 16:27:21 +0000485
Sid Manning8caf4de2012-09-17 12:49:38 +0000486// Handle: SHT_REL and SHT_RELA sections:
487// Increment over the sections, when REL/RELA section types are
488// found add the contents to the RelocationReferences map.
489
490 llvm::object::section_iterator sit(_objFile->begin_sections());
491 llvm::object::section_iterator sie(_objFile->end_sections());
492 for (; sit != sie; sit.increment(EC)) {
493 if (EC)
494 return;
495
496 const Elf_Shdr *section = _objFile->getElfSection(sit);
497
498 if (section->sh_type == llvm::ELF::SHT_RELA) {
499 llvm::StringRef sectionName;
500 if ((EC = _objFile->getSectionName(section, sectionName)))
501 return;
502 // Get rid of the leading .rela so Atoms can use their own section
503 // name to find the relocs.
504 sectionName = sectionName.drop_front(5);
505
506 auto rai(_objFile->beginELFRela(section));
507 auto rae(_objFile->endELFRela(section));
508
509 auto &Ref = _relocationAddendRefences[sectionName];
510 for (; rai != rae; rai++) {
511 Ref.push_back(&*rai);
512 }
513 }
514
515 if (section->sh_type == llvm::ELF::SHT_REL) {
516 llvm::StringRef sectionName;
517 if ((EC = _objFile->getSectionName(section, sectionName)))
518 return;
519 // Get rid of the leading .rel so Atoms can use their own section
520 // name to find the relocs.
521 sectionName = sectionName.drop_front(4);
522
523 auto ri(_objFile->beginELFRel(section));
524 auto re(_objFile->endELFRel(section));
525
526 auto &Ref = _relocationReferences[sectionName];
527 for (; ri != re; ri++) {
528 Ref.push_back(&*ri);
529 }
530 }
531 }
532
533
534// Increment over all the symbols collecting atoms and symbol
535// names for later use.
536
537 llvm::object::symbol_iterator it(_objFile->begin_symbols());
538 llvm::object::symbol_iterator ie(_objFile->end_symbols());
Sid Manning1a601412012-07-25 16:27:21 +0000539
Sid Manning429a4bc2012-07-27 14:52:18 +0000540 for (; it != ie; it.increment(EC)) {
Sid Manning1a601412012-07-25 16:27:21 +0000541 if (EC)
Sid Manning429a4bc2012-07-27 14:52:18 +0000542 return;
Sid Manning1a601412012-07-25 16:27:21 +0000543
Sid Manning8caf4de2012-09-17 12:49:38 +0000544 if ((EC = it->getSection(sit)))
Sid Manning429a4bc2012-07-27 14:52:18 +0000545 return;
Sid Manning1a601412012-07-25 16:27:21 +0000546
Sid Manning8caf4de2012-09-17 12:49:38 +0000547 const Elf_Shdr *section = _objFile->getElfSection(sit);
548 const Elf_Sym *symbol = _objFile->getElfSymbol(it);
Sid Manning1a601412012-07-25 16:27:21 +0000549
Sid Manning8caf4de2012-09-17 12:49:38 +0000550 llvm::StringRef symbolName;
551 if ((EC = _objFile->getSymbolName(section, symbol, symbolName)))
Sid Manning429a4bc2012-07-27 14:52:18 +0000552 return;
Sid Manning1a601412012-07-25 16:27:21 +0000553
Sid Manning8caf4de2012-09-17 12:49:38 +0000554 if (symbol->st_shndx == llvm::ELF::SHN_ABS) {
Sid Manning1a601412012-07-25 16:27:21 +0000555 // Create an absolute atom.
Sid Manning8caf4de2012-09-17 12:49:38 +0000556 auto *newAtom = new (_readerStorage.Allocate
557 <ELFAbsoluteAtom<target_endianness, is64Bits> > ())
558 ELFAbsoluteAtom<target_endianness, is64Bits>
Sid Manning2a590242012-10-18 17:16:19 +0000559 (*this, symbolName, symbol, symbol->st_value);
Sid Manning1a601412012-07-25 16:27:21 +0000560
Sid Manning8caf4de2012-09-17 12:49:38 +0000561 _absoluteAtoms._atoms.push_back(newAtom);
562 _symbolToAtomMapping.insert(std::make_pair(symbol, newAtom));
563
564 } else if (symbol->st_shndx == llvm::ELF::SHN_UNDEF) {
Sid Manning1a601412012-07-25 16:27:21 +0000565 // Create an undefined atom.
Sid Manning8caf4de2012-09-17 12:49:38 +0000566 auto *newAtom = new (_readerStorage.Allocate
567 <ELFUndefinedAtom<target_endianness, is64Bits> > ())
568 ELFUndefinedAtom<target_endianness, is64Bits>
569 (*this, symbolName, symbol);
570
571 _undefinedAtoms._atoms.push_back(newAtom);
572 _symbolToAtomMapping.insert(std::make_pair(symbol, newAtom));
573
Sid Manning1a601412012-07-25 16:27:21 +0000574 } else {
575 // This is actually a defined symbol. Add it to its section's list of
576 // symbols.
Sid Manning8caf4de2012-09-17 12:49:38 +0000577 if (symbol->getType() == llvm::ELF::STT_NOTYPE
578 || symbol->getType() == llvm::ELF::STT_OBJECT
579 || symbol->getType() == llvm::ELF::STT_FUNC
580 || symbol->getType() == llvm::ELF::STT_SECTION
581 || symbol->getType() == llvm::ELF::STT_FILE
582 || symbol->getType() == llvm::ELF::STT_TLS
583 || symbol->getType() == llvm::ELF::STT_COMMON
584 || symbol->st_shndx == llvm::ELF::SHN_COMMON) {
585 sectionSymbols[section].push_back(symbol);
Sid Manning1a601412012-07-25 16:27:21 +0000586 }
587 else {
Sid Manning8caf4de2012-09-17 12:49:38 +0000588 llvm::errs() << "Unable to create atom for: " << symbolName << "\n";
Sid Manning1a601412012-07-25 16:27:21 +0000589 EC = llvm::object::object_error::parse_failed;
590 return;
591 }
592 }
593 }
594
Sid Manning8caf4de2012-09-17 12:49:38 +0000595 for (auto &i : sectionSymbols) {
596 auto &symbols = i.second;
597 llvm::StringRef symbolName;
598 llvm::StringRef sectionName;
Sid Manning1a601412012-07-25 16:27:21 +0000599 // Sort symbols by position.
Sid Manning8caf4de2012-09-17 12:49:38 +0000600 std::stable_sort(symbols.begin(), symbols.end(),
Sid Manning1a601412012-07-25 16:27:21 +0000601 // From ReaderCOFF.cpp:
602 // For some reason MSVC fails to allow the lambda in this context with
603 // a "illegal use of local type in type instantiation". MSVC is clearly
604 // wrong here. Force a conversion to function pointer to work around.
605 static_cast<bool(*)(const Elf_Sym*, const Elf_Sym*)>(
606 [](const Elf_Sym *A, const Elf_Sym *B) -> bool {
607 return A->st_value < B->st_value;
608 }));
609
610 // i.first is the section the symbol lives in
Sid Manning8caf4de2012-09-17 12:49:38 +0000611 for (auto si = symbols.begin(), se = symbols.end(); si != se; ++si) {
Sid Manning1a601412012-07-25 16:27:21 +0000612
613 StringRef symbolContents;
Sid Manning8caf4de2012-09-17 12:49:38 +0000614 if ((EC = _objFile->getSectionContents(i.first, symbolContents)))
Sid Manning429a4bc2012-07-27 14:52:18 +0000615 return;
Sid Manning1a601412012-07-25 16:27:21 +0000616
Sid Manning8caf4de2012-09-17 12:49:38 +0000617 if ((EC = _objFile->getSymbolName(i.first, *si, symbolName)))
Sid Manning429a4bc2012-07-27 14:52:18 +0000618 return;
Sid Manning1a601412012-07-25 16:27:21 +0000619
Sid Manning8caf4de2012-09-17 12:49:38 +0000620 if ((EC = _objFile->getSectionName(i.first, sectionName)))
Sid Manning429a4bc2012-07-27 14:52:18 +0000621 return;
Sid Manning1a601412012-07-25 16:27:21 +0000622
Sid Manning8caf4de2012-09-17 12:49:38 +0000623 bool isCommon = false;
Sid Manning1a601412012-07-25 16:27:21 +0000624 if (((*si)->getType() == llvm::ELF::STT_COMMON)
Sid Manning8caf4de2012-09-17 12:49:38 +0000625 || (*si)->st_shndx == llvm::ELF::SHN_COMMON)
626 isCommon = true;
Sid Manning1a601412012-07-25 16:27:21 +0000627
628 // Get the symbol's content:
Sid Manning8caf4de2012-09-17 12:49:38 +0000629 llvm::ArrayRef<uint8_t> symbolData;
630 uint64_t contentSize;
Sid Manning1a601412012-07-25 16:27:21 +0000631 if (si + 1 == se) {
632 // if this is the last symbol, take up the remaining data.
Sid Manning8caf4de2012-09-17 12:49:38 +0000633 contentSize = (isCommon) ? 0
634 : ((i.first)->sh_size - (*si)->st_value);
Sid Manning1a601412012-07-25 16:27:21 +0000635 }
636 else {
Sid Manning8caf4de2012-09-17 12:49:38 +0000637 contentSize = (isCommon) ? 0
638 : (*(si + 1))->st_value - (*si)->st_value;
Sid Manning1a601412012-07-25 16:27:21 +0000639 }
640
Sid Manning8caf4de2012-09-17 12:49:38 +0000641 symbolData = llvm::ArrayRef<uint8_t>((uint8_t *)symbolContents.data()
642 + (*si)->st_value, contentSize);
643
644
645 unsigned int referenceStart = _references.size();
646
647 // Only relocations that are inside the domain of the atom are
648 // added.
649
650 // Add Rela (those with r_addend) references:
651 for (auto &rai : _relocationAddendRefences[sectionName]) {
652 if ((rai->r_offset >= (*si)->st_value) &&
653 (rai->r_offset < (*si)->st_value+contentSize)) {
654
655 auto *ERef = new (_readerStorage.Allocate
656 <ELFReference<target_endianness, is64Bits> > ())
657 ELFReference<target_endianness, is64Bits> (
658 rai, rai->r_offset-(*si)->st_value, nullptr);
659
660 _references.push_back(ERef);
661 }
662 }
663
664 // Add Rel references:
665 for (auto &ri : _relocationReferences[sectionName]) {
666 if (((ri)->r_offset >= (*si)->st_value) &&
667 ((ri)->r_offset < (*si)->st_value+contentSize)) {
668
669 auto *ERef = new (_readerStorage.Allocate
670 <ELFReference<target_endianness, is64Bits> > ())
671 ELFReference<target_endianness, is64Bits> (
672 (ri), (ri)->r_offset-(*si)->st_value, nullptr);
673
674 _references.push_back(ERef);
675 }
676 }
677
678 // Create the DefinedAtom and add it to the list of DefinedAtoms.
679 auto *newAtom = new (_readerStorage.Allocate
680 <ELFDefinedAtom<target_endianness, is64Bits> > ())
681 ELFDefinedAtom<target_endianness, is64Bits>
682 (*this, symbolName, sectionName,
683 *si, i.first, symbolData,
684 referenceStart, _references.size(), _references);
685
686 _definedAtoms._atoms.push_back(newAtom);
687 _symbolToAtomMapping.insert(std::make_pair((*si), newAtom));
688
Sid Manning1a601412012-07-25 16:27:21 +0000689 }
690 }
Sid Manning8caf4de2012-09-17 12:49:38 +0000691
692// All the Atoms and References are created. Now update each Reference's
693// target with the Atom pointer it refers to.
694 for (auto &ri : _references) {
695 const Elf_Sym *Symbol = _objFile->getElfSymbol(ri->targetSymbolIndex());
696 ri->setTarget(findAtom (Symbol));
697 }
Sid Manning1a601412012-07-25 16:27:21 +0000698 }
699
700 virtual void addAtom(const Atom&) {
701 llvm_unreachable("cannot add atoms to native .o files");
702 }
703
704 virtual const atom_collection<DefinedAtom> &defined() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000705 return _definedAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000706 }
707
708 virtual const atom_collection<UndefinedAtom> &undefined() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000709 return _undefinedAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000710 }
711
712 virtual const atom_collection<SharedLibraryAtom> &sharedLibrary() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000713 return _sharedLibraryAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000714 }
715
716 virtual const atom_collection<AbsoluteAtom> &absolute() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000717 return _absoluteAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000718 }
719
Sid Manning8caf4de2012-09-17 12:49:38 +0000720 Atom *findAtom(const Elf_Sym *symbol) {
721 return (_symbolToAtomMapping.lookup(symbol));
722 }
723
724
Sid Manning1a601412012-07-25 16:27:21 +0000725private:
726 std::unique_ptr<llvm::object::ELFObjectFile<target_endianness, is64Bits> >
Sid Manning8caf4de2012-09-17 12:49:38 +0000727 _objFile;
728 atom_collection_vector<DefinedAtom> _definedAtoms;
729 atom_collection_vector<UndefinedAtom> _undefinedAtoms;
730 atom_collection_vector<SharedLibraryAtom> _sharedLibraryAtoms;
731 atom_collection_vector<AbsoluteAtom> _absoluteAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000732
Sid Manning8caf4de2012-09-17 12:49:38 +0000733/// \brief _relocationAddendRefences and _relocationReferences contain the list
734/// of relocations references. In ELF, if a section named, ".text" has
735/// relocations will also have a section named ".rel.text" or ".rela.text"
736/// which will hold the entries. -- .rel or .rela is prepended to create
737/// the SHT_REL(A) section name.
738///
739 std::map<llvm::StringRef, std::vector<const Elf_Rela *> >
740 _relocationAddendRefences;
741 std::map<llvm::StringRef, std::vector<const Elf_Rel *> >
742 _relocationReferences;
743
744 std::vector<ELFReference<target_endianness, is64Bits> *> _references;
745 llvm::DenseMap<const Elf_Sym *, Atom *> _symbolToAtomMapping;
746
747 llvm::BumpPtrAllocator _readerStorage;
Sid Manning1a601412012-07-25 16:27:21 +0000748};
749
750// ReaderELF is reader object that will instantiate correct FileELF
751// by examining the memory buffer for ELF class and bitwidth
752
753class ReaderELF: public Reader {
754public:
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000755 ReaderELF(const ReaderOptionsELF &readerELFOptions,
756 ReaderOptionsArchive &readerOptionsArchive)
757 : _readerELFOptions(readerELFOptions),
758 _readerOptionsArchive(readerOptionsArchive),
759 _readerArchive(_readerOptionsArchive) {
760 _readerOptionsArchive.setReader(this);
761 }
762
Sid Manning1a601412012-07-25 16:27:21 +0000763 error_code parseFile(std::unique_ptr<MemoryBuffer> mb, std::vector<
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000764 std::unique_ptr<File> > &result) {
765 llvm::error_code ec;
Sid Manning1a601412012-07-25 16:27:21 +0000766 std::unique_ptr<File> f;
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000767 std::pair<unsigned char, unsigned char> Ident;
Sid Manning1a601412012-07-25 16:27:21 +0000768
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000769 llvm::sys::LLVMFileType fileType =
770 llvm::sys::IdentifyFileType(mb->getBufferStart(),
771 static_cast<unsigned>(mb->getBufferSize()));
772 switch (fileType) {
Sid Manning1a601412012-07-25 16:27:21 +0000773
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000774 case llvm::sys::ELF_Relocatable_FileType:
Sid Manning1a601412012-07-25 16:27:21 +0000775
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000776 Ident = llvm::object::getElfArchType(&*mb);
777 // Instantiate the correct FileELF template instance
778 // based on the Ident pair. Once the File is created
779 // we push the file to the vector of files already
780 // created during parser's life.
Sid Manning1a601412012-07-25 16:27:21 +0000781
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000782 if (Ident.first == llvm::ELF::ELFCLASS32 && Ident.second
783 == llvm::ELF::ELFDATA2LSB) {
784 f.reset(new FileELF<llvm::support::little, false>(std::move(mb), ec));
785
786 } else if (Ident.first == llvm::ELF::ELFCLASS32 && Ident.second
787 == llvm::ELF::ELFDATA2MSB) {
788 f.reset(new FileELF<llvm::support::big, false> (std::move(mb), ec));
789
790 } else if (Ident.first == llvm::ELF::ELFCLASS64 && Ident.second
791 == llvm::ELF::ELFDATA2MSB) {
792 f.reset(new FileELF<llvm::support::big, true> (std::move(mb), ec));
793
794 } else if (Ident.first == llvm::ELF::ELFCLASS64 && Ident.second
795 == llvm::ELF::ELFDATA2LSB) {
796 f.reset(new FileELF<llvm::support::little, true> (std::move(mb), ec));
797 }
798 if (!ec)
799 result.push_back(std::move(f));
800 break;
801
802 case llvm::sys::Archive_FileType:
803 ec = _readerArchive.parseFile(std::move(mb), result);
804 break;
805
806 default:
807 llvm_unreachable("not supported format");
808 break;
Sid Manning1a601412012-07-25 16:27:21 +0000809 }
810
811 if (ec)
812 return ec;
813
Sid Manning1a601412012-07-25 16:27:21 +0000814 return error_code::success();
815 }
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000816
817private:
818 const ReaderOptionsELF &_readerELFOptions;
819 ReaderOptionsArchive &_readerOptionsArchive;
820 ReaderArchive _readerArchive;
Sid Manning1a601412012-07-25 16:27:21 +0000821};
822
823} // namespace anonymous
Nick Kledzikabb69812012-05-31 22:34:00 +0000824
825namespace lld {
826
827ReaderOptionsELF::ReaderOptionsELF() {
828}
829
830ReaderOptionsELF::~ReaderOptionsELF() {
831}
832
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000833Reader *createReaderELF(const ReaderOptionsELF &options,
834 ReaderOptionsArchive &optionsArchive) {
835 return new ReaderELF(options, optionsArchive);
Nick Kledzikabb69812012-05-31 22:34:00 +0000836}
837
Sid Manning1a601412012-07-25 16:27:21 +0000838} // namespace LLD