blob: 51e191da0ee45b7a65a0757f7f1afe587d0a8c8d [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"
Michael J. Spencer84487f12015-07-24 21:03:07 +000015#include "lld/Core/LLVM.h"
Rui Ueyamab91bf1a2016-05-23 16:55:43 +000016#include "llvm/ADT/DenseSet.h"
Rui Ueyamac00718f2016-02-23 03:34:37 +000017#include "llvm/ADT/TinyPtrVector.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000018#include "llvm/Object/ELF.h"
19
20namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000021namespace elf {
Michael J. Spencer84487f12015-07-24 21:03:07 +000022
Rafael Espindola38c67a22016-04-15 14:41:56 +000023class SymbolBody;
24
Rui Ueyama0b289522016-02-25 18:43:51 +000025template <class ELFT> class ICF;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000026template <class ELFT> class DefinedRegular;
Michael J. Spencer84487f12015-07-24 21:03:07 +000027template <class ELFT> class ObjectFile;
Rafael Espindola832b93f2015-08-24 20:06:32 +000028template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000029template <class ELFT> class OutputSectionBase;
Michael J. Spencer84487f12015-07-24 21:03:07 +000030
Rafael Espindola71675852015-09-22 00:16:19 +000031// This corresponds to a section of an input file.
Rafael Espindolac159c962015-10-19 21:00:02 +000032template <class ELFT> class InputSectionBase {
33protected:
Rafael Espindola197d6a82016-04-22 16:39:59 +000034 typedef typename ELFT::Rel Elf_Rel;
35 typedef typename ELFT::Rela Elf_Rela;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000036 typedef typename ELFT::Shdr Elf_Shdr;
37 typedef typename ELFT::Sym Elf_Sym;
38 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +000039 const Elf_Shdr *Header;
40
41 // The file this section is from.
42 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000043
44public:
Simon Atanasyanadd74f32016-05-04 10:07:38 +000045 enum Kind { Regular, EHFrame, Merge, MipsReginfo, MipsOptions };
Rafael Espindolac159c962015-10-19 21:00:02 +000046 Kind SectionKind;
47
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000048 InputSectionBase() : Repl(this) {}
49
Rafael Espindolac159c962015-10-19 21:00:02 +000050 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
51 Kind SectionKind);
52 OutputSectionBase<ELFT> *OutSec = nullptr;
Rui Ueyama424b4082016-06-17 01:18:46 +000053 uint32_t Alignment;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000054
Rui Ueyamac4aaed92015-10-22 18:49:53 +000055 // Used for garbage collection.
Rui Ueyama6a9ef852016-02-25 18:49:09 +000056 bool Live;
Rui Ueyamac4aaed92015-10-22 18:49:53 +000057
Rui Ueyama0b289522016-02-25 18:43:51 +000058 // This pointer points to the "real" instance of this instance.
59 // Usually Repl == this. However, if ICF merges two sections,
60 // Repl pointer of one section points to another section. So,
61 // if you need to get a pointer to this instance, do not use
62 // this but instead this->Repl.
63 InputSectionBase<ELFT> *Repl;
64
Rafael Espindola71675852015-09-22 00:16:19 +000065 // Returns the size of this section (even if this is a common or BSS.)
Simon Atanasyan13f6da12016-03-31 21:26:23 +000066 size_t getSize() const;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000067
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000068 static InputSectionBase<ELFT> Discarded;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000069
Rafael Espindola5d83ccd2015-08-13 19:18:30 +000070 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +000071 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindolae1901cc2015-09-24 15:11:50 +000072 ObjectFile<ELFT> *getFile() const { return File; }
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000073 uintX_t getOffset(const DefinedRegular<ELFT> &Sym);
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000074
75 // Translate an offset in the input section to an offset in the output
76 // section.
77 uintX_t getOffset(uintX_t Offset);
78
Rafael Espindolac159c962015-10-19 21:00:02 +000079 ArrayRef<uint8_t> getSectionData() const;
Rui Ueyama12504642015-10-27 21:51:13 +000080
Rafael Espindola22ef9562016-04-13 01:40:19 +000081 void relocate(uint8_t *Buf, uint8_t *BufEnd);
82 std::vector<Relocation> Relocations;
Rafael Espindolac159c962015-10-19 21:00:02 +000083};
84
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000085template <class ELFT> InputSectionBase<ELFT> InputSectionBase<ELFT>::Discarded;
Rafael Espindolac159c962015-10-19 21:00:02 +000086
Rui Ueyama3ea87272016-05-22 00:13:04 +000087// SectionPiece represents a piece of splittable section contents.
88struct SectionPiece {
Rui Ueyama34dc99e2016-05-22 01:15:32 +000089 SectionPiece(size_t Off, ArrayRef<uint8_t> Data)
Davide Italianoe6c8fa42016-05-28 23:27:38 +000090 : InputOff(Off), Data((const uint8_t *)Data.data()), Size(Data.size()),
Rui Ueyamad8849272016-05-25 16:37:01 +000091 Live(!Config->GcSections) {}
92
93 ArrayRef<uint8_t> data() { return {Data, Size}; }
94 size_t size() const { return Size; }
Rui Ueyama34dc99e2016-05-22 01:15:32 +000095
Rui Ueyama3ea87272016-05-22 00:13:04 +000096 size_t InputOff;
97 size_t OutputOff = -1;
Rui Ueyamad8849272016-05-25 16:37:01 +000098
99private:
100 // We use bitfields because SplitInputSection is accessed by
101 // std::upper_bound very often.
102 // We want to save bits to make it cache friendly.
Davide Italianoe6c8fa42016-05-28 23:27:38 +0000103 const uint8_t *Data;
Rui Ueyamad8849272016-05-25 16:37:01 +0000104 uint32_t Size : 31;
105
106public:
107 uint32_t Live : 1;
Rui Ueyama3ea87272016-05-22 00:13:04 +0000108};
109
Rui Ueyama5a32c762016-01-06 03:16:23 +0000110// Usually sections are copied to the output as atomic chunks of data,
111// but some special types of sections are split into small pieces of data
112// and each piece is copied to a different place in the output.
113// This class represents such special sections.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000114template <class ELFT> class SplitInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000115 typedef typename ELFT::Shdr Elf_Shdr;
116 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000117
118public:
119 SplitInputSection(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
120 typename InputSectionBase<ELFT>::Kind SectionKind);
Rui Ueyama5a32c762016-01-06 03:16:23 +0000121
Rui Ueyama3ea87272016-05-22 00:13:04 +0000122 // Splittable sections are handled as a sequence of data
123 // rather than a single large blob of data.
124 std::vector<SectionPiece> Pieces;
Rui Ueyama5a32c762016-01-06 03:16:23 +0000125
Rui Ueyama34dc99e2016-05-22 01:15:32 +0000126 // Returns the SectionPiece at a given input section offset.
Rui Ueyama90fa3722016-05-22 00:41:38 +0000127 SectionPiece *getSectionPiece(uintX_t Offset);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000128};
129
Rafael Espindolac159c962015-10-19 21:00:02 +0000130// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000131template <class ELFT> class MergeInputSection : public SplitInputSection<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000132 typedef typename ELFT::uint uintX_t;
133 typedef typename ELFT::Sym Elf_Sym;
134 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000135
136public:
137 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
138 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000139 void splitIntoPieces();
140
141 // Mark the piece at a given offset live. Used by GC.
142 void markLiveAt(uintX_t Offset) { LiveOffsets.insert(Offset); }
143
144 // Translate an offset in the input section to an offset
145 // in the output section.
Rafael Espindola48225b42015-10-23 19:55:11 +0000146 uintX_t getOffset(uintX_t Offset);
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000147
Rui Ueyama406b4692016-05-27 14:39:13 +0000148 void finalizePieces();
149
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000150private:
Rui Ueyama406b4692016-05-27 14:39:13 +0000151 llvm::DenseMap<uintX_t, uintX_t> OffsetMap;
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000152 llvm::DenseSet<uintX_t> LiveOffsets;
Rafael Espindolac159c962015-10-19 21:00:02 +0000153};
154
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000155// This corresponds to a .eh_frame section of an input file.
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000156template <class ELFT> class EhInputSection : public SplitInputSection<ELFT> {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000157public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000158 typedef typename ELFT::Shdr Elf_Shdr;
159 typedef typename ELFT::uint uintX_t;
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000160 EhInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000161 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama88abd9b2016-05-22 23:53:00 +0000162 void split();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000163
164 // Translate an offset in the input section to an offset in the output
165 // section.
166 uintX_t getOffset(uintX_t Offset);
167
168 // Relocation section that refer to this one.
169 const Elf_Shdr *RelocSection = nullptr;
170};
171
Rafael Espindolac159c962015-10-19 21:00:02 +0000172// This corresponds to a non SHF_MERGE section of an input file.
173template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
Rui Ueyama0b289522016-02-25 18:43:51 +0000174 friend ICF<ELFT>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000175 typedef InputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000176 typedef typename ELFT::Shdr Elf_Shdr;
177 typedef typename ELFT::Rela Elf_Rela;
178 typedef typename ELFT::Rel Elf_Rel;
179 typedef typename ELFT::Sym Elf_Sym;
180 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000181
182public:
183 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
184
185 // Write this section to a mmap'ed file, assuming Buf is pointing to
186 // beginning of the output section.
187 void writeTo(uint8_t *Buf);
188
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000189 // Relocation sections that refer to this one.
Rui Ueyamac00718f2016-02-23 03:34:37 +0000190 llvm::TinyPtrVector<const Elf_Shdr *> RelocSections;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000191
Rui Ueyamaedffd912015-10-14 21:00:23 +0000192 // The offset from beginning of the output sections this section was assigned
193 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000194 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000195
Rafael Espindolac159c962015-10-19 21:00:02 +0000196 static bool classof(const InputSectionBase<ELFT> *S);
George Rimar58941ee2016-02-25 08:23:37 +0000197
198 InputSectionBase<ELFT> *getRelocatedSection();
199
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000200 // Register thunk related to the symbol. When the section is written
201 // to a mmap'ed file, target is requested to write an actual thunk code.
202 // Now thunks is supported for MIPS target only.
203 void addThunk(SymbolBody &Body);
204
205 // The offset of synthetic thunk code from beginning of this section.
206 uint64_t getThunkOff() const;
207
208 // Size of chunk with thunks code.
209 uint64_t getThunksSize() const;
210
Rui Ueyama2b6fb802016-04-28 18:42:04 +0000211 template <class RelTy>
212 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
213
George Rimar58941ee2016-02-25 08:23:37 +0000214private:
Rui Ueyamafc467e72016-03-13 05:06:50 +0000215 template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000216 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
Rui Ueyama0b289522016-02-25 18:43:51 +0000217
218 // Called by ICF to merge two input sections.
219 void replace(InputSection<ELFT> *Other);
220
221 // Used by ICF.
222 uint64_t GroupId = 0;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000223
224 llvm::TinyPtrVector<const SymbolBody *> Thunks;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000225};
226
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000227// MIPS .reginfo section provides information on the registers used by the code
228// in the object file. Linker should collect this information and write a single
229// .reginfo section in the output file. The output section contains a union of
230// used registers masks taken from input .reginfo sections and final value
231// of the `_gp` symbol. For details: Chapter 4 / "Register Information" at
232// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
233template <class ELFT>
234class MipsReginfoInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000235 typedef typename ELFT::Shdr Elf_Shdr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000236
237public:
Rui Ueyama70eed362016-01-06 22:42:43 +0000238 MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000239 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama70eed362016-01-06 22:42:43 +0000240
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000241 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
242};
243
244template <class ELFT>
245class MipsOptionsInputSection : public InputSectionBase<ELFT> {
246 typedef typename ELFT::Shdr Elf_Shdr;
247
248public:
249 MipsOptionsInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
250 static bool classof(const InputSectionBase<ELFT> *S);
251
252 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000253};
254
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000255} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000256} // namespace lld
257
258#endif