annotate lwlink/trunk/src/output.c @ 127:44dd2f2e42a7

Fixed reading object file incomplete references table
author lost
date Fri, 23 Jan 2009 05:49:16 +0000
parents 6084a3859fb4
children b972e45b5e07
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 }
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
83 // write a preamble
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
84 buf[0] = 0x00;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
85 buf[1] = sectlist[sn].ptr -> codesize >> 8;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
86 buf[2] = sectlist[sn].ptr -> codesize & 0xff;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
87 buf[3] = sectlist[sn].ptr -> loadaddress >> 8;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
88 buf[4] = sectlist[sn].ptr -> loadaddress & 0xff;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
89 writebytes(buf, 1, 5, of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
90 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
91 }
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
92 // write a postamble
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
93 buf[0] = 0xff;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
94 buf[1] = 0x00;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
95 buf[2] = 0x00;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
96 buf[3] = linkscript.execaddr >> 8;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
97 buf[4] = linkscript.execaddr & 0xff;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
98 writebytes(buf, 1, 5, of);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
99 }
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
100
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
101 void do_output_raw(FILE *of)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
102 {
123
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
103 int nskips = 0; // used to output blanks for BSS inline
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
104 int sn;
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
105
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
106 for (sn = 0; sn < nsects; sn++)
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
107 {
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
108 if (sectlist[sn].ptr -> flags & SECTION_BSS)
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
109 {
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
110 // no output for a BSS section
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
111 nskips += sectlist[sn].ptr -> codesize;
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
112 continue;
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
113 }
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
114 while (nskips > 0)
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
115 {
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
116 // the "" is not an error - it turns into a single NUL byte!
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
117 writebytes("", 1, 1, of);
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
118 nskips--;
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
119 }
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
120 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
6084a3859fb4 Added basic raw output module
lost
parents: 121
diff changeset
121 }
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents:
diff changeset
122 }