blob: d72f9f23dd49acc37a353e7e6b492895f123c4bb [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"
15#include "llvm/Object/ELF.h"
16
17namespace lld {
18namespace elf2 {
19
Michael J. Spencer84487f12015-07-24 21:03:07 +000020template <class ELFT> class ObjectFile;
Rafael Espindola832b93f2015-08-24 20:06:32 +000021template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000022template <class ELFT> class OutputSectionBase;
Michael J. Spencer84487f12015-07-24 21:03:07 +000023
Rafael Espindola71675852015-09-22 00:16:19 +000024// This corresponds to a section of an input file.
Rafael Espindolac159c962015-10-19 21:00:02 +000025template <class ELFT> class InputSectionBase {
26protected:
Rui Ueyama12504642015-10-27 21:51:13 +000027 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
28 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola7d4038dc2015-09-15 12:43:09 +000029 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola4ea00212015-09-21 22:01:00 +000030 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindolac5c82912015-08-24 19:28:31 +000031 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +000032 const Elf_Shdr *Header;
33
34 // The file this section is from.
35 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000036
37public:
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000038 enum Kind { Regular, EHFrame, Merge };
Rafael Espindolac159c962015-10-19 21:00:02 +000039 Kind SectionKind;
40
41 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
42 Kind SectionKind);
43 OutputSectionBase<ELFT> *OutSec = nullptr;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000044
Rui Ueyamac4aaed92015-10-22 18:49:53 +000045 // Used for garbage collection.
46 // Live bit makes sense only when Config->GcSections is true.
47 bool isLive() const { return !Config->GcSections || Live; }
48 bool Live = false;
49
Rafael Espindola71675852015-09-22 00:16:19 +000050 // Returns the size of this section (even if this is a common or BSS.)
Rafael Espindola83b0dc62015-08-13 22:21:37 +000051 size_t getSize() const { return Header->sh_size; }
52
Rafael Espindolac159c962015-10-19 21:00:02 +000053 static InputSectionBase<ELFT> Discarded;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000054
Rafael Espindola5d83ccd2015-08-13 19:18:30 +000055 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +000056 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindolae1901cc2015-09-24 15:11:50 +000057 ObjectFile<ELFT> *getFile() const { return File; }
Michael J. Spencer8039dae22015-07-29 00:30:10 +000058
Rafael Espindola83b0dc62015-08-13 22:21:37 +000059 // The writer sets and uses the addresses.
Michael J. Spencerbaae5382015-09-05 00:25:33 +000060 uintX_t getAlign() {
61 // The ELF spec states that a value of 0 means the section has no alignment
62 // constraits.
63 return std::max<uintX_t>(Header->sh_addralign, 1);
64 }
Rafael Espindola83b0dc62015-08-13 22:21:37 +000065
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.
75 InputSectionBase<ELFT> *getRelocTarget(const Elf_Rel &Rel);
76 InputSectionBase<ELFT> *getRelocTarget(const Elf_Rela &Rel);
Rafael Espindola9a6e4632015-11-11 10:18:52 +000077
78 template <bool isRela>
Simon Atanasyan09b3e362015-12-01 21:24:45 +000079 using RelIteratorRange =
80 llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, isRela> *>;
81
82 template <bool isRela>
83 void relocate(uint8_t *Buf, uint8_t *BufEnd, RelIteratorRange<isRela> Rels);
84
85private:
86 template <bool isRela>
87 uint8_t *findMipsPairedReloc(uint8_t *Buf, uint32_t Type,
88 RelIteratorRange<isRela> Rels);
Rafael Espindolac159c962015-10-19 21:00:02 +000089};
90
91template <class ELFT>
92InputSectionBase<ELFT>
93 InputSectionBase<ELFT>::Discarded(nullptr, nullptr,
94 InputSectionBase<ELFT>::Regular);
95
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000096template <class ELFT> class SplitInputSection : public InputSectionBase<ELFT> {
97 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
98 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
99
100public:
101 SplitInputSection(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
102 typename InputSectionBase<ELFT>::Kind SectionKind);
103 std::vector<std::pair<uintX_t, uintX_t>> Offsets;
104 std::pair<std::pair<uintX_t, uintX_t> *, uintX_t>
105 getRangeAndSize(uintX_t Offset);
106};
107
Rafael Espindolac159c962015-10-19 21:00:02 +0000108// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000109template <class ELFT> class MergeInputSection : public SplitInputSection<ELFT> {
Rafael Espindolac159c962015-10-19 21:00:02 +0000110 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
111 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
112 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
113
114public:
115 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
116 static bool classof(const InputSectionBase<ELFT> *S);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000117 // Translate an offset in the input section to an offset in the output
118 // section.
Rafael Espindola48225b42015-10-23 19:55:11 +0000119 uintX_t getOffset(uintX_t Offset);
Rafael Espindolac159c962015-10-19 21:00:02 +0000120};
121
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000122// This corresponds to a .eh_frame section of an input file.
123template <class ELFT> class EHInputSection : public SplitInputSection<ELFT> {
124public:
125 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
126 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
127 EHInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
128 static bool classof(const InputSectionBase<ELFT> *S);
129
130 // Translate an offset in the input section to an offset in the output
131 // section.
132 uintX_t getOffset(uintX_t Offset);
133
134 // Relocation section that refer to this one.
135 const Elf_Shdr *RelocSection = nullptr;
136};
137
Rafael Espindolac159c962015-10-19 21:00:02 +0000138// This corresponds to a non SHF_MERGE section of an input file.
139template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
140 typedef InputSectionBase<ELFT> Base;
141 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
142 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
143 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
144 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
145 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
146
147public:
148 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
149
150 // Write this section to a mmap'ed file, assuming Buf is pointing to
151 // beginning of the output section.
152 void writeTo(uint8_t *Buf);
153
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000154 // Relocation sections that refer to this one.
155 SmallVector<const Elf_Shdr *, 1> RelocSections;
156
Rui Ueyamaedffd912015-10-14 21:00:23 +0000157 // The offset from beginning of the output sections this section was assigned
158 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000159 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000160
Rafael Espindolac159c962015-10-19 21:00:02 +0000161 static bool classof(const InputSectionBase<ELFT> *S);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000162};
163
Michael J. Spencer84487f12015-07-24 21:03:07 +0000164} // namespace elf2
165} // namespace lld
166
167#endif