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