LIEF: Library to Instrument Executable Formats Version 0.15.1
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 BinaryParser;
29class Builder;
30class ChainedBindingInfo;
31class ChainedBindingInfoList;
32class LinkEdit;
33class SegmentCommand;
34
35namespace details {
36struct linkedit_data_command;
37struct dyld_chained_fixups_header;
38struct dyld_chained_starts_in_segment;
39}
40
47 friend class BinaryParser;
48 friend class Builder;
49 friend class LinkEdit;
50
51 public:
57 uint32_t offset = 0;
58 uint32_t size = 0;
59 uint16_t page_size = 0;
60 uint64_t segment_offset = 0;
61 uint32_t max_valid_pointer = 0;
63
65 size_t page_count() const {
66 return page_start.size();
67 }
68
69 std::vector<uint16_t> page_start;
70 std::vector<uint16_t> chain_starts;
71
73
74 LIEF_API friend std::ostream& operator<<(std::ostream& os, const chained_starts_in_segment& info);
75
76 private:
77 friend class BinaryParser;
78 chained_starts_in_segment(uint32_t offset, SegmentCommand& segment);
79 chained_starts_in_segment(uint32_t offset, const details::dyld_chained_starts_in_segment& info,
80 SegmentCommand& segment);
81 };
82
84 using chained_starts_in_segments_t = std::vector<chained_starts_in_segment>;
85
88
91
93 using binding_info_t = std::vector<std::unique_ptr<ChainedBindingInfo>>;
94
97
100
101
103 DyldChainedFixups(const details::linkedit_data_command& cmd);
104 std::unique_ptr<LoadCommand> clone() const override {
105 return std::unique_ptr<DyldChainedFixups>(new DyldChainedFixups(*this));
106 }
107
109
112 uint32_t data_offset() const {
113 return data_offset_;
114 }
115
117 uint32_t data_size() const {
118 return data_size_;
119 }
120
121 void data_offset(uint32_t offset) {
122 data_offset_ = offset;
123 }
124 void data_size(uint32_t size) {
125 data_size_ = size;
126 }
127
130 return all_bindings_;
131 }
132
135 return all_bindings_;
136 }
137
140 return chained_starts_in_segment_;
141 }
142
144 return chained_starts_in_segment_;
145 }
146
149 uint32_t fixups_version() const { return fixups_version_; }
150 void fixups_version(uint32_t version) { fixups_version_ = version; }
151
153 uint32_t starts_offset() const { return starts_offset_; }
154 void starts_offset(uint32_t offset) { starts_offset_ = offset; }
155
157 uint32_t imports_offset() const { return imports_offset_; }
158 void imports_offset(uint32_t offset) { imports_offset_ = offset; }
159
161 uint32_t symbols_offset() const { return symbols_offset_; }
162 void symbols_offset(uint32_t offset) { symbols_offset_ = offset; }
163
165 uint32_t imports_count() const { return imports_count_; }
166 void imports_count(uint32_t cnt) { imports_count_ = cnt; }
167
173 uint32_t symbols_format() const { return symbols_format_; }
174 void symbols_format(uint32_t fmt) { symbols_format_ = fmt; }
175
177 DYLD_CHAINED_FORMAT imports_format() const { return imports_format_; }
178 void imports_format(DYLD_CHAINED_FORMAT fmt) { imports_format_ = fmt; }
179
180
181 void accept(Visitor& visitor) const override;
182
183 std::ostream& print(std::ostream& os) const override;
184
185 static bool classof(const LoadCommand* cmd) {
186 return cmd->command() == LoadCommand::TYPE::DYLD_CHAINED_FIXUPS;
187 }
188
189 private:
190 void update_with(const details::dyld_chained_fixups_header& header);
191 DyldChainedFixups& operator=(const DyldChainedFixups& other);
193
194 uint32_t data_offset_ = 0;
195 uint32_t data_size_ = 0;
196
197 // Raw payload of the DyldChainedFixups.
198 // This payload is located in the __LINKEDIT segment
199 span<uint8_t> content_;
200
201 uint32_t fixups_version_ = 0;
202 uint32_t starts_offset_ = 0;
203 uint32_t imports_offset_ = 0;
204 uint32_t symbols_offset_ = 0;
205 uint32_t imports_count_ = 0;
206 uint32_t symbols_format_ = 0;
207 DYLD_CHAINED_FORMAT imports_format_ = DYLD_CHAINED_FORMAT::IMPORT;
208
209 chained_starts_in_segments_t chained_starts_in_segment_;
210
211 std::vector<std::unique_ptr<ChainedBindingInfoList>> internal_bindings_;
212 binding_info_t all_bindings_;
213};
214
215}
216}
217#endif
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:74
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:55
Class that represents the LC_DYLD_CHAINED_FIXUPS command.
Definition DyldChainedFixups.hpp:46
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:112
it_const_binding_info bindings() const
Iterator over the bindings (ChainedBindingInfo) associated with this command.
Definition DyldChainedFixups.hpp:134
void imports_count(uint32_t cnt)
Definition DyldChainedFixups.hpp:166
void starts_offset(uint32_t offset)
Definition DyldChainedFixups.hpp:154
it_binding_info bindings()
Iterator over the bindings (ChainedBindingInfo) associated with this command.
Definition DyldChainedFixups.hpp:129
it_chained_starts_in_segments_t chained_starts_in_segments()
Iterator over the chained fixup metadata.
Definition DyldChainedFixups.hpp:139
it_const_chained_starts_in_segments_t chained_starts_in_segments() const
Definition DyldChainedFixups.hpp:143
void data_size(uint32_t size)
Definition DyldChainedFixups.hpp:124
uint32_t data_size() const
Size of the LC_DYLD_CHAINED_FIXUPS payload.
Definition DyldChainedFixups.hpp:117
uint32_t symbols_offset() const
Offset of symbol strings in chain data.
Definition DyldChainedFixups.hpp:161
void data_offset(uint32_t offset)
Definition DyldChainedFixups.hpp:121
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:173
std::vector< chained_starts_in_segment > chained_starts_in_segments_t
Internal container for storing chained_starts_in_segment.
Definition DyldChainedFixups.hpp:84
uint32_t fixups_version() const
Chained fixups version. The loader (dyld v852.2) checks that this value is set to 0.
Definition DyldChainedFixups.hpp:149
uint32_t imports_offset() const
Offset of imports table in chain data.
Definition DyldChainedFixups.hpp:157
uint32_t imports_count() const
Number of imported symbol names.
Definition DyldChainedFixups.hpp:165
DYLD_CHAINED_FORMAT imports_format() const
The format of the imports (ChainedBindingInfo)
Definition DyldChainedFixups.hpp:177
void symbols_format(uint32_t fmt)
Definition DyldChainedFixups.hpp:174
void imports_format(DYLD_CHAINED_FORMAT fmt)
Definition DyldChainedFixups.hpp:178
void accept(Visitor &visitor) const override
std::unique_ptr< LoadCommand > clone() const override
Definition DyldChainedFixups.hpp:104
void imports_offset(uint32_t offset)
Definition DyldChainedFixups.hpp:158
std::ostream & print(std::ostream &os) const override
void fixups_version(uint32_t version)
Definition DyldChainedFixups.hpp:150
void symbols_offset(uint32_t offset)
Definition DyldChainedFixups.hpp:162
std::vector< std::unique_ptr< ChainedBindingInfo > > binding_info_t
Internal container for storing DyldBindingInfo.
Definition DyldChainedFixups.hpp:93
static bool classof(const LoadCommand *cmd)
Definition DyldChainedFixups.hpp:185
uint32_t starts_offset() const
offset of dyld_chained_starts_in_image in chain_data
Definition DyldChainedFixups.hpp:153
DyldChainedFixups(const details::linkedit_data_command &cmd)
Definition LinkEdit.hpp:42
Based class for the Mach-O load commands.
Definition LoadCommand.hpp:36
LoadCommand::TYPE command() const
Command type.
Definition LoadCommand.hpp:122
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:48
Definition Visitor.hpp:224
Iterator which returns reference on container's values.
Definition iterators.hpp:48
DYLD_CHAINED_FORMAT
Definition DyldChainedFormat.hpp:22
DYLD_CHAINED_PTR_FORMAT
Definition DyldChainedFormat.hpp:29
LIEF namespace.
Definition Abstract/Binary.hpp:32
tcb::span< ElementType, Extent > span
Definition span.hpp:22
Structure that mirrors the raw dyld_chained_starts_in_segment which aims at providing information abo...
Definition DyldChainedFixups.hpp:56
std::vector< uint16_t > chain_starts
Currently not supported.
Definition DyldChainedFixups.hpp:70
DYLD_CHAINED_PTR_FORMAT pointer_format
How pointers are encoded.
Definition DyldChainedFixups.hpp:62
friend std::ostream & operator<<(std::ostream &os, const chained_starts_in_segment &info)
SegmentCommand & segment
Segment in which the rebase/bind fixups take place.
Definition DyldChainedFixups.hpp:72
std::vector< uint16_t > page_start
Offset in the SegmentCommand of the first element of the chain.
Definition DyldChainedFixups.hpp:69
size_t page_count() const
How many pages are in the page_start array.
Definition DyldChainedFixups.hpp:65
#define LIEF_API
Definition visibility.h:41