summaryrefslogtreecommitdiffstats
path: root/cpukit/libdl/dlfcn.c
blob: 3b31bb2e8e339dfffcfcc2cf8d431de1ad97c42b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 *  COPYRIGHT (c) 2012 Chris Johns <chrisj@rtems.org>
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */
/**
 * @file
 *
 * @ingroup rtl
 *
 * @brief RTEMS POSIX Dynamic Module Loading Interface.
 *
 * This is the POSIX interface to run-time loading of code into RTEMS.
 */

#include <stdint.h>
#include <dlfcn.h>
#include <rtems/rtl/rtl.h>

static rtems_rtl_obj_t*
dl_get_obj_from_handle (void* handle)
{
  rtems_rtl_obj_t* obj;

  /*
   * Handle the special cases provided in NetBSD and Sun documentation.
   *   http://download.oracle.com/docs/cd/E19253-01/816-5168/dlsym-3c/index.html
   * We currently do not manage the loading dependences in the module mappings
   * so we cannot handle the searching based on loading order where overriding
   * can occur.
   */

  if ((handle == RTLD_DEFAULT) || (handle == RTLD_SELF))
    obj = rtems_rtl_baseimage ();
  else
    obj = rtems_rtl_check_handle (handle);

  return obj;
}

void*
dlopen (const char* name, int mode)
{
  rtems_rtl_obj_t* obj = NULL;

  if (!rtems_rtl_lock ())
    return NULL;

  _rtld_debug.r_state = RT_ADD;
  _rtld_debug_state ();

  if (name)
    obj = rtems_rtl_load_object (name, mode);
  else
    obj = rtems_rtl_baseimage ();

  _rtld_debug.r_state = RT_CONSISTENT;
  _rtld_debug_state();

  rtems_rtl_unlock ();

  return obj;
}

int
dlclose (void* handle)
{
  rtems_rtl_obj_t* obj;
  int              r;

  if (!rtems_rtl_lock ())
    return -1;

  obj = rtems_rtl_check_handle (handle);
  if (!obj)
  {
    rtems_rtl_unlock ();
    return -1;
  }

  _rtld_debug.r_state = RT_DELETE;
  _rtld_debug_state ();

  r = rtems_rtl_unload_object (obj) ? 0 : -1;

  _rtld_debug.r_state = RT_CONSISTENT;
  _rtld_debug_state ();

  rtems_rtl_unlock ();

  return r;
}

void*
dlsym (void* handle, const char *symbol)
{
  rtems_rtl_obj_t*     obj;
  rtems_rtl_obj_sym_t* sym = NULL;
  void*                symval = NULL;

  if (!rtems_rtl_lock ())
    return NULL;

  /*
   * If the handle is "default" search the global symbol table.
   */
  if (handle == RTLD_DEFAULT)
  {
    sym = rtems_rtl_symbol_global_find (symbol);
  }
  else
  {
    obj = dl_get_obj_from_handle (handle);
    if (obj)
      sym = rtems_rtl_symbol_obj_find (obj, symbol);
  }

  if (sym)
    symval = sym->value;

  rtems_rtl_unlock ();

  return symval;
}

const char*
dlerror (void)
{
  static char msg[64];
  rtems_rtl_get_error (msg, sizeof (msg));
  return msg;
}

int
dlinfo (void* handle, int request, void* p)
{
  rtems_rtl_obj_t* obj;
  int              rc = -1;

  if (!rtems_rtl_lock () || !p)
    return -1;

  obj = dl_get_obj_from_handle (handle);
  if (obj)
  {
    switch (request)
    {
      case RTLD_DI_UNRESOLVED:
        *((int*) p) = rtems_rtl_obj_unresolved (obj) ? 1 : 0;
        rc = 0;
        break;
      default:
        break;
    }
  }

  rtems_rtl_unlock ();

  return rc;
}