blob: 36cbf4e3a0a83fa58e99801efa292af7fc5b51ab [file] [log] [blame]
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001//===- OutputSections.h -----------------------------------------*- C++ -*-===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLD_ELF_OUTPUT_SECTIONS_H
11#define LLD_ELF_OUTPUT_SECTIONS_H
12
Davide Italiano85121bb2015-09-25 03:56:11 +000013#include "Config.h"
George Rimar58fa5242016-10-20 09:19:48 +000014#include "GdbIndex.h"
Simon Atanasyan41325112016-06-19 21:39:37 +000015#include "Relocations.h"
Davide Italiano85121bb2015-09-25 03:56:11 +000016
Rui Ueyamaa0752a52016-03-13 20:28:29 +000017#include "lld/Core/LLVM.h"
Simon Atanasyand2980d32016-03-29 14:07:22 +000018#include "llvm/ADT/SmallPtrSet.h"
Rui Ueyamaa0752a52016-03-13 20:28:29 +000019#include "llvm/MC/StringTableBuilder.h"
20#include "llvm/Object/ELF.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000021
22namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000023namespace elf {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000024
25class SymbolBody;
Rafael Espindola2deeb602016-07-21 20:18:30 +000026struct EhSectionPiece;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000027template <class ELFT> class SymbolTable;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000028template <class ELFT> class SymbolTableSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000029template <class ELFT> class StringTableSection;
Rui Ueyama0b9a9032016-05-24 04:19:20 +000030template <class ELFT> class EhInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000031template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000032template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000033template <class ELFT> class MergeInputSection;
Simon Atanasyan1d7df402015-12-20 10:57:34 +000034template <class ELFT> class MipsReginfoInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000035template <class ELFT> class OutputSection;
36template <class ELFT> class ObjectFile;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +000037template <class ELFT> class SharedFile;
38template <class ELFT> class SharedSymbol;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000039template <class ELFT> class DefinedRegular;
40
Rafael Espindola71675852015-09-22 00:16:19 +000041// This represents a section in an output file.
42// Different sub classes represent different types of sections. Some contain
43// input sections, others are created by the linker.
44// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000045// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000046template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000047public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +000048 typedef typename ELFT::uint uintX_t;
49 typedef typename ELFT::Shdr Elf_Shdr;
Eugene Leviant9d278b62016-08-10 18:10:41 +000050 enum Kind {
51 Base,
Eugene Leviant9d278b62016-08-10 18:10:41 +000052 Dynamic,
53 EHFrame,
54 EHFrameHdr,
55 GnuHashTable,
56 Got,
57 GotPlt,
58 HashTable,
59 Interp,
60 Merge,
61 MipsReginfo,
62 MipsOptions,
Simon Atanasyan85c6b442016-08-12 06:28:49 +000063 MipsAbiFlags,
Eugene Leviant9d278b62016-08-10 18:10:41 +000064 Plt,
65 Regular,
66 Reloc,
67 StrTable,
68 SymTable,
69 VersDef,
70 VersNeed,
71 VersTable
72 };
Rafael Espindola5805c4f2015-09-21 21:38:08 +000073
George Rimar9bec24a2016-02-18 14:20:08 +000074 OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000075 void setVA(uintX_t VA) { Header.sh_addr = VA; }
76 uintX_t getVA() const { return Header.sh_addr; }
Eugene Leviantb71d6f72016-10-06 09:39:28 +000077 void setLMAOffset(uintX_t LMAOff) { LMAOffset = LMAOff; }
78 uintX_t getLMA() const { return Header.sh_addr + LMAOffset; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000079 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Eugene Leviant3d9abec2016-09-29 09:20:33 +000080 uintX_t getFileOffset() { return Header.sh_offset; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000081 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000082 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000083 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000084
Rui Ueyama40845e62015-12-26 05:51:07 +000085 virtual void addSection(InputSectionBase<ELFT> *C) {}
Eugene Leviant9d278b62016-08-10 18:10:41 +000086 virtual Kind getKind() const { return Base; }
87 static bool classof(const OutputSectionBase<ELFT> *B) {
88 return B->getKind() == Base;
89 }
Rui Ueyama40845e62015-12-26 05:51:07 +000090
Rui Ueyama2317d0d2015-10-15 20:55:22 +000091 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000092
93 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000094 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000095 void setSize(uintX_t Val) { Header.sh_size = Val; }
Rafael Espindola571452c2016-04-11 13:44:05 +000096 uintX_t getFlags() const { return Header.sh_flags; }
Rafael Espindola10897f12016-09-13 14:23:14 +000097 void updateFlags(uintX_t Val) { Header.sh_flags |= Val; }
Rafael Espindola0b113672016-07-27 14:10:56 +000098 uint32_t getPhdrFlags() const;
Rafael Espindola571452c2016-04-11 13:44:05 +000099 uintX_t getFileOff() const { return Header.sh_offset; }
Rui Ueyama3b04d832016-07-14 05:46:24 +0000100 uintX_t getAlignment() const { return Header.sh_addralign; }
Rafael Espindola571452c2016-04-11 13:44:05 +0000101 uint32_t getType() const { return Header.sh_type; }
Rui Ueyama3b04d832016-07-14 05:46:24 +0000102
Rui Ueyama424b4082016-06-17 01:18:46 +0000103 void updateAlignment(uintX_t Alignment) {
104 if (Alignment > Header.sh_addralign)
105 Header.sh_addralign = Alignment;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000106 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000107
Rui Ueyama47091902016-03-30 19:41:51 +0000108 // If true, this section will be page aligned on disk.
109 // Typically the first section of each PT_LOAD segment has this flag.
110 bool PageAlign = false;
111
Eugene Leviant3d9abec2016-09-29 09:20:33 +0000112 // Pointer to the first section in PT_LOAD segment, which this section
113 // also resides in. This field is used to correctly compute file offset
114 // of a section. When two sections share the same load segment, difference
115 // between their file offsets should be equal to difference between their
116 // virtual addresses. To compute some section offset we use the following
117 // formula: Off = Off_first + VA - VA_first.
118 OutputSectionBase<ELFT> *FirstInPtLoad = nullptr;
119
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000120 virtual void finalize() {}
Rui Ueyama406b4692016-05-27 14:39:13 +0000121 virtual void finalizePieces() {}
Rui Ueyama809d8e22016-06-23 04:33:42 +0000122 virtual void assignOffsets() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +0000123 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +0000124 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000125
126protected:
127 StringRef Name;
Sean Silva580c1b62016-04-20 04:26:16 +0000128 Elf_Shdr Header;
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000129 uintX_t LMAOffset = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000130};
131
George Rimar58fa5242016-10-20 09:19:48 +0000132template <class ELFT>
133class GdbIndexSection final : public OutputSectionBase<ELFT> {
134 typedef typename ELFT::uint uintX_t;
135
136 const unsigned OffsetTypeSize = 4;
137 const unsigned CuListOffset = 6 * OffsetTypeSize;
138 const unsigned CompilationUnitSize = 16;
139 const unsigned AddressEntrySize = 16 + OffsetTypeSize;
140 const unsigned SymTabEntrySize = 2 * OffsetTypeSize;
141
142public:
143 GdbIndexSection();
144 void finalize() override;
145 void writeTo(uint8_t *Buf) override;
146
147 // Pairs of [CU Offset, CU length].
148 std::vector<std::pair<uintX_t, uintX_t>> CompilationUnits;
149
150private:
151 void parseDebugSections();
152 void readDwarf(InputSection<ELFT> *I);
153
154 uint32_t CuTypesOffset;
155};
156
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000157template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
158 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000159 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000160
161public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000162 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000163 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000164 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000165 void addEntry(SymbolBody &Sym);
Simon Atanasyan41325112016-06-19 21:39:37 +0000166 void addMipsEntry(SymbolBody &Sym, uintX_t Addend, RelExpr Expr);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000167 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000168 bool addTlsIndex();
Simon Atanasyan41325112016-06-19 21:39:37 +0000169 bool empty() const { return MipsPageEntries == 0 && Entries.empty(); }
Rafael Espindola58cd5db2016-04-19 22:46:03 +0000170 uintX_t getMipsLocalPageOffset(uintX_t Addr);
Simon Atanasyan41325112016-06-19 21:39:37 +0000171 uintX_t getMipsGotOffset(const SymbolBody &B, uintX_t Addend) const;
George Rimar90cd0a82015-12-01 19:20:26 +0000172 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
Rafael Espindola74031ba2016-04-07 15:20:56 +0000173 uintX_t getGlobalDynOffset(const SymbolBody &B) const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000174 typename Base::Kind getKind() const override { return Base::Got; }
175 static bool classof(const Base *B) { return B->getKind() == Base::Got; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000176
Igor Kudrin304860a2015-11-12 04:39:49 +0000177 // Returns the symbol which corresponds to the first entry of the global part
178 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
179 // table properties.
180 // Returns nullptr if the global part is empty.
181 const SymbolBody *getMipsFirstGlobalEntry() const;
182
183 // Returns the number of entries in the local part of GOT including
184 // the number of reserved entries. This method is MIPS-specific.
185 unsigned getMipsLocalEntriesNum() const;
186
Simon Atanasyan002e2442016-06-23 15:26:31 +0000187 // Returns offset of TLS part of the MIPS GOT table. This part goes
188 // after 'local' and 'global' entries.
Simon Atanasyanbc946932016-10-19 17:13:43 +0000189 uintX_t getMipsTlsOffset() const;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000190
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000191 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
Simon Atanasyanbc946932016-10-19 17:13:43 +0000192 uint32_t getTlsIndexOff() const { return TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000193
Rui Ueyama022d8e82016-05-24 03:36:07 +0000194 // Flag to force GOT to be in output if we have relocations
195 // that relies on its address.
196 bool HasGotOffRel = false;
197
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000198private:
199 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000200 uint32_t TlsIndexOff = -1;
Simon Atanasyan41325112016-06-19 21:39:37 +0000201 uint32_t MipsPageEntries = 0;
Simon Atanasyand2980d32016-03-29 14:07:22 +0000202 // Output sections referenced by MIPS GOT relocations.
203 llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000204 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
Simon Atanasyan41325112016-06-19 21:39:37 +0000205
206 // MIPS ABI requires to create unique GOT entry for each Symbol/Addend
207 // pairs. The `MipsGotMap` maps (S,A) pair to the GOT index in the `MipsLocal`
208 // or `MipsGlobal` vectors. In general it does not have a sence to take in
209 // account addend for preemptible symbols because the corresponding
210 // GOT entries should have one-to-one mapping with dynamic symbols table.
211 // But we use the same container's types for both kind of GOT entries
212 // to handle them uniformly.
George Rimara4c7e742016-10-20 08:36:42 +0000213 typedef std::pair<const SymbolBody *, uintX_t> MipsGotEntry;
Simon Atanasyan41325112016-06-19 21:39:37 +0000214 typedef std::vector<MipsGotEntry> MipsGotEntries;
215 llvm::DenseMap<MipsGotEntry, size_t> MipsGotMap;
216 MipsGotEntries MipsLocal;
Simon Atanasyanbed04bf2016-10-21 07:22:30 +0000217 MipsGotEntries MipsLocal32;
Simon Atanasyan41325112016-06-19 21:39:37 +0000218 MipsGotEntries MipsGlobal;
219
220 // Write MIPS-specific parts of the GOT.
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000221 void writeMipsGot(uint8_t *Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000222};
223
George Rimar648a2c32015-10-20 08:54:27 +0000224template <class ELFT>
225class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000226 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000227 typedef OutputSectionBase<ELFT> Base;
George Rimar648a2c32015-10-20 08:54:27 +0000228
229public:
230 GotPltSection();
231 void finalize() override;
232 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000233 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000234 bool empty() const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000235 typename Base::Kind getKind() const override { return Base::GotPlt; }
236 static bool classof(const Base *B) { return B->getKind() == Base::GotPlt; }
George Rimar648a2c32015-10-20 08:54:27 +0000237
238private:
239 std::vector<const SymbolBody *> Entries;
240};
241
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000242template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
243 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000244 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000245
246public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000247 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000248 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000249 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000250 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000251 bool empty() const { return Entries.empty(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000252 typename Base::Kind getKind() const override { return Base::Plt; }
253 static bool classof(const Base *B) { return B->getKind() == Base::Plt; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000254
255private:
George Rimar77b77792015-11-25 22:15:01 +0000256 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000257};
258
Rui Ueyama809d8e22016-06-23 04:33:42 +0000259template <class ELFT> class DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000260 typedef typename ELFT::uint uintX_t;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000261
262public:
263 DynamicReloc(uint32_t Type, const InputSectionBase<ELFT> *InputSec,
264 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
265 uintX_t Addend)
266 : Type(Type), Sym(Sym), InputSec(InputSec), OffsetInSec(OffsetInSec),
267 UseSymVA(UseSymVA), Addend(Addend) {}
268
269 DynamicReloc(uint32_t Type, const OutputSectionBase<ELFT> *OutputSec,
270 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
271 uintX_t Addend)
272 : Type(Type), Sym(Sym), OutputSec(OutputSec), OffsetInSec(OffsetInSec),
273 UseSymVA(UseSymVA), Addend(Addend) {}
274
275 uintX_t getOffset() const;
276 uintX_t getAddend() const;
277 uint32_t getSymIndex() const;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000278 const OutputSectionBase<ELFT> *getOutputSec() const { return OutputSec; }
Rui Ueyama809d8e22016-06-23 04:33:42 +0000279
Rafael Espindolade9857e2016-02-04 21:33:05 +0000280 uint32_t Type;
281
Rui Ueyama809d8e22016-06-23 04:33:42 +0000282private:
Rafael Espindolaac568f92016-04-11 13:47:35 +0000283 SymbolBody *Sym;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000284 const InputSectionBase<ELFT> *InputSec = nullptr;
285 const OutputSectionBase<ELFT> *OutputSec = nullptr;
Rafael Espindolaac568f92016-04-11 13:47:35 +0000286 uintX_t OffsetInSec;
287 bool UseSymVA;
288 uintX_t Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000289};
290
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000291struct SymbolTableEntry {
292 SymbolBody *Symbol;
Reid Kleckner2918d0b2016-10-20 00:13:34 +0000293 size_t StrTabOffset;
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000294};
295
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000296template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000297class SymbolTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000298 typedef OutputSectionBase<ELFT> Base;
299
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000300public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000301 typedef typename ELFT::Shdr Elf_Shdr;
302 typedef typename ELFT::Sym Elf_Sym;
303 typedef typename ELFT::SymRange Elf_Sym_Range;
304 typedef typename ELFT::uint uintX_t;
Rui Ueyamaace4f902016-05-24 04:25:47 +0000305 SymbolTableSection(StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000306
Rui Ueyama0db335f2015-10-07 16:58:54 +0000307 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000308 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000309 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000310 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000311 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000312 typename Base::Kind getKind() const override { return Base::SymTable; }
313 static bool classof(const Base *B) { return B->getKind() == Base::SymTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000314
George Rimara4c7e742016-10-20 08:36:42 +0000315 ArrayRef<SymbolTableEntry> getSymbols() const { return Symbols; }
Rafael Espindolae2c24612016-01-29 01:24:25 +0000316
317 unsigned NumLocals = 0;
318 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000319
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000320private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000321 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000322 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000323
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000324 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000325
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000326 // A vector of symbols and their string table offsets.
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000327 std::vector<SymbolTableEntry> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000328};
329
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000330// For more information about .gnu.version and .gnu.version_r see:
331// https://www.akkadia.org/drepper/symbol-versioning
332
George Rimard3566302016-06-20 11:55:12 +0000333// The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
334// contain symbol version definitions. The number of entries in this section
335// shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
336// The section shall contain an array of Elf_Verdef structures, optionally
337// followed by an array of Elf_Verdaux structures.
338template <class ELFT>
339class VersionDefinitionSection final : public OutputSectionBase<ELFT> {
340 typedef typename ELFT::Verdef Elf_Verdef;
341 typedef typename ELFT::Verdaux Elf_Verdaux;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000342 typedef OutputSectionBase<ELFT> Base;
George Rimard3566302016-06-20 11:55:12 +0000343
George Rimard3566302016-06-20 11:55:12 +0000344public:
345 VersionDefinitionSection();
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::VersDef; }
349 static bool classof(const Base *B) { return B->getKind() == Base::VersDef; }
Rui Ueyama9f619642016-07-16 02:29:45 +0000350
351private:
352 void writeOne(uint8_t *Buf, uint32_t Index, StringRef Name, size_t NameOff);
353
354 unsigned FileDefNameOff;
George Rimard3566302016-06-20 11:55:12 +0000355};
356
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000357// The .gnu.version section specifies the required version of each symbol in the
358// dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
359// table entry. An Elf_Versym is just a 16-bit integer that refers to a version
George Rimard3566302016-06-20 11:55:12 +0000360// identifier defined in the either .gnu.version_r or .gnu.version_d section.
361// The values 0 and 1 are reserved. All other values are used for versions in
362// the own object or in any of the dependencies.
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000363template <class ELFT>
364class VersionTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000365 typedef OutputSectionBase<ELFT> Base;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000366 typedef typename ELFT::Versym Elf_Versym;
367
368public:
369 VersionTableSection();
370 void finalize() override;
371 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000372 typename Base::Kind getKind() const override { return Base::VersTable; }
373 static bool classof(const Base *B) { return B->getKind() == Base::VersTable; }
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000374};
375
376// The .gnu.version_r section defines the version identifiers used by
377// .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
378// Elf_Verneed specifies the version requirements for a single DSO, and contains
379// a reference to a linked list of Elf_Vernaux data structures which define the
380// mapping from version identifiers to version names.
381template <class ELFT>
382class VersionNeedSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000383 typedef OutputSectionBase<ELFT> Base;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000384 typedef typename ELFT::Verneed Elf_Verneed;
385 typedef typename ELFT::Vernaux Elf_Vernaux;
386
387 // A vector of shared files that need Elf_Verneed data structures and the
388 // string table offsets of their sonames.
389 std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed;
390
George Rimard3566302016-06-20 11:55:12 +0000391 // The next available version identifier.
392 unsigned NextIndex;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000393
394public:
395 VersionNeedSection();
396 void addSymbol(SharedSymbol<ELFT> *SS);
397 void finalize() override;
398 void writeTo(uint8_t *Buf) override;
399 size_t getNeedNum() const { return Needed.size(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000400 typename Base::Kind getKind() const override { return Base::VersNeed; }
401 static bool classof(const Base *B) { return B->getKind() == Base::VersNeed; }
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000402};
403
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000404template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000405class RelocationSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000406 typedef typename ELFT::Rel Elf_Rel;
407 typedef typename ELFT::Rela Elf_Rela;
408 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000409 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000410
411public:
George Rimarc191acf2016-05-10 15:47:57 +0000412 RelocationSection(StringRef Name, bool Sort);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000413 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000414 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000415 void finalize() override;
416 void writeTo(uint8_t *Buf) override;
417 bool hasRelocs() const { return !Relocs.empty(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000418 typename Base::Kind getKind() const override { return Base::Reloc; }
Eugene Leviantaa498192016-08-31 08:51:39 +0000419 size_t getRelativeRelocCount() const { return NumRelativeRelocs; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000420 static bool classof(const Base *B) { return B->getKind() == Base::Reloc; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000421
422private:
George Rimarc191acf2016-05-10 15:47:57 +0000423 bool Sort;
Eugene Leviantaa498192016-08-31 08:51:39 +0000424 size_t NumRelativeRelocs = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000425 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000426};
427
428template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000429class OutputSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000430 typedef OutputSectionBase<ELFT> Base;
431
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000432public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000433 typedef typename ELFT::Shdr Elf_Shdr;
434 typedef typename ELFT::Sym Elf_Sym;
435 typedef typename ELFT::Rel Elf_Rel;
436 typedef typename ELFT::Rela Elf_Rela;
437 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000438 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000439 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000440 void sortInitFini();
441 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000442 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000443 void finalize() override;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000444 void assignOffsets() override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000445 typename Base::Kind getKind() const override { return Base::Regular; }
446 static bool classof(const Base *B) { return B->getKind() == Base::Regular; }
Rafael Espindola71675852015-09-22 00:16:19 +0000447 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000448};
449
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000450template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000451class MergeOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000452 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000453 typedef OutputSectionBase<ELFT> Base;
Rafael Espindolac159c962015-10-19 21:00:02 +0000454
455public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000456 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
457 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000458 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000459 void writeTo(uint8_t *Buf) override;
Justin Lebaree34a732016-10-17 22:24:36 +0000460 unsigned getOffset(llvm::CachedHashStringRef Val);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000461 void finalize() override;
Rui Ueyama406b4692016-05-27 14:39:13 +0000462 void finalizePieces() override;
Peter Collingbournee29e1422016-05-05 04:10:12 +0000463 bool shouldTailMerge() const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000464 typename Base::Kind getKind() const override { return Base::Merge; }
465 static bool classof(const Base *B) { return B->getKind() == Base::Merge; }
Rafael Espindolac159c962015-10-19 21:00:02 +0000466
467private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000468 llvm::StringTableBuilder Builder;
Rui Ueyama406b4692016-05-27 14:39:13 +0000469 std::vector<MergeInputSection<ELFT> *> Sections;
Rafael Espindolac159c962015-10-19 21:00:02 +0000470};
471
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000472struct CieRecord {
Rafael Espindola2deeb602016-07-21 20:18:30 +0000473 EhSectionPiece *Piece = nullptr;
474 std::vector<EhSectionPiece *> FdePieces;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000475};
476
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000477// Output section for .eh_frame.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000478template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +0000479class EhOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000480 typedef typename ELFT::uint uintX_t;
481 typedef typename ELFT::Shdr Elf_Shdr;
482 typedef typename ELFT::Rel Elf_Rel;
483 typedef typename ELFT::Rela Elf_Rela;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000484 typedef OutputSectionBase<ELFT> Base;
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000485
486public:
487 EhOutputSection();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000488 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000489 void finalize() override;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000490 bool empty() const { return Sections.empty(); }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000491
Rui Ueyama40845e62015-12-26 05:51:07 +0000492 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000493 typename Base::Kind getKind() const override { return Base::EHFrame; }
494 static bool classof(const Base *B) { return B->getKind() == Base::EHFrame; }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000495
Rui Ueyamade9777a2016-05-23 16:30:41 +0000496 size_t NumFdes = 0;
497
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000498private:
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000499 template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000500 void addSectionAux(EhInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000501
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000502 template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000503 CieRecord *addCie(EhSectionPiece &Piece, EhInputSection<ELFT> *Sec,
504 ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000505
506 template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000507 bool isFdeLive(EhSectionPiece &Piece, EhInputSection<ELFT> *Sec,
508 ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000509
Rui Ueyamae75e9332016-05-23 03:00:33 +0000510 uintX_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc);
511
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000512 std::vector<EhInputSection<ELFT> *> Sections;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000513 std::vector<CieRecord *> Cies;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000514
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000515 // CIE records are uniquified by their contents and personality functions.
516 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, SymbolBody *>, CieRecord> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000517};
518
Rafael Espindolac159c962015-10-19 21:00:02 +0000519template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000520class InterpSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000521 typedef OutputSectionBase<ELFT> Base;
522
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000523public:
524 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000525 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000526 typename Base::Kind getKind() const override { return Base::Interp; }
527 static bool classof(const Base *B) { return B->getKind() == Base::Interp; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000528};
529
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000530template <class ELFT>
531class StringTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000532 typedef OutputSectionBase<ELFT> Base;
533
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000534public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000535 typedef typename ELFT::uint uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000536 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000537 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000538 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000539 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000540 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000541 bool isDynamic() const { return Dynamic; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000542 typename Base::Kind getKind() const override { return Base::StrTable; }
543 static bool classof(const Base *B) { return B->getKind() == Base::StrTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000544
545private:
546 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000547 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000548 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000549 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000550};
551
552template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000553class HashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000554 typedef typename ELFT::Word Elf_Word;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000555 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000556
557public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000558 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000559 void finalize() override;
560 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000561 typename Base::Kind getKind() const override { return Base::HashTable; }
562 static bool classof(const Base *B) { return B->getKind() == Base::HashTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000563};
564
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000565// Outputs GNU Hash section. For detailed explanation see:
566// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
567template <class ELFT>
568class GnuHashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000569 typedef typename ELFT::Off Elf_Off;
570 typedef typename ELFT::Word Elf_Word;
571 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000572 typedef OutputSectionBase<ELFT> Base;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000573
574public:
575 GnuHashTableSection();
576 void finalize() override;
577 void writeTo(uint8_t *Buf) override;
578
Igor Kudrinf1d60292015-10-28 07:05:56 +0000579 // Adds symbols to the hash table.
580 // Sorts the input to satisfy GNU hash section requirements.
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000581 void addSymbols(std::vector<SymbolTableEntry> &Symbols);
Eugene Leviant9d278b62016-08-10 18:10:41 +0000582 typename Base::Kind getKind() const override { return Base::GnuHashTable; }
583 static bool classof(const Base *B) {
584 return B->getKind() == Base::GnuHashTable;
585 }
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000586
587private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000588 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000589 static unsigned calcMaskWords(unsigned NumHashed);
590
591 void writeHeader(uint8_t *&Buf);
592 void writeBloomFilter(uint8_t *&Buf);
593 void writeHashTable(uint8_t *Buf);
594
Rui Ueyama861c7312016-02-17 05:40:03 +0000595 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000596 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000597 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000598 uint32_t Hash;
599 };
600
Rui Ueyama861c7312016-02-17 05:40:03 +0000601 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000602
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000603 unsigned MaskWords;
604 unsigned NBuckets;
605 unsigned Shift2;
606};
607
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000608template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000609class DynamicSection final : public OutputSectionBase<ELFT> {
610 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000611 typedef typename ELFT::Dyn Elf_Dyn;
612 typedef typename ELFT::Rel Elf_Rel;
613 typedef typename ELFT::Rela Elf_Rela;
614 typedef typename ELFT::Shdr Elf_Shdr;
615 typedef typename ELFT::Sym Elf_Sym;
616 typedef typename ELFT::uint uintX_t;
Rafael Espindolade069362016-01-25 21:32:04 +0000617
Rui Ueyama909cc682016-02-02 03:11:27 +0000618 // The .dynamic section contains information for the dynamic linker.
619 // The section consists of fixed size entries, which consist of
620 // type and value fields. Value are one of plain integers, symbol
621 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000622 struct Entry {
623 int32_t Tag;
624 union {
625 OutputSectionBase<ELFT> *OutSec;
626 uint64_t Val;
627 const SymbolBody *Sym;
628 };
George Rimar03e05602016-08-19 15:23:39 +0000629 enum KindT { SecAddr, SecSize, SymAddr, PlainInt } Kind;
630 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec, KindT Kind = SecAddr)
631 : Tag(Tag), OutSec(OutSec), Kind(Kind) {}
Rafael Espindolade069362016-01-25 21:32:04 +0000632 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
633 Entry(int32_t Tag, const SymbolBody *Sym)
634 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
635 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000636
637 // finalize() fills this vector with the section contents. finalize()
638 // cannot directly create final section contents because when the
639 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000640 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000641
642public:
Rui Ueyamaace4f902016-05-24 04:25:47 +0000643 explicit DynamicSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000644 void finalize() override;
645 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000646 typename Base::Kind getKind() const override { return Base::Dynamic; }
647 static bool classof(const Base *B) { return B->getKind() == Base::Dynamic; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000648};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000649
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000650template <class ELFT>
651class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
652 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000653 typedef OutputSectionBase<ELFT> Base;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000654
655public:
656 MipsReginfoOutputSection();
657 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000658 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000659 typename Base::Kind getKind() const override { return Base::MipsReginfo; }
660 static bool classof(const Base *B) {
661 return B->getKind() == Base::MipsReginfo;
662 }
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000663
664private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000665 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000666};
667
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000668template <class ELFT>
669class MipsOptionsOutputSection final : public OutputSectionBase<ELFT> {
670 typedef llvm::object::Elf_Mips_Options<ELFT> Elf_Mips_Options;
671 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000672 typedef OutputSectionBase<ELFT> Base;
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000673
674public:
675 MipsOptionsOutputSection();
676 void writeTo(uint8_t *Buf) override;
677 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000678 typename Base::Kind getKind() const override { return Base::MipsOptions; }
679 static bool classof(const Base *B) {
680 return B->getKind() == Base::MipsOptions;
681 }
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000682
683private:
684 uint32_t GprMask = 0;
685};
686
Simon Atanasyan85c6b442016-08-12 06:28:49 +0000687template <class ELFT>
688class MipsAbiFlagsOutputSection final : public OutputSectionBase<ELFT> {
689 typedef llvm::object::Elf_Mips_ABIFlags<ELFT> Elf_Mips_ABIFlags;
690 typedef OutputSectionBase<ELFT> Base;
691
692public:
693 MipsAbiFlagsOutputSection();
694 void writeTo(uint8_t *Buf) override;
695 void addSection(InputSectionBase<ELFT> *S) override;
696 typename Base::Kind getKind() const override { return Base::MipsAbiFlags; }
697 static bool classof(const Base *B) {
698 return B->getKind() == Base::MipsAbiFlags;
699 }
700
701private:
702 Elf_Mips_ABIFlags Flags;
703};
704
George Rimarf6bc65a2016-01-15 13:34:52 +0000705// --eh-frame-hdr option tells linker to construct a header for all the
706// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
707// and also to a PT_GNU_EH_FRAME segment.
708// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
709// calling dl_iterate_phdr.
710// This section contains a lookup table for quick binary search of FDEs.
711// Detailed info about internals can be found in Ian Lance Taylor's blog:
712// http://www.airs.com/blog/archives/460 (".eh_frame")
713// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
714template <class ELFT>
715class EhFrameHeader final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000716 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000717 typedef OutputSectionBase<ELFT> Base;
George Rimarf6bc65a2016-01-15 13:34:52 +0000718
719public:
720 EhFrameHeader();
Rui Ueyamade9777a2016-05-23 16:30:41 +0000721 void finalize() override;
George Rimarf6bc65a2016-01-15 13:34:52 +0000722 void writeTo(uint8_t *Buf) override;
Rui Ueyamae75e9332016-05-23 03:00:33 +0000723 void addFde(uint32_t Pc, uint32_t FdeVA);
Eugene Leviant9d278b62016-08-10 18:10:41 +0000724 typename Base::Kind getKind() const override { return Base::EHFrameHdr; }
725 static bool classof(const Base *B) {
726 return B->getKind() == Base::EHFrameHdr;
727 }
George Rimarf6bc65a2016-01-15 13:34:52 +0000728
George Rimarf6bc65a2016-01-15 13:34:52 +0000729private:
730 struct FdeData {
Rui Ueyamae75e9332016-05-23 03:00:33 +0000731 uint32_t Pc;
732 uint32_t FdeVA;
George Rimarf6bc65a2016-01-15 13:34:52 +0000733 };
734
Rui Ueyamae75e9332016-05-23 03:00:33 +0000735 std::vector<FdeData> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000736};
737
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000738// All output sections that are hadnled by the linker specially are
739// globally accessible. Writer initializes them, so don't use them
740// until Writer is initialized.
741template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000742 typedef typename ELFT::uint uintX_t;
743 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000744 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000745 static EhFrameHeader<ELFT> *EhFrameHdr;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000746 static EhOutputSection<ELFT> *EhFrame;
George Rimar58fa5242016-10-20 09:19:48 +0000747 static GdbIndexSection<ELFT> *GdbIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000748 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000749 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000750 static GotSection<ELFT> *Got;
751 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000752 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000753 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000754 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000755 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000756 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000757 static PltSection<ELFT> *Plt;
758 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000759 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000760 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000761 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000762 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000763 static SymbolTableSection<ELFT> *DynSymTab;
764 static SymbolTableSection<ELFT> *SymTab;
George Rimard3566302016-06-20 11:55:12 +0000765 static VersionDefinitionSection<ELFT> *VerDef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000766 static VersionTableSection<ELFT> *VerSym;
767 static VersionNeedSection<ELFT> *VerNeed;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000768 static Elf_Phdr *TlsPhdr;
George Rimar58fa5242016-10-20 09:19:48 +0000769 static OutputSectionBase<ELFT> *DebugInfo;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000770 static OutputSectionBase<ELFT> *ElfHeader;
771 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000772
773 static OutputSectionBase<ELFT> *PreinitArray;
774 static OutputSectionBase<ELFT> *InitArray;
775 static OutputSectionBase<ELFT> *FiniArray;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000776};
Rui Ueyamad888d102015-10-09 19:34:55 +0000777
George Rimar6892afa2016-07-12 09:49:43 +0000778template <bool Is64Bits> struct SectionKey {
779 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
780 StringRef Name;
781 uint32_t Type;
782 uintX_t Flags;
783 uintX_t Alignment;
784};
785
786// This class knows how to create an output section for a given
787// input section. Output section type is determined by various
788// factors, including input section's sh_flags, sh_type and
789// linker scripts.
790template <class ELFT> class OutputSectionFactory {
791 typedef typename ELFT::Shdr Elf_Shdr;
792 typedef typename ELFT::uint uintX_t;
793 typedef typename elf::SectionKey<ELFT::Is64Bits> Key;
794
795public:
796 std::pair<OutputSectionBase<ELFT> *, bool> create(InputSectionBase<ELFT> *C,
797 StringRef OutsecName);
Rafael Espindola10897f12016-09-13 14:23:14 +0000798 std::pair<OutputSectionBase<ELFT> *, bool>
799 create(const SectionKey<ELFT::Is64Bits> &Key, InputSectionBase<ELFT> *C);
George Rimar6892afa2016-07-12 09:49:43 +0000800
George Rimar6892afa2016-07-12 09:49:43 +0000801private:
George Rimar6892afa2016-07-12 09:49:43 +0000802 llvm::SmallDenseMap<Key, OutputSectionBase<ELFT> *> Map;
803};
804
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000805template <class ELFT> uint64_t getHeaderSize() {
806 if (Config->OFormatBinary)
807 return 0;
808 return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
809}
810
Rui Ueyamad888d102015-10-09 19:34:55 +0000811template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000812template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000813template <class ELFT> EhOutputSection<ELFT> *Out<ELFT>::EhFrame;
George Rimar58fa5242016-10-20 09:19:48 +0000814template <class ELFT> GdbIndexSection<ELFT> *Out<ELFT>::GdbIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000815template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000816template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000817template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
818template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000819template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000820template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000821template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000822template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000823template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000824template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
825template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000826template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000827template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000828template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000829template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000830template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
831template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
George Rimard3566302016-06-20 11:55:12 +0000832template <class ELFT> VersionDefinitionSection<ELFT> *Out<ELFT>::VerDef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000833template <class ELFT> VersionTableSection<ELFT> *Out<ELFT>::VerSym;
834template <class ELFT> VersionNeedSection<ELFT> *Out<ELFT>::VerNeed;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000835template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
George Rimar58fa5242016-10-20 09:19:48 +0000836template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::DebugInfo;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000837template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
838template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000839template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::PreinitArray;
840template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::InitArray;
841template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::FiniArray;
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000842} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000843} // namespace lld
844
George Rimar6892afa2016-07-12 09:49:43 +0000845namespace llvm {
846template <bool Is64Bits> struct DenseMapInfo<lld::elf::SectionKey<Is64Bits>> {
847 typedef typename lld::elf::SectionKey<Is64Bits> Key;
848
849 static Key getEmptyKey();
850 static Key getTombstoneKey();
851 static unsigned getHashValue(const Key &Val);
852 static bool isEqual(const Key &LHS, const Key &RHS);
853};
854}
855
856#endif