comparison lwlink/output.c @ 139:106c2fe3c9d9

repo reorg
author lost
date Wed, 28 Jan 2009 05:59:14 +0000
parents lwlink-old/trunk/src/output.c@050864a47b38
children 302b8db5fd89
comparison
equal deleted inserted replaced
138:050864a47b38 139:106c2fe3c9d9
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 if (sectlist[sn].ptr -> codesize == 0)
84 {
85 // don't generate output for a zero size section
86 continue;
87 }
88 // write a preamble
89 buf[0] = 0x00;
90 buf[1] = sectlist[sn].ptr -> codesize >> 8;
91 buf[2] = sectlist[sn].ptr -> codesize & 0xff;
92 buf[3] = sectlist[sn].ptr -> loadaddress >> 8;
93 buf[4] = sectlist[sn].ptr -> loadaddress & 0xff;
94 writebytes(buf, 1, 5, of);
95 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
96 }
97 // write a postamble
98 buf[0] = 0xff;
99 buf[1] = 0x00;
100 buf[2] = 0x00;
101 buf[3] = linkscript.execaddr >> 8;
102 buf[4] = linkscript.execaddr & 0xff;
103 writebytes(buf, 1, 5, of);
104 }
105
106 void do_output_raw(FILE *of)
107 {
108 int nskips = 0; // used to output blanks for BSS inline
109 int sn;
110
111 for (sn = 0; sn < nsects; sn++)
112 {
113 if (sectlist[sn].ptr -> flags & SECTION_BSS)
114 {
115 // no output for a BSS section
116 nskips += sectlist[sn].ptr -> codesize;
117 continue;
118 }
119 while (nskips > 0)
120 {
121 // the "" is not an error - it turns into a single NUL byte!
122 writebytes("", 1, 1, of);
123 nskips--;
124 }
125 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
126 }
127 }