blob: cd122cf70298fb8baa5332a251a07e27cd6da3a0 [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"
Michael J. Spencer84487f12015-07-24 21:03:07 +000014#include "lld/Core/LLVM.h"
Rui Ueyamab91bf1a2016-05-23 16:55:43 +000015#include "llvm/ADT/DenseSet.h"
Rui Ueyamac00718f2016-02-23 03:34:37 +000016#include "llvm/ADT/TinyPtrVector.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000017#include "llvm/Object/ELF.h"
18
19namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000020namespace elf {
Michael J. Spencer84487f12015-07-24 21:03:07 +000021
Rafael Espindola38c67a22016-04-15 14:41:56 +000022class SymbolBody;
23
Rui Ueyama0b289522016-02-25 18:43:51 +000024template <class ELFT> class ICF;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000025template <class ELFT> class DefinedRegular;
Michael J. Spencer84487f12015-07-24 21:03:07 +000026template <class ELFT> class ObjectFile;
Rafael Espindola832b93f2015-08-24 20:06:32 +000027template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000028template <class ELFT> class OutputSectionBase;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029
Rafael Espindola22ef9562016-04-13 01:40:19 +000030enum RelExpr {
31 R_ABS,
32 R_GOT,
Rafael Espindola3f5d6342016-04-18 12:07:13 +000033 R_GOTONLY_PC,
34 R_GOTREL,
35 R_GOT_FROM_END,
Rafael Espindolade2c76e2016-05-04 14:38:55 +000036 R_GOT_OFF,
Rafael Espindola22ef9562016-04-13 01:40:19 +000037 R_GOT_PAGE_PC,
38 R_GOT_PC,
Rafael Espindolaebb04b92016-05-04 14:44:22 +000039 R_HINT,
Rafael Espindola22ef9562016-04-13 01:40:19 +000040 R_MIPS_GOT_LOCAL,
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +000041 R_MIPS_GOT_LOCAL_PAGE,
Rafael Espindola3f5d6342016-04-18 12:07:13 +000042 R_NEG_TLS,
Rafael Espindola22ef9562016-04-13 01:40:19 +000043 R_PAGE_PC,
44 R_PC,
45 R_PLT,
46 R_PLT_PC,
47 R_PPC_OPD,
48 R_PPC_PLT_OPD,
49 R_PPC_TOC,
50 R_RELAX_TLS_GD_TO_IE,
Rafael Espindola22ef9562016-04-13 01:40:19 +000051 R_RELAX_TLS_GD_TO_LE,
52 R_RELAX_TLS_IE_TO_LE,
53 R_RELAX_TLS_LD_TO_LE,
54 R_SIZE,
55 R_THUNK,
Rafael Espindola3f5d6342016-04-18 12:07:13 +000056 R_TLS,
Rafael Espindola22ef9562016-04-13 01:40:19 +000057 R_TLSGD,
58 R_TLSGD_PC,
59 R_TLSLD,
60 R_TLSLD_PC
61};
62
Rafael Espindolac6b17bd2016-04-20 17:30:22 +000063inline bool refersToGotEntry(RelExpr Expr) {
Simon Atanasyan4e3a15c2016-05-15 18:13:50 +000064 return Expr == R_GOT || Expr == R_GOT_OFF || Expr == R_MIPS_GOT_LOCAL ||
65 Expr == R_MIPS_GOT_LOCAL_PAGE || Expr == R_GOT_PAGE_PC ||
Rafael Espindolaa85efd92016-04-30 01:15:17 +000066 Expr == R_GOT_PC || Expr == R_GOT_FROM_END || Expr == R_TLSGD ||
67 Expr == R_TLSGD_PC;
Rafael Espindolac6b17bd2016-04-20 17:30:22 +000068}
69
Rafael Espindola22ef9562016-04-13 01:40:19 +000070struct Relocation {
71 RelExpr Expr;
72 uint32_t Type;
73 uint64_t Offset;
74 uint64_t Addend;
75 SymbolBody *Sym;
76};
77
Rafael Espindola71675852015-09-22 00:16:19 +000078// This corresponds to a section of an input file.
Rafael Espindolac159c962015-10-19 21:00:02 +000079template <class ELFT> class InputSectionBase {
80protected:
Rafael Espindola197d6a82016-04-22 16:39:59 +000081 typedef typename ELFT::Rel Elf_Rel;
82 typedef typename ELFT::Rela Elf_Rela;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000083 typedef typename ELFT::Shdr Elf_Shdr;
84 typedef typename ELFT::Sym Elf_Sym;
85 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +000086 const Elf_Shdr *Header;
87
88 // The file this section is from.
89 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000090
91public:
Simon Atanasyanadd74f32016-05-04 10:07:38 +000092 enum Kind { Regular, EHFrame, Merge, MipsReginfo, MipsOptions };
Rafael Espindolac159c962015-10-19 21:00:02 +000093 Kind SectionKind;
94
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000095 InputSectionBase() : Repl(this) {}
96
Rafael Espindolac159c962015-10-19 21:00:02 +000097 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
98 Kind SectionKind);
99 OutputSectionBase<ELFT> *OutSec = nullptr;
Rui Ueyama6a9ef852016-02-25 18:49:09 +0000100 uint32_t Align;
Rafael Espindola83b0dc62015-08-13 22:21:37 +0000101
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000102 // Used for garbage collection.
Rui Ueyama6a9ef852016-02-25 18:49:09 +0000103 bool Live;
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000104
Rui Ueyama0b289522016-02-25 18:43:51 +0000105 // This pointer points to the "real" instance of this instance.
106 // Usually Repl == this. However, if ICF merges two sections,
107 // Repl pointer of one section points to another section. So,
108 // if you need to get a pointer to this instance, do not use
109 // this but instead this->Repl.
110 InputSectionBase<ELFT> *Repl;
111
Rafael Espindola71675852015-09-22 00:16:19 +0000112 // Returns the size of this section (even if this is a common or BSS.)
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000113 size_t getSize() const;
Rafael Espindola83b0dc62015-08-13 22:21:37 +0000114
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000115 static InputSectionBase<ELFT> Discarded;
Rafael Espindola83b0dc62015-08-13 22:21:37 +0000116
Rafael Espindola5d83ccd2015-08-13 19:18:30 +0000117 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +0000118 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000119 ObjectFile<ELFT> *getFile() const { return File; }
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000120 uintX_t getOffset(const DefinedRegular<ELFT> &Sym);
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +0000121
122 // Translate an offset in the input section to an offset in the output
123 // section.
124 uintX_t getOffset(uintX_t Offset);
125
Rafael Espindolac159c962015-10-19 21:00:02 +0000126 ArrayRef<uint8_t> getSectionData() const;
Rui Ueyama12504642015-10-27 21:51:13 +0000127
Rafael Espindola22ef9562016-04-13 01:40:19 +0000128 void relocate(uint8_t *Buf, uint8_t *BufEnd);
129 std::vector<Relocation> Relocations;
Rafael Espindolac159c962015-10-19 21:00:02 +0000130};
131
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000132template <class ELFT> InputSectionBase<ELFT> InputSectionBase<ELFT>::Discarded;
Rafael Espindolac159c962015-10-19 21:00:02 +0000133
Rui Ueyama3ea87272016-05-22 00:13:04 +0000134// SectionPiece represents a piece of splittable section contents.
135struct SectionPiece {
Rui Ueyama34dc99e2016-05-22 01:15:32 +0000136 SectionPiece(size_t Off, ArrayRef<uint8_t> Data)
137 : InputOff(Off), Data(Data), Live(!Config->GcSections) {}
138 size_t size() const { return Data.size(); }
139
Rui Ueyama3ea87272016-05-22 00:13:04 +0000140 size_t InputOff;
141 size_t OutputOff = -1;
Rui Ueyama34dc99e2016-05-22 01:15:32 +0000142 ArrayRef<uint8_t> Data; // slice of the input section
Rui Ueyama3ea87272016-05-22 00:13:04 +0000143 bool Live;
144};
145
Rui Ueyama5a32c762016-01-06 03:16:23 +0000146// Usually sections are copied to the output as atomic chunks of data,
147// but some special types of sections are split into small pieces of data
148// and each piece is copied to a different place in the output.
149// This class represents such special sections.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000150template <class ELFT> class SplitInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000151 typedef typename ELFT::Shdr Elf_Shdr;
152 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000153
154public:
155 SplitInputSection(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
156 typename InputSectionBase<ELFT>::Kind SectionKind);
Rui Ueyama5a32c762016-01-06 03:16:23 +0000157
Rui Ueyama3ea87272016-05-22 00:13:04 +0000158 // Splittable sections are handled as a sequence of data
159 // rather than a single large blob of data.
160 std::vector<SectionPiece> Pieces;
Rui Ueyama5a32c762016-01-06 03:16:23 +0000161
Rui Ueyama34dc99e2016-05-22 01:15:32 +0000162 // Returns the SectionPiece at a given input section offset.
Rui Ueyama90fa3722016-05-22 00:41:38 +0000163 SectionPiece *getSectionPiece(uintX_t Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000164};
165
Rafael Espindolac159c962015-10-19 21:00:02 +0000166// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000167template <class ELFT> class MergeInputSection : public SplitInputSection<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000168 typedef typename ELFT::uint uintX_t;
169 typedef typename ELFT::Sym Elf_Sym;
170 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000171
172public:
173 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
174 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000175 void splitIntoPieces();
176
177 // Mark the piece at a given offset live. Used by GC.
178 void markLiveAt(uintX_t Offset) { LiveOffsets.insert(Offset); }
179
180 // Translate an offset in the input section to an offset
181 // in the output section.
Rafael Espindola48225b42015-10-23 19:55:11 +0000182 uintX_t getOffset(uintX_t Offset);
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000183
184private:
185 llvm::DenseSet<uintX_t> LiveOffsets;
Rafael Espindolac159c962015-10-19 21:00:02 +0000186};
187
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000188// This corresponds to a .eh_frame section of an input file.
189template <class ELFT> class EHInputSection : public SplitInputSection<ELFT> {
190public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000191 typedef typename ELFT::Shdr Elf_Shdr;
192 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000193 EHInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
194 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama88abd9b2016-05-22 23:53:00 +0000195 void split();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000196
197 // Translate an offset in the input section to an offset in the output
198 // section.
199 uintX_t getOffset(uintX_t Offset);
200
201 // Relocation section that refer to this one.
202 const Elf_Shdr *RelocSection = nullptr;
203};
204
Rafael Espindolac159c962015-10-19 21:00:02 +0000205// This corresponds to a non SHF_MERGE section of an input file.
206template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
Rui Ueyama0b289522016-02-25 18:43:51 +0000207 friend ICF<ELFT>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000208 typedef InputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000209 typedef typename ELFT::Shdr Elf_Shdr;
210 typedef typename ELFT::Rela Elf_Rela;
211 typedef typename ELFT::Rel Elf_Rel;
212 typedef typename ELFT::Sym Elf_Sym;
213 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000214
215public:
216 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
217
218 // Write this section to a mmap'ed file, assuming Buf is pointing to
219 // beginning of the output section.
220 void writeTo(uint8_t *Buf);
221
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000222 // Relocation sections that refer to this one.
Rui Ueyamac00718f2016-02-23 03:34:37 +0000223 llvm::TinyPtrVector<const Elf_Shdr *> RelocSections;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000224
Rui Ueyamaedffd912015-10-14 21:00:23 +0000225 // The offset from beginning of the output sections this section was assigned
226 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000227 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000228
Rafael Espindolac159c962015-10-19 21:00:02 +0000229 static bool classof(const InputSectionBase<ELFT> *S);
George Rimar58941ee2016-02-25 08:23:37 +0000230
231 InputSectionBase<ELFT> *getRelocatedSection();
232
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000233 // Register thunk related to the symbol. When the section is written
234 // to a mmap'ed file, target is requested to write an actual thunk code.
235 // Now thunks is supported for MIPS target only.
236 void addThunk(SymbolBody &Body);
237
238 // The offset of synthetic thunk code from beginning of this section.
239 uint64_t getThunkOff() const;
240
241 // Size of chunk with thunks code.
242 uint64_t getThunksSize() const;
243
Rui Ueyama2b6fb802016-04-28 18:42:04 +0000244 template <class RelTy>
245 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
246
George Rimar58941ee2016-02-25 08:23:37 +0000247private:
Rui Ueyamafc467e72016-03-13 05:06:50 +0000248 template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000249 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
Rui Ueyama0b289522016-02-25 18:43:51 +0000250
251 // Called by ICF to merge two input sections.
252 void replace(InputSection<ELFT> *Other);
253
254 // Used by ICF.
255 uint64_t GroupId = 0;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000256
257 llvm::TinyPtrVector<const SymbolBody *> Thunks;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000258};
259
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000260// MIPS .reginfo section provides information on the registers used by the code
261// in the object file. Linker should collect this information and write a single
262// .reginfo section in the output file. The output section contains a union of
263// used registers masks taken from input .reginfo sections and final value
264// of the `_gp` symbol. For details: Chapter 4 / "Register Information" at
265// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
266template <class ELFT>
267class MipsReginfoInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000268 typedef typename ELFT::Shdr Elf_Shdr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000269
270public:
Rui Ueyama70eed362016-01-06 22:42:43 +0000271 MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000272 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama70eed362016-01-06 22:42:43 +0000273
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000274 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
275};
276
277template <class ELFT>
278class MipsOptionsInputSection : public InputSectionBase<ELFT> {
279 typedef typename ELFT::Shdr Elf_Shdr;
280
281public:
282 MipsOptionsInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
283 static bool classof(const InputSectionBase<ELFT> *S);
284
285 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000286};
287
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000288} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000289} // namespace lld
290
291#endif