blob: 13259bde088920022df98cb496ba5752f376ec04 [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>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000036static inline typename ELFT::uint getAddend(const typename ELFT::Rel &Rel) {
Rafael Espindola932efcf2015-10-19 20:24:44 +000037 return 0;
38}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000039
40template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000041static inline typename ELFT::uint getAddend(const typename ELFT::Rela &Rel) {
Rafael Espindola932efcf2015-10-19 20:24:44 +000042 return Rel.r_addend;
43}
44
George Rimar12737b72016-02-25 08:40:26 +000045bool isValidCIdentifier(StringRef S);
46
Rafael Espindola71675852015-09-22 00:16:19 +000047// This represents a section in an output file.
48// Different sub classes represent different types of sections. Some contain
49// input sections, others are created by the linker.
50// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000051// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000052template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000053public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +000054 typedef typename ELFT::uint uintX_t;
55 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000056
George Rimar9bec24a2016-02-18 14:20:08 +000057 OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000058 void setVA(uintX_t VA) { Header.sh_addr = VA; }
59 uintX_t getVA() const { return Header.sh_addr; }
60 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000061 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000062 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000063 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000064
Rui Ueyama40845e62015-12-26 05:51:07 +000065 virtual void addSection(InputSectionBase<ELFT> *C) {}
66
Rui Ueyama2317d0d2015-10-15 20:55:22 +000067 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000068
69 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000070 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000071 void setSize(uintX_t Val) { Header.sh_size = Val; }
72 uintX_t getFlags() { return Header.sh_flags; }
73 uintX_t getFileOff() { return Header.sh_offset; }
74 uintX_t getAlign() {
75 // The ELF spec states that a value of 0 means the section has no alignment
76 // constraits.
77 return std::max<uintX_t>(Header.sh_addralign, 1);
78 }
79 uint32_t getType() { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +000080 void updateAlign(uintX_t Align) {
81 if (Align > Header.sh_addralign)
82 Header.sh_addralign = Align;
83 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000084
Rafael Espindola5805c4f2015-09-21 21:38:08 +000085 virtual void finalize() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000086 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000087 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000088
89protected:
90 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000091 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000092};
93
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000094template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
95 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000096 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000097
98public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +000099 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000100 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000101 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000102 void addEntry(SymbolBody &Sym);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000103 void addMipsLocalEntry();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000104 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000105 bool addTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000106 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000107 uintX_t getMipsLocalFullAddr(const SymbolBody &B);
108 uintX_t getMipsLocalPageAddr(uintX_t Addr);
George Rimar90cd0a82015-12-01 19:20:26 +0000109 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000110 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000111
Igor Kudrin304860a2015-11-12 04:39:49 +0000112 // Returns the symbol which corresponds to the first entry of the global part
113 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
114 // table properties.
115 // Returns nullptr if the global part is empty.
116 const SymbolBody *getMipsFirstGlobalEntry() const;
117
118 // Returns the number of entries in the local part of GOT including
119 // the number of reserved entries. This method is MIPS-specific.
120 unsigned getMipsLocalEntriesNum() const;
121
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000122 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000123
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000124private:
125 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000126 uint32_t TlsIndexOff = -1;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000127 uint32_t MipsLocalEntries = 0;
128 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
129
130 uintX_t getMipsLocalEntryAddr(uintX_t EntryValue);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000131};
132
George Rimar648a2c32015-10-20 08:54:27 +0000133template <class ELFT>
134class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000135 typedef typename ELFT::uint uintX_t;
George Rimar648a2c32015-10-20 08:54:27 +0000136
137public:
138 GotPltSection();
139 void finalize() override;
140 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000141 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000142 bool empty() const;
George Rimar648a2c32015-10-20 08:54:27 +0000143
144private:
145 std::vector<const SymbolBody *> Entries;
146};
147
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000148template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
149 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000150 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000151
152public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000153 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000154 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000155 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000156 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000157 bool empty() const { return Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000158
159private:
George Rimar77b77792015-11-25 22:15:01 +0000160 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000161};
162
163template <class ELFT> struct DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000164 typedef typename ELFT::uint uintX_t;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000165 uint32_t Type;
166
167 // Where the relocation is.
168 enum OffsetKind {
169 Off_Got, // The got entry of Sym.
170 Off_GotPlt, // The got.plt entry of Sym.
171 Off_Bss, // The bss entry of Sym (copy reloc).
172 Off_Sec, // The final position of the given input section and offset.
173 Off_LTlsIndex, // The local tls index.
174 Off_GTlsIndex, // The global tls index of Sym.
175 Off_GTlsOffset // The global tls offset of Sym.
176 } OKind;
177
178 SymbolBody *Sym = nullptr;
179 InputSectionBase<ELFT> *OffsetSec = nullptr;
180 uintX_t OffsetInSec = 0;
181 bool UseSymVA = false;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000182 uintX_t Addend = 0;
183
184 DynamicReloc(uint32_t Type, OffsetKind OKind, SymbolBody *Sym)
185 : Type(Type), OKind(OKind), Sym(Sym) {}
186
187 DynamicReloc(uint32_t Type, OffsetKind OKind, bool UseSymVA, SymbolBody *Sym)
188 : Type(Type), OKind(OKind), Sym(Sym), UseSymVA(UseSymVA) {}
189
190 DynamicReloc(uint32_t Type, InputSectionBase<ELFT> *OffsetSec,
191 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
192 uintX_t Addend)
193 : Type(Type), OKind(Off_Sec), Sym(Sym), OffsetSec(OffsetSec),
194 OffsetInSec(OffsetInSec), UseSymVA(UseSymVA), Addend(Addend) {}
195
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000196 uintX_t getOffset() const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000197};
198
199template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000200class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000201public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000202 typedef typename ELFT::Shdr Elf_Shdr;
203 typedef typename ELFT::Sym Elf_Sym;
204 typedef typename ELFT::SymRange Elf_Sym_Range;
205 typedef typename ELFT::uint uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000206 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000207 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000208
Rui Ueyama0db335f2015-10-07 16:58:54 +0000209 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000210 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000211 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000212 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000213 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000214
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000215 ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
Rafael Espindolae2c24612016-01-29 01:24:25 +0000216 return Symbols;
217 }
218
219 unsigned NumLocals = 0;
220 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000221
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000222private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000223 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000224 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000225
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000226 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000227 static uint8_t getSymbolBinding(SymbolBody *Body);
228
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000229 SymbolTable<ELFT> &Table;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000230
231 // A vector of symbols and their string table offsets.
232 std::vector<std::pair<SymbolBody *, size_t>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000233};
234
235template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000236class RelocationSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000237 typedef typename ELFT::Rel Elf_Rel;
238 typedef typename ELFT::Rela Elf_Rela;
239 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000240
241public:
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000242 RelocationSection(StringRef Name);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000243 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000244 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000245 void finalize() override;
246 void writeTo(uint8_t *Buf) override;
247 bool hasRelocs() const { return !Relocs.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000248
George Rimara07ff662015-12-21 10:12:06 +0000249 bool Static = false;
250
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000251private:
252 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000253};
254
255template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000256class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000257public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000258 typedef typename ELFT::Shdr Elf_Shdr;
259 typedef typename ELFT::Sym Elf_Sym;
260 typedef typename ELFT::Rel Elf_Rel;
261 typedef typename ELFT::Rela Elf_Rela;
262 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000263 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000264 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000265 void sortInitFini();
266 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000267 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000268 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000269
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000270private:
Rui Ueyama5af83682016-02-11 23:41:38 +0000271 void reassignOffsets();
Rafael Espindola71675852015-09-22 00:16:19 +0000272 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000273};
274
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000275template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000276class MergeOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000277 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000278
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000279 bool shouldTailMerge() const;
280
Rafael Espindolac159c962015-10-19 21:00:02 +0000281public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000282 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
283 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000284 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000285 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000286 unsigned getOffset(StringRef Val);
287 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000288
289private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000290 llvm::StringTableBuilder Builder;
Rafael Espindolac159c962015-10-19 21:00:02 +0000291};
292
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000293// FDE or CIE
294template <class ELFT> struct EHRegion {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000295 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000296 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
297 StringRef data() const;
298 EHInputSection<ELFT> *S;
299 unsigned Index;
300};
301
302template <class ELFT> struct Cie : public EHRegion<ELFT> {
303 Cie(EHInputSection<ELFT> *S, unsigned Index);
304 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000305 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000306};
307
308template <class ELFT>
309class EHOutputSection final : public OutputSectionBase<ELFT> {
310public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000311 typedef typename ELFT::uint uintX_t;
312 typedef typename ELFT::Shdr Elf_Shdr;
313 typedef typename ELFT::Rel Elf_Rel;
314 typedef typename ELFT::Rela Elf_Rela;
George Rimar9bec24a2016-02-18 14:20:08 +0000315 EHOutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000316 void writeTo(uint8_t *Buf) override;
317
Rui Ueyamafc467e72016-03-13 05:06:50 +0000318 template <class RelTy>
319 void addSectionAux(EHInputSection<ELFT> *S,
320 llvm::iterator_range<const RelTy *> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000321
Rui Ueyama40845e62015-12-26 05:51:07 +0000322 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000323
324private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000325 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000326
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000327 std::vector<EHInputSection<ELFT> *> Sections;
328 std::vector<Cie<ELFT>> Cies;
329
330 // Maps CIE content + personality to a index in Cies.
Rafael Espindola156ed8d2016-02-10 13:19:32 +0000331 llvm::DenseMap<std::pair<StringRef, SymbolBody *>, unsigned> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000332};
333
Rafael Espindolac159c962015-10-19 21:00:02 +0000334template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000335class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000336public:
337 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000338 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000339};
340
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000341template <class ELFT>
342class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000343public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000344 typedef typename ELFT::uint uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000345 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000346 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000347 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000348 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000349 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000350 bool isDynamic() const { return Dynamic; }
351
352private:
353 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000354 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000355 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000356 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000357};
358
359template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000360class HashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000361 typedef typename ELFT::Word Elf_Word;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000362
363public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000364 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000365 void finalize() override;
366 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000367};
368
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000369// Outputs GNU Hash section. For detailed explanation see:
370// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
371template <class ELFT>
372class GnuHashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000373 typedef typename ELFT::Off Elf_Off;
374 typedef typename ELFT::Word Elf_Word;
375 typedef typename ELFT::uint uintX_t;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000376
377public:
378 GnuHashTableSection();
379 void finalize() override;
380 void writeTo(uint8_t *Buf) override;
381
Igor Kudrinf1d60292015-10-28 07:05:56 +0000382 // Adds symbols to the hash table.
383 // Sorts the input to satisfy GNU hash section requirements.
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000384 void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000385
386private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000387 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000388 static unsigned calcMaskWords(unsigned NumHashed);
389
390 void writeHeader(uint8_t *&Buf);
391 void writeBloomFilter(uint8_t *&Buf);
392 void writeHashTable(uint8_t *Buf);
393
Rui Ueyama861c7312016-02-17 05:40:03 +0000394 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000395 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000396 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000397 uint32_t Hash;
398 };
399
Rui Ueyama861c7312016-02-17 05:40:03 +0000400 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000401
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000402 unsigned MaskWords;
403 unsigned NBuckets;
404 unsigned Shift2;
405};
406
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000407template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000408class DynamicSection final : public OutputSectionBase<ELFT> {
409 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000410 typedef typename ELFT::Dyn Elf_Dyn;
411 typedef typename ELFT::Rel Elf_Rel;
412 typedef typename ELFT::Rela Elf_Rela;
413 typedef typename ELFT::Shdr Elf_Shdr;
414 typedef typename ELFT::Sym Elf_Sym;
415 typedef typename ELFT::uint uintX_t;
Rafael Espindolade069362016-01-25 21:32:04 +0000416
Rui Ueyama909cc682016-02-02 03:11:27 +0000417 // The .dynamic section contains information for the dynamic linker.
418 // The section consists of fixed size entries, which consist of
419 // type and value fields. Value are one of plain integers, symbol
420 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000421 struct Entry {
422 int32_t Tag;
423 union {
424 OutputSectionBase<ELFT> *OutSec;
425 uint64_t Val;
426 const SymbolBody *Sym;
427 };
428 enum KindT { SecAddr, SymAddr, PlainInt } Kind;
429 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
430 : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
431 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
432 Entry(int32_t Tag, const SymbolBody *Sym)
433 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
434 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000435
436 // finalize() fills this vector with the section contents. finalize()
437 // cannot directly create final section contents because when the
438 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000439 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000440
441public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000442 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000443 void finalize() override;
444 void writeTo(uint8_t *Buf) override;
445
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000446 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
447 OutputSectionBase<ELFT> *InitArraySec = nullptr;
448 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000449
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000450private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000451 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000452};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000453
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000454template <class ELFT>
455class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
456 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
457
458public:
459 MipsReginfoOutputSection();
460 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000461 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000462
463private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000464 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000465};
466
George Rimarf6bc65a2016-01-15 13:34:52 +0000467// --eh-frame-hdr option tells linker to construct a header for all the
468// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
469// and also to a PT_GNU_EH_FRAME segment.
470// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
471// calling dl_iterate_phdr.
472// This section contains a lookup table for quick binary search of FDEs.
473// Detailed info about internals can be found in Ian Lance Taylor's blog:
474// http://www.airs.com/blog/archives/460 (".eh_frame")
475// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
476template <class ELFT>
477class EhFrameHeader final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000478 typedef typename ELFT::uint uintX_t;
George Rimarf6bc65a2016-01-15 13:34:52 +0000479
480public:
481 EhFrameHeader();
482 void writeTo(uint8_t *Buf) override;
483
484 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
485 void assignEhFrame(EHOutputSection<ELFT> *Sec);
486 void reserveFde();
487
488 bool Live = false;
489
490private:
491 struct FdeData {
492 uint8_t Enc;
493 size_t Off;
494 uint8_t *PCRel;
495 };
496
497 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
498
499 EHOutputSection<ELFT> *Sec = nullptr;
500 std::vector<FdeData> FdeList;
501};
502
Rui Ueyama634ddf02016-03-11 20:51:53 +0000503template <class ELFT>
504class BuildIdSection final : public OutputSectionBase<ELFT> {
505public:
506 BuildIdSection();
507 void writeTo(uint8_t *Buf) override;
508 void update(ArrayRef<uint8_t> Buf);
509 void writeBuildId();
510
511private:
512 uint64_t Hash = 0xcbf29ce484222325; // FNV1 hash basis
513 uint8_t *HashBuf;
514};
515
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000516// All output sections that are hadnled by the linker specially are
517// globally accessible. Writer initializes them, so don't use them
518// until Writer is initialized.
519template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000520 typedef typename ELFT::uint uintX_t;
521 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000522 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000523 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000524 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000525 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000526 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000527 static GotSection<ELFT> *Got;
528 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000529 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000530 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000531 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000532 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000533 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000534 static PltSection<ELFT> *Plt;
535 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000536 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000537 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000538 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000539 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000540 static SymbolTableSection<ELFT> *DynSymTab;
541 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000542 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000543 static OutputSectionBase<ELFT> *ElfHeader;
544 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000545};
Rui Ueyamad888d102015-10-09 19:34:55 +0000546
Rui Ueyama634ddf02016-03-11 20:51:53 +0000547template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000548template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000549template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000550template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000551template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000552template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
553template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000554template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000555template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000556template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000557template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000558template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000559template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
560template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000561template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000562template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000563template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000564template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000565template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
566template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000567template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000568template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
569template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000570
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000571} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000572} // namespace lld
573
574#endif // LLD_ELF_OUTPUT_SECTIONS_H