File indexing completed on 2025-05-11 08:24:33
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030 #include <stdio.h>
0031
0032 #include <dlfcn.h>
0033
0034 #include "dl-load.h"
0035
0036 #include <rtems/rtl/rtl-shell.h>
0037 #include <rtems/rtl/rtl-trace.h>
0038
0039 #define TEST_TRACE 0
0040 #if TEST_TRACE
0041 #define DL_DEBUG_TRACE (RTEMS_RTL_TRACE_ALL & ~RTEMS_RTL_TRACE_ALLOCATOR)
0042 #define DL_RTL_CMDS 1
0043 #else
0044 #define DL_DEBUG_TRACE 0
0045 #define DL_RTL_CMDS 0
0046 #endif
0047
0048 typedef const void *(*call_t)(void);
0049
0050 static void* dl_load_obj(const char* name)
0051 {
0052 void* handle;
0053 int unresolved;
0054 char* message = "loaded";
0055
0056 printf("load: %s\n", name);
0057
0058 handle = dlopen (name, RTLD_NOW | RTLD_GLOBAL);
0059 if (!handle)
0060 {
0061 printf("dlopen failed: %s\n", dlerror());
0062 return NULL;
0063 }
0064
0065 if (dlinfo (handle, RTLD_DI_UNRESOLVED, &unresolved) < 0)
0066 message = "dlinfo error checking unresolved status";
0067 else if (unresolved)
0068 message = "has unresolved externals";
0069
0070 printf ("handle: %p %s\n", handle, message);
0071
0072 return handle;
0073 }
0074
0075 int dl_load_test(void)
0076 {
0077 void* handle;
0078 call_t call;
0079 const void* dl_o1_str_addr;
0080 const void* dl_o2_str_addr;
0081
0082 #if DL_DEBUG_TRACE
0083 rtems_rtl_trace_set_mask (DL_DEBUG_TRACE);
0084 #endif
0085
0086 handle = dl_load_obj("/dl12-inc.o");
0087 if (!handle)
0088 return 1;
0089
0090 #if DL_RTL_CMDS
0091 {
0092 char* list[] = { "rtl", "list", NULL };
0093 rtems_rtl_shell_command (2, list);
0094 char* sym[] = { "rtl", "sym", NULL };
0095 rtems_rtl_shell_command (2, sym);
0096 }
0097 #endif
0098
0099 call = dlsym (handle, "dl_o1_func");
0100 if (call == NULL)
0101 {
0102 printf("dlsym failed: symbol not found\n");
0103 return 1;
0104 }
0105
0106 printf("\nAbout to call dl_o1_func at address %p\n", call);
0107
0108 dl_o1_str_addr = call ();
0109
0110 printf("\ndl_o1_func string literal address: %p\n", dl_o1_str_addr);
0111
0112 call = dlsym (handle, "dl_o2_func");
0113 if (call == NULL)
0114 {
0115 printf("dlsym failed: symbol not found\n");
0116 return 1;
0117 }
0118
0119 printf("\nAbout to call dl_o2_func at address %p\n", call);
0120
0121 dl_o2_str_addr = call ();
0122
0123 printf("\ndl_o2_func string literal address: %p\n", dl_o2_str_addr);
0124
0125 if (dl_o1_str_addr == dl_o2_str_addr) {
0126 printf("\nTwo symbols from different object files have the same address!\n");
0127 return 1;
0128 }
0129
0130 if (dlclose (handle) < 0)
0131 {
0132 printf("dlclose obj failed: %s\n", dlerror());
0133 return 1;
0134 }
0135
0136 printf ("\nhandle: %p closed\n", handle);
0137
0138 return 0;
0139 }