16#ifndef LIEF_MACHO_SEGMENT_COMMAND_H
17#define LIEF_MACHO_SEGMENT_COMMAND_H
24#include "LIEF/enums.hpp"
25#include "LIEF/span.hpp"
26#include "LIEF/visibility.h"
28#include "LIEF/iterators.hpp"
29#include "LIEF/MachO/LoadCommand.hpp"
43struct segment_command_32;
44struct segment_command_64;
56 using content_t = std::vector<uint8_t>;
59 using sections_t = std::vector<std::unique_ptr<Section>>;
80 PROTECTED_VERSION_1 = 0x8u,
106 std::unique_ptr<LoadCommand> clone()
const override {
107 return std::unique_ptr<SegmentCommand>(
new SegmentCommand(*
this));
110 ~SegmentCommand()
override;
113 const std::string&
name()
const {
119 return virtual_address_;
124 return virtual_size_;
139 return max_protection_;
144 return init_protection_;
162 it_const_sections sections()
const {
174 it_const_relocations relocations()
const {
180 Section* get_section(
const std::string& name);
192 void name(std::string name) {
193 name_ = std::move(name);
196 void virtual_address(uint64_t virtual_address) {
197 virtual_address_ = virtual_address;
199 void virtual_size(uint64_t virtual_size) {
200 virtual_size_ = virtual_size;
202 void file_offset(uint64_t file_offset) {
203 file_offset_ = file_offset;
205 void file_size(uint64_t file_size) {
206 file_size_ = file_size;
208 void max_protection(uint32_t max_protection) {
209 max_protection_ = max_protection;
211 void init_protection(uint32_t init_protection) {
212 init_protection_ = init_protection;
214 void numberof_sections(uint32_t nb_section) {
215 nb_sections_ = nb_section;
217 void flags(uint32_t flags) {
221 void content(content_t data);
235 std::ostream& print(std::ostream& os)
const override;
237 void accept(
Visitor& visitor)
const override;
240 const LoadCommand::TYPE type = cmd->
command();
241 return type == LoadCommand::TYPE::SEGMENT ||
242 type == LoadCommand::TYPE::SEGMENT_64;
246 span<uint8_t> writable_content() {
250 void content_resize(
size_t size);
251 void content_insert(
size_t where,
size_t size);
253 void content_extend(
size_t width) {
254 content_resize(data_.size() + width);
257 using update_fnc_t = std::function<void(std::vector<uint8_t>&)>;
258 using update_fnc_ws_t = std::function<void(std::vector<uint8_t>&,
size_t,
size_t)>;
260 LIEF_LOCAL
virtual void update_data(
const update_fnc_t& f);
261 LIEF_LOCAL
virtual void update_data(
const update_fnc_ws_t& f,
262 size_t where,
size_t size);
265 uint64_t virtual_address_ = 0;
266 uint64_t virtual_size_ = 0;
267 uint64_t file_offset_ = 0;
268 uint64_t file_size_ = 0;
269 uint32_t max_protection_ = 0;
270 uint32_t init_protection_ = 0;
271 uint32_t nb_sections_ = 0;
275 sections_t sections_;
276 relocations_t relocations_;
286ENABLE_BITMASK_OPERATORS(
LIEF::MachO::SegmentCommand::VM_PROTECTIONS)
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:73
Class which represents a MachO binary.
Definition MachO/Binary.hpp:73
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:54
Based class for the Mach-O load commands.
Definition LoadCommand.hpp:36
LoadCommand::TYPE command() const
Command type.
Definition LoadCommand.hpp:120
Class that represents a Mach-O section.
Definition MachO/Section.hpp:44
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:48
uint64_t virtual_address() const
Absolute virtual base address of the segment.
Definition SegmentCommand.hpp:118
std::vector< std::unique_ptr< Relocation > > relocations_t
Internal container for storing Mach-O Relocation.
Definition SegmentCommand.hpp:68
const Section * get_section(const std::string &name) const
Get the section with the given name.
it_relocations relocations()
Return an iterator over the MachO::Relocation linked to this segment.
Definition SegmentCommand.hpp:171
std::vector< std::unique_ptr< Section > > sections_t
Internal container for storing Mach-O Section.
Definition SegmentCommand.hpp:59
FLAGS
Definition SegmentCommand.hpp:76
span< const uint8_t > content() const
The raw content of this segment.
Definition SegmentCommand.hpp:183
const std::string & name() const
Name of the segment (e.g. __TEXT)
Definition SegmentCommand.hpp:113
VM_PROTECTIONS
Values for segment_command.initprot. From <mach/vm_prot.h>
Definition SegmentCommand.hpp:86
uint64_t file_size() const
Size of this segment in the binary file.
Definition SegmentCommand.hpp:128
uint32_t numberof_sections() const
The number of sections associated with this segment.
Definition SegmentCommand.hpp:148
uint32_t max_protection() const
The maximum of protections for this segment (cf. VM_PROTECTIONS)
Definition SegmentCommand.hpp:138
uint64_t virtual_size() const
Virtual size of the segment.
Definition SegmentCommand.hpp:123
int8_t index() const
The original index of this segment or -1 if not defined.
Definition SegmentCommand.hpp:188
uint32_t init_protection() const
The initial protections of this segment (cf. VM_PROTECTIONS)
Definition SegmentCommand.hpp:143
Section & add_section(const Section §ion)
Add a new section in this segment.
bool has_section(const std::string §ion_name) const
Check if the current segment embeds the given section name.
it_sections sections()
Return an iterator over the MachO::Section linked to this segment.
Definition SegmentCommand.hpp:158
uint64_t file_offset() const
Offset of the data of this segment in the file.
Definition SegmentCommand.hpp:133
bool has(const Section §ion) const
Check if the current segment embeds the given section.
void remove_all_sections()
Remove all the sections linked to this segment.
uint32_t flags() const
Flags associated with this segment (cf. SegmentCommand::FLAGS)
Definition SegmentCommand.hpp:153
Definition Visitor.hpp:219
Iterator which returns reference on container's values.
Definition iterators.hpp:48
LIEF namespace.
Definition Abstract/Binary.hpp:32