blob: 683d8bb377f6813d96a76f63b05ea6d58ac88180 [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"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000020
21namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000022namespace elf {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000023
24class SymbolBody;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000025template <class ELFT> class SymbolTable;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000026template <class ELFT> class SymbolTableSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000027template <class ELFT> class StringTableSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000028template <class ELFT> class EHInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000029template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000030template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000031template <class ELFT> class MergeInputSection;
Simon Atanasyan1d7df402015-12-20 10:57:34 +000032template <class ELFT> class MipsReginfoInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000033template <class ELFT> class OutputSection;
34template <class ELFT> class ObjectFile;
35template <class ELFT> class DefinedRegular;
36
Rafael Espindola5805c4f2015-09-21 21:38:08 +000037template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000038static inline typename ELFT::uint getAddend(const typename ELFT::Rel &Rel) {
Rafael Espindola932efcf2015-10-19 20:24:44 +000039 return 0;
40}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000041
42template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000043static inline typename ELFT::uint getAddend(const typename ELFT::Rela &Rel) {
Rafael Espindola932efcf2015-10-19 20:24:44 +000044 return Rel.r_addend;
45}
46
George Rimar12737b72016-02-25 08:40:26 +000047bool isValidCIdentifier(StringRef S);
48
Rafael Espindola71675852015-09-22 00:16:19 +000049// This represents a section in an output file.
50// Different sub classes represent different types of sections. Some contain
51// input sections, others are created by the linker.
52// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000053// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000054template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000055public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +000056 typedef typename ELFT::uint uintX_t;
57 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000058
George Rimar9bec24a2016-02-18 14:20:08 +000059 OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000060 void setVA(uintX_t VA) { Header.sh_addr = VA; }
61 uintX_t getVA() const { return Header.sh_addr; }
62 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000063 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000064 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000065 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000066
Rui Ueyama40845e62015-12-26 05:51:07 +000067 virtual void addSection(InputSectionBase<ELFT> *C) {}
68
Rui Ueyama2317d0d2015-10-15 20:55:22 +000069 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000070
71 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000072 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000073 void setSize(uintX_t Val) { Header.sh_size = Val; }
74 uintX_t getFlags() { return Header.sh_flags; }
75 uintX_t getFileOff() { return Header.sh_offset; }
76 uintX_t getAlign() {
77 // The ELF spec states that a value of 0 means the section has no alignment
78 // constraits.
79 return std::max<uintX_t>(Header.sh_addralign, 1);
80 }
81 uint32_t getType() { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +000082 void updateAlign(uintX_t Align) {
83 if (Align > Header.sh_addralign)
84 Header.sh_addralign = Align;
85 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000086
Rui Ueyama47091902016-03-30 19:41:51 +000087 // If true, this section will be page aligned on disk.
88 // Typically the first section of each PT_LOAD segment has this flag.
89 bool PageAlign = false;
90
Rafael Espindola5805c4f2015-09-21 21:38:08 +000091 virtual void finalize() {}
Rafael Espindola56004c52016-04-07 14:22:09 +000092 virtual void
93 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000094 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000095 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000096
97protected:
98 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000099 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000100};
101
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000102template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
103 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000104 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000105
106public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000107 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000108 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000109 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000110 void addEntry(SymbolBody &Sym);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000111 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000112 bool addTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000113 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000114 uintX_t getMipsLocalFullAddr(const SymbolBody &B);
115 uintX_t getMipsLocalPageAddr(uintX_t Addr);
George Rimar90cd0a82015-12-01 19:20:26 +0000116 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
Rafael Espindola74031ba2016-04-07 15:20:56 +0000117 uintX_t getGlobalDynOffset(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000118 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000119
Igor Kudrin304860a2015-11-12 04:39:49 +0000120 // Returns the symbol which corresponds to the first entry of the global part
121 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
122 // table properties.
123 // Returns nullptr if the global part is empty.
124 const SymbolBody *getMipsFirstGlobalEntry() const;
125
126 // Returns the number of entries in the local part of GOT including
127 // the number of reserved entries. This method is MIPS-specific.
128 unsigned getMipsLocalEntriesNum() const;
129
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000130 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
Rafael Espindola74031ba2016-04-07 15:20:56 +0000131 uint32_t getTlsIndexOff() { return TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000132
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000133private:
134 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000135 uint32_t TlsIndexOff = -1;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000136 uint32_t MipsLocalEntries = 0;
Simon Atanasyand2980d32016-03-29 14:07:22 +0000137 // Output sections referenced by MIPS GOT relocations.
138 llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000139 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
140
141 uintX_t getMipsLocalEntryAddr(uintX_t EntryValue);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000142};
143
George Rimar648a2c32015-10-20 08:54:27 +0000144template <class ELFT>
145class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000146 typedef typename ELFT::uint uintX_t;
George Rimar648a2c32015-10-20 08:54:27 +0000147
148public:
149 GotPltSection();
150 void finalize() override;
151 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000152 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000153 bool empty() const;
George Rimar648a2c32015-10-20 08:54:27 +0000154
155private:
156 std::vector<const SymbolBody *> Entries;
157};
158
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000159template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
160 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000161 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000162
163public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000164 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000165 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000166 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000167 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000168 bool empty() const { return Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000169
170private:
George Rimar77b77792015-11-25 22:15:01 +0000171 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000172};
173
174template <class ELFT> struct DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000175 typedef typename ELFT::uint uintX_t;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000176 uint32_t Type;
177
Rafael Espindolade9857e2016-02-04 21:33:05 +0000178 SymbolBody *Sym = nullptr;
Rafael Espindola56004c52016-04-07 14:22:09 +0000179 OutputSectionBase<ELFT> *OffsetSec = nullptr;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000180 uintX_t OffsetInSec = 0;
181 bool UseSymVA = false;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000182 uintX_t Addend = 0;
183
Rafael Espindola56004c52016-04-07 14:22:09 +0000184 DynamicReloc(uint32_t Type, OutputSectionBase<ELFT> *OffsetSec,
Rafael Espindolade9857e2016-02-04 21:33:05 +0000185 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
186 uintX_t Addend)
Rafael Espindola74031ba2016-04-07 15:20:56 +0000187 : Type(Type), Sym(Sym), OffsetSec(OffsetSec), OffsetInSec(OffsetInSec),
188 UseSymVA(UseSymVA), Addend(Addend) {}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000189};
190
191template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000192class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000193public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000194 typedef typename ELFT::Shdr Elf_Shdr;
195 typedef typename ELFT::Sym Elf_Sym;
196 typedef typename ELFT::SymRange Elf_Sym_Range;
197 typedef typename ELFT::uint uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000198 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000199 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000200
Rui Ueyama0db335f2015-10-07 16:58:54 +0000201 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000202 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000203 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000204 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000205 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000206
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000207 ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
Rafael Espindolae2c24612016-01-29 01:24:25 +0000208 return Symbols;
209 }
210
211 unsigned NumLocals = 0;
212 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000213
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000214private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000215 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000216 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000217
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000218 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000219 static uint8_t getSymbolBinding(SymbolBody *Body);
220
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 Ueyama15ef5e12015-10-07 19:18:16 +0000531// All output sections that are hadnled by the linker specially are
532// globally accessible. Writer initializes them, so don't use them
533// until Writer is initialized.
534template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000535 typedef typename ELFT::uint uintX_t;
536 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000537 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000538 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000539 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000540 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000541 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000542 static GotSection<ELFT> *Got;
543 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000544 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000545 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000546 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000547 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000548 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000549 static PltSection<ELFT> *Plt;
550 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000551 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000552 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000553 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000554 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000555 static SymbolTableSection<ELFT> *DynSymTab;
556 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000557 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000558 static OutputSectionBase<ELFT> *ElfHeader;
559 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000560};
Rui Ueyamad888d102015-10-09 19:34:55 +0000561
Rui Ueyama634ddf02016-03-11 20:51:53 +0000562template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000563template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000564template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000565template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000566template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000567template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
568template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000569template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000570template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000571template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000572template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000573template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000574template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
575template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000576template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000577template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000578template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000579template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000580template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
581template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000582template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000583template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
584template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000585
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000586} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000587} // namespace lld
588
589#endif // LLD_ELF_OUTPUT_SECTIONS_H