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