aboutsummaryrefslogtreecommitdiffstats
path: root/outdbg.c
blob: a55d3db8b583d90602a8e1e5a76f85054946b9d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* outdbg.c	output routines for the Netwide Assembler to produce
 *		a debugging trace
 *
 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
 * Julian Hall. All rights reserved. The software is
 * redistributable under the licence given in the file "Licence"
 * distributed in the NASM archive.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "nasm.h"
#include "nasmlib.h"
#include "outform.h"

#ifdef OF_DBG

FILE *dbgf;
efunc dbgef;

int segcode,segdata,segbss;

static void dbg_init(FILE *fp, efunc errfunc, ldfunc ldef)
{
  dbgf = fp;
  dbgef = errfunc;
  (void) ldef;
  segcode = seg_alloc();
  segdata = seg_alloc();
  segbss = seg_alloc();
  fprintf(fp,"NASM Output format debug dump - code=%d,data=%d,bss=%d\n",
	  segcode,segdata,segbss);
}

static void dbg_cleanup(void)
{
  fclose(dbgf);
}

static long dbg_section_names (char *name, int pass, int *bits)
{
    /*
     * We must have an initial default: let's make it 16.
     */
    if (!name)
	*bits = 16;

    if (!name)
	return 0;

    if (!strcmp(name, ".text"))
	return segcode;
    else if (!strcmp(name, ".data"))
	return segdata;
    else if (!strcmp(name, ".bss"))
	return segbss;
    else
	return NO_SEG;
}

static void dbg_deflabel (char *name, long segment, long offset,
			   int is_global) {
  fprintf(dbgf,"deflabel %s := %08lx:%08lx %s (%d)\n",name,segment,offset,
	  is_global ? "global" : "local", is_global);
}

static void dbg_out (long segto, void *data, unsigned long type,
		      long segment, long wrt) {
  long realbytes = type & OUT_SIZMASK;
  long ldata;
  int id;

  type &= OUT_TYPMASK;

  fprintf(dbgf,"out to %lx, len = %ld: ",segto,realbytes);

  switch(type) {
  case OUT_RESERVE:
    fprintf(dbgf,"reserved.\n"); break;
  case OUT_RAWDATA:
    fprintf(dbgf,"raw data = ");
    while (realbytes--) {
      id = *(unsigned char *)data;
      data = (char *)data + 1;
      fprintf(dbgf,"%02x ",id);
    }
    fprintf(dbgf,"\n"); break;
  case OUT_ADDRESS:
    ldata = 0; /* placate gcc */
    if (realbytes == 1)
      ldata = *((char *)data);
    else if (realbytes == 2)
      ldata = *((short *)data);
    else if (realbytes == 4)
      ldata = *((long *)data);
    fprintf(dbgf,"addr %08lx (seg %08lx, wrt %08lx)\n",ldata,
	    segment,wrt);break;
  case OUT_REL2ADR:
    fprintf(dbgf,"rel2adr %04x (seg %08lx)\n",(int)*(short *)data,segment);
    break;
  case OUT_REL4ADR:
    fprintf(dbgf,"rel4adr %08lx (seg %08lx)\n",*(long *)data,segment);
    break;
  default:
    fprintf(dbgf,"unknown\n");
    break;
  }
}

static long dbg_segbase(long segment) {
  return segment;
}

static int dbg_directive (char *directive, char *value, int pass) {
  return 0;
}

static void dbg_filename (char *inname, char *outname, efunc error) {
    standard_extension (inname, outname, ".dbg", error);
}

struct ofmt of_dbg = {
    "Trace of all info passed to output stage",
    "dbg",
    dbg_init,
    dbg_out,
    dbg_deflabel,
    dbg_section_names,
    dbg_segbase,
    dbg_directive,
    dbg_filename,
    dbg_cleanup
};

#endif /* OF_DBG */