blob: cc0d0909ec17ccf9df167da170b635ccd6d1a21a [file] [log] [blame]
Michael J. Spencere68f9032013-01-29 22:03:39 +00001//===- lib/ReaderWriter/ELF/SectionChunks.h -------------------------------===//
Shankar Easwaran6d9921f2013-01-21 20:09:55 +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//===----------------------------------------------------------------------===//
9
Michael J. Spencere68f9032013-01-29 22:03:39 +000010#ifndef LLD_READER_WRITER_ELF_SECTION_CHUNKS_H
11#define LLD_READER_WRITER_ELF_SECTION_CHUNKS_H
Shankar Easwaran6d9921f2013-01-21 20:09:55 +000012
Michael J. Spencere68f9032013-01-29 22:03:39 +000013#include "Chunk.h"
14#include "Layout.h"
15#include "TargetHandler.h"
16#include "Writer.h"
Michael J. Spencer43ecac52013-01-29 19:53:41 +000017
Shankar Easwaran6d9921f2013-01-21 20:09:55 +000018#include "lld/Core/DefinedAtom.h"
19#include "lld/Core/range.h"
20
21#include "llvm/ADT/ArrayRef.h"
22#include "llvm/ADT/OwningPtr.h"
23#include "llvm/ADT/StringRef.h"
Shankar Easwaran6d9921f2013-01-21 20:09:55 +000024#include "llvm/Object/ELF.h"
Shankar Easwaran6d9921f2013-01-21 20:09:55 +000025#include "llvm/Support/Allocator.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/ELF.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/FileOutputBuffer.h"
30
Shankar Easwaran6d9921f2013-01-21 20:09:55 +000031namespace lld {
32namespace elf {
33
34/// \brief A section contains a set of atoms that have similiar properties
35/// The atoms that have similiar properties are merged to form a section
36template<class ELFT>
37class Section : public Chunk<ELFT> {
38public:
39 // The Kind of section that the object represents
40 enum SectionKind {
41 K_Default,
42 K_Target, // The section is handed over to the target
43 K_SymbolTable,
44 K_StringTable,
45 };
46 // Create a section object, the section is set to the default type if the
47 // caller doesnot set it
Michael J. Spencere68f9032013-01-29 22:03:39 +000048 Section(const ELFTargetInfo &, const StringRef sectionName,
Michael J. Spencerbf77be32013-01-29 01:07:47 +000049 const int32_t contentType, const int32_t contentPermissions,
50 const int32_t order, const SectionKind kind = K_Default);
Shankar Easwaran6d9921f2013-01-21 20:09:55 +000051
52 /// return the section kind
53 inline SectionKind sectionKind() const {
54 return _sectionKind;
55 }
56
57 /// Align the offset to the required modulus defined by the atom alignment
58 uint64_t alignOffset(uint64_t offset, DefinedAtom::Alignment &atomAlign);
59
60 // \brief Append an atom to a Section. The atom gets pushed into a vector
61 // contains the atom, the atom file offset, the atom virtual address
62 // the atom file offset is aligned appropriately as set by the Reader
Michael J. Spencer42606572013-01-30 01:25:06 +000063 const AtomLayout &appendAtom(const Atom *atom);
Shankar Easwaran6d9921f2013-01-21 20:09:55 +000064
65 /// \brief Set the virtual address of each Atom in the Section. This
66 /// routine gets called after the linker fixes up the virtual address
67 /// of the section
68 inline void assignVirtualAddress(uint64_t &addr) {
69 for (auto &ai : _atoms) {
Michael J. Spencer42606572013-01-30 01:25:06 +000070 ai->_virtualAddr = addr + ai->_fileOffset;
Shankar Easwaran6d9921f2013-01-21 20:09:55 +000071 }
72 addr += this->memSize();
73 }
74
75 /// \brief Set the file offset of each Atom in the section. This routine
76 /// gets called after the linker fixes up the section offset
77 inline void assignOffsets(uint64_t offset) {
78 for (auto &ai : _atoms) {
Michael J. Spencer42606572013-01-30 01:25:06 +000079 ai->_fileOffset = offset + ai->_fileOffset;
Shankar Easwaran6d9921f2013-01-21 20:09:55 +000080 }
81 }
82
83 /// \brief Find the Atom address given a name, this is needed to to properly
84 /// apply relocation. The section class calls this to find the atom address
85 /// to fix the relocation
Michael J. Spencere68f9032013-01-29 22:03:39 +000086 inline bool findAtomAddrByName(const StringRef name, uint64_t &addr) {
Shankar Easwaran6d9921f2013-01-21 20:09:55 +000087 for (auto ai : _atoms) {
Michael J. Spencer42606572013-01-30 01:25:06 +000088 if (ai->_atom->name() == name) {
89 addr = ai->_virtualAddr;
Shankar Easwaran6d9921f2013-01-21 20:09:55 +000090 return true;
91 }
92 }
93 return false;
94 }
95
96 /// \brief Does the Atom occupy any disk space
97 inline bool occupiesNoDiskSpace() const {
98 return _contentType == DefinedAtom::typeZeroFill;
99 }
100
101 /// \brief The permission of the section is the most permissive permission
102 /// of all atoms that the section contains
103 inline void setContentPermissions(int32_t perm) {
104 _contentPermissions = std::max(perm, _contentPermissions);
105 }
106
107 /// \brief Get the section flags, defined by the permissions of the section
108 int64_t flags();
109
110 /// \brief Return the section type, the returned value is recorded in the
111 /// sh_type field of the Section Header
112 int type();
113
114 /// \brief convert the segment type to a String for diagnostics
115 /// and printing purposes
Michael J. Spencere68f9032013-01-29 22:03:39 +0000116 StringRef segmentKindToStr() const;
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000117
118 /// \brief Return the raw flags, we need this to sort segments
119 inline int64_t atomflags() const {
120 return _contentPermissions;
121 }
122
123 /// \brief Returns the section link field, the returned value is
124 /// recorded in the sh_link field of the Section Header
125 inline int link() const {
126 return _link;
127 }
128
129 inline void setLink(int32_t link) {
130 _link = link;
131 }
132
133 /// \brief Returns the section entsize field, the returned value is
134 /// recorded in the sh_entsize field of the Section Header
135 inline int entsize() const {
136 return _entSize;
137 }
138
139 /// \brief Returns the shinfo field, the returned value is
140 /// recorded in the sh_info field of the Section Header
141 inline int shinfo() const {
142 return _shInfo;
143 }
144
145 /// \brief Records the segmentType, that this section belongs to
Michael J. Spencere68f9032013-01-29 22:03:39 +0000146 inline void setSegment(const Layout::SegmentType segmentType) {
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000147 _segmentType = segmentType;
148 }
149
150 /// \brief for LLVM style RTTI information
151 static inline bool classof(const Chunk<ELFT> *c) {
152 return c->kind() == Chunk<ELFT>::K_ELFSection;
153 }
154
155 /// \brief Finalize the section contents before writing
156 inline void finalize() { }
157
158 /// \brief Write the section and the atom contents to the buffer
Michael J. Spenceradfb7eb2013-01-29 01:00:21 +0000159 void write(ELFWriter *writer, llvm::FileOutputBuffer &buffer);
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000160
161 /// Atom Iterators
Michael J. Spencer42606572013-01-30 01:25:06 +0000162 typedef typename std::vector<AtomLayout *>::iterator atom_iter;
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000163
164 range<atom_iter> atoms() { return _atoms; }
165
166protected:
167 int32_t _contentType;
168 int32_t _contentPermissions;
169 SectionKind _sectionKind;
Michael J. Spencer42606572013-01-30 01:25:06 +0000170 std::vector<AtomLayout *> _atoms;
Michael J. Spencere68f9032013-01-29 22:03:39 +0000171 Layout::SegmentType _segmentType;
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000172 int64_t _entSize;
173 int64_t _shInfo;
174 int64_t _link;
Michael J. Spencer42606572013-01-30 01:25:06 +0000175 llvm::BumpPtrAllocator _alloc;
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000176};
177
178// Create a section object, the section is set to the default type if the
179// caller doesnot set it
Michael J. Spencerbf77be32013-01-29 01:07:47 +0000180template <class ELFT>
181Section<ELFT>::Section(const ELFTargetInfo &ti, const StringRef sectionName,
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000182 const int32_t contentType,
Michael J. Spencerbf77be32013-01-29 01:07:47 +0000183 const int32_t contentPermissions, const int32_t order,
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000184 const SectionKind kind)
Michael J. Spencerbf77be32013-01-29 01:07:47 +0000185 : Chunk<ELFT>(sectionName, Chunk<ELFT>::K_ELFSection, ti),
186 _contentType(contentType), _contentPermissions(contentPermissions),
187 _sectionKind(kind), _entSize(0), _shInfo(0), _link(0) {
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000188 this->setOrder(order);
189}
190
191/// Align the offset to the required modulus defined by the atom alignment
192template<class ELFT>
193uint64_t
194Section<ELFT>::alignOffset(uint64_t offset, DefinedAtom::Alignment &atomAlign) {
195 uint64_t requiredModulus = atomAlign.modulus;
196 uint64_t align2 = 1u << atomAlign.powerOf2;
197 uint64_t currentModulus = (offset % align2);
198 uint64_t retOffset = offset;
199 if (currentModulus != requiredModulus) {
200 if (requiredModulus > currentModulus)
201 retOffset += requiredModulus - currentModulus;
202 else
203 retOffset += align2 + requiredModulus - currentModulus;
204 }
205 return retOffset;
206}
207
208// \brief Append an atom to a Section. The atom gets pushed into a vector
209// contains the atom, the atom file offset, the atom virtual address
210// the atom file offset is aligned appropriately as set by the Reader
Michael J. Spencer42606572013-01-30 01:25:06 +0000211template <class ELFT>
212const AtomLayout &Section<ELFT>::appendAtom(const Atom *atom) {
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000213 Atom::Definition atomType = atom->definition();
214 const DefinedAtom *definedAtom = cast<DefinedAtom>(atom);
215
216 DefinedAtom::Alignment atomAlign = definedAtom->alignment();
217 uint64_t align2 = 1u << atomAlign.powerOf2;
218 // Align the atom to the required modulus/ align the file offset and the
219 // memory offset seperately this is required so that BSS symbols are handled
220 // properly as the BSS symbols only occupy memory size and not file size
221 uint64_t fOffset = alignOffset(this->fileSize(), atomAlign);
222 uint64_t mOffset = alignOffset(this->memSize(), atomAlign);
223 switch (atomType) {
224 case Atom::definitionRegular:
225 switch(definedAtom->contentType()) {
226 case DefinedAtom::typeCode:
227 case DefinedAtom::typeData:
228 case DefinedAtom::typeConstant:
Michael J. Spencer289dced2013-01-29 16:38:03 +0000229 case DefinedAtom::typeGOT:
230 case DefinedAtom::typeStub:
231 case DefinedAtom::typeResolver:
Michael J. Spencer42606572013-01-30 01:25:06 +0000232 _atoms.push_back(new (_alloc) AtomLayout(atom, fOffset, 0));
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000233 this->_fsize = fOffset + definedAtom->size();
234 this->_msize = mOffset + definedAtom->size();
Michael J. Spencer289dced2013-01-29 16:38:03 +0000235 DEBUG_WITH_TYPE("Section",
236 llvm::dbgs() << "[" << this->name() << " " << this << "] "
237 << "Adding atom: " << atom->name() << "@"
238 << fOffset << "\n");
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000239 break;
240 case DefinedAtom::typeZeroFill:
Michael J. Spencer42606572013-01-30 01:25:06 +0000241 _atoms.push_back(new (_alloc) AtomLayout(atom, mOffset, 0));
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000242 this->_msize = mOffset + definedAtom->size();
243 break;
244 default:
Michael J. Spencer42606572013-01-30 01:25:06 +0000245 llvm::dbgs() << definedAtom->contentType() << "\n";
246 llvm_unreachable("Uexpected content type.");
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000247 }
248 break;
249 default:
250 llvm_unreachable("Expecting only definedAtoms being passed here");
251 break;
252 }
253 // Set the section alignment to the largest alignment
254 // std::max doesnot support uint64_t
255 if (this->_align2 < align2)
256 this->_align2 = align2;
Michael J. Spencer42606572013-01-30 01:25:06 +0000257
258 return *_atoms.back();
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000259}
260
261/// \brief Get the section flags, defined by the permissions of the section
262template<class ELFT>
263int64_t
264Section<ELFT>::flags() {
265 switch (_contentPermissions) {
266 case DefinedAtom::perm___:
267 return 0;
268
269 case DefinedAtom::permR__:
270 return llvm::ELF::SHF_ALLOC;
271
272 case DefinedAtom::permR_X:
273 return llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_EXECINSTR;
274
275 case DefinedAtom::permRW_:
276 case DefinedAtom::permRW_L:
277 return llvm::ELF::SHF_ALLOC | llvm::ELF::SHF_WRITE;
278
279 case DefinedAtom::permRWX:
280 return llvm::ELF::SHF_ALLOC |
281 llvm::ELF::SHF_WRITE |
282 llvm::ELF::SHF_EXECINSTR;
283
284 default:
285 break;
286 }
287 return llvm::ELF::SHF_ALLOC;
288}
289
290/// \brief Return the section type, the returned value is recorded in the
291/// sh_type field of the Section Header
292
293template<class ELFT>
294int
295Section<ELFT>::type() {
Michael J. Spencer289dced2013-01-29 16:38:03 +0000296 if (_sectionKind == K_SymbolTable)
297 return llvm::ELF::SHT_SYMTAB;
298
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000299 switch (_contentType) {
300 case DefinedAtom::typeCode:
301 case DefinedAtom::typeData:
302 case DefinedAtom::typeConstant:
Michael J. Spencer289dced2013-01-29 16:38:03 +0000303 case DefinedAtom::typeGOT:
304 case DefinedAtom::typeStub:
305 case DefinedAtom::typeResolver:
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000306 return llvm::ELF::SHT_PROGBITS;
307
308 case DefinedAtom::typeZeroFill:
309 return llvm::ELF::SHT_NOBITS;
310
311 // Case to handle section types
312 // Symtab, String Table ...
313 default:
314 return _contentType;
315 }
316}
317
318/// \brief convert the segment type to a String for diagnostics
319/// and printing purposes
320template<class ELFT>
321StringRef
322Section<ELFT>::segmentKindToStr() const {
323 switch(_segmentType) {
324 case llvm::ELF::PT_INTERP:
325 return "INTERP";
326 case llvm::ELF::PT_LOAD:
327 return "LOAD";
328 case llvm::ELF::PT_GNU_EH_FRAME:
329 return "EH_FRAME";
330 case llvm::ELF::PT_NOTE:
331 return "NOTE";
332 case llvm::ELF::PT_DYNAMIC:
333 return "DYNAMIC";
334 case llvm::ELF::PT_GNU_RELRO:
335 return "RELRO";
336 case llvm::ELF::PT_NULL:
337 return "NULL";
338 default:
339 return "UNKNOWN";
340 }
341}
342
343/// \brief Write the section and the atom contents to the buffer
Michael J. Spenceradfb7eb2013-01-29 01:00:21 +0000344template <class ELFT>
345void Section<ELFT>::write(ELFWriter *writer, llvm::FileOutputBuffer &buffer) {
346 uint8_t *chunkBuffer = buffer.getBufferStart();
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000347 for (auto &ai : _atoms) {
Michael J. Spencer289dced2013-01-29 16:38:03 +0000348 DEBUG_WITH_TYPE("Section",
Michael J. Spencer42606572013-01-30 01:25:06 +0000349 llvm::dbgs() << "Writing atom: " << ai->_atom->name()
350 << " | " << ai->_fileOffset << "\n");
351 const DefinedAtom *definedAtom = cast<DefinedAtom>(ai->_atom);
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000352 if (definedAtom->contentType() == DefinedAtom::typeZeroFill)
353 continue;
354 // Copy raw content of atom to file buffer.
355 llvm::ArrayRef<uint8_t> content = definedAtom->rawContent();
356 uint64_t contentSize = content.size();
357 if (contentSize == 0)
358 continue;
Michael J. Spencer42606572013-01-30 01:25:06 +0000359 uint8_t *atomContent = chunkBuffer + ai->_fileOffset;
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000360 std::copy_n(content.data(), contentSize, atomContent);
361 for (const auto ref : *definedAtom) {
362 uint32_t offset = ref->offsetInAtom();
363 uint64_t targetAddress = 0;
364 assert(ref->target() != nullptr && "Found the target to be NULL");
365 targetAddress = writer->addressOfAtom(ref->target());
Michael J. Spencer42606572013-01-30 01:25:06 +0000366 uint64_t fixupAddress = writer->addressOfAtom(ai->_atom) + offset;
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000367 // apply the relocation
368 writer->kindHandler()->applyFixup(ref->kind(),
369 ref->addend(),
370 &atomContent[offset],
371 fixupAddress,
372 targetAddress);
373 }
374 }
375}
376
377/// \brief A MergedSections represents a set of sections grouped by the same
378/// name. The output file that gets written by the linker has sections grouped
379/// by similiar names
380template<class ELFT>
381class MergedSections {
382public:
383 // Iterators
384 typedef typename std::vector<Chunk<ELFT> *>::iterator ChunkIter;
385
Michael J. Spencere68f9032013-01-29 22:03:39 +0000386 MergedSections(StringRef name);
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000387
388 // Appends a section into the list of sections that are part of this Merged
389 // Section
390 void appendSection(Chunk<ELFT> *c);
391
392 // Set the MergedSections is associated with a segment
393 inline void setHasSegment() { _hasSegment = true; }
394
395 /// Sets the ordinal
396 inline void setOrdinal(uint64_t ordinal) {
397 _ordinal = ordinal;
398 }
399
400 /// Sets the Memory size
401 inline void setMemSize(uint64_t memsz) {
402 _memSize = memsz;
403 }
404
405 /// Sets the size fo the merged Section
406 inline void setSize(uint64_t fsiz) {
407 _size = fsiz;
408 }
409
410 // The offset of the first section contained in the merged section is
411 // contained here
412 inline void setFileOffset(uint64_t foffset) {
413 _fileOffset = foffset;
414 }
415
416 // Sets the starting address of the section
417 inline void setAddr(uint64_t addr) {
418 _virtualAddr = addr;
419 }
420
421 inline range<ChunkIter> sections() { return _sections; }
422
423 // The below functions returns the properties of the MergeSection
424 inline bool hasSegment() const { return _hasSegment; }
425
Michael J. Spencere68f9032013-01-29 22:03:39 +0000426 inline StringRef name() const { return _name; }
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000427
428 inline int64_t shinfo() const { return _shInfo; }
429
430 inline uint64_t align2() const { return _align2; }
431
432 inline int64_t link() const { return _link; }
433
434 inline int64_t type() const { return _type; }
435
436 inline uint64_t virtualAddr() const { return _virtualAddr; }
437
438 inline int64_t ordinal() const { return _ordinal; }
439
440 inline int64_t kind() const { return _kind; }
441
442 inline uint64_t fileSize() const { return _size; }
443
444 inline int64_t entsize() const { return _entSize; }
445
446 inline uint64_t fileOffset() const { return _fileOffset; }
447
448 inline int64_t flags() const { return _flags; }
449
450 inline uint64_t memSize() { return _memSize; }
451
452private:
Michael J. Spencere68f9032013-01-29 22:03:39 +0000453 StringRef _name;
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000454 bool _hasSegment;
455 uint64_t _ordinal;
456 int64_t _flags;
457 uint64_t _size;
458 uint64_t _memSize;
459 uint64_t _fileOffset;
460 uint64_t _virtualAddr;
461 int64_t _shInfo;
462 int64_t _entSize;
463 int64_t _link;
464 uint64_t _align2;
465 int64_t _kind;
466 int64_t _type;
467 std::vector<Chunk<ELFT> *> _sections;
468};
469
470/// MergedSections
471template<class ELFT>
472MergedSections<ELFT>::MergedSections(StringRef name)
473 : _name(name)
474 ,_hasSegment(false)
475 ,_ordinal(0)
476 ,_flags(0)
477 ,_size(0)
478 ,_memSize(0)
479 ,_fileOffset(0)
480 ,_virtualAddr(0)
481 ,_shInfo(0)
482 ,_entSize(0)
483 ,_link(0)
484 ,_align2(0)
485 ,_kind(0)
486 ,_type(0) { }
487
488
489template<class ELFT>
490void
491MergedSections<ELFT>::appendSection(Chunk<ELFT> *c) {
492 if (c->align2() > _align2)
493 _align2 = c->align2();
494 if (const auto section = dyn_cast<Section<ELFT>>(c)) {
495 _link = section->link();
496 _shInfo = section->shinfo();
497 _entSize = section->entsize();
498 _type = section->type();
499 if (_flags < section->flags())
500 _flags = section->flags();
501 }
502 _kind = c->kind();
503 _sections.push_back(c);
504}
505
506/// \brief The class represents the ELF String Table
507template<class ELFT>
Michael J. Spencere68f9032013-01-29 22:03:39 +0000508class StringTable : public Section<ELFT> {
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000509public:
Michael J. Spencere68f9032013-01-29 22:03:39 +0000510 StringTable(const ELFTargetInfo &, const char *str, int32_t order);
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000511
512 static inline bool classof(const Chunk<ELFT> *c) {
513 return c->kind() == Section<ELFT>::K_StringTable;
514 }
515
Michael J. Spencere68f9032013-01-29 22:03:39 +0000516 uint64_t addString(const StringRef symname);
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000517
Michael J. Spenceradfb7eb2013-01-29 01:00:21 +0000518 void write(ELFWriter *writer, llvm::FileOutputBuffer &buffer);
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000519
520 inline void finalize() { }
521
522private:
Michael J. Spencere68f9032013-01-29 22:03:39 +0000523 std::vector<StringRef> _strings;
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000524};
525
Michael J. Spencerbf77be32013-01-29 01:07:47 +0000526template <class ELFT>
Michael J. Spencere68f9032013-01-29 22:03:39 +0000527StringTable<ELFT>::StringTable(const ELFTargetInfo &ti, const char *str,
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000528 int32_t order)
Michael J. Spencerbf77be32013-01-29 01:07:47 +0000529 : Section<ELFT>(ti, str, llvm::ELF::SHT_STRTAB, DefinedAtom::perm___, order,
530 Section<ELFT>::K_StringTable) {
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000531 // the string table has a NULL entry for which
532 // add an empty string
533 _strings.push_back("");
534 this->_fsize = 1;
535 this->_align2 = 1;
536 this->setOrder(order);
537}
538
539template<class ELFT>
540uint64_t
Michael J. Spencere68f9032013-01-29 22:03:39 +0000541StringTable<ELFT>::addString(const StringRef symname) {
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000542 _strings.push_back(symname);
543 uint64_t offset = this->_fsize;
544 this->_fsize += symname.size() + 1;
545 return offset;
546}
547
Michael J. Spenceradfb7eb2013-01-29 01:00:21 +0000548template <class ELFT>
Michael J. Spencere68f9032013-01-29 22:03:39 +0000549void StringTable<ELFT>::write(ELFWriter *writer,
Michael J. Spenceradfb7eb2013-01-29 01:00:21 +0000550 llvm::FileOutputBuffer &buffer) {
551 uint8_t *chunkBuffer = buffer.getBufferStart();
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000552 uint8_t *dest = chunkBuffer + this->fileOffset();
553 for (auto si : _strings) {
554 memcpy(dest, si.data(), si.size());
555 dest += si.size();
556 memcpy(dest, "", 1);
557 dest += 1;
558 }
559}
560
Michael J. Spencere68f9032013-01-29 22:03:39 +0000561/// \brief The SymbolTable class represents the symbol table in a ELF file
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000562template<class ELFT>
Michael J. Spencere68f9032013-01-29 22:03:39 +0000563class SymbolTable : public Section<ELFT> {
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000564public:
565 typedef llvm::object::Elf_Sym_Impl<ELFT> Elf_Sym;
566
Michael J. Spencere68f9032013-01-29 22:03:39 +0000567 SymbolTable(const ELFTargetInfo &ti, const char *str, int32_t order);
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000568
569 void addSymbol(const Atom *atom, int32_t sectionIndex, uint64_t addr = 0);
570
571 void finalize();
572
Michael J. Spenceradfb7eb2013-01-29 01:00:21 +0000573 void write(ELFWriter *writer, llvm::FileOutputBuffer &buffer);
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000574
575 static inline bool classof(const Chunk<ELFT> *c) {
576 return c->kind() == Section<ELFT>::K_SymbolTable;
577 }
578
Michael J. Spencere68f9032013-01-29 22:03:39 +0000579 inline void setStringSection(StringTable<ELFT> *s) {
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000580 _stringSection = s;
581 }
582
583private:
Michael J. Spencere68f9032013-01-29 22:03:39 +0000584 StringTable<ELFT> *_stringSection;
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000585 std::vector<Elf_Sym*> _symbolTable;
586 llvm::BumpPtrAllocator _symbolAllocate;
587 int64_t _link;
588};
589
590/// ELF Symbol Table
Michael J. Spencerbf77be32013-01-29 01:07:47 +0000591template <class ELFT>
Michael J. Spencere68f9032013-01-29 22:03:39 +0000592SymbolTable<ELFT>::SymbolTable(const ELFTargetInfo &ti, const char *str,
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000593 int32_t order)
Michael J. Spencerbf77be32013-01-29 01:07:47 +0000594 : Section<ELFT>(ti, str, llvm::ELF::SHT_SYMTAB, 0, order,
595 Section<ELFT>::K_SymbolTable) {
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000596 this->setOrder(order);
597 Elf_Sym *symbol = new (_symbolAllocate.Allocate<Elf_Sym>()) Elf_Sym;
598 memset((void *)symbol, 0, sizeof(Elf_Sym));
599 _symbolTable.push_back(symbol);
600 this->_entSize = sizeof(Elf_Sym);
601 this->_fsize = sizeof(Elf_Sym);
602 this->_align2 = sizeof(void *);
603}
604
605template<class ELFT>
606void
Michael J. Spencere68f9032013-01-29 22:03:39 +0000607SymbolTable<ELFT>::addSymbol(const Atom *atom,
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000608 int32_t sectionIndex,
609 uint64_t addr) {
610 Elf_Sym *symbol = new(_symbolAllocate.Allocate<Elf_Sym>()) Elf_Sym;
611 unsigned char binding = 0, type = 0;
612 symbol->st_name = _stringSection->addString(atom->name());
613 symbol->st_size = 0;
614 symbol->st_shndx = sectionIndex;
615 symbol->st_value = 0;
616 symbol->st_other = llvm::ELF::STV_DEFAULT;
617 if (const DefinedAtom *da = dyn_cast<const DefinedAtom>(atom)){
618 symbol->st_size = da->size();
Michael J. Spencere68f9032013-01-29 22:03:39 +0000619 DefinedAtom::ContentType ct;
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000620 switch (ct = da->contentType()){
Michael J. Spencere68f9032013-01-29 22:03:39 +0000621 case DefinedAtom::typeCode:
Michael J. Spencer289dced2013-01-29 16:38:03 +0000622 case DefinedAtom::typeStub:
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000623 symbol->st_value = addr;
624 type = llvm::ELF::STT_FUNC;
625 break;
Michael J. Spencer289dced2013-01-29 16:38:03 +0000626 case DefinedAtom::typeResolver:
627 symbol->st_value = addr;
628 type = llvm::ELF::STT_GNU_IFUNC;
629 break;
Michael J. Spencere68f9032013-01-29 22:03:39 +0000630 case DefinedAtom::typeData:
631 case DefinedAtom::typeConstant:
Michael J. Spencer289dced2013-01-29 16:38:03 +0000632 case DefinedAtom::typeGOT:
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000633 symbol->st_value = addr;
634 type = llvm::ELF::STT_OBJECT;
635 break;
Michael J. Spencere68f9032013-01-29 22:03:39 +0000636 case DefinedAtom::typeZeroFill:
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000637 type = llvm::ELF::STT_OBJECT;
638 symbol->st_value = addr;
639 break;
640 default:
641 type = llvm::ELF::STT_NOTYPE;
642 }
643 if (da->scope() == DefinedAtom::scopeTranslationUnit)
644 binding = llvm::ELF::STB_LOCAL;
645 else
646 binding = llvm::ELF::STB_GLOBAL;
647 } else if (const AbsoluteAtom *aa = dyn_cast<const AbsoluteAtom>(atom)){
648 type = llvm::ELF::STT_OBJECT;
649 symbol->st_shndx = llvm::ELF::SHN_ABS;
650 switch (aa->scope()) {
651 case AbsoluteAtom::scopeLinkageUnit:
652 symbol->st_other = llvm::ELF::STV_HIDDEN;
653 binding = llvm::ELF::STB_LOCAL;
654 break;
655 case AbsoluteAtom::scopeTranslationUnit:
656 binding = llvm::ELF::STB_LOCAL;
657 break;
658 case AbsoluteAtom::scopeGlobal:
659 binding = llvm::ELF::STB_GLOBAL;
660 break;
661 }
662 symbol->st_value = addr;
663 } else {
664 symbol->st_value = 0;
665 type = llvm::ELF::STT_NOTYPE;
666 binding = llvm::ELF::STB_WEAK;
667 }
668 symbol->setBindingAndType(binding, type);
669 _symbolTable.push_back(symbol);
670 this->_fsize += sizeof(Elf_Sym);
671}
672
673template<class ELFT>
674void
Michael J. Spencere68f9032013-01-29 22:03:39 +0000675SymbolTable<ELFT>::finalize() {
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000676 // sh_info should be one greater than last symbol with STB_LOCAL binding
677 // we sort the symbol table to keep all local symbols at the beginning
678 std::stable_sort(_symbolTable.begin(), _symbolTable.end(),
679 [](const Elf_Sym *A, const Elf_Sym *B) {
680 return A->getBinding() < B->getBinding();
681 });
682 uint16_t shInfo = 0;
683 for (auto i : _symbolTable) {
684 if (i->getBinding() != llvm::ELF::STB_LOCAL)
685 break;
686 shInfo++;
687 }
688 this->_shInfo = shInfo;
689 this->setLink(_stringSection->ordinal());
690}
691
Michael J. Spenceradfb7eb2013-01-29 01:00:21 +0000692template <class ELFT>
Michael J. Spencere68f9032013-01-29 22:03:39 +0000693void SymbolTable<ELFT>::write(ELFWriter *writer,
Michael J. Spenceradfb7eb2013-01-29 01:00:21 +0000694 llvm::FileOutputBuffer &buffer) {
695 uint8_t *chunkBuffer = buffer.getBufferStart();
Shankar Easwaran6d9921f2013-01-21 20:09:55 +0000696 uint8_t *dest = chunkBuffer + this->fileOffset();
697 for (auto sti : _symbolTable) {
698 memcpy(dest, sti, sizeof(Elf_Sym));
699 dest += sizeof(Elf_Sym);
700 }
701}
702
Michael J. Spencere68f9032013-01-29 22:03:39 +0000703template <class ELFT> class RelocationTable : public Section<ELFT> {
Michael J. Spencer289dced2013-01-29 16:38:03 +0000704public:
705 typedef llvm::object::Elf_Rel_Impl<ELFT, true> Elf_Rela;
706
Michael J. Spencere68f9032013-01-29 22:03:39 +0000707 RelocationTable(const ELFTargetInfo &ti, StringRef str, int32_t order)
Michael J. Spencer289dced2013-01-29 16:38:03 +0000708 : Section<ELFT>(ti, str, llvm::ELF::SHT_RELA, DefinedAtom::permR__, order,
709 Section<ELFT>::K_Default) {
710 this->setOrder(order);
711 this->_entSize = sizeof(Elf_Rela);
712 this->_align2 = llvm::alignOf<Elf_Rela>();
713 }
714
715 void addRelocation(const DefinedAtom &da, const Reference &r) {
716 _relocs.emplace_back(da, r);
717 this->_fsize = _relocs.size() * sizeof(Elf_Rela);
718 this->_msize = this->_fsize;
719 }
720
721 void write(ELFWriter *writer, llvm::FileOutputBuffer &buffer) {
722 uint8_t *chunkBuffer = buffer.getBufferStart();
723 uint8_t *dest = chunkBuffer + this->fileOffset();
724 for (const auto &rel : _relocs) {
725 Elf_Rela *r = reinterpret_cast<Elf_Rela *>(dest);
726 r->setSymbolAndType(0, rel.second.kind());
727 r->r_offset =
728 writer->addressOfAtom(&rel.first) + rel.second.offsetInAtom();
729 r->r_addend =
730 writer->addressOfAtom(rel.second.target()) + rel.second.addend();
731 dest += sizeof(Elf_Rela);
732 DEBUG_WITH_TYPE("ELFRelocationTable", llvm::dbgs()
733 << "IRELATIVE relocation at " << rel.first.name() << "@"
734 << r->r_offset << " to " << rel.second.target()->name()
735 << "@" << r->r_addend << "\n");
736 }
737 }
738
739private:
740 std::vector<std::pair<const DefinedAtom &, const Reference &>> _relocs;
741};
Michael J. Spencere68f9032013-01-29 22:03:39 +0000742} // end namespace elf
743} // end namespace lld
Michael J. Spencer289dced2013-01-29 16:38:03 +0000744
Michael J. Spencere68f9032013-01-29 22:03:39 +0000745#endif