-
Notifications
You must be signed in to change notification settings - Fork 9
/
output_aout.c
634 lines (535 loc) · 17.3 KB
/
output_aout.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
/* output_aout.c a.out output driver for vasm */
/* (c) in 2008-2016,2020,2021 by Frank Wille */
#include "vasm.h"
#include "output_aout.h"
#if defined(OUTAOUT) && defined(MID)
static char *copyright="vasm a.out output module 0.8a (c) 2008-2016,2020-2022 Frank Wille";
static section *sections[3];
static utaddr secsize[3];
static utaddr secoffs[3];
static const int sectype[] = { N_TEXT, N_DATA, N_BSS };
static const int secweak[] = { N_WEAKT, N_WEAKD, N_WEAKB };
static struct SymTabList aoutsymlist;
static struct StrTabList aoutstrlist;
static struct list treloclist;
static struct list dreloclist;
static int mid = -1;
static int isPIC = 1;
#define SECT_ALIGN 4 /* .text and .data are aligned to 32 bits */
static int aout_getinfo(symbol *sym)
{
int type;
switch (TYPE(sym)) {
case TYPE_UNKNOWN:
case TYPE_FILE:
case TYPE_SECTION: /* this will be ignored later */
type = AUX_UNKNOWN;
break;
case TYPE_OBJECT:
type = AUX_OBJECT;
break;
case TYPE_FUNCTION:
type = AUX_FUNC;
break;
default:
ierror(0);
break;
}
return type;
}
static int aout_getbind(symbol *sym)
{
if (sym->flags & WEAK)
return BIND_WEAK;
else if (sym->type!=IMPORT && !(sym->flags & EXPORT))
return BIND_LOCAL;
else if ((sym->type!=IMPORT && (sym->flags & EXPORT)) ||
(sym->type==IMPORT && (sym->flags & COMMON)))
return BIND_GLOBAL;
else
ierror(0);
return -1;
}
static uint32_t aoutstd_getrinfo(rlist **rl,int xtern,char *sname,int be)
/* Convert vasm relocation type into standard a.out relocations, */
/* as used by M68k and x86 targets. */
/* For xtern=-1, return true when this relocation requires a base symbol. */
{
nreloc *nr;
if (nr = (nreloc *)(*rl)->reloc) {
rlist *rl2 = (*rl)->next;
uint32_t r=0,s=4;
nreloc *nr2;
int b=0;
switch ((*rl)->type) {
case REL_ABS: b=-1; break;
case REL_PC: b=RSTDB_pcrel; break;
case REL_SD: b=RSTDB_baserel; break;
default: goto unsupp_reloc;
}
if (xtern == -1) /* just query symbol-based relocation */
return b==RSTDB_baserel || b==RSTDB_jmptable;
nr2 = rl2!=NULL ? (nreloc *)rl2->reloc : NULL;
if (nr->bitoffset==0 && (nr2==NULL || nr2->byteoffset!=nr->byteoffset)
&& (nr->mask & MAKEMASK(nr->size)) == MAKEMASK(nr->size)) {
switch (nr->size) {
case 8: s=0; break;
case 16: s=1; break;
case 32: s=2; break;
}
}
#ifdef VASM_CPU_JAGRISC
else if (nr->size==16 && nr2!=NULL && nr2->size==16 &&
nr2->byteoffset==nr->byteoffset &&
((nr->mask==0xffff && nr2->mask==0xffff0000) ||
(nr->mask==0xffff0000 && nr2->mask==0xffff)) &&
((nr->bitoffset==0 && nr2->bitoffset==16) ||
(nr->bitoffset==16 && nr2->bitoffset==0))) {
/* Jaguar RISC MOVEI instruction with swapped words, indicated by
a set RSTDB_copy bit. */
b = RSTDB_copy;
s = 2;
*rl = (*rl)->next; /* skip additional entry */
}
#endif
if (b!=0 && s<4) {
if (b > 0)
setbits(be,&r,32,(unsigned)b,1,1);
setbits(be,&r,32,RSTDB_length,RSTDS_length,s);
setbits(be,&r,32,RSTDB_extern,RSTDS_extern,xtern?1:0);
return readbits(be,&r,32,RELB_reloc,RELS_reloc);
}
}
unsupp_reloc:
unsupp_reloc_error(*rl);
return ~0;
}
static void aout_initwrite(section *firstsec)
{
section *sec;
if (mid == -1)
mid = MID;
initlist(&aoutstrlist.l);
aoutstrlist.hashtab = mycalloc(ASTRTABSIZE*sizeof(struct StrTabNode *));
aoutstrlist.nextoffset = 4; /* first string is always at offset 4 */
initlist(&aoutsymlist.l);
aoutsymlist.hashtab = mycalloc(ASYMTABSIZE*sizeof(struct SymbolNode *));
aoutsymlist.nextindex = 0;
initlist(&treloclist);
initlist(&dreloclist);
/* find exactly one .text, .data and .bss section for a.out */
sections[S_TEXT] = sections[S_DATA] = sections[S_BSS] = NULL;
secsize[S_TEXT] = secsize[S_DATA] = secsize[S_BSS] = 0;
for (sec=firstsec; sec; sec=sec->next) {
int i;
/* section size is assumed to be in in (sec->pc - sec->org), otherwise
we would have to calculate it from the atoms and store it there */
if (get_sec_size(sec) > 0 || (sec->flags & HAS_SYMBOLS)) {
i = get_sec_type(sec);
if (i == S_MISS) {
output_error(3,sec->attr); /* section attributes not supported */
i = S_TEXT;
}
if (i == S_ABS)
continue; /* ignore ORG sections for later */
if (!sections[i]) {
sections[i] = sec;
secsize[i] = get_sec_size(sec);
sec->idx = i; /* section index 0:text, 1:data, 2:bss */
}
else
output_error(7,sec->name);
}
}
/* now scan for absolute ORG-sections and add their aligned size to .text */
for (sec=firstsec; sec; sec=sec->next) {
if (sec->flags & ABSOLUTE)
secsize[S_TEXT] += balign(secsize[S_TEXT],sec->align) + get_sec_size(sec);
}
secoffs[S_TEXT] = 0;
secoffs[S_DATA] = secsize[S_TEXT] + balign(secsize[S_TEXT],SECT_ALIGN);
secoffs[S_BSS] = secoffs[S_DATA] + secsize[S_DATA] +
balign(secsize[S_DATA],SECT_ALIGN);
}
static uint32_t aout_addstr(char *s)
/* add a new symbol name to the string table and return its offset */
{
struct StrTabNode **chain;
struct StrTabNode *sn;
if (s == NULL)
return 0;
if (*s == '\0')
return 0;
/* search string in hash table */
chain = &aoutstrlist.hashtab[hashcode(s)%ASTRTABSIZE];
while (sn = *chain) {
if (!strcmp(s,sn->str))
return (sn->offset); /* it's already in, return offset */
chain = &sn->hashchain;
}
/* new string table entry */
*chain = sn = mymalloc(sizeof(struct StrTabNode));
sn->hashchain = NULL;
sn->str = s;
sn->offset = aoutstrlist.nextoffset;
addtail(&aoutstrlist.l,&sn->n);
aoutstrlist.nextoffset += strlen(s) + 1;
return sn->offset;
}
static struct SymbolNode *aout_addsym(char *name,uint8_t type,int8_t other,
int16_t desc,uint32_t value,int be)
/* append a new symbol to the symbol list */
{
struct SymbolNode *sym = mycalloc(sizeof(struct SymbolNode));
sym->name = name!=NULL ? name : emptystr;
sym->index = aoutsymlist.nextindex++;
setval(be,&sym->s.n_strx,4,aout_addstr(name));
sym->s.n_type = type;
sym->s.n_other = other;
setval(be,&sym->s.n_desc,2,desc);
setval(be,&sym->s.n_value,4,value);
addtail(&aoutsymlist.l,&sym->n);
return sym;
}
static uint32_t aout_addsymhash(char *name,taddr value,int bind,
int info,int type,int desc,int be)
/* add a new symbol, return its symbol table index */
{
struct SymbolNode **chain,*sym;
chain = &aoutsymlist.hashtab[hashcode(name?name:emptystr)%ASYMTABSIZE];
while (sym = *chain)
chain = &sym->hashchain;
/* new symbol table entry */
*chain = sym = aout_addsym(name,type,((bind&0xf)<<4)|(info&0xf),
desc,value,be);
return sym->index;
}
static int aout_findsym(char *name,int be)
/* find a symbol by its name, return symbol table index or -1 */
{
struct SymbolNode **chain = &aoutsymlist.hashtab[hashcode(name)%ASYMTABSIZE];
struct SymbolNode *sym;
while (sym = *chain) {
if (!strcmp(name,sym->name) && !(sym->s.n_type & N_STAB))
return ((int)sym->index);
chain = &sym->hashchain;
}
return (-1);
}
static void aout_symconvert(symbol *sym,int symbind,int syminfo,int be)
/* convert vasm symbol into a.out symbol(s) */
{
taddr val = get_sym_value(sym);
taddr size = get_sym_size(sym);
int ext = (symbind == BIND_GLOBAL) ? N_EXT : 0;
int type = 0;
if (TYPE(sym) == TYPE_SECTION) {
return; /* section symbols are ignored in a.out! */
}
else if (TYPE(sym) == TYPE_FILE) {
type = N_FN | N_EXT; /* special case: file name symbol */
size = 0;
}
else {
if (sym->flags & COMMON) {
/* common symbol */
#if 0 /* GNU binutils prefers N_UNDF with val!=0 instead of N_COMM! */
type = N_COMM | ext;
#else
type = N_UNDF | N_EXT;
#endif
val = size;
size = 0;
}
else if (sym->flags & WEAK) {
/* weak symbol */
switch (sym->type) {
case LABSYM: type=secweak[sym->sec->idx]; break;
case IMPORT: type=N_WEAKU; break;
case EXPRESSION: type=N_WEAKA; break;
default: ierror(0); break;
}
}
else if (sym->sec) {
/* address symbol */
if (!(sym->sec->flags & ABSOLUTE)) {
type = sectype[sym->sec->idx] | ext;
val += secoffs[sym->sec->idx]; /* a.out requires to add sec. offset */
}
else /* absolute ORG section: convert labels to ABS symbols */
type = N_ABS | ext;
}
else if (sym->type==EXPRESSION) {
if (sym->flags & EXPORT) {
/* absolute symbol */
type = N_ABS | ext;
}
else
return; /* ignore local expressions */
}
/* @@@ else if (indirect symbols?) {
aout_addsymhash(sym->name,0,symbind,0,N_INDR|ext,0,be);
aout_addsymhash(sym->indir_name,0,0,0,N_UNDF|N_EXT,0,be);
return;
}*/
else
ierror(0);
}
aout_addsymhash(sym->name,val,symbind,syminfo,type,0,be);
if (size) {
/* append N_SIZE symbol declaring the previous symbol's size */
aout_addsymhash(sym->name,size,symbind,syminfo,N_SIZE,0,be);
}
}
static void aout_addsymlist(symbol *sym,int bind,int type,int be)
/* add all symbols with specified bind and type to the a.out symbol list */
{
for (; sym; sym=sym->next) {
/* ignore symbols preceded by a '.' and internal symbols */
if ((sym->type!=IMPORT || (sym->flags&WEAK) || (sym->flags&COMMON))
&& *sym->name != '.' && *sym->name!=' ' && !(sym->flags&VASMINTERN)) {
int syminfo = aout_getinfo(sym);
int symbind = aout_getbind(sym);
if (symbind == bind && (!type || (syminfo == type))) {
aout_symconvert(sym,symbind,syminfo,be);
}
}
}
}
static void aout_debugsyms(int be)
/* add stabs to the a.out symbol list */
{
struct stabdef *nlist = first_nlist;
uint32_t val;
while (nlist != NULL) {
val = nlist->value;
if (nlist->base != NULL) {
/* include section base offset in symbol value */
if (LOCREF(nlist->base)) {
if (!(nlist->base->sec->flags & ABSOLUTE))
val += secoffs[nlist->base->sec->idx];
}
else
ierror(0); /* @@@ handle external references! How? */
}
aout_addsym(nlist->name.ptr,nlist->type,nlist->other,nlist->desc,val,be);
nlist = nlist->next;
}
}
static void aout_addreloclist(struct list *rlst,uint32_t raddr,
uint32_t rindex,uint32_t rinfo,int be)
/* add new relocation_info to .text or .data reloc-list */
{
struct RelocNode *rn = mymalloc(sizeof(struct RelocNode));
setval(be,rn->r.r_address,4,raddr);
setbits(be,rn->r.r_info,32,RELB_symbolnum,RELS_symbolnum,rindex);
setbits(be,rn->r.r_info,32,RELB_reloc,RELS_reloc,rinfo);
addtail(rlst,&rn->n);
if (isPIC && !readbits(be,rn->r.r_info,32,RSTDB_pcrel,1)
&& !readbits(be,rn->r.r_info,32,RSTDB_baserel,1)) {
/* the relocation is probably absolute, so it is no PIC anymore */
isPIC = 0;
}
}
static uint32_t aout_convert_rlist(int be,atom *a,int secid,
struct list *rlst,taddr pc,
uint32_t (*getrinfo)
(rlist **,int,char *,int))
/* convert all of an atom's relocs into a.out relocations */
{
uint32_t rsize = 0;
rlist *rl;
if (a->type == DATA)
rl = a->content.db->relocs;
else if (a->type == SPACE)
rl = a->content.sb->relocs;
else
rl = NULL;
if (rl == NULL)
return 0; /* no relocs or not the right atom type */
do {
nreloc *r = (nreloc *)rl->reloc;
symbol *refsym = r->sym;
taddr val = get_sym_value(refsym);
taddr add = nreloc_real_addend(r);
int baserel = rl->type == REL_SD; /* @@@ Amiga GNU-binutils compat. */
if (LOCREF(refsym)) {
/* this is a local relocation */
int rsecid = refsym->sec->idx;
aout_addreloclist(rlst,pc+r->byteoffset,sectype[rsecid],
getrinfo(&rl,0,sections[secid]->name,be),
be);
if (baserel) /* val is based on .data for small-data/bss */
val += rsecid==S_BSS ? secoffs[S_BSS]-secoffs[S_DATA] : 0;
else
val += secoffs[rsecid];
rsize += sizeof(struct relocation_info);
}
else if (EXTREF(refsym)) {
/* this is an external symbol reference */
int symidx;
if ((symidx = aout_findsym(refsym->name,be)) == -1)
symidx = aout_addsymhash(refsym->name,0,0,0,N_UNDF|N_EXT,0,be);
aout_addreloclist(rlst,pc+r->byteoffset,symidx,
getrinfo(&rl,1,sections[secid]->name,be),
be);
rsize += sizeof(struct relocation_info);
}
else
ierror(0);
/* patch addend for a.out */
if (rl->type == REL_PC)
val -= pc + r->byteoffset;
if (a->type == DATA)
setval(be,a->content.db->data+r->byteoffset,r->size>>3,val+add);
else if (a->type==SPACE && a->content.sb->space!=0) {
setval(be,a->content.sb->fill,r->size>>3,val+add);
a->content.sb->space = 0; /* we only need to patch 'fill' once */
}
}
while (rl = rl->next);
return rsize;
}
static uint32_t aout_addrelocs(int be,int secid,struct list *rlst,
uint32_t (*getrinfo)(rlist **,int,char *,int))
/* creates a.out relocations for a single section (.text or .data) */
{
uint32_t rtabsize=0;
if (sections[secid]) {
atom *a;
taddr pc=0,npc;
for (a=sections[secid]->first; a; a=a->next) {
npc = pcalign(a,pc);
rtabsize += aout_convert_rlist(be,a,secid,rlst,npc,getrinfo);
pc = npc + atom_size(a,sections[secid],npc);
}
}
return rtabsize;
}
static void aout_header(FILE *f,uint32_t mag,uint32_t flag,
uint32_t tsize,uint32_t dsize,uint32_t bsize,
uint32_t syms,uint32_t entry,
uint32_t trsize,uint32_t drsize,int be)
/* write an a.out header */
{
struct aout_hdr h;
SETMIDMAG(&h,mag,mid,flag);
setval(be,h.a_text,4,tsize);
setval(be,h.a_data,4,dsize);
setval(be,h.a_bss,4,bsize);
setval(be,h.a_syms,4,syms);
setval(be,h.a_entry,4,entry);
setval(be,h.a_trsize,4,trsize);
setval(be,h.a_drsize,4,drsize);
fwdata(f,&h,sizeof(struct aout_hdr));
}
static void aout_writesection(FILE *f,section *sec,taddr sec_align)
{
if (sec) {
atom *a;
taddr pc=0,npc;
for (a=sec->first; a; a=a->next) {
npc = fwpcalign(f,a,sec,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,sec,npc);
}
fwalign(f,pc,sec_align);
}
}
static void aout_writeorg(FILE *f,section *sec,taddr sec_align)
/* write all absolute ORG-sections appended to .text */
{
taddr pc = get_sec_size(sections[S_TEXT]);
taddr npc;
atom *a;
for (; sec; sec=sec->next) {
if (sec->flags & ABSOLUTE) {
fwalign(f,pc,sec->align);
for (a=sec->first; a; a=a->next) {
npc = fwpcalign(f,a,sec,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,sec,npc);
}
}
}
fwalign(f,pc,sec_align);
}
void aout_writerelocs(FILE *f,struct list *l)
{
struct RelocNode *rn;
while (rn = (struct RelocNode *)remhead(l))
fwdata(f,&rn->r,sizeof(struct relocation_info));
}
void aout_writesymbols(FILE *f)
{
struct SymbolNode *sym;
while (sym = (struct SymbolNode *)remhead(&aoutsymlist.l))
fwdata(f,&sym->s,sizeof(struct nlist32));
}
void aout_writestrings(FILE *f,int be)
{
if (aoutstrlist.nextoffset > 4) {
struct StrTabNode *stn;
fw32(f,aoutstrlist.nextoffset,be);
while (stn = (struct StrTabNode *)remhead(&aoutstrlist.l))
fwdata(f,stn->str,strlen(stn->str)+1);
}
}
static void write_output(FILE *f,section *sec,symbol *sym)
{
int be = BIGENDIAN;
uint32_t trsize,drsize;
aout_initwrite(sec);
aout_addsymlist(sym,BIND_GLOBAL,0,be);
aout_addsymlist(sym,BIND_WEAK,0,be);
if (!no_symbols) {
aout_addsymlist(sym,BIND_LOCAL,0,be);
aout_debugsyms(be);
}
trsize = aout_addrelocs(be,S_TEXT,&treloclist,aoutstd_getrinfo);
drsize = aout_addrelocs(be,S_DATA,&dreloclist,aoutstd_getrinfo);
aout_header(f,OMAGIC,isPIC?EX_PIC:0,
secsize[S_TEXT] + balign(secsize[S_TEXT],SECT_ALIGN),
secsize[S_DATA] + balign(secsize[S_DATA],SECT_ALIGN),
secsize[S_BSS],
aoutsymlist.nextindex * sizeof(struct nlist32),
0,trsize,drsize,be);
aout_writesection(f,sections[S_TEXT],0);
aout_writeorg(f,sec,SECT_ALIGN);
aout_writesection(f,sections[S_DATA],SECT_ALIGN);
aout_writerelocs(f,&treloclist);
aout_writerelocs(f,&dreloclist);
aout_writesymbols(f);
aout_writestrings(f,be);
}
static int output_args(char *p)
{
if (!strncmp(p,"-mid=",5)) {
mid = atoi(p+5);
return 1;
}
return 0;
}
int init_output_aout(char **cp,void (**wo)(FILE *,section *,symbol *),
int (**oa)(char *))
{
*cp = copyright;
*wo = write_output;
*oa = output_args;
unnamed_sections = 1; /* output format doesn't support named sections */
secname_attr = 1;
return 1;
}
#else
int init_output_aout(char **cp,void (**wo)(FILE *,section *,symbol *),
int (**oa)(char *))
{
return 0;
}
#endif