forked from Leffmann/vlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
targets.c
2064 lines (1753 loc) · 56.2 KB
/
targets.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
/* $VER: vlink targets.c V0.16a (17.06.17)
*
* This file is part of vlink, a portable linker for multiple
* object formats.
* Copyright (c) 1997-2017 Frank Wille
*/
#define TARGETS_C
#include "vlink.h"
struct FFFuncs *fff[] = {
#ifdef ADOS
&fff_amigahunk,
#endif
#ifdef EHF
&fff_ehf,
#endif
#ifdef ATARI_TOS
&fff_ataritos,
#endif
#ifdef ELF32_PPC_BE
&fff_elf32ppcbe,
#endif
#ifdef ELF32_AMIGA
&fff_elf32powerup,
&fff_elf32morphos,
&fff_elf32amigaos,
#endif
#ifdef ELF32_M68K
&fff_elf32m68k,
#endif
#ifdef ELF32_386
&fff_elf32i386,
#endif
#ifdef ELF32_AROS
&fff_elf32aros,
#endif
#ifdef ELF32_ARM_LE
&fff_elf32armle,
#endif
#ifdef ELF32_JAG
&fff_elf32jag,
#endif
#ifdef ELF64_X86
&fff_elf64x86,
#endif
#ifdef AOUT_NULL
&fff_aoutnull,
#endif
#ifdef AOUT_SUN010
&fff_aoutsun010,
#endif
#ifdef AOUT_SUN020
&fff_aoutsun020,
#endif
#ifdef AOUT_BSDM68K
&fff_aoutbsd68k,
#endif
#ifdef AOUT_BSDM68K4K
&fff_aoutbsd68k4k,
#endif
#ifdef AOUT_MINT
&fff_aoutmint,
#endif
#ifdef AOUT_BSDI386
&fff_aoutbsdi386,
#endif
#ifdef AOUT_PC386
&fff_aoutpc386,
#endif
#ifdef AOUT_JAGUAR
&fff_aoutjaguar,
#endif
#ifdef VOBJ
&fff_vobj_le,
&fff_vobj_be,
#endif
/* the raw binary file formats *must* be the last ones */
#ifdef RAWBIN1
&fff_rawbin1,
#endif
#ifdef RAWBIN2
&fff_rawbin2,
#endif
#ifdef AMSDOS
&fff_amsdos,
#endif
#ifdef CBMPRG
&fff_cbmprg,
#endif
#ifdef JAGSRV
&fff_jagsrv,
#endif
#ifdef SREC19
&fff_srec19,
#endif
#ifdef SREC28
&fff_srec28,
#endif
#ifdef SREC37
&fff_srec37,
#endif
#ifdef IHEX
&fff_ihex,
#endif
#ifdef SHEX1
&fff_shex1,
#endif
#ifdef RAWSEG
&fff_rawseg,
#endif
NULL
};
const char *sym_type[] = { "undef","abs","reloc","common","indirect" };
const char *sym_info[] = { ""," object"," function"," section"," file" };
const char *sym_bind[] = { "","local ","global ","weak " };
const char *reloc_name[] = {
"R_NONE",
"R_ABS",
"R_PC",
"R_GOT",
"R_GOTPC",
"R_GOTOFF",
"R_GLOBDAT",
"R_PLT",
"R_PLTPC",
"R_PLTOFF",
"R_SD",
"R_UABS",
"R_LOCALPC",
"R_LOADREL",
"R_COPY",
"R_JMPSLOT",
"R_SECOFF",
"","","","","","","","","","","","","","",""
"R_SD2",
"R_SD21",
"R_SECOFF",
"R_MOSDREL",
"R_AOSBREL",
NULL
};
/* common section names */
const char text_name[] = ".text";
const char data_name[] = ".data";
const char bss_name[] = ".bss";
const char sdata_name[] = ".sdata";
const char sbss_name[] = ".sbss";
const char sdata2_name[] = ".sdata2";
const char sbss2_name[] = ".sbss2";
const char ctors_name[] = ".ctors";
const char dtors_name[] = ".dtors";
const char got_name[] = ".got";
const char plt_name[] = ".plt";
const char sdabase_name[] = "_SDA_BASE_";
const char sda2base_name[] = "_SDA2_BASE_";
const char gotbase_name[] = "__GLOBAL_OFFSET_TABLE_";
const char pltbase_name[] = "__PROCEDURE_LINKAGE_TABLE_";
const char dynamic_name[] = "__DYNAMIC";
const char r13init_name[] = "__r13_init";
const char noname[] = "";
struct Symbol *findsymbol(struct GlobalVars *gv,struct Section *sec,
const char *name)
/* Return pointer to Symbol, otherwise return NULL.
Make sure to prefer symbols from sec's ObjectUnit. */
{
if (fff[gv->dest_format]->fndsymbol) {
return fff[gv->dest_format]->fndsymbol(gv,sec,name);
}
else {
struct Symbol *sym = gv->symbols[elf_hash(name)%SYMHTABSIZE];
struct Symbol *found = NULL;
while (sym) {
if (!strcmp(name,sym->name)) {
if (found && sec) {
/* ignore, when not from the refering ObjectUnit */
if (sym->relsect->obj == sec->obj)
found = sym;
}
else
found = sym;
}
sym = sym->glob_chain;
}
if (found!=NULL && found->type==SYM_INDIR)
return findsymbol(gv,sec,found->indir_name);
return found;
}
return NULL;
}
bool check_protection(struct GlobalVars *gv,const char *name)
/* checks if symbol name is protected against stripping */
{
struct SymNames *sn = gv->prot_syms;
while (sn) {
if (!strcmp(sn->name,name))
return TRUE;
sn = sn->next;
}
return FALSE;
}
static void unlink_objsymbol(struct Symbol *delsym)
/* unlink a symbol from an object unit's hash chain */
{
const char *fn = "unlink_objsymbol():";
struct ObjectUnit *ou = delsym->relsect ? delsym->relsect->obj : NULL;
if (ou) {
struct Symbol **chain = &ou->objsyms[elf_hash(delsym->name)%OBJSYMHTABSIZE];
struct Symbol *sym;
while (sym = *chain) {
if (sym == delsym)
break;
chain = &sym->obj_chain;
}
if (sym) {
/* unlink the symbol node from the chain */
*chain = delsym->obj_chain;
delsym->obj_chain = NULL;
}
else
ierror("%s %s could not be found in any object",fn,delsym->name);
}
else
ierror("%s %s has no object or section",fn,delsym->name);
}
static void remove_obj_symbol(struct Symbol *delsym)
/* delete a symbol from an object unit */
{
unlink_objsymbol(delsym);
free(delsym);
}
bool addglobsym(struct GlobalVars *gv,struct Symbol *newsym)
/* insert symbol into global symbol hash table */
{
struct Symbol **chain = &gv->symbols[elf_hash(newsym->name)%SYMHTABSIZE];
struct Symbol *sym;
struct ObjectUnit *newou = newsym->relsect ? newsym->relsect->obj : NULL;
while (sym = *chain) {
if (!strcmp(newsym->name,sym->name)) {
if (newsym->type==SYM_ABS && sym->type==SYM_ABS &&
newsym->value == sym->value)
return FALSE; /* absolute symbols with same value are ignored */
if (newsym->relsect == NULL || sym->relsect == NULL) {
/* redefined linker script symbol */
if (newou!=NULL && newou->lnkfile->type<ID_LIBBASE) {
static const char *objname = "ldscript";
error(19,newou ? newou->lnkfile->pathname : objname,
newsym->name, newou ? getobjname(newou) : objname,
sym->relsect ? getobjname(sym->relsect->obj) : objname);
}
/* redefinitions in libraries are silently ignored */
return FALSE;
}
if (sym->bind == SYMB_GLOBAL) {
/* symbol already defined with global binding */
if (newsym->bind == SYMB_GLOBAL) {
if (newou->lnkfile->type < ID_LIBBASE) {
if (newsym->type==SYM_COMMON && sym->type==SYM_COMMON) {
if ((newsym->size>sym->size && newsym->value>=sym->value) ||
(newsym->size>=sym->size && newsym->value>sym->value)) {
/* replace by common symbol with bigger size or alignment */
newsym->glob_chain = sym->glob_chain;
remove_obj_symbol(sym); /* delete old symbol in object unit */
break;
}
}
else {
/* Global symbol "x" is already defined in... */
error(19,newou->lnkfile->pathname,newsym->name,
getobjname(newou),getobjname(sym->relsect->obj));
}
return FALSE; /* ignore this symbol */
}
/* else: add global library symbol with same name to the chain -
some targets may want to choose between them! */
}
else
return FALSE; /* don't replace global by nonglobal symbols */
}
else {
if (newsym->bind == SYMB_WEAK)
return FALSE; /* don't replace weak by weak */
/* replace weak symbol by a global one */
newsym->glob_chain = sym->glob_chain;
remove_obj_symbol(sym); /* delete old symbol in object unit */
break;
}
}
chain = &sym->glob_chain;
}
*chain = newsym;
if (newou) {
if (trace_sym_access(gv,newsym->name))
fprintf(stderr,"Symbol %s defined in section %s in %s\n",
newsym->name,newsym->relsect->name,getobjname(newou));
}
return TRUE;
}
#if 0 /* not used */
void unlink_globsymbol(struct GlobalVars *gv,struct Symbol *sym)
/* remove a symbol from the global symbol list */
{
static const char *fn = "unlink_globsymbol(): ";
if (gv->symbols) {
struct Symbol *cptr;
struct Symbol **chain = &gv->symbols[elf_hash(sym->name)%SYMHTABSIZE];
while (cptr = *chain) {
if (cptr == sym)
break;
chain = &cptr->glob_chain;
}
if (cptr) {
/* delete the symbol node from the chain */
*chain = sym->glob_chain;
sym->glob_chain = NULL;
}
else
ierror("%s%s could not be found in global symbols list",fn,sym->name);
}
else
ierror("%ssymbols==NULL",fn);
}
#endif
void hide_shlib_symbols(struct GlobalVars *gv)
/* scan for all unreferenced SYMF_SHLIB symbols in the global symbol list
and remove them - they have to be invisible for the file we create */
{
int i;
for (i=0; i<SYMHTABSIZE; i++) {
struct Symbol *sym;
struct Symbol **chain = &gv->symbols[i];
while (sym = *chain) {
if ((sym->flags & SYMF_SHLIB) && !(sym->flags & SYMF_REFERENCED)) {
/* remove from global symbol list */
*chain = sym->glob_chain;
sym->glob_chain = NULL;
}
else
chain = &sym->glob_chain;
}
}
}
static void add_objsymbol(struct ObjectUnit *ou,struct Symbol *newsym)
/* Add a symbol to an object unit's symbol table. The symbol name
must be unique within the object, otherwise an internal error
will occur! */
{
struct Symbol *sym;
struct Symbol **chain = &ou->objsyms[elf_hash(newsym->name)%OBJSYMHTABSIZE];
while (sym = *chain) {
if (!strcmp(newsym->name,sym->name))
ierror("add_objsymbol(): %s defined twice",newsym->name);
chain = &sym->obj_chain;
}
*chain = newsym;
}
struct Symbol *addsymbol(struct GlobalVars *gv,struct Section *s,
const char *name,const char *iname,lword val,
uint8_t type,uint8_t flags,uint8_t info,uint8_t bind,
uint32_t size,bool chkdef)
/* Define a new symbol. If defined twice in the same object unit, then */
/* return a pointer to its first definition. Defining the symbol twice */
/* globally is only allowed in different object units of a library. */
{
struct Symbol *sym;
struct ObjectUnit *ou = s->obj;
struct Symbol **chain = &ou->objsyms[elf_hash(name)%OBJSYMHTABSIZE];
while (sym = *chain) {
if (!strcmp(name,sym->name)) {
if (chkdef) /* do we have to warn about multiple def. ourselves? */
error(56,ou->lnkfile->pathname,name,getobjname(ou));
return sym; /* return first definition to caller */
}
chain = &sym->obj_chain;
}
sym = alloczero(sizeof(struct Symbol));
sym->name = name;
sym->indir_name = iname;
sym->value = val;
sym->relsect = s;
sym->type = type;
sym->flags = flags;
sym->info = info;
sym->bind = bind;
sym->size = size;
if (type == SYM_COMMON) {
/* alignment of .common section must suit the biggest common-alignment */
uint8_t com_alignment = lshiftcnt(val);
if (com_alignment > s->alignment)
s->alignment = com_alignment;
}
if (check_protection(gv,name))
sym->flags |= SYMF_PROTECTED;
if (bind==SYMB_GLOBAL || bind==SYMB_WEAK) {
uint16_t flags = ou->lnkfile->flags;
if (flags & IFF_DELUNDERSCORE) {
if (*name == '_')
sym->name = name + 1; /* delete preceding underscore, if present */
}
else if (flags & IFF_ADDUNDERSCORE) {
char *new_name = alloc(strlen(name) + 2);
*new_name = '_';
strcpy(new_name+1,name);
sym->name = new_name;
}
if (!addglobsym(gv,sym)) {
free(sym);
return NULL;
}
}
*chain = sym;
return NULL; /* ok, symbol exists only once in this object */
}
struct Symbol *findlocsymbol(struct GlobalVars *gv,struct ObjectUnit *ou,
const char *name)
/* find a symbol which is local to the provided ObjectUnit */
{
struct Symbol *sym;
struct Symbol **chain = &ou->objsyms[elf_hash(name)%OBJSYMHTABSIZE];
while (sym = *chain) {
if (!strcmp(sym->name,name))
return sym;
chain = &sym->obj_chain;
}
return NULL;
}
void addlocsymbol(struct GlobalVars *gv,struct Section *s,char *name,
char *iname,lword val,uint8_t type,uint8_t flags,
uint8_t info,uint32_t size)
/* Define a new local symbol. Local symbols are allowed to be */
/* multiply defined. */
{
struct Symbol *sym;
struct Symbol **chain = &s->obj->objsyms[elf_hash(name)%OBJSYMHTABSIZE];
while (sym = *chain)
chain = &sym->obj_chain;
*chain = sym = alloczero(sizeof(struct Symbol));
sym->name = name;
sym->indir_name = iname;
sym->value = val;
sym->relsect = s;
sym->type = type;
sym->flags = flags;
sym->info = info;
sym->bind = SYMB_LOCAL;
sym->size = size;
if (check_protection(gv,name))
sym->flags |= SYMF_PROTECTED;
}
struct Symbol *addlnksymbol(struct GlobalVars *gv,const char *name,lword val,
uint8_t type,uint8_t flags,uint8_t info,
uint8_t bind,uint32_t size)
/* Define a new, target-specific, linker symbol. */
{
struct Symbol *sym;
struct Symbol **chain;
if (gv->lnksyms == NULL)
gv->lnksyms = alloc_hashtable(LNKSYMHTABSIZE);
chain = &gv->lnksyms[elf_hash(name)%LNKSYMHTABSIZE];
while (sym = *chain)
chain = &sym->obj_chain;
*chain = sym = alloczero(sizeof(struct Symbol));
sym->name = name;
sym->value = val;
sym->type = type;
sym->flags = flags;
sym->info = info;
sym->bind = bind;
sym->size = size;
return sym;
}
struct Symbol *findlnksymbol(struct GlobalVars *gv,const char *name)
/* return pointer to Symbol, if present */
{
struct Symbol *sym;
if (gv->lnksyms) {
sym = gv->lnksyms[elf_hash(name)%LNKSYMHTABSIZE];
while (sym) {
if (!strcmp(name,sym->name))
return sym; /* symbol found! */
sym = sym->obj_chain;
}
}
return NULL;
}
static void unlink_lnksymbol(struct GlobalVars *gv,struct Symbol *sym)
/* remove a linker-symbol from its list */
{
static const char *fn = "unlink_lnksymbol(): ";
if (gv->lnksyms) {
struct Symbol *cptr;
struct Symbol **chain = &gv->lnksyms[elf_hash(sym->name)%LNKSYMHTABSIZE];
while (cptr = *chain) {
if (cptr == sym)
break;
chain = &cptr->obj_chain;
}
if (cptr) {
/* delete the symbol node from the chain */
*chain = sym->obj_chain;
sym->obj_chain = NULL;
}
else
ierror("%s%s could not be found in linker-symbols list",fn,sym->name);
}
else
ierror("%slnksyms==NULL",fn);
}
void fixlnksymbols(struct GlobalVars *gv,struct LinkedSection *def_ls)
{
struct Symbol *sym,*next;
int i;
if (gv->lnksyms) {
for (i=0; i<LNKSYMHTABSIZE; i++) {
for (sym=gv->lnksyms[i]; sym; sym=next) {
next = sym->obj_chain;
if (sym->flags & SYMF_LNKSYM) {
if (fff[gv->dest_format]->setlnksym) {
/* do target-specific symbol modificatios (relsect, value, etc.) */
fff[gv->dest_format]->setlnksym(gv,sym);
if (sym->relsect==NULL && def_ls!=NULL) {
/* @@@ attach absolute symbols to the provided default section */
if (!listempty(&def_ls->sections))
sym->relsect = (struct Section *)def_ls->sections.first;
}
if (sym->type == SYM_RELOC)
sym->value += sym->relsect->va;
#if 0 /* @@@ not needed? */
add_objsymbol(sym->relsect->obj,sym);
#endif
if (sym->bind >= SYMB_GLOBAL)
addglobsym(gv,sym); /* make it globally visible */
/* add to final output section */
addtail(&sym->relsect->lnksec->symbols,&sym->n);
if (gv->map_file)
print_symbol(gv->map_file,sym);
}
}
}
gv->lnksyms[i] = NULL; /* clear chain, it's unusable now! */
}
}
}
struct Symbol *find_any_symbol(struct GlobalVars *gv,struct Section *sec,
const char *name)
/* return pointer to a global symbol or linker symbol */
{
struct Symbol *sym = findsymbol(gv,sec,name);
if (sym == NULL)
sym = findlnksymbol(gv,name);
return sym;
}
void reenter_global_objsyms(struct GlobalVars *gv,struct ObjectUnit *ou)
/* Check all global symbols of an object unit against the global symbol
table for redefinitions or common symbols.
This is required when a new unit has been pulled into the linking
process to resolve an undefined reference. */
{
int i;
for (i=0; i<OBJSYMHTABSIZE; i++) {
struct Symbol *sym = ou->objsyms[i];
while (sym) {
if (sym->bind==SYMB_GLOBAL) {
struct Symbol **chain = &gv->symbols[elf_hash(sym->name)%SYMHTABSIZE];
struct Symbol *gsym;
while (gsym = *chain) {
if (!strcmp(sym->name,gsym->name))
break;
chain = &gsym->glob_chain;
}
if (gsym!=NULL && gsym!=sym) {
if (sym->type==SYM_COMMON && gsym->type==SYM_COMMON) {
if ((sym->size>gsym->size && sym->value>=gsym->value) ||
(sym->size>=gsym->size && sym->value>gsym->value)) {
/* replace by common symbol with bigger size or alignment */
sym->glob_chain = gsym->glob_chain;
remove_obj_symbol(gsym); /* delete old symbol in object unit */
*chain = sym;
}
}
else {
if (ou->lnkfile->type < ID_SHAREDOBJ) {
/* Global symbol "x" is already defined in... */
error(19,ou->lnkfile->pathname,sym->name,getobjname(ou),
getobjname(gsym->relsect->obj));
}
#if 0
/* This causes problems when using the same symbols for different
CPUs, as with WarpOS/68k mixed-binaries (target amigaehf) */
else {
/* hide library symbol by replacing its name by:
__<object unit name>__<symbol name> */
char buf[10];
char *oname,*newname;
if (ou->objname == NULL) {
sprintf(buf,"o%08lx",(unsigned long)ou);
oname = buf;
}
else
oname = ou->objname;
newname = alloc(strlen(oname) + strlen(sym->name) + 5);
sprintf(newname,"__%s__%s",oname,sym->name);
sym->name = newname;
}
#endif
}
}
}
sym = sym->obj_chain;
}
}
}
struct RelocInsert *initRelocInsert(struct RelocInsert *ri,uint16_t pos,
uint16_t siz,lword msk)
{
if (ri == NULL)
ri = alloc(sizeof(struct RelocInsert));
ri->next = NULL;
ri->bpos = pos;
ri->bsiz = siz;
ri->mask = msk;
return ri;
}
struct Reloc *newreloc(struct GlobalVars *gv,struct Section *sec,
const char *xrefname,struct Section *rs,uint32_t id,
unsigned long offset,uint8_t rtype,lword addend)
/* allocate and init new relocation structure */
{
struct Reloc *r = alloczero(sizeof(struct Reloc));
if (r->xrefname = xrefname) {
if (sec->obj) {
uint16_t flags = sec->obj->lnkfile->flags;
if (flags & IFF_DELUNDERSCORE) {
if (*xrefname == '_')
r->xrefname = ++xrefname;
}
else if (flags & IFF_ADDUNDERSCORE) {
char *new_name = alloc(strlen(xrefname) + 2);
*new_name = '_';
strcpy(new_name+1,xrefname);
r->xrefname = new_name;
}
}
}
if (rs)
r->relocsect.ptr = rs;
else
r->relocsect.id = id;
r->offset = offset;
r->addend = addend;
r->rtype = rtype;
return r;
}
void addreloc(struct Section *sec,struct Reloc *r,
uint16_t pos,uint16_t siz,lword mask)
/* Add a relocation description of the current type to this relocation,
which will be inserted into the sections reloc list, if not
already done. */
{
struct RelocInsert *ri,*new;
new = initRelocInsert(NULL,pos,siz,mask);
if (ri = r->insert) {
while (ri->next)
ri = ri->next;
ri->next = new;
}
else
r->insert = new;
if (r->n.next==NULL && sec!=NULL) {
if (r->xrefname)
addtail(&sec->xrefs,&r->n); /* add to current section's XRef list */
else
addtail(&sec->relocs,&r->n); /* add to current section's Reloc list */
}
}
void addreloc_ri(struct Section *sec,struct Reloc *r,struct RelocInsert *ri)
/* Call addreloc with information from the supplied RelocInsert structure.
May result in multiple addreloc-calls. */
{
while (ri != NULL) {
addreloc(sec,r,ri->bpos,ri->bsiz,ri->mask);
ri = ri->next;
}
}
bool isstdreloc(struct Reloc *r,uint8_t type,uint16_t size)
/* return true when relocation type matches standard requirements */
{
struct RelocInsert *ri;
if (r->rtype==type && (ri = r->insert)!=NULL) {
if (ri->bpos==0 && ri->bsiz==size && ri->mask==-1 && ri->next==NULL)
return TRUE;
}
return FALSE;
}
struct Reloc *findreloc(struct Section *sec,unsigned long offset)
/* return possible relocation at offset */
{
if (sec) {
struct Reloc *reloc;
for (reloc=(struct Reloc *)sec->relocs.first;
reloc->n.next!=NULL; reloc=(struct Reloc *)reloc->n.next) {
if (reloc->offset == offset)
return reloc;
}
}
return NULL;
}
void addstabs(struct ObjectUnit *ou,struct Section *sec,char *name,
uint8_t type,int8_t other,int16_t desc,uint32_t value)
/* add an stab entry for debugging */
{
struct StabDebug *stab = alloc(sizeof(struct StabDebug));
stab->relsect = sec;
stab->name.ptr = name;
if (name) {
if (*name == '\0')
stab->name.ptr = NULL;
}
stab->n_type = type;
stab->n_othr = other;
stab->n_desc = desc;
stab->n_value = value;
addtail(&ou->stabs,&stab->n);
}
void fixstabs(struct ObjectUnit *ou)
/* fix offsets of relocatable stab entries */
{
struct StabDebug *stab;
for (stab=(struct StabDebug *)ou->stabs.first;
stab->n.next!=NULL; stab=(struct StabDebug *)stab->n.next) {
if (stab->relsect) {
stab->n_value += (uint32_t)stab->relsect->va;
}
}
}
struct TargetExt *addtargetext(struct Section *s,uint8_t id,uint8_t subid,
uint16_t flags,uint32_t size)
/* Add a new TargetExt structure of given type to a section. The contents */
/* of this structure is target-specific. */
{
struct TargetExt *te,*newte = alloc(size);
newte->next = NULL;
newte->id = id;
newte->sub_id = subid;
newte->flags = flags;
if (te = s->special) {
while (te->next)
te = te->next;
te->next = newte;
}
else
s->special = newte;
return newte;
}
bool checktargetext(struct LinkedSection *ls,uint8_t id,uint8_t subid)
/* Checks if one of the sections in LinkedSection has a TargetExt */
/* block with the given id. If subid = 0 it will be ignored. */
{
struct Section *sec = (struct Section *)ls->sections.first;
struct Section *nextsec;
struct TargetExt *te;
while (nextsec = (struct Section *)sec->n.next) {
if (te = sec->special) {
do {
if (te->id==id && (te->sub_id==subid || subid==0))
return TRUE;
}
while (te = te->next);
}
sec = nextsec;
}
return FALSE;
}
lword readsection(struct GlobalVars *gv,uint8_t rtype,uint8_t *src,
struct RelocInsert *ri)
/* Read data from section at 'src', using the field-offsets, sizes and masks
from the supplied list of RelocInsert structures. */
{
int be = gv->endianess != _LITTLE_ENDIAN_;
int maxfldsz = 0;
lword data = 0;
while (ri != NULL) {
lword mask = ri->mask;
lword v;
int n;
n = mask==-1 ? (int)ri->bsiz : highest_bit_set(mask) + 1;
if (n > maxfldsz)
maxfldsz = n;
/* read from bitfield */
v = readreloc(be,src,ri->bpos,ri->bsiz);
/* mask and denormalize the read value using 'mask' */
n = lshiftcnt(mask);
mask >>= n;
v &= mask;
v <<= n;
/* add to data value, next RelocInsert */
data += v;
ri = ri->next;
}
/* sign-extend, when needed */
if (rtype!=R_SD && rtype!=R_SD2 && rtype!=R_SD21)
return sign_extend(data,maxfldsz);
return data;
}
lword writesection(struct GlobalVars *gv,uint8_t *dest,struct Reloc *r,lword v)
/* Write 'v' into the bit-field defined by the relocation type in 'r'.
Do range checks first, depending on the reloc type.
Returns 0 on success or the masked and normalized value which failed
on the range check. */
{
bool be = gv->endianess != _LITTLE_ENDIAN_;
uint8_t t = r->rtype;
bool signedval = t==R_PC||t==R_GOTPC||t==R_GOTOFF||t==R_PLTPC||t==R_PLTOFF||
t==R_SD||t==R_SD2||t==R_SD21||t==R_MOSDREL;
struct RelocInsert *ri;
if (t == R_NONE)
return 0;
/* Reset all relocation fields to zero. */
for (ri=r->insert; ri!=NULL; ri=ri->next)
writereloc(be,dest,ri->bpos,ri->bsiz,0);
/* add value to relocation fields */
for (ri=r->insert; ri!=NULL; ri=ri->next) {
lword mask = ri->mask;
lword insval = v & mask;
lword oldval;
int bpos = ri->bpos;
int bsiz = ri->bsiz;
insval >>= lshiftcnt(mask); /* normalize according mask */
if (mask>=0 && signedval)
insval = sign_extend(insval,bsiz);
if (!checkrange(insval,signedval,bsiz))
return insval; /* range check failed on 'insval' */
/* add to value already present in this field */
oldval = readreloc(be,dest,bpos,bsiz);
if (mask>=0 && signedval)
oldval = sign_extend(oldval,bsiz);
writereloc(be,dest,bpos,bsiz,oldval+insval);
}
return 0;
}
void calc_relocs(struct GlobalVars *gv,struct LinkedSection *ls)
/* calculate and insert all relocations of a section */
{
const char *fn = "calc_reloc(): ";
struct Reloc *r;
if (ls == NULL)
return;
for (r=(struct Reloc *)ls->relocs.first; r->n.next!=NULL;
r=(struct Reloc *)r->n.next) {
lword s,a,p,val;
if (r->relocsect.lnk == NULL) {
if (r->flags & RELF_DYNLINK)
continue; /* NULL, because it was resolved by a shared object */
else
ierror("calc_relocs: Reloc type %d (%s) at %s+0x%lx (addend 0x%llx)"
" is missing a relocsect.lnk",