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 Relocation;
43class Section;
44
45namespace details {
46struct segment_command_32;
47struct segment_command_64;
48}
49
53
55 friend class BinaryParser;
56 friend class Binary;
57 friend class Section;
58 friend class Builder;
59
60 public:
61 using content_t = std::vector<uint8_t>;
62
64 using sections_t = std::vector<std::unique_ptr<Section>>;
65
68
71
73 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
74
77
81
82 enum class FLAGS : uint64_t {
85 HIGHVM = 0x1u,
88 FVMLIB = 0x2u,
91 NORELOC = 0x4u,
92 PROTECTED_VERSION_1 = 0x8u,
93 READ_ONLY = 0x10u,
94 };
95
97 enum class VM_PROTECTIONS {
99 READ = 0x1,
101 WRITE = 0x2,
103 EXECUTE = 0x4,
104 };
105
106 public:
108 SegmentCommand(const details::segment_command_32& cmd);
109 SegmentCommand(const details::segment_command_64& cmd);
110
113
115
116 SegmentCommand(std::string name);
117
118 void swap(SegmentCommand& other) noexcept;
119
120 std::unique_ptr<LoadCommand> clone() const override {
121 return std::unique_ptr<SegmentCommand>(new SegmentCommand(*this));
122 }
123
124 ~SegmentCommand() override;
125
127 const std::string& name() const {
128 return name_;
129 }
130
132 uint64_t virtual_address() const {
133 return virtual_address_;
134 }
135
137 uint64_t virtual_size() const {
138 return virtual_size_;
139 }
140
142 uint64_t file_size() const {
143 return file_size_;
144 }
145
147 uint64_t file_offset() const {
148 return file_offset_;
149 }
150
152 uint32_t max_protection() const {
153 return max_protection_;
154 }
155
157 uint32_t init_protection() const {
158 return init_protection_;
159 }
160
162 uint32_t numberof_sections() const {
163 return nb_sections_;
164 }
165
167 uint32_t flags() const {
168 return flags_;
169 }
170
173 return sections_;
174 }
175
177 return sections_;
178 }
179
187 return relocations_;
188 }
190 return relocations_;
191 }
192
194 const Section* get_section(const std::string& name) const LIEF_LIFETIMEBOUND;
196
199 return data_;
200 }
201
203 return data_;
204 }
205
207 std::unique_ptr<SpanStream> stream() const;
208
210 int8_t index() const {
211 return this->index_;
212 }
213
214 void name(std::string name) {
215 name_ = std::move(name);
216 }
217
219 virtual_address_ = virtual_address;
220 }
221 void virtual_size(uint64_t virtual_size) {
222 virtual_size_ = virtual_size;
223 }
224 void file_offset(uint64_t file_offset) {
225 file_offset_ = file_offset;
226 }
227 void file_size(uint64_t file_size) {
228 file_size_ = file_size;
229 }
231 max_protection_ = max_protection;
232 }
234 init_protection_ = init_protection;
235 }
236 void numberof_sections(uint32_t nb_section) {
237 nb_sections_ = nb_section;
238 }
239 void flags(uint32_t flags) {
240 flags_ = flags;
241 }
242
244
247
250
252 bool has(const Section& section) const;
253
255 bool has_section(const std::string& section_name) const;
256
257 bool is(VM_PROTECTIONS prot) const {
258 return (init_protection() & (uint32_t)prot) > 0 ||
259 (max_protection() & (uint32_t)prot) > 0;
260 }
261
262 std::ostream& print(std::ostream& os) const override;
263
264 void accept(Visitor& visitor) const override;
265
266 static bool classof(const LoadCommand* cmd) {
267 const LoadCommand::TYPE type = cmd->command();
268 return type == LoadCommand::TYPE::SEGMENT ||
270 }
271
272 protected:
273 LIEF_LOCAL void content_resize(size_t size);
274 LIEF_LOCAL void content_insert(size_t where, size_t size);
275
276 void content_extend(size_t width) {
277 content_resize(data_.size() + width);
278 }
279
280 using update_fnc_t = std::function<void(std::vector<uint8_t>&)>;
281 using update_fnc_ws_t =
282 std::function<void(std::vector<uint8_t>&, size_t, size_t)>;
283
284 LIEF_LOCAL virtual void update_data(const update_fnc_t& f);
285 LIEF_LOCAL virtual void update_data(const update_fnc_ws_t& f, size_t where,
286 size_t size);
287
288 std::string name_;
289 uint64_t virtual_address_ = 0;
290 uint64_t virtual_size_ = 0;
291 uint64_t file_offset_ = 0;
292 uint64_t file_size_ = 0;
293 uint32_t max_protection_ = 0;
294 uint32_t init_protection_ = 0;
295 uint32_t nb_sections_ = 0;
296 uint32_t flags_ = 0;
297 int8_t index_ = -1;
298 content_t data_;
299 sections_t sections_;
300 relocations_t relocations_;
301};
302
305
306}
307}
308
311
312#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:130
span< const uint8_t > data() const
Raw command.
Definition LoadCommand.hpp:140
TYPE
Definition LoadCommand.hpp:46
@ SEGMENT_64
Definition LoadCommand.hpp:72
@ SEGMENT
Definition LoadCommand.hpp:48
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:132
void swap(SegmentCommand &other) noexcept
friend class Section
Definition SegmentCommand.hpp:57
void file_offset(uint64_t file_offset)
Definition SegmentCommand.hpp:224
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing Mach-O Relocation.
Definition SegmentCommand.hpp:73
std::ostream & print(std::ostream &os) const override
void file_size(uint64_t file_size)
Definition SegmentCommand.hpp:227
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:67
it_relocations relocations()
Return an iterator over the MachO::Relocation linked to this segment.
Definition SegmentCommand.hpp:186
const_ref_iterator< const relocations_t &, const Relocation * > it_const_relocations
Iterator which outputs const Relocation&.
Definition SegmentCommand.hpp:79
std::vector< std::unique_ptr< Section > > sections_t
Internal container for storing Mach-O Section.
Definition SegmentCommand.hpp:64
friend class BinaryParser
Definition SegmentCommand.hpp:55
static bool classof(const LoadCommand *cmd)
Definition SegmentCommand.hpp:266
void content(content_t data)
void max_protection(uint32_t max_protection)
Definition SegmentCommand.hpp:230
FLAGS
Definition SegmentCommand.hpp:82
SegmentCommand(const details::segment_command_32 &cmd)
void virtual_size(uint64_t virtual_size)
Definition SegmentCommand.hpp:221
friend class DyldChainedFixupsCreator
Definition SegmentCommand.hpp:54
span< const uint8_t > content() const
The raw content of this segment.
Definition SegmentCommand.hpp:198
std::vector< uint8_t > content_t
Definition SegmentCommand.hpp:61
Section * get_section(const std::string &name)
const std::string & name() const
Name of the segment (e.g. __TEXT).
Definition SegmentCommand.hpp:127
VM_PROTECTIONS
Values for segment_command.initprot. From <mach/vm_prot.h>.
Definition SegmentCommand.hpp:97
uint64_t file_size() const
Size of this segment in the binary file.
Definition SegmentCommand.hpp:142
friend class Builder
Definition SegmentCommand.hpp:58
span< uint8_t > content()
Definition SegmentCommand.hpp:202
ref_iterator< relocations_t &, Relocation * > it_relocations
Iterator which outputs Relocation&.
Definition SegmentCommand.hpp:76
uint32_t numberof_sections() const
The number of sections associated with this segment.
Definition SegmentCommand.hpp:162
it_const_sections sections() const
Definition SegmentCommand.hpp:176
uint32_t max_protection() const
The maximum of protections for this segment (cf. VM_PROTECTIONS).
Definition SegmentCommand.hpp:152
const_ref_iterator< const sections_t &, const Section * > it_const_sections
Iterator which outputs const Section&.
Definition SegmentCommand.hpp:70
friend class Binary
Definition SegmentCommand.hpp:56
uint64_t virtual_size() const
Virtual size of the segment.
Definition SegmentCommand.hpp:137
void virtual_address(uint64_t virtual_address)
Definition SegmentCommand.hpp:218
int8_t index() const
The original index of this segment or -1 if not defined.
Definition SegmentCommand.hpp:210
uint32_t init_protection() const
The initial protections of this segment (cf. VM_PROTECTIONS).
Definition SegmentCommand.hpp:157
Section & add_section(const Section &section)
Add a new section in this segment.
it_const_relocations relocations() const
Definition SegmentCommand.hpp:189
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:239
SegmentCommand(std::string name, content_t content)
bool is(VM_PROTECTIONS prot) const
Definition SegmentCommand.hpp:257
it_sections sections()
Return an iterator over the MachO::Section linked to this segment.
Definition SegmentCommand.hpp:172
uint64_t file_offset() const
Offset of the data of this segment in the file.
Definition SegmentCommand.hpp:147
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:120
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:214
void init_protection(uint32_t init_protection)
Definition SegmentCommand.hpp:233
void numberof_sections(uint32_t nb_section)
Definition SegmentCommand.hpp:236
uint32_t flags() const
Flags associated with this segment (cf. SegmentCommand::FLAGS).
Definition SegmentCommand.hpp:167
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:320
#define LIEF_API
Definition visibility.h:45
#define LIEF_LOCAL
Definition visibility.h:46