1994-06-24 14:02:31 +00:00
|
|
|
/* $Id$ */
|
1985-03-01 10:30:23 +00:00
|
|
|
/*
|
1987-03-10 01:26:51 +00:00
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
1985-03-01 10:30:23 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
1987-01-30 20:48:31 +00:00
|
|
|
* Fortunately we sold our PMDS machine. Unfortunately, we also sold the
|
|
|
|
* documentation that came with it, so I don't know the a.out format
|
|
|
|
* of the machine. This program is written partly by guessing, and partly from
|
|
|
|
* an older conversion program for the PMDS, which put the whole program
|
|
|
|
* in data. The produced object file does not contain a namelist.
|
1985-03-01 10:30:23 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1987-01-30 20:48:31 +00:00
|
|
|
#include <out.h>
|
|
|
|
|
|
|
|
#define ASSERT(x) switch (2) { case 0: case (x): ; }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Header and section table of new format object file.
|
|
|
|
*/
|
|
|
|
struct outhead outhead;
|
|
|
|
struct outsect outsect[S_MAX];
|
1985-03-01 10:30:23 +00:00
|
|
|
|
1987-01-30 20:48:31 +00:00
|
|
|
char *output_file;
|
|
|
|
int outputfile_created;
|
|
|
|
|
|
|
|
int rom_in_data;
|
|
|
|
|
|
|
|
char *program ;
|
1985-03-01 10:30:23 +00:00
|
|
|
|
|
|
|
char flag ;
|
|
|
|
|
1987-01-30 20:48:31 +00:00
|
|
|
#define writef(a, b, c) fwrite((a), (b), (int)(c), output)
|
|
|
|
|
|
|
|
/* Output file definitions and such */
|
|
|
|
|
1985-03-01 10:30:23 +00:00
|
|
|
|
1987-01-30 20:48:31 +00:00
|
|
|
#define ENTRY 0
|
|
|
|
#define HDRSIZE 40
|
1985-03-01 10:30:23 +00:00
|
|
|
|
1987-01-30 20:48:31 +00:00
|
|
|
char hdr[HDRSIZE];
|
|
|
|
|
|
|
|
|
|
|
|
#define TEXTSG 0
|
|
|
|
#define ROMSG 1
|
|
|
|
#define DATASG 2
|
|
|
|
#define BSSSG 3
|
|
|
|
#define LSECT BSSSG+1
|
|
|
|
#define NSECT LSECT+1
|
|
|
|
|
|
|
|
FILE *output;
|
|
|
|
|
1987-11-04 10:41:32 +00:00
|
|
|
long align(a,b)
|
|
|
|
long a,b;
|
|
|
|
{
|
|
|
|
a += b - 1;
|
|
|
|
return a - a % b;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
follows(pa, pb)
|
|
|
|
register struct outsect *pa, *pb;
|
|
|
|
{
|
|
|
|
/* return 1 if pa follows pb */
|
|
|
|
|
|
|
|
return pa->os_base == align(pb->os_base+pb->os_size, pa->os_lign);
|
|
|
|
}
|
|
|
|
|
1987-01-30 20:48:31 +00:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
register int nsect;
|
|
|
|
long magic ;
|
|
|
|
long textsize ;
|
|
|
|
long datasize ;
|
|
|
|
long bsssize;
|
|
|
|
long symstart;
|
|
|
|
extern long ftell();
|
|
|
|
|
|
|
|
output = stdout;
|
|
|
|
program= argv[0] ;
|
1985-03-01 10:30:23 +00:00
|
|
|
if ( argc>1 && argv[1][0]=='-' ) {
|
|
|
|
flag=argv[1][1] ;
|
|
|
|
argc-- ; argv++ ;
|
|
|
|
}
|
1987-01-30 20:48:31 +00:00
|
|
|
switch (argc) {
|
2022-07-17 10:58:48 +00:00
|
|
|
case 3: if ((output = fopen(argv[2], "wb")) == (FILE *)0)
|
1987-01-30 20:48:31 +00:00
|
|
|
fatal("Can't write %s.\n", argv[2]);
|
|
|
|
output_file = argv[2];
|
|
|
|
outputfile_created = 1;
|
|
|
|
if (! rd_open(argv[1]))
|
|
|
|
fatal("Can't read %s.\n", argv[1]);
|
|
|
|
break;
|
|
|
|
default:fatal("Usage: %s <ACK object> <Mantra object>.\n", argv[0]);
|
1985-03-01 10:30:23 +00:00
|
|
|
}
|
1987-01-30 20:48:31 +00:00
|
|
|
rd_ohead(&outhead);
|
|
|
|
if (BADMAGIC(outhead))
|
|
|
|
fatal("Not an ack object file.\n");
|
|
|
|
if (outhead.oh_flags & HF_LINK)
|
|
|
|
fatal("Contains unresolved references.\n");
|
|
|
|
if (outhead.oh_nrelo > 0)
|
|
|
|
fprintf(stderr, "Warning: relocation information present.\n");
|
|
|
|
if ( outhead.oh_nsect!=LSECT && outhead.oh_nsect!=NSECT )
|
|
|
|
fatal("Input file must have %d sections, not %ld\n",
|
|
|
|
NSECT,outhead.oh_nsect) ;
|
|
|
|
rd_sect(outsect, outhead.oh_nsect);
|
|
|
|
/* A few checks */
|
|
|
|
if ( outsect[TEXTSG].os_base != ENTRY)
|
|
|
|
fatal("text must start at %d not at 0x%lx\n", ENTRY,
|
|
|
|
outsect[TEXTSG].os_base) ;
|
|
|
|
if ( outsect[BSSSG].os_flen != 0 )
|
|
|
|
fatal("bss space contains initialized data\n") ;
|
1987-11-04 10:41:32 +00:00
|
|
|
if (! follows(&outsect[BSSSG], &outsect[DATASG]))
|
1987-01-30 20:48:31 +00:00
|
|
|
fatal("bss segment must follow data segment\n") ;
|
|
|
|
if ( outsect[ROMSG].os_lign == 0x8000 ) {
|
|
|
|
/* 410 file with ROMSG in data space */
|
|
|
|
rom_in_data = 1;
|
|
|
|
magic= 0410 ;
|
1987-11-04 10:41:32 +00:00
|
|
|
if (! follows(&outsect[DATASG], &outsect[ROMSG]))
|
|
|
|
fatal("data segment must follow rom\n") ;
|
1987-01-30 20:48:31 +00:00
|
|
|
textsize= outsect[TEXTSG].os_size ;
|
1987-11-04 10:41:32 +00:00
|
|
|
outsect[ROMSG].os_size = outsect[DATASG].os_base - outsect[ROMSG].os_base;
|
|
|
|
outsect[DATASG].os_size = outsect[BSSSG].os_base - outsect[DATASG].os_base;
|
1987-01-30 20:48:31 +00:00
|
|
|
datasize= outsect[ROMSG].os_size + outsect[DATASG].os_size ;
|
|
|
|
} else
|
|
|
|
if ( outsect[DATASG].os_lign == 0x8000 ) {
|
|
|
|
/* 410 file with ROMSG in instruction space */
|
|
|
|
rom_in_data = 0;
|
|
|
|
magic= 0410 ;
|
1987-11-04 10:41:32 +00:00
|
|
|
if (! follows(&outsect[ROMSG], &outsect[TEXTSG]))
|
|
|
|
fatal("rom segment must follow text\n") ;
|
|
|
|
outsect[TEXTSG].os_size = outsect[ROMSG].os_base - outsect[TEXTSG].os_base;
|
1987-01-30 20:48:31 +00:00
|
|
|
textsize= outsect[TEXTSG].os_size + outsect[ROMSG].os_size ;
|
1987-11-04 10:41:32 +00:00
|
|
|
outsect[DATASG].os_size = outsect[BSSSG].os_base - outsect[DATASG].os_base;
|
1987-01-30 20:48:31 +00:00
|
|
|
datasize= outsect[DATASG].os_size ;
|
|
|
|
} else {
|
|
|
|
/* Plain 407 file */
|
|
|
|
rom_in_data = 1;
|
|
|
|
magic= 0407 ;
|
1987-11-04 10:41:32 +00:00
|
|
|
if (! follows(&outsect[ROMSG], &outsect[TEXTSG]))
|
1987-01-30 20:48:31 +00:00
|
|
|
fatal("rom segment must follow text\n") ;
|
1987-11-04 10:41:32 +00:00
|
|
|
if (! follows(&outsect[DATASG], &outsect[ROMSG]))
|
1987-01-30 20:48:31 +00:00
|
|
|
fatal("data segment must follow rom\n") ;
|
1987-11-04 10:41:32 +00:00
|
|
|
outsect[TEXTSG].os_size = outsect[ROMSG].os_base - outsect[TEXTSG].os_base;
|
|
|
|
outsect[ROMSG].os_size = outsect[DATASG].os_base - outsect[ROMSG].os_base;
|
|
|
|
outsect[DATASG].os_size = outsect[BSSSG].os_base - outsect[DATASG].os_base;
|
|
|
|
textsize= outsect[TEXTSG].os_size ;
|
|
|
|
datasize= outsect[ROMSG].os_size + outsect[DATASG].os_size ;
|
1985-03-01 10:30:23 +00:00
|
|
|
}
|
1987-01-30 20:48:31 +00:00
|
|
|
bsssize = outsect[BSSSG].os_size;
|
|
|
|
if ( outhead.oh_nsect==NSECT ) {
|
1987-11-04 10:41:32 +00:00
|
|
|
if (! follows(&outsect[LSECT], &outsect[BSSSG]))
|
1987-01-30 20:48:31 +00:00
|
|
|
fatal("end segment must follow bss\n") ;
|
|
|
|
if ( outsect[LSECT].os_size != 0 )
|
|
|
|
fatal("end segment must be empty\n") ;
|
1985-03-01 10:30:23 +00:00
|
|
|
}
|
1987-01-30 20:48:31 +00:00
|
|
|
|
|
|
|
/* Action at last */
|
|
|
|
fseek(output,(long) HDRSIZE,0);
|
|
|
|
emits(&outsect[TEXTSG]) ;
|
|
|
|
emits(&outsect[ROMSG]) ;
|
|
|
|
emits(&outsect[DATASG]) ;
|
|
|
|
fseek(output,0L,0);
|
|
|
|
sethdr(0, (long) magic);
|
|
|
|
sethdr(4, textsize);
|
|
|
|
sethdr(8, datasize);
|
|
|
|
sethdr(12, bsssize);
|
|
|
|
writef(hdr, 1, 40);
|
|
|
|
if ( ferror(output) ) {
|
|
|
|
fatal("output write error\n") ;
|
1985-03-01 10:30:23 +00:00
|
|
|
}
|
1987-01-30 20:48:31 +00:00
|
|
|
if ( outputfile_created ) chmod(argv[2],0755);
|
1985-03-01 10:30:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1987-01-30 20:48:31 +00:00
|
|
|
sethdr(off,val) long val ; {
|
|
|
|
hdr[off] = val>>24 ;
|
|
|
|
hdr[off+1] = val>>16 ;
|
|
|
|
hdr[off+2] = val>>8 ;
|
|
|
|
hdr[off+3] = val ;
|
1985-03-01 10:30:23 +00:00
|
|
|
}
|
|
|
|
|
1987-01-30 20:48:31 +00:00
|
|
|
/*
|
|
|
|
* Transfer the emitted byted from one file to another.
|
|
|
|
*/
|
|
|
|
emits(section) struct outsect *section ; {
|
|
|
|
char *p;
|
|
|
|
char *calloc();
|
|
|
|
long length = section->os_size;
|
1985-03-01 10:30:23 +00:00
|
|
|
|
1987-01-30 20:48:31 +00:00
|
|
|
rd_outsect(section - outsect);
|
|
|
|
while (section->os_size > 0) {
|
|
|
|
int i = section->os_size > 10*1024 ? 10*1024 : section->os_size;
|
|
|
|
|
|
|
|
section->os_size -= i;
|
|
|
|
if (!(p = calloc(i, 1))) {
|
|
|
|
fatal("No memory.\n");
|
|
|
|
}
|
|
|
|
rd_emit(p, i >= section->os_flen ? section->os_flen:(long) i);
|
|
|
|
section->os_flen -= i;
|
|
|
|
writef(p, 1, i);
|
|
|
|
free(p);
|
1985-03-01 10:30:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1987-01-30 20:48:31 +00:00
|
|
|
/* VARARGS1 */
|
|
|
|
fatal(s, a1, a2)
|
|
|
|
char *s;
|
|
|
|
{
|
|
|
|
fprintf(stderr,"%s: ",program) ;
|
|
|
|
fprintf(stderr, s, a1, a2);
|
|
|
|
if (outputfile_created)
|
|
|
|
unlink(output_file);
|
|
|
|
exit(-1);
|
1985-03-01 10:30:23 +00:00
|
|
|
}
|
1987-01-30 20:48:31 +00:00
|
|
|
|
|
|
|
rd_fatal() { fatal("read error.\n"); }
|