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 "InputSection.h" 13 #include "LinkerScript.h" 14 #include "lld/Common/LLVM.h" 15 16 #include <array> 17 18 namespace lld { 19 namespace elf { 20 21 struct PhdrEntry; 22 23 struct CompressedData { 24 std::unique_ptr<SmallVector<uint8_t, 0>[]> shards; 25 uint32_t numShards = 0; 26 uint32_t checksum = 0; 27 uint64_t uncompressedSize; 28 }; 29 30 // This represents a section in an output file. 31 // It is composed of multiple InputSections. 32 // The writer creates multiple OutputSections and assign them unique, 33 // non-overlapping file offsets and VAs. 34 class OutputSection final : public SectionBase { 35 public: 36 OutputSection(StringRef name, uint32_t type, uint64_t flags); 37 38 static bool classof(const SectionBase *s) { 39 return s->kind() == SectionBase::Output; 40 } 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 SmallVector<SectionCommand *, 0> commands; 86 SmallVector<StringRef, 0> 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 typeIsSet = 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 // The output section description is specified between DATA_SEGMENT_ALIGN and 104 // DATA_RELRO_END. 105 bool relro = false; 106 107 void finalize(); 108 template <class ELFT> void writeTo(uint8_t *buf); 109 // Check that the addends for dynamic relocations were written correctly. 110 void checkDynRelAddends(const uint8_t *bufStart); 111 template <class ELFT> void maybeCompress(); 112 113 void sort(llvm::function_ref<int(InputSectionBase *s)> order); 114 void sortInitFini(); 115 void sortCtorsDtors(); 116 117 private: 118 // Used for implementation of --compress-debug-sections option. 119 CompressedData compressed; 120 121 std::array<uint8_t, 4> getFiller(); 122 }; 123 124 struct OutputDesc final : SectionCommand { 125 OutputSection osec; 126 OutputDesc(StringRef name, uint32_t type, uint64_t flags) 127 : SectionCommand(OutputSectionKind), osec(name, type, flags) {} 128 129 static bool classof(const SectionCommand *c) { 130 return c->kind == OutputSectionKind; 131 } 132 }; 133 134 int getPriority(StringRef s); 135 136 InputSection *getFirstInputSection(const OutputSection *os); 137 llvm::ArrayRef<InputSection *> 138 getInputSections(const OutputSection &os, 139 SmallVector<InputSection *, 0> &storage); 140 141 // All output sections that are handled by the linker specially are 142 // globally accessible. Writer initializes them, so don't use them 143 // until Writer is initialized. 144 struct Out { 145 static uint8_t *bufferStart; 146 static PhdrEntry *tlsPhdr; 147 static OutputSection *elfHeader; 148 static OutputSection *programHeaders; 149 static OutputSection *preinitArray; 150 static OutputSection *initArray; 151 static OutputSection *finiArray; 152 }; 153 154 uint64_t getHeaderSize(); 155 156 extern llvm::SmallVector<OutputSection *, 0> outputSections; 157 } // namespace elf 158 } // namespace lld 159 160 #endif 161