1 //===- InputSection.h -------------------------------------------*- C++ -*-===// 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 10 #ifndef LLD_ELF_INPUT_SECTION_H 11 #define LLD_ELF_INPUT_SECTION_H 12 13 #include "Config.h" 14 #include "lld/Core/LLVM.h" 15 #include "llvm/ADT/TinyPtrVector.h" 16 #include "llvm/Object/ELF.h" 17 18 namespace lld { 19 namespace elf { 20 21 class SymbolBody; 22 23 template <class ELFT> class ICF; 24 template <class ELFT> class DefinedRegular; 25 template <class ELFT> class ObjectFile; 26 template <class ELFT> class OutputSection; 27 template <class ELFT> class OutputSectionBase; 28 29 enum RelExpr { 30 R_ABS, 31 R_GOT, 32 R_GOTONLY_PC, 33 R_GOTREL, 34 R_GOT_FROM_END, 35 R_GOT_PAGE_PC, 36 R_GOT_PC, 37 R_MIPS_GOT, 38 R_MIPS_GOT_LOCAL, 39 R_MIPS_GP0, 40 R_NEG_TLS, 41 R_PAGE_PC, 42 R_PC, 43 R_PLT, 44 R_PLT_PC, 45 R_PPC_OPD, 46 R_PPC_PLT_OPD, 47 R_PPC_TOC, 48 R_RELAX_TLS_GD_TO_IE, 49 R_RELAX_TLS_GD_TO_IE_PC, 50 R_RELAX_TLS_GD_TO_LE, 51 R_RELAX_TLS_IE_TO_LE, 52 R_RELAX_TLS_LD_TO_LE, 53 R_SIZE, 54 R_THUNK, 55 R_TLS, 56 R_TLSGD, 57 R_TLSGD_PC, 58 R_TLSLD, 59 R_TLSLD_PC 60 }; 61 62 struct Relocation { 63 RelExpr Expr; 64 uint32_t Type; 65 uint64_t Offset; 66 uint64_t Addend; 67 SymbolBody *Sym; 68 }; 69 70 // This corresponds to a section of an input file. 71 template <class ELFT> class InputSectionBase { 72 protected: 73 typedef typename ELFT::Rel Elf_Rel; 74 typedef typename ELFT::Rela Elf_Rela; 75 typedef typename ELFT::Shdr Elf_Shdr; 76 typedef typename ELFT::Sym Elf_Sym; 77 typedef typename ELFT::uint uintX_t; 78 const Elf_Shdr *Header; 79 80 // The file this section is from. 81 ObjectFile<ELFT> *File; 82 83 public: 84 enum Kind { Regular, EHFrame, Merge, MipsReginfo }; 85 Kind SectionKind; 86 87 InputSectionBase() : Repl(this) {} 88 89 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header, 90 Kind SectionKind); 91 OutputSectionBase<ELFT> *OutSec = nullptr; 92 uint32_t Align; 93 94 // Used for garbage collection. 95 bool Live; 96 97 // This pointer points to the "real" instance of this instance. 98 // Usually Repl == this. However, if ICF merges two sections, 99 // Repl pointer of one section points to another section. So, 100 // if you need to get a pointer to this instance, do not use 101 // this but instead this->Repl. 102 InputSectionBase<ELFT> *Repl; 103 104 // Returns the size of this section (even if this is a common or BSS.) 105 size_t getSize() const; 106 107 static InputSectionBase<ELFT> Discarded; 108 109 StringRef getSectionName() const; 110 const Elf_Shdr *getSectionHdr() const { return Header; } 111 ObjectFile<ELFT> *getFile() const { return File; } 112 uintX_t getOffset(const DefinedRegular<ELFT> &Sym); 113 114 // Translate an offset in the input section to an offset in the output 115 // section. 116 uintX_t getOffset(uintX_t Offset); 117 118 ArrayRef<uint8_t> getSectionData() const; 119 120 // Returns a section that Rel is pointing to. Used by the garbage collector. 121 InputSectionBase<ELFT> *getRelocTarget(const Elf_Rel &Rel) const; 122 InputSectionBase<ELFT> *getRelocTarget(const Elf_Rela &Rel) const; 123 124 void relocate(uint8_t *Buf, uint8_t *BufEnd); 125 std::vector<Relocation> Relocations; 126 }; 127 128 template <class ELFT> InputSectionBase<ELFT> InputSectionBase<ELFT>::Discarded; 129 130 // Usually sections are copied to the output as atomic chunks of data, 131 // but some special types of sections are split into small pieces of data 132 // and each piece is copied to a different place in the output. 133 // This class represents such special sections. 134 template <class ELFT> class SplitInputSection : public InputSectionBase<ELFT> { 135 typedef typename ELFT::Shdr Elf_Shdr; 136 typedef typename ELFT::uint uintX_t; 137 138 public: 139 SplitInputSection(ObjectFile<ELFT> *File, const Elf_Shdr *Header, 140 typename InputSectionBase<ELFT>::Kind SectionKind); 141 142 // For each piece of data, we maintain the offsets in the input section and 143 // in the output section. The latter may be -1 if it is not assigned yet. 144 std::vector<std::pair<uintX_t, uintX_t>> Offsets; 145 146 std::pair<std::pair<uintX_t, uintX_t> *, uintX_t> 147 getRangeAndSize(uintX_t Offset); 148 }; 149 150 // This corresponds to a SHF_MERGE section of an input file. 151 template <class ELFT> class MergeInputSection : public SplitInputSection<ELFT> { 152 typedef typename ELFT::uint uintX_t; 153 typedef typename ELFT::Sym Elf_Sym; 154 typedef typename ELFT::Shdr Elf_Shdr; 155 156 public: 157 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header); 158 static bool classof(const InputSectionBase<ELFT> *S); 159 // Translate an offset in the input section to an offset in the output 160 // section. 161 uintX_t getOffset(uintX_t Offset); 162 }; 163 164 // This corresponds to a .eh_frame section of an input file. 165 template <class ELFT> class EHInputSection : public SplitInputSection<ELFT> { 166 public: 167 typedef typename ELFT::Shdr Elf_Shdr; 168 typedef typename ELFT::uint uintX_t; 169 EHInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header); 170 static bool classof(const InputSectionBase<ELFT> *S); 171 172 // Translate an offset in the input section to an offset in the output 173 // section. 174 uintX_t getOffset(uintX_t Offset); 175 176 // Relocation section that refer to this one. 177 const Elf_Shdr *RelocSection = nullptr; 178 }; 179 180 // This corresponds to a non SHF_MERGE section of an input file. 181 template <class ELFT> class InputSection : public InputSectionBase<ELFT> { 182 friend ICF<ELFT>; 183 typedef InputSectionBase<ELFT> Base; 184 typedef typename ELFT::Shdr Elf_Shdr; 185 typedef typename ELFT::Rela Elf_Rela; 186 typedef typename ELFT::Rel Elf_Rel; 187 typedef typename ELFT::Sym Elf_Sym; 188 typedef typename ELFT::uint uintX_t; 189 190 public: 191 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header); 192 193 // Write this section to a mmap'ed file, assuming Buf is pointing to 194 // beginning of the output section. 195 void writeTo(uint8_t *Buf); 196 197 // Relocation sections that refer to this one. 198 llvm::TinyPtrVector<const Elf_Shdr *> RelocSections; 199 200 // The offset from beginning of the output sections this section was assigned 201 // to. The writer sets a value. 202 uint64_t OutSecOff = 0; 203 204 static bool classof(const InputSectionBase<ELFT> *S); 205 206 InputSectionBase<ELFT> *getRelocatedSection(); 207 208 // Register thunk related to the symbol. When the section is written 209 // to a mmap'ed file, target is requested to write an actual thunk code. 210 // Now thunks is supported for MIPS target only. 211 void addThunk(SymbolBody &Body); 212 213 // The offset of synthetic thunk code from beginning of this section. 214 uint64_t getThunkOff() const; 215 216 // Size of chunk with thunks code. 217 uint64_t getThunksSize() const; 218 219 private: 220 template <class RelTy> 221 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); 222 223 // Called by ICF to merge two input sections. 224 void replace(InputSection<ELFT> *Other); 225 226 // Used by ICF. 227 uint64_t GroupId = 0; 228 229 llvm::TinyPtrVector<const SymbolBody *> Thunks; 230 }; 231 232 // MIPS .reginfo section provides information on the registers used by the code 233 // in the object file. Linker should collect this information and write a single 234 // .reginfo section in the output file. The output section contains a union of 235 // used registers masks taken from input .reginfo sections and final value 236 // of the `_gp` symbol. For details: Chapter 4 / "Register Information" at 237 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 238 template <class ELFT> 239 class MipsReginfoInputSection : public InputSectionBase<ELFT> { 240 typedef typename ELFT::Shdr Elf_Shdr; 241 242 public: 243 MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr); 244 static bool classof(const InputSectionBase<ELFT> *S); 245 246 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo; 247 }; 248 249 } // namespace elf 250 } // namespace lld 251 252 #endif 253