blob: 45d9fd181ed0ebe434496a23ae88138999db6af9 [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;
Simon Atanasyan1d7df402015-12-20 10:57:34 +000034template <class ELFT> class MipsReginfoInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000035template <class ELFT> class OutputSection;
36template <class ELFT> class ObjectFile;
37template <class ELFT> class DefinedRegular;
Rafael Espindolacd076f02015-09-25 18:19:03 +000038template <class ELFT> class ELFSymbolBody;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000039
George Rimarbfb7bf72015-12-21 10:00:12 +000040// Flag to force GOT to be in output if we have relocations
41// that relies on its address.
42extern bool HasGotOffRel;
43
Rafael Espindola5805c4f2015-09-21 21:38:08 +000044template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000045static inline typename llvm::object::ELFFile<ELFT>::uintX_t
46getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rel &Rel) {
47 return 0;
48}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000049
50template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000051static inline typename llvm::object::ELFFile<ELFT>::uintX_t
52getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rela &Rel) {
53 return Rel.r_addend;
54}
55
56template <class ELFT>
57typename llvm::object::ELFFile<ELFT>::uintX_t getSymVA(const SymbolBody &S);
58
59template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +000060typename llvm::object::ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +000061getLocalRelTarget(const ObjectFile<ELFT> &File,
George Rimar0b8ed1d2015-12-21 10:37:33 +000062 const llvm::object::Elf_Rel_Impl<ELFT, IsRela> &Rel,
63 typename llvm::object::ELFFile<ELFT>::uintX_t Addend);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +000064bool canBePreempted(const SymbolBody *Body, bool NeedsGot);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000065template <class ELFT> bool includeInSymtab(const SymbolBody &B);
66
Rafael Espindola05a3dd22015-09-22 23:38:23 +000067bool includeInDynamicSymtab(const SymbolBody &B);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000068
69template <class ELFT>
70bool shouldKeepInSymtab(
Rafael Espindola444576d2015-10-09 19:25:07 +000071 const ObjectFile<ELFT> &File, StringRef Name,
72 const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym);
Davide Italiano85121bb2015-09-25 03:56:11 +000073
Rafael Espindola71675852015-09-22 00:16:19 +000074// This represents a section in an output file.
75// Different sub classes represent different types of sections. Some contain
76// input sections, others are created by the linker.
77// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000078// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000079template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000080public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000081 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
82 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000083
84 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
85 void setVA(uintX_t VA) { Header.sh_addr = VA; }
86 uintX_t getVA() const { return Header.sh_addr; }
87 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000088 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000089 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000090
Rui Ueyama2317d0d2015-10-15 20:55:22 +000091 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000092
93 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000094 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000095 void setSize(uintX_t Val) { Header.sh_size = Val; }
96 uintX_t getFlags() { return Header.sh_flags; }
97 uintX_t getFileOff() { return Header.sh_offset; }
98 uintX_t getAlign() {
99 // The ELF spec states that a value of 0 means the section has no alignment
100 // constraits.
101 return std::max<uintX_t>(Header.sh_addralign, 1);
102 }
103 uint32_t getType() { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +0000104 void updateAlign(uintX_t Align) {
105 if (Align > Header.sh_addralign)
106 Header.sh_addralign = Align;
107 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000108
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000109 virtual void finalize() {}
110 virtual void writeTo(uint8_t *Buf) = 0;
111
112protected:
113 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000114 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000115 ~OutputSectionBase() = default;
116};
117
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000118template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
119 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000120 typedef typename Base::uintX_t uintX_t;
121
122public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000123 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000124 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000125 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000126 void addEntry(SymbolBody *Sym);
George Rimar90cd0a82015-12-01 19:20:26 +0000127 bool addDynTlsEntry(SymbolBody *Sym);
George Rimar95c1a582015-12-07 08:02:20 +0000128 bool addCurrentModuleTlsIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000129 bool empty() const { return Entries.empty(); }
130 uintX_t getEntryAddr(const SymbolBody &B) const;
George Rimar90cd0a82015-12-01 19:20:26 +0000131 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000132 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000133
Igor Kudrin304860a2015-11-12 04:39:49 +0000134 // Returns the symbol which corresponds to the first entry of the global part
135 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
136 // table properties.
137 // Returns nullptr if the global part is empty.
138 const SymbolBody *getMipsFirstGlobalEntry() const;
139
140 // Returns the number of entries in the local part of GOT including
141 // the number of reserved entries. This method is MIPS-specific.
142 unsigned getMipsLocalEntriesNum() const;
143
George Rimarb17f7392015-12-01 18:24:07 +0000144 uint32_t getLocalTlsIndexVA() { return Base::getVA() + LocalTlsIndexOff; }
145
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000146private:
147 std::vector<const SymbolBody *> Entries;
George Rimarb17f7392015-12-01 18:24:07 +0000148 uint32_t LocalTlsIndexOff = -1;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000149};
150
George Rimar648a2c32015-10-20 08:54:27 +0000151template <class ELFT>
152class GotPltSection final : public OutputSectionBase<ELFT> {
153 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
154
155public:
156 GotPltSection();
157 void finalize() override;
158 void writeTo(uint8_t *Buf) override;
159 void addEntry(SymbolBody *Sym);
160 bool empty() const;
161 uintX_t getEntryAddr(const SymbolBody &B) const;
162
163private:
164 std::vector<const SymbolBody *> Entries;
165};
166
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000167template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
168 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000169 typedef typename Base::uintX_t uintX_t;
170
171public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000172 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000173 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000174 void writeTo(uint8_t *Buf) override;
175 void addEntry(SymbolBody *Sym);
176 bool empty() const { return Entries.empty(); }
177 uintX_t getEntryAddr(const SymbolBody &B) const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000178
179private:
George Rimar77b77792015-11-25 22:15:01 +0000180 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000181};
182
183template <class ELFT> struct DynamicReloc {
184 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000185 InputSectionBase<ELFT> *C;
186 const Elf_Rel *RI;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000187};
188
189template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000190class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000191public:
192 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
193 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
194 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000195 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000196 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000197 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000198
Rui Ueyama0db335f2015-10-07 16:58:54 +0000199 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000200 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000201 void addLocalSymbol(StringRef Name);
202 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000203 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000204 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000205
Igor Kudrinf1d60292015-10-28 07:05:56 +0000206 ArrayRef<SymbolBody *> getSymbols() const { return Symbols; }
Igor Kudrinab665fc2015-10-20 21:47:58 +0000207
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000208private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000209 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000210 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000211
Igor Kudrin853b88d2015-10-20 20:52:14 +0000212 static uint8_t getSymbolBinding(SymbolBody *Body);
213
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000214 SymbolTable<ELFT> &Table;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000215 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000216 std::vector<SymbolBody *> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000217 unsigned NumVisible = 0;
218 unsigned NumLocals = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000219};
220
221template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000222class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000223 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
224 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000225 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000226
227public:
George Rimar648a2c32015-10-20 08:54:27 +0000228 RelocationSection(StringRef Name, bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000229 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
George Rimar77b77792015-11-25 22:15:01 +0000230 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000231 void finalize() override;
232 void writeTo(uint8_t *Buf) override;
233 bool hasRelocs() const { return !Relocs.empty(); }
234 bool isRela() const { return IsRela; }
235
George Rimara07ff662015-12-21 10:12:06 +0000236 bool Static = false;
237
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000238private:
George Rimar5828c232015-11-30 17:49:19 +0000239 bool applyTlsDynamicReloc(SymbolBody *Body, uint32_t Type, Elf_Rel *P,
240 Elf_Rel *N);
241
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000242 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000243 const bool IsRela;
244};
245
246template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000247class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000248public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000249 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
250 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
251 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
252 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000253 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000254 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rafael Espindola71675852015-09-22 00:16:19 +0000255 void addSection(InputSection<ELFT> *C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000256 void writeTo(uint8_t *Buf) override;
257
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000258private:
Rafael Espindola71675852015-09-22 00:16:19 +0000259 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000260};
261
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000262template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000263class MergeOutputSection final : public OutputSectionBase<ELFT> {
264 typedef typename OutputSectionBase<ELFT>::uintX_t uintX_t;
265
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000266 bool shouldTailMerge() const;
267
Rafael Espindolac159c962015-10-19 21:00:02 +0000268public:
269 MergeOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
270 void addSection(MergeInputSection<ELFT> *S);
271 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000272 unsigned getOffset(StringRef Val);
273 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000274
275private:
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000276 llvm::StringTableBuilder Builder{llvm::StringTableBuilder::RAW};
Rafael Espindolac159c962015-10-19 21:00:02 +0000277};
278
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000279// FDE or CIE
280template <class ELFT> struct EHRegion {
281 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
282 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
283 StringRef data() const;
284 EHInputSection<ELFT> *S;
285 unsigned Index;
286};
287
288template <class ELFT> struct Cie : public EHRegion<ELFT> {
289 Cie(EHInputSection<ELFT> *S, unsigned Index);
290 std::vector<EHRegion<ELFT>> Fdes;
291};
292
293template <class ELFT>
294class EHOutputSection final : public OutputSectionBase<ELFT> {
295public:
296 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
297 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
298 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
299 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
300 EHOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
301 void writeTo(uint8_t *Buf) override;
302
303 template <bool IsRela>
304 void addSectionAux(
305 EHInputSection<ELFT> *S,
306 llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, IsRela> *>
307 Rels);
308
309 void addSection(EHInputSection<ELFT> *S);
310
311private:
George Rimar003be4f2015-12-17 09:23:40 +0000312 uintX_t readEntryLength(ArrayRef<uint8_t> D);
313
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000314 std::vector<EHInputSection<ELFT> *> Sections;
315 std::vector<Cie<ELFT>> Cies;
316
317 // Maps CIE content + personality to a index in Cies.
318 llvm::DenseMap<std::pair<StringRef, StringRef>, unsigned> CieMap;
319};
320
Rafael Espindolac159c962015-10-19 21:00:02 +0000321template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000322class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000323public:
324 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000325 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000326};
327
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000328template <class ELFT>
329class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000330public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000331 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000332 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000333 void add(StringRef S) { StrTabBuilder.add(S); }
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000334 size_t getOffset(StringRef S) const { return StrTabBuilder.getOffset(S); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000335 StringRef data() const { return StrTabBuilder.data(); }
336 void writeTo(uint8_t *Buf) override;
337
338 void finalize() override {
Rafael Espindola6779a232015-10-23 21:48:35 +0000339 StrTabBuilder.finalize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000340 this->Header.sh_size = StrTabBuilder.data().size();
341 }
342
343 bool isDynamic() const { return Dynamic; }
344
345private:
346 const bool Dynamic;
Rafael Espindola6779a232015-10-23 21:48:35 +0000347 llvm::StringTableBuilder StrTabBuilder{llvm::StringTableBuilder::ELF};
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000348};
349
350template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000351class HashTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000352 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
353
354public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000355 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000356 void finalize() override;
357 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000358};
359
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000360// Outputs GNU Hash section. For detailed explanation see:
361// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
362template <class ELFT>
363class GnuHashTableSection final : public OutputSectionBase<ELFT> {
364 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
365 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
366 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
367
368public:
369 GnuHashTableSection();
370 void finalize() override;
371 void writeTo(uint8_t *Buf) override;
372
Igor Kudrinf1d60292015-10-28 07:05:56 +0000373 // Adds symbols to the hash table.
374 // Sorts the input to satisfy GNU hash section requirements.
375 void addSymbols(std::vector<SymbolBody *> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000376
377private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000378 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000379 static unsigned calcMaskWords(unsigned NumHashed);
380
381 void writeHeader(uint8_t *&Buf);
382 void writeBloomFilter(uint8_t *&Buf);
383 void writeHashTable(uint8_t *Buf);
384
Igor Kudrinf1d60292015-10-28 07:05:56 +0000385 struct HashedSymbolData {
386 SymbolBody *Body;
387 uint32_t Hash;
388 };
389
390 std::vector<HashedSymbolData> HashedSymbols;
391
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000392 unsigned MaskWords;
393 unsigned NBuckets;
394 unsigned Shift2;
395};
396
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000397template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000398class DynamicSection final : public OutputSectionBase<ELFT> {
399 typedef OutputSectionBase<ELFT> Base;
400 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000401 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
402 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000403 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000404 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000405
406public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000407 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000408 void finalize() override;
409 void writeTo(uint8_t *Buf) override;
410
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000411 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
412 OutputSectionBase<ELFT> *InitArraySec = nullptr;
413 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000414
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000415private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000416 SymbolTable<ELFT> &SymTab;
Rafael Espindola167e62f2015-12-21 20:59:29 +0000417 const SymbolBody *InitSym = nullptr;
418 const SymbolBody *FiniSym = nullptr;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000419 uint32_t DtFlags = 0;
420 uint32_t DtFlags1 = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000421};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000422
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000423template <class ELFT>
424class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
425 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
426
427public:
428 MipsReginfoOutputSection();
429 void writeTo(uint8_t *Buf) override;
430
431 void addSection(MipsReginfoInputSection<ELFT> *S);
432
433private:
434 uint32_t GeneralMask = 0;
435};
436
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000437// All output sections that are hadnled by the linker specially are
438// globally accessible. Writer initializes them, so don't use them
439// until Writer is initialized.
440template <class ELFT> struct Out {
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000441 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000442 typedef typename llvm::object::ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000443 static DynamicSection<ELFT> *Dynamic;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000444 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000445 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000446 static GotSection<ELFT> *Got;
447 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000448 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000449 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000450 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000451 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000452 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000453 static PltSection<ELFT> *Plt;
454 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000455 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000456 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000457 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000458 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000459 static SymbolTableSection<ELFT> *DynSymTab;
460 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000461 static Elf_Phdr *TlsPhdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000462};
Rui Ueyamad888d102015-10-09 19:34:55 +0000463
464template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000465template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000466template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000467template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
468template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000469template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000470template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000471template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000472template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000473template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000474template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
475template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000476template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000477template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000478template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000479template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000480template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
481template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000482template <class ELFT> typename Out<ELFT>::Elf_Phdr *Out<ELFT>::TlsPhdr;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000483
484} // namespace elf2
485} // namespace lld
486
487#endif // LLD_ELF_OUTPUT_SECTIONS_H