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