📄 dismain.c
字号:
static char *sccsid = "@(#) dismain.c, Ver. 2.1 created 00:00:00 87/09/01"; /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright (C) 1987 G. M. Harding, all rights reserved * * * * Permission to copy and redistribute is hereby granted, * * provided full source code, with all copyright notices, * * accompanies any redistribution. * * * * This file contains the source code for the machine- * * independent portions of a disassembler program to run * * in a Unix (System III) environment. It expects, as its * * input, a file in standard a.out format, optionally con- * * taining symbol table information. If a symbol table is * * present, it will be used in the disassembly; otherwise, * * all address references will be literal (absolute). * * * * The disassembler program was originally written for an * * Intel 8088 CPU. However, all details of the actual CPU * * architecture are hidden in three machine-specific files * * named distabs.c, dishand.c, and disfp.c (the latter * * file is specific to the 8087 numeric co-processor). The * * code in this file is generic, and should require mini- * * mal revision if a different CPU is to be targeted. If a * * different version of Unix is to be targeted, changes to * * this file may be necessary, and if a completely differ- * * ent OS is to be targeted, all bets are off. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */#include "dis.h" /* Disassembler declarations */extern char *release; /* Contains release string */static char *IFILE = NULL; /* Points to input file name */static char *OFILE = NULL; /* Points to output file name */static char *PRG; /* Name of invoking program */static unsigned long zcount; /* Consecutive "0" byte count */int objflg = 0; /* Flag: output object bytes */#define unix 1#define i8086 1#define ibmpc 1#if unix && i8086 && ibmpc /* Set the CPU identifier */static int cpuid = 1;#elsestatic int cpuid = 0;#endif_PROTOTYPE(static void usage, (char *s ));_PROTOTYPE(static void fatal, (char *s, char *t ));_PROTOTYPE(static void zdump, (unsigned long beg ));_PROTOTYPE(static void prolog, (void));_PROTOTYPE(static void distext, (void));_PROTOTYPE(static void disdata, (void));_PROTOTYPE(static void disbss, (void));_PROTOTYPE(static char *invoker, (char *s));_PROTOTYPE(static int objdump, (char *c));_PROTOTYPE(static char *getlab, (int type));_PROTOTYPE(static void prolog, (void)); /* * * * * * * MISCELLANEOUS UTILITY FUNCTIONS * * * * * * */static voidusage(s) register char *s;{ fprintf(stderr,"Usage: %s [-o] ifile [ofile]\n",s); exit(-1);}static voidfatal(s,t) register char *s, *t;{ fprintf(stderr,"\07%s: %s\n",s,t); exit(-1);}static voidzdump(beg) unsigned long beg;{ beg = PC - beg; if (beg > 1L) printf("\t.zerow\t%ld\n",(beg >> 1)); if (beg & 1L) printf("\t.byte\t0\n");}static char *invoker(s) register char *s;{ register int k; k = strlen(s); while (k--) if (s[k] == '/') { s += k; ++s; break; } return (s);} /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * This rather tricky routine supports the disdata() func- * * tion. Its job is to output the code for a sequence of * * data bytes whenever the object buffer is full, or when * * a symbolic label is to be output. However, it must also * * keep track of consecutive zero words so that lengthy * * stretches of null data can be compressed by the use of * * an appropriate assembler pseudo-op. It does this by * * setting and testing a file-wide flag which counts suc- * * cessive full buffers of null data. The function returns * * a logical TRUE value if it outputs anything, logical * * FALSE otherwise. (This enables disdata() to determine * * whether to output a new synthetic label when there is * * no symbol table.) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */static intobjdump(c) register char *c;{/* * * * * * * * * * START OF objdump() * * * * * * * * * */ register int k; int retval = 0; if (objptr == OBJMAX) { for (k = 0; k < OBJMAX; ++k) if (objbuf[k]) break; if (k == OBJMAX) { zcount += k; objptr = 0; if (c == NULL) return (retval); } } if (zcount) { printf("\t.zerow\t%ld\n",(zcount >> 1)); ++retval; zcount = 0L; } if (objptr) { printf("\t.byte\t"); ++retval; } else return (retval); for (k = 0; k < objptr; ++k) { printf("0x%02.2x",objbuf[k]); if (k < (objptr - 1)) putchar(','); else putchar('\n'); } objptr = 0; return (retval);}/* * * * * * * * * * END OF objdump() * * * * * * * * * */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * This routine, called at the beginning of the input * * cycle for each object byte, and before any interpreta- * * tion is attempted, searches the symbol table for any * * symbolic name with a value corresponding to the cur- * * rent PC and a type corresponding to the segment type * * (i.e., text, data, or bss) specified by the function's * * argument. If any such name is found, a pointer to it is * * returned; otherwise, a NULL pointer is returned. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */static char *getlab(type)register int type;{/* * * * * * * * * * START OF getlab() * * * * * * * * * */ register int k; static char b[32], c[10]; if (symptr < 0) if ((type == N_TEXT) || ((type == N_DATA) && ( ! objptr ) && ( ! zcount ))) { if (type == N_TEXT) sprintf(b,"T%05.5lx:",PC); else sprintf(b,"D%05.5lx:",PC); return (b); } else return (NULL); for (k = 0; k <= symptr; ++k) if ((symtab[k].n_value == PC) && ((symtab[k].n_sclass & N_SECT) == type)) { sprintf(b,"%s:\n",getnam(k)); if (objflg && (type != N_TEXT)) sprintf(c,"| %05.5lx\n",PC); strcat(b,c); return (b); } return (NULL);}/* * * * * * * * * * * END OF getlab() * * * * * * * * * * */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * This routine performs a preliminary scan of the symbol * * table, before disassembly begins, and outputs declara- * * tions of globals and constants. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */static voidprolog(){/* * * * * * * * * * START OF prolog() * * * * * * * * * */ register int j, flag; if (symptr < 0) return; for (j = flag = 0; j <= symptr; ++j) if ((symtab[j].n_sclass & N_CLASS) == C_EXT) if (((symtab[j].n_sclass & N_SECT) > N_UNDF) && ((symtab[j].n_sclass & N_SECT) < N_COMM)) { char *c = getnam(j); printf("\t.globl\t%s",c); if (++flag == 1) { putchar('\t'); if (strlen(c) < 8) putchar('\t'); printf("| Internal global\n"); } else putchar('\n'); } else if (symtab[j].n_value) { char *c = getnam(j); printf("\t.comm\t%s,0x%08.8lx",c, symtab[j].n_value); if (++flag == 1) printf("\t| Internal global\n"); else putchar('\n'); } if (flag) putchar('\n'); for (j = flag = 0; j <= relptr; ++j) if (relo[j].r_symndx < S_BSS) { char *c = getnam(relo[j].r_symndx); ++flag; printf("\t.globl\t%s",c); putchar('\t'); if (strlen(c) < 8) putchar('\t'); printf("| Undef: %05.5lx\n",relo[j].r_vaddr); } if (flag) putchar('\n'); for (j = flag = 0; j <= symptr; ++j) if ((symtab[j].n_sclass & N_SECT) == N_ABS) { char *c = getnam(j); printf("%s=0x%08.8lx",c,symtab[j].n_value); if (++flag == 1) { printf("\t\t"); if (strlen(c) < 5) putchar('\t'); printf("| Literal\n"); } else putchar('\n'); } if (flag) putchar('\n');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -