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 {
|
|
73 int sn;
|
|
74 unsigned char buf[5];
|
|
75
|
|
76 for (sn = 0; sn < nsects; sn++)
|
|
77 {
|
|
78 if (sectlist[sn].ptr -> flags & SECTION_BSS)
|
|
79 {
|
|
80 // no output for a BSS section
|
|
81 continue;
|
|
82 }
|
|
83 // write a preamble
|
|
84 buf[0] = 0x00;
|
|
85 buf[1] = sectlist[sn].ptr -> codesize >> 8;
|
|
86 buf[2] = sectlist[sn].ptr -> codesize & 0xff;
|
|
87 buf[3] = sectlist[sn].ptr -> loadaddress >> 8;
|
|
88 buf[4] = sectlist[sn].ptr -> loadaddress & 0xff;
|
|
89 writebytes(buf, 1, 5, of);
|
|
90 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
|
|
91 }
|
|
92 // write a postamble
|
|
93 buf[0] = 0xff;
|
|
94 buf[1] = 0x00;
|
|
95 buf[2] = 0x00;
|
|
96 buf[3] = linkscript.execaddr >> 8;
|
|
97 buf[4] = linkscript.execaddr & 0xff;
|
|
98 writebytes(buf, 1, 5, of);
|
|
99 }
|
|
100
|
|
101 void do_output_raw(FILE *of)
|
|
102 {
|
123
|
103 int nskips = 0; // used to output blanks for BSS inline
|
|
104 int sn;
|
|
105
|
|
106 for (sn = 0; sn < nsects; sn++)
|
|
107 {
|
|
108 if (sectlist[sn].ptr -> flags & SECTION_BSS)
|
|
109 {
|
|
110 // no output for a BSS section
|
|
111 nskips += sectlist[sn].ptr -> codesize;
|
|
112 continue;
|
|
113 }
|
|
114 while (nskips > 0)
|
|
115 {
|
|
116 // the "" is not an error - it turns into a single NUL byte!
|
|
117 writebytes("", 1, 1, of);
|
|
118 nskips--;
|
|
119 }
|
|
120 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
|
|
121 }
|
121
|
122 }
|