home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / assemblers / cas.lha / orig / ds.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-09  |  5.2 KB  |  153 lines

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