LIEF: Library to Instrument Executable Formats Version 1.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>
20#include <vector>
21#include <ostream>
22#include <memory>
23
24#include "LIEF/enums.hpp"
25#include "LIEF/span.hpp"
27#include "LIEF/visibility.h"
28
29#include "LIEF/iterators.hpp"
31
32
33namespace LIEF {
34class SpanStream;
35namespace MachO {
36
37class Binary;
38class BinaryParser;
39class Builder;
41class DyldInfo;
42class Parser;
43class Relocation;
44class Section;
45
46namespace details {
47struct segment_command_32;
48struct segment_command_64;
49}
50
54
56 friend class BinaryParser;
57 friend class Parser;
58 friend class Binary;
59 friend class Section;
60 friend class Builder;
61
62 public:
63 using content_t = std::vector<uint8_t>;
64
66 using sections_t = std::vector<std::unique_ptr<Section>>;
67
70
73
75 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
76
79
83
84 enum class FLAGS : uint64_t {
87 HIGHVM = 0x1u,
90 FVMLIB = 0x2u,
93 NORELOC = 0x4u,
94 PROTECTED_VERSION_1 = 0x8u,
95 READ_ONLY = 0x10u,
96 };
97
99 enum class VM_PROTECTIONS {
101 READ = 0x1,
103 WRITE = 0x2,
105 EXECUTE = 0x4,
106 };
107
108 public:
110 SegmentCommand(const details::segment_command_32& cmd);
111 SegmentCommand(const details::segment_command_64& cmd);
112
115
117
118 SegmentCommand(std::string name);
119
120 void swap(SegmentCommand& other) noexcept;
121
122 std::unique_ptr<LoadCommand> clone() const override {
123 return std::unique_ptr<SegmentCommand>(new SegmentCommand(*this));
124 }
125
126 ~SegmentCommand() override;
127
129 const std::string& name() const {
130 return name_;
131 }
132
134 uint64_t virtual_address() const {
135 return virtual_address_;
136 }
137
139 uint64_t virtual_size() const {
140 return virtual_size_;
141 }
142
144 uint64_t file_size() const {
145 return file_size_;
146 }
147
149 uint64_t file_offset() const {
150 return file_offset_;
151 }
152
154 uint32_t max_protection() const {
155 return max_protection_;
156 }
157
159 uint32_t init_protection() const {
160 return init_protection_;
161 }
162
164 uint32_t numberof_sections() const {
165 return nb_sections_;
166 }
167
169 uint32_t flags() const {
170 return flags_;
171 }
172
175 return sections_;
176 }
177
179 return sections_;
180 }
181
189 return relocations_;
190 }
192 return relocations_;
193 }
194
196 const Section* get_section(const std::string& name) const LIEF_LIFETIMEBOUND;
198
201 return data_;
202 }
203
205 return data_;
206 }
207
209 std::unique_ptr<SpanStream> stream() const;
210
212 int8_t index() const {
213 return index_;
214 }
215
216 void name(std::string name) {
217 name_ = std::move(name);
218 }
219
221 virtual_address_ = virtual_address;
222 }
223 void virtual_size(uint64_t virtual_size) {
224 virtual_size_ = virtual_size;
225 }
226 void file_offset(uint64_t file_offset) {
227 file_offset_ = file_offset;
228 }
229 void file_size(uint64_t file_size) {
230 file_size_ = file_size;
231 }
233 max_protection_ = max_protection;
234 }
236 init_protection_ = init_protection;
237 }
238 void numberof_sections(uint32_t nb_section) {
239 nb_sections_ = nb_section;
240 }
241 void flags(uint32_t flags) {
242 flags_ = flags;
243 }
244
246
249
252
254 bool has(const Section& section) const;
255
257 bool has_section(const std::string& section_name) const;
258
259 bool is(VM_PROTECTIONS prot) const {
260 return (init_protection() & (uint32_t)prot) > 0 ||
261 (max_protection() & (uint32_t)prot) > 0;
262 }
263
264 std::ostream& print(std::ostream& os) const override;
265
266 void accept(Visitor& visitor) const override;
267
268 static bool classof(const LoadCommand* cmd) {
269 const LoadCommand::TYPE type = cmd->command();
270 return type == LoadCommand::TYPE::SEGMENT ||
272 }
273
274 protected:
275 LIEF_LOCAL void content_resize(size_t size);
276 LIEF_LOCAL void content_insert(size_t where, size_t size);
277
278 void content_extend(size_t width) {
279 content_resize(data_.size() + width);
280 }
281
282 using update_fnc_t = std::function<void(std::vector<uint8_t>&)>;
283 using update_fnc_ws_t =
284 std::function<void(std::vector<uint8_t>&, size_t, size_t)>;
285
286 LIEF_LOCAL virtual void update_data(const update_fnc_t& f);
287 LIEF_LOCAL virtual void update_data(const update_fnc_ws_t& f, size_t where,
288 size_t size);
289
290 std::string name_;
291 uint64_t virtual_address_ = 0;
292 uint64_t virtual_size_ = 0;
293 uint64_t file_offset_ = 0;
294 uint64_t file_size_ = 0;
295 uint32_t max_protection_ = 0;
296 uint32_t init_protection_ = 0;
297 uint32_t nb_sections_ = 0;
298 uint32_t flags_ = 0;
299 int8_t index_ = -1;
300 content_t data_;
301 sections_t sections_;
302 relocations_t relocations_;
303};
304
307
308}
309}
310
313
314#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:91
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:48
uint64_t virtual_address() const
Absolute virtual base address of the segment.
Definition SegmentCommand.hpp:134
void swap(SegmentCommand &other) noexcept
friend class Section
Definition SegmentCommand.hpp:59
void file_offset(uint64_t file_offset)
Definition SegmentCommand.hpp:226
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing Mach-O Relocation.
Definition SegmentCommand.hpp:75
std::ostream & print(std::ostream &os) const override
void file_size(uint64_t file_size)
Definition SegmentCommand.hpp:229
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:69
it_relocations relocations()
Return an iterator over the MachO::Relocation linked to this segment.
Definition SegmentCommand.hpp:188
const_ref_iterator< const relocations_t &, const Relocation * > it_const_relocations
Iterator which outputs const Relocation&.
Definition SegmentCommand.hpp:81
std::vector< std::unique_ptr< Section > > sections_t
Internal container for storing Mach-O Section.
Definition SegmentCommand.hpp:66
friend class BinaryParser
Definition SegmentCommand.hpp:56
static bool classof(const LoadCommand *cmd)
Definition SegmentCommand.hpp:268
void content(content_t data)
void max_protection(uint32_t max_protection)
Definition SegmentCommand.hpp:232
FLAGS
Definition SegmentCommand.hpp:84
SegmentCommand(const details::segment_command_32 &cmd)
void virtual_size(uint64_t virtual_size)
Definition SegmentCommand.hpp:223
friend class DyldChainedFixupsCreator
Definition SegmentCommand.hpp:55
span< const uint8_t > content() const
The raw content of this segment.
Definition SegmentCommand.hpp:200
std::vector< uint8_t > content_t
Definition SegmentCommand.hpp:63
Section * get_section(const std::string &name)
const std::string & name() const
Name of the segment (e.g. __TEXT).
Definition SegmentCommand.hpp:129
VM_PROTECTIONS
Values for segment_command.initprot. From <mach/vm_prot.h>.
Definition SegmentCommand.hpp:99
uint64_t file_size() const
Size of this segment in the binary file.
Definition SegmentCommand.hpp:144
friend class Builder
Definition SegmentCommand.hpp:60
span< uint8_t > content()
Definition SegmentCommand.hpp:204
ref_iterator< relocations_t &, Relocation * > it_relocations
Iterator which outputs Relocation&.
Definition SegmentCommand.hpp:78
uint32_t numberof_sections() const
The number of sections associated with this segment.
Definition SegmentCommand.hpp:164
it_const_sections sections() const
Definition SegmentCommand.hpp:178
uint32_t max_protection() const
The maximum of protections for this segment (cf. VM_PROTECTIONS).
Definition SegmentCommand.hpp:154
const_ref_iterator< const sections_t &, const Section * > it_const_sections
Iterator which outputs const Section&.
Definition SegmentCommand.hpp:72
friend class Binary
Definition SegmentCommand.hpp:58
uint64_t virtual_size() const
Virtual size of the segment.
Definition SegmentCommand.hpp:139
void virtual_address(uint64_t virtual_address)
Definition SegmentCommand.hpp:220
int8_t index() const
The original index of this segment or -1 if not defined.
Definition SegmentCommand.hpp:212
friend class Parser
Definition SegmentCommand.hpp:57
uint32_t init_protection() const
The initial protections of this segment (cf. VM_PROTECTIONS).
Definition SegmentCommand.hpp:159
Section & add_section(const Section &section)
Add a new section in this segment.
it_const_relocations relocations() const
Definition SegmentCommand.hpp:191
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:241
SegmentCommand(std::string name, content_t content)
bool is(VM_PROTECTIONS prot) const
Definition SegmentCommand.hpp:259
it_sections sections()
Return an iterator over the MachO::Section linked to this segment.
Definition SegmentCommand.hpp:174
uint64_t file_offset() const
Offset of the data of this segment in the file.
Definition SegmentCommand.hpp:149
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:122
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:216
void init_protection(uint32_t init_protection)
Definition SegmentCommand.hpp:235
void numberof_sections(uint32_t nb_section)
Definition SegmentCommand.hpp:238
uint32_t flags() const
Flags associated with this segment (cf. SegmentCommand::FLAGS).
Definition SegmentCommand.hpp:169
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: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: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:322
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46