LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
SegmentCommand.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_SEGMENT_COMMAND_H
17#define LIEF_MACHO_SEGMENT_COMMAND_H
18
19#include <string_view>
20#include <memory>
21#include <ostream>
22#include <string>
23#include <vector>
24
26#include "LIEF/enums.hpp"
27#include "LIEF/span.hpp"
28#include "LIEF/visibility.h"
29
31#include "LIEF/iterators.hpp"
32
33
34namespace LIEF {
35class SpanStream;
36namespace MachO {
37
38class Binary;
39class BinaryParser;
40class Builder;
42class DyldInfo;
43class Parser;
44class Relocation;
45class Section;
46
47namespace details {
48struct segment_command_32;
49struct segment_command_64;
50}
51
55
57 friend class BinaryParser;
58 friend class Parser;
59 friend class Binary;
60 friend class Section;
61 friend class Builder;
62
63 public:
64 using content_t = std::vector<uint8_t>;
65
67 using sections_t = std::vector<std::unique_ptr<Section>>;
68
71
74
76 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
77
80
84
85 enum class FLAGS : uint64_t {
88 HIGHVM = 0x1u,
91 FVMLIB = 0x2u,
94 NORELOC = 0x4u,
95 PROTECTED_VERSION_1 = 0x8u,
96 READ_ONLY = 0x10u,
97 };
98
100 enum class VM_PROTECTIONS {
102 READ = 0x1,
104 WRITE = 0x2,
106 EXECUTE = 0x4,
107 };
108
109 public:
111 SegmentCommand(const details::segment_command_32& cmd);
112 SegmentCommand(const details::segment_command_64& cmd);
113
116
118
119 SegmentCommand(std::string name);
120
121 void swap(SegmentCommand& other) noexcept;
122
123 std::unique_ptr<LoadCommand> clone() const override {
124 return std::make_unique<SegmentCommand>(*this);
125 }
126
127 ~SegmentCommand() override;
128
130 std::string_view name() const LIEF_LIFETIMEBOUND {
131 return name_;
132 }
133
135 uint64_t virtual_address() const {
136 return virtual_address_;
137 }
138
140 uint64_t virtual_size() const {
141 return virtual_size_;
142 }
143
145 uint64_t file_size() const {
146 return file_size_;
147 }
148
150 uint64_t file_offset() const {
151 return file_offset_;
152 }
153
155 uint32_t max_protection() const {
156 return max_protection_;
157 }
158
160 uint32_t init_protection() const {
161 return init_protection_;
162 }
163
165 uint32_t numberof_sections() const {
166 return nb_sections_;
167 }
168
170 uint32_t flags() const {
171 return flags_;
172 }
173
176 return sections_;
177 }
178
180 return sections_;
181 }
182
190 return relocations_;
191 }
193 return relocations_;
194 }
195
197 const Section* get_section(const std::string& name) const LIEF_LIFETIMEBOUND;
199
202 return data_;
203 }
204
206 return data_;
207 }
208
210 std::unique_ptr<SpanStream> stream() const;
211
213 int8_t index() const {
214 return index_;
215 }
216
217 void name(std::string name) {
218 name_ = std::move(name);
219 }
220
222 virtual_address_ = virtual_address;
223 }
224 void virtual_size(uint64_t virtual_size) {
225 virtual_size_ = virtual_size;
226 }
227 void file_offset(uint64_t file_offset) {
228 file_offset_ = file_offset;
229 }
230 void file_size(uint64_t file_size) {
231 file_size_ = file_size;
232 }
234 max_protection_ = max_protection;
235 }
237 init_protection_ = init_protection;
238 }
239 void numberof_sections(uint32_t nb_section) {
240 nb_sections_ = nb_section;
241 }
242 void flags(uint32_t flags) {
243 flags_ = flags;
244 }
245
247
250
253
255 bool has(const Section& section) const;
256
258 bool has_section(const std::string& section_name) const;
259
260 bool is(VM_PROTECTIONS prot) const {
261 return (init_protection() & (uint32_t)prot) > 0 ||
262 (max_protection() & (uint32_t)prot) > 0;
263 }
264
265 std::ostream& print(std::ostream& os) const override;
266
267 void accept(Visitor& visitor) const override;
268
269 static bool classof(const LoadCommand* cmd) {
270 const LoadCommand::TYPE type = cmd->command();
271 return type == LoadCommand::TYPE::SEGMENT ||
273 }
274
275 protected:
276 LIEF_LOCAL void content_resize(size_t size);
277 LIEF_LOCAL void content_insert(size_t where, size_t size);
278
279 void content_extend(size_t width) {
280 content_resize(data_.size() + width);
281 }
282
283 using update_fnc_t = std::function<void(std::vector<uint8_t>&)>;
284 using update_fnc_ws_t =
285 std::function<void(std::vector<uint8_t>&, size_t, size_t)>;
286
287 LIEF_LOCAL virtual void update_data(const update_fnc_t& f);
288 LIEF_LOCAL virtual void update_data(const update_fnc_ws_t& f, size_t where,
289 size_t size);
290
291 std::string name_;
292 uint64_t virtual_address_ = 0;
293 uint64_t virtual_size_ = 0;
294 uint64_t file_offset_ = 0;
295 uint64_t file_size_ = 0;
296 uint32_t max_protection_ = 0;
297 uint32_t init_protection_ = 0;
298 uint32_t nb_sections_ = 0;
299 uint32_t flags_ = 0;
300 int8_t index_ = -1;
301 content_t data_;
302 sections_t sections_;
303 relocations_t relocations_;
304};
305
308
309}
310}
311
314
315#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 used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:64
Definition DyldChainedFixupsCreator.hpp:41
Class that represents the LC_DYLD_INFO and LC_DYLD_INFO_ONLY commands.
Definition DyldInfo.hpp:51
LoadCommand::TYPE command() const
Command type.
Definition LoadCommand.hpp:132
span< const uint8_t > data() const
Raw command.
Definition LoadCommand.hpp:142
TYPE
Definition LoadCommand.hpp:47
@ SEGMENT_64
Definition LoadCommand.hpp:73
@ SEGMENT
Definition LoadCommand.hpp:49
The main interface to parse a Mach-O binary.
Definition MachO/Parser.hpp:42
Class that represents a Mach-O relocation.
Definition MachO/Relocation.hpp:40
Class that represents a Mach-O section.
Definition MachO/Section.hpp:49
uint64_t virtual_address() const
Absolute virtual base address of the segment.
Definition SegmentCommand.hpp:135
void swap(SegmentCommand &other) noexcept
friend class Section
Definition SegmentCommand.hpp:60
void file_offset(uint64_t file_offset)
Definition SegmentCommand.hpp:227
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing Mach-O Relocation.
Definition SegmentCommand.hpp:76
std::ostream & print(std::ostream &os) const override
void file_size(uint64_t file_size)
Definition SegmentCommand.hpp:230
const Section * get_section(const std::string &name) const
Get the section with the given name.
SegmentCommand & operator=(SegmentCommand other)
SegmentCommand(const details::segment_command_64 &cmd)
ref_iterator< sections_t &, Section * > it_sections
Iterator which outputs Section&.
Definition SegmentCommand.hpp:70
it_relocations relocations()
Return an iterator over the MachO::Relocation linked to this segment.
Definition SegmentCommand.hpp:189
const_ref_iterator< const relocations_t &, const Relocation * > it_const_relocations
Iterator which outputs const Relocation&.
Definition SegmentCommand.hpp:82
std::vector< std::unique_ptr< Section > > sections_t
Internal container for storing Mach-O Section.
Definition SegmentCommand.hpp:67
friend class BinaryParser
Definition SegmentCommand.hpp:57
static bool classof(const LoadCommand *cmd)
Definition SegmentCommand.hpp:269
void content(content_t data)
void max_protection(uint32_t max_protection)
Definition SegmentCommand.hpp:233
FLAGS
Definition SegmentCommand.hpp:85
SegmentCommand(const details::segment_command_32 &cmd)
void virtual_size(uint64_t virtual_size)
Definition SegmentCommand.hpp:224
friend class DyldChainedFixupsCreator
Definition SegmentCommand.hpp:56
span< const uint8_t > content() const
The raw content of this segment.
Definition SegmentCommand.hpp:201
std::vector< uint8_t > content_t
Definition SegmentCommand.hpp:64
Section * get_section(const std::string &name)
VM_PROTECTIONS
Values for segment_command.initprot. From <mach/vm_prot.h>.
Definition SegmentCommand.hpp:100
uint64_t file_size() const
Size of this segment in the binary file.
Definition SegmentCommand.hpp:145
friend class Builder
Definition SegmentCommand.hpp:61
std::string_view name() const
Name of the segment (e.g. __TEXT).
Definition SegmentCommand.hpp:130
span< uint8_t > content()
Definition SegmentCommand.hpp:205
ref_iterator< relocations_t &, Relocation * > it_relocations
Iterator which outputs Relocation&.
Definition SegmentCommand.hpp:79
uint32_t numberof_sections() const
The number of sections associated with this segment.
Definition SegmentCommand.hpp:165
it_const_sections sections() const
Definition SegmentCommand.hpp:179
uint32_t max_protection() const
The maximum of protections for this segment (cf. VM_PROTECTIONS).
Definition SegmentCommand.hpp:155
const_ref_iterator< const sections_t &, const Section * > it_const_sections
Iterator which outputs const Section&.
Definition SegmentCommand.hpp:73
friend class Binary
Definition SegmentCommand.hpp:59
uint64_t virtual_size() const
Virtual size of the segment.
Definition SegmentCommand.hpp:140
void virtual_address(uint64_t virtual_address)
Definition SegmentCommand.hpp:221
int8_t index() const
The original index of this segment or -1 if not defined.
Definition SegmentCommand.hpp:213
friend class Parser
Definition SegmentCommand.hpp:58
uint32_t init_protection() const
The initial protections of this segment (cf. VM_PROTECTIONS).
Definition SegmentCommand.hpp:160
Section & add_section(const Section &section)
Add a new section in this segment.
it_const_relocations relocations() const
Definition SegmentCommand.hpp:192
void accept(Visitor &visitor) const override
bool has_section(const std::string &section_name) const
Check if the current segment embeds the given section name.
void flags(uint32_t flags)
Definition SegmentCommand.hpp:242
SegmentCommand(std::string name, content_t content)
bool is(VM_PROTECTIONS prot) const
Definition SegmentCommand.hpp:260
it_sections sections()
Return an iterator over the MachO::Section linked to this segment.
Definition SegmentCommand.hpp:175
uint64_t file_offset() const
Offset of the data of this segment in the file.
Definition SegmentCommand.hpp:150
bool has(const Section &section) const
Check if the current segment embeds the given section.
SegmentCommand(const SegmentCommand &copy)
std::unique_ptr< LoadCommand > clone() const override
Definition SegmentCommand.hpp:123
std::unique_ptr< SpanStream > stream() const
Return a stream over the content of this segment.
SegmentCommand(std::string name)
void remove_all_sections()
Remove all the sections linked to this segment.
void name(std::string name)
Definition SegmentCommand.hpp:217
void init_protection(uint32_t init_protection)
Definition SegmentCommand.hpp:236
void numberof_sections(uint32_t nb_section)
Definition SegmentCommand.hpp:239
uint32_t flags() const
Flags associated with this segment (cf. SegmentCommand::FLAGS).
Definition SegmentCommand.hpp:170
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
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
#define LIEF_LOCAL
Definition visibility.h:46