LIEF: Library to Instrument Executable Formats Version 1.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 <cstdint>
19#include <string>
20#include <vector>
21#include <ostream>
22#include <memory>
23
24#include "LIEF/visibility.h"
25
27#include "LIEF/enums.hpp"
28
29#include "LIEF/iterators.hpp"
30
31namespace LIEF {
32class SpanStream;
33
34namespace MachO {
35
36class BinaryParser;
37class SegmentCommand;
38class Binary;
39class Relocation;
40
41namespace details {
42struct section_32;
43struct section_64;
44}
45
48
49 friend class BinaryParser;
50 friend class Binary;
51 friend class SegmentCommand;
52
53 public:
54 using content_t = std::vector<uint8_t>;
55
57 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
58
61
65
66 static constexpr auto FLAGS_MASK = uint32_t(0xffffff00u);
67 static constexpr auto TYPE_MASK = uint32_t(0xff);
68
69 enum class TYPE : uint64_t {
71 REGULAR = 0x00u,
73 ZEROFILL = 0x01u,
75 CSTRING_LITERALS = 0x02u,
77 IS_4BYTE_LITERALS = 0x03u,
79 IS_8BYTE_LITERALS = 0x04u,
81 LITERAL_POINTERS = 0x05u,
83 NON_LAZY_SYMBOL_POINTERS = 0x06u,
85 LAZY_SYMBOL_POINTERS = 0x07u,
87 SYMBOL_STUBS = 0x08u,
89 MOD_INIT_FUNC_POINTERS = 0x09u,
91 MOD_TERM_FUNC_POINTERS = 0x0au,
93 COALESCED = 0x0bu,
95 GB_ZEROFILL = 0x0cu,
97 INTERPOSING = 0x0du,
99 IS_16BYTE_LITERALS = 0x0eu,
101 DTRACE_DOF = 0x0fu,
103 LAZY_DYLIB_SYMBOL_POINTERS = 0x10u,
105 THREAD_LOCAL_REGULAR = 0x11u,
107 THREAD_LOCAL_ZEROFILL = 0x12u,
109 THREAD_LOCAL_VARIABLES = 0x13u,
111 THREAD_LOCAL_VARIABLE_POINTERS = 0x14u,
113 THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15u,
115 INIT_FUNC_OFFSETS = 0x16u,
116 };
117
118 enum class FLAGS : uint64_t {
120 PURE_INSTRUCTIONS = 0x80000000u,
123 NO_TOC = 0x40000000u,
126 STRIP_STATIC_SYMS = 0x20000000u,
128 NO_DEAD_STRIP = 0x10000000u,
130 LIVE_SUPPORT = 0x08000000u,
132 SELF_MODIFYING_CODE = 0x04000000u,
134 DEBUG_INFO = 0x02000000u,
136 SOME_INSTRUCTIONS = 0x00000400u,
138 EXT_RELOC = 0x00000200u,
140 LOC_RELOC = 0x00000100u,
141 };
142
143 public:
144 static std::unique_ptr<Section> create(const details::section_32& sec);
145 static std::unique_ptr<Section> create(const details::section_64& sec);
146
147 static std::unique_ptr<Section> create(std::string name,
148 const content_t& content,
150
151
152 static std::unique_ptr<Section> create(std::string name,
154 return create(std::move(name), /*content=*/{}, type);
155 }
156
157 Section(const details::section_32& sec);
158 Section(const details::section_64& sec);
159
162
163 ~Section() override;
164
165 virtual std::unique_ptr<Section> clone() const {
166 return std::unique_ptr<Section>(new Section(*this));
167 }
168
169 span<const uint8_t> content() const override;
170
172 return as_writable(static_cast<const Section*>(this)->content());
173 }
174
176 void content(const content_t& data) override;
177
179 const std::string& segment_name() const;
180
182 uint64_t address() const {
183 return virtual_address();
184 }
185
187 uint32_t alignment() const {
188 return align_;
189 }
190
199 uint32_t relocation_offset() const {
200 return relocations_offset_;
201 }
202
204 uint32_t numberof_relocations() const {
205 return nbof_relocations_;
206 }
207
211 FLAGS flags() const {
212 return FLAGS(flags_ & FLAGS_MASK);
213 }
214
217 TYPE type() const {
218 return TYPE(flags_ & TYPE_MASK);
219 }
220
223 uint32_t reserved1() const {
224 return reserved1_;
225 }
226
229 uint32_t reserved2() const {
230 return reserved2_;
231 }
232
235 uint32_t reserved3() const {
236 return reserved3_;
237 }
238
241 std::vector<FLAGS> flags_list() const;
242
244 uint32_t raw_flags() const {
245 return flags_;
246 }
247
249 bool has_segment() const {
250 return segment() != nullptr;
251 }
252
256 return segment_;
257 }
258 const SegmentCommand* segment() const {
259 return segment_;
260 }
261
263 std::unique_ptr<SpanStream> stream() const;
264
267 void clear(uint8_t v) {
268 span<uint8_t> write_content = content();
269 std::fill(write_content.begin(), write_content.end(), v);
270 }
271
277 return relocations_;
278 }
280 return relocations_;
281 }
282
283 void segment_name(const std::string& name);
284 void address(uint64_t address) {
286 }
287 void alignment(uint32_t align) {
288 align_ = align;
289 }
290 void relocation_offset(uint32_t offset) {
291 relocations_offset_ = offset;
292 }
293 void numberof_relocations(uint32_t nb_reloc) {
294 nbof_relocations_ = nb_reloc;
295 }
296 void flags(uint32_t flags) {
297 flags_ = flags_ | flags;
298 }
299 void flags(std::vector<FLAGS> flags);
300 void type(TYPE type) {
301 flags_ = (flags_ & FLAGS_MASK) | uint8_t(type);
302 }
303 void reserved1(uint32_t reserved1) {
304 reserved1_ = reserved1;
305 }
306 void reserved2(uint32_t reserved2) {
307 reserved2_ = reserved2;
308 }
309 void reserved3(uint32_t reserved3) {
310 reserved3_ = reserved3;
311 }
312
314 bool has(FLAGS flag) const;
315
317 void add(FLAGS flag);
318
320 void remove(FLAGS flag);
321
323 add(flag);
324 return *this;
325 }
327 remove(flag);
328 return *this;
329 }
330
331 template<class T>
332 const T* cast() const {
333 static_assert(std::is_base_of<Section, T>::value,
334 "Require Section inheritance");
335 if (T::classof(this)) {
336 return static_cast<const T*>(this);
337 }
338 return nullptr;
339 }
340
341 template<class T>
342 T* cast() {
343 return const_cast<T*>(static_cast<const Section*>(this)->cast<T>());
344 }
345
346 void accept(Visitor& visitor) const override;
347
348 LIEF_API friend std::ostream& operator<<(std::ostream& os,
349 const Section& section);
350
351 protected:
352 Section();
353
354 Section& operator=(const Section& copy);
355 Section(const Section& copy);
356
357 std::string segment_name_;
358 uint64_t original_size_ = 0;
359 uint32_t align_ = 0;
360 uint32_t relocations_offset_ = 0;
361 uint32_t nbof_relocations_ = 0;
362 uint32_t flags_ = 0;
363 uint32_t reserved1_ = 0;
364 uint32_t reserved2_ = 0;
365 uint32_t reserved3_ = 0;
366 content_t content_;
367 SegmentCommand* segment_ = nullptr;
368 relocations_t relocations_;
369};
370
373
374}
375}
376
378
379#endif
Class used to parse a single binary (i.e. non-FAT).
Definition BinaryParser.hpp:78
Class which represents a MachO binary.
Definition MachO/Binary.hpp:88
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:290
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:223
void address(uint64_t address)
Definition MachO/Section.hpp:284
ref_iterator< relocations_t &, Relocation * > it_relocations
Iterator which outputs Relocation&.
Definition MachO/Section.hpp:60
std::vector< FLAGS > flags_list() const
Return the Section::flags as a list of Section::FLAGS.
Section & operator+=(FLAGS flag)
Definition MachO/Section.hpp:322
const T * cast() const
Definition MachO/Section.hpp:332
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:287
it_const_relocations relocations() const
Definition MachO/Section.hpp:279
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:63
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing Mach-O Relocation.
Definition MachO/Section.hpp:57
uint32_t raw_flags() const
Section flags without applying the SECTION_FLAGS_MASK mask.
Definition MachO/Section.hpp:244
T * cast()
Definition MachO/Section.hpp:342
SegmentCommand * segment()
The segment associated with this section or a nullptr if not present.
Definition MachO/Section.hpp:255
void type(TYPE type)
Definition MachO/Section.hpp:300
friend class BinaryParser
Definition MachO/Section.hpp:49
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:152
std::vector< uint8_t > content_t
Definition MachO/Section.hpp:54
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:199
const std::string & segment_name() const
Return the name of the segment linked to this section.
friend class SegmentCommand
Definition MachO/Section.hpp:51
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:267
uint64_t address() const
Virtual base address of the section.
Definition MachO/Section.hpp:182
bool has_segment() const
Check if this section is correctly linked with a MachO::SegmentCommand.
Definition MachO/Section.hpp:249
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:235
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:50
void content(const content_t &data) override
Update the content of the section.
FLAGS
Definition MachO/Section.hpp:118
Section & operator=(Section &&)
static std::unique_ptr< Section > create(const details::section_32 &sec)
void reserved3(uint32_t reserved3)
Definition MachO/Section.hpp:309
void reserved1(uint32_t reserved1)
Definition MachO/Section.hpp:303
uint32_t alignment() const
Section alignment as a power of 2.
Definition MachO/Section.hpp:187
it_relocations relocations()
Return an iterator over the MachO::Relocation associated with this section.
Definition MachO/Section.hpp:276
TYPE type() const
Type of the section. This value can help to determine the purpose of the section (e....
Definition MachO/Section.hpp:217
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:204
virtual std::unique_ptr< Section > clone() const
Definition MachO/Section.hpp:165
TYPE
Definition MachO/Section.hpp:69
@ REGULAR
Regular section.
Definition MachO/Section.hpp:71
void numberof_relocations(uint32_t nb_reloc)
Definition MachO/Section.hpp:293
static constexpr auto TYPE_MASK
Definition MachO/Section.hpp:67
span< uint8_t > content()
Definition MachO/Section.hpp:171
static constexpr auto FLAGS_MASK
Definition MachO/Section.hpp:66
Section & operator-=(FLAGS flag)
Definition MachO/Section.hpp:326
const SegmentCommand * segment() const
Definition MachO/Section.hpp:258
void flags(uint32_t flags)
Definition MachO/Section.hpp:296
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:229
void reserved2(uint32_t reserved2)
Definition MachO/Section.hpp:306
Section(const details::section_64 &sec)
FLAGS flags() const
Section's flags masked with SECTION_FLAGS_MASK (see: Section::FLAGS).
Definition MachO/Section.hpp:211
void segment_name(const std::string &name)
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:51
Class which represents an abstracted section.
Definition Abstract/Section.hpp:29
virtual std::string name() const
section's name
Definition Abstract/Section.hpp:43
virtual uint64_t offset() const
Offset in the binary.
Definition Abstract/Section.hpp:69
virtual uint64_t virtual_address() const
Address where the section should be mapped.
Definition Abstract/Section.hpp:74
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 endianness_support.hpp:60
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:40
uint64_t align(uint64_t value, uint64_t align_on)
Definition utils.hpp:28
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 return const ref on container's values.
Definition iterators.hpp:286
#define LIEF_API
Definition visibility.h:43