blob: 58e12f827170f6099a1954574266d3f2b08a042c [file] [log] [blame]
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001//===- 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 "lld/Core/LLVM.h"
14
15#include "llvm/MC/StringTableBuilder.h"
16#include "llvm/Object/ELF.h"
17
Davide Italiano85121bb2015-09-25 03:56:11 +000018#include "Config.h"
19
Rafael Espindola5805c4f2015-09-21 21:38:08 +000020#include <type_traits>
21
22namespace lld {
23namespace elf2 {
24
25class SymbolBody;
26class SymbolTable;
27template <class ELFT> class SymbolTableSection;
28template <bool Is64Bits> class StringTableSection;
29template <class ELFT> class InputSection;
30template <class ELFT> class OutputSection;
31template <class ELFT> class ObjectFile;
32template <class ELFT> class DefinedRegular;
Rafael Espindolacd076f02015-09-25 18:19:03 +000033template <class ELFT> class ELFSymbolBody;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000034
Rafael Espindola5805c4f2015-09-21 21:38:08 +000035template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000036typename llvm::object::ELFFile<ELFT>::uintX_t getSymVA(const SymbolBody &S);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000037
38template <class ELFT>
39typename llvm::object::ELFFile<ELFT>::uintX_t
40getLocalSymVA(const typename llvm::object::ELFFile<ELFT>::Elf_Sym *Sym,
41 const ObjectFile<ELFT> &File);
Rafael Espindolaa6627382015-10-06 23:56:53 +000042bool canBePreempted(const SymbolBody *Body);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000043template <class ELFT> bool includeInSymtab(const SymbolBody &B);
44
Rafael Espindola05a3dd22015-09-22 23:38:23 +000045bool includeInDynamicSymtab(const SymbolBody &B);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000046
47template <class ELFT>
48bool shouldKeepInSymtab(
49 StringRef Name, const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym);
Davide Italiano85121bb2015-09-25 03:56:11 +000050
Rafael Espindola71675852015-09-22 00:16:19 +000051// This represents a section in an output file.
52// Different sub classes represent different types of sections. Some contain
53// input sections, others are created by the linker.
54// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000055// non-overlapping file offsets and VAs.
56template <bool Is64Bits> class OutputSectionBase {
57public:
58 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
59 typedef typename std::conditional<Is64Bits, llvm::ELF::Elf64_Shdr,
60 llvm::ELF::Elf32_Shdr>::type HeaderT;
61
62 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
63 void setVA(uintX_t VA) { Header.sh_addr = VA; }
64 uintX_t getVA() const { return Header.sh_addr; }
65 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
66 template <llvm::object::endianness E>
67 void writeHeaderTo(typename llvm::object::ELFFile<
68 llvm::object::ELFType<E, Is64Bits>>::Elf_Shdr *SHdr);
69 StringRef getName() { return Name; }
70 void setNameOffset(uintX_t Offset) { Header.sh_name = Offset; }
71
72 unsigned getSectionIndex() const { return SectionIndex; }
73 void setSectionIndex(unsigned I) { SectionIndex = I; }
74
75 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000076 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000077 void setSize(uintX_t Val) { Header.sh_size = Val; }
78 uintX_t getFlags() { return Header.sh_flags; }
79 uintX_t getFileOff() { return Header.sh_offset; }
80 uintX_t getAlign() {
81 // The ELF spec states that a value of 0 means the section has no alignment
82 // constraits.
83 return std::max<uintX_t>(Header.sh_addralign, 1);
84 }
85 uint32_t getType() { return Header.sh_type; }
86
87 static unsigned getAddrSize() { return Is64Bits ? 8 : 4; }
88
89 virtual void finalize() {}
90 virtual void writeTo(uint8_t *Buf) = 0;
91
92protected:
93 StringRef Name;
94 HeaderT Header;
95 unsigned SectionIndex;
96 ~OutputSectionBase() = default;
97};
98
99template <class ELFT>
100class GotSection final : public OutputSectionBase<ELFT::Is64Bits> {
101 typedef OutputSectionBase<ELFT::Is64Bits> Base;
102 typedef typename Base::uintX_t uintX_t;
103
104public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000105 GotSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000106 void finalize() override {
107 this->Header.sh_size = Entries.size() * this->getAddrSize();
108 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000109 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000110 void addEntry(SymbolBody *Sym);
111 bool empty() const { return Entries.empty(); }
112 uintX_t getEntryAddr(const SymbolBody &B) const;
113
114private:
115 std::vector<const SymbolBody *> Entries;
116};
117
118template <class ELFT>
119class PltSection final : public OutputSectionBase<ELFT::Is64Bits> {
120 typedef OutputSectionBase<ELFT::Is64Bits> Base;
121 typedef typename Base::uintX_t uintX_t;
122
123public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000124 PltSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000125 void finalize() override {
126 this->Header.sh_size = Entries.size() * EntrySize;
127 }
128 void writeTo(uint8_t *Buf) override;
129 void addEntry(SymbolBody *Sym);
130 bool empty() const { return Entries.empty(); }
131 uintX_t getEntryAddr(const SymbolBody &B) const;
132 static const unsigned EntrySize = 8;
133
134private:
135 std::vector<const SymbolBody *> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000136};
137
138template <class ELFT> struct DynamicReloc {
139 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
140 const InputSection<ELFT> &C;
141 const Elf_Rel &RI;
142};
143
144template <class ELFT>
145class SymbolTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
146public:
147 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
148 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
149 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
150 typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
151 SymbolTableSection(SymbolTable &Table,
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000152 StringTableSection<ELFT::Is64Bits> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000153
Rui Ueyama0db335f2015-10-07 16:58:54 +0000154 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000155 void writeTo(uint8_t *Buf) override;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000156 SymbolTable &getSymTable() const { return Table; }
Rui Ueyama0db335f2015-10-07 16:58:54 +0000157 void addSymbol(StringRef Name, bool isLocal = false);
Davide Italianoe44456b2015-09-23 01:50:53 +0000158 StringTableSection<ELFT::Is64Bits> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000159 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000160
161private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000162 void writeLocalSymbols(uint8_t *&Buf);
163 void writeGlobalSymbols(uint8_t *&Buf);
164
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000165 SymbolTable &Table;
166 StringTableSection<ELFT::Is64Bits> &StrTabSec;
167 unsigned NumVisible = 0;
168 unsigned NumLocals = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000169};
170
171template <class ELFT>
172class RelocationSection final : public OutputSectionBase<ELFT::Is64Bits> {
173 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
174 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000175 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000176
177public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000178 RelocationSection(bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000179 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
180 void finalize() override;
181 void writeTo(uint8_t *Buf) override;
182 bool hasRelocs() const { return !Relocs.empty(); }
183 bool isRela() const { return IsRela; }
184
185private:
186 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000187 const bool IsRela;
188};
189
190template <class ELFT>
191class OutputSection final : public OutputSectionBase<ELFT::Is64Bits> {
192public:
193 typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
194 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
195 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
196 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
197 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000198 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rafael Espindola71675852015-09-22 00:16:19 +0000199 void addSection(InputSection<ELFT> *C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000200 void writeTo(uint8_t *Buf) override;
201
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000202private:
Rafael Espindola71675852015-09-22 00:16:19 +0000203 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000204};
205
206template <bool Is64Bits>
207class InterpSection final : public OutputSectionBase<Is64Bits> {
208public:
209 InterpSection();
210
211 void writeTo(uint8_t *Buf);
212};
213
214template <bool Is64Bits>
215class StringTableSection final : public OutputSectionBase<Is64Bits> {
216public:
217 typedef typename OutputSectionBase<Is64Bits>::uintX_t uintX_t;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000218 StringTableSection(bool Dynamic);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000219 void add(StringRef S) { StrTabBuilder.add(S); }
220 size_t getFileOff(StringRef S) const { return StrTabBuilder.getOffset(S); }
221 StringRef data() const { return StrTabBuilder.data(); }
222 void writeTo(uint8_t *Buf) override;
223
224 void finalize() override {
225 StrTabBuilder.finalize(llvm::StringTableBuilder::ELF);
226 this->Header.sh_size = StrTabBuilder.data().size();
227 }
228
229 bool isDynamic() const { return Dynamic; }
230
231private:
232 const bool Dynamic;
233 llvm::StringTableBuilder StrTabBuilder;
234};
235
236template <class ELFT>
237class HashTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
238 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
239
240public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000241 HashTableSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000242 void addSymbol(SymbolBody *S);
Rui Ueyama0db335f2015-10-07 16:58:54 +0000243 void finalize() override;
244 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000245
246private:
247 uint32_t hash(StringRef Name) {
248 uint32_t H = 0;
249 for (char C : Name) {
250 H = (H << 4) + C;
251 uint32_t G = H & 0xf0000000;
252 if (G)
253 H ^= G >> 24;
254 H &= ~G;
255 }
256 return H;
257 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000258 std::vector<uint32_t> Hashes;
259};
260
261template <class ELFT>
262class DynamicSection final : public OutputSectionBase<ELFT::Is64Bits> {
263 typedef OutputSectionBase<ELFT::Is64Bits> Base;
264 typedef typename Base::HeaderT HeaderT;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000265 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
266 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
267 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Hal Finkeld26da922015-10-02 16:21:30 +0000268 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000269
270public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000271 DynamicSection(SymbolTable &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000272 void finalize() override;
273 void writeTo(uint8_t *Buf) override;
274
Rafael Espindola77572242015-10-02 19:37:55 +0000275 OutputSection<ELFT> *PreInitArraySec = nullptr;
276 OutputSection<ELFT> *InitArraySec = nullptr;
277 OutputSection<ELFT> *FiniArraySec = nullptr;
278
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000279private:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000280 SymbolTable &SymTab;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000281 const ELFSymbolBody<ELFT> *InitSym = nullptr;
282 const ELFSymbolBody<ELFT> *FiniSym = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000283};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000284
285// All output sections that are hadnled by the linker specially are
286// globally accessible. Writer initializes them, so don't use them
287// until Writer is initialized.
288template <class ELFT> struct Out {
289 static DynamicSection<ELFT> *Dynamic;
290 static GotSection<ELFT> *Got;
291 static HashTableSection<ELFT> *HashTab;
292 static InterpSection<ELFT::Is64Bits> *Interp;
293 static OutputSection<ELFT> *Bss;
294 static PltSection<ELFT> *Plt;
295 static RelocationSection<ELFT> *RelaDyn;
296 static StringTableSection<ELFT::Is64Bits> *DynStrTab;
297 static StringTableSection<ELFT::Is64Bits> *StrTab;
298 static SymbolTableSection<ELFT> *DynSymTab;
299 static SymbolTableSection<ELFT> *SymTab;
300};
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000301}
302}
303#endif