1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (c) 2014 Chris Johns <chrisj@rtems.org>. All rights reserved. |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #define DL06_DEBUG_TRACING 0 |
---|
29 | |
---|
30 | #include <stdio.h> |
---|
31 | |
---|
32 | #include <dlfcn.h> |
---|
33 | |
---|
34 | #include <rtems/rtl/rtl-trace.h> |
---|
35 | |
---|
36 | #include "dl-load.h" |
---|
37 | |
---|
38 | typedef int (*call_t)(int argc, const char* argv[]); |
---|
39 | |
---|
40 | static const char* call_args[] = { "1", "2", "3", "4" }; |
---|
41 | |
---|
42 | static void* dl_load_obj(const char* name) |
---|
43 | { |
---|
44 | void* handle; |
---|
45 | int unresolved; |
---|
46 | char* message = "loaded"; |
---|
47 | |
---|
48 | #if DL06_DEBUG_TRACING |
---|
49 | rtems_rtl_trace_set_mask(RTEMS_RTL_TRACE_ALL); |
---|
50 | #endif |
---|
51 | |
---|
52 | printf("\nload: %s\n", name); |
---|
53 | |
---|
54 | handle = dlopen (name, RTLD_NOW | RTLD_GLOBAL); |
---|
55 | if (!handle) |
---|
56 | { |
---|
57 | printf("dlopen failed: %s\n", dlerror()); |
---|
58 | return NULL; |
---|
59 | } |
---|
60 | |
---|
61 | if (dlinfo (handle, RTLD_DI_UNRESOLVED, &unresolved) < 0) |
---|
62 | message = "dlinfo error checking unresolved status"; |
---|
63 | else if (unresolved) |
---|
64 | message = "has unresolved externals"; |
---|
65 | |
---|
66 | printf ("handle: %p %s\n", handle, message); |
---|
67 | |
---|
68 | return handle; |
---|
69 | } |
---|
70 | |
---|
71 | int dl_load_test(void) |
---|
72 | { |
---|
73 | void* r1; |
---|
74 | call_t call; |
---|
75 | int call_ret; |
---|
76 | int ret; |
---|
77 | |
---|
78 | r1 = dl_load_obj("/dl06.rap"); |
---|
79 | if (!r1) |
---|
80 | return 1; |
---|
81 | |
---|
82 | #if 0 |
---|
83 | { |
---|
84 | char* list[] = { "rtl", "list", NULL }; |
---|
85 | rtems_rtl_shell_command (2, list); |
---|
86 | char* sym[] = { "rtl", "sym", NULL }; |
---|
87 | rtems_rtl_shell_command (2, sym); |
---|
88 | } |
---|
89 | #endif |
---|
90 | |
---|
91 | call = dlsym (r1, "rtems_main"); |
---|
92 | if (call == NULL) |
---|
93 | { |
---|
94 | printf("dlsym failed: symbol not found\n"); |
---|
95 | return 1; |
---|
96 | } |
---|
97 | |
---|
98 | call_ret = call (4, call_args); |
---|
99 | if (call_ret != 4) |
---|
100 | { |
---|
101 | printf("dlsym call failed: ret value bad\n"); |
---|
102 | return 1; |
---|
103 | } |
---|
104 | |
---|
105 | ret = 0; |
---|
106 | |
---|
107 | if (dlclose (r1) < 0) |
---|
108 | { |
---|
109 | printf("dlclose o1 failed: %s\n", dlerror()); |
---|
110 | ret = 1; |
---|
111 | } |
---|
112 | |
---|
113 | printf ("handle: %p closed\n", r1); |
---|
114 | |
---|
115 | return ret; |
---|
116 | } |
---|