LIEF: Library to Instrument Executable Formats Version 0.17.0
Loading...
Searching...
No Matches
DyldChainedFixups.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_DYLD_CHAINED_FIXUPS_H
17#define LIEF_MACHO_DYLD_CHAINED_FIXUPS_H
18#include <memory>
19#include "LIEF/span.hpp"
20#include "LIEF/iterators.hpp"
21#include "LIEF/visibility.h"
24
25namespace LIEF {
26namespace MachO {
27
28class Binary;
29class BinaryParser;
31class Builder;
33class ChainedBindingInfoList;
35class LinkEdit;
36class SegmentCommand;
37
38namespace details {
39struct linkedit_data_command;
40struct dyld_chained_fixups_header;
41struct dyld_chained_starts_in_segment;
42}
50 friend class BinaryParser;
51 friend class Builder;
52 friend class LinkEdit;
53 friend class BindingInfoIterator;
54 friend class Binary;
55 friend class DyldChainedFixupsCreator;
56
57 public: struct chained_starts_in_segment {
63 uint32_t offset = 0;
64 uint32_t size = 0;
65 uint16_t page_size = 0;
66 uint64_t segment_offset = 0;
67 uint32_t max_valid_pointer = 0;
68 DYLD_CHAINED_PTR_FORMAT pointer_format = DYLD_CHAINED_PTR_FORMAT::NONE;
69 size_t page_count() const {
72 return page_start.size();
73 }
74
75 std::vector<uint16_t> page_start;
76 std::vector<uint16_t> chain_starts;
77
78 SegmentCommand& segment;
79
80 LIEF_API friend std::ostream& operator<<(std::ostream& os, const chained_starts_in_segment& info);
81
82 static chained_starts_in_segment create_empty_chained(SegmentCommand& segment) {
83 return chained_starts_in_segment(0, segment);
84 }
85
86 private:
87 friend class BinaryParser;
88 friend class DyldChainedFixupsCreator;
89 chained_starts_in_segment(uint32_t offset, SegmentCommand& segment) :
90 offset(offset),
91 segment(segment)
92 {}
93 chained_starts_in_segment(uint32_t offset, const details::dyld_chained_starts_in_segment& info,
94 SegmentCommand& segment);
95 };
96 using chained_starts_in_segments_t = std::vector<chained_starts_in_segment>;
99 using it_chained_starts_in_segments_t = ref_iterator<chained_starts_in_segments_t&>;
102 using it_const_chained_starts_in_segments_t = const_ref_iterator<const chained_starts_in_segments_t&>;
105 using binding_info_t = std::vector<std::unique_ptr<ChainedBindingInfo>>;
108 using it_binding_info = ref_iterator<binding_info_t&, ChainedBindingInfo*>;
111 using it_const_binding_info = const_ref_iterator<const binding_info_t&, ChainedBindingInfo*>;
114
115
116 DyldChainedFixups();
117 DyldChainedFixups(const details::linkedit_data_command& cmd);
118 std::unique_ptr<LoadCommand> clone() const override {
119 return std::unique_ptr<DyldChainedFixups>(new DyldChainedFixups(*this));
120 }
121
122 ~DyldChainedFixups() override;
123 uint32_t data_offset() const {
127 return data_offset_;
128 }
129 uint32_t data_size() const {
132 return data_size_;
133 }
134
135 void data_offset(uint32_t offset) {
136 data_offset_ = offset;
137 }
138 void data_size(uint32_t size) {
139 data_size_ = size;
140 }
141 span<const uint8_t> payload() const {
144 return content_;
145 }
146 it_binding_info bindings() {
149 return all_bindings_;
150 }
151 it_const_binding_info bindings() const {
154 return all_bindings_;
155 }
156 it_chained_starts_in_segments_t chained_starts_in_segments() {
159 return chained_starts_in_segment_;
160 }
161
162 it_const_chained_starts_in_segments_t chained_starts_in_segments() const {
163 return chained_starts_in_segment_;
164 }
165 uint32_t fixups_version() const { return fixups_version_; }
169 void fixups_version(uint32_t version) { fixups_version_ = version; }
170 uint32_t starts_offset() const { return starts_offset_; }
173 void starts_offset(uint32_t offset) { starts_offset_ = offset; }
174 uint32_t imports_offset() const { return imports_offset_; }
177 void imports_offset(uint32_t offset) { imports_offset_ = offset; }
178 uint32_t symbols_offset() const { return symbols_offset_; }
181 void symbols_offset(uint32_t offset) { symbols_offset_ = offset; }
182 uint32_t imports_count() const { return imports_count_; }
185 void imports_count(uint32_t cnt) { imports_count_ = cnt; }
186 uint32_t symbols_format() const { return symbols_format_; }
193 void symbols_format(uint32_t fmt) { symbols_format_ = fmt; }
194 DYLD_CHAINED_FORMAT imports_format() const { return imports_format_; }
197 void imports_format(DYLD_CHAINED_FORMAT fmt) { imports_format_ = fmt; }
198
199 chained_starts_in_segment& add(chained_starts_in_segment start_info) {
200 chained_starts_in_segment_.push_back(std::move(start_info));
201 return chained_starts_in_segment_.back();
202 }
203
204 void accept(Visitor& visitor) const override;
205
206 std::ostream& print(std::ostream& os) const override;
207
208 static bool classof(const LoadCommand* cmd) {
209 return cmd->command() == LoadCommand::TYPE::DYLD_CHAINED_FIXUPS;
210 }
211
212 private:
213 LIEF_LOCAL void update_with(const details::dyld_chained_fixups_header& header);
214 LIEF_LOCAL DyldChainedFixups& operator=(const DyldChainedFixups& other);
215 LIEF_LOCAL DyldChainedFixups(const DyldChainedFixups& other);
216
217 uint32_t data_offset_ = 0;
218 uint32_t data_size_ = 0;
219
220 // Raw payload of the DyldChainedFixups.
221 // This payload is located in the __LINKEDIT segment
222 span<uint8_t> content_;
223
224 uint32_t fixups_version_ = 0;
225 uint32_t starts_offset_ = 0;
226 uint32_t imports_offset_ = 0;
227 uint32_t symbols_offset_ = 0;
228 uint32_t imports_count_ = 0;
229 uint32_t symbols_format_ = 0;
230 DYLD_CHAINED_FORMAT imports_format_ = DYLD_CHAINED_FORMAT::IMPORT;
231
232 chained_starts_in_segments_t chained_starts_in_segment_;
233
234 std::vector<std::unique_ptr<ChainedBindingInfoList>> internal_bindings_;
235 binding_info_t all_bindings_;
236};
237
238}
239}
240#endif
DyldChainedFormat.hpp
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::BindingInfoIterator
Definition BindingInfoIterator.hpp:36
LIEF::MachO::Builder
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:58
LIEF::MachO::ChainedBindingInfo
This class represents a symbol binding operation associated with the LC_DYLD_CHAINED_FIXUPS command.
Definition ChainedBindingInfo.hpp:48
LIEF::MachO::DyldChainedFixupsCreator
Definition DyldChainedFixupsCreator.hpp:41
LIEF::MachO::DyldChainedFixups
Class that represents the LC_DYLD_CHAINED_FIXUPS command.
Definition DyldChainedFixups.hpp:49
LIEF::MachO::DyldChainedFixups::data_offset
uint32_t data_offset() const
Offset of the LC_DYLD_CHAINED_FIXUPS chained payload. This offset should point in the __LINKEDIT segm...
Definition DyldChainedFixups.hpp:126
LIEF::MachO::DyldChainedFixups::bindings
it_const_binding_info bindings() const
Iterator over the bindings (ChainedBindingInfo) associated with this command.
Definition DyldChainedFixups.hpp:153
LIEF::MachO::DyldChainedFixups::imports_count
void imports_count(uint32_t cnt)
Definition DyldChainedFixups.hpp:185
LIEF::MachO::DyldChainedFixups::starts_offset
void starts_offset(uint32_t offset)
Definition DyldChainedFixups.hpp:173
LIEF::MachO::DyldChainedFixups::bindings
it_binding_info bindings()
Iterator over the bindings (ChainedBindingInfo) associated with this command.
Definition DyldChainedFixups.hpp:148
LIEF::MachO::DyldChainedFixups::chained_starts_in_segments
it_chained_starts_in_segments_t chained_starts_in_segments()
Iterator over the chained fixup metadata.
Definition DyldChainedFixups.hpp:158
LIEF::MachO::DyldChainedFixups::chained_starts_in_segments
it_const_chained_starts_in_segments_t chained_starts_in_segments() const
Definition DyldChainedFixups.hpp:162
LIEF::MachO::DyldChainedFixups::data_size
void data_size(uint32_t size)
Definition DyldChainedFixups.hpp:138
LIEF::MachO::DyldChainedFixups::data_size
uint32_t data_size() const
Size of the LC_DYLD_CHAINED_FIXUPS payload.
Definition DyldChainedFixups.hpp:131
LIEF::MachO::DyldChainedFixups::symbols_offset
uint32_t symbols_offset() const
Offset of symbol strings in chain data.
Definition DyldChainedFixups.hpp:180
LIEF::MachO::DyldChainedFixups::add
chained_starts_in_segment & add(chained_starts_in_segment start_info)
Definition DyldChainedFixups.hpp:199
LIEF::MachO::DyldChainedFixups::data_offset
void data_offset(uint32_t offset)
Definition DyldChainedFixups.hpp:135
LIEF::MachO::DyldChainedFixups::symbols_format
uint32_t symbols_format() const
The compression algorithm (if any) used to store the symbols 0 means uncompressed while 1 means zlib ...
Definition DyldChainedFixups.hpp:192
LIEF::MachO::DyldChainedFixups::payload
span< const uint8_t > payload() const
Return the raw content of the command.
Definition DyldChainedFixups.hpp:143
LIEF::MachO::DyldChainedFixups::DyldChainedFixups
DyldChainedFixups()
LIEF::MachO::DyldChainedFixups::fixups_version
uint32_t fixups_version() const
Chained fixups version. The loader (dyld v852.2) checks that this value is set to 0.
Definition DyldChainedFixups.hpp:168
LIEF::MachO::DyldChainedFixups::imports_offset
uint32_t imports_offset() const
Offset of imports table in chain data.
Definition DyldChainedFixups.hpp:176
LIEF::MachO::DyldChainedFixups::imports_count
uint32_t imports_count() const
Number of imported symbol names.
Definition DyldChainedFixups.hpp:184
LIEF::MachO::DyldChainedFixups::imports_format
DYLD_CHAINED_FORMAT imports_format() const
The format of the imports (ChainedBindingInfo)
Definition DyldChainedFixups.hpp:196
LIEF::MachO::DyldChainedFixups::symbols_format
void symbols_format(uint32_t fmt)
Definition DyldChainedFixups.hpp:193
LIEF::MachO::DyldChainedFixups::imports_format
void imports_format(DYLD_CHAINED_FORMAT fmt)
Definition DyldChainedFixups.hpp:197
LIEF::MachO::DyldChainedFixups::~DyldChainedFixups
~DyldChainedFixups() override
LIEF::MachO::DyldChainedFixups::accept
void accept(Visitor &visitor) const override
LIEF::MachO::DyldChainedFixups::clone
std::unique_ptr< LoadCommand > clone() const override
Definition DyldChainedFixups.hpp:118
LIEF::MachO::DyldChainedFixups::imports_offset
void imports_offset(uint32_t offset)
Definition DyldChainedFixups.hpp:177
LIEF::MachO::DyldChainedFixups::print
std::ostream & print(std::ostream &os) const override
LIEF::MachO::DyldChainedFixups::fixups_version
void fixups_version(uint32_t version)
Definition DyldChainedFixups.hpp:169
LIEF::MachO::DyldChainedFixups::symbols_offset
void symbols_offset(uint32_t offset)
Definition DyldChainedFixups.hpp:181
LIEF::MachO::DyldChainedFixups::classof
static bool classof(const LoadCommand *cmd)
Definition DyldChainedFixups.hpp:208
LIEF::MachO::DyldChainedFixups::starts_offset
uint32_t starts_offset() const
offset of dyld_chained_starts_in_image in chain_data
Definition DyldChainedFixups.hpp:172
LIEF::MachO::DyldChainedFixups::DyldChainedFixups
DyldChainedFixups(const details::linkedit_data_command &cmd)
LIEF::MachO::LinkEdit
Definition LinkEdit.hpp:43
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::SegmentCommand
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:50
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::DYLD_CHAINED_FORMAT
DYLD_CHAINED_FORMAT
Definition DyldChainedFormat.hpp:22
LIEF::MachO::DYLD_CHAINED_PTR_FORMAT
DYLD_CHAINED_PTR_FORMAT
Definition DyldChainedFormat.hpp:29
LIEF
LIEF namespace.
Definition Abstract/Binary.hpp:36
span.hpp
LIEF::MachO::DyldChainedFixups::chained_starts_in_segment::operator<<
friend std::ostream & operator<<(std::ostream &os, const chained_starts_in_segment &info)
LIEF::MachO::DyldChainedFixups::chained_starts_in_segment::create_empty_chained
static chained_starts_in_segment create_empty_chained(SegmentCommand &segment)
Definition DyldChainedFixups.hpp:82
LIEF::MachO::DyldChainedFixups::chained_starts_in_segment::page_count
size_t page_count() const
How many pages are in the page_start array.
Definition DyldChainedFixups.hpp:71
visibility.h
LIEF_API
#define LIEF_API
Definition visibility.h:41
LIEF_LOCAL
#define LIEF_LOCAL
Definition visibility.h:42