LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
MachO/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_MACHO_SECTION_H
17#define LIEF_MACHO_SECTION_H
18#include <string_view>
19#include <cstdint>
20#include <memory>
21#include <ostream>
22#include <string>
23#include <vector>
24
26#include "LIEF/visibility.h"
27
29#include "LIEF/enums.hpp"
30
31#include "LIEF/iterators.hpp"
32
33namespace LIEF {
34class SpanStream;
35
36namespace MachO {
37
38class BinaryParser;
39class SegmentCommand;
40class Binary;
41class Relocation;
42
43namespace details {
44struct section_32;
45struct section_64;
46}
47
50
51 friend class BinaryParser;
52 friend class Binary;
53 friend class SegmentCommand;
54
55 public:
56 using content_t = std::vector<uint8_t>;
57
59 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
60
63
67
68 static constexpr auto FLAGS_MASK = uint32_t(0xffffff00u);
69 static constexpr auto TYPE_MASK = uint32_t(0xff);
70
71 enum class TYPE : uint64_t {
73 REGULAR = 0x00u,
75 ZEROFILL = 0x01u,
77 CSTRING_LITERALS = 0x02u,
79 IS_4BYTE_LITERALS = 0x03u,
81 IS_8BYTE_LITERALS = 0x04u,
83 LITERAL_POINTERS = 0x05u,
85 NON_LAZY_SYMBOL_POINTERS = 0x06u,
87 LAZY_SYMBOL_POINTERS = 0x07u,
89 SYMBOL_STUBS = 0x08u,
91 MOD_INIT_FUNC_POINTERS = 0x09u,
93 MOD_TERM_FUNC_POINTERS = 0x0au,
95 COALESCED = 0x0bu,
97 GB_ZEROFILL = 0x0cu,
99 INTERPOSING = 0x0du,
101 IS_16BYTE_LITERALS = 0x0eu,
103 DTRACE_DOF = 0x0fu,
105 LAZY_DYLIB_SYMBOL_POINTERS = 0x10u,
107 THREAD_LOCAL_REGULAR = 0x11u,
109 THREAD_LOCAL_ZEROFILL = 0x12u,
111 THREAD_LOCAL_VARIABLES = 0x13u,
113 THREAD_LOCAL_VARIABLE_POINTERS = 0x14u,
115 THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15u,
117 INIT_FUNC_OFFSETS = 0x16u,
118 };
119
120 enum class FLAGS : uint64_t {
122 PURE_INSTRUCTIONS = 0x80000000u,
125 NO_TOC = 0x40000000u,
128 STRIP_STATIC_SYMS = 0x20000000u,
130 NO_DEAD_STRIP = 0x10000000u,
132 LIVE_SUPPORT = 0x08000000u,
134 SELF_MODIFYING_CODE = 0x04000000u,
136 DEBUG_INFO = 0x02000000u,
138 SOME_INSTRUCTIONS = 0x00000400u,
140 EXT_RELOC = 0x00000200u,
142 LOC_RELOC = 0x00000100u,
143 };
144
145 public:
146 static std::unique_ptr<Section> create(const details::section_32& sec);
147 static std::unique_ptr<Section> create(const details::section_64& sec);
148
149 static std::unique_ptr<Section> create(std::string name,
150 const content_t& content,
152
153
154 static std::unique_ptr<Section> create(std::string name,
156 return create(std::move(name), /*content=*/{}, type);
157 }
158
159 Section(const details::section_32& sec);
160 Section(const details::section_64& sec);
161
164
165 ~Section() override;
166
167 virtual std::unique_ptr<Section> clone() const {
168 return std::unique_ptr<Section>(new Section(*this));
169 }
170
172
174 return as_writable(static_cast<const Section*>(this)->content());
175 }
176
178 void content(const content_t& data) override;
179
181 std::string_view segment_name() const LIEF_LIFETIMEBOUND;
182
184 uint64_t address() const {
185 return virtual_address();
186 }
187
189 uint32_t alignment() const {
190 return align_;
191 }
192
201 uint32_t relocation_offset() const {
202 return relocations_offset_;
203 }
204
206 uint32_t numberof_relocations() const {
207 return nbof_relocations_;
208 }
209
213 FLAGS flags() const {
214 return FLAGS(flags_ & FLAGS_MASK);
215 }
216
219 TYPE type() const {
220 return TYPE(flags_ & TYPE_MASK);
221 }
222
225 uint32_t reserved1() const {
226 return reserved1_;
227 }
228
231 uint32_t reserved2() const {
232 return reserved2_;
233 }
234
237 uint32_t reserved3() const {
238 return reserved3_;
239 }
240
243 std::vector<FLAGS> flags_list() const;
244
246 uint32_t raw_flags() const {
247 return flags_;
248 }
249
251 bool has_segment() const {
252 return segment() != nullptr;
253 }
254
258 return segment_;
259 }
261 return segment_;
262 }
263
265 std::unique_ptr<SpanStream> stream() const;
266
269 void clear(uint8_t v) {
270 span<uint8_t> write_content = content();
271 std::fill(write_content.begin(), write_content.end(), v);
272 }
273
279 return relocations_;
280 }
282 return relocations_;
283 }
284
285 void segment_name(const std::string& name);
286 void address(uint64_t address) {
288 }
289 void alignment(uint32_t align) {
290 align_ = align;
291 }
292 void relocation_offset(uint32_t offset) {
293 relocations_offset_ = offset;
294 }
295 void numberof_relocations(uint32_t nb_reloc) {
296 nbof_relocations_ = nb_reloc;
297 }
298 void flags(uint32_t flags) {
299 flags_ = flags_ | flags;
300 }
301 void flags(std::vector<FLAGS> flags);
302 void type(TYPE type) {
303 flags_ = (flags_ & FLAGS_MASK) | uint8_t(type);
304 }
305 void reserved1(uint32_t reserved1) {
306 reserved1_ = reserved1;
307 }
308 void reserved2(uint32_t reserved2) {
309 reserved2_ = reserved2;
310 }
311 void reserved3(uint32_t reserved3) {
312 reserved3_ = reserved3;
313 }
314
316 bool has(FLAGS flag) const;
317
319 void add(FLAGS flag);
320
322 void remove(FLAGS flag);
323
325 add(flag);
326 return *this;
327 }
329 remove(flag);
330 return *this;
331 }
332
333 template<class T>
334 const T* cast() const {
335 static_assert(std::is_base_of_v<Section, T>, "Require Section inheritance");
336 if (T::classof(this)) {
337 return static_cast<const T*>(this);
338 }
339 return nullptr;
340 }
341
342 template<class T>
343 T* cast() {
344 return const_cast<T*>(static_cast<const Section*>(this)->cast<T>());
345 }
346
347 void accept(Visitor& visitor) const override;
348
349 LIEF_API friend std::ostream& operator<<(std::ostream& os,
350 const Section& section);
351
352 protected:
353 Section();
354
355 Section& operator=(const Section& copy);
356 Section(const Section& copy);
357
358 std::string segment_name_;
359 uint64_t original_size_ = 0;
360 uint32_t align_ = 0;
361 uint32_t relocations_offset_ = 0;
362 uint32_t nbof_relocations_ = 0;
363 uint32_t flags_ = 0;
364 uint32_t reserved1_ = 0;
365 uint32_t reserved2_ = 0;
366 uint32_t reserved3_ = 0;
367 content_t content_;
368 SegmentCommand* segment_ = nullptr;
369 relocations_t relocations_;
370};
371
374
375}
376}
377
379
380#endif
Class used to parse a single binary (i.e. non-FAT).
Definition BinaryParser.hpp:79
Class which represents a MachO binary.
Definition MachO/Binary.hpp:94
Class that represents a Mach-O relocation.
Definition MachO/Relocation.hpp:40
span< const uint8_t > content() const override
Section's content.
void relocation_offset(uint32_t offset)
Definition MachO/Section.hpp:292
Section(const details::section_32 &sec)
uint32_t reserved1() const
According to the official loader.h file, this value is reserved for offset or index.
Definition MachO/Section.hpp:225
void address(uint64_t address)
Definition MachO/Section.hpp:286
ref_iterator< relocations_t &, Relocation * > it_relocations
Iterator which outputs Relocation&.
Definition MachO/Section.hpp:62
std::vector< FLAGS > flags_list() const
Return the Section::flags as a list of Section::FLAGS.
Section & operator+=(FLAGS flag)
Definition MachO/Section.hpp:324
const T * cast() const
Definition MachO/Section.hpp:334
void add(FLAGS flag)
Append a Section::FLAGS to the current section.
static std::unique_ptr< Section > create(const details::section_64 &sec)
void alignment(uint32_t align)
Definition MachO/Section.hpp:289
it_const_relocations relocations() const
Definition MachO/Section.hpp:281
static std::unique_ptr< Section > create(std::string name, const content_t &content, TYPE type=TYPE::REGULAR)
const_ref_iterator< const relocations_t &, const Relocation * > it_const_relocations
Iterator which outputs const Relocation&.
Definition MachO/Section.hpp:65
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing Mach-O Relocation.
Definition MachO/Section.hpp:59
uint32_t raw_flags() const
Section flags without applying the SECTION_FLAGS_MASK mask.
Definition MachO/Section.hpp:246
T * cast()
Definition MachO/Section.hpp:343
SegmentCommand * segment()
The segment associated with this section or a nullptr if not present.
Definition MachO/Section.hpp:257
void type(TYPE type)
Definition MachO/Section.hpp:302
friend class BinaryParser
Definition MachO/Section.hpp:51
friend std::ostream & operator<<(std::ostream &os, const Section &section)
void flags(std::vector< FLAGS > flags)
static std::unique_ptr< Section > create(std::string name, TYPE type=TYPE::REGULAR)
Definition MachO/Section.hpp:154
std::vector< uint8_t > content_t
Definition MachO/Section.hpp:56
uint32_t relocation_offset() const
Offset of the relocation table. This value should be 0 for executable and libraries as the relocation...
Definition MachO/Section.hpp:201
friend class SegmentCommand
Definition MachO/Section.hpp:53
std::string_view segment_name() const
Return the name of the segment linked to this section.
void clear(uint8_t v)
Clear the content of this section by filling its values with the byte provided in parameter.
Definition MachO/Section.hpp:269
uint64_t address() const
Virtual base address of the section.
Definition MachO/Section.hpp:184
bool has_segment() const
Check if this section is correctly linked with a MachO::SegmentCommand.
Definition MachO/Section.hpp:251
uint32_t reserved3() const
This value is only present for 64 bits Mach-O files. In that case, the value is reserved.
Definition MachO/Section.hpp:237
std::unique_ptr< SpanStream > stream() const
Return a stream over the content of this section.
void remove(FLAGS flag)
Remove a Section::FLAGS to the current section.
friend class Binary
Definition MachO/Section.hpp:52
void content(const content_t &data) override
Update the content of the section.
FLAGS
Definition MachO/Section.hpp:120
Section & operator=(Section &&)
static std::unique_ptr< Section > create(const details::section_32 &sec)
void reserved3(uint32_t reserved3)
Definition MachO/Section.hpp:311
void reserved1(uint32_t reserved1)
Definition MachO/Section.hpp:305
uint32_t alignment() const
Section alignment as a power of 2.
Definition MachO/Section.hpp:189
it_relocations relocations()
Return an iterator over the MachO::Relocation associated with this section.
Definition MachO/Section.hpp:278
TYPE type() const
Type of the section. This value can help to determine the purpose of the section (e....
Definition MachO/Section.hpp:219
bool has(FLAGS flag) const
Check if the section has the given Section::FLAGS flag.
uint32_t numberof_relocations() const
Number of relocations associated with this section.
Definition MachO/Section.hpp:206
virtual std::unique_ptr< Section > clone() const
Definition MachO/Section.hpp:167
TYPE
Definition MachO/Section.hpp:71
@ REGULAR
Regular section.
Definition MachO/Section.hpp:73
void numberof_relocations(uint32_t nb_reloc)
Definition MachO/Section.hpp:295
static constexpr auto TYPE_MASK
Definition MachO/Section.hpp:69
static constexpr auto FLAGS_MASK
Definition MachO/Section.hpp:68
Section & operator-=(FLAGS flag)
Definition MachO/Section.hpp:328
const SegmentCommand * segment() const
Definition MachO/Section.hpp:260
void flags(uint32_t flags)
Definition MachO/Section.hpp:298
void accept(Visitor &visitor) const override
uint32_t reserved2() const
According to the official loader.h file, this value is reserved for count or sizeof.
Definition MachO/Section.hpp:231
void reserved2(uint32_t reserved2)
Definition MachO/Section.hpp:308
Section(const details::section_64 &sec)
FLAGS flags() const
Section's flags masked with SECTION_FLAGS_MASK (see: Section::FLAGS).
Definition MachO/Section.hpp:213
void segment_name(const std::string &name)
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:54
Class which represents an abstracted section.
Definition Abstract/Section.hpp:31
virtual uint64_t offset() const
Offset in the binary.
Definition Abstract/Section.hpp:71
virtual std::string_view name() const
Section's name.
Definition Abstract/Section.hpp:45
virtual uint64_t virtual_address() const
Address where the section should be mapped.
Definition Abstract/Section.hpp:76
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 endianness_support.hpp:59
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
const char * to_string(BuildToolVersion::TOOLS tool)
LIEF namespace.
Definition Abstract/Binary.hpp:41
uint64_t align(uint64_t value, uint64_t align_on)
Definition utils.hpp:29
span< uint8_t > as_writable(span< const uint8_t > buffer)
Definition span.hpp:24
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:316
#define LIEF_API
Definition visibility.h:45