]> git.saurik.com Git - apple/dyld.git/blob - unit-tests/test-cases/all_image_infos/main.c
dyld-132.13.tar.gz
[apple/dyld.git] / unit-tests / test-cases / all_image_infos / main.c
1 /*
2 * Copyright (c) 2008 Apple 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 #include <stdlib.h> // EXIT_SUCCESS
24 #include <stdio.h>
25 #include <mach-o/dyld.h>
26 #include <mach-o/dyld_images.h>
27 #include <mach/mach.h>
28
29 #include "test.h"
30
31 extern struct mach_header __dso_handle;
32
33 #ifndef DYLD_ALL_IMAGE_INFOS_OFFSET_OFFSET
34 #define DYLD_ALL_IMAGE_INFOS_OFFSET_OFFSET 0x1010
35 #endif
36
37
38 #if __i386__ || __ppc__
39 #define DYLD_BASE_ADDRESS 0x8fe00000
40 #elif __x86_64__ || __ppc64__
41 #define DYLD_BASE_ADDRESS 0x7fff5fc00000
42 #elif __arm__
43 #define DYLD_BASE_ADDRESS 0x2fe00000
44 #endif
45
46 struct dyld_all_image_infos* getImageInfos()
47 {
48 uint32_t offset = *((uint32_t*)(DYLD_BASE_ADDRESS+DYLD_ALL_IMAGE_INFOS_OFFSET_OFFSET));
49 if ( offset > 300000 ) {
50 FAIL("all_image_infos: offset appears to be outside dyld");
51 exit(0);
52 }
53 uintptr_t addr = DYLD_BASE_ADDRESS + offset;
54 return (struct dyld_all_image_infos*)addr;
55 }
56
57
58 struct dyld_all_image_infos* getImageInfosFromKernel()
59 {
60 task_dyld_info_data_t task_dyld_info;
61 mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
62
63 if ( task_info(mach_task_self(), TASK_DYLD_INFO, (task_info_t)&task_dyld_info, &count) ) {
64 FAIL("all_image_infos: task_info() failed");
65 exit(0);
66 }
67 return (struct dyld_all_image_infos*)(uintptr_t)task_dyld_info.all_image_info_addr;
68 }
69
70
71 int
72 main()
73 {
74 struct dyld_all_image_infos* infos = getImageInfos();
75 if ( infos->version != 7 ) {
76 FAIL("all_image_infos: dyld_all_image_infos is not version 7");
77 exit(0);
78 }
79
80 if ( infos->infoArrayCount < 2 ) {
81 FAIL("all_image_infos: dyld_all_image_infos.infoArrayCount is < 2");
82 exit(0);
83 }
84
85 if ( infos->infoArray[0].imageLoadAddress != &__dso_handle ) {
86 FAIL("all_image_infos: dyld_all_image_infos.infoArray for main executable is wrong");
87 exit(0);
88 }
89
90 if ( getImageInfosFromKernel() != infos ) {
91 FAIL("all_image_infos: task_info and dyld disagree");
92 exit(0);
93 }
94
95 PASS("all_image_infos");
96 return EXIT_SUCCESS;
97 }
98
99