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 addSection(InputSection *isec); 75 76 // The following members are normally only used in linker scripts. 77 MemoryRegion *memRegion = nullptr; 78 MemoryRegion *lmaRegion = nullptr; 79 Expr addrExpr; 80 Expr alignExpr; 81 Expr lmaExpr; 82 Expr subalignExpr; 83 std::vector<BaseCommand *> sectionCommands; 84 std::vector<StringRef> phdrs; 85 llvm::Optional<std::array<uint8_t, 4>> filler; 86 ConstraintKind constraint = ConstraintKind::NoConstraint; 87 std::string location; 88 std::string memoryRegionName; 89 std::string lmaRegionName; 90 bool nonAlloc = false; 91 bool noload = false; 92 bool expressionsUseSymbols = false; 93 bool usedInExpression = false; 94 bool inOverlay = false; 95 96 // Tracks whether the section has ever had an input section added to it, even 97 // if the section was later removed (e.g. because it is a synthetic section 98 // that wasn't needed). This is needed for orphan placement. 99 bool hasInputSections = false; 100 101 void finalize(); 102 template <class ELFT> void writeTo(uint8_t *buf); 103 template <class ELFT> void maybeCompress(); 104 105 void sort(llvm::function_ref<int(InputSectionBase *s)> order); 106 void sortInitFini(); 107 void sortCtorsDtors(); 108 109 private: 110 // Used for implementation of --compress-debug-sections option. 111 std::vector<uint8_t> zDebugHeader; 112 llvm::SmallVector<char, 1> compressedData; 113 114 std::array<uint8_t, 4> getFiller(); 115 }; 116 117 int getPriority(StringRef s); 118 119 std::vector<InputSection *> getInputSections(OutputSection* os); 120 121 // All output sections that are handled by the linker specially are 122 // globally accessible. Writer initializes them, so don't use them 123 // until Writer is initialized. 124 struct Out { 125 static uint8_t *bufferStart; 126 static uint8_t first; 127 static PhdrEntry *tlsPhdr; 128 static OutputSection *elfHeader; 129 static OutputSection *programHeaders; 130 static OutputSection *preinitArray; 131 static OutputSection *initArray; 132 static OutputSection *finiArray; 133 }; 134 135 } // namespace elf 136 } // namespace lld 137 138 namespace lld { 139 namespace elf { 140 141 uint64_t getHeaderSize(); 142 143 extern std::vector<OutputSection *> outputSections; 144 } // namespace elf 145 } // namespace lld 146 147 #endif 148