summaryrefslogtreecommitdiffstats
path: root/cpukit/libdl/rtl-obj.c
blob: 1683ec1c6da9f72b526e4ce2e80a51b33d2a2624 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
/*
 *  COPYRIGHT (c) 2012, 2018 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 Run-Time Linker Error
 */

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

#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <rtems/libio_.h>

#include <rtems/rtl/rtl.h>
#include "rtl-chain-iterator.h"
#include <rtems/rtl/rtl-obj.h>
#include "rtl-error.h"
#include "rtl-find-file.h"
#include "rtl-string.h"
#include <rtems/rtl/rtl-trace.h>

#define RTEMS_RTL_ELF_LOADER 1
#define RTEMS_RTL_RAP_LOADER 1

#if RTEMS_RTL_RAP_LOADER
#include "rtl-rap.h"
#define RTEMS_RTL_RAP_LOADER_COUNT 1
#else
#define RTEMS_RTL_RAP_LOADER_COUNT 0
#endif

#if RTEMS_RTL_ELF_LOADER
#include "rtl-elf.h"
#define RTEMS_RTL_ELF_LOADER_COUNT 1
#else
#define RTEMS_RTL_ELF_LOADER_COUNT 0
#endif

/**
 * The table of supported loader formats.
 */
#define RTEMS_RTL_LOADERS (RTEMS_RTL_ELF_LOADER_COUNT + RTEMS_RTL_RAP_LOADER_COUNT)
static const rtems_rtl_loader_table loaders[RTEMS_RTL_LOADERS] =
{
#if RTEMS_RTL_RAP_LOADER
  { .check     = rtems_rtl_rap_file_check,
    .load      = rtems_rtl_rap_file_load,
    .unload    = rtems_rtl_rap_file_unload,
    .unload    = rtems_rtl_rap_file_unload,
    .signature = rtems_rtl_rap_file_sig },
#endif
#if RTEMS_RTL_ELF_LOADER
  { .check     = rtems_rtl_elf_file_check,
    .load      = rtems_rtl_elf_file_load,
    .unload    = rtems_rtl_elf_file_unload,
    .signature = rtems_rtl_elf_file_sig },
#endif
};

rtems_rtl_obj*
rtems_rtl_obj_alloc (void)
{
  rtems_rtl_obj* obj = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT,
                                            sizeof (rtems_rtl_obj),
                                            true);
  if (obj)
  {
    /*
     * Initialise the chains.
     */
    rtems_chain_initialize_empty (&obj->sections);
    rtems_chain_initialize_empty (&obj->dependents);
    /*
     * No valid format.
     */
    obj->format = -1;
  }
  return obj;
}

static void
rtems_rtl_obj_free_names (rtems_rtl_obj* obj)
{
  if (rtems_rtl_obj_oname_valid (obj))
    rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) obj->oname);
  if (rtems_rtl_obj_aname_valid (obj))
    rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) obj->aname);
  if (rtems_rtl_obj_fname_valid (obj))
    rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) obj->fname);
}

bool
rtems_rtl_obj_free (rtems_rtl_obj* obj)
{
  if (obj->users > 0 || ((obj->flags & RTEMS_RTL_OBJ_LOCKED) != 0))
  {
    rtems_rtl_set_error (EINVAL, "cannot free obj still in use");
    return false;
  }
  if (!rtems_chain_is_node_off_chain (&obj->link))
    rtems_chain_extract (&obj->link);
  rtems_rtl_alloc_module_del (&obj->text_base, &obj->const_base, &obj->eh_base,
                              &obj->data_base, &obj->bss_base);
  rtems_rtl_obj_erase_sections (obj);
  rtems_rtl_obj_erase_dependents (obj);
  rtems_rtl_symbol_obj_erase (obj);
  rtems_rtl_obj_erase_trampoline (obj);
  rtems_rtl_obj_free_names (obj);
  if (obj->sec_num != NULL)
    free (obj->sec_num);
  if (obj->linkmap != NULL)
    rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) obj->linkmap);
  rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, obj);
  return true;
}

typedef struct rtems_rtl_obj_unresolved_data
{
  bool has_unresolved;
} rtems_rtl_obj_unresolved_data;

static bool
rtems_rtl_obj_unresolved_dependent (rtems_rtl_obj* obj,
                                    rtems_rtl_obj* dependent,
                                    void*          data)
{
  rtems_rtl_obj_unresolved_data* ud;
  ud = (rtems_rtl_obj_unresolved_data*) data;
  if ((dependent->flags & RTEMS_RTL_OBJ_DEP_VISITED) == 0)
  {
    dependent->flags |= RTEMS_RTL_OBJ_DEP_VISITED;
    if ((dependent->flags & RTEMS_RTL_OBJ_UNRESOLVED) != 0)
      ud->has_unresolved = true;
    else
    {
      rtems_rtl_obj_iterate_dependents (dependent,
                                        rtems_rtl_obj_unresolved_dependent,
                                        ud);
    }
    if (rtems_rtl_trace (RTEMS_RTL_TRACE_UNRESOLVED))
      printf ("rtl: obj: unresolved: dep: %s is %s\n",
              dependent->oname, ud->has_unresolved ? "unresolved" : "resolved");
  }
  return ud->has_unresolved;
}

static bool
rtems_rtl_obj_unresolved_object (rtems_chain_node* node, void* data)
{
  rtems_rtl_obj*                 obj = (rtems_rtl_obj*) node;
  rtems_rtl_obj_unresolved_data* ud;
  ud = (rtems_rtl_obj_unresolved_data*) data;
  ud->has_unresolved = (obj->flags & RTEMS_RTL_OBJ_UNRESOLVED) != 0;
  return !ud->has_unresolved;
}

