blob: 0b4edb9e355d5169e836c88926e3903790120c45 [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);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000103 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000104 bool addTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000105 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000106 uintX_t getMipsLocalFullAddr(const SymbolBody &B);
107 uintX_t getMipsLocalPageAddr(uintX_t Addr);
George Rimar90cd0a82015-12-01 19:20:26 +0000108 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000109 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000110
Igor Kudrin304860a2015-11-12 04:39:49 +0000111 // Returns the symbol which corresponds to the first entry of the global part
112 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
113 // table properties.
114 // Returns nullptr if the global part is empty.
115 const SymbolBody *getMipsFirstGlobalEntry() const;
116
117 // Returns the number of entries in the local part of GOT including
118 // the number of reserved entries. This method is MIPS-specific.
119 unsigned getMipsLocalEntriesNum() const;
120
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000121 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000122
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000123private:
124 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000125 uint32_t TlsIndexOff = -1;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000126 uint32_t MipsLocalEntries = 0;
127 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
128
129 uintX_t getMipsLocalEntryAddr(uintX_t EntryValue);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000130};
131
George Rimar648a2c32015-10-20 08:54:27 +0000132template <class ELFT>
133class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000134 typedef typename ELFT::uint uintX_t;
George Rimar648a2c32015-10-20 08:54:27 +0000135
136public:
137 GotPltSection();
138 void finalize() override;
139 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000140 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000141 bool empty() const;
George Rimar648a2c32015-10-20 08:54:27 +0000142
143private:
144 std::vector<const SymbolBody *> Entries;
145};
146
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000147template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
148 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000149 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000150
151public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000152 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000153 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000154 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000155 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000156 bool empty() const { return Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000157
158private:
George Rimar77b77792015-11-25 22:15:01 +0000159 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000160};
161
162template <class ELFT> struct DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000163 typedef typename ELFT::uint uintX_t;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000164 uint32_t Type;
165
166 // Where the relocation is.
167 enum OffsetKind {
168 Off_Got, // The got entry of Sym.
169 Off_GotPlt, // The got.plt entry of Sym.
170 Off_Bss, // The bss entry of Sym (copy reloc).
171 Off_Sec, // The final position of the given input section and offset.
172 Off_LTlsIndex, // The local tls index.
173 Off_GTlsIndex, // The global tls index of Sym.
174 Off_GTlsOffset // The global tls offset of Sym.
175 } OKind;
176
177 SymbolBody *Sym = nullptr;
178 InputSectionBase<ELFT> *OffsetSec = nullptr;
179 uintX_t OffsetInSec = 0;
180 bool UseSymVA = false;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000181 uintX_t Addend = 0;
182
183 DynamicReloc(uint32_t Type, OffsetKind OKind, SymbolBody *Sym)
184 : Type(Type), OKind(OKind), Sym(Sym) {}
185
186 DynamicReloc(uint32_t Type, OffsetKind OKind, bool UseSymVA, SymbolBody *Sym)
187 : Type(Type), OKind(OKind), Sym(Sym), UseSymVA(UseSymVA) {}
188
189 DynamicReloc(uint32_t Type, InputSectionBase<ELFT> *OffsetSec,
190 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
191 uintX_t Addend)
192 : Type(Type), OKind(Off_Sec), Sym(Sym), OffsetSec(OffsetSec),
193 OffsetInSec(OffsetInSec), UseSymVA(UseSymVA), Addend(Addend) {}
194
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000195 uintX_t getOffset() const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000196};
197
198template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000199class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000200public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000201 typedef typename ELFT::Shdr Elf_Shdr;
202 typedef typename ELFT::Sym Elf_Sym;
203 typedef typename ELFT::SymRange Elf_Sym_Range;
204 typedef typename ELFT::uint uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000205 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000206 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000207
Rui Ueyama0db335f2015-10-07 16:58:54 +0000208 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000209 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000210 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000211 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000212 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000213
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000214 ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
Rafael Espindolae2c24612016-01-29 01:24:25 +0000215 return Symbols;
216 }
217
218 unsigned NumLocals = 0;
219 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000220
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000221private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000222 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000223 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000224
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000225 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000226 static uint8_t getSymbolBinding(SymbolBody *Body);
227
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000228 SymbolTable<ELFT> &Table;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000229
230 // A vector of symbols and their string table offsets.
231 std::vector<std::pair<SymbolBody *, size_t>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000232};
233
234template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000235class RelocationSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000236 typedef typename ELFT::Rel Elf_Rel;
237 typedef typename ELFT::Rela Elf_Rela;
238 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000239
240public:
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000241 RelocationSection(StringRef Name);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000242 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000243 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000244 void finalize() override;
245 void writeTo(uint8_t *Buf) override;
246 bool hasRelocs() const { return !Relocs.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000247
George Rimara07ff662015-12-21 10:12:06 +0000248 bool Static = false;
249
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000250private:
251 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000252};
253
254template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000255class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000256public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000257 typedef typename ELFT::Shdr Elf_Shdr;
258 typedef typename ELFT::Sym Elf_Sym;
259 typedef typename ELFT::Rel Elf_Rel;
260 typedef typename ELFT::Rela Elf_Rela;
261 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000262 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000263 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000264 void sortInitFini();
265 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000266 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000267 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000268
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000269private:
Rui Ueyama5af83682016-02-11 23:41:38 +0000270 void reassignOffsets();
Rafael Espindola71675852015-09-22 00:16:19 +0000271 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000272};
273
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000274template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000275class MergeOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000276 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000277
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000278 bool shouldTailMerge() const;
279
Rafael Espindolac159c962015-10-19 21:00:02 +0000280public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000281 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
282 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000283 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000284 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000285 unsigned getOffset(StringRef Val);
286 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000287
288private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000289 llvm::StringTableBuilder Builder;
Rafael Espindolac159c962015-10-19 21:00:02 +0000290};
291
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000292// FDE or CIE
293template <class ELFT> struct EHRegion {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000294 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000295 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
296 StringRef data() const;
297 EHInputSection<ELFT> *S;
298 unsigned Index;
299};
300
301template <class ELFT> struct Cie : public EHRegion<ELFT> {
302 Cie(EHInputSection<ELFT> *S, unsigned Index);
303 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000304 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000305};
306
307template <class ELFT>
308class EHOutputSection final : public OutputSectionBase<ELFT> {
309public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000310 typedef typename ELFT::uint uintX_t;
311 typedef typename ELFT::Shdr Elf_Shdr;
312 typedef typename ELFT::Rel Elf_Rel;
313 typedef typename ELFT::Rela Elf_Rela;
George Rimar9bec24a2016-02-18 14:20:08 +0000314 EHOutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000315 void writeTo(uint8_t *Buf) override;
316
Rui Ueyamafc467e72016-03-13 05:06:50 +0000317 template <class RelTy>
318 void addSectionAux(EHInputSection<ELFT> *S,
319 llvm::iterator_range<const RelTy *> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000320
Rui Ueyama40845e62015-12-26 05:51:07 +0000321 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000322
323private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000324 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000325
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000326 std::vector<EHInputSection<ELFT> *> Sections;
327 std::vector<Cie<ELFT>> Cies;
328
329 // Maps CIE content + personality to a index in Cies.
Rafael Espindola156ed8d2016-02-10 13:19:32 +0000330 llvm::DenseMap<std::pair<StringRef, SymbolBody *>, unsigned> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000331};
332
Rafael Espindolac159c962015-10-19 21:00:02 +0000333template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000334class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000335public:
336 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000337 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000338};
339
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000340template <class ELFT>
341class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000342public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000343 typedef typename ELFT::uint uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000344 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000345 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000346 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000347 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000348 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000349 bool isDynamic() const { return Dynamic; }
350
351private:
352 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000353 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000354 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000355 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000356};
357
358template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000359class HashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000360 typedef typename ELFT::Word Elf_Word;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000361
362public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000363 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000364 void finalize() override;
365 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000366};
367
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000368// Outputs GNU Hash section. For detailed explanation see:
369// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
370template <class ELFT>
371class GnuHashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000372 typedef typename ELFT::Off Elf_Off;
373 typedef typename ELFT::Word Elf_Word;
374 typedef typename ELFT::uint uintX_t;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000375
376public:
377 GnuHashTableSection();
378 void finalize() override;
379 void writeTo(uint8_t *Buf) override;
380
Igor Kudrinf1d60292015-10-28 07:05:56 +0000381 // Adds symbols to the hash table.
382 // Sorts the input to satisfy GNU hash section requirements.
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000383 void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000384
385private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000386 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000387 static unsigned calcMaskWords(unsigned NumHashed);
388
389 void writeHeader(uint8_t *&Buf);
390 void writeBloomFilter(uint8_t *&Buf);
391 void writeHashTable(uint8_t *Buf);
392
Rui Ueyama861c7312016-02-17 05:40:03 +0000393 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000394 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000395 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000396 uint32_t Hash;
397 };
398
Rui Ueyama861c7312016-02-17 05:40:03 +0000399 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000400
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000401 unsigned MaskWords;
402 unsigned NBuckets;
403 unsigned Shift2;
404};
405
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000406template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000407class DynamicSection final : public OutputSectionBase<ELFT> {
408 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000409 typedef typename ELFT::Dyn Elf_Dyn;
410 typedef typename ELFT::Rel Elf_Rel;
411 typedef typename ELFT::Rela Elf_Rela;
412 typedef typename ELFT::Shdr Elf_Shdr;
413 typedef typename ELFT::Sym Elf_Sym;
414 typedef typename ELFT::uint uintX_t;
Rafael Espindolade069362016-01-25 21:32:04 +0000415
Rui Ueyama909cc682016-02-02 03:11:27 +0000416 // The .dynamic section contains information for the dynamic linker.
417 // The section consists of fixed size entries, which consist of
418 // type and value fields. Value are one of plain integers, symbol
419 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000420 struct Entry {
421 int32_t Tag;
422 union {
423 OutputSectionBase<ELFT> *OutSec;
424 uint64_t Val;
425 const SymbolBody *Sym;
426 };
427 enum KindT { SecAddr, SymAddr, PlainInt } Kind;
428 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
429 : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
430 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
431 Entry(int32_t Tag, const SymbolBody *Sym)
432 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
433 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000434
435 // finalize() fills this vector with the section contents. finalize()
436 // cannot directly create final section contents because when the
437 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000438 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000439
440public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000441 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000442 void finalize() override;
443 void writeTo(uint8_t *Buf) override;
444
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000445 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
446 OutputSectionBase<ELFT> *InitArraySec = nullptr;
447 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000448
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000449private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000450 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000451};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000452
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000453template <class ELFT>
454class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
455 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
456
457public:
458 MipsReginfoOutputSection();
459 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000460 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000461
462private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000463 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000464};
465
George Rimarf6bc65a2016-01-15 13:34:52 +0000466// --eh-frame-hdr option tells linker to construct a header for all the
467// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
468// and also to a PT_GNU_EH_FRAME segment.
469// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
470// calling dl_iterate_phdr.
471// This section contains a lookup table for quick binary search of FDEs.
472// Detailed info about internals can be found in Ian Lance Taylor's blog:
473// http://www.airs.com/blog/archives/460 (".eh_frame")
474// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
475template <class ELFT>
476class EhFrameHeader final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000477 typedef typename ELFT::uint uintX_t;
George Rimarf6bc65a2016-01-15 13:34:52 +0000478
479public:
480 EhFrameHeader();
481 void writeTo(uint8_t *Buf) override;
482
483 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
484 void assignEhFrame(EHOutputSection<ELFT> *Sec);
485 void reserveFde();
486
487 bool Live = false;
488
489private:
490 struct FdeData {
491 uint8_t Enc;
492 size_t Off;
493 uint8_t *PCRel;
494 };
495
496 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
497
498 EHOutputSection<ELFT> *Sec = nullptr;
499 std::vector<FdeData> FdeList;
500};
501
Rui Ueyama634ddf02016-03-11 20:51:53 +0000502template <class ELFT>
503class BuildIdSection final : public OutputSectionBase<ELFT> {
504public:
505 BuildIdSection();
506 void writeTo(uint8_t *Buf) override;
507 void update(ArrayRef<uint8_t> Buf);
508 void writeBuildId();
509
510private:
511 uint64_t Hash = 0xcbf29ce484222325; // FNV1 hash basis
512 uint8_t *HashBuf;
513};
514
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000515// All output sections that are hadnled by the linker specially are
516// globally accessible. Writer initializes them, so don't use them
517// until Writer is initialized.
518template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000519 typedef typename ELFT::uint uintX_t;
520 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000521 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000522 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000523 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000524 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000525 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000526 static GotSection<ELFT> *Got;
527 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000528 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000529 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000530 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000531 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000532 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000533 static PltSection<ELFT> *Plt;
534 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000535 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000536 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000537 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000538 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000539 static SymbolTableSection<ELFT> *DynSymTab;
540 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000541 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000542 static OutputSectionBase<ELFT> *ElfHeader;
543 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000544};
Rui Ueyamad888d102015-10-09 19:34:55 +0000545
Rui Ueyama634ddf02016-03-11 20:51:53 +0000546template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000547template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000548template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000549template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000550template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000551template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
552template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000553template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000554template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000555template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000556template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000557template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000558template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
559template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000560template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000561template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000562template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000563template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000564template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
565template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000566template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000567template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
568template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000569
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000570} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000571} // namespace lld
572
573#endif // LLD_ELF_OUTPUT_SECTIONS_H