dyld-832.7.1.tar.gz
[apple/dyld.git] / testing / test-cases / dlopen-haswell.dtest / main.c
1 // BUILD(macos): $CC a.c -dynamiclib -arch x86_64h -o $BUILD_DIR/libHaswellCheck.dylib -install_name $RUN_DIR/libHaswellCheck.dylib
2 // BUILD(macos): $CC main.c -o $BUILD_DIR/dlopen-haswell.exe -DRUN_DIR="$RUN_DIR"
3
4 // BUILD(ios,tvos,watchos,bridgeos):
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 #include "test_support.h"
17
18 typedef bool (*BoolFunc)(void);
19
20
21 bool isHaswell_dynamic()
22 {
23 struct host_basic_info info;
24 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
25 mach_port_t hostPort = mach_host_self();
26 kern_return_t result = host_info(hostPort, HOST_BASIC_INFO, (host_info_t)&info, &count);
27 mach_port_deallocate(mach_task_self(), hostPort);
28 if ( result == KERN_SUCCESS ) {
29 return (info.cpu_subtype == CPU_SUBTYPE_X86_64_H);
30 }
31 return false;
32 }
33
34
35 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
36 void* handle = dlopen(RUN_DIR "/libHaswellCheck.dylib", RTLD_LAZY);
37 if ( handle == NULL ) {
38 FAIL("dlopen(\"" RUN_DIR "/libHaswellCheck.dylib\") error: %s", dlerror());
39 }
40
41 BoolFunc libFunc = (BoolFunc)dlsym(handle, "isHaswell");
42 if ( libFunc == NULL ) {
43 FAIL("dlsym(\"isHaswell\") error: %s", dlerror());
44 }
45
46 // check if haswell slice of libHaswellCheck.dylib was loaded on haswell machines
47 bool dylibIsHaswellSlice = (*libFunc)();
48 bool runtimeIsHaswell = isHaswell_dynamic();
49
50 if ( dylibIsHaswellSlice != runtimeIsHaswell )
51 FAIL("dlopen-haswell, dylibIsHaswellSlice=%d, runtimeIsHaswell=%d", dylibIsHaswellSlice, runtimeIsHaswell);
52 else
53 PASS("Success");
54 }
55
56
57