Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: third_party/apple_cctools/cctools/libmacho/getsecbyname.c

Issue 564853002: Add third_party/apple_cctools containing virgin upstream code (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/apple_cctools/cctools/include/mach-o/getsect.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 #ifndef RLD
24 #include <mach-o/ldsyms.h>
25 #include <mach-o/swap.h>
26 #include <string.h>
27 #ifdef __DYNAMIC__
28 #include <mach-o/dyld.h> /* defines _dyld_lookup_and_bind() */
29 #endif /* defined(__DYNAMIC__) */
30 #ifndef __OPENSTEP__
31 #include <crt_externs.h>
32 #else /* defined(__OPENSTEP__) */
33
34 #if !defined(__DYNAMIC__)
35 #define DECLARE_VAR(var, type) \
36 extern type var
37 #define SETUP_VAR(var)
38 #define USE_VAR(var) var
39 #else
40 #define STRINGIFY(a) # a
41 #define DECLARE_VAR(var, type) \
42 static type * var ## _pointer = NULL
43 #define SETUP_VAR(var) \
44 if ( var ## _pointer == NULL) { \
45 _dyld_lookup_and_bind( STRINGIFY(_ ## var), \
46 (uint32_t *) & var ## _pointer, NULL); \
47 }
48 #define USE_VAR(var) (* var ## _pointer)
49 #endif
50 #endif /* __OPENSTEP__ */
51
52 /*
53 * This routine returns the section structure for the named section in the
54 * named segment for the mach_header pointer passed to it if it exist.
55 * Otherwise it returns zero.
56 */
57 const struct section *
58 getsectbynamefromheader(
59 struct mach_header *mhp,
60 const char *segname,
61 const char *sectname)
62 {
63 struct segment_command *sgp;
64 struct section *sp;
65 uint32_t i, j;
66
67 sgp = (struct segment_command *)
68 ((char *)mhp + sizeof(struct mach_header));
69 for(i = 0; i < mhp->ncmds; i++){
70 if(sgp->cmd == LC_SEGMENT)
71 if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 ||
72 mhp->filetype == MH_OBJECT){
73 sp = (struct section *)((char *)sgp +
74 sizeof(struct segment_command));
75 for(j = 0; j < sgp->nsects; j++){
76 if(strncmp(sp->sectname, sectname,
77 sizeof(sp->sectname)) == 0 &&
78 strncmp(sp->segname, segname,
79 sizeof(sp->segname)) == 0)
80 return(sp);
81 sp = (struct section *)((char *)sp +
82 sizeof(struct section));
83 }
84 }
85 sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
86 }
87 return((struct section *)0);
88 }
89
90 /*
91 * This routine returns the section structure for the named section in the
92 * named segment for the mach_header_64 pointer passed to it if it exist.
93 * Otherwise it returns zero.
94 */
95 const struct section_64 *
96 getsectbynamefromheader_64(
97 struct mach_header_64 *mhp,
98 const char *segname,
99 const char *sectname)
100 {
101 struct segment_command_64 *sgp;
102 struct section_64 *sp;
103 uint32_t i, j;
104
105 sgp = (struct segment_command_64 *)
106 ((char *)mhp + sizeof(struct mach_header_64));
107 for(i = 0; i < mhp->ncmds; i++){
108 if(sgp->cmd == LC_SEGMENT_64)
109 if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 ||
110 mhp->filetype == MH_OBJECT){
111 sp = (struct section_64 *)((char *)sgp +
112 sizeof(struct segment_command_64));
113 for(j = 0; j < sgp->nsects; j++){
114 if(strncmp(sp->sectname, sectname,
115 sizeof(sp->sectname)) == 0 &&
116 strncmp(sp->segname, segname,
117 sizeof(sp->segname)) == 0)
118 return(sp);
119 sp = (struct section_64 *)((char *)sp +
120 sizeof(struct section_64));
121 }
122 }
123 sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize);
124 }
125 return((struct section_64 *)0);
126 }
127
128 /*
129 * This routine returns the section structure for the named section in the
130 * named segment for the mach_header pointer passed to it if it exist.
131 * Otherwise it returns zero. If fSwap == YES (the mach header has been
132 * swapped to the endiannes of the current machine, but the segments and
133 * sections are different) then the segment and sections are swapped.
134 */
135 const struct section *
136 getsectbynamefromheaderwithswap(
137 struct mach_header *mhp,
138 const char *segname,
139 const char *sectname,
140 int fSwap)
141 {
142 struct segment_command *sgp;
143 struct section *sp;
144 uint32_t i, j;
145
146 sgp = (struct segment_command *)
147 ((char *)mhp + sizeof(struct mach_header));
148 for(i = 0; i < mhp->ncmds; i++){
149 if(sgp->cmd == (fSwap ? OSSwapInt32(LC_SEGMENT) : LC_SEGMENT)) {
150
151 if (fSwap) {
152 #ifdef __LITTLE_ENDIAN__
153 swap_segment_command(sgp, NX_BigEndian);
154 #else
155 swap_segment_command(sgp, NX_LittleEndian);
156 #endif /* __LITTLE_ENDIAN__ */
157 }
158
159 if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 ||
160 mhp->filetype == MH_OBJECT){
161 sp = (struct section *)((char *)sgp +
162 sizeof(struct segment_command));
163
164 if (fSwap) {
165 #ifdef __LITTLE_ENDIAN__
166 swap_section(sp, sgp->nsects, NX_BigEndian);
167 #else
168 swap_section(sp, sgp->nsects, NX_LittleEndian);
169 #endif /* __LITTLE_ENDIAN__ */
170 }
171
172 for(j = 0; j < sgp->nsects; j++){
173 if(strncmp(sp->sectname, sectname,
174 sizeof(sp->sectname)) == 0 &&
175 strncmp(sp->segname, segname,
176 sizeof(sp->segname)) == 0)
177 return(sp);
178 sp = (struct section *)((char *)sp +
179 sizeof(struct section));
180 }
181 }
182 sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
183 } else {
184 sgp = (struct segment_command *)((char *)sgp +
185 (fSwap ? OSSwapInt32(sgp->cmdsize) : sgp->cmdsize));
186 }
187 }
188 return((struct section *)0);
189 }
190
191 /*
192 * This routine returns the section_64 structure for the named section in the
193 * named segment for the mach_header_64 pointer passed to it if it exist.
194 * Otherwise it returns zero. If fSwap == YES (the mach header has been
195 * swapped to the endiannes of the current machine, but the segments and
196 * sections are different) then the segment and sections are swapped.
197 */
198 const struct section_64 *
199 getsectbynamefromheaderwithswap_64(
200 struct mach_header_64 *mhp,
201 const char *segname,
202 const char *sectname,
203 int fSwap)
204 {
205 struct segment_command_64 *sgp;
206 struct section_64 *sp;
207 uint32_t i, j;
208
209 sgp = (struct segment_command_64 *)
210 ((char *)mhp + sizeof(struct mach_header_64));
211 for(i = 0; i < mhp->ncmds; i++){
212 if(sgp->cmd == (fSwap ? OSSwapInt32(LC_SEGMENT) : LC_SEGMENT)) {
213
214 if (fSwap) {
215 #ifdef __LITTLE_ENDIAN__
216 swap_segment_command_64(sgp, NX_BigEndian);
217 #else
218 swap_segment_command_64(sgp, NX_LittleEndian);
219 #endif /* __LITTLE_ENDIAN__ */
220 }
221
222 if(strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0 ||
223 mhp->filetype == MH_OBJECT){
224 sp = (struct section_64 *)((char *)sgp +
225 sizeof(struct segment_command_64));
226
227 if (fSwap) {
228 #ifdef __LITTLE_ENDIAN__
229 swap_section_64(sp, sgp->nsects, NX_BigEndian);
230 #else
231 swap_section_64(sp, sgp->nsects, NX_LittleEndian);
232 #endif /* __LITTLE_ENDIAN__ */
233 }
234
235 for(j = 0; j < sgp->nsects; j++){
236 if(strncmp(sp->sectname, sectname,
237 sizeof(sp->sectname)) == 0 &&
238 strncmp(sp->segname, segname,
239 sizeof(sp->segname)) == 0)
240 return(sp);
241 sp = (struct section_64 *)((char *)sp +
242 sizeof(struct section_64));
243 }
244 }
245 sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize);
246 } else {
247 sgp = (struct segment_command_64 *)((char *)sgp +
248 (fSwap ? OSSwapInt32(sgp->cmdsize) : sgp->cmdsize));
249 }
250 }
251 return((struct section_64 *)0);
252 }
253
254 /*
255 * This routine returns the a pointer the section structure of the named
256 * section in the named segment if it exist in the mach executable it is
257 * linked into. Otherwise it returns zero.
258 */
259 #ifndef __LP64__
260
261 const struct section *
262 getsectbyname(
263 const char *segname,
264 const char *sectname)
265 {
266 #ifndef __OPENSTEP__
267 struct mach_header *mhp = _NSGetMachExecuteHeader();
268 #else /* defined(__OPENSTEP__) */
269 static struct mach_header *mhp = NULL;
270 DECLARE_VAR(_mh_execute_header, struct mach_header);
271 SETUP_VAR(_mh_execute_header);
272 mhp = (struct mach_header *)(& USE_VAR(_mh_execute_header));
273 #endif /* __OPENSTEP__ */
274 return(getsectbynamefromheader(mhp, segname, sectname));
275 }
276
277 #else /* defined(__LP64__) */
278
279 const struct section_64 *
280 getsectbyname(
281 const char *segname,
282 const char *sectname)
283 {
284 struct mach_header_64 *mhp = _NSGetMachExecuteHeader();
285
286 return(getsectbynamefromheader_64(mhp, segname, sectname));
287 }
288
289 #endif /* defined(__LP64__) */
290
291 /*
292 * This routine returns the a pointer to the data for the named section in the
293 * named segment if it exist in the mach executable it is linked into. Also
294 * it returns the size of the section data indirectly through the pointer size.
295 * Otherwise it returns zero for the pointer and the size.
296 */
297 char *
298 getsectdata(
299 const char *segname,
300 const char *sectname,
301 unsigned long *size)
302 {
303 #ifndef __LP64__
304 const struct section *sp;
305 #else /* defined(__LP64__) */
306 const struct section_64 *sp;
307 #endif /* defined(__LP64__) */
308
309 sp = getsectbyname(segname, sectname);
310 if(sp == NULL){
311 *size = 0;
312 return(NULL);
313 }
314 *size = sp->size;
315 return((char *)(sp->addr));
316 }
317
318 /*
319 * This routine returns the a pointer to the section contents of the named
320 * section in the named segment if it exists in the image pointed to by the
321 * mach header. Otherwise it returns zero.
322 */
323 #ifndef __LP64__
324
325 uint8_t *
326 getsectiondata(
327 const struct mach_header *mhp,
328 const char *segname,
329 const char *sectname,
330 unsigned long *size)
331 {
332 struct segment_command *sgp, *zero;
333 struct section *sp, *find;
334 uint32_t i, j;
335
336 zero = 0;
337 find = 0;
338 sp = 0;
339 sgp = (struct segment_command *)
340 ((char *)mhp + sizeof(struct mach_header));
341 for(i = 0; i < mhp->ncmds; i++){
342 if(sgp->cmd == LC_SEGMENT){
343 if(zero == 0 && sgp->fileoff == 0 && sgp->nsects != 0){
344 zero = sgp;
345 if(find != 0)
346 goto done;
347 }
348 if(find == 0 &&
349 strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0){
350 sp = (struct section *)((char *)sgp +
351 sizeof(struct segment_command));
352 for(j = 0; j < sgp->nsects; j++){
353 if(strncmp(sp->sectname, sectname,
354 sizeof(sp->sectname)) == 0 &&
355 strncmp(sp->segname, segname,
356 sizeof(sp->segname)) == 0){
357 find = sp;
358 if(zero != 0)
359 goto done;
360 }
361 sp = (struct section *)((char *)sp +
362 sizeof(struct section));
363 }
364 }
365 }
366 sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
367 }
368 return(0);
369 done:
370 *size = sp->size;
371 return((uint8_t *)((uintptr_t)mhp - zero->vmaddr + sp->addr));
372 }
373
374 uint8_t *
375 getsegmentdata(
376 const struct mach_header *mhp,
377 const char *segname,
378 unsigned long *size)
379 {
380 struct segment_command *sgp, *zero, *find;
381 uint32_t i;
382
383 zero = 0;
384 find = 0;
385 sgp = (struct segment_command *)
386 ((char *)mhp + sizeof(struct mach_header));
387 for(i = 0; i < mhp->ncmds; i++){
388 if(sgp->cmd == LC_SEGMENT){
389 if(zero == 0 && sgp->fileoff == 0 && sgp->nsects != 0){
390 zero = sgp;
391 if(find != 0)
392 goto done;
393 }
394 if(find == 0 &&
395 strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0){
396 find = sgp;
397 if(zero != 0)
398 goto done;
399 }
400 }
401 sgp = (struct segment_command *)((char *)sgp + sgp->cmdsize);
402 }
403 return(0);
404 done:
405 *size = sgp->vmsize;
406 return((uint8_t *)((uintptr_t)mhp - zero->vmaddr + sgp->vmaddr));
407 }
408
409 #else /* defined(__LP64__) */
410
411 uint8_t *
412 getsectiondata(
413 const struct mach_header_64 *mhp,
414 const char *segname,
415 const char *sectname,
416 unsigned long *size)
417 {
418 struct segment_command_64 *sgp, *zero;
419 struct section_64 *sp, *find;
420 uint32_t i, j;
421
422 zero = 0;
423 find = 0;
424 sp = 0;
425 sgp = (struct segment_command_64 *)
426 ((char *)mhp + sizeof(struct mach_header_64));
427 for(i = 0; i < mhp->ncmds; i++){
428 if(sgp->cmd == LC_SEGMENT_64){
429 if(zero == 0 && sgp->fileoff == 0 && sgp->nsects != 0){
430 zero = sgp;
431 if(find != 0)
432 goto done;
433 }
434 if(find == 0 &&
435 strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0){
436 sp = (struct section_64 *)((char *)sgp +
437 sizeof(struct segment_command_64));
438 for(j = 0; j < sgp->nsects; j++){
439 if(strncmp(sp->sectname, sectname,
440 sizeof(sp->sectname)) == 0 &&
441 strncmp(sp->segname, segname,
442 sizeof(sp->segname)) == 0){
443 find = sp;
444 if(zero != 0)
445 goto done;
446 }
447 sp = (struct section_64 *)((char *)sp +
448 sizeof(struct section_64));
449 }
450 }
451 }
452 sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize);
453 }
454 return(0);
455 done:
456 *size = sp->size;
457 return((uint8_t *)((uintptr_t)mhp - zero->vmaddr + sp->addr));
458 }
459
460 uint8_t *
461 getsegmentdata(
462 const struct mach_header_64 *mhp,
463 const char *segname,
464 unsigned long *size)
465 {
466 struct segment_command_64 *sgp, *zero, *find;
467 uint32_t i;
468
469 zero = 0;
470 find = 0;
471 sgp = (struct segment_command_64 *)
472 ((char *)mhp + sizeof(struct mach_header_64));
473 for(i = 0; i < mhp->ncmds; i++){
474 if(sgp->cmd == LC_SEGMENT_64){
475 if(zero == 0 && sgp->fileoff == 0 && sgp->nsects != 0){
476 zero = sgp;
477 if(find != 0)
478 goto done;
479 }
480 if(find == 0 &&
481 strncmp(sgp->segname, segname, sizeof(sgp->segname)) == 0){
482 find = sgp;
483 if(zero != 0)
484 goto done;
485 }
486 }
487 sgp = (struct segment_command_64 *)((char *)sgp + sgp->cmdsize);
488 }
489 return(0);
490 done:
491 *size = sgp->vmsize;
492 return((uint8_t *)((uintptr_t)mhp - zero->vmaddr + sgp->vmaddr));
493 }
494
495 #endif /* defined(__LP64__) */
496
497 /*
498 * This routine returns the a pointer to the data for the named section in the
499 * named segment if it exist in the mach header passed to it. Also it returns
500 * the size of the section data indirectly through the pointer size. Otherwise
501 * it returns zero for the pointer and the size.
502 */
503 char *
504 getsectdatafromheader(
505 struct mach_header *mhp,
506 const char *segname,
507 const char *sectname,
508 unsigned long *size)
509 {
510 const struct section *sp;
511
512 sp = getsectbynamefromheader(mhp, segname, sectname);
513 if(sp == NULL){
514 *size = 0;
515 return(NULL);
516 }
517 *size = sp->size;
518 return((char *)((uintptr_t)(sp->addr)));
519 }
520
521 /*
522 * This routine returns the a pointer to the data for the named section in the
523 * named segment if it exist in the 64-bit mach header passed to it. Also it
524 * returns the size of the section data indirectly through the pointer size.
525 * Otherwise it returns zero for the pointer and the size.
526 */
527 char *
528 getsectdatafromheader_64(
529 struct mach_header_64 *mhp,
530 const char *segname,
531 const char *sectname,
532 unsigned long *size)
533 {
534 const struct section_64 *sp;
535
536 sp = getsectbynamefromheader_64(mhp, segname, sectname);
537 if(sp == NULL){
538 *size = 0;
539 return(NULL);
540 }
541 *size = sp->size;
542 return((char *)((uintptr_t)(sp->addr)));
543 }
544
545 #ifdef __DYNAMIC__
546 /*
547 * This routine returns the a pointer to the data for the named section in the
548 * named segment if it exist in the named Framework. Also it returns the size
549 * of the section data indirectly through the pointer size. Otherwise it
550 * returns zero for the pointer and the size. The last component of the path
551 * of the Framework is passed as FrameworkName.
552 */
553 void *
554 getsectdatafromFramework(
555 const char *FrameworkName,
556 const char *segname,
557 const char *sectname,
558 unsigned long *size)
559 {
560 uint32_t i, n;
561 uintptr_t vmaddr_slide;
562 #ifndef __LP64__
563 struct mach_header *mh;
564 const struct section *s;
565 #else /* defined(__LP64__) */
566 struct mach_header_64 *mh;
567 const struct section_64 *s;
568 #endif /* defined(__LP64__) */
569 char *name, *p;
570
571 n = _dyld_image_count();
572 for(i = 0; i < n ; i++){
573 name = _dyld_get_image_name(i);
574 p = strrchr(name, '/');
575 if(p != NULL && p[1] != '\0')
576 name = p + 1;
577 if(strcmp(name, FrameworkName) != 0)
578 continue;
579 mh = _dyld_get_image_header(i);
580 vmaddr_slide = _dyld_get_image_vmaddr_slide(i);
581 #ifndef __LP64__
582 s = getsectbynamefromheader(mh, segname, sectname);
583 #else /* defined(__LP64__) */
584 s = getsectbynamefromheader_64(mh, segname, sectname);
585 #endif /* defined(__LP64__) */
586 if(s == NULL){
587 *size = 0;
588 return(NULL);
589 }
590 *size = s->size;
591 return((void *)(s->addr + vmaddr_slide));
592 }
593 *size = 0;
594 return(NULL);
595 }
596 #endif /* __DYNAMIC__ */
597 #endif /* !defined(RLD) */
OLDNEW
« no previous file with comments | « third_party/apple_cctools/cctools/include/mach-o/getsect.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698