blob: c34c94d336d533c0a763e7533fd67fc2ddf487e6 [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,
40 R_MIPS_GP0,
Rafael Espindola3f5d6342016-04-18 12:07:13 +000041 R_NEG_TLS,
Rafael Espindola22ef9562016-04-13 01:40:19 +000042 R_PAGE_PC,
43 R_PC,
44 R_PLT,
45 R_PLT_PC,
46 R_PPC_OPD,
47 R_PPC_PLT_OPD,
48 R_PPC_TOC,
49 R_RELAX_TLS_GD_TO_IE,
50 R_RELAX_TLS_GD_TO_IE_PC,
51 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
63struct Relocation {
64 RelExpr Expr;
65 uint32_t Type;
66 uint64_t Offset;
67 uint64_t Addend;
68 SymbolBody *Sym;
69};
70
Rafael Espindola71675852015-09-22 00:16:19 +000071// This corresponds to a section of an input file.
Rafael Espindolac159c962015-10-19 21:00:02 +000072template <class ELFT> class InputSectionBase {
73protected:
Rui Ueyama9328b2c2016-03-14 23:16:09 +000074 typedef typename ELFT::Rel Elf_Rel;
75 typedef typename ELFT::Rela Elf_Rela;
76 typedef typename ELFT::Shdr Elf_Shdr;
77 typedef typename ELFT::Sym Elf_Sym;
78 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +000079 const Elf_Shdr *Header;
80
81 // The file this section is from.
82 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000083
84public:
Simon Atanasyan1d7df402015-12-20 10:57:34 +000085 enum Kind { Regular, EHFrame, Merge, MipsReginfo };
Rafael Espindolac159c962015-10-19 21:00:02 +000086 Kind SectionKind;
87
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000088 InputSectionBase() : Repl(this) {}
89
Rafael Espindolac159c962015-10-19 21:00:02 +000090 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
91 Kind SectionKind);
92 OutputSectionBase<ELFT> *OutSec = nullptr;
Rui Ueyama6a9ef852016-02-25 18:49:09 +000093 uint32_t Align;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000094
Rui Ueyamac4aaed92015-10-22 18:49:53 +000095 // Used for garbage collection.
Rui Ueyama6a9ef852016-02-25 18:49:09 +000096 bool Live;
Rui Ueyamac4aaed92015-10-22 18:49:53 +000097
Rui Ueyama0b289522016-02-25 18:43:51 +000098 // This pointer points to the "real" instance of this instance.
99 // Usually Repl == this. However, if ICF merges two sections,
100 // Repl pointer of one section points to another section. So,
101 // if you need to get a pointer to this instance, do not use
102 // this but instead this->Repl.
103 InputSectionBase<ELFT> *Repl;
104
Rafael Espindola71675852015-09-22 00:16:19 +0000105 // Returns the size of this section (even if this is a common or BSS.)
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000106 size_t getSize() const;
Rafael Espindola83b0dc62015-08-13 22:21:37 +0000107
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000108 static InputSectionBase<ELFT> Discarded;
Rafael Espindola83b0dc62015-08-13 22:21:37 +0000109
Rafael Espindola5d83ccd2015-08-13 19:18:30 +0000110 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +0000111 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000112 ObjectFile<ELFT> *getFile() const { return File; }
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000113 uintX_t getOffset(const DefinedRegular<ELFT> &Sym);
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +0000114
115 // Translate an offset in the input section to an offset in the output
116 // section.
117 uintX_t getOffset(uintX_t Offset);
118
Rafael Espindolac159c962015-10-19 21:00:02 +0000119 ArrayRef<uint8_t> getSectionData() const;
Rui Ueyama12504642015-10-27 21:51:13 +0000120
121 // Returns a section that Rel is pointing to. Used by the garbage collector.
Rui Ueyamad7e4a282016-02-24 00:23:13 +0000122 InputSectionBase<ELFT> *getRelocTarget(const Elf_Rel &Rel) const;
123 InputSectionBase<ELFT> *getRelocTarget(const Elf_Rela &Rel) const;
Rafael Espindola9a6e4632015-11-11 10:18:52 +0000124
Rafael Espindola22ef9562016-04-13 01:40:19 +0000125 void relocate(uint8_t *Buf, uint8_t *BufEnd);
126 std::vector<Relocation> Relocations;
Rafael Espindolac159c962015-10-19 21:00:02 +0000127};
128
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000129template <class ELFT> InputSectionBase<ELFT> InputSectionBase<ELFT>::Discarded;
Rafael Espindolac159c962015-10-19 21:00:02 +0000130
Rui Ueyama5a32c762016-01-06 03:16:23 +0000131// Usually sections are copied to the output as atomic chunks of data,
132// but some special types of sections are split into small pieces of data
133// and each piece is copied to a different place in the output.
134// This class represents such special sections.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000135template <class ELFT> class SplitInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000136 typedef typename ELFT::Shdr Elf_Shdr;
137 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000138
139public:
140 SplitInputSection(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
141 typename InputSectionBase<ELFT>::Kind SectionKind);
Rui Ueyama5a32c762016-01-06 03:16:23 +0000142
143 // For each piece of data, we maintain the offsets in the input section and
144 // in the output section. The latter may be -1 if it is not assigned yet.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000145 std::vector<std::pair<uintX_t, uintX_t>> Offsets;
Rui Ueyama5a32c762016-01-06 03:16:23 +0000146
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000147 std::pair<std::pair<uintX_t, uintX_t> *, uintX_t>
148 getRangeAndSize(uintX_t Offset);
149};
150
Rafael Espindolac159c962015-10-19 21:00:02 +0000151// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000152template <class ELFT> class MergeInputSection : public SplitInputSection<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000153 typedef typename ELFT::uint uintX_t;
154 typedef typename ELFT::Sym Elf_Sym;
155 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000156
157public:
158 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
159 static bool classof(const InputSectionBase<ELFT> *S);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000160 // Translate an offset in the input section to an offset in the output
161 // section.
Rafael Espindola48225b42015-10-23 19:55:11 +0000162 uintX_t getOffset(uintX_t Offset);
Rafael Espindolac159c962015-10-19 21:00:02 +0000163};
164
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000165// This corresponds to a .eh_frame section of an input file.
166template <class ELFT> class EHInputSection : public SplitInputSection<ELFT> {
167public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000168 typedef typename ELFT::Shdr Elf_Shdr;
169 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000170 EHInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
171 static bool classof(const InputSectionBase<ELFT> *S);
172
173 // Translate an offset in the input section to an offset in the output
174 // section.
175 uintX_t getOffset(uintX_t Offset);
176
177 // Relocation section that refer to this one.
178 const Elf_Shdr *RelocSection = nullptr;
179};
180
Rafael Espindolac159c962015-10-19 21:00:02 +0000181// This corresponds to a non SHF_MERGE section of an input file.
182template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
Rui Ueyama0b289522016-02-25 18:43:51 +0000183 friend ICF<ELFT>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000184 typedef InputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000185 typedef typename ELFT::Shdr Elf_Shdr;
186 typedef typename ELFT::Rela Elf_Rela;
187 typedef typename ELFT::Rel Elf_Rel;
188 typedef typename ELFT::Sym Elf_Sym;
189 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000190
191public:
192 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
193
194 // Write this section to a mmap'ed file, assuming Buf is pointing to
195 // beginning of the output section.
196 void writeTo(uint8_t *Buf);
197
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000198 // Relocation sections that refer to this one.
Rui Ueyamac00718f2016-02-23 03:34:37 +0000199 llvm::TinyPtrVector<const Elf_Shdr *> RelocSections;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000200
Rui Ueyamaedffd912015-10-14 21:00:23 +0000201 // The offset from beginning of the output sections this section was assigned
202 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000203 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000204
Rafael Espindolac159c962015-10-19 21:00:02 +0000205 static bool classof(const InputSectionBase<ELFT> *S);
George Rimar58941ee2016-02-25 08:23:37 +0000206
207 InputSectionBase<ELFT> *getRelocatedSection();
208
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000209 // Register thunk related to the symbol. When the section is written
210 // to a mmap'ed file, target is requested to write an actual thunk code.
211 // Now thunks is supported for MIPS target only.
212 void addThunk(SymbolBody &Body);
213
214 // The offset of synthetic thunk code from beginning of this section.
215 uint64_t getThunkOff() const;
216
217 // Size of chunk with thunks code.
218 uint64_t getThunksSize() const;
219
George Rimar58941ee2016-02-25 08:23:37 +0000220private:
Rui Ueyamafc467e72016-03-13 05:06:50 +0000221 template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000222 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
Rui Ueyama0b289522016-02-25 18:43:51 +0000223
224 // Called by ICF to merge two input sections.
225 void replace(InputSection<ELFT> *Other);
226
227 // Used by ICF.
228 uint64_t GroupId = 0;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000229
230 llvm::TinyPtrVector<const SymbolBody *> Thunks;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000231};
232
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000233// MIPS .reginfo section provides information on the registers used by the code
234// in the object file. Linker should collect this information and write a single
235// .reginfo section in the output file. The output section contains a union of
236// used registers masks taken from input .reginfo sections and final value
237// of the `_gp` symbol. For details: Chapter 4 / "Register Information" at
238// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
239template <class ELFT>
240class MipsReginfoInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000241 typedef typename ELFT::Shdr Elf_Shdr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000242
243public:
Rui Ueyama70eed362016-01-06 22:42:43 +0000244 MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000245 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama70eed362016-01-06 22:42:43 +0000246
247 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000248};
249
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000250} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000251} // namespace lld
252
253#endif