blob: 4b2d819c6565fd9f784413f38fa784ec9d4cf83f [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;
38
George Rimarbfb7bf72015-12-21 10:00:12 +000039// Flag to force GOT to be in output if we have relocations
40// that relies on its address.
41extern bool HasGotOffRel;
42
Rafael Espindola5805c4f2015-09-21 21:38:08 +000043template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000044static inline typename llvm::object::ELFFile<ELFT>::uintX_t
45getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rel &Rel) {
46 return 0;
47}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000048
49template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000050static inline typename llvm::object::ELFFile<ELFT>::uintX_t
51getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rela &Rel) {
52 return Rel.r_addend;
53}
54
55template <class ELFT>
56typename llvm::object::ELFFile<ELFT>::uintX_t getSymVA(const SymbolBody &S);
57
58template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +000059typename llvm::object::ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +000060getLocalRelTarget(const ObjectFile<ELFT> &File,
George Rimar0b8ed1d2015-12-21 10:37:33 +000061 const llvm::object::Elf_Rel_Impl<ELFT, IsRela> &Rel,
62 typename llvm::object::ELFFile<ELFT>::uintX_t Addend);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +000063bool canBePreempted(const SymbolBody *Body, bool NeedsGot);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000064
Rafael Espindola05a3dd22015-09-22 23:38:23 +000065bool includeInDynamicSymtab(const SymbolBody &B);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000066
67template <class ELFT>
68bool shouldKeepInSymtab(
Rafael Espindola444576d2015-10-09 19:25:07 +000069 const ObjectFile<ELFT> &File, StringRef Name,
70 const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym);
Davide Italiano85121bb2015-09-25 03:56:11 +000071
Rafael Espindola71675852015-09-22 00:16:19 +000072// This represents a section in an output file.
73// Different sub classes represent different types of sections. Some contain
74// input sections, others are created by the linker.
75// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000076// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000077template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000078public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000079 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
80 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000081
82 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
83 void setVA(uintX_t VA) { Header.sh_addr = VA; }
84 uintX_t getVA() const { return Header.sh_addr; }
85 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000086 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000087 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000088
Rui Ueyama2317d0d2015-10-15 20:55:22 +000089 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000090
91 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000092 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000093 void setSize(uintX_t Val) { Header.sh_size = Val; }
94 uintX_t getFlags() { return Header.sh_flags; }
95 uintX_t getFileOff() { return Header.sh_offset; }
96 uintX_t getAlign() {
97 // The ELF spec states that a value of 0 means the section has no alignment
98 // constraits.
99 return std::max<uintX_t>(Header.sh_addralign, 1);
100 }
101 uint32_t getType() { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +0000102 void updateAlign(uintX_t Align) {
103 if (Align > Header.sh_addralign)
104 Header.sh_addralign = Align;
105 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000106
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000107 virtual void finalize() {}
108 virtual void writeTo(uint8_t *Buf) = 0;
109
110protected:
111 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000112 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000113 ~OutputSectionBase() = default;
114};
115
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000116template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
117 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000118 typedef typename Base::uintX_t uintX_t;
119
120public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000121 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000122 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000123 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000124 void addEntry(SymbolBody *Sym);
George Rimar90cd0a82015-12-01 19:20:26 +0000125 bool addDynTlsEntry(SymbolBody *Sym);
George Rimar95c1a582015-12-07 08:02:20 +0000126 bool addCurrentModuleTlsIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000127 bool empty() const { return Entries.empty(); }
128 uintX_t getEntryAddr(const SymbolBody &B) const;
George Rimar90cd0a82015-12-01 19:20:26 +0000129 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000130 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000131
Igor Kudrin304860a2015-11-12 04:39:49 +0000132 // Returns the symbol which corresponds to the first entry of the global part
133 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
134 // table properties.
135 // Returns nullptr if the global part is empty.
136 const SymbolBody *getMipsFirstGlobalEntry() const;
137
138 // Returns the number of entries in the local part of GOT including
139 // the number of reserved entries. This method is MIPS-specific.
140 unsigned getMipsLocalEntriesNum() const;
141
George Rimarb17f7392015-12-01 18:24:07 +0000142 uint32_t getLocalTlsIndexVA() { return Base::getVA() + LocalTlsIndexOff; }
143
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000144private:
145 std::vector<const SymbolBody *> Entries;
George Rimarb17f7392015-12-01 18:24:07 +0000146 uint32_t LocalTlsIndexOff = -1;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000147};
148
George Rimar648a2c32015-10-20 08:54:27 +0000149template <class ELFT>
150class GotPltSection final : public OutputSectionBase<ELFT> {
151 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
152
153public:
154 GotPltSection();
155 void finalize() override;
156 void writeTo(uint8_t *Buf) override;
157 void addEntry(SymbolBody *Sym);
158 bool empty() const;
159 uintX_t getEntryAddr(const SymbolBody &B) const;
160
161private:
162 std::vector<const SymbolBody *> Entries;
163};
164
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000165template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
166 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000167 typedef typename Base::uintX_t uintX_t;
168
169public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000170 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000171 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000172 void writeTo(uint8_t *Buf) override;
173 void addEntry(SymbolBody *Sym);
174 bool empty() const { return Entries.empty(); }
175 uintX_t getEntryAddr(const SymbolBody &B) const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000176
177private:
George Rimar77b77792015-11-25 22:15:01 +0000178 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000179};
180
181template <class ELFT> struct DynamicReloc {
182 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000183 InputSectionBase<ELFT> *C;
184 const Elf_Rel *RI;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000185};
186
187template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000188class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000189public:
190 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
191 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
192 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000193 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000194 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000195 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000196
Rui Ueyama0db335f2015-10-07 16:58:54 +0000197 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000198 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000199 void addLocalSymbol(StringRef Name);
200 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000201 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000202 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000203
Igor Kudrinf1d60292015-10-28 07:05:56 +0000204 ArrayRef<SymbolBody *> getSymbols() const { return Symbols; }
Igor Kudrinab665fc2015-10-20 21:47:58 +0000205
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000206private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000207 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000208 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000209
Igor Kudrin853b88d2015-10-20 20:52:14 +0000210 static uint8_t getSymbolBinding(SymbolBody *Body);
211
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000212 SymbolTable<ELFT> &Table;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000213 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000214 std::vector<SymbolBody *> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000215 unsigned NumVisible = 0;
216 unsigned NumLocals = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000217};
218
219template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000220class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000221 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
222 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000223 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000224
225public:
George Rimar648a2c32015-10-20 08:54:27 +0000226 RelocationSection(StringRef Name, bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000227 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
George Rimar77b77792015-11-25 22:15:01 +0000228 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000229 void finalize() override;
230 void writeTo(uint8_t *Buf) override;
231 bool hasRelocs() const { return !Relocs.empty(); }
232 bool isRela() const { return IsRela; }
233
George Rimara07ff662015-12-21 10:12:06 +0000234 bool Static = false;
235
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000236private:
George Rimar5828c232015-11-30 17:49:19 +0000237 bool applyTlsDynamicReloc(SymbolBody *Body, uint32_t Type, Elf_Rel *P,
238 Elf_Rel *N);
239
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000240 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000241 const bool IsRela;
242};
243
244template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000245class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000246public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000247 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
248 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
249 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
250 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000251 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000252 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rafael Espindola71675852015-09-22 00:16:19 +0000253 void addSection(InputSection<ELFT> *C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000254 void writeTo(uint8_t *Buf) override;
255
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000256private:
Rafael Espindola71675852015-09-22 00:16:19 +0000257 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000258};
259
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000260template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000261class MergeOutputSection final : public OutputSectionBase<ELFT> {
262 typedef typename OutputSectionBase<ELFT>::uintX_t uintX_t;
263
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000264 bool shouldTailMerge() const;
265
Rafael Espindolac159c962015-10-19 21:00:02 +0000266public:
267 MergeOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
268 void addSection(MergeInputSection<ELFT> *S);
269 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000270 unsigned getOffset(StringRef Val);
271 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000272
273private:
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000274 llvm::StringTableBuilder Builder{llvm::StringTableBuilder::RAW};
Rafael Espindolac159c962015-10-19 21:00:02 +0000275};
276
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000277// FDE or CIE
278template <class ELFT> struct EHRegion {
279 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
280 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
281 StringRef data() const;
282 EHInputSection<ELFT> *S;
283 unsigned Index;
284};
285
286template <class ELFT> struct Cie : public EHRegion<ELFT> {
287 Cie(EHInputSection<ELFT> *S, unsigned Index);
288 std::vector<EHRegion<ELFT>> Fdes;
289};
290
291template <class ELFT>
292class EHOutputSection final : public OutputSectionBase<ELFT> {
293public:
294 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
295 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
296 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
297 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
298 EHOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
299 void writeTo(uint8_t *Buf) override;
300
301 template <bool IsRela>
302 void addSectionAux(
303 EHInputSection<ELFT> *S,
304 llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, IsRela> *>
305 Rels);
306
307 void addSection(EHInputSection<ELFT> *S);
308
309private:
George Rimar003be4f2015-12-17 09:23:40 +0000310 uintX_t readEntryLength(ArrayRef<uint8_t> D);
311
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000312 std::vector<EHInputSection<ELFT> *> Sections;
313 std::vector<Cie<ELFT>> Cies;
314
315 // Maps CIE content + personality to a index in Cies.
316 llvm::DenseMap<std::pair<StringRef, StringRef>, unsigned> CieMap;
317};
318
Rafael Espindolac159c962015-10-19 21:00:02 +0000319template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000320class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000321public:
322 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000323 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000324};
325
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000326template <class ELFT>
327class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000328public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000329 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000330 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000331 void add(StringRef S) { StrTabBuilder.add(S); }
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000332 size_t getOffset(StringRef S) const { return StrTabBuilder.getOffset(S); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000333 StringRef data() const { return StrTabBuilder.data(); }
334 void writeTo(uint8_t *Buf) override;
335
336 void finalize() override {
Rafael Espindola6779a232015-10-23 21:48:35 +0000337 StrTabBuilder.finalize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000338 this->Header.sh_size = StrTabBuilder.data().size();
339 }
340
341 bool isDynamic() const { return Dynamic; }
342
343private:
344 const bool Dynamic;
Rafael Espindola6779a232015-10-23 21:48:35 +0000345 llvm::StringTableBuilder StrTabBuilder{llvm::StringTableBuilder::ELF};
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000346};
347
348template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000349class HashTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000350 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
351
352public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000353 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000354 void finalize() override;
355 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000356};
357
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000358// Outputs GNU Hash section. For detailed explanation see:
359// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
360template <class ELFT>
361class GnuHashTableSection final : public OutputSectionBase<ELFT> {
362 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
363 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
364 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
365
366public:
367 GnuHashTableSection();
368 void finalize() override;
369 void writeTo(uint8_t *Buf) override;
370
Igor Kudrinf1d60292015-10-28 07:05:56 +0000371 // Adds symbols to the hash table.
372 // Sorts the input to satisfy GNU hash section requirements.
373 void addSymbols(std::vector<SymbolBody *> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000374
375private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000376 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000377 static unsigned calcMaskWords(unsigned NumHashed);
378
379 void writeHeader(uint8_t *&Buf);
380 void writeBloomFilter(uint8_t *&Buf);
381 void writeHashTable(uint8_t *Buf);
382
Igor Kudrinf1d60292015-10-28 07:05:56 +0000383 struct HashedSymbolData {
384 SymbolBody *Body;
385 uint32_t Hash;
386 };
387
388 std::vector<HashedSymbolData> HashedSymbols;
389
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000390 unsigned MaskWords;
391 unsigned NBuckets;
392 unsigned Shift2;
393};
394
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000395template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000396class DynamicSection final : public OutputSectionBase<ELFT> {
397 typedef OutputSectionBase<ELFT> Base;
398 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000399 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
400 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000401 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000402 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000403
404public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000405 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000406 void finalize() override;
407 void writeTo(uint8_t *Buf) override;
408
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000409 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
410 OutputSectionBase<ELFT> *InitArraySec = nullptr;
411 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000412
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000413private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000414 SymbolTable<ELFT> &SymTab;
Rafael Espindola167e62f2015-12-21 20:59:29 +0000415 const SymbolBody *InitSym = nullptr;
416 const SymbolBody *FiniSym = nullptr;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000417 uint32_t DtFlags = 0;
418 uint32_t DtFlags1 = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000419};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000420
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000421template <class ELFT>
422class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
423 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
424
425public:
426 MipsReginfoOutputSection();
427 void writeTo(uint8_t *Buf) override;
428
429 void addSection(MipsReginfoInputSection<ELFT> *S);
430
431private:
432 uint32_t GeneralMask = 0;
433};
434
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000435// All output sections that are hadnled by the linker specially are
436// globally accessible. Writer initializes them, so don't use them
437// until Writer is initialized.
438template <class ELFT> struct Out {
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000439 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000440 typedef typename llvm::object::ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000441 static DynamicSection<ELFT> *Dynamic;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000442 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000443 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000444 static GotSection<ELFT> *Got;
445 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000446 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000447 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000448 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000449 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000450 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000451 static PltSection<ELFT> *Plt;
452 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000453 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000454 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000455 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000456 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000457 static SymbolTableSection<ELFT> *DynSymTab;
458 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000459 static Elf_Phdr *TlsPhdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000460};
Rui Ueyamad888d102015-10-09 19:34:55 +0000461
462template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000463template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000464template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000465template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
466template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000467template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000468template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000469template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000470template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000471template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000472template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
473template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000474template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000475template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000476template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000477template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000478template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
479template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000480template <class ELFT> typename Out<ELFT>::Elf_Phdr *Out<ELFT>::TlsPhdr;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000481
482} // namespace elf2
483} // namespace lld
484
485#endif // LLD_ELF_OUTPUT_SECTIONS_H