blob: 45e1a232e2a927c6bf0e67fa80a395dc9940f53e [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
Rafael Espindola17cb7c02016-12-19 17:01:01 +000023struct PhdrEntry;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000024class SymbolBody;
Rafael Espindola2deeb602016-07-21 20:18:30 +000025struct EhSectionPiece;
Rui Ueyama0b9a9032016-05-24 04:19:20 +000026template <class ELFT> class EhInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000027template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000028template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000029template <class ELFT> class MergeInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000030template <class ELFT> class OutputSection;
31template <class ELFT> class ObjectFile;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +000032template <class ELFT> class SharedFile;
33template <class ELFT> class SharedSymbol;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000034template <class ELFT> class DefinedRegular;
35
Rafael Espindola71675852015-09-22 00:16:19 +000036// This represents a section in an output file.
37// Different sub classes represent different types of sections. Some contain
38// input sections, others are created by the linker.
39// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000040// non-overlapping file offsets and VAs.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000041class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000042public:
Eugene Leviant9d278b62016-08-10 18:10:41 +000043 enum Kind {
44 Base,
Eugene Leviant9d278b62016-08-10 18:10:41 +000045 EHFrame,
Eugene Leviant9d278b62016-08-10 18:10:41 +000046 Merge,
Eugene Leviant9d278b62016-08-10 18:10:41 +000047 Regular,
Eugene Leviant9d278b62016-08-10 18:10:41 +000048 };
Rafael Espindola5805c4f2015-09-21 21:38:08 +000049
Rafael Espindolae08e78d2016-11-09 23:23:45 +000050 OutputSectionBase(StringRef Name, uint32_t Type, uint64_t Flags);
51 void setLMAOffset(uint64_t LMAOff) { LMAOffset = LMAOff; }
52 uint64_t getLMA() const { return Addr + LMAOffset; }
53 template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
Rafael Espindola63732f52016-11-04 13:20:45 +000054 StringRef getName() const { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000055
Rafael Espindolae08e78d2016-11-09 23:23:45 +000056 virtual void addSection(InputSectionData *C) {}
Eugene Leviant9d278b62016-08-10 18:10:41 +000057 virtual Kind getKind() const { return Base; }
Rafael Espindolae08e78d2016-11-09 23:23:45 +000058 static bool classof(const OutputSectionBase *B) {
Eugene Leviant9d278b62016-08-10 18:10:41 +000059 return B->getKind() == Base;
60 }
Rui Ueyama40845e62015-12-26 05:51:07 +000061
Rui Ueyama2317d0d2015-10-15 20:55:22 +000062 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000063
Rafael Espindola0b113672016-07-27 14:10:56 +000064 uint32_t getPhdrFlags() const;
Rui Ueyama3b04d832016-07-14 05:46:24 +000065
Rafael Espindolae08e78d2016-11-09 23:23:45 +000066 void updateAlignment(uint64_t Alignment) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000067 if (Alignment > Addralign)
68 Addralign = Alignment;
Rafael Espindola115f0f32015-11-03 14:13:40 +000069 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000070
Rui Ueyama47091902016-03-30 19:41:51 +000071 // If true, this section will be page aligned on disk.
72 // Typically the first section of each PT_LOAD segment has this flag.
73 bool PageAlign = false;
74
Eugene Leviant3d9abec2016-09-29 09:20:33 +000075 // Pointer to the first section in PT_LOAD segment, which this section
76 // also resides in. This field is used to correctly compute file offset
77 // of a section. When two sections share the same load segment, difference
78 // between their file offsets should be equal to difference between their
79 // virtual addresses. To compute some section offset we use the following
80 // formula: Off = Off_first + VA - VA_first.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000081 OutputSectionBase *FirstInPtLoad = nullptr;
Eugene Leviant3d9abec2016-09-29 09:20:33 +000082
Rafael Espindola5805c4f2015-09-21 21:38:08 +000083 virtual void finalize() {}
Rui Ueyama809d8e22016-06-23 04:33:42 +000084 virtual void assignOffsets() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000085 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000086 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000087
Rafael Espindola5805c4f2015-09-21 21:38:08 +000088 StringRef Name;
Rafael Espindola04a2e342016-11-09 01:42:41 +000089
90 // The following fields correspond to Elf_Shdr members.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000091 uint64_t Size = 0;
92 uint64_t Entsize = 0;
93 uint64_t Addralign = 0;
94 uint64_t Offset = 0;
95 uint64_t Flags = 0;
96 uint64_t LMAOffset = 0;
97 uint64_t Addr = 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +000098 uint32_t ShName = 0;
99 uint32_t Type = 0;
100 uint32_t Info = 0;
101 uint32_t Link = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000102};
103
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000104template <class ELFT> class OutputSection final : public OutputSectionBase {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000105
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000106public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000107 typedef typename ELFT::Shdr Elf_Shdr;
108 typedef typename ELFT::Sym Elf_Sym;
109 typedef typename ELFT::Rel Elf_Rel;
110 typedef typename ELFT::Rela Elf_Rela;
111 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000112 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000113 void addSection(InputSectionData *C) override;
Rui Ueyama31270312016-12-20 01:51:08 +0000114 void sort(std::function<int(InputSection<ELFT> *S)> Order);
Rui Ueyama5af83682016-02-11 23:41:38 +0000115 void sortInitFini();
116 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000117 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000118 void finalize() override;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000119 void assignOffsets() override;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000120 Kind getKind() const override { return Regular; }
121 static bool classof(const OutputSectionBase *B) {
122 return B->getKind() == Regular;
123 }
Rafael Espindola71675852015-09-22 00:16:19 +0000124 std::vector<InputSection<ELFT> *> Sections;
Eugene Leviant84569e62016-11-29 08:05:44 +0000125
126 // Location in the output buffer.
127 uint8_t *Loc = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000128};
129
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000130template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000131class MergeOutputSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000132 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000133
134public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000135 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
136 uintX_t Alignment);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000137 void addSection(InputSectionData *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000138 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000139 void finalize() override;
Peter Collingbournee29e1422016-05-05 04:10:12 +0000140 bool shouldTailMerge() const;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000141 Kind getKind() const override { return Merge; }
142 static bool classof(const OutputSectionBase *B) {
143 return B->getKind() == Merge;
144 }
Rafael Espindolac159c962015-10-19 21:00:02 +0000145
146private:
Rui Ueyama1880bbe2016-11-26 15:09:58 +0000147 void finalizeTailMerge();
148 void finalizeNoTailMerge();
149
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000150 llvm::StringTableBuilder Builder;
Rui Ueyama406b4692016-05-27 14:39:13 +0000151 std::vector<MergeInputSection<ELFT> *> Sections;
Rafael Espindolac159c962015-10-19 21:00:02 +0000152};
153
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000154struct CieRecord {
Rafael Espindola2deeb602016-07-21 20:18:30 +0000155 EhSectionPiece *Piece = nullptr;
156 std::vector<EhSectionPiece *> FdePieces;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000157};
158
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000159// Output section for .eh_frame.
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000160template <class ELFT> class EhOutputSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000161 typedef typename ELFT::uint uintX_t;
162 typedef typename ELFT::Shdr Elf_Shdr;
163 typedef typename ELFT::Rel Elf_Rel;
164 typedef typename ELFT::Rela Elf_Rela;
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000165
166public:
167 EhOutputSection();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000168 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000169 void finalize() override;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000170 bool empty() const { return Sections.empty(); }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000171
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000172 void addSection(InputSectionData *S) override;
173 Kind getKind() const override { return EHFrame; }
174 static bool classof(const OutputSectionBase *B) {
175 return B->getKind() == EHFrame;
176 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000177
Rui Ueyamade9777a2016-05-23 16:30:41 +0000178 size_t NumFdes = 0;
179
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000180private:
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000181 template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000182 void addSectionAux(EhInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000183
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000184 template <class RelTy>
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000185 CieRecord *addCie(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000186
187 template <class RelTy>
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000188 bool isFdeLive(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000189
Rui Ueyamae75e9332016-05-23 03:00:33 +0000190 uintX_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc);
191
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000192 std::vector<EhInputSection<ELFT> *> Sections;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000193 std::vector<CieRecord *> Cies;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000194
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000195 // CIE records are uniquified by their contents and personality functions.
196 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, SymbolBody *>, CieRecord> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000197};
198
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000199// All output sections that are hadnled by the linker specially are
200// globally accessible. Writer initializes them, so don't use them
201// until Writer is initialized.
202template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000203 typedef typename ELFT::uint uintX_t;
204 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000205
206 static uint8_t First;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000207 static EhOutputSection<ELFT> *EhFrame;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000208 static OutputSection<ELFT> *Bss;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000209 static OutputSectionBase *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000210 static uint8_t *OpdBuf;
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000211 static PhdrEntry *TlsPhdr;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000212 static OutputSectionBase *DebugInfo;
213 static OutputSectionBase *ElfHeader;
214 static OutputSectionBase *ProgramHeaders;
215 static OutputSectionBase *PreinitArray;
216 static OutputSectionBase *InitArray;
217 static OutputSectionBase *FiniArray;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000218};
Rui Ueyamad888d102015-10-09 19:34:55 +0000219
Rafael Espindola72447082017-01-05 14:35:41 +0000220struct SectionKey {
George Rimar6892afa2016-07-12 09:49:43 +0000221 StringRef Name;
Rafael Espindola72447082017-01-05 14:35:41 +0000222 uint64_t Flags;
223 uint64_t Alignment;
George Rimar6892afa2016-07-12 09:49:43 +0000224};
225
226// This class knows how to create an output section for a given
227// input section. Output section type is determined by various
228// factors, including input section's sh_flags, sh_type and
229// linker scripts.
230template <class ELFT> class OutputSectionFactory {
231 typedef typename ELFT::Shdr Elf_Shdr;
232 typedef typename ELFT::uint uintX_t;
George Rimar6892afa2016-07-12 09:49:43 +0000233
234public:
Rafael Espindolabd3ab092017-01-05 14:52:46 +0000235 OutputSectionFactory();
236 ~OutputSectionFactory();
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000237 std::pair<OutputSectionBase *, bool> create(InputSectionBase<ELFT> *C,
238 StringRef OutsecName);
Rafael Espindola72447082017-01-05 14:35:41 +0000239 std::pair<OutputSectionBase *, bool> create(const SectionKey &Key,
240 InputSectionBase<ELFT> *C);
George Rimar6892afa2016-07-12 09:49:43 +0000241
George Rimar6892afa2016-07-12 09:49:43 +0000242private:
Rafael Espindola72447082017-01-05 14:35:41 +0000243 llvm::SmallDenseMap<SectionKey, OutputSectionBase *> Map;
George Rimar6892afa2016-07-12 09:49:43 +0000244};
245
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000246template <class ELFT> uint64_t getHeaderSize() {
247 if (Config->OFormatBinary)
248 return 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000249 return Out<ELFT>::ElfHeader->Size + Out<ELFT>::ProgramHeaders->Size;
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000250}
251
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000252template <class ELFT> uint8_t Out<ELFT>::First;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000253template <class ELFT> EhOutputSection<ELFT> *Out<ELFT>::EhFrame;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000254template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000255template <class ELFT> OutputSectionBase *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000256template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000257template <class ELFT> PhdrEntry *Out<ELFT>::TlsPhdr;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000258template <class ELFT> OutputSectionBase *Out<ELFT>::DebugInfo;
259template <class ELFT> OutputSectionBase *Out<ELFT>::ElfHeader;
260template <class ELFT> OutputSectionBase *Out<ELFT>::ProgramHeaders;
261template <class ELFT> OutputSectionBase *Out<ELFT>::PreinitArray;
262template <class ELFT> OutputSectionBase *Out<ELFT>::InitArray;
263template <class ELFT> OutputSectionBase *Out<ELFT>::FiniArray;
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000264} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000265} // namespace lld
266
George Rimar6892afa2016-07-12 09:49:43 +0000267
268#endif