LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
SegmentCommand.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2025 R. Thomas
2 * Copyright 2017 - 2025 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"
26#include "LIEF/visibility.h"
27
28#include "LIEF/iterators.hpp"
30
31
32namespace LIEF {
33class SpanStream;
34namespace MachO {
35
36class Binary;
37class BinaryParser;
38class Builder;
40class DyldInfo;
41class Relocation;
42class Section;
43
44namespace details {
45struct segment_command_32;
46struct segment_command_64;
47}
48
51
53 friend class BinaryParser;
54 friend class Binary;
55 friend class Section;
56 friend class Builder;
57
58 public:
59 using content_t = std::vector<uint8_t>;
60
62 using sections_t = std::vector<std::unique_ptr<Section>>;
63
66
69
71 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
72
75
78
79 enum class FLAGS: uint64_t {
80 HIGHVM = 0x1u,
81 FVMLIB = 0x2u,
82 NORELOC = 0x4u,
84 READ_ONLY = 0x10u,
85 };
86
89 enum class VM_PROTECTIONS {
90 READ = 0x1,
91 WRITE = 0x2,
92 EXECUTE = 0x4,
93 };
94
95 public:
97 SegmentCommand(const details::segment_command_32& cmd);
98 SegmentCommand(const details::segment_command_64& cmd);
99
102
104
105 SegmentCommand(std::string name);
106
107 void swap(SegmentCommand& other) noexcept;
108
109 std::unique_ptr<LoadCommand> clone() const override {
110 return std::unique_ptr<SegmentCommand>(new SegmentCommand(*this));
111 }
112
113 ~SegmentCommand() override;
114
116 const std::string& name() const {
117 return name_;
118 }
119
121 uint64_t virtual_address() const {
122 return virtual_address_;
123 }
124
126 uint64_t virtual_size() const {
127 return virtual_size_;
128 }
129
131 uint64_t file_size() const {
132 return file_size_;
133 }
134
136 uint64_t file_offset() const {
137 return file_offset_;
138 }
139
141 uint32_t max_protection() const {
142 return max_protection_;
143 }
144
146 uint32_t init_protection() const {
147 return init_protection_;
148 }
149
151 uint32_t numberof_sections() const {
152 return nb_sections_;
153 }
154
156 uint32_t flags() const {
157 return flags_;
158 }
159
162 return sections_;
163 }
164
166 return sections_;
167 }
168
175 return relocations_;
176 }
178 return relocations_;
179 }
180
182 const Section* get_section(const std::string& name) const;
183 Section* get_section(const std::string& name);
184
187 return data_;
188 }
189
191 std::unique_ptr<SpanStream> stream() const;
192
194 int8_t index() const {
195 return this->index_;
196 }
197
198 void name(std::string name) {
199 name_ = std::move(name);
200 }
201
203 virtual_address_ = virtual_address;
204 }
205 void virtual_size(uint64_t virtual_size) {
206 virtual_size_ = virtual_size;
207 }
208 void file_offset(uint64_t file_offset) {
209 file_offset_ = file_offset;
210 }
211 void file_size(uint64_t file_size) {
212 file_size_ = file_size;
213 }
215 max_protection_ = max_protection;
216 }
218 init_protection_ = init_protection;
219 }
220 void numberof_sections(uint32_t nb_section) {
221 nb_sections_ = nb_section;
222 }
223 void flags(uint32_t flags) {
224 flags_ = flags;
225 }
226
228
230 Section& add_section(const Section& section);
231
234
236 bool has(const Section& section) const;
237
239 bool has_section(const std::string& section_name) const;
240
241 bool is(VM_PROTECTIONS prot) const {
242 return (init_protection() & (uint32_t)prot) > 0 ||
243 (max_protection() & (uint32_t)prot) > 0;
244 }
245
246 std::ostream& print(std::ostream& os) const override;
247
248 void accept(Visitor& visitor) const override;
249
250 static bool classof(const LoadCommand* cmd) {
251 const LoadCommand::TYPE type = cmd->command();
252 return type == LoadCommand::TYPE::SEGMENT ||
254 }
255
256 protected:
257 span<uint8_t> writable_content() {
258 return data_;
259 }
260
261 LIEF_LOCAL void content_resize(size_t size);
262 LIEF_LOCAL void content_insert(size_t where, size_t size);
263
264 void content_extend(size_t width) {
265 content_resize(data_.size() + width);
266 }
267
268 using update_fnc_t = std::function<void(std::vector<uint8_t>&)>;
269 using update_fnc_ws_t = std::function<void(std::vector<uint8_t>&, size_t, size_t)>;
270
271 LIEF_LOCAL virtual void update_data(const update_fnc_t& f);
272 LIEF_LOCAL virtual void update_data(const update_fnc_ws_t& f,
273 size_t where, size_t size);
274
275 std::string name_;
276 uint64_t virtual_address_ = 0;
277 uint64_t virtual_size_ = 0;
278 uint64_t file_offset_ = 0;
279 uint64_t file_size_ = 0;
280 uint32_t max_protection_ = 0;
281 uint32_t init_protection_ = 0;
282 uint32_t nb_sections_ = 0;
283 uint32_t flags_ = 0;
284 int8_t index_ = -1;
285 content_t data_;
286 sections_t sections_;
287 relocations_t relocations_;
288};
289
292
293}
294}
295
298
299#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 used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:63
Definition DyldChainedFixupsCreator.hpp:41
Class that represents the LC_DYLD_INFO and LC_DYLD_INFO_ONLY commands.
Definition DyldInfo.hpp:50
LoadCommand::TYPE command() const
Command type.
Definition LoadCommand.hpp:128
span< const uint8_t > data() const
Raw command.
Definition LoadCommand.hpp:138
TYPE
Definition LoadCommand.hpp:44
@ SEGMENT_64
Definition LoadCommand.hpp:70
@ SEGMENT
Definition LoadCommand.hpp:46
Class that represents a Mach-O relocation.
Definition MachO/Relocation.hpp:40
Class that represents a Mach-O section.
Definition MachO/Section.hpp:46
uint64_t virtual_address() const
Absolute virtual base address of the segment.
Definition SegmentCommand.hpp:121
void swap(SegmentCommand &other) noexcept
friend class Section
Definition SegmentCommand.hpp:55
void file_offset(uint64_t file_offset)
Definition SegmentCommand.hpp:208
const_ref_iterator< const relocations_t &, const Relocation * > it_const_relocations
Iterator which outputs const Relocation&.
Definition SegmentCommand.hpp:77
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing Mach-O Relocation.
Definition SegmentCommand.hpp:71
std::ostream & print(std::ostream &os) const override
void file_size(uint64_t file_size)
Definition SegmentCommand.hpp:211
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:65
it_relocations relocations()
Return an iterator over the MachO::Relocation linked to this segment.
Definition SegmentCommand.hpp:174
std::vector< std::unique_ptr< Section > > sections_t
Internal container for storing Mach-O Section.
Definition SegmentCommand.hpp:62
friend class BinaryParser
Definition SegmentCommand.hpp:53
static bool classof(const LoadCommand *cmd)
Definition SegmentCommand.hpp:250
void content(content_t data)
void max_protection(uint32_t max_protection)
Definition SegmentCommand.hpp:214
FLAGS
Definition SegmentCommand.hpp:79
@ PROTECTED_VERSION_1
Definition SegmentCommand.hpp:83
@ READ_ONLY
Definition SegmentCommand.hpp:84
@ NORELOC
This segment has nothing that was relocated in it and nothing relocated to it. It may be safely repla...
Definition SegmentCommand.hpp:82
@ FVMLIB
this segment is the VM that is allocated by a fixed VM library, for overlap checking in the link edit...
Definition SegmentCommand.hpp:81
@ HIGHVM
The file contents for this segment are for the high part of the virtual memory space; the low part is...
Definition SegmentCommand.hpp:80
SegmentCommand(const details::segment_command_32 &cmd)
void virtual_size(uint64_t virtual_size)
Definition SegmentCommand.hpp:205
friend class DyldChainedFixupsCreator
Definition SegmentCommand.hpp:52
span< const uint8_t > content() const
The raw content of this segment.
Definition SegmentCommand.hpp:186
std::vector< uint8_t > content_t
Definition SegmentCommand.hpp:59
Section * get_section(const std::string &name)
const std::string & name() const
Name of the segment (e.g. __TEXT).
Definition SegmentCommand.hpp:116
VM_PROTECTIONS
Values for segment_command.initprot. From <mach/vm_prot.h>.
Definition SegmentCommand.hpp:89
@ READ
Reading data within the segment is allowed.
Definition SegmentCommand.hpp:90
@ EXECUTE
Executing data within the segment is allowed.
Definition SegmentCommand.hpp:92
@ WRITE
Writing data within the segment is allowed.
Definition SegmentCommand.hpp:91
uint64_t file_size() const
Size of this segment in the binary file.
Definition SegmentCommand.hpp:131
friend class Builder
Definition SegmentCommand.hpp:56
ref_iterator< relocations_t &, Relocation * > it_relocations
Iterator which outputs Relocation&.
Definition SegmentCommand.hpp:74
uint32_t numberof_sections() const
The number of sections associated with this segment.
Definition SegmentCommand.hpp:151
it_const_sections sections() const
Definition SegmentCommand.hpp:165
uint32_t max_protection() const
The maximum of protections for this segment (cf. VM_PROTECTIONS).
Definition SegmentCommand.hpp:141
const_ref_iterator< const sections_t &, const Section * > it_const_sections
Iterator which outputs const Section&.
Definition SegmentCommand.hpp:68
friend class Binary
Definition SegmentCommand.hpp:54
uint64_t virtual_size() const
Virtual size of the segment.
Definition SegmentCommand.hpp:126
void virtual_address(uint64_t virtual_address)
Definition SegmentCommand.hpp:202
int8_t index() const
The original index of this segment or -1 if not defined.
Definition SegmentCommand.hpp:194
uint32_t init_protection() const
The initial protections of this segment (cf. VM_PROTECTIONS).
Definition SegmentCommand.hpp:146
Section & add_section(const Section &section)
Add a new section in this segment.
it_const_relocations relocations() const
Definition SegmentCommand.hpp:177
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:223
SegmentCommand(std::string name, content_t content)
bool is(VM_PROTECTIONS prot) const
Definition SegmentCommand.hpp:241
it_sections sections()
Return an iterator over the MachO::Section linked to this segment.
Definition SegmentCommand.hpp:161
uint64_t file_offset() const
Offset of the data of this segment in the file.
Definition SegmentCommand.hpp:136
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:109
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:198
void init_protection(uint32_t init_protection)
Definition SegmentCommand.hpp:217
void numberof_sections(uint32_t nb_section)
Definition SegmentCommand.hpp:220
uint32_t flags() const
Flags associated with this segment (cf. SegmentCommand::FLAGS).
Definition SegmentCommand.hpp:156
Definition SpanStream.hpp:32
Definition Visitor.hpp:210
Iterator which returns reference on container's values.
Definition iterators.hpp:46
#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:40
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:257
#define LIEF_API
Definition visibility.h:41
#define LIEF_LOCAL
Definition visibility.h:42