annotate lwlink/output.c @ 195:383caf808674

Allow relative addressing to ignore a # before the operand
author lost
date Sun, 22 Mar 2009 17:57:39 +0000
parents 857cb407229e
children bae1e3ecdce1
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
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
25 #include "config.h"
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
26 #endif
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
27
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
28 #include <stdio.h>
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
29 #include <stdlib.h>
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
30 #include <string.h>
121
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 #include "lwlink.h"
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
33
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
34 // this prevents warnings about not using the return value of fwrite()
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
35 // and, theoretically, can be replaced with a function that handles things
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
36 // better in the future
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
37 #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
38
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
39 void do_output_decb(FILE *of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
40 void do_output_raw(FILE *of);
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
41 void do_output_lwex0(FILE *of);
121
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 void do_output(void)
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 FILE *of;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
46
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
47 of = fopen(outfile, "wb");
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
48 if (!of)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
49 {
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
50 fprintf(stderr, "Cannot open output file %s: ", outfile);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
51 perror("");
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
52 exit(1);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
53 }
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 switch (outformat)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
56 {
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
57 case OUTPUT_DECB:
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
58 do_output_decb(of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
59 break;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
60
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
61 case OUTPUT_RAW:
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
62 do_output_raw(of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
63 break;
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
64
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
65 case OUTPUT_LWEX0:
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
66 do_output_lwex0(of);
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
67 break;
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
68
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
69 default:
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
70 fprintf(stderr, "Unknown output format doing output!\n");
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
71 exit(111);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
72 }
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 fclose(of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
75 }
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
76
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
77 void do_output_decb(FILE *of)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
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
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
81 unsigned char buf[5];
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 for (sn = 0; sn < nsects; sn++)
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 if (sectlist[sn].ptr -> flags & SECTION_BSS)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
86 {
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
87 // no output for a BSS section
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
88 continue;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
89 }
132
b972e45b5e07 Don't generate a preamble for a zero-sized section
lost
parents: 123
diff changeset
90 if (sectlist[sn].ptr -> codesize == 0)
b972e45b5e07 Don't generate a preamble for a zero-sized section
lost
parents: 123
diff changeset
91 {
b972e45b5e07 Don't generate a preamble for a zero-sized section
lost
parents: 123
diff changeset
92 // 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
93 continue;
b972e45b5e07 Don't generate a preamble for a zero-sized section
lost
parents: 123
diff changeset
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
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
113 // write a preamble
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
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
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
117 buf[3] = sectlist[sn].ptr -> loadaddress >> 8;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
118 buf[4] = sectlist[sn].ptr -> loadaddress & 0xff;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
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
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
129 }
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
130 // write a postamble
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
131 buf[0] = 0xff;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
132 buf[1] = 0x00;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
133 buf[2] = 0x00;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
134 buf[3] = linkscript.execaddr >> 8;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
135 buf[4] = linkscript.execaddr & 0xff;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
136 writebytes(buf, 1, 5, of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
137 }
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
138
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
139 void do_output_raw(FILE *of)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
140 {
123
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
141 int nskips = 0; // used to output blanks for BSS inline
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
142 int 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 for (sn = 0; sn < nsects; sn++)
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
145 {
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
146 if (sectlist[sn].ptr -> flags & SECTION_BSS)
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
147 {
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
148 // no output for a BSS section
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
149 nskips += sectlist[sn].ptr -> codesize;
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
150 continue;
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
151 }
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
152 while (nskips > 0)
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
153 {
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
154 // the "" is not an error - it turns into a single NUL byte!
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
155 writebytes("", 1, 1, of);
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
156 nskips--;
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
157 }
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
158 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
159 }
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
160 }
187
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 void do_output_lwex0(FILE *of)
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
163 {
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
164 int nskips = 0; // used to output blanks for BSS inline
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
165 int sn;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
166 int codedatasize = 0;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
167 unsigned char buf[32];
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
168
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 {
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
172 if (sectlist[sn].ptr -> flags & SECTION_BSS)
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
173 {
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
174 // no output for a BSS section
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
175 nskips += sectlist[sn].ptr -> codesize;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
176 continue;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
177 }
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
178 codedatasize += nskips;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
179 nskips = 0;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
180 codedatasize += sectlist[sn].ptr -> codesize;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
181 }
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 // output the file header
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
184 buf[0] = 'L';
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
185 buf[1] = 'W';
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
186 buf[2] = 'E';
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
187 buf[3] = 'X';
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
188 buf[4] = 0; // version 0
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
189 buf[5] = 0; // low stack
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
190 buf[6] = linkscript.stacksize / 256;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
191 buf[7] = linkscript.stacksize & 0xff;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
192 buf[8] = nskips / 256;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
193 buf[9] = nskips & 0xff;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
194 buf[10] = codedatasize / 256;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
195 buf[11] = codedatasize & 0xff;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
196 buf[12] = linkscript.execaddr / 256;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
197 buf[13] = linkscript.execaddr & 0xff;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
198 memset(buf + 14, 0, 18);
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
199
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
200 writebytes(buf, 1, 32, of);
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
201 // output the data
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
202 // NOTE: disjoint load addresses will not work correctly!!!!!
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
203 for (sn = 0; sn < nsects; sn++)
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
204 {
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
205 if (sectlist[sn].ptr -> flags & SECTION_BSS)
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
206 {
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
207 // no output for a BSS section
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
208 nskips += sectlist[sn].ptr -> codesize;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
209 continue;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
210 }
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
211 while (nskips > 0)
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
212 {
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
213 // 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
214 writebytes("", 1, 1, of);
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
215 nskips--;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
216 }
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 183
diff changeset
217 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
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 }