1 //===- OutputSections.h -----------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLD_ELF_OUTPUT_SECTIONS_H 11 #define LLD_ELF_OUTPUT_SECTIONS_H 12 13 #include "Config.h" 14 #include "InputSection.h" 15 #include "Relocations.h" 16 17 #include "lld/Core/LLVM.h" 18 #include "llvm/MC/StringTableBuilder.h" 19 #include "llvm/Object/ELF.h" 20 21 namespace lld { 22 namespace elf { 23 24 struct PhdrEntry; 25 class SymbolBody; 26 struct EhSectionPiece; 27 class EhInputSection; 28 class InputSection; 29 class InputSectionBase; 30 class MergeInputSection; 31 class OutputSection; 32 template <class ELFT> class ObjectFile; 33 template <class ELFT> class SharedFile; 34 class SharedSymbol; 35 class DefinedRegular; 36 37 // This represents a section in an output file. 38 // It is composed of multiple InputSections. 39 // The writer creates multiple OutputSections and assign them unique, 40 // non-overlapping file offsets and VAs. 41 class OutputSection final : public SectionBase { 42 public: 43 OutputSection(StringRef Name, uint32_t Type, uint64_t Flags); 44 45 static bool classof(const SectionBase *S) { 46 return S->kind() == SectionBase::Output; 47 } 48 49 uint64_t getLMA() const { return Addr + LMAOffset; } 50 template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr); 51 52 unsigned SectionIndex; 53 unsigned SortRank; 54 55 uint32_t getPhdrFlags() const; 56 57 void updateAlignment(uint32_t Val) { 58 if (Val > Alignment) 59 Alignment = Val; 60 } 61 62 // Pointer to the first section in PT_LOAD segment, which this section 63 // also resides in. This field is used to correctly compute file offset 64 // of a section. When two sections share the same load segment, difference 65 // between their file offsets should be equal to difference between their 66 // virtual addresses. To compute some section offset we use the following 67 // formula: Off = Off_first + VA - VA_first. 68 OutputSection *FirstInPtLoad = nullptr; 69 70 // Pointer to a relocation section for this section. Usually nullptr because 71 // we consume relocations, but if --emit-relocs is specified (which is rare), 72 // it may have a non-null value. 73 OutputSection *RelocationSection = nullptr; 74 75 // The following fields correspond to Elf_Shdr members. 76 uint64_t Size = 0; 77 uint64_t Offset = 0; 78 uint64_t LMAOffset = 0; 79 uint64_t Addr = 0; 80 uint32_t ShName = 0; 81 82 void addSection(InputSection *S); 83 void sort(std::function<int(InputSectionBase *S)> Order); 84 void sortInitFini(); 85 void sortCtorsDtors(); 86 void assignOffsets(); 87 std::vector<InputSection *> Sections; 88 89 // Used for implementation of --compress-debug-sections option. 90 std::vector<uint8_t> ZDebugHeader; 91 llvm::SmallVector<char, 1> CompressedData; 92 93 // Location in the output buffer. 94 uint8_t *Loc = nullptr; 95 }; 96 97 // All output sections that are handled by the linker specially are 98 // globally accessible. Writer initializes them, so don't use them 99 // until Writer is initialized. 100 struct Out { 101 static uint8_t First; 102 static OutputSection *Opd; 103 static uint8_t *OpdBuf; 104 static PhdrEntry *TlsPhdr; 105 static OutputSection *DebugInfo; 106 static OutputSection *ElfHeader; 107 static OutputSection *ProgramHeaders; 108 static OutputSection *PreinitArray; 109 static OutputSection *InitArray; 110 static OutputSection *FiniArray; 111 }; 112 113 struct SectionKey { 114 StringRef Name; 115 uint64_t Flags; 116 uint32_t Alignment; 117 }; 118 } 119 } 120 namespace llvm { 121 template <> struct DenseMapInfo<lld::elf::SectionKey> { 122 static lld::elf::SectionKey getEmptyKey(); 123 static lld::elf::SectionKey getTombstoneKey(); 124 static unsigned getHashValue(const lld::elf::SectionKey &Val); 125 static bool isEqual(const lld::elf::SectionKey &LHS, 126 const lld::elf::SectionKey &RHS); 127 }; 128 } 129 namespace lld { 130 namespace elf { 131 132 // This class knows how to create an output section for a given 133 // input section. Output section type is determined by various 134 // factors, including input section's sh_flags, sh_type and 135 // linker scripts. 136 class OutputSectionFactory { 137 public: 138 OutputSectionFactory(std::vector<OutputSection *> &OutputSections); 139 ~OutputSectionFactory(); 140 141 void addInputSec(InputSectionBase *IS, StringRef OutsecName); 142 void addInputSec(InputSectionBase *IS, StringRef OutsecName, 143 OutputSection *&Sec); 144 145 private: 146 llvm::SmallDenseMap<SectionKey, OutputSection *> Map; 147 std::vector<OutputSection *> &OutputSections; 148 }; 149 150 uint64_t getHeaderSize(); 151 void reportDiscarded(InputSectionBase *IS); 152 153 extern std::vector<OutputSection *> OutputSections; 154 extern std::vector<OutputSectionCommand *> OutputSectionCommands; 155 } // namespace elf 156 } // namespace lld 157 158 159 #endif 160