buildtask.c
9.53 KB
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
/**************************************************************************
* *
* Copyright (C) 1994, Silicon Graphics, Inc. *
* *
* These coded instructions, statements, and computer programs contain *
* unpublished proprietary information of Silicon Graphics, Inc., and *
* are protected by Federal copyright law. They may not be disclosed *
* to third parties or copied or duplicated in any form, in whole or *
* in part, without the prior written consent of Silicon Graphics, Inc. *
* *
*************************************************************************/
/*
* File: buildtask.c
* Creator: hsa@sgi.com
* Create Date: Fri Dec 23 13:32:56 PST 1994
*
* This program is a simple linker for RSP tasks. It concatenates
* objects compiled by rspasm, as well as their data segments, and
* builds an object table in DMEM (actual usage of this information
* is completely up to the ucode).
*
* The table in DMEM looks like this:
*
* offset size dest
* number of bits: 32 16 16
*
* So each object entry is 2 words.
*
* The offset is 32 bits because it is convenient for the ucode
* to compute the proper DRAM address once and write it back.
*
* (the ucode is responsible for filling in the dest)
*
* Dest is 16 bits because IMEM is only 4K.
*
* By convention, the first object listed should be the 'main'
* module.
*/
#include <stdio.h>
#define PROG_NAME "buildtask"
#define BUILDTASK_VERSION "1.0"
#define usage() fprintf(stderr,"usage : %s [-d off] [-o outfile] [-s skiplen] [-l len] object0 [-s skiplen] [-l len] object1 ...\n",PROG_NAME);
char *obj_names[256];
int obj_skip[256];
int obj_len[256];
char *obj_prefile[256];
char output_name[256];
char tmp_name[256];
char line[256];
int dmem_files = -1;
/* .dat array: */
unsigned long int dmem[1024];
int data_ptr = 0, obj_ptr = 0;
typedef struct
{
int offset;
int size;
} table_td;
table_td obj_table[256];
int
main(int argc, char *argv[])
{
FILE *in, *out, *sym, *dbg;
int i, j, obj_count = 0, dmem_offset = 0, obj_size,argsdone=0;
unsigned long int val, *ptr;
char c;
strcpy(output_name, "a.out");
fprintf(stderr,"%s : RSP Linker, Version %s, created %s %s\n",
PROG_NAME, BUILDTASK_VERSION, __DATE__, __TIME__);
while ((argc > 1) && (argv[1][0] == '-')) {
switch(argv[1][1]) {
case 's':
case 'l':
case 'p':
argsdone=1;
break;
case 'd':
dmem_offset = strtol(argv[2], NULL, 0);
argc--;
argv++;
break;
case 'o':
strcpy(output_name, argv[2]);
argc--;
argv++;
break;
case 'f':
dmem_files = strtol(argv[2], NULL, 0);
argc--;
argv++;
break;
default:
fprintf(stderr,"%s : option [%s] not recognized.\n",
PROG_NAME, argv[1]);
usage();
break;
}
if (argsdone) break;
argc--;
argv++;
}
if (argc < 2) {
fprintf(stderr,"%s : no objects specified.\n",PROG_NAME);
usage();
exit(-1);
}
obj_skip[obj_count]=0;
obj_len[obj_count]=-1;
obj_prefile[obj_count]=NULL;
for (i= 1; i<argc; i++) {
if (strcmp(argv[i], "-s")==0) {
if (i+2>=argc) {
fprintf(stderr,"%s : -s option must precede object name",PROG_NAME);
usage();
exit(-1);
}
i++;
obj_skip[obj_count] = strtol(argv[i], NULL, 0);
continue;
}
if (strcmp(argv[i], "-l")==0) {
if (i+2>=argc) {
fprintf(stderr,"%s : -l option must precede object name",PROG_NAME);
usage();
exit(-1);
}
i++;
obj_len[obj_count] = strtol(argv[i], NULL, 0);
continue;
}
if (strcmp(argv[i], "-p")==0) {
if (i+2>=argc) {
fprintf(stderr,"%s : -l option must precede object name",PROG_NAME);
usage();
exit(-1);
}
i++;
obj_prefile[obj_count] = argv[i];
continue;
}
if (strcmp(output_name, argv[i])==0) {
fprintf(stderr,
"%s : output file [%s] same as input file [%s].\n",
"buildtask", output_name, argv[i]);
usage();
exit(-1);
}
obj_names[obj_count] = (char *) malloc(strlen(argv[i]) + 2);
if (obj_names[obj_count] == NULL) {
fprintf(stderr,"%s : can't alloc memory.\n",PROG_NAME);
exit(-1);
}
strcpy(obj_names[obj_count++], argv[i]);
obj_skip[obj_count]=0;
obj_len[obj_count]=-1;
}
if (obj_count<1) {
fprintf(stderr,"%s : no objects specified(2).\n",PROG_NAME);
usage();
exit(-1);
}
if (obj_skip[obj_count]!=0 || obj_len[obj_count]!=-1 ||
obj_prefile[obj_count]!=NULL) {
fprintf(stderr,"%s : -s,-l,and -p must precede object name.\n",
PROG_NAME);
usage();
exit(-1);
}
fprintf(stderr,"%s : building task [%s] from objects:\n",
PROG_NAME, output_name);
for (i=0; i<obj_count; i++)
fprintf(stderr,"\t...loading object [%s]\n",obj_names[i]);
fprintf(stderr,"%s : building DMEM object table at offset %08x:\n",
PROG_NAME, dmem_offset);
/*
* open output files.
*/
if ((out=fopen(output_name,"w")) == NULL) {
fprintf(stderr,"%s : cannot open output file [%s]\n",
PROG_NAME, output_name);
usage();
exit(-1);
}
sprintf(tmp_name, "%s.sym", output_name);
if ((sym=fopen(tmp_name,"w")) == NULL) {
fprintf(stderr,"%s : cannot open .sym file [%s]\n",
PROG_NAME, tmp_name);
usage();
exit(-1);
}
sprintf(tmp_name, "%s.dbg", output_name);
if ((dbg=fopen(tmp_name,"w")) == NULL) {
fprintf(stderr,"%s : cannot open .dbg file [%s]\n",
PROG_NAME, tmp_name);
usage();
exit(-1);
}
/*
* for each object:
*/
for (i=0; i<obj_count; i++) {
/*
* update the offset to the object table:
*/
obj_table[i].offset = obj_ptr;
/*
* copy each object to the output file, determining the length:
*/
obj_size = 0;
if (obj_prefile[i]) {
/*
* open input object file.
*/
if ((in=fopen(obj_prefile[i],"r")) == NULL) {
fprintf(stderr,"%s : cannot open input file [%s]\n",
PROG_NAME, obj_prefile[i]);
exit(-1);
}
fread(&val, sizeof(unsigned long int), 1, in);
while (!feof(in)) {
fwrite(&val, sizeof(unsigned long int), 1, out);
obj_size += 4;
obj_ptr += 4;
fread(&val, sizeof(unsigned long int), 1, in);
}
fclose(in);
fprintf(stderr,"prefile=%s ",obj_prefile[i]);
}
fprintf(stderr,"skip=%d len=%d\n",obj_skip[i],obj_len[i]);
/*
* open input object file.
*/
if ((in=fopen(obj_names[i],"r")) == NULL) {
fprintf(stderr,"%s : cannot open input file [%s]\n",
PROG_NAME, obj_names[i]);
exit(-1);
}
fread(&val, sizeof(unsigned long int), 1, in);
while (!feof(in)) {
if (obj_skip[i] <= 0 && obj_len[i]!=0) {
fwrite(&val, sizeof(unsigned long int), 1, out);
obj_size += 4;
obj_ptr += 4;
obj_len[i]--;
} else {
obj_skip[i]--;
}
fread(&val, sizeof(unsigned long int), 1, in);
}
fclose(in);
/* must align to 64-bits for DMA transfer */
if (obj_ptr & 0x04) {
val = 0x0;
fwrite(&val, sizeof(unsigned long int), 1, out);
obj_ptr += 4;
obj_size += 4;
}
/*
* update the object size to the object table:
*/
obj_table[i].size = obj_size - 1;
/*
* copy the data into DMEM file:
*/
sprintf(tmp_name, "%s.dat", obj_names[i]);
if (dmem_files<0 || i<dmem_files) {
if ((in=fopen(tmp_name,"r")) == NULL) {
fprintf(stderr,"%s : cannot open input data file [%s]\n",
PROG_NAME, tmp_name);
} else {
fprintf(stderr,"\t...copying data [%s]\n",tmp_name);
/*
* allow for empty .dat files.
*/
fread(&val, sizeof(unsigned long int), 1, in);
while (!feof(in)) {
dmem[data_ptr] = val;
data_ptr++;
if (data_ptr > 1024) data_ptr=1024;
fread(&val, sizeof(unsigned long int), 1, in);
}
fclose(in);
}
}
/*
* copy the symbols into .sym file:
*/
sprintf(tmp_name, "%s.sym", obj_names[i]);
if ((in=fopen(tmp_name,"r")) == NULL) {
fprintf(stderr,"%s : cannot open input .sym file [%s]\n",
PROG_NAME, tmp_name);
} else {
while (fgets(line, 80, in) != NULL) {
fprintf(sym,"%s",line);
}
fclose(in);
}
/*
* copy the symbols into .dbg file:
*/
sprintf(tmp_name, "%s.dbg", obj_names[i]);
if ((in=fopen(tmp_name,"r")) == NULL) {
fprintf(stderr,"%s : cannot open input .dbg file [%s]\n",
PROG_NAME, tmp_name);
} else {
while (fgets(line, 80, in) != NULL) {
fprintf(dbg,"%s",line);
}
fclose(in);
}
}
fclose(out);
fclose(sym);
fclose(dbg);
/*
* update offset tables in DMEM.
*
* The table in DMEM looks like this:
*
* offset size dest
* number of bits: 32 16 16
*
* So each object is 2 words.
*
* (the ucode is responsible for filling in the dest)
*
*/
for (i=0; i<obj_count; i++) {
ptr = (unsigned long int *) &(dmem[(dmem_offset >> 2) + (i*2)]);
*ptr = obj_table[i].offset;
ptr++;
*ptr &= 0x0000ffff;
*ptr |= (obj_table[i].size << 16);
}
/*
* write out DMEM file:
*/
/*
* open output file.
*/
sprintf(tmp_name, "%s.dat", output_name);
if ((out=fopen(tmp_name,"w")) == NULL) {
fprintf(stderr,"%s : cannot open data output file [%s]\n",
PROG_NAME, tmp_name);
exit(-1);
}
fwrite(&(dmem[0]), sizeof(unsigned long int), data_ptr, out);
fclose(out);
}