blob: 14e9dfe48656fe2c08dda411814d3859f8697a3d [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"
Rui Ueyama3a41be22016-04-07 22:49:21 +000021#include "llvm/Support/MD5.h"
Rui Ueyamad86ec302016-04-07 23:51:56 +000022#include "llvm/Support/SHA1.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000023
24namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000025namespace elf {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000026
27class SymbolBody;
Rafael Espindola2deeb602016-07-21 20:18:30 +000028struct EhSectionPiece;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000029template <class ELFT> class SymbolTable;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000030template <class ELFT> class SymbolTableSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000031template <class ELFT> class StringTableSection;
Rui Ueyama0b9a9032016-05-24 04:19:20 +000032template <class ELFT> class EhInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000033template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000034template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000035template <class ELFT> class MergeInputSection;
Simon Atanasyan1d7df402015-12-20 10:57:34 +000036template <class ELFT> class MipsReginfoInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000037template <class ELFT> class OutputSection;
38template <class ELFT> class ObjectFile;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +000039template <class ELFT> class SharedFile;
40template <class ELFT> class SharedSymbol;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000041template <class ELFT> class DefinedRegular;
42
Rafael Espindola71675852015-09-22 00:16:19 +000043// This represents a section in an output file.
44// Different sub classes represent different types of sections. Some contain
45// input sections, others are created by the linker.
46// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000047// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000048template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000049public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +000050 typedef typename ELFT::uint uintX_t;
51 typedef typename ELFT::Shdr Elf_Shdr;
Eugene Leviant9d278b62016-08-10 18:10:41 +000052 enum Kind {
53 Base,
54 BuildId,
55 Dynamic,
56 EHFrame,
57 EHFrameHdr,
58 GnuHashTable,
59 Got,
60 GotPlt,
61 HashTable,
62 Interp,
63 Merge,
64 MipsReginfo,
65 MipsOptions,
Simon Atanasyan85c6b442016-08-12 06:28:49 +000066 MipsAbiFlags,
Eugene Leviant9d278b62016-08-10 18:10:41 +000067 Plt,
68 Regular,
69 Reloc,
70 StrTable,
71 SymTable,
72 VersDef,
73 VersNeed,
74 VersTable
75 };
Rafael Espindola5805c4f2015-09-21 21:38:08 +000076
George Rimar9bec24a2016-02-18 14:20:08 +000077 OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000078 void setVA(uintX_t VA) { Header.sh_addr = VA; }
79 uintX_t getVA() const { return Header.sh_addr; }
Eugene Leviantb71d6f72016-10-06 09:39:28 +000080 void setLMAOffset(uintX_t LMAOff) { LMAOffset = LMAOff; }
81 uintX_t getLMA() const { return Header.sh_addr + LMAOffset; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000082 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Eugene Leviant3d9abec2016-09-29 09:20:33 +000083 uintX_t getFileOffset() { return Header.sh_offset; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000084 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000085 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000086 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000087
Rui Ueyama40845e62015-12-26 05:51:07 +000088 virtual void addSection(InputSectionBase<ELFT> *C) {}
Eugene Leviant9d278b62016-08-10 18:10:41 +000089 virtual Kind getKind() const { return Base; }
90 static bool classof(const OutputSectionBase<ELFT> *B) {
91 return B->getKind() == Base;
92 }
Rui Ueyama40845e62015-12-26 05:51:07 +000093
Rui Ueyama2317d0d2015-10-15 20:55:22 +000094 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000095
96 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000097 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000098 void setSize(uintX_t Val) { Header.sh_size = Val; }
Rafael Espindola571452c2016-04-11 13:44:05 +000099 uintX_t getFlags() const { return Header.sh_flags; }
Rafael Espindola10897f12016-09-13 14:23:14 +0000100 void updateFlags(uintX_t Val) { Header.sh_flags |= Val; }
Rafael Espindola0b113672016-07-27 14:10:56 +0000101 uint32_t getPhdrFlags() const;
Rafael Espindola571452c2016-04-11 13:44:05 +0000102 uintX_t getFileOff() const { return Header.sh_offset; }
Rui Ueyama3b04d832016-07-14 05:46:24 +0000103 uintX_t getAlignment() const { return Header.sh_addralign; }
Rafael Espindola571452c2016-04-11 13:44:05 +0000104 uint32_t getType() const { return Header.sh_type; }
Rui Ueyama3b04d832016-07-14 05:46:24 +0000105
Rui Ueyama424b4082016-06-17 01:18:46 +0000106 void updateAlignment(uintX_t Alignment) {
107 if (Alignment > Header.sh_addralign)
108 Header.sh_addralign = Alignment;
Rafael Espindola115f0f32015-11-03 14:13:40 +0000109 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000110
Rui Ueyama47091902016-03-30 19:41:51 +0000111 // If true, this section will be page aligned on disk.
112 // Typically the first section of each PT_LOAD segment has this flag.
113 bool PageAlign = false;
114
Eugene Leviant3d9abec2016-09-29 09:20:33 +0000115 // Pointer to the first section in PT_LOAD segment, which this section
116 // also resides in. This field is used to correctly compute file offset
117 // of a section. When two sections share the same load segment, difference
118 // between their file offsets should be equal to difference between their
119 // virtual addresses. To compute some section offset we use the following
120 // formula: Off = Off_first + VA - VA_first.
121 OutputSectionBase<ELFT> *FirstInPtLoad = nullptr;
122
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000123 virtual void finalize() {}
Rui Ueyama406b4692016-05-27 14:39:13 +0000124 virtual void finalizePieces() {}
Rui Ueyama809d8e22016-06-23 04:33:42 +0000125 virtual void assignOffsets() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +0000126 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +0000127 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000128
129protected:
130 StringRef Name;
Sean Silva580c1b62016-04-20 04:26:16 +0000131 Elf_Shdr Header;
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000132 uintX_t LMAOffset = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000133};
134
George Rimar58fa5242016-10-20 09:19:48 +0000135template <class ELFT>
136class GdbIndexSection final : public OutputSectionBase<ELFT> {
137 typedef typename ELFT::uint uintX_t;
138
139 const unsigned OffsetTypeSize = 4;
140 const unsigned CuListOffset = 6 * OffsetTypeSize;
141 const unsigned CompilationUnitSize = 16;
142 const unsigned AddressEntrySize = 16 + OffsetTypeSize;
143 const unsigned SymTabEntrySize = 2 * OffsetTypeSize;
144
145public:
146 GdbIndexSection();
147 void finalize() override;
148 void writeTo(uint8_t *Buf) override;
149
150 // Pairs of [CU Offset, CU length].
151 std::vector<std::pair<uintX_t, uintX_t>> CompilationUnits;
152
153private:
154 void parseDebugSections();
155 void readDwarf(InputSection<ELFT> *I);
156
157 uint32_t CuTypesOffset;
158};
159
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000160template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
161 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000162 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000163
164public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000165 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000166 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000167 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000168 void addEntry(SymbolBody &Sym);
Simon Atanasyan41325112016-06-19 21:39:37 +0000169 void addMipsEntry(SymbolBody &Sym, uintX_t Addend, RelExpr Expr);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000170 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000171 bool addTlsIndex();
Simon Atanasyan41325112016-06-19 21:39:37 +0000172 bool empty() const { return MipsPageEntries == 0 && Entries.empty(); }
Rafael Espindola58cd5db2016-04-19 22:46:03 +0000173 uintX_t getMipsLocalPageOffset(uintX_t Addr);
Simon Atanasyan41325112016-06-19 21:39:37 +0000174 uintX_t getMipsGotOffset(const SymbolBody &B, uintX_t Addend) const;
George Rimar90cd0a82015-12-01 19:20:26 +0000175 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
Rafael Espindola74031ba2016-04-07 15:20:56 +0000176 uintX_t getGlobalDynOffset(const SymbolBody &B) const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000177 typename Base::Kind getKind() const override { return Base::Got; }
178 static bool classof(const Base *B) { return B->getKind() == Base::Got; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000179
Igor Kudrin304860a2015-11-12 04:39:49 +0000180 // Returns the symbol which corresponds to the first entry of the global part
181 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
182 // table properties.
183 // Returns nullptr if the global part is empty.
184 const SymbolBody *getMipsFirstGlobalEntry() const;
185
186 // Returns the number of entries in the local part of GOT including
187 // the number of reserved entries. This method is MIPS-specific.
188 unsigned getMipsLocalEntriesNum() const;
189
Simon Atanasyan002e2442016-06-23 15:26:31 +0000190 // Returns offset of TLS part of the MIPS GOT table. This part goes
191 // after 'local' and 'global' entries.
Simon Atanasyanbc946932016-10-19 17:13:43 +0000192 uintX_t getMipsTlsOffset() const;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000193
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000194 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
Simon Atanasyanbc946932016-10-19 17:13:43 +0000195 uint32_t getTlsIndexOff() const { return TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000196
Rui Ueyama022d8e82016-05-24 03:36:07 +0000197 // Flag to force GOT to be in output if we have relocations
198 // that relies on its address.
199 bool HasGotOffRel = false;
200
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000201private:
202 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000203 uint32_t TlsIndexOff = -1;
Simon Atanasyan41325112016-06-19 21:39:37 +0000204 uint32_t MipsPageEntries = 0;
Simon Atanasyand2980d32016-03-29 14:07:22 +0000205 // Output sections referenced by MIPS GOT relocations.
206 llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000207 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
Simon Atanasyan41325112016-06-19 21:39:37 +0000208
209 // MIPS ABI requires to create unique GOT entry for each Symbol/Addend
210 // pairs. The `MipsGotMap` maps (S,A) pair to the GOT index in the `MipsLocal`
211 // or `MipsGlobal` vectors. In general it does not have a sence to take in
212 // account addend for preemptible symbols because the corresponding
213 // GOT entries should have one-to-one mapping with dynamic symbols table.
214 // But we use the same container's types for both kind of GOT entries
215 // to handle them uniformly.
George Rimara4c7e742016-10-20 08:36:42 +0000216 typedef std::pair<const SymbolBody *, uintX_t> MipsGotEntry;
Simon Atanasyan41325112016-06-19 21:39:37 +0000217 typedef std::vector<MipsGotEntry> MipsGotEntries;
218 llvm::DenseMap<MipsGotEntry, size_t> MipsGotMap;
219 MipsGotEntries MipsLocal;
220 MipsGotEntries MipsGlobal;
221
222 // Write MIPS-specific parts of the GOT.
Simon Atanasyan919a58c2016-09-08 09:07:19 +0000223 void writeMipsGot(uint8_t *Buf);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000224};
225
George Rimar648a2c32015-10-20 08:54:27 +0000226template <class ELFT>
227class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000228 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000229 typedef OutputSectionBase<ELFT> Base;
George Rimar648a2c32015-10-20 08:54:27 +0000230
231public:
232 GotPltSection();
233 void finalize() override;
234 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000235 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000236 bool empty() const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000237 typename Base::Kind getKind() const override { return Base::GotPlt; }
238 static bool classof(const Base *B) { return B->getKind() == Base::GotPlt; }
George Rimar648a2c32015-10-20 08:54:27 +0000239
240private:
241 std::vector<const SymbolBody *> Entries;
242};
243
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000244template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
245 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000246 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000247
248public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000249 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000250 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000251 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000252 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000253 bool empty() const { return Entries.empty(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000254 typename Base::Kind getKind() const override { return Base::Plt; }
255 static bool classof(const Base *B) { return B->getKind() == Base::Plt; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000256
257private:
George Rimar77b77792015-11-25 22:15:01 +0000258 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000259};
260
Rui Ueyama809d8e22016-06-23 04:33:42 +0000261template <class ELFT> class DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000262 typedef typename ELFT::uint uintX_t;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000263
264public:
265 DynamicReloc(uint32_t Type, const InputSectionBase<ELFT> *InputSec,
266 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
267 uintX_t Addend)
268 : Type(Type), Sym(Sym), InputSec(InputSec), OffsetInSec(OffsetInSec),
269 UseSymVA(UseSymVA), Addend(Addend) {}
270
271 DynamicReloc(uint32_t Type, const OutputSectionBase<ELFT> *OutputSec,
272 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
273 uintX_t Addend)
274 : Type(Type), Sym(Sym), OutputSec(OutputSec), OffsetInSec(OffsetInSec),
275 UseSymVA(UseSymVA), Addend(Addend) {}
276
277 uintX_t getOffset() const;
278 uintX_t getAddend() const;
279 uint32_t getSymIndex() const;
Simon Atanasyan002e2442016-06-23 15:26:31 +0000280 const OutputSectionBase<ELFT> *getOutputSec() const { return OutputSec; }
Rui Ueyama809d8e22016-06-23 04:33:42 +0000281
Rafael Espindolade9857e2016-02-04 21:33:05 +0000282 uint32_t Type;
283
Rui Ueyama809d8e22016-06-23 04:33:42 +0000284private:
Rafael Espindolaac568f92016-04-11 13:47:35 +0000285 SymbolBody *Sym;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000286 const InputSectionBase<ELFT> *InputSec = nullptr;
287 const OutputSectionBase<ELFT> *OutputSec = nullptr;
Rafael Espindolaac568f92016-04-11 13:47:35 +0000288 uintX_t OffsetInSec;
289 bool UseSymVA;
290 uintX_t Addend;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000291};
292
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000293struct SymbolTableEntry {
294 SymbolBody *Symbol;
Reid Kleckner2918d0b2016-10-20 00:13:34 +0000295 size_t StrTabOffset;
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000296};
297
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000298template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000299class SymbolTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000300 typedef OutputSectionBase<ELFT> Base;
301
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000302public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000303 typedef typename ELFT::Shdr Elf_Shdr;
304 typedef typename ELFT::Sym Elf_Sym;
305 typedef typename ELFT::SymRange Elf_Sym_Range;
306 typedef typename ELFT::uint uintX_t;
Rui Ueyamaace4f902016-05-24 04:25:47 +0000307 SymbolTableSection(StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000308
Rui Ueyama0db335f2015-10-07 16:58:54 +0000309 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000310 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000311 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000312 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000313 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000314 typename Base::Kind getKind() const override { return Base::SymTable; }
315 static bool classof(const Base *B) { return B->getKind() == Base::SymTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000316
George Rimara4c7e742016-10-20 08:36:42 +0000317 ArrayRef<SymbolTableEntry> getSymbols() const { return Symbols; }
Rafael Espindolae2c24612016-01-29 01:24:25 +0000318
319 unsigned NumLocals = 0;
320 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000321
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000322private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000323 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000324 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000325
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000326 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000327
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000328 // A vector of symbols and their string table offsets.
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000329 std::vector<SymbolTableEntry> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000330};
331
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000332// For more information about .gnu.version and .gnu.version_r see:
333// https://www.akkadia.org/drepper/symbol-versioning
334
George Rimard3566302016-06-20 11:55:12 +0000335// The .gnu.version_d section which has a section type of SHT_GNU_verdef shall
336// contain symbol version definitions. The number of entries in this section
337// shall be contained in the DT_VERDEFNUM entry of the .dynamic section.
338// The section shall contain an array of Elf_Verdef structures, optionally
339// followed by an array of Elf_Verdaux structures.
340template <class ELFT>
341class VersionDefinitionSection final : public OutputSectionBase<ELFT> {
342 typedef typename ELFT::Verdef Elf_Verdef;
343 typedef typename ELFT::Verdaux Elf_Verdaux;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000344 typedef OutputSectionBase<ELFT> Base;
George Rimard3566302016-06-20 11:55:12 +0000345
George Rimard3566302016-06-20 11:55:12 +0000346public:
347 VersionDefinitionSection();
348 void finalize() override;
349 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000350 typename Base::Kind getKind() const override { return Base::VersDef; }
351 static bool classof(const Base *B) { return B->getKind() == Base::VersDef; }
Rui Ueyama9f619642016-07-16 02:29:45 +0000352
353private:
354 void writeOne(uint8_t *Buf, uint32_t Index, StringRef Name, size_t NameOff);
355
356 unsigned FileDefNameOff;
George Rimard3566302016-06-20 11:55:12 +0000357};
358
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000359// The .gnu.version section specifies the required version of each symbol in the
360// dynamic symbol table. It contains one Elf_Versym for each dynamic symbol
361// table entry. An Elf_Versym is just a 16-bit integer that refers to a version
George Rimard3566302016-06-20 11:55:12 +0000362// identifier defined in the either .gnu.version_r or .gnu.version_d section.
363// The values 0 and 1 are reserved. All other values are used for versions in
364// the own object or in any of the dependencies.
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000365template <class ELFT>
366class VersionTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000367 typedef OutputSectionBase<ELFT> Base;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000368 typedef typename ELFT::Versym Elf_Versym;
369
370public:
371 VersionTableSection();
372 void finalize() override;
373 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000374 typename Base::Kind getKind() const override { return Base::VersTable; }
375 static bool classof(const Base *B) { return B->getKind() == Base::VersTable; }
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000376};
377
378// The .gnu.version_r section defines the version identifiers used by
379// .gnu.version. It contains a linked list of Elf_Verneed data structures. Each
380// Elf_Verneed specifies the version requirements for a single DSO, and contains
381// a reference to a linked list of Elf_Vernaux data structures which define the
382// mapping from version identifiers to version names.
383template <class ELFT>
384class VersionNeedSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000385 typedef OutputSectionBase<ELFT> Base;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000386 typedef typename ELFT::Verneed Elf_Verneed;
387 typedef typename ELFT::Vernaux Elf_Vernaux;
388
389 // A vector of shared files that need Elf_Verneed data structures and the
390 // string table offsets of their sonames.
391 std::vector<std::pair<SharedFile<ELFT> *, size_t>> Needed;
392
George Rimard3566302016-06-20 11:55:12 +0000393 // The next available version identifier.
394 unsigned NextIndex;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000395
396public:
397 VersionNeedSection();
398 void addSymbol(SharedSymbol<ELFT> *SS);
399 void finalize() override;
400 void writeTo(uint8_t *Buf) override;
401 size_t getNeedNum() const { return Needed.size(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000402 typename Base::Kind getKind() const override { return Base::VersNeed; }
403 static bool classof(const Base *B) { return B->getKind() == Base::VersNeed; }
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000404};
405
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000406template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000407class RelocationSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000408 typedef typename ELFT::Rel Elf_Rel;
409 typedef typename ELFT::Rela Elf_Rela;
410 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000411 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000412
413public:
George Rimarc191acf2016-05-10 15:47:57 +0000414 RelocationSection(StringRef Name, bool Sort);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000415 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000416 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000417 void finalize() override;
418 void writeTo(uint8_t *Buf) override;
419 bool hasRelocs() const { return !Relocs.empty(); }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000420 typename Base::Kind getKind() const override { return Base::Reloc; }
Eugene Leviantaa498192016-08-31 08:51:39 +0000421 size_t getRelativeRelocCount() const { return NumRelativeRelocs; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000422 static bool classof(const Base *B) { return B->getKind() == Base::Reloc; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000423
424private:
George Rimarc191acf2016-05-10 15:47:57 +0000425 bool Sort;
Eugene Leviantaa498192016-08-31 08:51:39 +0000426 size_t NumRelativeRelocs = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000427 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000428};
429
430template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000431class OutputSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000432 typedef OutputSectionBase<ELFT> Base;
433
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000434public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000435 typedef typename ELFT::Shdr Elf_Shdr;
436 typedef typename ELFT::Sym Elf_Sym;
437 typedef typename ELFT::Rel Elf_Rel;
438 typedef typename ELFT::Rela Elf_Rela;
439 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000440 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000441 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000442 void sortInitFini();
443 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000444 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000445 void finalize() override;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000446 void assignOffsets() override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000447 typename Base::Kind getKind() const override { return Base::Regular; }
448 static bool classof(const Base *B) { return B->getKind() == Base::Regular; }
Rafael Espindola71675852015-09-22 00:16:19 +0000449 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000450};
451
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000452template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000453class MergeOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000454 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000455 typedef OutputSectionBase<ELFT> Base;
Rafael Espindolac159c962015-10-19 21:00:02 +0000456
457public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000458 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
459 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000460 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000461 void writeTo(uint8_t *Buf) override;
Justin Lebaree34a732016-10-17 22:24:36 +0000462 unsigned getOffset(llvm::CachedHashStringRef Val);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000463 void finalize() override;
Rui Ueyama406b4692016-05-27 14:39:13 +0000464 void finalizePieces() override;
Peter Collingbournee29e1422016-05-05 04:10:12 +0000465 bool shouldTailMerge() const;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000466 typename Base::Kind getKind() const override { return Base::Merge; }
467 static bool classof(const Base *B) { return B->getKind() == Base::Merge; }
Rafael Espindolac159c962015-10-19 21:00:02 +0000468
469private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000470 llvm::StringTableBuilder Builder;
Rui Ueyama406b4692016-05-27 14:39:13 +0000471 std::vector<MergeInputSection<ELFT> *> Sections;
Rafael Espindolac159c962015-10-19 21:00:02 +0000472};
473
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000474struct CieRecord {
Rafael Espindola2deeb602016-07-21 20:18:30 +0000475 EhSectionPiece *Piece = nullptr;
476 std::vector<EhSectionPiece *> FdePieces;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000477};
478
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000479// Output section for .eh_frame.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000480template <class ELFT>
Rui Ueyama1e479c22016-05-23 15:07:59 +0000481class EhOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000482 typedef typename ELFT::uint uintX_t;
483 typedef typename ELFT::Shdr Elf_Shdr;
484 typedef typename ELFT::Rel Elf_Rel;
485 typedef typename ELFT::Rela Elf_Rela;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000486 typedef OutputSectionBase<ELFT> Base;
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000487
488public:
489 EhOutputSection();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000490 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000491 void finalize() override;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000492 bool empty() const { return Sections.empty(); }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000493
Rui Ueyama40845e62015-12-26 05:51:07 +0000494 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000495 typename Base::Kind getKind() const override { return Base::EHFrame; }
496 static bool classof(const Base *B) { return B->getKind() == Base::EHFrame; }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000497
Rui Ueyamade9777a2016-05-23 16:30:41 +0000498 size_t NumFdes = 0;
499
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000500private:
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000501 template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000502 void addSectionAux(EhInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000503
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000504 template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000505 CieRecord *addCie(EhSectionPiece &Piece, EhInputSection<ELFT> *Sec,
506 ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000507
508 template <class RelTy>
Rafael Espindola2deeb602016-07-21 20:18:30 +0000509 bool isFdeLive(EhSectionPiece &Piece, EhInputSection<ELFT> *Sec,
510 ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000511
Rui Ueyamae75e9332016-05-23 03:00:33 +0000512 uintX_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc);
513
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000514 std::vector<EhInputSection<ELFT> *> Sections;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000515 std::vector<CieRecord *> Cies;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000516
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000517 // CIE records are uniquified by their contents and personality functions.
518 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, SymbolBody *>, CieRecord> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000519};
520
Rafael Espindolac159c962015-10-19 21:00:02 +0000521template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000522class InterpSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000523 typedef OutputSectionBase<ELFT> Base;
524
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000525public:
526 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000527 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000528 typename Base::Kind getKind() const override { return Base::Interp; }
529 static bool classof(const Base *B) { return B->getKind() == Base::Interp; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000530};
531
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000532template <class ELFT>
533class StringTableSection final : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000534 typedef OutputSectionBase<ELFT> Base;
535
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000536public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000537 typedef typename ELFT::uint uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000538 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000539 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000540 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000541 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000542 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000543 bool isDynamic() const { return Dynamic; }
Eugene Leviant9d278b62016-08-10 18:10:41 +0000544 typename Base::Kind getKind() const override { return Base::StrTable; }
545 static bool classof(const Base *B) { return B->getKind() == Base::StrTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000546
547private:
548 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000549 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000550 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000551 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000552};
553
554template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000555class HashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000556 typedef typename ELFT::Word Elf_Word;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000557 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000558
559public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000560 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000561 void finalize() override;
562 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000563 typename Base::Kind getKind() const override { return Base::HashTable; }
564 static bool classof(const Base *B) { return B->getKind() == Base::HashTable; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000565};
566
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000567// Outputs GNU Hash section. For detailed explanation see:
568// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
569template <class ELFT>
570class GnuHashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000571 typedef typename ELFT::Off Elf_Off;
572 typedef typename ELFT::Word Elf_Word;
573 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000574 typedef OutputSectionBase<ELFT> Base;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000575
576public:
577 GnuHashTableSection();
578 void finalize() override;
579 void writeTo(uint8_t *Buf) override;
580
Igor Kudrinf1d60292015-10-28 07:05:56 +0000581 // Adds symbols to the hash table.
582 // Sorts the input to satisfy GNU hash section requirements.
Michael J. Spencerf8a81482016-10-19 23:49:27 +0000583 void addSymbols(std::vector<SymbolTableEntry> &Symbols);
Eugene Leviant9d278b62016-08-10 18:10:41 +0000584 typename Base::Kind getKind() const override { return Base::GnuHashTable; }
585 static bool classof(const Base *B) {
586 return B->getKind() == Base::GnuHashTable;
587 }
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000588
589private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000590 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000591 static unsigned calcMaskWords(unsigned NumHashed);
592
593 void writeHeader(uint8_t *&Buf);
594 void writeBloomFilter(uint8_t *&Buf);
595 void writeHashTable(uint8_t *Buf);
596
Rui Ueyama861c7312016-02-17 05:40:03 +0000597 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000598 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000599 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000600 uint32_t Hash;
601 };
602
Rui Ueyama861c7312016-02-17 05:40:03 +0000603 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000604
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000605 unsigned MaskWords;
606 unsigned NBuckets;
607 unsigned Shift2;
608};
609
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000610template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000611class DynamicSection final : public OutputSectionBase<ELFT> {
612 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000613 typedef typename ELFT::Dyn Elf_Dyn;
614 typedef typename ELFT::Rel Elf_Rel;
615 typedef typename ELFT::Rela Elf_Rela;
616 typedef typename ELFT::Shdr Elf_Shdr;
617 typedef typename ELFT::Sym Elf_Sym;
618 typedef typename ELFT::uint uintX_t;
Rafael Espindolade069362016-01-25 21:32:04 +0000619
Rui Ueyama909cc682016-02-02 03:11:27 +0000620 // The .dynamic section contains information for the dynamic linker.
621 // The section consists of fixed size entries, which consist of
622 // type and value fields. Value are one of plain integers, symbol
623 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000624 struct Entry {
625 int32_t Tag;
626 union {
627 OutputSectionBase<ELFT> *OutSec;
628 uint64_t Val;
629 const SymbolBody *Sym;
630 };
George Rimar03e05602016-08-19 15:23:39 +0000631 enum KindT { SecAddr, SecSize, SymAddr, PlainInt } Kind;
632 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec, KindT Kind = SecAddr)
633 : Tag(Tag), OutSec(OutSec), Kind(Kind) {}
Rafael Espindolade069362016-01-25 21:32:04 +0000634 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
635 Entry(int32_t Tag, const SymbolBody *Sym)
636 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
637 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000638
639 // finalize() fills this vector with the section contents. finalize()
640 // cannot directly create final section contents because when the
641 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000642 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000643
644public:
Rui Ueyamaace4f902016-05-24 04:25:47 +0000645 explicit DynamicSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000646 void finalize() override;
647 void writeTo(uint8_t *Buf) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000648 typename Base::Kind getKind() const override { return Base::Dynamic; }
649 static bool classof(const Base *B) { return B->getKind() == Base::Dynamic; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000650};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000651
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000652template <class ELFT>
653class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
654 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000655 typedef OutputSectionBase<ELFT> Base;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000656
657public:
658 MipsReginfoOutputSection();
659 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000660 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000661 typename Base::Kind getKind() const override { return Base::MipsReginfo; }
662 static bool classof(const Base *B) {
663 return B->getKind() == Base::MipsReginfo;
664 }
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000665
666private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000667 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000668};
669
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000670template <class ELFT>
671class MipsOptionsOutputSection final : public OutputSectionBase<ELFT> {
672 typedef llvm::object::Elf_Mips_Options<ELFT> Elf_Mips_Options;
673 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000674 typedef OutputSectionBase<ELFT> Base;
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000675
676public:
677 MipsOptionsOutputSection();
678 void writeTo(uint8_t *Buf) override;
679 void addSection(InputSectionBase<ELFT> *S) override;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000680 typename Base::Kind getKind() const override { return Base::MipsOptions; }
681 static bool classof(const Base *B) {
682 return B->getKind() == Base::MipsOptions;
683 }
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000684
685private:
686 uint32_t GprMask = 0;
687};
688
Simon Atanasyan85c6b442016-08-12 06:28:49 +0000689template <class ELFT>
690class MipsAbiFlagsOutputSection final : public OutputSectionBase<ELFT> {
691 typedef llvm::object::Elf_Mips_ABIFlags<ELFT> Elf_Mips_ABIFlags;
692 typedef OutputSectionBase<ELFT> Base;
693
694public:
695 MipsAbiFlagsOutputSection();
696 void writeTo(uint8_t *Buf) override;
697 void addSection(InputSectionBase<ELFT> *S) override;
698 typename Base::Kind getKind() const override { return Base::MipsAbiFlags; }
699 static bool classof(const Base *B) {
700 return B->getKind() == Base::MipsAbiFlags;
701 }
702
703private:
704 Elf_Mips_ABIFlags Flags;
705};
706
George Rimarf6bc65a2016-01-15 13:34:52 +0000707// --eh-frame-hdr option tells linker to construct a header for all the
708// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
709// and also to a PT_GNU_EH_FRAME segment.
710// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
711// calling dl_iterate_phdr.
712// This section contains a lookup table for quick binary search of FDEs.
713// Detailed info about internals can be found in Ian Lance Taylor's blog:
714// http://www.airs.com/blog/archives/460 (".eh_frame")
715// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
716template <class ELFT>
717class EhFrameHeader final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000718 typedef typename ELFT::uint uintX_t;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000719 typedef OutputSectionBase<ELFT> Base;
George Rimarf6bc65a2016-01-15 13:34:52 +0000720
721public:
722 EhFrameHeader();
Rui Ueyamade9777a2016-05-23 16:30:41 +0000723 void finalize() override;
George Rimarf6bc65a2016-01-15 13:34:52 +0000724 void writeTo(uint8_t *Buf) override;
Rui Ueyamae75e9332016-05-23 03:00:33 +0000725 void addFde(uint32_t Pc, uint32_t FdeVA);
Eugene Leviant9d278b62016-08-10 18:10:41 +0000726 typename Base::Kind getKind() const override { return Base::EHFrameHdr; }
727 static bool classof(const Base *B) {
728 return B->getKind() == Base::EHFrameHdr;
729 }
George Rimarf6bc65a2016-01-15 13:34:52 +0000730
George Rimarf6bc65a2016-01-15 13:34:52 +0000731private:
732 struct FdeData {
Rui Ueyamae75e9332016-05-23 03:00:33 +0000733 uint32_t Pc;
734 uint32_t FdeVA;
George Rimarf6bc65a2016-01-15 13:34:52 +0000735 };
736
Rui Ueyamae75e9332016-05-23 03:00:33 +0000737 std::vector<FdeData> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000738};
739
Rui Ueyama3a41be22016-04-07 22:49:21 +0000740template <class ELFT> class BuildIdSection : public OutputSectionBase<ELFT> {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000741 typedef OutputSectionBase<ELFT> Base;
742
Rui Ueyama634ddf02016-03-11 20:51:53 +0000743public:
Rui Ueyama634ddf02016-03-11 20:51:53 +0000744 void writeTo(uint8_t *Buf) override;
Petr Hosekfdfcb792016-09-01 22:43:03 +0000745 virtual void writeBuildId(ArrayRef<uint8_t> Buf) = 0;
Eugene Leviant9d278b62016-08-10 18:10:41 +0000746 typename Base::Kind getKind() const override { return Base::BuildId; }
747 static bool classof(const Base *B) { return B->getKind() == Base::BuildId; }
Rui Ueyama3a41be22016-04-07 22:49:21 +0000748
749protected:
750 BuildIdSection(size_t HashSize);
751 size_t HashSize;
752 uint8_t *HashBuf = nullptr;
753};
754
Rafael Espindolad88d7162016-09-14 11:32:57 +0000755template <class ELFT>
756class BuildIdFastHash final : public BuildIdSection<ELFT> {
Rui Ueyama3a41be22016-04-07 22:49:21 +0000757public:
Rafael Espindolad88d7162016-09-14 11:32:57 +0000758 BuildIdFastHash() : BuildIdSection<ELFT>(8) {}
Petr Hosekfdfcb792016-09-01 22:43:03 +0000759 void writeBuildId(ArrayRef<uint8_t> Buf) override;
Rui Ueyama3a41be22016-04-07 22:49:21 +0000760};
761
762template <class ELFT> class BuildIdMd5 final : public BuildIdSection<ELFT> {
763public:
764 BuildIdMd5() : BuildIdSection<ELFT>(16) {}
Petr Hosekfdfcb792016-09-01 22:43:03 +0000765 void writeBuildId(ArrayRef<uint8_t> Buf) override;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000766};
767
Rui Ueyamad86ec302016-04-07 23:51:56 +0000768template <class ELFT> class BuildIdSha1 final : public BuildIdSection<ELFT> {
769public:
770 BuildIdSha1() : BuildIdSection<ELFT>(20) {}
Petr Hosekfdfcb792016-09-01 22:43:03 +0000771 void writeBuildId(ArrayRef<uint8_t> Buf) override;
Rui Ueyamad86ec302016-04-07 23:51:56 +0000772};
773
Eugene Leviant933dae72016-08-26 09:55:37 +0000774template <class ELFT> class BuildIdUuid final : public BuildIdSection<ELFT> {
775public:
776 BuildIdUuid() : BuildIdSection<ELFT>(16) {}
Petr Hosekfdfcb792016-09-01 22:43:03 +0000777 void writeBuildId(ArrayRef<uint8_t> Buf) override;
Eugene Leviant933dae72016-08-26 09:55:37 +0000778};
779
Rui Ueyama9194db72016-05-13 21:55:56 +0000780template <class ELFT>
781class BuildIdHexstring final : public BuildIdSection<ELFT> {
782public:
783 BuildIdHexstring();
Petr Hosekfdfcb792016-09-01 22:43:03 +0000784 void writeBuildId(ArrayRef<uint8_t>) override;
Rui Ueyama9194db72016-05-13 21:55:56 +0000785};
786
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000787// All output sections that are hadnled by the linker specially are
788// globally accessible. Writer initializes them, so don't use them
789// until Writer is initialized.
790template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000791 typedef typename ELFT::uint uintX_t;
792 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000793 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000794 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000795 static EhFrameHeader<ELFT> *EhFrameHdr;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000796 static EhOutputSection<ELFT> *EhFrame;
George Rimar58fa5242016-10-20 09:19:48 +0000797 static GdbIndexSection<ELFT> *GdbIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000798 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000799 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000800 static GotSection<ELFT> *Got;
801 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000802 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000803 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000804 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000805 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000806 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000807 static PltSection<ELFT> *Plt;
808 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000809 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000810 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000811 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000812 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000813 static SymbolTableSection<ELFT> *DynSymTab;
814 static SymbolTableSection<ELFT> *SymTab;
George Rimard3566302016-06-20 11:55:12 +0000815 static VersionDefinitionSection<ELFT> *VerDef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000816 static VersionTableSection<ELFT> *VerSym;
817 static VersionNeedSection<ELFT> *VerNeed;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000818 static Elf_Phdr *TlsPhdr;
George Rimar58fa5242016-10-20 09:19:48 +0000819 static OutputSectionBase<ELFT> *DebugInfo;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000820 static OutputSectionBase<ELFT> *ElfHeader;
821 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000822
823 static OutputSectionBase<ELFT> *PreinitArray;
824 static OutputSectionBase<ELFT> *InitArray;
825 static OutputSectionBase<ELFT> *FiniArray;
Rui Ueyamabdf836d2016-08-12 01:10:17 +0000826
827 // This pool owns dynamically-allocated output sections.
828 static std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> Pool;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000829};
Rui Ueyamad888d102015-10-09 19:34:55 +0000830
George Rimar6892afa2016-07-12 09:49:43 +0000831template <bool Is64Bits> struct SectionKey {
832 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
833 StringRef Name;
834 uint32_t Type;
835 uintX_t Flags;
836 uintX_t Alignment;
837};
838
839// This class knows how to create an output section for a given
840// input section. Output section type is determined by various
841// factors, including input section's sh_flags, sh_type and
842// linker scripts.
843template <class ELFT> class OutputSectionFactory {
844 typedef typename ELFT::Shdr Elf_Shdr;
845 typedef typename ELFT::uint uintX_t;
846 typedef typename elf::SectionKey<ELFT::Is64Bits> Key;
847
848public:
849 std::pair<OutputSectionBase<ELFT> *, bool> create(InputSectionBase<ELFT> *C,
850 StringRef OutsecName);
Rafael Espindola10897f12016-09-13 14:23:14 +0000851 std::pair<OutputSectionBase<ELFT> *, bool>
852 create(const SectionKey<ELFT::Is64Bits> &Key, InputSectionBase<ELFT> *C);
George Rimar6892afa2016-07-12 09:49:43 +0000853
George Rimar6892afa2016-07-12 09:49:43 +0000854private:
George Rimar6892afa2016-07-12 09:49:43 +0000855 llvm::SmallDenseMap<Key, OutputSectionBase<ELFT> *> Map;
856};
857
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000858template <class ELFT> uint64_t getHeaderSize() {
859 if (Config->OFormatBinary)
860 return 0;
861 return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
862}
863
Rui Ueyama634ddf02016-03-11 20:51:53 +0000864template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000865template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000866template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000867template <class ELFT> EhOutputSection<ELFT> *Out<ELFT>::EhFrame;
George Rimar58fa5242016-10-20 09:19:48 +0000868template <class ELFT> GdbIndexSection<ELFT> *Out<ELFT>::GdbIndex;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000869template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000870template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000871template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
872template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000873template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000874template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000875template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000876template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000877template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000878template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
879template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000880template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000881template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000882template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000883template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000884template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
885template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
George Rimard3566302016-06-20 11:55:12 +0000886template <class ELFT> VersionDefinitionSection<ELFT> *Out<ELFT>::VerDef;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +0000887template <class ELFT> VersionTableSection<ELFT> *Out<ELFT>::VerSym;
888template <class ELFT> VersionNeedSection<ELFT> *Out<ELFT>::VerNeed;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000889template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
George Rimar58fa5242016-10-20 09:19:48 +0000890template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::DebugInfo;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000891template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
892template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Rui Ueyamaa8f6fea2016-08-09 04:25:20 +0000893template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::PreinitArray;
894template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::InitArray;
895template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::FiniArray;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000896
Rui Ueyamabdf836d2016-08-12 01:10:17 +0000897template <class ELFT>
898std::vector<std::unique_ptr<OutputSectionBase<ELFT>>> Out<ELFT>::Pool;
899
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000900} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000901} // namespace lld
902
George Rimar6892afa2016-07-12 09:49:43 +0000903namespace llvm {
904template <bool Is64Bits> struct DenseMapInfo<lld::elf::SectionKey<Is64Bits>> {
905 typedef typename lld::elf::SectionKey<Is64Bits> Key;
906
907 static Key getEmptyKey();
908 static Key getTombstoneKey();
909 static unsigned getHashValue(const Key &Val);
910 static bool isEqual(const Key &LHS, const Key &RHS);
911};
912}
913
914#endif