forked from christinaa/libSystem_and_linker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getsecbyname.c
executable file
·508 lines (472 loc) · 13.4 KB
/
getsecbyname.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*
* Copyright (c) 1999 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@
*/
#ifndef RLD
#include <mach-o/ldsyms.h>
#include <mach-o/swap.h>
#include <string.h>
#ifdef __DYNAMIC__
#include <mach-o/dyld.h> /* defines _dyld_lookup_and_bind() */
#endif /* defined(__DYNAMIC__) */
#ifndef __OPENSTEP__
#ifdef HAVE_CRT_EXTERNS_H
#include <crt_externs.h>
#endif /* HAVE_CRT_EXTERNS_H */
#else /* defined(__OPENSTEP__) */
#if !defined(__DYNAMIC__)
#define DECLARE_VAR(var, type) \
extern type var
#define SETUP_VAR(var)
#define USE_VAR(var) var
#else
#define STRINGIFY(a) # a
#define DECLARE_VAR(var, type) \
static type * var ## _pointer = NULL
#define SETUP_VAR(var) \
if ( var ## _pointer == NULL) { \
_dyld_lookup_and_bind( STRINGIFY(_ ## var), \
(uint32_t *) & var ## _pointer, NULL); \
}
#define USE_VAR(var) (* var ## _pointer)
#endif
#endif /* __OPENSTEP__ */
/*
* This routine returns the section structure for the named section in the
* named segment for the mach_header pointer passed to it if it exist.
* Otherwise it returns zero.
*/
const struct section *
getsectbynamefromheader(
struct mach_header *mhp,
const char *segname,
const char *sectname)
{
struct segment_command *sgp;
struct section *sp;
uint32_t i, j;
sgp = (struct segment_command *)
((char *)mhp + sizeof(struct mach_header));
for(i = 0; i < mhp->ncmds; i++){
if(sgp->cmd == LC_SEGMENT)
if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 ||
mhp->filetype == MH_OBJECT){
sp = (struct section *)((char *)sgp +
sizeof(struct segment_command));
for(j = 0; j < sgp->nsects; j++){
if(strncmp(sp->sectname, sectname,
sizeof(sp->sectname)) == 0 &&
strncmp(sp->segname, segname,
sizeof(sp->segname)) == 0)
return(sp);
sp = (struct section *)((char *)sp +
sizeof(struct section));
}
}
sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
}
return((struct section *)0);
}
/*
* This routine returns the section structure for the named section in the
* named segment for the mach_header_64 pointer passed to it if it exist.
* Otherwise it returns zero.
*/
const struct section_64 *
getsectbynamefromheader_64(
struct mach_header_64 *mhp,
const char *segname,
const char *sectname)
{
struct segment_command_64 *sgp;
struct section_64 *sp;
uint32_t i, j;
sgp = (struct segment_command_64 *)
((char *)mhp + sizeof(struct mach_header_64));
for(i = 0; i < mhp->ncmds; i++){
if(sgp->cmd == LC_SEGMENT_64)
if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 ||
mhp->filetype == MH_OBJECT){
sp = (struct section_64 *)((char *)sgp +
sizeof(struct segment_command_64));
for(j = 0; j < sgp->nsects; j++){
if(strncmp(sp->sectname, sectname,
sizeof(sp->sectname)) == 0 &&
strncmp(sp->segname, segname,
sizeof(sp->segname)) == 0)
return(sp);
sp = (struct section_64 *)((char *)sp +
sizeof(struct section_64));
}
}
sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize);
}
return((struct section_64 *)0);
}
/*
* This routine returns the section structure for the named section in the
* named segment for the mach_header pointer passed to it if it exist.
* Otherwise it returns zero. If fSwap == YES (the mach header has been
* swapped to the endiannes of the current machine, but the segments and
* sections are different) then the segment and sections are swapped.
*/
const struct section *
getsectbynamefromheaderwithswap(
struct mach_header *mhp,
const char *segname,
const char *sectname,
int fSwap)
{
struct segment_command *sgp;
struct section *sp;
uint32_t i, j;
sgp = (struct segment_command *)
((char *)mhp + sizeof(struct mach_header));
for(i = 0; i < mhp->ncmds; i++){
if(sgp->cmd == (fSwap ? OSSwapInt32(LC_SEGMENT) : LC_SEGMENT)) {
if (fSwap) {
#ifdef __LITTLE_ENDIAN__
swap_segment_command(sgp, NX_BigEndian);
#else
swap_segment_command(sgp, NX_LittleEndian);
#endif /* __LITTLE_ENDIAN__ */
}
if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 ||
mhp->filetype == MH_OBJECT){
sp = (struct section *)((char *)sgp +
sizeof(struct segment_command));
if (fSwap) {
#ifdef __LITTLE_ENDIAN__
swap_section(sp, sgp->nsects, NX_BigEndian);
#else
swap_section(sp, sgp->nsects, NX_LittleEndian);
#endif /* __LITTLE_ENDIAN__ */
}
for(j = 0; j < sgp->nsects; j++){
if(strncmp(sp->sectname, sectname,
sizeof(sp->sectname)) == 0 &&
strncmp(sp->segname, segname,
sizeof(sp->segname)) == 0)
return(sp);
sp = (struct section *)((char *)sp +
sizeof(struct section));
}
}
sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
} else {
sgp = (struct segment_command *)((char *)sgp +
(fSwap ? OSSwapInt32(sgp->cmdsize) : sgp->cmdsize));
}
}
return((struct section *)0);
}
/*
* This routine returns the a pointer the section structure of the named
* section in the named segment if it exist in the mach executable it is
* linked into. Otherwise it returns zero.
*/
#ifndef __LP64__
const struct section *
getsectbyname(
const char *segname,
const char *sectname)
{
static struct mach_header *mhp = NULL;
#ifndef __OPENSTEP__
if(mhp == NULL)
mhp = _NSGetMachExecuteHeader();
#else /* defined(__OPENSTEP__) */
DECLARE_VAR(_mh_execute_header, struct mach_header);
SETUP_VAR(_mh_execute_header);
mhp = (struct mach_header *)(& USE_VAR(_mh_execute_header));
#endif /* __OPENSTEP__ */
return(getsectbynamefromheader(mhp, segname, sectname));
}
#else /* defined(__LP64__) */
const struct section_64 *
getsectbyname(
const char *segname,
const char *sectname)
{
static struct mach_header_64 *mhp = NULL;
if(mhp == NULL)
mhp = _NSGetMachExecuteHeader();
return(getsectbynamefromheader_64(mhp, segname, sectname));
}
#endif /* defined(__LP64__) */
/*
* This routine returns the a pointer to the data for the named section in the
* named segment if it exist in the mach executable it is linked into. Also
* it returns the size of the section data indirectly through the pointer size.
* Otherwise it returns zero for the pointer and the size.
*/
char *
getsectdata(
const char *segname,
const char *sectname,
unsigned long *size)
{
#ifndef __LP64__
const struct section *sp;
#else /* defined(__LP64__) */
const struct section_64 *sp;
#endif /* defined(__LP64__) */
sp = getsectbyname(segname, sectname);
if(sp == NULL){
*size = 0;
return(NULL);
}
*size = sp->size;
return((char *)(sp->addr));
}
/*
* This routine returns the a pointer to the section contents of the named
* section in the named segment if it exists in the image pointed to by the
* mach header. Otherwise it returns zero.
*/
#ifndef __LP64__
uint8_t *
getsectiondata(
const struct mach_header *mhp,
const char *segname,
const char *sectname,
unsigned long *size)
{
struct segment_command *sgp, *zero;
struct section *sp, *find;
uint32_t i, j;
zero = 0;
find = 0;
sp = 0;
sgp = (struct segment_command *)
((char *)mhp + sizeof(struct mach_header));
for(i = 0; i < mhp->ncmds; i++){
if(sgp->cmd == LC_SEGMENT){
if(zero == 0 && sgp->fileoff == 0 && sgp->nsects != 0){
zero = sgp;
if(find != 0)
goto done;
}
if(find == 0 &&
strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0){
sp = (struct section *)((char *)sgp +
sizeof(struct segment_command));
for(j = 0; j < sgp->nsects; j++){
if(strncmp(sp->sectname, sectname,
sizeof(sp->sectname)) == 0 &&
strncmp(sp->segname, segname,
sizeof(sp->segname)) == 0){
find = sp;
if(zero != 0)
goto done;
}
sp = (struct section *)((char *)sp +
sizeof(struct section));
}
}
}
sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
}
return(0);
done:
*size = sp->size;
return((uint8_t *)((uintptr_t)mhp - zero->vmaddr + sp->addr));
}
uint8_t *
getsegmentdata(
const struct mach_header *mhp,
const char *segname,
unsigned long *size)
{
struct segment_command *sgp, *zero, *find;
uint32_t i;
zero = 0;
find = 0;
sgp = (struct segment_command *)
((char *)mhp + sizeof(struct mach_header));
for(i = 0; i < mhp->ncmds; i++){
if(sgp->cmd == LC_SEGMENT){
if(zero == 0 && sgp->fileoff == 0 && sgp->nsects != 0){
zero = sgp;
if(find != 0)
goto done;
}
if(find == 0 &&
strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0){
find = sgp;
if(zero != 0)
goto done;
}
}
sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
}
return(0);
done:
*size = sgp->vmsize;
return((uint8_t *)((uintptr_t)mhp - zero->vmaddr + sgp->vmaddr));
}
#else /* defined(__LP64__) */
uint8_t *
getsectiondata(
const struct mach_header_64 *mhp,
const char *segname,
const char *sectname,
unsigned long *size)
{
struct segment_command_64 *sgp, *zero;
struct section_64 *sp, *find;
uint32_t i, j;
zero = 0;
find = 0;
sp = 0;
sgp = (struct segment_command_64 *)
((char *)mhp + sizeof(struct mach_header_64));
for(i = 0; i < mhp->ncmds; i++){
if(sgp->cmd == LC_SEGMENT_64){
if(zero == 0 && sgp->fileoff == 0 && sgp->nsects != 0){
zero = sgp;
if(find != 0)
goto done;
}
if(find == 0 &&
strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0){
sp = (struct section_64 *)((char *)sgp +
sizeof(struct segment_command_64));
for(j = 0; j < sgp->nsects; j++){
if(strncmp(sp->sectname, sectname,
sizeof(sp->sectname)) == 0 &&
strncmp(sp->segname, segname,
sizeof(sp->segname)) == 0){
find = sp;
if(zero != 0)
goto done;
}
sp = (struct section_64 *)((char *)sp +
sizeof(struct section_64));
}
}
}
sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize);
}
return(0);
done:
*size = sp->size;
return((uint8_t *)((uintptr_t)mhp - zero->vmaddr + sp->addr));
}
uint8_t *
getsegmentdata(
const struct mach_header_64 *mhp,
const char *segname,
unsigned long *size)
{
struct segment_command_64 *sgp, *zero, *find;
uint32_t i;
zero = 0;
find = 0;
sgp = (struct segment_command_64 *)
((char *)mhp + sizeof(struct mach_header_64));
for(i = 0; i < mhp->ncmds; i++){
if(sgp->cmd == LC_SEGMENT_64){
if(zero == 0 && sgp->fileoff == 0 && sgp->nsects != 0){
zero = sgp;
if(find != 0)
goto done;
}
if(find == 0 &&
strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0){
find = sgp;
if(zero != 0)
goto done;
}
}
sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize);
}
return(0);
done:
*size = sgp->vmsize;
return((uint8_t *)((uintptr_t)mhp - zero->vmaddr + sgp->vmaddr));
}
#endif /* defined(__LP64__) */
/*
* This routine returns the a pointer to the data for the named section in the
* named segment if it exist in the mach header passed to it. Also it returns
* the size of the section data indirectly through the pointer size. Otherwise
* it returns zero for the pointer and the size.
*/
char *
getsectdatafromheader(
struct mach_header *mhp,
const char *segname,
const char *sectname,
unsigned long *size)
{
const struct section *sp;
sp = getsectbynamefromheader(mhp, segname, sectname);
if(sp == NULL){
*size = 0;
return(NULL);
}
*size = sp->size;
return((char *)((uintptr_t)(sp->addr)));
}
/*
* This routine returns the a pointer to the data for the named section in the
* named segment if it exist in the 64-bit mach header passed to it. Also it
* returns the size of the section data indirectly through the pointer size.
* Otherwise it returns zero for the pointer and the size.
*/
char *
getsectdatafromheader_64(
struct mach_header_64 *mhp,
const char *segname,
const char *sectname,
unsigned long *size)
{
const struct section_64 *sp;
sp = getsectbynamefromheader_64(mhp, segname, sectname);
if(sp == NULL){
*size = 0;
return(NULL);
}
*size = sp->size;
return((char *)((uintptr_t)(sp->addr)));
}
#ifdef __DYNAMIC__
/*
* This routine returns the a pointer to the data for the named section in the
* named segment if it exist in the named Framework. Also it returns the size
* of the section data indirectly through the pointer size. Otherwise it
* returns zero for the pointer and the size. The last component of the path
* of the Framework is passed as FrameworkName.
*/
#include "OSLog.h"
void *
getsectdatafromFramework(
const char *FrameworkName,
const char *segname,
const char *sectname,
unsigned long *size)
{
/* no */
OSHalt("getsectdatafromFramework");
}
#endif /* __DYNAMIC__ */
#endif /* !defined(RLD) */