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"
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
52
54 friend class BinaryParser;
55 friend class Binary;
56 friend class Section;
57 friend class Builder;
58
59 public:
60 using content_t = std::vector<uint8_t>;
61
63 using sections_t = std::vector<std::unique_ptr<Section>>;
64
67
70
72 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
73
76
80
81 enum class FLAGS : uint64_t {
84 HIGHVM = 0x1u,
87 FVMLIB = 0x2u,
90 NORELOC = 0x4u,
91 PROTECTED_VERSION_1 = 0x8u,
92 READ_ONLY = 0x10u,
93 };
94
96 enum class VM_PROTECTIONS {
98 READ = 0x1,
100 WRITE = 0x2,
102 EXECUTE = 0x4,
103 };
104
105 public:
107 SegmentCommand(const details::segment_command_32& cmd);
108 SegmentCommand(const details::segment_command_64& cmd);
109
112
114
115 SegmentCommand(std::string name);
116
117 void swap(SegmentCommand& other) noexcept;
118
119 std::unique_ptr<LoadCommand> clone() const override {
120 return std::unique_ptr<SegmentCommand>(new SegmentCommand(*this));
121 }
122
123 ~SegmentCommand() override;
124
126 const std::string& name() const {
127 return name_;
128 }
129
131 uint64_t virtual_address() const {
132 return virtual_address_;
133 }
134
136 uint64_t virtual_size() const {
137 return virtual_size_;
138 }
139
141 uint64_t file_size() const {
142 return file_size_;
143 }
144
146 uint64_t file_offset() const {
147 return file_offset_;
148 }
149
151 uint32_t max_protection() const {
152 return max_protection_;
153 }
154
156 uint32_t init_protection() const {
157 return init_protection_;
158 }
159
161 uint32_t numberof_sections() const {
162 return nb_sections_;
163 }
164
166 uint32_t flags() const {
167 return flags_;
168 }
169
172 return sections_;
173 }
174
176 return sections_;
177 }
178
186 return relocations_;
187 }
189 return relocations_;
190 }
191
193 const Section* get_section(const std::string& name) const;
194 Section* get_section(const std::string& name);
195
198 return data_;
199 }
200
202 return data_;
203 }
204
206 std::unique_ptr<SpanStream> stream() const;
207
209 int8_t index() const {
210 return this->index_;
211 }
212
213 void name(std::string name) {
214 name_ = std::move(name);
215 }
216
218 virtual_address_ = virtual_address;
219 }
220 void virtual_size(uint64_t virtual_size) {
221 virtual_size_ = virtual_size;
222 }
223 void file_offset(uint64_t file_offset) {
224 file_offset_ = file_offset;
225 }
226 void file_size(uint64_t file_size) {
227 file_size_ = file_size;
228 }
230 max_protection_ = max_protection;
231 }
233 init_protection_ = init_protection;
234 }
235 void numberof_sections(uint32_t nb_section) {
236 nb_sections_ = nb_section;
237 }
238 void flags(uint32_t flags) {
239 flags_ = flags;
240 }
241
243
245 Section& add_section(const Section& section);
246
249
251 bool has(const Section& section) const;
252
254 bool has_section(const std::string& section_name) const;
255
256 bool is(VM_PROTECTIONS prot) const {
257 return (init_protection() & (uint32_t)prot) > 0 ||
258 (max_protection() & (uint32_t)prot) > 0;
259 }
260
261 std::ostream& print(std::ostream& os) const override;
262
263 void accept(Visitor& visitor) const override;
264
265 static bool classof(const LoadCommand* cmd) {
266 const LoadCommand::TYPE type = cmd->command();
267 return type == LoadCommand::TYPE::SEGMENT ||
269 }
270
271 protected:
272 LIEF_LOCAL void content_resize(size_t size);
273 LIEF_LOCAL void content_insert(size_t where, size_t size);
274
275 void content_extend(size_t width) {
276 content_resize(data_.size() + width);
277 }
278
279 using update_fnc_t = std::function<void(std::vector<uint8_t>&)>;
280 using update_fnc_ws_t =
281 std::function<void(std::vector<uint8_t>&, size_t, size_t)>;
282
283 LIEF_LOCAL virtual void update_data(const update_fnc_t& f);
284 LIEF_LOCAL virtual void update_data(const update_fnc_ws_t& f, size_t where,
285 size_t size);
286
287 std::string name_;
288 uint64_t virtual_address_ = 0;
289 uint64_t virtual_size_ = 0;
290 uint64_t file_offset_ = 0;
291 uint64_t file_size_ = 0;
292 uint32_t max_protection_ = 0;
293 uint32_t init_protection_ = 0;
294 uint32_t nb_sections_ = 0;
295 uint32_t flags_ = 0;
296 int8_t index_ = -1;
297 content_t data_;
298 sections_t sections_;
299 relocations_t relocations_;
300};
301
304
305}
306}
307
310
311#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:45
@ SEGMENT_64
Definition LoadCommand.hpp:71
@ SEGMENT
Definition LoadCommand.hpp:47
Class that represents a Mach-O relocation.
Definition MachO/Relocation.hpp:40
Class that represents a Mach-O section.
Definition MachO/Section.hpp:47
uint64_t virtual_address() const
Absolute virtual base address of the segment.
Definition SegmentCommand.hpp:131
void swap(SegmentCommand &other) noexcept
friend class Section
Definition SegmentCommand.hpp:56
void file_offset(uint64_t file_offset)
Definition SegmentCommand.hpp:223
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing Mach-O Relocation.
Definition SegmentCommand.hpp:72
std::ostream & print(std::ostream &os) const override
void file_size(uint64_t file_size)
Definition SegmentCommand.hpp:226
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:66
it_relocations relocations()
Return an iterator over the MachO::Relocation linked to this segment.
Definition SegmentCommand.hpp:185
const_ref_iterator< const relocations_t &, const Relocation * > it_const_relocations
Iterator which outputs const Relocation&.
Definition SegmentCommand.hpp:78
std::vector< std::unique_ptr< Section > > sections_t
Internal container for storing Mach-O Section.
Definition SegmentCommand.hpp:63
friend class BinaryParser
Definition SegmentCommand.hpp:54
static bool classof(const LoadCommand *cmd)
Definition SegmentCommand.hpp:265
void content(content_t data)
void max_protection(uint32_t max_protection)
Definition SegmentCommand.hpp:229
FLAGS
Definition SegmentCommand.hpp:81
SegmentCommand(const details::segment_command_32 &cmd)
void virtual_size(uint64_t virtual_size)
Definition SegmentCommand.hpp:220
friend class DyldChainedFixupsCreator
Definition SegmentCommand.hpp:53
span< const uint8_t > content() const
The raw content of this segment.
Definition SegmentCommand.hpp:197
std::vector< uint8_t > content_t
Definition SegmentCommand.hpp:60
Section * get_section(const std::string &name)
const std::string & name() const
Name of the segment (e.g. __TEXT).
Definition SegmentCommand.hpp:126
VM_PROTECTIONS
Values for segment_command.initprot. From <mach/vm_prot.h>.
Definition SegmentCommand.hpp:96
uint64_t file_size() const
Size of this segment in the binary file.
Definition SegmentCommand.hpp:141
friend class Builder
Definition SegmentCommand.hpp:57
span< uint8_t > content()
Definition SegmentCommand.hpp:201
ref_iterator< relocations_t &, Relocation * > it_relocations
Iterator which outputs Relocation&.
Definition SegmentCommand.hpp:75
uint32_t numberof_sections() const
The number of sections associated with this segment.
Definition SegmentCommand.hpp:161
it_const_sections sections() const
Definition SegmentCommand.hpp:175
uint32_t max_protection() const
The maximum of protections for this segment (cf. VM_PROTECTIONS).
Definition SegmentCommand.hpp:151
const_ref_iterator< const sections_t &, const Section * > it_const_sections
Iterator which outputs const Section&.
Definition SegmentCommand.hpp:69
friend class Binary
Definition SegmentCommand.hpp:55
uint64_t virtual_size() const
Virtual size of the segment.
Definition SegmentCommand.hpp:136
void virtual_address(uint64_t virtual_address)
Definition SegmentCommand.hpp:217
int8_t index() const
The original index of this segment or -1 if not defined.
Definition SegmentCommand.hpp:209
uint32_t init_protection() const
The initial protections of this segment (cf. VM_PROTECTIONS).
Definition SegmentCommand.hpp:156
Section & add_section(const Section &section)
Add a new section in this segment.
it_const_relocations relocations() const
Definition SegmentCommand.hpp:188
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:238
SegmentCommand(std::string name, content_t content)
bool is(VM_PROTECTIONS prot) const
Definition SegmentCommand.hpp:256
it_sections sections()
Return an iterator over the MachO::Section linked to this segment.
Definition SegmentCommand.hpp:171
uint64_t file_offset() const
Offset of the data of this segment in the file.
Definition SegmentCommand.hpp:146
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:119
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:213
void init_protection(uint32_t init_protection)
Definition SegmentCommand.hpp:232
void numberof_sections(uint32_t nb_section)
Definition SegmentCommand.hpp:235
uint32_t flags() const
Flags associated with this segment (cf. SegmentCommand::FLAGS).
Definition SegmentCommand.hpp:166
Definition SpanStream.hpp:32
Definition Visitor.hpp:212
Iterator which returns reference on container's values.
Definition iterators.hpp:45
#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: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:286
#define LIEF_API
Definition visibility.h:43
#define LIEF_LOCAL
Definition visibility.h:44