bool
rtems_rtl_obj_unresolved (rtems_rtl_obj* obj)
{
  rtems_rtl_obj_unresolved_data ud = {
    .has_unresolved = (obj->flags & RTEMS_RTL_OBJ_UNRESOLVED) != 0
  };
  if (rtems_rtl_trace (RTEMS_RTL_TRACE_UNRESOLVED))
    printf ("rtl: obj: unresolved: dep: %s is %s\n",
            obj->oname, ud.has_unresolved ? "unresolved" : "resolved");
  if (!ud.has_unresolved)
  {
    if ((obj->flags & RTEMS_RTL_OBJ_BASE) != 0)
    {
      rtems_rtl_data* rtl = rtems_rtl_data_unprotected ();
      rtems_rtl_chain_iterate (&rtl->objects,
                               rtems_rtl_obj_unresolved_object,
                               &ud);
    }
    else
    {
      rtems_rtl_obj_update_flags (RTEMS_RTL_OBJ_DEP_VISITED, 0);
      obj->flags |= RTEMS_RTL_OBJ_DEP_VISITED;
      rtems_rtl_obj_iterate_dependents (obj,
                                        rtems_rtl_obj_unresolved_dependent,
                                        &ud);
      rtems_rtl_obj_update_flags (RTEMS_RTL_OBJ_DEP_VISITED, 0);
    }
  }
  return ud.has_unresolved;
}

bool
rtems_rtl_parse_name (const char*  name,
                      const char** aname,
                      const char** oname,
                      off_t*       ooffset)
{
  const char* laname = NULL;
  const char* loname = NULL;
  const char* colon;
  const char* end;

  /*
   * Parse the name to determine if the object file is part of an archive or it
   * is an object file. If an archive check the name for a '@' to see if the
   * archive contains an offset.
   *
   * Note, if an archive the object file oofset may be know but the
   *       object file is not. Leave the object name as a NULL.
   */
  end = name + strlen (name);
  colon = strrchr (name, ':');
  if (colon == NULL || colon < strrchr(name, '/'))
    colon = end;

  loname = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT, colon - name + 1, true);
  if (!loname)
  {
    rtems_rtl_set_error (ENOMEM, "no memory for object file name");
    return false;
  }

  memcpy ((void*) loname, name, colon - name);

  /*
   * If the pointers match there is no ':' delimiter.
   */
  if (colon != end)
  {
    const char* at;

    /*
     * The file name is an archive and the object file name is next after the
     * delimiter. Move the pointer to the archive name.
     */
    laname = loname;
    ++colon;

    /*
     * See if there is a '@' to delimit an archive offset for the object in the
     * archive.
     */
    at = strchr (colon, '@');

    if (at == NULL)
      at = end;


    loname = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT, at - colon + 1, true);
    if (!loname)
    {
      rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) laname);
      rtems_rtl_set_error (ENOMEM, "no memory for object file name");
      return false;
    }

    memcpy ((void*) loname, colon, at - colon);

    if (at != end)
    {
      /*
       * The object name has an archive offset. If the number
       * does not parse 0 will be returned and the archive will be
       * searched.
       */
      *ooffset = strtoul (at + 1, 0, 0);
    }
  }

  *oname = loname;
  *aname = laname;
  return true;
}

static bool
rtems_rtl_obj_parse_name (rtems_rtl_obj* obj, const char* name)
{
  return rtems_rtl_parse_name (name, &(obj->aname), &(obj->oname), &(obj->ooffset));
}

/**
 * Section size summer iterator data.
 */
typedef struct
{
  uint32_t mask; /**< The selection mask to sum. */
  size_t   size; /**< The size of all section fragments. */
} rtems_rtl_obj_sect_summer_data;

static bool
rtems_rtl_obj_sect_summer (rtems_chain_node* node, void* data)
{
  rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;
  if ((sect->flags & RTEMS_RTL_OBJ_SECT_ARCH_ALLOC) == 0)
  {
    rtems_rtl_obj_sect_summer_data* summer = data;
    if ((sect->flags & summer->mask) == summer->mask)
      summer->size =
        rtems_rtl_obj_align (summer->size, sect->alignment) + sect->size;
  }
  return true;
}

static size_t
rtems_rtl_obj_section_size (const rtems_rtl_obj* obj, uint32_t mask)
{
  rtems_rtl_obj_sect_summer_data summer;
  summer.mask = mask;
  summer.size = 0;
  rtems_rtl_chain_iterate ((rtems_chain_control*) &obj->sections,
                           rtems_rtl_obj_sect_summer,
                           &summer);
  return summer.size;
}

/**
 * Section alignment iterator data. The first section's alignment sets the
 * alignment for that type of section.
 */
typedef struct
{
  uint32_t mask;      /**< The selection mask to look for alignment. */
  uint32_t alignment; /**< The alignment of the section type. */
} rtems_rtl_obj_sect_aligner_data;

/**
 * The section aligner iterator.
 */
static bool
rtems_rtl_obj_sect_aligner (rtems_chain_node* node, void* data)
{
  rtems_rtl_obj_sect*              sect = (rtems_rtl_obj_sect*) node;
  rtems_rtl_obj_sect_aligner_data* aligner = data;
  if ((sect->flags & aligner->mask) == aligner->mask)
  {
    aligner->alignment = sect->alignment;
    return false;
  }
  return true;
}

static size_t
rtems_rtl_obj_section_alignment (const rtems_rtl_obj* obj, uint32_t mask)
{
  rtems_rtl_obj_sect_aligner_data aligner;
  aligner.mask = mask;
  aligner.alignment = 0;
  rtems_rtl_chain_iterate ((rtems_chain_control*) &obj->sections,
                           rtems_rtl_obj_sect_aligner,
                           &aligner);
  return aligner.alignment;
}

