comparison lwlink/output.c @ 187:857cb407229e

Added LWEX0 (LWOS simple binary) target to lwlink
author lost
date Sun, 22 Mar 2009 04:24:39 +0000
parents 302b8db5fd89
children bae1e3ecdce1
comparison
equal deleted inserted replaced
186:bc2fae9438eb 187:857cb407229e
25 #include "config.h" 25 #include "config.h"
26 #endif 26 #endif
27 27
28 #include <stdio.h> 28 #include <stdio.h>
29 #include <stdlib.h> 29 #include <stdlib.h>
30 #include <string.h>
30 31
31 #include "lwlink.h" 32 #include "lwlink.h"
32 33
33 // this prevents warnings about not using the return value of fwrite() 34 // this prevents warnings about not using the return value of fwrite()
34 // and, theoretically, can be replaced with a function that handles things 35 // and, theoretically, can be replaced with a function that handles things
35 // better in the future 36 // better in the future
36 #define writebytes(s, l, c, f) do { int r; r = fwrite((s), (l), (c), (f)); } while (0) 37 #define writebytes(s, l, c, f) do { int r; r = fwrite((s), (l), (c), (f)); } while (0)
37 38
38 void do_output_decb(FILE *of); 39 void do_output_decb(FILE *of);
39 void do_output_raw(FILE *of); 40 void do_output_raw(FILE *of);
41 void do_output_lwex0(FILE *of);
40 42
41 void do_output(void) 43 void do_output(void)
42 { 44 {
43 FILE *of; 45 FILE *of;
44 46
56 do_output_decb(of); 58 do_output_decb(of);
57 break; 59 break;
58 60
59 case OUTPUT_RAW: 61 case OUTPUT_RAW:
60 do_output_raw(of); 62 do_output_raw(of);
63 break;
64
65 case OUTPUT_LWEX0:
66 do_output_lwex0(of);
61 break; 67 break;
62 68
63 default: 69 default:
64 fprintf(stderr, "Unknown output format doing output!\n"); 70 fprintf(stderr, "Unknown output format doing output!\n");
65 exit(111); 71 exit(111);
150 nskips--; 156 nskips--;
151 } 157 }
152 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of); 158 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
153 } 159 }
154 } 160 }
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 }