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>.  All rights reserved.
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #include <stdio.h>
0029 
0030 #include <dlfcn.h>
0031 
0032 #include "dl-load.h"
0033 
0034 #include <rtems/rtl/rtl-shell.h>
0035 #include <rtems/rtl/rtl-trace.h>
0036 
0037 #define TEST_TRACE 0
0038 #if TEST_TRACE
0039  #define DEBUG_TRACE (RTEMS_RTL_TRACE_DETAIL | \
0040                       RTEMS_RTL_TRACE_WARNING | \
0041                       RTEMS_RTL_TRACE_LOAD | \
0042                       RTEMS_RTL_TRACE_UNLOAD | \
0043                       RTEMS_RTL_TRACE_SYMBOL | \
0044                       RTEMS_RTL_TRACE_RELOC | \
0045                       RTEMS_RTL_TRACE_ALLOCATOR | \
0046                       RTEMS_RTL_TRACE_UNRESOLVED | \
0047                       RTEMS_RTL_TRACE_ARCHIVES | \
0048                       RTEMS_RTL_TRACE_DEPENDENCY)
0049  #define DL_DEBUG_TRACE DEBUG_TRACE /* RTEMS_RTL_TRACE_ALL */
0050  #define DL_RTL_CMDS    1
0051 #else
0052  #define DL_DEBUG_TRACE 0
0053  #define DL_RTL_CMDS    0
0054 #endif
0055 
0056 static void dl_load_dump (void)
0057 {
0058 #if DL_RTL_CMDS
0059   char* list[] = { "rtl", "list", NULL };
0060   char* sym[] = { "rtl", "sym", NULL };
0061   printf ("RTL List:\n");
0062   rtems_rtl_shell_command (2, list);
0063   printf ("RTL Sym:\n");
0064   rtems_rtl_shell_command (2, sym);
0065 #endif
0066 }
0067 
0068 typedef int (*call_t)(int argc, const char* argv[]);
0069 
0070 static const char* call_1[] = { "Line 1", "Line 2" };
0071 static const char* call_2[] = { "Call 2, line 1",
0072                                 "Call 2, line 2",
0073                                 "Call 2, line 3" };
0074 
0075 int dl_load_test(void)
0076 {
0077   void*  handle;
0078   call_t call;
0079   int    call_ret;
0080   int    unresolved;
0081   char*  message = "loaded";
0082 
0083 #if DL_DEBUG_TRACE
0084   rtems_rtl_trace_set_mask (DL_DEBUG_TRACE);
0085 #endif
0086 
0087   printf("load: /dl01-o1.o\n");
0088 
0089   handle = dlopen ("/dl01-o1.o", RTLD_NOW | RTLD_GLOBAL);
0090   if (!handle)
0091   {
0092     printf("dlopen failed: %s\n", dlerror());
0093     return 1;
0094   }
0095 
0096   if (dlinfo (handle, RTLD_DI_UNRESOLVED, &unresolved) < 0)
0097     message = "dlinfo error checking unresolved status";
0098   else if (unresolved)
0099     message = "has unresolved externals";
0100 
0101   printf ("handle: %p %s\n", handle, message);
0102 
0103   dl_load_dump ();
0104 
0105   call = dlsym (handle, "rtems_main");
0106   if (call == NULL)
0107   {
0108     printf("dlsym failed: symbol not found\n");
0109     return 1;
0110   }
0111 
0112   call_ret = call (2, call_1);
0113   if (call_ret != 2)
0114   {
0115     printf("dlsym call failed: ret value bad\n");
0116     return 1;
0117   }
0118 
0119   call_ret = call (3, call_2);
0120   if (call_ret != 3)
0121   {
0122     printf("dlsym call failed: ret value bad\n");
0123     return 1;
0124   }
0125 
0126   if (dlclose (handle) < 0)
0127   {
0128     printf("dlclose failed: %s\n", dlerror());
0129     return 1;
0130   }
0131 
0132   printf ("handle: %p closed\n", handle);
0133 
0134   return 0;
0135 }