1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2018 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
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
58 static unsigned long dbg_max_data_dump
= 128;
59 static bool section_labels
= true;
60 static bool subsections_via_symbols
= false;
61 static int32_t init_seg
;
63 const struct ofmt of_dbg
;
64 static void dbg_init(void)
67 fprintf(ofile
, "NASM Output format debug dump\n");
68 fprintf(ofile
, "input file = %s\n", inname
);
69 fprintf(ofile
, "output file = %s\n", outname
);
70 init_seg
= seg_alloc();
73 static void dbg_reset(void)
75 fprintf(ofile
, "*** pass reset: pass = %"PRId64
" (%s)\n",
76 pass_count(), pass_type_name());
79 static void dbg_cleanup(void)
83 struct Section
*tmp
= dbgsect
;
84 dbgsect
= dbgsect
->next
;
90 static int32_t dbg_add_section(char *name
, int *bits
, const char *whatwecallit
)
95 * We must have an initial default: let's make it 16.
101 fprintf(ofile
, "section_name on init: returning %d\n", init_seg
);
104 int n
= strcspn(name
, " \t");
105 char *sname
= nasm_strndup(name
, n
);
106 char *tail
= nasm_skip_spaces(name
+n
);
110 for (s
= dbgsect
; s
; s
= s
->next
)
111 if (!strcmp(s
->name
, sname
))
115 s
= nasm_malloc(sizeof(*s
));
117 s
->number
= seg
= seg_alloc();
120 fprintf(ofile
, "%s %s (%s) pass %"PRId64
" (%s) : returning %d\n",
121 whatwecallit
, name
, tail
, pass_count(), pass_type_name(), seg
);
124 backend_label(s
->name
, s
->number
+ 1, 0);
130 static int32_t dbg_section_names(char *name
, int *bits
)
132 return dbg_add_section(name
, bits
, "section_names");
135 static int32_t dbg_herelabel(const char *name
, enum label_type type
,
136 int32_t oldseg
, int32_t *subsection
,
139 int32_t newseg
= oldseg
;
141 if (subsections_via_symbols
&& type
!= LBL_LOCAL
) {
142 newseg
= *subsection
;
143 if (newseg
== NO_SEG
) {
144 newseg
= *subsection
= seg_alloc();
145 *copyoffset
= true; /* Minic MachO for now */
148 fprintf(ofile
, "herelabel %s type %d (seg %08x) -> %08x\n",
149 name
, type
, oldseg
, newseg
);
154 static void dbg_deflabel(char *name
, int32_t segment
, int64_t offset
,
155 int is_global
, char *special
)
157 fprintf(ofile
, "deflabel %s := %08"PRIx32
":%016"PRIx64
" %s (%d)%s%s\n",
158 name
, segment
, offset
,
159 is_global
== 2 ? "common" : is_global
? "global" : "local",
160 is_global
, special
? ": " : "", special
);
163 static const char *out_type(enum out_type type
)
165 static const char *out_types
[] = {
173 static char invalid_buf
[64];
175 if (type
>= sizeof(out_types
)/sizeof(out_types
[0])) {
176 sprintf(invalid_buf
, "[invalid type %d]", type
);
180 return out_types
[type
];
183 static const char *out_sign(enum out_sign sign
)
185 static const char *out_signs
[] = {
190 static char invalid_buf
[64];
192 if (sign
>= sizeof(out_signs
)/sizeof(out_signs
[0])) {
193 sprintf(invalid_buf
, "[invalid sign %d]", sign
);
197 return out_signs
[sign
];
200 static void dbg_out(const struct out_data
*data
)
203 "out to %"PRIx32
":%"PRIx64
" %s %s bits %d insoffs %d/%d "
205 data
->segment
, data
->offset
,
206 out_type(data
->type
), out_sign(data
->sign
),
207 data
->bits
, data
->insoffs
, data
->inslen
, data
->size
);
209 fprintf(ofile
, " ins %s(%d)",
210 nasm_insn_names
[data
->itemp
->opcode
], data
->itemp
->operands
);
212 fprintf(ofile
, " no ins (plain data)");
215 if (data
->type
== OUT_ADDRESS
|| data
->type
== OUT_RELADDR
||
216 data
->type
== OUT_SEGMENT
) {
217 fprintf(ofile
, " target %"PRIx32
":%"PRIx64
,
218 data
->tsegment
, data
->toffset
);
219 if (data
->twrt
!= NO_SEG
)
220 fprintf(ofile
, " wrt %"PRIx32
, data
->twrt
);
222 if (data
->type
== OUT_RELADDR
)
223 fprintf(ofile
, " relbase %"PRIx64
, data
->relbase
);
227 if (data
->type
== OUT_RAWDATA
) {
228 if ((size_t)data
->size
!= data
->size
) {
229 fprintf(ofile
, " data: <error: impossible size>\n");
230 } else if (!data
->data
) {
231 fprintf(ofile
, " data: <error: null pointer>\n");
232 } else if (dbg_max_data_dump
!= -1UL &&
233 data
->size
> dbg_max_data_dump
) {
234 fprintf(ofile
, " data: <%"PRIu64
" bytes>\n", data
->size
);
237 const uint8_t *bytes
= data
->data
;
238 for (i
= 0; i
< data
->size
; i
+= 16) {
239 fprintf(ofile
, " data:");
240 for (j
= 0; j
< 16; j
++) {
241 if (i
+j
>= data
->size
)
244 fprintf(ofile
, "%c%02x",
245 (j
== 8) ? '-' : ' ', bytes
[i
+j
]);
248 for (j
= 0; j
< 16; j
++) {
249 if (i
+j
>= data
->size
) {
252 if (bytes
[i
+j
] >= 32 && bytes
[i
+j
] <= 126)
253 putc(bytes
[i
+j
], ofile
);
263 /* This is probably the only place were we'll call this this way... */
264 nasm_do_legacy_output(data
);
267 static void dbg_legacy_out(int32_t segto
, const void *data
,
268 enum out_type type
, uint64_t size
,
269 int32_t segment
, int32_t wrt
)
273 if (type
== OUT_ADDRESS
)
274 fprintf(ofile
, " legacy: out to %"PRIx32
", len = %d: ",
275 segto
, (int)abs((int)size
));
277 fprintf(ofile
, " legacy: out to %"PRIx32
", len = %"PRId64
" (0x%"PRIx64
"): ",
278 segto
, (int64_t)size
, size
);
282 fprintf(ofile
, "reserved.\n");
285 fprintf(ofile
, "rawdata\n"); /* Already have a data dump */
288 ldata
= *(int64_t *)data
;
289 fprintf(ofile
, "addr %08"PRIx32
" (seg %08"PRIx32
", wrt %08"PRIx32
")\n",
290 ldata
, segment
, wrt
);
293 fprintf(ofile
, "rel1adr %02"PRIx8
" (seg %08"PRIx32
")\n",
294 (uint8_t)*(int64_t *)data
, segment
);
297 fprintf(ofile
, "rel2adr %04"PRIx16
" (seg %08"PRIx32
")\n",
298 (uint16_t)*(int64_t *)data
, segment
);
301 fprintf(ofile
, "rel4adr %08"PRIx32
" (seg %08"PRIx32
")\n",
302 (uint32_t)*(int64_t *)data
,
306 fprintf(ofile
, "rel8adr %016"PRIx64
" (seg %08"PRIx32
")\n",
307 (uint64_t)*(int64_t *)data
, segment
);
310 fprintf(ofile
, "unknown\n");
315 static void dbg_sectalign(int32_t seg
, unsigned int value
)
317 fprintf(ofile
, "set alignment (%d) for segment (%u)\n",
321 static enum directive_result
322 dbg_directive(enum directive directive
, char *value
)
326 * The .obj GROUP directive is nontrivial to emulate in a macro.
327 * It effectively creates a "pseudo-section" containing the first
328 * space-separated argument; the rest we ignore.
333 dbg_add_section(value
, &dummy
, "directive:group");
341 fprintf(ofile
, "directive [%s] value [%s] pass %"PRId64
" (%s)\n",
342 directive_dname(directive
), value
, pass_count(), pass_type_name());
346 static enum directive_result
347 dbg_pragma(const struct pragma
*pragma
);
349 static const struct pragma_facility dbg_pragma_list
[] = {
353 static enum directive_result
354 dbg_pragma(const struct pragma
*pragma
)
356 fprintf(ofile
, "pragma %s(%s) %s[%s] %s\n",
357 pragma
->facility_name
,
358 pragma
->facility
->name
? pragma
->facility
->name
: "<default>",
359 pragma
->opname
, directive_dname(pragma
->opcode
),
362 if (pragma
->facility
== &dbg_pragma_list
[0]) {
363 switch (pragma
->opcode
) {
365 if (!nasm_stricmp(pragma
->tail
, "unlimited")) {
366 dbg_max_data_dump
= -1UL;
372 arg
= strtoul(pragma
->tail
, &ep
, 0);
373 if (errno
|| *nasm_skip_spaces(ep
)) {
374 nasm_warn(WARN_PRAGMA_BAD
| ERR_PASS2
,
375 "invalid %%pragma dbg maxdump argument");
378 dbg_max_data_dump
= arg
;
383 section_labels
= false;
385 case D_SUBSECTIONS_VIA_SYMBOLS
:
386 subsections_via_symbols
= true;
395 static const char * const types
[] = {
396 "unknown", "label", "byte", "word", "dword", "float", "qword", "tbyte"
398 static void dbgdbg_init(void)
400 fprintf(ofile
, " With debug info\n");
402 static void dbgdbg_cleanup(void)
406 static void dbgdbg_linnum(const char *lnfname
, int32_t lineno
, int32_t segto
)
408 fprintf(ofile
, "dbglinenum %s(%"PRId32
") segment %"PRIx32
"\n",
409 lnfname
, lineno
, segto
);
411 static void dbgdbg_deflabel(char *name
, int32_t segment
,
412 int64_t offset
, int is_global
, char *special
)
414 fprintf(ofile
, "dbglabel %s := %08"PRIx32
":%016"PRIx64
" %s (%d)%s%s\n",
417 is_global
== 2 ? "common" : is_global
? "global" : "local",
418 is_global
, special
? ": " : "", special
);
420 static void dbgdbg_define(const char *type
, const char *params
)
422 fprintf(ofile
, "dbgdirective [%s] value [%s]\n", type
, params
);
424 static void dbgdbg_output(int output_type
, void *param
)
429 static void dbgdbg_typevalue(int32_t type
)
431 fprintf(ofile
, "new type: %s(%"PRIX32
")\n",
432 types
[TYM_TYPE(type
) >> 3], TYM_ELEMENTS(type
));
435 static const struct pragma_facility dbgdbg_pragma_list
[] = {
436 { "dbgdbg", dbg_pragma
},
437 { NULL
, dbg_pragma
} /* Won't trigger, "debug" is a reserved ns */
440 static const struct dfmt debug_debug_form
= {
441 "Trace of all info passed to debug stage",
453 static const struct dfmt
* const debug_debug_arr
[3] = {
459 extern macros_t dbg_stdmac
[];
461 const struct ofmt of_dbg
= {
462 "Trace of all info passed to output stage",