LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
MachO/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_MACHO_SECTION_H
17#define LIEF_MACHO_SECTION_H
18#include <string>
19#include <vector>
20#include <ostream>
21#include <memory>
22
23#include "LIEF/visibility.h"
24
26#include "LIEF/enums.hpp"
27
28#include "LIEF/iterators.hpp"
29
30namespace LIEF {
31class SpanStream;
32
33namespace MachO {
34
35class BinaryParser;
36class SegmentCommand;
37class Binary;
38class Relocation;
39
40namespace details {
41struct section_32;
42struct section_64;
43}
44class LIEF_API Section : public LIEF::Section {
47
48 friend class BinaryParser;
49 friend class Binary;
50 friend class SegmentCommand;
51
52 public:
53 using content_t = std::vector<uint8_t>;
54 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
57 using it_relocations = ref_iterator<relocations_t&, Relocation*>;
60 using it_const_relocations = const_ref_iterator<const relocations_t&, const Relocation*>;
63
64 static constexpr auto FLAGS_MASK = uint32_t(0xffffff00u);
65 static constexpr auto TYPE_MASK = uint32_t(0xff);
66
67 enum class TYPE: uint64_t {
68 REGULAR = 0x00u,
69 ZEROFILL = 0x01u,
70 CSTRING_LITERALS = 0x02u,
71 S_4BYTE_LITERALS = 0x03u,
72 S_8BYTE_LITERALS = 0x04u,
73 LITERAL_POINTERS = 0x05u,
74 NON_LAZY_SYMBOL_POINTERS = 0x06u,
75 LAZY_SYMBOL_POINTERS = 0x07u,
76 SYMBOL_STUBS = 0x08u,
77 MOD_INIT_FUNC_POINTERS = 0x09u,
78 MOD_TERM_FUNC_POINTERS = 0x0au,
79 COALESCED = 0x0bu,
80 GB_ZEROFILL = 0x0cu,
81 INTERPOSING = 0x0du,
82 S_16BYTE_LITERALS = 0x0eu,
83 DTRACE_DOF = 0x0fu,
84 LAZY_DYLIB_SYMBOL_POINTERS = 0x10u,
85 THREAD_LOCAL_REGULAR = 0x11u,
86 THREAD_LOCAL_ZEROFILL = 0x12u,
87 THREAD_LOCAL_VARIABLES = 0x13u,
88 THREAD_LOCAL_VARIABLE_POINTERS = 0x14u,
89 THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15u,
90 INIT_FUNC_OFFSETS = 0x16u,
91 };
92
93 enum class FLAGS: uint64_t {
94 PURE_INSTRUCTIONS = 0x80000000u,
95 NO_TOC = 0x40000000u,
96 STRIP_STATIC_SYMS = 0x20000000u,
97 NO_DEAD_STRIP = 0x10000000u,
98 LIVE_SUPPORT = 0x08000000u,
99 SELF_MODIFYING_CODE = 0x04000000u,
100 DEBUG_INFO = 0x02000000u,
101
102 SOME_INSTRUCTIONS = 0x00000400u,
103 EXT_RELOC = 0x00000200u,
104 LOC_RELOC = 0x00000100u,
105 };
106
107 public:
109 Section(const details::section_32& section_cmd);
110 Section(const details::section_64& section_cmd);
111
112 Section(std::string name);
113 Section(std::string name, content_t content);
114
115 Section& operator=(Section copy);
116 Section(const Section& copy);
117
118 void swap(Section& other) noexcept;
119
120 ~Section() override;
121
122 span<const uint8_t> content() const override;
123 void content(const content_t& data) override;
126 const std::string& segment_name() const;
129 uint64_t address() const {
132 return virtual_address();
133 }
134 uint32_t alignment() const {
137 return align_;
138 }
139 uint32_t relocation_offset() const {
148 return relocations_offset_;
149 }
150 uint32_t numberof_relocations() const {
153 return nbof_relocations_;
154 }
155 FLAGS flags() const {
160 return FLAGS(flags_ & FLAGS_MASK);
161 }
162 TYPE type() const {
166 return TYPE(flags_ & TYPE_MASK);
167 }
168 uint32_t reserved1() const {
172 return reserved1_;
173 }
174 uint32_t reserved2() const {
178 return reserved2_;
179 }
180 uint32_t reserved3() const {
184 return reserved3_;
185 }
186 std::vector<FLAGS> flags_list() const;
190 uint32_t raw_flags() const {
193 return flags_;
194 }
195 bool has_segment() const {
198 return segment() != nullptr;
199 }
200 SegmentCommand* segment() {
204 return segment_;
205 }
206 const SegmentCommand* segment() const {
207 return segment_;
208 }
209 std::unique_ptr<SpanStream> stream() const;
212 void clear(uint8_t v);
216 it_relocations relocations() {
222 return relocations_;
223 }
224 it_const_relocations relocations() const {
225 return relocations_;
226 }
227
228 void segment_name(const std::string& name);
229 void address(uint64_t address) {
230 virtual_address(address);
231 }
232 void alignment(uint32_t align) {
233 align_ = align;
234 }
235 void relocation_offset(uint32_t offset) {
236 relocations_offset_ = offset;
237 }
238 void numberof_relocations(uint32_t nb_reloc) {
239 nbof_relocations_ = nb_reloc;
240 }
241 void flags(uint32_t flags) {
242 flags_ = flags_ | flags;
243 }
244 void flags(std::vector<FLAGS> flags);
245 void type(TYPE type) {
246 flags_ = (flags_ & FLAGS_MASK) | uint8_t(type);
247 }
248 void reserved1(uint32_t reserved1) {
249 reserved1_ = reserved1;
250 }
251 void reserved2(uint32_t reserved2) {
252 reserved2_ = reserved2;
253 }
254 void reserved3(uint32_t reserved3) {
255 reserved3_ = reserved3;
256 }
257 bool has(FLAGS flag) const;
260 void add(FLAGS flag);
263 void remove(FLAGS flag);
266
267 Section& operator+=(FLAGS flag) {
268 add(flag);
269 return *this;
270 }
271 Section& operator-=(FLAGS flag) {
272 remove(flag);
273 return *this;
274 }
275
276 void accept(Visitor& visitor) const override;
277
278 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Section& section);
279
280 private:
281 std::string segment_name_;
282 uint64_t original_size_ = 0;
283 uint32_t align_ = 0;
284 uint32_t relocations_offset_ = 0;
285 uint32_t nbof_relocations_ = 0;
286 uint32_t flags_ = 0;
287 uint32_t reserved1_ = 0;
288 uint32_t reserved2_ = 0;
289 uint32_t reserved3_ = 0;
290 content_t content_;
291 SegmentCommand *segment_ = nullptr;
292 relocations_t relocations_;
293};
294
295LIEF_API const char* to_string(Section::TYPE type);
296LIEF_API const char* to_string(Section::FLAGS flag);
297
298}
299}
300
301ENABLE_BITMASK_OPERATORS(LIEF::MachO::Section::FLAGS)
302#endif
Section.hpp
LIEF::MachO::BinaryParser
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:74
LIEF::MachO::Binary
Class which represents a MachO binary.
Definition MachO/Binary.hpp:85
LIEF::MachO::Relocation
Class that represents a Mach-O relocation.
Definition MachO/Relocation.hpp:40
LIEF::MachO::Section
Class that represents a Mach-O section.
Definition MachO/Section.hpp:46
LIEF::MachO::Section::content
span< const uint8_t > content() const override
section's content
LIEF::MachO::Section::relocation_offset
void relocation_offset(uint32_t offset)
Definition MachO/Section.hpp:235
LIEF::MachO::Section::Section
Section(const Section &copy)
LIEF::MachO::Section::reserved1
uint32_t reserved1() const
According to the official loader.h file, this value is reserved for offset or index
Definition MachO/Section.hpp:171
LIEF::MachO::Section::Section
Section(std::string name, content_t content)
LIEF::MachO::Section::address
void address(uint64_t address)
Definition MachO/Section.hpp:229
LIEF::MachO::Section::flags_list
std::vector< FLAGS > flags_list() const
Return the Section::flags as a list of Section::FLAGS.
LIEF::MachO::Section::operator+=
Section & operator+=(FLAGS flag)
Definition MachO/Section.hpp:267
LIEF::MachO::Section::add
void add(FLAGS flag)
Append a Section::FLAGS to the current section.
LIEF::MachO::Section::alignment
void alignment(uint32_t align)
Definition MachO/Section.hpp:232
LIEF::MachO::Section::relocations
it_const_relocations relocations() const
Definition MachO/Section.hpp:224
LIEF::MachO::Section::raw_flags
uint32_t raw_flags() const
Section flags without applying the SECTION_FLAGS_MASK mask.
Definition MachO/Section.hpp:192
LIEF::MachO::Section::segment
SegmentCommand * segment()
The segment associated with this section or a nullptr if not present.
Definition MachO/Section.hpp:203
LIEF::MachO::Section::type
void type(TYPE type)
Definition MachO/Section.hpp:245
LIEF::MachO::Section::swap
void swap(Section &other) noexcept
LIEF::MachO::Section::operator<<
friend std::ostream & operator<<(std::ostream &os, const Section &section)
LIEF::MachO::Section::flags
void flags(std::vector< FLAGS > flags)
LIEF::MachO::Section::Section
Section()
LIEF::MachO::Section::relocation_offset
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:147
LIEF::MachO::Section::segment_name
const std::string & segment_name() const
Return the name of the segment linked to this section.
LIEF::MachO::Section::clear
void clear(uint8_t v)
Clear the content of this section by filling its values with the byte provided in parameter.
LIEF::MachO::Section::Section
Section(const details::section_32 &section_cmd)
LIEF::MachO::Section::address
uint64_t address() const
Virtual base address of the section.
Definition MachO/Section.hpp:131
LIEF::MachO::Section::has_segment
bool has_segment() const
Check if this section is correctly linked with a MachO::SegmentCommand.
Definition MachO/Section.hpp:197
LIEF::MachO::Section::reserved3
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:183
LIEF::MachO::Section::stream
std::unique_ptr< SpanStream > stream() const
Return a stream over the content of this section.
LIEF::MachO::Section::remove
void remove(FLAGS flag)
Remove a Section::FLAGS to the current section.
LIEF::MachO::Section::Section
Section(const details::section_64 &section_cmd)
LIEF::MachO::Section::content
void content(const content_t &data) override
Update the content of the section.
LIEF::MachO::Section::FLAGS
FLAGS
Definition MachO/Section.hpp:93
LIEF::MachO::Section::reserved3
void reserved3(uint32_t reserved3)
Definition MachO/Section.hpp:254
LIEF::MachO::Section::reserved1
void reserved1(uint32_t reserved1)
Definition MachO/Section.hpp:248
LIEF::MachO::Section::alignment
uint32_t alignment() const
Section alignment as a power of 2.
Definition MachO/Section.hpp:136
LIEF::MachO::Section::relocations
it_relocations relocations()
Return an iterator over the MachO::Relocation associated with this section.
Definition MachO/Section.hpp:221
LIEF::MachO::Section::type
TYPE type() const
Type of the section. This value can help to determine the purpose of the section (e....
Definition MachO/Section.hpp:165
LIEF::MachO::Section::~Section
~Section() override
LIEF::MachO::Section::has
bool has(FLAGS flag) const
Check if the section has the given Section::FLAGS flag.
LIEF::MachO::Section::numberof_relocations
uint32_t numberof_relocations() const
Number of relocations associated with this section.
Definition MachO/Section.hpp:152
LIEF::MachO::Section::TYPE
TYPE
Definition MachO/Section.hpp:67
LIEF::MachO::Section::numberof_relocations
void numberof_relocations(uint32_t nb_reloc)
Definition MachO/Section.hpp:238
LIEF::MachO::Section::operator-=
Section & operator-=(FLAGS flag)
Definition MachO/Section.hpp:271
LIEF::MachO::Section::segment
const SegmentCommand * segment() const
Definition MachO/Section.hpp:206
LIEF::MachO::Section::operator=
Section & operator=(Section copy)
LIEF::MachO::Section::flags
void flags(uint32_t flags)
Definition MachO/Section.hpp:241
LIEF::MachO::Section::Section
Section(std::string name)
LIEF::MachO::Section::accept
void accept(Visitor &visitor) const override
LIEF::MachO::Section::reserved2
uint32_t reserved2() const
According to the official loader.h file, this value is reserved for count or sizeof
Definition MachO/Section.hpp:177
LIEF::MachO::Section::reserved2
void reserved2(uint32_t reserved2)
Definition MachO/Section.hpp:251
LIEF::MachO::Section::flags
FLAGS flags() const
Section's flags masked with SECTION_FLAGS_MASK (see: Section::FLAGS)
Definition MachO/Section.hpp:159
LIEF::MachO::Section::segment_name
void segment_name(const std::string &name)
LIEF::MachO::SegmentCommand
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:50
LIEF::SpanStream
Definition SpanStream.hpp:32
enums.hpp
ENABLE_BITMASK_OPERATORS
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
iterators.hpp
LIEF::MachO::details
Definition endianness_support.hpp:59
LIEF::MachO
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
LIEF::MachO::to_string
const char * to_string(BuildToolVersion::TOOLS tool)
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
LIEF::align
uint64_t align(uint64_t value, uint64_t align_on)
Definition utils.hpp:25
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41