dyld-625.13.tar.gz
[apple/dyld.git] / testing / test-cases / dlopen-haswell.dtest / main.c
1 // BUILD_ONLY: MacOSX
2
3 // BUILD: $CC a.c -dynamiclib -arch x86_64h -o $BUILD_DIR/libHaswellCheck.dylib -install_name $RUN_DIR/libHaswellCheck.dylib
4 // BUILD: $CC main.c -o $BUILD_DIR/dlopen-haswell.exe -DRUN_DIR="$RUN_DIR"
5
6 // RUN: ./dlopen-haswell.exe
7
8
9 #include <stdbool.h>
10 #include <stdio.h>
11 #include <dlfcn.h>
12 #include <mach/host_info.h>
13 #include <mach/mach.h>
14 #include <mach/mach_host.h>
15
16 typedef bool (*BoolFunc)(void);
17
18
19 bool isHaswell_dynamic()
20 {
21 struct host_basic_info info;
22 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
23 mach_port_t hostPort = mach_host_self();
24 kern_return_t result = host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&info, &count);
25 mach_port_deallocate(mach_task_self(), hostPort);
26 if ( result == KERN_SUCCESS ) {
27 return (info.cpu_subtype == CPU_SUBTYPE_X86_64_H);
28 }
29 return false;
30 }
31
32
33 int main(int arg, const char* argv[])
34 {
35 printf("[BEGIN] dlopen-haswell\n");
36
37 void* handle = dlopen(RUN_DIR "/libHaswellCheck.dylib", RTLD_LAZY);
38 if ( handle == NULL ) {
39 printf("dlerror(): %s\n", dlerror());
40 printf("[FAIL] dlopen-haswell dlopen\n");
41 return 0;
42 }
43
44 BoolFunc libFunc = (BoolFunc)dlsym(handle, "isHaswell");
45 if ( libFunc == NULL ) {
46 printf("dlerror(): %s\n", dlerror());
47 printf("[FAIL] dlopen-haswell dlsym\n");
48 return 0;
49 }
50
51 // check if haswell slice of libHaswellCheck.dylib was loaded on haswell machines
52 bool dylibIsHaswellSlice = (*libFunc)();
53 bool runtimeIsHaswell = isHaswell_dynamic();
54
55 if ( dylibIsHaswellSlice != runtimeIsHaswell )
56 printf("[FAIL] dlopen-haswell, dylibIsHaswellSlice=%d, runtimeIsHaswell=%d\n", dylibIsHaswellSlice, runtimeIsHaswell);
57 else
58 printf("[PASS] dlopen-haswell\n");
59
60 return 0;
61 }
62
63
64