blob: ddb9ce0befac549c457911f89f5b6f567cd8470f [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;
Eugene Leviant3e6b0272016-07-28 19:24:13 +000028template <class ELFT> class DefinedCommon;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029template <class ELFT> class ObjectFile;
Rafael Espindola832b93f2015-08-24 20:06:32 +000030template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000031template <class ELFT> class OutputSectionBase;
Michael J. Spencer84487f12015-07-24 21:03:07 +000032
Rafael Espindola71675852015-09-22 00:16:19 +000033// This corresponds to a section of an input file.
Rafael Espindolac159c962015-10-19 21:00:02 +000034template <class ELFT> class InputSectionBase {
35protected:
Rui Ueyama1d12ac12016-07-07 03:55:55 +000036 typedef typename ELFT::Chdr Elf_Chdr;
Rafael Espindola197d6a82016-04-22 16:39:59 +000037 typedef typename ELFT::Rel Elf_Rel;
38 typedef typename ELFT::Rela Elf_Rela;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000039 typedef typename ELFT::Shdr Elf_Shdr;
40 typedef typename ELFT::Sym Elf_Sym;
41 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +000042 const Elf_Shdr *Header;
43
44 // The file this section is from.
45 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000046
George Rimar602fbee2016-06-24 11:18:44 +000047 // If a section is compressed, this vector has uncompressed section data.
48 SmallVector<char, 0> Uncompressed;
49
Michael J. Spencer84487f12015-07-24 21:03:07 +000050public:
Simon Atanasyanadd74f32016-05-04 10:07:38 +000051 enum Kind { Regular, EHFrame, Merge, MipsReginfo, MipsOptions };
Rafael Espindolac159c962015-10-19 21:00:02 +000052 Kind SectionKind;
53
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000054 InputSectionBase() : Repl(this) {}
55
Rafael Espindolac159c962015-10-19 21:00:02 +000056 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
57 Kind SectionKind);
58 OutputSectionBase<ELFT> *OutSec = nullptr;
Rui Ueyama424b4082016-06-17 01:18:46 +000059 uint32_t Alignment;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000060
Rui Ueyamac4aaed92015-10-22 18:49:53 +000061 // Used for garbage collection.
Rui Ueyama6a9ef852016-02-25 18:49:09 +000062 bool Live;
Rui Ueyamac4aaed92015-10-22 18:49:53 +000063
Rui Ueyama0b289522016-02-25 18:43:51 +000064 // This pointer points to the "real" instance of this instance.
65 // Usually Repl == this. However, if ICF merges two sections,
66 // Repl pointer of one section points to another section. So,
67 // if you need to get a pointer to this instance, do not use
68 // this but instead this->Repl.
69 InputSectionBase<ELFT> *Repl;
70
Rafael Espindola71675852015-09-22 00:16:19 +000071 // Returns the size of this section (even if this is a common or BSS.)
Simon Atanasyan13f6da12016-03-31 21:26:23 +000072 size_t getSize() const;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000073
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000074 static InputSectionBase<ELFT> Discarded;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000075
Rafael Espindola5d83ccd2015-08-13 19:18:30 +000076 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +000077 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindolae1901cc2015-09-24 15:11:50 +000078 ObjectFile<ELFT> *getFile() const { return File; }
Rui Ueyama809d8e22016-06-23 04:33:42 +000079 uintX_t getOffset(const DefinedRegular<ELFT> &Sym) const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000080
81 // Translate an offset in the input section to an offset in the output
82 // section.
Rui Ueyama809d8e22016-06-23 04:33:42 +000083 uintX_t getOffset(uintX_t Offset) const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000084
Rafael Espindolac159c962015-10-19 21:00:02 +000085 ArrayRef<uint8_t> getSectionData() const;
Rui Ueyama12504642015-10-27 21:51:13 +000086
George Rimar602fbee2016-06-24 11:18:44 +000087 void uncompress();
88
Rafael Espindola22ef9562016-04-13 01:40:19 +000089 void relocate(uint8_t *Buf, uint8_t *BufEnd);
Rui Ueyama809d8e22016-06-23 04:33:42 +000090 std::vector<Relocation<ELFT>> Relocations;
George Rimar602fbee2016-06-24 11:18:44 +000091
92 bool Compressed;
Rafael Espindolac159c962015-10-19 21:00:02 +000093};
94
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000095template <class ELFT> InputSectionBase<ELFT> InputSectionBase<ELFT>::Discarded;
Rafael Espindolac159c962015-10-19 21:00:02 +000096
Rui Ueyama3ea87272016-05-22 00:13:04 +000097// SectionPiece represents a piece of splittable section contents.
98struct SectionPiece {
Rui Ueyama34dc99e2016-05-22 01:15:32 +000099 SectionPiece(size_t Off, ArrayRef<uint8_t> Data)
Davide Italianoe6c8fa42016-05-28 23:27:38 +0000100 : InputOff(Off), Data((const uint8_t *)Data.data()), Size(Data.size()),
Rui Ueyamad8849272016-05-25 16:37:01 +0000101 Live(!Config->GcSections) {}
102
103 ArrayRef<uint8_t> data() { return {Data, Size}; }
104 size_t size() const { return Size; }
Rui Ueyama34dc99e2016-05-22 01:15:32 +0000105
Rui Ueyama3ea87272016-05-22 00:13:04 +0000106 size_t InputOff;
107 size_t OutputOff = -1;
Rui Ueyamad8849272016-05-25 16:37:01 +0000108
109private:
110 // We use bitfields because SplitInputSection is accessed by
111 // std::upper_bound very often.
112 // We want to save bits to make it cache friendly.
Davide Italianoe6c8fa42016-05-28 23:27:38 +0000113 const uint8_t *Data;
Rui Ueyamad8849272016-05-25 16:37:01 +0000114 uint32_t Size : 31;
115
116public:
117 uint32_t Live : 1;
Rui Ueyama3ea87272016-05-22 00:13:04 +0000118};
119
Rafael Espindolac159c962015-10-19 21:00:02 +0000120// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000121template <class ELFT> class MergeInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000122 typedef typename ELFT::uint uintX_t;
123 typedef typename ELFT::Sym Elf_Sym;
124 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000125
126public:
127 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
128 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000129 void splitIntoPieces();
130
131 // Mark the piece at a given offset live. Used by GC.
132 void markLiveAt(uintX_t Offset) { LiveOffsets.insert(Offset); }
133
134 // Translate an offset in the input section to an offset
135 // in the output section.
Rui Ueyama809d8e22016-06-23 04:33:42 +0000136 uintX_t getOffset(uintX_t Offset) const;
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000137
Rui Ueyama406b4692016-05-27 14:39:13 +0000138 void finalizePieces();
139
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000140 // Splittable sections are handled as a sequence of data
141 // rather than a single large blob of data.
142 std::vector<SectionPiece> Pieces;
143
144 // Returns the SectionPiece at a given input section offset.
145 SectionPiece *getSectionPiece(uintX_t Offset);
146 const SectionPiece *getSectionPiece(uintX_t Offset) const;
147
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000148private:
Rui Ueyamad6bd1372016-08-03 04:39:42 +0000149 std::vector<SectionPiece> splitStrings(ArrayRef<uint8_t> A, size_t Size);
150 std::vector<SectionPiece> splitNonStrings(ArrayRef<uint8_t> A, size_t Size);
151
Rui Ueyama406b4692016-05-27 14:39:13 +0000152 llvm::DenseMap<uintX_t, uintX_t> OffsetMap;
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000153 llvm::DenseSet<uintX_t> LiveOffsets;
Rafael Espindolac159c962015-10-19 21:00:02 +0000154};
155
Rafael Espindola2deeb602016-07-21 20:18:30 +0000156struct EhSectionPiece : public SectionPiece {
157 EhSectionPiece(size_t Off, ArrayRef<uint8_t> Data, unsigned FirstRelocation)
158 : SectionPiece(Off, Data), FirstRelocation(FirstRelocation) {}
159 unsigned FirstRelocation;
160};
161
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000162// This corresponds to a .eh_frame section of an input file.
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000163template <class ELFT> class EhInputSection : public InputSectionBase<ELFT> {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000164public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000165 typedef typename ELFT::Shdr Elf_Shdr;
166 typedef typename ELFT::uint uintX_t;
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000167 EhInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000168 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama88abd9b2016-05-22 23:53:00 +0000169 void split();
Rafael Espindola2deeb602016-07-21 20:18:30 +0000170 template <class RelTy> void split(ArrayRef<RelTy> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000171
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000172 // Splittable sections are handled as a sequence of data
173 // rather than a single large blob of data.
Rafael Espindola2deeb602016-07-21 20:18:30 +0000174 std::vector<EhSectionPiece> Pieces;
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000175
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000176 // Relocation section that refer to this one.
177 const Elf_Shdr *RelocSection = nullptr;
178};
179
Rafael Espindolac159c962015-10-19 21:00:02 +0000180// This corresponds to a non SHF_MERGE section of an input file.
181template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
Rui Ueyama0b289522016-02-25 18:43:51 +0000182 friend ICF<ELFT>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000183 typedef InputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000184 typedef typename ELFT::Shdr Elf_Shdr;
185 typedef typename ELFT::Rela Elf_Rela;
186 typedef typename ELFT::Rel Elf_Rel;
187 typedef typename ELFT::Sym Elf_Sym;
188 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000189
190public:
191 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
192
193 // Write this section to a mmap'ed file, assuming Buf is pointing to
194 // beginning of the output section.
195 void writeTo(uint8_t *Buf);
196
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000197 // Relocation sections that refer to this one.
Rui Ueyamac00718f2016-02-23 03:34:37 +0000198 llvm::TinyPtrVector<const Elf_Shdr *> RelocSections;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000199
Rui Ueyamaedffd912015-10-14 21:00:23 +0000200 // The offset from beginning of the output sections this section was assigned
201 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000202 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000203
Rafael Espindolac159c962015-10-19 21:00:02 +0000204 static bool classof(const InputSectionBase<ELFT> *S);
George Rimar58941ee2016-02-25 08:23:37 +0000205
206 InputSectionBase<ELFT> *getRelocatedSection();
207
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000208 // Register thunk related to the symbol. When the section is written
209 // to a mmap'ed file, target is requested to write an actual thunk code.
Peter Smithfb05cd92016-07-08 16:10:27 +0000210 // Now thunks is supported for MIPS and ARM target only.
211 void addThunk(const Thunk<ELFT> *T);
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000212
213 // The offset of synthetic thunk code from beginning of this section.
214 uint64_t getThunkOff() const;
215
216 // Size of chunk with thunks code.
217 uint64_t getThunksSize() const;
218
Rui Ueyama2b6fb802016-04-28 18:42:04 +0000219 template <class RelTy>
220 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
221
George Rimar58941ee2016-02-25 08:23:37 +0000222private:
Rui Ueyamafc467e72016-03-13 05:06:50 +0000223 template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000224 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
Rui Ueyama0b289522016-02-25 18:43:51 +0000225
226 // Called by ICF to merge two input sections.
227 void replace(InputSection<ELFT> *Other);
228
229 // Used by ICF.
230 uint64_t GroupId = 0;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000231
Peter Smithfb05cd92016-07-08 16:10:27 +0000232 llvm::TinyPtrVector<const Thunk<ELFT> *> Thunks;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000233};
234
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000235// MIPS .reginfo section provides information on the registers used by the code
236// in the object file. Linker should collect this information and write a single
237// .reginfo section in the output file. The output section contains a union of
238// used registers masks taken from input .reginfo sections and final value
239// of the `_gp` symbol. For details: Chapter 4 / "Register Information" at
240// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
241template <class ELFT>
242class MipsReginfoInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000243 typedef typename ELFT::Shdr Elf_Shdr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000244
245public:
Rui Ueyama70eed362016-01-06 22:42:43 +0000246 MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000247 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama70eed362016-01-06 22:42:43 +0000248
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000249 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
250};
251
252template <class ELFT>
253class MipsOptionsInputSection : public InputSectionBase<ELFT> {
254 typedef typename ELFT::Shdr Elf_Shdr;
255
256public:
257 MipsOptionsInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
258 static bool classof(const InputSectionBase<ELFT> *S);
259
260 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000261};
262
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000263// Common symbols don't belong to any section. But it is easier for us
264// to handle them as if they belong to some input section. So we defined
265// this class. CommonInputSection is a virtual singleton class that
266// "contains" all common symbols.
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000267template <class ELFT> class CommonInputSection : public InputSection<ELFT> {
268 typedef typename ELFT::uint uintX_t;
269
270public:
Rui Ueyama09d4f172016-07-29 03:39:44 +0000271 CommonInputSection(std::vector<DefinedCommon<ELFT> *> Syms);
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000272
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000273 // The singleton instance of this class.
274 static CommonInputSection<ELFT> *X;
275
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000276private:
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000277 static typename ELFT::Shdr Hdr;
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000278};
279
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000280template <class ELFT> CommonInputSection<ELFT> *CommonInputSection<ELFT>::X;
281template <class ELFT> typename ELFT::Shdr CommonInputSection<ELFT>::Hdr;
282
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000283} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000284} // namespace lld
285
286#endif