]> git.saurik.com Git - apple/dyld.git/blob - unit-tests/test-cases/text-relocs-perms/foo.c
dyld-732.8.tar.gz
[apple/dyld.git] / unit-tests / test-cases / text-relocs-perms / foo.c
1 /*
2 * Copyright (c) 2007-2010 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 <stdio.h> // fprintf(), NULL
24 #include <stdlib.h> // exit(), EXIT_SUCCESS
25 #include <unistd.h>
26 #include <Availability.h>
27 #include <mach-o/getsect.h>
28 #include <mach-o/ldsyms.h>
29 #include <mach/mach.h>
30 #include <mach/mach_vm.h>
31
32 #include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
33
34 #if __MAC_OS_X_VERSION_MIN_REQUIRED
35
36 static vm_prot_t getPermission(void* addr)
37 {
38 mach_vm_address_t address = (mach_vm_address_t)(uintptr_t)addr;
39 kern_return_t result;
40 mach_port_t object_name;
41 vm_region_basic_info_data_64_t info;
42 mach_msg_type_number_t count;
43 mach_vm_size_t size = 4096;
44
45 count = VM_REGION_BASIC_INFO_COUNT_64;
46 result = mach_vm_region(mach_task_self(),
47 &address,
48 &size,
49 VM_REGION_BASIC_INFO_64,
50 (vm_region_info_t)&info,
51 &count,
52 &object_name);
53 if ( result == KERN_SUCCESS )
54 return info.protection;
55 return 0;
56 }
57
58
59 static void* getStubAddr()
60 {
61 #if __LP64__
62 uint64_t size;
63 #else
64 uint32_t size;
65 #endif
66 uintptr_t slide = (uintptr_t)&_mh_dylib_header; // assume dylib is zero-base so slide == load address
67 #if __i386__
68 return getsectdatafromheader(&_mh_dylib_header, "__IMPORT", "__symbol_stub", &size) + slide;
69 #elif __ppc__
70 return getsectdatafromheader(&_mh_dylib_header, "TEXT", "__picsymbolstub1", &size) + slide;
71 #elif __ppc64__
72 return getsectdatafromheader_64(&_mh_dylib_header, "__TEXT", "__picsymbolstub1", &size) + slide;
73 #elif __x86_64__
74 return getsectdatafromheader_64(&_mh_dylib_header, "__TEXT", "__symbol_stub1", &size) + slide;
75 #elif __arm__
76 void* p = getsectdata("__TEXT", "__picsymbolstub4", (unsigned long*)&size);
77 if ( p != NULL )
78 return getsectdatafromheader(&_mh_dylib_header, "__TEXT", "__picsymbolstub4", &size) + slide;
79 return getsectdatafromheader(&_mh_dylib_header, "__TEXT", "__symbolstub1", &size) + slide;
80 #else
81 #error unknown arch
82 #endif
83 }
84
85
86 static void checkStubs(void* addr)
87 {
88 vm_prot_t perm = getPermission(addr);
89 //fprintf(stderr, "perm=0x%02X\n", perm);
90 if ( (perm == 0) || ((perm & VM_PROT_EXECUTE) == 0) ) {
91 FAIL("text-reloc-perms: missing exec permission 0x%0X at address %p", perm, addr);
92 exit(0);
93 }
94 }
95
96
97 void foo()
98 {
99 void* stubAddr = getStubAddr();
100 checkStubs(stubAddr);
101 }
102
103 #else
104
105 void foo()
106 {
107 // iOS does not have text relocs
108 }
109
110 #endif
111