annotate old-trunk/lwlink/output.c @ 356:7166254491ed

Finished pseudo ops
author lost@starbug
date Wed, 31 Mar 2010 18:46:32 -0600
parents eb230fa7d28e
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
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
3 Copyright © 2009 William Astle
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 LWLINK.
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
6
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
7 LWLINK 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 Actually output the binary
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
22 */
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
23
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
24 #include <config.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
25
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
26 #include <stdio.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
27 #include <stdlib.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
28 #include <string.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
29
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
30 #include "lwlink.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
31
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
32 // this prevents warnings about not using the return value of fwrite()
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
33 // and, theoretically, can be replaced with a function that handles things
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
34 // better in the future
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
35 #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
36
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
37 void do_output_decb(FILE *of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
38 void do_output_raw(FILE *of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
39 void do_output_lwex0(FILE *of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
40
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
41 void do_output(void)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
42 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
43 FILE *of;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
44
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45 of = fopen(outfile, "wb");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
46 if (!of)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
47 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
48 fprintf(stderr, "Cannot open output file %s: ", outfile);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
49 perror("");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
50 exit(1);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
51 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
52
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
53 switch (outformat)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
54 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
55 case OUTPUT_DECB:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
56 do_output_decb(of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
57 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
58
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
59 case OUTPUT_RAW:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
60 do_output_raw(of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
61 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
62
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
63 case OUTPUT_LWEX0:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
64 do_output_lwex0(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 default:
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
68 fprintf(stderr, "Unknown output format doing output!\n");
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
69 exit(111);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
70 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
71
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
72 fclose(of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
73 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
74
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
75 void do_output_decb(FILE *of)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
76 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
77 int sn, sn2;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
78 int cloc, olen;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
79 unsigned char buf[5];
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
80
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
81 for (sn = 0; sn < nsects; sn++)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
82 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
83 if (sectlist[sn].ptr -> flags & SECTION_BSS)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
84 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
85 // no output for a BSS section
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
86 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
87 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
88 if (sectlist[sn].ptr -> codesize == 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
89 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
90 // don't generate output for a zero size section
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
91 continue;
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 // calculate the length of this output block
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
95 cloc = sectlist[sn].ptr -> loadaddress;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
96 olen = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
97 for (sn2 = sn; sn2 < nsects; sn2++)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
98 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
99 // ignore BSS sections
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
100 if (sectlist[sn2].ptr -> flags & SECTION_BSS)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
101 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
102 // ignore zero length sections
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
103 if (sectlist[sn2].ptr -> codesize == 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
104 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
105 if (cloc != sectlist[sn2].ptr -> loadaddress)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
106 break;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
107 olen += sectlist[sn2].ptr -> codesize;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
108 cloc += sectlist[sn2].ptr -> codesize;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
109 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
110
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
111 // write a preamble
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
112 buf[0] = 0x00;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
113 buf[1] = olen >> 8;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
114 buf[2] = olen & 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
115 buf[3] = sectlist[sn].ptr -> loadaddress >> 8;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
116 buf[4] = sectlist[sn].ptr -> loadaddress & 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
117 writebytes(buf, 1, 5, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
118 for (; sn < sn2; sn++)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
119 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
120 if (sectlist[sn].ptr -> flags & SECTION_BSS)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
121 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
122 if (sectlist[sn].ptr -> codesize == 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
123 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
124 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
125 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
126 sn--;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
127 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
128 // write a postamble
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
129 buf[0] = 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
130 buf[1] = 0x00;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
131 buf[2] = 0x00;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
132 buf[3] = linkscript.execaddr >> 8;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
133 buf[4] = linkscript.execaddr & 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
134 writebytes(buf, 1, 5, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
135 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
136
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
137 void do_output_raw(FILE *of)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
138 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
139 int nskips = 0; // used to output blanks for BSS inline
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
140 int sn;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
141
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
142 for (sn = 0; sn < nsects; sn++)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
143 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
144 if (sectlist[sn].ptr -> flags & SECTION_BSS)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
145 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
146 // no output for a BSS section
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
147 nskips += sectlist[sn].ptr -> codesize;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
148 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
149 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
150 while (nskips > 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
151 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
152 // the "" is not an error - it turns into a single NUL byte!
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
153 writebytes("", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
154 nskips--;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
155 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
156 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
157 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
158 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
159
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
160 void do_output_lwex0(FILE *of)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
161 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
162 int nskips = 0; // used to output blanks for BSS inline
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
163 int sn;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
164 int codedatasize = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
165 unsigned char buf[32];
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
166
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
167 // calculate items for the file header
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
168 for (sn = 0; sn < nsects; sn++)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
169 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
170 if (sectlist[sn].ptr -> flags & SECTION_BSS)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
171 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
172 // no output for a BSS section
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
173 nskips += sectlist[sn].ptr -> codesize;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
174 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
175 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
176 codedatasize += nskips;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
177 nskips = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
178 codedatasize += sectlist[sn].ptr -> codesize;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
179 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
180
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
181 // output the file header
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
182 buf[0] = 'L';
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
183 buf[1] = 'W';
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
184 buf[2] = 'E';
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
185 buf[3] = 'X';
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
186 buf[4] = 0; // version 0
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
187 buf[5] = 0; // low stack
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
188 buf[6] = linkscript.stacksize / 256;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
189 buf[7] = linkscript.stacksize & 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
190 buf[8] = nskips / 256;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
191 buf[9] = nskips & 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
192 buf[10] = codedatasize / 256;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
193 buf[11] = codedatasize & 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
194 buf[12] = linkscript.execaddr / 256;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
195 buf[13] = linkscript.execaddr & 0xff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
196 memset(buf + 14, 0, 18);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
197
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
198 writebytes(buf, 1, 32, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
199 // output the data
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
200 // NOTE: disjoint load addresses will not work correctly!!!!!
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
201 for (sn = 0; sn < nsects; sn++)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
202 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
203 if (sectlist[sn].ptr -> flags & SECTION_BSS)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
204 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
205 // no output for a BSS section
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
206 nskips += sectlist[sn].ptr -> codesize;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
207 continue;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
208 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
209 while (nskips > 0)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
210 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
211 // the "" is not an error - it turns into a single NUL byte!
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
212 writebytes("", 1, 1, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
213 nskips--;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
214 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
215 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
216 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
217 }