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) 2023 On-Line Applications Research Corporation (OAR).
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 <errno.h>
0029 #include <stdio.h>
0030 #include "tmacros.h"
0031 #include <pthread.h>
0032 
0033 #include <dlfcn.h>
0034 
0035 #include "dl-load.h"
0036 
0037 #include <rtems/rtl/rtl-shell.h>
0038 #include <rtems/rtl/rtl-trace.h>
0039 
0040 #define TEST_TRACE 0
0041 #if TEST_TRACE
0042  #define SHOW_GLOBAL_SYMS 1
0043  #if SHOW_GLOBAL_SYMS
0044   #define TRACE_GLOBAL_SYMBOL RTEMS_RTL_TRACE_GLOBAL_SYM
0045  #else
0046   #define TRACE_GLOBAL_SYMBOL 0
0047  #endif
0048  #define DEBUG_TRACE (RTEMS_RTL_TRACE_DETAIL | \
0049                       RTEMS_RTL_TRACE_WARNING | \
0050                       RTEMS_RTL_TRACE_LOAD | \
0051                       RTEMS_RTL_TRACE_UNLOAD | \
0052                       TRACE_GLOBAL_SYMBOL | \
0053                       RTEMS_RTL_TRACE_SYMBOL | \
0054                       RTEMS_RTL_TRACE_RELOC | \
0055                       RTEMS_RTL_TRACE_ALLOCATOR | \
0056                       RTEMS_RTL_TRACE_UNRESOLVED | \
0057                       RTEMS_RTL_TRACE_ARCHIVES | \
0058                       RTEMS_RTL_TRACE_DEPENDENCY)
0059  #define DL_DEBUG_TRACE DEBUG_TRACE /* RTEMS_RTL_TRACE_ALL */
0060  #define DL_RTL_CMDS    1
0061 #else
0062  #define DL_DEBUG_TRACE 0
0063  #define DL_RTL_CMDS    0
0064 #endif
0065 
0066 static void dl_load_dump (void)
0067 {
0068 #if DL_RTL_CMDS
0069   char* list[] = { "rtl", "list", NULL };
0070   char* sym[] = { "rtl", "sym", NULL };
0071   printf ("RTL List:\n");
0072   rtems_rtl_shell_command (2, list);
0073   printf ("RTL Sym:\n");
0074   rtems_rtl_shell_command (2, sym);
0075 #endif
0076 }
0077 
0078 typedef int (*int_call_t)(void);
0079 typedef int* (*ptr_call_t)(void);
0080 
0081 void* get_errno_ptr(void);
0082 int get_errno(void);
0083 
0084 int_call_t int_call;
0085 ptr_call_t ptr_call;
0086 static int perform_test(void)
0087 {
0088   int    int_call_ret;
0089   int*   ptr_call_ret;
0090   ptr_call_ret = ptr_call ();
0091   if (ptr_call_ret != get_errno_ptr())
0092   {
0093     printf("dlsym ptr_call failed: ret value bad\n");
0094     return 1;
0095   }
0096 
0097   errno = 12345;
0098   int_call_ret = int_call ();
0099   if (int_call_ret != get_errno())
0100   {
0101     printf("dlsym int_call failed: ret value bad\n");
0102     return 1;
0103   }
0104   errno = 0;
0105 
0106   return 0;
0107 }
0108 
0109 static void *secondary_thread(void *arg)
0110 {
0111   printf("Running test on secondary thread\n");
0112   if (perform_test()) {
0113     printf("Test failed on secondary task\n");
0114     return (void *) 1;
0115   }
0116 
0117   return NULL;
0118 }
0119 
0120 static void start_secondary(void)
0121 {
0122   /* Run the test on a secondary thread */
0123   pthread_t threadId;
0124   int status;
0125   void *ret;
0126   status = pthread_create( &threadId, NULL, secondary_thread, NULL );
0127   rtems_test_assert( !status );
0128 
0129   /* Wait on thread to exit */
0130   status = pthread_join(threadId, &ret);
0131   rtems_test_assert( !status );
0132   rtems_test_assert( ret == NULL );
0133 }
0134 
0135 int dl_load_test(void)
0136 {
0137   void*  handle;
0138   int    unresolved;
0139   char*  message = "loaded";
0140 
0141 #if DL_DEBUG_TRACE
0142   rtems_rtl_trace_set_mask (DL_DEBUG_TRACE);
0143 #endif
0144 
0145   printf("load: /dl11-o1.o\n");
0146 
0147   handle = dlopen ("/dl11-o1.o", RTLD_NOW | RTLD_GLOBAL);
0148   if (!handle)
0149   {
0150     printf("dlopen failed: %s\n", dlerror());
0151     return 1;
0152   }
0153 
0154   if (dlinfo (handle, RTLD_DI_UNRESOLVED, &unresolved) < 0)
0155     message = "dlinfo error checking unresolved status";
0156   else if (unresolved)
0157     message = "has unresolved externals";
0158 
0159   printf ("handle: %p %s\n", handle, message);
0160 
0161   dl_load_dump ();
0162 
0163   ptr_call = dlsym (handle, "get_errno_ptr");
0164   if (ptr_call == NULL)
0165   {
0166     printf("dlsym failed: symbol get_errno_ptr not found\n");
0167     return 1;
0168   }
0169 
0170   int_call = dlsym (handle, "get_errno_val");
0171   if (int_call == NULL)
0172   {
0173     printf("dlsym failed: symbol get_errno_val not found\n");
0174     return 1;
0175   }
0176 
0177   /* Run the test on the init thread */
0178   printf("Running test on init task\n");
0179   if (perform_test()) {
0180     return 1;
0181   }
0182 
0183   start_secondary();
0184 
0185   if (dlclose (handle) < 0)
0186   {
0187     printf("dlclose failed: %s\n", dlerror());
0188     return 1;
0189   }
0190 
0191   printf ("handle: %p closed\n", handle);
0192 
0193   return 0;
0194 }
0195 
0196 /*
0197  * Disasseble these to see how the platform accesses TLS
0198  */
0199 void* get_errno_ptr(void)
0200 {
0201   return &errno;
0202 }
0203 
0204 int get_errno(void)
0205 {
0206   return errno;
0207 }