blob: dea9bb3c120e074d5943b5e449f0cf5dc320f39a [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);
George Rimar687138c2015-11-13 16:28:53 +0000121 void addDynTlsEntry(SymbolBody *Sym);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000122 uint32_t addLocalModuleTlsIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000123 bool empty() const { return Entries.empty(); }
124 uintX_t getEntryAddr(const SymbolBody &B) const;
125
Igor Kudrin304860a2015-11-12 04:39:49 +0000126 // Returns the symbol which corresponds to the first entry of the global part
127 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
128 // table properties.
129 // Returns nullptr if the global part is empty.
130 const SymbolBody *getMipsFirstGlobalEntry() const;
131
132 // Returns the number of entries in the local part of GOT including
133 // the number of reserved entries. This method is MIPS-specific.
134 unsigned getMipsLocalEntriesNum() const;
135
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000136private:
137 std::vector<const SymbolBody *> Entries;
138};
139
George Rimar648a2c32015-10-20 08:54:27 +0000140template <class ELFT>
141class GotPltSection final : public OutputSectionBase<ELFT> {
142 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
143
144public:
145 GotPltSection();
146 void finalize() override;
147 void writeTo(uint8_t *Buf) override;
148 void addEntry(SymbolBody *Sym);
149 bool empty() const;
150 uintX_t getEntryAddr(const SymbolBody &B) const;
151
152private:
153 std::vector<const SymbolBody *> Entries;
154};
155
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000156template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
157 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000158 typedef typename Base::uintX_t uintX_t;
159
160public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000161 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000162 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000163 void writeTo(uint8_t *Buf) override;
164 void addEntry(SymbolBody *Sym);
165 bool empty() const { return Entries.empty(); }
166 uintX_t getEntryAddr(const SymbolBody &B) const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000167
168private:
George Rimar77b77792015-11-25 22:15:01 +0000169 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000170};
171
172template <class ELFT> struct DynamicReloc {
173 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000174 InputSectionBase<ELFT> *C;
175 const Elf_Rel *RI;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000176};
177
178template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000179class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000180public:
181 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
182 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
183 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000184 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000185 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000186 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000187
Rui Ueyama0db335f2015-10-07 16:58:54 +0000188 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000189 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000190 void addLocalSymbol(StringRef Name);
191 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000192 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000193 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000194
Igor Kudrinf1d60292015-10-28 07:05:56 +0000195 ArrayRef<SymbolBody *> getSymbols() const { return Symbols; }
Igor Kudrinab665fc2015-10-20 21:47:58 +0000196
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000197private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000198 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000199 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000200
Igor Kudrin853b88d2015-10-20 20:52:14 +0000201 static uint8_t getSymbolBinding(SymbolBody *Body);
202
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000203 SymbolTable<ELFT> &Table;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000204 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000205 std::vector<SymbolBody *> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000206 unsigned NumVisible = 0;
207 unsigned NumLocals = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000208};
209
210template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000211class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000212 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
213 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000214 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000215
216public:
George Rimar648a2c32015-10-20 08:54:27 +0000217 RelocationSection(StringRef Name, bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000218 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
George Rimar77b77792015-11-25 22:15:01 +0000219 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000220 void finalize() override;
221 void writeTo(uint8_t *Buf) override;
222 bool hasRelocs() const { return !Relocs.empty(); }
223 bool isRela() const { return IsRela; }
224
225private:
George Rimar5828c232015-11-30 17:49:19 +0000226 bool applyTlsDynamicReloc(SymbolBody *Body, uint32_t Type, Elf_Rel *P,
227 Elf_Rel *N);
228
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000229 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000230 const bool IsRela;
231};
232
233template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000234class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000235public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000236 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
237 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
238 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
239 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000240 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000241 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rafael Espindola71675852015-09-22 00:16:19 +0000242 void addSection(InputSection<ELFT> *C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000243 void writeTo(uint8_t *Buf) override;
244
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000245private:
Rafael Espindola71675852015-09-22 00:16:19 +0000246 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000247};
248
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000249template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000250class MergeOutputSection final : public OutputSectionBase<ELFT> {
251 typedef typename OutputSectionBase<ELFT>::uintX_t uintX_t;
252
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000253 bool shouldTailMerge() const;
254
Rafael Espindolac159c962015-10-19 21:00:02 +0000255public:
256 MergeOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
257 void addSection(MergeInputSection<ELFT> *S);
258 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000259 unsigned getOffset(StringRef Val);
260 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000261
262private:
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000263 llvm::StringTableBuilder Builder{llvm::StringTableBuilder::RAW};
Rafael Espindolac159c962015-10-19 21:00:02 +0000264};
265
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000266// FDE or CIE
267template <class ELFT> struct EHRegion {
268 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
269 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
270 StringRef data() const;
271 EHInputSection<ELFT> *S;
272 unsigned Index;
273};
274
275template <class ELFT> struct Cie : public EHRegion<ELFT> {
276 Cie(EHInputSection<ELFT> *S, unsigned Index);
277 std::vector<EHRegion<ELFT>> Fdes;
278};
279
280template <class ELFT>
281class EHOutputSection final : public OutputSectionBase<ELFT> {
282public:
283 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
284 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
285 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
286 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
287 EHOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
288 void writeTo(uint8_t *Buf) override;
289
290 template <bool IsRela>
291 void addSectionAux(
292 EHInputSection<ELFT> *S,
293 llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, IsRela> *>
294 Rels);
295
296 void addSection(EHInputSection<ELFT> *S);
297
298private:
299 std::vector<EHInputSection<ELFT> *> Sections;
300 std::vector<Cie<ELFT>> Cies;
301
302 // Maps CIE content + personality to a index in Cies.
303 llvm::DenseMap<std::pair<StringRef, StringRef>, unsigned> CieMap;
304};
305
Rafael Espindolac159c962015-10-19 21:00:02 +0000306template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000307class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000308public:
309 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000310 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000311};
312
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000313template <class ELFT>
314class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000315public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000316 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000317 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000318 void add(StringRef S) { StrTabBuilder.add(S); }
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000319 size_t getOffset(StringRef S) const { return StrTabBuilder.getOffset(S); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000320 StringRef data() const { return StrTabBuilder.data(); }
321 void writeTo(uint8_t *Buf) override;
322
323 void finalize() override {
Rafael Espindola6779a232015-10-23 21:48:35 +0000324 StrTabBuilder.finalize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000325 this->Header.sh_size = StrTabBuilder.data().size();
326 }
327
328 bool isDynamic() const { return Dynamic; }
329
330private:
331 const bool Dynamic;
Rafael Espindola6779a232015-10-23 21:48:35 +0000332 llvm::StringTableBuilder StrTabBuilder{llvm::StringTableBuilder::ELF};
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000333};
334
335template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000336class HashTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000337 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
338
339public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000340 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000341 void finalize() override;
342 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000343};
344
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000345// Outputs GNU Hash section. For detailed explanation see:
346// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
347template <class ELFT>
348class GnuHashTableSection final : public OutputSectionBase<ELFT> {
349 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
350 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
351 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
352
353public:
354 GnuHashTableSection();
355 void finalize() override;
356 void writeTo(uint8_t *Buf) override;
357
Igor Kudrinf1d60292015-10-28 07:05:56 +0000358 // Adds symbols to the hash table.
359 // Sorts the input to satisfy GNU hash section requirements.
360 void addSymbols(std::vector<SymbolBody *> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000361
362private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000363 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000364 static unsigned calcMaskWords(unsigned NumHashed);
365
366 void writeHeader(uint8_t *&Buf);
367 void writeBloomFilter(uint8_t *&Buf);
368 void writeHashTable(uint8_t *Buf);
369
Igor Kudrinf1d60292015-10-28 07:05:56 +0000370 struct HashedSymbolData {
371 SymbolBody *Body;
372 uint32_t Hash;
373 };
374
375 std::vector<HashedSymbolData> HashedSymbols;
376
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000377 unsigned MaskWords;
378 unsigned NBuckets;
379 unsigned Shift2;
380};
381
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000382template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000383class DynamicSection final : public OutputSectionBase<ELFT> {
384 typedef OutputSectionBase<ELFT> Base;
385 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000386 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
387 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000388 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000389 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000390
391public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000392 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000393 void finalize() override;
394 void writeTo(uint8_t *Buf) override;
395
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000396 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
397 OutputSectionBase<ELFT> *InitArraySec = nullptr;
398 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000399
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000400private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000401 SymbolTable<ELFT> &SymTab;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000402 const ELFSymbolBody<ELFT> *InitSym = nullptr;
403 const ELFSymbolBody<ELFT> *FiniSym = nullptr;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000404 uint32_t DtFlags = 0;
405 uint32_t DtFlags1 = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000406};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000407
408// All output sections that are hadnled by the linker specially are
409// globally accessible. Writer initializes them, so don't use them
410// until Writer is initialized.
411template <class ELFT> struct Out {
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000412 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000413 typedef typename llvm::object::ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000414 static DynamicSection<ELFT> *Dynamic;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000415 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000416 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000417 static GotSection<ELFT> *Got;
418 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000419 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000420 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000421 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000422 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000423 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000424 static PltSection<ELFT> *Plt;
425 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000426 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000427 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000428 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000429 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000430 static SymbolTableSection<ELFT> *DynSymTab;
431 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000432 static Elf_Phdr *TlsPhdr;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000433 static uint32_t LocalModuleTlsIndexOffset;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000434};
Rui Ueyamad888d102015-10-09 19:34:55 +0000435
436template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000437template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000438template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000439template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
440template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000441template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000442template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000443template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000444template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000445template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000446template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
447template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000448template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000449template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000450template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000451template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000452template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
453template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000454template <class ELFT> typename Out<ELFT>::Elf_Phdr *Out<ELFT>::TlsPhdr;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000455template <class ELFT> uint32_t Out<ELFT>::LocalModuleTlsIndexOffset = -1;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000456
457} // namespace elf2
458} // namespace lld
459
460#endif // LLD_ELF_OUTPUT_SECTIONS_H