1 | /* |
---|
2 | * Copyright (c) 2014 Chris Johns <chrisj@rtems.org>. |
---|
3 | * All rights reserved. |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.org/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | #define TEST_TRACE 0 |
---|
11 | #if TEST_TRACE |
---|
12 | #define DEBUG_TRACE (RTEMS_RTL_TRACE_DETAIL | \ |
---|
13 | RTEMS_RTL_TRACE_WARNING | \ |
---|
14 | RTEMS_RTL_TRACE_LOAD | \ |
---|
15 | RTEMS_RTL_TRACE_UNLOAD | \ |
---|
16 | RTEMS_RTL_TRACE_SYMBOL | \ |
---|
17 | RTEMS_RTL_TRACE_RELOC | \ |
---|
18 | RTEMS_RTL_TRACE_ALLOCATOR | \ |
---|
19 | RTEMS_RTL_TRACE_UNRESOLVED | \ |
---|
20 | RTEMS_RTL_TRACE_ARCHIVES | \ |
---|
21 | RTEMS_RTL_TRACE_DEPENDENCY) |
---|
22 | #define DL_DEBUG_TRACE DEBUG_TRACE /* RTEMS_RTL_TRACE_ALL */ |
---|
23 | #define DL_RTL_CMDS 1 |
---|
24 | #else |
---|
25 | #define DL_DEBUG_TRACE 0 |
---|
26 | #define DL_RTL_CMDS 0 |
---|
27 | #endif |
---|
28 | |
---|
29 | #include <dlfcn.h> |
---|
30 | |
---|
31 | #include "dl-load.h" |
---|
32 | |
---|
33 | #include <tmacros.h> |
---|
34 | |
---|
35 | #include <rtems/rtl/rtl-shell.h> |
---|
36 | #include <rtems/rtl/rtl-trace.h> |
---|
37 | |
---|
38 | typedef int (*call_sig)(void); |
---|
39 | |
---|
40 | static void dl_load_dump (void) |
---|
41 | { |
---|
42 | #if DL_RTL_CMDS |
---|
43 | char* list[] = { "rtl", "list", NULL }; |
---|
44 | char* sym[] = { "rtl", "sym", NULL }; |
---|
45 | printf ("RTL List:\n"); |
---|
46 | rtems_rtl_shell_command (2, list); |
---|
47 | printf ("RTL Sym:\n"); |
---|
48 | rtems_rtl_shell_command (2, sym); |
---|
49 | #endif |
---|
50 | } |
---|
51 | |
---|
52 | static void dl_check_resolved(void* handle, bool has_unresolved) |
---|
53 | { |
---|
54 | int unresolved = 0; |
---|
55 | rtems_test_assert (dlinfo (handle, RTLD_DI_UNRESOLVED, &unresolved) == 0); |
---|
56 | if (has_unresolved) |
---|
57 | { |
---|
58 | if (unresolved == 0) |
---|
59 | { |
---|
60 | dl_load_dump(); |
---|
61 | rtems_test_assert (unresolved != 0); |
---|
62 | } |
---|
63 | } |
---|
64 | else |
---|
65 | { |
---|
66 | if (unresolved != 0) |
---|
67 | { |
---|
68 | dl_load_dump(); |
---|
69 | rtems_test_assert (unresolved == 0); |
---|
70 | } |
---|
71 | } |
---|
72 | printf ("handel: %p: no unresolved externals\n", handle); |
---|
73 | } |
---|
74 | |
---|
75 | static void* dl_load_obj(const char* name, bool has_unresolved) |
---|
76 | { |
---|
77 | void* handle; |
---|
78 | |
---|
79 | printf("load: %s\n", name); |
---|
80 | |
---|
81 | handle = dlopen (name, RTLD_NOW | RTLD_GLOBAL); |
---|
82 | if (!handle) |
---|
83 | { |
---|
84 | printf("dlopen failed: %s\n", dlerror()); |
---|
85 | return NULL; |
---|
86 | } |
---|
87 | |
---|
88 | dl_check_resolved (handle, has_unresolved); |
---|
89 | |
---|
90 | printf ("handle: %p loaded\n", handle); |
---|
91 | |
---|
92 | return handle; |
---|
93 | } |
---|
94 | |
---|
95 | static void dl_close (void* handle) |
---|
96 | { |
---|
97 | int r; |
---|
98 | printf ("handle: %p closing\n", handle); |
---|
99 | r = dlclose (handle); |
---|
100 | if (r != 0) |
---|
101 | printf("dlclose failed: %s\n", dlerror()); |
---|
102 | rtems_test_assert (r == 0); |
---|
103 | } |
---|
104 | |
---|
105 | static int dl_call (void* handle, const char* func) |
---|
106 | { |
---|
107 | call_sig call = dlsym (handle, func); |
---|
108 | if (call == NULL) |
---|
109 | { |
---|
110 | printf("dlsym failed: symbol not found: %s\n", func); |
---|
111 | return 1; |
---|
112 | } |
---|
113 | call (); |
---|
114 | return 0; |
---|
115 | } |
---|
116 | |
---|
117 | int dl_load_test(void) |
---|
118 | { |
---|
119 | void* o1; |
---|
120 | |
---|
121 | printf ("Test source (link in strstr): %s\n", dl_localise_file (__FILE__)); |
---|
122 | |
---|
123 | #if DL_DEBUG_TRACE |
---|
124 | rtems_rtl_trace_set_mask (DL_DEBUG_TRACE); |
---|
125 | #endif |
---|
126 | |
---|
127 | o1 = dl_load_obj("/dl08-o1.o", false); |
---|
128 | if (!o1) |
---|
129 | return 1; |
---|
130 | |
---|
131 | dl_check_resolved (o1, false); |
---|
132 | |
---|
133 | dl_load_dump (); |
---|
134 | |
---|
135 | printf ("Running rtems_main_o1:\n"); |
---|
136 | if (dl_call (o1, "rtems_main_o1")) |
---|
137 | return 1; |
---|
138 | |
---|
139 | dl_close (o1); |
---|
140 | |
---|
141 | return 0; |
---|
142 | } |
---|