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 DEBUG_TRACE (RTEMS_RTL_TRACE_DETAIL | \ |
---|
21 | RTEMS_RTL_TRACE_WARNING | \ |
---|
22 | RTEMS_RTL_TRACE_LOAD | \ |
---|
23 | RTEMS_RTL_TRACE_UNLOAD | \ |
---|
24 | RTEMS_RTL_TRACE_SYMBOL | \ |
---|
25 | RTEMS_RTL_TRACE_RELOC | \ |
---|
26 | RTEMS_RTL_TRACE_ALLOCATOR | \ |
---|
27 | RTEMS_RTL_TRACE_UNRESOLVED | \ |
---|
28 | RTEMS_RTL_TRACE_ARCHIVES | \ |
---|
29 | RTEMS_RTL_TRACE_GLOBAL_SYM | \ |
---|
30 | RTEMS_RTL_TRACE_DEPENDENCY) |
---|
31 | #define DL_DEBUG_TRACE DEBUG_TRACE /* RTEMS_RTL_TRACE_ALL */ |
---|
32 | #define DL_RTL_CMDS 1 |
---|
33 | #else |
---|
34 | #define DL_DEBUG_TRACE 0 |
---|
35 | #define DL_RTL_CMDS 0 |
---|
36 | #endif |
---|
37 | |
---|
38 | static void dl_load_dump (void) |
---|
39 | { |
---|
40 | #if DL_RTL_CMDS |
---|
41 | char* list[] = { "rtl", "list", NULL }; |
---|
42 | char* sym[] = { "rtl", "sym", NULL }; |
---|
43 | printf ("RTL List:\n"); |
---|
44 | rtems_rtl_shell_command (2, list); |
---|
45 | printf ("RTL Sym:\n"); |
---|
46 | rtems_rtl_shell_command (2, sym); |
---|
47 | #endif |
---|
48 | } |
---|
49 | |
---|
50 | typedef int (*call_t)(int argc, const char* argv[]); |
---|
51 | |
---|
52 | static const char* call_1[] = { "Line 1", "Line 2" }; |
---|
53 | static const char* call_2[] = { "Call 2, line 1", |
---|
54 | "Call 2, line 2", |
---|
55 | "Call 2, line 3" }; |
---|
56 | |
---|
57 | int dl_load_test(void) |
---|
58 | { |
---|
59 | void* handle; |
---|
60 | call_t call; |
---|
61 | int call_ret; |
---|
62 | int unresolved; |
---|
63 | char* message = "loaded"; |
---|
64 | |
---|
65 | #if DL_DEBUG_TRACE |
---|
66 | rtems_rtl_trace_set_mask (DL_DEBUG_TRACE); |
---|
67 | #endif |
---|
68 | |
---|
69 | printf("load: /dl01-o1.o\n"); |
---|
70 | |
---|
71 | handle = dlopen ("/dl01-o1.o", RTLD_NOW | RTLD_GLOBAL); |
---|
72 | if (!handle) |
---|
73 | { |
---|
74 | printf("dlopen failed: %s\n", dlerror()); |
---|
75 | return 1; |
---|
76 | } |
---|
77 | |
---|
78 | if (dlinfo (handle, RTLD_DI_UNRESOLVED, &unresolved) < 0) |
---|
79 | message = "dlinfo error checking unresolved status"; |
---|
80 | else if (unresolved) |
---|
81 | message = "has unresolved externals"; |
---|
82 | |
---|
83 | printf ("handle: %p %s\n", handle, message); |
---|
84 | |
---|
85 | dl_load_dump (); |
---|
86 | |
---|
87 | call = dlsym (handle, "rtems_main"); |
---|
88 | if (call == NULL) |
---|
89 | { |
---|
90 | printf("dlsym failed: symbol not found\n"); |
---|
91 | return 1; |
---|
92 | } |
---|
93 | |
---|
94 | call_ret = call (2, call_1); |
---|
95 | if (call_ret != 2) |
---|
96 | { |
---|
97 | printf("dlsym call failed: ret value bad\n"); |
---|
98 | return 1; |
---|
99 | } |
---|
100 | |
---|
101 | call_ret = call (3, call_2); |
---|
102 | if (call_ret != 3) |
---|
103 | { |
---|
104 | printf("dlsym call failed: ret value bad\n"); |
---|
105 | return 1; |
---|
106 | } |
---|
107 | |
---|
108 | if (dlclose (handle) < 0) |
---|
109 | { |
---|
110 | printf("dlclose failed: %s\n", dlerror()); |
---|
111 | return 1; |
---|
112 | } |
---|
113 | |
---|
114 | printf ("handle: %p closed\n", handle); |
---|
115 | |
---|
116 | return 0; |
---|
117 | } |
---|