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 rtems_rtld
0007  *
0008  * @brief RTEMS Run-Time Link Editor Dynamic Loading API Shell Support.
0009  *
0010  * Shell command wrappers for the Dynamic Loading API.
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 #ifdef HAVE_CONFIG_H
0039 #include "config.h"
0040 #endif
0041 
0042 #include <stdbool.h>
0043 #include <stdio.h>
0044 #include <stdlib.h>
0045 #include <string.h>
0046 
0047 #include <dlfcn.h>
0048 #include <rtems/rtl/dlfcn-shell.h>
0049 
0050 static void*
0051 convert_ascii_to_voidp (const char* arg)
0052 {
0053   if (strcmp (arg, "base") == 0)
0054     return RTLD_DEFAULT;
0055   return (void*) strtoul (arg, NULL, 16);
0056 }
0057 
0058 int
0059 shell_dlopen (int argc, char* argv[])
0060 {
0061   int arg;
0062   for (arg = 1; arg < argc; arg++)
0063   {
0064     void* handle = dlopen (argv[arg], RTLD_NOW | RTLD_GLOBAL);
0065     if (handle)
0066     {
0067       int   unresolved;
0068       char* message = "loaded";
0069       if (dlinfo (handle, RTLD_DI_UNRESOLVED, &unresolved) < 0)
0070         message = "dlinfo error checking unresolved status";
0071       else if (unresolved)
0072         message = "has unresolved externals";
0073       printf ("handle: %p %s\n", handle, message);
0074     }
0075     else
0076       printf ("error: %s\n", dlerror ());
0077   }
0078   return 0;
0079 }
0080 
0081 int
0082 shell_dlclose (int argc, char* argv[])
0083 {
0084   return 0;
0085 }
0086 
0087 static bool
0088 lookup_dlsym (void** value, int argc, char* argv[])
0089 {
0090   if (argc >= 3)
0091   {
0092     void* handle = convert_ascii_to_voidp (argv[1]);
0093     if (handle)
0094     {
0095       *value = dlsym (handle, argv[2]);
0096       if (*value)
0097         return true;
0098       else
0099         printf ("error: invalid handle or symbol\n");
0100     }
0101     else
0102       printf ("error: invalid handle");
0103   }
0104   else
0105     printf ("error: requires handle and symbol name\n");
0106   return false;
0107 }
0108 
0109 int
0110 shell_dlsym (int argc, char* argv[])
0111 {
0112   void* value;
0113   if (lookup_dlsym (&value, argc, argv))
0114   {
0115     printf ("%s = %p\n", argv[2], value);
0116     return 0;
0117   }
0118   return -1;
0119 }
0120 
0121 typedef int (*call_p)(int argc, char* argv[]);
0122 
0123 int
0124 shell_dlcall (int argc, char* argv[])
0125 {
0126   void* value;
0127   if (lookup_dlsym (&value, argc, argv))
0128   {
0129     call_p call = value;
0130     int    r;
0131     printf ("(*%p)(%d, ....)\n", value, argc - 3);
0132     r = call (argc - 3, argv + 3);
0133     printf ("return %d\n", r);
0134     return 0;
0135   }
0136   return -1;
0137 }