-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdyld.h
159 lines (114 loc) · 4.16 KB
/
dyld.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright (c) YungRaj
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
extern "C" {
#include <mach-o.h>
#include <mach/mach_types.h>
#include <sys/types.h>
#include <dyld_cache_format.h>
}
#include <vector>
#include <types.h>
class MachO;
class Segment;
class Section;
namespace xnu {
class Task;
class Kernel;
}; // namespace xnu
#define MH_DYLIB_IN_CACHE 0x80000000
namespace darwin {
namespace dyld {
class Library;
namespace shared_cache {
using Header = struct dyld_cache_header;
using MappingInfo = struct dyld_cache_mapping_info;
using AllImageInfos = struct dyld_all_image_infos;
using ImageInfo = struct dyld_image_info;
}; // namespace shared_cache
class Dyld {
public:
explicit Dyld(xnu::Kernel* kernel, xnu::Task* task)
: kernel(kernel), task(task) {
IterateAllImages();
}
~Dyld() = default;
char* GetMainImagePath() {
return main_image_path;
}
xnu::Task* GetTask() {
return task;
}
xnu::mach::VmAddress GetMainImageLoadBase() {
return main_image_load_base;
}
xnu::mach::VmAddress GetAllImageInfoAddr() {
return all_image_info_addr;
}
xnu::mach::VmAddress GetDyld() {
return dyld;
}
xnu::mach::VmAddress GetDyldSharedCache() {
return dyld_shared_cache;
}
dyld::shared_cache::ImageInfo* GetMainImageInfo() {
return main_image_info;
}
Offset GetSlide() {
return slide;
}
char* Contains(char* str, const char* substr);
int EndsWith(const char* str, const char* suffix);
void GetImageInfos();
void IterateAllImages();
dyld::shared_cache::Header* CacheGetHeader();
dyld::shared_cache::MappingInfo* CacheGetMappings(dyld::shared_cache::Header* cache_header);
dyld::shared_cache::MappingInfo* CacheGetMapping(dyld::shared_cache::Header* cache_header,
xnu::mach::VmProtection prot);
void CacheOffsetToAddress(UInt64 dyld_cache_offset, xnu::mach::VmAddress* address,
Offset* slide);
void CacheGetSymtabStrtab(struct symtab_command* symtab_command, xnu::mach::VmAddress* symtab,
xnu::mach::VmAddress* strtab, Offset* off);
xnu::mach::VmAddress GetImageLoadedAt(char* image_name, char** image_path);
xnu::mach::VmAddress GetImageSlide(xnu::mach::VmAddress address);
Size GetAdjustedLinkeditSize(xnu::mach::VmAddress address);
Size GetAdjustedStrtabSize(struct symtab_command* symtab_command, xnu::mach::VmAddress linkedit,
Offset linkedit_fileoff);
void RebuildSymtabStrtab(struct symtab_command* symtab_command, xnu::mach::VmAddress symtab_,
xnu::mach::VmAddress strtab_, xnu::mach::VmAddress linkedit,
Offset linkedit_fileoff);
void FixupObjectiveC(MachO* macho);
void FixupDyldRebaseBindOpcodes(MachO* macho, Segment* linkedit);
Size GetImageSize(xnu::mach::VmAddress address);
MachO* CacheDumpImage(char* image);
MachO* CacheDumpImageToFile(char* image, char* path);
Library* InjectLibrary(const char* path);
private:
char* main_image_path;
xnu::Kernel* kernel;
xnu::Task* task;
std::vector<Library*> libraries;
xnu::mach::VmAddress main_image_load_base;
xnu::mach::VmAddress dyld;
xnu::mach::VmAddress dyld_shared_cache;
Offset slide;
xnu::mach::VmAddress all_image_info_addr;
Size all_image_info_size;
dyld::shared_cache::AllImageInfos* all_image_infos;
dyld::shared_cache::ImageInfo* main_image_info;
};
} // namespace dyld
} // namespace darwin