pci: don't do sanity check for missing pci bus, the check can misfire.
[minix.git] / commands / dis88 / dismain.c
blobd317f0062ec192cc8668dc097ae752d095c242fc
1 static char *sccsid = "@(#) dismain.c, Ver. 2.1 created 00:00:00 87/09/01";
3 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
4 * *
5 * Copyright (C) 1987 G. M. Harding, all rights reserved *
6 * *
7 * Permission to copy and redistribute is hereby granted, *
8 * provided full source code, with all copyright notices, *
9 * accompanies any redistribution. *
10 * *
11 * This file contains the source code for the machine- *
12 * independent portions of a disassembler program to run *
13 * in a Unix (System III) environment. It expects, as its *
14 * input, a file in standard a.out format, optionally con- *
15 * taining symbol table information. If a symbol table is *
16 * present, it will be used in the disassembly; otherwise, *
17 * all address references will be literal (absolute). *
18 * *
19 * The disassembler program was originally written for an *
20 * Intel 8088 CPU. However, all details of the actual CPU *
21 * architecture are hidden in three machine-specific files *
22 * named distabs.c, dishand.c, and disfp.c (the latter *
23 * file is specific to the 8087 numeric co-processor). The *
24 * code in this file is generic, and should require mini- *
25 * mal revision if a different CPU is to be targeted. If a *
26 * different version of Unix is to be targeted, changes to *
27 * this file may be necessary, and if a completely differ- *
28 * ent OS is to be targeted, all bets are off. *
29 * *
30 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
32 #include "dis.h" /* Disassembler declarations */
34 extern char *release; /* Contains release string */
35 static char *IFILE = NULL; /* Points to input file name */
36 static char *OFILE = NULL; /* Points to output file name */
37 static char *PRG; /* Name of invoking program */
38 static unsigned long zcount; /* Consecutive "0" byte count */
39 int objflg = 0; /* Flag: output object bytes */
41 #define unix 1
42 #define i8086 1
43 #define ibmpc 1
45 #if unix && i8086 && ibmpc /* Set the CPU identifier */
46 static int cpuid = 1;
47 #else
48 static int cpuid = 0;
49 #endif
51 _PROTOTYPE(static void usage, (char *s ));
52 _PROTOTYPE(static void fatal, (char *s, char *t ));
53 _PROTOTYPE(static void zdump, (unsigned long beg ));
54 _PROTOTYPE(static void prolog, (void));
55 _PROTOTYPE(static void distext, (void));
56 _PROTOTYPE(static void disdata, (void));
57 _PROTOTYPE(static void disbss, (void));
59 _PROTOTYPE(static char *invoker, (char *s));
60 _PROTOTYPE(static int objdump, (char *c));
61 _PROTOTYPE(static char *getlab, (int type));
62 _PROTOTYPE(static void prolog, (void));
64 /* * * * * * * MISCELLANEOUS UTILITY FUNCTIONS * * * * * * */
66 static void
67 usage(s)
68 register char *s;
70 fprintf(stderr,"Usage: %s [-o] ifile [ofile]\n",s);
71 exit(-1);
74 static void
75 fatal(s,t)
76 register char *s, *t;
78 fprintf(stderr,"\07%s: %s\n",s,t);
79 exit(-1);
82 static void
83 zdump(beg)
84 unsigned long beg;
86 beg = PC - beg;
87 if (beg > 1L)
88 printf("\t.zerow\t%ld\n",(beg >> 1));
89 if (beg & 1L)
90 printf("\t.byte\t0\n");
93 static char *
94 invoker(s)
95 register char *s;
97 register int k;
99 k = strlen(s);
101 while (k--)
102 if (s[k] == '/')
104 s += k;
105 ++s;
106 break;
109 return (s);
112 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
114 * This rather tricky routine supports the disdata() func- *
115 * tion. Its job is to output the code for a sequence of *
116 * data bytes whenever the object buffer is full, or when *
117 * a symbolic label is to be output. However, it must also *
118 * keep track of consecutive zero words so that lengthy *
119 * stretches of null data can be compressed by the use of *
120 * an appropriate assembler pseudo-op. It does this by *
121 * setting and testing a file-wide flag which counts suc- *
122 * cessive full buffers of null data. The function returns *
123 * a logical TRUE value if it outputs anything, logical *
124 * FALSE otherwise. (This enables disdata() to determine *
125 * whether to output a new synthetic label when there is *
126 * no symbol table.) *
128 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
130 static int
131 objdump(c)
133 register char *c;
135 {/* * * * * * * * * * START OF objdump() * * * * * * * * * */
137 register int k;
138 int retval = 0;
140 if (objptr == OBJMAX)
142 for (k = 0; k < OBJMAX; ++k)
143 if (objbuf[k])
144 break;
145 if (k == OBJMAX)
147 zcount += k;
148 objptr = 0;
149 if (c == NULL)
150 return (retval);
154 if (zcount)
156 printf("\t.zerow\t%ld\n",(zcount >> 1));
157 ++retval;
158 zcount = 0L;
161 if (objptr)
163 printf("\t.byte\t");
164 ++retval;
166 else
167 return (retval);
169 for (k = 0; k < objptr; ++k)
171 printf("0x%02.2x",objbuf[k]);
172 if (k < (objptr - 1))
173 putchar(',');
174 else
175 putchar('\n');
178 objptr = 0;
180 return (retval);
182 }/* * * * * * * * * * END OF objdump() * * * * * * * * * */
184 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
186 * This routine, called at the beginning of the input *
187 * cycle for each object byte, and before any interpreta- *
188 * tion is attempted, searches the symbol table for any *
189 * symbolic name with a value corresponding to the cur- *
190 * rent PC and a type corresponding to the segment type *
191 * (i.e., text, data, or bss) specified by the function's *
192 * argument. If any such name is found, a pointer to it is *
193 * returned; otherwise, a NULL pointer is returned. *
195 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
197 static char *
198 getlab(type)
199 register int type;
200 {/* * * * * * * * * * START OF getlab() * * * * * * * * * */
202 register int k;
203 static char b[32], c[10];
205 if (symptr < 0)
206 if ((type == N_TEXT)
207 || ((type == N_DATA) && ( ! objptr ) && ( ! zcount )))
209 if (type == N_TEXT)
210 sprintf(b,"T%05.5lx:",PC);
211 else
212 sprintf(b,"D%05.5lx:",PC);
213 return (b);
215 else
216 return (NULL);
218 for (k = 0; k <= symptr; ++k)
219 if ((symtab[k].n_value == PC)
220 && ((symtab[k].n_sclass & N_SECT) == type))
222 sprintf(b,"%s:\n",getnam(k));
223 if (objflg && (type != N_TEXT))
224 sprintf(c,"| %05.5lx\n",PC);
225 strcat(b,c);
226 return (b);
229 return (NULL);
231 }/* * * * * * * * * * * END OF getlab() * * * * * * * * * * */
233 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
235 * This routine performs a preliminary scan of the symbol *
236 * table, before disassembly begins, and outputs declara- *
237 * tions of globals and constants. *
239 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
241 static void
242 prolog()
244 {/* * * * * * * * * * START OF prolog() * * * * * * * * * */
246 register int j, flag;
248 if (symptr < 0)
249 return;
251 for (j = flag = 0; j <= symptr; ++j)
252 if ((symtab[j].n_sclass & N_CLASS) == C_EXT)
253 if (((symtab[j].n_sclass & N_SECT) > N_UNDF)
254 && ((symtab[j].n_sclass & N_SECT) < N_COMM))
256 char *c = getnam(j);
257 printf("\t.globl\t%s",c);
258 if (++flag == 1)
260 putchar('\t');
261 if (strlen(c) < 8)
262 putchar('\t');
263 printf("| Internal global\n");
265 else
266 putchar('\n');
268 else
269 if (symtab[j].n_value)
271 char *c = getnam(j);
272 printf("\t.comm\t%s,0x%08.8lx",c,
273 symtab[j].n_value);
274 if (++flag == 1)
275 printf("\t| Internal global\n");
276 else
277 putchar('\n');
280 if (flag)
281 putchar('\n');
283 for (j = flag = 0; j <= relptr; ++j)
284 if (relo[j].r_symndx < S_BSS)
286 char *c = getnam(relo[j].r_symndx);
287 ++flag;
288 printf("\t.globl\t%s",c);
289 putchar('\t');
290 if (strlen(c) < 8)
291 putchar('\t');
292 printf("| Undef: %05.5lx\n",relo[j].r_vaddr);
295 if (flag)
296 putchar('\n');
298 for (j = flag = 0; j <= symptr; ++j)
299 if ((symtab[j].n_sclass & N_SECT) == N_ABS)
301 char *c = getnam(j);
302 printf("%s=0x%08.8lx",c,symtab[j].n_value);
303 if (++flag == 1)
305 printf("\t\t");
306 if (strlen(c) < 5)
307 putchar('\t');
308 printf("| Literal\n");
310 else
311 putchar('\n');
314 if (flag)
315 putchar('\n');
317 }/* * * * * * * * * * * END OF prolog() * * * * * * * * * * */
319 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
321 * This function is responsible for disassembly of the *
322 * object file's text segment. *
324 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
326 static void
327 distext()
329 {/* * * * * * * * * * START OF distext() * * * * * * * * * */
331 char *c;
332 register int j;
333 register void (*f)();
335 for (j = 0; j < (int)(HDR.a_hdrlen); ++j)
336 getchar();
338 printf("| %s, %s\n\n",PRG,release);
340 printf("| @(");
342 printf("#)\tDisassembly of %s",IFILE);
344 if (symptr < 0)
345 printf(" (no symbols)\n\n");
346 else
347 printf("\n\n");
349 if (HDR.a_flags & A_EXEC)
350 printf("| File is executable\n\n");
352 if (HDR.a_flags & A_SEP)
354 printf("| File has split I/D space, and may have\n");
355 printf("| extraneous instructions in text segment\n\n");
358 prolog();
360 printf("\t.text\t\t\t| loc = %05.5lx, size = %05.5lx\n\n",
361 PC,HDR.a_text);
363 segflg = 0;
365 for (PC = 0L; PC < HDR.a_text; ++PC)
367 j = getchar() & 0xff;
368 if ((j == 0) && ((PC + 1L) == HDR.a_text))
370 ++PC;
371 break;
373 if ((c = getlab(N_TEXT)) != NULL)
374 printf("%s",c);
375 f = optab[j].func;
376 (*f)(j);
379 }/* * * * * * * * * * END OF distext() * * * * * * * * * */
381 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
383 * This function handles the object file's data segment. *
384 * There is no good way to disassemble a data segment, be- *
385 * cause it is impossible to tell, from the object code *
386 * alone, what each data byte refers to. If it refers to *
387 * an external symbol, the reference can be resolved from *
388 * the relocation table, if there is one. However, if it *
389 * refers to a static symbol, it cannot be distinguished *
390 * from numeric, character, or other pointer data. In some *
391 * cases, one might make a semi-educated guess as to the *
392 * nature of the data, but such guesses are inherently *
393 * haphazard, and they are bound to be wrong a good por- *
394 * tion of the time. Consequently, the data segment is *
395 * disassembled as a byte stream, which will satisfy no *
396 * one but which, at least, will never mislead anyone. *
398 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
400 static void
401 disdata()
403 {/* * * * * * * * * * START OF disdata() * * * * * * * * * */
405 register char *c;
406 register int j;
407 unsigned long end;
409 putchar('\n');
411 if (HDR.a_flags & A_SEP)
413 PC = 0L;
414 end = HDR.a_data;
416 else
417 end = HDR.a_text + HDR.a_data;
419 printf("\t.data\t\t\t| loc = %05.5lx, size = %05.5lx\n\n",
420 PC,HDR.a_data);
422 segflg = 0;
424 for (objptr = 0, zcount = 0L; PC < end; ++PC)
426 if ((c = getlab(N_DATA)) != NULL)
428 objdump(c);
429 printf("%s",c);
431 if (objptr >= OBJMAX)
432 if (objdump(NULL) && (symptr < 0))
433 printf("D%05.5lx:",PC);
434 j = getchar() & 0xff;
435 objbuf[objptr++] = j;
438 objdump("");
440 }/* * * * * * * * * * END OF disdata() * * * * * * * * * */
442 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
444 * This function handles the object file's bss segment. *
445 * Disassembly of the bss segment is easy, because every- *
446 * thing in it is zero by definition. *
448 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
450 static void disbss()
452 {/* * * * * * * * * * START OF disbss() * * * * * * * * * */
454 register int j;
455 register char *c;
456 unsigned long beg, end;
458 putchar('\n');
460 if (HDR.a_flags & A_SEP)
461 end = HDR.a_data + HDR.a_bss;
462 else
463 end = HDR.a_text + HDR.a_data + HDR.a_bss;
465 printf("\t.bss\t\t\t| loc = %05.5lx, size = %05.5lx\n\n",
466 PC,HDR.a_bss);
468 segflg = 0;
470 for (beg = PC; PC < end; ++PC)
471 if ((c = getlab(N_BSS)) != NULL)
473 if (PC > beg)
475 zdump(beg);
476 beg = PC;
478 printf("%s",c);
481 if (PC > beg)
482 zdump(beg);
484 }/* * * * * * * * * * * END OF disbss() * * * * * * * * * * */
486 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
488 * This is the program entry point. The command line is *
489 * searched for an input file name, which must be present. *
490 * An optional output file name is also permitted; if none *
491 * is found, standard output is the default. One command- *
492 * line option is available: "-o", which causes the pro- *
493 * gram to include object code in comments along with its *
494 * mnemonic output. *
496 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
498 void
499 main(argc,argv)
501 int argc; /* Command-line args from OS */
502 register char **argv;
504 {/* * * * * * * * * * * START OF main() * * * * * * * * * * */
506 char a[1024];
507 register int fd;
508 long taboff, tabnum;
509 long reloff, relnum;
511 PRG = invoker(*argv);
513 while (*++argv != NULL) /* Process command-line args */
514 if (**argv == '-')
515 switch (*++*argv)
517 case 'o' :
518 if (*++*argv)
519 usage(PRG);
520 else
521 ++objflg;
522 break;
523 default :
524 usage(PRG);
526 else
527 if (IFILE == NULL)
528 IFILE = *argv;
529 else if (OFILE == NULL)
530 OFILE = *argv;
531 else
532 usage(PRG);
534 if (IFILE == NULL)
535 usage(PRG);
536 else
537 if ((fd = open(IFILE,0)) < 0)
539 sprintf(a,"can't access input file %s",IFILE);
540 fatal(PRG,a);
543 if (OFILE != NULL)
544 if (freopen(OFILE,"w",stdout) == NULL)
546 sprintf(a,"can't open output file %s",OFILE);
547 fatal(PRG,a);
550 if ( ! cpuid )
551 fprintf(stderr,"\07%s: warning: host/cpu clash\n",PRG);
553 read(fd, (char *) &HDR,sizeof(struct exec));
555 if (BADMAG(HDR))
557 sprintf(a,"input file %s not in object format",IFILE);
558 fatal(PRG,a);
561 if (HDR.a_cpu != A_I8086)
563 sprintf(a,"%s is not an 8086/8088 object file",IFILE);
564 fatal(PRG,a);
567 if (HDR.a_hdrlen <= A_MINHDR)
568 HDR.a_trsize = HDR.a_drsize = 0L;
569 HDR.a_tbase = HDR.a_dbase = 0L;
570 /* AST emergency patch
571 HDR.a_lnums = HDR.a_toffs = 0L;
574 reloff = HDR.a_text /* Compute reloc data offset */
575 + HDR.a_data
576 + (long)(HDR.a_hdrlen);
578 relnum =
579 (HDR.a_trsize + HDR.a_drsize) / sizeof(struct reloc);
581 taboff = reloff /* Compute name table offset */
582 + HDR.a_trsize
583 + HDR.a_drsize;
585 tabnum = HDR.a_syms / sizeof(struct nlist);
587 if (relnum > MAXSYM)
588 fatal(PRG,"reloc table overflow");
590 if (tabnum > MAXSYM)
591 fatal(PRG,"symbol table overflow");
593 if (relnum) /* Get reloc data */
594 if (lseek(fd,reloff,0) != reloff)
595 fatal(PRG,"lseek error");
596 else
598 for (relptr = 0; relptr < relnum; ++relptr)
599 read(fd, (char *) &relo[relptr],sizeof(struct reloc));
600 relptr--;
603 if (tabnum) /* Read in symtab */
604 if (lseek(fd,taboff,0) != taboff)
605 fatal(PRG,"lseek error");
606 else
608 for (symptr = 0; symptr < tabnum; ++symptr)
609 read(fd, (char *) &symtab[symptr],sizeof(struct nlist));
610 symptr--;
612 else
613 fprintf(stderr,"\07%s: warning: no symbols\n",PRG);
615 close(fd);
617 if (freopen(IFILE,"r",stdin) == NULL)
619 sprintf(a,"can't reopen input file %s",IFILE);
620 fatal(PRG,a);
623 distext();
625 disdata();
627 disbss();
629 exit(0);
631 }/* * * * * * * * * * * END OF main() * * * * * * * * * * */