promize.c
来自「mips架构的bootloader,99左右的版本 但源代码现在没人更新了」· C语言 代码 · 共 137 行
C
137 行
#include <stdio.h>#include "mipscoff.h"#define swap(x,y) ((y==2)?byte_swap(x,y):word_swap(x,y))int debug = 0; /* debug switch */char *usage[] = { "usage: promize [-d] file", "This program is intended to permit programs to be executed under", "sable as if they are executing from PROM. The achieve this, the", "data section is appended to the text section, and the bss section", "length is zeroed. Only the optional (a.out) header is updated,", "which means that programs that read the coff section headers show", "no change to the file. The program is invoked with one or more", "executable coff files, that are modified in place.", "\t-d\tdebug mode", 0};main(argc,argv)int argc;char *argv[];{int i,j,num_files;FILE *fp;for (i=1;i<argc;i++) { if (argv[i][0] == '-') { for (j=1;argv[i][j] != 0;j++) { if (argv[i][j] == 'd') debug = 1; else { fprintf(stderr,"unrecognized option %c\n", argv[i][j]); for (j=0;usage[j];j++) fprintf(stderr,"%s\n",usage[j]); exit(-1); } } } else num_files++; }if (num_files == 0) do_file(stdin);else { for (i=1;i<argc;i++) { if (argv[i][0] != '-') { fp = fopen(argv[i],"r+"); if (! fp) { printf("can't open %s\n",argv[i]); exit(-1); } do_file(fp); fclose(fp); } } }exit(0);} do_file(fp)FILE *fp;{int leflag;unsigned long offset;struct filehdr fhdr;struct aouthdr ohdr;fread(&fhdr,sizeof(fhdr),1,fp);if (fhdr.f_magic == 061001) { /* little endian */ leflag = 1; swap(&fhdr.f_magic,sizeof(fhdr.f_magic)); swap(&fhdr.f_nscns,sizeof(fhdr.f_nscns)); swap(&fhdr.f_opthdr,sizeof(fhdr.f_opthdr)); }else leflag = 0;if (debug) printf("fhdrsz=%d magic=0%o nscns=%d ohdrsz=%d\n", sizeof(fhdr),fhdr.f_magic,fhdr.f_nscns,fhdr.f_opthdr);if (fhdr.f_magic != 0540 && fhdr.f_magic != 0542) { fprintf(stderr,"0%o Bad magic number\n",fhdr.f_magic); exit(1); }fread(&ohdr,sizeof(ohdr),1,fp);if (leflag) { swap(&ohdr.entry,sizeof(ohdr.entry)); swap(&ohdr.tsize,sizeof(ohdr.tsize)); swap(&ohdr.text_start,sizeof(ohdr.text_start)); swap(&ohdr.dsize,sizeof(ohdr.dsize)); swap(&ohdr.data_start,sizeof(ohdr.data_start)); }if (debug) printf("entry=%lx tsize=%lx tbase=%lx dsize=%lx dbase=%lx\n", ohdr.entry,ohdr.tsize,ohdr.text_start,ohdr.dsize,ohdr.data_start);ohdr.tsize += ohdr.dsize;ohdr.dsize = 0;ohdr.bsize = 0;offset = sizeof(fhdr);fseek(fp,offset,0);if (leflag) { swap(&ohdr.entry,sizeof(ohdr.entry)); swap(&ohdr.tsize,sizeof(ohdr.tsize)); swap(&ohdr.text_start,sizeof(ohdr.text_start)); swap(&ohdr.dsize,sizeof(ohdr.dsize)); swap(&ohdr.data_start,sizeof(ohdr.data_start)); }fwrite(&ohdr,sizeof(ohdr),1,fp);}word_swap(vp)unsigned long *vp;{unsigned short v1,v2;v1 = (*vp)>>16;byte_swap(&v1);v2 = (*vp)&0xffff;byte_swap(&v2);*vp = (((unsigned long)v2)<<16)+v1;}byte_swap(vp)unsigned short *vp;{*vp = ((*vp)<<8)+((*vp)>>8);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?