blob: ecf49eea3bfabe23ed4bd321c930d44fbeda844f [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>
Rafael Espindola932efcf2015-10-19 20:24:44 +000036static inline typename llvm::object::ELFFile<ELFT>::uintX_t
37getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rel &Rel) {
38 return 0;
39}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000040
41template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000042static inline typename llvm::object::ELFFile<ELFT>::uintX_t
43getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rela &Rel) {
44 return Rel.r_addend;
45}
46
47template <class ELFT>
48typename llvm::object::ELFFile<ELFT>::uintX_t getSymVA(const SymbolBody &S);
49
50template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +000051typename llvm::object::ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +000052getLocalRelTarget(const ObjectFile<ELFT> &File,
Rafael Espindola932efcf2015-10-19 20:24:44 +000053 const llvm::object::Elf_Rel_Impl<ELFT, IsRela> &Rel);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +000054bool canBePreempted(const SymbolBody *Body, bool NeedsGot);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000055template <class ELFT> bool includeInSymtab(const SymbolBody &B);
56
Rafael Espindola05a3dd22015-09-22 23:38:23 +000057bool includeInDynamicSymtab(const SymbolBody &B);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000058
59template <class ELFT>
60bool shouldKeepInSymtab(
Rafael Espindola444576d2015-10-09 19:25:07 +000061 const ObjectFile<ELFT> &File, StringRef Name,
62 const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym);
Davide Italiano85121bb2015-09-25 03:56:11 +000063
Rafael Espindola71675852015-09-22 00:16:19 +000064// This represents a section in an output file.
65// Different sub classes represent different types of sections. Some contain
66// input sections, others are created by the linker.
67// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000068// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000069template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000070public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000071 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
72 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000073
74 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
75 void setVA(uintX_t VA) { Header.sh_addr = VA; }
76 uintX_t getVA() const { return Header.sh_addr; }
77 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000078 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000079 StringRef getName() { return Name; }
80 void setNameOffset(uintX_t Offset) { Header.sh_name = Offset; }
81
Rui Ueyama2317d0d2015-10-15 20:55:22 +000082 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000083
84 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000085 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000086 void setSize(uintX_t Val) { Header.sh_size = Val; }
87 uintX_t getFlags() { return Header.sh_flags; }
88 uintX_t getFileOff() { return Header.sh_offset; }
89 uintX_t getAlign() {
90 // The ELF spec states that a value of 0 means the section has no alignment
91 // constraits.
92 return std::max<uintX_t>(Header.sh_addralign, 1);
93 }
94 uint32_t getType() { return Header.sh_type; }
95
Rafael Espindola5805c4f2015-09-21 21:38:08 +000096 virtual void finalize() {}
97 virtual void writeTo(uint8_t *Buf) = 0;
98
99protected:
100 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000101 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000102 ~OutputSectionBase() = default;
103};
104
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000105template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
106 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000107 typedef typename Base::uintX_t uintX_t;
108
109public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000110 GotSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000111 void finalize() override {
Rui Ueyama5f551ae2015-10-14 14:02:06 +0000112 this->Header.sh_size = Entries.size() * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000113 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000114 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000115 void addEntry(SymbolBody *Sym);
116 bool empty() const { return Entries.empty(); }
117 uintX_t getEntryAddr(const SymbolBody &B) const;
118
119private:
120 std::vector<const SymbolBody *> Entries;
121};
122
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000123template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
124 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000125 typedef typename Base::uintX_t uintX_t;
126
127public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000128 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000129 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000130 void writeTo(uint8_t *Buf) override;
131 void addEntry(SymbolBody *Sym);
132 bool empty() const { return Entries.empty(); }
133 uintX_t getEntryAddr(const SymbolBody &B) const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000134
135private:
136 std::vector<const SymbolBody *> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000137};
138
139template <class ELFT> struct DynamicReloc {
140 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
141 const InputSection<ELFT> &C;
142 const Elf_Rel &RI;
143};
144
145template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000146class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000147public:
148 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
149 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
150 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000151 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000152 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000153 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000154
Rui Ueyama0db335f2015-10-07 16:58:54 +0000155 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000156 void writeTo(uint8_t *Buf) override;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000157 void addSymbol(StringRef Name, bool isLocal = false);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000158 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000159 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000160
161private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000162 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000163 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000164
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000165 SymbolTable<ELFT> &Table;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000166 StringTableSection<ELFT> &StrTabSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000167 unsigned NumVisible = 0;
168 unsigned NumLocals = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000169};
170
171template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000172class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000173 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
174 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000175 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000176
177public:
Rui Ueyamac58656c2015-10-13 16:59:30 +0000178 RelocationSection(bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000179 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
180 void finalize() override;
181 void writeTo(uint8_t *Buf) override;
182 bool hasRelocs() const { return !Relocs.empty(); }
183 bool isRela() const { return IsRela; }
184
185private:
186 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000187 const bool IsRela;
188};
189
190template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000191class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000192public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000193 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
194 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
195 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
196 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000197 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000198 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rafael Espindola71675852015-09-22 00:16:19 +0000199 void addSection(InputSection<ELFT> *C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000200 void writeTo(uint8_t *Buf) override;
201
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000202private:
Rafael Espindola71675852015-09-22 00:16:19 +0000203 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000204};
205
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000206template <class ELFT>
207class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000208public:
209 InterpSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000210 void writeTo(uint8_t *Buf);
211};
212
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000213template <class ELFT>
214class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000215public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000216 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000217 StringTableSection(bool Dynamic);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000218 void add(StringRef S) { StrTabBuilder.add(S); }
219 size_t getFileOff(StringRef S) const { return StrTabBuilder.getOffset(S); }
220 StringRef data() const { return StrTabBuilder.data(); }
221 void writeTo(uint8_t *Buf) override;
222
223 void finalize() override {
224 StrTabBuilder.finalize(llvm::StringTableBuilder::ELF);
225 this->Header.sh_size = StrTabBuilder.data().size();
226 }
227
228 bool isDynamic() const { return Dynamic; }
229
230private:
231 const bool Dynamic;
232 llvm::StringTableBuilder StrTabBuilder;
233};
234
235template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000236class HashTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000237 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
238
239public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000240 HashTableSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000241 void addSymbol(SymbolBody *S);
Rui Ueyama0db335f2015-10-07 16:58:54 +0000242 void finalize() override;
243 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000244
245private:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000246 std::vector<uint32_t> Hashes;
247};
248
249template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000250class DynamicSection final : public OutputSectionBase<ELFT> {
251 typedef OutputSectionBase<ELFT> Base;
252 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000253 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
254 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000255 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000256 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000257
258public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000259 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000260 void finalize() override;
261 void writeTo(uint8_t *Buf) override;
262
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000263 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
264 OutputSectionBase<ELFT> *InitArraySec = nullptr;
265 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000266
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000267private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000268 SymbolTable<ELFT> &SymTab;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000269 const ELFSymbolBody<ELFT> *InitSym = nullptr;
270 const ELFSymbolBody<ELFT> *FiniSym = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000271};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000272
273// All output sections that are hadnled by the linker specially are
274// globally accessible. Writer initializes them, so don't use them
275// until Writer is initialized.
276template <class ELFT> struct Out {
277 static DynamicSection<ELFT> *Dynamic;
278 static GotSection<ELFT> *Got;
279 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000280 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000281 static OutputSection<ELFT> *Bss;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000282 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000283 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000284 static PltSection<ELFT> *Plt;
285 static RelocationSection<ELFT> *RelaDyn;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000286 static StringTableSection<ELFT> *DynStrTab;
287 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000288 static SymbolTableSection<ELFT> *DynSymTab;
289 static SymbolTableSection<ELFT> *SymTab;
290};
Rui Ueyamad888d102015-10-09 19:34:55 +0000291
292template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
293template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
294template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000295template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rui Ueyamad888d102015-10-09 19:34:55 +0000296template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000297template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000298template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000299template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
300template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000301template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
302template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000303template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
304template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000305}
306}
307#endif