blob: f9c4b6eccbc45cf4947d3d98acab407874d0e6d9 [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 Ueyama3ce825e2015-10-09 21:07:25 +000026template <class ELFT> class SymbolTable;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000027template <class ELFT> class SymbolTableSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000028template <class ELFT> class StringTableSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000029template <class ELFT> class EHInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000030template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000031template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000032template <class ELFT> class MergeInputSection;
Simon Atanasyan1d7df402015-12-20 10:57:34 +000033template <class ELFT> class MipsReginfoInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000034template <class ELFT> class OutputSection;
35template <class ELFT> class ObjectFile;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +000036template <class ELFT> class SharedFile;
37template <class ELFT> class SharedSymbol;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000038template <class ELFT> class DefinedRegular;
39
Rafael Espindola5805c4f2015-09-21 21:38:08 +000040template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000041static inline typename ELFT::uint getAddend(const typename ELFT::Rel &Rel) {
Rafael Espindola932efcf2015-10-19 20:24:44 +000042 return 0;
43}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000044
45template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000046static inline typename ELFT::uint getAddend(const typename ELFT::Rela &Rel) {
Rafael Espindola932efcf2015-10-19 20:24:44 +000047 return Rel.r_addend;
48}
49
George Rimar12737b72016-02-25 08:40:26 +000050bool isValidCIdentifier(StringRef S);
51
Rafael Espindola71675852015-09-22 00:16:19 +000052// This represents a section in an output file.
53// Different sub classes represent different types of sections. Some contain
54// input sections, others are created by the linker.
55// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000056// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000057template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000058public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +000059 typedef typename ELFT::uint uintX_t;
60 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000061
George Rimar9bec24a2016-02-18 14:20:08 +000062 OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000063 void setVA(uintX_t VA) { Header.sh_addr = VA; }
64 uintX_t getVA() const { return Header.sh_addr; }
65 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000066 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000067 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000068 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000069
Rui Ueyama40845e62015-12-26 05:51:07 +000070 virtual void addSection(InputSectionBase<ELFT> *C) {}
71
Rui Ueyama2317d0d2015-10-15 20:55:22 +000072 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000073
74 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000075 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000076 void setSize(uintX_t Val) { Header.sh_size = Val; }
Rafael Espindola571452c2016-04-11 13:44:05 +000077 uintX_t getFlags() const { return Header.sh_flags; }
78 uintX_t getFileOff() const { return Header.sh_offset; }
79 uintX_t getAlign() const {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000080 // The ELF spec states that a value of 0 means the section has no alignment
81 // constraits.
82 return std::max<uintX_t>(Header.sh_addralign, 1);
83 }
Rafael Espindola571452c2016-04-11 13:44:05 +000084 uint32_t getType() const { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +000085 void updateAlign(uintX_t Align) {
86 if (Align > Header.sh_addralign)
87 Header.sh_addralign = Align;
88 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000089
Rui Ueyama47091902016-03-30 19:41:51 +000090 // If true, this section will be page aligned on disk.
91 // Typically the first section of each PT_LOAD segment has this flag.
92 bool PageAlign = false;
93
Rafael Espindola5805c4f2015-09-21 21:38:08 +000094 virtual void finalize() {}
Rafael Espindola56004c52016-04-07 14:22:09 +000095 virtual void
96 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000097 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000098 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000099
100protected:
101 StringRef Name;
Sean Silva580c1b62016-04-20 04:26:16 +0000102 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000103};
104
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000105template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
106 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000107 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000108
109public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000110 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000111 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000112 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000113 void addEntry(SymbolBody &Sym);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000114 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000115 bool addTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000116 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Rafael Espindola58cd5db2016-04-19 22:46:03 +0000117 uintX_t getMipsLocalEntryOffset(uintX_t EntryValue);
118 uintX_t getMipsLocalPageOffset(uintX_t Addr);
George Rimar90cd0a82015-12-01 19:20:26 +0000119 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
Rafael Espindola74031ba2016-04-07 15:20:56 +0000120 uintX_t getGlobalDynOffset(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000121 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000122
Igor Kudrin304860a2015-11-12 04:39:49 +0000123 // Returns the symbol which corresponds to the first entry of the global part
124 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
125 // table properties.
126 // Returns nullptr if the global part is empty.
127 const SymbolBody *getMipsFirstGlobalEntry() const;
128
129 // Returns the number of entries in the local part of GOT including
130 // the number of reserved entries. This method is MIPS-specific.
131 unsigned getMipsLocalEntriesNum() const;
132
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000133 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
Rafael Espindola74031ba2016-04-07 15:20:56 +0000134 uint32_t getTlsIndexOff() { return TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000135
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000136private:
137 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000138 uint32_t TlsIndexOff = -1;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000139 uint32_t MipsLocalEntries = 0;
Simon Atanasyand2980d32016-03-29 14:07:22 +0000140 // Output sections referenced by MIPS GOT relocations.
141 llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000142 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000143};
144
George Rimar648a2c32015-10-20 08:54:27 +0000145template <class ELFT>
146class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000147 typedef typename ELFT::uint uintX_t;
George Rimar648a2c32015-10-20 08:54:27 +0000148
149public:
150 GotPltSection();
151 void finalize() override;
152 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000153 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000154 bool empty() const;
George Rimar648a2c32015-10-20 08:54:27 +0000155
156private:
157 std::vector<const SymbolBody *> Entries;
158};
159
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000160template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
161 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000162 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000163
164public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000165 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000166 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000167 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000168 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000169 bool empty() const { return Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000170
171private:
George Rimar77b77792015-11-25 22:15:01 +0000172 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000173};
174
175template <class ELFT> struct DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000176 typedef typename ELFT::uint uintX_t;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000177 uint32_t Type;
178
Rafael Espindolaac568f92016-04-11 13:47:35 +0000179 SymbolBody *Sym;
Rafael Espindola34ffcbb2016-04-11 13:51:23 +0000180 const OutputSectionBase<ELFT> *OffsetSec;
Rafael Espindolaac568f92016-04-11 13:47:35 +0000181 uintX_t OffsetInSec;
182 bool UseSymVA;
183 uintX_t Addend;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000184
Rafael Espindola34ffcbb2016-04-11 13:51:23 +0000185 DynamicReloc(uint32_t Type, const OutputSectionBase<ELFT> *OffsetSec,
Rafael Espindolade9857e2016-02-04 21:33:05 +0000186 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
187 uintX_t Addend)
Rafael Espindola74031ba2016-04-07 15:20:56 +0000188 : Type(Type), Sym(Sym), OffsetSec(OffsetSec), OffsetInSec(OffsetInSec),
189 UseSymVA(UseSymVA), Addend(Addend) {}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000190};
191
192template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000193class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000194public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000195 typedef typename ELFT::Shdr Elf_Shdr;
196 typedef typename ELFT::Sym Elf_Sym;
197 typedef typename ELFT::SymRange Elf_Sym_Range;
198 typedef typename ELFT::uint uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000199 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000200 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000201
Rui Ueyama0db335f2015-10-07 16:58:54 +0000202 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000203 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000204 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000205 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000206 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000207
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000208 ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
Rafael Espindolae2c24612016-01-29 01:24:25 +0000209 return Symbols;
210 }
211
212 unsigned NumLocals = 0;
213 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000214
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000215private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000216 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000217 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000218
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000219 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000220
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000221 SymbolTable<ELFT> &Table;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000222
223 // A vector of symbols and their string table offsets.
224 std::vector<std::pair<SymbolBody *, size_t>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000225};
226
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000227// For more information about .gnu.version and .gnu.version_r see:
228// https://www.akkadia.org/drepper/symbol-versioning
229
230// The .gnu.version section specifies the required version of each symbol in the
231// dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
232// table entry. An Elf_Versym is just a 16-bit integer that refers to a version
233// identifier defined in the .gnu.version_r section.
234template <class ELFT>
235class VersionTableSection final : public OutputSectionBase<ELFT> {
236 typedef typename ELFT::Versym Elf_Versym;
237
238public:
239 VersionTableSection();
240 void finalize() override;
241 void writeTo(uint8_t *Buf) override;
242};
243
244// The .gnu.version_r section defines the version identifiers used by
245// .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
246// Elf_Verneed specifies the version requirements for a single DSO, and contains
247// a reference to a linked list of Elf_Vernaux data structures which define the
248// mapping from version identifiers to version names.
249template <class ELFT>
250class VersionNeedSection final : public OutputSectionBase<ELFT> {
251 typedef typename ELFT::Verneed Elf_Verneed;
252 typedef typename ELFT::Vernaux Elf_Vernaux;
253
254 // A vector of shared files that need Elf_Verneed data structures and the
255 // string table offsets of their sonames.
256 std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed;
257
258 // The next available version identifier. Identifiers start at 2 because 0 and
259 // 1 are reserved.
260 unsigned NextIndex = 2;
261
262public:
263 VersionNeedSection();
264 void addSymbol(SharedSymbol<ELFT> *SS);
265 void finalize() override;
266 void writeTo(uint8_t *Buf) override;
267 size_t getNeedNum() const { return Needed.size(); }
268};
269
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000270template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000271class RelocationSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000272 typedef typename ELFT::Rel Elf_Rel;
273 typedef typename ELFT::Rela Elf_Rela;
274 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000275
276public:
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000277 RelocationSection(StringRef Name);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000278 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000279 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000280 void finalize() override;
281 void writeTo(uint8_t *Buf) override;
282 bool hasRelocs() const { return !Relocs.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000283
George Rimara07ff662015-12-21 10:12:06 +0000284 bool Static = false;
285
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000286private:
287 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000288};
289
290template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000291class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000292public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000293 typedef typename ELFT::Shdr Elf_Shdr;
294 typedef typename ELFT::Sym Elf_Sym;
295 typedef typename ELFT::Rel Elf_Rel;
296 typedef typename ELFT::Rela Elf_Rela;
297 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000298 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000299 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000300 void sortInitFini();
301 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000302 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000303 void finalize() override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000304 void
305 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) override;
Rafael Espindola71675852015-09-22 00:16:19 +0000306 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000307};
308
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000309template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000310class MergeOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000311 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000312
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000313 bool shouldTailMerge() const;
314
Rafael Espindolac159c962015-10-19 21:00:02 +0000315public:
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;
Rafael Espindolac159c962015-10-19 21:00:02 +0000322
323private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000324 llvm::StringTableBuilder Builder;
Rafael Espindolac159c962015-10-19 21:00:02 +0000325};
326
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000327// FDE or CIE
328template <class ELFT> struct EHRegion {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000329 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000330 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
331 StringRef data() const;
332 EHInputSection<ELFT> *S;
333 unsigned Index;
334};
335
336template <class ELFT> struct Cie : public EHRegion<ELFT> {
337 Cie(EHInputSection<ELFT> *S, unsigned Index);
338 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000339 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000340};
341
342template <class ELFT>
343class EHOutputSection final : public OutputSectionBase<ELFT> {
344public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000345 typedef typename ELFT::uint uintX_t;
346 typedef typename ELFT::Shdr Elf_Shdr;
347 typedef typename ELFT::Rel Elf_Rel;
348 typedef typename ELFT::Rela Elf_Rela;
George Rimar9bec24a2016-02-18 14:20:08 +0000349 EHOutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000350 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000351 void finalize() override;
352 void
353 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000354
Rui Ueyamafc467e72016-03-13 05:06:50 +0000355 template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000356 void addSectionAux(EHInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000357
Rui Ueyama40845e62015-12-26 05:51:07 +0000358 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000359
360private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000361 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000362
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000363 std::vector<EHInputSection<ELFT> *> Sections;
364 std::vector<Cie<ELFT>> Cies;
365
366 // Maps CIE content + personality to a index in Cies.
Rafael Espindola156ed8d2016-02-10 13:19:32 +0000367 llvm::DenseMap<std::pair<StringRef, SymbolBody *>, unsigned> CieMap;
Rafael Espindola56004c52016-04-07 14:22:09 +0000368 bool Finalized = false;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000369};
370
Rafael Espindolac159c962015-10-19 21:00:02 +0000371template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000372class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000373public:
374 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000375 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000376};
377
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000378template <class ELFT>
379class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000380public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000381 typedef typename ELFT::uint uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000382 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000383 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000384 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000385 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000386 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000387 bool isDynamic() const { return Dynamic; }
388
389private:
390 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000391 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000392 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000393 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000394};
395
396template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000397class HashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000398 typedef typename ELFT::Word Elf_Word;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000399
400public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000401 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000402 void finalize() override;
403 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000404};
405
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000406// Outputs GNU Hash section. For detailed explanation see:
407// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
408template <class ELFT>
409class GnuHashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000410 typedef typename ELFT::Off Elf_Off;
411 typedef typename ELFT::Word Elf_Word;
412 typedef typename ELFT::uint uintX_t;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000413
414public:
415 GnuHashTableSection();
416 void finalize() override;
417 void writeTo(uint8_t *Buf) override;
418
Igor Kudrinf1d60292015-10-28 07:05:56 +0000419 // Adds symbols to the hash table.
420 // Sorts the input to satisfy GNU hash section requirements.
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000421 void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000422
423private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000424 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000425 static unsigned calcMaskWords(unsigned NumHashed);
426
427 void writeHeader(uint8_t *&Buf);
428 void writeBloomFilter(uint8_t *&Buf);
429 void writeHashTable(uint8_t *Buf);
430
Rui Ueyama861c7312016-02-17 05:40:03 +0000431 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000432 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000433 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000434 uint32_t Hash;
435 };
436
Rui Ueyama861c7312016-02-17 05:40:03 +0000437 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000438
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000439 unsigned MaskWords;
440 unsigned NBuckets;
441 unsigned Shift2;
442};
443
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000444template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000445class DynamicSection final : public OutputSectionBase<ELFT> {
446 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000447 typedef typename ELFT::Dyn Elf_Dyn;
448 typedef typename ELFT::Rel Elf_Rel;
449 typedef typename ELFT::Rela Elf_Rela;
450 typedef typename ELFT::Shdr Elf_Shdr;
451 typedef typename ELFT::Sym Elf_Sym;
452 typedef typename ELFT::uint uintX_t;
Rafael Espindolade069362016-01-25 21:32:04 +0000453
Rui Ueyama909cc682016-02-02 03:11:27 +0000454 // The .dynamic section contains information for the dynamic linker.
455 // The section consists of fixed size entries, which consist of
456 // type and value fields. Value are one of plain integers, symbol
457 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000458 struct Entry {
459 int32_t Tag;
460 union {
461 OutputSectionBase<ELFT> *OutSec;
462 uint64_t Val;
463 const SymbolBody *Sym;
464 };
465 enum KindT { SecAddr, SymAddr, PlainInt } Kind;
466 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
467 : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
468 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
469 Entry(int32_t Tag, const SymbolBody *Sym)
470 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
471 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000472
473 // finalize() fills this vector with the section contents. finalize()
474 // cannot directly create final section contents because when the
475 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000476 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000477
478public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000479 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000480 void finalize() override;
481 void writeTo(uint8_t *Buf) override;
482
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000483 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
484 OutputSectionBase<ELFT> *InitArraySec = nullptr;
485 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000486
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000487private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000488 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000489};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000490
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000491template <class ELFT>
492class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
493 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
494
495public:
496 MipsReginfoOutputSection();
497 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000498 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000499
500private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000501 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000502};
503
George Rimarf6bc65a2016-01-15 13:34:52 +0000504// --eh-frame-hdr option tells linker to construct a header for all the
505// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
506// and also to a PT_GNU_EH_FRAME segment.
507// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
508// calling dl_iterate_phdr.
509// This section contains a lookup table for quick binary search of FDEs.
510// Detailed info about internals can be found in Ian Lance Taylor's blog:
511// http://www.airs.com/blog/archives/460 (".eh_frame")
512// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
513template <class ELFT>
514class EhFrameHeader final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000515 typedef typename ELFT::uint uintX_t;
George Rimarf6bc65a2016-01-15 13:34:52 +0000516
517public:
518 EhFrameHeader();
519 void writeTo(uint8_t *Buf) override;
520
521 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
522 void assignEhFrame(EHOutputSection<ELFT> *Sec);
523 void reserveFde();
524
525 bool Live = false;
526
Rafael Espindola56004c52016-04-07 14:22:09 +0000527 EHOutputSection<ELFT> *Sec = nullptr;
528
George Rimarf6bc65a2016-01-15 13:34:52 +0000529private:
530 struct FdeData {
531 uint8_t Enc;
532 size_t Off;
533 uint8_t *PCRel;
534 };
535
536 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
537
George Rimarf6bc65a2016-01-15 13:34:52 +0000538 std::vector<FdeData> FdeList;
539};
540
Rui Ueyama3a41be22016-04-07 22:49:21 +0000541template <class ELFT> class BuildIdSection : public OutputSectionBase<ELFT> {
Rui Ueyama634ddf02016-03-11 20:51:53 +0000542public:
Rui Ueyama634ddf02016-03-11 20:51:53 +0000543 void writeTo(uint8_t *Buf) override;
Rui Ueyama3a41be22016-04-07 22:49:21 +0000544 virtual void update(ArrayRef<uint8_t> Buf) = 0;
545 virtual void writeBuildId() = 0;
546
547protected:
548 BuildIdSection(size_t HashSize);
549 size_t HashSize;
550 uint8_t *HashBuf = nullptr;
551};
552
553template <class ELFT> class BuildIdFnv1 final : public BuildIdSection<ELFT> {
554public:
555 BuildIdFnv1() : BuildIdSection<ELFT>(8) {}
556 void update(ArrayRef<uint8_t> Buf) override;
557 void writeBuildId() override;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000558
559private:
Rui Ueyama3a41be22016-04-07 22:49:21 +0000560 // 64-bit FNV-1 initial value
561 uint64_t Hash = 0xcbf29ce484222325;
562};
563
564template <class ELFT> class BuildIdMd5 final : public BuildIdSection<ELFT> {
565public:
566 BuildIdMd5() : BuildIdSection<ELFT>(16) {}
567 void update(ArrayRef<uint8_t> Buf) override;
568 void writeBuildId() override;
569
570private:
571 llvm::MD5 Hash;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000572};
573
Rui Ueyamad86ec302016-04-07 23:51:56 +0000574template <class ELFT> class BuildIdSha1 final : public BuildIdSection<ELFT> {
575public:
576 BuildIdSha1() : BuildIdSection<ELFT>(20) {}
577 void update(ArrayRef<uint8_t> Buf) override;
578 void writeBuildId() override;
579
580private:
581 llvm::SHA1 Hash;
582};
583
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000584// All output sections that are hadnled by the linker specially are
585// globally accessible. Writer initializes them, so don't use them
586// until Writer is initialized.
587template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000588 typedef typename ELFT::uint uintX_t;
589 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000590 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000591 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000592 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000593 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000594 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000595 static GotSection<ELFT> *Got;
596 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000597 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000598 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000599 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000600 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000601 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000602 static PltSection<ELFT> *Plt;
603 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000604 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000605 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000606 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000607 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000608 static SymbolTableSection<ELFT> *DynSymTab;
609 static SymbolTableSection<ELFT> *SymTab;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000610 static VersionTableSection<ELFT> *VerSym;
611 static VersionNeedSection<ELFT> *VerNeed;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000612 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000613 static OutputSectionBase<ELFT> *ElfHeader;
614 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000615};
Rui Ueyamad888d102015-10-09 19:34:55 +0000616
Rui Ueyama634ddf02016-03-11 20:51:53 +0000617template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000618template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000619template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000620template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000621template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000622template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
623template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000624template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000625template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000626template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000627template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000628template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000629template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
630template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000631template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000632template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000633template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000634template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000635template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
636template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000637template <class ELFT> VersionTableSection<ELFT> *Out<ELFT>::VerSym;
638template <class ELFT> VersionNeedSection<ELFT> *Out<ELFT>::VerNeed;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000639template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000640template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
641template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000642
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000643} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000644} // namespace lld
645
646#endif // LLD_ELF_OUTPUT_SECTIONS_H