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" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 15 | #include "lld/Core/LLVM.h" |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseSet.h" |
Rui Ueyama | c00718f | 2016-02-23 03:34:37 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/TinyPtrVector.h" |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 18 | #include "llvm/Object/ELF.h" |
| 19 | |
| 20 | namespace lld { |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 21 | namespace elf { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 22 | |
Rafael Espindola | 38c67a2 | 2016-04-15 14:41:56 +0000 | [diff] [blame] | 23 | class SymbolBody; |
| 24 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 25 | template <class ELFT> class ICF; |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 26 | template <class ELFT> class DefinedRegular; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 27 | template <class ELFT> class ObjectFile; |
Rafael Espindola | 832b93f | 2015-08-24 20:06:32 +0000 | [diff] [blame] | 28 | template <class ELFT> class OutputSection; |
Rui Ueyama | c7cc6ec | 2015-10-15 22:27:29 +0000 | [diff] [blame] | 29 | template <class ELFT> class OutputSectionBase; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 30 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 31 | // This corresponds to a section of an input file. |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 32 | template <class ELFT> class InputSectionBase { |
| 33 | protected: |
Rafael Espindola | 197d6a8 | 2016-04-22 16:39:59 +0000 | [diff] [blame] | 34 | typedef typename ELFT::Rel Elf_Rel; |
| 35 | typedef typename ELFT::Rela Elf_Rela; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 36 | typedef typename ELFT::Shdr Elf_Shdr; |
| 37 | typedef typename ELFT::Sym Elf_Sym; |
| 38 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 39 | const Elf_Shdr *Header; |
| 40 | |
| 41 | // The file this section is from. |
| 42 | ObjectFile<ELFT> *File; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 43 | |
| 44 | public: |
Simon Atanasyan | add74f3 | 2016-05-04 10:07:38 +0000 | [diff] [blame] | 45 | enum Kind { Regular, EHFrame, Merge, MipsReginfo, MipsOptions }; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 46 | Kind SectionKind; |
| 47 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 48 | InputSectionBase() : Repl(this) {} |
| 49 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 50 | InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header, |
| 51 | Kind SectionKind); |
| 52 | OutputSectionBase<ELFT> *OutSec = nullptr; |
Rui Ueyama | 6a9ef85 | 2016-02-25 18:49:09 +0000 | [diff] [blame] | 53 | uint32_t Align; |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 54 | |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 55 | // Used for garbage collection. |
Rui Ueyama | 6a9ef85 | 2016-02-25 18:49:09 +0000 | [diff] [blame] | 56 | bool Live; |
Rui Ueyama | c4aaed9 | 2015-10-22 18:49:53 +0000 | [diff] [blame] | 57 | |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 58 | // This pointer points to the "real" instance of this instance. |
| 59 | // Usually Repl == this. However, if ICF merges two sections, |
| 60 | // Repl pointer of one section points to another section. So, |
| 61 | // if you need to get a pointer to this instance, do not use |
| 62 | // this but instead this->Repl. |
| 63 | InputSectionBase<ELFT> *Repl; |
| 64 | |
Rafael Espindola | 7167585 | 2015-09-22 00:16:19 +0000 | [diff] [blame] | 65 | // 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] | 66 | size_t getSize() const; |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 67 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 68 | static InputSectionBase<ELFT> Discarded; |
Rafael Espindola | 83b0dc6 | 2015-08-13 22:21:37 +0000 | [diff] [blame] | 69 | |
Rafael Espindola | 5d83ccd | 2015-08-13 19:18:30 +0000 | [diff] [blame] | 70 | StringRef getSectionName() const; |
Michael J. Spencer | 8039dae2 | 2015-07-29 00:30:10 +0000 | [diff] [blame] | 71 | const Elf_Shdr *getSectionHdr() const { return Header; } |
Rafael Espindola | e1901cc | 2015-09-24 15:11:50 +0000 | [diff] [blame] | 72 | ObjectFile<ELFT> *getFile() const { return File; } |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 73 | uintX_t getOffset(const DefinedRegular<ELFT> &Sym); |
Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 74 | |
| 75 | // Translate an offset in the input section to an offset in the output |
| 76 | // section. |
| 77 | uintX_t getOffset(uintX_t Offset); |
| 78 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 79 | ArrayRef<uint8_t> getSectionData() const; |
Rui Ueyama | 1250464 | 2015-10-27 21:51:13 +0000 | [diff] [blame] | 80 | |
Rafael Espindola | 22ef956 | 2016-04-13 01:40:19 +0000 | [diff] [blame] | 81 | void relocate(uint8_t *Buf, uint8_t *BufEnd); |
| 82 | std::vector<Relocation> Relocations; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Rafael Espindola | ccfe3cb | 2016-04-04 14:04:16 +0000 | [diff] [blame] | 85 | template <class ELFT> InputSectionBase<ELFT> InputSectionBase<ELFT>::Discarded; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 86 | |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 87 | // SectionPiece represents a piece of splittable section contents. |
| 88 | struct SectionPiece { |
Rui Ueyama | 34dc99e | 2016-05-22 01:15:32 +0000 | [diff] [blame] | 89 | SectionPiece(size_t Off, ArrayRef<uint8_t> Data) |
| 90 | : InputOff(Off), Data(Data), Live(!Config->GcSections) {} |
| 91 | size_t size() const { return Data.size(); } |
| 92 | |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 93 | size_t InputOff; |
| 94 | size_t OutputOff = -1; |
Rui Ueyama | 34dc99e | 2016-05-22 01:15:32 +0000 | [diff] [blame] | 95 | ArrayRef<uint8_t> Data; // slice of the input section |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 96 | bool Live; |
| 97 | }; |
| 98 | |
Rui Ueyama | 5a32c76 | 2016-01-06 03:16:23 +0000 | [diff] [blame] | 99 | // Usually sections are copied to the output as atomic chunks of data, |
| 100 | // but some special types of sections are split into small pieces of data |
| 101 | // and each piece is copied to a different place in the output. |
| 102 | // This class represents such special sections. |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 103 | template <class ELFT> class SplitInputSection : public InputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 104 | typedef typename ELFT::Shdr Elf_Shdr; |
| 105 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 106 | |
| 107 | public: |
| 108 | SplitInputSection(ObjectFile<ELFT> *File, const Elf_Shdr *Header, |
| 109 | typename InputSectionBase<ELFT>::Kind SectionKind); |
Rui Ueyama | 5a32c76 | 2016-01-06 03:16:23 +0000 | [diff] [blame] | 110 | |
Rui Ueyama | 3ea8727 | 2016-05-22 00:13:04 +0000 | [diff] [blame] | 111 | // Splittable sections are handled as a sequence of data |
| 112 | // rather than a single large blob of data. |
| 113 | std::vector<SectionPiece> Pieces; |
Rui Ueyama | 5a32c76 | 2016-01-06 03:16:23 +0000 | [diff] [blame] | 114 | |
Rui Ueyama | 34dc99e | 2016-05-22 01:15:32 +0000 | [diff] [blame] | 115 | // Returns the SectionPiece at a given input section offset. |
Rui Ueyama | 90fa372 | 2016-05-22 00:41:38 +0000 | [diff] [blame] | 116 | SectionPiece *getSectionPiece(uintX_t Offset); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 119 | // This corresponds to a SHF_MERGE section of an input file. |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 120 | template <class ELFT> class MergeInputSection : public SplitInputSection<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 121 | typedef typename ELFT::uint uintX_t; |
| 122 | typedef typename ELFT::Sym Elf_Sym; |
| 123 | typedef typename ELFT::Shdr Elf_Shdr; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 124 | |
| 125 | public: |
| 126 | MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header); |
| 127 | static bool classof(const InputSectionBase<ELFT> *S); |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 128 | void splitIntoPieces(); |
| 129 | |
| 130 | // Mark the piece at a given offset live. Used by GC. |
| 131 | void markLiveAt(uintX_t Offset) { LiveOffsets.insert(Offset); } |
| 132 | |
| 133 | // Translate an offset in the input section to an offset |
| 134 | // in the output section. |
Rafael Espindola | 48225b4 | 2015-10-23 19:55:11 +0000 | [diff] [blame] | 135 | uintX_t getOffset(uintX_t Offset); |
Rui Ueyama | b91bf1a | 2016-05-23 16:55:43 +0000 | [diff] [blame] | 136 | |
| 137 | private: |
| 138 | llvm::DenseSet<uintX_t> LiveOffsets; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 141 | // This corresponds to a .eh_frame section of an input file. |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 142 | template <class ELFT> class EhInputSection : public SplitInputSection<ELFT> { |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 143 | public: |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 144 | typedef typename ELFT::Shdr Elf_Shdr; |
| 145 | typedef typename ELFT::uint uintX_t; |
Rui Ueyama | 0b9a903 | 2016-05-24 04:19:20 +0000 | [diff] [blame] | 146 | EhInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 147 | static bool classof(const InputSectionBase<ELFT> *S); |
Rui Ueyama | 88abd9b | 2016-05-22 23:53:00 +0000 | [diff] [blame] | 148 | void split(); |
Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 149 | |
| 150 | // Translate an offset in the input section to an offset in the output |
| 151 | // section. |
| 152 | uintX_t getOffset(uintX_t Offset); |
| 153 | |
| 154 | // Relocation section that refer to this one. |
| 155 | const Elf_Shdr *RelocSection = nullptr; |
| 156 | }; |
| 157 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 158 | // This corresponds to a non SHF_MERGE section of an input file. |
| 159 | template <class ELFT> class InputSection : public InputSectionBase<ELFT> { |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 160 | friend ICF<ELFT>; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 161 | typedef InputSectionBase<ELFT> Base; |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 162 | typedef typename ELFT::Shdr Elf_Shdr; |
| 163 | typedef typename ELFT::Rela Elf_Rela; |
| 164 | typedef typename ELFT::Rel Elf_Rel; |
| 165 | typedef typename ELFT::Sym Elf_Sym; |
| 166 | typedef typename ELFT::uint uintX_t; |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 167 | |
| 168 | public: |
| 169 | InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header); |
| 170 | |
| 171 | // Write this section to a mmap'ed file, assuming Buf is pointing to |
| 172 | // beginning of the output section. |
| 173 | void writeTo(uint8_t *Buf); |
| 174 | |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 175 | // Relocation sections that refer to this one. |
Rui Ueyama | c00718f | 2016-02-23 03:34:37 +0000 | [diff] [blame] | 176 | llvm::TinyPtrVector<const Elf_Shdr *> RelocSections; |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 177 | |
Rui Ueyama | edffd91 | 2015-10-14 21:00:23 +0000 | [diff] [blame] | 178 | // The offset from beginning of the output sections this section was assigned |
| 179 | // to. The writer sets a value. |
Rui Ueyama | 55c3f89 | 2015-10-15 01:58:40 +0000 | [diff] [blame] | 180 | uint64_t OutSecOff = 0; |
Rui Ueyama | edffd91 | 2015-10-14 21:00:23 +0000 | [diff] [blame] | 181 | |
Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 182 | static bool classof(const InputSectionBase<ELFT> *S); |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 183 | |
| 184 | InputSectionBase<ELFT> *getRelocatedSection(); |
| 185 | |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 186 | // Register thunk related to the symbol. When the section is written |
| 187 | // to a mmap'ed file, target is requested to write an actual thunk code. |
| 188 | // Now thunks is supported for MIPS target only. |
| 189 | void addThunk(SymbolBody &Body); |
| 190 | |
| 191 | // The offset of synthetic thunk code from beginning of this section. |
| 192 | uint64_t getThunkOff() const; |
| 193 | |
| 194 | // Size of chunk with thunks code. |
| 195 | uint64_t getThunksSize() const; |
| 196 | |
Rui Ueyama | 2b6fb80 | 2016-04-28 18:42:04 +0000 | [diff] [blame] | 197 | template <class RelTy> |
| 198 | void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); |
| 199 | |
George Rimar | 58941ee | 2016-02-25 08:23:37 +0000 | [diff] [blame] | 200 | private: |
Rui Ueyama | fc467e7 | 2016-03-13 05:06:50 +0000 | [diff] [blame] | 201 | template <class RelTy> |
Rafael Espindola | 0f7ccc3 | 2016-04-05 14:47:28 +0000 | [diff] [blame] | 202 | void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); |
Rui Ueyama | 0b28952 | 2016-02-25 18:43:51 +0000 | [diff] [blame] | 203 | |
| 204 | // Called by ICF to merge two input sections. |
| 205 | void replace(InputSection<ELFT> *Other); |
| 206 | |
| 207 | // Used by ICF. |
| 208 | uint64_t GroupId = 0; |
Simon Atanasyan | 13f6da1 | 2016-03-31 21:26:23 +0000 | [diff] [blame] | 209 | |
| 210 | llvm::TinyPtrVector<const SymbolBody *> Thunks; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 213 | // MIPS .reginfo section provides information on the registers used by the code |
| 214 | // in the object file. Linker should collect this information and write a single |
| 215 | // .reginfo section in the output file. The output section contains a union of |
| 216 | // used registers masks taken from input .reginfo sections and final value |
| 217 | // of the `_gp` symbol. For details: Chapter 4 / "Register Information" at |
| 218 | // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf |
| 219 | template <class ELFT> |
| 220 | class MipsReginfoInputSection : public InputSectionBase<ELFT> { |
Rui Ueyama | 9328b2c | 2016-03-14 23:16:09 +0000 | [diff] [blame] | 221 | typedef typename ELFT::Shdr Elf_Shdr; |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 222 | |
| 223 | public: |
Rui Ueyama | 70eed36 | 2016-01-06 22:42:43 +0000 | [diff] [blame] | 224 | MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr); |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 225 | static bool classof(const InputSectionBase<ELFT> *S); |
Rui Ueyama | 70eed36 | 2016-01-06 22:42:43 +0000 | [diff] [blame] | 226 | |
Simon Atanasyan | add74f3 | 2016-05-04 10:07:38 +0000 | [diff] [blame] | 227 | const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr; |
| 228 | }; |
| 229 | |
| 230 | template <class ELFT> |
| 231 | class MipsOptionsInputSection : public InputSectionBase<ELFT> { |
| 232 | typedef typename ELFT::Shdr Elf_Shdr; |
| 233 | |
| 234 | public: |
| 235 | MipsOptionsInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr); |
| 236 | static bool classof(const InputSectionBase<ELFT> *S); |
| 237 | |
| 238 | const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr; |
Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 239 | }; |
| 240 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 241 | } // namespace elf |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 242 | } // namespace lld |
| 243 | |
| 244 | #endif |