blob: 662ef06d707073a76654cf6d93df0e10baf3731a [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; }
Eugene Leviantb71d6f72016-10-06 09:39:28 +000079 void setLMAOffset(uintX_t LMAOff) { LMAOffset = LMAOff; }
80 uintX_t getLMA() const { return Header.sh_addr + LMAOffset; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000081 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Eugene Leviant3d9abec2016-09-29 09:20:33 +000082 uintX_t getFileOffset() { return Header.sh_offset; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000083 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000084 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000085 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000086
Rui Ueyama40845e62015-12-26 05:51:07 +000087 virtual void addSection(InputSectionBase<ELFT> *C) {}
Eugene Leviant9d278b62016-08-10 18:10:41 +000088 virtual Kind getKind() const { return Base; }
89 static bool classof(const OutputSectionBase<ELFT> *B) {
90 return B->getKind() == Base;
91 }
Rui Ueyama40845e62015-12-26 05:51:07 +000092
Rui Ueyama2317d0d2015-10-15 20:55:22 +000093 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000094
95 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000096 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000097 void setSize(uintX_t Val) { Header.sh_size = Val; }
Rafael Espindola571452c2016-04-11 13:44:05 +000098 uintX_t getFlags() const { return Header.sh_flags; }
Rafael Espindola10897f12016-09-13 14:23:14 +000099 void updateFlags(uintX_t Val) { Header.sh_flags |= Val; }
Rafael Espindola0b113672016-07-27 14:10:56 +0000100 uint32_t getPhdrFlags() const;
Rafael Espindola571452c2016-04-11 13:44:05 +0000101 uintX_t getFileOff() const { return Header.sh_offset; }
Rui Ueyama3b04d832016-07-14 05:46:24 +0000102 uintX_t getAlignment() const { return Header.sh_addralign; }
Rafael Espindola571452c2016-04-11 13:44:05 +0000103 uint32_t getType() const { return Header.sh_type; }
Rui Ueyama3b04d832016-07-14 05:46:24 +0000104
Rui Ueyama424b4082016-06-17 01:18:46 +0000105 void updateAlignment(uintX_t Alignment) {
106 if (Alignment > Header.sh_addralign)
107 Header.sh_addralign = Alignment;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000108 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000109
Rui Ueyama47091902016-03-30 19:41:51 +0000110 // If true, this section will be page aligned on disk.
111 // Typically the first section of each PT_LOAD segment has this flag.
112 bool PageAlign = false;
113
Eugene Leviant3d9abec2016-09-29 09:20:33 +0000114 // Pointer to the first section in PT_LOAD segment, which this section
115 // also resides in. This field is used to correctly compute file offset
116 // of a section. When two sections share the same load segment, difference
117 // between their file offsets should be equal to difference between their
118 // virtual addresses. To compute some section offset we use the following
119 // formula: Off = Off_first + VA - VA_first.
120 OutputSectionBase<ELFT> *FirstInPtLoad = nullptr;
121
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000122 virtual void finalize() {}
Rui Ueyama406b4692016-05-27 14:39:13 +0000123 virtual void finalizePieces() {}
Rui Ueyama809d8e22016-06-23 04:33:42 +0000124 virtual void assignOffsets() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +0000125 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +0000126 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000127
128protected:
129 StringRef Name;
Sean Silva580c1b62016-04-20 04:26:16 +0000130 Elf_Shdr Header;
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000131 uintX_t LMAOffset = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000132};
133
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000134template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
135 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000136 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000137
138public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000139 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000140 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000141 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000142 void addEntry(SymbolBody &Sym);
Simon Atanasyan41325112016-06-19 21:39:37 +0000143 void addMipsEntry(SymbolBody &Sym, uintX_t Addend, RelExpr Expr);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000144 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000145 bool addTlsIndex();
Simon Atanasyan41325112016-06-19 21:39:37 +0000146 bool empty() const { return MipsPageEntries == 0 && Entries.empty(); }
Rafael Espindola58cd5db2016-04-19 22:46:03 +0000147 uintX_t getMipsLocalPageOffset(uintX_t Addr);
Simon Atanasyan41325112016-06-19 21:39:37 +0000148 uintX_t getMipsGotOffset(const SymbolBody &B, uintX_t Addend) const;
George Rimar90cd0a82015-12-01 19:20:26 +0000149 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
Rafael Espindola74031ba2016-04-07 15:20:56 +0000150 uintX_t getGlobalDynOffset(const SymbolBody &B) const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000151 typename Base::Kind getKind() const override { return Base::Got; }
152 static bool classof(const Base *B) { return B->getKind() == Base::Got; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000153
Igor Kudrin304860a2015-11-12 04:39:49 +0000154 // Returns the symbol which corresponds to the first entry of the global part
155 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
156 // table properties.
157 // Returns nullptr if the global part is empty.
158 const SymbolBody *getMipsFirstGlobalEntry() const;
159
160 // Returns the number of entries in the local part of GOT including
161 // the number of reserved entries. This method is MIPS-specific.
162 unsigned getMipsLocalEntriesNum() const;
163
Simon Atanasyan002e2442016-06-23 15:26:31 +0000164 // Returns offset of TLS part of the MIPS GOT table. This part goes
165 // after 'local' and 'global' entries.
Simon Atanasyanbc946932016-10-19 17:13:43 +0000166 uintX_t getMipsTlsOffset() const;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000167
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000168 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
Simon Atanasyanbc946932016-10-19 17:13:43 +0000169 uint32_t getTlsIndexOff() const { return TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000170
Rui Ueyama022d8e82016-05-24 03:36:07 +0000171 // Flag to force GOT to be in output if we have relocations
172 // that relies on its address.
173 bool HasGotOffRel = false;
174
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000175private:
176 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000177 uint32_t TlsIndexOff = -1;
Simon Atanasyan41325112016-06-19 21:39:37 +0000178 uint32_t MipsPageEntries = 0;
Simon Atanasyand2980d32016-03-29 14:07:22 +0000179 // Output sections referenced by MIPS GOT relocations.
180 llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000181 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
Simon Atanasyan41325112016-06-19 21:39:37 +0000182
183 // MIPS ABI requires to create unique GOT entry for each Symbol/Addend
184 // pairs. The `MipsGotMap` maps (S,A) pair to the GOT index in the `MipsLocal`
185 // or `MipsGlobal` vectors. In general it does not have a sence to take in
186 // account addend for preemptible symbols because the corresponding
187 // GOT entries should have one-to-one mapping with dynamic symbols table.
188 // But we use the same container's types for both kind of GOT entries
189 // to handle them uniformly.
George Rimara4c7e742016-10-20 08:36:42 +0000190 typedef std::pair<const SymbolBody *, uintX_t> MipsGotEntry;
Simon Atanasyan41325112016-06-19 21:39:37 +0000191 typedef std::vector<MipsGotEntry> MipsGotEntries;
192 llvm::DenseMap<MipsGotEntry, size_t> MipsGotMap;
193 MipsGotEntries MipsLocal;
194 MipsGotEntries MipsGlobal;
195
196 // Write MIPS-specific parts of the GOT.
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000197 void writeMipsGot(uint8_t *Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000198};
199
George Rimar648a2c32015-10-20 08:54:27 +0000200template <class ELFT>
201class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000202 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000203 typedef OutputSectionBase<ELFT> Base;
George Rimar648a2c32015-10-20 08:54:27 +0000204
205public:
206 GotPltSection();
207 void finalize() override;
208 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000209 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000210 bool empty() const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000211 typename Base::Kind getKind() const override { return Base::GotPlt; }
212 static bool classof(const Base *B) { return B->getKind() == Base::GotPlt; }
George Rimar648a2c32015-10-20 08:54:27 +0000213
214private:
215 std::vector<const SymbolBody *> Entries;
216};
217
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000218template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
219 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000220 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000221
222public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000223 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000224 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000225 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000226 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000227 bool empty() const { return Entries.empty(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000228 typename Base::Kind getKind() const override { return Base::Plt; }
229 static bool classof(const Base *B) { return B->getKind() == Base::Plt; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000230
231private:
George Rimar77b77792015-11-25 22:15:01 +0000232 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000233};
234
Rui Ueyama809d8e22016-06-23 04:33:42 +0000235template <class ELFT> class DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000236 typedef typename ELFT::uint uintX_t;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000237
238public:
239 DynamicReloc(uint32_t Type, const InputSectionBase<ELFT> *InputSec,
240 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
241 uintX_t Addend)
242 : Type(Type), Sym(Sym), InputSec(InputSec), OffsetInSec(OffsetInSec),
243 UseSymVA(UseSymVA), Addend(Addend) {}
244
245 DynamicReloc(uint32_t Type, const OutputSectionBase<ELFT> *OutputSec,
246 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
247 uintX_t Addend)
248 : Type(Type), Sym(Sym), OutputSec(OutputSec), OffsetInSec(OffsetInSec),
249 UseSymVA(UseSymVA), Addend(Addend) {}
250
251 uintX_t getOffset() const;
252 uintX_t getAddend() const;
253 uint32_t getSymIndex() const;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000254 const OutputSectionBase<ELFT> *getOutputSec() const { return OutputSec; }
Rui Ueyama809d8e22016-06-23 04:33:42 +0000255
Rafael Espindolade9857e2016-02-04 21:33:05 +0000256 uint32_t Type;
257
Rui Ueyama809d8e22016-06-23 04:33:42 +0000258private:
Rafael Espindolaac568f92016-04-11 13:47:35 +0000259 SymbolBody *Sym;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000260 const InputSectionBase<ELFT> *InputSec = nullptr;
261 const OutputSectionBase<ELFT> *OutputSec = nullptr;
Rafael Espindolaac568f92016-04-11 13:47:35 +0000262 uintX_t OffsetInSec;
263 bool UseSymVA;
264 uintX_t Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000265};
266
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000267struct SymbolTableEntry {
268 SymbolBody *Symbol;
Reid Kleckner2918d0b2016-10-20 00:13:34 +0000269 size_t StrTabOffset;
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000270};
271
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000272template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000273class SymbolTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000274 typedef OutputSectionBase<ELFT> Base;
275
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000276public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000277 typedef typename ELFT::Shdr Elf_Shdr;
278 typedef typename ELFT::Sym Elf_Sym;
279 typedef typename ELFT::SymRange Elf_Sym_Range;
280 typedef typename ELFT::uint uintX_t;
Rui Ueyamaace4f902016-05-24 04:25:47 +0000281 SymbolTableSection(StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000282
Rui Ueyama0db335f2015-10-07 16:58:54 +0000283 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000284 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000285 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000286 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000287 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000288 typename Base::Kind getKind() const override { return Base::SymTable; }
289 static bool classof(const Base *B) { return B->getKind() == Base::SymTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000290
George Rimara4c7e742016-10-20 08:36:42 +0000291 ArrayRef<SymbolTableEntry> getSymbols() const { return Symbols; }
Rafael Espindolae2c24612016-01-29 01:24:25 +0000292
293 unsigned NumLocals = 0;
294 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000295
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000296private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000297 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000298 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000299
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000300 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000301
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000302 // A vector of symbols and their string table offsets.
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000303 std::vector<SymbolTableEntry> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000304};
305
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000306// For more information about .gnu.version and .gnu.version_r see:
307// https://www.akkadia.org/drepper/symbol-versioning
308
George Rimard3566302016-06-20 11:55:12 +0000309// The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
310// contain symbol version definitions. The number of entries in this section
311// shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
312// The section shall contain an array of Elf_Verdef structures, optionally
313// followed by an array of Elf_Verdaux structures.
314template <class ELFT>
315class VersionDefinitionSection final : public OutputSectionBase<ELFT> {
316 typedef typename ELFT::Verdef Elf_Verdef;
317 typedef typename ELFT::Verdaux Elf_Verdaux;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000318 typedef OutputSectionBase<ELFT> Base;
George Rimard3566302016-06-20 11:55:12 +0000319
George Rimard3566302016-06-20 11:55:12 +0000320public:
321 VersionDefinitionSection();
322 void finalize() override;
323 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000324 typename Base::Kind getKind() const override { return Base::VersDef; }
325 static bool classof(const Base *B) { return B->getKind() == Base::VersDef; }
Rui Ueyama9f619642016-07-16 02:29:45 +0000326
327private:
328 void writeOne(uint8_t *Buf, uint32_t Index, StringRef Name, size_t NameOff);
329
330 unsigned FileDefNameOff;
George Rimard3566302016-06-20 11:55:12 +0000331};
332
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000333// The .gnu.version section specifies the required version of each symbol in the
334// dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
335// table entry. An Elf_Versym is just a 16-bit integer that refers to a version
George Rimard3566302016-06-20 11:55:12 +0000336// identifier defined in the either .gnu.version_r or .gnu.version_d section.
337// The values 0 and 1 are reserved. All other values are used for versions in
338// the own object or in any of the dependencies.
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000339template <class ELFT>
340class VersionTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000341 typedef OutputSectionBase<ELFT> Base;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000342 typedef typename ELFT::Versym Elf_Versym;
343
344public:
345 VersionTableSection();
346 void finalize() override;
347 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000348 typename Base::Kind getKind() const override { return Base::VersTable; }
349 static bool classof(const Base *B) { return B->getKind() == Base::VersTable; }
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000350};
351
352// The .gnu.version_r section defines the version identifiers used by
353// .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
354// Elf_Verneed specifies the version requirements for a single DSO, and contains
355// a reference to a linked list of Elf_Vernaux data structures which define the
356// mapping from version identifiers to version names.
357template <class ELFT>
358class VersionNeedSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000359 typedef OutputSectionBase<ELFT> Base;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000360 typedef typename ELFT::Verneed Elf_Verneed;
361 typedef typename ELFT::Vernaux Elf_Vernaux;
362
363 // A vector of shared files that need Elf_Verneed data structures and the
364 // string table offsets of their sonames.
365 std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed;
366
George Rimard3566302016-06-20 11:55:12 +0000367 // The next available version identifier.
368 unsigned NextIndex;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000369
370public:
371 VersionNeedSection();
372 void addSymbol(SharedSymbol<ELFT> *SS);
373 void finalize() override;
374 void writeTo(uint8_t *Buf) override;
375 size_t getNeedNum() const { return Needed.size(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000376 typename Base::Kind getKind() const override { return Base::VersNeed; }
377 static bool classof(const Base *B) { return B->getKind() == Base::VersNeed; }
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000378};
379
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000380template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000381class RelocationSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000382 typedef typename ELFT::Rel Elf_Rel;
383 typedef typename ELFT::Rela Elf_Rela;
384 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000385 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000386
387public:
George Rimarc191acf2016-05-10 15:47:57 +0000388 RelocationSection(StringRef Name, bool Sort);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000389 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000390 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000391 void finalize() override;
392 void writeTo(uint8_t *Buf) override;
393 bool hasRelocs() const { return !Relocs.empty(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000394 typename Base::Kind getKind() const override { return Base::Reloc; }
Eugene Leviantaa498192016-08-31 08:51:39 +0000395 size_t getRelativeRelocCount() const { return NumRelativeRelocs; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000396 static bool classof(const Base *B) { return B->getKind() == Base::Reloc; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000397
398private:
George Rimarc191acf2016-05-10 15:47:57 +0000399 bool Sort;
Eugene Leviantaa498192016-08-31 08:51:39 +0000400 size_t NumRelativeRelocs = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000401 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000402};
403
404template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000405class OutputSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000406 typedef OutputSectionBase<ELFT> Base;
407
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000408public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000409 typedef typename ELFT::Shdr Elf_Shdr;
410 typedef typename ELFT::Sym Elf_Sym;
411 typedef typename ELFT::Rel Elf_Rel;
412 typedef typename ELFT::Rela Elf_Rela;
413 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000414 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000415 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000416 void sortInitFini();
417 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000418 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000419 void finalize() override;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000420 void assignOffsets() override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000421 typename Base::Kind getKind() const override { return Base::Regular; }
422 static bool classof(const Base *B) { return B->getKind() == Base::Regular; }
Rafael Espindola71675852015-09-22 00:16:19 +0000423 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000424};
425
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000426template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000427class MergeOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000428 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000429 typedef OutputSectionBase<ELFT> Base;
Rafael Espindolac159c962015-10-19 21:00:02 +0000430
431public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000432 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
433 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000434 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000435 void writeTo(uint8_t *Buf) override;
Justin Lebaree34a732016-10-17 22:24:36 +0000436 unsigned getOffset(llvm::CachedHashStringRef Val);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000437 void finalize() override;
Rui Ueyama406b4692016-05-27 14:39:13 +0000438 void finalizePieces() override;
Peter Collingbournee29e1422016-05-05 04:10:12 +0000439 bool shouldTailMerge() const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000440 typename Base::Kind getKind() const override { return Base::Merge; }
441 static bool classof(const Base *B) { return B->getKind() == Base::Merge; }
Rafael Espindolac159c962015-10-19 21:00:02 +0000442
443private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000444 llvm::StringTableBuilder Builder;
Rui Ueyama406b4692016-05-27 14:39:13 +0000445 std::vector<MergeInputSection<ELFT> *> Sections;
Rafael Espindolac159c962015-10-19 21:00:02 +0000446};
447
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000448struct CieRecord {
Rafael Espindola2deeb602016-07-21 20:18:30 +0000449 EhSectionPiece *Piece = nullptr;
450 std::vector<EhSectionPiece *> FdePieces;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000451};
452
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000453// Output section for .eh_frame.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000454template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +0000455class EhOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000456 typedef typename ELFT::uint uintX_t;
457 typedef typename ELFT::Shdr Elf_Shdr;
458 typedef typename ELFT::Rel Elf_Rel;
459 typedef typename ELFT::Rela Elf_Rela;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000460 typedef OutputSectionBase<ELFT> Base;
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000461
462public:
463 EhOutputSection();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000464 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000465 void finalize() override;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000466 bool empty() const { return Sections.empty(); }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000467
Rui Ueyama40845e62015-12-26 05:51:07 +0000468 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000469 typename Base::Kind getKind() const override { return Base::EHFrame; }
470 static bool classof(const Base *B) { return B->getKind() == Base::EHFrame; }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000471
Rui Ueyamade9777a2016-05-23 16:30:41 +0000472 size_t NumFdes = 0;
473
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000474private:
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000475 template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000476 void addSectionAux(EhInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000477
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000478 template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000479 CieRecord *addCie(EhSectionPiece &Piece, EhInputSection<ELFT> *Sec,
480 ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000481
482 template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000483 bool isFdeLive(EhSectionPiece &Piece, EhInputSection<ELFT> *Sec,
484 ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000485
Rui Ueyamae75e9332016-05-23 03:00:33 +0000486 uintX_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc);
487
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000488 std::vector<EhInputSection<ELFT> *> Sections;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000489 std::vector<CieRecord *> Cies;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000490
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000491 // CIE records are uniquified by their contents and personality functions.
492 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, SymbolBody *>, CieRecord> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000493};
494
Rafael Espindolac159c962015-10-19 21:00:02 +0000495template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000496class InterpSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000497 typedef OutputSectionBase<ELFT> Base;
498
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000499public:
500 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000501 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000502 typename Base::Kind getKind() const override { return Base::Interp; }
503 static bool classof(const Base *B) { return B->getKind() == Base::Interp; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000504};
505
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000506template <class ELFT>
507class StringTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000508 typedef OutputSectionBase<ELFT> Base;
509
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000510public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000511 typedef typename ELFT::uint uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000512 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000513 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000514 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000515 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000516 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000517 bool isDynamic() const { return Dynamic; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000518 typename Base::Kind getKind() const override { return Base::StrTable; }
519 static bool classof(const Base *B) { return B->getKind() == Base::StrTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000520
521private:
522 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000523 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000524 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000525 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000526};
527
528template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000529class HashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000530 typedef typename ELFT::Word Elf_Word;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000531 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000532
533public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000534 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000535 void finalize() override;
536 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000537 typename Base::Kind getKind() const override { return Base::HashTable; }
538 static bool classof(const Base *B) { return B->getKind() == Base::HashTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000539};
540
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000541// Outputs GNU Hash section. For detailed explanation see:
542// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
543template <class ELFT>
544class GnuHashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000545 typedef typename ELFT::Off Elf_Off;
546 typedef typename ELFT::Word Elf_Word;
547 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000548 typedef OutputSectionBase<ELFT> Base;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000549
550public:
551 GnuHashTableSection();
552 void finalize() override;
553 void writeTo(uint8_t *Buf) override;
554
Igor Kudrinf1d60292015-10-28 07:05:56 +0000555 // Adds symbols to the hash table.
556 // Sorts the input to satisfy GNU hash section requirements.
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000557 void addSymbols(std::vector<SymbolTableEntry> &Symbols);
Eugene Leviant9d278b62016-08-10 18:10:41 +0000558 typename Base::Kind getKind() const override { return Base::GnuHashTable; }
559 static bool classof(const Base *B) {
560 return B->getKind() == Base::GnuHashTable;
561 }
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000562
563private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000564 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000565 static unsigned calcMaskWords(unsigned NumHashed);
566
567 void writeHeader(uint8_t *&Buf);
568 void writeBloomFilter(uint8_t *&Buf);
569 void writeHashTable(uint8_t *Buf);
570
Rui Ueyama861c7312016-02-17 05:40:03 +0000571 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000572 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000573 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000574 uint32_t Hash;
575 };
576
Rui Ueyama861c7312016-02-17 05:40:03 +0000577 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000578
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000579 unsigned MaskWords;
580 unsigned NBuckets;
581 unsigned Shift2;
582};
583
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000584template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000585class DynamicSection final : public OutputSectionBase<ELFT> {
586 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000587 typedef typename ELFT::Dyn Elf_Dyn;
588 typedef typename ELFT::Rel Elf_Rel;
589 typedef typename ELFT::Rela Elf_Rela;
590 typedef typename ELFT::Shdr Elf_Shdr;
591 typedef typename ELFT::Sym Elf_Sym;
592 typedef typename ELFT::uint uintX_t;
Rafael Espindolade069362016-01-25 21:32:04 +0000593
Rui Ueyama909cc682016-02-02 03:11:27 +0000594 // The .dynamic section contains information for the dynamic linker.
595 // The section consists of fixed size entries, which consist of
596 // type and value fields. Value are one of plain integers, symbol
597 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000598 struct Entry {
599 int32_t Tag;
600 union {
601 OutputSectionBase<ELFT> *OutSec;
602 uint64_t Val;
603 const SymbolBody *Sym;
604 };
George Rimar03e05602016-08-19 15:23:39 +0000605 enum KindT { SecAddr, SecSize, SymAddr, PlainInt } Kind;
606 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec, KindT Kind = SecAddr)
607 : Tag(Tag), OutSec(OutSec), Kind(Kind) {}
Rafael Espindolade069362016-01-25 21:32:04 +0000608 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
609 Entry(int32_t Tag, const SymbolBody *Sym)
610 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
611 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000612
613 // finalize() fills this vector with the section contents. finalize()
614 // cannot directly create final section contents because when the
615 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000616 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000617
618public:
Rui Ueyamaace4f902016-05-24 04:25:47 +0000619 explicit DynamicSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000620 void finalize() override;
621 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000622 typename Base::Kind getKind() const override { return Base::Dynamic; }
623 static bool classof(const Base *B) { return B->getKind() == Base::Dynamic; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000624};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000625
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000626template <class ELFT>
627class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
628 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000629 typedef OutputSectionBase<ELFT> Base;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000630
631public:
632 MipsReginfoOutputSection();
633 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000634 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000635 typename Base::Kind getKind() const override { return Base::MipsReginfo; }
636 static bool classof(const Base *B) {
637 return B->getKind() == Base::MipsReginfo;
638 }
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000639
640private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000641 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000642};
643
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000644template <class ELFT>
645class MipsOptionsOutputSection final : public OutputSectionBase<ELFT> {
646 typedef llvm::object::Elf_Mips_Options<ELFT> Elf_Mips_Options;
647 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000648 typedef OutputSectionBase<ELFT> Base;
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000649
650public:
651 MipsOptionsOutputSection();
652 void writeTo(uint8_t *Buf) override;
653 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000654 typename Base::Kind getKind() const override { return Base::MipsOptions; }
655 static bool classof(const Base *B) {
656 return B->getKind() == Base::MipsOptions;
657 }
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000658
659private:
660 uint32_t GprMask = 0;
661};
662
Simon Atanasyan85c6b442016-08-12 06:28:49 +0000663template <class ELFT>
664class MipsAbiFlagsOutputSection final : public OutputSectionBase<ELFT> {
665 typedef llvm::object::Elf_Mips_ABIFlags<ELFT> Elf_Mips_ABIFlags;
666 typedef OutputSectionBase<ELFT> Base;
667
668public:
669 MipsAbiFlagsOutputSection();
670 void writeTo(uint8_t *Buf) override;
671 void addSection(InputSectionBase<ELFT> *S) override;
672 typename Base::Kind getKind() const override { return Base::MipsAbiFlags; }
673 static bool classof(const Base *B) {
674 return B->getKind() == Base::MipsAbiFlags;
675 }
676
677private:
678 Elf_Mips_ABIFlags Flags;
679};
680
George Rimarf6bc65a2016-01-15 13:34:52 +0000681// --eh-frame-hdr option tells linker to construct a header for all the
682// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
683// and also to a PT_GNU_EH_FRAME segment.
684// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
685// calling dl_iterate_phdr.
686// This section contains a lookup table for quick binary search of FDEs.
687// Detailed info about internals can be found in Ian Lance Taylor's blog:
688// http://www.airs.com/blog/archives/460 (".eh_frame")
689// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
690template <class ELFT>
691class EhFrameHeader final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000692 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000693 typedef OutputSectionBase<ELFT> Base;
George Rimarf6bc65a2016-01-15 13:34:52 +0000694
695public:
696 EhFrameHeader();
Rui Ueyamade9777a2016-05-23 16:30:41 +0000697 void finalize() override;
George Rimarf6bc65a2016-01-15 13:34:52 +0000698 void writeTo(uint8_t *Buf) override;
Rui Ueyamae75e9332016-05-23 03:00:33 +0000699 void addFde(uint32_t Pc, uint32_t FdeVA);
Eugene Leviant9d278b62016-08-10 18:10:41 +0000700 typename Base::Kind getKind() const override { return Base::EHFrameHdr; }
701 static bool classof(const Base *B) {
702 return B->getKind() == Base::EHFrameHdr;
703 }
George Rimarf6bc65a2016-01-15 13:34:52 +0000704
George Rimarf6bc65a2016-01-15 13:34:52 +0000705private:
706 struct FdeData {
Rui Ueyamae75e9332016-05-23 03:00:33 +0000707 uint32_t Pc;
708 uint32_t FdeVA;
George Rimarf6bc65a2016-01-15 13:34:52 +0000709 };
710
Rui Ueyamae75e9332016-05-23 03:00:33 +0000711 std::vector<FdeData> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000712};
713
Rui Ueyama3a41be22016-04-07 22:49:21 +0000714template <class ELFT> class BuildIdSection : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000715 typedef OutputSectionBase<ELFT> Base;
716
Rui Ueyama634ddf02016-03-11 20:51:53 +0000717public:
Rui Ueyama634ddf02016-03-11 20:51:53 +0000718 void writeTo(uint8_t *Buf) override;
Petr Hosekfdfcb792016-09-01 22:43:03 +0000719 virtual void writeBuildId(ArrayRef<uint8_t> Buf) = 0;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000720 typename Base::Kind getKind() const override { return Base::BuildId; }
721 static bool classof(const Base *B) { return B->getKind() == Base::BuildId; }
Rui Ueyama3a41be22016-04-07 22:49:21 +0000722
723protected:
724 BuildIdSection(size_t HashSize);
725 size_t HashSize;
726 uint8_t *HashBuf = nullptr;
727};
728
Rafael Espindolad88d7162016-09-14 11:32:57 +0000729template <class ELFT>
730class BuildIdFastHash final : public BuildIdSection<ELFT> {
Rui Ueyama3a41be22016-04-07 22:49:21 +0000731public:
Rafael Espindolad88d7162016-09-14 11:32:57 +0000732 BuildIdFastHash() : BuildIdSection<ELFT>(8) {}
Petr Hosekfdfcb792016-09-01 22:43:03 +0000733 void writeBuildId(ArrayRef<uint8_t> Buf) override;
Rui Ueyama3a41be22016-04-07 22:49:21 +0000734};
735
736template <class ELFT> class BuildIdMd5 final : public BuildIdSection<ELFT> {
737public:
738 BuildIdMd5() : BuildIdSection<ELFT>(16) {}
Petr Hosekfdfcb792016-09-01 22:43:03 +0000739 void writeBuildId(ArrayRef<uint8_t> Buf) override;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000740};
741
Rui Ueyamad86ec302016-04-07 23:51:56 +0000742template <class ELFT> class BuildIdSha1 final : public BuildIdSection<ELFT> {
743public:
744 BuildIdSha1() : BuildIdSection<ELFT>(20) {}
Petr Hosekfdfcb792016-09-01 22:43:03 +0000745 void writeBuildId(ArrayRef<uint8_t> Buf) override;
Rui Ueyamad86ec302016-04-07 23:51:56 +0000746};
747
Eugene Leviant933dae72016-08-26 09:55:37 +0000748template <class ELFT> class BuildIdUuid final : public BuildIdSection<ELFT> {
749public:
750 BuildIdUuid() : BuildIdSection<ELFT>(16) {}
Petr Hosekfdfcb792016-09-01 22:43:03 +0000751 void writeBuildId(ArrayRef<uint8_t> Buf) override;
Eugene Leviant933dae72016-08-26 09:55:37 +0000752};
753
Rui Ueyama9194db72016-05-13 21:55:56 +0000754template <class ELFT>
755class BuildIdHexstring final : public BuildIdSection<ELFT> {
756public:
757 BuildIdHexstring();
Petr Hosekfdfcb792016-09-01 22:43:03 +0000758 void writeBuildId(ArrayRef<uint8_t>) override;
Rui Ueyama9194db72016-05-13 21:55:56 +0000759};
760
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000761// All output sections that are hadnled by the linker specially are
762// globally accessible. Writer initializes them, so don't use them
763// until Writer is initialized.
764template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000765 typedef typename ELFT::uint uintX_t;
766 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000767 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000768 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000769 static EhFrameHeader<ELFT> *EhFrameHdr;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000770 static EhOutputSection<ELFT> *EhFrame;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000771 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000772 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000773 static GotSection<ELFT> *Got;
774 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000775 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000776 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000777 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000778 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000779 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000780 static PltSection<ELFT> *Plt;
781 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000782 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000783 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000784 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000785 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000786 static SymbolTableSection<ELFT> *DynSymTab;
787 static SymbolTableSection<ELFT> *SymTab;
George Rimard3566302016-06-20 11:55:12 +0000788 static VersionDefinitionSection<ELFT> *VerDef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000789 static VersionTableSection<ELFT> *VerSym;
790 static VersionNeedSection<ELFT> *VerNeed;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000791 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000792 static OutputSectionBase<ELFT> *ElfHeader;
793 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000794
795 static OutputSectionBase<ELFT> *PreinitArray;
796 static OutputSectionBase<ELFT> *InitArray;
797 static OutputSectionBase<ELFT> *FiniArray;
Rui Ueyamabdf836d2016-08-12 01:10:17 +0000798
799 // This pool owns dynamically-allocated output sections.
800 static std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> Pool;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000801};
Rui Ueyamad888d102015-10-09 19:34:55 +0000802
George Rimar6892afa2016-07-12 09:49:43 +0000803template <bool Is64Bits> struct SectionKey {
804 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
805 StringRef Name;
806 uint32_t Type;
807 uintX_t Flags;
808 uintX_t Alignment;
809};
810
811// This class knows how to create an output section for a given
812// input section. Output section type is determined by various
813// factors, including input section's sh_flags, sh_type and
814// linker scripts.
815template <class ELFT> class OutputSectionFactory {
816 typedef typename ELFT::Shdr Elf_Shdr;
817 typedef typename ELFT::uint uintX_t;
818 typedef typename elf::SectionKey<ELFT::Is64Bits> Key;
819
820public:
821 std::pair<OutputSectionBase<ELFT> *, bool> create(InputSectionBase<ELFT> *C,
822 StringRef OutsecName);
Rafael Espindola10897f12016-09-13 14:23:14 +0000823 std::pair<OutputSectionBase<ELFT> *, bool>
824 create(const SectionKey<ELFT::Is64Bits> &Key, InputSectionBase<ELFT> *C);
George Rimar6892afa2016-07-12 09:49:43 +0000825
George Rimar6892afa2016-07-12 09:49:43 +0000826private:
George Rimar6892afa2016-07-12 09:49:43 +0000827 llvm::SmallDenseMap<Key, OutputSectionBase<ELFT> *> Map;
828};
829
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000830template <class ELFT> uint64_t getHeaderSize() {
831 if (Config->OFormatBinary)
832 return 0;
833 return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
834}
835
Rui Ueyama634ddf02016-03-11 20:51:53 +0000836template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000837template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000838template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000839template <class ELFT> EhOutputSection<ELFT> *Out<ELFT>::EhFrame;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000840template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000841template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000842template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
843template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000844template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000845template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000846template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000847template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000848template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000849template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
850template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000851template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000852template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000853template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000854template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000855template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
856template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
George Rimard3566302016-06-20 11:55:12 +0000857template <class ELFT> VersionDefinitionSection<ELFT> *Out<ELFT>::VerDef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000858template <class ELFT> VersionTableSection<ELFT> *Out<ELFT>::VerSym;
859template <class ELFT> VersionNeedSection<ELFT> *Out<ELFT>::VerNeed;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000860template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000861template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
862template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000863template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::PreinitArray;
864template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::InitArray;
865template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::FiniArray;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000866
Rui Ueyamabdf836d2016-08-12 01:10:17 +0000867template <class ELFT>
868std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> Out<ELFT>::Pool;
869
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000870} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000871} // namespace lld
872
George Rimar6892afa2016-07-12 09:49:43 +0000873namespace llvm {
874template <bool Is64Bits> struct DenseMapInfo<lld::elf::SectionKey<Is64Bits>> {
875 typedef typename lld::elf::SectionKey<Is64Bits> Key;
876
877 static Key getEmptyKey();
878 static Key getTombstoneKey();
879 static unsigned getHashValue(const Key &Val);
880 static bool isEqual(const Key &LHS, const Key &RHS);
881};
882}
883
884#endif