blob: a0d4df035d837d1da1c4d862a6a16a5b1c94f958 [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; }
75 uintX_t getFlags() { return Header.sh_flags; }
76 uintX_t getFileOff() { return Header.sh_offset; }
77 uintX_t getAlign() {
78 // 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 }
82 uint32_t getType() { 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;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +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(); }
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000115 uintX_t getMipsLocalFullAddr(const SymbolBody &B);
116 uintX_t getMipsLocalPageAddr(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;
141
142 uintX_t getMipsLocalEntryAddr(uintX_t EntryValue);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000143};
144
George Rimar648a2c32015-10-20 08:54:27 +0000145template <class ELFT>
146class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000147 typedef typename ELFT::uint uintX_t;
George Rimar648a2c32015-10-20 08:54:27 +0000148
149public:
150 GotPltSection();
151 void finalize() override;
152 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000153 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000154 bool empty() const;
George Rimar648a2c32015-10-20 08:54:27 +0000155
156private:
157 std::vector<const SymbolBody *> Entries;
158};
159
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000160template <class ELFT> class PltSection 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 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000166 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000167 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000168 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000169 bool empty() const { return Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000170
171private:
George Rimar77b77792015-11-25 22:15:01 +0000172 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000173};
174
175template <class ELFT> struct DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000176 typedef typename ELFT::uint uintX_t;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000177 uint32_t Type;
178
Rafael Espindolade9857e2016-02-04 21:33:05 +0000179 SymbolBody *Sym = nullptr;
Rafael Espindola56004c52016-04-07 14:22:09 +0000180 OutputSectionBase<ELFT> *OffsetSec = nullptr;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000181 uintX_t OffsetInSec = 0;
182 bool UseSymVA = false;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000183 uintX_t Addend = 0;
184
Rafael Espindola56004c52016-04-07 14:22:09 +0000185 DynamicReloc(uint32_t Type, OutputSectionBase<ELFT> *OffsetSec,
Rafael Espindolade9857e2016-02-04 21:33:05 +0000186 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
187 uintX_t Addend)
Rafael Espindola74031ba2016-04-07 15:20:56 +0000188 : Type(Type), Sym(Sym), OffsetSec(OffsetSec), OffsetInSec(OffsetInSec),
189 UseSymVA(UseSymVA), Addend(Addend) {}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000190};
191
192template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000193class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000194public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000195 typedef typename ELFT::Shdr Elf_Shdr;
196 typedef typename ELFT::Sym Elf_Sym;
197 typedef typename ELFT::SymRange Elf_Sym_Range;
198 typedef typename ELFT::uint uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000199 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000200 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000201
Rui Ueyama0db335f2015-10-07 16:58:54 +0000202 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000203 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000204 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000205 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000206 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000207
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000208 ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
Rafael Espindolae2c24612016-01-29 01:24:25 +0000209 return Symbols;
210 }
211
212 unsigned NumLocals = 0;
213 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000214
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000215private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000216 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000217 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000218
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000219 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000220
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000221 SymbolTable<ELFT> &Table;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000222
223 // A vector of symbols and their string table offsets.
224 std::vector<std::pair<SymbolBody *, size_t>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000225};
226
227template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000228class RelocationSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000229 typedef typename ELFT::Rel Elf_Rel;
230 typedef typename ELFT::Rela Elf_Rela;
231 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000232
233public:
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000234 RelocationSection(StringRef Name);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000235 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000236 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000237 void finalize() override;
238 void writeTo(uint8_t *Buf) override;
239 bool hasRelocs() const { return !Relocs.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000240
George Rimara07ff662015-12-21 10:12:06 +0000241 bool Static = false;
242
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000243private:
244 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000245};
246
247template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000248class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000249public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000250 typedef typename ELFT::Shdr Elf_Shdr;
251 typedef typename ELFT::Sym Elf_Sym;
252 typedef typename ELFT::Rel Elf_Rel;
253 typedef typename ELFT::Rela Elf_Rela;
254 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000255 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000256 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000257 void sortInitFini();
258 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000259 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000260 void finalize() override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000261 void
262 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) override;
Rafael Espindola71675852015-09-22 00:16:19 +0000263 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000264};
265
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000266template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000267class MergeOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000268 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000269
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000270 bool shouldTailMerge() const;
271
Rafael Espindolac159c962015-10-19 21:00:02 +0000272public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000273 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
274 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000275 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000276 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000277 unsigned getOffset(StringRef Val);
278 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000279
280private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000281 llvm::StringTableBuilder Builder;
Rafael Espindolac159c962015-10-19 21:00:02 +0000282};
283
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000284// FDE or CIE
285template <class ELFT> struct EHRegion {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000286 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000287 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
288 StringRef data() const;
289 EHInputSection<ELFT> *S;
290 unsigned Index;
291};
292
293template <class ELFT> struct Cie : public EHRegion<ELFT> {
294 Cie(EHInputSection<ELFT> *S, unsigned Index);
295 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000296 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000297};
298
299template <class ELFT>
300class EHOutputSection final : public OutputSectionBase<ELFT> {
301public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000302 typedef typename ELFT::uint uintX_t;
303 typedef typename ELFT::Shdr Elf_Shdr;
304 typedef typename ELFT::Rel Elf_Rel;
305 typedef typename ELFT::Rela Elf_Rela;
George Rimar9bec24a2016-02-18 14:20:08 +0000306 EHOutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000307 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000308 void finalize() override;
309 void
310 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000311
Rui Ueyamafc467e72016-03-13 05:06:50 +0000312 template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000313 void addSectionAux(EHInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000314
Rui Ueyama40845e62015-12-26 05:51:07 +0000315 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000316
317private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000318 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000319
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000320 std::vector<EHInputSection<ELFT> *> Sections;
321 std::vector<Cie<ELFT>> Cies;
322
323 // Maps CIE content + personality to a index in Cies.
Rafael Espindola156ed8d2016-02-10 13:19:32 +0000324 llvm::DenseMap<std::pair<StringRef, SymbolBody *>, unsigned> CieMap;
Rafael Espindola56004c52016-04-07 14:22:09 +0000325 bool Finalized = false;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000326};
327
Rafael Espindolac159c962015-10-19 21:00:02 +0000328template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000329class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000330public:
331 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000332 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000333};
334
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000335template <class ELFT>
336class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000337public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000338 typedef typename ELFT::uint uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000339 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000340 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000341 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000342 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000343 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000344 bool isDynamic() const { return Dynamic; }
345
346private:
347 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000348 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000349 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000350 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000351};
352
353template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000354class HashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000355 typedef typename ELFT::Word Elf_Word;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000356
357public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000358 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000359 void finalize() override;
360 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000361};
362
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000363// Outputs GNU Hash section. For detailed explanation see:
364// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
365template <class ELFT>
366class GnuHashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000367 typedef typename ELFT::Off Elf_Off;
368 typedef typename ELFT::Word Elf_Word;
369 typedef typename ELFT::uint uintX_t;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000370
371public:
372 GnuHashTableSection();
373 void finalize() override;
374 void writeTo(uint8_t *Buf) override;
375
Igor Kudrinf1d60292015-10-28 07:05:56 +0000376 // Adds symbols to the hash table.
377 // Sorts the input to satisfy GNU hash section requirements.
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000378 void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000379
380private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000381 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000382 static unsigned calcMaskWords(unsigned NumHashed);
383
384 void writeHeader(uint8_t *&Buf);
385 void writeBloomFilter(uint8_t *&Buf);
386 void writeHashTable(uint8_t *Buf);
387
Rui Ueyama861c7312016-02-17 05:40:03 +0000388 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000389 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000390 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000391 uint32_t Hash;
392 };
393
Rui Ueyama861c7312016-02-17 05:40:03 +0000394 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000395
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000396 unsigned MaskWords;
397 unsigned NBuckets;
398 unsigned Shift2;
399};
400
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000401template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000402class DynamicSection final : public OutputSectionBase<ELFT> {
403 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000404 typedef typename ELFT::Dyn Elf_Dyn;
405 typedef typename ELFT::Rel Elf_Rel;
406 typedef typename ELFT::Rela Elf_Rela;
407 typedef typename ELFT::Shdr Elf_Shdr;
408 typedef typename ELFT::Sym Elf_Sym;
409 typedef typename ELFT::uint uintX_t;
Rafael Espindolade069362016-01-25 21:32:04 +0000410
Rui Ueyama909cc682016-02-02 03:11:27 +0000411 // The .dynamic section contains information for the dynamic linker.
412 // The section consists of fixed size entries, which consist of
413 // type and value fields. Value are one of plain integers, symbol
414 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000415 struct Entry {
416 int32_t Tag;
417 union {
418 OutputSectionBase<ELFT> *OutSec;
419 uint64_t Val;
420 const SymbolBody *Sym;
421 };
422 enum KindT { SecAddr, SymAddr, PlainInt } Kind;
423 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
424 : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
425 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
426 Entry(int32_t Tag, const SymbolBody *Sym)
427 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
428 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000429
430 // finalize() fills this vector with the section contents. finalize()
431 // cannot directly create final section contents because when the
432 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000433 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000434
435public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000436 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000437 void finalize() override;
438 void writeTo(uint8_t *Buf) override;
439
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000440 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
441 OutputSectionBase<ELFT> *InitArraySec = nullptr;
442 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000443
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000444private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000445 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000446};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000447
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000448template <class ELFT>
449class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
450 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
451
452public:
453 MipsReginfoOutputSection();
454 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000455 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000456
457private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000458 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000459};
460
George Rimarf6bc65a2016-01-15 13:34:52 +0000461// --eh-frame-hdr option tells linker to construct a header for all the
462// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
463// and also to a PT_GNU_EH_FRAME segment.
464// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
465// calling dl_iterate_phdr.
466// This section contains a lookup table for quick binary search of FDEs.
467// Detailed info about internals can be found in Ian Lance Taylor's blog:
468// http://www.airs.com/blog/archives/460 (".eh_frame")
469// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
470template <class ELFT>
471class EhFrameHeader final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000472 typedef typename ELFT::uint uintX_t;
George Rimarf6bc65a2016-01-15 13:34:52 +0000473
474public:
475 EhFrameHeader();
476 void writeTo(uint8_t *Buf) override;
477
478 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
479 void assignEhFrame(EHOutputSection<ELFT> *Sec);
480 void reserveFde();
481
482 bool Live = false;
483
Rafael Espindola56004c52016-04-07 14:22:09 +0000484 EHOutputSection<ELFT> *Sec = nullptr;
485
George Rimarf6bc65a2016-01-15 13:34:52 +0000486private:
487 struct FdeData {
488 uint8_t Enc;
489 size_t Off;
490 uint8_t *PCRel;
491 };
492
493 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
494
George Rimarf6bc65a2016-01-15 13:34:52 +0000495 std::vector<FdeData> FdeList;
496};
497
Rui Ueyama3a41be22016-04-07 22:49:21 +0000498template <class ELFT> class BuildIdSection : public OutputSectionBase<ELFT> {
Rui Ueyama634ddf02016-03-11 20:51:53 +0000499public:
Rui Ueyama634ddf02016-03-11 20:51:53 +0000500 void writeTo(uint8_t *Buf) override;
Rui Ueyama3a41be22016-04-07 22:49:21 +0000501 virtual void update(ArrayRef<uint8_t> Buf) = 0;
502 virtual void writeBuildId() = 0;
503
504protected:
505 BuildIdSection(size_t HashSize);
506 size_t HashSize;
507 uint8_t *HashBuf = nullptr;
508};
509
510template <class ELFT> class BuildIdFnv1 final : public BuildIdSection<ELFT> {
511public:
512 BuildIdFnv1() : BuildIdSection<ELFT>(8) {}
513 void update(ArrayRef<uint8_t> Buf) override;
514 void writeBuildId() override;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000515
516private:
Rui Ueyama3a41be22016-04-07 22:49:21 +0000517 // 64-bit FNV-1 initial value
518 uint64_t Hash = 0xcbf29ce484222325;
519};
520
521template <class ELFT> class BuildIdMd5 final : public BuildIdSection<ELFT> {
522public:
523 BuildIdMd5() : BuildIdSection<ELFT>(16) {}
524 void update(ArrayRef<uint8_t> Buf) override;
525 void writeBuildId() override;
526
527private:
528 llvm::MD5 Hash;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000529};
530
Rui Ueyamad86ec302016-04-07 23:51:56 +0000531template <class ELFT> class BuildIdSha1 final : public BuildIdSection<ELFT> {
532public:
533 BuildIdSha1() : BuildIdSection<ELFT>(20) {}
534 void update(ArrayRef<uint8_t> Buf) override;
535 void writeBuildId() override;
536
537private:
538 llvm::SHA1 Hash;
539};
540
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000541// All output sections that are hadnled by the linker specially are
542// globally accessible. Writer initializes them, so don't use them
543// until Writer is initialized.
544template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000545 typedef typename ELFT::uint uintX_t;
546 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000547 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000548 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000549 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000550 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000551 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000552 static GotSection<ELFT> *Got;
553 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000554 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000555 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000556 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000557 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000558 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000559 static PltSection<ELFT> *Plt;
560 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000561 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000562 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000563 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000564 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000565 static SymbolTableSection<ELFT> *DynSymTab;
566 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000567 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000568 static OutputSectionBase<ELFT> *ElfHeader;
569 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000570};
Rui Ueyamad888d102015-10-09 19:34:55 +0000571
Rui Ueyama634ddf02016-03-11 20:51:53 +0000572template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000573template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000574template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000575template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000576template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000577template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
578template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000579template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000580template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000581template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000582template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000583template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000584template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
585template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000586template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000587template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000588template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000589template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000590template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
591template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000592template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000593template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
594template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000595
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000596} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000597} // namespace lld
598
599#endif // LLD_ELF_OUTPUT_SECTIONS_H