annotate lwlink/output.c @ 425:9f0448022f1f

Fix address overflows in SREC and IHEX file formats Thanks to hider <stego@satx.rr.com> for pointing out an overflow that caused 16 bit addresses to be output as 5 hex digits in cases where an address counter wrapped. Simply mask the addresses down to 16 bits.
author William Astle <lost@l-w.ca>
date Thu, 03 Nov 2016 21:36:17 -0600
parents 55c1f9a321e9
children 1744f2d1a821
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
1 /*
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
2 output.c
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
3 Copyright © 2009 William Astle
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
4
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
5 This file is part of LWLINK.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
6
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
7 LWLINK is free software: you can redistribute it and/or modify it under the
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
10 version.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
11
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
15 more details.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
16
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
19
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
20
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
21 Actually output the binary
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
22 */
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
23
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
24 #include <stdio.h>
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
25 #include <stdlib.h>
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
26 #include <string.h>
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
27
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
28 #include "lwlink.h"
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
29
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
30 // this prevents warnings about not using the return value of fwrite()
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
31 // and, theoretically, can be replaced with a function that handles things
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
32 // better in the future
264
346966cffeef Clean up various warnings when building under -Wall
William Astle <lost@l-w.ca>
parents: 262
diff changeset
33 //#define writebytes(s, l, c, f) do { int r; r = fwrite((s), (l), (c), (f)); (void)r; } while (0)
346966cffeef Clean up various warnings when building under -Wall
William Astle <lost@l-w.ca>
parents: 262
diff changeset
34 #define writebytes(s, l, c, f) do { (void)(fwrite((s), (l), (c), (f)) && 1); } while (0)
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
35
148
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
36 void do_output_os9(FILE *of);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
37 void do_output_decb(FILE *of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
38 void do_output_raw(FILE *of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
39 void do_output_lwex0(FILE *of);
344
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
40 void do_output_srec(FILE *of);
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
41
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
42 void do_output(void)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
43 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
44 FILE *of;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
45
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
46 of = fopen(outfile, "wb");
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
47 if (!of)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
48 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
49 fprintf(stderr, "Cannot open output file %s: ", outfile);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
50 perror("");
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
51 exit(1);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
52 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
53
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
54 switch (outformat)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
55 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
56 case OUTPUT_DECB:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
57 do_output_decb(of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
58 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
59
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
60 case OUTPUT_RAW:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
61 do_output_raw(of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
62 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
63
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
64 case OUTPUT_LWEX0:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
65 do_output_lwex0(of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
66 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
67
148
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
68 case OUTPUT_OS9:
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
69 do_output_os9(of);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
70 break;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
71
344
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
72 case OUTPUT_SREC:
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
73 do_output_srec(of);
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
74 break;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
75
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
76 default:
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
77 fprintf(stderr, "Unknown output format doing output!\n");
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
78 exit(111);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
79 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
80
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
81 fclose(of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
82 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
83
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
84 void do_output_decb(FILE *of)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
85 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
86 int sn, sn2;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
87 int cloc, olen;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
88 unsigned char buf[5];
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
89
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
90 for (sn = 0; sn < nsects; sn++)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
91 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
92 if (sectlist[sn].ptr -> flags & SECTION_BSS)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
93 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
94 // no output for a BSS section
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
95 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
96 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
97 if (sectlist[sn].ptr -> codesize == 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
98 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
99 // don't generate output for a zero size section
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
100 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
101 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
102
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
103 // calculate the length of this output block
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
104 cloc = sectlist[sn].ptr -> loadaddress;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
105 olen = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
106 for (sn2 = sn; sn2 < nsects; sn2++)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
107 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
108 // ignore BSS sections
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
109 if (sectlist[sn2].ptr -> flags & SECTION_BSS)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
110 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
111 // ignore zero length sections
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
112 if (sectlist[sn2].ptr -> codesize == 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
113 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
114 if (cloc != sectlist[sn2].ptr -> loadaddress)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
115 break;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
116 olen += sectlist[sn2].ptr -> codesize;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
117 cloc += sectlist[sn2].ptr -> codesize;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
118 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
119
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
120 // write a preamble
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
121 buf[0] = 0x00;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
122 buf[1] = olen >> 8;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
123 buf[2] = olen & 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
124 buf[3] = sectlist[sn].ptr -> loadaddress >> 8;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
125 buf[4] = sectlist[sn].ptr -> loadaddress & 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
126 writebytes(buf, 1, 5, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
127 for (; sn < sn2; sn++)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
128 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
129 if (sectlist[sn].ptr -> flags & SECTION_BSS)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
130 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
131 if (sectlist[sn].ptr -> codesize == 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
132 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
133 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
134 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
135 sn--;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
136 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
137 // write a postamble
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
138 buf[0] = 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
139 buf[1] = 0x00;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
140 buf[2] = 0x00;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
141 buf[3] = linkscript.execaddr >> 8;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
142 buf[4] = linkscript.execaddr & 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
143 writebytes(buf, 1, 5, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
144 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
145
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
146 void do_output_raw(FILE *of)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
147 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
148 int nskips = 0; // used to output blanks for BSS inline
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
149 int sn;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
150
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
151 for (sn = 0; sn < nsects; sn++)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
152 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
153 if (sectlist[sn].ptr -> flags & SECTION_BSS)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
154 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
155 // no output for a BSS section
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
156 nskips += sectlist[sn].ptr -> codesize;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
157 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
158 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
159 while (nskips > 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
160 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
161 // the "" is not an error - it turns into a single NUL byte!
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
162 writebytes("", 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
163 nskips--;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
164 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
165 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
166 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
167 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
168
344
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
169 void do_output_srec(FILE *of)
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
170 {
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
171 const int SRECLEN = 16;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
172
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
173 int sn;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
174 int remainingcodebytes;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
175
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
176 int codeaddr;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
177 int i;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
178 int recaddr = 0;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
179 int recdlen = 0;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
180 int recsum;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
181 int reccnt = -1;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
182 unsigned char* sectcode;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
183 // no header yet; unnecessary
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
184
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
185 for (sn = 0; sn < nsects; sn++) // check all sections
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
186 {
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
187 if (sectlist[sn].ptr -> flags & SECTION_BSS) // ignore BSS sections
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
188 continue;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
189 if (sectlist[sn].ptr -> codesize == 0) // ignore empty sections
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
190 continue;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
191
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
192 recaddr = sectlist[sn].ptr -> loadaddress;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
193 remainingcodebytes = sectlist[sn].ptr -> codesize;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
194 sectcode = sectlist[sn].ptr -> code;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
195
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
196 while (remainingcodebytes)
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
197 {
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
198 recdlen = (SRECLEN>remainingcodebytes)?remainingcodebytes:SRECLEN;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
199 recsum = recdlen + 3;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
200 codeaddr = recaddr - sectlist[sn].ptr -> loadaddress;
425
9f0448022f1f Fix address overflows in SREC and IHEX file formats
William Astle <lost@l-w.ca>
parents: 344
diff changeset
201 fprintf(of, "S1%02X%04X", recdlen + 3, recaddr & 0xffff);
344
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
202 for (i = 0; i < recdlen; i++)
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
203 {
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
204 fprintf(of, "%02X", sectcode[codeaddr+i]);
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
205 recsum += sectcode[codeaddr+i];
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
206 }
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
207 recsum += (recaddr >> 8) & 0xFF;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
208 recsum += recaddr & 0xFF;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
209 fprintf(of, "%02X\r\n", (unsigned char)(~recsum));
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
210 reccnt += 1;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
211 remainingcodebytes -= recdlen;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
212 recaddr += recdlen;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
213 }
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
214 }
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
215 // S9 record as a footer to inform about start addr
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
216 recsum = 3;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
217 recsum += (linkscript.execaddr >> 8) & 0xFF;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
218 recsum += linkscript.execaddr & 0xFF;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
219 fprintf(of,"S903%04X%02X\r\n",linkscript.execaddr,(unsigned char)(~recsum));
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
220 }
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
221
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
222 void do_output_lwex0(FILE *of)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
223 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
224 int nskips = 0; // used to output blanks for BSS inline
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
225 int sn;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
226 int codedatasize = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
227 unsigned char buf[32];
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
228
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
229 // calculate items for the file header
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
230 for (sn = 0; sn < nsects; sn++)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
231 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
232 if (sectlist[sn].ptr -> flags & SECTION_BSS)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
233 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
234 // no output for a BSS section
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
235 nskips += sectlist[sn].ptr -> codesize;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
236 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
237 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
238 codedatasize += nskips;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
239 nskips = 0;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
240 codedatasize += sectlist[sn].ptr -> codesize;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
241 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
242
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
243 // output the file header
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
244 buf[0] = 'L';
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
245 buf[1] = 'W';
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
246 buf[2] = 'E';
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
247 buf[3] = 'X';
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
248 buf[4] = 0; // version 0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
249 buf[5] = 0; // low stack
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
250 buf[6] = linkscript.stacksize / 256;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
251 buf[7] = linkscript.stacksize & 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
252 buf[8] = nskips / 256;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
253 buf[9] = nskips & 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
254 buf[10] = codedatasize / 256;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
255 buf[11] = codedatasize & 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
256 buf[12] = linkscript.execaddr / 256;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
257 buf[13] = linkscript.execaddr & 0xff;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
258 memset(buf + 14, 0, 18);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
259
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
260 writebytes(buf, 1, 32, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
261 // output the data
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
262 // NOTE: disjoint load addresses will not work correctly!!!!!
180
11b710d231bd Fixed bug handling bss in lwex
lost@l-w.ca
parents: 151
diff changeset
263 nskips = 0;
0
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
264 for (sn = 0; sn < nsects; sn++)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
265 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
266 if (sectlist[sn].ptr -> flags & SECTION_BSS)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
267 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
268 // no output for a BSS section
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
269 nskips += sectlist[sn].ptr -> codesize;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
270 continue;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
271 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
272 while (nskips > 0)
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
273 {
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
274 // the "" is not an error - it turns into a single NUL byte!
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
275 writebytes("", 1, 1, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
276 nskips--;
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
277 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
278 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
279 }
2c24602be78f Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff changeset
280 }
148
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
281
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
282 void os9crc(unsigned char crc[3], unsigned char b)
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
283 {
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
284 b ^= crc[0];
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
285 crc[0] = crc[1];
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
286 crc[1] = crc[2];
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
287 crc[1] ^= b >> 7;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
288 crc[2] = b << 1;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
289 crc[1] ^= b >> 2;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
290 crc[2] ^= b << 6;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
291 b ^= b << 1;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
292 b ^= b << 2;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
293 b ^= b << 4;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
294 if (b & 0x80)
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
295 {
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
296 crc[0] ^= 0x80;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
297 crc[2] ^= 0x21;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
298 }
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
299 }
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
300
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
301
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
302 void do_output_os9(FILE *of)
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
303 {
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
304 int sn;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
305 int codedatasize = 0;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
306 int bsssize = 0;
151
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
307 int nameoff;
148
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
308 int i;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
309
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
310 unsigned char buf[16];
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
311 unsigned char crc[3];
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
312
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
313 // calculate items for the file header
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
314 for (sn = 0; sn < nsects; sn++)
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
315 {
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
316 if (sectlist[sn].ptr -> flags & SECTION_BSS)
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
317 {
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
318 // no output for a BSS section
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
319 bsssize += sectlist[sn].ptr -> codesize;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
320 continue;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
321 }
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
322 codedatasize += sectlist[sn].ptr -> codesize;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
323 }
258
ebda5c96665e Improved stack handling for os9 target in lwlink
William Astle <lost@l-w.ca>
parents: 180
diff changeset
324 bsssize += linkscript.stacksize;
148
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
325
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
326 // now bss size is the data size for the module
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
327 // and codesize is the length of the module minus the module header
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
328 // and CRC
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
329
151
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
330 codedatasize += 13; // add in headers
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
331 nameoff = codedatasize; // we'll put the name at the end
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
332 codedatasize += 3; // add in CRC
148
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
333 codedatasize += strlen(linkscript.name); // add in name length
262
7de7b14ebaee Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents: 258
diff changeset
334 if (linkscript.edition >= 0)
7de7b14ebaee Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents: 258
diff changeset
335 codedatasize += 1;
148
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
336
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
337 // output the file header
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
338 buf[0] = 0x87;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
339 buf[1] = 0xCD;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
340 buf[2] = (codedatasize >> 8) & 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
341 buf[3] = codedatasize & 0xff;
151
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
342 buf[4] = (nameoff >> 8) & 0xff;
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
343 buf[5] = nameoff & 0xff;
148
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
344 buf[6] = (linkscript.modtype << 4) | (linkscript.modlang);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
345 buf[7] = (linkscript.modattr << 4) | (linkscript.modrev);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
346 buf[8] = (~(buf[0] ^ buf[1] ^ buf[2] ^ buf[3] ^ buf[4] ^ buf[5] ^ buf[6] ^ buf[7])) & 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
347 buf[9] = (linkscript.execaddr >> 8) & 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
348 buf[10] = linkscript.execaddr & 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
349 buf[11] = (bsssize >> 8) & 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
350 buf[12] = bsssize & 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
351
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
352 crc[0] = 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
353 crc[1] = 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
354 crc[2] = 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
355
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
356 os9crc(crc, buf[0]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
357 os9crc(crc, buf[1]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
358 os9crc(crc, buf[2]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
359 os9crc(crc, buf[3]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
360 os9crc(crc, buf[4]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
361 os9crc(crc, buf[5]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
362 os9crc(crc, buf[6]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
363 os9crc(crc, buf[7]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
364 os9crc(crc, buf[8]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
365 os9crc(crc, buf[9]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
366 os9crc(crc, buf[10]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
367 os9crc(crc, buf[11]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
368 os9crc(crc, buf[12]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
369
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
370
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
371 writebytes(buf, 1, 13, of);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
372
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
373 // output the data
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
374 // NOTE: disjoint load addresses will not work correctly!!!!!
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
375 for (sn = 0; sn < nsects; sn++)
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
376 {
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
377 if (sectlist[sn].ptr -> flags & SECTION_BSS)
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
378 {
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
379 // no output for a BSS section
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
380 continue;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
381 }
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
382 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
383 for (i = 0; i < sectlist[sn].ptr -> codesize; i++)
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
384 os9crc(crc, sectlist[sn].ptr -> code[i]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
385 }
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
386
151
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
387 // output the name
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
388 for (i = 0; linkscript.name[i + 1]; i++)
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
389 {
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
390 writebytes(linkscript.name + i, 1, 1, of);
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
391 os9crc(crc, linkscript.name[i]);
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
392 }
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
393 buf[0] = linkscript.name[i] | 0x80;
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
394 writebytes(buf, 1, 1, of);
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
395 os9crc(crc, buf[0]);
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
396
262
7de7b14ebaee Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents: 258
diff changeset
397 if (linkscript.edition >= 0)
7de7b14ebaee Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents: 258
diff changeset
398 {
320
a640ff4ed95f Fix dumbosity in outputting the edition byte in os9 target
William Astle <lost@l-w.ca>
parents: 264
diff changeset
399 buf[0] = linkscript.edition & 0xff;
262
7de7b14ebaee Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents: 258
diff changeset
400 writebytes(buf, 1, 1, of);
7de7b14ebaee Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents: 258
diff changeset
401 os9crc(crc, buf[0]);
7de7b14ebaee Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents: 258
diff changeset
402 }
7de7b14ebaee Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents: 258
diff changeset
403
148
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
404 crc[0] ^= 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
405 crc[1] ^= 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
406 crc[2] ^= 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
407 writebytes(crc, 1, 3, of);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
408 }