blob: 5dd6c46c25b11087a6d26b3689a7a5ce6542ce60 [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"
Simon Atanasyan41325112016-06-19 21:39:37 +000014#include "Relocations.h"
Davide Italiano85121bb2015-09-25 03:56:11 +000015
Rui Ueyamaa0752a52016-03-13 20:28:29 +000016#include "lld/Core/LLVM.h"
Simon Atanasyand2980d32016-03-29 14:07:22 +000017#include "llvm/ADT/SmallPtrSet.h"
Rui Ueyamaa0752a52016-03-13 20:28:29 +000018#include "llvm/MC/StringTableBuilder.h"
19#include "llvm/Object/ELF.h"
Rui Ueyama3a41be22016-04-07 22:49:21 +000020#include "llvm/Support/MD5.h"
Rui Ueyamad86ec302016-04-07 23:51:56 +000021#include "llvm/Support/SHA1.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000022
23namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000024namespace elf {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000025
26class SymbolBody;
Rafael Espindola2deeb602016-07-21 20:18:30 +000027struct EhSectionPiece;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000028template <class ELFT> class SymbolTable;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000029template <class ELFT> class SymbolTableSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000030template <class ELFT> class StringTableSection;
Rui Ueyama0b9a9032016-05-24 04:19:20 +000031template <class ELFT> class EhInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000032template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000033template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000034template <class ELFT> class MergeInputSection;
Simon Atanasyan1d7df402015-12-20 10:57:34 +000035template <class ELFT> class MipsReginfoInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000036template <class ELFT> class OutputSection;
37template <class ELFT> class ObjectFile;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +000038template <class ELFT> class SharedFile;
39template <class ELFT> class SharedSymbol;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000040template <class ELFT> class DefinedRegular;
41
Rafael Espindola71675852015-09-22 00:16:19 +000042// This represents a section in an output file.
43// Different sub classes represent different types of sections. Some contain
44// input sections, others are created by the linker.
45// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000046// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000047template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000048public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +000049 typedef typename ELFT::uint uintX_t;
50 typedef typename ELFT::Shdr Elf_Shdr;
Eugene Leviant9d278b62016-08-10 18:10:41 +000051 enum Kind {
52 Base,
53 BuildId,
54 Dynamic,
55 EHFrame,
56 EHFrameHdr,
57 GnuHashTable,
58 Got,
59 GotPlt,
60 HashTable,
61 Interp,
62 Merge,
63 MipsReginfo,
64 MipsOptions,
Simon Atanasyan85c6b442016-08-12 06:28:49 +000065 MipsAbiFlags,
Eugene Leviant9d278b62016-08-10 18:10:41 +000066 Plt,
67 Regular,
68 Reloc,
69 StrTable,
70 SymTable,
71 VersDef,
72 VersNeed,
73 VersTable
74 };
Rafael Espindola5805c4f2015-09-21 21:38:08 +000075
George Rimar9bec24a2016-02-18 14:20:08 +000076 OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000077 void setVA(uintX_t VA) { Header.sh_addr = VA; }
78 uintX_t getVA() const { return Header.sh_addr; }
79 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Eugene Leviant3d9abec2016-09-29 09:20:33 +000080 uintX_t getFileOffset() { return Header.sh_offset; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000081 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000082 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000083 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000084
Rui Ueyama40845e62015-12-26 05:51:07 +000085 virtual void addSection(InputSectionBase<ELFT> *C) {}
Eugene Leviant9d278b62016-08-10 18:10:41 +000086 virtual Kind getKind() const { return Base; }
87 static bool classof(const OutputSectionBase<ELFT> *B) {
88 return B->getKind() == Base;
89 }
Rui Ueyama40845e62015-12-26 05:51:07 +000090
Rui Ueyama2317d0d2015-10-15 20:55:22 +000091 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000092
93 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000094 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000095 void setSize(uintX_t Val) { Header.sh_size = Val; }
Rafael Espindola571452c2016-04-11 13:44:05 +000096 uintX_t getFlags() const { return Header.sh_flags; }
Rafael Espindola10897f12016-09-13 14:23:14 +000097 void updateFlags(uintX_t Val) { Header.sh_flags |= Val; }
Rafael Espindola0b113672016-07-27 14:10:56 +000098 uint32_t getPhdrFlags() const;
Rafael Espindola571452c2016-04-11 13:44:05 +000099 uintX_t getFileOff() const { return Header.sh_offset; }
Rui Ueyama3b04d832016-07-14 05:46:24 +0000100 uintX_t getAlignment() const { return Header.sh_addralign; }
Rafael Espindola571452c2016-04-11 13:44:05 +0000101 uint32_t getType() const { return Header.sh_type; }
Rui Ueyama3b04d832016-07-14 05:46:24 +0000102
Rui Ueyama424b4082016-06-17 01:18:46 +0000103 void updateAlignment(uintX_t Alignment) {
104 if (Alignment > Header.sh_addralign)
105 Header.sh_addralign = Alignment;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000106 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000107
Rui Ueyama47091902016-03-30 19:41:51 +0000108 // If true, this section will be page aligned on disk.
109 // Typically the first section of each PT_LOAD segment has this flag.
110 bool PageAlign = false;
111
Eugene Leviant3d9abec2016-09-29 09:20:33 +0000112 // Pointer to the first section in PT_LOAD segment, which this section
113 // also resides in. This field is used to correctly compute file offset
114 // of a section. When two sections share the same load segment, difference
115 // between their file offsets should be equal to difference between their
116 // virtual addresses. To compute some section offset we use the following
117 // formula: Off = Off_first + VA - VA_first.
118 OutputSectionBase<ELFT> *FirstInPtLoad = nullptr;
119
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000120 virtual void finalize() {}
Rui Ueyama406b4692016-05-27 14:39:13 +0000121 virtual void finalizePieces() {}
Rui Ueyama809d8e22016-06-23 04:33:42 +0000122 virtual void assignOffsets() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +0000123 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +0000124 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000125
126protected:
127 StringRef Name;
Sean Silva580c1b62016-04-20 04:26:16 +0000128 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000129};
130
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000131template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
132 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000133 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000134
135public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000136 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000137 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000138 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000139 void addEntry(SymbolBody &Sym);
Simon Atanasyan41325112016-06-19 21:39:37 +0000140 void addMipsEntry(SymbolBody &Sym, uintX_t Addend, RelExpr Expr);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000141 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000142 bool addTlsIndex();
Simon Atanasyan41325112016-06-19 21:39:37 +0000143 bool empty() const { return MipsPageEntries == 0 && Entries.empty(); }
Rafael Espindola58cd5db2016-04-19 22:46:03 +0000144 uintX_t getMipsLocalPageOffset(uintX_t Addr);
Simon Atanasyan41325112016-06-19 21:39:37 +0000145 uintX_t getMipsGotOffset(const SymbolBody &B, uintX_t Addend) const;
George Rimar90cd0a82015-12-01 19:20:26 +0000146 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
Rafael Espindola74031ba2016-04-07 15:20:56 +0000147 uintX_t getGlobalDynOffset(const SymbolBody &B) const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000148 typename Base::Kind getKind() const override { return Base::Got; }
149 static bool classof(const Base *B) { return B->getKind() == Base::Got; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000150
Igor Kudrin304860a2015-11-12 04:39:49 +0000151 // Returns the symbol which corresponds to the first entry of the global part
152 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
153 // table properties.
154 // Returns nullptr if the global part is empty.
155 const SymbolBody *getMipsFirstGlobalEntry() const;
156
157 // Returns the number of entries in the local part of GOT including
158 // the number of reserved entries. This method is MIPS-specific.
159 unsigned getMipsLocalEntriesNum() const;
160
Simon Atanasyan002e2442016-06-23 15:26:31 +0000161 // Returns offset of TLS part of the MIPS GOT table. This part goes
162 // after 'local' and 'global' entries.
163 uintX_t getMipsTlsOffset();
164
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000165 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
Rafael Espindola74031ba2016-04-07 15:20:56 +0000166 uint32_t getTlsIndexOff() { return TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000167
Rui Ueyama022d8e82016-05-24 03:36:07 +0000168 // Flag to force GOT to be in output if we have relocations
169 // that relies on its address.
170 bool HasGotOffRel = false;
171
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000172private:
173 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000174 uint32_t TlsIndexOff = -1;
Simon Atanasyan41325112016-06-19 21:39:37 +0000175 uint32_t MipsPageEntries = 0;
Simon Atanasyand2980d32016-03-29 14:07:22 +0000176 // Output sections referenced by MIPS GOT relocations.
177 llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000178 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
Simon Atanasyan41325112016-06-19 21:39:37 +0000179
180 // MIPS ABI requires to create unique GOT entry for each Symbol/Addend
181 // pairs. The `MipsGotMap` maps (S,A) pair to the GOT index in the `MipsLocal`
182 // or `MipsGlobal` vectors. In general it does not have a sence to take in
183 // account addend for preemptible symbols because the corresponding
184 // GOT entries should have one-to-one mapping with dynamic symbols table.
185 // But we use the same container's types for both kind of GOT entries
186 // to handle them uniformly.
187 typedef std::pair<const SymbolBody*, uintX_t> MipsGotEntry;
188 typedef std::vector<MipsGotEntry> MipsGotEntries;
189 llvm::DenseMap<MipsGotEntry, size_t> MipsGotMap;
190 MipsGotEntries MipsLocal;
191 MipsGotEntries MipsGlobal;
192
193 // Write MIPS-specific parts of the GOT.
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000194 void writeMipsGot(uint8_t *Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000195};
196
George Rimar648a2c32015-10-20 08:54:27 +0000197template <class ELFT>
198class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000199 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000200 typedef OutputSectionBase<ELFT> Base;
George Rimar648a2c32015-10-20 08:54:27 +0000201
202public:
203 GotPltSection();
204 void finalize() override;
205 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000206 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000207 bool empty() const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000208 typename Base::Kind getKind() const override { return Base::GotPlt; }
209 static bool classof(const Base *B) { return B->getKind() == Base::GotPlt; }
George Rimar648a2c32015-10-20 08:54:27 +0000210
211private:
212 std::vector<const SymbolBody *> Entries;
213};
214
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000215template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
216 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000217 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000218
219public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000220 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000221 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000222 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000223 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000224 bool empty() const { return Entries.empty(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000225 typename Base::Kind getKind() const override { return Base::Plt; }
226 static bool classof(const Base *B) { return B->getKind() == Base::Plt; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000227
228private:
George Rimar77b77792015-11-25 22:15:01 +0000229 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000230};
231
Rui Ueyama809d8e22016-06-23 04:33:42 +0000232template <class ELFT> class DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000233 typedef typename ELFT::uint uintX_t;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000234
235public:
236 DynamicReloc(uint32_t Type, const InputSectionBase<ELFT> *InputSec,
237 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
238 uintX_t Addend)
239 : Type(Type), Sym(Sym), InputSec(InputSec), OffsetInSec(OffsetInSec),
240 UseSymVA(UseSymVA), Addend(Addend) {}
241
242 DynamicReloc(uint32_t Type, const OutputSectionBase<ELFT> *OutputSec,
243 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
244 uintX_t Addend)
245 : Type(Type), Sym(Sym), OutputSec(OutputSec), OffsetInSec(OffsetInSec),
246 UseSymVA(UseSymVA), Addend(Addend) {}
247
248 uintX_t getOffset() const;
249 uintX_t getAddend() const;
250 uint32_t getSymIndex() const;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000251 const OutputSectionBase<ELFT> *getOutputSec() const { return OutputSec; }
Rui Ueyama809d8e22016-06-23 04:33:42 +0000252
Rafael Espindolade9857e2016-02-04 21:33:05 +0000253 uint32_t Type;
254
Rui Ueyama809d8e22016-06-23 04:33:42 +0000255private:
Rafael Espindolaac568f92016-04-11 13:47:35 +0000256 SymbolBody *Sym;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000257 const InputSectionBase<ELFT> *InputSec = nullptr;
258 const OutputSectionBase<ELFT> *OutputSec = nullptr;
Rafael Espindolaac568f92016-04-11 13:47:35 +0000259 uintX_t OffsetInSec;
260 bool UseSymVA;
261 uintX_t Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000262};
263
264template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000265class SymbolTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000266 typedef OutputSectionBase<ELFT> Base;
267
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000268public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000269 typedef typename ELFT::Shdr Elf_Shdr;
270 typedef typename ELFT::Sym Elf_Sym;
271 typedef typename ELFT::SymRange Elf_Sym_Range;
272 typedef typename ELFT::uint uintX_t;
Rui Ueyamaace4f902016-05-24 04:25:47 +0000273 SymbolTableSection(StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000274
Rui Ueyama0db335f2015-10-07 16:58:54 +0000275 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000276 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000277 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000278 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000279 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000280 typename Base::Kind getKind() const override { return Base::SymTable; }
281 static bool classof(const Base *B) { return B->getKind() == Base::SymTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000282
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000283 ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
Rafael Espindolae2c24612016-01-29 01:24:25 +0000284 return Symbols;
285 }
286
287 unsigned NumLocals = 0;
288 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000289
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000290private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000291 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000292 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000293
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000294 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000295
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000296 // A vector of symbols and their string table offsets.
297 std::vector<std::pair<SymbolBody *, size_t>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000298};
299
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000300// For more information about .gnu.version and .gnu.version_r see:
301// https://www.akkadia.org/drepper/symbol-versioning
302
George Rimard3566302016-06-20 11:55:12 +0000303// The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
304// contain symbol version definitions. The number of entries in this section
305// shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
306// The section shall contain an array of Elf_Verdef structures, optionally
307// followed by an array of Elf_Verdaux structures.
308template <class ELFT>
309class VersionDefinitionSection final : public OutputSectionBase<ELFT> {
310 typedef typename ELFT::Verdef Elf_Verdef;
311 typedef typename ELFT::Verdaux Elf_Verdaux;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000312 typedef OutputSectionBase<ELFT> Base;
George Rimard3566302016-06-20 11:55:12 +0000313
George Rimard3566302016-06-20 11:55:12 +0000314public:
315 VersionDefinitionSection();
316 void finalize() override;
317 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000318 typename Base::Kind getKind() const override { return Base::VersDef; }
319 static bool classof(const Base *B) { return B->getKind() == Base::VersDef; }
Rui Ueyama9f619642016-07-16 02:29:45 +0000320
321private:
322 void writeOne(uint8_t *Buf, uint32_t Index, StringRef Name, size_t NameOff);
323
324 unsigned FileDefNameOff;
George Rimard3566302016-06-20 11:55:12 +0000325};
326
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000327// The .gnu.version section specifies the required version of each symbol in the
328// dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
329// table entry. An Elf_Versym is just a 16-bit integer that refers to a version
George Rimard3566302016-06-20 11:55:12 +0000330// identifier defined in the either .gnu.version_r or .gnu.version_d section.
331// The values 0 and 1 are reserved. All other values are used for versions in
332// the own object or in any of the dependencies.
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000333template <class ELFT>
334class VersionTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000335 typedef OutputSectionBase<ELFT> Base;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000336 typedef typename ELFT::Versym Elf_Versym;
337
338public:
339 VersionTableSection();
340 void finalize() override;
341 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000342 typename Base::Kind getKind() const override { return Base::VersTable; }
343 static bool classof(const Base *B) { return B->getKind() == Base::VersTable; }
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000344};
345
346// The .gnu.version_r section defines the version identifiers used by
347// .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
348// Elf_Verneed specifies the version requirements for a single DSO, and contains
349// a reference to a linked list of Elf_Vernaux data structures which define the
350// mapping from version identifiers to version names.
351template <class ELFT>
352class VersionNeedSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000353 typedef OutputSectionBase<ELFT> Base;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000354 typedef typename ELFT::Verneed Elf_Verneed;
355 typedef typename ELFT::Vernaux Elf_Vernaux;
356
357 // A vector of shared files that need Elf_Verneed data structures and the
358 // string table offsets of their sonames.
359 std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed;
360
George Rimard3566302016-06-20 11:55:12 +0000361 // The next available version identifier.
362 unsigned NextIndex;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000363
364public:
365 VersionNeedSection();
366 void addSymbol(SharedSymbol<ELFT> *SS);
367 void finalize() override;
368 void writeTo(uint8_t *Buf) override;
369 size_t getNeedNum() const { return Needed.size(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000370 typename Base::Kind getKind() const override { return Base::VersNeed; }
371 static bool classof(const Base *B) { return B->getKind() == Base::VersNeed; }
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000372};
373
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000374template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000375class RelocationSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000376 typedef typename ELFT::Rel Elf_Rel;
377 typedef typename ELFT::Rela Elf_Rela;
378 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000379 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000380
381public:
George Rimarc191acf2016-05-10 15:47:57 +0000382 RelocationSection(StringRef Name, bool Sort);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000383 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000384 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000385 void finalize() override;
386 void writeTo(uint8_t *Buf) override;
387 bool hasRelocs() const { return !Relocs.empty(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000388 typename Base::Kind getKind() const override { return Base::Reloc; }
Eugene Leviantaa498192016-08-31 08:51:39 +0000389 size_t getRelativeRelocCount() const { return NumRelativeRelocs; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000390 static bool classof(const Base *B) { return B->getKind() == Base::Reloc; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000391
392private:
George Rimarc191acf2016-05-10 15:47:57 +0000393 bool Sort;
Eugene Leviantaa498192016-08-31 08:51:39 +0000394 size_t NumRelativeRelocs = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000395 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000396};
397
398template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000399class OutputSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000400 typedef OutputSectionBase<ELFT> Base;
401
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000402public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000403 typedef typename ELFT::Shdr Elf_Shdr;
404 typedef typename ELFT::Sym Elf_Sym;
405 typedef typename ELFT::Rel Elf_Rel;
406 typedef typename ELFT::Rela Elf_Rela;
407 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000408 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000409 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000410 void sortInitFini();
411 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000412 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000413 void finalize() override;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000414 void assignOffsets() override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000415 typename Base::Kind getKind() const override { return Base::Regular; }
416 static bool classof(const Base *B) { return B->getKind() == Base::Regular; }
Rafael Espindola71675852015-09-22 00:16:19 +0000417 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000418};
419
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000420template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000421class MergeOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000422 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000423 typedef OutputSectionBase<ELFT> Base;
Rafael Espindolac159c962015-10-19 21:00:02 +0000424
425public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000426 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
427 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000428 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000429 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000430 unsigned getOffset(StringRef Val);
431 void finalize() override;
Rui Ueyama406b4692016-05-27 14:39:13 +0000432 void finalizePieces() override;
Peter Collingbournee29e1422016-05-05 04:10:12 +0000433 bool shouldTailMerge() const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000434 typename Base::Kind getKind() const override { return Base::Merge; }
435 static bool classof(const Base *B) { return B->getKind() == Base::Merge; }
Rafael Espindolac159c962015-10-19 21:00:02 +0000436
437private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000438 llvm::StringTableBuilder Builder;
Rui Ueyama406b4692016-05-27 14:39:13 +0000439 std::vector<MergeInputSection<ELFT> *> Sections;
Rafael Espindolac159c962015-10-19 21:00:02 +0000440};
441
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000442struct CieRecord {
Rafael Espindola2deeb602016-07-21 20:18:30 +0000443 EhSectionPiece *Piece = nullptr;
444 std::vector<EhSectionPiece *> FdePieces;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000445};
446
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000447// Output section for .eh_frame.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000448template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +0000449class EhOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000450 typedef typename ELFT::uint uintX_t;
451 typedef typename ELFT::Shdr Elf_Shdr;
452 typedef typename ELFT::Rel Elf_Rel;
453 typedef typename ELFT::Rela Elf_Rela;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000454 typedef OutputSectionBase<ELFT> Base;
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000455
456public:
457 EhOutputSection();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000458 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000459 void finalize() override;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000460 bool empty() const { return Sections.empty(); }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000461
Rui Ueyama40845e62015-12-26 05:51:07 +0000462 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000463 typename Base::Kind getKind() const override { return Base::EHFrame; }
464 static bool classof(const Base *B) { return B->getKind() == Base::EHFrame; }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000465
Rui Ueyamade9777a2016-05-23 16:30:41 +0000466 size_t NumFdes = 0;
467
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000468private:
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000469 template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000470 void addSectionAux(EhInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000471
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000472 template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000473 CieRecord *addCie(EhSectionPiece &Piece, EhInputSection<ELFT> *Sec,
474 ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000475
476 template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000477 bool isFdeLive(EhSectionPiece &Piece, EhInputSection<ELFT> *Sec,
478 ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000479
Rui Ueyamae75e9332016-05-23 03:00:33 +0000480 uintX_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc);
481
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000482 std::vector<EhInputSection<ELFT> *> Sections;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000483 std::vector<CieRecord *> Cies;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000484
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000485 // CIE records are uniquified by their contents and personality functions.
486 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, SymbolBody *>, CieRecord> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000487};
488
Rafael Espindolac159c962015-10-19 21:00:02 +0000489template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000490class InterpSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000491 typedef OutputSectionBase<ELFT> Base;
492
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000493public:
494 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000495 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000496 typename Base::Kind getKind() const override { return Base::Interp; }
497 static bool classof(const Base *B) { return B->getKind() == Base::Interp; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000498};
499
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000500template <class ELFT>
501class StringTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000502 typedef OutputSectionBase<ELFT> Base;
503
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000504public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000505 typedef typename ELFT::uint uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000506 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000507 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000508 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000509 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000510 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000511 bool isDynamic() const { return Dynamic; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000512 typename Base::Kind getKind() const override { return Base::StrTable; }
513 static bool classof(const Base *B) { return B->getKind() == Base::StrTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000514
515private:
516 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000517 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000518 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000519 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000520};
521
522template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000523class HashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000524 typedef typename ELFT::Word Elf_Word;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000525 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000526
527public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000528 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000529 void finalize() override;
530 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000531 typename Base::Kind getKind() const override { return Base::HashTable; }
532 static bool classof(const Base *B) { return B->getKind() == Base::HashTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000533};
534
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000535// Outputs GNU Hash section. For detailed explanation see:
536// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
537template <class ELFT>
538class GnuHashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000539 typedef typename ELFT::Off Elf_Off;
540 typedef typename ELFT::Word Elf_Word;
541 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000542 typedef OutputSectionBase<ELFT> Base;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000543
544public:
545 GnuHashTableSection();
546 void finalize() override;
547 void writeTo(uint8_t *Buf) override;
548
Igor Kudrinf1d60292015-10-28 07:05:56 +0000549 // Adds symbols to the hash table.
550 // Sorts the input to satisfy GNU hash section requirements.
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000551 void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols);
Eugene Leviant9d278b62016-08-10 18:10:41 +0000552 typename Base::Kind getKind() const override { return Base::GnuHashTable; }
553 static bool classof(const Base *B) {
554 return B->getKind() == Base::GnuHashTable;
555 }
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000556
557private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000558 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000559 static unsigned calcMaskWords(unsigned NumHashed);
560
561 void writeHeader(uint8_t *&Buf);
562 void writeBloomFilter(uint8_t *&Buf);
563 void writeHashTable(uint8_t *Buf);
564
Rui Ueyama861c7312016-02-17 05:40:03 +0000565 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000566 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000567 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000568 uint32_t Hash;
569 };
570
Rui Ueyama861c7312016-02-17 05:40:03 +0000571 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000572
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000573 unsigned MaskWords;
574 unsigned NBuckets;
575 unsigned Shift2;
576};
577
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000578template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000579class DynamicSection final : public OutputSectionBase<ELFT> {
580 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000581 typedef typename ELFT::Dyn Elf_Dyn;
582 typedef typename ELFT::Rel Elf_Rel;
583 typedef typename ELFT::Rela Elf_Rela;
584 typedef typename ELFT::Shdr Elf_Shdr;
585 typedef typename ELFT::Sym Elf_Sym;
586 typedef typename ELFT::uint uintX_t;
Rafael Espindolade069362016-01-25 21:32:04 +0000587
Rui Ueyama909cc682016-02-02 03:11:27 +0000588 // The .dynamic section contains information for the dynamic linker.
589 // The section consists of fixed size entries, which consist of
590 // type and value fields. Value are one of plain integers, symbol
591 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000592 struct Entry {
593 int32_t Tag;
594 union {
595 OutputSectionBase<ELFT> *OutSec;
596 uint64_t Val;
597 const SymbolBody *Sym;
598 };
George Rimar03e05602016-08-19 15:23:39 +0000599 enum KindT { SecAddr, SecSize, SymAddr, PlainInt } Kind;
600 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec, KindT Kind = SecAddr)
601 : Tag(Tag), OutSec(OutSec), Kind(Kind) {}
Rafael Espindolade069362016-01-25 21:32:04 +0000602 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
603 Entry(int32_t Tag, const SymbolBody *Sym)
604 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
605 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000606
607 // finalize() fills this vector with the section contents. finalize()
608 // cannot directly create final section contents because when the
609 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000610 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000611
612public:
Rui Ueyamaace4f902016-05-24 04:25:47 +0000613 explicit DynamicSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000614 void finalize() override;
615 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000616 typename Base::Kind getKind() const override { return Base::Dynamic; }
617 static bool classof(const Base *B) { return B->getKind() == Base::Dynamic; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000618};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000619
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000620template <class ELFT>
621class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
622 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000623 typedef OutputSectionBase<ELFT> Base;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000624
625public:
626 MipsReginfoOutputSection();
627 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000628 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000629 typename Base::Kind getKind() const override { return Base::MipsReginfo; }
630 static bool classof(const Base *B) {
631 return B->getKind() == Base::MipsReginfo;
632 }
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000633
634private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000635 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000636};
637
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000638template <class ELFT>
639class MipsOptionsOutputSection final : public OutputSectionBase<ELFT> {
640 typedef llvm::object::Elf_Mips_Options<ELFT> Elf_Mips_Options;
641 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000642 typedef OutputSectionBase<ELFT> Base;
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000643
644public:
645 MipsOptionsOutputSection();
646 void writeTo(uint8_t *Buf) override;
647 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000648 typename Base::Kind getKind() const override { return Base::MipsOptions; }
649 static bool classof(const Base *B) {
650 return B->getKind() == Base::MipsOptions;
651 }
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000652
653private:
654 uint32_t GprMask = 0;
655};
656
Simon Atanasyan85c6b442016-08-12 06:28:49 +0000657template <class ELFT>
658class MipsAbiFlagsOutputSection final : public OutputSectionBase<ELFT> {
659 typedef llvm::object::Elf_Mips_ABIFlags<ELFT> Elf_Mips_ABIFlags;
660 typedef OutputSectionBase<ELFT> Base;
661
662public:
663 MipsAbiFlagsOutputSection();
664 void writeTo(uint8_t *Buf) override;
665 void addSection(InputSectionBase<ELFT> *S) override;
666 typename Base::Kind getKind() const override { return Base::MipsAbiFlags; }
667 static bool classof(const Base *B) {
668 return B->getKind() == Base::MipsAbiFlags;
669 }
670
671private:
672 Elf_Mips_ABIFlags Flags;
673};
674
George Rimarf6bc65a2016-01-15 13:34:52 +0000675// --eh-frame-hdr option tells linker to construct a header for all the
676// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
677// and also to a PT_GNU_EH_FRAME segment.
678// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
679// calling dl_iterate_phdr.
680// This section contains a lookup table for quick binary search of FDEs.
681// Detailed info about internals can be found in Ian Lance Taylor's blog:
682// http://www.airs.com/blog/archives/460 (".eh_frame")
683// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
684template <class ELFT>
685class EhFrameHeader final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000686 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000687 typedef OutputSectionBase<ELFT> Base;
George Rimarf6bc65a2016-01-15 13:34:52 +0000688
689public:
690 EhFrameHeader();
Rui Ueyamade9777a2016-05-23 16:30:41 +0000691 void finalize() override;
George Rimarf6bc65a2016-01-15 13:34:52 +0000692 void writeTo(uint8_t *Buf) override;
Rui Ueyamae75e9332016-05-23 03:00:33 +0000693 void addFde(uint32_t Pc, uint32_t FdeVA);
Eugene Leviant9d278b62016-08-10 18:10:41 +0000694 typename Base::Kind getKind() const override { return Base::EHFrameHdr; }
695 static bool classof(const Base *B) {
696 return B->getKind() == Base::EHFrameHdr;
697 }
George Rimarf6bc65a2016-01-15 13:34:52 +0000698
George Rimarf6bc65a2016-01-15 13:34:52 +0000699private:
700 struct FdeData {
Rui Ueyamae75e9332016-05-23 03:00:33 +0000701 uint32_t Pc;
702 uint32_t FdeVA;
George Rimarf6bc65a2016-01-15 13:34:52 +0000703 };
704
Rui Ueyamae75e9332016-05-23 03:00:33 +0000705 std::vector<FdeData> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000706};
707
Rui Ueyama3a41be22016-04-07 22:49:21 +0000708template <class ELFT> class BuildIdSection : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000709 typedef OutputSectionBase<ELFT> Base;
710
Rui Ueyama634ddf02016-03-11 20:51:53 +0000711public:
Rui Ueyama634ddf02016-03-11 20:51:53 +0000712 void writeTo(uint8_t *Buf) override;
Petr Hosekfdfcb792016-09-01 22:43:03 +0000713 virtual void writeBuildId(ArrayRef<uint8_t> Buf) = 0;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000714 typename Base::Kind getKind() const override { return Base::BuildId; }
715 static bool classof(const Base *B) { return B->getKind() == Base::BuildId; }
Rui Ueyama3a41be22016-04-07 22:49:21 +0000716
717protected:
718 BuildIdSection(size_t HashSize);
719 size_t HashSize;
720 uint8_t *HashBuf = nullptr;
721};
722
Rafael Espindolad88d7162016-09-14 11:32:57 +0000723template <class ELFT>
724class BuildIdFastHash final : public BuildIdSection<ELFT> {
Rui Ueyama3a41be22016-04-07 22:49:21 +0000725public:
Rafael Espindolad88d7162016-09-14 11:32:57 +0000726 BuildIdFastHash() : BuildIdSection<ELFT>(8) {}
Petr Hosekfdfcb792016-09-01 22:43:03 +0000727 void writeBuildId(ArrayRef<uint8_t> Buf) override;
Rui Ueyama3a41be22016-04-07 22:49:21 +0000728};
729
730template <class ELFT> class BuildIdMd5 final : public BuildIdSection<ELFT> {
731public:
732 BuildIdMd5() : BuildIdSection<ELFT>(16) {}
Petr Hosekfdfcb792016-09-01 22:43:03 +0000733 void writeBuildId(ArrayRef<uint8_t> Buf) override;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000734};
735
Rui Ueyamad86ec302016-04-07 23:51:56 +0000736template <class ELFT> class BuildIdSha1 final : public BuildIdSection<ELFT> {
737public:
738 BuildIdSha1() : BuildIdSection<ELFT>(20) {}
Petr Hosekfdfcb792016-09-01 22:43:03 +0000739 void writeBuildId(ArrayRef<uint8_t> Buf) override;
Rui Ueyamad86ec302016-04-07 23:51:56 +0000740};
741
Eugene Leviant933dae72016-08-26 09:55:37 +0000742template <class ELFT> class BuildIdUuid final : public BuildIdSection<ELFT> {
743public:
744 BuildIdUuid() : BuildIdSection<ELFT>(16) {}
Petr Hosekfdfcb792016-09-01 22:43:03 +0000745 void writeBuildId(ArrayRef<uint8_t> Buf) override;
Eugene Leviant933dae72016-08-26 09:55:37 +0000746};
747
Rui Ueyama9194db72016-05-13 21:55:56 +0000748template <class ELFT>
749class BuildIdHexstring final : public BuildIdSection<ELFT> {
750public:
751 BuildIdHexstring();
Petr Hosekfdfcb792016-09-01 22:43:03 +0000752 void writeBuildId(ArrayRef<uint8_t>) override;
Rui Ueyama9194db72016-05-13 21:55:56 +0000753};
754
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000755// All output sections that are hadnled by the linker specially are
756// globally accessible. Writer initializes them, so don't use them
757// until Writer is initialized.
758template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000759 typedef typename ELFT::uint uintX_t;
760 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000761 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000762 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000763 static EhFrameHeader<ELFT> *EhFrameHdr;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000764 static EhOutputSection<ELFT> *EhFrame;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000765 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000766 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000767 static GotSection<ELFT> *Got;
768 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000769 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000770 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000771 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000772 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000773 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000774 static PltSection<ELFT> *Plt;
775 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000776 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000777 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000778 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000779 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000780 static SymbolTableSection<ELFT> *DynSymTab;
781 static SymbolTableSection<ELFT> *SymTab;
George Rimard3566302016-06-20 11:55:12 +0000782 static VersionDefinitionSection<ELFT> *VerDef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000783 static VersionTableSection<ELFT> *VerSym;
784 static VersionNeedSection<ELFT> *VerNeed;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000785 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000786 static OutputSectionBase<ELFT> *ElfHeader;
787 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000788
789 static OutputSectionBase<ELFT> *PreinitArray;
790 static OutputSectionBase<ELFT> *InitArray;
791 static OutputSectionBase<ELFT> *FiniArray;
Rui Ueyamabdf836d2016-08-12 01:10:17 +0000792
793 // This pool owns dynamically-allocated output sections.
794 static std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> Pool;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000795};
Rui Ueyamad888d102015-10-09 19:34:55 +0000796
George Rimar6892afa2016-07-12 09:49:43 +0000797template <bool Is64Bits> struct SectionKey {
798 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
799 StringRef Name;
800 uint32_t Type;
801 uintX_t Flags;
802 uintX_t Alignment;
803};
804
805// This class knows how to create an output section for a given
806// input section. Output section type is determined by various
807// factors, including input section's sh_flags, sh_type and
808// linker scripts.
809template <class ELFT> class OutputSectionFactory {
810 typedef typename ELFT::Shdr Elf_Shdr;
811 typedef typename ELFT::uint uintX_t;
812 typedef typename elf::SectionKey<ELFT::Is64Bits> Key;
813
814public:
815 std::pair<OutputSectionBase<ELFT> *, bool> create(InputSectionBase<ELFT> *C,
816 StringRef OutsecName);
Rafael Espindola10897f12016-09-13 14:23:14 +0000817 std::pair<OutputSectionBase<ELFT> *, bool>
818 create(const SectionKey<ELFT::Is64Bits> &Key, InputSectionBase<ELFT> *C);
George Rimar6892afa2016-07-12 09:49:43 +0000819
George Rimar6892afa2016-07-12 09:49:43 +0000820private:
George Rimar6892afa2016-07-12 09:49:43 +0000821 llvm::SmallDenseMap<Key, OutputSectionBase<ELFT> *> Map;
822};
823
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000824template <class ELFT> uint64_t getHeaderSize() {
825 if (Config->OFormatBinary)
826 return 0;
827 return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
828}
829
Rui Ueyama634ddf02016-03-11 20:51:53 +0000830template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000831template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000832template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000833template <class ELFT> EhOutputSection<ELFT> *Out<ELFT>::EhFrame;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000834template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000835template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000836template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
837template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000838template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000839template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000840template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000841template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000842template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000843template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
844template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000845template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000846template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000847template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000848template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000849template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
850template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
George Rimard3566302016-06-20 11:55:12 +0000851template <class ELFT> VersionDefinitionSection<ELFT> *Out<ELFT>::VerDef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000852template <class ELFT> VersionTableSection<ELFT> *Out<ELFT>::VerSym;
853template <class ELFT> VersionNeedSection<ELFT> *Out<ELFT>::VerNeed;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000854template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000855template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
856template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000857template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::PreinitArray;
858template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::InitArray;
859template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::FiniArray;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000860
Rui Ueyamabdf836d2016-08-12 01:10:17 +0000861template <class ELFT>
862std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> Out<ELFT>::Pool;
863
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000864} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000865} // namespace lld
866
George Rimar6892afa2016-07-12 09:49:43 +0000867namespace llvm {
868template <bool Is64Bits> struct DenseMapInfo<lld::elf::SectionKey<Is64Bits>> {
869 typedef typename lld::elf::SectionKey<Is64Bits> Key;
870
871 static Key getEmptyKey();
872 static Key getTombstoneKey();
873 static unsigned getHashValue(const Key &Val);
874 static bool isEqual(const Key &LHS, const Key &RHS);
875};
876}
877
878#endif