LIEF: Library to Instrument Executable Formats Version 0.16.0
Loading...
Searching...
No Matches
DyldChainedFixupsCreator.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_CREATOR_H
17#define LIEF_MACHO_DYLD_CHAINED_FIXUPS_CREATOR_H
18#include "LIEF/visibility.h"
21#include "LIEF/errors.hpp"
22
23#include <vector>
24#include <string>
25#include <unordered_map>
26#include <cstdint>
27
28namespace LIEF {
29namespace MachO {
30class RelocationFixup;
31class Symbol;
32class Binary;
34class SegmentCommand;
35class ChainedBindingInfoList;
36
37class strong_map_t;
38
39// This class aims to provide facilities to (re)create the LC_DYLD_CHAINED_FIXUPS
40// command.
42 public:
43 static constexpr uint32_t MAX_IMPORTS = (uint32_t(1) << 24) - 1;
44 static constexpr uint32_t BIND24_THRESHOLD = (uint32_t(1) << 16) - 1;
45 struct binding_info_t {
46 uint64_t address = 0;
47 std::string library;
48 std::string symbol;
49 bool weak;
50 uint64_t addend;
51 };
52
53 struct reloc_info_t {
54 uint64_t addr = 0;
55 uint64_t target = 0;
56 };
58
59 DyldChainedFixupsCreator& fixups_version(uint32_t value) {
60 fixups_version_ = value;
61 return *this;
62 }
63
64 DyldChainedFixupsCreator& imports_format(DYLD_CHAINED_FORMAT fmt) {
65 imports_format_ = fmt;
66 return *this;
67 }
68
69 DyldChainedFixupsCreator& add_relocation(uint64_t address, uint64_t target);
70
71 DyldChainedFixupsCreator&
72 add_relocations(const std::vector<reloc_info_t>& relocations)
73 {
74 std::copy(relocations.begin(), relocations.end(),
75 std::back_inserter(relocations_));
76 return *this;
77 }
78
79 DyldChainedFixupsCreator& add_binding(uint64_t address,
80 std::string symbol, std::string library,
81 uint64_t addend = 0, bool weak = false);
82
83 DyldChainedFixupsCreator& add_binding(uint64_t address,
84 std::string symbol,
85 uint64_t addend = 0, bool weak = false)
86 {
87 return add_binding(address, std::move(symbol), "", addend, weak);
88 }
89
90 DyldChainedFixupsCreator& add_bindings(const std::vector<binding_info_t>& bindings) {
91 std::copy(bindings.begin(), bindings.end(), std::back_inserter(bindings_));
92 return *this;
93 }
94
95 DyldChainedFixups* create(Binary& target);
96
97 private:
98 struct LIEF_LOCAL binding_rebase_t {
99 enum TYPE {
100 UNKNOWN = 0,
101 FIXUP, BINDING,
102 };
103 union {
104 ChainedBindingInfo* binding = nullptr;
105 RelocationFixup* fixup;
106 };
107 TYPE type = UNKNOWN;
108
109 binding_rebase_t(ChainedBindingInfo& info) :
110 binding(&info),
111 type(BINDING)
112 {}
113
114 binding_rebase_t(RelocationFixup& fixup) :
115 fixup(&fixup),
116 type(FIXUP)
117 {}
118
119 uint64_t addr() const;
120
121 bool is_binding() const {
122 return type == BINDING;
123 }
124
125 bool is_fixup() const {
126 return type == FIXUP;
127 }
128
129 friend bool operator<(const binding_rebase_t& lhs,
130 const binding_rebase_t& rhs) {
131 return lhs.addr() < rhs.addr();
132 }
133
134 friend bool operator>(const binding_rebase_t& lhs,
135 const binding_rebase_t& rhs) {
136 return lhs.addr() > rhs.addr();
137 }
138
139 friend bool operator>=(const binding_rebase_t& lhs,
140 const binding_rebase_t& rhs) {
141 return !(lhs < rhs);
142 }
143
144 friend bool operator<=(const binding_rebase_t& lhs,
145 const binding_rebase_t& rhs) {
146 return !(lhs > rhs);
147 }
148
149 };
150 LIEF_LOCAL result<size_t> lib2ord(const Binary& bin, const Symbol& sym,
151 const std::string& lib);
152 LIEF_LOCAL const Symbol* find_symbol(const Binary& bin, const std::string& name);
153
154 LIEF_LOCAL static DYLD_CHAINED_PTR_FORMAT pointer_format(const Binary& bin, size_t imp_count);
155 LIEF_LOCAL ok_error_t process_relocations(Binary& target, DYLD_CHAINED_PTR_FORMAT ptr_fmt);
156 LIEF_LOCAL ok_error_t process_bindings(
157 Binary& target, strong_map_t& strong_map,
158 std::unordered_map<std::string, size_t>& symbols_idx, DyldChainedFixups* cmd,
159 DyldChainedFixups::binding_info_t& all_bindings);
160
161 uint32_t fixups_version_ = 0;
162 DYLD_CHAINED_FORMAT imports_format_;
163 std::vector<binding_info_t> bindings_;
164 std::vector<reloc_info_t> relocations_;
165 std::unordered_map<SegmentCommand*, std::vector<binding_rebase_t>> segment_chains_;
166 std::unordered_map<std::string, size_t> lib2ord_;
167};
168}
169}
170#endif
Class which represents a MachO binary.
Definition MachO/Binary.hpp:85
This class represents a symbol binding operation associated with the LC_DYLD_CHAINED_FIXUPS command.
Definition ChainedBindingInfo.hpp:48
Definition DyldChainedFixupsCreator.hpp:41
DyldChainedFixupsCreator & fixups_version(uint32_t value)
Definition DyldChainedFixupsCreator.hpp:59
DyldChainedFixups * create(Binary &target)
DyldChainedFixupsCreator & add_relocations(const std::vector< reloc_info_t > &relocations)
Definition DyldChainedFixupsCreator.hpp:72
DyldChainedFixupsCreator & add_bindings(const std::vector< binding_info_t > &bindings)
Definition DyldChainedFixupsCreator.hpp:90
DyldChainedFixupsCreator & add_relocation(uint64_t address, uint64_t target)
DyldChainedFixupsCreator & add_binding(uint64_t address, std::string symbol, std::string library, uint64_t addend=0, bool weak=false)
DyldChainedFixupsCreator & add_binding(uint64_t address, std::string symbol, uint64_t addend=0, bool weak=false)
Definition DyldChainedFixupsCreator.hpp:83
DyldChainedFixupsCreator & imports_format(DYLD_CHAINED_FORMAT fmt)
Definition DyldChainedFixupsCreator.hpp:64
Class that represents a rebase relocation found in the LC_DYLD_CHAINED_FIXUPS command.
Definition RelocationFixup.hpp:50
Class which represents a LoadCommand::TYPE::SEGMENT / LoadCommand::TYPE::SEGMENT_64 command.
Definition SegmentCommand.hpp:50
Class that represents a Symbol in a Mach-O file.
Definition MachO/Symbol.hpp:47
Namespace related to the LIEF's Mach-O module.
Definition Abstract/Header.hpp:36
DYLD_CHAINED_FORMAT
Definition DyldChainedFormat.hpp:22
@ UNKNOWN
Definition MachO/enums.hpp:25
DYLD_CHAINED_PTR_FORMAT
Definition DyldChainedFormat.hpp:29
LIEF namespace.
Definition Abstract/Binary.hpp:36
result< ok_t > ok_error_t
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:109
#define LIEF_LOCAL
Definition visibility.h:42