blob: ebaa3f559418f1796ae6c16fc87bade3bafd273d [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
15#include "llvm/MC/StringTableBuilder.h"
16#include "llvm/Object/ELF.h"
17
Davide Italiano85121bb2015-09-25 03:56:11 +000018#include "Config.h"
19
Rafael Espindola5805c4f2015-09-21 21:38:08 +000020#include <type_traits>
21
22namespace lld {
23namespace elf2 {
24
25class SymbolBody;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000026template <class ELFT> class SymbolTable;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000027template <class ELFT> class SymbolTableSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000028template <class ELFT> class StringTableSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000029template <class ELFT> class EHInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000030template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000031template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000032template <class ELFT> class MergeInputSection;
Simon Atanasyan1d7df402015-12-20 10:57:34 +000033template <class ELFT> class MipsReginfoInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000034template <class ELFT> class OutputSection;
35template <class ELFT> class ObjectFile;
36template <class ELFT> class DefinedRegular;
37
Rafael Espindola5805c4f2015-09-21 21:38:08 +000038template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000039static inline typename llvm::object::ELFFile<ELFT>::uintX_t
40getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rel &Rel) {
41 return 0;
42}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000043
44template <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_Rela &Rel) {
47 return Rel.r_addend;
48}
49
Rafael Espindola932efcf2015-10-19 20:24:44 +000050template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +000051typename llvm::object::ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +000052getLocalRelTarget(const ObjectFile<ELFT> &File,
George Rimar0b8ed1d2015-12-21 10:37:33 +000053 const llvm::object::Elf_Rel_Impl<ELFT, IsRela> &Rel,
54 typename llvm::object::ELFFile<ELFT>::uintX_t Addend);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000055
Rui Ueyama89f4ec72015-12-25 07:01:09 +000056bool canBePreempted(const SymbolBody *Body, bool NeedsGot);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000057
Rafael Espindola71675852015-09-22 00:16:19 +000058// This represents a section in an output file.
59// Different sub classes represent different types of sections. Some contain
60// input sections, others are created by the linker.
61// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000062// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000063template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000064public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000065 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
66 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000067
68 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
69 void setVA(uintX_t VA) { Header.sh_addr = VA; }
70 uintX_t getVA() const { return Header.sh_addr; }
71 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000072 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000073 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000074 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000075
Rui Ueyama40845e62015-12-26 05:51:07 +000076 virtual void addSection(InputSectionBase<ELFT> *C) {}
77
Rui Ueyama2317d0d2015-10-15 20:55:22 +000078 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000079
80 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000081 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000082 void setSize(uintX_t Val) { Header.sh_size = Val; }
83 uintX_t getFlags() { return Header.sh_flags; }
84 uintX_t getFileOff() { return Header.sh_offset; }
85 uintX_t getAlign() {
86 // The ELF spec states that a value of 0 means the section has no alignment
87 // constraits.
88 return std::max<uintX_t>(Header.sh_addralign, 1);
89 }
90 uint32_t getType() { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +000091 void updateAlign(uintX_t Align) {
92 if (Align > Header.sh_addralign)
93 Header.sh_addralign = Align;
94 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000095
Rafael Espindola5805c4f2015-09-21 21:38:08 +000096 virtual void finalize() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000097 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000098 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000099
100protected:
101 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000102 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000103};
104
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000105template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
106 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000107 typedef typename Base::uintX_t uintX_t;
108
109public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000110 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000111 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000112 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000113 void addEntry(SymbolBody *Sym);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000114 void addMipsLocalEntry();
George Rimar90cd0a82015-12-01 19:20:26 +0000115 bool addDynTlsEntry(SymbolBody *Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000116 bool addTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000117 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000118 uintX_t getMipsLocalFullAddr(const SymbolBody &B);
119 uintX_t getMipsLocalPageAddr(uintX_t Addr);
George Rimar90cd0a82015-12-01 19:20:26 +0000120 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000121 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000122
Igor Kudrin304860a2015-11-12 04:39:49 +0000123 // Returns the symbol which corresponds to the first entry of the global part
124 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
125 // table properties.
126 // Returns nullptr if the global part is empty.
127 const SymbolBody *getMipsFirstGlobalEntry() const;
128
129 // Returns the number of entries in the local part of GOT including
130 // the number of reserved entries. This method is MIPS-specific.
131 unsigned getMipsLocalEntriesNum() const;
132
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000133 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000134
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000135private:
136 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000137 uint32_t TlsIndexOff = -1;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000138 uint32_t MipsLocalEntries = 0;
139 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
140
141 uintX_t getMipsLocalEntryAddr(uintX_t EntryValue);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000142};
143
George Rimar648a2c32015-10-20 08:54:27 +0000144template <class ELFT>
145class GotPltSection final : public OutputSectionBase<ELFT> {
146 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
147
148public:
149 GotPltSection();
150 void finalize() override;
151 void writeTo(uint8_t *Buf) override;
152 void addEntry(SymbolBody *Sym);
153 bool empty() const;
George Rimar648a2c32015-10-20 08:54:27 +0000154
155private:
156 std::vector<const SymbolBody *> Entries;
157};
158
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000159template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
160 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000161 typedef typename Base::uintX_t uintX_t;
162
163public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000164 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000165 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000166 void writeTo(uint8_t *Buf) override;
167 void addEntry(SymbolBody *Sym);
168 bool empty() const { return Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000169
170private:
George Rimar77b77792015-11-25 22:15:01 +0000171 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000172};
173
174template <class ELFT> struct DynamicReloc {
Rafael Espindolade9857e2016-02-04 21:33:05 +0000175 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
176 uint32_t Type;
177
178 // Where the relocation is.
179 enum OffsetKind {
180 Off_Got, // The got entry of Sym.
181 Off_GotPlt, // The got.plt entry of Sym.
182 Off_Bss, // The bss entry of Sym (copy reloc).
183 Off_Sec, // The final position of the given input section and offset.
184 Off_LTlsIndex, // The local tls index.
185 Off_GTlsIndex, // The global tls index of Sym.
186 Off_GTlsOffset // The global tls offset of Sym.
187 } OKind;
188
189 SymbolBody *Sym = nullptr;
190 InputSectionBase<ELFT> *OffsetSec = nullptr;
191 uintX_t OffsetInSec = 0;
192 bool UseSymVA = false;
193 InputSectionBase<ELFT> *TargetSec = nullptr;
194 uintX_t OffsetInTargetSec = 0;
195 uintX_t Addend = 0;
196
197 DynamicReloc(uint32_t Type, OffsetKind OKind, SymbolBody *Sym)
198 : Type(Type), OKind(OKind), Sym(Sym) {}
199
200 DynamicReloc(uint32_t Type, OffsetKind OKind, bool UseSymVA, SymbolBody *Sym)
201 : Type(Type), OKind(OKind), Sym(Sym), UseSymVA(UseSymVA) {}
202
203 DynamicReloc(uint32_t Type, InputSectionBase<ELFT> *OffsetSec,
204 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
205 uintX_t Addend)
206 : Type(Type), OKind(Off_Sec), Sym(Sym), OffsetSec(OffsetSec),
207 OffsetInSec(OffsetInSec), UseSymVA(UseSymVA), Addend(Addend) {}
208
209 DynamicReloc(uint32_t Type, InputSectionBase<ELFT> *OffsetSec,
210 uintX_t OffsetInSec, InputSectionBase<ELFT> *TargetSec,
211 uintX_t OffsetInTargetSec, uintX_t Addend)
212 : Type(Type), OKind(Off_Sec), OffsetSec(OffsetSec),
213 OffsetInSec(OffsetInSec), TargetSec(TargetSec),
214 OffsetInTargetSec(OffsetInTargetSec), Addend(Addend) {}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000215};
216
217template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000218class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000219public:
220 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
221 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
222 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000223 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000224 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000225 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000226
Rui Ueyama0db335f2015-10-07 16:58:54 +0000227 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000228 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000229 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000230 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000231 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000232
Rafael Espindolae2c24612016-01-29 01:24:25 +0000233 ArrayRef<std::pair<SymbolBody *, unsigned>> getSymbols() const {
234 return Symbols;
235 }
236
237 unsigned NumLocals = 0;
238 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000239
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000240private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000241 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000242 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000243
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000244 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000245 static uint8_t getSymbolBinding(SymbolBody *Body);
246
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000247 SymbolTable<ELFT> &Table;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000248 std::vector<std::pair<SymbolBody *, unsigned>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000249};
250
251template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000252class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000253 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
254 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000255 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000256
257public:
George Rimar648a2c32015-10-20 08:54:27 +0000258 RelocationSection(StringRef Name, bool IsRela);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000259 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000260 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000261 void finalize() override;
262 void writeTo(uint8_t *Buf) override;
263 bool hasRelocs() const { return !Relocs.empty(); }
264 bool isRela() const { return IsRela; }
265
George Rimara07ff662015-12-21 10:12:06 +0000266 bool Static = false;
267
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000268private:
269 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000270 const bool IsRela;
271};
272
273template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000274class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000275public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000276 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
277 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
278 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
279 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000280 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000281 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000282 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000283 void sortInitFini();
284 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000285 void writeTo(uint8_t *Buf) override;
286
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000287private:
Rui Ueyama5af83682016-02-11 23:41:38 +0000288 void reassignOffsets();
Rafael Espindola71675852015-09-22 00:16:19 +0000289 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000290};
291
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000292template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000293class MergeOutputSection final : public OutputSectionBase<ELFT> {
294 typedef typename OutputSectionBase<ELFT>::uintX_t uintX_t;
295
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000296 bool shouldTailMerge() const;
297
Rafael Espindolac159c962015-10-19 21:00:02 +0000298public:
299 MergeOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000300 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000301 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000302 unsigned getOffset(StringRef Val);
303 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000304
305private:
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000306 llvm::StringTableBuilder Builder{llvm::StringTableBuilder::RAW};
Rafael Espindolac159c962015-10-19 21:00:02 +0000307};
308
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000309// FDE or CIE
310template <class ELFT> struct EHRegion {
311 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
312 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
313 StringRef data() const;
314 EHInputSection<ELFT> *S;
315 unsigned Index;
316};
317
318template <class ELFT> struct Cie : public EHRegion<ELFT> {
319 Cie(EHInputSection<ELFT> *S, unsigned Index);
320 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000321 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000322};
323
324template <class ELFT>
325class EHOutputSection final : public OutputSectionBase<ELFT> {
326public:
327 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
328 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
329 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
330 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
331 EHOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
332 void writeTo(uint8_t *Buf) override;
333
334 template <bool IsRela>
335 void addSectionAux(
336 EHInputSection<ELFT> *S,
337 llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, IsRela> *>
338 Rels);
339
Rui Ueyama40845e62015-12-26 05:51:07 +0000340 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000341
342private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000343 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000344
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000345 std::vector<EHInputSection<ELFT> *> Sections;
346 std::vector<Cie<ELFT>> Cies;
347
348 // Maps CIE content + personality to a index in Cies.
Rafael Espindola156ed8d2016-02-10 13:19:32 +0000349 llvm::DenseMap<std::pair<StringRef, SymbolBody *>, unsigned> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000350};
351
Rafael Espindolac159c962015-10-19 21:00:02 +0000352template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000353class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000354public:
355 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000356 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000357};
358
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000359template <class ELFT>
360class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000361public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000362 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000363 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000364 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000365 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000366 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000367 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000368 bool isDynamic() const { return Dynamic; }
369
370private:
371 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000372 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000373 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000374 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000375};
376
377template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000378class HashTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000379 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
380
381public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000382 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000383 void finalize() override;
384 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000385};
386
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000387// Outputs GNU Hash section. For detailed explanation see:
388// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
389template <class ELFT>
390class GnuHashTableSection final : public OutputSectionBase<ELFT> {
391 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
392 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
393 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
394
395public:
396 GnuHashTableSection();
397 void finalize() override;
398 void writeTo(uint8_t *Buf) override;
399
Igor Kudrinf1d60292015-10-28 07:05:56 +0000400 // Adds symbols to the hash table.
401 // Sorts the input to satisfy GNU hash section requirements.
Rafael Espindolae2c24612016-01-29 01:24:25 +0000402 void addSymbols(std::vector<std::pair<SymbolBody *, unsigned>> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000403
404private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000405 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000406 static unsigned calcMaskWords(unsigned NumHashed);
407
408 void writeHeader(uint8_t *&Buf);
409 void writeBloomFilter(uint8_t *&Buf);
410 void writeHashTable(uint8_t *Buf);
411
Igor Kudrinf1d60292015-10-28 07:05:56 +0000412 struct HashedSymbolData {
413 SymbolBody *Body;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000414 unsigned STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000415 uint32_t Hash;
416 };
417
418 std::vector<HashedSymbolData> HashedSymbols;
419
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000420 unsigned MaskWords;
421 unsigned NBuckets;
422 unsigned Shift2;
423};
424
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000425template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000426class DynamicSection final : public OutputSectionBase<ELFT> {
427 typedef OutputSectionBase<ELFT> Base;
428 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000429 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
430 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000431 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000432 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindolade069362016-01-25 21:32:04 +0000433 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
434
Rui Ueyama909cc682016-02-02 03:11:27 +0000435 // The .dynamic section contains information for the dynamic linker.
436 // The section consists of fixed size entries, which consist of
437 // type and value fields. Value are one of plain integers, symbol
438 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000439 struct Entry {
440 int32_t Tag;
441 union {
442 OutputSectionBase<ELFT> *OutSec;
443 uint64_t Val;
444 const SymbolBody *Sym;
445 };
446 enum KindT { SecAddr, SymAddr, PlainInt } Kind;
447 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
448 : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
449 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
450 Entry(int32_t Tag, const SymbolBody *Sym)
451 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
452 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000453
454 // finalize() fills this vector with the section contents. finalize()
455 // cannot directly create final section contents because when the
456 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000457 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000458
459public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000460 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000461 void finalize() override;
462 void writeTo(uint8_t *Buf) override;
463
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000464 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
465 OutputSectionBase<ELFT> *InitArraySec = nullptr;
466 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000467
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000468private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000469 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000470};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000471
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000472template <class ELFT>
473class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
474 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
475
476public:
477 MipsReginfoOutputSection();
478 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000479 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000480
481private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000482 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000483};
484
George Rimarf6bc65a2016-01-15 13:34:52 +0000485// --eh-frame-hdr option tells linker to construct a header for all the
486// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
487// and also to a PT_GNU_EH_FRAME segment.
488// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
489// calling dl_iterate_phdr.
490// This section contains a lookup table for quick binary search of FDEs.
491// Detailed info about internals can be found in Ian Lance Taylor's blog:
492// http://www.airs.com/blog/archives/460 (".eh_frame")
493// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
494template <class ELFT>
495class EhFrameHeader final : public OutputSectionBase<ELFT> {
496 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
497
498public:
499 EhFrameHeader();
500 void writeTo(uint8_t *Buf) override;
501
502 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
503 void assignEhFrame(EHOutputSection<ELFT> *Sec);
504 void reserveFde();
505
506 bool Live = false;
507
508private:
509 struct FdeData {
510 uint8_t Enc;
511 size_t Off;
512 uint8_t *PCRel;
513 };
514
515 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
516
517 EHOutputSection<ELFT> *Sec = nullptr;
518 std::vector<FdeData> FdeList;
519};
520
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000521// All output sections that are hadnled by the linker specially are
522// globally accessible. Writer initializes them, so don't use them
523// until Writer is initialized.
524template <class ELFT> struct Out {
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000525 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000526 typedef typename llvm::object::ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000527 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000528 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000529 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000530 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000531 static GotSection<ELFT> *Got;
532 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000533 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000534 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000535 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000536 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000537 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000538 static PltSection<ELFT> *Plt;
539 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000540 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000541 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000542 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000543 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000544 static SymbolTableSection<ELFT> *DynSymTab;
545 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000546 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000547 static OutputSectionBase<ELFT> *ElfHeader;
548 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000549};
Rui Ueyamad888d102015-10-09 19:34:55 +0000550
551template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000552template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000553template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000554template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000555template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
556template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000557template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000558template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000559template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000560template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000561template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000562template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
563template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000564template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000565template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000566template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000567template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000568template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
569template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000570template <class ELFT> typename Out<ELFT>::Elf_Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000571template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
572template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000573
574} // namespace elf2
575} // namespace lld
576
577#endif // LLD_ELF_OUTPUT_SECTIONS_H