-
Notifications
You must be signed in to change notification settings - Fork 9
/
output_bin.c
330 lines (288 loc) · 8.84 KB
/
output_bin.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
/* output_bin.c binary output driver for vasm */
/* (c) in 2002-2009,2013-2023 by Volker Barthelmann and Frank Wille */
#include "vasm.h"
#ifdef OUTBIN
static char *copyright="vasm binary output module 2.2 (c) 2002-2023 Volker Barthelmann and Frank Wille";
enum {
BINFMT_RAW, /* no header */
BINFMT_CBMPRG, /* Commodore VIC-20/C-64 PRG format */
BINFMT_ATARICOM, /* Atari 800 DOS COM format */
BINFMT_APPLEBIN, /* Apple DOS 3.3 binary file */
BINFMT_DRAGONBIN, /* Dragon DOS binary format */
BINFMT_COCOML, /* Tandy Color Computer machine lang. file */
BINFMT_ORICMC, /* Oric machine code file */
BINFMT_ORICMCX, /* Oric machine code file auto-exec */
BINFMT_FOENIXPGX, /* C256 Foenix single-section format */
BINFMT_FOENIXPGZ /* C256 Foenix multi-section format */
};
static int binfmt = BINFMT_RAW;
static char *exec_symname;
static taddr exec_addr;
static int orgcmp(const void *sec1,const void *sec2)
{
if (ULLTADDR((*(section **)sec1)->org) > ULLTADDR((*(section **)sec2)->org))
return 1;
if (ULLTADDR((*(section **)sec1)->org) < ULLTADDR((*(section **)sec2)->org))
return -1;
return 0;
}
static void write_output(FILE *f,section *sec,symbol *sym)
{
section *s,*s2,**seclist,**slp;
unsigned long long pc=0,npc;
size_t nsecs;
long hdroffs;
char *nptr;
atom *p;
if (!sec)
return;
for (; sym; sym=sym->next) {
if (sym->type == IMPORT)
output_error(6,sym->name); /* undefined symbol */
if (exec_symname!=NULL && !strcmp(exec_symname,sym->name)) {
exec_addr = sym->pc;
exec_symname = NULL; /* found the start-symbol */
}
}
if (exec_symname != NULL)
output_error(6,exec_symname); /* start-symbol not found */
/* we don't support overlapping sections */
for (s=sec,nsecs=0; s!=NULL; s=s->next) {
for (s2=s->next; s2; s2=s2->next) {
if (((ULLTADDR(s2->org) >= ULLTADDR(s->org) &&
ULLTADDR(s2->org) < ULLTADDR(s->pc)) ||
(ULLTADDR(s2->pc) > ULLTADDR(s->org) &&
ULLTADDR(s2->pc) <= ULLTADDR(s->pc))))
output_error(0);
}
nsecs++;
}
/* make an array of section pointers, sorted by their start address */
seclist = (section **)mymalloc(nsecs * sizeof(section *));
for (s=sec,slp=seclist; s!=NULL; s=s->next)
*slp++ = s;
if (nsecs > 1)
qsort(seclist,nsecs,sizeof(section *),orgcmp);
sec = *seclist; /* sec is now the first section in memory */
/* write an optional header first */
switch (binfmt) {
case BINFMT_APPLEBIN:
/* Apple DOS binary file header:
* 00-01: load address (little endian)
* 02-03: file length (little endian)
*/
fw16(f,sec->org,0);
hdroffs = ftell(f); /* remember location of file length */
fw16(f,0,0); /* skip file length, will be patched later */
break;
case BINFMT_ATARICOM:
/* ATARI COM header: $FFFF */
fw16(f,0xffff,0);
break;
case BINFMT_CBMPRG:
/* Commodore 6502 PRG header:
* 00: LSB of load address
* 01: MSB of load address
*/
fw16(f,sec->org,0);
break;
case BINFMT_DRAGONBIN:
/* Dragon DOS file header:
* 00: $55
* 01: filetype binary $02
* 02-03: load address (big endian)
* 04-05: file length (big endian)
* 06-07: execution address (big endian)
* 08: $AA
*/
fw16(f,0x5502,1);
fw16(f,sec->org,1);
hdroffs = ftell(f); /* remember location of file length */
fw16(f,0,1); /* skip file length, will be patched later */
fw16(f,exec_addr?exec_addr:sec->org,1);
fw8(f,0xaa);
break;
case BINFMT_FOENIXPGX:
/* C256 Foenix single-segment header PGX
* 00: "PGX"
* 03: $01 for 65816 CPU
* 04-07: load/execute address (little endian)
*/
fw32(f,0x50475801,1);
fw32(f,sec->org,0);
break;
case BINFMT_FOENIXPGZ:
/* C256 Foenix multi-segment header PGZ: "Z" followed by seg. headers */
fw8(f,'Z');
break;
case BINFMT_ORICMC:
case BINFMT_ORICMCX:
/* Oric machine code file header:
* 00-03: sync: $16,$16,$16,$16
* 04: end of sync: $24
* 05-06: two reserved bytes
* 07: file data type ($00 BASIC, $80 machine code)
* 08: how to execute ($80 RUN as BASIC, $c7 execute as machine code)
* 09-10: last address of data stored (big-endian)
* 11-12: first address of data stored (big-endian)
* 13: reserved
* 14- : null-terminated file name, maximum of 15 characters in name
*/
fw32(f,0x16161616,1);
fw8(f,0x24);
fw16(f,0,1);
fw8(f,0x80);
fw8(f,binfmt==BINFMT_ORICMCX?0xc7:0); /* auto-exec or not */
hdroffs = ftell(f); /* remember location of last address */
fw16(f,0,1); /* skip last address, will be patched later */
fw16(f,sec->org,1);
fw8(f,0);
nptr = outname;
while (*nptr) {
if (!stricmp(nptr,".tap") || (nptr-outname)>=15)
break; /* remove .tap extension, no more than 15 characters */
fw8(f,toupper((unsigned char)*nptr++));
}
fw8(f,0);
break;
}
for (slp=seclist; nsecs>0; nsecs--) {
s = *slp++;
/* strip uninitialized space atoms from section */
if (s->last)
s->last->next = NULL;
else
s->first = NULL;
/* write optional section header or pad to next section start */
switch (binfmt) {
case BINFMT_ATARICOM:
/* for each section
* 00-01: address of first byte (little endian)
* 02-03: address of last byte (little endian)
*/
fw16(f,s->org,0);
fw16(f,s->pc-1,0);
break;
case BINFMT_COCOML:
/* segment header with length and load address
* 00: $00
* 01-02: length of segment in bytes (big endian)
* 03-04: load address of segment (big endian)
*/
fw8(f,0);
fw16(f,s->pc-s->org,1);
fw16(f,s->org,1);
break;
case BINFMT_FOENIXPGZ:
/* 00-02: load-address of segment (little endian)
* 03-05: size of segment (little endian)
*/
fw24(f,s->org,0);
fw24(f,s->pc-s->org,0);
break;
default:
/* fill gap between sections with pad-bytes */
if (s!=seclist[0] && ULLTADDR(s->org) > pc)
fwalignpattern(f,ULLTADDR(s->org)-pc,s->pad,s->padbytes);
break;
}
/* write section contents */
for (p=s->first,pc=ULLTADDR(s->org); p; p=p->next) {
npc = ULLTADDR(fwpcalign(f,p,s,pc));
if (p->type == DATA)
fwdata(f,p->content.db->data,p->content.db->size);
else if (p->type == SPACE)
fwsblock(f,p->content.sb);
pc = npc + atom_size(p,s,npc);
}
}
/* patch the header or write trailer */
switch (binfmt) {
case BINFMT_APPLEBIN:
fseek(f,hdroffs,SEEK_SET);
fw16(f,pc-sec->org,0); /* total file length */
break;
case BINFMT_DRAGONBIN:
fseek(f,hdroffs,SEEK_SET);
fw16(f,pc-sec->org,1); /* total file length */
break;
case BINFMT_COCOML:
/* Color Computer machine language trailer
* 00-02: end of program token: $ff,$00,$00
* 03-04: execution address (big endian)
*/
fw8(f,0xff);
fw16(f,0,1);
fw16(f,exec_addr?exec_addr:sec->org,1);
break;
case BINFMT_FOENIXPGZ:
/* trailer with execute-address and a zero-size to indicate that
no more segments follow */
fw24(f,exec_addr?exec_addr:sec->org,0);
fw24(f,0,0);
break;
case BINFMT_ORICMC:
case BINFMT_ORICMCX:
fseek(f,hdroffs,SEEK_SET);
fw16(f,pc-1,1); /* last address of file */
break;
}
myfree(seclist);
}
static int output_args(char *p)
{
if (!strncmp(p,"-exec=",6)) {
exec_symname = p + 6;
return 1;
}
else if (!strcmp(p,"-apple-bin")) {
binfmt = BINFMT_APPLEBIN;
return 1;
}
else if (!strcmp(p,"-atari-com")) {
binfmt = BINFMT_ATARICOM;
return 1;
}
else if (!strcmp(p,"-cbm-prg")) {
binfmt = BINFMT_CBMPRG;
return 1;
}
else if (!strcmp(p,"-coco-ml")) {
binfmt = BINFMT_COCOML;
return 1;
}
else if (!strcmp(p,"-dragon-bin")) {
binfmt = BINFMT_DRAGONBIN;
return 1;
}
else if (!strcmp(p,"-foenix-pgx")) {
binfmt = BINFMT_FOENIXPGX;
return 1;
}
else if (!strcmp(p,"-foenix-pgz")) {
binfmt = BINFMT_FOENIXPGZ;
return 1;
}
else if (!strcmp(p,"-oric-mc")) {
binfmt = BINFMT_ORICMC;
return 1;
}
else if (!strcmp(p,"-oric-mcx")) {
binfmt = BINFMT_ORICMCX;
return 1;
}
return 0;
}
int init_output_bin(char **cp,void (**wo)(FILE *,section *,symbol *),int (**oa)(char *))
{
*cp = copyright;
*wo = write_output;
*oa = output_args;
return 1;
}
#else
int init_output_bin(char **cp,void (**wo)(FILE *,section *,symbol *),int (**oa)(char *))
{
return 0;
}
#endif