blob: f06c2ed632d5ce97da399701d6232e4d63e46f11 [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"
George Rimar58fa5242016-10-20 09:19:48 +000014#include "GdbIndex.h"
Simon Atanasyan41325112016-06-19 21:39:37 +000015#include "Relocations.h"
Davide Italiano85121bb2015-09-25 03:56:11 +000016
Rui Ueyamaa0752a52016-03-13 20:28:29 +000017#include "lld/Core/LLVM.h"
Simon Atanasyand2980d32016-03-29 14:07:22 +000018#include "llvm/ADT/SmallPtrSet.h"
Rui Ueyamaa0752a52016-03-13 20:28:29 +000019#include "llvm/MC/StringTableBuilder.h"
20#include "llvm/Object/ELF.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;
Rafael Espindola2deeb602016-07-21 20:18:30 +000026struct EhSectionPiece;
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;
Rui Ueyama0b9a9032016-05-24 04:19:20 +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;
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 Espindola71675852015-09-22 00:16:19 +000040// This represents a section in an output file.
41// Different sub classes represent different types of sections. Some contain
42// input sections, others are created by the linker.
43// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000044// non-overlapping file offsets and VAs.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000045class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000046public:
Eugene Leviant9d278b62016-08-10 18:10:41 +000047 enum Kind {
48 Base,
Eugene Leviant9d278b62016-08-10 18:10:41 +000049 Dynamic,
50 EHFrame,
51 EHFrameHdr,
52 GnuHashTable,
53 Got,
54 GotPlt,
55 HashTable,
Eugene Leviant9d278b62016-08-10 18:10:41 +000056 Merge,
Eugene Leviant9d278b62016-08-10 18:10:41 +000057 Plt,
58 Regular,
59 Reloc,
60 StrTable,
61 SymTable,
62 VersDef,
63 VersNeed,
64 VersTable
65 };
Rafael Espindola5805c4f2015-09-21 21:38:08 +000066
Rafael Espindolae08e78d2016-11-09 23:23:45 +000067 OutputSectionBase(StringRef Name, uint32_t Type, uint64_t Flags);
68 void setLMAOffset(uint64_t LMAOff) { LMAOffset = LMAOff; }
69 uint64_t getLMA() const { return Addr + LMAOffset; }
70 template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
Rafael Espindola63732f52016-11-04 13:20:45 +000071 StringRef getName() const { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000072
Rafael Espindolae08e78d2016-11-09 23:23:45 +000073 virtual void addSection(InputSectionData *C) {}
Eugene Leviant9d278b62016-08-10 18:10:41 +000074 virtual Kind getKind() const { return Base; }
Rafael Espindolae08e78d2016-11-09 23:23:45 +000075 static bool classof(const OutputSectionBase *B) {
Eugene Leviant9d278b62016-08-10 18:10:41 +000076 return B->getKind() == Base;
77 }
Rui Ueyama40845e62015-12-26 05:51:07 +000078
Rui Ueyama2317d0d2015-10-15 20:55:22 +000079 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000080
Rafael Espindola0b113672016-07-27 14:10:56 +000081 uint32_t getPhdrFlags() const;
Rui Ueyama3b04d832016-07-14 05:46:24 +000082
Rafael Espindolae08e78d2016-11-09 23:23:45 +000083 void updateAlignment(uint64_t Alignment) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000084 if (Alignment > Addralign)
85 Addralign = Alignment;
Rafael Espindola115f0f32015-11-03 14:13:40 +000086 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000087
Rui Ueyama47091902016-03-30 19:41:51 +000088 // If true, this section will be page aligned on disk.
89 // Typically the first section of each PT_LOAD segment has this flag.
90 bool PageAlign = false;
91
Eugene Leviant3d9abec2016-09-29 09:20:33 +000092 // Pointer to the first section in PT_LOAD segment, which this section
93 // also resides in. This field is used to correctly compute file offset
94 // of a section. When two sections share the same load segment, difference
95 // between their file offsets should be equal to difference between their
96 // virtual addresses. To compute some section offset we use the following
97 // formula: Off = Off_first + VA - VA_first.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000098 OutputSectionBase *FirstInPtLoad = nullptr;
Eugene Leviant3d9abec2016-09-29 09:20:33 +000099
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000100 virtual void finalize() {}
Rui Ueyama406b4692016-05-27 14:39:13 +0000101 virtual void finalizePieces() {}
Rui Ueyama809d8e22016-06-23 04:33:42 +0000102 virtual void assignOffsets() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +0000103 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +0000104 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000105
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000106 StringRef Name;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000107
108 // The following fields correspond to Elf_Shdr members.
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000109 uint64_t Size = 0;
110 uint64_t Entsize = 0;
111 uint64_t Addralign = 0;
112 uint64_t Offset = 0;
113 uint64_t Flags = 0;
114 uint64_t LMAOffset = 0;
115 uint64_t Addr = 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000116 uint32_t ShName = 0;
117 uint32_t Type = 0;
118 uint32_t Info = 0;
119 uint32_t Link = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000120};
121
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000122template <class ELFT> class GdbIndexSection final : public OutputSectionBase {
George Rimar58fa5242016-10-20 09:19:48 +0000123 typedef typename ELFT::uint uintX_t;
124
125 const unsigned OffsetTypeSize = 4;
126 const unsigned CuListOffset = 6 * OffsetTypeSize;
127 const unsigned CompilationUnitSize = 16;
128 const unsigned AddressEntrySize = 16 + OffsetTypeSize;
129 const unsigned SymTabEntrySize = 2 * OffsetTypeSize;
130
131public:
132 GdbIndexSection();
133 void finalize() override;
134 void writeTo(uint8_t *Buf) override;
135
136 // Pairs of [CU Offset, CU length].
137 std::vector<std::pair<uintX_t, uintX_t>> CompilationUnits;
138
139private:
140 void parseDebugSections();
141 void readDwarf(InputSection<ELFT> *I);
142
143 uint32_t CuTypesOffset;
144};
145
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000146template <class ELFT> class GotSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000147 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000148
149public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000150 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000151 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000152 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000153 void addEntry(SymbolBody &Sym);
Simon Atanasyan41325112016-06-19 21:39:37 +0000154 void addMipsEntry(SymbolBody &Sym, uintX_t Addend, RelExpr Expr);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000155 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000156 bool addTlsIndex();
Simon Atanasyan41325112016-06-19 21:39:37 +0000157 bool empty() const { return MipsPageEntries == 0 && Entries.empty(); }
Rafael Espindola58cd5db2016-04-19 22:46:03 +0000158 uintX_t getMipsLocalPageOffset(uintX_t Addr);
Simon Atanasyan41325112016-06-19 21:39:37 +0000159 uintX_t getMipsGotOffset(const SymbolBody &B, uintX_t Addend) const;
George Rimar90cd0a82015-12-01 19:20:26 +0000160 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
Rafael Espindola74031ba2016-04-07 15:20:56 +0000161 uintX_t getGlobalDynOffset(const SymbolBody &B) const;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000162 Kind getKind() const override { return Got; }
163 static bool classof(const OutputSectionBase *B) {
164 return B->getKind() == Got;
165 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000166
Igor Kudrin304860a2015-11-12 04:39:49 +0000167 // Returns the symbol which corresponds to the first entry of the global part
168 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
169 // table properties.
170 // Returns nullptr if the global part is empty.
171 const SymbolBody *getMipsFirstGlobalEntry() const;
172
173 // Returns the number of entries in the local part of GOT including
174 // the number of reserved entries. This method is MIPS-specific.
175 unsigned getMipsLocalEntriesNum() const;
176
Simon Atanasyan002e2442016-06-23 15:26:31 +0000177 // Returns offset of TLS part of the MIPS GOT table. This part goes
178 // after 'local' and 'global' entries.
Simon Atanasyanbc946932016-10-19 17:13:43 +0000179 uintX_t getMipsTlsOffset() const;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000180
Rafael Espindola04a2e342016-11-09 01:42:41 +0000181 uintX_t getTlsIndexVA() { return this->Addr + TlsIndexOff; }
Simon Atanasyanbc946932016-10-19 17:13:43 +0000182 uint32_t getTlsIndexOff() const { return TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000183
Rui Ueyama022d8e82016-05-24 03:36:07 +0000184 // Flag to force GOT to be in output if we have relocations
185 // that relies on its address.
186 bool HasGotOffRel = false;
187
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000188private:
189 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000190 uint32_t TlsIndexOff = -1;
Simon Atanasyan41325112016-06-19 21:39:37 +0000191 uint32_t MipsPageEntries = 0;
Simon Atanasyand2980d32016-03-29 14:07:22 +0000192 // Output sections referenced by MIPS GOT relocations.
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000193 llvm::SmallPtrSet<const OutputSectionBase *, 10> MipsOutSections;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000194 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
Simon Atanasyan41325112016-06-19 21:39:37 +0000195
196 // MIPS ABI requires to create unique GOT entry for each Symbol/Addend
197 // pairs. The `MipsGotMap` maps (S,A) pair to the GOT index in the `MipsLocal`
198 // or `MipsGlobal` vectors. In general it does not have a sence to take in
199 // account addend for preemptible symbols because the corresponding
200 // GOT entries should have one-to-one mapping with dynamic symbols table.
201 // But we use the same container's types for both kind of GOT entries
202 // to handle them uniformly.
George Rimara4c7e742016-10-20 08:36:42 +0000203 typedef std::pair<const SymbolBody *, uintX_t> MipsGotEntry;
Simon Atanasyan41325112016-06-19 21:39:37 +0000204 typedef std::vector<MipsGotEntry> MipsGotEntries;
205 llvm::DenseMap<MipsGotEntry, size_t> MipsGotMap;
206 MipsGotEntries MipsLocal;
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000207 MipsGotEntries MipsLocal32;
Simon Atanasyan41325112016-06-19 21:39:37 +0000208 MipsGotEntries MipsGlobal;
209
210 // Write MIPS-specific parts of the GOT.
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000211 void writeMipsGot(uint8_t *Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000212};
213
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000214template <class ELFT> class GotPltSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000215 typedef typename ELFT::uint uintX_t;
George Rimar648a2c32015-10-20 08:54:27 +0000216
217public:
218 GotPltSection();
219 void finalize() override;
220 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000221 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000222 bool empty() const;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000223 Kind getKind() const override { return GotPlt; }
224 static bool classof(const OutputSectionBase *B) {
225 return B->getKind() == GotPlt;
226 }
George Rimar648a2c32015-10-20 08:54:27 +0000227
228private:
229 std::vector<const SymbolBody *> Entries;
230};
231
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000232template <class ELFT> class PltSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000233 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000234
235public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000236 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000237 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000238 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000239 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000240 bool empty() const { return Entries.empty(); }
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000241 Kind getKind() const override { return Plt; }
242 static bool classof(const OutputSectionBase *B) {
243 return B->getKind() == Plt;
244 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000245
246private:
George Rimar77b77792015-11-25 22:15:01 +0000247 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000248};
249
Rui Ueyama809d8e22016-06-23 04:33:42 +0000250template <class ELFT> class DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000251 typedef typename ELFT::uint uintX_t;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000252
253public:
254 DynamicReloc(uint32_t Type, const InputSectionBase<ELFT> *InputSec,
255 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
256 uintX_t Addend)
257 : Type(Type), Sym(Sym), InputSec(InputSec), OffsetInSec(OffsetInSec),
258 UseSymVA(UseSymVA), Addend(Addend) {}
259
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000260 DynamicReloc(uint32_t Type, const OutputSectionBase *OutputSec,
Rui Ueyama809d8e22016-06-23 04:33:42 +0000261 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
262 uintX_t Addend)
263 : Type(Type), Sym(Sym), OutputSec(OutputSec), OffsetInSec(OffsetInSec),
264 UseSymVA(UseSymVA), Addend(Addend) {}
265
266 uintX_t getOffset() const;
267 uintX_t getAddend() const;
268 uint32_t getSymIndex() const;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000269 const OutputSectionBase *getOutputSec() const { return OutputSec; }
Rui Ueyama809d8e22016-06-23 04:33:42 +0000270
Rafael Espindolade9857e2016-02-04 21:33:05 +0000271 uint32_t Type;
272
Rui Ueyama809d8e22016-06-23 04:33:42 +0000273private:
Rafael Espindolaac568f92016-04-11 13:47:35 +0000274 SymbolBody *Sym;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000275 const InputSectionBase<ELFT> *InputSec = nullptr;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000276 const OutputSectionBase *OutputSec = nullptr;
Rafael Espindolaac568f92016-04-11 13:47:35 +0000277 uintX_t OffsetInSec;
278 bool UseSymVA;
279 uintX_t Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000280};
281
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000282struct SymbolTableEntry {
283 SymbolBody *Symbol;
Reid Kleckner2918d0b2016-10-20 00:13:34 +0000284 size_t StrTabOffset;
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000285};
286
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000287template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000288class SymbolTableSection final : public OutputSectionBase {
289 typedef OutputSectionBase Base;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000290
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000291public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000292 typedef typename ELFT::Shdr Elf_Shdr;
293 typedef typename ELFT::Sym Elf_Sym;
294 typedef typename ELFT::SymRange Elf_Sym_Range;
295 typedef typename ELFT::uint uintX_t;
Rui Ueyamaace4f902016-05-24 04:25:47 +0000296 SymbolTableSection(StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000297
Rui Ueyama0db335f2015-10-07 16:58:54 +0000298 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000299 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000300 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000301 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000302 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000303 typename Base::Kind getKind() const override { return Base::SymTable; }
304 static bool classof(const Base *B) { return B->getKind() == Base::SymTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000305
George Rimara4c7e742016-10-20 08:36:42 +0000306 ArrayRef<SymbolTableEntry> getSymbols() const { return Symbols; }
Rafael Espindolae2c24612016-01-29 01:24:25 +0000307
308 unsigned NumLocals = 0;
309 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000310
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000311private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000312 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000313 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000314
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000315 const OutputSectionBase *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000316
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000317 // A vector of symbols and their string table offsets.
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000318 std::vector<SymbolTableEntry> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000319};
320
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000321// For more information about .gnu.version and .gnu.version_r see:
322// https://www.akkadia.org/drepper/symbol-versioning
323
George Rimard3566302016-06-20 11:55:12 +0000324// The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
325// contain symbol version definitions. The number of entries in this section
326// shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
327// The section shall contain an array of Elf_Verdef structures, optionally
328// followed by an array of Elf_Verdaux structures.
329template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000330class VersionDefinitionSection final : public OutputSectionBase {
George Rimard3566302016-06-20 11:55:12 +0000331 typedef typename ELFT::Verdef Elf_Verdef;
332 typedef typename ELFT::Verdaux Elf_Verdaux;
333
George Rimard3566302016-06-20 11:55:12 +0000334public:
335 VersionDefinitionSection();
336 void finalize() override;
337 void writeTo(uint8_t *Buf) override;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000338 Kind getKind() const override { return VersDef; }
339 static bool classof(const OutputSectionBase *B) {
340 return B->getKind() == VersDef;
341 }
Rui Ueyama9f619642016-07-16 02:29:45 +0000342
343private:
344 void writeOne(uint8_t *Buf, uint32_t Index, StringRef Name, size_t NameOff);
345
346 unsigned FileDefNameOff;
George Rimard3566302016-06-20 11:55:12 +0000347};
348
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000349// The .gnu.version section specifies the required version of each symbol in the
350// dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
351// table entry. An Elf_Versym is just a 16-bit integer that refers to a version
George Rimard3566302016-06-20 11:55:12 +0000352// identifier defined in the either .gnu.version_r or .gnu.version_d section.
353// The values 0 and 1 are reserved. All other values are used for versions in
354// the own object or in any of the dependencies.
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000355template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000356class VersionTableSection final : public OutputSectionBase {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000357 typedef typename ELFT::Versym Elf_Versym;
358
359public:
360 VersionTableSection();
361 void finalize() override;
362 void writeTo(uint8_t *Buf) override;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000363 Kind getKind() const override { return VersTable; }
364 static bool classof(const OutputSectionBase *B) {
365 return B->getKind() == VersTable;
366 }
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000367};
368
369// The .gnu.version_r section defines the version identifiers used by
370// .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
371// Elf_Verneed specifies the version requirements for a single DSO, and contains
372// a reference to a linked list of Elf_Vernaux data structures which define the
373// mapping from version identifiers to version names.
374template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000375class VersionNeedSection final : public OutputSectionBase {
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000376 typedef typename ELFT::Verneed Elf_Verneed;
377 typedef typename ELFT::Vernaux Elf_Vernaux;
378
379 // A vector of shared files that need Elf_Verneed data structures and the
380 // string table offsets of their sonames.
381 std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed;
382
George Rimard3566302016-06-20 11:55:12 +0000383 // The next available version identifier.
384 unsigned NextIndex;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000385
386public:
387 VersionNeedSection();
388 void addSymbol(SharedSymbol<ELFT> *SS);
389 void finalize() override;
390 void writeTo(uint8_t *Buf) override;
391 size_t getNeedNum() const { return Needed.size(); }
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000392 Kind getKind() const override { return VersNeed; }
393 static bool classof(const OutputSectionBase *B) {
394 return B->getKind() == VersNeed;
395 }
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000396};
397
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000398template <class ELFT> class RelocationSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000399 typedef typename ELFT::Rel Elf_Rel;
400 typedef typename ELFT::Rela Elf_Rela;
401 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000402
403public:
George Rimarc191acf2016-05-10 15:47:57 +0000404 RelocationSection(StringRef Name, bool Sort);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000405 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000406 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000407 void finalize() override;
408 void writeTo(uint8_t *Buf) override;
409 bool hasRelocs() const { return !Relocs.empty(); }
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000410 Kind getKind() const override { return Reloc; }
Eugene Leviantaa498192016-08-31 08:51:39 +0000411 size_t getRelativeRelocCount() const { return NumRelativeRelocs; }
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000412 static bool classof(const OutputSectionBase *B) {
413 return B->getKind() == Reloc;
414 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000415
416private:
George Rimarc191acf2016-05-10 15:47:57 +0000417 bool Sort;
Eugene Leviantaa498192016-08-31 08:51:39 +0000418 size_t NumRelativeRelocs = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000419 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000420};
421
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000422template <class ELFT> class OutputSection final : public OutputSectionBase {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000423
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000424public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000425 typedef typename ELFT::Shdr Elf_Shdr;
426 typedef typename ELFT::Sym Elf_Sym;
427 typedef typename ELFT::Rel Elf_Rel;
428 typedef typename ELFT::Rela Elf_Rela;
429 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000430 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000431 void addSection(InputSectionData *C) override;
George Rimar1a33c0f2016-11-10 09:05:20 +0000432 void sort(std::function<unsigned(InputSection<ELFT> *S)> Order);
Rui Ueyama5af83682016-02-11 23:41:38 +0000433 void sortInitFini();
434 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000435 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000436 void finalize() override;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000437 void assignOffsets() override;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000438 Kind getKind() const override { return Regular; }
439 static bool classof(const OutputSectionBase *B) {
440 return B->getKind() == Regular;
441 }
Rafael Espindola71675852015-09-22 00:16:19 +0000442 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000443};
444
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000445template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000446class MergeOutputSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000447 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000448
449public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000450 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
451 uintX_t Alignment);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000452 void addSection(InputSectionData *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000453 void writeTo(uint8_t *Buf) override;
Justin Lebaree34a732016-10-17 22:24:36 +0000454 unsigned getOffset(llvm::CachedHashStringRef Val);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000455 void finalize() override;
Rui Ueyama406b4692016-05-27 14:39:13 +0000456 void finalizePieces() override;
Peter Collingbournee29e1422016-05-05 04:10:12 +0000457 bool shouldTailMerge() const;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000458 Kind getKind() const override { return Merge; }
459 static bool classof(const OutputSectionBase *B) {
460 return B->getKind() == Merge;
461 }
Rafael Espindolac159c962015-10-19 21:00:02 +0000462
463private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000464 llvm::StringTableBuilder Builder;
Rui Ueyama406b4692016-05-27 14:39:13 +0000465 std::vector<MergeInputSection<ELFT> *> Sections;
Rafael Espindolac159c962015-10-19 21:00:02 +0000466};
467
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000468struct CieRecord {
Rafael Espindola2deeb602016-07-21 20:18:30 +0000469 EhSectionPiece *Piece = nullptr;
470 std::vector<EhSectionPiece *> FdePieces;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000471};
472
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000473// Output section for .eh_frame.
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000474template <class ELFT> class EhOutputSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000475 typedef typename ELFT::uint uintX_t;
476 typedef typename ELFT::Shdr Elf_Shdr;
477 typedef typename ELFT::Rel Elf_Rel;
478 typedef typename ELFT::Rela Elf_Rela;
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000479
480public:
481 EhOutputSection();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000482 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000483 void finalize() override;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000484 bool empty() const { return Sections.empty(); }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000485
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000486 void addSection(InputSectionData *S) override;
487 Kind getKind() const override { return EHFrame; }
488 static bool classof(const OutputSectionBase *B) {
489 return B->getKind() == EHFrame;
490 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000491
Rui Ueyamade9777a2016-05-23 16:30:41 +0000492 size_t NumFdes = 0;
493
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000494private:
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000495 template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000496 void addSectionAux(EhInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000497
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000498 template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000499 CieRecord *addCie(EhSectionPiece &Piece, EhInputSection<ELFT> *Sec,
500 ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000501
502 template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000503 bool isFdeLive(EhSectionPiece &Piece, EhInputSection<ELFT> *Sec,
504 ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000505
Rui Ueyamae75e9332016-05-23 03:00:33 +0000506 uintX_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc);
507
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000508 std::vector<EhInputSection<ELFT> *> Sections;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000509 std::vector<CieRecord *> Cies;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000510
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000511 // CIE records are uniquified by their contents and personality functions.
512 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, SymbolBody *>, CieRecord> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000513};
514
Rafael Espindolac159c962015-10-19 21:00:02 +0000515template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000516class StringTableSection final : public OutputSectionBase {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000517
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000518public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000519 typedef typename ELFT::uint uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000520 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000521 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000522 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000523 bool isDynamic() const { return Dynamic; }
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000524 Kind getKind() const override { return StrTable; }
525 static bool classof(const OutputSectionBase *B) {
526 return B->getKind() == StrTable;
527 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000528
529private:
530 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000531 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000532 std::vector<StringRef> Strings;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000533};
534
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000535template <class ELFT> class HashTableSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000536 typedef typename ELFT::Word Elf_Word;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000537
538public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000539 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000540 void finalize() override;
541 void writeTo(uint8_t *Buf) override;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000542 Kind getKind() const override { return HashTable; }
543 static bool classof(const OutputSectionBase *B) {
544 return B->getKind() == HashTable;
545 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000546};
547
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000548// Outputs GNU Hash section. For detailed explanation see:
549// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
550template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000551class GnuHashTableSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000552 typedef typename ELFT::Off Elf_Off;
553 typedef typename ELFT::Word Elf_Word;
554 typedef typename ELFT::uint uintX_t;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000555
556public:
557 GnuHashTableSection();
558 void finalize() override;
559 void writeTo(uint8_t *Buf) override;
560
Igor Kudrinf1d60292015-10-28 07:05:56 +0000561 // Adds symbols to the hash table.
562 // Sorts the input to satisfy GNU hash section requirements.
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000563 void addSymbols(std::vector<SymbolTableEntry> &Symbols);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000564 Kind getKind() const override { return GnuHashTable; }
565 static bool classof(const OutputSectionBase *B) {
566 return B->getKind() == GnuHashTable;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000567 }
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000568
569private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000570 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000571 static unsigned calcMaskWords(unsigned NumHashed);
572
573 void writeHeader(uint8_t *&Buf);
574 void writeBloomFilter(uint8_t *&Buf);
575 void writeHashTable(uint8_t *Buf);
576
Rui Ueyama861c7312016-02-17 05:40:03 +0000577 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000578 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000579 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000580 uint32_t Hash;
581 };
582
Rui Ueyama861c7312016-02-17 05:40:03 +0000583 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000584
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000585 unsigned MaskWords;
586 unsigned NBuckets;
587 unsigned Shift2;
588};
589
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000590template <class ELFT> class DynamicSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000591 typedef typename ELFT::Dyn Elf_Dyn;
592 typedef typename ELFT::Rel Elf_Rel;
593 typedef typename ELFT::Rela Elf_Rela;
594 typedef typename ELFT::Shdr Elf_Shdr;
595 typedef typename ELFT::Sym Elf_Sym;
596 typedef typename ELFT::uint uintX_t;
Rafael Espindolade069362016-01-25 21:32:04 +0000597
Rui Ueyama909cc682016-02-02 03:11:27 +0000598 // The .dynamic section contains information for the dynamic linker.
599 // The section consists of fixed size entries, which consist of
600 // type and value fields. Value are one of plain integers, symbol
601 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000602 struct Entry {
603 int32_t Tag;
604 union {
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000605 OutputSectionBase *OutSec;
Rafael Espindolade069362016-01-25 21:32:04 +0000606 uint64_t Val;
607 const SymbolBody *Sym;
608 };
George Rimar03e05602016-08-19 15:23:39 +0000609 enum KindT { SecAddr, SecSize, SymAddr, PlainInt } Kind;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000610 Entry(int32_t Tag, OutputSectionBase *OutSec, KindT Kind = SecAddr)
George Rimar03e05602016-08-19 15:23:39 +0000611 : Tag(Tag), OutSec(OutSec), Kind(Kind) {}
Rafael Espindolade069362016-01-25 21:32:04 +0000612 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
613 Entry(int32_t Tag, const SymbolBody *Sym)
614 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
615 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000616
617 // finalize() fills this vector with the section contents. finalize()
618 // cannot directly create final section contents because when the
619 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000620 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000621
622public:
Rui Ueyamaa9593932016-11-02 02:18:01 +0000623 DynamicSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000624 void finalize() override;
625 void writeTo(uint8_t *Buf) override;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000626 Kind getKind() const override { return Dynamic; }
627 static bool classof(const OutputSectionBase *B) {
628 return B->getKind() == Dynamic;
629 }
Rui Ueyamaa9593932016-11-02 02:18:01 +0000630
631private:
632 void addEntries();
633 void Add(Entry E) { Entries.push_back(E); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000634};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000635
George Rimarf6bc65a2016-01-15 13:34:52 +0000636// --eh-frame-hdr option tells linker to construct a header for all the
637// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
638// and also to a PT_GNU_EH_FRAME segment.
639// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
640// calling dl_iterate_phdr.
641// This section contains a lookup table for quick binary search of FDEs.
642// Detailed info about internals can be found in Ian Lance Taylor's blog:
643// http://www.airs.com/blog/archives/460 (".eh_frame")
644// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000645template <class ELFT> class EhFrameHeader final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000646 typedef typename ELFT::uint uintX_t;
George Rimarf6bc65a2016-01-15 13:34:52 +0000647
648public:
649 EhFrameHeader();
Rui Ueyamade9777a2016-05-23 16:30:41 +0000650 void finalize() override;
George Rimarf6bc65a2016-01-15 13:34:52 +0000651 void writeTo(uint8_t *Buf) override;
Rui Ueyamae75e9332016-05-23 03:00:33 +0000652 void addFde(uint32_t Pc, uint32_t FdeVA);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000653 Kind getKind() const override { return EHFrameHdr; }
654 static bool classof(const OutputSectionBase *B) {
655 return B->getKind() == EHFrameHdr;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000656 }
George Rimarf6bc65a2016-01-15 13:34:52 +0000657
George Rimarf6bc65a2016-01-15 13:34:52 +0000658private:
659 struct FdeData {
Rui Ueyamae75e9332016-05-23 03:00:33 +0000660 uint32_t Pc;
661 uint32_t FdeVA;
George Rimarf6bc65a2016-01-15 13:34:52 +0000662 };
663
Rui Ueyamae75e9332016-05-23 03:00:33 +0000664 std::vector<FdeData> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000665};
666
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000667// All output sections that are hadnled by the linker specially are
668// globally accessible. Writer initializes them, so don't use them
669// until Writer is initialized.
670template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000671 typedef typename ELFT::uint uintX_t;
672 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000673
674 static uint8_t First;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000675 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000676 static EhFrameHeader<ELFT> *EhFrameHdr;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000677 static EhOutputSection<ELFT> *EhFrame;
George Rimar58fa5242016-10-20 09:19:48 +0000678 static GdbIndexSection<ELFT> *GdbIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000679 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000680 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000681 static GotSection<ELFT> *Got;
682 static HashTableSection<ELFT> *HashTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000683 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000684 static OutputSection<ELFT> *MipsRldMap;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000685 static OutputSectionBase *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000686 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000687 static PltSection<ELFT> *Plt;
688 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000689 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000690 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000691 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000692 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000693 static SymbolTableSection<ELFT> *DynSymTab;
694 static SymbolTableSection<ELFT> *SymTab;
George Rimard3566302016-06-20 11:55:12 +0000695 static VersionDefinitionSection<ELFT> *VerDef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000696 static VersionTableSection<ELFT> *VerSym;
697 static VersionNeedSection<ELFT> *VerNeed;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000698 static Elf_Phdr *TlsPhdr;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000699 static OutputSectionBase *DebugInfo;
700 static OutputSectionBase *ElfHeader;
701 static OutputSectionBase *ProgramHeaders;
702 static OutputSectionBase *PreinitArray;
703 static OutputSectionBase *InitArray;
704 static OutputSectionBase *FiniArray;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000705};
Rui Ueyamad888d102015-10-09 19:34:55 +0000706
George Rimar6892afa2016-07-12 09:49:43 +0000707template <bool Is64Bits> struct SectionKey {
708 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
709 StringRef Name;
710 uint32_t Type;
711 uintX_t Flags;
712 uintX_t Alignment;
713};
714
715// This class knows how to create an output section for a given
716// input section. Output section type is determined by various
717// factors, including input section's sh_flags, sh_type and
718// linker scripts.
719template <class ELFT> class OutputSectionFactory {
720 typedef typename ELFT::Shdr Elf_Shdr;
721 typedef typename ELFT::uint uintX_t;
722 typedef typename elf::SectionKey<ELFT::Is64Bits> Key;
723
724public:
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000725 std::pair<OutputSectionBase *, bool> create(InputSectionBase<ELFT> *C,
726 StringRef OutsecName);
727 std::pair<OutputSectionBase *, bool>
Rafael Espindola10897f12016-09-13 14:23:14 +0000728 create(const SectionKey<ELFT::Is64Bits> &Key, InputSectionBase<ELFT> *C);
George Rimar6892afa2016-07-12 09:49:43 +0000729
George Rimar6892afa2016-07-12 09:49:43 +0000730private:
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000731 llvm::SmallDenseMap<Key, OutputSectionBase *> Map;
George Rimar6892afa2016-07-12 09:49:43 +0000732};
733
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000734template <class ELFT> uint64_t getHeaderSize() {
735 if (Config->OFormatBinary)
736 return 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000737 return Out<ELFT>::ElfHeader->Size + Out<ELFT>::ProgramHeaders->Size;
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000738}
739
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000740template <class ELFT> uint8_t Out<ELFT>::First;
Rui Ueyamad888d102015-10-09 19:34:55 +0000741template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000742template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000743template <class ELFT> EhOutputSection<ELFT> *Out<ELFT>::EhFrame;
George Rimar58fa5242016-10-20 09:19:48 +0000744template <class ELFT> GdbIndexSection<ELFT> *Out<ELFT>::GdbIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000745template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000746template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000747template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
748template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000749template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000750template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000751template <class ELFT> OutputSectionBase *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000752template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000753template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
754template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000755template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000756template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000757template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000758template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000759template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
760template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
George Rimard3566302016-06-20 11:55:12 +0000761template <class ELFT> VersionDefinitionSection<ELFT> *Out<ELFT>::VerDef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000762template <class ELFT> VersionTableSection<ELFT> *Out<ELFT>::VerSym;
763template <class ELFT> VersionNeedSection<ELFT> *Out<ELFT>::VerNeed;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000764template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000765template <class ELFT> OutputSectionBase *Out<ELFT>::DebugInfo;
766template <class ELFT> OutputSectionBase *Out<ELFT>::ElfHeader;
767template <class ELFT> OutputSectionBase *Out<ELFT>::ProgramHeaders;
768template <class ELFT> OutputSectionBase *Out<ELFT>::PreinitArray;
769template <class ELFT> OutputSectionBase *Out<ELFT>::InitArray;
770template <class ELFT> OutputSectionBase *Out<ELFT>::FiniArray;
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000771} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000772} // namespace lld
773
George Rimar6892afa2016-07-12 09:49:43 +0000774namespace llvm {
775template <bool Is64Bits> struct DenseMapInfo<lld::elf::SectionKey<Is64Bits>> {
776 typedef typename lld::elf::SectionKey<Is64Bits> Key;
777
778 static Key getEmptyKey();
779 static Key getTombstoneKey();
780 static unsigned getHashValue(const Key &Val);
781 static bool isEqual(const Key &LHS, const Key &RHS);
782};
783}
784
785#endif