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 - 2024 R. Thomas
2 * Copyright 2017 - 2024 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}
48class LIEF_API SegmentCommand : public LoadCommand {
51
52 friend class DyldChainedFixupsCreator;
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 using sections_t = std::vector<std::unique_ptr<Section>>;
63 using it_sections = ref_iterator<sections_t&, Section*>;
66 using it_const_sections = const_ref_iterator<const sections_t&, const Section*>;
69 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
72 using it_relocations = ref_iterator<relocations_t&, Relocation*>;
75 using it_const_relocations = const_ref_iterator<const relocations_t&, const Relocation*>;
78
79 enum class FLAGS: uint64_t {
80 HIGHVM = 0x1u,
81 FVMLIB = 0x2u,
82 NORELOC = 0x4u,
83 PROTECTED_VERSION_1 = 0x8u,
84 READ_ONLY = 0x10u,
85 };
86 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
100 SegmentCommand& operator=(SegmentCommand other);
101 SegmentCommand(const SegmentCommand& copy);
102
103 SegmentCommand(std::string name, content_t content);
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 const std::string& name() const {
117 return name_;
118 }
119 uint64_t virtual_address() const {
122 return virtual_address_;
123 }
124 uint64_t virtual_size() const {
127 return virtual_size_;
128 }
129 uint64_t file_size() const {
132 return file_size_;
133 }
134 uint64_t file_offset() const {
137 return file_offset_;
138 }
139 uint32_t max_protection() const {
142 return max_protection_;
143 }
144 uint32_t init_protection() const {
147 return init_protection_;
148 }
149 uint32_t numberof_sections() const {
152 return nb_sections_;
153 }
154 uint32_t flags() const {
157 return flags_;
158 }
159 it_sections sections() {
162 return sections_;
163 }
164
165 it_const_sections sections() const {
166 return sections_;
167 }
168 it_relocations relocations() {
175 return relocations_;
176 }
177 it_const_relocations relocations() const {
178 return relocations_;
179 }
180 const Section* get_section(const std::string& name) const;
183 Section* get_section(const std::string& name);
184 span<const uint8_t> content() const {
187 return data_;
188 }
189 std::unique_ptr<SpanStream> stream() const;
192 int8_t index() const {
195 return this->index_;
196 }
197
198 void name(std::string name) {
199 name_ = std::move(name);
200 }
201
202 void virtual_address(uint64_t virtual_address) {
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 }
214 void max_protection(uint32_t max_protection) {
215 max_protection_ = max_protection;
216 }
217 void init_protection(uint32_t init_protection) {
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
227 void content(content_t data);
228 Section& add_section(const Section& section);
231 void remove_all_sections();
234 bool has(const Section& section) const;
237 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 ||
253 type == LoadCommand::TYPE::SEGMENT_64;
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
290LIEF_API const char* to_string(SegmentCommand::FLAGS flag);
291LIEF_API const char* to_string(SegmentCommand::VM_PROTECTIONS protection);
292
293}
294}
295
296ENABLE_BITMASK_OPERATORS(LIEF::MachO::SegmentCommand::FLAGS)
297ENABLE_BITMASK_OPERATORS(LIEF::MachO::SegmentCommand::VM_PROTECTIONS)
298
299#endif
LoadCommand.hpp
LIEF::MachO::BinaryParser
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:75
LIEF::MachO::Binary
Class which represents a MachO binary.
Definition MachO/Binary.hpp:86
LIEF::MachO::Builder
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:58
LIEF::MachO::DyldChainedFixupsCreator
Definition DyldChainedFixupsCreator.hpp:41
LIEF::MachO::DyldInfo
Class that represents the LC_DYLD_INFO and LC_DYLD_INFO_ONLY commands.
Definition DyldInfo.hpp:50
LIEF::MachO::LoadCommand
Based class for the Mach-O load commands.
Definition LoadCommand.hpp:37
LIEF::MachO::LoadCommand::command
LoadCommand::TYPE command() const
Command type.
Definition LoadCommand.hpp:125
LIEF::MachO::Relocation
Class that represents a Mach-O relocation.
Definition MachO/Relocation.hpp:40
LIEF::MachO::Section
Class that represents a Mach-O section.
Definition MachO/Section.hpp:46
LIEF::MachO::SegmentCommand
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:50
LIEF::MachO::SegmentCommand::virtual_address
uint64_t virtual_address() const
Absolute virtual base address of the segment.
Definition SegmentCommand.hpp:121
LIEF::MachO::SegmentCommand::swap
void swap(SegmentCommand &other) noexcept
LIEF::MachO::SegmentCommand::file_offset
void file_offset(uint64_t file_offset)
Definition SegmentCommand.hpp:208
LIEF::MachO::SegmentCommand::print
std::ostream & print(std::ostream &os) const override
LIEF::MachO::SegmentCommand::file_size
void file_size(uint64_t file_size)
Definition SegmentCommand.hpp:211
LIEF::MachO::SegmentCommand::get_section
const Section * get_section(const std::string &name) const
Get the section with the given name.
LIEF::MachO::SegmentCommand::operator=
SegmentCommand & operator=(SegmentCommand other)
LIEF::MachO::SegmentCommand::SegmentCommand
SegmentCommand(const details::segment_command_64 &cmd)
LIEF::MachO::SegmentCommand::relocations
it_relocations relocations()
Return an iterator over the MachO::Relocation linked to this segment.
Definition SegmentCommand.hpp:174
LIEF::MachO::SegmentCommand::classof
static bool classof(const LoadCommand *cmd)
Definition SegmentCommand.hpp:250
LIEF::MachO::SegmentCommand::content
void content(content_t data)
LIEF::MachO::SegmentCommand::max_protection
void max_protection(uint32_t max_protection)
Definition SegmentCommand.hpp:214
LIEF::MachO::SegmentCommand::FLAGS
FLAGS
Definition SegmentCommand.hpp:79
LIEF::MachO::SegmentCommand::SegmentCommand
SegmentCommand(const details::segment_command_32 &cmd)
LIEF::MachO::SegmentCommand::virtual_size
void virtual_size(uint64_t virtual_size)
Definition SegmentCommand.hpp:205
LIEF::MachO::SegmentCommand::content
span< const uint8_t > content() const
The raw content of this segment.
Definition SegmentCommand.hpp:186
LIEF::MachO::SegmentCommand::get_section
Section * get_section(const std::string &name)
LIEF::MachO::SegmentCommand::name
const std::string & name() const
Name of the segment (e.g. __TEXT)
Definition SegmentCommand.hpp:116
LIEF::MachO::SegmentCommand::VM_PROTECTIONS
VM_PROTECTIONS
Values for segment_command.initprot. From <mach/vm_prot.h>
Definition SegmentCommand.hpp:89
LIEF::MachO::SegmentCommand::file_size
uint64_t file_size() const
Size of this segment in the binary file.
Definition SegmentCommand.hpp:131
LIEF::MachO::SegmentCommand::~SegmentCommand
~SegmentCommand() override
LIEF::MachO::SegmentCommand::numberof_sections
uint32_t numberof_sections() const
The number of sections associated with this segment.
Definition SegmentCommand.hpp:151
LIEF::MachO::SegmentCommand::sections
it_const_sections sections() const
Definition SegmentCommand.hpp:165
LIEF::MachO::SegmentCommand::max_protection
uint32_t max_protection() const
The maximum of protections for this segment (cf. VM_PROTECTIONS)
Definition SegmentCommand.hpp:141
LIEF::MachO::SegmentCommand::SegmentCommand
SegmentCommand()
LIEF::MachO::SegmentCommand::virtual_size
uint64_t virtual_size() const
Virtual size of the segment.
Definition SegmentCommand.hpp:126
LIEF::MachO::SegmentCommand::virtual_address
void virtual_address(uint64_t virtual_address)
Definition SegmentCommand.hpp:202
LIEF::MachO::SegmentCommand::index
int8_t index() const
The original index of this segment or -1 if not defined.
Definition SegmentCommand.hpp:194
LIEF::MachO::SegmentCommand::init_protection
uint32_t init_protection() const
The initial protections of this segment (cf. VM_PROTECTIONS)
Definition SegmentCommand.hpp:146
LIEF::MachO::SegmentCommand::add_section
Section & add_section(const Section &section)
Add a new section in this segment.
LIEF::MachO::SegmentCommand::relocations
it_const_relocations relocations() const
Definition SegmentCommand.hpp:177
LIEF::MachO::SegmentCommand::accept
void accept(Visitor &visitor) const override
LIEF::MachO::SegmentCommand::has_section
bool has_section(const std::string &section_name) const
Check if the current segment embeds the given section name.
LIEF::MachO::SegmentCommand::flags
void flags(uint32_t flags)
Definition SegmentCommand.hpp:223
LIEF::MachO::SegmentCommand::SegmentCommand
SegmentCommand(std::string name, content_t content)
LIEF::MachO::SegmentCommand::is
bool is(VM_PROTECTIONS prot) const
Definition SegmentCommand.hpp:241
LIEF::MachO::SegmentCommand::sections
it_sections sections()
Return an iterator over the MachO::Section linked to this segment.
Definition SegmentCommand.hpp:161
LIEF::MachO::SegmentCommand::file_offset
uint64_t file_offset() const
Offset of the data of this segment in the file.
Definition SegmentCommand.hpp:136
LIEF::MachO::SegmentCommand::has
bool has(const Section &section) const
Check if the current segment embeds the given section.
LIEF::MachO::SegmentCommand::SegmentCommand
SegmentCommand(const SegmentCommand &copy)
LIEF::MachO::SegmentCommand::clone
std::unique_ptr< LoadCommand > clone() const override
Definition SegmentCommand.hpp:109
LIEF::MachO::SegmentCommand::stream
std::unique_ptr< SpanStream > stream() const
Return a stream over the content of this segment.
LIEF::MachO::SegmentCommand::SegmentCommand
SegmentCommand(std::string name)
LIEF::MachO::SegmentCommand::remove_all_sections
void remove_all_sections()
Remove all the sections linked to this segment.
LIEF::MachO::SegmentCommand::name
void name(std::string name)
Definition SegmentCommand.hpp:198
LIEF::MachO::SegmentCommand::init_protection
void init_protection(uint32_t init_protection)
Definition SegmentCommand.hpp:217
LIEF::MachO::SegmentCommand::numberof_sections
void numberof_sections(uint32_t nb_section)
Definition SegmentCommand.hpp:220
LIEF::MachO::SegmentCommand::flags
uint32_t flags() const
Flags associated with this segment (cf. SegmentCommand::FLAGS)
Definition SegmentCommand.hpp:156
LIEF::SpanStream
Definition SpanStream.hpp:32
enums.hpp
ENABLE_BITMASK_OPERATORS
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
iterators.hpp
LIEF::MachO::details
Definition endianness_support.hpp:59
LIEF::MachO
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
LIEF::MachO::to_string
const char * to_string(BuildToolVersion::TOOLS tool)
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
span.hpp
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42