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