static bool
rtems_rtl_obj_section_handler (uint32_t                   mask,
                               rtems_rtl_obj*             obj,
                               int                        fd,
                               rtems_rtl_obj_sect_handler handler,
                               void*                      data)
{
  rtems_chain_node* node = rtems_chain_first (&obj->sections);
  while (!rtems_chain_is_tail (&obj->sections, node))
  {
    rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;
    if ((sect->flags & mask) != 0)
    {
      if (!handler (obj, fd, sect, data))
        return false;
    }
    node = rtems_chain_next (node);
  }
  return true;
}

bool
rtems_rtl_obj_find_file (rtems_rtl_obj* obj, const char* name)
{
  const char*     pname;
  rtems_rtl_data* rtl;

  /*
   * Parse the name. The object descriptor will have the archive name and/or
   * object name fields filled in. A find of the file will result in the file
   * name (fname) field pointing to the actual file if present on the file
   * system.
   */
  if (!rtems_rtl_obj_parse_name (obj, name))
    return false;

  /*
   * If the archive field (aname) is set we use that name else we use the
   * object field (oname). If selected name is absolute we just point the aname
   * field to the fname field to that name. If the field is relative we search
   * the paths set in the RTL for the file.
   */
  if (rtems_rtl_obj_aname_valid (obj))
    pname = rtems_rtl_obj_aname (obj);
  else
    pname = rtems_rtl_obj_oname (obj);

  rtl = rtems_rtl_lock ();

  if (!rtems_rtl_find_file (pname, rtl->paths, &obj->fname, &obj->fsize))
  {
    rtems_rtl_set_error (ENOENT, "file not found");
    rtems_rtl_unlock ();
    return false;
  }

  rtems_rtl_unlock ();

  return true;
}

bool
rtems_rtl_obj_add_section (rtems_rtl_obj* obj,
                           int            section,
                           const char*    name,
                           size_t         size,
                           off_t          offset,
                           uint32_t       alignment,
                           int            link,
                           int            info,
                           uint32_t       flags)
{
  if (size > 0)
  {
    rtems_rtl_obj_sect* sect = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT,
                                                    sizeof (rtems_rtl_obj_sect),
                                                    true);
    if (!sect)
    {
      rtems_rtl_set_error (ENOMEM, "adding allocated section");
      return false;
    }
    sect->section = section;
    sect->name = rtems_rtl_strdup (name);
    sect->size = size;
    sect->offset = offset;
    sect->alignment = alignment;
    sect->link = link;
    sect->info = info;
    sect->flags = flags;
    sect->base = NULL;
    rtems_chain_append (&obj->sections, &sect->node);

    if (rtems_rtl_trace (RTEMS_RTL_TRACE_SECTION))
      printf ("rtl: sect: add: %-2d: %s (%zu) 0x%08" PRIu32 "\n",
              section, name, size, flags);
  }
  return true;
}

void
rtems_rtl_obj_erase_sections (rtems_rtl_obj* obj)
{
  rtems_chain_node* node = rtems_chain_first (&obj->sections);
  while (!rtems_chain_is_tail (&obj->sections, node))
  {
    rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;
    rtems_chain_node*   next_node = rtems_chain_next (node);
    rtems_chain_extract (node);
    rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, (void*) sect->name);
    rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, sect);
    node = next_node;
  }
}

/**
 * Section finder iterator data.
 */
typedef struct
{
  rtems_rtl_obj_sect*  sect;  /**< The matching section. */
  const char*          name;  /**< The name to match. */
  int                  index; /**< The index to match. */
  uint32_t             mask;  /**< The mask to match. */
  unsigned int         flags; /**< The flags to use when matching. */
} rtems_rtl_obj_sect_finder;

static bool
rtems_rtl_obj_sect_match_name (rtems_chain_node* node, void* data)
{
  rtems_rtl_obj_sect*        sect = (rtems_rtl_obj_sect*) node;
  rtems_rtl_obj_sect_finder* match = data;
  if (strcmp (sect->name, match->name) == 0)
  {
    match->sect = sect;
    return false;
  }
  return true;
}

rtems_rtl_obj_sect*
rtems_rtl_obj_find_section (const rtems_rtl_obj* obj,
                            const char*          name)
{
  rtems_rtl_obj_sect_finder match;
  match.sect = NULL;
  match.name = name;
  match.mask = 0;
  match.flags = 0;
  rtems_rtl_chain_iterate ((rtems_chain_control*) &obj->sections,
                           rtems_rtl_obj_sect_match_name,
                           &match);
  return match.sect;
}

static bool
rtems_rtl_obj_sect_match_index (rtems_chain_node* node, void* data)
{
  rtems_rtl_obj_sect*        sect = (rtems_rtl_obj_sect*) node;
  rtems_rtl_obj_sect_finder* match = data;
  if (sect->section == match->index)
  {
    match->sect = sect;
    return false;
  }
  return true;
}

rtems_rtl_obj_sect*
rtems_rtl_obj_find_section_by_index (const rtems_rtl_obj* obj,
                                     int                  index)
{
  rtems_rtl_obj_sect_finder match;
  match.sect = NULL;
  match.index = index;
  match.mask = 0;
  match.flags = 0;
  rtems_rtl_chain_iterate ((rtems_chain_control*) &obj->sections,
                           rtems_rtl_obj_sect_match_index,
                           &match);
  return match.sect;
}

static bool
rtems_rtl_obj_sect_match_mask (rtems_chain_node* node, void* data)
{
  rtems_rtl_obj_sect*        sect = (rtems_rtl_obj_sect*) node;
  rtems_rtl_obj_sect_finder* match = data;
  if (match->flags == 0)
  {
    if (match->index < 0 || sect->section == match->index)
      match->flags = 1;
    if (match->index >= 0)
      return true;
  }
  if ((sect->flags & match->mask) != 0)
  {
    match->sect = sect;
    return false;
  }
  return true;
}

