blob: 7ed145229a1c86c429c4fda58b414ba24a968d9d [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"
Rui Ueyama3a41be22016-04-07 22:49:21 +000019#include "llvm/Support/MD5.h"
Rui Ueyamad86ec302016-04-07 23:51:56 +000020#include "llvm/Support/SHA1.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000021
22namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000023namespace elf {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000024
25class SymbolBody;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +000026struct SectionPiece;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000027template <class ELFT> class SymbolTable;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000028template <class ELFT> class SymbolTableSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000029template <class ELFT> class StringTableSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000030template <class ELFT> class EHInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000031template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000032template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000033template <class ELFT> class MergeInputSection;
Simon Atanasyan1d7df402015-12-20 10:57:34 +000034template <class ELFT> class MipsReginfoInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000035template <class ELFT> class OutputSection;
36template <class ELFT> class ObjectFile;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +000037template <class ELFT> class SharedFile;
38template <class ELFT> class SharedSymbol;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000039template <class ELFT> class DefinedRegular;
40
Rafael Espindola5805c4f2015-09-21 21:38:08 +000041template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000042static inline typename ELFT::uint getAddend(const typename ELFT::Rel &Rel) {
Rafael Espindola932efcf2015-10-19 20:24:44 +000043 return 0;
44}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000045
46template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000047static inline typename ELFT::uint getAddend(const typename ELFT::Rela &Rel) {
Rafael Espindola932efcf2015-10-19 20:24:44 +000048 return Rel.r_addend;
49}
50
George Rimar12737b72016-02-25 08:40:26 +000051bool isValidCIdentifier(StringRef S);
52
Rafael Espindola71675852015-09-22 00:16:19 +000053// This represents a section in an output file.
54// Different sub classes represent different types of sections. Some contain
55// input sections, others are created by the linker.
56// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000057// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000058template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000059public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +000060 typedef typename ELFT::uint uintX_t;
61 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000062
George Rimar9bec24a2016-02-18 14:20:08 +000063 OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000064 void setVA(uintX_t VA) { Header.sh_addr = VA; }
65 uintX_t getVA() const { return Header.sh_addr; }
66 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000067 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000068 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000069 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000070
Rui Ueyama40845e62015-12-26 05:51:07 +000071 virtual void addSection(InputSectionBase<ELFT> *C) {}
72
Rui Ueyama2317d0d2015-10-15 20:55:22 +000073 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000074
75 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000076 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000077 void setSize(uintX_t Val) { Header.sh_size = Val; }
Rafael Espindola571452c2016-04-11 13:44:05 +000078 uintX_t getFlags() const { return Header.sh_flags; }
79 uintX_t getFileOff() const { return Header.sh_offset; }
80 uintX_t getAlign() const {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000081 // The ELF spec states that a value of 0 means the section has no alignment
82 // constraits.
83 return std::max<uintX_t>(Header.sh_addralign, 1);
84 }
Rafael Espindola571452c2016-04-11 13:44:05 +000085 uint32_t getType() const { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +000086 void updateAlign(uintX_t Align) {
87 if (Align > Header.sh_addralign)
88 Header.sh_addralign = Align;
89 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000090
Rui Ueyama47091902016-03-30 19:41:51 +000091 // If true, this section will be page aligned on disk.
92 // Typically the first section of each PT_LOAD segment has this flag.
93 bool PageAlign = false;
94
Rafael Espindola5805c4f2015-09-21 21:38:08 +000095 virtual void finalize() {}
Rafael Espindola56004c52016-04-07 14:22:09 +000096 virtual void
97 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000098 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000099 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000100
101protected:
102 StringRef Name;
Sean Silva580c1b62016-04-20 04:26:16 +0000103 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000104};
105
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000106template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
107 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000108 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000109
110public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000111 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000112 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000113 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000114 void addEntry(SymbolBody &Sym);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000115 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000116 bool addTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000117 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Rafael Espindola58cd5db2016-04-19 22:46:03 +0000118 uintX_t getMipsLocalEntryOffset(uintX_t EntryValue);
119 uintX_t getMipsLocalPageOffset(uintX_t Addr);
George Rimar90cd0a82015-12-01 19:20:26 +0000120 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
Rafael Espindola74031ba2016-04-07 15:20:56 +0000121 uintX_t getGlobalDynOffset(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000122 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000123
Igor Kudrin304860a2015-11-12 04:39:49 +0000124 // Returns the symbol which corresponds to the first entry of the global part
125 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
126 // table properties.
127 // Returns nullptr if the global part is empty.
128 const SymbolBody *getMipsFirstGlobalEntry() const;
129
130 // Returns the number of entries in the local part of GOT including
131 // the number of reserved entries. This method is MIPS-specific.
132 unsigned getMipsLocalEntriesNum() const;
133
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000134 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
Rafael Espindola74031ba2016-04-07 15:20:56 +0000135 uint32_t getTlsIndexOff() { return TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000136
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000137private:
138 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000139 uint32_t TlsIndexOff = -1;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000140 uint32_t MipsLocalEntries = 0;
Simon Atanasyand2980d32016-03-29 14:07:22 +0000141 // Output sections referenced by MIPS GOT relocations.
142 llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000143 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000144};
145
George Rimar648a2c32015-10-20 08:54:27 +0000146template <class ELFT>
147class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000148 typedef typename ELFT::uint uintX_t;
George Rimar648a2c32015-10-20 08:54:27 +0000149
150public:
151 GotPltSection();
152 void finalize() override;
153 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000154 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000155 bool empty() const;
George Rimar648a2c32015-10-20 08:54:27 +0000156
157private:
158 std::vector<const SymbolBody *> Entries;
159};
160
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000161template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
162 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000163 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000164
165public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000166 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000167 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000168 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000169 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000170 bool empty() const { return Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000171
172private:
George Rimar77b77792015-11-25 22:15:01 +0000173 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000174};
175
176template <class ELFT> struct DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000177 typedef typename ELFT::uint uintX_t;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000178 uint32_t Type;
179
Rafael Espindolaac568f92016-04-11 13:47:35 +0000180 SymbolBody *Sym;
Rafael Espindola34ffcbb2016-04-11 13:51:23 +0000181 const OutputSectionBase<ELFT> *OffsetSec;
Rafael Espindolaac568f92016-04-11 13:47:35 +0000182 uintX_t OffsetInSec;
183 bool UseSymVA;
184 uintX_t Addend;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000185
Rafael Espindola34ffcbb2016-04-11 13:51:23 +0000186 DynamicReloc(uint32_t Type, const OutputSectionBase<ELFT> *OffsetSec,
Rafael Espindolade9857e2016-02-04 21:33:05 +0000187 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
188 uintX_t Addend)
Rafael Espindola74031ba2016-04-07 15:20:56 +0000189 : Type(Type), Sym(Sym), OffsetSec(OffsetSec), OffsetInSec(OffsetInSec),
190 UseSymVA(UseSymVA), Addend(Addend) {}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000191};
192
193template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000194class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000195public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000196 typedef typename ELFT::Shdr Elf_Shdr;
197 typedef typename ELFT::Sym Elf_Sym;
198 typedef typename ELFT::SymRange Elf_Sym_Range;
199 typedef typename ELFT::uint uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000200 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000201 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000202
Rui Ueyama0db335f2015-10-07 16:58:54 +0000203 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000204 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000205 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000206 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000207 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000208
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000209 ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
Rafael Espindolae2c24612016-01-29 01:24:25 +0000210 return Symbols;
211 }
212
213 unsigned NumLocals = 0;
214 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000215
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000216private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000217 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000218 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000219
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000220 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000221
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000222 SymbolTable<ELFT> &Table;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000223
224 // A vector of symbols and their string table offsets.
225 std::vector<std::pair<SymbolBody *, size_t>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000226};
227
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000228// For more information about .gnu.version and .gnu.version_r see:
229// https://www.akkadia.org/drepper/symbol-versioning
230
231// The .gnu.version section specifies the required version of each symbol in the
232// dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
233// table entry. An Elf_Versym is just a 16-bit integer that refers to a version
234// identifier defined in the .gnu.version_r section.
235template <class ELFT>
236class VersionTableSection final : public OutputSectionBase<ELFT> {
237 typedef typename ELFT::Versym Elf_Versym;
238
239public:
240 VersionTableSection();
241 void finalize() override;
242 void writeTo(uint8_t *Buf) override;
243};
244
245// The .gnu.version_r section defines the version identifiers used by
246// .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
247// Elf_Verneed specifies the version requirements for a single DSO, and contains
248// a reference to a linked list of Elf_Vernaux data structures which define the
249// mapping from version identifiers to version names.
250template <class ELFT>
251class VersionNeedSection final : public OutputSectionBase<ELFT> {
252 typedef typename ELFT::Verneed Elf_Verneed;
253 typedef typename ELFT::Vernaux Elf_Vernaux;
254
255 // A vector of shared files that need Elf_Verneed data structures and the
256 // string table offsets of their sonames.
257 std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed;
258
259 // The next available version identifier. Identifiers start at 2 because 0 and
260 // 1 are reserved.
261 unsigned NextIndex = 2;
262
263public:
264 VersionNeedSection();
265 void addSymbol(SharedSymbol<ELFT> *SS);
266 void finalize() override;
267 void writeTo(uint8_t *Buf) override;
268 size_t getNeedNum() const { return Needed.size(); }
269};
270
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000271template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000272class RelocationSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000273 typedef typename ELFT::Rel Elf_Rel;
274 typedef typename ELFT::Rela Elf_Rela;
275 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000276
277public:
George Rimarc191acf2016-05-10 15:47:57 +0000278 RelocationSection(StringRef Name, bool Sort);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000279 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000280 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000281 void finalize() override;
282 void writeTo(uint8_t *Buf) override;
283 bool hasRelocs() const { return !Relocs.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000284
George Rimara07ff662015-12-21 10:12:06 +0000285 bool Static = false;
286
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000287private:
George Rimarc191acf2016-05-10 15:47:57 +0000288 bool Sort;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000289 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000290};
291
292template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000293class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000294public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000295 typedef typename ELFT::Shdr Elf_Shdr;
296 typedef typename ELFT::Sym Elf_Sym;
297 typedef typename ELFT::Rel Elf_Rel;
298 typedef typename ELFT::Rela Elf_Rela;
299 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000300 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000301 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000302 void sortInitFini();
303 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000304 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000305 void finalize() override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000306 void
307 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) override;
Rafael Espindola71675852015-09-22 00:16:19 +0000308 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000309};
310
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000311template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000312class MergeOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000313 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000314
315public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000316 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
317 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000318 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000319 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000320 unsigned getOffset(StringRef Val);
321 void finalize() override;
Peter Collingbournee29e1422016-05-05 04:10:12 +0000322 bool shouldTailMerge() const;
Rafael Espindolac159c962015-10-19 21:00:02 +0000323
324private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000325 llvm::StringTableBuilder Builder;
Rafael Espindolac159c962015-10-19 21:00:02 +0000326};
327
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000328struct CieRecord {
329 SectionPiece *Piece = nullptr;
330 std::vector<SectionPiece *> FdePieces;
331 uint8_t FdeEncoding = 0;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000332};
333
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000334// Output section for .eh_frame.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000335template <class ELFT>
336class EHOutputSection final : public OutputSectionBase<ELFT> {
337public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000338 typedef typename ELFT::uint uintX_t;
339 typedef typename ELFT::Shdr Elf_Shdr;
340 typedef typename ELFT::Rel Elf_Rel;
341 typedef typename ELFT::Rela Elf_Rela;
George Rimar9bec24a2016-02-18 14:20:08 +0000342 EHOutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000343 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000344 void finalize() override;
345 void
346 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000347
Rui Ueyama40845e62015-12-26 05:51:07 +0000348 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000349
350private:
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000351 template <class RelTy>
352 void addSectionAux(EHInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
353
354 std::vector<SectionPiece> splitInputSection(const EHInputSection<ELFT> *Sec);
355
356 template <class RelTy>
357 CieRecord &addCie(SectionPiece &Piece, EHInputSection<ELFT> *Sec,
358 ArrayRef<RelTy> Rels);
359
360 template <class RelTy>
361 bool isFdeLive(SectionPiece &Piece, EHInputSection<ELFT> *Sec,
362 ArrayRef<RelTy> Rels);
363
George Rimarf6bc65a2016-01-15 13:34:52 +0000364 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000365
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000366 std::vector<EHInputSection<ELFT> *> Sections;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000367 std::vector<CieRecord *> Cies;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000368
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000369 // CIE records are uniquified by their contents and personality functions.
370 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, SymbolBody *>, CieRecord> CieMap;
371
Rafael Espindola56004c52016-04-07 14:22:09 +0000372 bool Finalized = false;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000373};
374
Rafael Espindolac159c962015-10-19 21:00:02 +0000375template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000376class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000377public:
378 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000379 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000380};
381
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000382template <class ELFT>
383class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000384public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000385 typedef typename ELFT::uint uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000386 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000387 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000388 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000389 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000390 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000391 bool isDynamic() const { return Dynamic; }
392
393private:
394 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000395 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000396 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000397 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000398};
399
400template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000401class HashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000402 typedef typename ELFT::Word Elf_Word;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000403
404public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000405 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000406 void finalize() override;
407 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000408};
409
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000410// Outputs GNU Hash section. For detailed explanation see:
411// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
412template <class ELFT>
413class GnuHashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000414 typedef typename ELFT::Off Elf_Off;
415 typedef typename ELFT::Word Elf_Word;
416 typedef typename ELFT::uint uintX_t;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000417
418public:
419 GnuHashTableSection();
420 void finalize() override;
421 void writeTo(uint8_t *Buf) override;
422
Igor Kudrinf1d60292015-10-28 07:05:56 +0000423 // Adds symbols to the hash table.
424 // Sorts the input to satisfy GNU hash section requirements.
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000425 void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000426
427private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000428 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000429 static unsigned calcMaskWords(unsigned NumHashed);
430
431 void writeHeader(uint8_t *&Buf);
432 void writeBloomFilter(uint8_t *&Buf);
433 void writeHashTable(uint8_t *Buf);
434
Rui Ueyama861c7312016-02-17 05:40:03 +0000435 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000436 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000437 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000438 uint32_t Hash;
439 };
440
Rui Ueyama861c7312016-02-17 05:40:03 +0000441 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000442
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000443 unsigned MaskWords;
444 unsigned NBuckets;
445 unsigned Shift2;
446};
447
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000448template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000449class DynamicSection final : public OutputSectionBase<ELFT> {
450 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000451 typedef typename ELFT::Dyn Elf_Dyn;
452 typedef typename ELFT::Rel Elf_Rel;
453 typedef typename ELFT::Rela Elf_Rela;
454 typedef typename ELFT::Shdr Elf_Shdr;
455 typedef typename ELFT::Sym Elf_Sym;
456 typedef typename ELFT::uint uintX_t;
Rafael Espindolade069362016-01-25 21:32:04 +0000457
Rui Ueyama909cc682016-02-02 03:11:27 +0000458 // The .dynamic section contains information for the dynamic linker.
459 // The section consists of fixed size entries, which consist of
460 // type and value fields. Value are one of plain integers, symbol
461 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000462 struct Entry {
463 int32_t Tag;
464 union {
465 OutputSectionBase<ELFT> *OutSec;
466 uint64_t Val;
467 const SymbolBody *Sym;
468 };
469 enum KindT { SecAddr, SymAddr, PlainInt } Kind;
470 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
471 : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
472 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
473 Entry(int32_t Tag, const SymbolBody *Sym)
474 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
475 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000476
477 // finalize() fills this vector with the section contents. finalize()
478 // cannot directly create final section contents because when the
479 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000480 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000481
482public:
Rui Ueyama14703662016-05-16 21:06:31 +0000483 explicit DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000484 void finalize() override;
485 void writeTo(uint8_t *Buf) override;
486
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000487 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
488 OutputSectionBase<ELFT> *InitArraySec = nullptr;
489 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000490
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000491private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000492 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000493};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000494
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000495template <class ELFT>
496class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
497 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
498
499public:
500 MipsReginfoOutputSection();
501 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000502 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000503
504private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000505 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000506};
507
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000508template <class ELFT>
509class MipsOptionsOutputSection final : public OutputSectionBase<ELFT> {
510 typedef llvm::object::Elf_Mips_Options<ELFT> Elf_Mips_Options;
511 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
512
513public:
514 MipsOptionsOutputSection();
515 void writeTo(uint8_t *Buf) override;
516 void addSection(InputSectionBase<ELFT> *S) override;
517
518private:
519 uint32_t GprMask = 0;
520};
521
George Rimarf6bc65a2016-01-15 13:34:52 +0000522// --eh-frame-hdr option tells linker to construct a header for all the
523// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
524// and also to a PT_GNU_EH_FRAME segment.
525// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
526// calling dl_iterate_phdr.
527// This section contains a lookup table for quick binary search of FDEs.
528// Detailed info about internals can be found in Ian Lance Taylor's blog:
529// http://www.airs.com/blog/archives/460 (".eh_frame")
530// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
531template <class ELFT>
532class EhFrameHeader final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000533 typedef typename ELFT::uint uintX_t;
George Rimarf6bc65a2016-01-15 13:34:52 +0000534
535public:
536 EhFrameHeader();
537 void writeTo(uint8_t *Buf) override;
538
539 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
540 void assignEhFrame(EHOutputSection<ELFT> *Sec);
541 void reserveFde();
542
543 bool Live = false;
544
Rafael Espindola56004c52016-04-07 14:22:09 +0000545 EHOutputSection<ELFT> *Sec = nullptr;
546
George Rimarf6bc65a2016-01-15 13:34:52 +0000547private:
548 struct FdeData {
549 uint8_t Enc;
550 size_t Off;
551 uint8_t *PCRel;
552 };
553
554 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
555
George Rimarf6bc65a2016-01-15 13:34:52 +0000556 std::vector<FdeData> FdeList;
557};
558
Rui Ueyama3a41be22016-04-07 22:49:21 +0000559template <class ELFT> class BuildIdSection : public OutputSectionBase<ELFT> {
Rui Ueyama634ddf02016-03-11 20:51:53 +0000560public:
Rui Ueyama634ddf02016-03-11 20:51:53 +0000561 void writeTo(uint8_t *Buf) override;
Rui Ueyamadd368fc2016-05-02 23:35:59 +0000562 virtual void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) = 0;
Rui Ueyama3a41be22016-04-07 22:49:21 +0000563
564protected:
565 BuildIdSection(size_t HashSize);
566 size_t HashSize;
567 uint8_t *HashBuf = nullptr;
568};
569
570template <class ELFT> class BuildIdFnv1 final : public BuildIdSection<ELFT> {
571public:
572 BuildIdFnv1() : BuildIdSection<ELFT>(8) {}
Rui Ueyamadd368fc2016-05-02 23:35:59 +0000573 void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override;
Rui Ueyama3a41be22016-04-07 22:49:21 +0000574};
575
576template <class ELFT> class BuildIdMd5 final : public BuildIdSection<ELFT> {
577public:
578 BuildIdMd5() : BuildIdSection<ELFT>(16) {}
Rui Ueyamadd368fc2016-05-02 23:35:59 +0000579 void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000580};
581
Rui Ueyamad86ec302016-04-07 23:51:56 +0000582template <class ELFT> class BuildIdSha1 final : public BuildIdSection<ELFT> {
583public:
584 BuildIdSha1() : BuildIdSection<ELFT>(20) {}
Rui Ueyamadd368fc2016-05-02 23:35:59 +0000585 void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override;
Rui Ueyamad86ec302016-04-07 23:51:56 +0000586};
587
Rui Ueyama9194db72016-05-13 21:55:56 +0000588template <class ELFT>
589class BuildIdHexstring final : public BuildIdSection<ELFT> {
590public:
591 BuildIdHexstring();
592 void writeBuildId(ArrayRef<ArrayRef<uint8_t>> Bufs) override;
593};
594
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000595// All output sections that are hadnled by the linker specially are
596// globally accessible. Writer initializes them, so don't use them
597// until Writer is initialized.
598template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000599 typedef typename ELFT::uint uintX_t;
600 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000601 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000602 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000603 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000604 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000605 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000606 static GotSection<ELFT> *Got;
607 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000608 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000609 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000610 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000611 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000612 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000613 static PltSection<ELFT> *Plt;
614 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000615 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000616 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000617 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000618 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000619 static SymbolTableSection<ELFT> *DynSymTab;
620 static SymbolTableSection<ELFT> *SymTab;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000621 static VersionTableSection<ELFT> *VerSym;
622 static VersionNeedSection<ELFT> *VerNeed;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000623 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000624 static OutputSectionBase<ELFT> *ElfHeader;
625 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000626};
Rui Ueyamad888d102015-10-09 19:34:55 +0000627
Rui Ueyama634ddf02016-03-11 20:51:53 +0000628template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000629template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000630template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000631template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000632template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000633template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
634template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000635template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000636template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000637template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000638template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000639template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000640template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
641template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000642template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000643template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000644template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000645template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000646template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
647template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000648template <class ELFT> VersionTableSection<ELFT> *Out<ELFT>::VerSym;
649template <class ELFT> VersionNeedSection<ELFT> *Out<ELFT>::VerNeed;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000650template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000651template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
652template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000653
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000654} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000655} // namespace lld
656
657#endif // LLD_ELF_OUTPUT_SECTIONS_H