nasmlib/file.c: Windows _chsize_s() *returns* errno
[nasm.git] / output / outdbg.c
blobd4f4ec248b3acefea27ecc3a226cc7728d180816
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outdbg.c output routines for the Netwide Assembler to produce
36 * a debugging trace
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
46 #include "nasm.h"
47 #include "nasmlib.h"
48 #include "output/outform.h"
50 #ifdef OF_DBG
52 struct Section {
53 struct Section *next;
54 int32_t number;
55 char *name;
56 } *dbgsect;
58 const struct ofmt of_dbg;
59 static void dbg_init(void)
61 dbgsect = NULL;
62 fprintf(ofile, "NASM Output format debug dump\n");
65 static void dbg_cleanup(void)
67 dfmt->cleanup();
68 while (dbgsect) {
69 struct Section *tmp = dbgsect;
70 dbgsect = dbgsect->next;
71 nasm_free(tmp->name);
72 nasm_free(tmp);
76 static int32_t dbg_section_names(char *name, int pass, int *bits)
78 int seg;
81 * We must have an initial default: let's make it 16.
83 if (!name)
84 *bits = 16;
86 if (!name)
87 fprintf(ofile, "section_name on init: returning %d\n",
88 seg = seg_alloc());
89 else {
90 int n = strcspn(name, " \t");
91 char *sname = nasm_strndup(name, n);
92 struct Section *s;
94 seg = NO_SEG;
95 for (s = dbgsect; s; s = s->next)
96 if (!strcmp(s->name, sname))
97 seg = s->number;
99 if (seg == NO_SEG) {
100 s = nasm_malloc(sizeof(*s));
101 s->name = sname;
102 s->number = seg = seg_alloc();
103 s->next = dbgsect;
104 dbgsect = s;
105 fprintf(ofile, "section_name %s (pass %d): returning %d\n",
106 name, pass, seg);
109 return seg;
112 static void dbg_deflabel(char *name, int32_t segment, int64_t offset,
113 int is_global, char *special)
115 fprintf(ofile, "deflabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
116 name, segment, offset,
117 is_global == 2 ? "common" : is_global ? "global" : "local",
118 is_global, special ? ": " : "", special);
121 static void dbg_out(int32_t segto, const void *data,
122 enum out_type type, uint64_t size,
123 int32_t segment, int32_t wrt)
125 int32_t ldata;
126 int id;
128 if (type == OUT_ADDRESS)
129 fprintf(ofile, "out to %"PRIx32", len = %d: ", segto, (int)abs((int)size));
130 else
131 fprintf(ofile, "out to %"PRIx32", len = %"PRIu64": ", segto, size);
133 switch (type) {
134 case OUT_RESERVE:
135 fprintf(ofile, "reserved.\n");
136 break;
137 case OUT_RAWDATA:
138 fprintf(ofile, "raw data = ");
139 while (size--) {
140 id = *(uint8_t *)data;
141 data = (char *)data + 1;
142 fprintf(ofile, "%02x ", id);
144 fprintf(ofile, "\n");
145 break;
146 case OUT_ADDRESS:
147 ldata = *(int64_t *)data;
148 fprintf(ofile, "addr %08"PRIx32" (seg %08"PRIx32", wrt %08"PRIx32")\n",
149 ldata, segment, wrt);
150 break;
151 case OUT_REL1ADR:
152 fprintf(ofile, "rel1adr %02"PRIx8" (seg %08"PRIx32")\n",
153 (uint8_t)*(int64_t *)data, segment);
154 break;
155 case OUT_REL2ADR:
156 fprintf(ofile, "rel2adr %04"PRIx16" (seg %08"PRIx32")\n",
157 (uint16_t)*(int64_t *)data, segment);
158 break;
159 case OUT_REL4ADR:
160 fprintf(ofile, "rel4adr %08"PRIx32" (seg %08"PRIx32")\n",
161 (uint32_t)*(int64_t *)data,
162 segment);
163 break;
164 case OUT_REL8ADR:
165 fprintf(ofile, "rel8adr %016"PRIx64" (seg %08"PRIx32")\n",
166 (uint64_t)*(int64_t *)data, segment);
167 break;
168 default:
169 fprintf(ofile, "unknown\n");
170 break;
174 static void dbg_sectalign(int32_t seg, unsigned int value)
176 fprintf(ofile, "set alignment (%d) for segment (%u)\n",
177 seg, value);
180 static int32_t dbg_segbase(int32_t segment)
182 return segment;
185 static int dbg_directive(enum directives directive, char *value, int pass)
187 fprintf(ofile, "directive [%s] value [%s] (pass %d)\n",
188 directives[directive], value, pass);
189 return 1;
192 static void dbg_filename(char *inname, char *outname)
194 standard_extension(inname, outname, ".dbg");
197 static int dbg_set_info(enum geninfo type, char **val)
199 (void)type;
200 (void)val;
201 return 0;
204 static const char * const types[] = {
205 "unknown", "label", "byte", "word", "dword", "float", "qword", "tbyte"
207 static void dbgdbg_init(void)
209 fprintf(ofile, " With debug info\n");
211 static void dbgdbg_cleanup(void)
215 static void dbgdbg_linnum(const char *lnfname, int32_t lineno, int32_t segto)
217 fprintf(ofile, "dbglinenum %s(%"PRId32") := %08"PRIx32"\n",
218 lnfname, lineno, segto);
220 static void dbgdbg_deflabel(char *name, int32_t segment,
221 int64_t offset, int is_global, char *special)
223 fprintf(ofile, "dbglabel %s := %08"PRIx32":%016"PRIx64" %s (%d)%s%s\n",
224 name,
225 segment, offset,
226 is_global == 2 ? "common" : is_global ? "global" : "local",
227 is_global, special ? ": " : "", special);
229 static void dbgdbg_define(const char *type, const char *params)
231 fprintf(ofile, "dbgdirective [%s] value [%s]\n", type, params);
233 static void dbgdbg_output(int output_type, void *param)
235 (void)output_type;
236 (void)param;
238 static void dbgdbg_typevalue(int32_t type)
240 fprintf(ofile, "new type: %s(%"PRIX32")\n",
241 types[TYM_TYPE(type) >> 3], TYM_ELEMENTS(type));
243 static const struct dfmt debug_debug_form = {
244 "Trace of all info passed to debug stage",
245 "debug",
246 dbgdbg_init,
247 dbgdbg_linnum,
248 dbgdbg_deflabel,
249 dbgdbg_define,
250 dbgdbg_typevalue,
251 dbgdbg_output,
252 dbgdbg_cleanup,
255 static const struct dfmt * const debug_debug_arr[3] = {
256 &debug_debug_form,
257 &null_debug_form,
258 NULL
261 const struct ofmt of_dbg = {
262 "Trace of all info passed to output stage",
263 "dbg",
264 OFMT_TEXT,
266 debug_debug_arr,
267 &debug_debug_form,
268 NULL,
269 dbg_init,
270 dbg_set_info,
271 dbg_out,
272 dbg_deflabel,
273 dbg_section_names,
274 dbg_sectalign,
275 dbg_segbase,
276 dbg_directive,
277 dbg_filename,
278 dbg_cleanup
281 #endif /* OF_DBG */