source: rtems/cpukit/libdl/rtl-obj.h @ c6eead1

5
Last change on this file since c6eead1 was c6eead1, checked in by Chris Johns <chrisj@…>, on 12/07/16 at 06:20:38

libdl: Add C++ exception support to loaded modules.

This has been tested on SPARC, i386, PowerPC and ARM.

Closes #2767.

  • Property mode set to 100644
File size: 22.8 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.org/license/LICENSE.
7 */
8/**
9 * @file
10 *
11 * @ingroup rtems_rtl
12 *
13 * @brief RTEMS Run-Time Linker Object Support.
14 */
15
16#if !defined (_RTEMS_RTL_OBJ_H_)
17#define _RTEMS_RTL_OBJ_H_
18
19#include <rtems.h>
20#include <rtems/chain.h>
21#include <rtems/rtl/rtl-sym.h>
22#include <rtems/rtl/rtl-unresolved.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif /* __cplusplus */
27
28/**
29 * Loader format flags.
30 */
31#define RTEMS_RTL_FMT_ELF     (1 << 0)
32#define RTEMS_RTL_FMT_COMP    (1 << 1)
33#define RTEMS_RTL_FMT_PRIVATE (1 << 16)
34
35/**
36 * Loader format definition.
37 */
38typedef struct rtems_rtl_loader_format_s
39{
40  /**
41   * The format label. This can be used to determine and manage
42   * specific formats.
43   */
44  const char* label;
45
46  /**
47   * The format flags.
48   */
49  uint32_t flags;
50} rtems_rtl_loader_format_t;
51
52/**
53 * The type of the format loader check handler. This handler checks the format
54 * and if it is detected as suitable it returns true.
55 */
56typedef bool (*rtems_rtl_loader_check) (rtems_rtl_obj_t* obj, int fd);
57
58/**
59 * The type of the format loader load handler. This handler loads the specific
60 * format.
61 */
62typedef bool (*rtems_rtl_loader_load) (rtems_rtl_obj_t* obj, int fd);
63
64/**
65 * The type of the format loader unload handler. This handler unloads the
66 * specific format.
67 */
68typedef bool (*rtems_rtl_loader_unload) (rtems_rtl_obj_t* obj);
69
70/**
71 * The type of the format loader signature handler. This handler checks the
72 * format signature.
73 */
74typedef rtems_rtl_loader_format_t* (*rtems_rtl_loader_sig) (void);
75
76/**
77 * Table for supported loadable formats.
78 */
79typedef struct rtems_rtl_loader_table_s
80{
81  rtems_rtl_loader_check  check;     /**< The check handler. */
82  rtems_rtl_loader_load   load;      /**< The loader. */
83  rtems_rtl_loader_unload unload;    /**< The unloader. */
84  rtems_rtl_loader_sig    signature; /**< The loader's signature. */
85} rtems_rtl_loader_table_t;
86
87/**
88 * Flags for the various section types.
89 */
90#define RTEMS_RTL_OBJ_SECT_TEXT  (1 << 0)  /**< Section holds program text. */
91#define RTEMS_RTL_OBJ_SECT_CONST (1 << 1)  /**< Section holds program text. */
92#define RTEMS_RTL_OBJ_SECT_DATA  (1 << 2)  /**< Section holds program data. */
93#define RTEMS_RTL_OBJ_SECT_BSS   (1 << 3)  /**< Section holds program bss. */
94#define RTEMS_RTL_OBJ_SECT_EH    (1 << 4)  /**< Section holds exception data. */
95#define RTEMS_RTL_OBJ_SECT_REL   (1 << 5)  /**< Section holds relocation records. */
96#define RTEMS_RTL_OBJ_SECT_RELA  (1 << 6)  /**< Section holds relocation addend
97                                            *   records. */
98#define RTEMS_RTL_OBJ_SECT_SYM   (1 << 7)  /**< Section holds symbols. */
99#define RTEMS_RTL_OBJ_SECT_STR   (1 << 8)  /**< Section holds strings. */
100#define RTEMS_RTL_OBJ_SECT_ALLOC (1 << 9)  /**< Section allocates runtime memory. */
101#define RTEMS_RTL_OBJ_SECT_LOAD  (1 << 10) /**< Section is loaded from object file. */
102#define RTEMS_RTL_OBJ_SECT_WRITE (1 << 11) /**< Section is writable, ie data. */
103#define RTEMS_RTL_OBJ_SECT_EXEC  (1 << 12) /**< Section is executable. */
104#define RTEMS_RTL_OBJ_SECT_ZERO  (1 << 13) /**< Section is preset to zero. */
105#define RTEMS_RTL_OBJ_SECT_CTOR  (1 << 14) /**< Section contains constructors. */
106#define RTEMS_RTL_OBJ_SECT_DTOR  (1 << 15) /**< Section contains destructors. */
107
108/**
109 * An object file is made up of sections and the can be more than
110 * one of a specific type of sections. All sections and grouped
111 * together in memory.
112 */
113struct rtems_rtl_obj_sect_s
114{
115  rtems_chain_node node;        /**< The node's link in the chain. */
116  int              section;     /**< The section number. */
117  const char*      name;        /**< The section's name. */
118  size_t           size;        /**< The size of the section in memory. */
119  off_t            offset;      /**< Offset into the object file. Relative to
120                                 *   the start of the object file. */
121  uint32_t         alignment;   /**< Alignment of this section. */
122  int              link;        /**< Section link field. */
123  int              info;        /**< Secfion info field. */
124  uint32_t         flags;       /**< The section's flags. */
125  void*            base;        /**< The base address of the section in
126                                 *   memory. */
127};
128
129/**
130 * Object file descriptor flags.
131 */
132#define RTEMS_RTL_OBJ_LOCKED     (1 << 0) /**< Lock the object file so it cannot
133                                           *   be unloaded. */
134#define RTEMS_RTL_OBJ_UNRESOLVED (1 << 1) /**< The object file has unresolved
135                                           *   external symbols. */
136
137/**
138 * RTL Object. There is one for each object module loaded plus one for the base
139 * kernel image.
140 */
141struct rtems_rtl_obj_s
142{
143  rtems_chain_node     link;         /**< The node's link in the chain. */
144  uint32_t             flags;        /**< The status of the object file. */
145  uint32_t             users;        /**< References to the object file. */
146  int                  format;       /**< The format of the object file. */
147  const char*          fname;        /**< The file name for the object. */
148  const char*          oname;        /**< The object file name. Can be
149                                      *   relative. */
150  const char*          aname;        /**< The archive name containing the
151                                      *   object. NULL means the object is not
152                                      *   in a lib */
153  off_t                ooffset;      /**< The object offset in the archive. */
154  size_t               fsize;        /**< Size of the object file. */
155  rtems_chain_control  sections;     /**< The sections of interest in the
156                                      *   object file. */
157  rtems_rtl_obj_sym_t* local_table;  /**< Local symbol table. */
158  size_t               local_syms;   /**< Local symbol count. */
159  size_t               local_size;   /**< Local symbol memory usage. */
160  rtems_rtl_obj_sym_t* global_table; /**< Global symbol table. */
161  size_t               global_syms;  /**< Global symbol count. */
162  size_t               global_size;  /**< Global symbol memory usage. */
163  uint32_t             unresolved;   /**< The number of unresolved relocations. */
164  void*                text_base;    /**< The base address of the text section
165                                      *   in memory. */
166  size_t               text_size;     /**< The size of the text section. */
167  void*                const_base;   /**< The base address of the const section
168                                      *   in memory. */
169  void*                eh_base;      /**< The base address of the eh section
170                                      *   in memory. */
171  size_t               eh_size;      /**< The size of the eh section. */
172  void*                data_base;    /**< The base address of the data section
173                                      *   in memory. */
174  void*                bss_base;     /**< The base address of the bss section
175                                      *   in memory. */
176  size_t               bss_size;     /**< The size of the bss section. */
177  size_t               exec_size;    /**< The amount of executable memory
178                                      *   allocated */
179  void*                entry;        /**< The entry point of the module. */
180  uint32_t             checksum;     /**< The checksum of the text sections. A
181                                      *   zero means do not checksum. */
182  uint32_t*            sec_num;      /**< The sec nums of each obj. */
183  uint32_t             obj_num;      /**< The count of elf files in an rtl obj. */
184  struct link_map*     linkmap;      /**< For GDB. */
185  void*                loader;       /**< The file details specific to a loader. */
186};
187
188/**
189 * A section handler is called once for each section that needs to be
190 * processed by this handler.
191 *
192 * @param obj The object file's descriptor the section belongs too.
193 * @param fd The file descriptor of the object file beling loaded.
194 * @param sect The section the handler is being invoked to handle.
195 * @param data A user supplied data variable.
196 * @retval true The operation was successful.
197 * @retval false The operation failed and the RTL has been set.
198 */
199typedef bool (*rtems_rtl_obj_sect_handler_t)(rtems_rtl_obj_t*      obj,
200                                             int                   fd,
201                                             rtems_rtl_obj_sect_t* sect,
202                                             void*                 data);
203
204/**
205 * Get the file name.
206 *
207 * @param obj The object file.
208 * @return const char* The string.
209 */
210static inline const char* rtems_rtl_obj_fname (const rtems_rtl_obj_t* obj)
211{
212  return obj->fname;
213}
214
215/**
216 * Is the file name valid ?
217 *
218 * @param obj The object file.
219 * @return bool There is a file name
220 */
221static inline bool rtems_rtl_obj_fname_valid (const rtems_rtl_obj_t* obj)
222{
223  return obj->fname;
224}
225
226/**
227 * Get the object name.
228 *
229 * @param obj The object file.
230 * @return const char* The string.
231 */
232static inline const char* rtems_rtl_obj_oname (const rtems_rtl_obj_t* obj)
233{
234  return obj->oname;
235}
236
237/**
238 * Is the object name valid ?
239 *
240 * @param obj The object file.
241 * @return bool There is an object name
242 */
243static inline bool rtems_rtl_obj_oname_valid (const rtems_rtl_obj_t* obj)
244{
245  return obj->oname;
246}
247
248/**
249 * Get the archive name.
250 *
251 * @param obj The object file.
252 * @return const char* The string.
253 */
254static inline const char* rtems_rtl_obj_aname (const rtems_rtl_obj_t* obj)
255{
256  return obj->aname;
257}
258
259/**
260 * Is the archive name valid ?
261 *
262 * @param obj The object file.
263 * @return bool There is an archive name
264 */
265static inline bool rtems_rtl_obj_aname_valid (const rtems_rtl_obj_t* obj)
266{
267  return obj->aname;
268}
269
270/**
271 * Is the address inside the text section?
272 *
273 * @param obj The object file.
274 * @return bool There is an archive name
275 */
276static inline bool rtems_rtl_obj_text_inside (const rtems_rtl_obj_t* obj,
277                                              const void*            address)
278{
279  return
280    (address >= obj->text_base) &&
281    (address < (obj->text_base + obj->text_size));
282}
283
284/**
285 * Allocate an object structure on the heap.
286 *
287 * @retval NULL No memory for the object.
288 */
289rtems_rtl_obj_t* rtems_rtl_obj_alloc (void);
290
291/**
292 * Free the object structure and related resources.
293 *
294 * @param obj The object file's descriptor to free.
295 * @retval false The object has dependences.
296 * @retval true The object has been freed.
297 */
298bool rtems_rtl_obj_free (rtems_rtl_obj_t* obj);
299
300/**
301 * Does the object file have unresolved external references ? If it does the
302 * results of executing code is unpredictable.
303 *
304 * @param obj The object file's descriptor.
305 * @retval true The object file has unresolved externals.
306 * @retval false The object file has all external references resolved.
307 */
308bool rtems_rtl_obj_unresolved (rtems_rtl_obj_t* obj);
309
310/**
311 * Parses a filename and returns newly allocated strings with the archive name,
312 * object name, and the object's offset
313 *
314 * @param name The filename of the object
315 * @param aname Address of a string pointer that holds the archive name
316 * @param oname Address of a string pointer that holds the object name
317 * @param ooffset Address of an int that holds the object offset
318 * @retval true The parsing was successful
319 * @retval false The parsing was unsuccessful
320 */
321bool rtems_rtl_parse_name (const char*  name,
322                           const char** aname,
323                           const char** oname,
324                           off_t*       ooffset);
325
326/**
327 * Check of the name matches the object file's object name.
328 *
329 * @param obj The object file's descriptor.
330 * @param name The name to match.
331 */
332bool rtems_rtl_match_name (rtems_rtl_obj_t* obj, const char* name);
333
334/**
335 * Find an object file on disk that matches the name. The object descriptor is
336 * fill in with the various parts of a name. A name can have archive, object
337 * file and offset components. The search path in the RTL is searched.
338 *
339 * @param obj The object file's descriptor.
340 * @param name The name to locate on disk.
341 * @retval true The file has been found.
342 * @retval false The file could not be located. The RTL error has been set.
343 */
344bool rtems_rtl_obj_find_file (rtems_rtl_obj_t* obj, const char* name);
345
346/**
347 * Add a section to the object descriptor.
348 *
349 * @param obj The object file's descriptor.
350 * @param section The section's index number.
351 * @param name The name of the section.
352 * @param size The size of the section in memory.
353 * @param offset The offset of the section in the object file.
354 * @param alignment The alignment of the section in memory.
355 * @param link The section's link field (from the ELF format).
356 * @param info The section's info field (from the ELF format).
357 * @param flags The section's flags.
358 * @retval true The section has been added.
359 * @retval false The section has not been added. See the RTL error.
360 */
361bool rtems_rtl_obj_add_section (rtems_rtl_obj_t* obj,
362                                int              section,
363                                const char*      name,
364                                size_t           size,
365                                off_t            offset,
366                                uint32_t         alignment,
367                                int              link,
368                                int              info,
369                                uint32_t         flags);
370
371/**
372 * Erase the object file descriptor's sections.
373 *
374 * @param obj The object file's descriptor.
375 */
376void rtems_rtl_obj_erase_sections (rtems_rtl_obj_t* obj);
377
378/**
379 * Find the section given a name.
380 *
381 * @param obj The object file's descriptor.
382 * @param name The name of the section to find.
383 * @retval NULL The section was not found.
384 * @return rtems_rtl_obj_sect_t* The named section.
385 */
386rtems_rtl_obj_sect_t* rtems_rtl_obj_find_section (const rtems_rtl_obj_t* obj,
387                                                  const char*            name);
388
389/**
390 * Find a section given a section's index number.
391 *
392 * @param obj The object file's descriptor.
393 * @param index The section's index to find.
394 * @retval NULL The section was not found.
395 * @return rtems_rtl_obj_sect_t* The found section.
396 */
397rtems_rtl_obj_sect_t* rtems_rtl_obj_find_section_by_index (const rtems_rtl_obj_t* obj,
398                                                           int                    index);
399
400/**
401 * The text section size. Only use once all the sections has been added. It
402 * includes alignments between sections that are part of the object's text
403 * area. The consts sections are included in this section.
404 *
405 * @param obj The object file's descriptor.
406 * @return size_t The size of the text area of the object file.
407 */
408size_t rtems_rtl_obj_text_size (const rtems_rtl_obj_t* obj);
409
410/**
411 * The text section alignment for the object file. Only use once all the
412 * sections has been added. The section alignment is the alignment of the first
413 * text type section loaded the text section.
414 *
415 * You can assume the alignment is a positive integral power of 2 if not 0 or
416 * 1. If 0 or 1 then there is no alignment.
417 *
418 * @param obj The object file's descriptor.
419 * @return uint32_t The alignment. Can be 0 or 1 for not aligned or the alignment.
420 */
421uint32_t rtems_rtl_obj_text_alignment (const rtems_rtl_obj_t* obj);
422
423/**
424 * The const section size. Only use once all the sections has been added. It
425 * includes alignments between sections that are part of the object's const
426 * area. The consts sections are included in this section.
427 *
428 * @param obj The object file's descriptor.
429 * @return size_t The size of the const area of the object file.
430 */
431size_t rtems_rtl_obj_const_size (const rtems_rtl_obj_t* obj);
432
433/**
434 * The const section alignment for the object file. Only use once all the
435 * sections has been added. The section alignment is the alignment of the first
436 * const type section loaded the const section.
437 *
438 * You can assume the alignment is a positive integral power of 2 if not 0 or
439 * 1. If 0 or 1 then there is no alignment.
440 *
441 * @param obj The object file's descriptor.
442 * @return uint32_t The alignment. Can be 0 or 1 for not aligned or the alignment.
443 */
444uint32_t rtems_rtl_obj_const_alignment (const rtems_rtl_obj_t* obj);
445
446/**
447 * The eh section size. Only use once all the sections has been added. It
448 * includes alignments between sections that are part of the object's bss area.
449 *
450 * @param obj The object file's descriptor.
451 * @return size_t The size of the bss area of the object file.
452 */
453size_t rtems_rtl_obj_eh_size (const rtems_rtl_obj_t* obj);
454
455/**
456 * The eh section alignment for the object file. Only use once all the sections
457 * has been added. The section alignment is the alignment of the first bss type
458 * section loaded the bss section.
459 *
460 * You can assume the alignment is a positive integral power of 2 if not 0 or
461 * 1. If 0 or 1 then there is no alignment.
462 *
463 * @param obj The object file's descriptor.
464 * @return uint32_t The alignment. Can be 0 or 1 for not aligned or the alignment.
465 */
466uint32_t rtems_rtl_obj_eh_alignment (const rtems_rtl_obj_t* obj);
467
468/**
469 * The data section size. Only use once all the sections has been added. It
470 * includes alignments between sections that are part of the object's data
471 * area.
472 *
473 * @param obj The object file's descriptor.
474 * @return size_t The size of the data area of the object file.
475 */
476size_t rtems_rtl_obj_data_size (const rtems_rtl_obj_t* obj);
477
478/**
479 * The data section alignment for the object file. Only use once all the
480 * sections has been added. The section alignment is the alignment of the first
481 * data type section loaded the data section.
482 *
483 * You can assume the alignment is a positive integral power of 2 if not 0 or
484 * 1. If 0 or 1 then there is no alignment.
485 *
486 * @param obj The object file's descriptor.
487 * @return uint32_t The alignment. Can be 0 or 1 for not aligned or the alignment.
488 */
489uint32_t rtems_rtl_obj_data_alignment (const rtems_rtl_obj_t* obj);
490
491/**
492 * The bss section size. Only use once all the sections has been added. It
493 * includes alignments between sections that are part of the object's bss area.
494 *
495 * @param obj The object file's descriptor.
496 * @return size_t The size of the bss area of the object file.
497 */
498size_t rtems_rtl_obj_bss_size (const rtems_rtl_obj_t* obj);
499
500/**
501 * The bss section alignment for the object file. Only use once all the
502 * sections has been added. The section alignment is the alignment of the first
503 * bss type section loaded the bss section.
504 *
505 * You can assume the alignment is a positive integral power of 2 if not 0 or
506 * 1. If 0 or 1 then there is no alignment.
507 *
508 * @param obj The object file's descriptor.
509 * @return uint32_t The alignment. Can be 0 or 1 for not aligned or the alignment.
510 */
511uint32_t rtems_rtl_obj_bss_alignment (const rtems_rtl_obj_t* obj);
512
513/**
514 * Relocate the object file. The object file's section are parsed for any
515 * relocation type sections.
516 *
517 * @param obj The object file's descriptor.
518 * @param fd The object file's file descriptor.
519 * @param handler The object file's format specific relocation handler.
520 * @param data User specific data handle.
521 * @retval true The object file was relocated.
522 * @retval false The relocation failed. The RTL error is set.
523 */
524bool rtems_rtl_obj_relocate (rtems_rtl_obj_t*             obj,
525                             int                          fd,
526                             rtems_rtl_obj_sect_handler_t handler,
527                             void*                        data);
528
529/**
530 * Synchronize caches to make code visible to CPU(s)
531 *
532 * @param obj The object file's descriptor.
533 */
534void rtems_rtl_obj_synchronize_cache (rtems_rtl_obj_t*    obj);
535
536/**
537 * Relocate an object file's unresolved reference.
538 *
539 * @param rec The unresolved relocation record.
540 * @param sym The unresolved relocation's referenced symbol.
541 * @retval true The object file record was relocated.
542 * @retval false The relocation failed. The RTL error is set.
543 */
544bool rtems_rtl_obj_relocate_unresolved (rtems_rtl_unresolv_reloc_t* reloc,
545                                        rtems_rtl_obj_sym_t*        sym);
546
547/**
548 * Load the symbols from the object file. Only the exported or public symbols
549 * are read into memory and held in the global symbol table.
550 *
551 * @param obj The object file's descriptor.
552 * @param fd The object file's file descriptor.
553 * @param handler The object file's format specific symbol handler.
554 * @param data User specific data handle.
555 * @retval true The object file's symbol where loaded.
556 * @retval false The symbol loading failed. The RTL error is set.
557 */
558bool rtems_rtl_obj_load_symbols (rtems_rtl_obj_t*             obj,
559                                 int                          fd,
560                                 rtems_rtl_obj_sect_handler_t handler,
561                                 void*                        data);
562
563/**
564 * Load the sections that have been allocated memory in the target. The bss
565 * type section does not load any data, it is set to 0. The text and data
566 * sections read the detault data from the object file into the target memory.
567 *
568 * @param obj The object file's descriptor.
569 * @param fd The object file's file descriptor.
570 * @param handler The object file's format specific load handler.
571 * @param data User specific data handle.
572 * @retval true The object has been sucessfully loaded.
573 * @retval false The load failed. The RTL error has been set.
574 */
575bool rtems_rtl_obj_load_sections (rtems_rtl_obj_t*             obj,
576                                  int                          fd,
577                                  rtems_rtl_obj_sect_handler_t handler,
578                                  void*                        data);
579
580/**
581 * Invoke the constructors the object has. Constructors are a table of pointers
582 * to "void (*)(void);" where NULL pointers are skipped. The table's size is
583 * taken from the section's size. The objet ELF specific code is responisble
584 * for flagging which sections contain constructors.
585 *
586 * @param obj The object file's descriptor.
587 */
588void rtems_rtl_obj_run_ctors (rtems_rtl_obj_t* obj);
589
590/**
591 * Invoke the destructors the object has. Destructors are a table of pointers
592 * to "void (*)(void);" where NULL pointers are skipped. The table's size is
593 * taken from the section's size. The objet ELF specific code is responisble
594 * for flagging which sections contain destructors.
595 *
596 * @param obj The object file's descriptor.
597 */
598void rtems_rtl_obj_run_dtors (rtems_rtl_obj_t* obj);
599
600/**
601 * Load the object file, reading all sections into memory, symbols and
602 * performing any relocation fixups.
603 *
604 * @param obj The object file's descriptor.
605 * @retval true The object file has been loaded.
606 * @retval false The load failed. The RTL error has been set.
607 */
608bool rtems_rtl_obj_load (rtems_rtl_obj_t* obj);
609
610/**
611 * Unload the object file, erasing all symbols and releasing all memory.
612 *
613 * @param obj The object file's descriptor.
614 * @retval true The object file has been unloaded.
615 * @retval false The unload failed. The RTL error has been set.
616 */
617bool rtems_rtl_obj_unload (rtems_rtl_obj_t* obj);
618
619#ifdef __cplusplus
620}
621#endif /* __cplusplus */
622
623#endif
Note: See TracBrowser for help on using the repository browser.