blob: d374680a06aba8483dbeba68f14e04ee9de7f684 [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;
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 Espindola71675852015-09-22 00:16:19 +000041// This represents a section in an output file.
42// Different sub classes represent different types of sections. Some contain
43// input sections, others are created by the linker.
44// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000045// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000046template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000047public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +000048 typedef typename ELFT::uint uintX_t;
49 typedef typename ELFT::Shdr Elf_Shdr;
Eugene Leviant9d278b62016-08-10 18:10:41 +000050 enum Kind {
51 Base,
Eugene Leviant9d278b62016-08-10 18:10:41 +000052 Dynamic,
53 EHFrame,
54 EHFrameHdr,
55 GnuHashTable,
56 Got,
57 GotPlt,
58 HashTable,
Eugene Leviant9d278b62016-08-10 18:10:41 +000059 Merge,
60 MipsReginfo,
61 MipsOptions,
Simon Atanasyan85c6b442016-08-12 06:28:49 +000062 MipsAbiFlags,
Eugene Leviant9d278b62016-08-10 18:10:41 +000063 Plt,
64 Regular,
65 Reloc,
66 StrTable,
67 SymTable,
68 VersDef,
69 VersNeed,
70 VersTable
71 };
Rafael Espindola5805c4f2015-09-21 21:38:08 +000072
George Rimar9bec24a2016-02-18 14:20:08 +000073 OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags);
Eugene Leviantb71d6f72016-10-06 09:39:28 +000074 void setLMAOffset(uintX_t LMAOff) { LMAOffset = LMAOff; }
Rafael Espindola04a2e342016-11-09 01:42:41 +000075 uintX_t getLMA() const { return Addr + LMAOffset; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000076 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola63732f52016-11-04 13:20:45 +000077 StringRef getName() const { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000078
Rui Ueyama40845e62015-12-26 05:51:07 +000079 virtual void addSection(InputSectionBase<ELFT> *C) {}
Eugene Leviant9d278b62016-08-10 18:10:41 +000080 virtual Kind getKind() const { return Base; }
81 static bool classof(const OutputSectionBase<ELFT> *B) {
82 return B->getKind() == Base;
83 }
Rui Ueyama40845e62015-12-26 05:51:07 +000084
Rui Ueyama2317d0d2015-10-15 20:55:22 +000085 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000086
Rafael Espindola0b113672016-07-27 14:10:56 +000087 uint32_t getPhdrFlags() const;
Rui Ueyama3b04d832016-07-14 05:46:24 +000088
Rui Ueyama424b4082016-06-17 01:18:46 +000089 void updateAlignment(uintX_t Alignment) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000090 if (Alignment > Addralign)
91 Addralign = Alignment;
Rafael Espindola115f0f32015-11-03 14:13:40 +000092 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000093
Rui Ueyama47091902016-03-30 19:41:51 +000094 // If true, this section will be page aligned on disk.
95 // Typically the first section of each PT_LOAD segment has this flag.
96 bool PageAlign = false;
97
Eugene Leviant3d9abec2016-09-29 09:20:33 +000098 // Pointer to the first section in PT_LOAD segment, which this section
99 // also resides in. This field is used to correctly compute file offset
100 // of a section. When two sections share the same load segment, difference
101 // between their file offsets should be equal to difference between their
102 // virtual addresses. To compute some section offset we use the following
103 // formula: Off = Off_first + VA - VA_first.
104 OutputSectionBase<ELFT> *FirstInPtLoad = nullptr;
105
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000106 virtual void finalize() {}
Rui Ueyama406b4692016-05-27 14:39:13 +0000107 virtual void finalizePieces() {}
Rui Ueyama809d8e22016-06-23 04:33:42 +0000108 virtual void assignOffsets() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +0000109 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +0000110 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000111
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000112 StringRef Name;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000113
114 // The following fields correspond to Elf_Shdr members.
115 uintX_t Size = 0;
116 uintX_t Entsize = 0;
117 uintX_t Addralign = 0;
118 uintX_t Offset = 0;
119 uintX_t Flags = 0;
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000120 uintX_t LMAOffset = 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000121 uintX_t Addr = 0;
122 uint32_t ShName = 0;
123 uint32_t Type = 0;
124 uint32_t Info = 0;
125 uint32_t Link = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000126};
127
George Rimar58fa5242016-10-20 09:19:48 +0000128template <class ELFT>
129class GdbIndexSection final : public OutputSectionBase<ELFT> {
130 typedef typename ELFT::uint uintX_t;
131
132 const unsigned OffsetTypeSize = 4;
133 const unsigned CuListOffset = 6 * OffsetTypeSize;
134 const unsigned CompilationUnitSize = 16;
135 const unsigned AddressEntrySize = 16 + OffsetTypeSize;
136 const unsigned SymTabEntrySize = 2 * OffsetTypeSize;
137
138public:
139 GdbIndexSection();
140 void finalize() override;
141 void writeTo(uint8_t *Buf) override;
142
143 // Pairs of [CU Offset, CU length].
144 std::vector<std::pair<uintX_t, uintX_t>> CompilationUnits;
145
146private:
147 void parseDebugSections();
148 void readDwarf(InputSection<ELFT> *I);
149
150 uint32_t CuTypesOffset;
151};
152
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000153template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
154 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000155 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000156
157public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000158 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000159 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000160 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000161 void addEntry(SymbolBody &Sym);
Simon Atanasyan41325112016-06-19 21:39:37 +0000162 void addMipsEntry(SymbolBody &Sym, uintX_t Addend, RelExpr Expr);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000163 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000164 bool addTlsIndex();
Simon Atanasyan41325112016-06-19 21:39:37 +0000165 bool empty() const { return MipsPageEntries == 0 && Entries.empty(); }
Rafael Espindola58cd5db2016-04-19 22:46:03 +0000166 uintX_t getMipsLocalPageOffset(uintX_t Addr);
Simon Atanasyan41325112016-06-19 21:39:37 +0000167 uintX_t getMipsGotOffset(const SymbolBody &B, uintX_t Addend) const;
George Rimar90cd0a82015-12-01 19:20:26 +0000168 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
Rafael Espindola74031ba2016-04-07 15:20:56 +0000169 uintX_t getGlobalDynOffset(const SymbolBody &B) const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000170 typename Base::Kind getKind() const override { return Base::Got; }
171 static bool classof(const Base *B) { return B->getKind() == Base::Got; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000172
Igor Kudrin304860a2015-11-12 04:39:49 +0000173 // Returns the symbol which corresponds to the first entry of the global part
174 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
175 // table properties.
176 // Returns nullptr if the global part is empty.
177 const SymbolBody *getMipsFirstGlobalEntry() const;
178
179 // Returns the number of entries in the local part of GOT including
180 // the number of reserved entries. This method is MIPS-specific.
181 unsigned getMipsLocalEntriesNum() const;
182
Simon Atanasyan002e2442016-06-23 15:26:31 +0000183 // Returns offset of TLS part of the MIPS GOT table. This part goes
184 // after 'local' and 'global' entries.
Simon Atanasyanbc946932016-10-19 17:13:43 +0000185 uintX_t getMipsTlsOffset() const;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000186
Rafael Espindola04a2e342016-11-09 01:42:41 +0000187 uintX_t getTlsIndexVA() { return this->Addr + TlsIndexOff; }
Simon Atanasyanbc946932016-10-19 17:13:43 +0000188 uint32_t getTlsIndexOff() const { return TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000189
Rui Ueyama022d8e82016-05-24 03:36:07 +0000190 // Flag to force GOT to be in output if we have relocations
191 // that relies on its address.
192 bool HasGotOffRel = false;
193
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000194private:
195 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000196 uint32_t TlsIndexOff = -1;
Simon Atanasyan41325112016-06-19 21:39:37 +0000197 uint32_t MipsPageEntries = 0;
Simon Atanasyand2980d32016-03-29 14:07:22 +0000198 // Output sections referenced by MIPS GOT relocations.
199 llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000200 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
Simon Atanasyan41325112016-06-19 21:39:37 +0000201
202 // MIPS ABI requires to create unique GOT entry for each Symbol/Addend
203 // pairs. The `MipsGotMap` maps (S,A) pair to the GOT index in the `MipsLocal`
204 // or `MipsGlobal` vectors. In general it does not have a sence to take in
205 // account addend for preemptible symbols because the corresponding
206 // GOT entries should have one-to-one mapping with dynamic symbols table.
207 // But we use the same container's types for both kind of GOT entries
208 // to handle them uniformly.
George Rimara4c7e742016-10-20 08:36:42 +0000209 typedef std::pair<const SymbolBody *, uintX_t> MipsGotEntry;
Simon Atanasyan41325112016-06-19 21:39:37 +0000210 typedef std::vector<MipsGotEntry> MipsGotEntries;
211 llvm::DenseMap<MipsGotEntry, size_t> MipsGotMap;
212 MipsGotEntries MipsLocal;
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000213 MipsGotEntries MipsLocal32;
Simon Atanasyan41325112016-06-19 21:39:37 +0000214 MipsGotEntries MipsGlobal;
215
216 // Write MIPS-specific parts of the GOT.
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000217 void writeMipsGot(uint8_t *Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000218};
219
George Rimar648a2c32015-10-20 08:54:27 +0000220template <class ELFT>
221class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000222 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000223 typedef OutputSectionBase<ELFT> Base;
George Rimar648a2c32015-10-20 08:54:27 +0000224
225public:
226 GotPltSection();
227 void finalize() override;
228 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000229 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000230 bool empty() const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000231 typename Base::Kind getKind() const override { return Base::GotPlt; }
232 static bool classof(const Base *B) { return B->getKind() == Base::GotPlt; }
George Rimar648a2c32015-10-20 08:54:27 +0000233
234private:
235 std::vector<const SymbolBody *> Entries;
236};
237
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000238template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
239 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000240 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000241
242public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000243 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000244 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000245 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000246 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000247 bool empty() const { return Entries.empty(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000248 typename Base::Kind getKind() const override { return Base::Plt; }
249 static bool classof(const Base *B) { return B->getKind() == Base::Plt; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000250
251private:
George Rimar77b77792015-11-25 22:15:01 +0000252 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000253};
254
Rui Ueyama809d8e22016-06-23 04:33:42 +0000255template <class ELFT> class DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000256 typedef typename ELFT::uint uintX_t;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000257
258public:
259 DynamicReloc(uint32_t Type, const InputSectionBase<ELFT> *InputSec,
260 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
261 uintX_t Addend)
262 : Type(Type), Sym(Sym), InputSec(InputSec), OffsetInSec(OffsetInSec),
263 UseSymVA(UseSymVA), Addend(Addend) {}
264
265 DynamicReloc(uint32_t Type, const OutputSectionBase<ELFT> *OutputSec,
266 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
267 uintX_t Addend)
268 : Type(Type), Sym(Sym), OutputSec(OutputSec), OffsetInSec(OffsetInSec),
269 UseSymVA(UseSymVA), Addend(Addend) {}
270
271 uintX_t getOffset() const;
272 uintX_t getAddend() const;
273 uint32_t getSymIndex() const;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000274 const OutputSectionBase<ELFT> *getOutputSec() const { return OutputSec; }
Rui Ueyama809d8e22016-06-23 04:33:42 +0000275
Rafael Espindolade9857e2016-02-04 21:33:05 +0000276 uint32_t Type;
277
Rui Ueyama809d8e22016-06-23 04:33:42 +0000278private:
Rafael Espindolaac568f92016-04-11 13:47:35 +0000279 SymbolBody *Sym;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000280 const InputSectionBase<ELFT> *InputSec = nullptr;
281 const OutputSectionBase<ELFT> *OutputSec = nullptr;
Rafael Espindolaac568f92016-04-11 13:47:35 +0000282 uintX_t OffsetInSec;
283 bool UseSymVA;
284 uintX_t Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000285};
286
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000287struct SymbolTableEntry {
288 SymbolBody *Symbol;
Reid Kleckner2918d0b2016-10-20 00:13:34 +0000289 size_t StrTabOffset;
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000290};
291
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000292template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000293class SymbolTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000294 typedef OutputSectionBase<ELFT> Base;
295
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000296public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000297 typedef typename ELFT::Shdr Elf_Shdr;
298 typedef typename ELFT::Sym Elf_Sym;
299 typedef typename ELFT::SymRange Elf_Sym_Range;
300 typedef typename ELFT::uint uintX_t;
Rui Ueyamaace4f902016-05-24 04:25:47 +0000301 SymbolTableSection(StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000302
Rui Ueyama0db335f2015-10-07 16:58:54 +0000303 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000304 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000305 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000306 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000307 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000308 typename Base::Kind getKind() const override { return Base::SymTable; }
309 static bool classof(const Base *B) { return B->getKind() == Base::SymTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000310
George Rimara4c7e742016-10-20 08:36:42 +0000311 ArrayRef<SymbolTableEntry> getSymbols() const { return Symbols; }
Rafael Espindolae2c24612016-01-29 01:24:25 +0000312
313 unsigned NumLocals = 0;
314 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000315
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000316private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000317 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000318 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000319
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000320 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000321
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000322 // A vector of symbols and their string table offsets.
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000323 std::vector<SymbolTableEntry> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000324};
325
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000326// For more information about .gnu.version and .gnu.version_r see:
327// https://www.akkadia.org/drepper/symbol-versioning
328
George Rimard3566302016-06-20 11:55:12 +0000329// The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
330// contain symbol version definitions. The number of entries in this section
331// shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
332// The section shall contain an array of Elf_Verdef structures, optionally
333// followed by an array of Elf_Verdaux structures.
334template <class ELFT>
335class VersionDefinitionSection final : public OutputSectionBase<ELFT> {
336 typedef typename ELFT::Verdef Elf_Verdef;
337 typedef typename ELFT::Verdaux Elf_Verdaux;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000338 typedef OutputSectionBase<ELFT> Base;
George Rimard3566302016-06-20 11:55:12 +0000339
George Rimard3566302016-06-20 11:55:12 +0000340public:
341 VersionDefinitionSection();
342 void finalize() override;
343 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000344 typename Base::Kind getKind() const override { return Base::VersDef; }
345 static bool classof(const Base *B) { return B->getKind() == Base::VersDef; }
Rui Ueyama9f619642016-07-16 02:29:45 +0000346
347private:
348 void writeOne(uint8_t *Buf, uint32_t Index, StringRef Name, size_t NameOff);
349
350 unsigned FileDefNameOff;
George Rimard3566302016-06-20 11:55:12 +0000351};
352
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000353// The .gnu.version section specifies the required version of each symbol in the
354// dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
355// table entry. An Elf_Versym is just a 16-bit integer that refers to a version
George Rimard3566302016-06-20 11:55:12 +0000356// identifier defined in the either .gnu.version_r or .gnu.version_d section.
357// The values 0 and 1 are reserved. All other values are used for versions in
358// the own object or in any of the dependencies.
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000359template <class ELFT>
360class VersionTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000361 typedef OutputSectionBase<ELFT> Base;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000362 typedef typename ELFT::Versym Elf_Versym;
363
364public:
365 VersionTableSection();
366 void finalize() override;
367 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000368 typename Base::Kind getKind() const override { return Base::VersTable; }
369 static bool classof(const Base *B) { return B->getKind() == Base::VersTable; }
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000370};
371
372// The .gnu.version_r section defines the version identifiers used by
373// .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
374// Elf_Verneed specifies the version requirements for a single DSO, and contains
375// a reference to a linked list of Elf_Vernaux data structures which define the
376// mapping from version identifiers to version names.
377template <class ELFT>
378class VersionNeedSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000379 typedef OutputSectionBase<ELFT> Base;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000380 typedef typename ELFT::Verneed Elf_Verneed;
381 typedef typename ELFT::Vernaux Elf_Vernaux;
382
383 // A vector of shared files that need Elf_Verneed data structures and the
384 // string table offsets of their sonames.
385 std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed;
386
George Rimard3566302016-06-20 11:55:12 +0000387 // The next available version identifier.
388 unsigned NextIndex;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000389
390public:
391 VersionNeedSection();
392 void addSymbol(SharedSymbol<ELFT> *SS);
393 void finalize() override;
394 void writeTo(uint8_t *Buf) override;
395 size_t getNeedNum() const { return Needed.size(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000396 typename Base::Kind getKind() const override { return Base::VersNeed; }
397 static bool classof(const Base *B) { return B->getKind() == Base::VersNeed; }
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000398};
399
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000400template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000401class RelocationSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000402 typedef typename ELFT::Rel Elf_Rel;
403 typedef typename ELFT::Rela Elf_Rela;
404 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000405 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000406
407public:
George Rimarc191acf2016-05-10 15:47:57 +0000408 RelocationSection(StringRef Name, bool Sort);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000409 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000410 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000411 void finalize() override;
412 void writeTo(uint8_t *Buf) override;
413 bool hasRelocs() const { return !Relocs.empty(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000414 typename Base::Kind getKind() const override { return Base::Reloc; }
Eugene Leviantaa498192016-08-31 08:51:39 +0000415 size_t getRelativeRelocCount() const { return NumRelativeRelocs; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000416 static bool classof(const Base *B) { return B->getKind() == Base::Reloc; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000417
418private:
George Rimarc191acf2016-05-10 15:47:57 +0000419 bool Sort;
Eugene Leviantaa498192016-08-31 08:51:39 +0000420 size_t NumRelativeRelocs = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000421 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000422};
423
424template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000425class OutputSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000426 typedef OutputSectionBase<ELFT> Base;
427
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000428public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000429 typedef typename ELFT::Shdr Elf_Shdr;
430 typedef typename ELFT::Sym Elf_Sym;
431 typedef typename ELFT::Rel Elf_Rel;
432 typedef typename ELFT::Rela Elf_Rela;
433 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000434 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000435 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000436 void sortInitFini();
437 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000438 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000439 void finalize() override;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000440 void assignOffsets() override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000441 typename Base::Kind getKind() const override { return Base::Regular; }
442 static bool classof(const Base *B) { return B->getKind() == Base::Regular; }
Rafael Espindola71675852015-09-22 00:16:19 +0000443 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000444};
445
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000446template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000447class MergeOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000448 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000449 typedef OutputSectionBase<ELFT> Base;
Rafael Espindolac159c962015-10-19 21:00:02 +0000450
451public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000452 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
453 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000454 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000455 void writeTo(uint8_t *Buf) override;
Justin Lebaree34a732016-10-17 22:24:36 +0000456 unsigned getOffset(llvm::CachedHashStringRef Val);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000457 void finalize() override;
Rui Ueyama406b4692016-05-27 14:39:13 +0000458 void finalizePieces() override;
Peter Collingbournee29e1422016-05-05 04:10:12 +0000459 bool shouldTailMerge() const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000460 typename Base::Kind getKind() const override { return Base::Merge; }
461 static bool classof(const Base *B) { return B->getKind() == Base::Merge; }
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 Espindola0c6a4f12015-11-11 19:54:14 +0000474template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +0000475class EhOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000476 typedef typename ELFT::uint uintX_t;
477 typedef typename ELFT::Shdr Elf_Shdr;
478 typedef typename ELFT::Rel Elf_Rel;
479 typedef typename ELFT::Rela Elf_Rela;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000480 typedef OutputSectionBase<ELFT> Base;
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000481
482public:
483 EhOutputSection();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000484 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000485 void finalize() override;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000486 bool empty() const { return Sections.empty(); }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000487
Rui Ueyama40845e62015-12-26 05:51:07 +0000488 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000489 typename Base::Kind getKind() const override { return Base::EHFrame; }
490 static bool classof(const Base *B) { return B->getKind() == Base::EHFrame; }
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>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000516class StringTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000517 typedef OutputSectionBase<ELFT> Base;
518
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000519public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000520 typedef typename ELFT::uint uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000521 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000522 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000523 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000524 bool isDynamic() const { return Dynamic; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000525 typename Base::Kind getKind() const override { return Base::StrTable; }
526 static bool classof(const Base *B) { return B->getKind() == Base::StrTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000527
528private:
529 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000530 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000531 std::vector<StringRef> Strings;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000532};
533
534template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000535class HashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000536 typedef typename ELFT::Word Elf_Word;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000537 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000538
539public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000540 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000541 void finalize() override;
542 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000543 typename Base::Kind getKind() const override { return Base::HashTable; }
544 static bool classof(const Base *B) { return B->getKind() == Base::HashTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000545};
546
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000547// Outputs GNU Hash section. For detailed explanation see:
548// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
549template <class ELFT>
550class GnuHashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000551 typedef typename ELFT::Off Elf_Off;
552 typedef typename ELFT::Word Elf_Word;
553 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000554 typedef OutputSectionBase<ELFT> Base;
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);
Eugene Leviant9d278b62016-08-10 18:10:41 +0000564 typename Base::Kind getKind() const override { return Base::GnuHashTable; }
565 static bool classof(const Base *B) {
566 return B->getKind() == Base::GnuHashTable;
567 }
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 Espindola5805c4f2015-09-21 21:38:08 +0000590template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000591class DynamicSection final : public OutputSectionBase<ELFT> {
592 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000593 typedef typename ELFT::Dyn Elf_Dyn;
594 typedef typename ELFT::Rel Elf_Rel;
595 typedef typename ELFT::Rela Elf_Rela;
596 typedef typename ELFT::Shdr Elf_Shdr;
597 typedef typename ELFT::Sym Elf_Sym;
598 typedef typename ELFT::uint uintX_t;
Rafael Espindolade069362016-01-25 21:32:04 +0000599
Rui Ueyama909cc682016-02-02 03:11:27 +0000600 // The .dynamic section contains information for the dynamic linker.
601 // The section consists of fixed size entries, which consist of
602 // type and value fields. Value are one of plain integers, symbol
603 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000604 struct Entry {
605 int32_t Tag;
606 union {
607 OutputSectionBase<ELFT> *OutSec;
608 uint64_t Val;
609 const SymbolBody *Sym;
610 };
George Rimar03e05602016-08-19 15:23:39 +0000611 enum KindT { SecAddr, SecSize, SymAddr, PlainInt } Kind;
612 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec, KindT Kind = SecAddr)
613 : Tag(Tag), OutSec(OutSec), Kind(Kind) {}
Rafael Espindolade069362016-01-25 21:32:04 +0000614 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
615 Entry(int32_t Tag, const SymbolBody *Sym)
616 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
617 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000618
619 // finalize() fills this vector with the section contents. finalize()
620 // cannot directly create final section contents because when the
621 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000622 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000623
624public:
Rui Ueyamaa9593932016-11-02 02:18:01 +0000625 DynamicSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000626 void finalize() override;
627 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000628 typename Base::Kind getKind() const override { return Base::Dynamic; }
629 static bool classof(const Base *B) { return B->getKind() == Base::Dynamic; }
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
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000636template <class ELFT>
637class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
638 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000639 typedef OutputSectionBase<ELFT> Base;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000640
641public:
642 MipsReginfoOutputSection();
643 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000644 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000645 typename Base::Kind getKind() const override { return Base::MipsReginfo; }
646 static bool classof(const Base *B) {
647 return B->getKind() == Base::MipsReginfo;
648 }
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000649
650private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000651 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000652};
653
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000654template <class ELFT>
655class MipsOptionsOutputSection final : public OutputSectionBase<ELFT> {
656 typedef llvm::object::Elf_Mips_Options<ELFT> Elf_Mips_Options;
657 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000658 typedef OutputSectionBase<ELFT> Base;
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000659
660public:
661 MipsOptionsOutputSection();
662 void writeTo(uint8_t *Buf) override;
663 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000664 typename Base::Kind getKind() const override { return Base::MipsOptions; }
665 static bool classof(const Base *B) {
666 return B->getKind() == Base::MipsOptions;
667 }
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000668
669private:
670 uint32_t GprMask = 0;
671};
672
Simon Atanasyan85c6b442016-08-12 06:28:49 +0000673template <class ELFT>
674class MipsAbiFlagsOutputSection final : public OutputSectionBase<ELFT> {
675 typedef llvm::object::Elf_Mips_ABIFlags<ELFT> Elf_Mips_ABIFlags;
676 typedef OutputSectionBase<ELFT> Base;
677
678public:
679 MipsAbiFlagsOutputSection();
680 void writeTo(uint8_t *Buf) override;
681 void addSection(InputSectionBase<ELFT> *S) override;
682 typename Base::Kind getKind() const override { return Base::MipsAbiFlags; }
683 static bool classof(const Base *B) {
684 return B->getKind() == Base::MipsAbiFlags;
685 }
686
687private:
688 Elf_Mips_ABIFlags Flags;
689};
690
George Rimarf6bc65a2016-01-15 13:34:52 +0000691// --eh-frame-hdr option tells linker to construct a header for all the
692// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
693// and also to a PT_GNU_EH_FRAME segment.
694// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
695// calling dl_iterate_phdr.
696// This section contains a lookup table for quick binary search of FDEs.
697// Detailed info about internals can be found in Ian Lance Taylor's blog:
698// http://www.airs.com/blog/archives/460 (".eh_frame")
699// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
700template <class ELFT>
701class EhFrameHeader final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000702 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000703 typedef OutputSectionBase<ELFT> Base;
George Rimarf6bc65a2016-01-15 13:34:52 +0000704
705public:
706 EhFrameHeader();
Rui Ueyamade9777a2016-05-23 16:30:41 +0000707 void finalize() override;
George Rimarf6bc65a2016-01-15 13:34:52 +0000708 void writeTo(uint8_t *Buf) override;
Rui Ueyamae75e9332016-05-23 03:00:33 +0000709 void addFde(uint32_t Pc, uint32_t FdeVA);
Eugene Leviant9d278b62016-08-10 18:10:41 +0000710 typename Base::Kind getKind() const override { return Base::EHFrameHdr; }
711 static bool classof(const Base *B) {
712 return B->getKind() == Base::EHFrameHdr;
713 }
George Rimarf6bc65a2016-01-15 13:34:52 +0000714
George Rimarf6bc65a2016-01-15 13:34:52 +0000715private:
716 struct FdeData {
Rui Ueyamae75e9332016-05-23 03:00:33 +0000717 uint32_t Pc;
718 uint32_t FdeVA;
George Rimarf6bc65a2016-01-15 13:34:52 +0000719 };
720
Rui Ueyamae75e9332016-05-23 03:00:33 +0000721 std::vector<FdeData> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000722};
723
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000724// All output sections that are hadnled by the linker specially are
725// globally accessible. Writer initializes them, so don't use them
726// until Writer is initialized.
727template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000728 typedef typename ELFT::uint uintX_t;
729 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000730
731 static uint8_t First;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000732 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000733 static EhFrameHeader<ELFT> *EhFrameHdr;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000734 static EhOutputSection<ELFT> *EhFrame;
George Rimar58fa5242016-10-20 09:19:48 +0000735 static GdbIndexSection<ELFT> *GdbIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000736 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000737 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000738 static GotSection<ELFT> *Got;
739 static HashTableSection<ELFT> *HashTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000740 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000741 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000742 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000743 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000744 static PltSection<ELFT> *Plt;
745 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000746 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000747 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000748 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000749 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000750 static SymbolTableSection<ELFT> *DynSymTab;
751 static SymbolTableSection<ELFT> *SymTab;
George Rimard3566302016-06-20 11:55:12 +0000752 static VersionDefinitionSection<ELFT> *VerDef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000753 static VersionTableSection<ELFT> *VerSym;
754 static VersionNeedSection<ELFT> *VerNeed;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000755 static Elf_Phdr *TlsPhdr;
George Rimar58fa5242016-10-20 09:19:48 +0000756 static OutputSectionBase<ELFT> *DebugInfo;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000757 static OutputSectionBase<ELFT> *ElfHeader;
758 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000759 static OutputSectionBase<ELFT> *PreinitArray;
760 static OutputSectionBase<ELFT> *InitArray;
761 static OutputSectionBase<ELFT> *FiniArray;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000762};
Rui Ueyamad888d102015-10-09 19:34:55 +0000763
George Rimar6892afa2016-07-12 09:49:43 +0000764template <bool Is64Bits> struct SectionKey {
765 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
766 StringRef Name;
767 uint32_t Type;
768 uintX_t Flags;
769 uintX_t Alignment;
770};
771
772// This class knows how to create an output section for a given
773// input section. Output section type is determined by various
774// factors, including input section's sh_flags, sh_type and
775// linker scripts.
776template <class ELFT> class OutputSectionFactory {
777 typedef typename ELFT::Shdr Elf_Shdr;
778 typedef typename ELFT::uint uintX_t;
779 typedef typename elf::SectionKey<ELFT::Is64Bits> Key;
780
781public:
782 std::pair<OutputSectionBase<ELFT> *, bool> create(InputSectionBase<ELFT> *C,
783 StringRef OutsecName);
Rafael Espindola10897f12016-09-13 14:23:14 +0000784 std::pair<OutputSectionBase<ELFT> *, bool>
785 create(const SectionKey<ELFT::Is64Bits> &Key, InputSectionBase<ELFT> *C);
George Rimar6892afa2016-07-12 09:49:43 +0000786
George Rimar6892afa2016-07-12 09:49:43 +0000787private:
George Rimar6892afa2016-07-12 09:49:43 +0000788 llvm::SmallDenseMap<Key, OutputSectionBase<ELFT> *> Map;
789};
790
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000791template <class ELFT> uint64_t getHeaderSize() {
792 if (Config->OFormatBinary)
793 return 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000794 return Out<ELFT>::ElfHeader->Size + Out<ELFT>::ProgramHeaders->Size;
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000795}
796
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000797template <class ELFT> uint8_t Out<ELFT>::First;
Rui Ueyamad888d102015-10-09 19:34:55 +0000798template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000799template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000800template <class ELFT> EhOutputSection<ELFT> *Out<ELFT>::EhFrame;
George Rimar58fa5242016-10-20 09:19:48 +0000801template <class ELFT> GdbIndexSection<ELFT> *Out<ELFT>::GdbIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000802template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000803template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000804template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
805template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000806template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000807template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000808template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000809template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000810template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
811template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000812template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000813template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000814template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000815template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000816template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
817template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
George Rimard3566302016-06-20 11:55:12 +0000818template <class ELFT> VersionDefinitionSection<ELFT> *Out<ELFT>::VerDef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000819template <class ELFT> VersionTableSection<ELFT> *Out<ELFT>::VerSym;
820template <class ELFT> VersionNeedSection<ELFT> *Out<ELFT>::VerNeed;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000821template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
George Rimar58fa5242016-10-20 09:19:48 +0000822template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::DebugInfo;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000823template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
824template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000825template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::PreinitArray;
826template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::InitArray;
827template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::FiniArray;
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000828} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000829} // namespace lld
830
George Rimar6892afa2016-07-12 09:49:43 +0000831namespace llvm {
832template <bool Is64Bits> struct DenseMapInfo<lld::elf::SectionKey<Is64Bits>> {
833 typedef typename lld::elf::SectionKey<Is64Bits> Key;
834
835 static Key getEmptyKey();
836 static Key getTombstoneKey();
837 static unsigned getHashValue(const Key &Val);
838 static bool isEqual(const Key &LHS, const Key &RHS);
839};
840}
841
842#endif