source: rtems/cpukit/libdl/rtl-obj.c @ 80cf60e

5
Last change on this file since 80cf60e was 80cf60e, checked in by Sebastian Huber <sebastian.huber@…>, on 04/15/20 at 07:48:32

Canonicalize config.h include

Use the following variant which was already used by most source files:

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

  • Property mode set to 100644
File size: 41.0 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 rtl
12 *
13 * @brief RTEMS Run-Time Linker Error
14 */
15
16#ifdef HAVE_CONFIG_H
17#include "config.h"
18#endif
19
20#include <errno.h>
21#include <inttypes.h>
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25
26#include <rtems/libio_.h>
27
28#include <rtems/rtl/rtl.h>
29#include "rtl-chain-iterator.h"
30#include <rtems/rtl/rtl-obj.h>
31#include "rtl-error.h"
32#include "rtl-find-file.h"
33#include "rtl-string.h"
34#include <rtems/rtl/rtl-trace.h>
35
36#define RTEMS_RTL_ELF_LOADER 1
37#define RTEMS_RTL_RAP_LOADER 1
38
39#if RTEMS_RTL_RAP_LOADER
40#include "rtl-rap.h"
41#define RTEMS_RTL_RAP_LOADER_COUNT 1
42#else
43#define RTEMS_RTL_RAP_LOADER_COUNT 0
44#endif
45
46#if RTEMS_RTL_ELF_LOADER
47#include "rtl-elf.h"
48#define RTEMS_RTL_ELF_LOADER_COUNT 1
49#else
50#define RTEMS_RTL_ELF_LOADER_COUNT 0
51#endif
52
53/**
54 * The table of supported loader formats.
55 */
56#define RTEMS_RTL_LOADERS (RTEMS_RTL_ELF_LOADER_COUNT + RTEMS_RTL_RAP_LOADER_COUNT)
57static const rtems_rtl_loader_table loaders[RTEMS_RTL_LOADERS] =
58{
59#if RTEMS_RTL_RAP_LOADER
60  { .check     = rtems_rtl_rap_file_check,
61    .load      = rtems_rtl_rap_file_load,
62    .unload    = rtems_rtl_rap_file_unload,
63    .unload    = rtems_rtl_rap_file_unload,
64    .signature = rtems_rtl_rap_file_sig },
65#endif
66#if RTEMS_RTL_ELF_LOADER
67  { .check     = rtems_rtl_elf_file_check,
68    .load      = rtems_rtl_elf_file_load,
69    .unload    = rtems_rtl_elf_file_unload,
70    .signature = rtems_rtl_elf_file_sig },
71#endif
72};
73
74rtems_rtl_obj*
75rtems_rtl_obj_alloc (void)
76{
77  rtems_rtl_obj* obj = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT,
78                                            sizeof (rtems_rtl_obj),
79                                            true);
80  if (obj)
81  {
82    /*
83     * Initialise the chains.
84     */
85    rtems_chain_initialize_empty (&obj->sections);
86    rtems_chain_initialize_empty (&obj->dependents);
87    /*
88     * No valid format.
89     */
90    obj->format = -1;
91  }
92  return obj;
93}
94
95static void
96rtems_rtl_obj_free_names (rtems_rtl_obj* obj)
97{
98  if (rtems_rtl_obj_oname_valid (obj))
99    rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) obj->oname);
100  if (rtems_rtl_obj_aname_valid (obj))
101    rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) obj->aname);
102  if (rtems_rtl_obj_fname_valid (obj))
103    rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) obj->fname);
104}
105
106bool
107rtems_rtl_obj_free (rtems_rtl_obj* obj)
108{
109  if (obj->users > 0 || ((obj->flags & RTEMS_RTL_OBJ_LOCKED) != 0))
110  {
111    rtems_rtl_set_error (EINVAL, "cannot free obj still in use");
112    return false;
113  }
114  if (!rtems_chain_is_node_off_chain (&obj->link))
115    rtems_chain_extract (&obj->link);
116  rtems_rtl_alloc_module_del (&obj->text_base, &obj->const_base, &obj->eh_base,
117                              &obj->data_base, &obj->bss_base);
118  rtems_rtl_obj_erase_sections (obj);
119  rtems_rtl_obj_erase_dependents (obj);
120  rtems_rtl_symbol_obj_erase (obj);
121  rtems_rtl_obj_erase_trampoline (obj);
122  rtems_rtl_obj_free_names (obj);
123  if (obj->sec_num != NULL)
124    free (obj->sec_num);
125  if (obj->linkmap != NULL)
126    rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) obj->linkmap);
127  rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, obj);
128  return true;
129}
130
131typedef struct rtems_rtl_obj_unresolved_data
132{
133  bool has_unresolved;
134} rtems_rtl_obj_unresolved_data;
135
136static bool
137rtems_rtl_obj_unresolved_dependent (rtems_rtl_obj* obj,
138                                    rtems_rtl_obj* dependent,
139                                    void*          data)
140{
141  rtems_rtl_obj_unresolved_data* ud;
142  ud = (rtems_rtl_obj_unresolved_data*) data;
143  if ((dependent->flags & RTEMS_RTL_OBJ_DEP_VISITED) == 0)
144  {
145    dependent->flags |= RTEMS_RTL_OBJ_DEP_VISITED;
146    if ((dependent->flags & RTEMS_RTL_OBJ_UNRESOLVED) != 0)
147      ud->has_unresolved = true;
148    else
149    {
150      rtems_rtl_obj_iterate_dependents (dependent,
151                                        rtems_rtl_obj_unresolved_dependent,
152                                        ud);
153    }
154    if (rtems_rtl_trace (RTEMS_RTL_TRACE_UNRESOLVED))
155      printf ("rtl: obj: unresolved: dep: %s is %s\n",
156              dependent->oname, ud->has_unresolved ? "unresolved" : "resolved");
157  }
158  return ud->has_unresolved;
159}
160
161static bool
162rtems_rtl_obj_unresolved_object (rtems_chain_node* node, void* data)
163{
164  rtems_rtl_obj*                 obj = (rtems_rtl_obj*) node;
165  rtems_rtl_obj_unresolved_data* ud;
166  ud = (rtems_rtl_obj_unresolved_data*) data;
167  ud->has_unresolved = (obj->flags & RTEMS_RTL_OBJ_UNRESOLVED) != 0;
168  return !ud->has_unresolved;
169}
170
171bool
172rtems_rtl_obj_unresolved (rtems_rtl_obj* obj)
173{
174  rtems_rtl_obj_unresolved_data ud = {
175    .has_unresolved = (obj->flags & RTEMS_RTL_OBJ_UNRESOLVED) != 0
176  };
177  if (rtems_rtl_trace (RTEMS_RTL_TRACE_UNRESOLVED))
178    printf ("rtl: obj: unresolved: dep: %s is %s\n",
179            obj->oname, ud.has_unresolved ? "unresolved" : "resolved");
180  if (!ud.has_unresolved)
181  {
182    if ((obj->flags & RTEMS_RTL_OBJ_BASE) != 0)
183    {
184      rtems_rtl_data* rtl = rtems_rtl_data_unprotected ();
185      rtems_rtl_chain_iterate (&rtl->objects,
186                               rtems_rtl_obj_unresolved_object,
187                               &ud);
188    }
189    else
190    {
191      rtems_rtl_obj_update_flags (RTEMS_RTL_OBJ_DEP_VISITED, 0);
192      obj->flags |= RTEMS_RTL_OBJ_DEP_VISITED;
193      rtems_rtl_obj_iterate_dependents (obj,
194                                        rtems_rtl_obj_unresolved_dependent,
195                                        &ud);
196      rtems_rtl_obj_update_flags (RTEMS_RTL_OBJ_DEP_VISITED, 0);
197    }
198  }
199  return ud.has_unresolved;
200}
201
202bool
203rtems_rtl_parse_name (const char*  name,
204                      const char** aname,
205                      const char** oname,
206                      off_t*       ooffset)
207{
208  const char* laname = NULL;
209  const char* loname = NULL;
210  const char* colon;
211  const char* end;
212
213  /*
214   * Parse the name to determine if the object file is part of an archive or it
215   * is an object file. If an archive check the name for a '@' to see if the
216   * archive contains an offset.
217   *
218   * Note, if an archive the object file oofset may be know but the
219   *       object file is not. Leave the object name as a NULL.
220   */
221  end = name + strlen (name);
222  colon = strrchr (name, ':');
223  if (colon == NULL || colon < strrchr(name, '/'))
224    colon = end;
225
226  loname = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT, colon - name + 1, true);
227  if (!loname)
228  {
229    rtems_rtl_set_error (ENOMEM, "no memory for object file name");
230    return false;
231  }
232
233  memcpy ((void*) loname, name, colon - name);
234
235  /*
236   * If the pointers match there is no ':' delimiter.
237   */
238  if (colon != end)
239  {
240    const char* at;
241
242    /*
243     * The file name is an archive and the object file name is next after the
244     * delimiter. Move the pointer to the archive name.
245     */
246    laname = loname;
247    ++colon;
248
249    /*
250     * See if there is a '@' to delimit an archive offset for the object in the
251     * archive.
252     */
253    at = strchr (colon, '@');
254
255    if (at == NULL)
256      at = end;
257
258
259    loname = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT, at - colon + 1, true);
260    if (!loname)
261    {
262      rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) laname);
263      rtems_rtl_set_error (ENOMEM, "no memory for object file name");
264      return false;
265    }
266
267    memcpy ((void*) loname, colon, at - colon);
268
269    if (at != end)
270    {
271      /*
272       * The object name has an archive offset. If the number
273       * does not parse 0 will be returned and the archive will be
274       * searched.
275       */
276      *ooffset = strtoul (at + 1, 0, 0);
277    }
278  }
279
280  *oname = loname;
281  *aname = laname;
282  return true;
283}
284
285static bool
286rtems_rtl_obj_parse_name (rtems_rtl_obj* obj, const char* name)
287{
288  return rtems_rtl_parse_name (name, &(obj->aname), &(obj->oname), &(obj->ooffset));
289}
290
291/**
292 * Section size summer iterator data.
293 */
294typedef struct
295{
296  uint32_t mask; /**< The selection mask to sum. */
297  size_t   size; /**< The size of all section fragments. */
298} rtems_rtl_obj_sect_summer_data;
299
300static bool
301rtems_rtl_obj_sect_summer (rtems_chain_node* node, void* data)
302{
303  rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;
304  if ((sect->flags & RTEMS_RTL_OBJ_SECT_ARCH_ALLOC) == 0)
305  {
306    rtems_rtl_obj_sect_summer_data* summer = data;
307    if ((sect->flags & summer->mask) == summer->mask)
308      summer->size =
309        rtems_rtl_obj_align (summer->size, sect->alignment) + sect->size;
310  }
311  return true;
312}
313
314static size_t
315rtems_rtl_obj_section_size (const rtems_rtl_obj* obj, uint32_t mask)
316{
317  rtems_rtl_obj_sect_summer_data summer;
318  summer.mask = mask;
319  summer.size = 0;
320  rtems_rtl_chain_iterate ((rtems_chain_control*) &obj->sections,
321                           rtems_rtl_obj_sect_summer,
322                           &summer);
323  return summer.size;
324}
325
326/**
327 * Section alignment iterator data. The first section's alignment sets the
328 * alignment for that type of section.
329 */
330typedef struct
331{
332  uint32_t mask;      /**< The selection mask to look for alignment. */
333  uint32_t alignment; /**< The alignment of the section type. */
334} rtems_rtl_obj_sect_aligner_data;
335
336/**
337 * The section aligner iterator.
338 */
339static bool
340rtems_rtl_obj_sect_aligner (rtems_chain_node* node, void* data)
341{
342  rtems_rtl_obj_sect*              sect = (rtems_rtl_obj_sect*) node;
343  rtems_rtl_obj_sect_aligner_data* aligner = data;
344  if ((sect->flags & aligner->mask) == aligner->mask)
345  {
346    aligner->alignment = sect->alignment;
347    return false;
348  }
349  return true;
350}
351
352static size_t
353rtems_rtl_obj_section_alignment (const rtems_rtl_obj* obj, uint32_t mask)
354{
355  rtems_rtl_obj_sect_aligner_data aligner;
356  aligner.mask = mask;
357  aligner.alignment = 0;
358  rtems_rtl_chain_iterate ((rtems_chain_control*) &obj->sections,
359                           rtems_rtl_obj_sect_aligner,
360                           &aligner);
361  return aligner.alignment;
362}
363
364static bool
365rtems_rtl_obj_section_handler (uint32_t                   mask,
366                               rtems_rtl_obj*             obj,
367                               int                        fd,
368                               rtems_rtl_obj_sect_handler handler,
369                               void*                      data)
370{
371  rtems_chain_node* node = rtems_chain_first (&obj->sections);
372  while (!rtems_chain_is_tail (&obj->sections, node))
373  {
374    rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;
375    if ((sect->flags & mask) != 0)
376    {
377      if (!handler (obj, fd, sect, data))
378        return false;
379    }
380    node = rtems_chain_next (node);
381  }
382  return true;
383}
384
385bool
386rtems_rtl_obj_find_file (rtems_rtl_obj* obj, const char* name)
387{
388  const char*     pname;
389  rtems_rtl_data* rtl;
390
391  /*
392   * Parse the name. The object descriptor will have the archive name and/or
393   * object name fields filled in. A find of the file will result in the file
394   * name (fname) field pointing to the actual file if present on the file
395   * system.
396   */
397  if (!rtems_rtl_obj_parse_name (obj, name))
398    return false;
399
400  /*
401   * If the archive field (aname) is set we use that name else we use the
402   * object field (oname). If selected name is absolute we just point the aname
403   * field to the fname field to that name. If the field is relative we search
404   * the paths set in the RTL for the file.
405   */
406  if (rtems_rtl_obj_aname_valid (obj))
407    pname = rtems_rtl_obj_aname (obj);
408  else
409    pname = rtems_rtl_obj_oname (obj);
410
411  rtl = rtems_rtl_lock ();
412
413  if (!rtems_rtl_find_file (pname, rtl->paths, &obj->fname, &obj->fsize))
414  {
415    rtems_rtl_set_error (ENOENT, "file not found");
416    rtems_rtl_unlock ();
417    return false;
418  }
419
420  rtems_rtl_unlock ();
421
422  return true;
423}
424
425bool
426rtems_rtl_obj_add_section (rtems_rtl_obj* obj,
427                           int            section,
428                           const char*    name,
429                           size_t         size,
430                           off_t          offset,
431                           uint32_t       alignment,
432                           int            link,
433                           int            info,
434                           uint32_t       flags)
435{
436  if (size > 0)
437  {
438    rtems_rtl_obj_sect* sect = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT,
439                                                    sizeof (rtems_rtl_obj_sect),
440                                                    true);
441    if (!sect)
442    {
443      rtems_rtl_set_error (ENOMEM, "adding allocated section");
444      return false;
445    }
446    sect->section = section;
447    sect->name = rtems_rtl_strdup (name);
448    sect->size = size;
449    sect->offset = offset;
450    sect->alignment = alignment;
451    sect->link = link;
452    sect->info = info;
453    sect->flags = flags;
454    sect->base = NULL;
455    rtems_chain_append (&obj->sections, &sect->node);
456
457    if (rtems_rtl_trace (RTEMS_RTL_TRACE_SECTION))
458      printf ("rtl: sect: add: %-2d: %s (%zu) 0x%08" PRIu32 "\n",
459              section, name, size, flags);
460  }
461  return true;
462}
463
464void
465rtems_rtl_obj_erase_sections (rtems_rtl_obj* obj)
466{
467  rtems_chain_node* node = rtems_chain_first (&obj->sections);
468  while (!rtems_chain_is_tail (&obj->sections, node))
469  {
470    rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;
471    rtems_chain_node*   next_node = rtems_chain_next (node);
472    rtems_chain_extract (node);
473    rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) sect->name);
474    rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, sect);
475    node = next_node;
476  }
477}
478
479/**
480 * Section finder iterator data.
481 */
482typedef struct
483{
484  rtems_rtl_obj_sect*  sect;  /**< The matching section. */
485  const char*          name;  /**< The name to match. */
486  int                  index; /**< The index to match. */
487  uint32_t             mask;  /**< The mask to match. */
488  unsigned int         flags; /**< The flags to use when matching. */
489} rtems_rtl_obj_sect_finder;
490
491static bool
492rtems_rtl_obj_sect_match_name (rtems_chain_node* node, void* data)
493{
494  rtems_rtl_obj_sect*        sect = (rtems_rtl_obj_sect*) node;
495  rtems_rtl_obj_sect_finder* match = data;
496  if (strcmp (sect->name, match->name) == 0)
497  {
498    match->sect = sect;
499    return false;
500  }
501  return true;
502}
503
504rtems_rtl_obj_sect*
505rtems_rtl_obj_find_section (const rtems_rtl_obj* obj,
506                            const char*          name)
507{
508  rtems_rtl_obj_sect_finder match;
509  match.sect = NULL;
510  match.name = name;
511  match.mask = 0;
512  match.flags = 0;
513  rtems_rtl_chain_iterate ((rtems_chain_control*) &obj->sections,
514                           rtems_rtl_obj_sect_match_name,
515                           &match);
516  return match.sect;
517}
518
519static bool
520rtems_rtl_obj_sect_match_index (rtems_chain_node* node, void* data)
521{
522  rtems_rtl_obj_sect*        sect = (rtems_rtl_obj_sect*) node;
523  rtems_rtl_obj_sect_finder* match = data;
524  if (sect->section == match->index)
525  {
526    match->sect = sect;
527    return false;
528  }
529  return true;
530}
531
532rtems_rtl_obj_sect*
533rtems_rtl_obj_find_section_by_index (const rtems_rtl_obj* obj,
534                                     int                  index)
535{
536  rtems_rtl_obj_sect_finder match;
537  match.sect = NULL;
538  match.index = index;
539  match.mask = 0;
540  match.flags = 0;
541  rtems_rtl_chain_iterate ((rtems_chain_control*) &obj->sections,
542                           rtems_rtl_obj_sect_match_index,
543                           &match);
544  return match.sect;
545}
546
547static bool
548rtems_rtl_obj_sect_match_mask (rtems_chain_node* node, void* data)
549{
550  rtems_rtl_obj_sect*        sect = (rtems_rtl_obj_sect*) node;
551  rtems_rtl_obj_sect_finder* match = data;
552  if (match->flags == 0)
553  {
554    if (match->index < 0 || sect->section == match->index)
555      match->flags = 1;
556    if (match->index >= 0)
557      return true;
558  }
559  if ((sect->flags & match->mask) != 0)
560  {
561    match->sect = sect;
562    return false;
563  }
564  return true;
565}
566
567rtems_rtl_obj_sect*
568rtems_rtl_obj_find_section_by_mask (const rtems_rtl_obj* obj,
569                                    int                  index,
570                                    uint32_t             mask)
571{
572  rtems_rtl_obj_sect_finder match;
573  match.sect = NULL;
574  match.index = index;
575  match.mask = mask;
576  match.flags = 0;
577  rtems_rtl_chain_iterate ((rtems_chain_control*) &obj->sections,
578                           rtems_rtl_obj_sect_match_mask,
579                           &match);
580  return match.sect;
581}
582
583bool
584rtems_rtl_obj_alloc_trampoline (rtems_rtl_obj* obj)
585{
586  if (obj->tramps_size == 0)
587    return true;
588  obj->trampoline = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT,
589                                         obj->tramps_size,
590                                         true);
591  if (obj->trampoline == NULL)
592    rtems_rtl_set_error (ENOMEM, "no memory for the trampoline");
593  obj->tramp_brk = obj->trampoline;
594  return obj->trampoline != NULL;
595}
596
597void
598rtems_rtl_obj_erase_trampoline (rtems_rtl_obj* obj)
599{
600  rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, obj->trampoline);
601}
602
603bool
604rtems_rtl_obj_alloc_dependents (rtems_rtl_obj* obj, size_t dependents)
605{
606  rtems_rtl_obj_depends* depends;
607  size_t                 size;
608
609  size = sizeof (rtems_rtl_obj_depends) + sizeof (rtems_rtl_obj*) * dependents;
610
611  depends = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT,
612                                 size,
613                                 true);
614  if (depends == NULL)
615  {
616    rtems_rtl_set_error (ENOMEM, "no memory for the dependency");
617  }
618  else
619  {
620    depends->dependents = dependents;
621    rtems_chain_append (&obj->dependents, &depends->node);
622  }
623
624  return depends != NULL;
625}
626
627void
628rtems_rtl_obj_erase_dependents (rtems_rtl_obj* obj)
629{
630  rtems_chain_node* node = rtems_chain_first (&obj->dependents);
631  while (!rtems_chain_is_tail (&obj->dependents, node))
632  {
633    rtems_rtl_obj_depends* depends = (rtems_rtl_obj_depends*) node;
634    rtems_chain_node*      next_node = rtems_chain_next (node);
635    rtems_chain_extract (node);
636    rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, depends);
637    node = next_node;
638  }
639}
640
641bool
642rtems_rtl_obj_add_dependent (rtems_rtl_obj* obj, rtems_rtl_obj* dependent)
643{
644  rtems_rtl_obj**   free_slot;
645  rtems_chain_node* node;
646
647  if (obj == dependent || dependent == rtems_rtl_baseimage ())
648    return false;
649
650  if (rtems_rtl_trace (RTEMS_RTL_TRACE_DEPENDENCY))
651    printf ("rtl: depend: add: %s -> %s\n", obj->oname, dependent->oname);
652
653  free_slot = NULL;
654
655  node = rtems_chain_first (&obj->dependents);
656  while (!rtems_chain_is_tail (&obj->dependents, node))
657  {
658    rtems_rtl_obj_depends* depends = (rtems_rtl_obj_depends*) node;
659    size_t                 d;
660    for (d = 0; d < depends->dependents; ++d)
661    {
662      if (free_slot == NULL && depends->depends[d] == NULL)
663        free_slot = &(depends->depends[d]);
664      if (depends->depends[d] == dependent)
665        return false;
666    }
667    node = rtems_chain_next (node);
668  }
669
670  if (free_slot == NULL)
671  {
672    if (rtems_rtl_obj_alloc_dependents (obj,
673                                        RTEMS_RTL_DEPENDENCY_BLOCK_SIZE))
674    {
675      rtems_rtl_obj_depends* depends;
676      node = rtems_chain_last (&obj->dependents);
677      depends = (rtems_rtl_obj_depends*) node;
678      free_slot = &(depends->depends[0]);
679      if (*free_slot != NULL)
680      {
681        rtems_rtl_set_error (EINVAL, "new dependency node not empty");
682        free_slot = NULL;
683      }
684    }
685  }
686
687  if (free_slot != NULL)
688    *free_slot = dependent;
689
690  return free_slot != NULL;
691}
692
693
694bool
695rtems_rtl_obj_remove_dependencies (rtems_rtl_obj* obj)
696{
697  /*
698   * If there are no references unload the object.
699   */
700  if (obj->refs == 0)
701  {
702    /*
703     * Remove the refences from the object files this file depend on. The
704     * unload happens once the list of objects to be unloaded has been made and
705     * the destructors have been called for all those modules.
706     */
707    rtems_chain_node* node = rtems_chain_first (&obj->dependents);
708    while (!rtems_chain_is_tail (&obj->dependents, node))
709    {
710      rtems_rtl_obj_depends* depends = (rtems_rtl_obj_depends*) node;
711      size_t                 d;
712      for (d = 0; d < depends->dependents; ++d)
713      {
714        if (depends->depends[d] != NULL)
715        {
716          rtems_rtl_obj_dec_reference (depends->depends[d]);
717          depends->depends[d] = NULL;
718        }
719      }
720      node = rtems_chain_next (node);
721    }
722    return true;
723  }
724  return false;
725}
726
727bool
728rtems_rtl_obj_iterate_dependents (rtems_rtl_obj*                 obj,
729                                  rtems_rtl_obj_depends_iterator iterator,
730                                  void*                          data)
731{
732  rtems_chain_node* node = rtems_chain_first (&obj->dependents);
733  while (!rtems_chain_is_tail (&obj->dependents, node))
734  {
735    rtems_rtl_obj_depends* depends = (rtems_rtl_obj_depends*) node;
736    size_t                 d;
737    for (d = 0; d < depends->dependents; ++d)
738    {
739      if (depends->depends[d])
740      {
741        if (iterator (obj, depends->depends[d], data))
742          return true;
743      }
744    }
745    node = rtems_chain_next (node);
746  }
747  return false;
748}
749
750size_t
751rtems_rtl_obj_text_size (const rtems_rtl_obj* obj)
752{
753  const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_TEXT;
754  return rtems_rtl_obj_section_size (obj, flags);
755}
756
757uint32_t
758rtems_rtl_obj_text_alignment (const rtems_rtl_obj* obj)
759{
760  const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_TEXT;
761  return rtems_rtl_obj_section_alignment (obj, flags);
762}
763
764size_t
765rtems_rtl_obj_const_size (const rtems_rtl_obj* obj)
766{
767  const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_CONST;
768  return rtems_rtl_obj_section_size (obj, flags);
769}
770
771uint32_t
772rtems_rtl_obj_const_alignment (const rtems_rtl_obj* obj)
773{
774  const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_CONST;
775  return rtems_rtl_obj_section_alignment (obj, flags);
776}
777
778uint32_t
779rtems_rtl_obj_eh_alignment (const rtems_rtl_obj* obj)
780{
781  const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_EH;
782  return rtems_rtl_obj_section_alignment (obj, flags);
783}
784
785size_t
786rtems_rtl_obj_eh_size (const rtems_rtl_obj* obj)
787{
788  const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_EH;
789  return rtems_rtl_obj_section_size (obj, flags);
790}
791
792size_t
793rtems_rtl_obj_data_size (const rtems_rtl_obj* obj)
794{
795  const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_DATA;
796  return rtems_rtl_obj_section_size (obj, flags);
797}
798
799uint32_t
800rtems_rtl_obj_data_alignment (const rtems_rtl_obj* obj)
801{
802  const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_DATA;
803  return rtems_rtl_obj_section_alignment (obj, flags);
804}
805
806size_t
807rtems_rtl_obj_bss_size (const rtems_rtl_obj* obj)
808{
809  return rtems_rtl_obj_section_size (obj, RTEMS_RTL_OBJ_SECT_BSS);
810}
811
812uint32_t
813rtems_rtl_obj_bss_alignment (const rtems_rtl_obj* obj)
814{
815  return rtems_rtl_obj_section_alignment (obj, RTEMS_RTL_OBJ_SECT_BSS);
816}
817
818bool
819rtems_rtl_obj_relocate (rtems_rtl_obj*             obj,
820                        int                        fd,
821                        rtems_rtl_obj_sect_handler handler,
822                        void*                      data)
823{
824  const uint32_t flags = (RTEMS_RTL_OBJ_SECT_LOAD |
825                          RTEMS_RTL_OBJ_SECT_REL |
826                          RTEMS_RTL_OBJ_SECT_RELA);
827  bool r = rtems_rtl_obj_section_handler (flags, obj, fd, handler, data);
828  rtems_rtl_obj_update_flags (RTEMS_RTL_OBJ_RELOC_TAG, 0);
829  return r;
830}
831
832/**
833 * Cache synchronization after runtime object load (dlopen)
834 */
835typedef struct
836{
837  uint32_t mask;
838  void     *start_va;
839  void     *end_va;
840  size_t   cache_line_size;
841} rtems_rtl_obj_sect_sync_ctx;
842
843static bool
844rtems_rtl_obj_sect_sync_handler (rtems_chain_node* node, void* data)
845{
846  rtems_rtl_obj_sect*          sect = (rtems_rtl_obj_sect*) node;
847  rtems_rtl_obj_sect_sync_ctx* sync_ctx = data;
848  uintptr_t                    old_end;
849  uintptr_t                    new_start;
850
851  if ((sect->flags & sync_ctx->mask) == 0 || sect->size == 0)
852    return true;
853
854  if (sync_ctx->end_va == sync_ctx->start_va)
855  {
856    sync_ctx->start_va = sect->base;
857  }
858  else
859  {
860    old_end = (uintptr_t) sync_ctx->end_va & ~(sync_ctx->cache_line_size - 1);
861    new_start = (uintptr_t) sect->base & ~(sync_ctx->cache_line_size - 1);
862    if ((sect->base <  sync_ctx->start_va) ||
863        (new_start - old_end > sync_ctx->cache_line_size))
864    {
865      rtems_cache_instruction_sync_after_code_change(sync_ctx->start_va,
866                             sync_ctx->end_va - sync_ctx->start_va + 1);
867      sync_ctx->start_va = sect->base;
868    }
869  }
870
871  sync_ctx->end_va = sect->base + sect->size;
872
873  return true;
874}
875
876void
877rtems_rtl_obj_synchronize_cache (rtems_rtl_obj* obj)
878{
879  rtems_rtl_obj_sect_sync_ctx sync_ctx;
880
881  if (rtems_cache_get_instruction_line_size() == 0)
882    return;
883
884  sync_ctx.cache_line_size = rtems_cache_get_maximal_line_size();
885
886  sync_ctx.mask = RTEMS_RTL_OBJ_SECT_TEXT | RTEMS_RTL_OBJ_SECT_CONST |
887                  RTEMS_RTL_OBJ_SECT_DATA | RTEMS_RTL_OBJ_SECT_BSS |
888                  RTEMS_RTL_OBJ_SECT_EH   | RTEMS_RTL_OBJ_SECT_EXEC;
889
890  sync_ctx.start_va = 0;
891  sync_ctx.end_va = sync_ctx.start_va;
892  rtems_rtl_chain_iterate (&obj->sections,
893                           rtems_rtl_obj_sect_sync_handler,
894                           &sync_ctx);
895
896  if (sync_ctx.end_va != sync_ctx.start_va)
897  {
898    size_t size = sync_ctx.end_va - sync_ctx.start_va;
899    rtems_cache_instruction_sync_after_code_change(sync_ctx.start_va,
900                                                   size);
901  }
902
903  if (obj->trampoline != NULL)
904  {
905    rtems_cache_instruction_sync_after_code_change(obj->trampoline,
906                                                   obj->tramps_size);
907  }
908}
909
910bool
911rtems_rtl_obj_load_symbols (rtems_rtl_obj*             obj,
912                            int                        fd,
913                            rtems_rtl_obj_sect_handler handler,
914                            void*                      data)
915{
916  uint32_t mask = RTEMS_RTL_OBJ_SECT_SYM;
917  bool     ok;
918  ok = rtems_rtl_obj_section_handler (mask, obj, fd, handler, data);
919  if (ok)
920    rtems_rtl_symbol_obj_sort (obj);
921  return ok;
922}
923
924static int
925rtems_rtl_obj_sections_linked_to_order (rtems_rtl_obj* obj,
926                                        int            section,
927                                        uint32_t       visited_mask)
928{
929  rtems_chain_control* sections = &obj->sections;
930  rtems_chain_node*    node = rtems_chain_first (sections);
931  /*
932   * Find the section being linked-to. If the linked-to link field is 0 we have
933   * the end and the section's order is the position we are after.
934   */
935  while (!rtems_chain_is_tail (sections, node))
936  {
937    rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;
938    if (sect->section == section)
939    {
940      const uint32_t mask = sect->flags & RTEMS_RTL_OBJ_SECT_TYPES;
941      int            order = 0;
942      if (sect->link != 0)
943      {
944        /*
945         * Have we already visited this type of section? Avoid nesting for
946         * ever.
947         */
948        if ((sect->flags & visited_mask) != 0)
949        {
950          rtems_rtl_set_error (errno, "section link loop");
951          return -1;
952        }
953        return rtems_rtl_obj_sections_linked_to_order (obj,
954                                                       sect->link,
955                                                       visited_mask | mask);
956      }
957      node = rtems_chain_first (sections);
958      while (!rtems_chain_is_tail (sections, node))
959      {
960        sect = (rtems_rtl_obj_sect*) node;
961        if ((sect->flags & mask) == mask)
962        {
963          if (sect->section == section)
964            return order;
965          ++order;
966        }
967        node = rtems_chain_next (node);
968      }
969    }
970    node = rtems_chain_next (node);
971  }
972  rtems_rtl_set_error (errno, "section link not found");
973  return -1;
974}
975
976static void
977rtems_rtl_obj_sections_link_order (uint32_t mask, rtems_rtl_obj* obj)
978{
979  rtems_chain_control* sections = &obj->sections;
980  rtems_chain_node*    node = rtems_chain_first (sections);
981  int                  order = 0;
982  while (!rtems_chain_is_tail (sections, node))
983  {
984    rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;
985    if ((sect->flags & mask) == mask)
986    {
987      /*
988       * If the section is linked in order find the linked-to section's order
989       * and move the section in the section list to
990       */
991      if (sect->link == 0)
992        sect->load_order = order++;
993      else
994      {
995        sect->load_order =
996          rtems_rtl_obj_sections_linked_to_order (obj,
997                                                  sect->link,
998                                                  mask);
999      }
1000    }
1001    node = rtems_chain_next (node);
1002  }
1003}
1004
1005static void
1006rtems_rtl_obj_sections_locate (uint32_t            mask,
1007                               rtems_rtl_alloc_tag tag,
1008                               rtems_rtl_obj*      obj,
1009                               uint8_t*            base)
1010{
1011  rtems_chain_control* sections = &obj->sections;
1012  rtems_chain_node*    node = rtems_chain_first (sections);
1013  size_t               base_offset = 0;
1014  int                  order = 0;
1015
1016  if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD_SECT))
1017    printf ("rtl: locating section: mask:%08" PRIx32 " base:%p\n", mask, base);
1018
1019  while (!rtems_chain_is_tail (sections, node))
1020  {
1021    rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;
1022
1023    if ((sect->size != 0) && ((sect->flags & mask) == mask))
1024    {
1025      if (sect->load_order == order)
1026      {
1027        if ((sect->flags & RTEMS_RTL_OBJ_SECT_ARCH_ALLOC) == 0)
1028        {
1029          base_offset = rtems_rtl_obj_align (base_offset, sect->alignment);
1030          sect->base = base + base_offset;
1031        }
1032
1033        if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD_SECT))
1034          printf ("rtl: locating:%2d: %s -> %p (s:%zi f:%04" PRIx32
1035                  " a:%" PRIu32 " l:%02d)\n",
1036                  order, sect->name, sect->base, sect->size,
1037                  sect->flags, sect->alignment, sect->link);
1038
1039        if (sect->base)
1040          base_offset += sect->size;
1041
1042        ++order;
1043
1044        node = rtems_chain_first (sections);
1045        continue;
1046      }
1047    }
1048
1049    node = rtems_chain_next (node);
1050  }
1051}
1052
1053bool
1054rtems_rtl_obj_alloc_sections (rtems_rtl_obj*             obj,
1055                              int                        fd,
1056                              rtems_rtl_obj_sect_handler handler,
1057                              void*                      data)
1058{
1059  size_t text_size;
1060  size_t const_size;
1061  size_t eh_size;
1062  size_t data_size;
1063  size_t bss_size;
1064
1065  text_size  = rtems_rtl_obj_text_size (obj) + rtems_rtl_obj_const_alignment (obj);
1066  const_size = rtems_rtl_obj_const_size (obj) + rtems_rtl_obj_eh_alignment (obj);
1067  eh_size    = rtems_rtl_obj_eh_size (obj) + rtems_rtl_obj_data_alignment (obj);
1068  data_size  = rtems_rtl_obj_data_size (obj) + rtems_rtl_obj_bss_alignment (obj);
1069  bss_size   = rtems_rtl_obj_bss_size (obj);
1070
1071  /*
1072   * Set the sizes held in the object data. We need this for a fast reference.
1073   */
1074  obj->text_size  = text_size;
1075  obj->const_size = const_size;
1076  obj->data_size  = data_size;
1077  obj->eh_size    = eh_size;
1078  obj->bss_size   = bss_size;
1079
1080  /*
1081   * Perform any specific allocations for sections.
1082   */
1083  if (handler != NULL)
1084  {
1085    if (!rtems_rtl_obj_section_handler (RTEMS_RTL_OBJ_SECT_TYPES,
1086                                        obj,
1087                                        fd,
1088                                        handler,
1089                                        data))
1090    {
1091      obj->exec_size = 0;
1092      return false;
1093    }
1094  }
1095
1096  /*
1097   * Let the allocator manage the actual allocation. The user can use the
1098   * standard heap or provide a specific allocator with memory protection.
1099   */
1100  if (!rtems_rtl_alloc_module_new (&obj->text_base, text_size,
1101                                   &obj->const_base, const_size,
1102                                   &obj->eh_base, eh_size,
1103                                   &obj->data_base, data_size,
1104                                   &obj->bss_base, bss_size))
1105  {
1106    obj->exec_size = 0;
1107    rtems_rtl_set_error (ENOMEM, "no memory to load obj");
1108    return false;
1109  }
1110
1111  obj->exec_size = text_size + const_size + eh_size + data_size + bss_size;
1112
1113  if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD_SECT))
1114  {
1115    printf ("rtl: load sect: text  - b:%p s:%zi a:%" PRIu32 "\n",
1116            obj->text_base, text_size, rtems_rtl_obj_text_alignment (obj));
1117    printf ("rtl: load sect: const - b:%p s:%zi a:%" PRIu32 "\n",
1118            obj->const_base, const_size, rtems_rtl_obj_const_alignment (obj));
1119    printf ("rtl: load sect: eh    - b:%p s:%zi a:%" PRIu32 "\n",
1120            obj->eh_base, eh_size, rtems_rtl_obj_eh_alignment (obj));
1121    printf ("rtl: load sect: data  - b:%p s:%zi a:%" PRIu32 "\n",
1122            obj->data_base, data_size, rtems_rtl_obj_data_alignment (obj));
1123    printf ("rtl: load sect: bss   - b:%p s:%zi a:%" PRIu32 "\n",
1124            obj->bss_base, bss_size, rtems_rtl_obj_bss_alignment (obj));
1125  }
1126
1127  /*
1128   * Determine the load order.
1129   */
1130  rtems_rtl_obj_sections_link_order (RTEMS_RTL_OBJ_SECT_TEXT,  obj);
1131  rtems_rtl_obj_sections_link_order (RTEMS_RTL_OBJ_SECT_CONST, obj);
1132  rtems_rtl_obj_sections_link_order (RTEMS_RTL_OBJ_SECT_EH,    obj);
1133  rtems_rtl_obj_sections_link_order (RTEMS_RTL_OBJ_SECT_DATA,  obj);
1134  rtems_rtl_obj_sections_link_order (RTEMS_RTL_OBJ_SECT_BSS,   obj);
1135
1136  /*
1137   * Locate all text, data and bss sections in seperate operations so each type of
1138   * section is grouped together.
1139   */
1140  rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_TEXT,
1141                                 rtems_rtl_alloc_text_tag (),
1142                                 obj, obj->text_base);
1143  rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_CONST,
1144                                 rtems_rtl_alloc_const_tag (),
1145                                 obj, obj->const_base);
1146  rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_EH,
1147                                 rtems_rtl_alloc_eh_tag (),
1148                                 obj, obj->eh_base);
1149  rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_DATA,
1150                                 rtems_rtl_alloc_data_tag (),
1151                                 obj, obj->data_base);
1152  rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_BSS,
1153                                 rtems_rtl_alloc_bss_tag (),
1154                                 obj, obj->bss_base);
1155
1156  return true;
1157}
1158
1159static bool
1160rtems_rtl_obj_sections_loader (uint32_t                   mask,
1161                               rtems_rtl_alloc_tag        tag,
1162                               rtems_rtl_obj*             obj,
1163                               int                        fd,
1164                               uint8_t*                   base,
1165                               rtems_rtl_obj_sect_handler handler,
1166                               void*                      data)
1167{
1168  rtems_chain_control* sections = &obj->sections;
1169  rtems_chain_node*    node = rtems_chain_first (sections);
1170  int                  order = 0;
1171
1172  if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD_SECT))
1173    printf ("rtl: loading section: mask:%08" PRIx32 " base:%p\n", mask, base);
1174
1175  rtems_rtl_alloc_wr_enable (tag, base);
1176
1177  while (!rtems_chain_is_tail (sections, node))
1178  {
1179    rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;
1180
1181    if ((sect->size != 0) && ((sect->flags & mask) == mask))
1182    {
1183      if (sect->load_order == order)
1184      {
1185        if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD_SECT))
1186          printf ("rtl: loading:%2d: %s -> %p (s:%zi f:%04" PRIx32
1187                  " a:%" PRIu32 " l:%02d)\n",
1188                  order, sect->name, sect->base, sect->size,
1189                  sect->flags, sect->alignment, sect->link);
1190
1191        if ((sect->flags & RTEMS_RTL_OBJ_SECT_LOAD) == RTEMS_RTL_OBJ_SECT_LOAD)
1192        {
1193          if (!handler (obj, fd, sect, data))
1194          {
1195            sect->base = 0;
1196            rtems_rtl_alloc_wr_disable (tag, base);
1197            return false;
1198          }
1199        }
1200        else if ((sect->flags & RTEMS_RTL_OBJ_SECT_ZERO) == RTEMS_RTL_OBJ_SECT_ZERO)
1201        {
1202          memset (sect->base, 0, sect->size);
1203        }
1204        else
1205        {
1206          /*
1207           * This section is not to be loaded, clear the base.
1208           */
1209          sect->base = 0;
1210        }
1211
1212        ++order;
1213
1214        node = rtems_chain_first (sections);
1215        continue;
1216      }
1217    }
1218
1219    node = rtems_chain_next (node);
1220  }
1221
1222  rtems_rtl_alloc_wr_disable (tag, base);
1223
1224  return true;
1225}
1226
1227bool
1228rtems_rtl_obj_load_sections (rtems_rtl_obj*             obj,
1229                             int                        fd,
1230                             rtems_rtl_obj_sect_handler handler,
1231                             void*                      data)
1232{
1233  /*
1234   * Load all text, data and bsssections in seperate operations so each type of
1235   * section is grouped together. Finish by loading any architecure specific
1236   * sections.
1237   */
1238  if (!rtems_rtl_obj_sections_loader (RTEMS_RTL_OBJ_SECT_TEXT,
1239                                      rtems_rtl_alloc_text_tag (),
1240                                      obj, fd, obj->text_base, handler, data) ||
1241      !rtems_rtl_obj_sections_loader (RTEMS_RTL_OBJ_SECT_CONST,
1242                                      rtems_rtl_alloc_const_tag (),
1243                                      obj, fd, obj->const_base, handler, data) ||
1244      !rtems_rtl_obj_sections_loader (RTEMS_RTL_OBJ_SECT_EH,
1245                                      rtems_rtl_alloc_eh_tag (),
1246                                      obj, fd, obj->eh_base, handler, data) ||
1247      !rtems_rtl_obj_sections_loader (RTEMS_RTL_OBJ_SECT_DATA,
1248                                      rtems_rtl_alloc_data_tag (),
1249                                      obj, fd, obj->data_base, handler, data) ||
1250      !rtems_rtl_obj_sections_loader (RTEMS_RTL_OBJ_SECT_BSS,
1251                                      rtems_rtl_alloc_bss_tag (),
1252                                      obj, fd, obj->bss_base, handler, data))
1253  {
1254    rtems_rtl_alloc_module_del (&obj->text_base, &obj->const_base, &obj->eh_base,
1255                                &obj->data_base, &obj->bss_base);
1256    obj->exec_size = 0;
1257    return false;
1258  }
1259
1260  return true;
1261}
1262
1263static void
1264rtems_rtl_obj_run_cdtors (rtems_rtl_obj* obj, uint32_t mask)
1265{
1266  rtems_chain_node* node = rtems_chain_first (&obj->sections);
1267  while (!rtems_chain_is_tail (&obj->sections, node))
1268  {
1269    rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;
1270    if ((sect->flags & mask) == mask)
1271    {
1272      rtems_rtl_cdtor* handler;
1273      size_t           handlers = sect->size / sizeof (rtems_rtl_cdtor);
1274      int              c;
1275      for (c = 0, handler = sect->base; c < handlers; ++c)
1276        if (*handler)
1277          (*handler) ();
1278    }
1279    node = rtems_chain_next (node);
1280  }
1281}
1282
1283static bool
1284rtems_rtl_obj_cdtors_to_run (rtems_rtl_obj* obj, uint32_t mask)
1285{
1286  rtems_chain_node* node = rtems_chain_first (&obj->sections);
1287  while (!rtems_chain_is_tail (&obj->sections, node))
1288  {
1289    rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;
1290    if ((sect->flags & mask) == mask)
1291      return true;
1292    node = rtems_chain_next (node);
1293  }
1294  return false;
1295}
1296
1297bool
1298rtems_rtl_obj_ctors_to_run (rtems_rtl_obj* obj)
1299{
1300  return rtems_rtl_obj_cdtors_to_run (obj, RTEMS_RTL_OBJ_SECT_CTOR);
1301}
1302
1303void
1304rtems_rtl_obj_run_ctors (rtems_rtl_obj* obj)
1305{
1306  rtems_rtl_obj_run_cdtors (obj, RTEMS_RTL_OBJ_SECT_CTOR);
1307}
1308
1309bool
1310rtems_rtl_obj_dtors_to_run (rtems_rtl_obj* obj)
1311{
1312  return rtems_rtl_obj_cdtors_to_run (obj, RTEMS_RTL_OBJ_SECT_DTOR);
1313}
1314
1315void
1316rtems_rtl_obj_run_dtors (rtems_rtl_obj* obj)
1317{
1318  rtems_rtl_obj_run_cdtors (obj, RTEMS_RTL_OBJ_SECT_DTOR);
1319}
1320
1321static bool
1322rtems_rtl_obj_file_load (rtems_rtl_obj* obj, int fd)
1323{
1324  int l;
1325
1326  for (l = 0; l < (sizeof (loaders) / sizeof (rtems_rtl_loader_table)); ++l)
1327  {
1328    if (loaders[l].check (obj, fd))
1329    {
1330      obj->format = l;
1331      return loaders[l].load (obj, fd);
1332    }
1333  }
1334
1335  rtems_rtl_set_error (ENOENT, "no format loader found");
1336  return false;
1337}
1338
1339static void
1340rtems_rtl_obj_set_error (int num, const char* text)
1341{
1342  rtems_rtl_set_error (num, text);
1343}
1344
1345size_t
1346rtems_rtl_obj_get_reference (rtems_rtl_obj* obj)
1347{
1348  return obj->refs;
1349}
1350
1351void
1352rtems_rtl_obj_inc_reference (rtems_rtl_obj* obj)
1353{
1354  ++obj->refs;
1355}
1356
1357void
1358rtems_rtl_obj_dec_reference (rtems_rtl_obj* obj)
1359{
1360  if (obj->refs)
1361    --obj->refs;
1362}
1363
1364bool
1365rtems_rtl_obj_orphaned (rtems_rtl_obj* obj)
1366{
1367  return ((obj->flags & RTEMS_RTL_OBJ_LOCKED) == 0 &&
1368          obj->users == 0 &&
1369          rtems_rtl_obj_get_reference (obj) == 0);
1370}
1371
1372bool
1373rtems_rtl_obj_load (rtems_rtl_obj* obj)
1374{
1375  int fd;
1376
1377  if (!rtems_rtl_obj_fname_valid (obj))
1378  {
1379    rtems_rtl_set_error (ENOMEM, "invalid object file name path");
1380    return false;
1381  }
1382
1383  fd = open (rtems_rtl_obj_fname (obj), O_RDONLY);
1384  if (fd < 0)
1385  {
1386    rtems_rtl_set_error (errno, "opening for object file");
1387    return false;
1388  }
1389
1390  /*
1391   * Find the object file in the archive if it is an archive that
1392   * has been opened.
1393   */
1394  if (rtems_rtl_obj_aname_valid (obj))
1395  {
1396    off_t enames = 0;
1397    if (!rtems_rtl_obj_archive_find_obj (fd,
1398                                         obj->fsize,
1399                                         &obj->oname,
1400                                         &obj->ooffset,
1401                                         &obj->fsize,
1402                                         &enames,
1403                                         rtems_rtl_obj_set_error))
1404    {
1405      close (fd);
1406      return false;
1407    }
1408  }
1409
1410  /*
1411   * Call the format specific loader.
1412   */
1413  if (!rtems_rtl_obj_file_load (obj, fd))
1414  {
1415    close (fd);
1416    return false;
1417  }
1418
1419   /*
1420    * For GDB
1421    */
1422  if (!_rtld_linkmap_add (obj))
1423  {
1424    close (fd);
1425    return false;
1426  }
1427
1428  close (fd);
1429
1430  return true;
1431}
1432
1433bool
1434rtems_rtl_obj_unload (rtems_rtl_obj* obj)
1435{
1436  bool ok = false;
1437  if (obj->format >= 0 && obj->format < RTEMS_RTL_LOADERS)
1438  {
1439    _rtld_linkmap_delete(obj);
1440    ok = loaders[obj->format].unload (obj);
1441  }
1442  else
1443  {
1444    rtems_rtl_set_error (EINVAL, "invalid object loader format");
1445  }
1446  return ok;
1447}
Note: See TracBrowser for help on using the repository browser.