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 = 1; 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 alignment(alignment), flags(flags), entsize(entsize), type(type), 100 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 // Input sections are part of an output section. Special sections 123 // like .eh_frame and merge sections are first combined into a 124 // synthetic section that is then added to an output section. In all 125 // cases this points one level up. 126 SectionBase *parent = nullptr; 127 128 // Section index of the relocation section if exists. 129 uint32_t relSecIdx = 0; 130 131 template <class ELFT> ObjFile<ELFT> *getFile() const { 132 return cast_or_null<ObjFile<ELFT>>(file); 133 } 134 135 // If basic block sections are enabled, many code sections could end up with 136 // one or two jump instructions at the end that could be relaxed to a smaller 137 // instruction. The members below help trimming the trailing jump instruction 138 // and shrinking a section. 139 uint8_t bytesDropped = 0; 140 141 // Whether the section needs to be padded with a NOP filler due to 142 // deleteFallThruJmpInsn. 143 bool nopFiller = false; 144 145 void drop_back(unsigned num) { 146 assert(bytesDropped + num < 256); 147 bytesDropped += num; 148 } 149 150 void push_back(uint64_t num) { 151 assert(bytesDropped >= num); 152 bytesDropped -= num; 153 } 154 155 void trim() { 156 if (bytesDropped) { 157 rawData = rawData.drop_back(bytesDropped); 158 bytesDropped = 0; 159 } 160 } 161 162 ArrayRef<uint8_t> data() const { 163 if (uncompressedSize >= 0) 164 uncompress(); 165 return rawData; 166 } 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() = default; 246 SectionPiece(size_t off, uint32_t hash, bool live) 247 : inputOff(off), live(live), hash(hash >> 1) {} 248 249 uint32_t inputOff; 250 uint32_t live : 1; 251 uint32_t hash : 31; 252 uint64_t outputOff = 0; 253 }; 254 255 static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big"); 256 257 // This corresponds to a SHF_MERGE section of an input file. 258 class MergeInputSection : public InputSectionBase { 259 public: 260 template <class ELFT> 261 MergeInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header, 262 StringRef name); 263 MergeInputSection(uint64_t flags, uint32_t type, uint64_t entsize, 264 ArrayRef<uint8_t> data, StringRef name); 265 266 static bool classof(const SectionBase *s) { return s->kind() == Merge; } 267 void splitIntoPieces(); 268 269 // Translate an offset in the input section to an offset in the parent 270 // MergeSyntheticSection. 271 uint64_t getParentOffset(uint64_t offset) const; 272 273 // Splittable sections are handled as a sequence of data 274 // rather than a single large blob of data. 275 SmallVector<SectionPiece, 0> pieces; 276 277 // Returns I'th piece's data. This function is very hot when 278 // string merging is enabled, so we want to inline. 279 LLVM_ATTRIBUTE_ALWAYS_INLINE 280 llvm::CachedHashStringRef getData(size_t i) const { 281 size_t begin = pieces[i].inputOff; 282 size_t end = 283 (pieces.size() - 1 == i) ? data().size() : pieces[i + 1].inputOff; 284 return {toStringRef(data().slice(begin, end - begin)), pieces[i].hash}; 285 } 286 287 // Returns the SectionPiece at a given input section offset. 288 SectionPiece *getSectionPiece(uint64_t offset); 289 const SectionPiece *getSectionPiece(uint64_t offset) const { 290 return const_cast<MergeInputSection *>(this)->getSectionPiece(offset); 291 } 292 293 SyntheticSection *getParent() const; 294 295 private: 296 void splitStrings(StringRef s, size_t size); 297 void splitNonStrings(ArrayRef<uint8_t> a, size_t size); 298 }; 299 300 struct EhSectionPiece { 301 EhSectionPiece(size_t off, InputSectionBase *sec, uint32_t size, 302 unsigned firstRelocation) 303 : inputOff(off), sec(sec), size(size), firstRelocation(firstRelocation) {} 304 305 ArrayRef<uint8_t> data() const { 306 return {sec->data().data() + this->inputOff, size}; 307 } 308 309 size_t inputOff; 310 ssize_t outputOff = -1; 311 InputSectionBase *sec; 312 uint32_t size; 313 unsigned firstRelocation; 314 }; 315 316 // This corresponds to a .eh_frame section of an input file. 317 class EhInputSection : public InputSectionBase { 318 public: 319 template <class ELFT> 320 EhInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header, 321 StringRef name); 322 static bool classof(const SectionBase *s) { return s->kind() == EHFrame; } 323 template <class ELFT> void split(); 324 template <class ELFT, class RelTy> void split(ArrayRef<RelTy> rels); 325 326 // Splittable sections are handled as a sequence of data 327 // rather than a single large blob of data. 328 SmallVector<EhSectionPiece, 0> pieces; 329 330 SyntheticSection *getParent() const; 331 }; 332 333 // This is a section that is added directly to an output section 334 // instead of needing special combination via a synthetic section. This 335 // includes all input sections with the exceptions of SHF_MERGE and 336 // .eh_frame. It also includes the synthetic sections themselves. 337 class InputSection : public InputSectionBase { 338 public: 339 InputSection(InputFile *f, uint64_t flags, uint32_t type, uint32_t alignment, 340 ArrayRef<uint8_t> data, StringRef name, Kind k = Regular); 341 template <class ELFT> 342 InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header, 343 StringRef name); 344 345 // Write this section to a mmap'ed file, assuming Buf is pointing to 346 // beginning of the output section. 347 template <class ELFT> void writeTo(uint8_t *buf); 348 349 OutputSection *getParent() const; 350 351 // This variable has two usages. Initially, it represents an index in the 352 // OutputSection's InputSection list, and is used when ordering SHF_LINK_ORDER 353 // sections. After assignAddresses is called, it represents the offset from 354 // the beginning of the output section this section was assigned to. 355 uint64_t outSecOff = 0; 356 357 static bool classof(const SectionBase *s); 358 359 InputSectionBase *getRelocatedSection() const; 360 361 template <class ELFT, class RelTy> 362 void relocateNonAlloc(uint8_t *buf, llvm::ArrayRef<RelTy> rels); 363 364 // Points to the canonical section. If ICF folds two sections, repl pointer of 365 // one section points to the other. 366 InputSection *repl = this; 367 368 // Used by ICF. 369 uint32_t eqClass[2] = {0, 0}; 370 371 // Called by ICF to merge two input sections. 372 void replace(InputSection *other); 373 374 static InputSection discarded; 375 376 private: 377 template <class ELFT, class RelTy> 378 void copyRelocations(uint8_t *buf, llvm::ArrayRef<RelTy> rels); 379 380 template <class ELFT> void copyShtGroup(uint8_t *buf); 381 }; 382 383 static_assert(sizeof(InputSection) <= 160, "InputSection is too big"); 384 385 inline bool isDebugSection(const InputSectionBase &sec) { 386 return (sec.flags & llvm::ELF::SHF_ALLOC) == 0 && 387 (sec.name.startswith(".debug") || sec.name.startswith(".zdebug")); 388 } 389 390 // The list of all input sections. 391 extern SmallVector<InputSectionBase *, 0> inputSections; 392 393 // The set of TOC entries (.toc + addend) for which we should not apply 394 // toc-indirect to toc-relative relaxation. const Symbol * refers to the 395 // STT_SECTION symbol associated to the .toc input section. 396 extern llvm::DenseSet<std::pair<const Symbol *, uint64_t>> ppc64noTocRelax; 397 398 } // namespace elf 399 400 std::string toString(const elf::InputSectionBase *); 401 } // namespace lld 402 403 #endif 404