blob: 0b6117ee242e0ce762e041bf03f368bc1369026b [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
Rui Ueyama0b289522016-02-25 18:43:51 +000021template <class ELFT> class ICF;
Michael J. Spencer84487f12015-07-24 21:03:07 +000022template <class ELFT> class ObjectFile;
Rafael Espindola832b93f2015-08-24 20:06:32 +000023template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000024template <class ELFT> class OutputSectionBase;
Michael J. Spencer84487f12015-07-24 21:03:07 +000025
Rafael Espindola71675852015-09-22 00:16:19 +000026// This corresponds to a section of an input file.
Rafael Espindolac159c962015-10-19 21:00:02 +000027template <class ELFT> class InputSectionBase {
28protected:
Rui Ueyama9328b2c2016-03-14 23:16:09 +000029 typedef typename ELFT::Rel Elf_Rel;
30 typedef typename ELFT::Rela Elf_Rela;
31 typedef typename ELFT::Shdr Elf_Shdr;
32 typedef typename ELFT::Sym Elf_Sym;
33 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +000034 const Elf_Shdr *Header;
35
36 // The file this section is from.
37 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000038
39public:
Simon Atanasyan1d7df402015-12-20 10:57:34 +000040 enum Kind { Regular, EHFrame, Merge, MipsReginfo };
Rafael Espindolac159c962015-10-19 21:00:02 +000041 Kind SectionKind;
42
43 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
44 Kind SectionKind);
45 OutputSectionBase<ELFT> *OutSec = nullptr;
Rui Ueyama6a9ef852016-02-25 18:49:09 +000046 uint32_t Align;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000047
Rui Ueyamac4aaed92015-10-22 18:49:53 +000048 // Used for garbage collection.
Rui Ueyama6a9ef852016-02-25 18:49:09 +000049 bool Live;
Rui Ueyamac4aaed92015-10-22 18:49:53 +000050
Rui Ueyama0b289522016-02-25 18:43:51 +000051 // This pointer points to the "real" instance of this instance.
52 // Usually Repl == this. However, if ICF merges two sections,
53 // Repl pointer of one section points to another section. So,
54 // if you need to get a pointer to this instance, do not use
55 // this but instead this->Repl.
56 InputSectionBase<ELFT> *Repl;
57
Rafael Espindola71675852015-09-22 00:16:19 +000058 // Returns the size of this section (even if this is a common or BSS.)
Rafael Espindola83b0dc62015-08-13 22:21:37 +000059 size_t getSize() const { return Header->sh_size; }
60
Rui Ueyama733153d2016-02-24 18:33:35 +000061 static InputSectionBase<ELFT> *Discarded;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000062
Rafael Espindola5d83ccd2015-08-13 19:18:30 +000063 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +000064 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindolae1901cc2015-09-24 15:11:50 +000065 ObjectFile<ELFT> *getFile() const { return File; }
Rafael Espindola48225b42015-10-23 19:55:11 +000066 uintX_t getOffset(const Elf_Sym &Sym);
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000067
68 // Translate an offset in the input section to an offset in the output
69 // section.
70 uintX_t getOffset(uintX_t Offset);
71
Rafael Espindolac159c962015-10-19 21:00:02 +000072 ArrayRef<uint8_t> getSectionData() const;
Rui Ueyama12504642015-10-27 21:51:13 +000073
74 // Returns a section that Rel is pointing to. Used by the garbage collector.
Rui Ueyamad7e4a282016-02-24 00:23:13 +000075 InputSectionBase<ELFT> *getRelocTarget(const Elf_Rel &Rel) const;
76 InputSectionBase<ELFT> *getRelocTarget(const Elf_Rela &Rel) const;
Rafael Espindola9a6e4632015-11-11 10:18:52 +000077
Rafael Espindola69082f02016-03-18 18:11:26 +000078 template <class RelTy>
79 void relocate(uint8_t *Buf, uint8_t *BufEnd,
80 llvm::iterator_range<const RelTy *> Rels);
Simon Atanasyan09b3e362015-12-01 21:24:45 +000081
82private:
Rui Ueyamafc467e72016-03-13 05:06:50 +000083 template <class RelTy>
84 uint8_t *findMipsPairedReloc(uint8_t *Buf, const RelTy *Rel,
85 const RelTy *End);
Rafael Espindolac159c962015-10-19 21:00:02 +000086};
87
88template <class ELFT>
Rui Ueyama733153d2016-02-24 18:33:35 +000089InputSectionBase<ELFT> *
90 InputSectionBase<ELFT>::Discarded = (InputSectionBase<ELFT> *)-1ULL;
Rafael Espindolac159c962015-10-19 21:00:02 +000091
Rui Ueyama5a32c762016-01-06 03:16:23 +000092// Usually sections are copied to the output as atomic chunks of data,
93// but some special types of sections are split into small pieces of data
94// and each piece is copied to a different place in the output.
95// This class represents such special sections.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000096template <class ELFT> class SplitInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +000097 typedef typename ELFT::Shdr Elf_Shdr;
98 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000099
100public:
101 SplitInputSection(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
102 typename InputSectionBase<ELFT>::Kind SectionKind);
Rui Ueyama5a32c762016-01-06 03:16:23 +0000103
104 // For each piece of data, we maintain the offsets in the input section and
105 // in the output section. The latter may be -1 if it is not assigned yet.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000106 std::vector<std::pair<uintX_t, uintX_t>> Offsets;
Rui Ueyama5a32c762016-01-06 03:16:23 +0000107
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000108 std::pair<std::pair<uintX_t, uintX_t> *, uintX_t>
109 getRangeAndSize(uintX_t Offset);
110};
111
Rafael Espindolac159c962015-10-19 21:00:02 +0000112// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000113template <class ELFT> class MergeInputSection : public SplitInputSection<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000114 typedef typename ELFT::uint uintX_t;
115 typedef typename ELFT::Sym Elf_Sym;
116 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000117
118public:
119 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
120 static bool classof(const InputSectionBase<ELFT> *S);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000121 // Translate an offset in the input section to an offset in the output
122 // section.
Rafael Espindola48225b42015-10-23 19:55:11 +0000123 uintX_t getOffset(uintX_t Offset);
Rafael Espindolac159c962015-10-19 21:00:02 +0000124};
125
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000126// This corresponds to a .eh_frame section of an input file.
127template <class ELFT> class EHInputSection : public SplitInputSection<ELFT> {
128public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000129 typedef typename ELFT::Shdr Elf_Shdr;
130 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000131 EHInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
132 static bool classof(const InputSectionBase<ELFT> *S);
133
134 // Translate an offset in the input section to an offset in the output
135 // section.
136 uintX_t getOffset(uintX_t Offset);
137
138 // Relocation section that refer to this one.
139 const Elf_Shdr *RelocSection = nullptr;
140};
141
Rafael Espindolac159c962015-10-19 21:00:02 +0000142// This corresponds to a non SHF_MERGE section of an input file.
143template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
Rui Ueyama0b289522016-02-25 18:43:51 +0000144 friend ICF<ELFT>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000145 typedef InputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000146 typedef typename ELFT::Shdr Elf_Shdr;
147 typedef typename ELFT::Rela Elf_Rela;
148 typedef typename ELFT::Rel Elf_Rel;
149 typedef typename ELFT::Sym Elf_Sym;
150 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000151
152public:
153 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
154
155 // Write this section to a mmap'ed file, assuming Buf is pointing to
156 // beginning of the output section.
157 void writeTo(uint8_t *Buf);
158
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000159 // Relocation sections that refer to this one.
Rui Ueyamac00718f2016-02-23 03:34:37 +0000160 llvm::TinyPtrVector<const Elf_Shdr *> RelocSections;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000161
Rui Ueyamaedffd912015-10-14 21:00:23 +0000162 // The offset from beginning of the output sections this section was assigned
163 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000164 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000165
Rafael Espindolac159c962015-10-19 21:00:02 +0000166 static bool classof(const InputSectionBase<ELFT> *S);
George Rimar58941ee2016-02-25 08:23:37 +0000167
168 InputSectionBase<ELFT> *getRelocatedSection();
169
170private:
Rui Ueyamafc467e72016-03-13 05:06:50 +0000171 template <class RelTy>
172 void copyRelocations(uint8_t *Buf, llvm::iterator_range<const RelTy *> Rels);
Rui Ueyama0b289522016-02-25 18:43:51 +0000173
174 // Called by ICF to merge two input sections.
175 void replace(InputSection<ELFT> *Other);
176
177 // Used by ICF.
178 uint64_t GroupId = 0;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000179};
180
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000181// MIPS .reginfo section provides information on the registers used by the code
182// in the object file. Linker should collect this information and write a single
183// .reginfo section in the output file. The output section contains a union of
184// used registers masks taken from input .reginfo sections and final value
185// of the `_gp` symbol. For details: Chapter 4 / "Register Information" at
186// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
187template <class ELFT>
188class MipsReginfoInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000189 typedef typename ELFT::Shdr Elf_Shdr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000190
191public:
Rui Ueyama70eed362016-01-06 22:42:43 +0000192 MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000193 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama70eed362016-01-06 22:42:43 +0000194
195 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000196};
197
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000198} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000199} // namespace lld
200
201#endif