annotate lwlink/output.c @ 523:1744f2d1a821

Change behaviour of --format=raw; old behaviour kept for --format=raw2 Make the default --format=raw output just ignore BSS flagged sections. If you put any in the middle of your code, your output file will break. Don't do dumb things like that. If you really need the old behaviour, change to --format=raw2 Neither behaviour is really ideal but doing the "right" thing is a boatload more complicated and is still going to do the wrong thing from time to time. Most situations will likely load the BSS sections with separate link script directives anyway or be assembling directly to raw without using the linker.
author William Astle <lost@l-w.ca>
date Mon, 24 Jan 2022 13:55:40 -0700
parents 9f0448022f1f
children cde1a5a48636
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);
523
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
39 void do_output_raw2(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
40 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
41 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
42
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 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
44 {
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 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
46
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 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
48 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
49 {
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 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
51 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
52 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
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
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 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
56 {
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 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
58 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
59 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
60
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 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
62 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
63 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
64
523
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
65 case OUTPUT_RAW2:
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
66 do_output_raw2(of);
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
67 break;
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
68
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
69 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
70 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
71 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
72
148
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
73 case OUTPUT_OS9:
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
74 do_output_os9(of);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
75 break;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
76
344
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
77 case OUTPUT_SREC:
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
78 do_output_srec(of);
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
79 break;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
80
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
81 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
82 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
83 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
84 }
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 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
87 }
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
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 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
90 {
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 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
92 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
93 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
94
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 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
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 -> 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
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 // 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
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 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
103 {
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 // 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
105 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
106 }
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 // 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
109 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
110 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
111 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
112 {
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 // 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
114 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
115 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
116 // 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
117 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
118 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
119 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
120 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
121 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
122 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
123 }
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
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 // 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
126 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
127 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
128 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
129 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
130 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
131 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
132 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
133 {
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 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
135 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
136 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
137 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
138 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
139 }
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 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
141 }
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 // 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
143 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
144 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
145 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
146 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
147 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
148 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
149 }
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 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
152 {
523
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
153 int sn;
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
154
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
155
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
156 for (sn = 0; sn < nsects; sn++)
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
157 {
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
158 if (sectlist[sn].ptr -> flags & SECTION_BSS)
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
159 {
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
160 // no output for a BSS section
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
161 continue;
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
162 }
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
163 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of);
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
164 }
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
165 }
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
166
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
167 void do_output_raw2(FILE *of)
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
168 {
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
169 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
170 int sn;
523
1744f2d1a821 Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents: 425
diff changeset
171
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
172
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
173 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
174 {
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
175 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
176 {
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
177 // 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
178 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
179 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
180 }
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
181 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
182 {
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
183 // 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
184 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
185 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
186 }
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
187 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
188 }
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
189 }
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
190
344
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
191 void do_output_srec(FILE *of)
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
192 {
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
193 const int SRECLEN = 16;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
194
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
195 int sn;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
196 int 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 int codeaddr;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
199 int i;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
200 int recaddr = 0;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
201 int recdlen = 0;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
202 int recsum;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
203 int reccnt = -1;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
204 unsigned char* sectcode;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
205 // no header yet; unnecessary
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 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
208 {
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
209 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
210 continue;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
211 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
212 continue;
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 recaddr = sectlist[sn].ptr -> loadaddress;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
215 remainingcodebytes = sectlist[sn].ptr -> codesize;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
216 sectcode = sectlist[sn].ptr -> code;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
217
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
218 while (remainingcodebytes)
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
219 {
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
220 recdlen = (SRECLEN>remainingcodebytes)?remainingcodebytes:SRECLEN;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
221 recsum = recdlen + 3;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
222 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
223 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
224 for (i = 0; i < recdlen; i++)
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
225 {
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
226 fprintf(of, "%02X", sectcode[codeaddr+i]);
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
227 recsum += sectcode[codeaddr+i];
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
228 }
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
229 recsum += (recaddr >> 8) & 0xFF;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
230 recsum += recaddr & 0xFF;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
231 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
232 reccnt += 1;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
233 remainingcodebytes -= recdlen;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
234 recaddr += recdlen;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
235 }
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
236 }
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
237 // 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
238 recsum = 3;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
239 recsum += (linkscript.execaddr >> 8) & 0xFF;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
240 recsum += linkscript.execaddr & 0xFF;
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
241 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
242 }
55c1f9a321e9 Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents: 320
diff changeset
243
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
244 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
245 {
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 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
247 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
248 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
249 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
250
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 // 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
252 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
253 {
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 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
255 {
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 // 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
257 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
258 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
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 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
261 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
262 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
263 }
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
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 // 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
266 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
267 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
268 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
269 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
270 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
271 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
272 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
273 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
274 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
275 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
276 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
277 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
278 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
279 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
280 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
281
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
282 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
283 // 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
284 // NOTE: disjoint load addresses will not work correctly!!!!!
180
11b710d231bd Fixed bug handling bss in lwex
lost@l-w.ca
parents: 151
diff changeset
285 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
286 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
287 {
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
288 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
289 {
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
290 // 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
291 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
292 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
293 }
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
294 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
295 {
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
296 // 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
297 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
298 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
299 }
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
300 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
301 }
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
302 }
148
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 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
305 {
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
306 b ^= crc[0];
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
307 crc[0] = crc[1];
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
308 crc[1] = crc[2];
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
309 crc[1] ^= b >> 7;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
310 crc[2] = b << 1;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
311 crc[1] ^= b >> 2;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
312 crc[2] ^= b << 6;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
313 b ^= b << 1;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
314 b ^= b << 2;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
315 b ^= b << 4;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
316 if (b & 0x80)
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 crc[0] ^= 0x80;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
319 crc[2] ^= 0x21;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
320 }
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
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
323
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
324 void do_output_os9(FILE *of)
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 int sn;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
327 int codedatasize = 0;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
328 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
329 int nameoff;
148
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
330 int i;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
331
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
332 unsigned char buf[16];
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
333 unsigned char crc[3];
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
334
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
335 // calculate items for the file header
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
336 for (sn = 0; sn < nsects; sn++)
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
337 {
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
338 if (sectlist[sn].ptr -> flags & SECTION_BSS)
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
339 {
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
340 // no output for a BSS section
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
341 bsssize += sectlist[sn].ptr -> codesize;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
342 continue;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
343 }
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
344 codedatasize += sectlist[sn].ptr -> codesize;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
345 }
258
ebda5c96665e Improved stack handling for os9 target in lwlink
William Astle <lost@l-w.ca>
parents: 180
diff changeset
346 bsssize += linkscript.stacksize;
148
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
347
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
348 // 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
349 // 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
350 // and CRC
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
351
151
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
352 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
353 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
354 codedatasize += 3; // add in CRC
148
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
355 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
356 if (linkscript.edition >= 0)
7de7b14ebaee Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents: 258
diff changeset
357 codedatasize += 1;
148
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
358
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
359 // output the file header
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
360 buf[0] = 0x87;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
361 buf[1] = 0xCD;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
362 buf[2] = (codedatasize >> 8) & 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
363 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
364 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
365 buf[5] = nameoff & 0xff;
148
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
366 buf[6] = (linkscript.modtype << 4) | (linkscript.modlang);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
367 buf[7] = (linkscript.modattr << 4) | (linkscript.modrev);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
368 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
369 buf[9] = (linkscript.execaddr >> 8) & 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
370 buf[10] = linkscript.execaddr & 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
371 buf[11] = (bsssize >> 8) & 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
372 buf[12] = bsssize & 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
373
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
374 crc[0] = 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
375 crc[1] = 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
376 crc[2] = 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
377
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
378 os9crc(crc, buf[0]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
379 os9crc(crc, buf[1]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
380 os9crc(crc, buf[2]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
381 os9crc(crc, buf[3]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
382 os9crc(crc, buf[4]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
383 os9crc(crc, buf[5]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
384 os9crc(crc, buf[6]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
385 os9crc(crc, buf[7]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
386 os9crc(crc, buf[8]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
387 os9crc(crc, buf[9]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
388 os9crc(crc, buf[10]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
389 os9crc(crc, buf[11]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
390 os9crc(crc, buf[12]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
391
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
392
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
393 writebytes(buf, 1, 13, of);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
394
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
395 // output the data
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
396 // 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
397 for (sn = 0; sn < nsects; sn++)
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
398 {
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
399 if (sectlist[sn].ptr -> flags & SECTION_BSS)
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
400 {
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
401 // no output for a BSS section
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
402 continue;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
403 }
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
404 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
405 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
406 os9crc(crc, sectlist[sn].ptr -> code[i]);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
407 }
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
408
151
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
409 // output the name
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
410 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
411 {
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
412 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
413 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
414 }
3b58d76ea032 Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents: 148
diff changeset
415 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
416 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
417 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
418
262
7de7b14ebaee Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents: 258
diff changeset
419 if (linkscript.edition >= 0)
7de7b14ebaee Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents: 258
diff changeset
420 {
320
a640ff4ed95f Fix dumbosity in outputting the edition byte in os9 target
William Astle <lost@l-w.ca>
parents: 264
diff changeset
421 buf[0] = linkscript.edition & 0xff;
262
7de7b14ebaee Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents: 258
diff changeset
422 writebytes(buf, 1, 1, of);
7de7b14ebaee Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents: 258
diff changeset
423 os9crc(crc, buf[0]);
7de7b14ebaee Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents: 258
diff changeset
424 }
7de7b14ebaee Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents: 258
diff changeset
425
148
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
426 crc[0] ^= 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
427 crc[1] ^= 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
428 crc[2] ^= 0xff;
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
429 writebytes(crc, 1, 3, of);
08fb11004df9 Initial pass at OS9 module support for lwlink
lost@l-w.ca
parents: 0
diff changeset
430 }