LIEF: Library to Instrument Executable Formats Version 0.15.0
Loading...
Searching...
No Matches
SegmentCommand.hpp
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"
29#include "LIEF/MachO/LoadCommand.hpp"
30
31
32namespace LIEF {
33namespace MachO {
34
35class BinaryParser;
36class Binary;
37class Builder;
38class Section;
39class Relocation;
40class DyldInfo;
41
42namespace details {
43struct segment_command_32;
44struct segment_command_64;
45}
46
48class LIEF_API SegmentCommand : public LoadCommand {
49
50 friend class BinaryParser;
51 friend class Binary;
52 friend class Section;
53 friend class Builder;
54
55 public:
56 using content_t = std::vector<uint8_t>;
57
59 using sections_t = std::vector<std::unique_ptr<Section>>;
60
63
66
68 using relocations_t = std::vector<std::unique_ptr<Relocation>>;
69
72
75
76 enum class FLAGS: uint64_t {
77 HIGHVM = 0x1u,
78 FVMLIB = 0x2u,
79 NORELOC = 0x4u,
80 PROTECTED_VERSION_1 = 0x8u,
81 READ_ONLY = 0x10u,
82 };
83
86 enum class VM_PROTECTIONS {
87 READ = 0x1,
88 WRITE = 0x2,
89 EXECUTE = 0x4,
90 };
91
92 public:
94 SegmentCommand(const details::segment_command_32& cmd);
95 SegmentCommand(const details::segment_command_64& cmd);
96
97 SegmentCommand& operator=(SegmentCommand other);
98 SegmentCommand(const SegmentCommand& copy);
99
100 SegmentCommand(std::string name, content_t content);
101
102 SegmentCommand(std::string name);
103
104 void swap(SegmentCommand& other) noexcept;
105
106 std::unique_ptr<LoadCommand> clone() const override {
107 return std::unique_ptr<SegmentCommand>(new SegmentCommand(*this));
108 }
109
110 ~SegmentCommand() override;
111
113 const std::string& name() const {
114 return name_;
115 }
116
118 uint64_t virtual_address() const {
119 return virtual_address_;
120 }
121
123 uint64_t virtual_size() const {
124 return virtual_size_;
125 }
126
128 uint64_t file_size() const {
129 return file_size_;
130 }
131
133 uint64_t file_offset() const {
134 return file_offset_;
135 }
136
138 uint32_t max_protection() const {
139 return max_protection_;
140 }
141
143 uint32_t init_protection() const {
144 return init_protection_;
145 }
146
148 uint32_t numberof_sections() const {
149 return nb_sections_;
150 }
151
153 uint32_t flags() const {
154 return flags_;
155 }
156
159 return sections_;
160 }
161
162 it_const_sections sections() const {
163 return sections_;
164 }
165
172 return relocations_;
173 }
174 it_const_relocations relocations() const {
175 return relocations_;
176 }
177
179 const Section* get_section(const std::string& name) const;
180 Section* get_section(const std::string& name);
181
183 span<const uint8_t> content() const {
184 return data_;
185 }
186
188 int8_t index() const {
189 return this->index_;
190 }
191
192 void name(std::string name) {
193 name_ = std::move(name);
194 }
195
196 void virtual_address(uint64_t virtual_address) {
197 virtual_address_ = virtual_address;
198 }
199 void virtual_size(uint64_t virtual_size) {
200 virtual_size_ = virtual_size;
201 }
202 void file_offset(uint64_t file_offset) {
203 file_offset_ = file_offset;
204 }
205 void file_size(uint64_t file_size) {
206 file_size_ = file_size;
207 }
208 void max_protection(uint32_t max_protection) {
209 max_protection_ = max_protection;
210 }
211 void init_protection(uint32_t init_protection) {
212 init_protection_ = init_protection;
213 }
214 void numberof_sections(uint32_t nb_section) {
215 nb_sections_ = nb_section;
216 }
217 void flags(uint32_t flags) {
218 flags_ = flags;
219 }
220
221 void content(content_t data);
222
224 Section& add_section(const Section& section);
225
228
230 bool has(const Section& section) const;
231
233 bool has_section(const std::string& section_name) const;
234
235 std::ostream& print(std::ostream& os) const override;
236
237 void accept(Visitor& visitor) const override;
238
239 static bool classof(const LoadCommand* cmd) {
240 const LoadCommand::TYPE type = cmd->command();
241 return type == LoadCommand::TYPE::SEGMENT ||
242 type == LoadCommand::TYPE::SEGMENT_64;
243 }
244
245 protected:
246 span<uint8_t> writable_content() {
247 return data_;
248 }
249
250 void content_resize(size_t size);
251 void content_insert(size_t where, size_t size);
252
253 void content_extend(size_t width) {
254 content_resize(data_.size() + width);
255 }
256
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)>;
259
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);
263
264 std::string name_;
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;
272 uint32_t flags_ = 0;
273 int8_t index_ = -1;
274 content_t data_;
275 sections_t sections_;
276 relocations_t relocations_;
277};
278
279LIEF_API const char* to_string(SegmentCommand::FLAGS flag);
280LIEF_API const char* to_string(SegmentCommand::VM_PROTECTIONS protection);
281
282}
283}
284
285ENABLE_BITMASK_OPERATORS(LIEF::MachO::SegmentCommand::FLAGS)
286ENABLE_BITMASK_OPERATORS(LIEF::MachO::SegmentCommand::VM_PROTECTIONS)
287
288#endif
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 &section)
Add a new section in this segment.
bool has_section(const std::string &section_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 &section) 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