blob: e1634eed3360ee4401577f849863c6db0ff3b4ba [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; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000080 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000081 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000082 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000083
Rui Ueyama40845e62015-12-26 05:51:07 +000084 virtual void addSection(InputSectionBase<ELFT> *C) {}
Eugene Leviant9d278b62016-08-10 18:10:41 +000085 virtual Kind getKind() const { return Base; }
86 static bool classof(const OutputSectionBase<ELFT> *B) {
87 return B->getKind() == Base;
88 }
Rui Ueyama40845e62015-12-26 05:51:07 +000089
Rui Ueyama2317d0d2015-10-15 20:55:22 +000090 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000091
92 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000093 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000094 void setSize(uintX_t Val) { Header.sh_size = Val; }
Rafael Espindola571452c2016-04-11 13:44:05 +000095 uintX_t getFlags() const { return Header.sh_flags; }
Rafael Espindola10897f12016-09-13 14:23:14 +000096 void updateFlags(uintX_t Val) { Header.sh_flags |= Val; }
Rafael Espindola0b113672016-07-27 14:10:56 +000097 uint32_t getPhdrFlags() const;
Rafael Espindola571452c2016-04-11 13:44:05 +000098 uintX_t getFileOff() const { return Header.sh_offset; }
Rui Ueyama3b04d832016-07-14 05:46:24 +000099 uintX_t getAlignment() const { return Header.sh_addralign; }
Rafael Espindola571452c2016-04-11 13:44:05 +0000100 uint32_t getType() const { return Header.sh_type; }
Rui Ueyama3b04d832016-07-14 05:46:24 +0000101
Rui Ueyama424b4082016-06-17 01:18:46 +0000102 void updateAlignment(uintX_t Alignment) {
103 if (Alignment > Header.sh_addralign)
104 Header.sh_addralign = Alignment;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000105 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000106
Rui Ueyama47091902016-03-30 19:41:51 +0000107 // If true, this section will be page aligned on disk.
108 // Typically the first section of each PT_LOAD segment has this flag.
109 bool PageAlign = false;
110
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000111 virtual void finalize() {}
Rui Ueyama406b4692016-05-27 14:39:13 +0000112 virtual void finalizePieces() {}
Rui Ueyama809d8e22016-06-23 04:33:42 +0000113 virtual void assignOffsets() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +0000114 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +0000115 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000116
117protected:
118 StringRef Name;
Sean Silva580c1b62016-04-20 04:26:16 +0000119 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000120};
121
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000122template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
123 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000124 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000125
126public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000127 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000128 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000129 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000130 void addEntry(SymbolBody &Sym);
Simon Atanasyan41325112016-06-19 21:39:37 +0000131 void addMipsEntry(SymbolBody &Sym, uintX_t Addend, RelExpr Expr);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000132 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000133 bool addTlsIndex();
Simon Atanasyan41325112016-06-19 21:39:37 +0000134 bool empty() const { return MipsPageEntries == 0 && Entries.empty(); }
Rafael Espindola58cd5db2016-04-19 22:46:03 +0000135 uintX_t getMipsLocalPageOffset(uintX_t Addr);
Simon Atanasyan41325112016-06-19 21:39:37 +0000136 uintX_t getMipsGotOffset(const SymbolBody &B, uintX_t Addend) const;
George Rimar90cd0a82015-12-01 19:20:26 +0000137 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
Rafael Espindola74031ba2016-04-07 15:20:56 +0000138 uintX_t getGlobalDynOffset(const SymbolBody &B) const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000139 typename Base::Kind getKind() const override { return Base::Got; }
140 static bool classof(const Base *B) { return B->getKind() == Base::Got; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000141
Igor Kudrin304860a2015-11-12 04:39:49 +0000142 // Returns the symbol which corresponds to the first entry of the global part
143 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
144 // table properties.
145 // Returns nullptr if the global part is empty.
146 const SymbolBody *getMipsFirstGlobalEntry() const;
147
148 // Returns the number of entries in the local part of GOT including
149 // the number of reserved entries. This method is MIPS-specific.
150 unsigned getMipsLocalEntriesNum() const;
151
Simon Atanasyan002e2442016-06-23 15:26:31 +0000152 // Returns offset of TLS part of the MIPS GOT table. This part goes
153 // after 'local' and 'global' entries.
154 uintX_t getMipsTlsOffset();
155
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000156 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
Rafael Espindola74031ba2016-04-07 15:20:56 +0000157 uint32_t getTlsIndexOff() { return TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000158
Rui Ueyama022d8e82016-05-24 03:36:07 +0000159 // Flag to force GOT to be in output if we have relocations
160 // that relies on its address.
161 bool HasGotOffRel = false;
162
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000163private:
164 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000165 uint32_t TlsIndexOff = -1;
Simon Atanasyan41325112016-06-19 21:39:37 +0000166 uint32_t MipsPageEntries = 0;
Simon Atanasyand2980d32016-03-29 14:07:22 +0000167 // Output sections referenced by MIPS GOT relocations.
168 llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000169 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
Simon Atanasyan41325112016-06-19 21:39:37 +0000170
171 // MIPS ABI requires to create unique GOT entry for each Symbol/Addend
172 // pairs. The `MipsGotMap` maps (S,A) pair to the GOT index in the `MipsLocal`
173 // or `MipsGlobal` vectors. In general it does not have a sence to take in
174 // account addend for preemptible symbols because the corresponding
175 // GOT entries should have one-to-one mapping with dynamic symbols table.
176 // But we use the same container's types for both kind of GOT entries
177 // to handle them uniformly.
178 typedef std::pair<const SymbolBody*, uintX_t> MipsGotEntry;
179 typedef std::vector<MipsGotEntry> MipsGotEntries;
180 llvm::DenseMap<MipsGotEntry, size_t> MipsGotMap;
181 MipsGotEntries MipsLocal;
182 MipsGotEntries MipsGlobal;
183
184 // Write MIPS-specific parts of the GOT.
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000185 void writeMipsGot(uint8_t *Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000186};
187
George Rimar648a2c32015-10-20 08:54:27 +0000188template <class ELFT>
189class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000190 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000191 typedef OutputSectionBase<ELFT> Base;
George Rimar648a2c32015-10-20 08:54:27 +0000192
193public:
194 GotPltSection();
195 void finalize() override;
196 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000197 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000198 bool empty() const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000199 typename Base::Kind getKind() const override { return Base::GotPlt; }
200 static bool classof(const Base *B) { return B->getKind() == Base::GotPlt; }
George Rimar648a2c32015-10-20 08:54:27 +0000201
202private:
203 std::vector<const SymbolBody *> Entries;
204};
205
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000206template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
207 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000208 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000209
210public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000211 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000212 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000213 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000214 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000215 bool empty() const { return Entries.empty(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000216 typename Base::Kind getKind() const override { return Base::Plt; }
217 static bool classof(const Base *B) { return B->getKind() == Base::Plt; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000218
219private:
George Rimar77b77792015-11-25 22:15:01 +0000220 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000221};
222
Rui Ueyama809d8e22016-06-23 04:33:42 +0000223template <class ELFT> class DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000224 typedef typename ELFT::uint uintX_t;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000225
226public:
227 DynamicReloc(uint32_t Type, const InputSectionBase<ELFT> *InputSec,
228 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
229 uintX_t Addend)
230 : Type(Type), Sym(Sym), InputSec(InputSec), OffsetInSec(OffsetInSec),
231 UseSymVA(UseSymVA), Addend(Addend) {}
232
233 DynamicReloc(uint32_t Type, const OutputSectionBase<ELFT> *OutputSec,
234 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
235 uintX_t Addend)
236 : Type(Type), Sym(Sym), OutputSec(OutputSec), OffsetInSec(OffsetInSec),
237 UseSymVA(UseSymVA), Addend(Addend) {}
238
239 uintX_t getOffset() const;
240 uintX_t getAddend() const;
241 uint32_t getSymIndex() const;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000242 const OutputSectionBase<ELFT> *getOutputSec() const { return OutputSec; }
Rui Ueyama809d8e22016-06-23 04:33:42 +0000243
Rafael Espindolade9857e2016-02-04 21:33:05 +0000244 uint32_t Type;
245
Rui Ueyama809d8e22016-06-23 04:33:42 +0000246private:
Rafael Espindolaac568f92016-04-11 13:47:35 +0000247 SymbolBody *Sym;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000248 const InputSectionBase<ELFT> *InputSec = nullptr;
249 const OutputSectionBase<ELFT> *OutputSec = nullptr;
Rafael Espindolaac568f92016-04-11 13:47:35 +0000250 uintX_t OffsetInSec;
251 bool UseSymVA;
252 uintX_t Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000253};
254
255template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000256class SymbolTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000257 typedef OutputSectionBase<ELFT> Base;
258
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000259public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000260 typedef typename ELFT::Shdr Elf_Shdr;
261 typedef typename ELFT::Sym Elf_Sym;
262 typedef typename ELFT::SymRange Elf_Sym_Range;
263 typedef typename ELFT::uint uintX_t;
Rui Ueyamaace4f902016-05-24 04:25:47 +0000264 SymbolTableSection(StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000265
Rui Ueyama0db335f2015-10-07 16:58:54 +0000266 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000267 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000268 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000269 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000270 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000271 typename Base::Kind getKind() const override { return Base::SymTable; }
272 static bool classof(const Base *B) { return B->getKind() == Base::SymTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000273
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000274 ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
Rafael Espindolae2c24612016-01-29 01:24:25 +0000275 return Symbols;
276 }
277
278 unsigned NumLocals = 0;
279 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000280
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000281private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000282 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000283 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000284
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000285 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000286
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000287 // A vector of symbols and their string table offsets.
288 std::vector<std::pair<SymbolBody *, size_t>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000289};
290
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000291// For more information about .gnu.version and .gnu.version_r see:
292// https://www.akkadia.org/drepper/symbol-versioning
293
George Rimard3566302016-06-20 11:55:12 +0000294// The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
295// contain symbol version definitions. The number of entries in this section
296// shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
297// The section shall contain an array of Elf_Verdef structures, optionally
298// followed by an array of Elf_Verdaux structures.
299template <class ELFT>
300class VersionDefinitionSection final : public OutputSectionBase<ELFT> {
301 typedef typename ELFT::Verdef Elf_Verdef;
302 typedef typename ELFT::Verdaux Elf_Verdaux;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000303 typedef OutputSectionBase<ELFT> Base;
George Rimard3566302016-06-20 11:55:12 +0000304
George Rimard3566302016-06-20 11:55:12 +0000305public:
306 VersionDefinitionSection();
307 void finalize() override;
308 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000309 typename Base::Kind getKind() const override { return Base::VersDef; }
310 static bool classof(const Base *B) { return B->getKind() == Base::VersDef; }
Rui Ueyama9f619642016-07-16 02:29:45 +0000311
312private:
313 void writeOne(uint8_t *Buf, uint32_t Index, StringRef Name, size_t NameOff);
314
315 unsigned FileDefNameOff;
George Rimard3566302016-06-20 11:55:12 +0000316};
317
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000318// The .gnu.version section specifies the required version of each symbol in the
319// dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
320// table entry. An Elf_Versym is just a 16-bit integer that refers to a version
George Rimard3566302016-06-20 11:55:12 +0000321// identifier defined in the either .gnu.version_r or .gnu.version_d section.
322// The values 0 and 1 are reserved. All other values are used for versions in
323// the own object or in any of the dependencies.
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000324template <class ELFT>
325class VersionTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000326 typedef OutputSectionBase<ELFT> Base;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000327 typedef typename ELFT::Versym Elf_Versym;
328
329public:
330 VersionTableSection();
331 void finalize() override;
332 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000333 typename Base::Kind getKind() const override { return Base::VersTable; }
334 static bool classof(const Base *B) { return B->getKind() == Base::VersTable; }
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000335};
336
337// The .gnu.version_r section defines the version identifiers used by
338// .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
339// Elf_Verneed specifies the version requirements for a single DSO, and contains
340// a reference to a linked list of Elf_Vernaux data structures which define the
341// mapping from version identifiers to version names.
342template <class ELFT>
343class VersionNeedSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000344 typedef OutputSectionBase<ELFT> Base;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000345 typedef typename ELFT::Verneed Elf_Verneed;
346 typedef typename ELFT::Vernaux Elf_Vernaux;
347
348 // A vector of shared files that need Elf_Verneed data structures and the
349 // string table offsets of their sonames.
350 std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed;
351
George Rimard3566302016-06-20 11:55:12 +0000352 // The next available version identifier.
353 unsigned NextIndex;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000354
355public:
356 VersionNeedSection();
357 void addSymbol(SharedSymbol<ELFT> *SS);
358 void finalize() override;
359 void writeTo(uint8_t *Buf) override;
360 size_t getNeedNum() const { return Needed.size(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000361 typename Base::Kind getKind() const override { return Base::VersNeed; }
362 static bool classof(const Base *B) { return B->getKind() == Base::VersNeed; }
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000363};
364
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000365template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000366class RelocationSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000367 typedef typename ELFT::Rel Elf_Rel;
368 typedef typename ELFT::Rela Elf_Rela;
369 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000370 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000371
372public:
George Rimarc191acf2016-05-10 15:47:57 +0000373 RelocationSection(StringRef Name, bool Sort);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000374 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000375 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000376 void finalize() override;
377 void writeTo(uint8_t *Buf) override;
378 bool hasRelocs() const { return !Relocs.empty(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000379 typename Base::Kind getKind() const override { return Base::Reloc; }
Eugene Leviantaa498192016-08-31 08:51:39 +0000380 size_t getRelativeRelocCount() const { return NumRelativeRelocs; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000381 static bool classof(const Base *B) { return B->getKind() == Base::Reloc; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000382
383private:
George Rimarc191acf2016-05-10 15:47:57 +0000384 bool Sort;
Eugene Leviantaa498192016-08-31 08:51:39 +0000385 size_t NumRelativeRelocs = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000386 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000387};
388
389template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000390class OutputSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000391 typedef OutputSectionBase<ELFT> Base;
392
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000393public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000394 typedef typename ELFT::Shdr Elf_Shdr;
395 typedef typename ELFT::Sym Elf_Sym;
396 typedef typename ELFT::Rel Elf_Rel;
397 typedef typename ELFT::Rela Elf_Rela;
398 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000399 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000400 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000401 void sortInitFini();
402 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000403 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000404 void finalize() override;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000405 void assignOffsets() override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000406 typename Base::Kind getKind() const override { return Base::Regular; }
407 static bool classof(const Base *B) { return B->getKind() == Base::Regular; }
Rafael Espindola71675852015-09-22 00:16:19 +0000408 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000409};
410
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000411template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000412class MergeOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000413 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000414 typedef OutputSectionBase<ELFT> Base;
Rafael Espindolac159c962015-10-19 21:00:02 +0000415
416public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000417 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
418 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000419 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000420 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000421 unsigned getOffset(StringRef Val);
422 void finalize() override;
Rui Ueyama406b4692016-05-27 14:39:13 +0000423 void finalizePieces() override;
Peter Collingbournee29e1422016-05-05 04:10:12 +0000424 bool shouldTailMerge() const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000425 typename Base::Kind getKind() const override { return Base::Merge; }
426 static bool classof(const Base *B) { return B->getKind() == Base::Merge; }
Rafael Espindolac159c962015-10-19 21:00:02 +0000427
428private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000429 llvm::StringTableBuilder Builder;
Rui Ueyama406b4692016-05-27 14:39:13 +0000430 std::vector<MergeInputSection<ELFT> *> Sections;
Rafael Espindolac159c962015-10-19 21:00:02 +0000431};
432
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000433struct CieRecord {
Rafael Espindola2deeb602016-07-21 20:18:30 +0000434 EhSectionPiece *Piece = nullptr;
435 std::vector<EhSectionPiece *> FdePieces;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000436};
437
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000438// Output section for .eh_frame.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000439template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +0000440class EhOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000441 typedef typename ELFT::uint uintX_t;
442 typedef typename ELFT::Shdr Elf_Shdr;
443 typedef typename ELFT::Rel Elf_Rel;
444 typedef typename ELFT::Rela Elf_Rela;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000445 typedef OutputSectionBase<ELFT> Base;
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000446
447public:
448 EhOutputSection();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000449 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000450 void finalize() override;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000451 bool empty() const { return Sections.empty(); }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000452
Rui Ueyama40845e62015-12-26 05:51:07 +0000453 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000454 typename Base::Kind getKind() const override { return Base::EHFrame; }
455 static bool classof(const Base *B) { return B->getKind() == Base::EHFrame; }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000456
Rui Ueyamade9777a2016-05-23 16:30:41 +0000457 size_t NumFdes = 0;
458
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000459private:
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000460 template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000461 void addSectionAux(EhInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000462
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000463 template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000464 CieRecord *addCie(EhSectionPiece &Piece, EhInputSection<ELFT> *Sec,
465 ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000466
467 template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000468 bool isFdeLive(EhSectionPiece &Piece, EhInputSection<ELFT> *Sec,
469 ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000470
Rui Ueyamae75e9332016-05-23 03:00:33 +0000471 uintX_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc);
472
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000473 std::vector<EhInputSection<ELFT> *> Sections;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000474 std::vector<CieRecord *> Cies;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000475
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000476 // CIE records are uniquified by their contents and personality functions.
477 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, SymbolBody *>, CieRecord> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000478};
479
Rafael Espindolac159c962015-10-19 21:00:02 +0000480template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000481class InterpSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000482 typedef OutputSectionBase<ELFT> Base;
483
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000484public:
485 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000486 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000487 typename Base::Kind getKind() const override { return Base::Interp; }
488 static bool classof(const Base *B) { return B->getKind() == Base::Interp; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000489};
490
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000491template <class ELFT>
492class StringTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000493 typedef OutputSectionBase<ELFT> Base;
494
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000495public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000496 typedef typename ELFT::uint uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000497 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000498 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000499 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000500 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000501 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000502 bool isDynamic() const { return Dynamic; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000503 typename Base::Kind getKind() const override { return Base::StrTable; }
504 static bool classof(const Base *B) { return B->getKind() == Base::StrTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000505
506private:
507 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000508 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000509 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000510 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000511};
512
513template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000514class HashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000515 typedef typename ELFT::Word Elf_Word;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000516 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000517
518public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000519 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000520 void finalize() override;
521 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000522 typename Base::Kind getKind() const override { return Base::HashTable; }
523 static bool classof(const Base *B) { return B->getKind() == Base::HashTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000524};
525
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000526// Outputs GNU Hash section. For detailed explanation see:
527// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
528template <class ELFT>
529class GnuHashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000530 typedef typename ELFT::Off Elf_Off;
531 typedef typename ELFT::Word Elf_Word;
532 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000533 typedef OutputSectionBase<ELFT> Base;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000534
535public:
536 GnuHashTableSection();
537 void finalize() override;
538 void writeTo(uint8_t *Buf) override;
539
Igor Kudrinf1d60292015-10-28 07:05:56 +0000540 // Adds symbols to the hash table.
541 // Sorts the input to satisfy GNU hash section requirements.
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000542 void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols);
Eugene Leviant9d278b62016-08-10 18:10:41 +0000543 typename Base::Kind getKind() const override { return Base::GnuHashTable; }
544 static bool classof(const Base *B) {
545 return B->getKind() == Base::GnuHashTable;
546 }
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000547
548private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000549 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000550 static unsigned calcMaskWords(unsigned NumHashed);
551
552 void writeHeader(uint8_t *&Buf);
553 void writeBloomFilter(uint8_t *&Buf);
554 void writeHashTable(uint8_t *Buf);
555
Rui Ueyama861c7312016-02-17 05:40:03 +0000556 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000557 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000558 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000559 uint32_t Hash;
560 };
561
Rui Ueyama861c7312016-02-17 05:40:03 +0000562 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000563
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000564 unsigned MaskWords;
565 unsigned NBuckets;
566 unsigned Shift2;
567};
568
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000569template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000570class DynamicSection final : public OutputSectionBase<ELFT> {
571 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000572 typedef typename ELFT::Dyn Elf_Dyn;
573 typedef typename ELFT::Rel Elf_Rel;
574 typedef typename ELFT::Rela Elf_Rela;
575 typedef typename ELFT::Shdr Elf_Shdr;
576 typedef typename ELFT::Sym Elf_Sym;
577 typedef typename ELFT::uint uintX_t;
Rafael Espindolade069362016-01-25 21:32:04 +0000578
Rui Ueyama909cc682016-02-02 03:11:27 +0000579 // The .dynamic section contains information for the dynamic linker.
580 // The section consists of fixed size entries, which consist of
581 // type and value fields. Value are one of plain integers, symbol
582 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000583 struct Entry {
584 int32_t Tag;
585 union {
586 OutputSectionBase<ELFT> *OutSec;
587 uint64_t Val;
588 const SymbolBody *Sym;
589 };
George Rimar03e05602016-08-19 15:23:39 +0000590 enum KindT { SecAddr, SecSize, SymAddr, PlainInt } Kind;
591 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec, KindT Kind = SecAddr)
592 : Tag(Tag), OutSec(OutSec), Kind(Kind) {}
Rafael Espindolade069362016-01-25 21:32:04 +0000593 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
594 Entry(int32_t Tag, const SymbolBody *Sym)
595 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
596 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000597
598 // finalize() fills this vector with the section contents. finalize()
599 // cannot directly create final section contents because when the
600 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000601 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000602
603public:
Rui Ueyamaace4f902016-05-24 04:25:47 +0000604 explicit DynamicSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000605 void finalize() override;
606 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000607 typename Base::Kind getKind() const override { return Base::Dynamic; }
608 static bool classof(const Base *B) { return B->getKind() == Base::Dynamic; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000609};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000610
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000611template <class ELFT>
612class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
613 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000614 typedef OutputSectionBase<ELFT> Base;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000615
616public:
617 MipsReginfoOutputSection();
618 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000619 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000620 typename Base::Kind getKind() const override { return Base::MipsReginfo; }
621 static bool classof(const Base *B) {
622 return B->getKind() == Base::MipsReginfo;
623 }
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000624
625private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000626 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000627};
628
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000629template <class ELFT>
630class MipsOptionsOutputSection final : public OutputSectionBase<ELFT> {
631 typedef llvm::object::Elf_Mips_Options<ELFT> Elf_Mips_Options;
632 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000633 typedef OutputSectionBase<ELFT> Base;
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000634
635public:
636 MipsOptionsOutputSection();
637 void writeTo(uint8_t *Buf) override;
638 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000639 typename Base::Kind getKind() const override { return Base::MipsOptions; }
640 static bool classof(const Base *B) {
641 return B->getKind() == Base::MipsOptions;
642 }
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000643
644private:
645 uint32_t GprMask = 0;
646};
647
Simon Atanasyan85c6b442016-08-12 06:28:49 +0000648template <class ELFT>
649class MipsAbiFlagsOutputSection final : public OutputSectionBase<ELFT> {
650 typedef llvm::object::Elf_Mips_ABIFlags<ELFT> Elf_Mips_ABIFlags;
651 typedef OutputSectionBase<ELFT> Base;
652
653public:
654 MipsAbiFlagsOutputSection();
655 void writeTo(uint8_t *Buf) override;
656 void addSection(InputSectionBase<ELFT> *S) override;
657 typename Base::Kind getKind() const override { return Base::MipsAbiFlags; }
658 static bool classof(const Base *B) {
659 return B->getKind() == Base::MipsAbiFlags;
660 }
661
662private:
663 Elf_Mips_ABIFlags Flags;
664};
665
George Rimarf6bc65a2016-01-15 13:34:52 +0000666// --eh-frame-hdr option tells linker to construct a header for all the
667// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
668// and also to a PT_GNU_EH_FRAME segment.
669// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
670// calling dl_iterate_phdr.
671// This section contains a lookup table for quick binary search of FDEs.
672// Detailed info about internals can be found in Ian Lance Taylor's blog:
673// http://www.airs.com/blog/archives/460 (".eh_frame")
674// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
675template <class ELFT>
676class EhFrameHeader final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000677 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000678 typedef OutputSectionBase<ELFT> Base;
George Rimarf6bc65a2016-01-15 13:34:52 +0000679
680public:
681 EhFrameHeader();
Rui Ueyamade9777a2016-05-23 16:30:41 +0000682 void finalize() override;
George Rimarf6bc65a2016-01-15 13:34:52 +0000683 void writeTo(uint8_t *Buf) override;
Rui Ueyamae75e9332016-05-23 03:00:33 +0000684 void addFde(uint32_t Pc, uint32_t FdeVA);
Eugene Leviant9d278b62016-08-10 18:10:41 +0000685 typename Base::Kind getKind() const override { return Base::EHFrameHdr; }
686 static bool classof(const Base *B) {
687 return B->getKind() == Base::EHFrameHdr;
688 }
George Rimarf6bc65a2016-01-15 13:34:52 +0000689
George Rimarf6bc65a2016-01-15 13:34:52 +0000690private:
691 struct FdeData {
Rui Ueyamae75e9332016-05-23 03:00:33 +0000692 uint32_t Pc;
693 uint32_t FdeVA;
George Rimarf6bc65a2016-01-15 13:34:52 +0000694 };
695
Rui Ueyamae75e9332016-05-23 03:00:33 +0000696 std::vector<FdeData> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000697};
698
Rui Ueyama3a41be22016-04-07 22:49:21 +0000699template <class ELFT> class BuildIdSection : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000700 typedef OutputSectionBase<ELFT> Base;
701
Rui Ueyama634ddf02016-03-11 20:51:53 +0000702public:
Rui Ueyama634ddf02016-03-11 20:51:53 +0000703 void writeTo(uint8_t *Buf) override;
Petr Hosekfdfcb792016-09-01 22:43:03 +0000704 virtual void writeBuildId(ArrayRef<uint8_t> Buf) = 0;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000705 typename Base::Kind getKind() const override { return Base::BuildId; }
706 static bool classof(const Base *B) { return B->getKind() == Base::BuildId; }
Rui Ueyama3a41be22016-04-07 22:49:21 +0000707
708protected:
709 BuildIdSection(size_t HashSize);
710 size_t HashSize;
711 uint8_t *HashBuf = nullptr;
712};
713
714template <class ELFT> class BuildIdFnv1 final : public BuildIdSection<ELFT> {
715public:
716 BuildIdFnv1() : BuildIdSection<ELFT>(8) {}
Petr Hosekfdfcb792016-09-01 22:43:03 +0000717 void writeBuildId(ArrayRef<uint8_t> Buf) override;
Rui Ueyama3a41be22016-04-07 22:49:21 +0000718};
719
720template <class ELFT> class BuildIdMd5 final : public BuildIdSection<ELFT> {
721public:
722 BuildIdMd5() : BuildIdSection<ELFT>(16) {}
Petr Hosekfdfcb792016-09-01 22:43:03 +0000723 void writeBuildId(ArrayRef<uint8_t> Buf) override;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000724};
725
Rui Ueyamad86ec302016-04-07 23:51:56 +0000726template <class ELFT> class BuildIdSha1 final : public BuildIdSection<ELFT> {
727public:
728 BuildIdSha1() : BuildIdSection<ELFT>(20) {}
Petr Hosekfdfcb792016-09-01 22:43:03 +0000729 void writeBuildId(ArrayRef<uint8_t> Buf) override;
Rui Ueyamad86ec302016-04-07 23:51:56 +0000730};
731
Eugene Leviant933dae72016-08-26 09:55:37 +0000732template <class ELFT> class BuildIdUuid final : public BuildIdSection<ELFT> {
733public:
734 BuildIdUuid() : BuildIdSection<ELFT>(16) {}
Petr Hosekfdfcb792016-09-01 22:43:03 +0000735 void writeBuildId(ArrayRef<uint8_t> Buf) override;
Eugene Leviant933dae72016-08-26 09:55:37 +0000736};
737
Rui Ueyama9194db72016-05-13 21:55:56 +0000738template <class ELFT>
739class BuildIdHexstring final : public BuildIdSection<ELFT> {
740public:
741 BuildIdHexstring();
Petr Hosekfdfcb792016-09-01 22:43:03 +0000742 void writeBuildId(ArrayRef<uint8_t>) override;
Rui Ueyama9194db72016-05-13 21:55:56 +0000743};
744
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000745// All output sections that are hadnled by the linker specially are
746// globally accessible. Writer initializes them, so don't use them
747// until Writer is initialized.
748template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000749 typedef typename ELFT::uint uintX_t;
750 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000751 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000752 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000753 static EhFrameHeader<ELFT> *EhFrameHdr;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000754 static EhOutputSection<ELFT> *EhFrame;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000755 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000756 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000757 static GotSection<ELFT> *Got;
758 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000759 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000760 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000761 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000762 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000763 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000764 static PltSection<ELFT> *Plt;
765 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000766 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000767 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000768 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000769 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000770 static SymbolTableSection<ELFT> *DynSymTab;
771 static SymbolTableSection<ELFT> *SymTab;
George Rimard3566302016-06-20 11:55:12 +0000772 static VersionDefinitionSection<ELFT> *VerDef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000773 static VersionTableSection<ELFT> *VerSym;
774 static VersionNeedSection<ELFT> *VerNeed;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000775 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000776 static OutputSectionBase<ELFT> *ElfHeader;
777 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000778
779 static OutputSectionBase<ELFT> *PreinitArray;
780 static OutputSectionBase<ELFT> *InitArray;
781 static OutputSectionBase<ELFT> *FiniArray;
Rui Ueyamabdf836d2016-08-12 01:10:17 +0000782
783 // This pool owns dynamically-allocated output sections.
784 static std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> Pool;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000785};
Rui Ueyamad888d102015-10-09 19:34:55 +0000786
George Rimar6892afa2016-07-12 09:49:43 +0000787template <bool Is64Bits> struct SectionKey {
788 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
789 StringRef Name;
790 uint32_t Type;
791 uintX_t Flags;
792 uintX_t Alignment;
793};
794
795// This class knows how to create an output section for a given
796// input section. Output section type is determined by various
797// factors, including input section's sh_flags, sh_type and
798// linker scripts.
799template <class ELFT> class OutputSectionFactory {
800 typedef typename ELFT::Shdr Elf_Shdr;
801 typedef typename ELFT::uint uintX_t;
802 typedef typename elf::SectionKey<ELFT::Is64Bits> Key;
803
804public:
805 std::pair<OutputSectionBase<ELFT> *, bool> create(InputSectionBase<ELFT> *C,
806 StringRef OutsecName);
Rafael Espindola10897f12016-09-13 14:23:14 +0000807 std::pair<OutputSectionBase<ELFT> *, bool>
808 create(const SectionKey<ELFT::Is64Bits> &Key, InputSectionBase<ELFT> *C);
George Rimar6892afa2016-07-12 09:49:43 +0000809
George Rimar6892afa2016-07-12 09:49:43 +0000810private:
George Rimar6892afa2016-07-12 09:49:43 +0000811 llvm::SmallDenseMap<Key, OutputSectionBase<ELFT> *> Map;
812};
813
Rui Ueyama634ddf02016-03-11 20:51:53 +0000814template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000815template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000816template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000817template <class ELFT> EhOutputSection<ELFT> *Out<ELFT>::EhFrame;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000818template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000819template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000820template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
821template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000822template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000823template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000824template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000825template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000826template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000827template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
828template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000829template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000830template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000831template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000832template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000833template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
834template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
George Rimard3566302016-06-20 11:55:12 +0000835template <class ELFT> VersionDefinitionSection<ELFT> *Out<ELFT>::VerDef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000836template <class ELFT> VersionTableSection<ELFT> *Out<ELFT>::VerSym;
837template <class ELFT> VersionNeedSection<ELFT> *Out<ELFT>::VerNeed;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000838template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000839template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
840template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000841template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::PreinitArray;
842template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::InitArray;
843template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::FiniArray;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000844
Rui Ueyamabdf836d2016-08-12 01:10:17 +0000845template <class ELFT>
846std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> Out<ELFT>::Pool;
847
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000848} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000849} // namespace lld
850
George Rimar6892afa2016-07-12 09:49:43 +0000851namespace llvm {
852template <bool Is64Bits> struct DenseMapInfo<lld::elf::SectionKey<Is64Bits>> {
853 typedef typename lld::elf::SectionKey<Is64Bits> Key;
854
855 static Key getEmptyKey();
856 static Key getTombstoneKey();
857 static unsigned getHashValue(const Key &Val);
858 static bool isEqual(const Key &LHS, const Key &RHS);
859};
860}
861
862#endif