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/Common/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 ObjFile; 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; 58 59 // True if this section has already been placed to a linker script 60 // output section. This is needed because, in a linker script, you 61 // can refer to the same section more than once. For example, in 62 // the following linker script, 63 // 64 // .foo : { *(.text) } 65 // .bar : { *(.text) } 66 // 67 // .foo takes all .text sections, and .bar becomes empty. To achieve 68 // this, we need to memorize whether a section has been placed or 69 // not for each input section. 70 unsigned Assigned : 1; 71 72 // These corresponds to the fields in Elf_Shdr. 73 uint32_t Alignment; 74 uint64_t Flags; 75 uint64_t Entsize; 76 uint32_t Type; 77 uint32_t Link; 78 uint32_t Info; 79 80 OutputSection *getOutputSection(); 81 const OutputSection *getOutputSection() const { 82 return const_cast<SectionBase *>(this)->getOutputSection(); 83 } 84 85 // Translate an offset in the input section to an offset in the output 86 // section. 87 uint64_t getOffset(uint64_t Offset) const; 88 89 uint64_t getOffset(const DefinedRegular &Sym) const; 90 91 protected: 92 SectionBase(Kind SectionKind, StringRef Name, uint64_t Flags, 93 uint64_t Entsize, uint64_t Alignment, uint32_t Type, 94 uint32_t Info, uint32_t Link) 95 : Name(Name), SectionKind(SectionKind), Alignment(Alignment), 96 Flags(Flags), Entsize(Entsize), Type(Type), Link(Link), Info(Info) { 97 Live = false; 98 Assigned = false; 99 } 100 }; 101 102 // This corresponds to a section of an input file. 103 class InputSectionBase : public SectionBase { 104 public: 105 InputSectionBase() 106 : SectionBase(Regular, "", /*Flags*/ 0, /*Entsize*/ 0, /*Alignment*/ 0, 107 /*Type*/ 0, 108 /*Info*/ 0, /*Link*/ 0), 109 Repl(this) { 110 NumRelocations = 0; 111 AreRelocsRela = false; 112 } 113 114 template <class ELFT> 115 InputSectionBase(ObjFile<ELFT> *File, const typename ELFT::Shdr *Header, 116 StringRef Name, Kind SectionKind); 117 118 InputSectionBase(InputFile *File, uint64_t Flags, uint32_t Type, 119 uint64_t Entsize, uint32_t Link, uint32_t Info, 120 uint32_t Alignment, ArrayRef<uint8_t> Data, StringRef Name, 121 Kind SectionKind); 122 123 static bool classof(const SectionBase *S) { return S->kind() != Output; } 124 125 // The file which contains this section. It's dynamic type is always 126 // ObjFile<ELFT>, but in order to avoid ELFT, we use InputFile as 127 // its static type. 128 InputFile *File; 129 130 template <class ELFT> ObjFile<ELFT> *getFile() const { 131 return cast_or_null<ObjFile<ELFT>>(File); 132 } 133 134 ArrayRef<uint8_t> Data; 135 uint64_t getOffsetInFile() const; 136 137 static InputSectionBase Discarded; 138 139 // Input sections are part of an output section. Special sections 140 // like .eh_frame and merge sections are first combined into a 141 // synthetic section that is then added to an output section. In all 142 // cases this points one level up. 143 SectionBase *Parent = nullptr; 144 145 // Relocations that refer to this section. 146 const void *FirstRelocation = nullptr; 147 unsigned NumRelocations : 31; 148 unsigned AreRelocsRela : 1; 149 150 template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const { 151 assert(!AreRelocsRela); 152 return llvm::makeArrayRef( 153 static_cast<const typename ELFT::Rel *>(FirstRelocation), 154 NumRelocations); 155 } 156 157 template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const { 158 assert(AreRelocsRela); 159 return llvm::makeArrayRef( 160 static_cast<const typename ELFT::Rela *>(FirstRelocation), 161 NumRelocations); 162 } 163 164 // This pointer points to the "real" instance of this instance. 165 // Usually Repl == this. However, if ICF merges two sections, 166 // Repl pointer of one section points to another section. So, 167 // if you need to get a pointer to this instance, do not use 168 // this but instead this->Repl. 169 InputSectionBase *Repl; 170 171 // InputSections that are dependent on us (reverse dependency for GC) 172 llvm::TinyPtrVector<InputSection *> DependentSections; 173 174 // Returns the size of this section (even if this is a common or BSS.) 175 size_t getSize() const; 176 177 InputSection *getLinkOrderDep() const; 178 179 // Compilers emit zlib-compressed debug sections if the -gz option 180 // is given. This function checks if this section is compressed, and 181 // if so, decompress in memory. 182 void maybeUncompress(); 183 184 // Returns a source location string. Used to construct an error message. 185 template <class ELFT> std::string getLocation(uint64_t Offset); 186 template <class ELFT> std::string getSrcMsg(uint64_t Offset); 187 template <class ELFT> std::string getObjMsg(uint64_t Offset); 188 189 // Each section knows how to relocate itself. These functions apply 190 // relocations, assuming that Buf points to this section's copy in 191 // the mmap'ed output buffer. 192 template <class ELFT> void relocate(uint8_t *Buf, uint8_t *BufEnd); 193 void relocateAlloc(uint8_t *Buf, uint8_t *BufEnd); 194 195 // The native ELF reloc data type is not very convenient to handle. 196 // So we convert ELF reloc records to our own records in Relocations.cpp. 197 // This vector contains such "cooked" relocations. 198 std::vector<Relocation> Relocations; 199 200 template <typename T> llvm::ArrayRef<T> getDataAs() const { 201 size_t S = Data.size(); 202 assert(S % sizeof(T) == 0); 203 return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T)); 204 } 205 206 private: 207 // A pointer that owns uncompressed data if a section is compressed by zlib. 208 // Since the feature is not used often, this is usually a nullptr. 209 std::unique_ptr<char[]> UncompressBuf; 210 }; 211 212 // SectionPiece represents a piece of splittable section contents. 213 // We allocate a lot of these and binary search on them. This means that they 214 // have to be as compact as possible, which is why we don't store the size (can 215 // be found by looking at the next one) and put the hash in a side table. 216 struct SectionPiece { 217 SectionPiece(size_t Off, bool Live) 218 : InputOff(Off), Live(Live || !Config->GcSections), OutputOff(-1) {} 219 220 size_t InputOff : 8 * sizeof(ssize_t) - 1; 221 size_t Live : 1; 222 ssize_t OutputOff; 223 }; 224 static_assert(sizeof(SectionPiece) == 2 * sizeof(size_t), 225 "SectionPiece is too big"); 226 227 // This corresponds to a SHF_MERGE section of an input file. 228 class MergeInputSection : public InputSectionBase { 229 public: 230 template <class ELFT> 231 MergeInputSection(ObjFile<ELFT> *F, const typename ELFT::Shdr *Header, 232 StringRef Name); 233 static bool classof(const SectionBase *S) { return S->kind() == Merge; } 234 void splitIntoPieces(); 235 236 // Mark the piece at a given offset live. Used by GC. 237 void markLiveAt(uint64_t Offset) { 238 assert(this->Flags & llvm::ELF::SHF_ALLOC); 239 LiveOffsets.insert(Offset); 240 } 241 242 // Translate an offset in the input section to an offset 243 // in the output section. 244 uint64_t getOffset(uint64_t Offset) const; 245 246 // Splittable sections are handled as a sequence of data 247 // rather than a single large blob of data. 248 std::vector<SectionPiece> Pieces; 249 250 // Returns I'th piece's data. This function is very hot when 251 // string merging is enabled, so we want to inline. 252 LLVM_ATTRIBUTE_ALWAYS_INLINE 253 llvm::CachedHashStringRef getData(size_t I) const { 254 size_t Begin = Pieces[I].InputOff; 255 size_t End; 256 if (Pieces.size() - 1 == I) 257 End = this->Data.size(); 258 else 259 End = Pieces[I + 1].InputOff; 260 261 StringRef S = {(const char *)(this->Data.data() + Begin), End - Begin}; 262 return {S, Hashes[I]}; 263 } 264 265 // Returns the SectionPiece at a given input section offset. 266 SectionPiece *getSectionPiece(uint64_t Offset); 267 const SectionPiece *getSectionPiece(uint64_t Offset) const; 268 269 SyntheticSection *getParent() const; 270 271 private: 272 void splitStrings(ArrayRef<uint8_t> A, size_t Size); 273 void splitNonStrings(ArrayRef<uint8_t> A, size_t Size); 274 275 std::vector<uint32_t> Hashes; 276 277 mutable llvm::DenseMap<uint64_t, uint64_t> OffsetMap; 278 mutable llvm::once_flag InitOffsetMap; 279 280 llvm::DenseSet<uint64_t> LiveOffsets; 281 }; 282 283 struct EhSectionPiece { 284 EhSectionPiece(size_t Off, InputSectionBase *Sec, uint32_t Size, 285 unsigned FirstRelocation) 286 : InputOff(Off), Sec(Sec), Size(Size), FirstRelocation(FirstRelocation) {} 287 288 ArrayRef<uint8_t> data() { return {Sec->Data.data() + this->InputOff, Size}; } 289 290 size_t InputOff; 291 ssize_t OutputOff = -1; 292 InputSectionBase *Sec; 293 uint32_t Size; 294 unsigned FirstRelocation; 295 }; 296 297 // This corresponds to a .eh_frame section of an input file. 298 class EhInputSection : public InputSectionBase { 299 public: 300 template <class ELFT> 301 EhInputSection(ObjFile<ELFT> *F, const typename ELFT::Shdr *Header, 302 StringRef Name); 303 static bool classof(const SectionBase *S) { return S->kind() == EHFrame; } 304 template <class ELFT> void split(); 305 template <class ELFT, class RelTy> void split(ArrayRef<RelTy> Rels); 306 307 // Splittable sections are handled as a sequence of data 308 // rather than a single large blob of data. 309 std::vector<EhSectionPiece> Pieces; 310 311 SyntheticSection *getParent() const; 312 }; 313 314 // This is a section that is added directly to an output section 315 // instead of needing special combination via a synthetic section. This 316 // includes all input sections with the exceptions of SHF_MERGE and 317 // .eh_frame. It also includes the synthetic sections themselves. 318 class InputSection : public InputSectionBase { 319 public: 320 InputSection(uint64_t Flags, uint32_t Type, uint32_t Alignment, 321 ArrayRef<uint8_t> Data, StringRef Name, Kind K = Regular); 322 template <class ELFT> 323 InputSection(ObjFile<ELFT> *F, const typename ELFT::Shdr *Header, 324 StringRef Name); 325 326 // Write this section to a mmap'ed file, assuming Buf is pointing to 327 // beginning of the output section. 328 template <class ELFT> void writeTo(uint8_t *Buf); 329 330 OutputSection *getParent() const; 331 332 // The offset from beginning of the output sections this section was assigned 333 // to. The writer sets a value. 334 uint64_t OutSecOff = 0; 335 336 static bool classof(const SectionBase *S); 337 338 InputSectionBase *getRelocatedSection(); 339 340 template <class ELFT, class RelTy> 341 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); 342 343 // Used by ICF. 344 uint32_t Class[2] = {0, 0}; 345 346 // Called by ICF to merge two input sections. 347 void replace(InputSection *Other); 348 349 private: 350 template <class ELFT, class RelTy> 351 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); 352 353 template <class ELFT> void copyShtGroup(uint8_t *Buf); 354 }; 355 356 // The list of all input sections. 357 extern std::vector<InputSectionBase *> InputSections; 358 359 // Builds section order for handling --symbol-ordering-file. 360 llvm::DenseMap<SectionBase *, int> buildSectionOrder(); 361 362 } // namespace elf 363 364 std::string toString(const elf::InputSectionBase *); 365 } // namespace lld 366 367 #endif 368