1 //===- OutputSections.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_OUTPUT_SECTIONS_H 10 #define LLD_ELF_OUTPUT_SECTIONS_H 11 12 #include "Config.h" 13 #include "InputSection.h" 14 #include "LinkerScript.h" 15 #include "Relocations.h" 16 #include "lld/Common/LLVM.h" 17 #include "llvm/MC/StringTableBuilder.h" 18 #include "llvm/Object/ELF.h" 19 #include <array> 20 21 namespace lld { 22 namespace elf { 23 24 struct PhdrEntry; 25 class InputSection; 26 class InputSectionBase; 27 28 // This represents a section in an output file. 29 // It is composed of multiple InputSections. 30 // The writer creates multiple OutputSections and assign them unique, 31 // non-overlapping file offsets and VAs. 32 class OutputSection final : public BaseCommand, public SectionBase { 33 public: 34 OutputSection(StringRef name, uint32_t type, uint64_t flags); 35 36 static bool classof(const SectionBase *s) { 37 return s->kind() == SectionBase::Output; 38 } 39 40 static bool classof(const BaseCommand *c); 41 42 uint64_t getLMA() const { return ptLoad ? addr + ptLoad->lmaOffset : addr; } 43 template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *sHdr); 44 45 uint32_t sectionIndex = UINT32_MAX; 46 unsigned sortRank; 47 48 uint32_t getPhdrFlags() const; 49 50 // Pointer to the PT_LOAD segment, which this section resides in. This field 51 // is used to correctly compute file offset of a section. When two sections 52 // share the same load segment, difference between their file offsets should 53 // be equal to difference between their virtual addresses. To compute some 54 // section offset we use the following formula: Off = Off_first + VA - 55 // VA_first, where Off_first and VA_first is file offset and VA of first 56 // section in PT_LOAD. 57 PhdrEntry *ptLoad = nullptr; 58 59 // Pointer to a relocation section for this section. Usually nullptr because 60 // we consume relocations, but if --emit-relocs is specified (which is rare), 61 // it may have a non-null value. 62 OutputSection *relocationSection = nullptr; 63 64 // Initially this field is the number of InputSections that have been added to 65 // the OutputSection so far. Later on, after a call to assignAddresses, it 66 // corresponds to the Elf_Shdr member. 67 uint64_t size = 0; 68 69 // The following fields correspond to Elf_Shdr members. 70 uint64_t offset = 0; 71 uint64_t addr = 0; 72 uint32_t shName = 0; 73 74 void recordSection(InputSectionBase *isec); 75 void commitSection(InputSection *isec); 76 void finalizeInputSections(); 77 78 // The following members are normally only used in linker scripts. 79 MemoryRegion *memRegion = nullptr; 80 MemoryRegion *lmaRegion = nullptr; 81 Expr addrExpr; 82 Expr alignExpr; 83 Expr lmaExpr; 84 Expr subalignExpr; 85 std::vector<BaseCommand *> sectionCommands; 86 std::vector<StringRef> phdrs; 87 llvm::Optional<std::array<uint8_t, 4>> filler; 88 ConstraintKind constraint = ConstraintKind::NoConstraint; 89 std::string location; 90 std::string memoryRegionName; 91 std::string lmaRegionName; 92 bool nonAlloc = false; 93 bool noload = false; 94 bool expressionsUseSymbols = false; 95 bool usedInExpression = false; 96 bool inOverlay = false; 97 98 // Tracks whether the section has ever had an input section added to it, even 99 // if the section was later removed (e.g. because it is a synthetic section 100 // that wasn't needed). This is needed for orphan placement. 101 bool hasInputSections = false; 102 103 void finalize(); 104 template <class ELFT> void writeTo(uint8_t *buf); 105 // Check that the addends for dynamic relocations were written correctly. 106 void checkDynRelAddends(const uint8_t *bufStart); 107 template <class ELFT> void maybeCompress(); 108 109 void sort(llvm::function_ref<int(InputSectionBase *s)> order); 110 void sortInitFini(); 111 void sortCtorsDtors(); 112 113 private: 114 // Used for implementation of --compress-debug-sections option. 115 std::vector<uint8_t> zDebugHeader; 116 llvm::SmallVector<char, 0> compressedData; 117 118 std::array<uint8_t, 4> getFiller(); 119 }; 120 121 int getPriority(StringRef s); 122 123 InputSection *getFirstInputSection(const OutputSection *os); 124 std::vector<InputSection *> getInputSections(const OutputSection *os); 125 126 // All output sections that are handled by the linker specially are 127 // globally accessible. Writer initializes them, so don't use them 128 // until Writer is initialized. 129 struct Out { 130 static uint8_t *bufferStart; 131 static uint8_t first; 132 static PhdrEntry *tlsPhdr; 133 static OutputSection *elfHeader; 134 static OutputSection *programHeaders; 135 static OutputSection *preinitArray; 136 static OutputSection *initArray; 137 static OutputSection *finiArray; 138 }; 139 140 uint64_t getHeaderSize(); 141 142 extern std::vector<OutputSection *> outputSections; 143 } // namespace elf 144 } // namespace lld 145 146 #endif 147