rtems_rtl_obj_sect*
rtems_rtl_obj_find_section_by_mask (const rtems_rtl_obj* obj,
                                    int                  index,
                                    uint32_t             mask)
{
  rtems_rtl_obj_sect_finder match;
  match.sect = NULL;
  match.index = index;
  match.mask = mask;
  match.flags = 0;
  rtems_rtl_chain_iterate ((rtems_chain_control*) &obj->sections,
                           rtems_rtl_obj_sect_match_mask,
                           &match);
  return match.sect;
}

bool
rtems_rtl_obj_alloc_trampoline (rtems_rtl_obj* obj)
{
  if (obj->tramps_size == 0)
    return true;
  obj->trampoline = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT,
                                         obj->tramps_size,
                                         true);
  if (obj->trampoline == NULL)
    rtems_rtl_set_error (ENOMEM, "no memory for the trampoline");
  obj->tramp_brk = obj->trampoline;
  return obj->trampoline != NULL;
}

void
rtems_rtl_obj_erase_trampoline (rtems_rtl_obj* obj)
{
  rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, obj->trampoline);
}

bool
rtems_rtl_obj_alloc_dependents (rtems_rtl_obj* obj, size_t dependents)
{
  rtems_rtl_obj_depends* depends;
  size_t                 size;

  size = sizeof (rtems_rtl_obj_depends) + sizeof (rtems_rtl_obj*) * dependents;

  depends = rtems_rtl_alloc_new (RTEMS_RTL_ALLOC_OBJECT,
                                 size,
                                 true);
  if (depends == NULL)
  {
    rtems_rtl_set_error (ENOMEM, "no memory for the dependency");
  }
  else
  {
    depends->dependents = dependents;
    rtems_chain_append (&obj->dependents, &depends->node);
  }

  return depends != NULL;
}

void
rtems_rtl_obj_erase_dependents (rtems_rtl_obj* obj)
{
  rtems_chain_node* node = rtems_chain_first (&obj->dependents);
  while (!rtems_chain_is_tail (&obj->dependents, node))
  {
    rtems_rtl_obj_depends* depends = (rtems_rtl_obj_depends*) node;
    rtems_chain_node*      next_node = rtems_chain_next (node);
    rtems_chain_extract (node);
    rtems_rtl_alloc_del (RTEMS_RTL_ALLOC_OBJECT, depends);
    node = next_node;
  }
}

bool
rtems_rtl_obj_add_dependent (rtems_rtl_obj* obj, rtems_rtl_obj* dependent)
{
  rtems_rtl_obj**   free_slot;
  rtems_chain_node* node;

  if (obj == dependent || dependent == rtems_rtl_baseimage ())
    return false;

  if (rtems_rtl_trace (RTEMS_RTL_TRACE_DEPENDENCY))
    printf ("rtl: depend: add: %s -> %s\n", obj->oname, dependent->oname);

  free_slot = NULL;

  node = rtems_chain_first (&obj->dependents);
  while (!rtems_chain_is_tail (&obj->dependents, node))
  {
    rtems_rtl_obj_depends* depends = (rtems_rtl_obj_depends*) node;
    size_t                 d;
    for (d = 0; d < depends->dependents; ++d)
    {
      if (free_slot == NULL && depends->depends[d] == NULL)
        free_slot = &(depends->depends[d]);
      if (depends->depends[d] == dependent)
        return false;
    }
    node = rtems_chain_next (node);
  }

  if (free_slot == NULL)
  {
    if (rtems_rtl_obj_alloc_dependents (obj,
                                        RTEMS_RTL_DEPENDENCY_BLOCK_SIZE))
    {
      rtems_rtl_obj_depends* depends;
      node = rtems_chain_last (&obj->dependents);
      depends = (rtems_rtl_obj_depends*) node;
      free_slot = &(depends->depends[0]);
      if (*free_slot != NULL)
      {
        rtems_rtl_set_error (EINVAL, "new dependency node not empty");
        free_slot = NULL;
      }
    }
  }

  if (free_slot != NULL)
    *free_slot = dependent;

  return free_slot != NULL;
}


bool
rtems_rtl_obj_remove_dependencies (rtems_rtl_obj* obj)
{
  /*
   * If there are no references unload the object.
   */
  if (obj->refs == 0)
  {
    /*
     * Remove the refences from the object files this file depend on. The
     * unload happens once the list of objects to be unloaded has been made and
     * the destructors have been called for all those modules.
     */
    rtems_chain_node* node = rtems_chain_first (&obj->dependents);
    while (!rtems_chain_is_tail (&obj->dependents, node))
    {
      rtems_rtl_obj_depends* depends = (rtems_rtl_obj_depends*) node;
      size_t                 d;
      for (d = 0; d < depends->dependents; ++d)
      {
        if (depends->depends[d] != NULL)
        {
          rtems_rtl_obj_dec_reference (depends->depends[d]);
          depends->depends[d] = NULL;
        }
      }
      node = rtems_chain_next (node);
    }
    return true;
  }
  return false;
}

bool
rtems_rtl_obj_iterate_dependents (rtems_rtl_obj*                 obj,
                                  rtems_rtl_obj_depends_iterator iterator,
                                  void*                          data)
{
  rtems_chain_node* node = rtems_chain_first (&obj->dependents);
  while (!rtems_chain_is_tail (&obj->dependents, node))
  {
    rtems_rtl_obj_depends* depends = (rtems_rtl_obj_depends*) node;
    size_t                 d;
    for (d = 0; d < depends->dependents; ++d)
    {
      if (depends->depends[d])
      {
        if (iterator (obj, depends->depends[d], data))
          return true;
      }
    }
    node = rtems_chain_next (node);
  }
  return false;
}

