blob: 68103525dcaf06bf1d05b91588b0d48d9778f32a [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 {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000023namespace elf {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000024
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
Rafael Espindolae02f4df2016-03-08 21:17:31 +000056bool canBePreempted(const SymbolBody *Body);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000057
George Rimar12737b72016-02-25 08:40:26 +000058bool isValidCIdentifier(StringRef S);
59
Rafael Espindola71675852015-09-22 00:16:19 +000060// This represents a section in an output file.
61// Different sub classes represent different types of sections. Some contain
62// input sections, others are created by the linker.
63// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000064// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000065template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000066public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000067 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
68 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000069
George Rimar9bec24a2016-02-18 14:20:08 +000070 OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000071 void setVA(uintX_t VA) { Header.sh_addr = VA; }
72 uintX_t getVA() const { return Header.sh_addr; }
73 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000074 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000075 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000076 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000077
Rui Ueyama40845e62015-12-26 05:51:07 +000078 virtual void addSection(InputSectionBase<ELFT> *C) {}
79
Rui Ueyama2317d0d2015-10-15 20:55:22 +000080 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000081
82 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000083 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000084 void setSize(uintX_t Val) { Header.sh_size = Val; }
85 uintX_t getFlags() { return Header.sh_flags; }
86 uintX_t getFileOff() { return Header.sh_offset; }
87 uintX_t getAlign() {
88 // The ELF spec states that a value of 0 means the section has no alignment
89 // constraits.
90 return std::max<uintX_t>(Header.sh_addralign, 1);
91 }
92 uint32_t getType() { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +000093 void updateAlign(uintX_t Align) {
94 if (Align > Header.sh_addralign)
95 Header.sh_addralign = Align;
96 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000097
Rafael Espindola5805c4f2015-09-21 21:38:08 +000098 virtual void finalize() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000099 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +0000100 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000101
102protected:
103 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000104 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000105};
106
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000107template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
108 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000109 typedef typename Base::uintX_t uintX_t;
110
111public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000112 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000113 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000114 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000115 void addEntry(SymbolBody *Sym);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000116 void addMipsLocalEntry();
George Rimar90cd0a82015-12-01 19:20:26 +0000117 bool addDynTlsEntry(SymbolBody *Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000118 bool addTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000119 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000120 uintX_t getMipsLocalFullAddr(const SymbolBody &B);
121 uintX_t getMipsLocalPageAddr(uintX_t Addr);
George Rimar90cd0a82015-12-01 19:20:26 +0000122 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000123 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000124
Igor Kudrin304860a2015-11-12 04:39:49 +0000125 // Returns the symbol which corresponds to the first entry of the global part
126 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
127 // table properties.
128 // Returns nullptr if the global part is empty.
129 const SymbolBody *getMipsFirstGlobalEntry() const;
130
131 // Returns the number of entries in the local part of GOT including
132 // the number of reserved entries. This method is MIPS-specific.
133 unsigned getMipsLocalEntriesNum() const;
134
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000135 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000136
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000137private:
138 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000139 uint32_t TlsIndexOff = -1;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000140 uint32_t MipsLocalEntries = 0;
141 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
142
143 uintX_t getMipsLocalEntryAddr(uintX_t EntryValue);
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;
George Rimar648a2c32015-10-20 08:54:27 +0000156
157private:
158 std::vector<const SymbolBody *> Entries;
159};
160
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000161template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
162 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000163 typedef typename Base::uintX_t uintX_t;
164
165public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000166 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000167 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000168 void writeTo(uint8_t *Buf) override;
169 void addEntry(SymbolBody *Sym);
170 bool empty() const { return Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000171
172private:
George Rimar77b77792015-11-25 22:15:01 +0000173 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000174};
175
176template <class ELFT> struct DynamicReloc {
Rafael Espindolade9857e2016-02-04 21:33:05 +0000177 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
178 uint32_t Type;
179
180 // Where the relocation is.
181 enum OffsetKind {
182 Off_Got, // The got entry of Sym.
183 Off_GotPlt, // The got.plt entry of Sym.
184 Off_Bss, // The bss entry of Sym (copy reloc).
185 Off_Sec, // The final position of the given input section and offset.
186 Off_LTlsIndex, // The local tls index.
187 Off_GTlsIndex, // The global tls index of Sym.
188 Off_GTlsOffset // The global tls offset of Sym.
189 } OKind;
190
191 SymbolBody *Sym = nullptr;
192 InputSectionBase<ELFT> *OffsetSec = nullptr;
193 uintX_t OffsetInSec = 0;
194 bool UseSymVA = false;
195 InputSectionBase<ELFT> *TargetSec = nullptr;
196 uintX_t OffsetInTargetSec = 0;
197 uintX_t Addend = 0;
198
199 DynamicReloc(uint32_t Type, OffsetKind OKind, SymbolBody *Sym)
200 : Type(Type), OKind(OKind), Sym(Sym) {}
201
202 DynamicReloc(uint32_t Type, OffsetKind OKind, bool UseSymVA, SymbolBody *Sym)
203 : Type(Type), OKind(OKind), Sym(Sym), UseSymVA(UseSymVA) {}
204
205 DynamicReloc(uint32_t Type, InputSectionBase<ELFT> *OffsetSec,
206 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
207 uintX_t Addend)
208 : Type(Type), OKind(Off_Sec), Sym(Sym), OffsetSec(OffsetSec),
209 OffsetInSec(OffsetInSec), UseSymVA(UseSymVA), Addend(Addend) {}
210
211 DynamicReloc(uint32_t Type, InputSectionBase<ELFT> *OffsetSec,
212 uintX_t OffsetInSec, InputSectionBase<ELFT> *TargetSec,
213 uintX_t OffsetInTargetSec, uintX_t Addend)
214 : Type(Type), OKind(Off_Sec), OffsetSec(OffsetSec),
215 OffsetInSec(OffsetInSec), TargetSec(TargetSec),
216 OffsetInTargetSec(OffsetInTargetSec), Addend(Addend) {}
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000217
218 uintX_t getOffset() const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000219};
220
221template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000222class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000223public:
224 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
225 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
226 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000227 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000228 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000229 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000230
Rui Ueyama0db335f2015-10-07 16:58:54 +0000231 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000232 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000233 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000234 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000235 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000236
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000237 ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
Rafael Espindolae2c24612016-01-29 01:24:25 +0000238 return Symbols;
239 }
240
241 unsigned NumLocals = 0;
242 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000243
George Rimar4cfe5722016-03-03 07:49:35 +0000244 // Local symbol -> ID, filled only when producing relocatable output.
245 llvm::DenseMap<const Elf_Sym *, uint32_t> Locals;
246
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000247private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000248 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000249 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000250
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000251 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000252 static uint8_t getSymbolBinding(SymbolBody *Body);
253
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000254 SymbolTable<ELFT> &Table;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000255
256 // A vector of symbols and their string table offsets.
257 std::vector<std::pair<SymbolBody *, size_t>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000258};
259
260template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000261class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000262 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
263 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000264 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000265
266public:
George Rimar648a2c32015-10-20 08:54:27 +0000267 RelocationSection(StringRef Name, bool IsRela);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000268 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000269 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000270 void finalize() override;
271 void writeTo(uint8_t *Buf) override;
272 bool hasRelocs() const { return !Relocs.empty(); }
273 bool isRela() const { return IsRela; }
274
George Rimara07ff662015-12-21 10:12:06 +0000275 bool Static = false;
276
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000277private:
278 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000279 const bool IsRela;
280};
281
282template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000283class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000284public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000285 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
286 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
287 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
288 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000289 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000290 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000291 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000292 void sortInitFini();
293 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000294 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000295 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000296
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000297private:
Rui Ueyama5af83682016-02-11 23:41:38 +0000298 void reassignOffsets();
Rafael Espindola71675852015-09-22 00:16:19 +0000299 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000300};
301
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000302template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000303class MergeOutputSection final : public OutputSectionBase<ELFT> {
304 typedef typename OutputSectionBase<ELFT>::uintX_t uintX_t;
305
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000306 bool shouldTailMerge() const;
307
Rafael Espindolac159c962015-10-19 21:00:02 +0000308public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000309 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
310 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000311 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000312 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000313 unsigned getOffset(StringRef Val);
314 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000315
316private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000317 llvm::StringTableBuilder Builder;
Rafael Espindolac159c962015-10-19 21:00:02 +0000318};
319
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000320// FDE or CIE
321template <class ELFT> struct EHRegion {
322 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
323 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
324 StringRef data() const;
325 EHInputSection<ELFT> *S;
326 unsigned Index;
327};
328
329template <class ELFT> struct Cie : public EHRegion<ELFT> {
330 Cie(EHInputSection<ELFT> *S, unsigned Index);
331 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000332 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000333};
334
335template <class ELFT>
336class EHOutputSection final : public OutputSectionBase<ELFT> {
337public:
338 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
339 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
340 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
341 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
George Rimar9bec24a2016-02-18 14:20:08 +0000342 EHOutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000343 void writeTo(uint8_t *Buf) override;
344
345 template <bool IsRela>
346 void addSectionAux(
347 EHInputSection<ELFT> *S,
348 llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, IsRela> *>
349 Rels);
350
Rui Ueyama40845e62015-12-26 05:51:07 +0000351 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000352
353private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000354 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000355
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000356 std::vector<EHInputSection<ELFT> *> Sections;
357 std::vector<Cie<ELFT>> Cies;
358
359 // Maps CIE content + personality to a index in Cies.
Rafael Espindola156ed8d2016-02-10 13:19:32 +0000360 llvm::DenseMap<std::pair<StringRef, SymbolBody *>, unsigned> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000361};
362
Rafael Espindolac159c962015-10-19 21:00:02 +0000363template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000364class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000365public:
366 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000367 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000368};
369
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000370template <class ELFT>
371class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000372public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000373 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000374 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000375 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000376 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000377 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000378 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000379 bool isDynamic() const { return Dynamic; }
380
381private:
382 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000383 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000384 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000385 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000386};
387
388template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000389class HashTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000390 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
391
392public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000393 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000394 void finalize() override;
395 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000396};
397
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000398// Outputs GNU Hash section. For detailed explanation see:
399// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
400template <class ELFT>
401class GnuHashTableSection final : public OutputSectionBase<ELFT> {
402 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
403 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
404 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
405
406public:
407 GnuHashTableSection();
408 void finalize() override;
409 void writeTo(uint8_t *Buf) override;
410
Igor Kudrinf1d60292015-10-28 07:05:56 +0000411 // Adds symbols to the hash table.
412 // Sorts the input to satisfy GNU hash section requirements.
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000413 void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000414
415private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000416 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000417 static unsigned calcMaskWords(unsigned NumHashed);
418
419 void writeHeader(uint8_t *&Buf);
420 void writeBloomFilter(uint8_t *&Buf);
421 void writeHashTable(uint8_t *Buf);
422
Rui Ueyama861c7312016-02-17 05:40:03 +0000423 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000424 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000425 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000426 uint32_t Hash;
427 };
428
Rui Ueyama861c7312016-02-17 05:40:03 +0000429 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000430
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000431 unsigned MaskWords;
432 unsigned NBuckets;
433 unsigned Shift2;
434};
435
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000436template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000437class DynamicSection final : public OutputSectionBase<ELFT> {
438 typedef OutputSectionBase<ELFT> Base;
439 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000440 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
441 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000442 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000443 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindolade069362016-01-25 21:32:04 +0000444 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
445
Rui Ueyama909cc682016-02-02 03:11:27 +0000446 // The .dynamic section contains information for the dynamic linker.
447 // The section consists of fixed size entries, which consist of
448 // type and value fields. Value are one of plain integers, symbol
449 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000450 struct Entry {
451 int32_t Tag;
452 union {
453 OutputSectionBase<ELFT> *OutSec;
454 uint64_t Val;
455 const SymbolBody *Sym;
456 };
457 enum KindT { SecAddr, SymAddr, PlainInt } Kind;
458 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
459 : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
460 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
461 Entry(int32_t Tag, const SymbolBody *Sym)
462 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
463 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000464
465 // finalize() fills this vector with the section contents. finalize()
466 // cannot directly create final section contents because when the
467 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000468 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000469
470public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000471 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000472 void finalize() override;
473 void writeTo(uint8_t *Buf) override;
474
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000475 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
476 OutputSectionBase<ELFT> *InitArraySec = nullptr;
477 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000478
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000479private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000480 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000481};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000482
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000483template <class ELFT>
484class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
485 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
486
487public:
488 MipsReginfoOutputSection();
489 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000490 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000491
492private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000493 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000494};
495
George Rimarf6bc65a2016-01-15 13:34:52 +0000496// --eh-frame-hdr option tells linker to construct a header for all the
497// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
498// and also to a PT_GNU_EH_FRAME segment.
499// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
500// calling dl_iterate_phdr.
501// This section contains a lookup table for quick binary search of FDEs.
502// Detailed info about internals can be found in Ian Lance Taylor's blog:
503// http://www.airs.com/blog/archives/460 (".eh_frame")
504// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
505template <class ELFT>
506class EhFrameHeader final : public OutputSectionBase<ELFT> {
507 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
508
509public:
510 EhFrameHeader();
511 void writeTo(uint8_t *Buf) override;
512
513 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
514 void assignEhFrame(EHOutputSection<ELFT> *Sec);
515 void reserveFde();
516
517 bool Live = false;
518
519private:
520 struct FdeData {
521 uint8_t Enc;
522 size_t Off;
523 uint8_t *PCRel;
524 };
525
526 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
527
528 EHOutputSection<ELFT> *Sec = nullptr;
529 std::vector<FdeData> FdeList;
530};
531
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000532// All output sections that are hadnled by the linker specially are
533// globally accessible. Writer initializes them, so don't use them
534// until Writer is initialized.
535template <class ELFT> struct Out {
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000536 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000537 typedef typename llvm::object::ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000538 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000539 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000540 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000541 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000542 static GotSection<ELFT> *Got;
543 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000544 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000545 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000546 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000547 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000548 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000549 static PltSection<ELFT> *Plt;
550 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000551 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000552 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000553 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000554 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000555 static SymbolTableSection<ELFT> *DynSymTab;
556 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000557 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000558 static OutputSectionBase<ELFT> *ElfHeader;
559 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000560};
Rui Ueyamad888d102015-10-09 19:34:55 +0000561
562template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000563template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000564template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000565template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000566template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
567template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000568template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000569template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000570template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000571template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000572template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000573template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
574template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000575template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000576template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000577template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000578template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000579template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
580template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000581template <class ELFT> typename Out<ELFT>::Elf_Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000582template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
583template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000584
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000585} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000586} // namespace lld
587
588#endif // LLD_ELF_OUTPUT_SECTIONS_H