1 //===- InputSection.h -------------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLD_ELF_INPUT_SECTION_H 10 #define LLD_ELF_INPUT_SECTION_H 11 12 #include "Relocations.h" 13 #include "lld/Common/CommonLinkerContext.h" 14 #include "lld/Common/LLVM.h" 15 #include "lld/Common/Memory.h" 16 #include "llvm/ADT/CachedHashString.h" 17 #include "llvm/ADT/DenseSet.h" 18 #include "llvm/ADT/StringExtras.h" 19 #include "llvm/ADT/TinyPtrVector.h" 20 #include "llvm/Object/ELF.h" 21 #include "llvm/Support/Compiler.h" 22 23 namespace lld { 24 namespace elf { 25 26 class InputFile; 27 class Symbol; 28 29 class Defined; 30 struct Partition; 31 class SyntheticSection; 32 template <class ELFT> class ObjFile; 33 class OutputSection; 34 35 LLVM_LIBRARY_VISIBILITY extern std::vector<Partition> partitions; 36 37 // Returned by InputSectionBase::relsOrRelas. At least one member is empty. 38 template <class ELFT> struct RelsOrRelas { 39 ArrayRef<typename ELFT::Rel> rels; 40 ArrayRef<typename ELFT::Rela> relas; 41 bool areRelocsRel() const { return rels.size(); } 42 }; 43 44 // This is the base class of all sections that lld handles. Some are sections in 45 // input files, some are sections in the produced output file and some exist 46 // just as a convenience for implementing special ways of combining some 47 // sections. 48 class SectionBase { 49 public: 50 enum Kind { Regular, Synthetic, EHFrame, Merge, Output }; 51 52 Kind kind() const { return (Kind)sectionKind; } 53 54 uint8_t sectionKind : 3; 55 56 // The next two bit fields are only used by InputSectionBase, but we 57 // put them here so the struct packs better. 58 59 uint8_t bss : 1; 60 61 // Set for sections that should not be folded by ICF. 62 uint8_t keepUnique : 1; 63 64 uint8_t partition = 1; 65 uint32_t type; 66 StringRef name; 67 68 // The 1-indexed partition that this section is assigned to by the garbage 69 // collector, or 0 if this section is dead. Normally there is only one 70 // partition, so this will either be 0 or 1. 71 elf::Partition &getPartition() const; 72 73 // These corresponds to the fields in Elf_Shdr. 74 uint64_t flags; 75 uint32_t addralign; 76 uint32_t entsize; 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 getVA(uint64_t offset = 0) const; 90 91 bool isLive() const { return partition != 0; } 92 void markLive() { partition = 1; } 93 void markDead() { partition = 0; } 94 95 protected: 96 constexpr SectionBase(Kind sectionKind, StringRef name, uint64_t flags, 97 uint32_t entsize, uint32_t addralign, uint32_t type, 98 uint32_t info, uint32_t link) 99 : sectionKind(sectionKind), bss(false), keepUnique(false), type(type), 100 name(name), flags(flags), addralign(addralign), entsize(entsize), 101 link(link), info(info) {} 102 }; 103 104 struct RISCVRelaxAux; 105 106 // This corresponds to a section of an input file. 107 class InputSectionBase : public SectionBase { 108 public: 109 template <class ELFT> 110 InputSectionBase(ObjFile<ELFT> &file, const typename ELFT::Shdr &header, 111 StringRef name, Kind sectionKind); 112 113 InputSectionBase(InputFile *file, uint64_t flags, uint32_t type, 114 uint64_t entsize, uint32_t link, uint32_t info, 115 uint32_t addralign, ArrayRef<uint8_t> data, StringRef name, 116 Kind sectionKind); 117 118 static bool classof(const SectionBase *s) { return s->kind() != Output; } 119 120 // The file which contains this section. Its dynamic type is always 121 // ObjFile<ELFT>, but in order to avoid ELFT, we use InputFile as 122 // its static type. 123 InputFile *file; 124 125 // Input sections are part of an output section. Special sections 126 // like .eh_frame and merge sections are first combined into a 127 // synthetic section that is then added to an output section. In all 128 // cases this points one level up. 129 SectionBase *parent = nullptr; 130 131 // Section index of the relocation section if exists. 132 uint32_t relSecIdx = 0; 133 134 template <class ELFT> ObjFile<ELFT> *getFile() const { 135 return cast_or_null<ObjFile<ELFT>>(file); 136 } 137 138 // Used by --optimize-bb-jumps and RISC-V linker relaxation temporarily to 139 // indicate the number of bytes which is not counted in the size. This should 140 // be reset to zero after uses. 141 uint32_t bytesDropped = 0; 142 143 mutable bool compressed = false; 144 145 // Whether the section needs to be padded with a NOP filler due to 146 // deleteFallThruJmpInsn. 147 bool nopFiller = false; 148 149 void drop_back(unsigned num) { 150 assert(bytesDropped + num < 256); 151 bytesDropped += num; 152 } 153 154 void push_back(uint64_t num) { 155 assert(bytesDropped >= num); 156 bytesDropped -= num; 157 } 158 159 mutable const uint8_t *content_; 160 uint64_t size; 161 162 void trim() { 163 if (bytesDropped) { 164 size -= bytesDropped; 165 bytesDropped = 0; 166 } 167 } 168 169 ArrayRef<uint8_t> content() const { 170 return ArrayRef<uint8_t>(content_, size); 171 } 172 ArrayRef<uint8_t> contentMaybeDecompress() const { 173 if (compressed) 174 decompress(); 175 return content(); 176 } 177 178 // The next member in the section group if this section is in a group. This is 179 // used by --gc-sections. 180 InputSectionBase *nextInSectionGroup = nullptr; 181 182 template <class ELFT> RelsOrRelas<ELFT> relsOrRelas() const; 183 184 // InputSections that are dependent on us (reverse dependency for GC) 185 llvm::TinyPtrVector<InputSection *> dependentSections; 186 187 // Returns the size of this section (even if this is a common or BSS.) 188 size_t getSize() const; 189 190 InputSection *getLinkOrderDep() const; 191 192 // Get a symbol that encloses this offset from within the section. If type is 193 // not zero, return a symbol with the specified type. 194 Defined *getEnclosingSymbol(uint64_t offset, uint8_t type = 0) const; 195 Defined *getEnclosingFunction(uint64_t offset) const { 196 return getEnclosingSymbol(offset, llvm::ELF::STT_FUNC); 197 } 198 199 // Returns a source location string. Used to construct an error message. 200 std::string getLocation(uint64_t offset) const; 201 std::string getSrcMsg(const Symbol &sym, uint64_t offset) const; 202 std::string getObjMsg(uint64_t offset) const; 203 204 // Each section knows how to relocate itself. These functions apply 205 // relocations, assuming that Buf points to this section's copy in 206 // the mmap'ed output buffer. 207 template <class ELFT> void relocate(uint8_t *buf, uint8_t *bufEnd); 208 static uint64_t getRelocTargetVA(const InputFile *File, RelType Type, 209 int64_t A, uint64_t P, const Symbol &Sym, 210 RelExpr Expr); 211 212 // The native ELF reloc data type is not very convenient to handle. 213 // So we convert ELF reloc records to our own records in Relocations.cpp. 214 // This vector contains such "cooked" relocations. 215 SmallVector<Relocation, 0> relocations; 216 217 void addReloc(const Relocation &r) { relocations.push_back(r); } 218 MutableArrayRef<Relocation> relocs() { return relocations; } 219 ArrayRef<Relocation> relocs() const { return relocations; } 220 221 union { 222 // These are modifiers to jump instructions that are necessary when basic 223 // block sections are enabled. Basic block sections creates opportunities 224 // to relax jump instructions at basic block boundaries after reordering the 225 // basic blocks. 226 JumpInstrMod *jumpInstrMod = nullptr; 227 228 // Auxiliary information for RISC-V linker relaxation. RISC-V does not use 229 // jumpInstrMod. 230 RISCVRelaxAux *relaxAux; 231 232 // The compressed content size when `compressed` is true. 233 size_t compressedSize; 234 }; 235 236 // A function compiled with -fsplit-stack calling a function 237 // compiled without -fsplit-stack needs its prologue adjusted. Find 238 // such functions and adjust their prologues. This is very similar 239 // to relocation. See https://gcc.gnu.org/wiki/SplitStacks for more 240 // information. 241 template <typename ELFT> 242 void adjustSplitStackFunctionPrologues(uint8_t *buf, uint8_t *end); 243 244 245 template <typename T> llvm::ArrayRef<T> getDataAs() const { 246 size_t s = content().size(); 247 assert(s % sizeof(T) == 0); 248 return llvm::ArrayRef<T>((const T *)content().data(), s / sizeof(T)); 249 } 250 251 protected: 252 template <typename ELFT> 253 void parseCompressedHeader(); 254 void decompress() const; 255 }; 256 257 // SectionPiece represents a piece of splittable section contents. 258 // We allocate a lot of these and binary search on them. This means that they 259 // have to be as compact as possible, which is why we don't store the size (can 260 // be found by looking at the next one). 261 struct SectionPiece { 262 SectionPiece() = default; 263 SectionPiece(size_t off, uint32_t hash, bool live) 264 : inputOff(off), live(live), hash(hash >> 1) {} 265 266 uint32_t inputOff; 267 uint32_t live : 1; 268 uint32_t hash : 31; 269 uint64_t outputOff = 0; 270 }; 271 272 static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big"); 273 274 // This corresponds to a SHF_MERGE section of an input file. 275 class MergeInputSection : public InputSectionBase { 276 public: 277 template <class ELFT> 278 MergeInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header, 279 StringRef name); 280 MergeInputSection(uint64_t flags, uint32_t type, uint64_t entsize, 281 ArrayRef<uint8_t> data, StringRef name); 282 283 static bool classof(const SectionBase *s) { return s->kind() == Merge; } 284 void splitIntoPieces(); 285 286 // Translate an offset in the input section to an offset in the parent 287 // MergeSyntheticSection. 288 uint64_t getParentOffset(uint64_t offset) const; 289 290 // Splittable sections are handled as a sequence of data 291 // rather than a single large blob of data. 292 SmallVector<SectionPiece, 0> pieces; 293 294 // Returns I'th piece's data. This function is very hot when 295 // string merging is enabled, so we want to inline. 296 LLVM_ATTRIBUTE_ALWAYS_INLINE 297 llvm::CachedHashStringRef getData(size_t i) const { 298 size_t begin = pieces[i].inputOff; 299 size_t end = 300 (pieces.size() - 1 == i) ? content().size() : pieces[i + 1].inputOff; 301 return {toStringRef(content().slice(begin, end - begin)), pieces[i].hash}; 302 } 303 304 // Returns the SectionPiece at a given input section offset. 305 SectionPiece &getSectionPiece(uint64_t offset); 306 const SectionPiece &getSectionPiece(uint64_t offset) const { 307 return const_cast<MergeInputSection *>(this)->getSectionPiece(offset); 308 } 309 310 SyntheticSection *getParent() const { 311 return cast_or_null<SyntheticSection>(parent); 312 } 313 314 private: 315 void splitStrings(StringRef s, size_t size); 316 void splitNonStrings(ArrayRef<uint8_t> a, size_t size); 317 }; 318 319 struct EhSectionPiece { 320 EhSectionPiece(size_t off, InputSectionBase *sec, uint32_t size, 321 unsigned firstRelocation) 322 : inputOff(off), sec(sec), size(size), firstRelocation(firstRelocation) {} 323 324 ArrayRef<uint8_t> data() const { 325 return {sec->content().data() + this->inputOff, size}; 326 } 327 328 size_t inputOff; 329 ssize_t outputOff = -1; 330 InputSectionBase *sec; 331 uint32_t size; 332 unsigned firstRelocation; 333 }; 334 335 // This corresponds to a .eh_frame section of an input file. 336 class EhInputSection : public InputSectionBase { 337 public: 338 template <class ELFT> 339 EhInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header, 340 StringRef name); 341 static bool classof(const SectionBase *s) { return s->kind() == EHFrame; } 342 template <class ELFT> void split(); 343 template <class ELFT, class RelTy> void split(ArrayRef<RelTy> rels); 344 345 // Splittable sections are handled as a sequence of data 346 // rather than a single large blob of data. 347 SmallVector<EhSectionPiece, 0> cies, fdes; 348 349 SyntheticSection *getParent() const; 350 uint64_t getParentOffset(uint64_t offset) const; 351 }; 352 353 // This is a section that is added directly to an output section 354 // instead of needing special combination via a synthetic section. This 355 // includes all input sections with the exceptions of SHF_MERGE and 356 // .eh_frame. It also includes the synthetic sections themselves. 357 class InputSection : public InputSectionBase { 358 public: 359 InputSection(InputFile *f, uint64_t flags, uint32_t type, uint32_t addralign, 360 ArrayRef<uint8_t> data, StringRef name, Kind k = Regular); 361 template <class ELFT> 362 InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header, 363 StringRef name); 364 365 static bool classof(const SectionBase *s) { 366 return s->kind() == SectionBase::Regular || 367 s->kind() == SectionBase::Synthetic; 368 } 369 370 // Write this section to a mmap'ed file, assuming Buf is pointing to 371 // beginning of the output section. 372 template <class ELFT> void writeTo(uint8_t *buf); 373 374 OutputSection *getParent() const { 375 return reinterpret_cast<OutputSection *>(parent); 376 } 377 378 // This variable has two usages. Initially, it represents an index in the 379 // OutputSection's InputSection list, and is used when ordering SHF_LINK_ORDER 380 // sections. After assignAddresses is called, it represents the offset from 381 // the beginning of the output section this section was assigned to. 382 uint64_t outSecOff = 0; 383 384 InputSectionBase *getRelocatedSection() const; 385 386 template <class ELFT, class RelTy> 387 void relocateNonAlloc(uint8_t *buf, llvm::ArrayRef<RelTy> rels); 388 389 // Points to the canonical section. If ICF folds two sections, repl pointer of 390 // one section points to the other. 391 InputSection *repl = this; 392 393 // Used by ICF. 394 uint32_t eqClass[2] = {0, 0}; 395 396 // Called by ICF to merge two input sections. 397 void replace(InputSection *other); 398 399 static InputSection discarded; 400 401 private: 402 template <class ELFT, class RelTy> void copyRelocations(uint8_t *buf); 403 404 template <class ELFT, class RelTy, class RelIt> 405 void copyRelocations(uint8_t *buf, llvm::iterator_range<RelIt> rels); 406 407 template <class ELFT> void copyShtGroup(uint8_t *buf); 408 }; 409 410 static_assert(sizeof(InputSection) <= 160, "InputSection is too big"); 411 412 class SyntheticSection : public InputSection { 413 public: 414 SyntheticSection(uint64_t flags, uint32_t type, uint32_t addralign, 415 StringRef name) 416 : InputSection(nullptr, flags, type, addralign, {}, name, 417 InputSectionBase::Synthetic) {} 418 419 virtual ~SyntheticSection() = default; 420 virtual size_t getSize() const = 0; 421 virtual bool updateAllocSize() { return false; } 422 // If the section has the SHF_ALLOC flag and the size may be changed if 423 // thunks are added, update the section size. 424 virtual bool isNeeded() const { return true; } 425 virtual void finalizeContents() {} 426 virtual void writeTo(uint8_t *buf) = 0; 427 428 static bool classof(const SectionBase *sec) { 429 return sec->kind() == InputSectionBase::Synthetic; 430 } 431 }; 432 433 inline bool isDebugSection(const InputSectionBase &sec) { 434 return (sec.flags & llvm::ELF::SHF_ALLOC) == 0 && 435 sec.name.starts_with(".debug"); 436 } 437 438 // The set of TOC entries (.toc + addend) for which we should not apply 439 // toc-indirect to toc-relative relaxation. const Symbol * refers to the 440 // STT_SECTION symbol associated to the .toc input section. 441 extern llvm::DenseSet<std::pair<const Symbol *, uint64_t>> ppc64noTocRelax; 442 443 } // namespace elf 444 445 std::string toString(const elf::InputSectionBase *); 446 } // namespace lld 447 448 #endif 449