132-13
[darwin-xtools.git] / dyld / unit-tests / test-cases / all_image_infos / main.c
blob30a84275bcfbe9274f1d5c2ae339fd1b6dabec32
1 /*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
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.
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.
21 * @APPLE_LICENSE_HEADER_END@
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>
29 #include "test.h"
31 extern struct mach_header __dso_handle;
33 #ifndef DYLD_ALL_IMAGE_INFOS_OFFSET_OFFSET
34 #define DYLD_ALL_IMAGE_INFOS_OFFSET_OFFSET 0x1010
35 #endif
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
46 struct dyld_all_image_infos* getImageInfos()
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);
53 uintptr_t addr = DYLD_BASE_ADDRESS + offset;
54 return (struct dyld_all_image_infos*)addr;
58 struct dyld_all_image_infos* getImageInfosFromKernel()
60 task_dyld_info_data_t task_dyld_info;
61 mach_msg_type_number_t count = TASK_DYLD_INFO_COUNT;
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);
67 return (struct dyld_all_image_infos*)(uintptr_t)task_dyld_info.all_image_info_addr;
71 int
72 main()
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);
80 if ( infos->infoArrayCount < 2 ) {
81 FAIL("all_image_infos: dyld_all_image_infos.infoArrayCount is < 2");
82 exit(0);
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);
90 if ( getImageInfosFromKernel() != infos ) {
91 FAIL("all_image_infos: task_info and dyld disagree");
92 exit(0);
95 PASS("all_image_infos");
96 return EXIT_SUCCESS;