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, uint32_t Hash, bool Live) 218 : InputOff(Off), Hash(Hash), OutputOff(-1), 219 Live(Live || !Config->GcSections) {} 220 221 uint32_t InputOff; 222 uint32_t Hash; 223 int64_t OutputOff : 63; 224 uint64_t Live : 1; 225 }; 226 227 static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big"); 228 229 // This corresponds to a SHF_MERGE section of an input file. 230 class MergeInputSection : public InputSectionBase { 231 public: 232 template <class ELFT> 233 MergeInputSection(ObjFile<ELFT> *F, const typename ELFT::Shdr *Header, 234 StringRef Name); 235 static bool classof(const SectionBase *S) { return S->kind() == Merge; } 236 void splitIntoPieces(); 237 238 // Mark the piece at a given offset live. Used by GC. 239 void markLiveAt(uint64_t Offset) { 240 if (this->Flags & llvm::ELF::SHF_ALLOC) 241 LiveOffsets.insert(Offset); 242 } 243 244 // Translate an offset in the input section to an offset 245 // in the output section. 246 uint64_t getOffset(uint64_t Offset) const; 247 248 // Splittable sections are handled as a sequence of data 249 // rather than a single large blob of data. 250 std::vector<SectionPiece> Pieces; 251 252 // Returns I'th piece's data. This function is very hot when 253 // string merging is enabled, so we want to inline. 254 LLVM_ATTRIBUTE_ALWAYS_INLINE 255 llvm::CachedHashStringRef getData(size_t I) const { 256 size_t Begin = Pieces[I].InputOff; 257 size_t End = 258 (Pieces.size() - 1 == I) ? Data.size() : Pieces[I + 1].InputOff; 259 return {toStringRef(Data.slice(Begin, End - Begin)), Pieces[I].Hash}; 260 } 261 262 // Returns the SectionPiece at a given input section offset. 263 SectionPiece *getSectionPiece(uint64_t Offset); 264 const SectionPiece *getSectionPiece(uint64_t Offset) const; 265 266 SyntheticSection *getParent() const; 267 268 private: 269 void splitStrings(ArrayRef<uint8_t> A, size_t Size); 270 void splitNonStrings(ArrayRef<uint8_t> A, size_t Size); 271 272 llvm::DenseSet<uint64_t> LiveOffsets; 273 }; 274 275 struct EhSectionPiece { 276 EhSectionPiece(size_t Off, InputSectionBase *Sec, uint32_t Size, 277 unsigned FirstRelocation) 278 : InputOff(Off), Sec(Sec), Size(Size), FirstRelocation(FirstRelocation) {} 279 280 ArrayRef<uint8_t> data() { return {Sec->Data.data() + this->InputOff, Size}; } 281 282 size_t InputOff; 283 ssize_t OutputOff = -1; 284 InputSectionBase *Sec; 285 uint32_t Size; 286 unsigned FirstRelocation; 287 }; 288 289 // This corresponds to a .eh_frame section of an input file. 290 class EhInputSection : public InputSectionBase { 291 public: 292 template <class ELFT> 293 EhInputSection(ObjFile<ELFT> *F, const typename ELFT::Shdr *Header, 294 StringRef Name); 295 static bool classof(const SectionBase *S) { return S->kind() == EHFrame; } 296 template <class ELFT> void split(); 297 template <class ELFT, class RelTy> void split(ArrayRef<RelTy> Rels); 298 299 // Splittable sections are handled as a sequence of data 300 // rather than a single large blob of data. 301 std::vector<EhSectionPiece> Pieces; 302 303 SyntheticSection *getParent() const; 304 }; 305 306 // This is a section that is added directly to an output section 307 // instead of needing special combination via a synthetic section. This 308 // includes all input sections with the exceptions of SHF_MERGE and 309 // .eh_frame. It also includes the synthetic sections themselves. 310 class InputSection : public InputSectionBase { 311 public: 312 InputSection(uint64_t Flags, uint32_t Type, uint32_t Alignment, 313 ArrayRef<uint8_t> Data, StringRef Name, Kind K = Regular); 314 template <class ELFT> 315 InputSection(ObjFile<ELFT> *F, const typename ELFT::Shdr *Header, 316 StringRef Name); 317 318 // Write this section to a mmap'ed file, assuming Buf is pointing to 319 // beginning of the output section. 320 template <class ELFT> void writeTo(uint8_t *Buf); 321 322 OutputSection *getParent() const; 323 324 // The offset from beginning of the output sections this section was assigned 325 // to. The writer sets a value. 326 uint64_t OutSecOff = 0; 327 328 static bool classof(const SectionBase *S); 329 330 InputSectionBase *getRelocatedSection(); 331 332 template <class ELFT, class RelTy> 333 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); 334 335 // Used by ICF. 336 uint32_t Class[2] = {0, 0}; 337 338 // Called by ICF to merge two input sections. 339 void replace(InputSection *Other); 340 341 private: 342 template <class ELFT, class RelTy> 343 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels); 344 345 template <class ELFT> void copyShtGroup(uint8_t *Buf); 346 }; 347 348 // The list of all input sections. 349 extern std::vector<InputSectionBase *> InputSections; 350 351 // Builds section order for handling --symbol-ordering-file. 352 llvm::DenseMap<SectionBase *, int> buildSectionOrder(); 353 354 } // namespace elf 355 356 std::string toString(const elf::InputSectionBase *); 357 } // namespace lld 358 359 #endif 360