blob: 95b679013a45e4254c0bc82e8622e46ff658c77e [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 Espindola56004c52016-04-07 14:22:09 +000091 virtual void
92 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000093 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000094 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000095
96protected:
97 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000098 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000099};
100
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000101template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
102 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000103 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000104
105public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000106 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000107 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000108 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000109 void addEntry(SymbolBody &Sym);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000110 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000111 bool addTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000112 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000113 uintX_t getMipsLocalFullAddr(const SymbolBody &B);
114 uintX_t getMipsLocalPageAddr(uintX_t Addr);
George Rimar90cd0a82015-12-01 19:20:26 +0000115 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000116 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000117
Igor Kudrin304860a2015-11-12 04:39:49 +0000118 // Returns the symbol which corresponds to the first entry of the global part
119 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
120 // table properties.
121 // Returns nullptr if the global part is empty.
122 const SymbolBody *getMipsFirstGlobalEntry() const;
123
124 // Returns the number of entries in the local part of GOT including
125 // the number of reserved entries. This method is MIPS-specific.
126 unsigned getMipsLocalEntriesNum() const;
127
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000128 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000129
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000130private:
131 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000132 uint32_t TlsIndexOff = -1;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000133 uint32_t MipsLocalEntries = 0;
Simon Atanasyand2980d32016-03-29 14:07:22 +0000134 // Output sections referenced by MIPS GOT relocations.
135 llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000136 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
137
138 uintX_t getMipsLocalEntryAddr(uintX_t EntryValue);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000139};
140
George Rimar648a2c32015-10-20 08:54:27 +0000141template <class ELFT>
142class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000143 typedef typename ELFT::uint uintX_t;
George Rimar648a2c32015-10-20 08:54:27 +0000144
145public:
146 GotPltSection();
147 void finalize() override;
148 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000149 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000150 bool empty() const;
George Rimar648a2c32015-10-20 08:54:27 +0000151
152private:
153 std::vector<const SymbolBody *> Entries;
154};
155
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000156template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
157 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000158 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000159
160public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000161 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000162 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000163 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000164 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000165 bool empty() const { return Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000166
167private:
George Rimar77b77792015-11-25 22:15:01 +0000168 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000169};
170
171template <class ELFT> struct DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000172 typedef typename ELFT::uint uintX_t;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000173 uint32_t Type;
174
175 // Where the relocation is.
176 enum OffsetKind {
177 Off_Got, // The got entry of Sym.
178 Off_GotPlt, // The got.plt entry of Sym.
179 Off_Bss, // The bss entry of Sym (copy reloc).
180 Off_Sec, // The final position of the given input section and offset.
181 Off_LTlsIndex, // The local tls index.
182 Off_GTlsIndex, // The global tls index of Sym.
183 Off_GTlsOffset // The global tls offset of Sym.
184 } OKind;
185
186 SymbolBody *Sym = nullptr;
Rafael Espindola56004c52016-04-07 14:22:09 +0000187 OutputSectionBase<ELFT> *OffsetSec = nullptr;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000188 uintX_t OffsetInSec = 0;
189 bool UseSymVA = false;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000190 uintX_t Addend = 0;
191
192 DynamicReloc(uint32_t Type, OffsetKind OKind, SymbolBody *Sym)
193 : Type(Type), OKind(OKind), Sym(Sym) {}
194
195 DynamicReloc(uint32_t Type, OffsetKind OKind, bool UseSymVA, SymbolBody *Sym)
196 : Type(Type), OKind(OKind), Sym(Sym), UseSymVA(UseSymVA) {}
197
Rafael Espindola56004c52016-04-07 14:22:09 +0000198 DynamicReloc(uint32_t Type, OutputSectionBase<ELFT> *OffsetSec,
Rafael Espindolade9857e2016-02-04 21:33:05 +0000199 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
200 uintX_t Addend)
201 : Type(Type), OKind(Off_Sec), Sym(Sym), OffsetSec(OffsetSec),
202 OffsetInSec(OffsetInSec), UseSymVA(UseSymVA), Addend(Addend) {}
203
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000204 uintX_t getOffset() const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000205};
206
207template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000208class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000209public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000210 typedef typename ELFT::Shdr Elf_Shdr;
211 typedef typename ELFT::Sym Elf_Sym;
212 typedef typename ELFT::SymRange Elf_Sym_Range;
213 typedef typename ELFT::uint uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000214 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000215 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000216
Rui Ueyama0db335f2015-10-07 16:58:54 +0000217 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000218 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000219 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000220 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000221 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000222
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000223 ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
Rafael Espindolae2c24612016-01-29 01:24:25 +0000224 return Symbols;
225 }
226
227 unsigned NumLocals = 0;
228 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000229
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000230private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000231 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000232 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000233
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000234 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000235 static uint8_t getSymbolBinding(SymbolBody *Body);
236
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000237 SymbolTable<ELFT> &Table;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000238
239 // A vector of symbols and their string table offsets.
240 std::vector<std::pair<SymbolBody *, size_t>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000241};
242
243template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000244class RelocationSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000245 typedef typename ELFT::Rel Elf_Rel;
246 typedef typename ELFT::Rela Elf_Rela;
247 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000248
249public:
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000250 RelocationSection(StringRef Name);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000251 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000252 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000253 void finalize() override;
254 void writeTo(uint8_t *Buf) override;
255 bool hasRelocs() const { return !Relocs.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000256
George Rimara07ff662015-12-21 10:12:06 +0000257 bool Static = false;
258
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000259private:
260 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000261};
262
263template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000264class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000265public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000266 typedef typename ELFT::Shdr Elf_Shdr;
267 typedef typename ELFT::Sym Elf_Sym;
268 typedef typename ELFT::Rel Elf_Rel;
269 typedef typename ELFT::Rela Elf_Rela;
270 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000271 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000272 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000273 void sortInitFini();
274 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000275 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000276 void finalize() override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000277 void
278 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) override;
Rafael Espindola71675852015-09-22 00:16:19 +0000279 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000280};
281
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000282template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000283class MergeOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000284 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000285
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000286 bool shouldTailMerge() const;
287
Rafael Espindolac159c962015-10-19 21:00:02 +0000288public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000289 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
290 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000291 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000292 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000293 unsigned getOffset(StringRef Val);
294 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000295
296private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000297 llvm::StringTableBuilder Builder;
Rafael Espindolac159c962015-10-19 21:00:02 +0000298};
299
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000300// FDE or CIE
301template <class ELFT> struct EHRegion {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000302 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000303 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
304 StringRef data() const;
305 EHInputSection<ELFT> *S;
306 unsigned Index;
307};
308
309template <class ELFT> struct Cie : public EHRegion<ELFT> {
310 Cie(EHInputSection<ELFT> *S, unsigned Index);
311 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000312 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000313};
314
315template <class ELFT>
316class EHOutputSection final : public OutputSectionBase<ELFT> {
317public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000318 typedef typename ELFT::uint uintX_t;
319 typedef typename ELFT::Shdr Elf_Shdr;
320 typedef typename ELFT::Rel Elf_Rel;
321 typedef typename ELFT::Rela Elf_Rela;
George Rimar9bec24a2016-02-18 14:20:08 +0000322 EHOutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000323 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000324 void finalize() override;
325 void
326 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000327
Rui Ueyamafc467e72016-03-13 05:06:50 +0000328 template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000329 void addSectionAux(EHInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000330
Rui Ueyama40845e62015-12-26 05:51:07 +0000331 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000332
333private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000334 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000335
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000336 std::vector<EHInputSection<ELFT> *> Sections;
337 std::vector<Cie<ELFT>> Cies;
338
339 // Maps CIE content + personality to a index in Cies.
Rafael Espindola156ed8d2016-02-10 13:19:32 +0000340 llvm::DenseMap<std::pair<StringRef, SymbolBody *>, unsigned> CieMap;
Rafael Espindola56004c52016-04-07 14:22:09 +0000341 bool Finalized = false;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000342};
343
Rafael Espindolac159c962015-10-19 21:00:02 +0000344template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000345class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000346public:
347 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000348 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000349};
350
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000351template <class ELFT>
352class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000353public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000354 typedef typename ELFT::uint uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000355 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000356 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000357 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000358 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000359 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000360 bool isDynamic() const { return Dynamic; }
361
362private:
363 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000364 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000365 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000366 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000367};
368
369template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000370class HashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000371 typedef typename ELFT::Word Elf_Word;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000372
373public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000374 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000375 void finalize() override;
376 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000377};
378
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000379// Outputs GNU Hash section. For detailed explanation see:
380// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
381template <class ELFT>
382class GnuHashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000383 typedef typename ELFT::Off Elf_Off;
384 typedef typename ELFT::Word Elf_Word;
385 typedef typename ELFT::uint uintX_t;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000386
387public:
388 GnuHashTableSection();
389 void finalize() override;
390 void writeTo(uint8_t *Buf) override;
391
Igor Kudrinf1d60292015-10-28 07:05:56 +0000392 // Adds symbols to the hash table.
393 // Sorts the input to satisfy GNU hash section requirements.
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000394 void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000395
396private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000397 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000398 static unsigned calcMaskWords(unsigned NumHashed);
399
400 void writeHeader(uint8_t *&Buf);
401 void writeBloomFilter(uint8_t *&Buf);
402 void writeHashTable(uint8_t *Buf);
403
Rui Ueyama861c7312016-02-17 05:40:03 +0000404 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000405 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000406 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000407 uint32_t Hash;
408 };
409
Rui Ueyama861c7312016-02-17 05:40:03 +0000410 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000411
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000412 unsigned MaskWords;
413 unsigned NBuckets;
414 unsigned Shift2;
415};
416
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000417template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000418class DynamicSection final : public OutputSectionBase<ELFT> {
419 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000420 typedef typename ELFT::Dyn Elf_Dyn;
421 typedef typename ELFT::Rel Elf_Rel;
422 typedef typename ELFT::Rela Elf_Rela;
423 typedef typename ELFT::Shdr Elf_Shdr;
424 typedef typename ELFT::Sym Elf_Sym;
425 typedef typename ELFT::uint uintX_t;
Rafael Espindolade069362016-01-25 21:32:04 +0000426
Rui Ueyama909cc682016-02-02 03:11:27 +0000427 // The .dynamic section contains information for the dynamic linker.
428 // The section consists of fixed size entries, which consist of
429 // type and value fields. Value are one of plain integers, symbol
430 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000431 struct Entry {
432 int32_t Tag;
433 union {
434 OutputSectionBase<ELFT> *OutSec;
435 uint64_t Val;
436 const SymbolBody *Sym;
437 };
438 enum KindT { SecAddr, SymAddr, PlainInt } Kind;
439 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
440 : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
441 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
442 Entry(int32_t Tag, const SymbolBody *Sym)
443 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
444 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000445
446 // finalize() fills this vector with the section contents. finalize()
447 // cannot directly create final section contents because when the
448 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000449 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000450
451public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000452 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000453 void finalize() override;
454 void writeTo(uint8_t *Buf) override;
455
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000456 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
457 OutputSectionBase<ELFT> *InitArraySec = nullptr;
458 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000459
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000460private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000461 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000462};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000463
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000464template <class ELFT>
465class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
466 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
467
468public:
469 MipsReginfoOutputSection();
470 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000471 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000472
473private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000474 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000475};
476
George Rimarf6bc65a2016-01-15 13:34:52 +0000477// --eh-frame-hdr option tells linker to construct a header for all the
478// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
479// and also to a PT_GNU_EH_FRAME segment.
480// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
481// calling dl_iterate_phdr.
482// This section contains a lookup table for quick binary search of FDEs.
483// Detailed info about internals can be found in Ian Lance Taylor's blog:
484// http://www.airs.com/blog/archives/460 (".eh_frame")
485// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
486template <class ELFT>
487class EhFrameHeader final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000488 typedef typename ELFT::uint uintX_t;
George Rimarf6bc65a2016-01-15 13:34:52 +0000489
490public:
491 EhFrameHeader();
492 void writeTo(uint8_t *Buf) override;
493
494 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
495 void assignEhFrame(EHOutputSection<ELFT> *Sec);
496 void reserveFde();
497
498 bool Live = false;
499
Rafael Espindola56004c52016-04-07 14:22:09 +0000500 EHOutputSection<ELFT> *Sec = nullptr;
501
George Rimarf6bc65a2016-01-15 13:34:52 +0000502private:
503 struct FdeData {
504 uint8_t Enc;
505 size_t Off;
506 uint8_t *PCRel;
507 };
508
509 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
510
George Rimarf6bc65a2016-01-15 13:34:52 +0000511 std::vector<FdeData> FdeList;
512};
513
Rui Ueyama634ddf02016-03-11 20:51:53 +0000514template <class ELFT>
515class BuildIdSection final : public OutputSectionBase<ELFT> {
516public:
517 BuildIdSection();
518 void writeTo(uint8_t *Buf) override;
519 void update(ArrayRef<uint8_t> Buf);
520 void writeBuildId();
521
522private:
523 uint64_t Hash = 0xcbf29ce484222325; // FNV1 hash basis
524 uint8_t *HashBuf;
525};
526
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000527// All output sections that are hadnled by the linker specially are
528// globally accessible. Writer initializes them, so don't use them
529// until Writer is initialized.
530template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000531 typedef typename ELFT::uint uintX_t;
532 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000533 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000534 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000535 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000536 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000537 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000538 static GotSection<ELFT> *Got;
539 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000540 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000541 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000542 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000543 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000544 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000545 static PltSection<ELFT> *Plt;
546 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000547 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000548 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000549 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000550 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000551 static SymbolTableSection<ELFT> *DynSymTab;
552 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000553 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000554 static OutputSectionBase<ELFT> *ElfHeader;
555 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000556};
Rui Ueyamad888d102015-10-09 19:34:55 +0000557
Rui Ueyama634ddf02016-03-11 20:51:53 +0000558template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000559template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000560template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000561template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000562template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000563template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
564template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000565template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000566template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000567template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000568template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000569template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000570template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
571template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000572template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000573template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000574template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000575template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000576template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
577template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000578template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000579template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
580template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000581
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000582} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000583} // namespace lld
584
585#endif // LLD_ELF_OUTPUT_SECTIONS_H