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" |
| 20 | |
| 21 | namespace lld { |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 22 | namespace elf { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 23 | |
Rafael Espindola | e7553e4 | 2016-08-31 13:28:33 +0000 | [diff] [blame] | 24 | class DefinedCommon; |
Rafael Espindola | 38c67a2 | 2016-04-15 14:41:56 +0000 | [diff] [blame] | 25 | class SymbolBody; |
Rafael Espindola | 32aca87 | 2016-10-05 18:40:00 +0000 | [diff] [blame] | 26 | struct SectionPiece; |
Rafael Espindola | 38c67a2 | 2016-04-15 14:41:56 +0000 | [diff] [blame] | 27 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 28 | template <class ELFT> class ICF; |
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; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 32 | template <class ELFT> 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: |
| 42 | enum Kind { Regular, EHFrame, Merge, MipsReginfo, MipsOptions, MipsAbiFlags }; |
| 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) |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 48 | : SectionKind(SectionKind), Live(Live), Compressed(Compressed), |
Rafael Espindola | c7e1e03 | 2016-09-12 13:13:53 +0000 | [diff] [blame] | 49 | 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 | 388838e | 2016-10-20 05:23:23 +0000 | [diff] [blame] | 57 | unsigned Live : 1; // for garbage collection |
Rafael Espindola | 16853bb | 2016-09-08 12:33:41 +0000 | [diff] [blame] | 58 | unsigned Compressed : 1; |
Rafael Espindola | 16853bb | 2016-09-08 12:33:41 +0000 | [diff] [blame] | 59 | uint32_t Alignment; |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 60 | StringRef Name; |
Rafael Espindola | c7e1e03 | 2016-09-12 13:13:53 +0000 | [diff] [blame] | 61 | ArrayRef<uint8_t> Data; |
| 62 | |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 63 | template <typename T> llvm::ArrayRef<T> getDataAs() const { |
| 64 | size_t S = Data.size(); |
| 65 | assert(S % sizeof(T) == 0); |
| 66 | return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T)); |
| 67 | } |
| 68 | |
Rafael Espindola | 54f1614 | 2016-09-12 13:06:10 +0000 | [diff] [blame] | 69 | // If a section is compressed, this has the uncompressed section data. |
Rui Ueyama | 0538408 | 2016-10-12 22:36:31 +0000 | [diff] [blame] | 70 | std::unique_ptr<uint8_t[]> UncompressedData; |
Rafael Espindola | 0a75850 | 2016-09-07 20:41:19 +0000 | [diff] [blame] | 71 | |
| 72 | std::vector<Relocation> Relocations; |
Eugene Leviant | 97403d1 | 2016-09-01 09:55:57 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 75 | // This corresponds to a section of an input file. |
Eugene Leviant | 97403d1 | 2016-09-01 09:55:57 +0000 | [diff] [blame] | 76 | template <class ELFT> class InputSectionBase : public InputSectionData { |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 77 | protected: |
Rui Ueyama | 1d12ac1 | 2016-07-07 03:55:55 +0000 | [diff] [blame] | 78 | typedef typename ELFT::Chdr Elf_Chdr; |
Rafael Espindola | 197d6a8 | 2016-04-22 16:39:59 +0000 | [diff] [blame] | 79 | typedef typename ELFT::Rel Elf_Rel; |
| 80 | typedef typename ELFT::Rela Elf_Rela; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 81 | typedef typename ELFT::Shdr Elf_Shdr; |
| 82 | typedef typename ELFT::Sym Elf_Sym; |
| 83 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 84 | |
| 85 | // The file this section is from. |
| 86 | ObjectFile<ELFT> *File; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 87 | |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 88 | // These corresponds to the fields in Elf_Shdr. |
| 89 | uintX_t Flags; |
| 90 | uintX_t Entsize; |
| 91 | uint32_t Type; |
| 92 | uint32_t Link; |
| 93 | uint32_t Info; |
| 94 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 95 | public: |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 96 | InputSectionBase() |
Rafael Espindola | c7e1e03 | 2016-09-12 13:13:53 +0000 | [diff] [blame] | 97 | : InputSectionData(Regular, "", ArrayRef<uint8_t>(), false, false), |
| 98 | Repl(this) {} |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 99 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 100 | InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header, |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 101 | StringRef Name, Kind SectionKind); |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 102 | InputSectionBase(ObjectFile<ELFT> *File, uintX_t Flags, uint32_t Type, |
| 103 | uintX_t Entsize, uint32_t Link, uint32_t Info, |
| 104 | uintX_t Addralign, ArrayRef<uint8_t> Data, StringRef Name, |
| 105 | Kind SectionKind); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 106 | OutputSectionBase<ELFT> *OutSec = nullptr; |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 107 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 108 | // This pointer points to the "real" instance of this instance. |
| 109 | // Usually Repl == this. However, if ICF merges two sections, |
| 110 | // Repl pointer of one section points to another section. So, |
| 111 | // if you need to get a pointer to this instance, do not use |
| 112 | // this but instead this->Repl. |
| 113 | InputSectionBase<ELFT> *Repl; |
| 114 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 115 | // Returns the size of this section (even if this is a common or BSS.) |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 116 | size_t getSize() const; |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 117 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 118 | static InputSectionBase<ELFT> Discarded; |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 119 | |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 120 | uintX_t getFlags() const { return Flags; } |
| 121 | uint32_t getType() const { return Type; } |
| 122 | uintX_t getEntsize() const { return Entsize; } |
| 123 | uint32_t getLink() const { return Link; } |
| 124 | uint32_t getInfo() const { return Info; } |
Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 125 | ObjectFile<ELFT> *getFile() const { return File; } |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 126 | uintX_t getOffset(const DefinedRegular<ELFT> &Sym) const; |
Peter Smith | 0a259f3 | 2016-10-10 09:39:26 +0000 | [diff] [blame] | 127 | InputSectionBase *getLinkOrderDep() const; |
Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 128 | // Translate an offset in the input section to an offset in the output |
| 129 | // section. |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 130 | uintX_t getOffset(uintX_t Offset) const; |
Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 131 | |
George Rimar | 602fbee | 2016-06-24 11:18:44 +0000 | [diff] [blame] | 132 | void uncompress(); |
| 133 | |
Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 134 | void relocate(uint8_t *Buf, uint8_t *BufEnd); |
Rui Ueyama | 0538408 | 2016-10-12 22:36:31 +0000 | [diff] [blame] | 135 | |
| 136 | private: |
| 137 | std::pair<ArrayRef<uint8_t>, uint64_t> |
| 138 | getElfCompressedData(ArrayRef<uint8_t> Data); |
| 139 | |
| 140 | std::pair<ArrayRef<uint8_t>, uint64_t> |
| 141 | getRawCompressedData(ArrayRef<uint8_t> Data); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 144 | template <class ELFT> InputSectionBase<ELFT> InputSectionBase<ELFT>::Discarded; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 145 | |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 146 | // SectionPiece represents a piece of splittable section contents. |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 147 | // We allocate a lot of these and binary search on them. This means that they |
| 148 | // have to be as compact as possible, which is why we don't store the size (can |
| 149 | // 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] | 150 | struct SectionPiece { |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 151 | SectionPiece(size_t Off, bool Live = false) |
| 152 | : InputOff(Off), OutputOff(-1), Live(Live || !Config->GcSections) {} |
Rui Ueyama | 34dc99e | 2016-05-22 01:15:32 +0000 | [diff] [blame] | 153 | |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 154 | size_t InputOff; |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 155 | ssize_t OutputOff : 8 * sizeof(ssize_t) - 1; |
Hans Wennborg | 7314c48 | 2016-10-20 15:59:08 +0000 | [diff] [blame] | 156 | size_t Live : 1; |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 157 | }; |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 158 | static_assert(sizeof(SectionPiece) == 2 * sizeof(size_t), |
| 159 | "SectionPiece is too big"); |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 160 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 161 | // This corresponds to a SHF_MERGE section of an input file. |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 162 | template <class ELFT> class MergeInputSection : public InputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 163 | typedef typename ELFT::uint uintX_t; |
| 164 | typedef typename ELFT::Sym Elf_Sym; |
| 165 | typedef typename ELFT::Shdr Elf_Shdr; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 166 | |
| 167 | public: |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 168 | MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, |
| 169 | StringRef Name); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 170 | static bool classof(const InputSectionBase<ELFT> *S); |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 171 | void splitIntoPieces(); |
| 172 | |
| 173 | // Mark the piece at a given offset live. Used by GC. |
Rafael Espindola | 116d83f | 2016-10-19 23:13:40 +0000 | [diff] [blame] | 174 | void markLiveAt(uintX_t Offset) { |
Rafael Espindola | 58139d1 | 2016-10-25 16:14:25 +0000 | [diff] [blame] | 175 | assert(this->getFlags() & llvm::ELF::SHF_ALLOC); |
Rafael Espindola | 116d83f | 2016-10-19 23:13:40 +0000 | [diff] [blame] | 176 | LiveOffsets.insert(Offset); |
| 177 | } |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 178 | |
| 179 | // Translate an offset in the input section to an offset |
| 180 | // in the output section. |
Rui Ueyama | 809d8e2 | 2016-06-23 04:33:42 +0000 | [diff] [blame] | 181 | uintX_t getOffset(uintX_t Offset) const; |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 182 | |
Rui Ueyama | 406b469 | 2016-05-27 14:39:13 +0000 | [diff] [blame] | 183 | void finalizePieces(); |
| 184 | |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 185 | // Splittable sections are handled as a sequence of data |
| 186 | // rather than a single large blob of data. |
| 187 | std::vector<SectionPiece> Pieces; |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 188 | ArrayRef<uint8_t> getData(std::vector<SectionPiece>::const_iterator I) const; |
| 189 | std::vector<uint32_t> Hashes; |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 190 | |
| 191 | // Returns the SectionPiece at a given input section offset. |
| 192 | SectionPiece *getSectionPiece(uintX_t Offset); |
| 193 | const SectionPiece *getSectionPiece(uintX_t Offset) const; |
| 194 | |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 195 | private: |
Rui Ueyama | d6bd137 | 2016-08-03 04:39:42 +0000 | [diff] [blame] | 196 | std::vector<SectionPiece> splitStrings(ArrayRef<uint8_t> A, size_t Size); |
| 197 | std::vector<SectionPiece> splitNonStrings(ArrayRef<uint8_t> A, size_t Size); |
| 198 | |
Rui Ueyama | 406b469 | 2016-05-27 14:39:13 +0000 | [diff] [blame] | 199 | llvm::DenseMap<uintX_t, uintX_t> OffsetMap; |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 200 | llvm::DenseSet<uintX_t> LiveOffsets; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 201 | }; |
| 202 | |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 203 | struct EhSectionPiece : public SectionPiece { |
| 204 | EhSectionPiece(size_t Off, ArrayRef<uint8_t> Data, unsigned FirstRelocation) |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 205 | : SectionPiece(Off, false), Data(Data.data()), Size(Data.size()), |
Rafael Espindola | 32aca87 | 2016-10-05 18:40:00 +0000 | [diff] [blame] | 206 | FirstRelocation(FirstRelocation) {} |
| 207 | const uint8_t *Data; |
Rafael Espindola | 113860b | 2016-10-20 10:55:58 +0000 | [diff] [blame] | 208 | uint32_t Size; |
| 209 | uint32_t size() const { return Size; } |
| 210 | |
| 211 | ArrayRef<uint8_t> data() { return {Data, Size}; } |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 212 | unsigned FirstRelocation; |
| 213 | }; |
| 214 | |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 215 | // This corresponds to a .eh_frame section of an input file. |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 216 | template <class ELFT> class EhInputSection : public InputSectionBase<ELFT> { |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 217 | public: |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 218 | typedef typename ELFT::Shdr Elf_Shdr; |
| 219 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 220 | EhInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 221 | static bool classof(const InputSectionBase<ELFT> *S); |
Rui Ueyama | 88abd9b | 2016-05-22 23:53:00 +0000 | [diff] [blame] | 222 | void split(); |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 223 | template <class RelTy> void split(ArrayRef<RelTy> Rels); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 224 | |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 225 | // Splittable sections are handled as a sequence of data |
| 226 | // rather than a single large blob of data. |
Rafael Espindola | 2deeb60 | 2016-07-21 20:18:30 +0000 | [diff] [blame] | 227 | std::vector<EhSectionPiece> Pieces; |
Rafael Espindola | 6eae9f2 | 2016-07-21 13:32:37 +0000 | [diff] [blame] | 228 | |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 229 | // Relocation section that refer to this one. |
| 230 | const Elf_Shdr *RelocSection = nullptr; |
| 231 | }; |
| 232 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 233 | // This corresponds to a non SHF_MERGE section of an input file. |
| 234 | template <class ELFT> class InputSection : public InputSectionBase<ELFT> { |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 235 | friend ICF<ELFT>; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 236 | typedef InputSectionBase<ELFT> Base; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 237 | typedef typename ELFT::Shdr Elf_Shdr; |
| 238 | typedef typename ELFT::Rela Elf_Rela; |
| 239 | typedef typename ELFT::Rel Elf_Rel; |
| 240 | typedef typename ELFT::Sym Elf_Sym; |
| 241 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 242 | |
| 243 | public: |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 244 | InputSection(uintX_t Flags, uint32_t Type, uintX_t Addralign, |
| 245 | ArrayRef<uint8_t> Data); |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 246 | InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name); |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 247 | |
| 248 | // Write this section to a mmap'ed file, assuming Buf is pointing to |
| 249 | // beginning of the output section. |
| 250 | void writeTo(uint8_t *Buf); |
| 251 | |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 252 | // Relocation sections that refer to this one. |
Rui Ueyama | c00718f | 2016-02-23 03:34:37 +0000 | [diff] [blame] | 253 | llvm::TinyPtrVector<const Elf_Shdr *> RelocSections; |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 254 | |
Rui Ueyama | edffd91 | 2015-10-14 21:00:23 +0000 | [diff] [blame] | 255 | // The offset from beginning of the output sections this section was assigned |
| 256 | // to. The writer sets a value. |
Rui Ueyama | 55c3f89 | 2015-10-15 01:58:40 +0000 | [diff] [blame] | 257 | uint64_t OutSecOff = 0; |
Rui Ueyama | edffd91 | 2015-10-14 21:00:23 +0000 | [diff] [blame] | 258 | |
Peter Smith | 0760605 | 2016-10-10 10:10:27 +0000 | [diff] [blame] | 259 | // InputSection that is dependent on us (reverse dependency for GC) |
| 260 | InputSectionBase<ELFT> *DependentSection = nullptr; |
| 261 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 262 | static bool classof(const InputSectionBase<ELFT> *S); |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 263 | |
| 264 | InputSectionBase<ELFT> *getRelocatedSection(); |
| 265 | |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 266 | // Register thunk related to the symbol. When the section is written |
| 267 | // 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] | 268 | // Now thunks is supported for MIPS and ARM target only. |
| 269 | void addThunk(const Thunk<ELFT> *T); |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 270 | |
| 271 | // The offset of synthetic thunk code from beginning of this section. |
| 272 | uint64_t getThunkOff() const; |
| 273 | |
| 274 | // Size of chunk with thunks code. |
| 275 | uint64_t getThunksSize() const; |
| 276 | |
Rui Ueyama | 2b6fb80 | 2016-04-28 18:42:04 +0000 | [diff] [blame] | 277 | template <class RelTy> |
| 278 | void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); |
| 279 | |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 280 | // Common symbols don't belong to any section. But it is easier for us |
| 281 | // to handle them as if they belong to some input section. So we defined |
| 282 | // this section that "contains" all common symbols. |
| 283 | static InputSection<ELFT> *CommonInputSection; |
| 284 | |
| 285 | static InputSection<ELFT> |
| 286 | createCommonInputSection(std::vector<DefinedCommon *> Syms); |
| 287 | |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 288 | private: |
Rui Ueyama | fc467e7 | 2016-03-13 05:06:50 +0000 | [diff] [blame] | 289 | template <class RelTy> |
Rafael Espindola | 0f7ccc3 | 2016-04-05 14:47:28 +0000 | [diff] [blame] | 290 | void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 291 | |
| 292 | // Called by ICF to merge two input sections. |
| 293 | void replace(InputSection<ELFT> *Other); |
| 294 | |
| 295 | // Used by ICF. |
| 296 | uint64_t GroupId = 0; |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 297 | |
Peter Smith | fb05cd9 | 2016-07-08 16:10:27 +0000 | [diff] [blame] | 298 | llvm::TinyPtrVector<const Thunk<ELFT> *> Thunks; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 299 | }; |
| 300 | |
Rafael Espindola | 0e09052 | 2016-10-26 00:54:03 +0000 | [diff] [blame] | 301 | template <class ELFT> |
| 302 | InputSection<ELFT> *InputSection<ELFT>::CommonInputSection; |
| 303 | |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 304 | // MIPS .reginfo section provides information on the registers used by the code |
| 305 | // in the object file. Linker should collect this information and write a single |
| 306 | // .reginfo section in the output file. The output section contains a union of |
| 307 | // used registers masks taken from input .reginfo sections and final value |
| 308 | // of the `_gp` symbol. For details: Chapter 4 / "Register Information" at |
| 309 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 310 | template <class ELFT> |
| 311 | class MipsReginfoInputSection : public InputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 312 | typedef typename ELFT::Shdr Elf_Shdr; |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 313 | |
| 314 | public: |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 315 | MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr, |
| 316 | StringRef Name); |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 317 | static bool classof(const InputSectionBase<ELFT> *S); |
Rui Ueyama | 70eed36 | 2016-01-06 22:42:43 +0000 | [diff] [blame] | 318 | |
Simon Atanasyan | add74f3 | 2016-05-04 10:07:38 +0000 | [diff] [blame] | 319 | const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr; |
| 320 | }; |
| 321 | |
| 322 | template <class ELFT> |
| 323 | class MipsOptionsInputSection : public InputSectionBase<ELFT> { |
| 324 | typedef typename ELFT::Shdr Elf_Shdr; |
| 325 | |
| 326 | public: |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 327 | MipsOptionsInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr, |
| 328 | StringRef Name); |
Simon Atanasyan | add74f3 | 2016-05-04 10:07:38 +0000 | [diff] [blame] | 329 | static bool classof(const InputSectionBase<ELFT> *S); |
| 330 | |
| 331 | const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr; |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 332 | }; |
| 333 | |
Simon Atanasyan | 85c6b44 | 2016-08-12 06:28:49 +0000 | [diff] [blame] | 334 | template <class ELFT> |
| 335 | class MipsAbiFlagsInputSection : public InputSectionBase<ELFT> { |
| 336 | typedef typename ELFT::Shdr Elf_Shdr; |
| 337 | |
| 338 | public: |
Rafael Espindola | 042a3f2 | 2016-09-08 14:06:08 +0000 | [diff] [blame] | 339 | MipsAbiFlagsInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr, |
| 340 | StringRef Name); |
Simon Atanasyan | 85c6b44 | 2016-08-12 06:28:49 +0000 | [diff] [blame] | 341 | static bool classof(const InputSectionBase<ELFT> *S); |
| 342 | |
| 343 | const llvm::object::Elf_Mips_ABIFlags<ELFT> *Flags = nullptr; |
| 344 | }; |
| 345 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 346 | } // namespace elf |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 347 | } // namespace lld |
| 348 | |
| 349 | #endif |