LIEF: Library to Instrument Executable Formats Version 1.0.0
Loading...
Searching...
No Matches
ELF/Section.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_ELF_SECTION_H
17#define LIEF_ELF_SECTION_H
18
19#include <string>
20#include <ostream>
21#include <vector>
22#include <memory>
23
24#include "LIEF/utils.hpp"
25#include "LIEF/visibility.h"
27
28#include "LIEF/ELF/enums.hpp"
29#include "LIEF/iterators.hpp"
30
31namespace LIEF {
32class SpanStream;
33namespace ELF {
34
35namespace DataHandler {
36class Handler;
37}
38
39class Segment;
40class Parser;
41class Binary;
42class Builder;
43class ExeLayout;
44class ObjectFileLayout;
45
46
49 friend class Parser;
50 friend class Binary;
51 friend class Builder;
52 friend class ExeLayout;
53 friend class ObjectFileLayout;
54
55 public:
56 using segments_t = std::vector<Segment*>;
59
60 static constexpr uint32_t MAX_SECTION_SIZE = 2_GB;
61
62 enum class TYPE : uint64_t {
63 SHT_NULL_ = 0,
64 PROGBITS = 1,
65 SYMTAB = 2,
66 STRTAB = 3,
67 RELA = 4,
68 HASH = 5,
69 DYNAMIC = 6,
70 NOTE = 7,
71 NOBITS = 8,
72 REL = 9,
73 SHLIB = 10,
74 DYNSYM = 11,
75 INIT_ARRAY = 14,
76 FINI_ARRAY = 15,
77 PREINIT_ARRAY = 16,
78 GROUP = 17,
79 SYMTAB_SHNDX = 18,
80 RELR = 19,
81
82 ANDROID_REL = 0x60000001,
83 ANDROID_RELA = 0x60000002,
84 LLVM_ADDRSIG = 0x6fff4c03,
86 ANDROID_RELR = 0x6fffff00,
87 GNU_ATTRIBUTES = 0x6ffffff5,
88 GNU_HASH = 0x6ffffff6,
89 GNU_VERDEF = 0x6ffffffd,
90 GNU_VERNEED = 0x6ffffffe,
91 GNU_VERSYM = 0x6fffffff,
92
93 _ID_SHIFT_ = 32,
94 _ARM_ID_ = 1LLU,
95 _HEX_ID_ = 2LLU,
96 _X86_64_ID_ = 2LLU,
97 _MIPS_ID_ = 3LLU,
98 _RISCV_ID_ = 4LLU,
99
100 ARM_EXIDX =
101 0x70000001U + (_ARM_ID_ << _ID_SHIFT_),
102 ARM_PREEMPTMAP =
103 0x70000002U +
104 (_ARM_ID_ << _ID_SHIFT_),
105 ARM_ATTRIBUTES =
106 0x70000003U +
107 (_ARM_ID_ << _ID_SHIFT_),
108 ARM_DEBUGOVERLAY = 0x70000004U + (_ARM_ID_ << _ID_SHIFT_),
109 ARM_OVERLAYSECTION = 0x70000005U + (_ARM_ID_ << _ID_SHIFT_),
110
111 HEX_ORDERED =
112 0x70000000 +
113 (_HEX_ID_ << _ID_SHIFT_),
115
116 /* this section based on their sizes */
117 X86_64_UNWIND =
118 0x70000001 + (_X86_64_ID_ << _ID_SHIFT_),
119
120 MIPS_LIBLIST = 0x70000000 + (_MIPS_ID_ << _ID_SHIFT_),
121 MIPS_MSYM = 0x70000001 + (_MIPS_ID_ << _ID_SHIFT_),
122 MIPS_CONFLICT = 0x70000002 + (_MIPS_ID_ << _ID_SHIFT_),
123 MIPS_GPTAB = 0x70000003 + (_MIPS_ID_ << _ID_SHIFT_),
124 MIPS_UCODE = 0x70000004 + (_MIPS_ID_ << _ID_SHIFT_),
125 MIPS_DEBUG = 0x70000005 + (_MIPS_ID_ << _ID_SHIFT_),
126 MIPS_REGINFO = 0x70000006 + (_MIPS_ID_ << _ID_SHIFT_),
127 MIPS_PACKAGE = 0x70000007 + (_MIPS_ID_ << _ID_SHIFT_),
128 MIPS_PACKSYM = 0x70000008 + (_MIPS_ID_ << _ID_SHIFT_),
129 MIPS_RELD = 0x70000009 + (_MIPS_ID_ << _ID_SHIFT_),
130 MIPS_IFACE = 0x7000000b + (_MIPS_ID_ << _ID_SHIFT_),
131 MIPS_CONTENT = 0x7000000c + (_MIPS_ID_ << _ID_SHIFT_),
132 MIPS_OPTIONS = 0x7000000d + (_MIPS_ID_ << _ID_SHIFT_),
133 MIPS_SHDR = 0x70000010 + (_MIPS_ID_ << _ID_SHIFT_),
134 MIPS_FDESC = 0x70000011 + (_MIPS_ID_ << _ID_SHIFT_),
135 MIPS_EXTSYM = 0x70000012 + (_MIPS_ID_ << _ID_SHIFT_),
136 MIPS_DENSE = 0x70000013 + (_MIPS_ID_ << _ID_SHIFT_),
137 MIPS_PDESC = 0x70000014 + (_MIPS_ID_ << _ID_SHIFT_),
138 MIPS_LOCSYM = 0x70000015 + (_MIPS_ID_ << _ID_SHIFT_),
139 MIPS_AUXSYM = 0x70000016 + (_MIPS_ID_ << _ID_SHIFT_),
140 MIPS_OPTSYM = 0x70000017 + (_MIPS_ID_ << _ID_SHIFT_),
141 MIPS_LOCSTR = 0x70000018 + (_MIPS_ID_ << _ID_SHIFT_),
142 MIPS_LINE = 0x70000019 + (_MIPS_ID_ << _ID_SHIFT_),
143 MIPS_RFDESC = 0x7000001a + (_MIPS_ID_ << _ID_SHIFT_),
144 MIPS_DELTASYM = 0x7000001b + (_MIPS_ID_ << _ID_SHIFT_),
145 MIPS_DELTAINST = 0x7000001c + (_MIPS_ID_ << _ID_SHIFT_),
146 MIPS_DELTACLASS = 0x7000001d + (_MIPS_ID_ << _ID_SHIFT_),
147 MIPS_DWARF = 0x7000001e + (_MIPS_ID_ << _ID_SHIFT_),
148 MIPS_DELTADECL = 0x7000001f + (_MIPS_ID_ << _ID_SHIFT_),
149 MIPS_SYMBOL_LIB = 0x70000020 + (_MIPS_ID_ << _ID_SHIFT_),
150 MIPS_EVENTS = 0x70000021 + (_MIPS_ID_ << _ID_SHIFT_),
151 MIPS_TRANSLATE = 0x70000022 + (_MIPS_ID_ << _ID_SHIFT_),
152 MIPS_PIXIE = 0x70000023 + (_MIPS_ID_ << _ID_SHIFT_),
153 MIPS_XLATE = 0x70000024 + (_MIPS_ID_ << _ID_SHIFT_),
154 MIPS_XLATE_DEBUG = 0x70000025 + (_MIPS_ID_ << _ID_SHIFT_),
155 MIPS_WHIRL = 0x70000026 + (_MIPS_ID_ << _ID_SHIFT_),
156 MIPS_EH_REGION = 0x70000027 + (_MIPS_ID_ << _ID_SHIFT_),
157 MIPS_XLATE_OLD = 0x70000028 + (_MIPS_ID_ << _ID_SHIFT_),
158 MIPS_PDR_EXCEPTION = 0x70000029 + (_MIPS_ID_ << _ID_SHIFT_),
159 MIPS_ABIFLAGS = 0x7000002a + (_MIPS_ID_ << _ID_SHIFT_),
160 MIPS_XHASH = 0x7000002b + (_MIPS_ID_ << _ID_SHIFT_),
161
162 RISCV_ATTRIBUTES = 0x70000003 + (_RISCV_ID_ << _ID_SHIFT_),
163 };
164
165 enum class FLAGS : uint64_t {
166 NONE = 0x000000000,
167 WRITE = 0x000000001,
168 ALLOC = 0x000000002,
169 EXECINSTR =
170 0x000000004,
171 MERGE = 0x000000010,
172 STRINGS =
173 0x000000020,
174 INFO_LINK = 0x000000040,
176 LINK_ORDER =
177 0x000000080,
178 OS_NONCONFORMING = 0x000000100,
180 GROUP = 0x000000200,
181 TLS = 0x000000400,
182 COMPRESSED = 0x000000800,
183 GNU_RETAIN = 0x000200000,
184 EXCLUDE = 0x080000000,
185
186 _ID_SHIFT_ = 32,
187 _XCORE_ID_ = 1LLU,
188 _HEX_ID_ = 3LLU,
189 _X86_64_ID_ = 2LLU,
190 _MIPS_ID_ = 4LLU,
191 _ARM_ID_ = 5LLU,
192
193 XCORE_SHF_DP_SECTION = 0x010000000 + (_XCORE_ID_ << _ID_SHIFT_),
194 XCORE_SHF_CP_SECTION = 0x020000000 + (_XCORE_ID_ << _ID_SHIFT_),
195
196 X86_64_LARGE = 0x010000000 + (_X86_64_ID_ << _ID_SHIFT_),
197
198 HEX_GPREL = 0x010000000 + (_HEX_ID_ << _ID_SHIFT_),
199
200 MIPS_NODUPES = 0x001000000 + (_MIPS_ID_ << _ID_SHIFT_),
201 MIPS_NAMES = 0x002000000 + (_MIPS_ID_ << _ID_SHIFT_),
202 MIPS_LOCAL = 0x004000000 + (_MIPS_ID_ << _ID_SHIFT_),
203 MIPS_NOSTRIP = 0x008000000 + (_MIPS_ID_ << _ID_SHIFT_),
204 MIPS_GPREL = 0x010000000 + (_MIPS_ID_ << _ID_SHIFT_),
205 MIPS_MERGE = 0x020000000 + (_MIPS_ID_ << _ID_SHIFT_),
206 MIPS_ADDR = 0x040000000 + (_MIPS_ID_ << _ID_SHIFT_),
207 MIPS_STRING = 0x080000000 + (_MIPS_ID_ << _ID_SHIFT_),
208
209 ARM_PURECODE = 0x020000000 + (_ARM_ID_ << _ID_SHIFT_),
210 };
211
212 static constexpr uint64_t FLAG_MASK =
213 (uint64_t(1) << uint8_t(FLAGS::_ID_SHIFT_)) - 1;
214 static constexpr uint64_t TYPE_MASK =
215 (uint64_t(1) << uint8_t(TYPE::_ID_SHIFT_)) - 1;
216
217 static TYPE type_from(uint32_t value, ARCH arch);
218 static uint32_t to_value(TYPE type) {
219 return static_cast<uint64_t>(type) & TYPE_MASK;
220 }
221
222 Section(const std::string& name, TYPE type = TYPE::PROGBITS) :
223 LIEF::Section(name),
224 type_{type} {}
225
226 Section() = default;
227 ~Section() override = default;
228
230 swap(other);
231 return *this;
232 }
233 Section(const Section& other);
234 void swap(Section& other) noexcept;
235
236 TYPE type() const {
237 return type_;
238 }
239
241 span<const uint8_t> content() const override;
242
244 void content(const std::vector<uint8_t>& data) override;
245
246 void content(std::vector<uint8_t>&& data);
247
249 uint64_t flags() const {
250 return flags_;
251 }
252
254 bool has(FLAGS flag) const;
255
257 bool has(const Segment& segment) const;
258
260 std::vector<FLAGS> flags_list() const;
261
262 uint64_t size() const override {
263 return size_;
264 }
265
266 void size(uint64_t size) override;
267
268 void offset(uint64_t offset) override;
269
270 uint64_t offset() const override {
271 return offset_;
272 }
273
275 uint64_t file_offset() const {
276 return this->offset();
277 }
278
283 uint64_t original_size() const {
284 return original_size_;
285 }
286
288 uint64_t alignment() const {
289 return address_align_;
290 }
291
294 uint64_t information() const {
295 return info_;
296 }
297
304 uint64_t entry_size() const {
305 return entry_size_;
306 }
307
309 uint32_t link() const {
310 return link_;
311 }
312
314 Section& clear(uint8_t value = 0);
315
317 void add(FLAGS flag);
318
320 void remove(FLAGS flag);
321
322 void type(TYPE type) {
323 type_ = type;
324 }
325
326 void flags(uint64_t flags) {
327 flags_ = flags;
328 }
329
330 void clear_flags() {
331 flags_ = 0;
332 }
333
334 void file_offset(uint64_t offset) {
335 this->offset(offset);
336 }
337
338 void link(uint32_t link) {
339 link_ = link;
340 }
341
342 void information(uint32_t info) {
343 info_ = info;
344 }
345
346 void alignment(uint64_t alignment) {
347 address_align_ = alignment;
348 }
349
350 void entry_size(uint64_t entry_size) {
351 entry_size_ = entry_size;
352 }
353
355 return segments_;
356 }
357
359 return segments_;
360 }
361
363 is_frame_ = true;
364 return *this;
365 }
366
367 bool is_frame() const {
368 return is_frame_;
369 }
370
371 void accept(Visitor& visitor) const override;
372
374 add(c);
375 return *this;
376 }
377
379 remove(c);
380 return *this;
381 }
382
384 std::unique_ptr<SpanStream> stream() const;
385
386 LIEF_API friend std::ostream& operator<<(std::ostream& os,
387 const Section& section);
388
389 private:
390 template<class T>
391 LIEF_LOCAL Section(const T& header, ARCH arch);
392
393 LIEF_LOCAL span<uint8_t> writable_content();
394 ARCH arch_ = ARCH::NONE;
395 TYPE type_ = TYPE::SHT_NULL_;
396 uint64_t flags_ = 0;
397 uint64_t original_size_ = 0;
398 uint32_t link_ = 0;
399 uint32_t info_ = 0;
400 uint64_t address_align_ = 0;
401 uint64_t entry_size_ = 0;
402 segments_t segments_;
403 bool is_frame_ = false;
404 DataHandler::Handler* datahandler_ = nullptr;
405 std::vector<uint8_t> content_c_;
406};
407
410
411}
412}
413
415
416#endif
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:48
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
friend class ObjectFileLayout
Definition ELF/Section.hpp:53
void size(uint64_t size) override
Change the section size.
static TYPE type_from(uint32_t value, ARCH arch)
ref_iterator< segments_t & > it_segments
Definition ELF/Section.hpp:57
span< const uint8_t > content() const override
Section's content.
it_segments segments()
Definition ELF/Section.hpp:354
uint64_t original_size() const
Original size of the section's data.
Definition ELF/Section.hpp:283
friend class ExeLayout
Definition ELF/Section.hpp:52
Section & operator+=(FLAGS c)
Definition ELF/Section.hpp:373
void clear_flags()
Definition ELF/Section.hpp:330
uint64_t flags() const
Section flags.
Definition ELF/Section.hpp:249
std::vector< Segment * > segments_t
Definition ELF/Section.hpp:56
static constexpr uint64_t TYPE_MASK
Definition ELF/Section.hpp:214
Section & as_frame()
Definition ELF/Section.hpp:362
void offset(uint64_t offset) override
static constexpr uint64_t FLAG_MASK
Definition ELF/Section.hpp:212
friend std::ostream & operator<<(std::ostream &os, const Section &section)
void remove(FLAGS flag)
Remove the given ELF_SECTION_FLAGS.
std::vector< FLAGS > flags_list() const
Return section flags as a std::set.
bool is_frame() const
Definition ELF/Section.hpp:367
uint64_t file_offset() const
Definition ELF/Section.hpp:275
~Section() override=default
void alignment(uint64_t alignment)
Definition ELF/Section.hpp:346
void information(uint32_t info)
Definition ELF/Section.hpp:342
static uint32_t to_value(TYPE type)
Definition ELF/Section.hpp:218
std::unique_ptr< SpanStream > stream() const
Return a stream over the content of this section.
Section(const Section &other)
const_ref_iterator< const segments_t & > it_const_segments
Definition ELF/Section.hpp:58
TYPE type() const
Definition ELF/Section.hpp:236
friend class Builder
Definition ELF/Section.hpp:51
TYPE
Definition ELF/Section.hpp:62
@ _ID_SHIFT_
Definition ELF/Section.hpp:93
@ SHT_NULL_
Definition ELF/Section.hpp:63
@ PROGBITS
Definition ELF/Section.hpp:64
Section & operator-=(FLAGS c)
Definition ELF/Section.hpp:378
void content(std::vector< uint8_t > &&data)
uint32_t link() const
Index to another section.
Definition ELF/Section.hpp:309
uint64_t size() const override
section's size (size in the binary, not the virtual size)
Definition ELF/Section.hpp:262
friend class Binary
Definition ELF/Section.hpp:50
void content(const std::vector< uint8_t > &data) override
Set section content.
Section & clear(uint8_t value=0)
Clear the content of the section with the given value.
void accept(Visitor &visitor) const override
void swap(Section &other) noexcept
bool has(const Segment &segment) const
True if the section is wrapped by the given Segment
void entry_size(uint64_t entry_size)
Definition ELF/Section.hpp:350
friend class Parser
Definition ELF/Section.hpp:49
Section(const std::string &name, TYPE type=TYPE::PROGBITS)
Definition ELF/Section.hpp:222
FLAGS
Definition ELF/Section.hpp:165
@ _ID_SHIFT_
Definition ELF/Section.hpp:186
void type(TYPE type)
Definition ELF/Section.hpp:322
uint64_t information() const
Section information. The meaning of this value depends on the section's type.
Definition ELF/Section.hpp:294
uint64_t offset() const override
Offset in the binary.
Definition ELF/Section.hpp:270
static constexpr uint32_t MAX_SECTION_SIZE
Definition ELF/Section.hpp:60
void link(uint32_t link)
Definition ELF/Section.hpp:338
void add(FLAGS flag)
Add the given ELF_SECTION_FLAGS.
Section & operator=(Section other)
Definition ELF/Section.hpp:229
uint64_t alignment() const
Section file alignment.
Definition ELF/Section.hpp:288
it_const_segments segments() const
Definition ELF/Section.hpp:358
bool has(FLAGS flag) const
True if the section has the given flag
uint64_t entry_size() const
This function returns the size of an element in the case of a section that contains an array.
Definition ELF/Section.hpp:304
void file_offset(uint64_t offset)
Definition ELF/Section.hpp:334
void flags(uint64_t flags)
Definition ELF/Section.hpp:326
Class which represents the ELF segments.
Definition Segment.hpp:48
Class which represents an abstracted section.
Definition Abstract/Section.hpp:29
virtual std::string name() const
section's name
Definition Abstract/Section.hpp:43
Definition SpanStream.hpp:32
Definition Visitor.hpp:212
Iterator which returns reference on container's values.
Definition iterators.hpp:45
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
Definition ELF/Binary.hpp:38
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
const char * to_string(DynamicEntry::TAG e)
ARCH
Definition ELF/enums.hpp:30
@ NONE
Definition ELF/enums.hpp:31
LIEF namespace.
Definition Abstract/Binary.hpp:40
tcb::span< ElementType, Extent > span
Definition span.hpp:22
ref_iterator< CT, U, typename decay_t< CT >::const_iterator > const_ref_iterator
Iterator which return const ref on container's values.
Definition iterators.hpp:286
#define LIEF_API
Definition visibility.h:43
#define LIEF_LOCAL
Definition visibility.h:44