source: trunk/minix/commands/dis88/dismain.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 17.5 KB
Line 
1static char *sccsid = "@(#) dismain.c, Ver. 2.1 created 00:00:00 87/09/01";
2
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 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
31
32#include "dis.h" /* Disassembler declarations */
33
34extern char *release; /* Contains release string */
35static char *IFILE = NULL; /* Points to input file name */
36static char *OFILE = NULL; /* Points to output file name */
37static char *PRG; /* Name of invoking program */
38static unsigned long zcount; /* Consecutive "0" byte count */
39int objflg = 0; /* Flag: output object bytes */
40
41#define unix 1
42#define i8086 1
43#define ibmpc 1
44
45#if unix && i8086 && ibmpc /* Set the CPU identifier */
46static int cpuid = 1;
47#else
48static int cpuid = 0;
49#endif
50
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));
58
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));
63
64 /* * * * * * * MISCELLANEOUS UTILITY FUNCTIONS * * * * * * */
65
66static void
67usage(s)
68 register char *s;
69{
70 fprintf(stderr,"Usage: %s [-o] ifile [ofile]\n",s);
71 exit(-1);
72}
73
74static void
75fatal(s,t)
76 register char *s, *t;
77{
78 fprintf(stderr,"\07%s: %s\n",s,t);
79 exit(-1);
80}
81
82static void
83zdump(beg)
84 unsigned long beg;
85{
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");
91}
92
93static char *
94invoker(s)
95 register char *s;
96{
97 register int k;
98
99 k = strlen(s);
100
101 while (k--)
102 if (s[k] == '/')
103 {
104 s += k;
105 ++s;
106 break;
107 }
108
109 return (s);
110}
111
112 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
113 * *
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.) *
127 * *
128 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
129
130static int
131objdump(c)
132
133 register char *c;
134
135{/* * * * * * * * * * START OF objdump() * * * * * * * * * */
136
137 register int k;
138 int retval = 0;
139
140 if (objptr == OBJMAX)
141 {
142 for (k = 0; k < OBJMAX; ++k)
143 if (objbuf[k])
144 break;
145 if (k == OBJMAX)
146 {
147 zcount += k;
148 objptr = 0;
149 if (c == NULL)
150 return (retval);
151 }
152 }
153
154 if (zcount)
155 {
156 printf("\t.zerow\t%ld\n",(zcount >> 1));
157 ++retval;
158 zcount = 0L;
159 }
160
161 if (objptr)
162 {
163 printf("\t.byte\t");
164 ++retval;
165 }
166 else
167 return (retval);
168
169 for (k = 0; k < objptr; ++k)
170 {
171 printf("0x%02.2x",objbuf[k]);
172 if (k < (objptr - 1))
173 putchar(',');
174 else
175 putchar('\n');
176 }
177
178 objptr = 0;
179
180 return (retval);
181
182}/* * * * * * * * * * END OF objdump() * * * * * * * * * */
183
184 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
185 * *
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. *
194 * *
195 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
196
197static char *
198getlab(type)
199register int type;
200{/* * * * * * * * * * START OF getlab() * * * * * * * * * */
201
202 register int k;
203 static char b[32], c[10];
204
205 if (symptr < 0)
206 if ((type == N_TEXT)
207 || ((type == N_DATA) && ( ! objptr ) && ( ! zcount )))
208 {
209 if (type == N_TEXT)
210 sprintf(b,"T%05.5lx:",PC);
211 else
212 sprintf(b,"D%05.5lx:",PC);
213 return (b);
214 }
215 else
216 return (NULL);
217
218 for (k = 0; k <= symptr; ++k)
219 if ((symtab[k].n_value == PC)
220 && ((symtab[k].n_sclass & N_SECT) == type))
221 {
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);
227 }
228
229 return (NULL);
230
231}/* * * * * * * * * * * END OF getlab() * * * * * * * * * * */
232
233 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
234 * *
235 * This routine performs a preliminary scan of the symbol *
236 * table, before disassembly begins, and outputs declara- *
237 * tions of globals and constants. *
238 * *
239 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
240
241static void
242prolog()
243
244{/* * * * * * * * * * START OF prolog() * * * * * * * * * */
245
246 register int j, flag;
247
248 if (symptr < 0)
249 return;
250
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))
255 {
256 char *c = getnam(j);
257 printf("\t.globl\t%s",c);
258 if (++flag == 1)
259 {
260 putchar('\t');
261 if (strlen(c) < 8)
262 putchar('\t');
263 printf("| Internal global\n");
264 }
265 else
266 putchar('\n');
267 }
268 else
269 if (symtab[j].n_value)
270 {
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');
278 }
279
280 if (flag)
281 putchar('\n');
282
283 for (j = flag = 0; j <= relptr; ++j)
284 if (relo[j].r_symndx < S_BSS)
285 {
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);
293 }
294
295 if (flag)
296 putchar('\n');
297
298 for (j = flag = 0; j <= symptr; ++j)
299 if ((symtab[j].n_sclass & N_SECT) == N_ABS)
300 {
301 char *c = getnam(j);
302 printf("%s=0x%08.8lx",c,symtab[j].n_value);
303 if (++flag == 1)
304 {
305 printf("\t\t");
306 if (strlen(c) < 5)
307 putchar('\t');
308 printf("| Literal\n");
309 }
310 else
311 putchar('\n');
312 }
313
314 if (flag)
315 putchar('\n');
316
317}/* * * * * * * * * * * END OF prolog() * * * * * * * * * * */
318
319 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
320 * *
321 * This function is responsible for disassembly of the *
322 * object file's text segment. *
323 * *
324 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
325
326static void
327distext()
328
329{/* * * * * * * * * * START OF distext() * * * * * * * * * */
330
331 char *c;
332 register int j;
333 register void (*f)();
334
335 for (j = 0; j < (int)(HDR.a_hdrlen); ++j)
336 getchar();
337
338 printf("| %s, %s\n\n",PRG,release);
339
340 printf("| @(");
341
342 printf("#)\tDisassembly of %s",IFILE);
343
344 if (symptr < 0)
345 printf(" (no symbols)\n\n");
346 else
347 printf("\n\n");
348
349 if (HDR.a_flags & A_EXEC)
350 printf("| File is executable\n\n");
351
352 if (HDR.a_flags & A_SEP)
353 {
354 printf("| File has split I/D space, and may have\n");
355 printf("| extraneous instructions in text segment\n\n");
356 }
357
358 prolog();
359
360 printf("\t.text\t\t\t| loc = %05.5lx, size = %05.5lx\n\n",
361 PC,HDR.a_text);
362
363 segflg = 0;
364
365 for (PC = 0L; PC < HDR.a_text; ++PC)
366 {
367 j = getchar() & 0xff;
368 if ((j == 0) && ((PC + 1L) == HDR.a_text))
369 {
370 ++PC;
371 break;
372 }
373 if ((c = getlab(N_TEXT)) != NULL)
374 printf("%s",c);
375 f = optab[j].func;
376 (*f)(j);
377 }
378
379}/* * * * * * * * * * END OF distext() * * * * * * * * * */
380
381 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
382 * *
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. *
397 * *
398 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
399
400static void
401disdata()
402
403{/* * * * * * * * * * START OF disdata() * * * * * * * * * */
404
405 register char *c;
406 register int j;
407 unsigned long end;
408
409 putchar('\n');
410
411 if (HDR.a_flags & A_SEP)
412 {
413 PC = 0L;
414 end = HDR.a_data;
415 }
416 else
417 end = HDR.a_text + HDR.a_data;
418
419 printf("\t.data\t\t\t| loc = %05.5lx, size = %05.5lx\n\n",
420 PC,HDR.a_data);
421
422 segflg = 0;
423
424 for (objptr = 0, zcount = 0L; PC < end; ++PC)
425 {
426 if ((c = getlab(N_DATA)) != NULL)
427 {
428 objdump(c);
429 printf("%s",c);
430 }
431 if (objptr >= OBJMAX)
432 if (objdump(NULL) && (symptr < 0))
433 printf("D%05.5lx:",PC);
434 j = getchar() & 0xff;
435 objbuf[objptr++] = j;
436 }
437
438 objdump("");
439
440}/* * * * * * * * * * END OF disdata() * * * * * * * * * */
441
442 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
443 * *
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. *
447 * *
448 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
449
450static void disbss()
451
452{/* * * * * * * * * * START OF disbss() * * * * * * * * * */
453
454 register int j;
455 register char *c;
456 unsigned long beg, end;
457
458 putchar('\n');
459
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;
464
465 printf("\t.bss\t\t\t| loc = %05.5lx, size = %05.5lx\n\n",
466 PC,HDR.a_bss);
467
468 segflg = 0;
469
470 for (beg = PC; PC < end; ++PC)
471 if ((c = getlab(N_BSS)) != NULL)
472 {
473 if (PC > beg)
474 {
475 zdump(beg);
476 beg = PC;
477 }
478 printf("%s",c);
479 }
480
481 if (PC > beg)
482 zdump(beg);
483
484}/* * * * * * * * * * * END OF disbss() * * * * * * * * * * */
485
486 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
487 * *
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. *
495 * *
496 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
497
498void
499main(argc,argv)
500
501 int argc; /* Command-line args from OS */
502 register char **argv;
503
504{/* * * * * * * * * * * START OF main() * * * * * * * * * * */
505
506 char a[1024];
507 register int fd;
508 long taboff, tabnum;
509 long reloff, relnum;
510
511 PRG = invoker(*argv);
512
513 while (*++argv != NULL) /* Process command-line args */
514 if (**argv == '-')
515 switch (*++*argv)
516 {
517 case 'o' :
518 if (*++*argv)
519 usage(PRG);
520 else
521 ++objflg;
522 break;
523 default :
524 usage(PRG);
525 }
526 else
527 if (IFILE == NULL)
528 IFILE = *argv;
529 else if (OFILE == NULL)
530 OFILE = *argv;
531 else
532 usage(PRG);
533
534 if (IFILE == NULL)
535 usage(PRG);
536 else
537 if ((fd = open(IFILE,0)) < 0)
538 {
539 sprintf(a,"can't access input file %s",IFILE);
540 fatal(PRG,a);
541 }
542
543 if (OFILE != NULL)
544 if (freopen(OFILE,"w",stdout) == NULL)
545 {
546 sprintf(a,"can't open output file %s",OFILE);
547 fatal(PRG,a);
548 }
549
550 if ( ! cpuid )
551 fprintf(stderr,"\07%s: warning: host/cpu clash\n",PRG);
552
553 read(fd, (char *) &HDR,sizeof(struct exec));
554
555 if (BADMAG(HDR))
556 {
557 sprintf(a,"input file %s not in object format",IFILE);
558 fatal(PRG,a);
559 }
560
561 if (HDR.a_cpu != A_I8086)
562 {
563 sprintf(a,"%s is not an 8086/8088 object file",IFILE);
564 fatal(PRG,a);
565 }
566
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;
572*/
573
574 reloff = HDR.a_text /* Compute reloc data offset */
575 + HDR.a_data
576 + (long)(HDR.a_hdrlen);
577
578 relnum =
579 (HDR.a_trsize + HDR.a_drsize) / sizeof(struct reloc);
580
581 taboff = reloff /* Compute name table offset */
582 + HDR.a_trsize
583 + HDR.a_drsize;
584
585 tabnum = HDR.a_syms / sizeof(struct nlist);
586
587 if (relnum > MAXSYM)
588 fatal(PRG,"reloc table overflow");
589
590 if (tabnum > MAXSYM)
591 fatal(PRG,"symbol table overflow");
592
593 if (relnum) /* Get reloc data */
594 if (lseek(fd,reloff,0) != reloff)
595 fatal(PRG,"lseek error");
596 else
597 {
598 for (relptr = 0; relptr < relnum; ++relptr)
599 read(fd, (char *) &relo[relptr],sizeof(struct reloc));
600 relptr--;
601 }
602
603 if (tabnum) /* Read in symtab */
604 if (lseek(fd,taboff,0) != taboff)
605 fatal(PRG,"lseek error");
606 else
607 {
608 for (symptr = 0; symptr < tabnum; ++symptr)
609 read(fd, (char *) &symtab[symptr],sizeof(struct nlist));
610 symptr--;
611 }
612 else
613 fprintf(stderr,"\07%s: warning: no symbols\n",PRG);
614
615 close(fd);
616
617 if (freopen(IFILE,"r",stdin) == NULL)
618 {
619 sprintf(a,"can't reopen input file %s",IFILE);
620 fatal(PRG,a);
621 }
622
623 distext();
624
625 disdata();
626
627 disbss();
628
629 exit(0);
630
631}/* * * * * * * * * * * END OF main() * * * * * * * * * * */
Note: See TracBrowser for help on using the repository browser.