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