source: rtems/cpukit/libdl/dlfcn-shell.c @ 696b9121

5
Last change on this file since 696b9121 was f59d435d, checked in by Chris Johns <chrisj@…>, on 04/12/18 at 07:46:49

libdl: Remove _t from all structures as this is reserved for the standards

  • Property mode set to 100644
File size: 2.3 KB
Line 
1/*
2 *  COPYRIGHT (c) 2012, 2018 Chris Johns <chrisj@rtems.org>
3 *
4 *  The license and distribution terms for this file may be
5 *  found in the file LICENSE in this distribution or at
6 *  http://www.rtems.org/license/LICENSE.
7 */
8/**
9 * @file
10 *
11 * @ingroup rtems_rtld
12 *
13 * @brief RTEMS Run-Time Link Editor Dynamic Loading API Shell Support.
14 *
15 * Shell command wrappers for the Dynamic Loading API.
16 */
17
18#if HAVE_CONFIG_H
19#include "config.h"
20#endif
21
22#include <stdbool.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include <dlfcn.h>
28#include <rtems/rtl/dlfcn-shell.h>
29
30static void*
31convert_ascii_to_voidp (const char* arg)
32{
33  if (strcmp (arg, "base") == 0)
34    return RTLD_DEFAULT;
35  return (void*) strtoul (arg, NULL, 16);
36}
37
38int
39shell_dlopen (int argc, char* argv[])
40{
41  int arg;
42  for (arg = 1; arg < argc; arg++)
43  {
44    void* handle = dlopen (argv[arg], RTLD_NOW | RTLD_GLOBAL);
45    if (handle)
46    {
47      int   unresolved;
48      char* message = "loaded";
49      if (dlinfo (handle, RTLD_DI_UNRESOLVED, &unresolved) < 0)
50        message = "dlinfo error checking unresolved status";
51      else if (unresolved)
52        message = "has unresolved externals";
53      printf ("handle: %p %s\n", handle, message);
54    }
55    else
56      printf ("error: %s\n", dlerror ());
57  }
58  return 0;
59}
60
61int
62shell_dlclose (int argc, char* argv[])
63{
64  return 0;
65}
66
67static bool
68lookup_dlsym (void** value, int argc, char* argv[])
69{
70  if (argc >= 3)
71  {
72    void* handle = convert_ascii_to_voidp (argv[1]);
73    if (handle)
74    {
75      *value = dlsym (handle, argv[2]);
76      if (*value)
77        return true;
78      else
79        printf ("error: invalid handle or symbol\n");
80    }
81    else
82      printf ("error: invalid handle");
83  }
84  else
85    printf ("error: requires handle and symbol name\n");
86  return false;
87}
88
89int
90shell_dlsym (int argc, char* argv[])
91{
92  void* value;
93  if (lookup_dlsym (&value, argc, argv))
94  {
95    printf ("%s = %p\n", argv[2], value);
96    return 0;
97  }
98  return -1;
99}
100
101typedef int (*call_p)(int argc, char* argv[]);
102
103int
104shell_dlcall (int argc, char* argv[])
105{
106  void* value;
107  if (lookup_dlsym (&value, argc, argv))
108  {
109    call_p call = value;
110    int    r;
111    printf ("(*%p)(%d, ....)\n", value, argc - 3);
112    r = call (argc - 3, argv + 3);
113    printf ("return %d\n", r);
114    return 0;
115  }
116  return -1;
117}
Note: See TracBrowser for help on using the repository browser.