-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy patht_os9.c
448 lines (362 loc) · 11.8 KB
/
t_os9.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
/* $VER: vlink t_os9.c V0.16h (16.01.21)
*
* This file is part of vlink, a portable linker for multiple
* object formats.
* Copyright (c) 1997-2021 Frank Wille
*/
#include "config.h"
#ifdef OS_9
#define T_OS9_C
#include "vlink.h"
#include "os9mod.h"
static void init(struct GlobalVars *,int);
static int options(struct GlobalVars *,int,const char **,int *);
static int identify(struct GlobalVars *,char *,uint8_t *,unsigned long,bool);
static void readconv(struct GlobalVars *,struct LinkFile *);
static int targetlink(struct GlobalVars *,struct LinkedSection *,
struct Section *);
static unsigned long hdrsize_6809(struct GlobalVars *);
static void writeobject(struct GlobalVars *,FILE *);
static void writeshared(struct GlobalVars *,FILE *);
static void writeexec_6809(struct GlobalVars *,FILE *);
struct FFFuncs fff_os9_6809 = {
"os9-6809",
defaultscript,
NULL,
init,
options,
hdrsize_6809,
identify,
readconv,
NULL,
targetlink,
NULL,
NULL,
NULL,
NULL,NULL,NULL,
writeobject,
writeshared,
writeexec_6809,
NULL,NULL,
0,
0x7ffe,
0,
0,
RTAB_STANDARD,RTAB_STANDARD,
_BIG_ENDIAN_,
16,0,
FFF_BASEINCR|FFF_NOFILE
};
/* symbol labeling the module name */
static const char modname_sym[] = "__modname";
/* precalculated CRC table */
static uint32_t *crctab;
/* options */
static bool os9noshare; /* non-shareable module */
static int os9mem,os9rev; /* module header settings */
static const char *os9name; /* module name */
static void init(struct GlobalVars *gv,int mode)
/* create an artificial object containing the module name */
{
if (mode == FFINI_DESTFMT) {
if (gv->ldscript == NULL)
gv->pcrel_ctors = TRUE; /* pcrel-con/destructors for default script */
}
else if (mode==FFINI_RESOLVE && gv->dest_name!=NULL &&
findsymbol(gv,NULL,modname_sym,0)==NULL) {
size_t modnamelen = strlen(os9name?os9name:base_name(gv->dest_name));
char *modname = alloc(modnamelen);
struct ObjectUnit *ou;
struct Section *sec;
/* make module name from destination file name */
strncpy(modname,os9name?os9name:base_name(gv->dest_name),modnamelen);
modname[modnamelen-1] |= 0x80; /* set bit 7 on last character */
/* create artificial object with a __MODNAME section for the name */
ou = art_objunit(gv,"MODULE",(uint8_t *)modname,modnamelen);
sec = add_section(ou,"__MODNAME",(uint8_t *)modname,modnamelen,ST_CODE,
SF_ALLOC,SP_READ|SP_EXEC,0,TRUE);
/* add a symbol labeling the module name */
addsymbol(gv,sec,modname_sym,NULL,0,SYM_RELOC,0,SYMI_OBJECT,
SYMB_GLOBAL,modnamelen,FALSE);
/* enqueue artificial object unit into linking process */
ou->lnkfile->type = ID_OBJECT;
add_objunit(gv,ou,FALSE);
}
}
static int options(struct GlobalVars *gv,int argc,const char *argv[],int *i)
{
if (!strncmp(argv[*i],"-os9-mem=",9)) {
int last = strlen(argv[*i]) - 1;
long mem;
sscanf(&argv[*i][9],"%li",&mem);
if (argv[*i][last]=='k' || argv[*i][last]=='K')
mem <<= 10; /* value is in KBytes instead of Bytes */
os9mem = mem>256 ? mem : 256;
}
else if (!strncmp(argv[*i],"-os9-name=",10)) {
os9name = &argv[*i][10];
}
else if (!strcmp(argv[*i],"-os9-ns")) {
os9noshare = TRUE;
}
else if (!strncmp(argv[*i],"-os9-rev=",9)) {
long rev;
sscanf(&argv[*i][9],"%li",&rev);
if (rev<0 || rev>15)
error(130,argv[*i]); /* bad assignment */
else
os9rev = rev;
}
else return 0;
return 1;
}
/*****************************************************************/
/* Read OS9 */
/*****************************************************************/
static int identify(struct GlobalVars *gv,char *name,uint8_t *p,
unsigned long plen,bool lib)
/* identify an OS9 object or module */
{
return ID_UNKNOWN; /* @@@ no read-support at the moment */
}
static void readconv(struct GlobalVars *gv,struct LinkFile *lf)
{
ierror("readconv(): Can't read OS9 files");
}
/*****************************************************************/
/* Link OS9 module */
/*****************************************************************/
static int targetlink(struct GlobalVars *gv,struct LinkedSection *ls,
struct Section *s)
/* returns 1, if target requires the combination of the two sections, */
/* returns -1, if target doesn't want to combine them, */
/* returns 0, if target doesn't care - standard linking rules are used. */
{
/* OS9 requires that all sections of type CODE or DATA or BSS */
/* will be combined, because there are only those three available! */
if (ls->type == s->type)
return 1;
return 0;
}
/*****************************************************************/
/* Write OS9 modules and objects */
/*****************************************************************/
static uint16_t modname_address(struct GlobalVars *gv,uint16_t defval)
{
struct Symbol *sym = findsymbol(gv,NULL,modname_sym,0);
if (sym == NULL)
ierror("%s symbol disappeared",modname_sym);
return sym->relsect->lnksec!=NULL ? (uint16_t)sym->value : defval;
}
static void checkPIC(struct LinkedSection *ls)
/* check that section has position independent code, no absolute relocs */
{
#if 0 /* @@@ cannot be checked easily, as ABS,y must be allowed! */
struct Reloc *r;
for (r=(struct Reloc *)ls->relocs.first; r->n.next!=NULL;
r=(struct Reloc *)r->n.next) {
if (r->rtype==R_ABS || r->rtype==R_UABS) {
}
}
#endif
}
static void writedatarefs(FILE *f,struct list *refs,unsigned cnt)
{
struct Reloc *rel;
fwrite16be(f,cnt);
while (rel = (struct Reloc *)remhead(refs)) {
fwrite16be(f,rel->offset);
cnt--;
}
if (cnt != 0)
ierror("data refcnt doesn't match");
}
static uint8_t header_parity_check(void *hdr,int len)
{
#if 0
uint8_t c,r=0,*p=hdr;
int i,j;
for (i=0; i<len; i++) {
c = *p++;
for (j=0; j<8; j++) {
r += !(c & 1);
c >>= 1;
}
}
return r;
#else
uint8_t c=0,*p=hdr;
int i;
for (i=0; i<len; i++)
c ^= *p++;
return ~c;
#endif
}
static void crc24_init(uint32_t poly)
{
uint32_t crc,c;
int i,j;
crctab = alloc(sizeof(uint32_t)*256);
if (crctab == NULL)
ierror("crc24_init: out of memory");
for (i=0; i<256; i++) {
crc = 0;
c = (uint32_t)i << 16;
for (j=0; j<8; j++) {
if ((crc ^ c) & 0x800000L)
crc = (crc << 1) ^ poly;
else
crc <<= 1;
c <<= 1;
}
crctab[i] = crc;
}
}
static void crc24(uint32_t poly,uint8_t *dest,uint8_t *buf,size_t len)
{
uint32_t crc = 0xffffff;
size_t i;
crc24_init(poly);
for (i=0; i<len; i++)
crc = (crc<<8) ^ crctab[((crc>>16) ^ (uint32_t)buf[i]) & 0xff];
crc ^= 0xffffff;
dest[0] = (crc>>16) & 0xff;
dest[1] = (crc>>8) & 0xff;
dest[2] = crc & 0xff;
}
static unsigned long hdrsize_6809(struct GlobalVars *gv)
{
return sizeof(mh6809);
}
static void writeshared(struct GlobalVars *gv,FILE *f)
{
error(30); /* Target file format doesn't support shared objects */
}
static void writeobject(struct GlobalVars *gv,FILE *f)
/* creates an OS9 relocatable object file */
{
ierror("OS9 object file generation has not yet been implemented");
}
static void writeexec_6809(struct GlobalVars *gv,FILE *f)
/* creates an OS9/6809 module (which is position independent and reentrant) */
{
struct list dtrefs,ddrefs;
unsigned dtrefcnt,ddrefcnt;
struct LinkedSection *ls;
struct Reloc *rel,*nextrel;
struct RelocInsert *ri;
lword rmask=makemask(16);
size_t initdata_size,bss_size,stk_size,file_size;
uint16_t entryoffs;
unsigned long lma;
uint8_t *buf,crc[3];
mh6809 hdr;
/* create output file */
if ((f = fopen(gv->dest_name,"wb")) == NULL) {
error(29,gv->dest_name); /* Can't create output file */
return;
}
entryoffs = entry_address(gv);
stk_size = os9mem ? os9mem : OS9_6809_DEFSTK;
/* determine size of initialized data */
for (ls=(struct LinkedSection *)gv->lnksec.first, initdata_size=bss_size=0;
ls->n.next!=NULL; ls=(struct LinkedSection *)ls->n.next) {
if (ls->type != ST_CODE) {
if ((ls->flags & SF_ALLOC) && !(ls->ld_flags & LSF_NOLOAD))
initdata_size += ls->size;
else
bss_size += ls->size;
}
}
/* write position-independent code sections, including module header */
lma = 0;
while ((ls=load_next_section(gv))!=NULL && ls->type==ST_CODE) {
if (lma==0 && ls->copybase<sizeof(mh6809))
error(137,(unsigned)ls->copybase,sizeof(6809)); /* not enough space */
if (ls->copybase > lma)
fwritegap(gv,f,ls->copybase-lma);
lma = ls->copybase + ls->size;
checkPIC(ls);
calc_relocs(gv,ls);
fwritefullsect(gv,f,ls);
}
/* write size of initialized data */
fwrite16be(f,initdata_size);
/* write initialized data sections, which are copied at startup */
initlist(&dtrefs);
initlist(&ddrefs);
dtrefcnt = ddrefcnt = 0;
if (ls != NULL) do {
if (ls->size>0 && (ls->flags & SF_ALLOC) && !(ls->ld_flags & LSF_NOLOAD)) {
if (ls->type == ST_CODE)
error(136,ls->name); /* executable section in data segment */
if (ls->copybase > lma)
fwritegap(gv,f,ls->copybase-lma);
lma = ls->copybase + ls->size;
/* move data-text and data-data relocations into their own list */
rel = (struct Reloc *)ls->relocs.first;
while (nextrel = (struct Reloc *)rel->n.next) {
if (rel->rtype==R_ABS || rel->rtype==R_UABS) {
/* only byte-aligned 16-bit relocs are allowed */
if ((ri=rel->insert)==NULL || ri->bpos!=0 || ri->bsiz!=16
|| (ri->mask & rmask)!=rmask)
error(32,fff_os9_6809.tname,reloc_name[rel->rtype],
(int)rel->insert->bpos,(int)rel->insert->bsiz,
mtaddr(gv,rel->insert->mask),ls->name,rel->offset);
remnode(&rel->n);
writesection(gv,ls->data,rel->offset,rel,
rel->relocsect.lnk->base+rel->addend);
rel->offset += (lword)ls->base; /* offset relative to whole segment */
if (rel->relocsect.lnk->type==ST_CODE) {
addtail(&dtrefs,&rel->n);
dtrefcnt++;
}
else {
addtail(&ddrefs,&rel->n);
ddrefcnt++;
}
}
rel = nextrel;
}
/* execute remaining relocs and write initialized data sections */
calc_relocs(gv,ls);
fwritefullsect(gv,f,ls);
}
} while (ls = load_next_section(gv));
/* write data-text and data-data references */
writedatarefs(f,&dtrefs,dtrefcnt);
writedatarefs(f,&ddrefs,ddrefcnt);
file_size = ftell(f);
/* prepare the module header */
write16be(hdr.m_sync,OS9_6809_SYNC);
write16be(hdr.m_size,file_size+3); /* including CRC, yet to be written */
write16be(hdr.m_name,modname_address(gv,sizeof(mh6809)));
hdr.m_tylan = 0x11; /* program module with 6809 code */
hdr.m_attrev = (os9noshare?0:0x80) + (os9rev&15);
hdr.m_parity = header_parity_check(&hdr,offsetof(mh6809,m_parity));
write16be(hdr.m_exec,entryoffs);
write16be(hdr.m_data,initdata_size+bss_size+stk_size);
/* write the module header and close the output file */
fseek(f,0,SEEK_SET);
fwritex(f,&hdr,sizeof(mh6809));
fclose(f);
/* read it again for CRC generation */
if ((f = fopen(gv->dest_name,"rb+")) == NULL) {
error(8,gv->dest_name); /* Cannot open output file */
return;
}
buf = alloc(file_size);
if (fread(buf,1,file_size,f) == file_size) {
/* do 24-bit CRC for OS-9 and append it */
fpos_t pos;
fgetpos(f,&pos);
crc24(0x800063,crc,buf,file_size);
fsetpos(f,&pos); /* Needed to write after reading acc. to ISO-C! */
fwritex(f,crc,3);
}
else
error(7,gv->dest_name);
free(buf);
fclose(f);
}
#endif