blob: 35a4d81a07cb526a702888745818d979eee25af2 [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 Manninge861d432012-10-01 23:23:05 +0000297 switch (_section->sh_type) {
298 case llvm::ELF::SHT_PROGBITS:
299 case llvm::ELF::SHT_DYNAMIC:
300 switch (_section->sh_flags) {
Hemant Kulkarni736f7fb2012-11-21 21:07:36 +0000301 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR
302 | llvm::ELF::SHF_WRITE):
303 ret = typeCode;
304 break;
Sid Manninge861d432012-10-01 23:23:05 +0000305 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR):
306 ret = typeCode;
307 break;
308 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE):
309 ret = typeData;
310 break;
311 case llvm::ELF::SHF_ALLOC:
312 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_MERGE):
313 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_MERGE |
314 llvm::ELF::SHF_STRINGS):
315 ret = typeConstant;
316 break;
317 }
318 break;
319 case llvm::ELF::SHT_NOBITS:
320 ret = typeZeroFill;
321 break;
322 case llvm::ELF::SHT_NULL:
323 if ((_symbol->getType() == llvm::ELF::STT_COMMON)
324 || _symbol->st_shndx == llvm::ELF::SHN_COMMON)
325 ret = typeZeroFill;
326 break;
327 }
Sid Manning2a590242012-10-18 17:16:19 +0000328
Sid Manninge861d432012-10-01 23:23:05 +0000329 return ret;
Sid Manning1a601412012-07-25 16:27:21 +0000330 }
331
332 virtual Alignment alignment() const {
333
334 // Unallocated common symbols specify their alignment
335 // constraints in st_value.
Sid Manning8caf4de2012-09-17 12:49:38 +0000336 if ((_symbol->getType() == llvm::ELF::STT_COMMON)
337 || _symbol->st_shndx == llvm::ELF::SHN_COMMON) {
Sid Manning05c82a42012-10-03 21:46:48 +0000338 return Alignment(llvm::Log2_64(_symbol->st_value));
Sid Manning1a601412012-07-25 16:27:21 +0000339 }
Sid Manning8caf4de2012-09-17 12:49:38 +0000340 return Alignment(llvm::Log2_64(_section->sh_addralign));
Sid Manning1a601412012-07-25 16:27:21 +0000341 }
342
343 // Do we have a choice for ELF? All symbols
344 // live in explicit sections.
345 virtual SectionChoice sectionChoice() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000346 if (_symbol->st_shndx > llvm::ELF::SHN_LORESERVE)
Sid Manning1a601412012-07-25 16:27:21 +0000347 return sectionBasedOnContent;
348
349 return sectionCustomRequired;
350 }
351
352 virtual llvm::StringRef customSectionName() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000353 return _sectionName;
Sid Manning1a601412012-07-25 16:27:21 +0000354 }
355
356 // It isn't clear that __attribute__((used)) is transmitted to
357 // the ELF object file.
358 virtual DeadStripKind deadStrip() const {
359 return deadStripNormal;
360 }
361
362 virtual ContentPermissions permissions() const {
363
Sid Manning8caf4de2012-09-17 12:49:38 +0000364 switch (_section->sh_type) {
Sid Manning1a601412012-07-25 16:27:21 +0000365 // permRW_L is for sections modified by the runtime
366 // loader.
367 case llvm::ELF::SHT_REL:
368 case llvm::ELF::SHT_RELA:
369 return permRW_L;
370
371 case llvm::ELF::SHT_DYNAMIC:
372 case llvm::ELF::SHT_PROGBITS:
Sid Manning8caf4de2012-09-17 12:49:38 +0000373 switch (_section->sh_flags) {
Sid Manning1a601412012-07-25 16:27:21 +0000374
375 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR):
376 return permR_X;
377
378 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE):
379 return permRW_;
380
381 case llvm::ELF::SHF_ALLOC:
382 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_MERGE):
383 case (llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_MERGE
384 | llvm::ELF::SHF_STRINGS):
385 return permR__;
386 }
387 default:
388 return perm___;
389 }
390 }
391
392 // Many non ARM architectures use ELF file format
393 // This not really a place to put a architecture
394 // specific method in an atom. A better approach is
395 // needed.
396 //
397 virtual bool isThumb() const {
398 return false;
399 }
400
401 // FIXME Not Sure if ELF supports alias atoms. Find out more.
402 virtual bool isAlias() const {
403 return false;
404 }
405
406 virtual llvm::ArrayRef<uint8_t> rawContent() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000407 return _contentData;
Sid Manning1a601412012-07-25 16:27:21 +0000408 }
409
Sid Manning8caf4de2012-09-17 12:49:38 +0000410 DefinedAtom::reference_iterator begin() const {
411 uintptr_t index = _referenceStartIndex;
412 const void *it = reinterpret_cast<const void*>(index);
413 return reference_iterator(*this, it);
Sid Manning1a601412012-07-25 16:27:21 +0000414 }
415
Sid Manning8caf4de2012-09-17 12:49:38 +0000416 DefinedAtom::reference_iterator end() const {
417 uintptr_t index = _referenceEndIndex;
418 const void *it = reinterpret_cast<const void*>(index);
419 return reference_iterator(*this, it);
420 }
421
422 const Reference *derefIterator(const void *It) const {
423 uintptr_t index = reinterpret_cast<uintptr_t>(It);
424 assert(index >= _referenceStartIndex);
425 assert(index < _referenceEndIndex);
426 return ((_referenceList)[index]);
427 }
428
429 void incrementIterator(const void*& It) const {
430 uintptr_t index = reinterpret_cast<uintptr_t>(It);
431 ++index;
432 It = reinterpret_cast<const void*>(index);
Sid Manning1a601412012-07-25 16:27:21 +0000433 }
434
435private:
Sid Manning1a601412012-07-25 16:27:21 +0000436
Sid Manning8caf4de2012-09-17 12:49:38 +0000437 const File &_owningFile;
438 llvm::StringRef _symbolName;
439 llvm::StringRef _sectionName;
440 const Elf_Sym *_symbol;
441 const Elf_Shdr *_section;
Sid Manning8caf4de2012-09-17 12:49:38 +0000442 // _contentData will hold the bits that make up the atom.
443 llvm::ArrayRef<uint8_t> _contentData;
Sid Manning1a601412012-07-25 16:27:21 +0000444
445 uint64_t _ordinal;
Sid Manning8caf4de2012-09-17 12:49:38 +0000446 unsigned int _referenceStartIndex;
447 unsigned int _referenceEndIndex;
448 std::vector<ELFReference<target_endianness, is64Bits> *> &_referenceList;
Sid Manning1a601412012-07-25 16:27:21 +0000449};
450
451
452// FileELF will read a binary, find out based on the symbol table contents
453// what kind of symbol it is and create corresponding atoms for it
454
455template<llvm::support::endianness target_endianness, bool is64Bits>
456class FileELF: public File {
457
Sid Manning8caf4de2012-09-17 12:49:38 +0000458 typedef llvm::object::Elf_Sym_Impl
459 <target_endianness, is64Bits> Elf_Sym;
460 typedef llvm::object::Elf_Shdr_Impl
461 <target_endianness, is64Bits> Elf_Shdr;
462 typedef llvm::object::Elf_Rel_Impl
463 <target_endianness, is64Bits, false> Elf_Rel;
464 typedef llvm::object::Elf_Rel_Impl
465 <target_endianness, is64Bits, true> Elf_Rela;
Sid Manning1a601412012-07-25 16:27:21 +0000466
467public:
468 FileELF(std::unique_ptr<llvm::MemoryBuffer> MB, llvm::error_code &EC) :
469 File(MB->getBufferIdentifier()) {
470
Sid Manning8caf4de2012-09-17 12:49:38 +0000471 llvm::OwningPtr<llvm::object::Binary> binaryFile;
472 EC = llvm::object::createBinary(MB.release(), binaryFile);
Sid Manning1a601412012-07-25 16:27:21 +0000473 if (EC)
474 return;
475
476 // Point Obj to correct class and bitwidth ELF object
Sid Manning8caf4de2012-09-17 12:49:38 +0000477 _objFile.reset(llvm::dyn_cast<llvm::object::ELFObjectFile<target_endianness,
478 is64Bits> >(binaryFile.get()));
Sid Manning1a601412012-07-25 16:27:21 +0000479
Sid Manning8caf4de2012-09-17 12:49:38 +0000480 if (!_objFile) {
Sid Manning1a601412012-07-25 16:27:21 +0000481 EC = make_error_code(llvm::object::object_error::invalid_file_type);
482 return;
483 }
484
Sid Manning8caf4de2012-09-17 12:49:38 +0000485 binaryFile.take();
Sid Manning1a601412012-07-25 16:27:21 +0000486
Sid Manning8caf4de2012-09-17 12:49:38 +0000487 std::map< const Elf_Shdr *, std::vector<const Elf_Sym *>> sectionSymbols;
Sid Manning1a601412012-07-25 16:27:21 +0000488
Sid Manning8caf4de2012-09-17 12:49:38 +0000489// Handle: SHT_REL and SHT_RELA sections:
490// Increment over the sections, when REL/RELA section types are
491// found add the contents to the RelocationReferences map.
492
493 llvm::object::section_iterator sit(_objFile->begin_sections());
494 llvm::object::section_iterator sie(_objFile->end_sections());
495 for (; sit != sie; sit.increment(EC)) {
496 if (EC)
497 return;
498
499 const Elf_Shdr *section = _objFile->getElfSection(sit);
500
501 if (section->sh_type == llvm::ELF::SHT_RELA) {
502 llvm::StringRef sectionName;
503 if ((EC = _objFile->getSectionName(section, sectionName)))
504 return;
505 // Get rid of the leading .rela so Atoms can use their own section
506 // name to find the relocs.
507 sectionName = sectionName.drop_front(5);
508
509 auto rai(_objFile->beginELFRela(section));
510 auto rae(_objFile->endELFRela(section));
511
512 auto &Ref = _relocationAddendRefences[sectionName];
513 for (; rai != rae; rai++) {
514 Ref.push_back(&*rai);
515 }
516 }
517
518 if (section->sh_type == llvm::ELF::SHT_REL) {
519 llvm::StringRef sectionName;
520 if ((EC = _objFile->getSectionName(section, sectionName)))
521 return;
522 // Get rid of the leading .rel so Atoms can use their own section
523 // name to find the relocs.
524 sectionName = sectionName.drop_front(4);
525
526 auto ri(_objFile->beginELFRel(section));
527 auto re(_objFile->endELFRel(section));
528
529 auto &Ref = _relocationReferences[sectionName];
530 for (; ri != re; ri++) {
531 Ref.push_back(&*ri);
532 }
533 }
534 }
535
536
537// Increment over all the symbols collecting atoms and symbol
538// names for later use.
539
540 llvm::object::symbol_iterator it(_objFile->begin_symbols());
541 llvm::object::symbol_iterator ie(_objFile->end_symbols());
Sid Manning1a601412012-07-25 16:27:21 +0000542
Sid Manning429a4bc2012-07-27 14:52:18 +0000543 for (; it != ie; it.increment(EC)) {
Sid Manning1a601412012-07-25 16:27:21 +0000544 if (EC)
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 if ((EC = it->getSection(sit)))
Sid Manning429a4bc2012-07-27 14:52:18 +0000548 return;
Sid Manning1a601412012-07-25 16:27:21 +0000549
Sid Manning8caf4de2012-09-17 12:49:38 +0000550 const Elf_Shdr *section = _objFile->getElfSection(sit);
551 const Elf_Sym *symbol = _objFile->getElfSymbol(it);
Sid Manning1a601412012-07-25 16:27:21 +0000552
Sid Manning8caf4de2012-09-17 12:49:38 +0000553 llvm::StringRef symbolName;
554 if ((EC = _objFile->getSymbolName(section, symbol, symbolName)))
Sid Manning429a4bc2012-07-27 14:52:18 +0000555 return;
Sid Manning1a601412012-07-25 16:27:21 +0000556
Sid Manning8caf4de2012-09-17 12:49:38 +0000557 if (symbol->st_shndx == llvm::ELF::SHN_ABS) {
Sid Manning1a601412012-07-25 16:27:21 +0000558 // Create an absolute atom.
Sid Manning8caf4de2012-09-17 12:49:38 +0000559 auto *newAtom = new (_readerStorage.Allocate
560 <ELFAbsoluteAtom<target_endianness, is64Bits> > ())
561 ELFAbsoluteAtom<target_endianness, is64Bits>
Sid Manning2a590242012-10-18 17:16:19 +0000562 (*this, symbolName, symbol, symbol->st_value);
Sid Manning1a601412012-07-25 16:27:21 +0000563
Sid Manning8caf4de2012-09-17 12:49:38 +0000564 _absoluteAtoms._atoms.push_back(newAtom);
565 _symbolToAtomMapping.insert(std::make_pair(symbol, newAtom));
566
567 } else if (symbol->st_shndx == llvm::ELF::SHN_UNDEF) {
Sid Manning1a601412012-07-25 16:27:21 +0000568 // Create an undefined atom.
Sid Manning8caf4de2012-09-17 12:49:38 +0000569 auto *newAtom = new (_readerStorage.Allocate
570 <ELFUndefinedAtom<target_endianness, is64Bits> > ())
571 ELFUndefinedAtom<target_endianness, is64Bits>
572 (*this, symbolName, symbol);
573
574 _undefinedAtoms._atoms.push_back(newAtom);
575 _symbolToAtomMapping.insert(std::make_pair(symbol, newAtom));
576
Sid Manning1a601412012-07-25 16:27:21 +0000577 } else {
578 // This is actually a defined symbol. Add it to its section's list of
579 // symbols.
Sid Manning8caf4de2012-09-17 12:49:38 +0000580 if (symbol->getType() == llvm::ELF::STT_NOTYPE
581 || symbol->getType() == llvm::ELF::STT_OBJECT
582 || symbol->getType() == llvm::ELF::STT_FUNC
583 || symbol->getType() == llvm::ELF::STT_SECTION
584 || symbol->getType() == llvm::ELF::STT_FILE
585 || symbol->getType() == llvm::ELF::STT_TLS
586 || symbol->getType() == llvm::ELF::STT_COMMON
587 || symbol->st_shndx == llvm::ELF::SHN_COMMON) {
588 sectionSymbols[section].push_back(symbol);
Sid Manning1a601412012-07-25 16:27:21 +0000589 }
590 else {
Sid Manning8caf4de2012-09-17 12:49:38 +0000591 llvm::errs() << "Unable to create atom for: " << symbolName << "\n";
Sid Manning1a601412012-07-25 16:27:21 +0000592 EC = llvm::object::object_error::parse_failed;
593 return;
594 }
595 }
596 }
597
Sid Manning8caf4de2012-09-17 12:49:38 +0000598 for (auto &i : sectionSymbols) {
599 auto &symbols = i.second;
600 llvm::StringRef symbolName;
601 llvm::StringRef sectionName;
Sid Manning1a601412012-07-25 16:27:21 +0000602 // Sort symbols by position.
Sid Manning8caf4de2012-09-17 12:49:38 +0000603 std::stable_sort(symbols.begin(), symbols.end(),
Sid Manning1a601412012-07-25 16:27:21 +0000604 // From ReaderCOFF.cpp:
605 // For some reason MSVC fails to allow the lambda in this context with
606 // a "illegal use of local type in type instantiation". MSVC is clearly
607 // wrong here. Force a conversion to function pointer to work around.
608 static_cast<bool(*)(const Elf_Sym*, const Elf_Sym*)>(
609 [](const Elf_Sym *A, const Elf_Sym *B) -> bool {
610 return A->st_value < B->st_value;
611 }));
612
613 // i.first is the section the symbol lives in
Sid Manning8caf4de2012-09-17 12:49:38 +0000614 for (auto si = symbols.begin(), se = symbols.end(); si != se; ++si) {
Sid Manning1a601412012-07-25 16:27:21 +0000615
616 StringRef symbolContents;
Sid Manning8caf4de2012-09-17 12:49:38 +0000617 if ((EC = _objFile->getSectionContents(i.first, symbolContents)))
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->getSymbolName(i.first, *si, symbolName)))
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 if ((EC = _objFile->getSectionName(i.first, sectionName)))
Sid Manning429a4bc2012-07-27 14:52:18 +0000624 return;
Sid Manning1a601412012-07-25 16:27:21 +0000625
Sid Manning8caf4de2012-09-17 12:49:38 +0000626 bool isCommon = false;
Sid Manning1a601412012-07-25 16:27:21 +0000627 if (((*si)->getType() == llvm::ELF::STT_COMMON)
Sid Manning8caf4de2012-09-17 12:49:38 +0000628 || (*si)->st_shndx == llvm::ELF::SHN_COMMON)
629 isCommon = true;
Sid Manning1a601412012-07-25 16:27:21 +0000630
631 // Get the symbol's content:
Sid Manning8caf4de2012-09-17 12:49:38 +0000632 llvm::ArrayRef<uint8_t> symbolData;
633 uint64_t contentSize;
Sid Manning1a601412012-07-25 16:27:21 +0000634 if (si + 1 == se) {
635 // if this is the last symbol, take up the remaining data.
Sid Manning8caf4de2012-09-17 12:49:38 +0000636 contentSize = (isCommon) ? 0
637 : ((i.first)->sh_size - (*si)->st_value);
Sid Manning1a601412012-07-25 16:27:21 +0000638 }
639 else {
Sid Manning8caf4de2012-09-17 12:49:38 +0000640 contentSize = (isCommon) ? 0
641 : (*(si + 1))->st_value - (*si)->st_value;
Sid Manning1a601412012-07-25 16:27:21 +0000642 }
643
Sid Manning8caf4de2012-09-17 12:49:38 +0000644 symbolData = llvm::ArrayRef<uint8_t>((uint8_t *)symbolContents.data()
645 + (*si)->st_value, contentSize);
646
647
648 unsigned int referenceStart = _references.size();
649
650 // Only relocations that are inside the domain of the atom are
651 // added.
652
653 // Add Rela (those with r_addend) references:
654 for (auto &rai : _relocationAddendRefences[sectionName]) {
655 if ((rai->r_offset >= (*si)->st_value) &&
656 (rai->r_offset < (*si)->st_value+contentSize)) {
657
658 auto *ERef = new (_readerStorage.Allocate
659 <ELFReference<target_endianness, is64Bits> > ())
660 ELFReference<target_endianness, is64Bits> (
661 rai, rai->r_offset-(*si)->st_value, nullptr);
662
663 _references.push_back(ERef);
664 }
665 }
666
667 // Add Rel references:
668 for (auto &ri : _relocationReferences[sectionName]) {
669 if (((ri)->r_offset >= (*si)->st_value) &&
670 ((ri)->r_offset < (*si)->st_value+contentSize)) {
671
672 auto *ERef = new (_readerStorage.Allocate
673 <ELFReference<target_endianness, is64Bits> > ())
674 ELFReference<target_endianness, is64Bits> (
675 (ri), (ri)->r_offset-(*si)->st_value, nullptr);
676
677 _references.push_back(ERef);
678 }
679 }
680
681 // Create the DefinedAtom and add it to the list of DefinedAtoms.
682 auto *newAtom = new (_readerStorage.Allocate
683 <ELFDefinedAtom<target_endianness, is64Bits> > ())
684 ELFDefinedAtom<target_endianness, is64Bits>
685 (*this, symbolName, sectionName,
686 *si, i.first, symbolData,
687 referenceStart, _references.size(), _references);
688
689 _definedAtoms._atoms.push_back(newAtom);
690 _symbolToAtomMapping.insert(std::make_pair((*si), newAtom));
691
Sid Manning1a601412012-07-25 16:27:21 +0000692 }
693 }
Sid Manning8caf4de2012-09-17 12:49:38 +0000694
695// All the Atoms and References are created. Now update each Reference's
696// target with the Atom pointer it refers to.
697 for (auto &ri : _references) {
698 const Elf_Sym *Symbol = _objFile->getElfSymbol(ri->targetSymbolIndex());
699 ri->setTarget(findAtom (Symbol));
700 }
Sid Manning1a601412012-07-25 16:27:21 +0000701 }
702
703 virtual void addAtom(const Atom&) {
704 llvm_unreachable("cannot add atoms to native .o files");
705 }
706
707 virtual const atom_collection<DefinedAtom> &defined() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000708 return _definedAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000709 }
710
711 virtual const atom_collection<UndefinedAtom> &undefined() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000712 return _undefinedAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000713 }
714
715 virtual const atom_collection<SharedLibraryAtom> &sharedLibrary() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000716 return _sharedLibraryAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000717 }
718
719 virtual const atom_collection<AbsoluteAtom> &absolute() const {
Sid Manning8caf4de2012-09-17 12:49:38 +0000720 return _absoluteAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000721 }
722
Sid Manning8caf4de2012-09-17 12:49:38 +0000723 Atom *findAtom(const Elf_Sym *symbol) {
724 return (_symbolToAtomMapping.lookup(symbol));
725 }
726
727
Sid Manning1a601412012-07-25 16:27:21 +0000728private:
729 std::unique_ptr<llvm::object::ELFObjectFile<target_endianness, is64Bits> >
Sid Manning8caf4de2012-09-17 12:49:38 +0000730 _objFile;
731 atom_collection_vector<DefinedAtom> _definedAtoms;
732 atom_collection_vector<UndefinedAtom> _undefinedAtoms;
733 atom_collection_vector<SharedLibraryAtom> _sharedLibraryAtoms;
734 atom_collection_vector<AbsoluteAtom> _absoluteAtoms;
Sid Manning1a601412012-07-25 16:27:21 +0000735
Sid Manning8caf4de2012-09-17 12:49:38 +0000736/// \brief _relocationAddendRefences and _relocationReferences contain the list
737/// of relocations references. In ELF, if a section named, ".text" has
738/// relocations will also have a section named ".rel.text" or ".rela.text"
739/// which will hold the entries. -- .rel or .rela is prepended to create
740/// the SHT_REL(A) section name.
741///
742 std::map<llvm::StringRef, std::vector<const Elf_Rela *> >
743 _relocationAddendRefences;
744 std::map<llvm::StringRef, std::vector<const Elf_Rel *> >
745 _relocationReferences;
746
747 std::vector<ELFReference<target_endianness, is64Bits> *> _references;
748 llvm::DenseMap<const Elf_Sym *, Atom *> _symbolToAtomMapping;
749
750 llvm::BumpPtrAllocator _readerStorage;
Sid Manning1a601412012-07-25 16:27:21 +0000751};
752
753// ReaderELF is reader object that will instantiate correct FileELF
754// by examining the memory buffer for ELF class and bitwidth
755
756class ReaderELF: public Reader {
757public:
Michael J. Spencera5d22812012-11-13 19:58:58 +0000758 ReaderELF(const ReaderOptionsELF &,
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000759 ReaderOptionsArchive &readerOptionsArchive)
Michael J. Spencera5d22812012-11-13 19:58:58 +0000760 : _readerOptionsArchive(readerOptionsArchive)
761 , _readerArchive(_readerOptionsArchive) {
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000762 _readerOptionsArchive.setReader(this);
763 }
764
Sid Manning1a601412012-07-25 16:27:21 +0000765 error_code parseFile(std::unique_ptr<MemoryBuffer> mb, std::vector<
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000766 std::unique_ptr<File> > &result) {
767 llvm::error_code ec;
Sid Manning1a601412012-07-25 16:27:21 +0000768 std::unique_ptr<File> f;
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000769 std::pair<unsigned char, unsigned char> Ident;
Sid Manning1a601412012-07-25 16:27:21 +0000770
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000771 llvm::sys::LLVMFileType fileType =
772 llvm::sys::IdentifyFileType(mb->getBufferStart(),
773 static_cast<unsigned>(mb->getBufferSize()));
774 switch (fileType) {
Michael J. Spencera5d22812012-11-13 19:58:58 +0000775 case llvm::sys::ELF_Relocatable_FileType:
776 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
Michael J. Spencera5d22812012-11-13 19:58:58 +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));
Sid Manning1a601412012-07-25 16:27:21 +0000785
Michael J. Spencera5d22812012-11-13 19:58:58 +0000786 } 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));
Sid Manning1a601412012-07-25 16:27:21 +0000789
Michael J. Spencera5d22812012-11-13 19:58:58 +0000790 } 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));
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000793
Michael J. Spencera5d22812012-11-13 19:58:58 +0000794 } 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;
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000801
Michael J. Spencera5d22812012-11-13 19:58:58 +0000802 case llvm::sys::Archive_FileType:
803 ec = _readerArchive.parseFile(std::move(mb), result);
804 break;
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000805
Michael J. Spencera5d22812012-11-13 19:58:58 +0000806 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:
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000818 ReaderOptionsArchive &_readerOptionsArchive;
819 ReaderArchive _readerArchive;
Sid Manning1a601412012-07-25 16:27:21 +0000820};
821
822} // namespace anonymous
Nick Kledzikabb69812012-05-31 22:34:00 +0000823
824namespace lld {
825
826ReaderOptionsELF::ReaderOptionsELF() {
827}
828
829ReaderOptionsELF::~ReaderOptionsELF() {
830}
831
Shankar Easwaran70b4dcf2012-11-13 18:39:10 +0000832Reader *createReaderELF(const ReaderOptionsELF &options,
833 ReaderOptionsArchive &optionsArchive) {
834 return new ReaderELF(options, optionsArchive);
Nick Kledzikabb69812012-05-31 22:34:00 +0000835}
836
Sid Manning1a601412012-07-25 16:27:21 +0000837} // namespace LLD