NASM 0.95
[platform/upstream/nasm.git] / outdbg.c
1 /* outdbg.c     output routines for the Netwide Assembler to produce
2  *              a debugging trace
3  *
4  * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5  * Julian Hall. All rights reserved. The software is
6  * redistributable under the licence given in the file "Licence"
7  * distributed in the NASM archive.
8  */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14
15 #include "nasm.h"
16 #include "nasmlib.h"
17 #include "outform.h"
18
19 #ifdef OF_DBG
20
21 FILE *dbgf;
22 efunc dbgef;
23
24 int segcode,segdata,segbss;
25
26 static void dbg_init(FILE *fp, efunc errfunc, ldfunc ldef)
27 {
28   dbgf = fp;
29   dbgef = errfunc;
30   (void) ldef;
31   segcode = seg_alloc();
32   segdata = seg_alloc();
33   segbss = seg_alloc();
34   fprintf(fp,"NASM Output format debug dump - code=%d,data=%d,bss=%d\n",
35           segcode,segdata,segbss);
36 }
37
38 static void dbg_cleanup(void)
39 {
40   fclose(dbgf);
41 }
42
43 static long dbg_section_names (char *name, int pass, int *bits)
44 {
45     /*
46      * We must have an initial default: let's make it 16.
47      */
48     if (!name)
49         *bits = 16;
50
51     if (!name)
52         return 0;
53
54     if (!strcmp(name, ".text"))
55         return segcode;
56     else if (!strcmp(name, ".data"))
57         return segdata;
58     else if (!strcmp(name, ".bss"))
59         return segbss;
60     else
61         return NO_SEG;
62 }
63
64 static void dbg_deflabel (char *name, long segment, long offset,
65                            int is_global) {
66     fprintf(dbgf,"deflabel %s := %08lx:%08lx %s (%d)\n",name,segment,offset,
67             is_global ? "global" : "local", is_global);
68 }
69
70 static void dbg_out (long segto, void *data, unsigned long type,
71                       long segment, long wrt) {
72   long realbytes = type & OUT_SIZMASK;
73   long ldata;
74   int id;
75
76   type &= OUT_TYPMASK;
77
78   fprintf(dbgf,"out to %lx, len = %ld: ",segto,realbytes);
79
80   switch(type) {
81   case OUT_RESERVE:
82     fprintf(dbgf,"reserved.\n"); break;
83   case OUT_RAWDATA:
84     fprintf(dbgf,"raw data = ");
85     while (realbytes--) {
86       id = *(unsigned char *)data;
87       data = (char *)data + 1;
88       fprintf(dbgf,"%02x ",id);
89     }
90     fprintf(dbgf,"\n"); break;
91   case OUT_ADDRESS:
92     ldata = 0; /* placate gcc */
93     if (realbytes == 1)
94       ldata = *((char *)data);
95     else if (realbytes == 2)
96       ldata = *((short *)data);
97     else if (realbytes == 4)
98       ldata = *((long *)data);
99     fprintf(dbgf,"addr %08lx (seg %08lx, wrt %08lx)\n",ldata,
100             segment,wrt);break;
101   case OUT_REL2ADR:
102     fprintf(dbgf,"rel2adr %04x (seg %08lx)\n",(int)*(short *)data,segment);
103     break;
104   case OUT_REL4ADR:
105     fprintf(dbgf,"rel4adr %08lx (seg %08lx)\n",*(long *)data,segment);
106     break;
107   default:
108     fprintf(dbgf,"unknown\n");
109     break;
110   }
111 }
112
113 static long dbg_segbase(long segment) {
114   return segment;
115 }
116
117 static int dbg_directive (char *directive, char *value, int pass) {
118   return 0;
119 }
120
121 static void dbg_filename (char *inname, char *outname, efunc error) {
122     standard_extension (inname, outname, ".dbg", error);
123 }
124
125 struct ofmt of_dbg = {
126     "Trace of all info passed to output stage",
127     "dbg",
128     dbg_init,
129     dbg_out,
130     dbg_deflabel,
131     dbg_section_names,
132     dbg_segbase,
133     dbg_directive,
134     dbg_filename,
135     dbg_cleanup
136 };
137
138 #endif /* OF_DBG */