blob: 7814222cccc85ccbf76ca77e1e362070027740fb [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
Rafael Espindolac159c962015-10-19 21:00:02 +000015#include "llvm/ADT/MapVector.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000016#include "llvm/MC/StringTableBuilder.h"
17#include "llvm/Object/ELF.h"
18
Davide Italiano85121bb2015-09-25 03:56:11 +000019#include "Config.h"
20
Rafael Espindola5805c4f2015-09-21 21:38:08 +000021#include <type_traits>
22
23namespace lld {
24namespace elf2 {
25
26class SymbolBody;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000027template <class ELFT> class SymbolTable;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000028template <class ELFT> class SymbolTableSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000029template <class ELFT> class StringTableSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000030template <class ELFT> class EHInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000031template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000032template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000033template <class ELFT> class MergeInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000034template <class ELFT> class OutputSection;
35template <class ELFT> class ObjectFile;
36template <class ELFT> class DefinedRegular;
Rafael Espindolacd076f02015-09-25 18:19:03 +000037template <class ELFT> class ELFSymbolBody;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000038
Rafael Espindola5805c4f2015-09-21 21:38:08 +000039template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000040static inline typename llvm::object::ELFFile<ELFT>::uintX_t
41getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rel &Rel) {
42 return 0;
43}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000044
45template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000046static inline typename llvm::object::ELFFile<ELFT>::uintX_t
47getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rela &Rel) {
48 return Rel.r_addend;
49}
50
51template <class ELFT>
52typename llvm::object::ELFFile<ELFT>::uintX_t getSymVA(const SymbolBody &S);
53
54template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +000055typename llvm::object::ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +000056getLocalRelTarget(const ObjectFile<ELFT> &File,
Rafael Espindola932efcf2015-10-19 20:24:44 +000057 const llvm::object::Elf_Rel_Impl<ELFT, IsRela> &Rel);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +000058bool canBePreempted(const SymbolBody *Body, bool NeedsGot);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000059template <class ELFT> bool includeInSymtab(const SymbolBody &B);
60
Rafael Espindola05a3dd22015-09-22 23:38:23 +000061bool includeInDynamicSymtab(const SymbolBody &B);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000062
63template <class ELFT>
64bool shouldKeepInSymtab(
Rafael Espindola444576d2015-10-09 19:25:07 +000065 const ObjectFile<ELFT> &File, StringRef Name,
66 const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym);
Davide Italiano85121bb2015-09-25 03:56:11 +000067
Rafael Espindola71675852015-09-22 00:16:19 +000068// This represents a section in an output file.
69// Different sub classes represent different types of sections. Some contain
70// input sections, others are created by the linker.
71// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000072// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000073template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000074public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000075 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
76 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000077
78 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
79 void setVA(uintX_t VA) { Header.sh_addr = VA; }
80 uintX_t getVA() const { return Header.sh_addr; }
81 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000082 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000083 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000084
Rui Ueyama2317d0d2015-10-15 20:55:22 +000085 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000086
87 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000088 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000089 void setSize(uintX_t Val) { Header.sh_size = Val; }
90 uintX_t getFlags() { return Header.sh_flags; }
91 uintX_t getFileOff() { return Header.sh_offset; }
92 uintX_t getAlign() {
93 // The ELF spec states that a value of 0 means the section has no alignment
94 // constraits.
95 return std::max<uintX_t>(Header.sh_addralign, 1);
96 }
97 uint32_t getType() { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +000098 void updateAlign(uintX_t Align) {
99 if (Align > Header.sh_addralign)
100 Header.sh_addralign = Align;
101 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000102
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000103 virtual void finalize() {}
104 virtual void writeTo(uint8_t *Buf) = 0;
105
106protected:
107 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000108 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000109 ~OutputSectionBase() = default;
110};
111
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000112template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
113 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000114 typedef typename Base::uintX_t uintX_t;
115
116public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000117 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000118 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000119 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000120 void addEntry(SymbolBody *Sym);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000121 uint32_t addLocalModuleTlsIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000122 bool empty() const { return Entries.empty(); }
123 uintX_t getEntryAddr(const SymbolBody &B) const;
124
125private:
126 std::vector<const SymbolBody *> Entries;
127};
128
George Rimar648a2c32015-10-20 08:54:27 +0000129template <class ELFT>
130class GotPltSection final : public OutputSectionBase<ELFT> {
131 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
132
133public:
134 GotPltSection();
135 void finalize() override;
136 void writeTo(uint8_t *Buf) override;
137 void addEntry(SymbolBody *Sym);
138 bool empty() const;
139 uintX_t getEntryAddr(const SymbolBody &B) const;
140
141private:
142 std::vector<const SymbolBody *> Entries;
143};
144
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000145template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
146 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000147 typedef typename Base::uintX_t uintX_t;
148
149public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000150 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000151 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000152 void writeTo(uint8_t *Buf) override;
153 void addEntry(SymbolBody *Sym);
154 bool empty() const { return Entries.empty(); }
155 uintX_t getEntryAddr(const SymbolBody &B) const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000156
157private:
158 std::vector<const SymbolBody *> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000159};
160
161template <class ELFT> struct DynamicReloc {
162 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000163 InputSectionBase<ELFT> &C;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000164 const Elf_Rel &RI;
165};
166
167template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000168class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000169public:
170 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
171 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
172 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000173 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000174 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000175 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000176
Rui Ueyama0db335f2015-10-07 16:58:54 +0000177 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000178 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000179 void addLocalSymbol(StringRef Name);
180 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000181 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000182 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000183
Igor Kudrinf1d60292015-10-28 07:05:56 +0000184 ArrayRef<SymbolBody *> getSymbols() const { return Symbols; }
Igor Kudrinab665fc2015-10-20 21:47:58 +0000185
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000186private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000187 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000188 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000189
Igor Kudrin853b88d2015-10-20 20:52:14 +0000190 static uint8_t getSymbolBinding(SymbolBody *Body);
191
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000192 SymbolTable<ELFT> &Table;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000193 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000194 std::vector<SymbolBody *> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000195 unsigned NumVisible = 0;
196 unsigned NumLocals = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000197};
198
199template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000200class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000201 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
202 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000203 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000204
205public:
George Rimar648a2c32015-10-20 08:54:27 +0000206 RelocationSection(StringRef Name, bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000207 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
208 void finalize() override;
209 void writeTo(uint8_t *Buf) override;
210 bool hasRelocs() const { return !Relocs.empty(); }
211 bool isRela() const { return IsRela; }
212
213private:
214 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000215 const bool IsRela;
216};
217
218template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000219class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000220public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000221 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
222 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
223 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
224 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000225 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000226 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rafael Espindola71675852015-09-22 00:16:19 +0000227 void addSection(InputSection<ELFT> *C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000228 void writeTo(uint8_t *Buf) override;
229
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000230private:
Rafael Espindola71675852015-09-22 00:16:19 +0000231 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000232};
233
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000234template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000235class MergeOutputSection final : public OutputSectionBase<ELFT> {
236 typedef typename OutputSectionBase<ELFT>::uintX_t uintX_t;
237
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000238 bool shouldTailMerge() const;
239
Rafael Espindolac159c962015-10-19 21:00:02 +0000240public:
241 MergeOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
242 void addSection(MergeInputSection<ELFT> *S);
243 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000244 unsigned getOffset(StringRef Val);
245 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000246
247private:
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000248 llvm::StringTableBuilder Builder{llvm::StringTableBuilder::RAW};
Rafael Espindolac159c962015-10-19 21:00:02 +0000249};
250
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000251// FDE or CIE
252template <class ELFT> struct EHRegion {
253 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
254 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
255 StringRef data() const;
256 EHInputSection<ELFT> *S;
257 unsigned Index;
258};
259
260template <class ELFT> struct Cie : public EHRegion<ELFT> {
261 Cie(EHInputSection<ELFT> *S, unsigned Index);
262 std::vector<EHRegion<ELFT>> Fdes;
263};
264
265template <class ELFT>
266class EHOutputSection final : public OutputSectionBase<ELFT> {
267public:
268 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
269 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
270 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
271 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
272 EHOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
273 void writeTo(uint8_t *Buf) override;
274
275 template <bool IsRela>
276 void addSectionAux(
277 EHInputSection<ELFT> *S,
278 llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, IsRela> *>
279 Rels);
280
281 void addSection(EHInputSection<ELFT> *S);
282
283private:
284 std::vector<EHInputSection<ELFT> *> Sections;
285 std::vector<Cie<ELFT>> Cies;
286
287 // Maps CIE content + personality to a index in Cies.
288 llvm::DenseMap<std::pair<StringRef, StringRef>, unsigned> CieMap;
289};
290
Rafael Espindolac159c962015-10-19 21:00:02 +0000291template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000292class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000293public:
294 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000295 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000296};
297
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000298template <class ELFT>
299class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000300public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000301 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000302 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000303 void add(StringRef S) { StrTabBuilder.add(S); }
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000304 size_t getOffset(StringRef S) const { return StrTabBuilder.getOffset(S); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000305 StringRef data() const { return StrTabBuilder.data(); }
306 void writeTo(uint8_t *Buf) override;
307
308 void finalize() override {
Rafael Espindola6779a232015-10-23 21:48:35 +0000309 StrTabBuilder.finalize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000310 this->Header.sh_size = StrTabBuilder.data().size();
311 }
312
313 bool isDynamic() const { return Dynamic; }
314
315private:
316 const bool Dynamic;
Rafael Espindola6779a232015-10-23 21:48:35 +0000317 llvm::StringTableBuilder StrTabBuilder{llvm::StringTableBuilder::ELF};
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000318};
319
320template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000321class HashTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000322 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
323
324public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000325 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000326 void finalize() override;
327 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000328};
329
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000330// Outputs GNU Hash section. For detailed explanation see:
331// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
332template <class ELFT>
333class GnuHashTableSection final : public OutputSectionBase<ELFT> {
334 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
335 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
336 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
337
338public:
339 GnuHashTableSection();
340 void finalize() override;
341 void writeTo(uint8_t *Buf) override;
342
Igor Kudrinf1d60292015-10-28 07:05:56 +0000343 // Adds symbols to the hash table.
344 // Sorts the input to satisfy GNU hash section requirements.
345 void addSymbols(std::vector<SymbolBody *> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000346
347private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000348 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000349 static unsigned calcMaskWords(unsigned NumHashed);
350
351 void writeHeader(uint8_t *&Buf);
352 void writeBloomFilter(uint8_t *&Buf);
353 void writeHashTable(uint8_t *Buf);
354
Igor Kudrinf1d60292015-10-28 07:05:56 +0000355 struct HashedSymbolData {
356 SymbolBody *Body;
357 uint32_t Hash;
358 };
359
360 std::vector<HashedSymbolData> HashedSymbols;
361
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000362 unsigned MaskWords;
363 unsigned NBuckets;
364 unsigned Shift2;
365};
366
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000367template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000368class DynamicSection final : public OutputSectionBase<ELFT> {
369 typedef OutputSectionBase<ELFT> Base;
370 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000371 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
372 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000373 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000374 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000375
376public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000377 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000378 void finalize() override;
379 void writeTo(uint8_t *Buf) override;
380
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000381 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
382 OutputSectionBase<ELFT> *InitArraySec = nullptr;
383 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000384
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000385private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000386 SymbolTable<ELFT> &SymTab;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000387 const ELFSymbolBody<ELFT> *InitSym = nullptr;
388 const ELFSymbolBody<ELFT> *FiniSym = nullptr;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000389 uint32_t DtFlags = 0;
390 uint32_t DtFlags1 = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000391};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000392
393// All output sections that are hadnled by the linker specially are
394// globally accessible. Writer initializes them, so don't use them
395// until Writer is initialized.
396template <class ELFT> struct Out {
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000397 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000398 typedef typename llvm::object::ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000399 static DynamicSection<ELFT> *Dynamic;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000400 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000401 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000402 static GotSection<ELFT> *Got;
403 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000404 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000405 static OutputSection<ELFT> *Bss;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000406 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000407 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000408 static PltSection<ELFT> *Plt;
409 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000410 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000411 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000412 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000413 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000414 static SymbolTableSection<ELFT> *DynSymTab;
415 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000416 static Elf_Phdr *TlsPhdr;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000417 static uint32_t LocalModuleTlsIndexOffset;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000418};
Rui Ueyamad888d102015-10-09 19:34:55 +0000419
420template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000421template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000422template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000423template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
424template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000425template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000426template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000427template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000428template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000429template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
430template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000431template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000432template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000433template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000434template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000435template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
436template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000437template <class ELFT> typename Out<ELFT>::Elf_Phdr *Out<ELFT>::TlsPhdr;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000438template <class ELFT> uint32_t Out<ELFT>::LocalModuleTlsIndexOffset = -1;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000439
440} // namespace elf2
441} // namespace lld
442
443#endif // LLD_ELF_OUTPUT_SECTIONS_H