1 | /* |
---|
2 | * Copyright (c) 2014 Chris Johns <chrisj@rtems.org>. All rights reserved. |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.rtems.org/license/LICENSE. |
---|
7 | */ |
---|
8 | |
---|
9 | #include <stdio.h> |
---|
10 | |
---|
11 | #include <dlfcn.h> |
---|
12 | |
---|
13 | #include "dl-load.h" |
---|
14 | |
---|
15 | #include <rtems/rtl/rtl-shell.h> |
---|
16 | #include <rtems/rtl/rtl-trace.h> |
---|
17 | |
---|
18 | #define TEST_TRACE 0 |
---|
19 | #if TEST_TRACE |
---|
20 | #define DL_DEBUG_TRACE (RTEMS_RTL_TRACE_ALL & ~RTEMS_RTL_TRACE_ALLOCATOR) |
---|
21 | #define DL_RTL_CMDS 1 |
---|
22 | #else |
---|
23 | #define DL_DEBUG_TRACE 0 |
---|
24 | #define DL_RTL_CMDS 0 |
---|
25 | #endif |
---|
26 | |
---|
27 | typedef int (*call_t)(int argc, const char* argv[]); |
---|
28 | |
---|
29 | static const char* call_args[] = { "1", "2", "3", "4" }; |
---|
30 | |
---|
31 | static void* dl_load_obj(const char* name) |
---|
32 | { |
---|
33 | void* handle; |
---|
34 | int unresolved; |
---|
35 | char* message = "loaded"; |
---|
36 | |
---|
37 | printf("load: %s\n", name); |
---|
38 | |
---|
39 | handle = dlopen (name, RTLD_NOW | RTLD_GLOBAL); |
---|
40 | if (!handle) |
---|
41 | { |
---|
42 | printf("dlopen failed: %s\n", dlerror()); |
---|
43 | return NULL; |
---|
44 | } |
---|
45 | |
---|
46 | if (dlinfo (handle, RTLD_DI_UNRESOLVED, &unresolved) < 0) |
---|
47 | message = "dlinfo error checking unresolved status"; |
---|
48 | else if (unresolved) |
---|
49 | message = "has unresolved externals"; |
---|
50 | |
---|
51 | printf ("handle: %p %s\n", handle, message); |
---|
52 | |
---|
53 | return handle; |
---|
54 | } |
---|
55 | |
---|
56 | int dl_load_test(void) |
---|
57 | { |
---|
58 | void* o1; |
---|
59 | void* o2; |
---|
60 | call_t call; |
---|
61 | int call_ret; |
---|
62 | int ret; |
---|
63 | |
---|
64 | #if DL_DEBUG_TRACE |
---|
65 | rtems_rtl_trace_set_mask (DL_DEBUG_TRACE); |
---|
66 | #endif |
---|
67 | |
---|
68 | o1 = dl_load_obj("/dl02-o1.o"); |
---|
69 | if (!o1) |
---|
70 | return 1; |
---|
71 | o2 = dl_load_obj("/dl02-o2.o"); |
---|
72 | if (!o2) |
---|
73 | return 1; |
---|
74 | |
---|
75 | #if DL_RTL_CMDS |
---|
76 | { |
---|
77 | char* list[] = { "rtl", "list", NULL }; |
---|
78 | rtems_rtl_shell_command (2, list); |
---|
79 | char* sym[] = { "rtl", "sym", NULL }; |
---|
80 | rtems_rtl_shell_command (2, sym); |
---|
81 | } |
---|
82 | #endif |
---|
83 | |
---|
84 | call = dlsym (o1, "rtems_main"); |
---|
85 | if (call == NULL) |
---|
86 | { |
---|
87 | printf("dlsym failed: symbol not found\n"); |
---|
88 | return 1; |
---|
89 | } |
---|
90 | |
---|
91 | call_ret = call (4, call_args); |
---|
92 | if (call_ret != 4) |
---|
93 | { |
---|
94 | printf("dlsym call failed: ret value bad\n"); |
---|
95 | return 1; |
---|
96 | } |
---|
97 | |
---|
98 | ret = 0; |
---|
99 | |
---|
100 | if (dlclose (o1) < 0) |
---|
101 | { |
---|
102 | printf("dlclose o1 failed: %s\n", dlerror()); |
---|
103 | ret = 1; |
---|
104 | } |
---|
105 | |
---|
106 | printf ("handle: %p closed\n", o1); |
---|
107 | |
---|
108 | if (dlclose (o2) < 0) |
---|
109 | { |
---|
110 | printf("dlclose o1 failed: %s\n", dlerror()); |
---|
111 | ret = 1; |
---|
112 | } |
---|
113 | |
---|
114 | printf ("handle: %p closed\n", o2); |
---|
115 | |
---|
116 | return ret; |
---|
117 | } |
---|