/* $Id$ */ /* * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". */ /* This is a special cv.c program for testing purposes ; * it converts to a stripped version of COFF */ #include typedef struct filehdr { unsigned short f_magic; /* magic number */ unsigned short f_nscns; /* number of sections */ long f_timdat; /* time & date stamp */ long f_symptr; /* file pointer to symtab */ long f_nsyms; /* number of symtab entries */ unsigned short f_opthdr; /* sizeof(optional hdr) */ unsigned short f_flags; /* flags */ } FILHDR; typedef struct aouthdr { short magic; short vstamp; long tsize; long dsize; long bsize; long entry; long text_start; long data_start; } AOUTHDR; typedef struct scnhdr { char s_name[8]; long s_paddr; long s_vaddr; long s_size; long s_scnptr; long s_relptr; long s_lnnoptr; unsigned short s_nreloc; unsigned short s_nlnno; long s_flags; } SCNHDR; #define AOUTHSZ sizeof(AOUTHDR) #define SCNHSZ sizeof(SCNHDR) #define FILHSZ sizeof(FILHDR) #include #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]; char *output_file; int outputfile_created; int rom_in_data; char *program ; char flag ; #define readf(a, b, c) fread((a), (b), (int)(c), input) #define writef(a, b, c) fwrite((a), (b), (int)(c), output) /* Output file definitions and such */ #define TS 0 #define DS 1 #define BS 2 #define NS 3 FILHDR filh; AOUTHDR aouth; SCNHDR scnh[NS]; #define ENTRY 0 #define MC68MAGIC 0520 #define MYFLAGS 01017 #define STYP_TEXT 0x20 #define STYP_DATA 0x40 #define STYP_BSS 0x80 #define _TEXT ".text" #define _DATA ".data" #define _BSS ".bss" #define TEXTSG 0 #define ROMSG 1 #define DATASG 2 #define BSSSG 3 #define LSECT BSSSG+1 #define NSECT LSECT+1 FILE *input; FILE *output; 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); } main(argc, argv) int argc; char *argv[]; { register int nsect; long magic ; long textsize, datasize, bsssize; extern long ftell(); long filepos; int scn; ASSERT(sizeof(struct outhead) == SZ_HEAD); ASSERT(sizeof(struct outsect) == SZ_SECT); input = stdin; output = stdout; program= argv[0] ; if ( argc>1 && argv[1][0]=='-' ) { flag=argv[1][1] ; argc-- ; argv++ ; } switch (argc) { case 1: break; case 3: if ((output = fopen(argv[2], "wb")) == (FILE *)0) fatal("Can't write %s.\n", argv[2]); output_file = argv[2]; outputfile_created = 1; /* FALLTHROUGH */ case 2: if ((input = fopen(argv[1], "rb")) == (FILE *)0) fatal("Can't read %s.\n", argv[1]); break; default:fatal("Usage: %s
.\n", argv[0]); } if ( !rhead(input,&outhead) ) fatal("Reading header failed.\n"); if (BADMAGIC(outhead)) fatal("Not an ack object file.\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) ; for ( nsect=0 ; nsectos_flen ; while (n > 0) { blk = n > BUFSIZ ? BUFSIZ : n; readf(buffer, 1, blk); writef(buffer, 1, blk); n -= blk; } if ((n=section->os_size - section->os_flen) > 0) { fseek(output, n-1, 1); writef("\0",1,1); } } rhead(f,head) struct outhead *head ; FILE *f ; { char buf[SZ_HEAD] ; if ( fread(buf,SZ_HEAD,1,f)!=1 ) return 0 ; iconvert(buf,(char *)head,SF_HEAD) ; return 1 ; } rsect(f,sect) struct outsect *sect ; FILE *f ; { char buf[SZ_SECT] ; if ( fread(buf,SZ_SECT,1,f)!=1 ) return 0 ; iconvert(buf,(char *)sect,SF_SECT) ; return 1 ; } iconvert(buf,str,fmt) char *buf, *str, *fmt ; { register char *nf, *ni, *no ; int last, i ; long value ; ni=buf ; no=str ; nf=fmt ; while ( last = *nf++ ) { last -= '0' ; if ( last<1 || last >9 ) fatal("illegal out.h format string\n"); value=0 ; i=last ; while ( i-- ) { value = (value<<8) + (ni[i]&0xFF) ; } switch ( last ) { case 0 : break ; case 1 : *no= value ; break ; case 2 : *(unsigned short *)no = value ; break ; case 4 : *(long *)no = value ; break ; default : fatal("illegal out.h format string\n"); } ni += last ; no += last ; } } /* VARARGS1 */ fatal(s, a1, a2) char *s; { fprintf(stderr,"%s: ",program) ; fprintf(stderr, s, a1, a2); if (outputfile_created) unlink(output_file); exit(-1); }