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