LIEF: Library to Instrument Executable Formats Version
Loading...
Searching...
No Matches
DyldChainedFixups.hpp
1/* Copyright 2017 - 2023 R. Thomas
2 * Copyright 2017 - 2023 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"
22#include "LIEF/MachO/LoadCommand.hpp"
23
24namespace LIEF {
25namespace MachO {
26
27class BinaryParser;
28class Builder;
29class ChainedBindingInfo;
30class ChainedBindingInfoList;
31class LinkEdit;
32class SegmentCommand;
33
34namespace details {
35struct linkedit_data_command;
36struct dyld_chained_fixups_header;
37struct dyld_chained_starts_in_segment;
38}
39
45class LIEF_API DyldChainedFixups : public LoadCommand {
46 friend class BinaryParser;
47 friend class Builder;
48 friend class LinkEdit;
49
50 public:
56 uint32_t offset = 0;
57 uint32_t size = 0;
58 uint16_t page_size = 0;
59 uint64_t segment_offset = 0;
60 uint32_t max_valid_pointer = 0;
61 DYLD_CHAINED_PTR_FORMAT pointer_format;
62
64 size_t page_count() const {
65 return page_start.size();
66 }
67
68 std::vector<uint16_t> page_start;
69 std::vector<uint16_t> chain_starts;
70
72
73 LIEF_API friend std::ostream& operator<<(std::ostream& os, const chained_starts_in_segment& info);
74
75 private:
76 friend class BinaryParser;
77 chained_starts_in_segment(uint32_t offset, SegmentCommand& segment);
78 chained_starts_in_segment(uint32_t offset, const details::dyld_chained_starts_in_segment& info,
79 SegmentCommand& segment);
80 };
81
83 using chained_starts_in_segments_t = std::vector<chained_starts_in_segment>;
84
87
90
92 using binding_info_t = std::vector<std::unique_ptr<ChainedBindingInfo>>;
93
96
99
100
102 DyldChainedFixups(const details::linkedit_data_command& cmd);
103 DyldChainedFixups* clone() const override;
104
105 ~DyldChainedFixups() override;
106
109 uint32_t data_offset() const;
110
112 uint32_t data_size() const;
113
114 void data_offset(uint32_t offset);
115 void data_size(uint32_t size);
116
119 return all_bindings_;
120 }
121
124 return all_bindings_;
125 }
126
129 return chained_starts_in_segment_;
130 }
131
132 it_const_chained_starts_in_segments_t chained_starts_in_segments() const {
133 return chained_starts_in_segment_;
134 }
135
138 uint32_t fixups_version() const { return fixups_version_; }
139 void fixups_version(uint32_t version) { fixups_version_ = version; }
140
142 uint32_t starts_offset() const { return starts_offset_; }
143 void starts_offset(uint32_t offset) { starts_offset_ = offset; }
144
146 uint32_t imports_offset() const { return imports_offset_; }
147 void imports_offset(uint32_t offset) { imports_offset_ = offset; }
148
150 uint32_t symbols_offset() const { return symbols_offset_; }
151 void symbols_offset(uint32_t offset) { symbols_offset_ = offset; }
152
154 uint32_t imports_count() const { return imports_count_; }
155 void imports_count(uint32_t cnt) { imports_count_ = cnt; }
156
162 uint32_t symbols_format() const { return symbols_format_; }
163 void symbols_format(uint32_t fmt) { symbols_format_ = fmt; }
164
166 DYLD_CHAINED_FORMAT imports_format() const { return imports_format_; }
167 void imports_format(DYLD_CHAINED_FORMAT fmt) { imports_format_ = fmt; }
168
169
170 void accept(Visitor& visitor) const override;
171
172 std::ostream& print(std::ostream& os) const override;
173
174 static bool classof(const LoadCommand* cmd);
175
176 private:
177 void update_with(const details::dyld_chained_fixups_header& header);
178 DyldChainedFixups& operator=(const DyldChainedFixups& other);
179 DyldChainedFixups(const DyldChainedFixups& other);
180
181 uint32_t data_offset_ = 0;
182 uint32_t data_size_ = 0;
183
184 // Raw payload of the DyldChainedFixups.
185 // This payload is located in the __LINKEDIT segment
186 span<uint8_t> content_;
187
188 uint32_t fixups_version_ = 0;
189 uint32_t starts_offset_ = 0;
190 uint32_t imports_offset_ = 0;
191 uint32_t symbols_offset_ = 0;
192 uint32_t imports_count_ = 0;
193 uint32_t symbols_format_ = 0;
194 DYLD_CHAINED_FORMAT imports_format_ = DYLD_CHAINED_FORMAT::IMPORT;
195
196 chained_starts_in_segments_t chained_starts_in_segment_;
197
198 std::vector<std::unique_ptr<ChainedBindingInfoList>> internal_bindings_;
199 binding_info_t all_bindings_;
200};
201
202}
203}
204#endif
Class used to parse a single binary (i.e. non-FAT)
Definition BinaryParser.hpp:73
Class used to rebuild a Mach-O file.
Definition MachO/Builder.hpp:58
Class that represents the LC_DYLD_CHAINED_FIXUPS command.
Definition DyldChainedFixups.hpp:45
uint32_t data_offset() const
Offset of the LC_DYLD_CHAINED_FIXUPS chained payload. This offset should point in the __LINKEDIT segm...
it_const_binding_info bindings() const
Iterator over the bindings (ChainedBindingInfo) associated with this command.
Definition DyldChainedFixups.hpp:123
it_binding_info bindings()
Iterator over the bindings (ChainedBindingInfo) associated with this command.
Definition DyldChainedFixups.hpp:118
it_chained_starts_in_segments_t chained_starts_in_segments()
Iterator over the chained fixup metadata.
Definition DyldChainedFixups.hpp:128
uint32_t data_size() const
Size of the LC_DYLD_CHAINED_FIXUPS payload.
uint32_t symbols_offset() const
Offset of symbol strings in chain data.
Definition DyldChainedFixups.hpp:150
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:162
std::vector< chained_starts_in_segment > chained_starts_in_segments_t
Internal container for storing chained_starts_in_segment.
Definition DyldChainedFixups.hpp:83
uint32_t fixups_version() const
Chained fixups version. The loader (dyld v852.2) checks that this value is set to 0.
Definition DyldChainedFixups.hpp:138
uint32_t imports_offset() const
Offset of imports table in chain data.
Definition DyldChainedFixups.hpp:146
uint32_t imports_count() const
Number of imported symbol names.
Definition DyldChainedFixups.hpp:154
std::vector< std::unique_ptr< ChainedBindingInfo > > binding_info_t
Internal container for storing DyldBindingInfo.
Definition DyldChainedFixups.hpp:92
DYLD_CHAINED_FORMAT imports_format() const
The format of the imports (ChainedBindingInfo)
Definition DyldChainedFixups.hpp:166
uint32_t starts_offset() const
offset of dyld_chained_starts_in_image in chain_data
Definition DyldChainedFixups.hpp:142
Definition LinkEdit.hpp:48
Based class for the Mach-O load commands.
Definition LoadCommand.hpp:39
Class which represents a LOAD_COMMAND_TYPES::LC_SEGMENT / LOAD_COMMAND_TYPES::LC_SEGMENT_64 command.
Definition SegmentCommand.hpp:48
Definition Visitor.hpp:219
Iterator which returns reference on container's values.
Definition iterators.hpp:48
LIEF namespace.
Definition Abstract/Binary.hpp:32
Structure that mirrors the raw dyld_chained_starts_in_segment which aims at providing information abo...
Definition DyldChainedFixups.hpp:55
std::vector< uint16_t > chain_starts
Currently not supported.
Definition DyldChainedFixups.hpp:69
DYLD_CHAINED_PTR_FORMAT pointer_format
How pointers are encoded.
Definition DyldChainedFixups.hpp:61
SegmentCommand & segment
Segment in which the rebase/bind fixups take place.
Definition DyldChainedFixups.hpp:71
std::vector< uint16_t > page_start
Offset in the SegmentCommand of the first element of the chain.
Definition DyldChainedFixups.hpp:68
size_t page_count() const
How many pages are in the page_start array.
Definition DyldChainedFixups.hpp:64