home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / assemblers / cas.lha / ds.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-14  |  5.3 KB  |  155 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef unsigned char byte;
  5. typedef unsigned short word;
  6.  
  7. void FATAL(char *Msg) { printf(Msg), putchar('\n'); exit(1); }
  8.  
  9. byte GetB(FILE *FP) {
  10.    int A;
  11.    A = fgetc(FP); if (A == EOF) FATAL("Unexpected EOF.");
  12.    return ((byte)(A&0xff));
  13. }
  14.  
  15. word GetW(FILE *FP) {
  16.    int A, B;
  17.    A = fgetc(FP); if (A == EOF) FATAL("Unexpected EOF.");
  18.    B = fgetc(FP); if (B == EOF) FATAL("Unexpected EOF.");
  19.    return ((word)((A&0xff) << 8 | B&0xff));
  20. }
  21.  
  22. unsigned long GetL(FILE *FP) {
  23.    int A, B, C, D;
  24.    A = fgetc(FP); if (A == EOF) FATAL("Unexpected EOF.");
  25.    B = fgetc(FP); if (B == EOF) FATAL("Unexpected EOF.");
  26.    C = fgetc(FP); if (C == EOF) FATAL("Unexpected EOF.");
  27.    D = fgetc(FP); if (D == EOF) FATAL("Unexpected EOF.");
  28.    return ((unsigned int)((A&0xff) << 24 | (B&0xff) << 16 | (C&0xff) << 8 | D&0xff));
  29. }
  30.  
  31. main(int AC, char **AV) {
  32.    FILE *FP; int Field; word MAGIC;
  33.    long SegLoc, Files, Segs, Gaps, Syms, Exps, Relocs, Sum;
  34.    if (AC != 2) fprintf(stderr, "Use; display Input.\n"), exit(1);
  35.    FP = fopen(AV[1], "rb+");
  36.    if (FP == 0) fprintf(stderr, "Cannot open %s.\n", AV[1]), exit(1);
  37.    MAGIC = GetW(FP);
  38.    if (MAGIC != 0x55aa)
  39.       fprintf(stderr, "Invalid object file (%4x).\n", MAGIC), exit(1);
  40.    SegLoc = GetL(FP), Files = GetL(FP), Segs = GetL(FP), Gaps = GetL(FP),
  41.    Syms = GetL(FP), Exps = GetL(FP), Relocs = GetL(FP), Sum = GetL(FP);
  42.    if (Sum != (SegLoc + Files + Segs + Gaps + Syms + Exps + Relocs)&0xffffffff)
  43.       fprintf(stderr, "Corrupt object file."), exit(0);
  44. printf("IMAGE:\n");
  45.    for (Field = 0; ftell(FP) < SegLoc; Field++) {
  46.       if (Field%0x10 == 0) printf("%4x:", Field);
  47.       printf(" %2x", fgetc(FP));
  48.       if (Field%0x10 == 0x0f) putchar('\n');
  49.    }
  50.    if (Field%0x10 > 0) putchar('\n');
  51. if (ftell(FP) != SegLoc) fprintf(stderr, "INTERNAL ERROR (0).\n"), exit(0);
  52.    printf("FILE(S): %ld\n", Files);
  53.    if (Files > 0) {
  54.       long S; char Buf[0x100];
  55.       printf("## Name,\n");
  56.       for (S = 0; S < Files; S++) {
  57.          word L;
  58.          L = GetW(FP), fread(Buf, 1, L, FP), Buf[L] = '\0';
  59.          printf("%2ld %15s\n", S, Buf);
  60.       }
  61.    }
  62.    printf("SEGMENT(S): %ld\n", Segs);
  63.    if (Segs > 5) { /* The number of types */
  64.       long S;
  65.       printf("## Line File Rel Type Base Size  Loc\n");
  66.       for (S = 5; S < Segs; S++) {
  67.          word U, Rel, Type, Size, Base, Line, File; long Loc;
  68.          Line = GetW(FP), File = GetW(FP);
  69.          U = GetW(FP), Size = GetW(FP), Base = GetW(FP), Loc = GetL(FP);
  70.          Rel = (U >> 8)&1, Type = U&0xff;
  71.          printf("%2ld %4d %4d  %c %4x %4x %4x %8lx\n",
  72.             S, Line, File, Rel? 'r': ' ', Type, Base, Size, Loc
  73.          );
  74.       }
  75.    }
  76.    printf("GAP(S): %ld\n", Gaps);
  77.    if (Gaps > 0) {
  78.       long S;
  79.       printf("##  Seg  Off Size\n");
  80.       for (S = 0; S < Gaps; S++) {
  81.          word Seg, Off, Size;
  82.          Seg = GetW(FP), Off = GetW(FP), Size = GetW(FP);
  83.          printf("%2ld %4x %4x %4x\n", S, Seg, Off, Size);
  84.       }
  85.    }
  86.    printf("SYMBOL(S): %ld\n", Syms);
  87.    if (Syms > 0) {
  88.       long S; char Buf[0x80];
  89.       printf("##     Scope Var Type   Value  Name\n");
  90.       for (S = 0; S < Syms; S++) {
  91.          byte B; word U, L, Offset;
  92.          B = GetB(FP), U = GetW(FP), Offset = GetW(FP), L = GetW(FP);
  93.          if (L > 0) fread(&Buf, 1, L, FP);
  94.          Buf[L] = '\0';
  95.          printf("%2lx ", S);
  96.          switch (B&0xc) {
  97.             case 0x0: printf("undefined"); break;
  98.             case 0x4: printf("    local"); break;
  99.             case 0x8: printf(" external"); break;
  100.             case 0xc: printf("   global"); break;
  101.          }
  102.          printf("  %c  ", B&1? 'v': ' ');
  103.          if (B&2) { /* Address */
  104.             printf("ADDR  %2d:%4x", U, Offset);
  105.          } else {
  106.             printf(" NUM  %4x   ", Offset);
  107.          }
  108.          printf(" %s\n", Buf);
  109.       }
  110.    }
  111.    printf("EXPRESSION(S): %ld\n", Exps);
  112.    if (Exps > 0) {
  113.       long S;
  114.       printf("## Line File  Tag Args...\n");
  115.       for (S = 0; S < Exps; S++) {
  116.          char Tag, Op; word Seg, Line, File, A, B, C, Value, Offset, Sym;
  117.          Line = GetW(FP), File = GetW(FP), Tag = GetB(FP);
  118.          printf("%2lx %4d %4d ", S, Line, File);
  119.          switch (Tag) {
  120.             case 0: Value = GetW(FP); printf(" NUM %4x", Value); break;
  121.             case 1:
  122.                Seg = GetW(FP), Offset = GetW(FP);
  123.                printf("ADDR %2d %4x", Seg, Offset);
  124.             break;
  125.             case 2: Sym = GetW(FP); printf(" SYM %2x", Sym); break;
  126.             case 3:
  127.                Op = GetB(FP), A = GetW(FP);
  128.                printf("  UN %2d %4x", Op, A);
  129.             break;
  130.             case 4:
  131.                Op = GetB(FP), A = GetW(FP), B = GetW(FP);
  132.                printf(" BIN %2d %4x %4x", Op, A, B);
  133.             break;
  134.             case 5:
  135.                A = GetW(FP), B = GetW(FP), C = GetW(FP);
  136.                printf("COND %4x %4x %4x", A, B, C);
  137.             break;
  138.          }
  139.          putchar('\n');
  140.       }
  141.    }
  142.    printf("RELOCATION(S): %ld\n", Relocs);
  143.    if (Relocs > 0) {
  144.       long S;
  145.       printf("Line File Tag  Exp  Seg: Off\n");
  146.       for (S = 0; S < Relocs; S++) {
  147.          word U, Line, File, Index, Offset; char Tag;
  148.          Line = GetW(FP), File = GetW(FP),
  149.          Tag = GetB(FP), Index = GetW(FP), U = GetW(FP), Offset = GetW(FP);
  150.          printf("%4d %4d  %c %4x  @ %2d:%4x\n", Line, File, Tag, Index, U, Offset);
  151.       }
  152.    }
  153.    fclose(FP);
  154. }
  155.