]> git.saurik.com Git - apple/boot.git/blob - i386/nasm/outdbg.c
d7a1af3f779265842eac020ebed87f2aee6f17bf
[apple/boot.git] / i386 / nasm / outdbg.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /* outdbg.c output routines for the Netwide Assembler to produce
25 * a debugging trace
26 *
27 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
28 * Julian Hall. All rights reserved. The software is
29 * redistributable under the licence given in the file "Licence"
30 * distributed in the NASM archive.
31 */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37
38 #include "nasm.h"
39 #include "nasmlib.h"
40 #include "outform.h"
41
42 #ifdef OF_DBG
43
44 struct Section {
45 struct Section *next;
46 long number;
47 char *name;
48 } *dbgsect;
49
50 FILE *dbgf;
51 efunc dbgef;
52
53 static void dbg_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
54 {
55 dbgf = fp;
56 dbgef = errfunc;
57 dbgsect = NULL;
58 (void) ldef;
59 fprintf(fp,"NASM Output format debug dump\n");
60 }
61
62 static void dbg_cleanup(void)
63 {
64 while (dbgsect) {
65 struct Section *tmp = dbgsect;
66 dbgsect = dbgsect->next;
67 nasm_free (tmp->name);
68 nasm_free (tmp);
69 }
70 fclose(dbgf);
71 }
72
73 static long dbg_section_names (char *name, int pass, int *bits)
74 {
75 int seg;
76
77 /*
78 * We must have an initial default: let's make it 16.
79 */
80 if (!name)
81 *bits = 16;
82
83 if (!name)
84 fprintf(dbgf, "section_name on init: returning %d\n",
85 seg = seg_alloc());
86 else {
87 int n = strcspn(name, " \t");
88 char *sname = nasm_strndup(name, n);
89 struct Section *s;
90
91 seg = NO_SEG;
92 for (s = dbgsect; s; s = s->next)
93 if (!strcmp(s->name, sname))
94 seg = s->number;
95
96 if (seg == NO_SEG) {
97 s = nasm_malloc(sizeof(*s));
98 s->name = sname;
99 s->number = seg = seg_alloc();
100 s->next = dbgsect;
101 dbgsect = s;
102 fprintf(dbgf, "section_name %s (pass %d): returning %d\n",
103 name, pass, seg);
104 }
105 }
106 return seg;
107 }
108
109 static void dbg_deflabel (char *name, long segment, long offset,
110 int is_global, char *special) {
111 fprintf(dbgf,"deflabel %s := %08lx:%08lx %s (%d)%s%s\n",
112 name, segment, offset,
113 is_global == 2 ? "common" : is_global ? "global" : "local",
114 is_global,
115 special ? ": " : "", special);
116 }
117
118 static void dbg_out (long segto, void *data, unsigned long type,
119 long segment, long wrt) {
120 long realbytes = type & OUT_SIZMASK;
121 long ldata;
122 int id;
123
124 type &= OUT_TYPMASK;
125
126 fprintf(dbgf,"out to %lx, len = %ld: ",segto,realbytes);
127
128 switch(type) {
129 case OUT_RESERVE:
130 fprintf(dbgf,"reserved.\n"); break;
131 case OUT_RAWDATA:
132 fprintf(dbgf,"raw data = ");
133 while (realbytes--) {
134 id = *(unsigned char *)data;
135 data = (char *)data + 1;
136 fprintf(dbgf,"%02x ",id);
137 }
138 fprintf(dbgf,"\n"); break;
139 case OUT_ADDRESS:
140 ldata = 0; /* placate gcc */
141 if (realbytes == 1)
142 ldata = *((char *)data);
143 else if (realbytes == 2)
144 ldata = *((short *)data);
145 else if (realbytes == 4)
146 ldata = *((long *)data);
147 fprintf(dbgf,"addr %08lx (seg %08lx, wrt %08lx)\n",ldata,
148 segment,wrt);break;
149 case OUT_REL2ADR:
150 fprintf(dbgf,"rel2adr %04x (seg %08lx)\n",(int)*(short *)data,segment);
151 break;
152 case OUT_REL4ADR:
153 fprintf(dbgf,"rel4adr %08lx (seg %08lx)\n",*(long *)data,segment);
154 break;
155 default:
156 fprintf(dbgf,"unknown\n");
157 break;
158 }
159 }
160
161 static long dbg_segbase(long segment) {
162 return segment;
163 }
164
165 static int dbg_directive (char *directive, char *value, int pass) {
166 fprintf(dbgf, "directive [%s] value [%s] (pass %d)\n",
167 directive, value, pass);
168 return 1;
169 }
170
171 static void dbg_filename (char *inname, char *outname, efunc error) {
172 standard_extension (inname, outname, ".dbg", error);
173 }
174
175 struct ofmt of_dbg = {
176 "Trace of all info passed to output stage",
177 "dbg",
178 NULL,
179 dbg_init,
180 dbg_out,
181 dbg_deflabel,
182 dbg_section_names,
183 dbg_segbase,
184 dbg_directive,
185 dbg_filename,
186 dbg_cleanup
187 };
188
189 #endif /* OF_DBG */