forked from StarWolf3000/vasm-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_xfile.c
374 lines (308 loc) · 9.11 KB
/
output_xfile.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
/* output_xfile.c Sharp X68000 Xfile output driver for vasm */
/* (c) in 2018 by Frank Wille */
#include "vasm.h"
#include "output_xfile.h"
#if defined(OUTXFIL) && defined(VASM_CPU_M68K)
static char *copyright="vasm xfile output module 0.1 (c) 2018 Frank Wille";
static int max_relocs_per_atom;
static section *sections[3];
static utaddr secsize[3];
static utaddr secoffs[3];
static utaddr sdabase,lastoffs;
#define SECT_ALIGN 2 /* Xfile sections have to be aligned to 16 bits */
static int get_sec_type(section *s)
/* scan section attributes for type, 0=text, 1=data, 2=bss */
{
char *a = s->attr;
while (*a) {
switch (*a++) {
case 'c':
return _TEXT;
case 'd':
return _DATA;
case 'u':
return _BSS;
}
}
output_error(3,s->attr); /* section attributes not supported */
return 0;
}
static void xfile_initwrite(section *sec,symbol *sym)
{
int nsyms = 0;
int i;
/* find exactly one .text, .data and .bss section for xfile */
sections[_TEXT] = sections[_DATA] = sections[_BSS] = NULL;
secsize[_TEXT] = secsize[_DATA] = secsize[_BSS] = 0;
for (; sec; sec=sec->next) {
/* 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 ((sec->pc - sec->org) > 0 || (sec->flags & HAS_SYMBOLS)) {
i = get_sec_type(sec);
if (!sections[i]) {
sections[i] = sec;
secsize[i] = (get_sec_size(sec) + SECT_ALIGN - 1) /
SECT_ALIGN * SECT_ALIGN;
sec->idx = i; /* section index 0:text, 1:data, 2:bss */
}
else
output_error(7,sec->name);
}
}
secoffs[_TEXT] = 0;
secoffs[_DATA] = secsize[_TEXT] + balign(secsize[_TEXT],SECT_ALIGN);
secoffs[_BSS] = secoffs[_DATA] + secsize[_DATA] +
balign(secsize[_DATA],SECT_ALIGN);
/* define small data base as .data+32768 @@@FIXME! */
sdabase = secoffs[_DATA] + 0x8000;
}
static void xfile_header(FILE *f,unsigned long tsize,unsigned long dsize,
unsigned long bsize)
{
XFILE hdr;
memset(&hdr,0,sizeof(XFILE));
setval(1,hdr.x_id,2,0x4855); /* "HU" ID for Human68k */
setval(1,hdr.x_textsz,4,tsize);
setval(1,hdr.x_datasz,4,dsize);
setval(1,hdr.x_heapsz,4,bsize);
/* x_relocsz and x_syminfsz will be patched later */
fwdata(f,&hdr,sizeof(XFILE));
}
static void checkdefined(symbol *sym)
{
if (sym->type == IMPORT)
output_error(6,sym->name);
}
static taddr xfile_sym_value(symbol *sym)
{
taddr val = get_sym_value(sym);
/* all sections form a contiguous block, so add section offset */
if (sym->type==LABSYM && sym->sec!=NULL)
val += secoffs[sym->sec->idx];
return val;
}
static int write_reloc68k(atom *a,rlist *rl,int signedval,taddr val)
{
nreloc *nrel;
char *p;
if (rl->type > LAST_STANDARD_RELOC) {
unsupp_reloc_error(rl);
return 0;
}
nrel = (nreloc *)rl->reloc;
if (field_overflow(signedval,nrel->size,val)) {
output_atom_error(12,a,rl->type,(unsigned long)nrel->mask,nrel->sym->name,
(unsigned long)nrel->addend,nrel->size);
return 0;
}
if (a->type == DATA)
p = a->content.db->data + nrel->byteoffset;
else if (a->type == SPACE)
p = (char *)a->content.sb->fill; /* @@@ ignore offset completely? */
else
return 1;
setbits(1,p,(nrel->bitoffset+nrel->size+7)&~7,nrel->bitoffset,nrel->size,val);
return 1;
}
static void do_relocs(taddr pc,atom *a)
/* Try to resolve all relocations in a DATA or SPACE atom.
Very simple implementation which can only handle basic 68k relocs. */
{
int rcnt = 0;
section *sec;
rlist *rl;
if (a->type == DATA)
rl = a->content.db->relocs;
else if (a->type == SPACE)
rl = a->content.sb->relocs;
else
rl = NULL;
while (rl) {
switch (rl->type) {
case REL_SD:
checkdefined(((nreloc *)rl->reloc)->sym);
write_reloc68k(a,rl,1,
(xfile_sym_value(((nreloc *)rl->reloc)->sym)
+ nreloc_real_addend(rl->reloc)) - sdabase);
break;
case REL_PC:
checkdefined(((nreloc *)rl->reloc)->sym);
write_reloc68k(a,rl,1,
(xfile_sym_value(((nreloc *)rl->reloc)->sym)
+ nreloc_real_addend(rl->reloc)) -
(pc + ((nreloc *)rl->reloc)->byteoffset));
break;
case REL_ABS:
rcnt++;
checkdefined(((nreloc *)rl->reloc)->sym);
sec = ((nreloc *)rl->reloc)->sym->sec;
if (!write_reloc68k(a,rl,0,
secoffs[sec?sec->idx:0] +
((nreloc *)rl->reloc)->addend))
break; /* field overflow */
if (((nreloc *)rl->reloc)->size == 32)
break; /* only support 32-bit absolute */
default:
unsupp_reloc_error(rl);
break;
}
if (a->type == SPACE)
break; /* all SPACE relocs are identical, one is enough */
rl = rl->next;
}
if (rcnt > max_relocs_per_atom)
max_relocs_per_atom = rcnt;
}
static void xfile_writesection(FILE *f,section *sec,taddr sec_align)
{
if (sec) {
utaddr pc = secoffs[sec->idx];
utaddr npc;
atom *a;
for (a=sec->first; a; a=a->next) {
npc = fwpcalign(f,a,sec,pc);
do_relocs(npc,a);
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 size_t xfile_symboltable(FILE *f,symbol *sym)
/* The symbol table only contains expression (absolute) and label symbols,
but doesn't differentiate between local and global scope. */
{
static int labtype[] = { XSYM_TEXT,XSYM_DATA,XSYM_BSS };
size_t len = 0;
char *p;
for (; sym; sym=sym->next) {
if (!(sym->flags & VASMINTERN)
&& (sym->type==LABSYM || sym->type==EXPRESSION)) {
if (sym->flags & WEAK)
output_error(10,sym->name); /* weak symbol not supported */
/* symbol type */
if (sym->type == LABSYM)
fw16(f,labtype[sym->sec->idx],1);
else /* EXPRESSION */
fw16(f,XSYM_ABS,1);
/* symbol value */
fw32(f,xfile_sym_value(sym),1);
len += 6; /* 2 bytes type, 4 bytes value */
/* symbol name - 16-bit aligned */
p = sym->name;
do {
fw8(f,*p);
len++;
} while (*p++);
if (len & 1) {
fw8(f,0);
len++;
}
}
}
return len;
}
static int offscmp(const void *offs1,const void *offs2)
{
return *(int *)offs1 - *(int *)offs2;
}
static size_t xfile_writerelocs(FILE *f,section *sec)
{
int *sortoffs = mymalloc(max_relocs_per_atom*sizeof(int));
size_t sz = 0;
if (sec) {
utaddr pc = secoffs[sec->idx];
utaddr npc;
atom *a;
rlist *rl;
for (a=sec->first; a; a=a->next) {
int nrel = 0;
npc = pcalign(a,pc);
if (a->type == DATA)
rl = a->content.db->relocs;
else if (a->type == SPACE)
rl = a->content.sb->relocs;
else
rl = NULL;
while (rl) {
if (rl->type==REL_ABS && ((nreloc *)rl->reloc)->size==32)
sortoffs[nrel++] = ((nreloc *)rl->reloc)->byteoffset;
rl = rl->next;
}
if (nrel) {
int i;
/* first sort the atom's relocs */
if (nrel > 1)
qsort(sortoffs,nrel,sizeof(int),offscmp);
/* write distances in bytes between them */
for (i=0; i<nrel; i++) {
/* determine 16bit distance to next relocation */
utaddr newoffs = npc + sortoffs[i];
taddr diff = newoffs - lastoffs;
if (diff < 0) {
ierror(0);
}
else if (diff > 0xffff) {
/* write a large distance >= 64K */
fw16(f,1,1);
fw32(f,diff,1);
sz += 6;
}
else {
fw16(f,diff,1);
sz += 2;
}
lastoffs = newoffs;
}
}
pc = npc + atom_size(a,sec,npc);
}
}
myfree(sortoffs);
return sz;
}
static void write_output(FILE *f,section *sec,symbol *sym)
{
size_t relocsz,syminfsz;
xfile_initwrite(sec,sym);
max_relocs_per_atom = 1;
xfile_header(f,secsize[_TEXT],secsize[_DATA],secsize[_BSS]);
xfile_writesection(f,sections[_TEXT],SECT_ALIGN);
xfile_writesection(f,sections[_DATA],SECT_ALIGN);
relocsz = xfile_writerelocs(f,sections[_TEXT]);
relocsz += xfile_writerelocs(f,sections[_DATA]);
syminfsz = no_symbols ? 0 : xfile_symboltable(f,sym);
/* finally patch reloc- and symbol-table size into the header */
fseek(f,offsetof(XFILE,x_relocsz),SEEK_SET);
fw32(f,relocsz,1);
fseek(f,offsetof(XFILE,x_syminfsz),SEEK_SET);
fw32(f,syminfsz,1);
}
static int output_args(char *p)
{
#if 0
if (!strncmp(p,"-startoffs=",11)) {
startoffs = atoi(p+11);
return 1;
}
#endif
return 0;
}
int init_output_xfile(char **cp,void (**wo)(FILE *,section *,symbol *),
int (**oa)(char *))
{
*cp = copyright;
*wo = write_output;
*oa = output_args;
return 1;
}
#else
int init_output_xfile(char **cp,void (**wo)(FILE *,section *,symbol *),
int (**oa)(char *))
{
return 0;
}
#endif