blob: 19ea8ddaa84dcd68c0ce0521f0729480c331585b [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"
14
Rui Ueyamaa0752a52016-03-13 20:28:29 +000015#include "lld/Core/LLVM.h"
Simon Atanasyand2980d32016-03-29 14:07:22 +000016#include "llvm/ADT/SmallPtrSet.h"
Rui Ueyamaa0752a52016-03-13 20:28:29 +000017#include "llvm/MC/StringTableBuilder.h"
18#include "llvm/Object/ELF.h"
Rui Ueyama3a41be22016-04-07 22:49:21 +000019#include "llvm/Support/MD5.h"
Rui Ueyamad86ec302016-04-07 23:51:56 +000020#include "llvm/Support/SHA1.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;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000026template <class ELFT> class SymbolTable;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000027template <class ELFT> class SymbolTableSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000028template <class ELFT> class StringTableSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000029template <class ELFT> class EHInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000030template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000031template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000032template <class ELFT> class MergeInputSection;
Simon Atanasyan1d7df402015-12-20 10:57:34 +000033template <class ELFT> class MipsReginfoInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000034template <class ELFT> class OutputSection;
35template <class ELFT> class ObjectFile;
36template <class ELFT> class DefinedRegular;
37
Rafael Espindola5805c4f2015-09-21 21:38:08 +000038template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000039static inline typename ELFT::uint getAddend(const typename ELFT::Rel &Rel) {
Rafael Espindola932efcf2015-10-19 20:24:44 +000040 return 0;
41}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000042
43template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000044static inline typename ELFT::uint getAddend(const typename ELFT::Rela &Rel) {
Rafael Espindola932efcf2015-10-19 20:24:44 +000045 return Rel.r_addend;
46}
47
George Rimar12737b72016-02-25 08:40:26 +000048bool isValidCIdentifier(StringRef S);
49
Rafael Espindola71675852015-09-22 00:16:19 +000050// This represents a section in an output file.
51// Different sub classes represent different types of sections. Some contain
52// input sections, others are created by the linker.
53// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000054// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000055template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000056public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +000057 typedef typename ELFT::uint uintX_t;
58 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000059
George Rimar9bec24a2016-02-18 14:20:08 +000060 OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000061 void setVA(uintX_t VA) { Header.sh_addr = VA; }
62 uintX_t getVA() const { return Header.sh_addr; }
63 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000064 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000065 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000066 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000067
Rui Ueyama40845e62015-12-26 05:51:07 +000068 virtual void addSection(InputSectionBase<ELFT> *C) {}
69
Rui Ueyama2317d0d2015-10-15 20:55:22 +000070 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000071
72 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000073 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000074 void setSize(uintX_t Val) { Header.sh_size = Val; }
Rafael Espindola571452c2016-04-11 13:44:05 +000075 uintX_t getFlags() const { return Header.sh_flags; }
76 uintX_t getFileOff() const { return Header.sh_offset; }
77 uintX_t getAlign() const {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000078 // The ELF spec states that a value of 0 means the section has no alignment
79 // constraits.
80 return std::max<uintX_t>(Header.sh_addralign, 1);
81 }
Rafael Espindola571452c2016-04-11 13:44:05 +000082 uint32_t getType() const { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +000083 void updateAlign(uintX_t Align) {
84 if (Align > Header.sh_addralign)
85 Header.sh_addralign = Align;
86 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000087
Rui Ueyama47091902016-03-30 19:41:51 +000088 // If true, this section will be page aligned on disk.
89 // Typically the first section of each PT_LOAD segment has this flag.
90 bool PageAlign = false;
91
Rafael Espindola5805c4f2015-09-21 21:38:08 +000092 virtual void finalize() {}
Rafael Espindola56004c52016-04-07 14:22:09 +000093 virtual void
94 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000095 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000096 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000097
98protected:
99 StringRef Name;
Sean Silva580c1b62016-04-20 04:26:16 +0000100 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000101};
102
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000103template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
104 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000105 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000106
107public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000108 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000109 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000110 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000111 void addEntry(SymbolBody &Sym);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000112 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000113 bool addTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000114 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Rafael Espindola58cd5db2016-04-19 22:46:03 +0000115 uintX_t getMipsLocalEntryOffset(uintX_t EntryValue);
116 uintX_t getMipsLocalPageOffset(uintX_t Addr);
George Rimar90cd0a82015-12-01 19:20:26 +0000117 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
Rafael Espindola74031ba2016-04-07 15:20:56 +0000118 uintX_t getGlobalDynOffset(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000119 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000120
Igor Kudrin304860a2015-11-12 04:39:49 +0000121 // Returns the symbol which corresponds to the first entry of the global part
122 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
123 // table properties.
124 // Returns nullptr if the global part is empty.
125 const SymbolBody *getMipsFirstGlobalEntry() const;
126
127 // Returns the number of entries in the local part of GOT including
128 // the number of reserved entries. This method is MIPS-specific.
129 unsigned getMipsLocalEntriesNum() const;
130
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000131 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
Rafael Espindola74031ba2016-04-07 15:20:56 +0000132 uint32_t getTlsIndexOff() { return TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000133
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000134private:
135 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000136 uint32_t TlsIndexOff = -1;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000137 uint32_t MipsLocalEntries = 0;
Simon Atanasyand2980d32016-03-29 14:07:22 +0000138 // Output sections referenced by MIPS GOT relocations.
139 llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000140 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000141};
142
George Rimar648a2c32015-10-20 08:54:27 +0000143template <class ELFT>
144class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000145 typedef typename ELFT::uint uintX_t;
George Rimar648a2c32015-10-20 08:54:27 +0000146
147public:
148 GotPltSection();
149 void finalize() override;
150 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000151 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000152 bool empty() const;
George Rimar648a2c32015-10-20 08:54:27 +0000153
154private:
155 std::vector<const SymbolBody *> Entries;
156};
157
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000158template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
159 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000160 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000161
162public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000163 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000164 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000165 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000166 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000167 bool empty() const { return Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000168
169private:
George Rimar77b77792015-11-25 22:15:01 +0000170 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000171};
172
173template <class ELFT> struct DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000174 typedef typename ELFT::uint uintX_t;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000175 uint32_t Type;
176
Rafael Espindolaac568f92016-04-11 13:47:35 +0000177 SymbolBody *Sym;
Rafael Espindola34ffcbb2016-04-11 13:51:23 +0000178 const OutputSectionBase<ELFT> *OffsetSec;
Rafael Espindolaac568f92016-04-11 13:47:35 +0000179 uintX_t OffsetInSec;
180 bool UseSymVA;
181 uintX_t Addend;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000182
Rafael Espindola34ffcbb2016-04-11 13:51:23 +0000183 DynamicReloc(uint32_t Type, const OutputSectionBase<ELFT> *OffsetSec,
Rafael Espindolade9857e2016-02-04 21:33:05 +0000184 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
185 uintX_t Addend)
Rafael Espindola74031ba2016-04-07 15:20:56 +0000186 : Type(Type), Sym(Sym), OffsetSec(OffsetSec), OffsetInSec(OffsetInSec),
187 UseSymVA(UseSymVA), Addend(Addend) {}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000188};
189
190template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000191class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000192public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000193 typedef typename ELFT::Shdr Elf_Shdr;
194 typedef typename ELFT::Sym Elf_Sym;
195 typedef typename ELFT::SymRange Elf_Sym_Range;
196 typedef typename ELFT::uint uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000197 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000198 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000199
Rui Ueyama0db335f2015-10-07 16:58:54 +0000200 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000201 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000202 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000203 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000204 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000205
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000206 ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
Rafael Espindolae2c24612016-01-29 01:24:25 +0000207 return Symbols;
208 }
209
210 unsigned NumLocals = 0;
211 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000212
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000213private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000214 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000215 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000216
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000217 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000218
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000219 SymbolTable<ELFT> &Table;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000220
221 // A vector of symbols and their string table offsets.
222 std::vector<std::pair<SymbolBody *, size_t>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000223};
224
225template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000226class RelocationSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000227 typedef typename ELFT::Rel Elf_Rel;
228 typedef typename ELFT::Rela Elf_Rela;
229 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000230
231public:
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000232 RelocationSection(StringRef Name);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000233 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000234 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000235 void finalize() override;
236 void writeTo(uint8_t *Buf) override;
237 bool hasRelocs() const { return !Relocs.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000238
George Rimara07ff662015-12-21 10:12:06 +0000239 bool Static = false;
240
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000241private:
242 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000243};
244
245template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000246class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000247public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000248 typedef typename ELFT::Shdr Elf_Shdr;
249 typedef typename ELFT::Sym Elf_Sym;
250 typedef typename ELFT::Rel Elf_Rel;
251 typedef typename ELFT::Rela Elf_Rela;
252 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000253 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000254 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000255 void sortInitFini();
256 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000257 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000258 void finalize() override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000259 void
260 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) override;
Rafael Espindola71675852015-09-22 00:16:19 +0000261 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000262};
263
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000264template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000265class MergeOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000266 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000267
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000268 bool shouldTailMerge() const;
269
Rafael Espindolac159c962015-10-19 21:00:02 +0000270public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000271 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
272 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000273 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000274 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000275 unsigned getOffset(StringRef Val);
276 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000277
278private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000279 llvm::StringTableBuilder Builder;
Rafael Espindolac159c962015-10-19 21:00:02 +0000280};
281
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000282// FDE or CIE
283template <class ELFT> struct EHRegion {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000284 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000285 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
286 StringRef data() const;
287 EHInputSection<ELFT> *S;
288 unsigned Index;
289};
290
291template <class ELFT> struct Cie : public EHRegion<ELFT> {
292 Cie(EHInputSection<ELFT> *S, unsigned Index);
293 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000294 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000295};
296
297template <class ELFT>
298class EHOutputSection final : public OutputSectionBase<ELFT> {
299public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000300 typedef typename ELFT::uint uintX_t;
301 typedef typename ELFT::Shdr Elf_Shdr;
302 typedef typename ELFT::Rel Elf_Rel;
303 typedef typename ELFT::Rela Elf_Rela;
George Rimar9bec24a2016-02-18 14:20:08 +0000304 EHOutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000305 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000306 void finalize() override;
307 void
308 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000309
Rui Ueyamafc467e72016-03-13 05:06:50 +0000310 template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000311 void addSectionAux(EHInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000312
Rui Ueyama40845e62015-12-26 05:51:07 +0000313 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000314
315private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000316 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000317
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000318 std::vector<EHInputSection<ELFT> *> Sections;
319 std::vector<Cie<ELFT>> Cies;
320
321 // Maps CIE content + personality to a index in Cies.
Rafael Espindola156ed8d2016-02-10 13:19:32 +0000322 llvm::DenseMap<std::pair<StringRef, SymbolBody *>, unsigned> CieMap;
Rafael Espindola56004c52016-04-07 14:22:09 +0000323 bool Finalized = false;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000324};
325
Rafael Espindolac159c962015-10-19 21:00:02 +0000326template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000327class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000328public:
329 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000330 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000331};
332
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000333template <class ELFT>
334class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000335public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000336 typedef typename ELFT::uint uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000337 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000338 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000339 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000340 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000341 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000342 bool isDynamic() const { return Dynamic; }
343
344private:
345 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000346 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000347 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000348 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000349};
350
351template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000352class HashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000353 typedef typename ELFT::Word Elf_Word;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000354
355public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000356 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000357 void finalize() override;
358 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000359};
360
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000361// Outputs GNU Hash section. For detailed explanation see:
362// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
363template <class ELFT>
364class GnuHashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000365 typedef typename ELFT::Off Elf_Off;
366 typedef typename ELFT::Word Elf_Word;
367 typedef typename ELFT::uint uintX_t;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000368
369public:
370 GnuHashTableSection();
371 void finalize() override;
372 void writeTo(uint8_t *Buf) override;
373
Igor Kudrinf1d60292015-10-28 07:05:56 +0000374 // Adds symbols to the hash table.
375 // Sorts the input to satisfy GNU hash section requirements.
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000376 void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000377
378private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000379 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000380 static unsigned calcMaskWords(unsigned NumHashed);
381
382 void writeHeader(uint8_t *&Buf);
383 void writeBloomFilter(uint8_t *&Buf);
384 void writeHashTable(uint8_t *Buf);
385
Rui Ueyama861c7312016-02-17 05:40:03 +0000386 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000387 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000388 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000389 uint32_t Hash;
390 };
391
Rui Ueyama861c7312016-02-17 05:40:03 +0000392 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000393
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000394 unsigned MaskWords;
395 unsigned NBuckets;
396 unsigned Shift2;
397};
398
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000399template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000400class DynamicSection final : public OutputSectionBase<ELFT> {
401 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000402 typedef typename ELFT::Dyn Elf_Dyn;
403 typedef typename ELFT::Rel Elf_Rel;
404 typedef typename ELFT::Rela Elf_Rela;
405 typedef typename ELFT::Shdr Elf_Shdr;
406 typedef typename ELFT::Sym Elf_Sym;
407 typedef typename ELFT::uint uintX_t;
Rafael Espindolade069362016-01-25 21:32:04 +0000408
Rui Ueyama909cc682016-02-02 03:11:27 +0000409 // The .dynamic section contains information for the dynamic linker.
410 // The section consists of fixed size entries, which consist of
411 // type and value fields. Value are one of plain integers, symbol
412 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000413 struct Entry {
414 int32_t Tag;
415 union {
416 OutputSectionBase<ELFT> *OutSec;
417 uint64_t Val;
418 const SymbolBody *Sym;
419 };
420 enum KindT { SecAddr, SymAddr, PlainInt } Kind;
421 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
422 : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
423 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
424 Entry(int32_t Tag, const SymbolBody *Sym)
425 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
426 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000427
428 // finalize() fills this vector with the section contents. finalize()
429 // cannot directly create final section contents because when the
430 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000431 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000432
433public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000434 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000435 void finalize() override;
436 void writeTo(uint8_t *Buf) override;
437
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000438 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
439 OutputSectionBase<ELFT> *InitArraySec = nullptr;
440 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000441
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000442private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000443 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000444};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000445
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000446template <class ELFT>
447class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
448 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
449
450public:
451 MipsReginfoOutputSection();
452 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000453 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000454
455private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000456 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000457};
458
George Rimarf6bc65a2016-01-15 13:34:52 +0000459// --eh-frame-hdr option tells linker to construct a header for all the
460// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
461// and also to a PT_GNU_EH_FRAME segment.
462// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
463// calling dl_iterate_phdr.
464// This section contains a lookup table for quick binary search of FDEs.
465// Detailed info about internals can be found in Ian Lance Taylor's blog:
466// http://www.airs.com/blog/archives/460 (".eh_frame")
467// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
468template <class ELFT>
469class EhFrameHeader final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000470 typedef typename ELFT::uint uintX_t;
George Rimarf6bc65a2016-01-15 13:34:52 +0000471
472public:
473 EhFrameHeader();
474 void writeTo(uint8_t *Buf) override;
475
476 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
477 void assignEhFrame(EHOutputSection<ELFT> *Sec);
478 void reserveFde();
479
480 bool Live = false;
481
Rafael Espindola56004c52016-04-07 14:22:09 +0000482 EHOutputSection<ELFT> *Sec = nullptr;
483
George Rimarf6bc65a2016-01-15 13:34:52 +0000484private:
485 struct FdeData {
486 uint8_t Enc;
487 size_t Off;
488 uint8_t *PCRel;
489 };
490
491 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
492
George Rimarf6bc65a2016-01-15 13:34:52 +0000493 std::vector<FdeData> FdeList;
494};
495
Rui Ueyama3a41be22016-04-07 22:49:21 +0000496template <class ELFT> class BuildIdSection : public OutputSectionBase<ELFT> {
Rui Ueyama634ddf02016-03-11 20:51:53 +0000497public:
Rui Ueyama634ddf02016-03-11 20:51:53 +0000498 void writeTo(uint8_t *Buf) override;
Rui Ueyama3a41be22016-04-07 22:49:21 +0000499 virtual void update(ArrayRef<uint8_t> Buf) = 0;
500 virtual void writeBuildId() = 0;
501
502protected:
503 BuildIdSection(size_t HashSize);
504 size_t HashSize;
505 uint8_t *HashBuf = nullptr;
506};
507
508template <class ELFT> class BuildIdFnv1 final : public BuildIdSection<ELFT> {
509public:
510 BuildIdFnv1() : BuildIdSection<ELFT>(8) {}
511 void update(ArrayRef<uint8_t> Buf) override;
512 void writeBuildId() override;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000513
514private:
Rui Ueyama3a41be22016-04-07 22:49:21 +0000515 // 64-bit FNV-1 initial value
516 uint64_t Hash = 0xcbf29ce484222325;
517};
518
519template <class ELFT> class BuildIdMd5 final : public BuildIdSection<ELFT> {
520public:
521 BuildIdMd5() : BuildIdSection<ELFT>(16) {}
522 void update(ArrayRef<uint8_t> Buf) override;
523 void writeBuildId() override;
524
525private:
526 llvm::MD5 Hash;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000527};
528
Rui Ueyamad86ec302016-04-07 23:51:56 +0000529template <class ELFT> class BuildIdSha1 final : public BuildIdSection<ELFT> {
530public:
531 BuildIdSha1() : BuildIdSection<ELFT>(20) {}
532 void update(ArrayRef<uint8_t> Buf) override;
533 void writeBuildId() override;
534
535private:
536 llvm::SHA1 Hash;
537};
538
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000539// All output sections that are hadnled by the linker specially are
540// globally accessible. Writer initializes them, so don't use them
541// until Writer is initialized.
542template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000543 typedef typename ELFT::uint uintX_t;
544 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000545 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000546 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000547 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000548 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000549 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000550 static GotSection<ELFT> *Got;
551 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000552 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000553 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000554 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000555 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000556 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000557 static PltSection<ELFT> *Plt;
558 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000559 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000560 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000561 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000562 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000563 static SymbolTableSection<ELFT> *DynSymTab;
564 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000565 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000566 static OutputSectionBase<ELFT> *ElfHeader;
567 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000568};
Rui Ueyamad888d102015-10-09 19:34:55 +0000569
Rui Ueyama634ddf02016-03-11 20:51:53 +0000570template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000571template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000572template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000573template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000574template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000575template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
576template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000577template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000578template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000579template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000580template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000581template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000582template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
583template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000584template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000585template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000586template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000587template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000588template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
589template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000590template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000591template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
592template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000593
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000594} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000595} // namespace lld
596
597#endif // LLD_ELF_OUTPUT_SECTIONS_H