-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdylib_table.c
384 lines (360 loc) · 11.5 KB
/
dylib_table.c
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* 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 2.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.opensource.apple.com/apsl/ 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, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/* NOTE: This is no longer compiled into libstuff as of 2/27/2019 */
#ifndef RLD
#include <stdio.h>
#include <stdlib.h>
#include <libc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <mach/mach.h>
#include "stuff/bool.h"
#include "stuff/ofile.h"
#include "stuff/errors.h"
#include "stuff/allocate.h"
#include "stuff/dylib_table.h"
/* This may change to "/MacOSX/System" someday */
#define SYSTEM_LIBRARY "/System/Library"
/*
* parse_dylib_table() opens the file_name passed to it and parses it as a
* dylib table. The flag and argument parameters are used for error messages
* if there is a problem parsing the file. The file should
* contains lines of the form:
* <hex address> <short name>
* The fields are to be separated by spaces or tabs. Lines with just spaces
* and tabs are ignored. This routine returns the parsed table as an
* array of dylib_table structs and returns a pointer to the table. The last
* entry in the table has a NULL name.
*/
struct dylib_table *
parse_dylib_table(
char *file_name,/* file name of dylib table file */
char *flag, /* "-dylib_file" or "default" */
char *argument) /* -dylib_file argument or "dylib table" */
{
int fd;
struct stat stat_buf;
uint32_t j, k, file_size, new_dylib_table_size;
char *file_addr, *endp;
struct dylib_table *new_dylib_table;
if((fd = open(file_name, O_RDONLY, 0)) == -1)
system_fatal("Can't open: %s for %s %s",
file_name, flag, argument);
if(fstat(fd, &stat_buf) == -1)
system_fatal("Can't stat file: %s for %s %s",
file_name, flag, argument);
/*
* For some reason mapping files with zero size fails
* so it has to be handled specially. Also, deal with files that are
* too large to be valid seg_addr tables.
*/
file_addr = NULL;
if (stat_buf.st_size > 0xFFFFFFFF) {
fatal("File too large (%llu): %s for %s %s", stat_buf.st_size,
file_name, flag, argument);
}
else if (stat_buf.st_size == 0) {
fatal("Empty file: %s for %s %s", file_name, flag, argument);
}
else {
file_addr = mmap(0, stat_buf.st_size, PROT_READ|PROT_WRITE,
MAP_FILE|MAP_PRIVATE, fd, 0);
if((intptr_t)file_addr == -1)
system_error("can't map file: %s for %s %s", file_name, flag,
argument);
}
close(fd);
file_size = (uint32_t)stat_buf.st_size;
/*
* Got the file mapped now parse it.
*/
if(file_addr[file_size - 1] != '\n')
fatal("file: %s for %s %s does not end in new line",
file_name, flag, argument);
new_dylib_table_size = 0;
for(j = 1; j < file_size; j++){
if(file_addr[j] == '\n'){
new_dylib_table_size++;
}
}
new_dylib_table_size++;
new_dylib_table = allocate(sizeof(struct dylib_table) *
new_dylib_table_size);
k = 0;
for(j = 0; j < file_size; /* no increment expression */ ){
/* Skip blank lines */
while(file_addr[j] == ' ' || file_addr[j] == '\t')
j++;
if(file_addr[j] == '\n'){
j++;
continue;
}
new_dylib_table[k].seg1addr =
(uint32_t)strtoul(file_addr + j, &endp, 16);
if(endp == NULL)
fatal("improper hexadecimal number on line %u in "
"file: %s for %s %s", j, file_name, flag, argument);
j = (uint32_t)(endp - file_addr);
if(j == file_size)
fatal("missing library name on line %u in file: "
"%s for %s %s", j, file_name, flag, argument);
/*
* Since we checked to see the file ends in a '\n' we can
* be assured this won't run off the end of the file.
*/
while(file_addr[j] == ' ' || file_addr[j] == '\t')
j++;
if(file_addr[j] == '\n')
fatal("missing library name on line %u in file: "
"%s for %s %s", j, file_name, flag, argument);
new_dylib_table[k].name = file_addr + j;
k++;
while(file_addr[j] != '\n')
j++;
file_addr[j] = '\0';
j++;
}
new_dylib_table[k].seg1addr = 0;
new_dylib_table[k].name = NULL;
return(new_dylib_table);
}
/*
* parse_default_dylib_table() opens and parses ~rc/Data/DylibTable .
*/
struct dylib_table *
parse_default_dylib_table(
char **file_name)
{
size_t i;
FILE *fp;
*file_name = allocate(MAXPATHLEN+1);
fp = popen("/bin/echo ~rc/Data/DylibTable", "r");
if(fp == NULL)
fatal("must use -dylib_table (popen failed on \"/bin/echo "
"~rc/Data/DylibTable\"");
if(fgets(*file_name, MAXPATHLEN, fp) == NULL)
fatal("must use -dylib_table (fgets failed from popen of "
"\"/bin/echo ~rc/Data/DylibTable\"");
i = strlen(*file_name);
if(i == 0 || (*file_name)[i-1] != '\n')
fatal("must use -dylib_table (file name from popen of "
"\"/bin/echo ~rc/Data/DylibTable\" greater than "
"MAXPATHLEN");
(*file_name)[i-1] = '\0';
pclose(fp);
return(parse_dylib_table(*file_name, "default", "dylib table"));
}
/*
* search_dylib_table() searches the specified dylib table for the specified
* name and returns the entry to it if it is found. If it is not found NULL
* is returned.
*/
struct dylib_table *
search_dylib_table(
struct dylib_table *dylib_table,
char *name)
{
struct dylib_table *p;
if(dylib_table == NULL)
return(NULL);
for(p = dylib_table; p->name != NULL; p++){
if(strcmp(p->name, name) == 0)
return(p);
}
return(NULL);
}
static char *versions[] = { "A", "B", "C", "0", NULL };
/*
* guess_dylib_install_name() is passed a name from the dylib file and then
* the install name is guessed based on the files on the system the program is
* running on. This routines makes up conventional install names base on the
* name passed in and then sees if there is a file on the system with that
* install_name. If it finds one it returns the install_name else it returns
* NULL.
*/
char *
guess_dylib_install_name(
char *name)
{
uint32_t i;
char *guess;
struct stat stat_buf;
for(i = 0; versions[i] != NULL; i++){
guess = makestr(
SYSTEM_LIBRARY,"/Frameworks/JavaVM.framework/Libraries/lib",
name, ".", versions[i], ".dylib", NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
for(i = 0; versions[i] != NULL; i++){
guess = makestr(SYSTEM_LIBRARY,"/Frameworks/", name,
".framework/Versions/", versions[i], "/", name, NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
for(i = 0; versions[i] != NULL; i++){
guess = makestr(SYSTEM_LIBRARY,"/PrivateFrameworks/", name,
".framework/Versions/", versions[i], "/", name, NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
for(i = 0; versions[i] != NULL; i++){
guess = makestr(SYSTEM_LIBRARY,"Printers/", name, ".",
versions[i],".dylib", NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
for(i = 0; versions[i] != NULL; i++){
#ifdef __GONZO_BUNSEN_BEAKER__
guess = makestr("/Local/Library/Frameworks/", name,
".framework/Versions/", versions[i], "/", name, NULL);
#else
guess = makestr("/MacOSX/Library/Frameworks/", name,
".framework/Versions/", versions[i], "/", name, NULL);
#endif
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
#ifndef __GONZO_BUNSEN_BEAKER__
for(i = 0; versions[i] != NULL; i++){
guess = makestr("/AppleInternal/Library/Frameworks/", name,
".framework/Versions/", versions[i], "/", name, NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
#endif
for(i = 0; versions[i] != NULL; i++){
guess = makestr("/lib/", name, ".", versions[i], ".dylib", NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
for(i = 0; versions[i] != NULL; i++){
guess = makestr("/usr/lib/", name, ".", versions[i], ".dylib",
NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
for(i = 0; versions[i] != NULL; i++){
guess = makestr("/usr/lib/java/", name, ".", versions[i],
".dylib", NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
for(i = 0; versions[i] != NULL; i++){
guess = makestr("/usr/local/lib/", name, ".", versions[i],
".dylib", NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
for(i = 0; versions[i] != NULL; i++){
guess = makestr("/usr/canna/dylib/", name, ".", versions[i],
".dylib", NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
/* From ALLOW_MACOSX_PR1_PATHS */
for(i = 0; versions[i] != NULL; i++){
guess = makestr(SYSTEM_LIBRARY,"/Frameworks/CarbonCore.framework/"
"Versions/", versions[i], "/Support/", name,
".dylib", NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
for(i = 0; versions[i] != NULL; i++){
guess = makestr(SYSTEM_LIBRARY,"Frameworks/Carbon.framework/"
"Versions/", versions[i], "/Libraries/", name,
".dylib", NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
for(i = 0; versions[i] != NULL; i++){
guess = makestr(SYSTEM_LIBRARY,"/Frameworks/CoreGraphics.framework/"
"Versions/", versions[i], "/Libraries/", name, ".",
versions[i], ".dylib", NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
/* From ALLOW_MACOSX_DP3_PATHS */
for(i = 0; versions[i] != NULL; i++){
guess = makestr(SYSTEM_LIBRARY,"/Components/", name, ".qtx", NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
for(i = 0; versions[i] != NULL; i++){
guess = makestr(SYSTEM_LIBRARY,"/Frameworks/Carbon.framework/"
"Versions/", versions[i], "/Resources//", name,
".qtx", NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
for(i = 0; versions[i] != NULL; i++){
guess = makestr(SYSTEM_LIBRARY,"/Frameworks/CoreGraphics.framework/"
"Versions/", versions[i], "/Resources/", name, ".",
versions[i], ".qtx", NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
for(i = 0; versions[i] != NULL; i++){
guess = makestr(SYSTEM_LIBRARY,"/Frameworks/JavaVM.framework/"
"Versions/1.2/Libraries/", name, ".", versions[i],
".dylib", NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
for(i = 0; versions[i] != NULL; i++){
guess = makestr(SYSTEM_LIBRARY,"/Frameworks/PrintingCore.framework/"
"Versions/", versions[i], "/Libraries/", name, ".",
versions[i], ".dylib", NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
for(i = 0; versions[i] != NULL; i++){
guess = makestr(SYSTEM_LIBRARY,"/Frameworks/QuickTime.framework/"
"Versions/", versions[i], "/", name, NULL);
if(stat(guess, &stat_buf) != -1)
return(guess);
free(guess);
}
return(NULL);
}
#endif /* !defined(RLD) */