blob: 7548bc936b0ec16f6548f4d3fea904ebbd666ee2 [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
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,
George Rimar0b8ed1d2015-12-21 10:37:33 +000057 const llvm::object::Elf_Rel_Impl<ELFT, IsRela> &Rel,
58 typename llvm::object::ELFFile<ELFT>::uintX_t Addend);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000059
Rui Ueyama89f4ec72015-12-25 07:01:09 +000060bool canBePreempted(const SymbolBody *Body, bool NeedsGot);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000061
62template <class ELFT>
63bool shouldKeepInSymtab(
Rafael Espindola444576d2015-10-09 19:25:07 +000064 const ObjectFile<ELFT> &File, StringRef Name,
65 const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym);
Davide Italiano85121bb2015-09-25 03:56:11 +000066
Rafael Espindola71675852015-09-22 00:16:19 +000067// This represents a section in an output file.
68// Different sub classes represent different types of sections. Some contain
69// input sections, others are created by the linker.
70// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000071// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000072template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000073public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000074 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
75 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000076
77 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
78 void setVA(uintX_t VA) { Header.sh_addr = VA; }
79 uintX_t getVA() const { return Header.sh_addr; }
80 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000081 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000082 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000083
Rui Ueyama40845e62015-12-26 05:51:07 +000084 virtual void addSection(InputSectionBase<ELFT> *C) {}
85
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;
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +0000106 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000107
108protected:
109 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000110 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000111};
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);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000122 void addMipsLocalEntry();
George Rimar90cd0a82015-12-01 19:20:26 +0000123 bool addDynTlsEntry(SymbolBody *Sym);
George Rimar95c1a582015-12-07 08:02:20 +0000124 bool addCurrentModuleTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000125 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000126 uintX_t getEntryAddr(const SymbolBody &B) const;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000127 uintX_t getMipsLocalFullAddr(const SymbolBody &B);
128 uintX_t getMipsLocalPageAddr(uintX_t Addr);
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;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000147 uint32_t MipsLocalEntries = 0;
148 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
149
150 uintX_t getMipsLocalEntryAddr(uintX_t EntryValue);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000151};
152
George Rimar648a2c32015-10-20 08:54:27 +0000153template <class ELFT>
154class GotPltSection final : public OutputSectionBase<ELFT> {
155 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
156
157public:
158 GotPltSection();
159 void finalize() override;
160 void writeTo(uint8_t *Buf) override;
161 void addEntry(SymbolBody *Sym);
162 bool empty() const;
163 uintX_t getEntryAddr(const SymbolBody &B) const;
164
165private:
166 std::vector<const SymbolBody *> Entries;
167};
168
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000169template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
170 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000171 typedef typename Base::uintX_t uintX_t;
172
173public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000174 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000175 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000176 void writeTo(uint8_t *Buf) override;
177 void addEntry(SymbolBody *Sym);
178 bool empty() const { return Entries.empty(); }
179 uintX_t getEntryAddr(const SymbolBody &B) const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000180
181private:
George Rimar77b77792015-11-25 22:15:01 +0000182 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000183};
184
185template <class ELFT> struct DynamicReloc {
186 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000187 InputSectionBase<ELFT> *C;
188 const Elf_Rel *RI;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000189};
190
191template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000192class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000193public:
194 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
195 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
196 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000197 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000198 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000199 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000200
Rui Ueyama0db335f2015-10-07 16:58:54 +0000201 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000202 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000203 void addLocalSymbol(StringRef Name);
204 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000205 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000206 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000207
Igor Kudrinf1d60292015-10-28 07:05:56 +0000208 ArrayRef<SymbolBody *> getSymbols() const { return Symbols; }
Igor Kudrinab665fc2015-10-20 21:47:58 +0000209
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000210private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000211 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000212 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000213
Igor Kudrin853b88d2015-10-20 20:52:14 +0000214 static uint8_t getSymbolBinding(SymbolBody *Body);
215
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000216 SymbolTable<ELFT> &Table;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000217 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000218 std::vector<SymbolBody *> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000219 unsigned NumLocals = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000220};
221
222template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000223class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000224 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
225 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000226 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000227
228public:
George Rimar648a2c32015-10-20 08:54:27 +0000229 RelocationSection(StringRef Name, bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000230 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
George Rimar77b77792015-11-25 22:15:01 +0000231 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000232 void finalize() override;
233 void writeTo(uint8_t *Buf) override;
234 bool hasRelocs() const { return !Relocs.empty(); }
235 bool isRela() const { return IsRela; }
236
George Rimara07ff662015-12-21 10:12:06 +0000237 bool Static = false;
238
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000239private:
George Rimar5828c232015-11-30 17:49:19 +0000240 bool applyTlsDynamicReloc(SymbolBody *Body, uint32_t Type, Elf_Rel *P,
241 Elf_Rel *N);
242
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000243 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000244 const bool IsRela;
245};
246
247template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000248class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000249public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000250 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
251 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
252 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
253 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000254 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000255 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000256 void addSection(InputSectionBase<ELFT> *C) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000257 void writeTo(uint8_t *Buf) override;
258
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000259private:
Rafael Espindola71675852015-09-22 00:16:19 +0000260 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000261};
262
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000263template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000264class MergeOutputSection final : public OutputSectionBase<ELFT> {
265 typedef typename OutputSectionBase<ELFT>::uintX_t uintX_t;
266
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000267 bool shouldTailMerge() const;
268
Rafael Espindolac159c962015-10-19 21:00:02 +0000269public:
270 MergeOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000271 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000272 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000273 unsigned getOffset(StringRef Val);
274 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000275
276private:
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000277 llvm::StringTableBuilder Builder{llvm::StringTableBuilder::RAW};
Rafael Espindolac159c962015-10-19 21:00:02 +0000278};
279
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000280// FDE or CIE
281template <class ELFT> struct EHRegion {
282 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
283 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
284 StringRef data() const;
285 EHInputSection<ELFT> *S;
286 unsigned Index;
287};
288
289template <class ELFT> struct Cie : public EHRegion<ELFT> {
290 Cie(EHInputSection<ELFT> *S, unsigned Index);
291 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000292 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000293};
294
295template <class ELFT>
296class EHOutputSection final : public OutputSectionBase<ELFT> {
297public:
298 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
299 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
300 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
301 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
302 EHOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
303 void writeTo(uint8_t *Buf) override;
304
305 template <bool IsRela>
306 void addSectionAux(
307 EHInputSection<ELFT> *S,
308 llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, IsRela> *>
309 Rels);
310
Rui Ueyama40845e62015-12-26 05:51:07 +0000311 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000312
313private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000314 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000315 uintX_t readEntryLength(ArrayRef<uint8_t> D);
316
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000317 std::vector<EHInputSection<ELFT> *> Sections;
318 std::vector<Cie<ELFT>> Cies;
319
320 // Maps CIE content + personality to a index in Cies.
321 llvm::DenseMap<std::pair<StringRef, StringRef>, unsigned> CieMap;
322};
323
Rafael Espindolac159c962015-10-19 21:00:02 +0000324template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000325class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000326public:
327 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000328 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000329};
330
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000331template <class ELFT>
332class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000333public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000334 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000335 StringTableSection(StringRef Name, bool Dynamic);
Rui Ueyama76c00632016-01-07 02:35:32 +0000336 void reserve(StringRef S);
337 size_t addString(StringRef S);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000338 void writeTo(uint8_t *Buf) override;
Rui Ueyama76c00632016-01-07 02:35:32 +0000339 size_t getSize() const { return Used + Reserved; }
340 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000341 bool isDynamic() const { return Dynamic; }
342
343private:
344 const bool Dynamic;
Rui Ueyama76c00632016-01-07 02:35:32 +0000345 std::vector<StringRef> Strings;
346 size_t Used = 1; // ELF string tables start with a NUL byte, so 1.
347 size_t Reserved = 0;
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 Espindolade069362016-01-25 21:32:04 +0000405 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
406
407 struct Entry {
408 int32_t Tag;
409 union {
410 OutputSectionBase<ELFT> *OutSec;
411 uint64_t Val;
412 const SymbolBody *Sym;
413 };
414 enum KindT { SecAddr, SymAddr, PlainInt } Kind;
415 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
416 : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
417 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
418 Entry(int32_t Tag, const SymbolBody *Sym)
419 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
420 };
421 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000422
423public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000424 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000425 void finalize() override;
426 void writeTo(uint8_t *Buf) override;
427
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000428 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
429 OutputSectionBase<ELFT> *InitArraySec = nullptr;
430 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000431
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000432private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000433 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000434};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000435
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000436template <class ELFT>
437class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
438 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
439
440public:
441 MipsReginfoOutputSection();
442 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000443 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000444
445private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000446 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000447};
448
George Rimarf6bc65a2016-01-15 13:34:52 +0000449// --eh-frame-hdr option tells linker to construct a header for all the
450// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
451// and also to a PT_GNU_EH_FRAME segment.
452// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
453// calling dl_iterate_phdr.
454// This section contains a lookup table for quick binary search of FDEs.
455// Detailed info about internals can be found in Ian Lance Taylor's blog:
456// http://www.airs.com/blog/archives/460 (".eh_frame")
457// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
458template <class ELFT>
459class EhFrameHeader final : public OutputSectionBase<ELFT> {
460 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
461
462public:
463 EhFrameHeader();
464 void writeTo(uint8_t *Buf) override;
465
466 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
467 void assignEhFrame(EHOutputSection<ELFT> *Sec);
468 void reserveFde();
469
470 bool Live = false;
471
472private:
473 struct FdeData {
474 uint8_t Enc;
475 size_t Off;
476 uint8_t *PCRel;
477 };
478
479 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
480
481 EHOutputSection<ELFT> *Sec = nullptr;
482 std::vector<FdeData> FdeList;
483};
484
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000485// All output sections that are hadnled by the linker specially are
486// globally accessible. Writer initializes them, so don't use them
487// until Writer is initialized.
488template <class ELFT> struct Out {
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000489 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000490 typedef typename llvm::object::ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000491 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000492 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000493 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000494 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000495 static GotSection<ELFT> *Got;
496 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000497 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000498 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000499 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000500 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000501 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000502 static PltSection<ELFT> *Plt;
503 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000504 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000505 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000506 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000507 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000508 static SymbolTableSection<ELFT> *DynSymTab;
509 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000510 static Elf_Phdr *TlsPhdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000511};
Rui Ueyamad888d102015-10-09 19:34:55 +0000512
513template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000514template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000515template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000516template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000517template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
518template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000519template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000520template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000521template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000522template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000523template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000524template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
525template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000526template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000527template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000528template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000529template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000530template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
531template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000532template <class ELFT> typename Out<ELFT>::Elf_Phdr *Out<ELFT>::TlsPhdr;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000533
534} // namespace elf2
535} // namespace lld
536
537#endif // LLD_ELF_OUTPUT_SECTIONS_H