size_t
rtems_rtl_obj_text_size (const rtems_rtl_obj* obj)
{
  const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_TEXT;
  return rtems_rtl_obj_section_size (obj, flags);
}

uint32_t
rtems_rtl_obj_text_alignment (const rtems_rtl_obj* obj)
{
  const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_TEXT;
  return rtems_rtl_obj_section_alignment (obj, flags);
}

size_t
rtems_rtl_obj_const_size (const rtems_rtl_obj* obj)
{
  const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_CONST;
  return rtems_rtl_obj_section_size (obj, flags);
}

uint32_t
rtems_rtl_obj_const_alignment (const rtems_rtl_obj* obj)
{
  const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_CONST;
  return rtems_rtl_obj_section_alignment (obj, flags);
}

uint32_t
rtems_rtl_obj_eh_alignment (const rtems_rtl_obj* obj)
{
  const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_EH;
  return rtems_rtl_obj_section_alignment (obj, flags);
}

size_t
rtems_rtl_obj_eh_size (const rtems_rtl_obj* obj)
{
  const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_EH;
  return rtems_rtl_obj_section_size (obj, flags);
}

size_t
rtems_rtl_obj_data_size (const rtems_rtl_obj* obj)
{
  const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_DATA;
  return rtems_rtl_obj_section_size (obj, flags);
}

uint32_t
rtems_rtl_obj_data_alignment (const rtems_rtl_obj* obj)
{
  const uint32_t flags = RTEMS_RTL_OBJ_SECT_LOAD | RTEMS_RTL_OBJ_SECT_DATA;
  return rtems_rtl_obj_section_alignment (obj, flags);
}

size_t
rtems_rtl_obj_bss_size (const rtems_rtl_obj* obj)
{
  return rtems_rtl_obj_section_size (obj, RTEMS_RTL_OBJ_SECT_BSS);
}

uint32_t
rtems_rtl_obj_bss_alignment (const rtems_rtl_obj* obj)
{
  return rtems_rtl_obj_section_alignment (obj, RTEMS_RTL_OBJ_SECT_BSS);
}

bool
rtems_rtl_obj_relocate (rtems_rtl_obj*             obj,
                        int                        fd,
                        rtems_rtl_obj_sect_handler handler,
                        void*                      data)
{
  const uint32_t flags = (RTEMS_RTL_OBJ_SECT_LOAD |
                          RTEMS_RTL_OBJ_SECT_REL |
                          RTEMS_RTL_OBJ_SECT_RELA);
  bool r = rtems_rtl_obj_section_handler (flags, obj, fd, handler, data);
  rtems_rtl_obj_update_flags (RTEMS_RTL_OBJ_RELOC_TAG, 0);
  return r;
}

/**
 * Cache synchronization after runtime object load (dlopen)
 */
typedef struct
{
  uint32_t mask;
  void     *start_va;
  void     *end_va;
  size_t   cache_line_size;
} rtems_rtl_obj_sect_sync_ctx;

static bool
rtems_rtl_obj_sect_sync_handler (rtems_chain_node* node, void* data)
{
  rtems_rtl_obj_sect*          sect = (rtems_rtl_obj_sect*) node;
  rtems_rtl_obj_sect_sync_ctx* sync_ctx = data;
  uintptr_t                    old_end;
  uintptr_t                    new_start;

  if ((sect->flags & sync_ctx->mask) == 0 || sect->size == 0)
    return true;

  if (sync_ctx->end_va == sync_ctx->start_va)
  {
    sync_ctx->start_va = sect->base;
  }
  else
  {
    old_end = (uintptr_t) sync_ctx->end_va & ~(sync_ctx->cache_line_size - 1);
    new_start = (uintptr_t) sect->base & ~(sync_ctx->cache_line_size - 1);
    if ((sect->base <  sync_ctx->start_va) ||
        (new_start - old_end > sync_ctx->cache_line_size))
    {
      rtems_cache_instruction_sync_after_code_change(sync_ctx->start_va,
                             sync_ctx->end_va - sync_ctx->start_va + 1);
      sync_ctx->start_va = sect->base;
    }
  }

  sync_ctx->end_va = sect->base + sect->size;

  return true;
}

void
rtems_rtl_obj_synchronize_cache (rtems_rtl_obj* obj)
{
  rtems_rtl_obj_sect_sync_ctx sync_ctx;

  if (rtems_cache_get_instruction_line_size() == 0)
    return;

  sync_ctx.cache_line_size = rtems_cache_get_maximal_line_size();

  sync_ctx.mask = RTEMS_RTL_OBJ_SECT_TEXT | RTEMS_RTL_OBJ_SECT_CONST |
                  RTEMS_RTL_OBJ_SECT_DATA | RTEMS_RTL_OBJ_SECT_BSS |
                  RTEMS_RTL_OBJ_SECT_EH   | RTEMS_RTL_OBJ_SECT_EXEC;

  sync_ctx.start_va = 0;
  sync_ctx.end_va = sync_ctx.start_va;
  rtems_rtl_chain_iterate (&obj->sections,
                           rtems_rtl_obj_sect_sync_handler,
                           &sync_ctx);

  if (sync_ctx.end_va != sync_ctx.start_va)
  {
    size_t size = sync_ctx.end_va - sync_ctx.start_va;
    rtems_cache_instruction_sync_after_code_change(sync_ctx.start_va,
                                                   size);
  }

  if (obj->trampoline != NULL)
  {
    rtems_cache_instruction_sync_after_code_change(obj->trampoline,
                                                   obj->tramps_size);
  }
}

