blob: eabe5fb3c51fdbbc81ab480ff73d7cac28421e1b [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;
Michael J. Spencer84487f12015-07-24 21:03:07 +000028template <class ELFT> class ObjectFile;
Rafael Espindola832b93f2015-08-24 20:06:32 +000029template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000030template <class ELFT> class OutputSectionBase;
Michael J. Spencer84487f12015-07-24 21:03:07 +000031
Rafael Espindola71675852015-09-22 00:16:19 +000032// This corresponds to a section of an input file.
Rafael Espindolac159c962015-10-19 21:00:02 +000033template <class ELFT> class InputSectionBase {
34protected:
Rui Ueyama1d12ac12016-07-07 03:55:55 +000035 typedef typename ELFT::Chdr Elf_Chdr;
Rafael Espindola197d6a82016-04-22 16:39:59 +000036 typedef typename ELFT::Rel Elf_Rel;
37 typedef typename ELFT::Rela Elf_Rela;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000038 typedef typename ELFT::Shdr Elf_Shdr;
39 typedef typename ELFT::Sym Elf_Sym;
40 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +000041 const Elf_Shdr *Header;
42
43 // The file this section is from.
44 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000045
George Rimar602fbee2016-06-24 11:18:44 +000046 // If a section is compressed, this vector has uncompressed section data.
47 SmallVector<char, 0> Uncompressed;
48
Michael J. Spencer84487f12015-07-24 21:03:07 +000049public:
Simon Atanasyanadd74f32016-05-04 10:07:38 +000050 enum Kind { Regular, EHFrame, Merge, MipsReginfo, MipsOptions };
Rafael Espindolac159c962015-10-19 21:00:02 +000051 Kind SectionKind;
52
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000053 InputSectionBase() : Repl(this) {}
54
Rafael Espindolac159c962015-10-19 21:00:02 +000055 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
56 Kind SectionKind);
57 OutputSectionBase<ELFT> *OutSec = nullptr;
Rui Ueyama424b4082016-06-17 01:18:46 +000058 uint32_t Alignment;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000059
Rui Ueyamac4aaed92015-10-22 18:49:53 +000060 // Used for garbage collection.
Rui Ueyama6a9ef852016-02-25 18:49:09 +000061 bool Live;
Rui Ueyamac4aaed92015-10-22 18:49:53 +000062
Rui Ueyama0b289522016-02-25 18:43:51 +000063 // This pointer points to the "real" instance of this instance.
64 // Usually Repl == this. However, if ICF merges two sections,
65 // Repl pointer of one section points to another section. So,
66 // if you need to get a pointer to this instance, do not use
67 // this but instead this->Repl.
68 InputSectionBase<ELFT> *Repl;
69
Rafael Espindola71675852015-09-22 00:16:19 +000070 // Returns the size of this section (even if this is a common or BSS.)
Simon Atanasyan13f6da12016-03-31 21:26:23 +000071 size_t getSize() const;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000072
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000073 static InputSectionBase<ELFT> Discarded;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000074
Rafael Espindola5d83ccd2015-08-13 19:18:30 +000075 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +000076 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindolae1901cc2015-09-24 15:11:50 +000077 ObjectFile<ELFT> *getFile() const { return File; }
Rui Ueyama809d8e22016-06-23 04:33:42 +000078 uintX_t getOffset(const DefinedRegular<ELFT> &Sym) const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000079
80 // Translate an offset in the input section to an offset in the output
81 // section.
Rui Ueyama809d8e22016-06-23 04:33:42 +000082 uintX_t getOffset(uintX_t Offset) const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000083
Rafael Espindolac159c962015-10-19 21:00:02 +000084 ArrayRef<uint8_t> getSectionData() const;
Rui Ueyama12504642015-10-27 21:51:13 +000085
George Rimar602fbee2016-06-24 11:18:44 +000086 void uncompress();
87
Rafael Espindola22ef9562016-04-13 01:40:19 +000088 void relocate(uint8_t *Buf, uint8_t *BufEnd);
Rui Ueyama809d8e22016-06-23 04:33:42 +000089 std::vector<Relocation<ELFT>> Relocations;
George Rimar602fbee2016-06-24 11:18:44 +000090
91 bool Compressed;
Rafael Espindolac159c962015-10-19 21:00:02 +000092};
93
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000094template <class ELFT> InputSectionBase<ELFT> InputSectionBase<ELFT>::Discarded;
Rafael Espindolac159c962015-10-19 21:00:02 +000095
Rui Ueyama3ea87272016-05-22 00:13:04 +000096// SectionPiece represents a piece of splittable section contents.
97struct SectionPiece {
Rui Ueyama34dc99e2016-05-22 01:15:32 +000098 SectionPiece(size_t Off, ArrayRef<uint8_t> Data)
Davide Italianoe6c8fa42016-05-28 23:27:38 +000099 : InputOff(Off), Data((const uint8_t *)Data.data()), Size(Data.size()),
Rui Ueyamad8849272016-05-25 16:37:01 +0000100 Live(!Config->GcSections) {}
101
102 ArrayRef<uint8_t> data() { return {Data, Size}; }
103 size_t size() const { return Size; }
Rui Ueyama34dc99e2016-05-22 01:15:32 +0000104
Rui Ueyama3ea87272016-05-22 00:13:04 +0000105 size_t InputOff;
106 size_t OutputOff = -1;
Rui Ueyamad8849272016-05-25 16:37:01 +0000107
108private:
109 // We use bitfields because SplitInputSection is accessed by
110 // std::upper_bound very often.
111 // We want to save bits to make it cache friendly.
Davide Italianoe6c8fa42016-05-28 23:27:38 +0000112 const uint8_t *Data;
Rui Ueyamad8849272016-05-25 16:37:01 +0000113 uint32_t Size : 31;
114
115public:
116 uint32_t Live : 1;
Rui Ueyama3ea87272016-05-22 00:13:04 +0000117};
118
Rui Ueyama5a32c762016-01-06 03:16:23 +0000119// Usually sections are copied to the output as atomic chunks of data,
120// but some special types of sections are split into small pieces of data
121// and each piece is copied to a different place in the output.
122// This class represents such special sections.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000123template <class ELFT> class SplitInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000124 typedef typename ELFT::Shdr Elf_Shdr;
125 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000126
127public:
128 SplitInputSection(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
129 typename InputSectionBase<ELFT>::Kind SectionKind);
Rui Ueyama5a32c762016-01-06 03:16:23 +0000130
Rui Ueyama3ea87272016-05-22 00:13:04 +0000131 // Splittable sections are handled as a sequence of data
132 // rather than a single large blob of data.
133 std::vector<SectionPiece> Pieces;
Rui Ueyama5a32c762016-01-06 03:16:23 +0000134
Rui Ueyama34dc99e2016-05-22 01:15:32 +0000135 // Returns the SectionPiece at a given input section offset.
Rui Ueyama90fa3722016-05-22 00:41:38 +0000136 SectionPiece *getSectionPiece(uintX_t Offset);
Rui Ueyama809d8e22016-06-23 04:33:42 +0000137 const SectionPiece *getSectionPiece(uintX_t Offset) const;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000138};
139
Rafael Espindolac159c962015-10-19 21:00:02 +0000140// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000141template <class ELFT> class MergeInputSection : public SplitInputSection<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000142 typedef typename ELFT::uint uintX_t;
143 typedef typename ELFT::Sym Elf_Sym;
144 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000145
146public:
147 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
148 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000149 void splitIntoPieces();
150
151 // Mark the piece at a given offset live. Used by GC.
152 void markLiveAt(uintX_t Offset) { LiveOffsets.insert(Offset); }
153
154 // Translate an offset in the input section to an offset
155 // in the output section.
Rui Ueyama809d8e22016-06-23 04:33:42 +0000156 uintX_t getOffset(uintX_t Offset) const;
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000157
Rui Ueyama406b4692016-05-27 14:39:13 +0000158 void finalizePieces();
159
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000160private:
Rui Ueyama406b4692016-05-27 14:39:13 +0000161 llvm::DenseMap<uintX_t, uintX_t> OffsetMap;
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000162 llvm::DenseSet<uintX_t> LiveOffsets;
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.
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000166template <class ELFT> class EhInputSection : public SplitInputSection<ELFT> {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000167public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000168 typedef typename ELFT::Shdr Elf_Shdr;
169 typedef typename ELFT::uint uintX_t;
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000170 EhInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000171 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama88abd9b2016-05-22 23:53:00 +0000172 void split();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000173
174 // Translate an offset in the input section to an offset in the output
175 // section.
Rui Ueyama809d8e22016-06-23 04:33:42 +0000176 uintX_t getOffset(uintX_t Offset) const;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000177
178 // Relocation section that refer to this one.
179 const Elf_Shdr *RelocSection = nullptr;
180};
181
Rafael Espindolac159c962015-10-19 21:00:02 +0000182// This corresponds to a non SHF_MERGE section of an input file.
183template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
Rui Ueyama0b289522016-02-25 18:43:51 +0000184 friend ICF<ELFT>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000185 typedef InputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000186 typedef typename ELFT::Shdr Elf_Shdr;
187 typedef typename ELFT::Rela Elf_Rela;
188 typedef typename ELFT::Rel Elf_Rel;
189 typedef typename ELFT::Sym Elf_Sym;
190 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000191
192public:
193 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
194
195 // Write this section to a mmap'ed file, assuming Buf is pointing to
196 // beginning of the output section.
197 void writeTo(uint8_t *Buf);
198
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000199 // Relocation sections that refer to this one.
Rui Ueyamac00718f2016-02-23 03:34:37 +0000200 llvm::TinyPtrVector<const Elf_Shdr *> RelocSections;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000201
Rui Ueyamaedffd912015-10-14 21:00:23 +0000202 // The offset from beginning of the output sections this section was assigned
203 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000204 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000205
Rafael Espindolac159c962015-10-19 21:00:02 +0000206 static bool classof(const InputSectionBase<ELFT> *S);
George Rimar58941ee2016-02-25 08:23:37 +0000207
208 InputSectionBase<ELFT> *getRelocatedSection();
209
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000210 // Register thunk related to the symbol. When the section is written
211 // to a mmap'ed file, target is requested to write an actual thunk code.
Peter Smithfb05cd92016-07-08 16:10:27 +0000212 // Now thunks is supported for MIPS and ARM target only.
213 void addThunk(const Thunk<ELFT> *T);
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000214
215 // The offset of synthetic thunk code from beginning of this section.
216 uint64_t getThunkOff() const;
217
218 // Size of chunk with thunks code.
219 uint64_t getThunksSize() const;
220
Rui Ueyama2b6fb802016-04-28 18:42:04 +0000221 template <class RelTy>
222 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
223
George Rimar58941ee2016-02-25 08:23:37 +0000224private:
Rui Ueyamafc467e72016-03-13 05:06:50 +0000225 template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000226 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
Rui Ueyama0b289522016-02-25 18:43:51 +0000227
228 // Called by ICF to merge two input sections.
229 void replace(InputSection<ELFT> *Other);
230
231 // Used by ICF.
232 uint64_t GroupId = 0;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000233
Peter Smithfb05cd92016-07-08 16:10:27 +0000234 llvm::TinyPtrVector<const Thunk<ELFT> *> Thunks;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000235};
236
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000237// MIPS .reginfo section provides information on the registers used by the code
238// in the object file. Linker should collect this information and write a single
239// .reginfo section in the output file. The output section contains a union of
240// used registers masks taken from input .reginfo sections and final value
241// of the `_gp` symbol. For details: Chapter 4 / "Register Information" at
242// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
243template <class ELFT>
244class MipsReginfoInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000245 typedef typename ELFT::Shdr Elf_Shdr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000246
247public:
Rui Ueyama70eed362016-01-06 22:42:43 +0000248 MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000249 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama70eed362016-01-06 22:42:43 +0000250
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000251 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
252};
253
254template <class ELFT>
255class MipsOptionsInputSection : public InputSectionBase<ELFT> {
256 typedef typename ELFT::Shdr Elf_Shdr;
257
258public:
259 MipsOptionsInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
260 static bool classof(const InputSectionBase<ELFT> *S);
261
262 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000263};
264
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000265} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000266} // namespace lld
267
268#endif