-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfdstuff.c
1994 lines (1769 loc) · 55.3 KB
/
bfdstuff.c
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
/* Cexp interface to object/symbol file using BFD; runtime loader support */
/* NOTE: read the comment near the end of this file about C++ exception handling */
/* IMPORTANT LICENSING INFORMATION:
*
* linking this code against 'libbfd'/ 'libopcodes'
* may be subject to the GPL conditions.
* (This file itself is distributed under the more
* liberal 'SLAC' license)
*
*/
/* SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
*
* Authorship
* ----------
* This software (CEXP - C-expression interpreter and runtime
* object loader/linker) was created by
*
* Till Straumann <[email protected]>, 2002-2008,
* Stanford Linear Accelerator Center, Stanford University.
*
* Acknowledgement of sponsorship
* ------------------------------
* This software was produced by
* the Stanford Linear Accelerator Center, Stanford University,
* under Contract DE-AC03-76SFO0515 with the Department of Energy.
*
* Government disclaimer of liability
* ----------------------------------
* Neither the United States nor the United States Department of Energy,
* nor any of their employees, makes any warranty, express or implied, or
* assumes any legal liability or responsibility for the accuracy,
* completeness, or usefulness of any data, apparatus, product, or process
* disclosed, or represents that its use would not infringe privately owned
* rights.
*
* Stanford disclaimer of liability
* --------------------------------
* Stanford University makes no representations or warranties, express or
* implied, nor assumes any liability for the use of this software.
*
* Stanford disclaimer of copyright
* --------------------------------
* Stanford University, owner of the copyright, hereby disclaims its
* copyright and all other rights in this software. Hence, anyone may
* freely use it for any purpose without restriction.
*
* Maintenance of notices
* ----------------------
* In the interest of clarity regarding the origin and status of this
* SLAC software, this and all the preceding Stanford University notices
* are to remain affixed to any copy or derivative of this software made
* or distributed by the recipient and are to be affixed to any copy of
* software made or distributed by the recipient that contains a copy or
* derivative of this software.
*
* SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
*/
/*#include <libiberty.h>*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#define _INSIDE_CEXP_
#include "cexp.h"
#include "cexpmodP.h"
#include "cexpsymsP.h"
#include "cexpsegsP.h"
#include "cexpHelp.h"
#include "cexpveneerP.h"
/* Oh well; rtems/score/ppctypes.h defines boolean and bfd
* defines boolean as well :-( Can't you people use names
* less prone to clashes???
* We redefine bfd's boolean here
*/
#define boolean bfdddd_bbboolean
#ifdef USE_PMBFD
#include "pmbfd.h"
#include "pmelf.h"
#define reloc_get_address(abfd, r) pmbfd_reloc_get_address(abfd, r)
#define reloc_get_name(abfd, r) pmbfd_reloc_get_name(abfd, r)
#define bfd_asymbol_set_value(s,v) pmbfd_asymbol_set_value(s,v)
#undef HAVE_ELF_BFD_H
#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
#define PMELF_ATTR_MATCH PMELF_MATCH_ATTRIBUTES_RELAXD
#else
#define PMELF_ATTR_MATCH PMELF_MATCH_ATTRIBUTES_STRICT
#endif
#else
#include <bfd.h>
#include <libiberty.h>
#include <stdint.h>
#ifdef HAVE_ELF_BFD_H
#include "elf-bfd.h"
#endif
#undef boolean
#define reloc_get_address(abfd, r) ((r)->address)
#define reloc_get_name(abfd,r) ((r)->howto->name)
#define bfd_asymbol_set_value(s,v) ((s)->value = (v))
#define bfd_set_output_section(s, os) ((s)->output_section = (os))
#endif
#define NumberOf(arr) (sizeof(arr)/sizeof(arr[0]))
#define DEBUG_COMMON (1<<0)
#define DEBUG_SECT (1<<1)
#define DEBUG_RELOC (1<<2)
#define DEBUG_SYM (1<<3)
#define DEBUG_CDPRI (1<<4)
#define DEBUG_PROGRESS (1<<5)
#define DEBUG (0)
#ifdef DEBUG
int32_t cexpBfdDebug = DEBUG;
#else
#define cexpBfdDebug (0)
#endif
#include "cexp_regex.h"
/* magic symbol names for C++ support; probably gcc specific */
#define CTOR_DTOR_PATTERN "^_+GLOBAL_([_.$]|_sub_)[ID][_.$]"
/* static/global CTOR/DTOR has no init_priority (highest priority is 1) */
#define INIT_PRIO_NONE 1000000
#ifdef OBSOLETE_EH_STUFF /* old suselinux-ppc modified gcc needed that */
#define EH_FRAME_BEGIN_PATTERN "__FRAME_BEGIN__"
#define EH_FRAME_END_PATTERN "__FRAME_END__"
#endif
#define EH_SECTION_NAME ".eh_frame"
#define TEXT_SECTION_NAME ".text"
#define EXIDX_SECTION_NAME ".ARM.exidx"
#define DSO_HANDLE_NAME "__dso_handle"
#define EXIDX_START_NAME "__exidx_start"
#define EXIDX_END_NAME "__exidx_end"
/* this probably only makes sense on ELF */
#if defined(HAVE_ELF_BFD_H) || defined(_PMBFD_)
/* filter dangerous sections; we do ont support shared objects */
#define FIXUP_SECTION_NAME ".fixup"
#define GOT_SECTION_NAME ".got"
#define GOT2_SECTION_NAME ".got2"
#endif
#define ISELF(abfd) (bfd_get_flavour((abfd)) == bfd_target_elf_flavour)
/* using one static variable for the pattern matcher is
* OK since the entire cexpLoadFile() routine is called
* with the WRITE_LOCK held, so mutex is guaranteed for
* the ctorCtorRegexp.
*/
static cexp_regex *ctorDtorRegexp=0;
/*
* Allow to override the object-attribute matcher
*/
int _cexpForceIgnoreObjAttrMismatches = 0;
#ifdef HAVE_BFD_DISASSEMBLER
/* as a side effect, this code gives access to a disassembler which
* itself is implemented by libopcodes
*/
extern void cexpDisassemblerInstall(bfd *abfd);
#else
#define cexpDisassemblerInstall(a) do {} while(0)
#endif
/* this is PowerPC specific; note that some architectures
* (mpc860) have smaller cache lines. Setting this to a smaller
* value than the actual cache line size is safe and performance
* is not an issue here
*/
#if defined(HAVE_RTEMS_CACHE_H)
#include <rtems/rtems/cache.h>
#elif defined(__PPC__) || defined(__PPC) || defined(_ARCH_PPC) || defined(PPC)
# define CACHE_LINE_SIZE 16
# define FLUSHINVAL_LINE(addr) \
__asm__ __volatile__( \
"dcbf 0, %0\n" /* flush out one data cache line */ \
"icbi 0, %0\n" /* invalidate cached instructions for this line */ \
::"r"(addr))
/* enforce flush completion and discard preloaded instructions */
# define FLUSHFINISH() __asm__ __volatile__("sync; isync")
#elif defined(__mc68000__) || defined(__mc68000) || defined(mc68000) || defined(__m68k__)
# define CACHE_LINE_SIZE 16
# if defined(__rtems__)
extern void _CPU_cache_flush_1_data_line(void *addr);
extern void _CPU_cache_invalidate_1_instruction_line(void *addr);
# define FLUSHINVAL_LINE(addr) \
do { \
_CPU_cache_flush_1_data_line(addr); \
_CPU_cache_invalidate_1_instruction_line(addr); \
} while (0)
# define FLUSHFINISH() do {} while (0)
# else
/* m68k cache flush instructions are only available in supervisor mode;
* PLUS they operate on physical addresses :-(
*/
# error("don't know how to flush/invalidate cache on this system")
# endif /* defined __rtems__ */
#endif
/* data we need for linking */
typedef struct LinkDataRec_ {
bfd *abfd;
CexpSegment segs;
int nsegs;
asymbol **st;
asymbol ***new_commons;
long num_new_commons;
char *dummy_section_name;
CexpSymTbl cst;
int errors;
int num_alloc_sections;
int num_section_names; /* differs because of linkonce sections */
BitmapWord *depend;
int nCtors;
int nDtors;
#ifdef OBSOLETE_EH_STUFF
asymbol *eh_frame_b_sym;
asymbol *eh_frame_e_sym;
#else
asection *eh_section;
#endif
unsigned long text_vma;
asection *text;
asection *eh;
void *iniCallback;
void *finiCallback;
void *exidx;
unsigned long nExidx;
CexpModule module;
} LinkDataRec, *LinkData;
/* forward declarations */
/* Hmm - c++ exception handling is _highly_ compiler and version and
* target dependent :-(.
*
* It is very unlikely that exception handling works with anything other
* than gcc.
*
* Gcc (2.95.3) seems to implement (at least) two flavours of exception
* handling: longjump and exception frame tables. The latter requires
* the dynamic loader to load and register the exception frame table, the
* former is transparent.
* Unfortunately, the __register_frame()/__deregister_frame() routines
* are absent on targets which do not support this kind of exception handling.
*
* Therefore, we use the CEXP symbol table itself to retrieve the actual
* routines __register_frame() and __deregister_frame(). Our private pointers
* are initialized when we read the system symbol table.
*/
static void (*my__register_frame)(void*)=0;
static void (*my__deregister_frame)(void*)=0;
/* Support for __cxa_atexit() */
static void (*my__cxa_finalize)(/*dso_handle*/ void*)=0;
static CexpSym my__dso_handle = 0;
static asymbol *
asymFromCexpSym(bfd *abfd, CexpSym csym, BitmapWord *depend, CexpModule mod);
static void
bfdCleanupCallback(CexpModule);
static void
bfdstuff_complete_init(CexpSymTbl);
/* how to decide where a particular section should go */
static CexpSegment
segOf(LinkData ld, asection *sect)
{
return cexpSegsMatch(ld->segs, ld->abfd, sect);
}
/* determine the alignment power of a common symbol
* (currently only works for ELF)
*/
#if defined(HAVE_ELF_BFD_H)
static inline unsigned
elf_get_align(bfd *abfd, asymbol *sp)
{
elf_symbol_type *esp;
unsigned rval;
if ( (esp = elf_symbol_from(abfd, sp)) && (rval = esp->internal_elf_sym.st_value) ) {
return rval;
}
return 1;
}
static inline int
elf_get_size(bfd *abfd, asymbol *asym)
{
elf_symbol_type *elfsp= ISELF(abfd) ? elf_symbol_from(abfd, asym) : 0;
return elfsp ? elfsp->internal_elf_sym.st_size : 0 ;
}
#elif defined(_PMBFD_)
/* exported by pmbfd.h */
#else
#define elf_get_align(abfd,sp) 1
#define elf_get_size(abfd,sp) 0
#endif
#if defined(HAVE_ELF_BFD_H) || defined(_PMBFD_)
/* we sort in descending order; hence the routine must return b-a */
static int
align_size_compare(const void *a, const void *b)
{
asymbol *spa=**(asymbol***)a;
asymbol *spb=**(asymbol***)b;
unsigned algn_a, algn_b;
algn_a = elf_get_align(bfd_asymbol_bfd(spa), spa);
algn_b = elf_get_align(bfd_asymbol_bfd(spb), spb);
return algn_b - algn_a;
}
#endif
static inline int
elf_get_align_pwr(bfd *abfd, asymbol *sp)
{
register unsigned long rval=0,tst;
for (tst=1; tst < elf_get_align(abfd, sp); rval++)
tst<<=1;
return rval;
}
/* determine if a given symbol is a constructor or destructor
* by matching to a magic pattern.
*
* RETURNS: 1 if the symbol is a constructor
* -1 if it is a destructor
* 0 otherwise
*/
static inline int
isCtorDtor(asymbol *asym, int quiet, int *pprio)
{
/* From bfd/linker.c: */
/* "A constructor or destructor name starts like this:
_+GLOBAL_[_.$][ID][_.$] where the first [_.$] and
the second are the same character (we accept any
character there, in case a new object file format
comes along with even worse naming restrictions)." */
if (cexp_regexec(ctorDtorRegexp,bfd_asymbol_name(asym))) {
register const char *tail = ctorDtorRegexp->endp[0];
/* read the priority */
if (pprio) {
if (isdigit((int)*tail))
*pprio = atoi(tail);
else
*pprio = INIT_PRIO_NONE;
}
switch (*(tail-2)) {
case 'I': return 1; /* constructor */
case 'D': return -1; /* destructor */
default:
if (!quiet) {
fprintf(stderr,"WARNING: unclassified BSF_CONSTRUCTOR symbol;");
fprintf(stderr," ignoring '%s'\n",bfd_asymbol_name(asym));
}
break;
}
}
return 0;
}
/* this routine defines which symbols are retained by Cexp in its
* internal symbol table
*/
static const char *
filter(void *ext_sym, void *closure)
{
LinkData ld =closure;
asymbol *asym=*(asymbol**)ext_sym;
ld = ld;
/*
* silence warning about unused var (bfd_get_section_name macro
* does currently not use the first argument).
*/
if ( !(BSF_KEEP & asym->flags) )
return 0;
return BSF_SECTION_SYM & asym->flags ?
bfd_get_section_name(ld->abfd,bfd_get_section(asym)) :
bfd_asymbol_name(asym);
}
/* this routine is responsible for converting external (BFD) symbols
* to their internal representation (CexpSym). Only symbols who have
* passed the filter() above will be seen by assign().
*/
static void
assign(void *ext_sym, CexpSym cesp, void *closure)
{
LinkData ld=(LinkData)closure;
asymbol *asym=*(asymbol**)ext_sym;
int s;
CexpType t=TVoid;
if ( cexpBfdDebug & DEBUG_SYM ) {
printf("assigning symbol: %s\n",bfd_asymbol_name(asym));
}
s = elf_get_size(ld->abfd, asym);
if (BSF_FUNCTION & asym->flags) {
t=TFuncP;
if (0==s)
s=sizeof(void(*)());
} else if (BSF_SECTION_SYM & asym->flags) {
/* section name: leave it void* */
} else {
/* must be BSF_OBJECT */
if (asym->flags & BSF_OLD_COMMON) {
/* value holds the size */
s = bfd_asymbol_value(asym);
}
t = cexpTypeGuessFromSize(s);
}
/* last attempt: if there is no size info and no flag set,
* at least look at the section; functions are in the text
* section...
*/
if (TVoid == t &&
! (asym->flags & (BSF_FUNCTION | BSF_OBJECT | BSF_SECTION_SYM)) &&
0 == s) {
if (ld->text && ld->text == bfd_get_section(asym))
t=TFuncP;
}
cesp->size = s;
cesp->value.type = t;
/* We can only set the real value after relocation.
* Therefore, store a pointer to the external symbol,
* so we have a handle even after the internal symbols
* are sorted. Note that the aindex[] built by cexpCreateSymTbl()
* will be incorrect and has to be re-sorted further
* down the line.
*/
cesp->value.ptv = ext_sym;
if (asym->flags & BSF_GLOBAL)
cesp->flags |= CEXP_SYMFLG_GLBL;
if (asym->flags & BSF_WEAK)
cesp->flags |= CEXP_SYMFLG_WEAK;
if (asym->flags & BSF_SECTION_SYM)
cesp->flags |= CEXP_SYMFLG_SECT;
}
/* call this after relocation to assign the internal
* symbol representation their values
*/
int
cexpSymTabSetValues(LinkDataRec *ldr)
{
CexpSymTbl cst = ldr->cst;
CexpSym cesp;
for (cesp=cst->syms; cesp->name; cesp++) {
asymbol *sp=*(asymbol**)cesp->value.ptv;
cesp->value.ptv=(CexpVal)bfd_asymbol_value(sp);
}
cexpSymTblFixup(cst, cexpSegsGet( ldr->segs, CEXP_SEG_VENR ) );
/* build the address index */
return cexpIndexSymTbl( cst );
}
/* All 's_xxx()' routines defined here are for use with
* bfd_map_over_sections()
*/
/* compute the size requirements for a section */
static void
s_count(bfd *abfd, asection *sect, PTR arg)
{
LinkData ld = (LinkData)arg;
CexpSegment seg = segOf(ld, sect);
flagword flags = bfd_get_section_flags(abfd,sect);
const char *secn = bfd_get_section_name(abfd,sect);
if ( cexpBfdDebug & DEBUG_SECT ) {
printf("Section %s, flags 0x%08x\n", secn, flags);
printf("size: %lu, alignment %i\n",
(unsigned long)bfd_section_size(abfd,sect),
(1<<bfd_get_section_alignment(abfd,sect)));
}
if (SEC_ALLOC & flags) {
seg->size+=(1<<bfd_get_section_alignment(abfd,sect))-1;
seg->size+=bfd_section_size(abfd,sect);
/* exception handler frame tables have to be 0 terminated and
* we must reserve a little extra space.
*/
if ( ! (SEC_LINK_ONCE & flags) ) {
/* speed up the name comparison by skipping the numerous
* linkonce sections - we assume that none of the ones
* we check for below here are LINKONCE
*/
if ( !strcmp(secn, EH_SECTION_NAME) ) {
#ifdef OBSOLETE_EH_STUFF
asymbol *asym;
/* create a symbol to tag the terminating 0 */
asym=ld->eh_frame_e_sym=bfd_make_empty_symbol(abfd);
bfd_asymbol_name(asym) = EH_FRAME_END_PATTERN;
asym->value=(symvalue)bfd_section_size(abfd,sect);
asym->section=sect;
#else
ld->eh_section = sect;
#endif
/* allocate space for a terminating 0 */
seg->size+=sizeof(long);
}
#if defined(HAVE_ELF_BFD_H) || defined(_PMBFD_)
/* filter dangerous sections; we do not support shared objects
* do this in s_count and not earlier because we allow a got
* in the main system to support linux/solaris & friends...
*/
else if (bfd_section_size(abfd,sect) > 0) {
if ( !strcmp(FIXUP_SECTION_NAME, secn) ||
!strcmp(GOT_SECTION_NAME, secn) ||
!strcmp(GOT2_SECTION_NAME, secn) ) {
if ( !ld->errors++ ) {
fprintf(stderr,
"Uh oh - a `%s` section should not be present"
" - did you compile with -fPIC against my advice?\n",
secn);
}
}
}
#endif
}
}
}
/* VMA of -1 means 'invalid' */
static int
check_get_section_vma(bfd *abfd, asection *sect, unsigned long *p_vma)
{
return (unsigned long)-1 == (*p_vma = bfd_get_section_vma(abfd, sect)) ? 1 : 0;
}
/* compute the section's vma for loading */
static void
s_setvma(bfd *abfd, asection *sect, PTR arg)
{
CexpSegment seg=segOf((LinkData)arg, sect);
LinkData ld=(LinkData)arg;
if (SEC_ALLOC & bfd_get_section_flags(abfd, sect)) {
if ( 0 == bfd_section_size(abfd, sect) )
return;
if ( (unsigned long)-1 == seg->vmacalc ) {
/* we get here if we try to use a segment that
* is not initialized or has size zero.
*/
fprintf(stderr,"Internal Error: trying to use segment of size 0\n");
ld->errors++;
return;
}
seg->vmacalc=align_power(seg->vmacalc, bfd_get_section_alignment(abfd,sect));
if ( cexpBfdDebug & DEBUG_SECT ) {
printf("%s allocated at 0x%08lx\n",
bfd_get_section_name(abfd,sect),
(unsigned long)seg->vmacalc);
}
bfd_set_section_vma(abfd,sect,seg->vmacalc);
seg->vmacalc+=bfd_section_size(abfd,sect);
if
#ifdef OBSOLETE_EH_STUFF
( ld->eh_frame_e_sym && bfd_get_section(ld->eh_frame_e_sym) == sect )
#else
( ld->eh_section && ld->eh_section == sect )
#endif
{
/* allocate space for a terminating 0 */
seg->vmacalc+=sizeof(long);
}
#if defined bfd_set_output_section
/* Only needed with real BFD */
bfd_set_output_section(sect, sect);
#endif
if (ld->text && sect == ld->text) {
if ( check_get_section_vma(abfd,sect, &ld->text_vma) )
ld->text_vma = 0;
}
if ( 0 == strcmp(EXIDX_SECTION_NAME, bfd_get_section_name(abfd,sect)) ) {
ld->exidx = (void*)bfd_get_section_vma(abfd, sect);
ld->nExidx = bfd_get_section_size(sect)/8;
}
}
}
/* just count the number of section names we have to remember
* (debugger support)
*/
static void
s_nsects(bfd *abfd, asection *sect, PTR arg)
{
LinkData ld=(LinkData)arg;
if ( SEC_ALLOC & bfd_get_section_flags(ld->abfd, sect) ) {
if ( 0 == bfd_section_size(ld->abfd, sect) ) {
/* Effectively remove zero-sized sections */
bfd_set_section_flags( ld->abfd, sect, bfd_get_section_flags( ld->abfd, sect ) & ~SEC_ALLOC);
} else {
ld->num_section_names++;
}
}
}
/* find basic sections and the number of sections which are
* actually allocated and resolve gnu.linkonce.xxx sections
*/
static void
s_basic(bfd *abfd, asection *sect, PTR arg)
{
LinkData ld=(LinkData)arg;
flagword flags=bfd_get_section_flags(ld->abfd, sect);
if (!strcmp(TEXT_SECTION_NAME,bfd_get_section_name(abfd,sect))) {
ld->text = sect;
}
if ( SEC_ALLOC & flags ) {
const char *secn=bfd_get_section_name(ld->abfd,sect);
/* temporarily store a pointer to the name */
ld->module->section_syms[ld->num_section_names++] = (CexpSym)secn;
}
}
static void
s_eliminate_linkonce(bfd *abfd, asection *sect, PTR arg)
{
LinkData ld=(LinkData)arg;
flagword flags=bfd_get_section_flags(ld->abfd, sect);
if ( SEC_LINK_ONCE & flags ) {
const char *secn=bfd_get_section_name(ld->abfd,sect);
if (cexpSymLookup(secn, 0)) {
/* a linkonce section with this name had been loaded already;
* discard this one...
*/
if ( (SEC_LINK_DUPLICATES & flags) )
fprintf(stderr,"Warning: section '%s' exists; discarding...\n", secn);
/* discard this section (older gcc creating .gnu.linkonce.xxx sections) */
if ( cexpBfdDebug & DEBUG_SECT ) {
if ( SEC_ALLOC & flags ) {
printf("Removing linkonce section '%s'\n", secn);
}
}
bfd_set_section_flags( abfd, sect, bfd_get_section_flags( abfd, sect ) & ~SEC_ALLOC);
if ( (SEC_GROUP & flags) ) {
/* more recent gcc using ELF section groups:
* remove the entire group; this relies on the fact
* that group sections must occur first, i.e., before
* the group contents
*/
#if 0 /* bfd_discard_group() unfortunately not implemented; generic version
does nothing */
bfd_discard_group(abfd, sect);
#else
#if defined(HAVE_ELF_BFD_H) || defined(_PMBFD_)
if ( ISELF(ld->abfd) ) {
asection *s,*frst;
/* group seems to be a circular list pointed to
* by the group section
*/
frst = s = elf_next_in_group(sect);
while ( s ) {
if ( cexpBfdDebug & DEBUG_SECT ) {
printf("Removing linkonce group member '%s'\n",bfd_get_section_name(ld->abfd, s));
}
bfd_set_section_flags( ld->abfd, s, bfd_get_section_flags( ld->abfd, s ) & ~SEC_ALLOC );
if ( (s = elf_next_in_group(s)) == frst )
break;
}
}
#endif
#endif
}
}
}
if ( SEC_ALLOC & bfd_get_section_flags(abfd, sect) )
ld->num_alloc_sections++;
}
/* Extracted from bfd.h; unfortunately conversion of relocation error
* codes to message strings is not available in libbfd (unless I missed it)
*/
static char *reloc_err_msg[]={
"ok (no error)",
"The relocation was performed, but there was an overflow",
"The address to relocate was not within the section supplied",
"'continue' (Used by special functions)",
"Unsupported relocation size/type requested",
"bfd_reloc_other",
"The symbol to relocate against was undefined",
"The relocation was performed, but may not be ok/dangerous"
};
/* read the section contents and process the relocations.
*/
static void
s_reloc(bfd *abfd, asection *sect, PTR arg)
{
LinkData ld=(LinkData)arg;
int i;
long err;
unsigned long vma;
if ( ! (SEC_ALLOC & bfd_get_section_flags(abfd, sect) ) )
return;
if ( check_get_section_vma(abfd, sect, &vma) ) {
fprintf(stderr,"Internal Error: trying to load relocations into non-existing memory segment\n");
ld->errors++;
return;
}
/* read section contents to its memory segment
* NOTE: this automatically clears section with
* ALLOC set but with no CONTENTS (such as
* bss)
*/
if (!bfd_get_section_contents(
abfd,
sect,
(PTR)vma,
0,
bfd_section_size(abfd,sect))) {
bfd_perror("reading section contents");
ld->errors++;
return;
}
/* if there are relocations, resolve them */
if ( (SEC_RELOC & bfd_get_section_flags(abfd, sect)) ) {
asection *symsect;
#ifndef _PMBFD_
arelent **cr=0;
arelent *r;
#else
pmbfd_areltab *cr=0;
pmbfd_arelent *r;
pmbfd_relent_t rtype;
#endif
long sz;
sz=bfd_get_reloc_upper_bound(abfd,sect);
if (sz<=0) {
fprintf(stderr,"No relocs for section %s???\n",
bfd_get_section_name(abfd,sect));
ld->errors++;
return;
}
/* slurp the relocation records; build a list */
cr=xmalloc(sz);
/*
* API of bfd_canonicalize_reloc() differs from
* pmbfd_canonicalize_reloc(). We want to emphasize
* that, hence we don't simply redefine bfd_canonicalize_reloc()
*/
#ifndef _PMBFD_
sz=bfd_canonicalize_reloc(abfd,sect,cr,ld->st);
#else
sz=pmbfd_canonicalize_reloc(abfd,sect,cr,ld->st);
#endif
if (sz<=0) {
fprintf(stderr,"ERROR: unable to canonicalize relocs\n");
free(cr);
ld->errors++;
return;
}
#ifdef _PMBFD_
rtype=pmbfd_get_relent_type(abfd, cr);
if ( Relent_UNKNOWN == rtype ) {
fprintf(stderr,"ERROR: section with unknown relocation entry type\n");
free(cr);
ld->errors++;
return;
}
#endif
for (i=0, r=0; i<sz; i++) {
asymbol **ppsym;
CexpSym ts = 0;
CexpModule mod = 0;
asymbol *sp = 0;
#ifndef _PMBFD_
r=cr[i];
ppsym = r->sym_ptr_ptr;
#else
r = pmbfd_reloc_next(abfd, cr, r);
{
long idx = pmbfd_reloc_get_sym_idx(abfd, r);
if ( idx < 0 ) {
union {
unsigned long l;
char c[sizeof(unsigned long)];
} *p = (void*)r;
fprintf(stderr,"Bad symbol index in reloc detected (section %s)\n",
bfd_get_section_name(abfd,sect));
fprintf(stderr,"[%u] 0x%08lx: 0x%08lx 0x%08lx\n",
i, p->l, (p+1)->l, (p+2)->l);
ld->errors++;
continue;
}
ppsym = ld->st + idx;
}
#endif
symsect=bfd_get_section(*ppsym);
/* must look up even if not undefined in order to locate veneer info */
ts=cexpSymLookup(bfd_asymbol_name((sp=*ppsym)),&mod);
if ( bfd_is_und_section(symsect) ) {
/* reloc references an undefined symbol which we have to look-up */
if (ts && (ts->flags & CEXP_SYMFLG_GLBL) ) {
if (my__dso_handle == ts) {
/* they are looking for __dso_handle; give them
* our module handle.
*
* We assume the system module has its own
* __dso_handle symbol and hence we can simply
* compare the symbol handles.
*
* The pathological case of a module asking for
* __dso_handle and a g++ version which supports
* __cxa_atexit/__cxa_finalize() but does NOT
* define its own __dso_handle will fail.
* (assert() when the symbols are looked up)
*
* We will get an undefined symbol reference
* if the module asks for __dso_handle but libgcc has
* no __cxa_finalize() and no __dso_handle.
*
* We will get this error if the module asks for
* __dso_handle but libc has no __cxa_finalize
*/
if ( !my__cxa_finalize ) {
fprintf(stderr,
"Error: found '"DSO_HANDLE_NAME"' but not '__cxa_finalize'\n");
fprintf(stderr,
" No '__cxa_atexit' support!\n");
ld->errors++;
continue;
}
sp = bfd_make_empty_symbol(abfd);
/* copy pointer to name */
bfd_asymbol_name(sp) = bfd_asymbol_name(ts);
bfd_asymbol_set_value(sp, (symvalue)ld->module);
bfd_set_section(sp, bfd_abs_section_ptr);
sp->flags = BSF_LOCAL;
} else {
/* Resolved reference; replace the symbol pointer
* in this slot with a new asymbol holding the
* resolved value.
*/
sp=asymFromCexpSym(abfd,ts,ld->depend,mod);
}
*ppsym = sp;
symsect = bfd_get_section(*ppsym);
} else {
fprintf(stderr,"Unresolved symbol: %s\n",bfd_asymbol_name(sp));
ld->errors++;
continue;
}
}
/* Ignore relocs that reference dropped linkonce sections */
/* FIXME: We really should have a dedicated flag (and not overload SEC_ALLOC)
* to indicate that we deal with a dropped linkonce. Unfortunately,
* in BFD, there is no flag available to the user...
*/
else if ( !(SEC_ALLOC & bfd_get_section_flags(abfd, symsect) ) && ! bfd_is_abs_section(symsect) && !bfd_is_com_section(symsect) ) {
/* FIXME: should we add a paranoia check that the 'addend' is zero? gcc only skips
* NULL relocs in .eh_frame if the relocation value is zero.
*/
/* reloc referencing a symbol in a dropped linkonce should not happen unless from .eh_frame;
* warn about it:
*/
if ( sect != ld->eh_section || (cexpBfdDebug & DEBUG_RELOC) )
{
fprintf(stderr, "INFO:\n");
fprintf(stderr, "Ignoring/skipping reloc [0x%08lx = %s@%s]\n",
(unsigned long)bfd_asymbol_value(*ppsym),
bfd_asymbol_name(*ppsym),
bfd_get_section_name(abfd, symsect));
fprintf(stderr, " [IN DROPPED LINKONCE] => 0x%08lx@%s (sym_ptr_ptr = 0x%08lx)\n",
(unsigned long)reloc_get_address(abfd, r),
bfd_get_section_name(abfd, sect),
(unsigned long)ppsym
);
}
continue;
}
if ( cexpBfdDebug & DEBUG_RELOC ) {
printf("relocating [0x%08lx = %s@%s]\n",
(unsigned long)bfd_asymbol_value(*ppsym),
bfd_asymbol_name(*ppsym),
bfd_get_section_name(abfd, symsect)
);
printf(" => 0x%08lx@%s (sym_ptr_ptr = 0x%08lx)\n",
(unsigned long)reloc_get_address(abfd, r),
bfd_get_section_name(abfd, sect),
(unsigned long)ppsym
);
}
#ifndef _PMBFD_
err=bfd_perform_relocation(
abfd,
r,
(PTR)vma,
sect,
0 /* output bfd */,
0
);
#else
err=pmbfd_perform_relocation(
abfd,
rtype,
r,
*ppsym,
sect,
/* Can't resolve veneers in the to-be-loaded module ATM */
ts && (ts->flags & CEXP_SYMFLG_VENR) ? &((CexpSymXtraVeneerInfo)ts->xtra.info)->veneer : 0
);
#endif
if ( err ) {
fprintf(stderr,
"Relocation of type '%s'@0x%08lx[%s] ->'%s' failed: %s (check compiler flags!)\n",
reloc_get_name(abfd, r),
reloc_get_address(abfd, r),
bfd_get_section_name(abfd,sect),
bfd_asymbol_name(*ppsym),
err >= NumberOf(reloc_err_msg) ? "unknown error" : reloc_err_msg[err]);
ld->errors++;
}
}
free(cr);
}
}
typedef struct CtorSymRec_ {
asymbol *sym; /* the symbol */
int pri; /* priority we have extracted already */
} CtorSymRec, *CtorSym;
static int
ctor_cmp(const void *a, const void *b)
{
register int diff;
register CtorSym sa = (CtorSym)a;
register CtorSym sb = (CtorSym)b;
/* priorities are in inverse order */
if ( (diff = sa->pri - sb->pri ) )
return diff;
/* same priority -- enforce keeping the current
* ordering
*/
return sa-sb;