bool
rtems_rtl_obj_load_symbols (rtems_rtl_obj*             obj,
                            int                        fd,
                            rtems_rtl_obj_sect_handler handler,
                            void*                      data)
{
  uint32_t mask = RTEMS_RTL_OBJ_SECT_SYM;
  bool     ok;
  ok = rtems_rtl_obj_section_handler (mask, obj, fd, handler, data);
  if (ok)
    rtems_rtl_symbol_obj_sort (obj);
  return ok;
}

static int
rtems_rtl_obj_sections_linked_to_order (rtems_rtl_obj* obj,
                                        int            section,
                                        uint32_t       visited_mask)
{
  rtems_chain_control* sections = &obj->sections;
  rtems_chain_node*    node = rtems_chain_first (sections);
  /*
   * Find the section being linked-to. If the linked-to link field is 0 we have
   * the end and the section's order is the position we are after.
   */
  while (!rtems_chain_is_tail (sections, node))
  {
    rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;
    if (sect->section == section)
    {
      const uint32_t mask = sect->flags & RTEMS_RTL_OBJ_SECT_TYPES;
      int            order = 0;
      if (sect->link != 0)
      {
        /*
         * Have we already visited this type of section? Avoid nesting for
         * ever.
         */
        if ((sect->flags & visited_mask) != 0)
        {
          rtems_rtl_set_error (errno, "section link loop");
          return -1;
        }
        return rtems_rtl_obj_sections_linked_to_order (obj,
                                                       sect->link,
                                                       visited_mask | mask);
      }
      node = rtems_chain_first (sections);
      while (!rtems_chain_is_tail (sections, node))
      {
        sect = (rtems_rtl_obj_sect*) node;
        if ((sect->flags & mask) == mask)
        {
          if (sect->section == section)
            return order;
          ++order;
        }
        node = rtems_chain_next (node);
      }
    }
    node = rtems_chain_next (node);
  }
  rtems_rtl_set_error (errno, "section link not found");
  return -1;
}

static void
rtems_rtl_obj_sections_link_order (uint32_t mask, rtems_rtl_obj* obj)
{
  rtems_chain_control* sections = &obj->sections;
  rtems_chain_node*    node = rtems_chain_first (sections);
  int                  order = 0;
  while (!rtems_chain_is_tail (sections, node))
  {
    rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;
    if ((sect->flags & mask) == mask)
    {
      /*
       * If the section is linked in order find the linked-to section's order
       * and move the section in the section list to
       */
      if (sect->link == 0)
        sect->load_order = order++;
      else
      {
        sect->load_order =
          rtems_rtl_obj_sections_linked_to_order (obj,
                                                  sect->link,
                                                  mask);
      }
    }
    node = rtems_chain_next (node);
  }
}

static void
rtems_rtl_obj_sections_locate (uint32_t            mask,
                               rtems_rtl_alloc_tag tag,
                               rtems_rtl_obj*      obj,
                               uint8_t*            base)
{
  rtems_chain_control* sections = &obj->sections;
  rtems_chain_node*    node = rtems_chain_first (sections);
  size_t               base_offset = 0;
  int                  order = 0;

  if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD_SECT))
    printf ("rtl: locating section: mask:%08" PRIx32 " base:%p\n", mask, base);

  while (!rtems_chain_is_tail (sections, node))
  {
    rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;

    if ((sect->size != 0) && ((sect->flags & mask) == mask))
    {
      if (sect->load_order == order)
      {
        if ((sect->flags & RTEMS_RTL_OBJ_SECT_ARCH_ALLOC) == 0)
        {
          base_offset = rtems_rtl_obj_align (base_offset, sect->alignment);
          sect->base = base + base_offset;
        }

        if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD_SECT))
          printf ("rtl: locating:%2d: %s -> %p (s:%zi f:%04" PRIx32
                  " a:%" PRIu32 " l:%02d)\n",
                  order, sect->name, sect->base, sect->size,
                  sect->flags, sect->alignment, sect->link);

        if (sect->base)
          base_offset += sect->size;

        ++order;

        node = rtems_chain_first (sections);
        continue;
      }
    }

    node = rtems_chain_next (node);
  }
}

