Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup rtl
0007  *
0008  * @brief RTEMS POSIX Dynamic Module Loading Interface.
0009  *
0010  * This is the POSIX interface to run-time loading of code into RTEMS.
0011  */
0012 
0013 /*
0014  *  COPYRIGHT (c) 2012, 2018 Chris Johns <chrisj@rtems.org>
0015  *
0016  * Redistribution and use in source and binary forms, with or without
0017  * modification, are permitted provided that the following conditions
0018  * are met:
0019  * 1. Redistributions of source code must retain the above copyright
0020  *    notice, this list of conditions and the following disclaimer.
0021  * 2. Redistributions in binary form must reproduce the above copyright
0022  *    notice, this list of conditions and the following disclaimer in the
0023  *    documentation and/or other materials provided with the distribution.
0024  *
0025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0028  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0029  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0030  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0031  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0033  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0034  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0035  * POSSIBILITY OF SUCH DAMAGE.
0036  */
0037 
0038 #include <stdint.h>
0039 #include <dlfcn.h>
0040 #include <rtems/rtl/rtl.h>
0041 
0042 static rtems_rtl_obj*
0043 dl_get_obj_from_handle (void* handle)
0044 {
0045   rtems_rtl_obj* obj;
0046 
0047   /*
0048    * Handle the special cases provided in NetBSD and Sun documentation.
0049    *   http://download.oracle.com/docs/cd/E19253-01/816-5168/dlsym-3c/index.html
0050    * We currently do not manage the loading dependences in the module mappings
0051    * so we cannot handle the searching based on loading order where overriding
0052    * can occur.
0053    */
0054 
0055   if ((handle == RTLD_DEFAULT) || (handle == RTLD_SELF))
0056     obj = rtems_rtl_baseimage ();
0057   else
0058     obj = rtems_rtl_check_handle (handle);
0059 
0060   return obj;
0061 }
0062 
0063 void*
0064 dlopen (const char* name, int mode)
0065 {
0066   rtems_rtl_obj* obj = NULL;
0067 
0068   if (!rtems_rtl_lock ())
0069     return NULL;
0070 
0071   _rtld_debug.r_state = RT_ADD;
0072   _rtld_debug_state ();
0073 
0074   if (name)
0075     obj = rtems_rtl_load (name, mode);
0076   else
0077     obj = rtems_rtl_baseimage ();
0078 
0079   _rtld_debug.r_state = RT_CONSISTENT;
0080   _rtld_debug_state();
0081 
0082   rtems_rtl_unlock ();
0083 
0084   return obj;
0085 }
0086 
0087 int
0088 dlclose (void* handle)
0089 {
0090   rtems_rtl_obj* obj;
0091   int            r;
0092 
0093   if (!rtems_rtl_lock ())
0094     return -1;
0095 
0096   obj = rtems_rtl_check_handle (handle);
0097   if (!obj)
0098   {
0099     rtems_rtl_unlock ();
0100     return -1;
0101   }
0102 
0103   _rtld_debug.r_state = RT_DELETE;
0104   _rtld_debug_state ();
0105 
0106   r = rtems_rtl_unload (obj) ? 0 : -1;
0107 
0108   _rtld_debug.r_state = RT_CONSISTENT;
0109   _rtld_debug_state ();
0110 
0111   rtems_rtl_unlock ();
0112 
0113   return r;
0114 }
0115 
0116 void*
0117 dlsym (void* handle, const char *symbol)
0118 {
0119   rtems_rtl_obj*     obj;
0120   rtems_rtl_obj_sym* sym = NULL;
0121   void*              symval = NULL;
0122 
0123   if (!rtems_rtl_lock ())
0124     return NULL;
0125 
0126   /*
0127    * If the handle is "default" search the global symbol table.
0128    */
0129   if (handle == RTLD_DEFAULT)
0130   {
0131     sym = rtems_rtl_symbol_global_find (symbol);
0132   }
0133   else
0134   {
0135     obj = dl_get_obj_from_handle (handle);
0136     if (obj)
0137       sym = rtems_rtl_symbol_obj_find (obj, symbol);
0138   }
0139 
0140   if (sym)
0141     symval = sym->value;
0142 
0143   rtems_rtl_unlock ();
0144 
0145   return symval;
0146 }
0147 
0148 const char*
0149 dlerror (void)
0150 {
0151   static char msg[64];
0152   int         eno;
0153   eno = rtems_rtl_get_error (msg, sizeof (msg));
0154   if (eno == 0)
0155     return NULL;
0156   return msg;
0157 }
0158 
0159 int
0160 dlinfo (void* handle, int request, void* p)
0161 {
0162   rtems_rtl_obj* obj;
0163   int            rc = -1;
0164 
0165   if (!rtems_rtl_lock () || !p)
0166     return -1;
0167 
0168   obj = dl_get_obj_from_handle (handle);
0169   if (obj)
0170   {
0171     switch (request)
0172     {
0173       case RTLD_DI_UNRESOLVED:
0174         *((int*) p) = rtems_rtl_obj_unresolved (obj) ? 1 : 0;
0175         rc = 0;
0176         break;
0177       default:
0178         break;
0179     }
0180   }
0181 
0182   rtems_rtl_unlock ();
0183 
0184   return rc;
0185 }