blob: f03ce0cc7c4a5349b971b323ea112117be5945fd [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 Ueyamac00718f2016-02-23 03:34:37 +000015#include "llvm/ADT/TinyPtrVector.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000016#include "llvm/Object/ELF.h"
17
18namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000019namespace elf {
Michael J. Spencer84487f12015-07-24 21:03:07 +000020
Rafael Espindola38c67a22016-04-15 14:41:56 +000021class SymbolBody;
22
Rui Ueyama0b289522016-02-25 18:43:51 +000023template <class ELFT> class ICF;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000024template <class ELFT> class DefinedRegular;
Michael J. Spencer84487f12015-07-24 21:03:07 +000025template <class ELFT> class ObjectFile;
Rafael Espindola832b93f2015-08-24 20:06:32 +000026template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000027template <class ELFT> class OutputSectionBase;
Michael J. Spencer84487f12015-07-24 21:03:07 +000028
Rafael Espindola22ef9562016-04-13 01:40:19 +000029enum RelExpr {
30 R_ABS,
31 R_GOT,
Rafael Espindola3f5d6342016-04-18 12:07:13 +000032 R_GOTONLY_PC,
33 R_GOTREL,
Rafael Espindola58cd5db2016-04-19 22:46:03 +000034 R_GOT_OFF,
Rafael Espindola3f5d6342016-04-18 12:07:13 +000035 R_GOT_FROM_END,
Rafael Espindola22ef9562016-04-13 01:40:19 +000036 R_GOT_PAGE_PC,
37 R_GOT_PC,
38 R_MIPS_GOT,
39 R_MIPS_GOT_LOCAL,
Rafael Espindola3f5d6342016-04-18 12:07:13 +000040 R_NEG_TLS,
Rafael Espindola22ef9562016-04-13 01:40:19 +000041 R_PAGE_PC,
42 R_PC,
43 R_PLT,
44 R_PLT_PC,
45 R_PPC_OPD,
46 R_PPC_PLT_OPD,
47 R_PPC_TOC,
48 R_RELAX_TLS_GD_TO_IE,
49 R_RELAX_TLS_GD_TO_IE_PC,
50 R_RELAX_TLS_GD_TO_LE,
51 R_RELAX_TLS_IE_TO_LE,
52 R_RELAX_TLS_LD_TO_LE,
53 R_SIZE,
54 R_THUNK,
Rafael Espindola3f5d6342016-04-18 12:07:13 +000055 R_TLS,
Rafael Espindola22ef9562016-04-13 01:40:19 +000056 R_TLSGD,
57 R_TLSGD_PC,
58 R_TLSLD,
59 R_TLSLD_PC
60};
61
Rafael Espindolac6b17bd2016-04-20 17:30:22 +000062inline bool refersToGotEntry(RelExpr Expr) {
63 return Expr == R_GOT || Expr == R_GOT_OFF || Expr == R_MIPS_GOT ||
64 Expr == R_MIPS_GOT_LOCAL || Expr == R_GOT_PAGE_PC ||
Rafael Espindolaa85efd92016-04-30 01:15:17 +000065 Expr == R_GOT_PC || Expr == R_GOT_FROM_END || Expr == R_TLSGD ||
66 Expr == R_TLSGD_PC;
Rafael Espindolac6b17bd2016-04-20 17:30:22 +000067}
68
Rafael Espindola22ef9562016-04-13 01:40:19 +000069struct Relocation {
70 RelExpr Expr;
71 uint32_t Type;
72 uint64_t Offset;
73 uint64_t Addend;
74 SymbolBody *Sym;
75};
76
Rafael Espindola71675852015-09-22 00:16:19 +000077// This corresponds to a section of an input file.
Rafael Espindolac159c962015-10-19 21:00:02 +000078template <class ELFT> class InputSectionBase {
79protected:
Rafael Espindola197d6a82016-04-22 16:39:59 +000080 typedef typename ELFT::Rel Elf_Rel;
81 typedef typename ELFT::Rela Elf_Rela;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000082 typedef typename ELFT::Shdr Elf_Shdr;
83 typedef typename ELFT::Sym Elf_Sym;
84 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +000085 const Elf_Shdr *Header;
86
87 // The file this section is from.
88 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000089
90public:
Simon Atanasyanadd74f32016-05-04 10:07:38 +000091 enum Kind { Regular, EHFrame, Merge, MipsReginfo, MipsOptions };
Rafael Espindolac159c962015-10-19 21:00:02 +000092 Kind SectionKind;
93
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000094 InputSectionBase() : Repl(this) {}
95
Rafael Espindolac159c962015-10-19 21:00:02 +000096 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
97 Kind SectionKind);
98 OutputSectionBase<ELFT> *OutSec = nullptr;
Rui Ueyama6a9ef852016-02-25 18:49:09 +000099 uint32_t Align;
Rafael Espindola83b0dc62015-08-13 22:21:37 +0000100
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000101 // Used for garbage collection.
Rui Ueyama6a9ef852016-02-25 18:49:09 +0000102 bool Live;
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000103
Rui Ueyama0b289522016-02-25 18:43:51 +0000104 // This pointer points to the "real" instance of this instance.
105 // Usually Repl == this. However, if ICF merges two sections,
106 // Repl pointer of one section points to another section. So,
107 // if you need to get a pointer to this instance, do not use
108 // this but instead this->Repl.
109 InputSectionBase<ELFT> *Repl;
110
Rafael Espindola71675852015-09-22 00:16:19 +0000111 // Returns the size of this section (even if this is a common or BSS.)
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000112 size_t getSize() const;
Rafael Espindola83b0dc62015-08-13 22:21:37 +0000113
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000114 static InputSectionBase<ELFT> Discarded;
Rafael Espindola83b0dc62015-08-13 22:21:37 +0000115
Rafael Espindola5d83ccd2015-08-13 19:18:30 +0000116 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +0000117 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000118 ObjectFile<ELFT> *getFile() const { return File; }
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000119 uintX_t getOffset(const DefinedRegular<ELFT> &Sym);
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +0000120
121 // Translate an offset in the input section to an offset in the output
122 // section.
123 uintX_t getOffset(uintX_t Offset);
124
Rafael Espindolac159c962015-10-19 21:00:02 +0000125 ArrayRef<uint8_t> getSectionData() const;
Rui Ueyama12504642015-10-27 21:51:13 +0000126
Rafael Espindola22ef9562016-04-13 01:40:19 +0000127 void relocate(uint8_t *Buf, uint8_t *BufEnd);
128 std::vector<Relocation> Relocations;
Rafael Espindolac159c962015-10-19 21:00:02 +0000129};
130
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000131template <class ELFT> InputSectionBase<ELFT> InputSectionBase<ELFT>::Discarded;
Rafael Espindolac159c962015-10-19 21:00:02 +0000132
Rui Ueyama5a32c762016-01-06 03:16:23 +0000133// Usually sections are copied to the output as atomic chunks of data,
134// but some special types of sections are split into small pieces of data
135// and each piece is copied to a different place in the output.
136// This class represents such special sections.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000137template <class ELFT> class SplitInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000138 typedef typename ELFT::Shdr Elf_Shdr;
139 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000140
141public:
142 SplitInputSection(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
143 typename InputSectionBase<ELFT>::Kind SectionKind);
Rui Ueyama5a32c762016-01-06 03:16:23 +0000144
145 // For each piece of data, we maintain the offsets in the input section and
146 // in the output section. The latter may be -1 if it is not assigned yet.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000147 std::vector<std::pair<uintX_t, uintX_t>> Offsets;
Rui Ueyama5a32c762016-01-06 03:16:23 +0000148
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000149 std::pair<std::pair<uintX_t, uintX_t> *, uintX_t>
150 getRangeAndSize(uintX_t Offset);
151};
152
Rafael Espindolac159c962015-10-19 21:00:02 +0000153// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000154template <class ELFT> class MergeInputSection : public SplitInputSection<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000155 typedef typename ELFT::uint uintX_t;
156 typedef typename ELFT::Sym Elf_Sym;
157 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000158
159public:
160 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
161 static bool classof(const InputSectionBase<ELFT> *S);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000162 // Translate an offset in the input section to an offset in the output
163 // section.
Rafael Espindola48225b42015-10-23 19:55:11 +0000164 uintX_t getOffset(uintX_t Offset);
Rafael Espindolac159c962015-10-19 21:00:02 +0000165};
166
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000167// This corresponds to a .eh_frame section of an input file.
168template <class ELFT> class EHInputSection : public SplitInputSection<ELFT> {
169public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000170 typedef typename ELFT::Shdr Elf_Shdr;
171 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000172 EHInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
173 static bool classof(const InputSectionBase<ELFT> *S);
174
175 // Translate an offset in the input section to an offset in the output
176 // section.
177 uintX_t getOffset(uintX_t Offset);
178
179 // Relocation section that refer to this one.
180 const Elf_Shdr *RelocSection = nullptr;
181};
182
Rafael Espindolac159c962015-10-19 21:00:02 +0000183// This corresponds to a non SHF_MERGE section of an input file.
184template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
Rui Ueyama0b289522016-02-25 18:43:51 +0000185 friend ICF<ELFT>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000186 typedef InputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000187 typedef typename ELFT::Shdr Elf_Shdr;
188 typedef typename ELFT::Rela Elf_Rela;
189 typedef typename ELFT::Rel Elf_Rel;
190 typedef typename ELFT::Sym Elf_Sym;
191 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000192
193public:
194 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
195
196 // Write this section to a mmap'ed file, assuming Buf is pointing to
197 // beginning of the output section.
198 void writeTo(uint8_t *Buf);
199
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000200 // Relocation sections that refer to this one.
Rui Ueyamac00718f2016-02-23 03:34:37 +0000201 llvm::TinyPtrVector<const Elf_Shdr *> RelocSections;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000202
Rui Ueyamaedffd912015-10-14 21:00:23 +0000203 // The offset from beginning of the output sections this section was assigned
204 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000205 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000206
Rafael Espindolac159c962015-10-19 21:00:02 +0000207 static bool classof(const InputSectionBase<ELFT> *S);
George Rimar58941ee2016-02-25 08:23:37 +0000208
209 InputSectionBase<ELFT> *getRelocatedSection();
210
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000211 // Register thunk related to the symbol. When the section is written
212 // to a mmap'ed file, target is requested to write an actual thunk code.
213 // Now thunks is supported for MIPS target only.
214 void addThunk(SymbolBody &Body);
215
216 // The offset of synthetic thunk code from beginning of this section.
217 uint64_t getThunkOff() const;
218
219 // Size of chunk with thunks code.
220 uint64_t getThunksSize() const;
221
Rui Ueyama2b6fb802016-04-28 18:42:04 +0000222 template <class RelTy>
223 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
224
George Rimar58941ee2016-02-25 08:23:37 +0000225private:
Rui Ueyamafc467e72016-03-13 05:06:50 +0000226 template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000227 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
Rui Ueyama0b289522016-02-25 18:43:51 +0000228
229 // Called by ICF to merge two input sections.
230 void replace(InputSection<ELFT> *Other);
231
232 // Used by ICF.
233 uint64_t GroupId = 0;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000234
235 llvm::TinyPtrVector<const SymbolBody *> Thunks;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000236};
237
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000238// MIPS .reginfo section provides information on the registers used by the code
239// in the object file. Linker should collect this information and write a single
240// .reginfo section in the output file. The output section contains a union of
241// used registers masks taken from input .reginfo sections and final value
242// of the `_gp` symbol. For details: Chapter 4 / "Register Information" at
243// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
244template <class ELFT>
245class MipsReginfoInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000246 typedef typename ELFT::Shdr Elf_Shdr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000247
248public:
Rui Ueyama70eed362016-01-06 22:42:43 +0000249 MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000250 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama70eed362016-01-06 22:42:43 +0000251
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000252 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
253};
254
255template <class ELFT>
256class MipsOptionsInputSection : public InputSectionBase<ELFT> {
257 typedef typename ELFT::Shdr Elf_Shdr;
258
259public:
260 MipsOptionsInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
261 static bool classof(const InputSectionBase<ELFT> *S);
262
263 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000264};
265
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000266} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000267} // namespace lld
268
269#endif