annotate lwasm/output.c @ 448:5cccf90bf838 3.0 tip

Fixed bug with complex external references generating invalid relocations in the object file
author lost@l-w.ca
date Fri, 05 Nov 2010 22:27:00 -0600
parents 35a0b086bf4a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
1 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
2 output.c
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
3 Copyright © 2009, 2010 William Astle
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
4
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
5 This file is part of LWASM.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
6
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
10 version.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
11
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
15 more details.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
16
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
19
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
20
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
21 Contains the code for actually outputting the assembled code
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
22 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
23 #include <config.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
24 #include <errno.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
25 #include <stdio.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
26 #include <string.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
27 #include <unistd.h>
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
28
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
29 #include <lw_alloc.h>
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
30 #include <lw_expr.h>
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
31
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
32 #include "lwasm.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
33
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
34 void write_code_raw(asmstate_t *as, FILE *of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
35 void write_code_decb(asmstate_t *as, FILE *of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
36 void write_code_rawrel(asmstate_t *as, FILE *of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
37 void write_code_obj(asmstate_t *as, FILE *of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
38 void write_code_os9(asmstate_t *as, FILE *of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
39
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
40 // this prevents warnings about not using the return value of fwrite()
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
41 #define writebytes(s, l, c, f) do { int r; r = fwrite((s), (l), (c), (f)); } while (0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
42
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
43 void do_output(asmstate_t *as)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
44 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45 FILE *of;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
46
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
47 if (as -> errorcount > 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
48 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
49 fprintf(stderr, "Not doing output due to assembly errors.\n");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
50 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
51 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
52
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
53 of = fopen(as -> output_file, "wb");
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
54 if (!of)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
55 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
56 fprintf(stderr, "Cannot open '%s' for output", as -> output_file);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
57 perror("");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
58 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
59 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
60
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
61 switch (as -> output_format)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
62 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
63 case OUTPUT_RAW:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
64 write_code_raw(as, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
65 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
66
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
67 case OUTPUT_DECB:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
68 write_code_decb(as, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
69 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
70
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
71 case OUTPUT_RAWREL:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
72 write_code_rawrel(as, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
73 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
74
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
75 case OUTPUT_OBJ:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
76 write_code_obj(as, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
77 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
78
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
79 case OUTPUT_OS9:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
80 write_code_os9(as, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
81 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
82
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
83 default:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
84 fprintf(stderr, "BUG: unrecognized output format when generating output file\n");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
85 fclose(of);
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
86 unlink(as -> output_file);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
87 return;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
88 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
89
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
90 fclose(of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
91 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
92
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
93 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
94 rawrel output treats an ORG directive as an offset from the start of the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
95 file. Undefined results will occur if an ORG directive moves the output
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
96 pointer backward. This particular implementation uses "fseek" to handle
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
97 ORG requests and to skip over RMBs.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
98
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
99 This simple brain damanged method simply does an fseek before outputting
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
100 each instruction.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
101 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
102 void write_code_rawrel(asmstate_t *as, FILE *of)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
103 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
104 line_t *cl;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
105
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
106 for (cl = as -> line_head; cl; cl = cl -> next)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
107 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
108 if (cl -> outputl <= 0)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
109 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
110
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
111 fseek(of, lw_expr_intval(cl -> addr), SEEK_SET);
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
112 writebytes(cl -> output, cl -> outputl, 1, of);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
113 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
114 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
115
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
116 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
117 raw merely writes all the bytes directly to the file as is. ORG is just a
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
118 reference for the assembler to handle absolute references. Multiple ORG
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
119 statements will produce mostly useless results
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
120 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
121 void write_code_raw(asmstate_t *as, FILE *of)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
122 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
123 line_t *cl;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
124
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
125 for (cl = as -> line_head; cl; cl = cl -> next)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
126 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
127 if (cl -> len > 0 && cl -> outputl == 0)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
128 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
129 int i;
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
130 for (i = 0; i < cl -> len; i++)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
131 writebytes("\0", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
132 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
133 }
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
134 else if (cl -> outputl > 0)
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
135 writebytes(cl -> output, cl -> outputl, 1, of);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
136 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
137 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
138
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
139
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
140 /*
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
141 OS9 target also just writes all the bytes in order. No need for anything
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
142 else.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
143 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
144 void write_code_os9(asmstate_t *as, FILE *of)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
145 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
146 line_t *cl;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
147
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
148 for (cl = as -> line_head; cl; cl = cl -> next)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
149 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
150 if (cl -> inmod == 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
151 continue;
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
152 if (cl -> len > 0 && cl -> outputl == 0)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
153 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
154 int i;
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
155 for (i = 0; i < cl -> len; i++)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
156 writebytes("\0", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
157 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
158 }
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
159 else if (cl -> outputl > 0)
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
160 writebytes(cl -> output, cl -> outputl, 1, of);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
161 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
162 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
163
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
164 void write_code_decb(asmstate_t *as, FILE *of)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
165 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
166 long preambloc;
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
167 line_t *cl;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
168 int blocklen = -1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
169 int nextcalc = -1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
170 unsigned char outbuf[5];
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
171 int caddr;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
172
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
173 for (cl = as -> line_head; cl; cl = cl -> next)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
174 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
175 if (cl -> outputl < 0)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
176 continue;
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
177 caddr = lw_expr_intval(cl -> addr);
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
178 if (caddr != nextcalc && cl -> outputl > 0)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
179 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
180 // need preamble here
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
181 if (blocklen > 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
182 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
183 // update previous preamble if needed
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
184 fseek(of, preambloc, SEEK_SET);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
185 outbuf[0] = (blocklen >> 8) & 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
186 outbuf[1] = blocklen & 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
187 writebytes(outbuf, 2, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
188 fseek(of, 0, SEEK_END);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
189 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
190 blocklen = 0;
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
191 nextcalc = caddr;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
192 outbuf[0] = 0x00;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
193 outbuf[1] = 0x00;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
194 outbuf[2] = 0x00;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
195 outbuf[3] = (nextcalc >> 8) & 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
196 outbuf[4] = nextcalc & 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
197 preambloc = ftell(of) + 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
198 writebytes(outbuf, 5, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
199 }
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
200 nextcalc += cl -> outputl;
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
201 writebytes(cl -> output, cl -> outputl, 1, of);
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
202 blocklen += cl -> outputl;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
203 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
204 if (blocklen > 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
205 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
206 fseek(of, preambloc, SEEK_SET);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
207 outbuf[0] = (blocklen >> 8) & 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
208 outbuf[1] = blocklen & 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
209 writebytes(outbuf, 2, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
210 fseek(of, 0, SEEK_END);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
211 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
212
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
213 // now write postamble
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
214 outbuf[0] = 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
215 outbuf[1] = 0x00;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
216 outbuf[2] = 0x00;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
217 outbuf[3] = (as -> execaddr >> 8) & 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
218 outbuf[4] = (as -> execaddr) & 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
219 writebytes(outbuf, 5, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
220 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
221
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
222 void write_code_obj_sbadd(sectiontab_t *s, unsigned char b)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
223 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
224 if (s -> oblen >= s -> obsize)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
225 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
226 s -> obytes = lw_realloc(s -> obytes, s -> obsize + 128);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
227 s -> obsize += 128;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
228 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
229 s -> obytes[s -> oblen] = b;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
230 s -> oblen += 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
231 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
232
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
233
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
234 int write_code_obj_expraux(lw_expr_t e, void *of)
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
235 {
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
236 int tt;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
237 int v;
448
5cccf90bf838 Fixed bug with complex external references generating invalid relocations in the object file
lost@l-w.ca
parents: 412
diff changeset
238 int count = 1;
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
239 unsigned char buf[16];
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
240
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
241 tt = lw_expr_type(e);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
242
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
243 switch (tt)
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
244 {
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
245 case lw_expr_type_oper:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
246 buf[0] = 0x04;
448
5cccf90bf838 Fixed bug with complex external references generating invalid relocations in the object file
lost@l-w.ca
parents: 412
diff changeset
247
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
248 switch (lw_expr_whichop(e))
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
249 {
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
250 case lw_expr_oper_plus:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
251 buf[1] = 0x01;
448
5cccf90bf838 Fixed bug with complex external references generating invalid relocations in the object file
lost@l-w.ca
parents: 412
diff changeset
252 count = lw_expr_operandcount(e) - 1;
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
253 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
254
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
255 case lw_expr_oper_minus:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
256 buf[1] = 0x02;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
257 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
258
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
259 case lw_expr_oper_times:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
260 buf[1] = 0x03;
448
5cccf90bf838 Fixed bug with complex external references generating invalid relocations in the object file
lost@l-w.ca
parents: 412
diff changeset
261 count = lw_expr_operandcount(e) - 1;
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
262 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
263
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
264 case lw_expr_oper_divide:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
265 buf[1] = 0x04;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
266 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
267
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
268 case lw_expr_oper_mod:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
269 buf[1] = 0x05;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
270 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
271
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
272 case lw_expr_oper_intdiv:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
273 buf[1] = 0x06;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
274 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
275
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
276 case lw_expr_oper_bwand:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
277 buf[1] = 0x07;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
278 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
279
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
280 case lw_expr_oper_bwor:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
281 buf[1] = 0x08;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
282 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
283
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
284 case lw_expr_oper_bwxor:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
285 buf[1] = 0x09;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
286 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
287
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
288 case lw_expr_oper_and:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
289 buf[1] = 0x0A;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
290 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
291
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
292 case lw_expr_oper_or:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
293 buf[1] = 0x0B;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
294 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
295
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
296 case lw_expr_oper_neg:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
297 buf[1] = 0x0C;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
298 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
299
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
300 case lw_expr_oper_com:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
301 buf[1] = 0x0D;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
302 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
303
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
304 default:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
305 buf[1] = 0xff;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
306 }
448
5cccf90bf838 Fixed bug with complex external references generating invalid relocations in the object file
lost@l-w.ca
parents: 412
diff changeset
307 while (count--)
5cccf90bf838 Fixed bug with complex external references generating invalid relocations in the object file
lost@l-w.ca
parents: 412
diff changeset
308 writebytes(buf, 2, 1, of);
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
309 return 0;
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
310
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
311 case lw_expr_type_int:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
312 v = lw_expr_intval(e);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
313 buf[0] = 0x01;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
314 buf[1] = (v >> 8) & 0xff;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
315 buf[2] = v & 0xff;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
316 writebytes(buf, 3, 1, of);
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
317 return 0;
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
318
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
319 case lw_expr_type_special:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
320 v = lw_expr_specint(e);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
321 switch (v)
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
322 {
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
323 case lwasm_expr_secbase:
377
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
324 {
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
325 // replaced with a synthetic symbol
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
326 sectiontab_t *se;
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
327 se = lw_expr_specptr(e);
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
328
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
329 writebytes("\x03\x02", 2, 1, of);
377
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
330 writebytes(se -> name, strlen(se -> name) + 1, 1, of);
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
331 return 0;
377
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
332 }
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
333 case lwasm_expr_import:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
334 {
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
335 importlist_t *ie;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
336 ie = lw_expr_specptr(e);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
337 buf[0] = 0x02;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
338 writebytes(buf, 1, 1, of);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
339 writebytes(ie -> symbol, strlen(ie -> symbol) + 1, 1, of);
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
340 return 0;
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
341 }
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
342 case lwasm_expr_syment:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
343 {
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
344 struct symtabe *se;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
345 se = lw_expr_specptr(e);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
346 buf[0] = 0x03;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
347 writebytes(buf, 1, 1, of);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
348 writebytes(se -> symbol, strlen(se -> symbol), 1, of);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
349 if (se -> context != -1)
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
350 {
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
351 sprintf(buf, "\x01%d", se -> context);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
352 writebytes(buf, strlen(buf), 1, of);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
353 }
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
354 writebytes("", 1, 1, of);
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
355 return 0;
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
356 }
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
357 }
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
358
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
359 default:
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
360 // unrecognized term type - replace with integer 0
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
361 // fprintf(stderr, "Unrecognized term type: %s\n", lw_expr_print(e));
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
362 buf[0] = 0x01;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
363 buf[1] = 0x00;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
364 buf[2] = 0x00;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
365 writebytes(buf, 3, 1, of);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
366 break;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
367 }
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
368 return 0;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
369 }
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
370
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
371
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
372 void write_code_obj(asmstate_t *as, FILE *of)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
373 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
374 line_t *l;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
375 sectiontab_t *s;
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
376 reloctab_t *re;
375
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
377 exportlist_t *ex;
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
378 struct symtabe *se;
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
379
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
380 int i;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
381 unsigned char buf[16];
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
382
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
383 // output the magic number and file header
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
384 // the 8 is NOT an error
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
385 writebytes("LWOBJ16", 8, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
386
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
387 // run through the entire system and build the byte streams for each
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
388 // section; at the same time, generate a list of "local" symbols to
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
389 // output for each section
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
390 // NOTE: for "local" symbols, we will append \x01 and the ascii string
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
391 // of the context identifier (so sym in context 1 would be "sym\x011"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
392 // we can do this because the linker can handle symbols with any
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
393 // character other than NUL.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
394 // also we will generate a list of incomplete references for each
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
395 // section along with the actual definition that will be output
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
396
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
397 // once all this information is generated, we will output each section
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
398 // to the file
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
399
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
400 // NOTE: we build everything in memory then output it because the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
401 // assembler accepts multiple instances of the same section but the
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
402 // linker expects only one instance of each section in the object file
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
403 // so we need to collect all the various pieces of a section together
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
404 // (also, the assembler treated multiple instances of the same section
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
405 // as continuations of previous sections so we would need to collect
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
406 // them together anyway.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
407
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
408 for (l = as -> line_head; l; l = l -> next)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
409 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
410 if (l -> csect)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
411 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
412 // we're in a section - need to output some bytes
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
413 if (l -> outputl > 0)
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
414 for (i = 0; i < l -> outputl; i++)
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
415 write_code_obj_sbadd(l -> csect, l -> output[i]);
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
416 else if (l -> outputl == 0 || l -> outputl == -1)
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
417 for (i = 0; i < l -> len; i++)
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
418 write_code_obj_sbadd(l -> csect, 0);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
419 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
420 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
421
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
422 // run through the sections
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
423 for (s = as -> sections; s; s = s -> next)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
424 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
425 // write the name
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
426 writebytes(s -> name, strlen(s -> name) + 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
427
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
428 // write the flags
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
429 if (s -> flags & section_flag_bss)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
430 writebytes("\x01", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
431
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
432 // indicate end of flags - the "" is NOT an error
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
433 writebytes("", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
434
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
435 // now the local symbols
377
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
436
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
437 // a symbol for section base address
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
438 writebytes("\x02", 1, 1, of);
55ed7d06b136 Fixed intersection internal references in object target
lost@starbug
parents: 376
diff changeset
439 writebytes(s -> name, strlen(s -> name) + 1, 1, of);
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
440 // address 0; "\0" is not an error
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
441 writebytes("\0", 2, 1, of);
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
442 for (se = as -> symtab.head; se; se = se -> next)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
443 {
393
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
444 lw_expr_t te;
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
445
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
446 debug_message(as, 200, "Consider symbol %s (%p) for export in section %p", se -> symbol, se -> section, s);
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
447
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
448 // ignore symbols not in this section
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
449 if (se -> section != s)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
450 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
451
393
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
452 debug_message(as, 200, " In section");
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
453
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
454 if (se -> flags & symbol_flag_set)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
455 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
456
393
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
457 debug_message(as, 200, " Not symbol_flag_set");
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
458
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
459 te = lw_expr_copy(se -> value);
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
460 debug_message(as, 200, " Value=%s", lw_expr_print(te));
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
461 as -> exportcheck = 1;
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
462 as -> csect = s;
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
463 lwasm_reduce_expr(as, te);
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
464 as -> exportcheck = 0;
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
465
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
466 debug_message(as, 200, " Value2=%s", lw_expr_print(te));
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
467
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
468 // don't output non-constant symbols
393
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
469 if (!lw_expr_istype(te, lw_expr_type_int))
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
470 {
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
471 lw_expr_destroy(te);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
472 continue;
393
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
473 }
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
474
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
475 writebytes(se -> symbol, strlen(se -> symbol), 1, of);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
476 if (se -> context >= 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
477 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
478 writebytes("\x01", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
479 sprintf(buf, "%d", se -> context);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
480 writebytes(buf, strlen(buf), 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
481 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
482 // the "" is NOT an error
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
483 writebytes("", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
484
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
485 // write the address
393
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
486 buf[0] = (lw_expr_intval(te) >> 8) & 0xff;
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
487 buf[1] = lw_expr_intval(te) & 0xff;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
488 writebytes(buf, 2, 1, of);
393
c94436adce83 Actually include local symbols in object files
lost@l-w.ca
parents: 387
diff changeset
489 lw_expr_destroy(te);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
490 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
491 // flag end of local symbol table - "" is NOT an error
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
492 writebytes("", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
493
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
494 // now the exports -- FIXME
375
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
495 for (ex = as -> exportlist; ex; ex = ex -> next)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
496 {
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
497 int eval;
375
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
498 lw_expr_t te;
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
499 line_t tl;
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
500
412
35a0b086bf4a Added dummy .bank pseudo and fixed segfault in output for object target (on imported symbol)
lost@l-w.ca
parents: 393
diff changeset
501 if (ex -> se == NULL)
35a0b086bf4a Added dummy .bank pseudo and fixed segfault in output for object target (on imported symbol)
lost@l-w.ca
parents: 393
diff changeset
502 continue;
375
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
503 if (ex -> se -> section != s)
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
504 continue;
375
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
505 te = lw_expr_copy(ex -> se -> value);
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
506 as -> csect = ex -> se -> section;
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
507 as -> exportcheck = 1;
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
508 tl.as = as;
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
509 as -> cl = &tl;
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
510 lwasm_reduce_expr(as, te);
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
511 as -> exportcheck = 0;
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
512 as -> cl = NULL;
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
513 if (!lw_expr_istype(te, lw_expr_type_int))
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
514 {
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
515 lw_expr_destroy(te);
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
516 continue;
375
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
517 }
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
518 eval = lw_expr_intval(te);
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
519 lw_expr_destroy(te);
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
520 writebytes(ex -> symbol, strlen(ex -> symbol) + 1, 1, of);
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
521 buf[0] = (eval >> 8) & 0xff;
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
522 buf[1] = eval & 0xff;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
523 writebytes(buf, 2, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
524 }
375
3498b2d88376 Added export list to object output
lost@starbug
parents: 374
diff changeset
525
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
526 // flag end of exported symbols - "" is NOT an error
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
527 writebytes("", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
528
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
529 // FIXME - relocation table
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
530 for (re = s -> reloctab; re; re = re -> next)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
531 {
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
532 int offset;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
533 lw_expr_t te;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
534 line_t tl;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
535
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
536 tl.as = as;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
537 as -> cl = &tl;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
538 as -> csect = s;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
539 as -> exportcheck = 1;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
540
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
541 if (re -> expr == NULL)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
542 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
543 // this is an error but we'll simply ignore it
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
544 // and not output this expression
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
545 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
546 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
547
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
548 // work through each term in the expression and output
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
549 // the proper equivalent to the object file
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
550 if (re -> size == 1)
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
551 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
552 // flag an 8 bit relocation (low 8 bits will be used)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
553 buf[0] = 0xFF;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
554 buf[1] = 0x01;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
555 writebytes(buf, 2, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
556 }
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
557
387
a741d2e4869f Various bugfixes; fixed lwobjdump to display symbols with unprintable characters more sensibly; start of a (nonfunctional for now) testing framework
lost@l-w.ca
parents: 377
diff changeset
558 te = lw_expr_copy(re -> offset);
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
559 lwasm_reduce_expr(as, te);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
560 if (!lw_expr_istype(te, lw_expr_type_int))
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
561 {
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
562 lw_expr_destroy(te);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
563 continue;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
564 }
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
565 offset = lw_expr_intval(te);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
566 lw_expr_destroy(te);
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
567
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
568 // output expression
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
569 lw_expr_testterms(re -> expr, write_code_obj_expraux, of);
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
570
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
571 // flag end of expressions
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
572 writebytes("", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
573
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
574 // write the offset
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
575 buf[0] = (offset >> 8) & 0xff;
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
576 buf[1] = offset & 0xff;
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
577 writebytes(buf, 2, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
578 }
376
91c0fe026940 Output incomplete references in object target
lost@starbug
parents: 375
diff changeset
579
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
580 // flag end of incomplete references list
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
581 writebytes("", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
582
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
583 // now blast out the code
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
584
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
585 // length
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
586 buf[0] = s -> oblen >> 8 & 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
587 buf[1] = s -> oblen & 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
588 writebytes(buf, 2, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
589
374
d99322ef6f21 Stage 1: actually do output
lost@starbug
parents: 339
diff changeset
590 if (!(s -> flags & section_flag_bss))
339
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
591 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
592 writebytes(s -> obytes, s -> oblen, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
593 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
594 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
595
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
596 // flag no more sections
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
597 // the "" is NOT an error
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
598 writebytes("", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
599 }