Mercurial > hg > index.cgi
comparison lwlink/output.c @ 0:2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
author | lost@l-w.ca |
---|---|
date | Wed, 19 Jan 2011 22:27:17 -0700 |
parents | |
children | 08fb11004df9 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:2c24602be78f |
---|---|
1 /* | |
2 output.c | |
3 Copyright © 2009 William Astle | |
4 | |
5 This file is part of LWLINK. | |
6 | |
7 LWLINK is free software: you can redistribute it and/or modify it under the | |
8 terms of the GNU General Public License as published by the Free Software | |
9 Foundation, either version 3 of the License, or (at your option) any later | |
10 version. | |
11 | |
12 This program is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
15 more details. | |
16 | |
17 You should have received a copy of the GNU General Public License along with | |
18 this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 | |
21 Actually output the binary | |
22 */ | |
23 | |
24 #include <stdio.h> | |
25 #include <stdlib.h> | |
26 #include <string.h> | |
27 | |
28 #include "lwlink.h" | |
29 | |
30 // this prevents warnings about not using the return value of fwrite() | |
31 // and, theoretically, can be replaced with a function that handles things | |
32 // better in the future | |
33 #define writebytes(s, l, c, f) do { int r; r = fwrite((s), (l), (c), (f)); } while (0) | |
34 | |
35 void do_output_decb(FILE *of); | |
36 void do_output_raw(FILE *of); | |
37 void do_output_lwex0(FILE *of); | |
38 | |
39 void do_output(void) | |
40 { | |
41 FILE *of; | |
42 | |
43 of = fopen(outfile, "wb"); | |
44 if (!of) | |
45 { | |
46 fprintf(stderr, "Cannot open output file %s: ", outfile); | |
47 perror(""); | |
48 exit(1); | |
49 } | |
50 | |
51 switch (outformat) | |
52 { | |
53 case OUTPUT_DECB: | |
54 do_output_decb(of); | |
55 break; | |
56 | |
57 case OUTPUT_RAW: | |
58 do_output_raw(of); | |
59 break; | |
60 | |
61 case OUTPUT_LWEX0: | |
62 do_output_lwex0(of); | |
63 break; | |
64 | |
65 default: | |
66 fprintf(stderr, "Unknown output format doing output!\n"); | |
67 exit(111); | |
68 } | |
69 | |
70 fclose(of); | |
71 } | |
72 | |
73 void do_output_decb(FILE *of) | |
74 { | |
75 int sn, sn2; | |
76 int cloc, olen; | |
77 unsigned char buf[5]; | |
78 | |
79 for (sn = 0; sn < nsects; sn++) | |
80 { | |
81 if (sectlist[sn].ptr -> flags & SECTION_BSS) | |
82 { | |
83 // no output for a BSS section | |
84 continue; | |
85 } | |
86 if (sectlist[sn].ptr -> codesize == 0) | |
87 { | |
88 // don't generate output for a zero size section | |
89 continue; | |
90 } | |
91 | |
92 // calculate the length of this output block | |
93 cloc = sectlist[sn].ptr -> loadaddress; | |
94 olen = 0; | |
95 for (sn2 = sn; sn2 < nsects; sn2++) | |
96 { | |
97 // ignore BSS sections | |
98 if (sectlist[sn2].ptr -> flags & SECTION_BSS) | |
99 continue; | |
100 // ignore zero length sections | |
101 if (sectlist[sn2].ptr -> codesize == 0) | |
102 continue; | |
103 if (cloc != sectlist[sn2].ptr -> loadaddress) | |
104 break; | |
105 olen += sectlist[sn2].ptr -> codesize; | |
106 cloc += sectlist[sn2].ptr -> codesize; | |
107 } | |
108 | |
109 // write a preamble | |
110 buf[0] = 0x00; | |
111 buf[1] = olen >> 8; | |
112 buf[2] = olen & 0xff; | |
113 buf[3] = sectlist[sn].ptr -> loadaddress >> 8; | |
114 buf[4] = sectlist[sn].ptr -> loadaddress & 0xff; | |
115 writebytes(buf, 1, 5, of); | |
116 for (; sn < sn2; sn++) | |
117 { | |
118 if (sectlist[sn].ptr -> flags & SECTION_BSS) | |
119 continue; | |
120 if (sectlist[sn].ptr -> codesize == 0) | |
121 continue; | |
122 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of); | |
123 } | |
124 sn--; | |
125 } | |
126 // write a postamble | |
127 buf[0] = 0xff; | |
128 buf[1] = 0x00; | |
129 buf[2] = 0x00; | |
130 buf[3] = linkscript.execaddr >> 8; | |
131 buf[4] = linkscript.execaddr & 0xff; | |
132 writebytes(buf, 1, 5, of); | |
133 } | |
134 | |
135 void do_output_raw(FILE *of) | |
136 { | |
137 int nskips = 0; // used to output blanks for BSS inline | |
138 int sn; | |
139 | |
140 for (sn = 0; sn < nsects; sn++) | |
141 { | |
142 if (sectlist[sn].ptr -> flags & SECTION_BSS) | |
143 { | |
144 // no output for a BSS section | |
145 nskips += sectlist[sn].ptr -> codesize; | |
146 continue; | |
147 } | |
148 while (nskips > 0) | |
149 { | |
150 // the "" is not an error - it turns into a single NUL byte! | |
151 writebytes("", 1, 1, of); | |
152 nskips--; | |
153 } | |
154 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of); | |
155 } | |
156 } | |
157 | |
158 void do_output_lwex0(FILE *of) | |
159 { | |
160 int nskips = 0; // used to output blanks for BSS inline | |
161 int sn; | |
162 int codedatasize = 0; | |
163 unsigned char buf[32]; | |
164 | |
165 // calculate items for the file header | |
166 for (sn = 0; sn < nsects; sn++) | |
167 { | |
168 if (sectlist[sn].ptr -> flags & SECTION_BSS) | |
169 { | |
170 // no output for a BSS section | |
171 nskips += sectlist[sn].ptr -> codesize; | |
172 continue; | |
173 } | |
174 codedatasize += nskips; | |
175 nskips = 0; | |
176 codedatasize += sectlist[sn].ptr -> codesize; | |
177 } | |
178 | |
179 // output the file header | |
180 buf[0] = 'L'; | |
181 buf[1] = 'W'; | |
182 buf[2] = 'E'; | |
183 buf[3] = 'X'; | |
184 buf[4] = 0; // version 0 | |
185 buf[5] = 0; // low stack | |
186 buf[6] = linkscript.stacksize / 256; | |
187 buf[7] = linkscript.stacksize & 0xff; | |
188 buf[8] = nskips / 256; | |
189 buf[9] = nskips & 0xff; | |
190 buf[10] = codedatasize / 256; | |
191 buf[11] = codedatasize & 0xff; | |
192 buf[12] = linkscript.execaddr / 256; | |
193 buf[13] = linkscript.execaddr & 0xff; | |
194 memset(buf + 14, 0, 18); | |
195 | |
196 writebytes(buf, 1, 32, of); | |
197 // output the data | |
198 // NOTE: disjoint load addresses will not work correctly!!!!! | |
199 for (sn = 0; sn < nsects; sn++) | |
200 { | |
201 if (sectlist[sn].ptr -> flags & SECTION_BSS) | |
202 { | |
203 // no output for a BSS section | |
204 nskips += sectlist[sn].ptr -> codesize; | |
205 continue; | |
206 } | |
207 while (nskips > 0) | |
208 { | |
209 // the "" is not an error - it turns into a single NUL byte! | |
210 writebytes("", 1, 1, of); | |
211 nskips--; | |
212 } | |
213 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of); | |
214 } | |
215 } |