blob: d8fc041ffe688898cf2acd06f40f3790ad7ce225 [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
13#include "lld/Core/LLVM.h"
14
15#include "llvm/MC/StringTableBuilder.h"
16#include "llvm/Object/ELF.h"
17
Davide Italiano85121bb2015-09-25 03:56:11 +000018#include "Config.h"
19
Rafael Espindola5805c4f2015-09-21 21:38:08 +000020#include <type_traits>
21
22namespace lld {
23namespace elf2 {
24
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 Espindola5805c4f2015-09-21 21:38:08 +000029template <class ELFT> class InputSection;
30template <class ELFT> class OutputSection;
31template <class ELFT> class ObjectFile;
32template <class ELFT> class DefinedRegular;
Rafael Espindolacd076f02015-09-25 18:19:03 +000033template <class ELFT> class ELFSymbolBody;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000034
Rafael Espindola5805c4f2015-09-21 21:38:08 +000035template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000036typename llvm::object::ELFFile<ELFT>::uintX_t getSymVA(const SymbolBody &S);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000037
38template <class ELFT>
39typename llvm::object::ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +000040getLocalRelTarget(const ObjectFile<ELFT> &File,
Hal Finkel230c5c52015-10-16 22:37:32 +000041 const typename llvm::object::ELFFile<ELFT>::Elf_Rel &Sym);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +000042bool canBePreempted(const SymbolBody *Body, bool NeedsGot);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000043template <class ELFT> bool includeInSymtab(const SymbolBody &B);
44
Rafael Espindola05a3dd22015-09-22 23:38:23 +000045bool includeInDynamicSymtab(const SymbolBody &B);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000046
47template <class ELFT>
48bool shouldKeepInSymtab(
Rafael Espindola444576d2015-10-09 19:25:07 +000049 const ObjectFile<ELFT> &File, StringRef Name,
50 const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym);
Davide Italiano85121bb2015-09-25 03:56:11 +000051
Rafael Espindola71675852015-09-22 00:16:19 +000052// This represents a section in an output file.
53// Different sub classes represent different types of sections. Some contain
54// input sections, others are created by the linker.
55// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000056// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000057template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000058public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000059 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
60 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000061
62 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
63 void setVA(uintX_t VA) { Header.sh_addr = VA; }
64 uintX_t getVA() const { return Header.sh_addr; }
65 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000066 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000067 StringRef getName() { return Name; }
68 void setNameOffset(uintX_t Offset) { Header.sh_name = Offset; }
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; }
83
Rafael Espindola5805c4f2015-09-21 21:38:08 +000084 virtual void finalize() {}
85 virtual void writeTo(uint8_t *Buf) = 0;
86
87protected:
88 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000089 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000090 ~OutputSectionBase() = default;
91};
92
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000093template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
94 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000095 typedef typename Base::uintX_t uintX_t;
96
97public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +000098 GotSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +000099 void finalize() override {
Rui Ueyama5f551ae2015-10-14 14:02:06 +0000100 this->Header.sh_size = Entries.size() * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000101 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000102 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000103 void addEntry(SymbolBody *Sym);
104 bool empty() const { return Entries.empty(); }
105 uintX_t getEntryAddr(const SymbolBody &B) const;
106
107private:
108 std::vector<const SymbolBody *> Entries;
109};
110
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000111template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
112 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000113 typedef typename Base::uintX_t uintX_t;
114
115public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000116 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000117 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000118 void writeTo(uint8_t *Buf) override;
119 void addEntry(SymbolBody *Sym);
120 bool empty() const { return Entries.empty(); }
121 uintX_t getEntryAddr(const SymbolBody &B) const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000122
123private:
124 std::vector<const SymbolBody *> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000125};
126
127template <class ELFT> struct DynamicReloc {
128 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
129 const InputSection<ELFT> &C;
130 const Elf_Rel &RI;
131};
132
133template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000134class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000135public:
136 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
137 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
138 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000139 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000140 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000141 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000142
Rui Ueyama0db335f2015-10-07 16:58:54 +0000143 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000144 void writeTo(uint8_t *Buf) override;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000145 void addSymbol(StringRef Name, bool isLocal = false);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000146 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000147 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000148
149private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000150 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000151 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000152
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000153 SymbolTable<ELFT> &Table;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000154 StringTableSection<ELFT> &StrTabSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000155 unsigned NumVisible = 0;
156 unsigned NumLocals = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000157};
158
159template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000160class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000161 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
162 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000163 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000164
165public:
Rui Ueyamac58656c2015-10-13 16:59:30 +0000166 RelocationSection(bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000167 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
168 void finalize() override;
169 void writeTo(uint8_t *Buf) override;
170 bool hasRelocs() const { return !Relocs.empty(); }
171 bool isRela() const { return IsRela; }
172
173private:
174 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000175 const bool IsRela;
176};
177
178template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000179class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000180public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000181 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
182 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
183 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
184 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000185 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000186 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rafael Espindola71675852015-09-22 00:16:19 +0000187 void addSection(InputSection<ELFT> *C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000188 void writeTo(uint8_t *Buf) override;
189
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000190private:
Rafael Espindola71675852015-09-22 00:16:19 +0000191 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000192};
193
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000194template <class ELFT>
195class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000196public:
197 InterpSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000198 void writeTo(uint8_t *Buf);
199};
200
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000201template <class ELFT>
202class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000203public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000204 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000205 StringTableSection(bool Dynamic);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000206 void add(StringRef S) { StrTabBuilder.add(S); }
207 size_t getFileOff(StringRef S) const { return StrTabBuilder.getOffset(S); }
208 StringRef data() const { return StrTabBuilder.data(); }
209 void writeTo(uint8_t *Buf) override;
210
211 void finalize() override {
212 StrTabBuilder.finalize(llvm::StringTableBuilder::ELF);
213 this->Header.sh_size = StrTabBuilder.data().size();
214 }
215
216 bool isDynamic() const { return Dynamic; }
217
218private:
219 const bool Dynamic;
220 llvm::StringTableBuilder StrTabBuilder;
221};
222
223template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000224class HashTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000225 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
226
227public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000228 HashTableSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000229 void addSymbol(SymbolBody *S);
Rui Ueyama0db335f2015-10-07 16:58:54 +0000230 void finalize() override;
231 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000232
233private:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000234 std::vector<uint32_t> Hashes;
235};
236
237template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000238class DynamicSection final : public OutputSectionBase<ELFT> {
239 typedef OutputSectionBase<ELFT> Base;
240 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000241 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
242 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000243 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000244 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000245
246public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000247 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000248 void finalize() override;
249 void writeTo(uint8_t *Buf) override;
250
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000251 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
252 OutputSectionBase<ELFT> *InitArraySec = nullptr;
253 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000254
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000255private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000256 SymbolTable<ELFT> &SymTab;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000257 const ELFSymbolBody<ELFT> *InitSym = nullptr;
258 const ELFSymbolBody<ELFT> *FiniSym = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000259};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000260
261// All output sections that are hadnled by the linker specially are
262// globally accessible. Writer initializes them, so don't use them
263// until Writer is initialized.
264template <class ELFT> struct Out {
265 static DynamicSection<ELFT> *Dynamic;
266 static GotSection<ELFT> *Got;
267 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000268 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000269 static OutputSection<ELFT> *Bss;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000270 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000271 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000272 static PltSection<ELFT> *Plt;
273 static RelocationSection<ELFT> *RelaDyn;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000274 static StringTableSection<ELFT> *DynStrTab;
275 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000276 static SymbolTableSection<ELFT> *DynSymTab;
277 static SymbolTableSection<ELFT> *SymTab;
278};
Rui Ueyamad888d102015-10-09 19:34:55 +0000279
280template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
281template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
282template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000283template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rui Ueyamad888d102015-10-09 19:34:55 +0000284template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000285template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000286template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000287template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
288template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000289template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
290template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000291template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
292template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000293}
294}
295#endif