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