blob: 2dd213d11af19bcb70ac4a428b64bf68ffd892a4 [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"
Simon Atanasyan41325112016-06-19 21:39:37 +000014#include "Relocations.h"
Davide Italiano85121bb2015-09-25 03:56:11 +000015
Rui Ueyamaa0752a52016-03-13 20:28:29 +000016#include "lld/Core/LLVM.h"
17#include "llvm/MC/StringTableBuilder.h"
18#include "llvm/Object/ELF.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000019
20namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000021namespace elf {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000022
23class SymbolBody;
Rafael Espindola2deeb602016-07-21 20:18:30 +000024struct EhSectionPiece;
Rui Ueyama0b9a9032016-05-24 04:19:20 +000025template <class ELFT> class EhInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000026template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000027template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000028template <class ELFT> class MergeInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000029template <class ELFT> class OutputSection;
30template <class ELFT> class ObjectFile;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +000031template <class ELFT> class SharedFile;
32template <class ELFT> class SharedSymbol;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000033template <class ELFT> class DefinedRegular;
34
Rafael Espindola71675852015-09-22 00:16:19 +000035// This represents a section in an output file.
36// Different sub classes represent different types of sections. Some contain
37// input sections, others are created by the linker.
38// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000039// non-overlapping file offsets and VAs.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000040class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000041public:
Eugene Leviant9d278b62016-08-10 18:10:41 +000042 enum Kind {
43 Base,
Eugene Leviant9d278b62016-08-10 18:10:41 +000044 EHFrame,
Eugene Leviant9d278b62016-08-10 18:10:41 +000045 Merge,
Eugene Leviant9d278b62016-08-10 18:10:41 +000046 Regular,
Eugene Leviant9d278b62016-08-10 18:10:41 +000047 };
Rafael Espindola5805c4f2015-09-21 21:38:08 +000048
Rafael Espindolae08e78d2016-11-09 23:23:45 +000049 OutputSectionBase(StringRef Name, uint32_t Type, uint64_t Flags);
50 void setLMAOffset(uint64_t LMAOff) { LMAOffset = LMAOff; }
51 uint64_t getLMA() const { return Addr + LMAOffset; }
52 template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
Rafael Espindola63732f52016-11-04 13:20:45 +000053 StringRef getName() const { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000054
Rafael Espindolae08e78d2016-11-09 23:23:45 +000055 virtual void addSection(InputSectionData *C) {}
Eugene Leviant9d278b62016-08-10 18:10:41 +000056 virtual Kind getKind() const { return Base; }
Rafael Espindolae08e78d2016-11-09 23:23:45 +000057 static bool classof(const OutputSectionBase *B) {
Eugene Leviant9d278b62016-08-10 18:10:41 +000058 return B->getKind() == Base;
59 }
Rui Ueyama40845e62015-12-26 05:51:07 +000060
Rui Ueyama2317d0d2015-10-15 20:55:22 +000061 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000062
Rafael Espindola0b113672016-07-27 14:10:56 +000063 uint32_t getPhdrFlags() const;
Rui Ueyama3b04d832016-07-14 05:46:24 +000064
Rafael Espindolae08e78d2016-11-09 23:23:45 +000065 void updateAlignment(uint64_t Alignment) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000066 if (Alignment > Addralign)
67 Addralign = Alignment;
Rafael Espindola115f0f32015-11-03 14:13:40 +000068 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000069
Rui Ueyama47091902016-03-30 19:41:51 +000070 // If true, this section will be page aligned on disk.
71 // Typically the first section of each PT_LOAD segment has this flag.
72 bool PageAlign = false;
73
Eugene Leviant3d9abec2016-09-29 09:20:33 +000074 // Pointer to the first section in PT_LOAD segment, which this section
75 // also resides in. This field is used to correctly compute file offset
76 // of a section. When two sections share the same load segment, difference
77 // between their file offsets should be equal to difference between their
78 // virtual addresses. To compute some section offset we use the following
79 // formula: Off = Off_first + VA - VA_first.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000080 OutputSectionBase *FirstInPtLoad = nullptr;
Eugene Leviant3d9abec2016-09-29 09:20:33 +000081
Rafael Espindola5805c4f2015-09-21 21:38:08 +000082 virtual void finalize() {}
Rui Ueyama809d8e22016-06-23 04:33:42 +000083 virtual void assignOffsets() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000084 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000085 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000086
Rafael Espindola5805c4f2015-09-21 21:38:08 +000087 StringRef Name;
Rafael Espindola04a2e342016-11-09 01:42:41 +000088
89 // The following fields correspond to Elf_Shdr members.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000090 uint64_t Size = 0;
91 uint64_t Entsize = 0;
92 uint64_t Addralign = 0;
93 uint64_t Offset = 0;
94 uint64_t Flags = 0;
95 uint64_t LMAOffset = 0;
96 uint64_t Addr = 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +000097 uint32_t ShName = 0;
98 uint32_t Type = 0;
99 uint32_t Info = 0;
100 uint32_t Link = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000101};
102
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000103template <class ELFT> class OutputSection final : public OutputSectionBase {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000104
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000105public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000106 typedef typename ELFT::Shdr Elf_Shdr;
107 typedef typename ELFT::Sym Elf_Sym;
108 typedef typename ELFT::Rel Elf_Rel;
109 typedef typename ELFT::Rela Elf_Rela;
110 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000111 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000112 void addSection(InputSectionData *C) override;
George Rimar1a33c0f2016-11-10 09:05:20 +0000113 void sort(std::function<unsigned(InputSection<ELFT> *S)> Order);
Rui Ueyama5af83682016-02-11 23:41:38 +0000114 void sortInitFini();
115 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000116 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000117 void finalize() override;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000118 void assignOffsets() override;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000119 Kind getKind() const override { return Regular; }
120 static bool classof(const OutputSectionBase *B) {
121 return B->getKind() == Regular;
122 }
Rafael Espindola71675852015-09-22 00:16:19 +0000123 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000124};
125
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000126template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000127class MergeOutputSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000128 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000129
130public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000131 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
132 uintX_t Alignment);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000133 void addSection(InputSectionData *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000134 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000135 void finalize() override;
Peter Collingbournee29e1422016-05-05 04:10:12 +0000136 bool shouldTailMerge() const;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000137 Kind getKind() const override { return Merge; }
138 static bool classof(const OutputSectionBase *B) {
139 return B->getKind() == Merge;
140 }
Rafael Espindolac159c962015-10-19 21:00:02 +0000141
142private:
Rui Ueyama1880bbe2016-11-26 15:09:58 +0000143 void finalizeTailMerge();
144 void finalizeNoTailMerge();
145
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000146 llvm::StringTableBuilder Builder;
Rui Ueyama406b4692016-05-27 14:39:13 +0000147 std::vector<MergeInputSection<ELFT> *> Sections;
Rafael Espindolac159c962015-10-19 21:00:02 +0000148};
149
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000150struct CieRecord {
Rafael Espindola2deeb602016-07-21 20:18:30 +0000151 EhSectionPiece *Piece = nullptr;
152 std::vector<EhSectionPiece *> FdePieces;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000153};
154
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000155// Output section for .eh_frame.
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000156template <class ELFT> class EhOutputSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000157 typedef typename ELFT::uint uintX_t;
158 typedef typename ELFT::Shdr Elf_Shdr;
159 typedef typename ELFT::Rel Elf_Rel;
160 typedef typename ELFT::Rela Elf_Rela;
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000161
162public:
163 EhOutputSection();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000164 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000165 void finalize() override;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000166 bool empty() const { return Sections.empty(); }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000167
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000168 void addSection(InputSectionData *S) override;
169 Kind getKind() const override { return EHFrame; }
170 static bool classof(const OutputSectionBase *B) {
171 return B->getKind() == EHFrame;
172 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000173
Rui Ueyamade9777a2016-05-23 16:30:41 +0000174 size_t NumFdes = 0;
175
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000176private:
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000177 template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000178 void addSectionAux(EhInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000179
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000180 template <class RelTy>
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000181 CieRecord *addCie(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000182
183 template <class RelTy>
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000184 bool isFdeLive(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000185
Rui Ueyamae75e9332016-05-23 03:00:33 +0000186 uintX_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc);
187
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000188 std::vector<EhInputSection<ELFT> *> Sections;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000189 std::vector<CieRecord *> Cies;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000190
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000191 // CIE records are uniquified by their contents and personality functions.
192 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, SymbolBody *>, CieRecord> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000193};
194
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000195// All output sections that are hadnled by the linker specially are
196// globally accessible. Writer initializes them, so don't use them
197// until Writer is initialized.
198template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000199 typedef typename ELFT::uint uintX_t;
200 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000201
202 static uint8_t First;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000203 static EhOutputSection<ELFT> *EhFrame;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000204 static OutputSection<ELFT> *Bss;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000205 static OutputSectionBase *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000206 static uint8_t *OpdBuf;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000207 static Elf_Phdr *TlsPhdr;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000208 static OutputSectionBase *DebugInfo;
209 static OutputSectionBase *ElfHeader;
210 static OutputSectionBase *ProgramHeaders;
211 static OutputSectionBase *PreinitArray;
212 static OutputSectionBase *InitArray;
213 static OutputSectionBase *FiniArray;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000214};
Rui Ueyamad888d102015-10-09 19:34:55 +0000215
George Rimar6892afa2016-07-12 09:49:43 +0000216template <bool Is64Bits> struct SectionKey {
217 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
218 StringRef Name;
219 uint32_t Type;
220 uintX_t Flags;
221 uintX_t Alignment;
222};
223
224// This class knows how to create an output section for a given
225// input section. Output section type is determined by various
226// factors, including input section's sh_flags, sh_type and
227// linker scripts.
228template <class ELFT> class OutputSectionFactory {
229 typedef typename ELFT::Shdr Elf_Shdr;
230 typedef typename ELFT::uint uintX_t;
231 typedef typename elf::SectionKey<ELFT::Is64Bits> Key;
232
233public:
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000234 std::pair<OutputSectionBase *, bool> create(InputSectionBase<ELFT> *C,
235 StringRef OutsecName);
236 std::pair<OutputSectionBase *, bool>
Rafael Espindola10897f12016-09-13 14:23:14 +0000237 create(const SectionKey<ELFT::Is64Bits> &Key, InputSectionBase<ELFT> *C);
George Rimar6892afa2016-07-12 09:49:43 +0000238
George Rimar6892afa2016-07-12 09:49:43 +0000239private:
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000240 llvm::SmallDenseMap<Key, OutputSectionBase *> Map;
George Rimar6892afa2016-07-12 09:49:43 +0000241};
242
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000243template <class ELFT> uint64_t getHeaderSize() {
244 if (Config->OFormatBinary)
245 return 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000246 return Out<ELFT>::ElfHeader->Size + Out<ELFT>::ProgramHeaders->Size;
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000247}
248
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000249template <class ELFT> uint8_t Out<ELFT>::First;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000250template <class ELFT> EhOutputSection<ELFT> *Out<ELFT>::EhFrame;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000251template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000252template <class ELFT> OutputSectionBase *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000253template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000254template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000255template <class ELFT> OutputSectionBase *Out<ELFT>::DebugInfo;
256template <class ELFT> OutputSectionBase *Out<ELFT>::ElfHeader;
257template <class ELFT> OutputSectionBase *Out<ELFT>::ProgramHeaders;
258template <class ELFT> OutputSectionBase *Out<ELFT>::PreinitArray;
259template <class ELFT> OutputSectionBase *Out<ELFT>::InitArray;
260template <class ELFT> OutputSectionBase *Out<ELFT>::FiniArray;
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000261} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000262} // namespace lld
263
George Rimar6892afa2016-07-12 09:49:43 +0000264namespace llvm {
265template <bool Is64Bits> struct DenseMapInfo<lld::elf::SectionKey<Is64Bits>> {
266 typedef typename lld::elf::SectionKey<Is64Bits> Key;
267
268 static Key getEmptyKey();
269 static Key getTombstoneKey();
270 static unsigned getHashValue(const Key &Val);
271 static bool isEqual(const Key &LHS, const Key &RHS);
272};
273}
274
275#endif