bool
rtems_rtl_obj_alloc_sections (rtems_rtl_obj*             obj,
                              int                        fd,
                              rtems_rtl_obj_sect_handler handler,
                              void*                      data)
{
  size_t text_size;
  size_t const_size;
  size_t eh_size;
  size_t data_size;
  size_t bss_size;

  text_size  = rtems_rtl_obj_text_size (obj) + rtems_rtl_obj_const_alignment (obj);
  const_size = rtems_rtl_obj_const_size (obj) + rtems_rtl_obj_eh_alignment (obj);
  eh_size    = rtems_rtl_obj_eh_size (obj) + rtems_rtl_obj_data_alignment (obj);
  data_size  = rtems_rtl_obj_data_size (obj) + rtems_rtl_obj_bss_alignment (obj);
  bss_size   = rtems_rtl_obj_bss_size (obj);

  /*
   * Set the sizes held in the object data. We need this for a fast reference.
   */
  obj->text_size  = text_size;
  obj->const_size = const_size;
  obj->data_size  = data_size;
  obj->eh_size    = eh_size;
  obj->bss_size   = bss_size;

  /*
   * Perform any specific allocations for sections.
   */
  if (handler != NULL)
  {
    if (!rtems_rtl_obj_section_handler (RTEMS_RTL_OBJ_SECT_TYPES,
                                        obj,
                                        fd,
                                        handler,
                                        data))
    {
      obj->exec_size = 0;
      return false;
    }
  }

  /*
   * Let the allocator manage the actual allocation. The user can use the
   * standard heap or provide a specific allocator with memory protection.
   */
  if (!rtems_rtl_alloc_module_new (&obj->text_base, text_size,
                                   &obj->const_base, const_size,
                                   &obj->eh_base, eh_size,
                                   &obj->data_base, data_size,
                                   &obj->bss_base, bss_size))
  {
    obj->exec_size = 0;
    rtems_rtl_set_error (ENOMEM, "no memory to load obj");
    return false;
  }

  obj->exec_size = text_size + const_size + eh_size + data_size + bss_size;

  if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD_SECT))
  {
    printf ("rtl: load sect: text  - b:%p s:%zi a:%" PRIu32 "\n",
            obj->text_base, text_size, rtems_rtl_obj_text_alignment (obj));
    printf ("rtl: load sect: const - b:%p s:%zi a:%" PRIu32 "\n",
            obj->const_base, const_size, rtems_rtl_obj_const_alignment (obj));
    printf ("rtl: load sect: eh    - b:%p s:%zi a:%" PRIu32 "\n",
            obj->eh_base, eh_size, rtems_rtl_obj_eh_alignment (obj));
    printf ("rtl: load sect: data  - b:%p s:%zi a:%" PRIu32 "\n",
            obj->data_base, data_size, rtems_rtl_obj_data_alignment (obj));
    printf ("rtl: load sect: bss   - b:%p s:%zi a:%" PRIu32 "\n",
            obj->bss_base, bss_size, rtems_rtl_obj_bss_alignment (obj));
  }

  /*
   * Determine the load order.
   */
  rtems_rtl_obj_sections_link_order (RTEMS_RTL_OBJ_SECT_TEXT,  obj);
  rtems_rtl_obj_sections_link_order (RTEMS_RTL_OBJ_SECT_CONST, obj);
  rtems_rtl_obj_sections_link_order (RTEMS_RTL_OBJ_SECT_EH,    obj);
  rtems_rtl_obj_sections_link_order (RTEMS_RTL_OBJ_SECT_DATA,  obj);
  rtems_rtl_obj_sections_link_order (RTEMS_RTL_OBJ_SECT_BSS,   obj);

  /*
   * Locate all text, data and bss sections in seperate operations so each type of
   * section is grouped together.
   */
  rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_TEXT,
                                 rtems_rtl_alloc_text_tag (),
                                 obj, obj->text_base);
  rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_CONST,
                                 rtems_rtl_alloc_const_tag (),
                                 obj, obj->const_base);
  rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_EH,
                                 rtems_rtl_alloc_eh_tag (),
                                 obj, obj->eh_base);
  rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_DATA,
                                 rtems_rtl_alloc_data_tag (),
                                 obj, obj->data_base);
  rtems_rtl_obj_sections_locate (RTEMS_RTL_OBJ_SECT_BSS,
                                 rtems_rtl_alloc_bss_tag (),
                                 obj, obj->bss_base);

  return true;
}

static bool
rtems_rtl_obj_sections_loader (uint32_t                   mask,
                               rtems_rtl_alloc_tag        tag,
                               rtems_rtl_obj*             obj,
                               int                        fd,
                               uint8_t*                   base,
                               rtems_rtl_obj_sect_handler handler,
                               void*                      data)
{
  rtems_chain_control* sections = &obj->sections;
  rtems_chain_node*    node = rtems_chain_first (sections);
  int                  order = 0;

  if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD_SECT))
    printf ("rtl: loading section: mask:%08" PRIx32 " base:%p\n", mask, base);

  rtems_rtl_alloc_wr_enable (tag, base);

  while (!rtems_chain_is_tail (sections, node))
  {
    rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;

    if ((sect->size != 0) && ((sect->flags & mask) == mask))
    {
      if (sect->load_order == order)
      {
        if (rtems_rtl_trace (RTEMS_RTL_TRACE_LOAD_SECT))
          printf ("rtl: loading:%2d: %s -> %p (s:%zi f:%04" PRIx32
                  " a:%" PRIu32 " l:%02d)\n",
                  order, sect->name, sect->base, sect->size,
                  sect->flags, sect->alignment, sect->link);

        if ((sect->flags & RTEMS_RTL_OBJ_SECT_LOAD) == RTEMS_RTL_OBJ_SECT_LOAD)
        {
          if (!handler (obj, fd, sect, data))
          {
            sect->base = 0;
            rtems_rtl_alloc_wr_disable (tag, base);
            return false;
          }
        }
        else if ((sect->flags & RTEMS_RTL_OBJ_SECT_ZERO) == RTEMS_RTL_OBJ_SECT_ZERO)
        {
          memset (sect->base, 0, sect->size);
        }
        else
        {
          /*
           * This section is not to be loaded, clear the base.
           */
          sect->base = 0;
        }

        ++order;

        node = rtems_chain_first (sections);
        continue;
      }
    }

    node = rtems_chain_next (node);
  }

  rtems_rtl_alloc_wr_disable (tag, base);

  return true;
}

bool
rtems_rtl_obj_load_sections (rtems_rtl_obj*             obj,
                             int                        fd,
                             rtems_rtl_obj_sect_handler handler,
                             void*                      data)
{
  /*
   * Load all text, data and bsssections in seperate operations so each type of
   * section is grouped together. Finish by loading any architecure specific
   * sections.
   */
  if (!rtems_rtl_obj_sections_loader (RTEMS_RTL_OBJ_SECT_TEXT,
                                      rtems_rtl_alloc_text_tag (),
                                      obj, fd, obj->text_base, handler, data) ||
      !rtems_rtl_obj_sections_loader (RTEMS_RTL_OBJ_SECT_CONST,
                                      rtems_rtl_alloc_const_tag (),
                                      obj, fd, obj->const_base, handler, data) ||
      !rtems_rtl_obj_sections_loader (RTEMS_RTL_OBJ_SECT_EH,
                                      rtems_rtl_alloc_eh_tag (),
                                      obj, fd, obj->eh_base, handler, data) ||
      !rtems_rtl_obj_sections_loader (RTEMS_RTL_OBJ_SECT_DATA,
                                      rtems_rtl_alloc_data_tag (),
                                      obj, fd, obj->data_base, handler, data) ||
      !rtems_rtl_obj_sections_loader (RTEMS_RTL_OBJ_SECT_BSS,
                                      rtems_rtl_alloc_bss_tag (),
                                      obj, fd, obj->bss_base, handler, data))
  {
    rtems_rtl_alloc_module_del (&obj->text_base, &obj->const_base, &obj->eh_base,
                                &obj->data_base, &obj->bss_base);
    obj->exec_size = 0;
    return false;
  }

  return true;
}

