Mercurial > hg-old > index.cgi
annotate lwlink/output.c @ 183:302b8db5fd89
modified lwlink to merge contiguous sections in the DECB output file to avoid the explosion of preambles
author | lost |
---|---|
date | Sat, 21 Mar 2009 17:03:42 +0000 |
parents | 106c2fe3c9d9 |
children | 857cb407229e |
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> | |
30 | |
31 #include "lwlink.h" | |
32 | |
33 // this prevents warnings about not using the return value of fwrite() | |
34 // and, theoretically, can be replaced with a function that handles things | |
35 // better in the future | |
36 #define writebytes(s, l, c, f) do { int r; r = fwrite((s), (l), (c), (f)); } while (0) | |
37 | |
38 void do_output_decb(FILE *of); | |
39 void do_output_raw(FILE *of); | |
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; | |
62 | |
63 default: | |
64 fprintf(stderr, "Unknown output format doing output!\n"); | |
65 exit(111); | |
66 } | |
67 | |
68 fclose(of); | |
69 } | |
70 | |
71 void do_output_decb(FILE *of) | |
72 { | |
183
302b8db5fd89
modified lwlink to merge contiguous sections in the DECB output file to avoid the explosion of preambles
lost
parents:
139
diff
changeset
|
73 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
|
74 int cloc, olen; |
121 | 75 unsigned char buf[5]; |
76 | |
77 for (sn = 0; sn < nsects; sn++) | |
78 { | |
79 if (sectlist[sn].ptr -> flags & SECTION_BSS) | |
80 { | |
81 // no output for a BSS section | |
82 continue; | |
83 } | |
132 | 84 if (sectlist[sn].ptr -> codesize == 0) |
85 { | |
86 // don't generate output for a zero size section | |
87 continue; | |
88 } | |
183
302b8db5fd89
modified lwlink to merge contiguous sections in the DECB output file to avoid the explosion of preambles
lost
parents:
139
diff
changeset
|
89 |
302b8db5fd89
modified lwlink to merge contiguous sections in the DECB output file to avoid the explosion of preambles
lost
parents:
139
diff
changeset
|
90 // 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
|
91 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
|
92 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
|
93 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
|
94 { |
302b8db5fd89
modified lwlink to merge contiguous sections in the DECB output file to avoid the explosion of preambles
lost
parents:
139
diff
changeset
|
95 // 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
|
96 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
|
97 continue; |
302b8db5fd89
modified lwlink to merge contiguous sections in the DECB output file to avoid the explosion of preambles
lost
parents:
139
diff
changeset
|
98 // 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
|
99 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
|
100 continue; |
302b8db5fd89
modified lwlink to merge contiguous sections in the DECB output file to avoid the explosion of preambles
lost
parents:
139
diff
changeset
|
101 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
|
102 break; |
302b8db5fd89
modified lwlink to merge contiguous sections in the DECB output file to avoid the explosion of preambles
lost
parents:
139
diff
changeset
|
103 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
|
104 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
|
105 } |
302b8db5fd89
modified lwlink to merge contiguous sections in the DECB output file to avoid the explosion of preambles
lost
parents:
139
diff
changeset
|
106 |
121 | 107 // write a preamble |
108 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
|
109 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
|
110 buf[2] = olen & 0xff; |
121 | 111 buf[3] = sectlist[sn].ptr -> loadaddress >> 8; |
112 buf[4] = sectlist[sn].ptr -> loadaddress & 0xff; | |
113 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
|
114 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
|
115 { |
302b8db5fd89
modified lwlink to merge contiguous sections in the DECB output file to avoid the explosion of preambles
lost
parents:
139
diff
changeset
|
116 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
|
117 continue; |
302b8db5fd89
modified lwlink to merge contiguous sections in the DECB output file to avoid the explosion of preambles
lost
parents:
139
diff
changeset
|
118 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
|
119 continue; |
302b8db5fd89
modified lwlink to merge contiguous sections in the DECB output file to avoid the explosion of preambles
lost
parents:
139
diff
changeset
|
120 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
|
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 sn--; |
121 | 123 } |
124 // write a postamble | |
125 buf[0] = 0xff; | |
126 buf[1] = 0x00; | |
127 buf[2] = 0x00; | |
128 buf[3] = linkscript.execaddr >> 8; | |
129 buf[4] = linkscript.execaddr & 0xff; | |
130 writebytes(buf, 1, 5, of); | |
131 } | |
132 | |
133 void do_output_raw(FILE *of) | |
134 { | |
123 | 135 int nskips = 0; // used to output blanks for BSS inline |
136 int sn; | |
137 | |
138 for (sn = 0; sn < nsects; sn++) | |
139 { | |
140 if (sectlist[sn].ptr -> flags & SECTION_BSS) | |
141 { | |
142 // no output for a BSS section | |
143 nskips += sectlist[sn].ptr -> codesize; | |
144 continue; | |
145 } | |
146 while (nskips > 0) | |
147 { | |
148 // the "" is not an error - it turns into a single NUL byte! | |
149 writebytes("", 1, 1, of); | |
150 nskips--; | |
151 } | |
152 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of); | |
153 } | |
121 | 154 } |