Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:32

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (c) 2014 Chris Johns <chrisj@rtems.org>.
0005  * All rights reserved.
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #define TEST_TRACE 0
0030 #if TEST_TRACE
0031  #define DEBUG_TRACE (RTEMS_RTL_TRACE_DETAIL | \
0032                       RTEMS_RTL_TRACE_WARNING | \
0033                       RTEMS_RTL_TRACE_LOAD | \
0034                       RTEMS_RTL_TRACE_UNLOAD | \
0035                       RTEMS_RTL_TRACE_SYMBOL | \
0036                       RTEMS_RTL_TRACE_RELOC | \
0037                       RTEMS_RTL_TRACE_LOAD_SECT | \
0038                       RTEMS_RTL_TRACE_ALLOCATOR | \
0039                       RTEMS_RTL_TRACE_UNRESOLVED | \
0040                       RTEMS_RTL_TRACE_ARCHIVES | \
0041                       RTEMS_RTL_TRACE_DEPENDENCY)
0042  /* RTEMS_RTL_TRACE_ALL */
0043  #define DL_DEBUG_TRACE DEBUG_TRACE
0044  #define DL_RTL_CMDS    1
0045 #else
0046  #define DL_DEBUG_TRACE 0
0047  #define DL_RTL_CMDS    0
0048 #endif
0049 
0050 #include <dlfcn.h>
0051 
0052 #include "dl-load.h"
0053 
0054 #include <tmacros.h>
0055 
0056 #include <rtems/rtl/rtl-shell.h>
0057 #include <rtems/rtl/rtl-trace.h>
0058 
0059 typedef int (*call_sig)(void);
0060 
0061 static void dl_load_dump (void)
0062 {
0063 #if DL_RTL_CMDS
0064   char* list[] = { "rtl", "list", NULL };
0065   char* sym[] = { "rtl", "sym", NULL };
0066   printf ("RTL List:\n");
0067   rtems_rtl_shell_command (2, list);
0068   printf ("RTL Sym:\n");
0069   rtems_rtl_shell_command (2, sym);
0070 #endif
0071 }
0072 
0073 static void dl_check_resolved(void* handle, bool has_unresolved)
0074 {
0075   int unresolved = 0;
0076   rtems_test_assert (dlinfo (handle, RTLD_DI_UNRESOLVED, &unresolved) == 0);
0077   if (has_unresolved)
0078   {
0079     if (unresolved == 0)
0080     {
0081       dl_load_dump();
0082       rtems_test_assert (unresolved != 0);
0083     }
0084   }
0085   else
0086   {
0087     if (unresolved != 0)
0088     {
0089       dl_load_dump();
0090       rtems_test_assert (unresolved == 0);
0091     }
0092   }
0093   printf ("handel: %p: no unresolved externals\n", handle);
0094 }
0095 
0096 static void* dl_load_obj(const char* name, bool has_unresolved)
0097 {
0098   void* handle;
0099 
0100   printf("load: %s\n", name);
0101 
0102   handle = dlopen (name, RTLD_NOW | RTLD_GLOBAL);
0103   if (!handle)
0104   {
0105     printf("dlopen failed: %s\n", dlerror());
0106     return NULL;
0107   }
0108 
0109   dl_check_resolved (handle, has_unresolved);
0110 
0111   printf ("handle: %p loaded\n", handle);
0112 
0113   return handle;
0114 }
0115 
0116 static void dl_close (void* handle)
0117 {
0118   int r;
0119   printf ("handle: %p closing\n", handle);
0120   r = dlclose (handle);
0121   if (r != 0)
0122     printf("dlclose failed: %s\n", dlerror());
0123   rtems_test_assert (r == 0);
0124 }
0125 
0126 static int dl_call (void* handle, const char* func)
0127 {
0128   call_sig call = dlsym (handle, func);
0129   if (call == NULL)
0130   {
0131     printf("dlsym failed: symbol not found: %s\n", func);
0132     return 1;
0133   }
0134   call ();
0135   return 0;
0136 }
0137 
0138 int dl_load_test(void)
0139 {
0140   void* o1;
0141 
0142   printf ("Test source (link in strstr): %s\n", dl_localise_file (__FILE__));
0143 
0144 #if DL_DEBUG_TRACE
0145   rtems_rtl_trace_set_mask (DL_DEBUG_TRACE);
0146 #endif
0147 
0148   o1 = dl_load_obj("/dl08-o1.o", false);
0149   if (!o1)
0150     return 1;
0151 
0152   dl_check_resolved (o1, false);
0153 
0154   dl_load_dump ();
0155 
0156   printf ("Running rtems_main_o1:\n");
0157   if (dl_call (o1, "rtems_main_o1"))
0158     return 1;
0159 
0160   dl_close (o1);
0161 
0162   return 0;
0163 }