/* DLX Generator. Based heavily on the DXE generator. */ #include #include #include #include #include "dlx.h" //#define __HACKME // uncomment to allow relative symbols void exit_cleanup(void) { remove("dlx__tmp.o"); } class datastor { protected: void* dat; long len; public: datastor() { dat=malloc(0); len=0; } void write(void* d2, long l2) { dat=realloc(dat, len+l2); memcpy(dat+len, d2, l2); len+=l2; } void* data() { return(dat); } long length() { return(len); } }; int main(int argc, char **argv) { int errors = 0; unsigned bss_start = 0; FILHDR fh; FILE *input_f, *output_f; SCNHDR sc; char *data, *strings; SYMENT *sym; RELOC *relocs; int strsz, i; dlxheader dh; datastor imports; if (argc < 3) { printf("Usage: dlxgen output.dlx input.o [input2.o ... -lgcc -lc]\n"); return 1; } input_f = fopen(argv[2], "rb"); if (!input_f) { perror(argv[2]); return 1; } fread(&fh, 1, FILHSZ, input_f); if (fh.f_nscns != 1 || argc > 3) { char command[1024]; fclose(input_f); strcpy(command,"ld -X -S -r -o dlx__tmp.o -L"); strcat(command,getenv("DJDIR")); strcat(command,"/lib "); for(i=2;argv[i];i++) { strcat(command,argv[i]); strcat(command," "); } strcat(command," -T dlx.ld "); printf("%s\n",command); i = system(command); if(i) return i; input_f = fopen("dlx__tmp.o", "rb"); if (!input_f) { perror("dlx__tmp.o"); return 1; } else atexit(exit_cleanup); fread(&fh, 1, FILHSZ, input_f); if (fh.f_nscns != 1) { printf("Error: input file has more than one section; use -M for map\n"); return 1; } } fseek(input_f, fh.f_opthdr, 1); fread(&sc, 1, SCNHSZ, input_f); dh.magic = DLX_MAGIC; dh.libmainpos = -1; dh.extablepos = -1; dh.libloadpos = -1; dh.prgsize = sc.s_size; dh.numrelocs = sc.s_nreloc; dh.numimports = 0; data = (char *)malloc(sc.s_size); fseek(input_f, sc.s_scnptr, 0); fread(data, 1, sc.s_size, input_f); sym = (SYMENT *)malloc(sizeof(SYMENT)*fh.f_nsyms); fseek(input_f, fh.f_symptr, 0); fread(sym, fh.f_nsyms, SYMESZ, input_f); fread(&strsz, 1, 4, input_f); strings = (char *)malloc(strsz); fread(strings+4, 1, strsz-4, input_f); strings[0] = 0; relocs = (RELOC *)malloc(sizeof(RELOC)*sc.s_nreloc); fseek(input_f, sc.s_relptr, 0); fread(relocs, sc.s_nreloc, RELSZ, input_f); for (i=0; i