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