LIEF: Library to Instrument Executable Formats Version 0.16.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 private:
83 friend class BinaryParser;
84 friend class DyldChainedFixupsCreator;
85 chained_starts_in_segment(uint32_t offset, SegmentCommand& segment) :
86 offset(offset),
87 segment(segment)
88 {}
89 chained_starts_in_segment(uint32_t offset, const details::dyld_chained_starts_in_segment& info,
90 SegmentCommand& segment);
91 };
92 using chained_starts_in_segments_t = std::vector<chained_starts_in_segment>;
95 using it_chained_starts_in_segments_t = ref_iterator<chained_starts_in_segments_t&>;
98 using it_const_chained_starts_in_segments_t = const_ref_iterator<const chained_starts_in_segments_t&>;
101 using binding_info_t = std::vector<std::unique_ptr<ChainedBindingInfo>>;
104 using it_binding_info = ref_iterator<binding_info_t&, ChainedBindingInfo*>;
107 using it_const_binding_info = const_ref_iterator<const binding_info_t&, ChainedBindingInfo*>;
110
111
112 DyldChainedFixups();
113 DyldChainedFixups(const details::linkedit_data_command& cmd);
114 std::unique_ptr<LoadCommand> clone() const override {
115 return std::unique_ptr<DyldChainedFixups>(new DyldChainedFixups(*this));
116 }
117
118 ~DyldChainedFixups() override;
119 uint32_t data_offset() const {
123 return data_offset_;
124 }
125 uint32_t data_size() const {
128 return data_size_;
129 }
130
131 void data_offset(uint32_t offset) {
132 data_offset_ = offset;
133 }
134 void data_size(uint32_t size) {
135 data_size_ = size;
136 }
137 span<const uint8_t> payload() const {
140 return content_;
141 }
142 it_binding_info bindings() {
145 return all_bindings_;
146 }
147 it_const_binding_info bindings() const {
150 return all_bindings_;
151 }
152 it_chained_starts_in_segments_t chained_starts_in_segments() {
155 return chained_starts_in_segment_;
156 }
157
158 it_const_chained_starts_in_segments_t chained_starts_in_segments() const {
159 return chained_starts_in_segment_;
160 }
161 uint32_t fixups_version() const { return fixups_version_; }
165 void fixups_version(uint32_t version) { fixups_version_ = version; }
166 uint32_t starts_offset() const { return starts_offset_; }
169 void starts_offset(uint32_t offset) { starts_offset_ = offset; }
170 uint32_t imports_offset() const { return imports_offset_; }
173 void imports_offset(uint32_t offset) { imports_offset_ = offset; }
174 uint32_t symbols_offset() const { return symbols_offset_; }
177 void symbols_offset(uint32_t offset) { symbols_offset_ = offset; }
178 uint32_t imports_count() const { return imports_count_; }
181 void imports_count(uint32_t cnt) { imports_count_ = cnt; }
182 uint32_t symbols_format() const { return symbols_format_; }
189 void symbols_format(uint32_t fmt) { symbols_format_ = fmt; }
190 DYLD_CHAINED_FORMAT imports_format() const { return imports_format_; }
193 void imports_format(DYLD_CHAINED_FORMAT fmt) { imports_format_ = fmt; }
194
195
196 void accept(Visitor& visitor) const override;
197
198 std::ostream& print(std::ostream& os) const override;
199
200 static bool classof(const LoadCommand* cmd) {
201 return cmd->command() == LoadCommand::TYPE::DYLD_CHAINED_FIXUPS;
202 }
203
204 private:
205 LIEF_LOCAL void update_with(const details::dyld_chained_fixups_header& header);
206 LIEF_LOCAL DyldChainedFixups& operator=(const DyldChainedFixups& other);
207 LIEF_LOCAL DyldChainedFixups(const DyldChainedFixups& other);
208
209 uint32_t data_offset_ = 0;
210 uint32_t data_size_ = 0;
211
212 // Raw payload of the DyldChainedFixups.
213 // This payload is located in the __LINKEDIT segment
214 span<uint8_t> content_;
215
216 uint32_t fixups_version_ = 0;
217 uint32_t starts_offset_ = 0;
218 uint32_t imports_offset_ = 0;
219 uint32_t symbols_offset_ = 0;
220 uint32_t imports_count_ = 0;
221 uint32_t symbols_format_ = 0;
222 DYLD_CHAINED_FORMAT imports_format_ = DYLD_CHAINED_FORMAT::IMPORT;
223
224 chained_starts_in_segments_t chained_starts_in_segment_;
225
226 std::vector<std::unique_ptr<ChainedBindingInfoList>> internal_bindings_;
227 binding_info_t all_bindings_;
228};
229
230}
231}
232#endif
DyldChainedFormat.hpp
LoadCommand.hpp
LIEF::MachO::BinaryParser
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:74
LIEF::MachO::Binary
Class which represents a MachO binary.
Definition MachO/Binary.hpp:85
LIEF::MachO::BindingInfoIterator
Definition BindingInfoIterator.hpp:36
LIEF::MachO::Builder
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:57
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:122
LIEF::MachO::DyldChainedFixups::bindings
it_const_binding_info bindings() const
Iterator over the bindings (ChainedBindingInfo) associated with this command.
Definition DyldChainedFixups.hpp:149
LIEF::MachO::DyldChainedFixups::imports_count
void imports_count(uint32_t cnt)
Definition DyldChainedFixups.hpp:181
LIEF::MachO::DyldChainedFixups::starts_offset
void starts_offset(uint32_t offset)
Definition DyldChainedFixups.hpp:169
LIEF::MachO::DyldChainedFixups::bindings
it_binding_info bindings()
Iterator over the bindings (ChainedBindingInfo) associated with this command.
Definition DyldChainedFixups.hpp:144
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:154
LIEF::MachO::DyldChainedFixups::chained_starts_in_segments
it_const_chained_starts_in_segments_t chained_starts_in_segments() const
Definition DyldChainedFixups.hpp:158
LIEF::MachO::DyldChainedFixups::data_size
void data_size(uint32_t size)
Definition DyldChainedFixups.hpp:134
LIEF::MachO::DyldChainedFixups::data_size
uint32_t data_size() const
Size of the LC_DYLD_CHAINED_FIXUPS payload.
Definition DyldChainedFixups.hpp:127
LIEF::MachO::DyldChainedFixups::symbols_offset
uint32_t symbols_offset() const
Offset of symbol strings in chain data.
Definition DyldChainedFixups.hpp:176
LIEF::MachO::DyldChainedFixups::data_offset
void data_offset(uint32_t offset)
Definition DyldChainedFixups.hpp:131
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:188
LIEF::MachO::DyldChainedFixups::payload
span< const uint8_t > payload() const
Return the raw content of the command.
Definition DyldChainedFixups.hpp:139
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:164
LIEF::MachO::DyldChainedFixups::imports_offset
uint32_t imports_offset() const
Offset of imports table in chain data.
Definition DyldChainedFixups.hpp:172
LIEF::MachO::DyldChainedFixups::imports_count
uint32_t imports_count() const
Number of imported symbol names.
Definition DyldChainedFixups.hpp:180
LIEF::MachO::DyldChainedFixups::imports_format
DYLD_CHAINED_FORMAT imports_format() const
The format of the imports (ChainedBindingInfo)
Definition DyldChainedFixups.hpp:192
LIEF::MachO::DyldChainedFixups::symbols_format
void symbols_format(uint32_t fmt)
Definition DyldChainedFixups.hpp:189
LIEF::MachO::DyldChainedFixups::imports_format
void imports_format(DYLD_CHAINED_FORMAT fmt)
Definition DyldChainedFixups.hpp:193
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:114
LIEF::MachO::DyldChainedFixups::imports_offset
void imports_offset(uint32_t offset)
Definition DyldChainedFixups.hpp:173
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:165
LIEF::MachO::DyldChainedFixups::symbols_offset
void symbols_offset(uint32_t offset)
Definition DyldChainedFixups.hpp:177
LIEF::MachO::DyldChainedFixups::classof
static bool classof(const LoadCommand *cmd)
Definition DyldChainedFixups.hpp:200
LIEF::MachO::DyldChainedFixups::starts_offset
uint32_t starts_offset() const
offset of dyld_chained_starts_in_image in chain_data
Definition DyldChainedFixups.hpp:168
LIEF::MachO::DyldChainedFixups::DyldChainedFixups
DyldChainedFixups(const details::linkedit_data_command &cmd)
LIEF::MachO::LinkEdit
Definition LinkEdit.hpp:42
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:124
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::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