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" |
Rui Ueyama | 0fcdc73 | 2016-05-24 20:24:43 +0000 | [diff] [blame] | 14 | #include "Relocations.h" |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 15 | #include "Thunks.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 16 | #include "lld/Core/LLVM.h" |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/DenseSet.h" |
Rui Ueyama | c00718f | 2016-02-23 03:34:37 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/TinyPtrVector.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 19 | #include "llvm/Object/ELF.h" |
Rui Ueyama | 77f2a87 | 2016-11-18 05:05:43 +0000 | [diff] [blame] | 20 | #include <mutex> |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 21 | |
| 22 | namespace lld { |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 23 | namespace elf { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 24 | |
Rafael Espindola | e7553e4 | 2016-08-31 13:28:33 +0000 | [diff] [blame] | 25 | class DefinedCommon; |
Rafael Espindola | 38c67a2 | 2016-04-15 14:41:56 +0000 | [diff] [blame] | 26 | class SymbolBody; |
Rafael Espindola | 32aca87 | 2016-10-05 18:40:00 +0000 | [diff] [blame] | 27 | struct SectionPiece; |
Rafael Espindola | 38c67a2 | 2016-04-15 14:41:56 +0000 | [diff] [blame] | 28 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 29 | template <class ELFT> class DefinedRegular; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 30 | template <class ELFT> class ObjectFile; |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 31 | template <class ELFT> class OutputSection; |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 32 | class OutputSectionBase; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 33 | |
Eugene Leviant | 97403d1 | 2016-09-01 09:55:57 +0000 | [diff] [blame] | 34 | // We need non-template input section class to store symbol layout |
| 35 | // in linker script parser structures, where we do not have ELFT |
| 36 | // template parameter. For each scripted output section symbol we |
| 37 | // store pointer to preceding InputSectionData object or nullptr, |
| 38 | // if symbol should be placed at the very beginning of the output |
| 39 | // section |
| 40 | class InputSectionData { |
| 41 | public: |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 42 | enum Kind { Regular, EHFrame, Merge, Synthetic, }; |
Eugene Leviant | 97403d1 | 2016-09-01 09:55:57 +0000 | [diff] [blame] | 43 | |
| 44 | // The garbage collector sets sections' Live bits. |
| 45 | // If GC is disabled, all sections are considered live by default. |
Rafael Espindola | c7e1e03 | 2016-09-12 13:13:53 +0000 | [diff] [blame] | 46 | InputSectionData(Kind SectionKind, StringRef Name, ArrayRef<uint8_t> Data, |
| 47 | bool Compressed, bool Live) |
Rui Ueyama | f94efdd | 2016-11-20 23:15:52 +0000 | [diff] [blame] | 48 | : SectionKind(SectionKind), Live(Live), Assigned(false), |
| 49 | Compressed(Compressed), Name(Name), Data(Data) {} |
Eugene Leviant | 97403d1 | 2016-09-01 09:55:57 +0000 | [diff] [blame] | 50 | |
Rafael Espindola | 16853bb | 2016-09-08 12:33:41 +0000 | [diff] [blame] | 51 | private: |
| 52 | unsigned SectionKind : 3; |
Eugene Leviant | 97403d1 | 2016-09-01 09:55:57 +0000 | [diff] [blame] | 53 | |
Rafael Espindola | 16853bb | 2016-09-08 12:33:41 +0000 | [diff] [blame] | 54 | public: |
| 55 | Kind kind() const { return (Kind)SectionKind; } |
| 56 | |
Rui Ueyama | f94efdd | 2016-11-20 23:15:52 +0000 | [diff] [blame] | 57 | unsigned Live : 1; // for garbage collection |
| 58 | unsigned Assigned : 1; // for linker script |
| 59 | unsigned Compressed : 1; // true if section data is compressed |
Rafael Espindola | 16853bb | 2016-09-08 12:33:41 +0000 | [diff] [blame] | 60 | uint32_t Alignment; |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 61 | StringRef Name; |
Rafael Espindola | c7e1e03 | 2016-09-12 13:13:53 +0000 | [diff] [blame] | 62 | ArrayRef<uint8_t> Data; |
| 63 | |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 64 | template <typename T> llvm::ArrayRef<T> getDataAs() const { |
| 65 | size_t S = Data.size(); |
| 66 | assert(S % sizeof(T) == 0); |
| 67 | return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T)); |
| 68 | } |
| 69 | |
Rafael Espindola | 0a75850 | 2016-09-07 20:41:19 +0000 | [diff] [blame] | 70 | std::vector<Relocation> Relocations; |
Eugene Leviant | 97403d1 | 2016-09-01 09:55:57 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 73 | // This corresponds to a section of an input file. |
Eugene Leviant | 97403d1 | 2016-09-01 09:55:57 +0000 | [diff] [blame] | 74 | template <class ELFT> class InputSectionBase : public InputSectionData { |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 75 | protected: |
Rui Ueyama | 1d12ac1 | 2016-07-07 03:55:55 +0000 | [diff] [blame] | 76 | typedef typename ELFT::Chdr Elf_Chdr; |
Rafael Espindola | 197d6a8 | 2016-04-22 16:39:59 +0000 | [diff] [blame] | 77 | typedef typename ELFT::Rel Elf_Rel; |
| 78 | typedef typename ELFT::Rela Elf_Rela; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 79 | typedef typename ELFT::Shdr Elf_Shdr; |
| 80 | typedef typename ELFT::Sym Elf_Sym; |
| 81 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 82 | |
| 83 | // The file this section is from. |
| 84 | ObjectFile<ELFT> *File; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 85 | |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 86 | public: |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 87 | // These corresponds to the fields in Elf_Shdr. |
| 88 | uintX_t Flags; |
Eugene Leviant | c468120 | 2016-11-01 09:17:50 +0000 | [diff] [blame] | 89 | uintX_t Offset = 0; |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 90 | uintX_t Entsize; |
| 91 | uint32_t Type; |
| 92 | uint32_t Link; |
| 93 | uint32_t Info; |
| 94 | |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 95 | InputSectionBase() |
Rafael Espindola | c7e1e03 | 2016-09-12 13:13:53 +0000 | [diff] [blame] | 96 | : InputSectionData(Regular, "", ArrayRef<uint8_t>(), false, false), |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 97 | Repl(this) { |
| 98 | NumRelocations = 0; |
| 99 | AreRelocsRela = false; |
| 100 | } |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 101 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 102 | InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header, |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 103 | StringRef Name, Kind SectionKind); |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 104 | InputSectionBase(ObjectFile<ELFT> *File, uintX_t Flags, uint32_t Type, |
| 105 | uintX_t Entsize, uint32_t Link, uint32_t Info, |
| 106 | uintX_t Addralign, ArrayRef<uint8_t> Data, StringRef Name, |
| 107 | Kind SectionKind); |
Rafael Espindola | e08e78d | 2016-11-09 23:23:45 +0000 | [diff] [blame] | 108 | OutputSectionBase *OutSec = nullptr; |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 109 | |
Rafael Espindola | 9f0c4bb | 2016-11-10 14:53:24 +0000 | [diff] [blame] | 110 | // Relocations that refer to this section. |
| 111 | const Elf_Rel *FirstRelocation = nullptr; |
| 112 | unsigned NumRelocations : 31; |
| 113 | unsigned AreRelocsRela : 1; |
| 114 | ArrayRef<Elf_Rel> rels() const { |
| 115 | assert(!AreRelocsRela); |
| 116 | return llvm::makeArrayRef(FirstRelocation, NumRelocations); |
| 117 | } |
| 118 | ArrayRef<Elf_Rela> relas() const { |
| 119 | assert(AreRelocsRela); |
| 120 | return llvm::makeArrayRef(static_cast<const Elf_Rela *>(FirstRelocation), |
| 121 | NumRelocations); |
| 122 | } |
| 123 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 124 | // This pointer points to the "real" instance of this instance. |
| 125 | // Usually Repl == this. However, if ICF merges two sections, |
| 126 | // Repl pointer of one section points to another section. So, |
| 127 | // if you need to get a pointer to this instance, do not use |
| 128 | // this but instead this->Repl. |
| 129 | InputSectionBase<ELFT> *Repl; |
| 130 | |
Rafael Espindola | 1a54112 | 2016-11-08 14:47:16 +0000 | [diff] [blame] | 131 | // Returns the size of this section (even if this is a common or BSS.) |
| 132 | size_t getSize() const; |
| 133 | |
Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 134 | ObjectFile<ELFT> *getFile() const { return File; } |
Rafael Espindola | 77dbe9a | 2016-11-09 14:39:20 +0000 | [diff] [blame] | 135 | llvm::object::ELFFile<ELFT> getObj() const { return File->getObj(); } |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 136 | uintX_t getOffset(const DefinedRegular<ELFT> &Sym) const; |
Peter Smith | 0a259f3 | 2016-10-10 09:39:26 +0000 | [diff] [blame] | 137 | InputSectionBase *getLinkOrderDep() const; |
Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 138 | // Translate an offset in the input section to an offset in the output |
| 139 | // section. |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 140 | uintX_t getOffset(uintX_t Offset) const; |
Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 141 | |
George Rimar | 602fbee | 2016-06-24 11:18:44 +0000 | [diff] [blame] | 142 | void uncompress(); |
| 143 | |
Rui Ueyama | da06bfb | 2016-11-25 18:51:53 +0000 | [diff] [blame] | 144 | // Returns a source location string. Used to construct an error message. |
| 145 | std::string getLocation(uintX_t Offset); |
| 146 | |
Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 147 | void relocate(uint8_t *Buf, uint8_t *BufEnd); |
Rui Ueyama | 0538408 | 2016-10-12 22:36:31 +0000 | [diff] [blame] | 148 | |
| 149 | private: |
| 150 | std::pair<ArrayRef<uint8_t>, uint64_t> |
| 151 | getElfCompressedData(ArrayRef<uint8_t> Data); |
| 152 | |
| 153 | std::pair<ArrayRef<uint8_t>, uint64_t> |
| 154 | getRawCompressedData(ArrayRef<uint8_t> Data); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 157 | // SectionPiece represents a piece of splittable section contents. |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 158 | // We allocate a lot of these and binary search on them. This means that they |
| 159 | // have to be as compact as possible, which is why we don't store the size (can |
| 160 | // be found by looking at the next one) and put the hash in a side table. |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 161 | struct SectionPiece { |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 162 | SectionPiece(size_t Off, bool Live = false) |
| 163 | : InputOff(Off), OutputOff(-1), Live(Live || !Config->GcSections) {} |
Rui Ueyama | 34dc99e | 2016-05-22 01:15:32 +0000 | [diff] [blame] | 164 | |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 165 | size_t InputOff; |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 166 | ssize_t OutputOff : 8 * sizeof(ssize_t) - 1; |
Hans Wennborg | 7314c48 | 2016-10-20 15:59:08 +0000 | [diff] [blame] | 167 | size_t Live : 1; |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 168 | }; |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 169 | static_assert(sizeof(SectionPiece) == 2 * sizeof(size_t), |
| 170 | "SectionPiece is too big"); |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 171 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 172 | // This corresponds to a SHF_MERGE section of an input file. |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 173 | template <class ELFT> class MergeInputSection : public InputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 174 | typedef typename ELFT::uint uintX_t; |
| 175 | typedef typename ELFT::Sym Elf_Sym; |
| 176 | typedef typename ELFT::Shdr Elf_Shdr; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 177 | |
| 178 | public: |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 179 | MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, |
| 180 | StringRef Name); |
Rafael Espindola | 99558ef | 2016-10-26 18:44:57 +0000 | [diff] [blame] | 181 | static bool classof(const InputSectionData *S); |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 182 | void splitIntoPieces(); |
| 183 | |
| 184 | // Mark the piece at a given offset live. Used by GC. |
Rafael Espindola | 116d83f | 2016-10-19 23:13:40 +0000 | [diff] [blame] | 185 | void markLiveAt(uintX_t Offset) { |
Rafael Espindola | 1854a8e | 2016-10-26 12:36:56 +0000 | [diff] [blame] | 186 | assert(this->Flags & llvm::ELF::SHF_ALLOC); |
Rafael Espindola | 116d83f | 2016-10-19 23:13:40 +0000 | [diff] [blame] | 187 | LiveOffsets.insert(Offset); |
| 188 | } |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 189 | |
| 190 | // Translate an offset in the input section to an offset |
| 191 | // in the output section. |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 192 | uintX_t getOffset(uintX_t Offset) const; |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 193 | |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 194 | // Splittable sections are handled as a sequence of data |
| 195 | // rather than a single large blob of data. |
| 196 | std::vector<SectionPiece> Pieces; |
Rui Ueyama | 77f2a87 | 2016-11-18 05:05:43 +0000 | [diff] [blame] | 197 | llvm::CachedHashStringRef getData(size_t Idx) const; |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 198 | |
| 199 | // Returns the SectionPiece at a given input section offset. |
| 200 | SectionPiece *getSectionPiece(uintX_t Offset); |
| 201 | const SectionPiece *getSectionPiece(uintX_t Offset) const; |
| 202 | |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 203 | private: |
Rui Ueyama | e8a077b | 2016-11-26 15:15:11 +0000 | [diff] [blame^] | 204 | void splitStrings(ArrayRef<uint8_t> A, size_t Size); |
| 205 | void splitNonStrings(ArrayRef<uint8_t> A, size_t Size); |
Rui Ueyama | d6bd137 | 2016-08-03 04:39:42 +0000 | [diff] [blame] | 206 | |
Rui Ueyama | 77f2a87 | 2016-11-18 05:05:43 +0000 | [diff] [blame] | 207 | std::vector<uint32_t> Hashes; |
| 208 | |
| 209 | mutable llvm::DenseMap<uintX_t, uintX_t> OffsetMap; |
| 210 | mutable std::once_flag InitOffsetMap; |
| 211 | |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 212 | llvm::DenseSet<uintX_t> LiveOffsets; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 213 | }; |
| 214 | |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 215 | struct EhSectionPiece : public SectionPiece { |
Eugene Leviant | 531df4f | 2016-11-23 09:45:17 +0000 | [diff] [blame] | 216 | EhSectionPiece(size_t Off, InputSectionData *ID, uint32_t Size, |
| 217 | unsigned FirstRelocation) |
| 218 | : SectionPiece(Off, false), ID(ID), Size(Size), |
Rafael Espindola | 32aca87 | 2016-10-05 18:40:00 +0000 | [diff] [blame] | 219 | FirstRelocation(FirstRelocation) {} |
Eugene Leviant | 531df4f | 2016-11-23 09:45:17 +0000 | [diff] [blame] | 220 | InputSectionData *ID; |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 221 | uint32_t Size; |
| 222 | uint32_t size() const { return Size; } |
| 223 | |
Eugene Leviant | 531df4f | 2016-11-23 09:45:17 +0000 | [diff] [blame] | 224 | ArrayRef<uint8_t> data() { return {ID->Data.data() + this->InputOff, Size}; } |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 225 | unsigned FirstRelocation; |
| 226 | }; |
| 227 | |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 228 | // This corresponds to a .eh_frame section of an input file. |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 229 | template <class ELFT> class EhInputSection : public InputSectionBase<ELFT> { |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 230 | public: |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 231 | typedef typename ELFT::Shdr Elf_Shdr; |
| 232 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 233 | EhInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name); |
Rafael Espindola | 99558ef | 2016-10-26 18:44:57 +0000 | [diff] [blame] | 234 | static bool classof(const InputSectionData *S); |
Rui Ueyama | 88abd9b | 2016-05-22 23:53:00 +0000 | [diff] [blame] | 235 | void split(); |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 236 | template <class RelTy> void split(ArrayRef<RelTy> Rels); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 237 | |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 238 | // Splittable sections are handled as a sequence of data |
| 239 | // rather than a single large blob of data. |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 240 | std::vector<EhSectionPiece> Pieces; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 241 | }; |
| 242 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 243 | // This corresponds to a non SHF_MERGE section of an input file. |
| 244 | template <class ELFT> class InputSection : public InputSectionBase<ELFT> { |
| 245 | typedef InputSectionBase<ELFT> Base; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 246 | typedef typename ELFT::Shdr Elf_Shdr; |
| 247 | typedef typename ELFT::Rela Elf_Rela; |
| 248 | typedef typename ELFT::Rel Elf_Rel; |
| 249 | typedef typename ELFT::Sym Elf_Sym; |
| 250 | typedef typename ELFT::uint uintX_t; |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 251 | typedef InputSectionData::Kind Kind; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 252 | |
| 253 | public: |
Rafael Espindola | 6ff570a | 2016-11-09 16:55:07 +0000 | [diff] [blame] | 254 | InputSection(); |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 255 | InputSection(uintX_t Flags, uint32_t Type, uintX_t Addralign, |
Eugene Leviant | 41ca327 | 2016-11-10 09:48:29 +0000 | [diff] [blame] | 256 | ArrayRef<uint8_t> Data, StringRef Name, |
| 257 | Kind K = InputSectionData::Regular); |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 258 | InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 259 | |
Rafael Espindola | 6ff570a | 2016-11-09 16:55:07 +0000 | [diff] [blame] | 260 | static InputSection<ELFT> Discarded; |
| 261 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 262 | // Write this section to a mmap'ed file, assuming Buf is pointing to |
| 263 | // beginning of the output section. |
Rafael Espindola | 1a54112 | 2016-11-08 14:47:16 +0000 | [diff] [blame] | 264 | void writeTo(uint8_t *Buf); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 265 | |
Rui Ueyama | edffd91 | 2015-10-14 21:00:23 +0000 | [diff] [blame] | 266 | // The offset from beginning of the output sections this section was assigned |
| 267 | // to. The writer sets a value. |
Rui Ueyama | 55c3f89 | 2015-10-15 01:58:40 +0000 | [diff] [blame] | 268 | uint64_t OutSecOff = 0; |
Rui Ueyama | edffd91 | 2015-10-14 21:00:23 +0000 | [diff] [blame] | 269 | |
Peter Smith | 0760605 | 2016-10-10 10:10:27 +0000 | [diff] [blame] | 270 | // InputSection that is dependent on us (reverse dependency for GC) |
| 271 | InputSectionBase<ELFT> *DependentSection = nullptr; |
| 272 | |
Rafael Espindola | 99558ef | 2016-10-26 18:44:57 +0000 | [diff] [blame] | 273 | static bool classof(const InputSectionData *S); |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 274 | |
| 275 | InputSectionBase<ELFT> *getRelocatedSection(); |
| 276 | |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 277 | // Register thunk related to the symbol. When the section is written |
| 278 | // to a mmap'ed file, target is requested to write an actual thunk code. |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 279 | // Now thunks is supported for MIPS and ARM target only. |
| 280 | void addThunk(const Thunk<ELFT> *T); |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 281 | |
| 282 | // The offset of synthetic thunk code from beginning of this section. |
| 283 | uint64_t getThunkOff() const; |
| 284 | |
| 285 | // Size of chunk with thunks code. |
| 286 | uint64_t getThunksSize() const; |
| 287 | |
Rui Ueyama | 2b6fb80 | 2016-04-28 18:42:04 +0000 | [diff] [blame] | 288 | template <class RelTy> |
| 289 | void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); |
| 290 | |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 291 | // Used by ICF. |
| 292 | uint64_t GroupId = 0; |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 293 | |
| 294 | // Called by ICF to merge two input sections. |
| 295 | void replace(InputSection<ELFT> *Other); |
| 296 | |
Rui Ueyama | bd1f063 | 2016-11-20 02:39:59 +0000 | [diff] [blame] | 297 | private: |
| 298 | template <class RelTy> |
| 299 | void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 300 | |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 301 | llvm::TinyPtrVector<const Thunk<ELFT> *> Thunks; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 302 | }; |
| 303 | |
Rafael Espindola | 6ff570a | 2016-11-09 16:55:07 +0000 | [diff] [blame] | 304 | template <class ELFT> InputSection<ELFT> InputSection<ELFT>::Discarded; |
| 305 | |
Rui Ueyama | 3fc0f7e | 2016-11-23 18:07:33 +0000 | [diff] [blame] | 306 | template <class ELFT> std::string toString(const InputSectionBase<ELFT> *); |
| 307 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 308 | } // namespace elf |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 309 | } // namespace lld |
| 310 | |
| 311 | #endif |