Rafael Espindola | 9d06ab6 | 2015-09-22 00:01:39 +0000 | [diff] [blame] | 1 | //===- InputSection.h -------------------------------------------*- C++ -*-===// |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
| 9 | |
Rafael Espindola | 9d06ab6 | 2015-09-22 00:01:39 +0000 | [diff] [blame] | 10 | #ifndef LLD_ELF_INPUT_SECTION_H |
| 11 | #define LLD_ELF_INPUT_SECTION_H |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 12 | |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame^] | 13 | #include "Config.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 14 | #include "lld/Core/LLVM.h" |
| 15 | #include "llvm/Object/ELF.h" |
| 16 | |
| 17 | namespace lld { |
| 18 | namespace elf2 { |
| 19 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 20 | template <class ELFT> class ObjectFile; |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 21 | template <class ELFT> class OutputSection; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 22 | template <class ELFT> class OutputSectionBase; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 23 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 24 | // This corresponds to a section of an input file. |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 25 | template <class ELFT> class InputSectionBase { |
| 26 | protected: |
Rafael Espindola | 7d4038dc | 2015-09-15 12:43:09 +0000 | [diff] [blame] | 27 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 28 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
Rafael Espindola | c5c8291 | 2015-08-24 19:28:31 +0000 | [diff] [blame] | 29 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 30 | const Elf_Shdr *Header; |
| 31 | |
| 32 | // The file this section is from. |
| 33 | ObjectFile<ELFT> *File; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 34 | |
| 35 | public: |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 36 | enum Kind { Regular, Merge }; |
| 37 | Kind SectionKind; |
| 38 | |
| 39 | InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header, |
| 40 | Kind SectionKind); |
| 41 | OutputSectionBase<ELFT> *OutSec = nullptr; |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 42 | |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame^] | 43 | // Used for garbage collection. |
| 44 | // Live bit makes sense only when Config->GcSections is true. |
| 45 | bool isLive() const { return !Config->GcSections || Live; } |
| 46 | bool Live = false; |
| 47 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 48 | // Returns the size of this section (even if this is a common or BSS.) |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 49 | size_t getSize() const { return Header->sh_size; } |
| 50 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 51 | static InputSectionBase<ELFT> Discarded; |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 52 | |
Rafael Espindola | 5d83ccd | 2015-08-13 19:18:30 +0000 | [diff] [blame] | 53 | StringRef getSectionName() const; |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 54 | const Elf_Shdr *getSectionHdr() const { return Header; } |
Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 55 | ObjectFile<ELFT> *getFile() const { return File; } |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 56 | |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 57 | // The writer sets and uses the addresses. |
Michael J. Spencer | baae538 | 2015-09-05 00:25:33 +0000 | [diff] [blame] | 58 | uintX_t getAlign() { |
| 59 | // The ELF spec states that a value of 0 means the section has no alignment |
| 60 | // constraits. |
| 61 | return std::max<uintX_t>(Header->sh_addralign, 1); |
| 62 | } |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 63 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 64 | uintX_t getOffset(const Elf_Sym &Sym) const; |
| 65 | ArrayRef<uint8_t> getSectionData() const; |
| 66 | }; |
| 67 | |
| 68 | template <class ELFT> |
| 69 | InputSectionBase<ELFT> |
| 70 | InputSectionBase<ELFT>::Discarded(nullptr, nullptr, |
| 71 | InputSectionBase<ELFT>::Regular); |
| 72 | |
| 73 | // This corresponds to a SHF_MERGE section of an input file. |
| 74 | template <class ELFT> class MergeInputSection : public InputSectionBase<ELFT> { |
| 75 | typedef InputSectionBase<ELFT> Base; |
| 76 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
| 77 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 78 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 79 | |
| 80 | public: |
| 81 | MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header); |
| 82 | static bool classof(const InputSectionBase<ELFT> *S); |
| 83 | uintX_t getOffset(uintX_t Offset) const; |
| 84 | }; |
| 85 | |
| 86 | // This corresponds to a non SHF_MERGE section of an input file. |
| 87 | template <class ELFT> class InputSection : public InputSectionBase<ELFT> { |
| 88 | typedef InputSectionBase<ELFT> Base; |
| 89 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr; |
| 90 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela; |
| 91 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel; |
| 92 | typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym; |
| 93 | typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t; |
| 94 | |
| 95 | public: |
| 96 | InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header); |
| 97 | |
| 98 | // Write this section to a mmap'ed file, assuming Buf is pointing to |
| 99 | // beginning of the output section. |
| 100 | void writeTo(uint8_t *Buf); |
| 101 | |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 102 | // Relocation sections that refer to this one. |
| 103 | SmallVector<const Elf_Shdr *, 1> RelocSections; |
| 104 | |
Rui Ueyama | edffd91 | 2015-10-14 21:00:23 +0000 | [diff] [blame] | 105 | // The offset from beginning of the output sections this section was assigned |
| 106 | // to. The writer sets a value. |
Rui Ueyama | 55c3f89 | 2015-10-15 01:58:40 +0000 | [diff] [blame] | 107 | uint64_t OutSecOff = 0; |
Rui Ueyama | edffd91 | 2015-10-14 21:00:23 +0000 | [diff] [blame] | 108 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 109 | static bool classof(const InputSectionBase<ELFT> *S); |
Rafael Espindola | 444576d | 2015-10-09 19:25:07 +0000 | [diff] [blame] | 110 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 111 | private: |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 112 | template <bool isRela> |
Hal Finkel | 87bbd5f | 2015-10-12 21:19:18 +0000 | [diff] [blame] | 113 | void relocate(uint8_t *Buf, uint8_t *BufEnd, |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 114 | llvm::iterator_range< |
| 115 | const llvm::object::Elf_Rel_Impl<ELFT, isRela> *> Rels, |
Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 116 | const ObjectFile<ELFT> &File, uintX_t BaseAddr); |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 119 | } // namespace elf2 |
| 120 | } // namespace lld |
| 121 | |
| 122 | #endif |