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 "Relocations.h" 15 #include "Thunks.h" 16 #include "lld/Core/LLVM.h" 17 #include "llvm/ADT/CachedHashString.h" 18 #include "llvm/ADT/DenseSet.h" 19 #include "llvm/ADT/TinyPtrVector.h" 20 #include "llvm/Object/ELF.h" 21 #include "llvm/Support/Threading.h" 22 #include <mutex> 23 24 namespace lld { 25 namespace elf { 26 27 class DefinedCommon; 28 class SymbolBody; 29 struct SectionPiece; 30 31 class DefinedRegular; 32 class SyntheticSection; 33 template <class ELFT> class EhFrameSection; 34 class MergeSyntheticSection; 35 template <class ELFT> class ObjectFile; 36 class OutputSection; 37 38 // This is the base class of all sections that lld handles. Some are sections in 39 // input files, some are sections in the produced output file and some exist 40 // just as a convenience for implementing special ways of combining some 41 // sections. 42 class SectionBase { 43 public: 44 enum Kind { Regular, EHFrame, Merge, Synthetic, Output }; 45 46 Kind kind() const { return (Kind)SectionKind; } 47 48 StringRef Name; 49 50 unsigned SectionKind : 3; 51 52 // The next two bit fields are only used by InputSectionBase, but we 53 // put them here so the struct packs better. 54 55 // The garbage collector sets sections' Live bits. 56 // If GC is disabled, all sections are considered live by default. 57 unsigned Live : 1; // for garbage collection 58 unsigned Assigned : 1; // for linker script 59 60 uint32_t Alignment; 61 62 // These corresponds to the fields in Elf_Shdr. 63 uint64_t Flags; 64 uint64_t Entsize; 65 uint32_t Type; 66 uint32_t Link; 67 uint32_t Info; 68 69 OutputSection *getOutputSection(); 70 const OutputSection *getOutputSection() const { 71 return const_cast<SectionBase *>(this)->getOutputSection(); 72 } 73 74 // Translate an offset in the input section to an offset in the output 75 // section. 76 uint64_t getOffset(uint64_t Offset) const; 77 78 uint64_t getOffset(const DefinedRegular &Sym) const; 79 80 protected: 81 SectionBase(Kind SectionKind, StringRef Name, uint64_t Flags, 82 uint64_t Entsize, uint64_t Alignment, uint32_t Type, 83 uint32_t Info, uint32_t Link) 84 : Name(Name), SectionKind(SectionKind), Alignment(Alignment), 85 Flags(Flags), Entsize(Entsize), Type(Type), Link(Link), Info(Info) { 86 Live = false; 87 Assigned = false; 88 } 89 }; 90 91 // This corresponds to a section of an input file. 92 class InputSectionBase : public SectionBase { 93 public: 94 static bool classof(const SectionBase *S); 95 96 // The file this section is from. 97 InputFile *File; 98 99 ArrayRef<uint8_t> Data; 100 uint64_t getOffsetInFile() const; 101 102 static InputSectionBase Discarded; 103 104 InputSectionBase() 105 : SectionBase(Regular, "", /*Flags*/ 0, /*Entsize*/ 0, /*Alignment*/ 0, 106 /*Type*/ 0, 107 /*Info*/ 0, /*Link*/ 0), 108 Repl(this) { 109 Live = false; 110 Assigned = false; 111 NumRelocations = 0; 112 AreRelocsRela = false; 113 } 114 115 template <class ELFT> 116 InputSectionBase(ObjectFile<ELFT> *File, const typename ELFT::Shdr *Header, 117 StringRef Name, Kind SectionKind); 118 119 InputSectionBase(InputFile *File, uint64_t Flags, uint32_t Type, 120 uint64_t Entsize, uint32_t Link, uint32_t Info, 121 uint32_t Alignment, ArrayRef<uint8_t> Data, StringRef Name, 122 Kind SectionKind); 123 124 // Input sections are part of an output section. Special sections 125 // like .eh_frame and merge sections are first combined into a 126 // synthetic section that is then added to an output section. In all 127 // cases this points one level up. 128 SectionBase *Parent = nullptr; 129 130 // Relocations that refer to this section. 131 const void *FirstRelocation = nullptr; 132 unsigned NumRelocations : 31; 133 unsigned AreRelocsRela : 1; 134 template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const { 135 assert(!AreRelocsRela); 136 return llvm::makeArrayRef( 137 static_cast<const typename ELFT::Rel *>(FirstRelocation), 138 NumRelocations); 139 } 140 template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const { 141 assert(AreRelocsRela); 142 return llvm::makeArrayRef( 143 static_cast<const typename ELFT::Rela *>(FirstRelocation), 144 NumRelocations); 145 } 146 147 // This pointer points to the "real" instance of this instance. 148 // Usually Repl == this. However, if ICF merges two sections, 149 // Repl pointer of one section points to another section. So, 150 // if you need to get a pointer to this instance, do not use 151 // this but instead this->Repl. 152 InputSectionBase *Repl; 153 154 // InputSections that are dependent on us (reverse dependency for GC) 155 llvm::TinyPtrVector<InputSectionBase *> DependentSections; 156 157 // Returns the size of this section (even if this is a common or BSS.) 158 size_t getSize() const; 159 160 template <class ELFT> ObjectFile<ELFT> *getFile() const; 161 162 template <class ELFT> llvm::object::ELFFile<ELFT> getObj() const { 163 return getFile<ELFT>()->getObj(); 164 } 165 166 InputSection *getLinkOrderDep() const; 167 168 void uncompress(); 169 170 // Returns a source location string. Used to construct an error message. 171 template <class ELFT> std::string getLocation(uint64_t Offset); 172 template <class ELFT> std::string getSrcMsg(uint64_t Offset); 173 template <class ELFT> std::string getObjMsg(uint64_t Offset); 174 175 template <class ELFT> void relocate(uint8_t *Buf, uint8_t *BufEnd); 176 void relocateAlloc(uint8_t *Buf, uint8_t *BufEnd); 177 template <class ELFT> void relocateNonAlloc(uint8_t *Buf, uint8_t *BufEnd); 178 179 std::vector<Relocation> Relocations; 180 181 template <typename T> llvm::ArrayRef<T> getDataAs() const { 182 size_t S = Data.size(); 183 assert(S % sizeof(T) == 0); 184 return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T)); 185 } 186 }; 187 188 // SectionPiece represents a piece of splittable section contents. 189 // We allocate a lot of these and binary search on them. This means that they 190 // have to be as compact as possible, which is why we don't store the size (can 191 // be found by looking at the next one) and put the hash in a side table. 192 struct SectionPiece { 193 SectionPiece(size_t Off, bool Live = false) 194 : InputOff(Off), OutputOff(-1), Live(Live || !Config->GcSections) {} 195 196 size_t InputOff; 197 ssize_t OutputOff : 8 * sizeof(ssize_t) - 1; 198 size_t Live : 1; 199 }; 200 static_assert(sizeof(SectionPiece) == 2 * sizeof(size_t), 201 "SectionPiece is too big"); 202 203 // This corresponds to a SHF_MERGE section of an input file. 204 class MergeInputSection : public InputSectionBase { 205 public: 206 template <class ELFT> 207 MergeInputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header, 208 StringRef Name); 209 static bool classof(const SectionBase *S); 210 void splitIntoPieces(); 211 212 // Mark the piece at a given offset live. Used by GC. 213 void markLiveAt(uint64_t Offset) { 214 assert(this->Flags & llvm::ELF::SHF_ALLOC); 215 LiveOffsets.insert(Offset); 216 } 217 218 // Translate an offset in the input section to an offset 219 // in the output section. 220 uint64_t getOffset(uint64_t Offset) const; 221 222 // Splittable sections are handled as a sequence of data 223 // rather than a single large blob of data. 224 std::vector<SectionPiece> Pieces; 225 226 // Returns I'th piece's data. This function is very hot when 227 // string merging is enabled, so we want to inline. 228 LLVM_ATTRIBUTE_ALWAYS_INLINE 229 llvm::CachedHashStringRef getData(size_t I) const { 230 size_t Begin = Pieces[I].InputOff; 231 size_t End; 232 if (Pieces.size() - 1 == I) 233 End = this->Data.size(); 234 else 235 End = Pieces[I + 1].InputOff; 236 237 StringRef S = {(const char *)(this->Data.data() + Begin), End - Begin}; 238 return {S, Hashes[I]}; 239 } 240 241 // Returns the SectionPiece at a given input section offset. 242 SectionPiece *getSectionPiece(uint64_t Offset); 243 const SectionPiece *getSectionPiece(uint64_t Offset) const; 244 245 SyntheticSection *getParent() const; 246 247 private: 248 void splitStrings(ArrayRef<uint8_t> A, size_t Size); 249 void splitNonStrings(ArrayRef<uint8_t> A, size_t Size); 250 251 std::vector<uint32_t> Hashes; 252 253 mutable llvm::DenseMap<uint64_t, uint64_t> OffsetMap; 254 mutable llvm::once_flag InitOffsetMap; 255 256 llvm::DenseSet<uint64_t> LiveOffsets; 257 }; 258 259 struct EhSectionPiece : public SectionPiece { 260 EhSectionPiece(size_t Off, InputSectionBase *ID, uint32_t Size, 261 unsigned FirstRelocation) 262 : SectionPiece(Off, false), ID(ID), Size(Size), 263 FirstRelocation(FirstRelocation) {} 264 InputSectionBase *ID; 265 uint32_t Size; 266 uint32_t size() const { return Size; } 267 268 ArrayRef<uint8_t> data() { return {ID->Data.data() + this->InputOff, Size}; } 269 unsigned FirstRelocation; 270 }; 271 272 // This corresponds to a .eh_frame section of an input file. 273 class EhInputSection : public InputSectionBase { 274 public: 275 template <class ELFT> 276 EhInputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header, 277 StringRef Name); 278 static bool classof(const SectionBase *S); 279 template <class ELFT> void split(); 280 template <class ELFT, class RelTy> void split(ArrayRef<RelTy> Rels); 281 282 // Splittable sections are handled as a sequence of data 283 // rather than a single large blob of data. 284 std::vector<EhSectionPiece> Pieces; 285 286 SyntheticSection *getParent() const; 287 }; 288 289 // This is a section that is added directly to an output section 290 // instead of needing special combination via a synthetic section. This 291 // includes all input sections with the exceptions of SHF_MERGE and 292 // .eh_frame. It also includes the synthetic sections themselves. 293 class InputSection : public InputSectionBase { 294 public: 295 InputSection(uint64_t Flags, uint32_t Type, uint32_t Alignment, 296 ArrayRef<uint8_t> Data, StringRef Name, Kind K = Regular); 297 template <class ELFT> 298 InputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header, 299 StringRef Name); 300 301 // Write this section to a mmap'ed file, assuming Buf is pointing to 302 // beginning of the output section. 303 template <class ELFT> void writeTo(uint8_t *Buf); 304 305 OutputSection *getParent() const; 306 307 // The offset from beginning of the output sections this section was assigned 308 // to. The writer sets a value. 309 uint64_t OutSecOff = 0; 310 311 static bool classof(const SectionBase *S); 312 313 InputSectionBase *getRelocatedSection(); 314 315 template <class ELFT, class RelTy> 316 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); 317 318 // Used by ICF. 319 uint32_t Class[2] = {0, 0}; 320 321 // Called by ICF to merge two input sections. 322 void replace(InputSection *Other); 323 324 private: 325 template <class ELFT, class RelTy> 326 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); 327 328 template <class ELFT> void copyShtGroup(uint8_t *Buf); 329 }; 330 331 // The list of all input sections. 332 extern std::vector<InputSectionBase *> InputSections; 333 334 } // namespace elf 335 336 std::string toString(const elf::InputSectionBase *); 337 } // namespace lld 338 339 #endif 340