annotate lwasm/symdump.c @ 577:e49d24f4a9a5

Correct bug in the object file output code leading to stack corruption It turns out leaving a pointer to a stack allocated temporary in a persistent data structure is not conducive to correct program operation. Undo the export check setup in the object file output sequence so a pointer to stack allocated memory is not left hanging when the function returns. This seems to correct at least one mysterious crash bug, and possibly others. Thanks to Boisy Pitre for reporting the crash bug that led to this discovery, as well as a previous crash bug that likely has the same root cause. Additional thanks to Ciaran Anscomb whose debugger wielding wizardry revealed the exact location of this particular bit of unbrilliance.
author William Astle <lost@l-w.ca>
date Sat, 03 Aug 2024 14:30:06 -0600
parents 1c3220ed87a8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
487
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
1 /*
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
2 symdump.c
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
3
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
4 Copyright © 2019 William Astle
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
5
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
6 This file is part of LWTOOLS.
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
7
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
11 version.
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
12
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
16 more details.
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
17
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
20 */
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
21
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
22 #include <stdio.h>
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
23 #include <stdlib.h>
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
24 #include <string.h>
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
25
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
26 #include <lw_alloc.h>
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
27 #include <lw_expr.h>
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
28 #include <lw_string.h>
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
29
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
30 #include "lwasm.h"
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
31
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
32 struct listinfo
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
33 {
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
34 sectiontab_t *sect;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
35 asmstate_t *as;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
36 int complex;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
37 };
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
38
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
39 int dump_symbols_test(lw_expr_t e, void *p)
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
40 {
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
41 struct listinfo *li = p;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
42
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
43 if (li -> complex)
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
44 return 0;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
45
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
46 if (lw_expr_istype(e, lw_expr_type_special))
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
47 {
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
48 if (lw_expr_specint(e) == lwasm_expr_secbase)
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
49 {
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
50 if (li -> sect)
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
51 {
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
52 li -> complex = 1;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
53 }
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
54 else
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
55 {
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
56 li -> sect = lw_expr_specptr(e);
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
57 }
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
58 }
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
59 }
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
60 return 0;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
61 }
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
62
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
63 void dump_symbols_aux(asmstate_t *as, FILE *of, struct symtabe *se)
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
64 {
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
65 struct symtabe *s;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
66 lw_expr_t te;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
67 struct listinfo li;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
68
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
69 li.as = as;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
70
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
71 if (!se)
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
72 return;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
73
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
74 dump_symbols_aux(as, of, se -> left);
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
75
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
76 for (s = se; s; s = s -> nextver)
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
77 {
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
78 if (s -> flags & symbol_flag_nolist)
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
79 continue;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
80
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
81 if (s -> context >= 0)
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
82 continue;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
83
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
84 lwasm_reduce_expr(as, s -> value);
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
85
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
86 fprintf(of, "%s ", s -> symbol);
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
87 if (s -> flags & symbol_flag_set)
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
88 fputs("SET", of);
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
89 else
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
90 fputs("EQU", of);
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
91 te = lw_expr_copy(s -> value);
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
92 li.complex = 0;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
93 li.sect = NULL;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
94 lw_expr_testterms(te, dump_symbols_test, &li);
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
95 if (li.sect)
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
96 {
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
97 as -> exportcheck = 1;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
98 as -> csect = li.sect;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
99 lwasm_reduce_expr(as, te);
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
100 as -> exportcheck = 0;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
101 }
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
102
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
103 if (lw_expr_istype(te, lw_expr_type_int))
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
104 {
490
1c3220ed87a8 Fix missing hex sigil in symbol dump
William Astle <lost@l-w.ca>
parents: 487
diff changeset
105 fprintf(of, " $%04X\n", lw_expr_intval(te));
487
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
106 }
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
107 else
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
108 {
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
109 fprintf(of, " 0 ; <<incomplete>>\n");
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
110 }
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
111 lw_expr_destroy(te);
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
112 }
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
113
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
114 dump_symbols_aux(as, of, se -> right);
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
115 }
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
116
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
117 void do_symdump(asmstate_t *as)
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
118 {
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
119 FILE *of;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
120
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
121 if (!(as -> flags & FLAG_SYMDUMP))
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
122 {
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
123 return;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
124 }
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
125 else
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
126 {
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
127 if (as -> symbol_dump_file)
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
128 {
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
129 if (strcmp(as -> symbol_dump_file, "-") == 0)
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
130 {
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
131 of = stdout;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
132 }
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
133 else
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
134 of = fopen(as -> symbol_dump_file, "w");
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
135 }
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
136 else
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
137 of = stdout;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
138
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
139 if (!of)
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
140 {
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
141 fprintf(stderr, "Cannot open list file; list not generated\n");
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
142 return;
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
143 }
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
144 }
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
145 dump_symbols_aux(as, of, as -> symtab.head);
7fbf3171ca15 Add symbol table dump in assembly format
William Astle <lost@l-w.ca>
parents:
diff changeset
146 }