annotate lwlink/output.c @ 254:c7a41b4c89b3 2.x

Added struct support to LWASM
author lost
date Sat, 19 Dec 2009 06:38:43 +0000
parents 4dc2a10997a6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
1 /*
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
2 output.c
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
3 Copyright © 2009 William Astle
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
4
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
5 This file is part of LWLINK.
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
6
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
7 LWLINK is free software: you can redistribute it and/or modify it under the
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
10 version.
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
11
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
15 more details.
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
16
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
19
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
20
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
21 Actually output the binary
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
22 */
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
23
212
bae1e3ecdce1 More preparation for gnulib integration
lost
parents: 187
diff changeset
24 #include <config.h>
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
25
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
26 #include <stdio.h>
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
27 #include <stdlib.h>
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
28 #include <string.h>
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
29
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
30 #include "lwlink.h"
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
31
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
32 // this prevents warnings about not using the return value of fwrite()
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
33 // and, theoretically, can be replaced with a function that handles things
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
34 // better in the future
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
35 #define writebytes(s, l, c, f) do { int r; r = fwrite((s), (l), (c), (f)); } while (0)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
36
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
37 void do_output_decb(FILE *of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
38 void do_output_raw(FILE *of);
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
39 void do_output_lwex0(FILE *of);
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
40
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
41 void do_output(void)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
42 {
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
43 FILE *of;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
44
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
45 of = fopen(outfile, "wb");
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
46 if (!of)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
47 {
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
48 fprintf(stderr, "Cannot open output file %s: ", outfile);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
49 perror("");
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
50 exit(1);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
51 }
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
52
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
53 switch (outformat)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
54 {
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
55 case OUTPUT_DECB:
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
56 do_output_decb(of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
57 break;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
58
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
59 case OUTPUT_RAW:
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
60 do_output_raw(of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
61 break;
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
62
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
63 case OUTPUT_LWEX0:
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
64 do_output_lwex0(of);
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
65 break;
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
66
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
67 default:
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
68 fprintf(stderr, "Unknown output format doing output!\n");
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
69 exit(111);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
70 }
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
71
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
72 fclose(of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
73 }
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
74
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
75 void do_output_decb(FILE *of)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
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
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
79 unsigned char buf[5];
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
80
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
81 for (sn = 0; sn < nsects; sn++)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
82 {
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
83 if (sectlist[sn].ptr -> flags & SECTION_BSS)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
84 {
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
85 // no output for a BSS section
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
86 continue;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
87 }
132
b972e45b5e07 Don't generate a preamble for a zero-sized section
lost
parents: 123
diff changeset
88 if (sectlist[sn].ptr -> codesize == 0)
b972e45b5e07 Don't generate a preamble for a zero-sized section
lost
parents: 123
diff changeset
89 {
b972e45b5e07 Don't generate a preamble for a zero-sized section
lost
parents: 123
diff changeset
90 // don't generate output for a zero size section
b972e45b5e07 Don't generate a preamble for a zero-sized section
lost
parents: 123
diff changeset
91 continue;
b972e45b5e07 Don't generate a preamble for a zero-sized section
lost
parents: 123
diff changeset
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
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
111 // write a preamble
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
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
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
115 buf[3] = sectlist[sn].ptr -> loadaddress >> 8;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
116 buf[4] = sectlist[sn].ptr -> loadaddress & 0xff;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
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
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
127 }
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
128 // write a postamble
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
129 buf[0] = 0xff;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
130 buf[1] = 0x00;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
131 buf[2] = 0x00;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
132 buf[3] = linkscript.execaddr >> 8;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
133 buf[4] = linkscript.execaddr & 0xff;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
134 writebytes(buf, 1, 5, of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
135 }
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
136
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
137 void do_output_raw(FILE *of)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
138 {
123
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
139 int nskips = 0; // used to output blanks for BSS inline
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
140 int sn;
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
141
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
142 for (sn = 0; sn < nsects; sn++)
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
143 {
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
144 if (sectlist[sn].ptr -> flags & SECTION_BSS)
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
145 {
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
146 // no output for a BSS section
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
147 nskips += sectlist[sn].ptr -> codesize;
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
148 continue;
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
149 }
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
150 while (nskips > 0)
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
151 {
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
152 // the "" is not an error - it turns into a single NUL byte!
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
153 writebytes("", 1, 1, of);
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
154 nskips--;
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
155 }
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
156 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
157 }
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
158 }
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
159
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
160 void do_output_lwex0(FILE *of)
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
161 {
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
162 int nskips = 0; // used to output blanks for BSS inline
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
163 int sn;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
164 int codedatasize = 0;
252
4dc2a10997a6 Fixed LWEX binary output when BSS size > 0
lost
parents: 212
diff changeset
165 int coffs;
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
166 unsigned char buf[32];
252
4dc2a10997a6 Fixed LWEX binary output when BSS size > 0
lost
parents: 212
diff changeset
167 int fsize = 0;
4dc2a10997a6 Fixed LWEX binary output when BSS size > 0
lost
parents: 212
diff changeset
168
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
169 // calculate items for the file header
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
170 for (sn = 0; sn < nsects; sn++)
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
171 {
252
4dc2a10997a6 Fixed LWEX binary output when BSS size > 0
lost
parents: 212
diff changeset
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
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
173 if (sectlist[sn].ptr -> flags & SECTION_BSS)
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
174 {
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
175 // no output for a BSS section
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
176 nskips += sectlist[sn].ptr -> codesize;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
177 continue;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
178 }
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
179 codedatasize += nskips;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
180 nskips = 0;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
181 codedatasize += sectlist[sn].ptr -> codesize;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
182 }
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
183
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
184 // output the file header
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
185 buf[0] = 'L';
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
186 buf[1] = 'W';
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
187 buf[2] = 'E';
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
188 buf[3] = 'X';
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
189 buf[4] = 0; // version 0
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
190 buf[5] = 0; // low stack
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
191 buf[6] = linkscript.stacksize / 256;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
192 buf[7] = linkscript.stacksize & 0xff;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
193 buf[8] = nskips / 256;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
194 buf[9] = nskips & 0xff;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
195 buf[10] = codedatasize / 256;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
196 buf[11] = codedatasize & 0xff;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
197 buf[12] = linkscript.execaddr / 256;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
198 buf[13] = linkscript.execaddr & 0xff;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
199 memset(buf + 14, 0, 18);
252
4dc2a10997a6 Fixed LWEX binary output when BSS size > 0
lost
parents: 212
diff changeset
200
4dc2a10997a6 Fixed LWEX binary output when BSS size > 0
lost
parents: 212
diff changeset
201 // fprintf(stderr, "Exec addr %04X, stacksize %04X, codesize %04X, bss size %04X\n", linkscript.execaddr, linkscript.stacksize, codedatasize, nskips);
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
202
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
203 writebytes(buf, 1, 32, of);
252
4dc2a10997a6 Fixed LWEX binary output when BSS size > 0
lost
parents: 212
diff changeset
204 fsize = 32;
4dc2a10997a6 Fixed LWEX binary output when BSS size > 0
lost
parents: 212
diff changeset
205 coffs = 0x100;
4dc2a10997a6 Fixed LWEX binary output when BSS size > 0
lost
parents: 212
diff changeset
206 nskips = 0;
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
207 // output the data
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
208 // NOTE: disjoint load addresses will not work correctly!!!!!
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
209 for (sn = 0; sn < nsects; sn++)
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
210 {
252
4dc2a10997a6 Fixed LWEX binary output when BSS size > 0
lost
parents: 212
diff changeset
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
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
212 if (sectlist[sn].ptr -> flags & SECTION_BSS)
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
213 {
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
214 // no output for a BSS section
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
215 nskips += sectlist[sn].ptr -> codesize;
252
4dc2a10997a6 Fixed LWEX binary output when BSS size > 0
lost
parents: 212
diff changeset
216 coffs += sectlist[sn].ptr -> codesize;
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
217 continue;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
218 }
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
219 while (nskips > 0)
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
220 {
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
221 // the "" is not an error - it turns into a single NUL byte!
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
222 writebytes("", 1, 1, of);
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
223 nskips--;
252
4dc2a10997a6 Fixed LWEX binary output when BSS size > 0
lost
parents: 212
diff changeset
224 fsize += 1;
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
225 }
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
226 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
252
4dc2a10997a6 Fixed LWEX binary output when BSS size > 0
lost
parents: 212
diff changeset
227 coffs += sectlist[sn].ptr -> codesize;
4dc2a10997a6 Fixed LWEX binary output when BSS size > 0
lost
parents: 212
diff changeset
228 fsize += sectlist[sn].ptr -> codesize;
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
229 }
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
230 }