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 at rtems.org>.  All rights reserved.
0005  * Copyright (c) 2019 On-Line Applications Research
0006  * Copyright (c) 2024 Ranulfo Raphael
0007  *
0008  * Redistribution and use in source and binary forms, with or without
0009  * modification, are permitted provided that the following conditions
0010  * are met:
0011  * 1. Redistributions of source code must retain the above copyright
0012  *    notice, this list of conditions and the following disclaimer.
0013  * 2. Redistributions in binary form must reproduce the above copyright
0014  *    notice, this list of conditions and the following disclaimer in the
0015  *    documentation and/or other materials provided with the distribution.
0016  *
0017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0027  * POSSIBILITY OF SUCH DAMAGE.
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 }