blob: 28c621b1609e53da8a3338075ea725e97bf4a4e6 [file] [log] [blame]
Rafael Espindola9d06ab62015-09-22 00:01:39 +00001//===- InputSection.h -------------------------------------------*- C++ -*-===//
Michael J. Spencer84487f12015-07-24 21:03:07 +00002//
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
Rafael Espindola9d06ab62015-09-22 00:01:39 +000010#ifndef LLD_ELF_INPUT_SECTION_H
11#define LLD_ELF_INPUT_SECTION_H
Michael J. Spencer84487f12015-07-24 21:03:07 +000012
Rui Ueyamac4aaed92015-10-22 18:49:53 +000013#include "Config.h"
Rui Ueyama0fcdc732016-05-24 20:24:43 +000014#include "Relocations.h"
Peter Smithfb05cd92016-07-08 16:10:27 +000015#include "Thunks.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000016#include "lld/Core/LLVM.h"
Rui Ueyamab91bf1a2016-05-23 16:55:43 +000017#include "llvm/ADT/DenseSet.h"
Rui Ueyamac00718f2016-02-23 03:34:37 +000018#include "llvm/ADT/TinyPtrVector.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000019#include "llvm/Object/ELF.h"
20
21namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000022namespace elf {
Michael J. Spencer84487f12015-07-24 21:03:07 +000023
Rafael Espindola38c67a22016-04-15 14:41:56 +000024class SymbolBody;
25
Rui Ueyama0b289522016-02-25 18:43:51 +000026template <class ELFT> class ICF;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000027template <class ELFT> class DefinedRegular;
Eugene Leviant3e6b0272016-07-28 19:24:13 +000028template <class ELFT> class DefinedCommon;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029template <class ELFT> class ObjectFile;
Rafael Espindola832b93f2015-08-24 20:06:32 +000030template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000031template <class ELFT> class OutputSectionBase;
Michael J. Spencer84487f12015-07-24 21:03:07 +000032
Rafael Espindola71675852015-09-22 00:16:19 +000033// This corresponds to a section of an input file.
Rafael Espindolac159c962015-10-19 21:00:02 +000034template <class ELFT> class InputSectionBase {
35protected:
Rui Ueyama1d12ac12016-07-07 03:55:55 +000036 typedef typename ELFT::Chdr Elf_Chdr;
Rafael Espindola197d6a82016-04-22 16:39:59 +000037 typedef typename ELFT::Rel Elf_Rel;
38 typedef typename ELFT::Rela Elf_Rela;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000039 typedef typename ELFT::Shdr Elf_Shdr;
40 typedef typename ELFT::Sym Elf_Sym;
41 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +000042 const Elf_Shdr *Header;
43
44 // The file this section is from.
45 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000046
George Rimar602fbee2016-06-24 11:18:44 +000047 // If a section is compressed, this vector has uncompressed section data.
48 SmallVector<char, 0> Uncompressed;
49
Michael J. Spencer84487f12015-07-24 21:03:07 +000050public:
Simon Atanasyan85c6b442016-08-12 06:28:49 +000051 enum Kind {
52 Regular,
53 EHFrame,
54 Merge,
55 MipsReginfo,
56 MipsOptions,
57 MipsAbiFlags,
58 Layout
59 };
Rafael Espindolac159c962015-10-19 21:00:02 +000060 Kind SectionKind;
61
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000062 InputSectionBase() : Repl(this) {}
63
Rafael Espindolac159c962015-10-19 21:00:02 +000064 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
65 Kind SectionKind);
66 OutputSectionBase<ELFT> *OutSec = nullptr;
Rui Ueyama424b4082016-06-17 01:18:46 +000067 uint32_t Alignment;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000068
Rui Ueyamac4aaed92015-10-22 18:49:53 +000069 // Used for garbage collection.
Rui Ueyama6a9ef852016-02-25 18:49:09 +000070 bool Live;
Rui Ueyamac4aaed92015-10-22 18:49:53 +000071
Rui Ueyama0b289522016-02-25 18:43:51 +000072 // This pointer points to the "real" instance of this instance.
73 // Usually Repl == this. However, if ICF merges two sections,
74 // Repl pointer of one section points to another section. So,
75 // if you need to get a pointer to this instance, do not use
76 // this but instead this->Repl.
77 InputSectionBase<ELFT> *Repl;
78
Rafael Espindola71675852015-09-22 00:16:19 +000079 // Returns the size of this section (even if this is a common or BSS.)
Simon Atanasyan13f6da12016-03-31 21:26:23 +000080 size_t getSize() const;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000081
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000082 static InputSectionBase<ELFT> Discarded;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000083
Rafael Espindola5d83ccd2015-08-13 19:18:30 +000084 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +000085 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindolae1901cc2015-09-24 15:11:50 +000086 ObjectFile<ELFT> *getFile() const { return File; }
Rui Ueyama809d8e22016-06-23 04:33:42 +000087 uintX_t getOffset(const DefinedRegular<ELFT> &Sym) const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000088
89 // Translate an offset in the input section to an offset in the output
90 // section.
Rui Ueyama809d8e22016-06-23 04:33:42 +000091 uintX_t getOffset(uintX_t Offset) const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000092
Rafael Espindolac159c962015-10-19 21:00:02 +000093 ArrayRef<uint8_t> getSectionData() const;
Rui Ueyama12504642015-10-27 21:51:13 +000094
George Rimar602fbee2016-06-24 11:18:44 +000095 void uncompress();
96
Rafael Espindola22ef9562016-04-13 01:40:19 +000097 void relocate(uint8_t *Buf, uint8_t *BufEnd);
Rui Ueyama809d8e22016-06-23 04:33:42 +000098 std::vector<Relocation<ELFT>> Relocations;
George Rimar602fbee2016-06-24 11:18:44 +000099
100 bool Compressed;
Rafael Espindolac159c962015-10-19 21:00:02 +0000101};
102
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000103template <class ELFT> InputSectionBase<ELFT> InputSectionBase<ELFT>::Discarded;
Rafael Espindolac159c962015-10-19 21:00:02 +0000104
Rui Ueyama3ea87272016-05-22 00:13:04 +0000105// SectionPiece represents a piece of splittable section contents.
106struct SectionPiece {
Rui Ueyama34dc99e2016-05-22 01:15:32 +0000107 SectionPiece(size_t Off, ArrayRef<uint8_t> Data)
Davide Italianoe6c8fa42016-05-28 23:27:38 +0000108 : InputOff(Off), Data((const uint8_t *)Data.data()), Size(Data.size()),
Rui Ueyamad8849272016-05-25 16:37:01 +0000109 Live(!Config->GcSections) {}
110
111 ArrayRef<uint8_t> data() { return {Data, Size}; }
112 size_t size() const { return Size; }
Rui Ueyama34dc99e2016-05-22 01:15:32 +0000113
Rui Ueyama3ea87272016-05-22 00:13:04 +0000114 size_t InputOff;
115 size_t OutputOff = -1;
Rui Ueyamad8849272016-05-25 16:37:01 +0000116
117private:
118 // We use bitfields because SplitInputSection is accessed by
119 // std::upper_bound very often.
120 // We want to save bits to make it cache friendly.
Davide Italianoe6c8fa42016-05-28 23:27:38 +0000121 const uint8_t *Data;
Rui Ueyamad8849272016-05-25 16:37:01 +0000122 uint32_t Size : 31;
123
124public:
125 uint32_t Live : 1;
Rui Ueyama3ea87272016-05-22 00:13:04 +0000126};
127
Rafael Espindolac159c962015-10-19 21:00:02 +0000128// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000129template <class ELFT> class MergeInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000130 typedef typename ELFT::uint uintX_t;
131 typedef typename ELFT::Sym Elf_Sym;
132 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000133
134public:
135 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
136 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000137 void splitIntoPieces();
138
139 // Mark the piece at a given offset live. Used by GC.
140 void markLiveAt(uintX_t Offset) { LiveOffsets.insert(Offset); }
141
142 // Translate an offset in the input section to an offset
143 // in the output section.
Rui Ueyama809d8e22016-06-23 04:33:42 +0000144 uintX_t getOffset(uintX_t Offset) const;
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000145
Rui Ueyama406b4692016-05-27 14:39:13 +0000146 void finalizePieces();
147
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000148 // Splittable sections are handled as a sequence of data
149 // rather than a single large blob of data.
150 std::vector<SectionPiece> Pieces;
151
152 // Returns the SectionPiece at a given input section offset.
153 SectionPiece *getSectionPiece(uintX_t Offset);
154 const SectionPiece *getSectionPiece(uintX_t Offset) const;
155
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000156private:
Rui Ueyamad6bd1372016-08-03 04:39:42 +0000157 std::vector<SectionPiece> splitStrings(ArrayRef<uint8_t> A, size_t Size);
158 std::vector<SectionPiece> splitNonStrings(ArrayRef<uint8_t> A, size_t Size);
159
Rui Ueyama406b4692016-05-27 14:39:13 +0000160 llvm::DenseMap<uintX_t, uintX_t> OffsetMap;
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000161 llvm::DenseSet<uintX_t> LiveOffsets;
Rafael Espindolac159c962015-10-19 21:00:02 +0000162};
163
Rafael Espindola2deeb602016-07-21 20:18:30 +0000164struct EhSectionPiece : public SectionPiece {
165 EhSectionPiece(size_t Off, ArrayRef<uint8_t> Data, unsigned FirstRelocation)
166 : SectionPiece(Off, Data), FirstRelocation(FirstRelocation) {}
167 unsigned FirstRelocation;
168};
169
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000170// This corresponds to a .eh_frame section of an input file.
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000171template <class ELFT> class EhInputSection : public InputSectionBase<ELFT> {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000172public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000173 typedef typename ELFT::Shdr Elf_Shdr;
174 typedef typename ELFT::uint uintX_t;
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000175 EhInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000176 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama88abd9b2016-05-22 23:53:00 +0000177 void split();
Rafael Espindola2deeb602016-07-21 20:18:30 +0000178 template <class RelTy> void split(ArrayRef<RelTy> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000179
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000180 // Splittable sections are handled as a sequence of data
181 // rather than a single large blob of data.
Rafael Espindola2deeb602016-07-21 20:18:30 +0000182 std::vector<EhSectionPiece> Pieces;
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000183
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000184 // Relocation section that refer to this one.
185 const Elf_Shdr *RelocSection = nullptr;
186};
187
Rafael Espindolac159c962015-10-19 21:00:02 +0000188// This corresponds to a non SHF_MERGE section of an input file.
189template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
Rui Ueyama0b289522016-02-25 18:43:51 +0000190 friend ICF<ELFT>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000191 typedef InputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000192 typedef typename ELFT::Shdr Elf_Shdr;
193 typedef typename ELFT::Rela Elf_Rela;
194 typedef typename ELFT::Rel Elf_Rel;
195 typedef typename ELFT::Sym Elf_Sym;
196 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000197
198public:
199 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
200
201 // Write this section to a mmap'ed file, assuming Buf is pointing to
202 // beginning of the output section.
203 void writeTo(uint8_t *Buf);
204
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000205 // Relocation sections that refer to this one.
Rui Ueyamac00718f2016-02-23 03:34:37 +0000206 llvm::TinyPtrVector<const Elf_Shdr *> RelocSections;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000207
Rui Ueyamaedffd912015-10-14 21:00:23 +0000208 // The offset from beginning of the output sections this section was assigned
209 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000210 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000211
Rafael Espindolac159c962015-10-19 21:00:02 +0000212 static bool classof(const InputSectionBase<ELFT> *S);
George Rimar58941ee2016-02-25 08:23:37 +0000213
214 InputSectionBase<ELFT> *getRelocatedSection();
215
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000216 // Register thunk related to the symbol. When the section is written
217 // to a mmap'ed file, target is requested to write an actual thunk code.
Peter Smithfb05cd92016-07-08 16:10:27 +0000218 // Now thunks is supported for MIPS and ARM target only.
219 void addThunk(const Thunk<ELFT> *T);
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000220
221 // The offset of synthetic thunk code from beginning of this section.
222 uint64_t getThunkOff() const;
223
224 // Size of chunk with thunks code.
225 uint64_t getThunksSize() const;
226
Rui Ueyama2b6fb802016-04-28 18:42:04 +0000227 template <class RelTy>
228 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
229
George Rimar58941ee2016-02-25 08:23:37 +0000230private:
Rui Ueyamafc467e72016-03-13 05:06:50 +0000231 template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000232 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
Rui Ueyama0b289522016-02-25 18:43:51 +0000233
234 // Called by ICF to merge two input sections.
235 void replace(InputSection<ELFT> *Other);
236
237 // Used by ICF.
238 uint64_t GroupId = 0;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000239
Peter Smithfb05cd92016-07-08 16:10:27 +0000240 llvm::TinyPtrVector<const Thunk<ELFT> *> Thunks;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000241};
242
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000243// MIPS .reginfo section provides information on the registers used by the code
244// in the object file. Linker should collect this information and write a single
245// .reginfo section in the output file. The output section contains a union of
246// used registers masks taken from input .reginfo sections and final value
247// of the `_gp` symbol. For details: Chapter 4 / "Register Information" at
248// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
249template <class ELFT>
250class MipsReginfoInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000251 typedef typename ELFT::Shdr Elf_Shdr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000252
253public:
Rui Ueyama70eed362016-01-06 22:42:43 +0000254 MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000255 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama70eed362016-01-06 22:42:43 +0000256
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000257 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
258};
259
260template <class ELFT>
261class MipsOptionsInputSection : public InputSectionBase<ELFT> {
262 typedef typename ELFT::Shdr Elf_Shdr;
263
264public:
265 MipsOptionsInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
266 static bool classof(const InputSectionBase<ELFT> *S);
267
268 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000269};
270
Simon Atanasyan85c6b442016-08-12 06:28:49 +0000271template <class ELFT>
272class MipsAbiFlagsInputSection : public InputSectionBase<ELFT> {
273 typedef typename ELFT::Shdr Elf_Shdr;
274
275public:
276 MipsAbiFlagsInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
277 static bool classof(const InputSectionBase<ELFT> *S);
278
279 const llvm::object::Elf_Mips_ABIFlags<ELFT> *Flags = nullptr;
280};
281
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000282// Common symbols don't belong to any section. But it is easier for us
283// to handle them as if they belong to some input section. So we defined
284// this class. CommonInputSection is a virtual singleton class that
285// "contains" all common symbols.
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000286template <class ELFT> class CommonInputSection : public InputSection<ELFT> {
287 typedef typename ELFT::uint uintX_t;
288
289public:
Rui Ueyama09d4f172016-07-29 03:39:44 +0000290 CommonInputSection(std::vector<DefinedCommon<ELFT> *> Syms);
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000291
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000292 // The singleton instance of this class.
293 static CommonInputSection<ELFT> *X;
294
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000295private:
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000296 static typename ELFT::Shdr Hdr;
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000297};
298
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000299template <class ELFT> CommonInputSection<ELFT> *CommonInputSection<ELFT>::X;
300template <class ELFT> typename ELFT::Shdr CommonInputSection<ELFT>::Hdr;
301
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000302} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000303} // namespace lld
304
305#endif