blob: 5004c252d875e7f7ccc43762c17449cb44553a53 [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
Davide Italiano85121bb2015-09-25 03:56:11 +000013#include "Config.h"
14
Rui Ueyamaa0752a52016-03-13 20:28:29 +000015#include "lld/Core/LLVM.h"
16#include "llvm/MC/StringTableBuilder.h"
17#include "llvm/Object/ELF.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000018
19namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000020namespace elf {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000021
22class SymbolBody;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000023template <class ELFT> class SymbolTable;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000024template <class ELFT> class SymbolTableSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000025template <class ELFT> class StringTableSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000026template <class ELFT> class EHInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000027template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000028template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000029template <class ELFT> class MergeInputSection;
Simon Atanasyan1d7df402015-12-20 10:57:34 +000030template <class ELFT> class MipsReginfoInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000031template <class ELFT> class OutputSection;
32template <class ELFT> class ObjectFile;
33template <class ELFT> class DefinedRegular;
34
Rafael Espindola5805c4f2015-09-21 21:38:08 +000035template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000036static inline typename llvm::object::ELFFile<ELFT>::uintX_t
37getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rel &Rel) {
38 return 0;
39}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000040
41template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000042static inline typename llvm::object::ELFFile<ELFT>::uintX_t
43getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rela &Rel) {
44 return Rel.r_addend;
45}
46
George Rimar12737b72016-02-25 08:40:26 +000047bool isValidCIdentifier(StringRef S);
48
Rafael Espindola71675852015-09-22 00:16:19 +000049// This represents a section in an output file.
50// Different sub classes represent different types of sections. Some contain
51// input sections, others are created by the linker.
52// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000053// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000054template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000055public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000056 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
57 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000058
George Rimar9bec24a2016-02-18 14:20:08 +000059 OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000060 void setVA(uintX_t VA) { Header.sh_addr = VA; }
61 uintX_t getVA() const { return Header.sh_addr; }
62 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000063 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000064 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000065 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000066
Rui Ueyama40845e62015-12-26 05:51:07 +000067 virtual void addSection(InputSectionBase<ELFT> *C) {}
68
Rui Ueyama2317d0d2015-10-15 20:55:22 +000069 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000070
71 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000072 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000073 void setSize(uintX_t Val) { Header.sh_size = Val; }
74 uintX_t getFlags() { return Header.sh_flags; }
75 uintX_t getFileOff() { return Header.sh_offset; }
76 uintX_t getAlign() {
77 // The ELF spec states that a value of 0 means the section has no alignment
78 // constraits.
79 return std::max<uintX_t>(Header.sh_addralign, 1);
80 }
81 uint32_t getType() { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +000082 void updateAlign(uintX_t Align) {
83 if (Align > Header.sh_addralign)
84 Header.sh_addralign = Align;
85 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000086
Rafael Espindola5805c4f2015-09-21 21:38:08 +000087 virtual void finalize() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000088 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000089 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000090
91protected:
92 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000093 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000094};
95
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000096template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
97 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000098 typedef typename Base::uintX_t uintX_t;
99
100public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000101 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000102 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000103 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000104 void addEntry(SymbolBody &Sym);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000105 void addMipsLocalEntry();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000106 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000107 bool addTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000108 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000109 uintX_t getMipsLocalFullAddr(const SymbolBody &B);
110 uintX_t getMipsLocalPageAddr(uintX_t Addr);
George Rimar90cd0a82015-12-01 19:20:26 +0000111 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000112 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000113
Igor Kudrin304860a2015-11-12 04:39:49 +0000114 // Returns the symbol which corresponds to the first entry of the global part
115 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
116 // table properties.
117 // Returns nullptr if the global part is empty.
118 const SymbolBody *getMipsFirstGlobalEntry() const;
119
120 // Returns the number of entries in the local part of GOT including
121 // the number of reserved entries. This method is MIPS-specific.
122 unsigned getMipsLocalEntriesNum() const;
123
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000124 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000125
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000126private:
127 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000128 uint32_t TlsIndexOff = -1;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000129 uint32_t MipsLocalEntries = 0;
130 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
131
132 uintX_t getMipsLocalEntryAddr(uintX_t EntryValue);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000133};
134
George Rimar648a2c32015-10-20 08:54:27 +0000135template <class ELFT>
136class GotPltSection final : public OutputSectionBase<ELFT> {
137 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
138
139public:
140 GotPltSection();
141 void finalize() override;
142 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000143 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000144 bool empty() const;
George Rimar648a2c32015-10-20 08:54:27 +0000145
146private:
147 std::vector<const SymbolBody *> Entries;
148};
149
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000150template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
151 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000152 typedef typename Base::uintX_t uintX_t;
153
154public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000155 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000156 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000157 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000158 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000159 bool empty() const { return Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000160
161private:
George Rimar77b77792015-11-25 22:15:01 +0000162 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000163};
164
165template <class ELFT> struct DynamicReloc {
Rafael Espindolade9857e2016-02-04 21:33:05 +0000166 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
167 uint32_t Type;
168
169 // Where the relocation is.
170 enum OffsetKind {
171 Off_Got, // The got entry of Sym.
172 Off_GotPlt, // The got.plt entry of Sym.
173 Off_Bss, // The bss entry of Sym (copy reloc).
174 Off_Sec, // The final position of the given input section and offset.
175 Off_LTlsIndex, // The local tls index.
176 Off_GTlsIndex, // The global tls index of Sym.
177 Off_GTlsOffset // The global tls offset of Sym.
178 } OKind;
179
180 SymbolBody *Sym = nullptr;
181 InputSectionBase<ELFT> *OffsetSec = nullptr;
182 uintX_t OffsetInSec = 0;
183 bool UseSymVA = false;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000184 uintX_t Addend = 0;
185
186 DynamicReloc(uint32_t Type, OffsetKind OKind, SymbolBody *Sym)
187 : Type(Type), OKind(OKind), Sym(Sym) {}
188
189 DynamicReloc(uint32_t Type, OffsetKind OKind, bool UseSymVA, SymbolBody *Sym)
190 : Type(Type), OKind(OKind), Sym(Sym), UseSymVA(UseSymVA) {}
191
192 DynamicReloc(uint32_t Type, InputSectionBase<ELFT> *OffsetSec,
193 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
194 uintX_t Addend)
195 : Type(Type), OKind(Off_Sec), Sym(Sym), OffsetSec(OffsetSec),
196 OffsetInSec(OffsetInSec), UseSymVA(UseSymVA), Addend(Addend) {}
197
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000198 uintX_t getOffset() const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000199};
200
201template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000202class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000203public:
204 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
205 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
206 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000207 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000208 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000209 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000210
Rui Ueyama0db335f2015-10-07 16:58:54 +0000211 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000212 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000213 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000214 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000215 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000216
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000217 ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
Rafael Espindolae2c24612016-01-29 01:24:25 +0000218 return Symbols;
219 }
220
221 unsigned NumLocals = 0;
222 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000223
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000224private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000225 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000226 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000227
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000228 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000229 static uint8_t getSymbolBinding(SymbolBody *Body);
230
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000231 SymbolTable<ELFT> &Table;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000232
233 // A vector of symbols and their string table offsets.
234 std::vector<std::pair<SymbolBody *, size_t>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000235};
236
237template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000238class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000239 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
240 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000241 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000242
243public:
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000244 RelocationSection(StringRef Name);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000245 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000246 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000247 void finalize() override;
248 void writeTo(uint8_t *Buf) override;
249 bool hasRelocs() const { return !Relocs.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000250
George Rimara07ff662015-12-21 10:12:06 +0000251 bool Static = false;
252
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000253private:
254 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000255};
256
257template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000258class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000259public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000260 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
261 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
262 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
263 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000264 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000265 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000266 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000267 void sortInitFini();
268 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000269 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000270 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000271
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000272private:
Rui Ueyama5af83682016-02-11 23:41:38 +0000273 void reassignOffsets();
Rafael Espindola71675852015-09-22 00:16:19 +0000274 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000275};
276
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000277template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000278class MergeOutputSection final : public OutputSectionBase<ELFT> {
279 typedef typename OutputSectionBase<ELFT>::uintX_t uintX_t;
280
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000281 bool shouldTailMerge() const;
282
Rafael Espindolac159c962015-10-19 21:00:02 +0000283public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000284 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
285 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000286 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000287 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000288 unsigned getOffset(StringRef Val);
289 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000290
291private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000292 llvm::StringTableBuilder Builder;
Rafael Espindolac159c962015-10-19 21:00:02 +0000293};
294
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000295// FDE or CIE
296template <class ELFT> struct EHRegion {
297 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
298 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
299 StringRef data() const;
300 EHInputSection<ELFT> *S;
301 unsigned Index;
302};
303
304template <class ELFT> struct Cie : public EHRegion<ELFT> {
305 Cie(EHInputSection<ELFT> *S, unsigned Index);
306 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000307 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000308};
309
310template <class ELFT>
311class EHOutputSection final : public OutputSectionBase<ELFT> {
312public:
313 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
314 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
315 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
316 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
George Rimar9bec24a2016-02-18 14:20:08 +0000317 EHOutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000318 void writeTo(uint8_t *Buf) override;
319
Rui Ueyamafc467e72016-03-13 05:06:50 +0000320 template <class RelTy>
321 void addSectionAux(EHInputSection<ELFT> *S,
322 llvm::iterator_range<const RelTy *> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000323
Rui Ueyama40845e62015-12-26 05:51:07 +0000324 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000325
326private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000327 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000328
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000329 std::vector<EHInputSection<ELFT> *> Sections;
330 std::vector<Cie<ELFT>> Cies;
331
332 // Maps CIE content + personality to a index in Cies.
Rafael Espindola156ed8d2016-02-10 13:19:32 +0000333 llvm::DenseMap<std::pair<StringRef, SymbolBody *>, unsigned> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000334};
335
Rafael Espindolac159c962015-10-19 21:00:02 +0000336template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000337class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000338public:
339 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000340 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000341};
342
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000343template <class ELFT>
344class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000345public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000346 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000347 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000348 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000349 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000350 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000351 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000352 bool isDynamic() const { return Dynamic; }
353
354private:
355 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000356 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000357 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000358 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000359};
360
361template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000362class HashTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000363 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
364
365public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000366 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000367 void finalize() override;
368 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000369};
370
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000371// Outputs GNU Hash section. For detailed explanation see:
372// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
373template <class ELFT>
374class GnuHashTableSection final : public OutputSectionBase<ELFT> {
375 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
376 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
377 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
378
379public:
380 GnuHashTableSection();
381 void finalize() override;
382 void writeTo(uint8_t *Buf) override;
383
Igor Kudrinf1d60292015-10-28 07:05:56 +0000384 // Adds symbols to the hash table.
385 // Sorts the input to satisfy GNU hash section requirements.
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000386 void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000387
388private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000389 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000390 static unsigned calcMaskWords(unsigned NumHashed);
391
392 void writeHeader(uint8_t *&Buf);
393 void writeBloomFilter(uint8_t *&Buf);
394 void writeHashTable(uint8_t *Buf);
395
Rui Ueyama861c7312016-02-17 05:40:03 +0000396 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000397 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000398 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000399 uint32_t Hash;
400 };
401
Rui Ueyama861c7312016-02-17 05:40:03 +0000402 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000403
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000404 unsigned MaskWords;
405 unsigned NBuckets;
406 unsigned Shift2;
407};
408
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000409template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000410class DynamicSection final : public OutputSectionBase<ELFT> {
411 typedef OutputSectionBase<ELFT> Base;
412 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000413 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
414 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000415 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000416 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindolade069362016-01-25 21:32:04 +0000417 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
418
Rui Ueyama909cc682016-02-02 03:11:27 +0000419 // The .dynamic section contains information for the dynamic linker.
420 // The section consists of fixed size entries, which consist of
421 // type and value fields. Value are one of plain integers, symbol
422 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000423 struct Entry {
424 int32_t Tag;
425 union {
426 OutputSectionBase<ELFT> *OutSec;
427 uint64_t Val;
428 const SymbolBody *Sym;
429 };
430 enum KindT { SecAddr, SymAddr, PlainInt } Kind;
431 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
432 : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
433 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
434 Entry(int32_t Tag, const SymbolBody *Sym)
435 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
436 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000437
438 // finalize() fills this vector with the section contents. finalize()
439 // cannot directly create final section contents because when the
440 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000441 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000442
443public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000444 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000445 void finalize() override;
446 void writeTo(uint8_t *Buf) override;
447
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000448 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
449 OutputSectionBase<ELFT> *InitArraySec = nullptr;
450 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000451
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000452private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000453 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000454};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000455
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000456template <class ELFT>
457class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
458 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
459
460public:
461 MipsReginfoOutputSection();
462 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000463 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000464
465private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000466 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000467};
468
George Rimarf6bc65a2016-01-15 13:34:52 +0000469// --eh-frame-hdr option tells linker to construct a header for all the
470// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
471// and also to a PT_GNU_EH_FRAME segment.
472// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
473// calling dl_iterate_phdr.
474// This section contains a lookup table for quick binary search of FDEs.
475// Detailed info about internals can be found in Ian Lance Taylor's blog:
476// http://www.airs.com/blog/archives/460 (".eh_frame")
477// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
478template <class ELFT>
479class EhFrameHeader final : public OutputSectionBase<ELFT> {
480 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
481
482public:
483 EhFrameHeader();
484 void writeTo(uint8_t *Buf) override;
485
486 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
487 void assignEhFrame(EHOutputSection<ELFT> *Sec);
488 void reserveFde();
489
490 bool Live = false;
491
492private:
493 struct FdeData {
494 uint8_t Enc;
495 size_t Off;
496 uint8_t *PCRel;
497 };
498
499 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
500
501 EHOutputSection<ELFT> *Sec = nullptr;
502 std::vector<FdeData> FdeList;
503};
504
Rui Ueyama634ddf02016-03-11 20:51:53 +0000505template <class ELFT>
506class BuildIdSection final : public OutputSectionBase<ELFT> {
507public:
508 BuildIdSection();
509 void writeTo(uint8_t *Buf) override;
510 void update(ArrayRef<uint8_t> Buf);
511 void writeBuildId();
512
513private:
514 uint64_t Hash = 0xcbf29ce484222325; // FNV1 hash basis
515 uint8_t *HashBuf;
516};
517
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000518// All output sections that are hadnled by the linker specially are
519// globally accessible. Writer initializes them, so don't use them
520// until Writer is initialized.
521template <class ELFT> struct Out {
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000522 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000523 typedef typename llvm::object::ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000524 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000525 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000526 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000527 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000528 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000529 static GotSection<ELFT> *Got;
530 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000531 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000532 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000533 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000534 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000535 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000536 static PltSection<ELFT> *Plt;
537 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000538 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000539 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000540 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000541 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000542 static SymbolTableSection<ELFT> *DynSymTab;
543 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000544 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000545 static OutputSectionBase<ELFT> *ElfHeader;
546 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000547};
Rui Ueyamad888d102015-10-09 19:34:55 +0000548
Rui Ueyama634ddf02016-03-11 20:51:53 +0000549template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000550template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000551template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000552template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000553template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000554template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
555template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000556template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000557template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000558template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000559template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000560template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000561template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
562template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000563template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000564template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000565template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000566template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000567template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
568template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000569template <class ELFT> typename Out<ELFT>::Elf_Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000570template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
571template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000572
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000573} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000574} // namespace lld
575
576#endif // LLD_ELF_OUTPUT_SECTIONS_H