-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchroot_util.cpp
210 lines (194 loc) · 7.54 KB
/
chroot_util.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright (c) 2019 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
* Reserved. This file contains Original Code and/or Modifications of
* Original Code as defined in and that are subject to the Apple Public
* Source License Version 1.0 (the 'License'). You may not use this file
* except in compliance with the License. Please obtain a copy of the
* License at http://www.apple.com/publicsource and read it before using
* this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License."
*
* @APPLE_LICENSE_HEADER_END@
*/
#include <cassert>
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <dirent.h>
#include <libgen.h>
#include <unistd.h>
#include <sys/attr.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <mach-o/fat.h>
#include <mach-o/loader.h>
#include <copyfile.h>
#include <set>
#include <string>
#include <vector>
#include <functional>
#include <filesystem>
#include "StringUtils.h"
#include "MachOFile.h"
std::set<std::string> scanForDependencies(const std::string& path) {
__block std::set<std::string> result;
struct stat stat_buf;
int fd = open(path.c_str(), O_RDONLY, 0);
if (fd == -1) {
return result;
}
if (fstat(fd, &stat_buf) == -1) {
close(fd);
return result;
}
const void* buffer = mmap(NULL, (size_t)stat_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (buffer == MAP_FAILED) {
close(fd);
return result;
}
auto scanner = ^(const char *loadPath, bool isWeak, bool isReExport, bool isUpward, uint32_t compatVersion, uint32_t curVersion, bool &stop) {
if (isWeak) { return; } // We explicily avoid LC_LOAD_WEAK_DYLIB since we are trying to build a minimal chroot
if (loadPath[0] != '/') { return; } // Only include absolute dependencies
result.insert(loadPath);
};
Diagnostics diag;
if ( dyld3::FatFile::isFatFile(buffer) ) {
const dyld3::FatFile* ff = (dyld3::FatFile*)buffer;
ff->forEachSlice(diag, stat_buf.st_size, ^(uint32_t sliceCpuType, uint32_t sliceCpuSubType, const void* sliceStart, uint64_t sliceSize, bool& stop) {
const dyld3::MachOFile* mf = (dyld3::MachOFile*)sliceStart;
mf->forEachDependentDylib(scanner);
});
} else {
const dyld3::MachOFile* mf = (dyld3::MachOFile*)buffer;
if (mf->isMachO(diag, stat_buf.st_size)) {
mf->forEachDependentDylib(scanner);
}
}
close(fd);
return result;
}
std::string withoutPrefixPath(const std::string& path, const std::string& prefix ) {
std::string result = path;
size_t pos = result.find(prefix);
result.erase(pos, prefix.length());
return result;
}
void add_symlinks_to_dylib(const std::string path) {
static std::set<std::string> alreadyMatched;
size_t pos = path.rfind(".framework/Versions/");
auto prefixPath = path.substr(0, pos);
if (alreadyMatched.find(prefixPath) != alreadyMatched.end()) { return; }
if (pos == std::string::npos) { return; }
// fprintf(stderr, "PATH: %s\n", path.c_str());
size_t versionStart = pos+20;
size_t versionEnd = versionStart;
while (path[versionEnd] != '/') {
++versionEnd;
}
size_t frameworkNameBegin = pos;
while (path[frameworkNameBegin-1] != '/') {
--frameworkNameBegin;
}
auto frameworkName = path.substr(frameworkNameBegin, pos-frameworkNameBegin);
auto version = path.substr(versionStart, versionEnd-versionStart);
std::string mainLinkPath = prefixPath + ".framework/" + frameworkName;
std::string mainLinkTarget = "Versions/Current/" + frameworkName;
std::string versionLinkPath = prefixPath + ".framework/Versions/Current";
std::string versionLinkTarget = version;;
alreadyMatched.insert(prefixPath);
if (!std::filesystem::exists(versionLinkPath)) {
std::filesystem::create_symlink(version, versionLinkPath);
}
if (!std::filesystem::exists(mainLinkPath)) {
std::filesystem::create_symlink(mainLinkTarget, mainLinkPath);
}
}
void add_symlink(const std::string& target, const std::string& path) {
if (!std::filesystem::exists(path)) {
std::filesystem::create_symlink(target, path);
}
}
void buildChroot(const std::string& chroot, const std::string& fallback, const std::vector<std::string>& binaries) {
auto chrootPath = std::filesystem::path(chroot);
auto fallbackPath = std::filesystem::path(fallback);
for (const auto& binary : binaries) {
if (std::filesystem::exists(chroot + binary)) { continue; }
std::filesystem::create_directories(std::filesystem::path(chroot + binary).parent_path());
std::filesystem::copy(fallback + binary, chroot + binary);
}
bool foundNewEntries = true;
std::set<std::string> scannedFiles;
std::string devfsPath = chroot + "/dev";
while (foundNewEntries) {
foundNewEntries = false;
for(auto file = std::filesystem::recursive_directory_iterator(chroot);
file != std::filesystem::recursive_directory_iterator();
++file ) {
auto filePath = file->path().string();
if (filePath == devfsPath) {
file.disable_recursion_pending();
continue;
}
if (scannedFiles.find(filePath) != scannedFiles.end()) { continue; }
scannedFiles.insert(filePath);
auto candidates = scanForDependencies(filePath);
for (const auto& candidate : candidates) {
if (std::filesystem::exists(chroot + candidate)) { continue; }
if (!std::filesystem::exists(fallback + candidate)) { continue; }
std::filesystem::create_directories(std::filesystem::path(chroot + candidate).parent_path());
std::filesystem::copy(fallback + candidate, chroot + candidate);
add_symlinks_to_dylib(chroot + candidate);
foundNewEntries = true;
}
}
}
add_symlink("libSystem.B.dylib", chroot + "/usr/lib/libSystem.dylib");
add_symlink("libSystem.dylib", chroot + "/usr/lib/libc.dylib");
}
int main(int argc, const char * argv[]) {
std::vector<std::string> binaries;
std::vector<std::string> overlays;
std::string fallback;
std::string chroot;
for (int i = 1; i < argc; ++i) {
const char* arg = argv[i];
if (arg[0] == '-') {
if (strcmp(arg, "-chroot") == 0) {
chroot = argv[++i];
} else if (strcmp(arg, "-fallback") == 0) {
fallback = argv[++i];
} else if (strcmp(arg, "-add_file") == 0) {
binaries.push_back(argv[++i]);
} else {
fprintf(stderr, "unknown option: %s\n", arg);
exit(-1);
}
} else {
fprintf(stderr, "unknown option: %s\n", arg);
exit(-1);
}
}
if (chroot.length() == 0) {
fprintf(stderr, "No -chroot <dir>\n");
exit(-1);
}
if (fallback.length() == 0) {
fprintf(stderr, "No -fallback <dir>\n");
exit(-1);
}
buildChroot(chroot, fallback, binaries);
// insert code here...
return 0;
}