blob: 2c1f7351e1fbe880b829af289216b6e0b9078a10 [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"
Simon Atanasyand2980d32016-03-29 14:07:22 +000016#include "llvm/ADT/SmallPtrSet.h"
Rui Ueyamaa0752a52016-03-13 20:28:29 +000017#include "llvm/MC/StringTableBuilder.h"
18#include "llvm/Object/ELF.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000019
20namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000021namespace elf {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000022
23class SymbolBody;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000024template <class ELFT> class SymbolTable;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000025template <class ELFT> class SymbolTableSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000026template <class ELFT> class StringTableSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000027template <class ELFT> class EHInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000028template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000029template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000030template <class ELFT> class MergeInputSection;
Simon Atanasyan1d7df402015-12-20 10:57:34 +000031template <class ELFT> class MipsReginfoInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000032template <class ELFT> class OutputSection;
33template <class ELFT> class ObjectFile;
34template <class ELFT> class DefinedRegular;
35
Rafael Espindola5805c4f2015-09-21 21:38:08 +000036template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000037static inline typename ELFT::uint getAddend(const typename ELFT::Rel &Rel) {
Rafael Espindola932efcf2015-10-19 20:24:44 +000038 return 0;
39}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000040
41template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000042static inline typename ELFT::uint getAddend(const typename ELFT::Rela &Rel) {
Rafael Espindola932efcf2015-10-19 20:24:44 +000043 return Rel.r_addend;
44}
45
George Rimar12737b72016-02-25 08:40:26 +000046bool isValidCIdentifier(StringRef S);
47
Rafael Espindola71675852015-09-22 00:16:19 +000048// This represents a section in an output file.
49// Different sub classes represent different types of sections. Some contain
50// input sections, others are created by the linker.
51// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000052// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000053template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000054public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +000055 typedef typename ELFT::uint uintX_t;
56 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000057
George Rimar9bec24a2016-02-18 14:20:08 +000058 OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000059 void setVA(uintX_t VA) { Header.sh_addr = VA; }
60 uintX_t getVA() const { return Header.sh_addr; }
61 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000062 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000063 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000064 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000065
Rui Ueyama40845e62015-12-26 05:51:07 +000066 virtual void addSection(InputSectionBase<ELFT> *C) {}
67
Rui Ueyama2317d0d2015-10-15 20:55:22 +000068 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000069
70 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000071 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000072 void setSize(uintX_t Val) { Header.sh_size = Val; }
73 uintX_t getFlags() { return Header.sh_flags; }
74 uintX_t getFileOff() { return Header.sh_offset; }
75 uintX_t getAlign() {
76 // The ELF spec states that a value of 0 means the section has no alignment
77 // constraits.
78 return std::max<uintX_t>(Header.sh_addralign, 1);
79 }
80 uint32_t getType() { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +000081 void updateAlign(uintX_t Align) {
82 if (Align > Header.sh_addralign)
83 Header.sh_addralign = Align;
84 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000085
Rui Ueyama47091902016-03-30 19:41:51 +000086 // If true, this section will be page aligned on disk.
87 // Typically the first section of each PT_LOAD segment has this flag.
88 bool PageAlign = false;
89
Rafael Espindola5805c4f2015-09-21 21:38:08 +000090 virtual void finalize() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000091 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000092 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000093
94protected:
95 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000096 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000097};
98
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000099template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
100 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000101 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000102
103public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000104 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000105 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000106 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000107 void addEntry(SymbolBody &Sym);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000108 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000109 bool addTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000110 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000111 uintX_t getMipsLocalFullAddr(const SymbolBody &B);
112 uintX_t getMipsLocalPageAddr(uintX_t Addr);
George Rimar90cd0a82015-12-01 19:20:26 +0000113 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000114 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000115
Igor Kudrin304860a2015-11-12 04:39:49 +0000116 // Returns the symbol which corresponds to the first entry of the global part
117 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
118 // table properties.
119 // Returns nullptr if the global part is empty.
120 const SymbolBody *getMipsFirstGlobalEntry() const;
121
122 // Returns the number of entries in the local part of GOT including
123 // the number of reserved entries. This method is MIPS-specific.
124 unsigned getMipsLocalEntriesNum() const;
125
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000126 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000127
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000128private:
129 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000130 uint32_t TlsIndexOff = -1;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000131 uint32_t MipsLocalEntries = 0;
Simon Atanasyand2980d32016-03-29 14:07:22 +0000132 // Output sections referenced by MIPS GOT relocations.
133 llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000134 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
135
136 uintX_t getMipsLocalEntryAddr(uintX_t EntryValue);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000137};
138
George Rimar648a2c32015-10-20 08:54:27 +0000139template <class ELFT>
140class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000141 typedef typename ELFT::uint uintX_t;
George Rimar648a2c32015-10-20 08:54:27 +0000142
143public:
144 GotPltSection();
145 void finalize() override;
146 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000147 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000148 bool empty() const;
George Rimar648a2c32015-10-20 08:54:27 +0000149
150private:
151 std::vector<const SymbolBody *> Entries;
152};
153
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000154template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
155 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000156 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000157
158public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000159 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000160 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000161 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000162 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000163 bool empty() const { return Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000164
165private:
George Rimar77b77792015-11-25 22:15:01 +0000166 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000167};
168
169template <class ELFT> struct DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000170 typedef typename ELFT::uint uintX_t;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000171 uint32_t Type;
172
173 // Where the relocation is.
174 enum OffsetKind {
175 Off_Got, // The got entry of Sym.
176 Off_GotPlt, // The got.plt entry of Sym.
177 Off_Bss, // The bss entry of Sym (copy reloc).
178 Off_Sec, // The final position of the given input section and offset.
179 Off_LTlsIndex, // The local tls index.
180 Off_GTlsIndex, // The global tls index of Sym.
181 Off_GTlsOffset // The global tls offset of Sym.
182 } OKind;
183
184 SymbolBody *Sym = nullptr;
185 InputSectionBase<ELFT> *OffsetSec = nullptr;
186 uintX_t OffsetInSec = 0;
187 bool UseSymVA = false;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000188 uintX_t Addend = 0;
189
190 DynamicReloc(uint32_t Type, OffsetKind OKind, SymbolBody *Sym)
191 : Type(Type), OKind(OKind), Sym(Sym) {}
192
193 DynamicReloc(uint32_t Type, OffsetKind OKind, bool UseSymVA, SymbolBody *Sym)
194 : Type(Type), OKind(OKind), Sym(Sym), UseSymVA(UseSymVA) {}
195
196 DynamicReloc(uint32_t Type, InputSectionBase<ELFT> *OffsetSec,
197 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
198 uintX_t Addend)
199 : Type(Type), OKind(Off_Sec), Sym(Sym), OffsetSec(OffsetSec),
200 OffsetInSec(OffsetInSec), UseSymVA(UseSymVA), Addend(Addend) {}
201
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000202 uintX_t getOffset() const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000203};
204
205template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000206class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000207public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000208 typedef typename ELFT::Shdr Elf_Shdr;
209 typedef typename ELFT::Sym Elf_Sym;
210 typedef typename ELFT::SymRange Elf_Sym_Range;
211 typedef typename ELFT::uint uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000212 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000213 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000214
Rui Ueyama0db335f2015-10-07 16:58:54 +0000215 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000216 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000217 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000218 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000219 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000220
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000221 ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
Rafael Espindolae2c24612016-01-29 01:24:25 +0000222 return Symbols;
223 }
224
225 unsigned NumLocals = 0;
226 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000227
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000228private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000229 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000230 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000231
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000232 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000233 static uint8_t getSymbolBinding(SymbolBody *Body);
234
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000235 SymbolTable<ELFT> &Table;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000236
237 // A vector of symbols and their string table offsets.
238 std::vector<std::pair<SymbolBody *, size_t>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000239};
240
241template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000242class RelocationSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000243 typedef typename ELFT::Rel Elf_Rel;
244 typedef typename ELFT::Rela Elf_Rela;
245 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000246
247public:
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000248 RelocationSection(StringRef Name);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000249 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000250 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000251 void finalize() override;
252 void writeTo(uint8_t *Buf) override;
253 bool hasRelocs() const { return !Relocs.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000254
George Rimara07ff662015-12-21 10:12:06 +0000255 bool Static = false;
256
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000257private:
258 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000259};
260
261template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000262class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000263public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000264 typedef typename ELFT::Shdr Elf_Shdr;
265 typedef typename ELFT::Sym Elf_Sym;
266 typedef typename ELFT::Rel Elf_Rel;
267 typedef typename ELFT::Rela Elf_Rela;
268 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000269 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000270 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000271 void sortInitFini();
272 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000273 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000274 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000275
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000276private:
Rui Ueyama5af83682016-02-11 23:41:38 +0000277 void reassignOffsets();
Rafael Espindola71675852015-09-22 00:16:19 +0000278 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000279};
280
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000281template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000282class MergeOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000283 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000284
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000285 bool shouldTailMerge() const;
286
Rafael Espindolac159c962015-10-19 21:00:02 +0000287public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000288 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
289 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000290 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000291 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000292 unsigned getOffset(StringRef Val);
293 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000294
295private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000296 llvm::StringTableBuilder Builder;
Rafael Espindolac159c962015-10-19 21:00:02 +0000297};
298
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000299// FDE or CIE
300template <class ELFT> struct EHRegion {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000301 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000302 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
303 StringRef data() const;
304 EHInputSection<ELFT> *S;
305 unsigned Index;
306};
307
308template <class ELFT> struct Cie : public EHRegion<ELFT> {
309 Cie(EHInputSection<ELFT> *S, unsigned Index);
310 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000311 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000312};
313
314template <class ELFT>
315class EHOutputSection final : public OutputSectionBase<ELFT> {
316public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000317 typedef typename ELFT::uint uintX_t;
318 typedef typename ELFT::Shdr Elf_Shdr;
319 typedef typename ELFT::Rel Elf_Rel;
320 typedef typename ELFT::Rela Elf_Rela;
George Rimar9bec24a2016-02-18 14:20:08 +0000321 EHOutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000322 void writeTo(uint8_t *Buf) override;
323
Rui Ueyamafc467e72016-03-13 05:06:50 +0000324 template <class RelTy>
325 void addSectionAux(EHInputSection<ELFT> *S,
326 llvm::iterator_range<const RelTy *> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000327
Rui Ueyama40845e62015-12-26 05:51:07 +0000328 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000329
330private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000331 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000332
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000333 std::vector<EHInputSection<ELFT> *> Sections;
334 std::vector<Cie<ELFT>> Cies;
335
336 // Maps CIE content + personality to a index in Cies.
Rafael Espindola156ed8d2016-02-10 13:19:32 +0000337 llvm::DenseMap<std::pair<StringRef, SymbolBody *>, unsigned> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000338};
339
Rafael Espindolac159c962015-10-19 21:00:02 +0000340template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000341class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000342public:
343 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000344 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000345};
346
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000347template <class ELFT>
348class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000349public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000350 typedef typename ELFT::uint uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000351 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000352 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000353 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000354 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000355 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000356 bool isDynamic() const { return Dynamic; }
357
358private:
359 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000360 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000361 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000362 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000363};
364
365template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000366class HashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000367 typedef typename ELFT::Word Elf_Word;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000368
369public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000370 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000371 void finalize() override;
372 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000373};
374
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000375// Outputs GNU Hash section. For detailed explanation see:
376// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
377template <class ELFT>
378class GnuHashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000379 typedef typename ELFT::Off Elf_Off;
380 typedef typename ELFT::Word Elf_Word;
381 typedef typename ELFT::uint uintX_t;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000382
383public:
384 GnuHashTableSection();
385 void finalize() override;
386 void writeTo(uint8_t *Buf) override;
387
Igor Kudrinf1d60292015-10-28 07:05:56 +0000388 // Adds symbols to the hash table.
389 // Sorts the input to satisfy GNU hash section requirements.
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000390 void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000391
392private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000393 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000394 static unsigned calcMaskWords(unsigned NumHashed);
395
396 void writeHeader(uint8_t *&Buf);
397 void writeBloomFilter(uint8_t *&Buf);
398 void writeHashTable(uint8_t *Buf);
399
Rui Ueyama861c7312016-02-17 05:40:03 +0000400 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000401 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000402 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000403 uint32_t Hash;
404 };
405
Rui Ueyama861c7312016-02-17 05:40:03 +0000406 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000407
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000408 unsigned MaskWords;
409 unsigned NBuckets;
410 unsigned Shift2;
411};
412
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000413template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000414class DynamicSection final : public OutputSectionBase<ELFT> {
415 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000416 typedef typename ELFT::Dyn Elf_Dyn;
417 typedef typename ELFT::Rel Elf_Rel;
418 typedef typename ELFT::Rela Elf_Rela;
419 typedef typename ELFT::Shdr Elf_Shdr;
420 typedef typename ELFT::Sym Elf_Sym;
421 typedef typename ELFT::uint uintX_t;
Rafael Espindolade069362016-01-25 21:32:04 +0000422
Rui Ueyama909cc682016-02-02 03:11:27 +0000423 // The .dynamic section contains information for the dynamic linker.
424 // The section consists of fixed size entries, which consist of
425 // type and value fields. Value are one of plain integers, symbol
426 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000427 struct Entry {
428 int32_t Tag;
429 union {
430 OutputSectionBase<ELFT> *OutSec;
431 uint64_t Val;
432 const SymbolBody *Sym;
433 };
434 enum KindT { SecAddr, SymAddr, PlainInt } Kind;
435 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
436 : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
437 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
438 Entry(int32_t Tag, const SymbolBody *Sym)
439 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
440 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000441
442 // finalize() fills this vector with the section contents. finalize()
443 // cannot directly create final section contents because when the
444 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000445 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000446
447public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000448 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000449 void finalize() override;
450 void writeTo(uint8_t *Buf) override;
451
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000452 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
453 OutputSectionBase<ELFT> *InitArraySec = nullptr;
454 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000455
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000456private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000457 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000458};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000459
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000460template <class ELFT>
461class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
462 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
463
464public:
465 MipsReginfoOutputSection();
466 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000467 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000468
469private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000470 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000471};
472
George Rimarf6bc65a2016-01-15 13:34:52 +0000473// --eh-frame-hdr option tells linker to construct a header for all the
474// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
475// and also to a PT_GNU_EH_FRAME segment.
476// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
477// calling dl_iterate_phdr.
478// This section contains a lookup table for quick binary search of FDEs.
479// Detailed info about internals can be found in Ian Lance Taylor's blog:
480// http://www.airs.com/blog/archives/460 (".eh_frame")
481// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
482template <class ELFT>
483class EhFrameHeader final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000484 typedef typename ELFT::uint uintX_t;
George Rimarf6bc65a2016-01-15 13:34:52 +0000485
486public:
487 EhFrameHeader();
488 void writeTo(uint8_t *Buf) override;
489
490 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
491 void assignEhFrame(EHOutputSection<ELFT> *Sec);
492 void reserveFde();
493
494 bool Live = false;
495
496private:
497 struct FdeData {
498 uint8_t Enc;
499 size_t Off;
500 uint8_t *PCRel;
501 };
502
503 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
504
505 EHOutputSection<ELFT> *Sec = nullptr;
506 std::vector<FdeData> FdeList;
507};
508
Rui Ueyama634ddf02016-03-11 20:51:53 +0000509template <class ELFT>
510class BuildIdSection final : public OutputSectionBase<ELFT> {
511public:
512 BuildIdSection();
513 void writeTo(uint8_t *Buf) override;
514 void update(ArrayRef<uint8_t> Buf);
515 void writeBuildId();
516
517private:
518 uint64_t Hash = 0xcbf29ce484222325; // FNV1 hash basis
519 uint8_t *HashBuf;
520};
521
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000522// All output sections that are hadnled by the linker specially are
523// globally accessible. Writer initializes them, so don't use them
524// until Writer is initialized.
525template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000526 typedef typename ELFT::uint uintX_t;
527 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000528 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000529 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000530 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000531 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000532 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000533 static GotSection<ELFT> *Got;
534 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000535 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000536 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000537 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000538 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000539 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000540 static PltSection<ELFT> *Plt;
541 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000542 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000543 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000544 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000545 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000546 static SymbolTableSection<ELFT> *DynSymTab;
547 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000548 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000549 static OutputSectionBase<ELFT> *ElfHeader;
550 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000551};
Rui Ueyamad888d102015-10-09 19:34:55 +0000552
Rui Ueyama634ddf02016-03-11 20:51:53 +0000553template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000554template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000555template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000556template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000557template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000558template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
559template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000560template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000561template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000562template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000563template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000564template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000565template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
566template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000567template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000568template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000569template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000570template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000571template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
572template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000573template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000574template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
575template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000576
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000577} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000578} // namespace lld
579
580#endif // LLD_ELF_OUTPUT_SECTIONS_H