static void
rtems_rtl_obj_run_cdtors (rtems_rtl_obj* obj, uint32_t mask)
{
  rtems_chain_node* node = rtems_chain_first (&obj->sections);
  while (!rtems_chain_is_tail (&obj->sections, node))
  {
    rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;
    if ((sect->flags & mask) == mask)
    {
      rtems_rtl_cdtor* handler;
      size_t           handlers = sect->size / sizeof (rtems_rtl_cdtor);
      int              c;
      for (c = 0, handler = sect->base; c < handlers; ++c)
        if (*handler)
          (*handler) ();
    }
    node = rtems_chain_next (node);
  }
}

static bool
rtems_rtl_obj_cdtors_to_run (rtems_rtl_obj* obj, uint32_t mask)
{
  rtems_chain_node* node = rtems_chain_first (&obj->sections);
  while (!rtems_chain_is_tail (&obj->sections, node))
  {
    rtems_rtl_obj_sect* sect = (rtems_rtl_obj_sect*) node;
    if ((sect->flags & mask) == mask)
      return true;
    node = rtems_chain_next (node);
  }
  return false;
}

bool
rtems_rtl_obj_ctors_to_run (rtems_rtl_obj* obj)
{
  return rtems_rtl_obj_cdtors_to_run (obj, RTEMS_RTL_OBJ_SECT_CTOR);
}

void
rtems_rtl_obj_run_ctors (rtems_rtl_obj* obj)
{
  rtems_rtl_obj_run_cdtors (obj, RTEMS_RTL_OBJ_SECT_CTOR);
}

bool
rtems_rtl_obj_dtors_to_run (rtems_rtl_obj* obj)
{
  return rtems_rtl_obj_cdtors_to_run (obj, RTEMS_RTL_OBJ_SECT_DTOR);
}

void
rtems_rtl_obj_run_dtors (rtems_rtl_obj* obj)
{
  rtems_rtl_obj_run_cdtors (obj, RTEMS_RTL_OBJ_SECT_DTOR);
}

static bool
rtems_rtl_obj_file_load (rtems_rtl_obj* obj, int fd)
{
  int l;

  for (l = 0; l < (sizeof (loaders) / sizeof (rtems_rtl_loader_table)); ++l)
  {
    if (loaders[l].check (obj, fd))
    {
      obj->format = l;
      return loaders[l].load (obj, fd);
    }
  }

  rtems_rtl_set_error (ENOENT, "no format loader found");
  return false;
}

static void
rtems_rtl_obj_set_error (int num, const char* text)
{
  rtems_rtl_set_error (num, text);
}

size_t
rtems_rtl_obj_get_reference (rtems_rtl_obj* obj)
{
  return obj->refs;
}

void
rtems_rtl_obj_inc_reference (rtems_rtl_obj* obj)
{
  ++obj->refs;
}

void
rtems_rtl_obj_dec_reference (rtems_rtl_obj* obj)
{
  if (obj->refs)
    --obj->refs;
}

bool
rtems_rtl_obj_orphaned (rtems_rtl_obj* obj)
{
  return ((obj->flags & RTEMS_RTL_OBJ_LOCKED) == 0 &&
          obj->users == 0 &&
          rtems_rtl_obj_get_reference (obj) == 0);
}

bool
rtems_rtl_obj_load (rtems_rtl_obj* obj)
{
  int fd;

  if (!rtems_rtl_obj_fname_valid (obj))
  {
    rtems_rtl_set_error (ENOMEM, "invalid object file name path");
    return false;
  }

  fd = open (rtems_rtl_obj_fname (obj), O_RDONLY);
  if (fd < 0)
  {
    rtems_rtl_set_error (errno, "opening for object file");
    return false;
  }

  /*
   * Find the object file in the archive if it is an archive that
   * has been opened.
   */
  if (rtems_rtl_obj_aname_valid (obj))
  {
    off_t enames = 0;
    if (!rtems_rtl_obj_archive_find_obj (fd,
                                         obj->fsize,
                                         &obj->oname,
                                         &obj->ooffset,
                                         &obj->fsize,
                                         &enames,
                                         rtems_rtl_obj_set_error))
    {
      close (fd);
      return false;
    }
  }

  /*
   * Call the format specific loader.
   */
  if (!rtems_rtl_obj_file_load (obj, fd))
  {
    close (fd);
    return false;
  }

   /*
    * For GDB
    */
  if (!_rtld_linkmap_add (obj))
  {
    close (fd);
    return false;
  }

  close (fd);

  return true;
}

bool
rtems_rtl_obj_unload (rtems_rtl_obj* obj)
{
  bool ok = false;
  if (obj->format >= 0 && obj->format < RTEMS_RTL_LOADERS)
  {
    _rtld_linkmap_delete(obj);
    ok = loaders[obj->format].unload (obj);
  }
  else
  {
    rtems_rtl_set_error (EINVAL, "invalid object loader format");
  }
  return ok;
}