LIEF: Library to Instrument Executable Formats Version 0.15.1
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 {
31namespace MachO {
32
33class BinaryParser;
34class SegmentCommand;
35class Binary;
36class Relocation;
37
38namespace details {
39struct section_32;
40struct section_64;
41}
42
45
46 friend class BinaryParser;
47 friend class Binary;
48 friend class SegmentCommand;
49
50 public:
51 using content_t = std::vector<uint8_t>;
52
54 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
55
58
61
62 static constexpr auto FLAGS_MASK = uint32_t(0xffffff00u);
63 static constexpr auto TYPE_MASK = uint32_t(0xff);
64
65 enum class TYPE: uint64_t {
66 REGULAR = 0x00u,
67 ZEROFILL = 0x01u,
68 CSTRING_LITERALS = 0x02u,
69 S_4BYTE_LITERALS = 0x03u,
70 S_8BYTE_LITERALS = 0x04u,
71 LITERAL_POINTERS = 0x05u,
72 NON_LAZY_SYMBOL_POINTERS = 0x06u,
73 LAZY_SYMBOL_POINTERS = 0x07u,
74 SYMBOL_STUBS = 0x08u,
75 MOD_INIT_FUNC_POINTERS = 0x09u,
76 MOD_TERM_FUNC_POINTERS = 0x0au,
77 COALESCED = 0x0bu,
78 GB_ZEROFILL = 0x0cu,
79 INTERPOSING = 0x0du,
80 S_16BYTE_LITERALS = 0x0eu,
81 DTRACE_DOF = 0x0fu,
82 LAZY_DYLIB_SYMBOL_POINTERS = 0x10u,
83 THREAD_LOCAL_REGULAR = 0x11u,
84 THREAD_LOCAL_ZEROFILL = 0x12u,
85 THREAD_LOCAL_VARIABLES = 0x13u,
86 THREAD_LOCAL_VARIABLE_POINTERS = 0x14u,
87 THREAD_LOCAL_INIT_FUNCTION_POINTERS = 0x15u,
88 INIT_FUNC_OFFSETS = 0x16u,
89 };
90
91 enum class FLAGS: uint64_t {
92 PURE_INSTRUCTIONS = 0x80000000u,
93 NO_TOC = 0x40000000u,
94 STRIP_STATIC_SYMS = 0x20000000u,
95 NO_DEAD_STRIP = 0x10000000u,
96 LIVE_SUPPORT = 0x08000000u,
97 SELF_MODIFYING_CODE = 0x04000000u,
98 DEBUG_INFO = 0x02000000u,
99
100 SOME_INSTRUCTIONS = 0x00000400u,
101 EXT_RELOC = 0x00000200u,
102 LOC_RELOC = 0x00000100u,
103 };
104
105 public:
107 Section(const details::section_32& section_cmd);
108 Section(const details::section_64& section_cmd);
109
110 Section(std::string name);
111 Section(std::string name, content_t content);
112
114 Section(const Section& copy);
115
116 void swap(Section& other) noexcept;
117
118 ~Section() override;
119
120 span<const uint8_t> content() const override;
121
123 void content(const content_t& data) override;
124
126 const std::string& segment_name() const;
127
129 uint64_t address() const {
130 return virtual_address();
131 }
132
134 uint32_t alignment() const {
135 return align_;
136 }
137
145 uint32_t relocation_offset() const {
146 return relocations_offset_;
147 }
148
150 uint32_t numberof_relocations() const {
151 return nbof_relocations_;
152 }
153
157 FLAGS flags() const {
158 return FLAGS(flags_ & FLAGS_MASK);
159 }
160
163 TYPE type() const {
164 return TYPE(flags_ & TYPE_MASK);
165 }
166
169 uint32_t reserved1() const {
170 return reserved1_;
171 }
172
175 uint32_t reserved2() const {
176 return reserved2_;
177 }
178
181 uint32_t reserved3() const {
182 return reserved3_;
183 }
184
187 std::vector<FLAGS> flags_list() const;
188
190 uint32_t raw_flags() const {
191 return flags_;
192 }
193
195 bool has_segment() const {
196 return segment() != nullptr;
197 }
198
202 return segment_;
203 }
204 const SegmentCommand* segment() const {
205 return segment_;
206 }
207
210 void clear(uint8_t v);
211
217 return relocations_;
218 }
220 return relocations_;
221 }
222
223 void segment_name(const std::string& name);
224 void address(uint64_t address) {
225 virtual_address(address);
226 }
227 void alignment(uint32_t align) {
228 align_ = align;
229 }
230 void relocation_offset(uint32_t offset) {
231 relocations_offset_ = offset;
232 }
233 void numberof_relocations(uint32_t nb_reloc) {
234 nbof_relocations_ = nb_reloc;
235 }
236 void flags(uint32_t flags) {
237 flags_ = flags_ | flags;
238 }
239 void flags(std::vector<FLAGS> flags);
240 void type(TYPE type) {
241 flags_ = (flags_ & FLAGS_MASK) | uint8_t(type);
242 }
243 void reserved1(uint32_t reserved1) {
244 reserved1_ = reserved1;
245 }
246 void reserved2(uint32_t reserved2) {
247 reserved2_ = reserved2;
248 }
249 void reserved3(uint32_t reserved3) {
250 reserved3_ = reserved3;
251 }
252
254 bool has(FLAGS flag) const;
255
257 void add(FLAGS flag);
258
260 void remove(FLAGS flag);
261
263 add(flag);
264 return *this;
265 }
267 remove(flag);
268 return *this;
269 }
270
271 void accept(Visitor& visitor) const override;
272
273 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Section& section);
274
275 private:
276 std::string segment_name_;
277 uint64_t original_size_ = 0;
278 uint32_t align_ = 0;
279 uint32_t relocations_offset_ = 0;
280 uint32_t nbof_relocations_ = 0;
281 uint32_t flags_ = 0;
282 uint32_t reserved1_ = 0;
283 uint32_t reserved2_ = 0;
284 uint32_t reserved3_ = 0;
285 content_t content_;
286 SegmentCommand *segment_ = nullptr;
287 relocations_t relocations_;
288};
289
292
293}
294}
295
297#endif
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:74
Class which represents a MachO binary.
Definition MachO/Binary.hpp:77
Class that represents a Mach-O section.
Definition MachO/Section.hpp:44
span< const uint8_t > content() const override
section's content
void relocation_offset(uint32_t offset)
Definition MachO/Section.hpp:230
Section(const Section &copy)
uint32_t reserved1() const
According to the official loader.h file, this value is reserved for offset or index
Definition MachO/Section.hpp:169
Section(std::string name, content_t content)
void address(uint64_t address)
Definition MachO/Section.hpp:224
std::vector< FLAGS > flags_list() const
Return the Section::flags as a list of Section::FLAGS.
Section & operator+=(FLAGS flag)
Definition MachO/Section.hpp:262
void add(FLAGS flag)
Append a Section::FLAGS to the current section.
void alignment(uint32_t align)
Definition MachO/Section.hpp:227
it_const_relocations relocations() const
Definition MachO/Section.hpp:219
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing Mach-O Relocation.
Definition MachO/Section.hpp:54
uint32_t raw_flags() const
Section flags without applying the SECTION_FLAGS_MASK mask.
Definition MachO/Section.hpp:190
SegmentCommand * segment()
The segment associated with this section or a nullptr if not present.
Definition MachO/Section.hpp:201
void type(TYPE type)
Definition MachO/Section.hpp:240
void swap(Section &other) noexcept
friend std::ostream & operator<<(std::ostream &os, const Section &section)
void flags(std::vector< FLAGS > flags)
std::vector< uint8_t > content_t
Definition MachO/Section.hpp:51
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:145
const std::string & 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.
Section(const details::section_32 &section_cmd)
uint64_t address() const
Virtual base address of the section.
Definition MachO/Section.hpp:129
bool has_segment() const
Check if this section is correctly linked with a MachO::SegmentCommand.
Definition MachO/Section.hpp:195
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:181
void remove(FLAGS flag)
Remove a Section::FLAGS to the current section.
Section(const details::section_64 &section_cmd)
void content(const content_t &data) override
Update the content of the section.
FLAGS
Definition MachO/Section.hpp:91
void reserved3(uint32_t reserved3)
Definition MachO/Section.hpp:249
void reserved1(uint32_t reserved1)
Definition MachO/Section.hpp:243
uint32_t alignment() const
Section alignment as a power of 2.
Definition MachO/Section.hpp:134
it_relocations relocations()
Return an iterator over the MachO::Relocation associated with this section.
Definition MachO/Section.hpp:216
TYPE type() const
Type of the section. This value can help to determine the purpose of the section (e....
Definition MachO/Section.hpp:163
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:150
TYPE
Definition MachO/Section.hpp:65
void numberof_relocations(uint32_t nb_reloc)
Definition MachO/Section.hpp:233
Section & operator-=(FLAGS flag)
Definition MachO/Section.hpp:266
const SegmentCommand * segment() const
Definition MachO/Section.hpp:204
Section & operator=(Section copy)
void flags(uint32_t flags)
Definition MachO/Section.hpp:236
Section(std::string name)
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:175
void reserved2(uint32_t reserved2)
Definition MachO/Section.hpp:246
FLAGS flags() const
Section's flags masked with SECTION_FLAGS_MASK (see: Section::FLAGS)
Definition MachO/Section.hpp:157
void segment_name(const std::string &name)
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:48
Class which represents an abstracted section.
Definition Abstract/Section.hpp:29
Definition Visitor.hpp:224
Iterator which returns reference on container's values.
Definition iterators.hpp:48
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
const char * to_string(BuildToolVersion::TOOLS tool)
LIEF namespace.
Definition Abstract/Binary.hpp:32
uint64_t align(uint64_t value, uint64_t align_on)
Definition utils.hpp:29
tcb::span< ElementType, Extent > span
Definition span.hpp:22
#define LIEF_API
Definition visibility.h:41