blob: d01eb7355aed4e519431572c95d466737f3b8d72 [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
Rafael Espindola5805c4f2015-09-21 21:38:08 +000040template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000041static inline typename llvm::object::ELFFile<ELFT>::uintX_t
42getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rel &Rel) {
43 return 0;
44}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000045
46template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000047static inline typename llvm::object::ELFFile<ELFT>::uintX_t
48getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rela &Rel) {
49 return Rel.r_addend;
50}
51
52template <class ELFT>
53typename llvm::object::ELFFile<ELFT>::uintX_t getSymVA(const SymbolBody &S);
54
55template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +000056typename llvm::object::ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +000057getLocalRelTarget(const ObjectFile<ELFT> &File,
Rafael Espindola932efcf2015-10-19 20:24:44 +000058 const llvm::object::Elf_Rel_Impl<ELFT, IsRela> &Rel);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +000059bool canBePreempted(const SymbolBody *Body, bool NeedsGot);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000060template <class ELFT> bool includeInSymtab(const SymbolBody &B);
61
Rafael Espindola05a3dd22015-09-22 23:38:23 +000062bool includeInDynamicSymtab(const SymbolBody &B);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000063
64template <class ELFT>
65bool shouldKeepInSymtab(
Rafael Espindola444576d2015-10-09 19:25:07 +000066 const ObjectFile<ELFT> &File, StringRef Name,
67 const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym);
Davide Italiano85121bb2015-09-25 03:56:11 +000068
Rafael Espindola71675852015-09-22 00:16:19 +000069// This represents a section in an output file.
70// Different sub classes represent different types of sections. Some contain
71// input sections, others are created by the linker.
72// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000073// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000074template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000075public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000076 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
77 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000078
79 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
80 void setVA(uintX_t VA) { Header.sh_addr = VA; }
81 uintX_t getVA() const { return Header.sh_addr; }
82 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000083 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000084 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000085
Rui Ueyama2317d0d2015-10-15 20:55:22 +000086 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000087
88 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000089 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000090 void setSize(uintX_t Val) { Header.sh_size = Val; }
91 uintX_t getFlags() { return Header.sh_flags; }
92 uintX_t getFileOff() { return Header.sh_offset; }
93 uintX_t getAlign() {
94 // The ELF spec states that a value of 0 means the section has no alignment
95 // constraits.
96 return std::max<uintX_t>(Header.sh_addralign, 1);
97 }
98 uint32_t getType() { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +000099 void updateAlign(uintX_t Align) {
100 if (Align > Header.sh_addralign)
101 Header.sh_addralign = Align;
102 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000103
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000104 virtual void finalize() {}
105 virtual void writeTo(uint8_t *Buf) = 0;
106
107protected:
108 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000109 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000110 ~OutputSectionBase() = default;
111};
112
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000113template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
114 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000115 typedef typename Base::uintX_t uintX_t;
116
117public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000118 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000119 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000120 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000121 void addEntry(SymbolBody *Sym);
George Rimar90cd0a82015-12-01 19:20:26 +0000122 bool addDynTlsEntry(SymbolBody *Sym);
George Rimar95c1a582015-12-07 08:02:20 +0000123 bool addCurrentModuleTlsIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000124 bool empty() const { return Entries.empty(); }
125 uintX_t getEntryAddr(const SymbolBody &B) const;
George Rimar90cd0a82015-12-01 19:20:26 +0000126 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000127 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000128
Igor Kudrin304860a2015-11-12 04:39:49 +0000129 // Returns the symbol which corresponds to the first entry of the global part
130 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
131 // table properties.
132 // Returns nullptr if the global part is empty.
133 const SymbolBody *getMipsFirstGlobalEntry() const;
134
135 // Returns the number of entries in the local part of GOT including
136 // the number of reserved entries. This method is MIPS-specific.
137 unsigned getMipsLocalEntriesNum() const;
138
George Rimarb17f7392015-12-01 18:24:07 +0000139 uint32_t getLocalTlsIndexVA() { return Base::getVA() + LocalTlsIndexOff; }
140
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000141private:
142 std::vector<const SymbolBody *> Entries;
George Rimarb17f7392015-12-01 18:24:07 +0000143 uint32_t LocalTlsIndexOff = -1;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000144};
145
George Rimar648a2c32015-10-20 08:54:27 +0000146template <class ELFT>
147class GotPltSection final : public OutputSectionBase<ELFT> {
148 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
149
150public:
151 GotPltSection();
152 void finalize() override;
153 void writeTo(uint8_t *Buf) override;
154 void addEntry(SymbolBody *Sym);
155 bool empty() const;
156 uintX_t getEntryAddr(const SymbolBody &B) const;
157
158private:
159 std::vector<const SymbolBody *> Entries;
160};
161
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000162template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
163 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000164 typedef typename Base::uintX_t uintX_t;
165
166public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000167 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000168 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000169 void writeTo(uint8_t *Buf) override;
170 void addEntry(SymbolBody *Sym);
171 bool empty() const { return Entries.empty(); }
172 uintX_t getEntryAddr(const SymbolBody &B) const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000173
174private:
George Rimar77b77792015-11-25 22:15:01 +0000175 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000176};
177
178template <class ELFT> struct DynamicReloc {
179 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000180 InputSectionBase<ELFT> *C;
181 const Elf_Rel *RI;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000182};
183
184template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000185class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000186public:
187 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
188 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
189 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000190 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000191 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000192 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000193
Rui Ueyama0db335f2015-10-07 16:58:54 +0000194 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000195 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000196 void addLocalSymbol(StringRef Name);
197 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000198 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000199 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000200
Igor Kudrinf1d60292015-10-28 07:05:56 +0000201 ArrayRef<SymbolBody *> getSymbols() const { return Symbols; }
Igor Kudrinab665fc2015-10-20 21:47:58 +0000202
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000203private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000204 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000205 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000206
Igor Kudrin853b88d2015-10-20 20:52:14 +0000207 static uint8_t getSymbolBinding(SymbolBody *Body);
208
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000209 SymbolTable<ELFT> &Table;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000210 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000211 std::vector<SymbolBody *> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000212 unsigned NumVisible = 0;
213 unsigned NumLocals = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000214};
215
216template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000217class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000218 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
219 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000220 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000221
222public:
George Rimar648a2c32015-10-20 08:54:27 +0000223 RelocationSection(StringRef Name, bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000224 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
George Rimar77b77792015-11-25 22:15:01 +0000225 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000226 void finalize() override;
227 void writeTo(uint8_t *Buf) override;
228 bool hasRelocs() const { return !Relocs.empty(); }
229 bool isRela() const { return IsRela; }
230
231private:
George Rimar5828c232015-11-30 17:49:19 +0000232 bool applyTlsDynamicReloc(SymbolBody *Body, uint32_t Type, Elf_Rel *P,
233 Elf_Rel *N);
234
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000235 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000236 const bool IsRela;
237};
238
239template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000240class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000241public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000242 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
243 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
244 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
245 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000246 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000247 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rafael Espindola71675852015-09-22 00:16:19 +0000248 void addSection(InputSection<ELFT> *C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000249 void writeTo(uint8_t *Buf) override;
250
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000251private:
Rafael Espindola71675852015-09-22 00:16:19 +0000252 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000253};
254
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000255template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000256class MergeOutputSection final : public OutputSectionBase<ELFT> {
257 typedef typename OutputSectionBase<ELFT>::uintX_t uintX_t;
258
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000259 bool shouldTailMerge() const;
260
Rafael Espindolac159c962015-10-19 21:00:02 +0000261public:
262 MergeOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
263 void addSection(MergeInputSection<ELFT> *S);
264 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000265 unsigned getOffset(StringRef Val);
266 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000267
268private:
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000269 llvm::StringTableBuilder Builder{llvm::StringTableBuilder::RAW};
Rafael Espindolac159c962015-10-19 21:00:02 +0000270};
271
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000272// FDE or CIE
273template <class ELFT> struct EHRegion {
274 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
275 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
276 StringRef data() const;
277 EHInputSection<ELFT> *S;
278 unsigned Index;
279};
280
281template <class ELFT> struct Cie : public EHRegion<ELFT> {
282 Cie(EHInputSection<ELFT> *S, unsigned Index);
283 std::vector<EHRegion<ELFT>> Fdes;
284};
285
286template <class ELFT>
287class EHOutputSection final : public OutputSectionBase<ELFT> {
288public:
289 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
290 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
291 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
292 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
293 EHOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
294 void writeTo(uint8_t *Buf) override;
295
296 template <bool IsRela>
297 void addSectionAux(
298 EHInputSection<ELFT> *S,
299 llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, IsRela> *>
300 Rels);
301
302 void addSection(EHInputSection<ELFT> *S);
303
304private:
George Rimar003be4f2015-12-17 09:23:40 +0000305 uintX_t readEntryLength(ArrayRef<uint8_t> D);
306
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000307 std::vector<EHInputSection<ELFT> *> Sections;
308 std::vector<Cie<ELFT>> Cies;
309
310 // Maps CIE content + personality to a index in Cies.
311 llvm::DenseMap<std::pair<StringRef, StringRef>, unsigned> CieMap;
312};
313
Rafael Espindolac159c962015-10-19 21:00:02 +0000314template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000315class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000316public:
317 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000318 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000319};
320
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000321template <class ELFT>
322class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000323public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000324 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000325 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000326 void add(StringRef S) { StrTabBuilder.add(S); }
Rui Ueyama9fbb3d82015-10-24 17:44:52 +0000327 size_t getOffset(StringRef S) const { return StrTabBuilder.getOffset(S); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000328 StringRef data() const { return StrTabBuilder.data(); }
329 void writeTo(uint8_t *Buf) override;
330
331 void finalize() override {
Rafael Espindola6779a232015-10-23 21:48:35 +0000332 StrTabBuilder.finalize();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000333 this->Header.sh_size = StrTabBuilder.data().size();
334 }
335
336 bool isDynamic() const { return Dynamic; }
337
338private:
339 const bool Dynamic;
Rafael Espindola6779a232015-10-23 21:48:35 +0000340 llvm::StringTableBuilder StrTabBuilder{llvm::StringTableBuilder::ELF};
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000341};
342
343template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000344class HashTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000345 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
346
347public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000348 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000349 void finalize() override;
350 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000351};
352
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000353// Outputs GNU Hash section. For detailed explanation see:
354// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
355template <class ELFT>
356class GnuHashTableSection final : public OutputSectionBase<ELFT> {
357 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
358 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
359 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
360
361public:
362 GnuHashTableSection();
363 void finalize() override;
364 void writeTo(uint8_t *Buf) override;
365
Igor Kudrinf1d60292015-10-28 07:05:56 +0000366 // Adds symbols to the hash table.
367 // Sorts the input to satisfy GNU hash section requirements.
368 void addSymbols(std::vector<SymbolBody *> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000369
370private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000371 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000372 static unsigned calcMaskWords(unsigned NumHashed);
373
374 void writeHeader(uint8_t *&Buf);
375 void writeBloomFilter(uint8_t *&Buf);
376 void writeHashTable(uint8_t *Buf);
377
Igor Kudrinf1d60292015-10-28 07:05:56 +0000378 struct HashedSymbolData {
379 SymbolBody *Body;
380 uint32_t Hash;
381 };
382
383 std::vector<HashedSymbolData> HashedSymbols;
384
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000385 unsigned MaskWords;
386 unsigned NBuckets;
387 unsigned Shift2;
388};
389
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000390template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000391class DynamicSection final : public OutputSectionBase<ELFT> {
392 typedef OutputSectionBase<ELFT> Base;
393 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000394 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
395 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000396 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000397 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000398
399public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000400 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000401 void finalize() override;
402 void writeTo(uint8_t *Buf) override;
403
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000404 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
405 OutputSectionBase<ELFT> *InitArraySec = nullptr;
406 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000407
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000408private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000409 SymbolTable<ELFT> &SymTab;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000410 const ELFSymbolBody<ELFT> *InitSym = nullptr;
411 const ELFSymbolBody<ELFT> *FiniSym = nullptr;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000412 uint32_t DtFlags = 0;
413 uint32_t DtFlags1 = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000414};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000415
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000416template <class ELFT>
417class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
418 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
419
420public:
421 MipsReginfoOutputSection();
422 void writeTo(uint8_t *Buf) override;
423
424 void addSection(MipsReginfoInputSection<ELFT> *S);
425
426private:
427 uint32_t GeneralMask = 0;
428};
429
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000430// All output sections that are hadnled by the linker specially are
431// globally accessible. Writer initializes them, so don't use them
432// until Writer is initialized.
433template <class ELFT> struct Out {
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000434 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000435 typedef typename llvm::object::ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000436 static DynamicSection<ELFT> *Dynamic;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000437 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000438 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000439 static GotSection<ELFT> *Got;
440 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000441 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000442 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000443 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000444 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000445 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000446 static PltSection<ELFT> *Plt;
447 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000448 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000449 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000450 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000451 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000452 static SymbolTableSection<ELFT> *DynSymTab;
453 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000454 static Elf_Phdr *TlsPhdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000455};
Rui Ueyamad888d102015-10-09 19:34:55 +0000456
457template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000458template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000459template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000460template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
461template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000462template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000463template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000464template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000465template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000466template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000467template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
468template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000469template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000470template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000471template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000472template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000473template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
474template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000475template <class ELFT> typename Out<ELFT>::Elf_Phdr *Out<ELFT>::TlsPhdr;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000476
477} // namespace elf2
478} // namespace lld
479
480#endif // LLD_ELF_OUTPUT_SECTIONS_H