1 2 #include <stdio.h> 3 #include "module.h" 4 5 #if SPECIAL_MAGIC_DEFINE != 42 6 #error "SPECIAL_MAGIC_DEFINE is not defined" 7 #endif 8 9 int func_from_language_runtime(void); 10 typedef int (*fptr) (void); 11 12 #ifdef _WIN32 13 14 #include <windows.h> 15 16 static wchar_t* 17 win32_get_last_error (void) 18 { 19 wchar_t *msg = NULL; 20 21 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER 22 | FORMAT_MESSAGE_IGNORE_INSERTS 23 | FORMAT_MESSAGE_FROM_SYSTEM, 24 NULL, GetLastError (), 0, 25 (LPWSTR) &msg, 0, NULL); 26 return msg; 27 } 28 29 int main(int argc, char **argv) 30 { 31 HINSTANCE handle; 32 fptr importedfunc; 33 int expected, actual; 34 int ret = 1; 35 if(argc==0) {}; 36 37 handle = LoadLibraryA (argv[1]); 38 if (!handle) { 39 wchar_t *msg = win32_get_last_error (); 40 printf ("Could not open %s: %S\n", argv[1], msg); 41 goto nohandle; 42 } 43 44 importedfunc = (fptr) GetProcAddress (handle, "func"); 45 if (importedfunc == NULL) { 46 wchar_t *msg = win32_get_last_error (); 47 printf ("Could not find 'func': %S\n", msg); 48 goto out; 49 } 50 51 actual = importedfunc (); 52 expected = func_from_language_runtime (); 53 if (actual != expected) { 54 printf ("Got %i instead of %i\n", actual, expected); 55 goto out; 56 } 57 58 ret = 0; 59 out: 60 FreeLibrary (handle); 61 nohandle: 62 return ret; 63 } 64 65 #else 66 67 #include<dlfcn.h> 68 #include<assert.h> 69 70 int main(int argc, char **argv) { 71 void *dl; 72 fptr importedfunc; 73 int expected, actual; 74 char *error; 75 int ret = 1; 76 if(argc==0) {}; 77 78 dlerror(); 79 dl = dlopen(argv[1], RTLD_LAZY); 80 error = dlerror(); 81 if(error) { 82 printf("Could not open %s: %s\n", argv[1], error); 83 goto nodl; 84 } 85 86 importedfunc = (fptr) dlsym(dl, "func"); 87 if (importedfunc == NULL) { 88 printf ("Could not find 'func'\n"); 89 goto out; 90 } 91 92 assert(importedfunc != func_from_language_runtime); 93 94 actual = (*importedfunc)(); 95 expected = func_from_language_runtime (); 96 if (actual != expected) { 97 printf ("Got %i instead of %i\n", actual, expected); 98 goto out; 99 } 100 101 ret = 0; 102 out: 103 dlclose(dl); 104 nodl: 105 return ret; 106 } 107 108 #endif 109