forked from StarWolf3000/vasm-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_elf.c
905 lines (738 loc) · 24.5 KB
/
output_elf.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
/* elf.c ELF output driver for vasm */
/* (c) in 2002-2016,2020,2022 by Frank Wille */
#include "vasm.h"
#include "output_elf.h"
#include "stabs.h"
#if ELFCPU && defined(OUTELF)
static char *copyright="vasm ELF output module 2.7b (c) 2002-2016,2020,2022 Frank Wille";
static int keep_empty_sects;
static int be,cpu,bits;
static unsigned elfrelsize,shtreloc;
static hashtable *elfsymhash;
static struct list shdrlist,symlist,relalist;
static struct StrTabList shstrlist,strlist,stabstrlist;
static unsigned symtabidx,strtabidx,shstrtabidx;
static unsigned symindex,shdrindex;
static unsigned stabidx,stabstralign;
static utaddr stablen;
static char stabname[] = ".stab";
static unsigned addString(struct StrTabList *sl,const char *s)
{
struct StrTabNode *sn = mymalloc(sizeof(struct StrTabNode));
unsigned idx = sl->index;
sn->str = s;
addtail(&(sl->l),&(sn->n));
sl->index += (unsigned)strlen(s) + 1;
return idx;
}
static void init_lists(void)
{
elfsymhash = new_hashtable(ELFSYMHTABSIZE);
initlist(&shdrlist);
initlist(&symlist);
initlist(&relalist);
shstrlist.index = strlist.index = 0;
initlist(&shstrlist.l);
initlist(&strlist.l);
symindex = shdrindex = stabidx = 0;
addString(&shstrlist,emptystr); /* first string is always "" */
symtabidx = addString(&shstrlist,".symtab");
strtabidx = addString(&shstrlist,".strtab");
shstrtabidx = addString(&shstrlist,".shstrtab");
addString(&strlist,emptystr);
if (!no_symbols && first_nlist) {
initlist(&stabstrlist.l);
addString(&stabstrlist,emptystr);
}
}
static struct Shdr32Node *addShdr32(void)
{
struct Shdr32Node *s = mycalloc(sizeof(struct Shdr32Node));
addtail(&shdrlist,&(s->n));
shdrindex++;
return s;
}
static struct Shdr64Node *addShdr64(void)
{
struct Shdr64Node *s = mycalloc(sizeof(struct Shdr64Node));
addtail(&shdrlist,&(s->n));
shdrindex++;
return s;
}
static struct Symbol32Node *addSymbol32(const char *name)
{
struct Symbol32Node *sn = mycalloc(sizeof(struct Symbol32Node));
hashdata data;
addtail(&symlist,&(sn->n));
if (name) {
sn->name = name;
setval(be,sn->s.st_name,4,addString(&strlist,name));
}
data.ptr = sn;
sn->idx = symindex++;
add_hashentry(elfsymhash,name?name:emptystr,data);
return sn;
}
static struct Symbol64Node *addSymbol64(const char *name)
{
struct Symbol64Node *sn = mycalloc(sizeof(struct Symbol64Node));
hashdata data;
addtail(&symlist,&(sn->n));
if (name) {
sn->name = name;
setval(be,sn->s.st_name,4,addString(&strlist,name));
}
data.ptr = sn;
sn->idx = symindex++;
add_hashentry(elfsymhash,name?name:emptystr,data);
return sn;
}
static void newSym32(const char *name,elfull value,elfull size,uint8_t bind,
uint8_t type,unsigned shndx)
{
struct Symbol32Node *elfsym = addSymbol32(name);
setval(be,elfsym->s.st_value,4,value);
setval(be,elfsym->s.st_size,4,size);
elfsym->s.st_info[0] = ELF32_ST_INFO(bind,type);
setval(be,elfsym->s.st_shndx,2,shndx);
}
static void newSym64(const char *name,elfull value,elfull size,uint8_t bind,
uint8_t type,unsigned shndx)
{
struct Symbol64Node *elfsym = addSymbol64(name);
setval(be,elfsym->s.st_value,8,value);
setval(be,elfsym->s.st_size,8,size);
elfsym->s.st_info[0] = ELF64_ST_INFO(bind,type);
setval(be,elfsym->s.st_shndx,2,shndx);
}
static void addRel32(elfull o,elfull a,elfull i,elfull r)
{
if (RELA) {
struct Rela32Node *rn = mymalloc(sizeof(struct Rela32Node));
setval(be,rn->r.r_offset,4,o);
setval(be,rn->r.r_addend,4,a);
setval(be,rn->r.r_info,4,ELF32_R_INFO(i,r));
addtail(&relalist,&(rn->n));
}
else {
struct Rel32Node *rn = mymalloc(sizeof(struct Rel32Node));
setval(be,rn->r.r_offset,4,o);
setval(be,rn->r.r_info,4,ELF32_R_INFO(i,r));
addtail(&relalist,&(rn->n));
}
}
static void addRel64(elfull o,elfull a,elfull i,elfull r)
{
if (RELA) {
struct Rela64Node *rn = mymalloc(sizeof(struct Rela64Node));
setval(be,rn->r.r_offset,8,o);
setval(be,rn->r.r_addend,8,a);
setval(be,rn->r.r_info,8,ELF64_R_INFO(i,r));
addtail(&relalist,&(rn->n));
}
else {
struct Rel64Node *rn = mymalloc(sizeof(struct Rel64Node));
setval(be,rn->r.r_offset,8,o);
setval(be,rn->r.r_info,8,ELF64_R_INFO(i,r));
addtail(&relalist,&(rn->n));
}
}
static void *makeShdr32(elfull name,elfull type,elfull flags,elfull offset,
elfull size,elfull info,elfull align,elfull entsize)
{
struct Shdr32Node *shn;
shn = addShdr32();
setval(be,shn->s.sh_name,4,name);
setval(be,shn->s.sh_type,4,type);
setval(be,shn->s.sh_flags,4,flags);
setval(be,shn->s.sh_offset,4,offset);
setval(be,shn->s.sh_size,4,size);
setval(be,shn->s.sh_info,4,info);
setval(be,shn->s.sh_addralign,4,align);
setval(be,shn->s.sh_entsize,4,entsize);
/* @@@ set sh_addr to org? */
return shn;
}
static void *makeShdr64(elfull name,elfull type,elfull flags,elfull offset,
elfull size,elfull info,elfull align,elfull entsize)
{
struct Shdr64Node *shn;
shn = addShdr64();
setval(be,shn->s.sh_name,4,name);
setval(be,shn->s.sh_type,4,type);
setval(be,shn->s.sh_flags,8,flags);
setval(be,shn->s.sh_offset,8,offset);
setval(be,shn->s.sh_size,8,size);
setval(be,shn->s.sh_info,4,info);
setval(be,shn->s.sh_addralign,8,align);
setval(be,shn->s.sh_entsize,8,entsize);
/* @@@ set sh_addr to org? */
return shn;
}
static unsigned findelfsymbol(const char *name)
/* find symbol with given name in symlist, return its index */
{
hashdata data;
if (find_name(elfsymhash,name,&data))
return ((struct Symbol32Node *)data.ptr)->idx;
return 0;
}
static void init_ident(unsigned char *id,uint8_t class)
{
static const char elfid[4] = { 0x7f,'E','L','F' };
memcpy(&id[EI_MAG0],elfid,4);
id[EI_CLASS] = class;
id[EI_DATA] = be ? ELFDATA2MSB : ELFDATA2LSB;
id[EI_VERSION] = EV_CURRENT;
memset(&id[EI_PAD],0,EI_NIDENT-EI_PAD);
}
static uint32_t elf_sec_type(section *s)
/* scan section attributes for type */
{
if (!strncmp(s->name,".note",5))
return SHT_NOTE;
switch (get_sec_type(s)) {
case S_TEXT:
case S_DATA:
return SHT_PROGBITS;
case S_BSS:
return SHT_NOBITS;
}
#if 0
output_error(3,attr); /* section attributes not supported */
return SHT_NULL;
#else
return SHT_PROGBITS;
#endif
}
static utaddr get_sec_flags(const char *a)
/* scan section attributes for flags (read, write, alloc, execute) */
{
utaddr f = 0;
while (*a) {
switch (*a++) {
case 'a':
f |= SHF_ALLOC;
break;
case 'w':
f |= SHF_WRITE;
break;
case 'x':
f |= SHF_EXECINSTR;
break;
}
}
return f;
}
static uint8_t get_sym_info(symbol *s)
/* determine symbol-info: function, object, section, etc. */
{
switch (TYPE(s)) {
case TYPE_OBJECT:
return STT_OBJECT;
case TYPE_FUNCTION:
return STT_FUNC;
case TYPE_SECTION:
return STT_SECTION;
case TYPE_FILE:
return STT_FILE;
}
return STT_NOTYPE;
}
static unsigned get_sym_index(symbol *s)
{
if (s->flags & COMMON)
return SHN_COMMON;
if (s->type == IMPORT)
return SHN_UNDEF;
if (s->sec)
return (unsigned)s->sec->idx;
return SHN_ABS;
}
static utaddr get_reloc_type(atom *a,rlist **rl,
utaddr *roffset,taddr *addend,symbol **refsym)
{
rlist *rl2;
utaddr mask;
int pos,size;
utaddr t = RTYPE_ILLEGAL;
*roffset = 0;
*addend = 0;
*refsym = NULL;
#ifdef VASM_CPU_M68K
#include "elf_reloc_68k.h"
#endif
#ifdef VASM_CPU_PPC
#include "elf_reloc_ppc.h"
#endif
#ifdef VASM_CPU_ARM
#include "elf_reloc_arm.h"
#endif
#ifdef VASM_CPU_X86
if (bytespertaddr == 8) {
#include "elf_reloc_x86_64.h"
}
else {
#include "elf_reloc_386.h"
}
#endif
#ifdef VASM_CPU_JAGRISC
#include "elf_reloc_jag.h"
#endif
if (t == RTYPE_ILLEGAL)
unsupp_reloc_error(a,*rl);
return t;
}
static utaddr make_relocs(atom *a,rlist *rl,utaddr pc,
void (*newsym)(const char *,elfull,elfull,uint8_t,
uint8_t,unsigned),
void (*addrel)(elfull,elfull,elfull,elfull))
/* convert all of an atom's relocations into ELF32/ELF64 relocs */
{
utaddr ro = 0;
if (rl) {
do {
utaddr rtype,offset;
taddr addend;
symbol *refsym;
rtype = get_reloc_type(a,&rl,&offset,&addend,&refsym);
if (rtype != RTYPE_ILLEGAL) {
if (LOCREF(refsym)) {
/* this is a local relocation */
addrel(pc+offset,addend,refsym->sec->idx,rtype);
ro += elfrelsize;
}
else if (EXTREF(refsym)) {
/* this is an external symbol reference */
unsigned idx = findelfsymbol(refsym->name);
if (idx == 0) {
/* create a new symbol, which can be referenced */
idx = symindex;
newsym(refsym->name,0,0,STB_GLOBAL,STT_NOTYPE,0);
}
addrel(pc+offset,addend,idx,rtype);
ro += elfrelsize;
}
else
ierror(0);
}
}
while (rl = rl->next);
}
return ro;
}
static utaddr make_stabreloc(utaddr pc,struct stabdef *nlist,
void (*newsym)(const char *,elfull,elfull,uint8_t,
uint8_t,unsigned),
void (*addrel)(elfull,elfull,elfull,elfull))
{
rlist dummyrl;
rlist *rl = &dummyrl;
nreloc nrel;
nrel.byteoffset = offsetof(struct nlist32,n_value);
nrel.bitoffset = 0;
nrel.size = bits;
nrel.mask = DEFMASK;
nrel.addend = nlist->value;
nrel.sym = nlist->base;
rl->next = NULL;
rl->reloc = &nrel;
rl->type = REL_ABS;
return make_relocs(NULL,rl,pc,newsym,addrel);
}
/* create .rel(a)XXX section header */
static void make_relsechdr(const char *sname,utaddr roffs,utaddr len,unsigned idx,
void *(*makeshdr)(elfull,elfull,elfull,elfull,
elfull,elfull,elfull,elfull))
{
char *rname = mymalloc(strlen(sname) + 6);
if (RELA)
sprintf(rname,".rela%s",sname);
else
sprintf(rname,".rel%s",sname);
makeshdr(addString(&shstrlist,rname),shtreloc,0,
roffs, /* relative offset - will be fixed later! */
len,idx,bytespertaddr,elfrelsize);
}
static utaddr prog_sec_hdrs(section *sec,utaddr soffset,
void *(*makeshdr)(elfull,elfull,elfull,elfull,
elfull,elfull,elfull,elfull),
void (*newsym)(const char *,elfull,elfull,
uint8_t,uint8_t,
unsigned))
{
section *secp;
struct stabdef *nlist = first_nlist;
/* generate section headers for program sections */
for (secp=sec; secp; secp=secp->next) {
if (keep_empty_sects ||
get_sec_size(secp)!=0 || (secp->flags & HAS_SYMBOLS)) {
uint32_t type = elf_sec_type(secp);
/* add section base symbol */
newsym(NULL,0,0,STB_LOCAL,STT_SECTION,shdrindex);
secp->idx = shdrindex;
makeshdr(addString(&shstrlist,secp->name),
type,get_sec_flags(secp->attr),soffset,
get_sec_size(secp),0,secp->align,0);
if (type != SHT_NOBITS)
soffset += get_sec_size(secp);
}
else
secp->idx = 0;
}
/* look for stabs (32 bits only) */
if (!no_symbols && bits==32 && nlist!=NULL) {
struct Shdr32Node *shn;
const char *cuname = NULL;
/* count them, set name of compilation unit */
while (nlist != NULL) {
stablen++;
if (nlist->type==N_SO && nlist->name.ptr!=NULL && *nlist->name.ptr!='\0')
cuname = nlist->name.ptr;
nlist = nlist->next;
}
/* add all symbol strings to .stabstr, cu name should be first(?) */
addString(&stabstrlist,cuname!=NULL?cuname:filename);
nlist = first_nlist;
while (nlist != NULL) {
nlist->name.idx = nlist->name.ptr != NULL ?
addString(&stabstrlist,nlist->name.ptr) : 0;
nlist = nlist->next;
}
/* make .stab section, preceded by a compilation unit header (stablen+1) */
stabidx = shdrindex;
shn = makeshdr(addString(&shstrlist,stabname),SHT_PROGBITS,0,soffset,
(stablen+1)*sizeof(struct nlist32),0,4,
sizeof(struct nlist32));
soffset += (stablen+1) * sizeof(struct nlist32);
setval(be,shn->s.sh_link,4,shdrindex); /* associated .stabstr section */
/* make .stabstr section */
makeshdr(addString(&shstrlist,".stabstr"),SHT_STRTAB,0,soffset,
stabstrlist.index,0,1,0);
soffset += stabstrlist.index;
stabstralign = balign(soffset,4);
soffset += stabstralign;
}
return soffset;
}
static unsigned build_symbol_table(symbol *first,
void (*newsym)(const char *,elfull,elfull,
uint8_t,uint8_t,
unsigned))
{
symbol *symp;
unsigned firstglobal;
/* file name symbol, when defined */
if (filename)
newsym(filename,0,0,STB_LOCAL,STT_FILE,SHN_ABS);
if (!no_symbols) /* symbols with local binding first */
for (symp=first; symp; symp=symp->next)
if (*symp->name!='.' && *symp->name!=' ' && !(symp->flags&VASMINTERN))
if (symp->type!=IMPORT && !(symp->flags & (EXPORT|WEAK)))
newsym(symp->name,get_sym_value(symp),get_sym_size(symp),
STB_LOCAL,get_sym_info(symp),get_sym_index(symp));
firstglobal = symindex; /* now the global and weak symbols */
for (symp=first; symp; symp=symp->next)
if (*symp->name != '.' && !(symp->flags&VASMINTERN))
if ((symp->type!=IMPORT && (symp->flags & (EXPORT|WEAK))) ||
(symp->type==IMPORT && (symp->flags & (COMMON|WEAK))))
newsym(symp->name,get_sym_value(symp),get_sym_size(symp),
(symp->flags & WEAK) ? STB_WEAK : STB_GLOBAL,
get_sym_info(symp),get_sym_index(symp));
return firstglobal;
}
static void make_reloc_sections(section *sec,
void (*newsym)(const char *,elfull,elfull,
uint8_t,uint8_t,
unsigned),
void (*addrel)(elfull,elfull,elfull,elfull),
void *(*makeshdr)(elfull,elfull,elfull,elfull,
elfull,elfull,elfull,elfull))
{
struct stabdef *nlist = first_nlist;
utaddr roffset = 0;
utaddr basero,pc;
section *secp;
/* ".rela.xxx" or ".rel.xxx" relocation sections */
for (secp=sec; secp; secp=secp->next) {
if (secp->idx) {
atom *a;
utaddr npc;
for (a=secp->first,basero=roffset,pc=0; a; a=a->next) {
npc = pcalign(a,pc);
if (a->type == DATA)
roffset += make_relocs(a,a->content.db->relocs,npc,newsym,addrel);
if (a->type == SPACE)
roffset += make_relocs(a,a->content.sb->relocs,npc,newsym,addrel);
pc = npc + atom_size(a,secp,npc);
}
if (basero != roffset) /* were there any relocations? */
make_relsechdr(secp->name,basero,roffset-basero,secp->idx,makeshdr);
}
}
if (!no_symbols) {
/* look for relocations in .stab */
basero = roffset;
pc = sizeof(struct nlist32); /* skip compilation unit header */
while (nlist != NULL) {
if (nlist->base != NULL)
roffset += make_stabreloc(pc,nlist,newsym,addrel);
nlist = nlist->next;
pc += sizeof(struct nlist32);
}
/* create .rel(a).stab when needed */
if (basero != roffset)
make_relsechdr(stabname,basero,roffset-basero,stabidx,makeshdr);
}
}
static void write_strtab(FILE *f,struct StrTabList *strl)
{
struct StrTabNode *stn;
while (stn = (struct StrTabNode *)remhead(&(strl->l)))
fwdata(f,stn->str,strlen(stn->str)+1);
}
static void write_section_data(FILE *f,section *sec)
{
struct stabdef *nlist = first_nlist;
section *secp;
for (secp=sec; secp; secp=secp->next) {
if (secp->idx && elf_sec_type(secp)!=SHT_NOBITS) {
atom *a;
utaddr pc=0,npc;
for (a=secp->first; a; a=a->next) {
npc = fwpcalign(f,a,secp,pc);
if (a->type == DATA) {
fwdata(f,a->content.db->data,a->content.db->size);
}
else if (a->type == SPACE) {
fwsblock(f,a->content.sb);
}
pc = npc + atom_size(a,secp,npc);
}
}
}
if (!no_symbols && nlist!=NULL) {
/* write compilation unit header - precedes nlist entries */
fw32(f,1,be); /* source name is first entry in .stabstr */
fw32(f,stablen,be);
fw32(f,stabstrlist.index,be);
/* write .stab */
while (nlist != NULL) {
struct nlist32 n;
setval(be,&n.n_strx,4,nlist->name.idx);
n.n_type = nlist->type;
n.n_other = nlist->other;
setval(be,&n.n_desc,2,nlist->desc);
setval(be,&n.n_value,4,nlist->value);
fwdata(f,&n,sizeof(struct nlist32));
nlist = nlist->next;
}
/* write .stabstr and align */
write_strtab(f,&stabstrlist);
fwspace(f,stabstralign);
}
}
static void write_ELF64(FILE *f,section *sec,symbol *sym)
{
struct Elf64_Ehdr header;
unsigned firstglobal,align1,align2,i;
utaddr soffset=sizeof(struct Elf64_Ehdr);
struct Shdr64Node *shn;
struct Symbol64Node *elfsym;
elfrelsize = RELA ? sizeof(struct Elf64_Rela) : sizeof(struct Elf64_Rel);
/* initialize ELF header */
memset(&header,0,sizeof(struct Elf64_Ehdr));
init_ident(header.e_ident,ELFCLASS64);
setval(be,header.e_type,2,ET_REL);
setval(be,header.e_machine,2,cpu);
setval(be,header.e_version,4,EV_CURRENT);
setval(be,header.e_ehsize,2,sizeof(struct Elf64_Ehdr));
setval(be,header.e_shentsize,2,sizeof(struct Elf64_Shdr));
init_lists();
addShdr64(); /* first section header is always zero */
addSymbol64(NULL); /* first symbol is empty */
/* make program section headers, symbols and relocations */
soffset = prog_sec_hdrs(sec,soffset,makeShdr64,newSym64);
firstglobal = build_symbol_table(sym,newSym64);
make_reloc_sections(sec,newSym64,addRel64,makeShdr64);
/* ".shstrtab" section header string table */
makeShdr64(shstrtabidx,SHT_STRTAB,0,
soffset,shstrlist.index,0,1,0);
soffset += shstrlist.index;
align1 = balign(soffset,4);
soffset += align1;
/* set last values in ELF header */
setval(be,header.e_shoff,8,soffset); /* remember offset of Shdr table */
soffset += (shdrindex+2)*sizeof(struct Elf64_Shdr);
setval(be,header.e_shstrndx,2,shdrindex-1);
setval(be,header.e_shnum,2,shdrindex+2);
/* ".symtab" symbol table */
shn = makeShdr64(symtabidx,SHT_SYMTAB,0,soffset,
symindex*sizeof(struct Elf64_Sym),
firstglobal,8,sizeof(struct Elf64_Sym));
setval(be,shn->s.sh_link,4,shdrindex); /* associated .strtab section */
soffset += symindex * sizeof(struct Elf64_Sym);
/* ".strtab" string table */
makeShdr64(strtabidx,SHT_STRTAB,0,soffset,strlist.index,0,1,0);
soffset += strlist.index;
align2 = balign(soffset,4);
soffset += align2; /* offset for first Reloc-entry */
/* write ELF header */
fwdata(f,&header,sizeof(struct Elf64_Ehdr));
/* write initialized section contents */
write_section_data(f,sec);
/* write .shstrtab string table */
write_strtab(f,&shstrlist);
/* write section headers */
fwspace(f,align1);
i = 0;
while (shn = (struct Shdr64Node *)remhead(&shdrlist)) {
if (readval(be,shn->s.sh_type,4) == shtreloc) {
/* set correct offset and link to symtab */
setval(be,shn->s.sh_offset,8,readval(be,shn->s.sh_offset,8)+soffset);
setval(be,shn->s.sh_link,4,shdrindex-2); /* index of associated symtab */
}
fwdata(f,&(shn->s),sizeof(struct Elf64_Shdr));
i++;
}
/* write symbol table */
while (elfsym = (struct Symbol64Node *)remhead(&symlist))
fwdata(f,&(elfsym->s),sizeof(struct Elf64_Sym));
/* write .strtab string table */
write_strtab(f,&strlist);
/* write relocations */
fwspace(f,align2);
if (RELA) {
struct Rela64Node *rn;
while (rn = (struct Rela64Node *)remhead(&relalist))
fwdata(f,&(rn->r),sizeof(struct Elf64_Rela));
}
else {
struct Rel64Node *rn;
while (rn = (struct Rel64Node *)remhead(&relalist))
fwdata(f,&(rn->r),sizeof(struct Elf64_Rel));
}
}
static void write_ELF32(FILE *f,section *sec,symbol *sym)
{
struct Elf32_Ehdr header;
unsigned firstglobal,align1,align2,i;
utaddr soffset=sizeof(struct Elf32_Ehdr);
struct Shdr32Node *shn;
struct Symbol32Node *elfsym;
elfrelsize = RELA ? sizeof(struct Elf32_Rela) : sizeof(struct Elf32_Rel);
/* initialize ELF header */
memset(&header,0,sizeof(struct Elf32_Ehdr));
init_ident(header.e_ident,ELFCLASS32);
setval(be,header.e_type,2,ET_REL);
setval(be,header.e_machine,2,cpu);
setval(be,header.e_version,4,EV_CURRENT);
#ifdef VASM_CPU_ARM
setval(be,header.e_flags,4,0x04000000); /* EABI version 4 */
#endif
setval(be,header.e_ehsize,2,sizeof(struct Elf32_Ehdr));
setval(be,header.e_shentsize,2,sizeof(struct Elf32_Shdr));
init_lists();
addShdr32(); /* first section header is always zero */
addSymbol32(NULL); /* first symbol is empty */
/* make program section headers, symbols and relocations */
soffset = prog_sec_hdrs(sec,soffset,makeShdr32,newSym32);
firstglobal = build_symbol_table(sym,newSym32);
make_reloc_sections(sec,newSym32,addRel32,makeShdr32);
/* ".shstrtab" section header string table */
makeShdr32(shstrtabidx,SHT_STRTAB,0,
soffset,shstrlist.index,0,1,0);
soffset += shstrlist.index;
align1 = balign(soffset,4);
soffset += align1;
/* set last values in ELF header */
setval(be,header.e_shoff,4,soffset); /* remember offset of Shdr table */
soffset += (shdrindex+2)*sizeof(struct Elf32_Shdr);
setval(be,header.e_shstrndx,2,shdrindex-1);
setval(be,header.e_shnum,2,shdrindex+2);
/* ".symtab" symbol table */
shn = makeShdr32(symtabidx,SHT_SYMTAB,0,soffset,
symindex*sizeof(struct Elf32_Sym),
firstglobal,4,sizeof(struct Elf32_Sym));
setval(be,shn->s.sh_link,4,shdrindex); /* associated .strtab section */
soffset += symindex * sizeof(struct Elf32_Sym);
/* ".strtab" string table */
makeShdr32(strtabidx,SHT_STRTAB,0,soffset,strlist.index,0,1,0);
soffset += strlist.index;
align2 = balign(soffset,4);
soffset += align2; /* offset for first Reloc-entry */
/* write ELF header */
fwdata(f,&header,sizeof(struct Elf32_Ehdr));
/* write initialized section contents */
write_section_data(f,sec);
/* write .shstrtab string table */
write_strtab(f,&shstrlist);
/* write section headers */
fwspace(f,align1);
i = 0;
while (shn = (struct Shdr32Node *)remhead(&shdrlist)) {
if (readval(be,shn->s.sh_type,4) == shtreloc) {
/* set correct offset and link to symtab */
setval(be,shn->s.sh_offset,4,readval(be,shn->s.sh_offset,4)+soffset);
setval(be,shn->s.sh_link,4,shdrindex-2); /* index of associated symtab */
}
fwdata(f,&(shn->s),sizeof(struct Elf32_Shdr));
i++;
}
/* write symbol table */
while (elfsym = (struct Symbol32Node *)remhead(&symlist))
fwdata(f,&(elfsym->s),sizeof(struct Elf32_Sym));
/* write .strtab string table */
write_strtab(f,&strlist);
/* write relocations */
fwspace(f,align2);
if (RELA) {
struct Rela32Node *rn;
while (rn = (struct Rela32Node *)remhead(&relalist))
fwdata(f,&(rn->r),sizeof(struct Elf32_Rela));
}
else {
struct Rel32Node *rn;
while (rn = (struct Rel32Node *)remhead(&relalist))
fwdata(f,&(rn->r),sizeof(struct Elf32_Rel));
}
}
static void write_output(FILE *f,section *sec,symbol *sym)
{
cpu = ELFCPU; /* cpu ID */
be = BIGENDIAN; /* true for big endian */
bits = bytespertaddr * BITSPERBYTE;
shtreloc = RELA ? SHT_RELA : SHT_REL;
if (bits==32 && cpu!=EM_NONE)
write_ELF32(f,sec,sym);
else if (bits==64 && cpu!=EM_NONE)
write_ELF64(f,sec,sym);
else
output_error(1,cpuname); /* output module doesn't support cpu */
if (debug && elfsymhash->collisions)
fprintf(stderr,"*** %d ELF symbol collisions!\n",elfsymhash->collisions);
}
static int output_args(char *p)
{
if (!strcmp(p,"-keepempty")) {
keep_empty_sects = 1;
return 1;
}
return 0;
}
int init_output_elf(char **cp,void (**wo)(FILE *,section *,symbol *),
int (**oa)(char *))
{
*cp = copyright;
*wo = write_output;
*oa = output_args;
secname_attr = 1; /* attribute is used to differentiate between sections */
return 1;
}
#else
int init_output_elf(char **cp,void (**wo)(FILE *,section *,symbol *),
int (**oa)(char *))
{
return 0;
}
#endif