source: rtems/cpukit/libdl/rtl.h @ ae5fe7e6

4.115
Last change on this file since ae5fe7e6 was ae5fe7e6, checked in by Chris Johns <chrisj@…>, on 10/27/14 at 01:09:41

cpukit: Add libdl with the Runtime Loader (RTL) code.

This is a merge of the RTL project.

  • Property mode set to 100644
File size: 10.4 KB
Line 
1/*
2 *  COPYRIGHT (c) 2012 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.com/license/LICENSE.
7 */
8/**
9 * @file
10 *
11 * @ingroup rtems_rtl
12 *
13 * @brief RTEMS Run-Time Linker
14 *
15 * This is the POSIX interface to run-time loading of code into RTEMS.
16 */
17
18#if !defined (_RTEMS_RTL_H_)
19#define _RTEMS_RTL_H_
20
21#include <link.h>
22#include <rtems.h>
23#include <rtems/chain.h>
24
25#include <rtems/rtl/rtl-allocator.h>
26#include <rtems/rtl/rtl-fwd.h>
27#include <rtems/rtl/rtl-obj.h>
28#include <rtems/rtl/rtl-obj-cache.h>
29#include <rtems/rtl/rtl-obj-comp.h>
30#include <rtems/rtl/rtl-unresolved.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif /* __cplusplus */
35
36/**
37 * @defgroup rtems_rtl RTEMS Runtime Link Editor
38 *
39 * The module implements a runtime link editor with the standard dlopen, and
40 * dlclose family of functions.
41 *
42 * The runtime link editor is different to that found on Unix type systems. The
43 * object modules are compiled for PIC or position indepentent code and
44 * therefore require relocating when loaded.
45 *
46 * The object file format is currently ELF and object files can be separate
47 * files or in an archive. Object files in an archive are referenced by
48 * specifing 'archive:object' format. For example 'libfoo.a:bar.o'.
49 */
50
51/**
52 * Macros to glue two tokens.
53 */
54#ifdef __STDC__
55#define RTL_XGLUE(a,b) a##b
56#else
57#define RTL_XGLUE(a,b) a/**/b
58#endif
59
60#define RTL_GLUE(a,b) RTL_XGLUE(a,b)
61
62/**
63 * The number of buckets in the global symbol table.
64 */
65#define RTEMS_RTL_SYMS_GLOBAL_BUCKETS (32)
66
67/**
68 * The number of relocation record per block in the unresolved table.
69 */
70#define RTEMS_RTL_UNRESOLVED_BLOCK_SIZE (64)
71
72/**
73 * The global debugger interface variable.
74 */
75extern struct r_debug _rtld_debug;
76
77/**
78 * Debugger break function. Call when debugging to have it read the _rtld_debug
79 * variable.
80 */
81extern void _rtld_debug_state (void);
82
83/**
84 * The type of constructor/destructor function.
85 */
86typedef void (*rtems_rtl_cdtor_t)(void);
87
88/**
89 * The global RTL data. This structure is allocated on the heap when the first
90 * call to the RTL is made and never released.
91 *
92 * The global symbol table is a hash table held in this structure and the
93 * actual symbols are part of the object's structure. If this is a problem we
94 * could look at a hash table per object file.
95 */
96struct rtems_rtl_data_s
97{
98  rtems_id               lock;           /**< The RTL lock id */
99  rtems_rtl_alloc_data_t allocator;      /**< The allocator data. */
100  rtems_chain_control    objects;        /**< List if loaded object files. */
101  const char*            paths;          /**< Search paths for archives. */
102  rtems_rtl_symbols_t    globals;        /**< Global symbol table. */
103  rtems_rtl_unresolved_t unresolved;     /**< Unresolved symbols. */
104  rtems_rtl_obj_t*       base;           /**< Base object file. */
105  rtems_rtl_obj_cache_t  symbols;        /**< Symbols object file cache. */
106  rtems_rtl_obj_cache_t  strings;        /**< Strings object file cache. */
107  rtems_rtl_obj_cache_t  relocs;         /**< Relocations object file cache. */
108  rtems_rtl_obj_comp_t   decomp;         /**< The decompression compressor. */
109  int                    last_errno;     /**< Last error number. */
110  char                   last_error[64]; /**< Last error string. */
111};
112
113/**
114 * Get the RTL data with out locking. This call assmes the RTL is locked.
115 *
116 * @return rtems_rtl_data_t* The RTL data after being locked.
117 * @retval NULL The RTL data is not initialised.
118 */
119rtems_rtl_data_t* rtems_rtl_data (void);
120
121/**
122 * Get the RTL global symbol table with out locking. This call assmes the RTL
123 * is locked.
124 *
125 * @return rtems_rtl_symbols_t* The RTL global symbols after being locked.
126 * @retval NULL The RTL data is not initialised.
127 */
128rtems_rtl_symbols_t* rtems_rtl_global_symbols (void);
129
130/**
131 * Get the RTL resolved table with out locking. This call assmes the RTL
132 * is locked.
133 *
134 * @return rtems_rtl_unresolv_t* The RTL unresolved symbols and reloc records.
135 * @retval NULL The RTL data is not initialised.
136 */
137rtems_rtl_unresolved_t* rtems_rtl_unresolved (void);
138
139/**
140 * Get the RTL symbols, strings, or relocations object file caches. This call
141 * assmes the RTL is locked.
142 *
143 * @param symbols Pointer to the location to set the cache into. Returns NULL
144 *                is rtl is not initialised. If NULL is passed in no value set.
145 * @param strings Pointer to the location to set the cache into. Returns NULL
146 *                is rtl is not initialised. If NULL is passed in no value set.
147 * @param relocs Pointer to the location to set the cache into. Returns NULL
148 *               is rtl is not initialised. If NULL is passed in no value set.
149 */
150void rtems_rtl_obj_caches (rtems_rtl_obj_cache_t** symbols,
151                           rtems_rtl_obj_cache_t** strings,
152                           rtems_rtl_obj_cache_t** relocs);
153
154/**
155 * Flush all the object file caches.
156 */
157void rtems_rtl_obj_caches_flush (void);
158
159/**
160 * Get the RTL decompressor setting the cache and the offset in the file the
161 * compressed stream starts. This call assmes the RTL is locked.
162 *
163 * @param decomp Pointer to the location to set the compressor into. Returns
164 *               NULL is rtl is not initialised.
165 * @param cache The cache to read the file with. Saves needing an extrs buffer.
166 * @param offset The offset in the file the compressed stream starts.
167 */
168void rtems_rtl_obj_comp (rtems_rtl_obj_comp_t** decomp,
169                         rtems_rtl_obj_cache_t* cache,
170                         int                    fd,
171                         int                    compression,
172                         off_t                  offset);
173
174/**
175 * Lock the Run-time Linker.
176 *
177 * @return rtems_rtl_data_t* The RTL data after being locked.
178 * @retval NULL The RTL data could not be initialised or locked. Typically this
179 *              means the lock could not be created.
180 */
181rtems_rtl_data_t* rtems_rtl_lock (void);
182
183/**
184 * Unlock the Run-time Linker.
185 *
186 * @return True The RTL is unlocked.
187 * @return False The RTL could not be unlocked. Not much you can do.
188 */
189bool rtems_rtl_unlock (void);
190
191/**
192 * Check a pointer is a valid object file descriptor returning the pointer as
193 * that type.
194 *
195 * Assumes the RTL has been locked.
196 *
197 * @param handle Pointer to the object file to be validated.
198 * @return rtl_obj* The object file descriptor. NULL is returned if invalid.
199 */
200rtems_rtl_obj_t* rtems_rtl_check_handle (void* handle);
201
202/**
203 * Find the object given a file name.
204 *
205 * @param name The name of the object file.
206 * @retval NULL No object file with that name found.
207 * @return rtems_rtl_obj_t* The object file descriptor.
208 */
209rtems_rtl_obj_t* rtems_rtl_find_obj (const char* name);
210
211/**
212 * Load an object file into memory relocating it. It will not be resolved
213 * against other symbols in other object files or the base image.
214 *
215 * The name can be a file name for an object file or it can be encoded to
216 * reference an archive of object modules (static library). This encoding is
217 * specific to RTEMS and allows dependences to specify an archive without the
218 * searching overhead normally incurred by linkers locating object files in an
219 * archive. The file name format rules are:
220 *
221 *  1. Absolute file references a specific object file in the architecture
222 *     specific location on the file system.
223 *
224 *  2. Relative file references an object format file in the search path.
225 *
226 *  3. Absolute archive and file reference to a specific location in the file
227 *     system. The archive and file are encoded as 'archive:file [@offset]'
228 *     where 'archive' is a valid file at the absolute path in the file system,
229 *     and 'file' is a contained in the archive, and optionally an offset to
230 *     the 'file' in the 'archive'. If no offset is provided the archive is
231 *     searched.
232 *
233 *  4. Relative archive and file in the search path. The encoding is the same
234 *     as described in item 3 of this list.
235 *
236 * Assumes the RTL has been locked.
237 *
238 * @param name The name of the object file.
239 * @param mode The mode of the load as defined by the dlopen call.
240 * @return rtl_obj* The object file descriptor. NULL is returned if the load fails.
241 */
242rtems_rtl_obj_t* rtems_rtl_load_object (const char* name, int mode);
243
244/**
245 * Unload an object file. This only happens when the user count is 0.
246 *
247 * Assumes the RTL has been locked.
248 *
249 * @param obj The object file descriptor.
250 * @retval true The object file has been unloaded.
251 * @retval false The object file could not be unloaded.
252 */
253bool rtems_rtl_unload_object (rtems_rtl_obj_t* obj);
254
255/**
256 * Run any constructor functions the object file may contain. This call
257 * assumes the linker is unlocked.
258 *
259 * @param obj The object file.
260 */
261void rtems_rtl_run_ctors (rtems_rtl_obj_t* obj);
262
263/**
264 * Get the last error message clearing it. This operation locks the run time
265 * linker and therefore keeps the RTL thread safe but this call is not thread
266 * safe is multiple threads are loading object files at the same time. This
267 * call is provided to map to the dlopen family of calls.
268 *
269 * @param message Pointer to a buffer to copy the message into.
270 * @param max_message The maximum message that can be copied.
271 * @return int The last error number.
272 */
273int rtems_rtl_get_error (char* message, size_t max_message);
274
275/**
276 * Append the path to the search path.
277 *
278 * @path The path to append.
279 * @retval false The path could not be appended.
280 * @retval true The path was appended.
281 */
282bool rtems_rtl_path_append (const char* path);
283
284/**
285 * Prepend the path to the search path.
286 *
287 * @path The path to prepend.
288 * @retval false The path could not be prepended.
289 * @retval true The path was prepended.
290 */
291
292bool rtems_rtl_path_prepend (const char* path);
293
294/**
295 * Add an exported symbol table to the global symbol table. This call is
296 * normally used by an object file when loaded that contains a global symbol
297 * table.
298 *
299 * @param esyms The exported symbol table.
300 * @param count The size of the exported symbol table.
301 */
302void rtems_rtl_base_sym_global_add (const unsigned char* esyms,
303                                    unsigned int         count);
304
305/**
306 * Return the object file descriptor for the base image. The object file
307 * descriptor returned is created when the run time linker is initialised.
308 *
309 * Assumes the RTL has been locked.
310 *
311 * @return rtl_obj* The object file descriptor for the base image. NULL is
312 *                  returned if the load fails.
313 */
314rtems_rtl_obj_t* rtems_rtl_baseimage (void);
315
316#ifdef __cplusplus
317}
318#endif /* __cplusplus */
319
320#endif
Note: See TracBrowser for help on using the repository browser.