LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
ELF/Section.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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
46class LIEF_API Section : public LIEF::Section {
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*>;
57 using it_segments = ref_iterator<segments_t&>;
58 using it_const_segments = const_ref_iterator<const segments_t&>;
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,
85 ANDROID_RELR = 0x6fffff00,
86 GNU_ATTRIBUTES = 0x6ffffff5,
87 GNU_HASH = 0x6ffffff6,
88 GNU_VERDEF = 0x6ffffffd,
89 GNU_VERNEED = 0x6ffffffe,
90 GNU_VERSYM = 0x6fffffff,
91
92 _ID_SHIFT_ = 32,
93 _ARM_ID_ = 1LLU, _HEX_ID_ = 2LLU, _X86_64_ID_ = 2LLU,
94 _MIPS_ID_ = 3LLU, _RISCV_ID_ = 4LLU,
95
96 ARM_EXIDX = 0x70000001U + (_ARM_ID_ << _ID_SHIFT_),
97 ARM_PREEMPTMAP = 0x70000002U + (_ARM_ID_ << _ID_SHIFT_),
98 ARM_ATTRIBUTES = 0x70000003U + (_ARM_ID_ << _ID_SHIFT_),
99 ARM_DEBUGOVERLAY = 0x70000004U + (_ARM_ID_ << _ID_SHIFT_),
100 ARM_OVERLAYSECTION = 0x70000005U + (_ARM_ID_ << _ID_SHIFT_),
101
102 HEX_ORDERED = 0x70000000 + (_HEX_ID_ << _ID_SHIFT_),
103
104 /* this section based on their sizes */
105 X86_64_UNWIND = 0x70000001 + (_X86_64_ID_ << _ID_SHIFT_),
106
107 MIPS_REGINFO = 0x70000006 + (_MIPS_ID_ << _ID_SHIFT_),
108 MIPS_OPTIONS = 0x7000000d + (_MIPS_ID_ << _ID_SHIFT_),
109 MIPS_ABIFLAGS = 0x7000002a + (_MIPS_ID_ << _ID_SHIFT_),
110
111 RISCV_ATTRIBUTES = 0x70000003 + (_RISCV_ID_ << _ID_SHIFT_),
112 };
113
114 enum class FLAGS : uint64_t {
115 NONE = 0x000000000,
116 WRITE = 0x000000001,
117 ALLOC = 0x000000002,
118 EXECINSTR = 0x000000004,
119 MERGE = 0x000000010,
120 STRINGS = 0x000000020,
121 INFO_LINK = 0x000000040,
122 LINK_ORDER = 0x000000080,
123 OS_NONCONFORMING = 0x000000100,
124 GROUP = 0x000000200,
125 TLS = 0x000000400,
126 COMPRESSED = 0x000000800,
127 GNU_RETAIN = 0x000200000,
128 EXCLUDE = 0x080000000,
129
130 _ID_SHIFT_ = 32,
131 _XCORE_ID_ = 1LLU, _HEX_ID_ = 3LLU, _X86_64_ID_ = 2LLU,
132 _MIPS_ID_ = 4LLU, _ARM_ID_ = 5LLU,
133
134 XCORE_SHF_DP_SECTION = 0x010000000 + (_XCORE_ID_ << _ID_SHIFT_),
135 XCORE_SHF_CP_SECTION = 0x020000000 + (_XCORE_ID_ << _ID_SHIFT_),
136
137 X86_64_LARGE = 0x010000000 + (_X86_64_ID_ << _ID_SHIFT_),
138
139 HEX_GPREL = 0x010000000 + (_HEX_ID_ << _ID_SHIFT_),
140
141 MIPS_NODUPES = 0x001000000 + (_MIPS_ID_ << _ID_SHIFT_),
142 MIPS_NAMES = 0x002000000 + (_MIPS_ID_ << _ID_SHIFT_),
143 MIPS_LOCAL = 0x004000000 + (_MIPS_ID_ << _ID_SHIFT_),
144 MIPS_NOSTRIP = 0x008000000 + (_MIPS_ID_ << _ID_SHIFT_),
145 MIPS_GPREL = 0x010000000 + (_MIPS_ID_ << _ID_SHIFT_),
146 MIPS_MERGE = 0x020000000 + (_MIPS_ID_ << _ID_SHIFT_),
147 MIPS_ADDR = 0x040000000 + (_MIPS_ID_ << _ID_SHIFT_),
148 MIPS_STRING = 0x080000000 + (_MIPS_ID_ << _ID_SHIFT_),
149
150 ARM_PURECODE = 0x020000000 + (_ARM_ID_ << _ID_SHIFT_),
151 };
152
153 static constexpr uint64_t FLAG_MASK = (uint64_t(1) << uint8_t(FLAGS::_ID_SHIFT_)) - 1;
154 static constexpr uint64_t TYPE_MASK = (uint64_t(1) << uint8_t(TYPE::_ID_SHIFT_)) - 1;
155
156 static TYPE type_from(uint32_t value, ARCH arch);
157 static uint32_t to_value(TYPE type) {
158 return static_cast<uint64_t>(type) & TYPE_MASK;
159 }
160
161 Section(const std::string& name, TYPE type = TYPE::PROGBITS) :
162 LIEF::Section(name),
163 type_{type}
164 {}
165
166 Section() = default;
167 ~Section() override = default;
168
169 Section& operator=(Section other) {
170 swap(other);
171 return *this;
172 }
173 Section(const Section& other);
174 void swap(Section& other) noexcept;
175
176 TYPE type() const {
177 return type_;
178 }
179 span<const uint8_t> content() const override;
182 void content(const std::vector<uint8_t>& data) override;
185
186 void content(std::vector<uint8_t>&& data);
187 uint64_t flags() const {
190 return flags_;
191 }
192 bool has(FLAGS flag) const;
195 bool has(const Segment& segment) const;
198 std::vector<FLAGS> flags_list() const;
201
202 uint64_t size() const override {
203 return size_;
204 }
205
206 void size(uint64_t size) override;
207
208 void offset(uint64_t offset) override;
209
210 uint64_t offset() const override {
211 return offset_;
212 }
213 uint64_t file_offset() const {
216 return this->offset();
217 }
218 uint64_t original_size() const {
224 return original_size_;
225 }
226 uint64_t alignment() const {
229 return address_align_;
230 }
231 uint64_t information() const {
235 return info_;
236 }
237 uint64_t entry_size() const {
245 return entry_size_;
246 }
247 uint32_t link() const {
250 return link_;
251 }
252 Section& clear(uint8_t value = 0);
255 void add(FLAGS flag);
258 void remove(FLAGS flag);
261
262 void type(TYPE type) {
263 type_ = type;
264 }
265
266 void flags(uint64_t flags) {
267 flags_ = flags;
268 }
269
270 void clear_flags() {
271 flags_ = 0;
272 }
273
274 void file_offset(uint64_t offset) {
275 this->offset(offset);
276 }
277
278 void link(uint32_t link) {
279 link_ = link;
280 }
281
282 void information(uint32_t info) {
283 info_ = info;
284 }
285
286 void alignment(uint64_t alignment) {
287 address_align_ = alignment;
288 }
289
290 void entry_size(uint64_t entry_size) {
291 entry_size_ = entry_size;
292 }
293
294 it_segments segments() {
295 return segments_;
296 }
297
298 it_const_segments segments() const {
299 return segments_;
300 }
301
302 Section& as_frame() {
303 is_frame_ = true;
304 return *this;
305 }
306
307 bool is_frame() const {
308 return is_frame_;
309 }
310
311 void accept(Visitor& visitor) const override;
312
313 Section& operator+=(FLAGS c) {
314 add(c);
315 return *this;
316 }
317
318 Section& operator-=(FLAGS c) {
319 remove(c);
320 return *this;
321 }
322 std::unique_ptr<SpanStream> stream() const;
325
326 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Section& section);
327
328 private:
329 template<class T>
330 LIEF_LOCAL Section(const T& header, ARCH arch);
331
332 LIEF_LOCAL span<uint8_t> writable_content();
333 ARCH arch_ = ARCH::NONE;
334 TYPE type_ = TYPE::SHT_NULL_;
335 uint64_t flags_ = 0;
336 uint64_t original_size_ = 0;
337 uint32_t link_ = 0;
338 uint32_t info_ = 0;
339 uint64_t address_align_ = 0;
340 uint64_t entry_size_ = 0;
341 segments_t segments_;
342 bool is_frame_ = false;
343 DataHandler::Handler* datahandler_ = nullptr;
344 std::vector<uint8_t> content_c_;
345};
346
347LIEF_API const char* to_string(Section::TYPE e);
348LIEF_API const char* to_string(Section::FLAGS e);
349
350}
351}
352
353ENABLE_BITMASK_OPERATORS(LIEF::ELF::Section::FLAGS)
354
355#endif
Section.hpp
enums.hpp
LIEF::ELF::Binary
Class which represents an ELF binary.
Definition ELF/Binary.hpp:59
LIEF::ELF::Builder
Class which takes an ELF::Binary object and reconstructs a valid binary.
Definition ELF/Builder.hpp:48
LIEF::ELF::Parser
Class which parses and transforms an ELF file into a ELF::Binary object.
Definition ELF/Parser.hpp:45
LIEF::ELF::Section
Class wich represents an ELF Section.
Definition ELF/Section.hpp:48
LIEF::ELF::Section::size
void size(uint64_t size) override
Change the section size.
LIEF::ELF::Section::type_from
static TYPE type_from(uint32_t value, ARCH arch)
LIEF::ELF::Section::content
span< const uint8_t > content() const override
Section's content.
LIEF::ELF::Section::segments
it_segments segments()
Definition ELF/Section.hpp:294
LIEF::ELF::Section::original_size
uint64_t original_size() const
Original size of the section's data.
Definition ELF/Section.hpp:223
LIEF::ELF::Section::operator+=
Section & operator+=(FLAGS c)
Definition ELF/Section.hpp:313
LIEF::ELF::Section::clear_flags
void clear_flags()
Definition ELF/Section.hpp:270
LIEF::ELF::Section::flags
uint64_t flags() const
Section flags.
Definition ELF/Section.hpp:189
LIEF::ELF::Section::as_frame
Section & as_frame()
Definition ELF/Section.hpp:302
LIEF::ELF::Section::offset
void offset(uint64_t offset) override
LIEF::ELF::Section::operator<<
friend std::ostream & operator<<(std::ostream &os, const Section &section)
LIEF::ELF::Section::remove
void remove(FLAGS flag)
Remove the given ELF_SECTION_FLAGS.
LIEF::ELF::Section::flags_list
std::vector< FLAGS > flags_list() const
Return section flags as a std::set
LIEF::ELF::Section::is_frame
bool is_frame() const
Definition ELF/Section.hpp:307
LIEF::ELF::Section::file_offset
uint64_t file_offset() const
Definition ELF/Section.hpp:215
LIEF::ELF::Section::~Section
~Section() override=default
LIEF::ELF::Section::alignment
void alignment(uint64_t alignment)
Definition ELF/Section.hpp:286
LIEF::ELF::Section::information
void information(uint32_t info)
Definition ELF/Section.hpp:282
LIEF::ELF::Section::to_value
static uint32_t to_value(TYPE type)
Definition ELF/Section.hpp:157
LIEF::ELF::Section::stream
std::unique_ptr< SpanStream > stream() const
Return a stream over the content of this section.
LIEF::ELF::Section::Section
Section(const Section &other)
LIEF::ELF::Section::type
TYPE type() const
Definition ELF/Section.hpp:176
LIEF::ELF::Section::Section
Section()=default
LIEF::ELF::Section::TYPE
TYPE
Definition ELF/Section.hpp:62
LIEF::ELF::Section::operator-=
Section & operator-=(FLAGS c)
Definition ELF/Section.hpp:318
LIEF::ELF::Section::content
void content(std::vector< uint8_t > &&data)
LIEF::ELF::Section::link
uint32_t link() const
Index to another section.
Definition ELF/Section.hpp:249
LIEF::ELF::Section::size
uint64_t size() const override
section's size (size in the binary, not the virtual size)
Definition ELF/Section.hpp:202
LIEF::ELF::Section::content
void content(const std::vector< uint8_t > &data) override
Set section content.
LIEF::ELF::Section::clear
Section & clear(uint8_t value=0)
Clear the content of the section with the given value
LIEF::ELF::Section::accept
void accept(Visitor &visitor) const override
LIEF::ELF::Section::swap
void swap(Section &other) noexcept
LIEF::ELF::Section::has
bool has(const Segment &segment) const
True if the section is wrapped by the given Segment
LIEF::ELF::Section::entry_size
void entry_size(uint64_t entry_size)
Definition ELF/Section.hpp:290
LIEF::ELF::Section::Section
Section(const std::string &name, TYPE type=TYPE::PROGBITS)
Definition ELF/Section.hpp:161
LIEF::ELF::Section::FLAGS
FLAGS
Definition ELF/Section.hpp:114
LIEF::ELF::Section::type
void type(TYPE type)
Definition ELF/Section.hpp:262
LIEF::ELF::Section::information
uint64_t information() const
Section information. The meaning of this value depends on the section's type.
Definition ELF/Section.hpp:234
LIEF::ELF::Section::offset
uint64_t offset() const override
Offset in the binary.
Definition ELF/Section.hpp:210
LIEF::ELF::Section::link
void link(uint32_t link)
Definition ELF/Section.hpp:278
LIEF::ELF::Section::add
void add(FLAGS flag)
Add the given ELF_SECTION_FLAGS.
LIEF::ELF::Section::operator=
Section & operator=(Section other)
Definition ELF/Section.hpp:169
LIEF::ELF::Section::alignment
uint64_t alignment() const
Section file alignment.
Definition ELF/Section.hpp:228
LIEF::ELF::Section::segments
it_const_segments segments() const
Definition ELF/Section.hpp:298
LIEF::ELF::Section::has
bool has(FLAGS flag) const
True if the section has the given flag
LIEF::ELF::Section::entry_size
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:244
LIEF::ELF::Section::file_offset
void file_offset(uint64_t offset)
Definition ELF/Section.hpp:274
LIEF::ELF::Section::flags
void flags(uint64_t flags)
Definition ELF/Section.hpp:266
LIEF::ELF::Segment
Class which represents the ELF segments.
Definition Segment.hpp:46
LIEF::SpanStream
Definition SpanStream.hpp:32
ENABLE_BITMASK_OPERATORS
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
iterators.hpp
LIEF::ELF::DataHandler
Definition ELF/Binary.hpp:38
LIEF::ELF
Namespace related to the LIEF's ELF module.
Definition Abstract/Header.hpp:28
LIEF::ELF::to_string
const char * to_string(DynamicEntry::TAG e)
LIEF::ELF::ARCH
ARCH
Definition ELF/enums.hpp:30
LIEF::ELF::ARCH::NONE
@ NONE
Definition ELF/enums.hpp:31
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
utils.hpp
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42