annotate lwlink/output.c @ 140:8d65ef71fc01

repo reorg
author lost
date Wed, 28 Jan 2009 06:00:48 +0000
parents 106c2fe3c9d9
children 302b8db5fd89
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>
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
30
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
31 #include "lwlink.h"
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
32
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
33 // this prevents warnings about not using the return value of fwrite()
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
34 // and, theoretically, can be replaced with a function that handles things
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
35 // better in the future
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
36 #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
37
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
38 void do_output_decb(FILE *of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
39 void do_output_raw(FILE *of);
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;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
62
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
63 default:
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
64 fprintf(stderr, "Unknown output format doing output!\n");
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
65 exit(111);
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
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
68 fclose(of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
69 }
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 void do_output_decb(FILE *of)
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 int sn;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
74 unsigned char buf[5];
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 for (sn = 0; sn < nsects; sn++)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
77 {
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
78 if (sectlist[sn].ptr -> flags & SECTION_BSS)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
79 {
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
80 // no output for a BSS section
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
81 continue;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
82 }
132
b972e45b5e07 Don't generate a preamble for a zero-sized section
lost
parents: 123
diff changeset
83 if (sectlist[sn].ptr -> codesize == 0)
b972e45b5e07 Don't generate a preamble for a zero-sized section
lost
parents: 123
diff changeset
84 {
b972e45b5e07 Don't generate a preamble for a zero-sized section
lost
parents: 123
diff changeset
85 // 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
86 continue;
b972e45b5e07 Don't generate a preamble for a zero-sized section
lost
parents: 123
diff changeset
87 }
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
88 // write a preamble
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
89 buf[0] = 0x00;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
90 buf[1] = sectlist[sn].ptr -> codesize >> 8;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
91 buf[2] = sectlist[sn].ptr -> codesize & 0xff;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
92 buf[3] = sectlist[sn].ptr -> loadaddress >> 8;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
93 buf[4] = sectlist[sn].ptr -> loadaddress & 0xff;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
94 writebytes(buf, 1, 5, of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
95 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
96 }
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
97 // write a postamble
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
98 buf[0] = 0xff;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
99 buf[1] = 0x00;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
100 buf[2] = 0x00;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
101 buf[3] = linkscript.execaddr >> 8;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
102 buf[4] = linkscript.execaddr & 0xff;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
103 writebytes(buf, 1, 5, of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
104 }
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
105
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
106 void do_output_raw(FILE *of)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
107 {
123
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
108 int nskips = 0; // used to output blanks for BSS inline
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
109 int sn;
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
110
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
111 for (sn = 0; sn < nsects; sn++)
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
112 {
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
113 if (sectlist[sn].ptr -> flags & SECTION_BSS)
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
114 {
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
115 // no output for a BSS section
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
116 nskips += sectlist[sn].ptr -> codesize;
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
117 continue;
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
118 }
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
119 while (nskips > 0)
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
120 {
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
121 // the "" is not an error - it turns into a single NUL byte!
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
122 writebytes("", 1, 1, of);
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
123 nskips--;
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
124 }
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
125 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
126 }
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
127 }