Back to home page

LXR

 
 

    


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

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_DEBUG_CMD   1
0045 #else
0046 #define DL_DEBUG_TRACE 0
0047 #define DL_DEBUG_CMD    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_DEBUG_CMD
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 bool dl_check_resolved(void* handle, bool has_unresolved)
0074 {
0075   int unresolved = 0;
0076   if (dlinfo (handle, RTLD_DI_UNRESOLVED, &unresolved) == 0)
0077     return 1;
0078   if (has_unresolved)
0079   {
0080     if (unresolved == 0)
0081     {
0082       dl_load_dump();
0083       return false;
0084     }
0085   }
0086   else
0087   {
0088     if (unresolved != 0)
0089     {
0090       dl_load_dump();
0091       return false;
0092     }
0093   }
0094   printf ("handel: %p: no unresolved externals\n", handle);
0095   return true;
0096 }
0097 
0098 static void* dl_load_obj(const char* name, bool has_unresolved)
0099 {
0100   void* handle;
0101 
0102   printf("load: %s\n", name);
0103 
0104   handle = dlopen (name, RTLD_NOW | RTLD_GLOBAL);
0105   if (!handle)
0106   {
0107     printf("dlopen failed: %s\n", dlerror());
0108     return NULL;
0109   }
0110 
0111   dl_check_resolved (handle, has_unresolved);
0112 
0113   printf ("handle: %p loaded\n", handle);
0114 
0115   return handle;
0116 }
0117 
0118 int dl_load_test(void)
0119 {
0120   void* o1;
0121 
0122   printf ("Test source (link in strstr): %s\n", dl_localise_file (__FILE__));
0123 
0124 #if DL_DEBUG_TRACE
0125   rtems_rtl_trace_set_mask (DL_DEBUG_TRACE);
0126 #endif
0127 
0128   o1 = dl_load_obj("/dl10-o1.o", false);
0129   if (!o1)
0130     return 1;
0131 
0132   if (!dl_check_resolved (o1, false))
0133     return 1;
0134 
0135   dl_load_dump ();
0136 
0137   return 0;
0138 }