Mercurial > hg > index.cgi
annotate lwlink/output.c @ 583:000381ee2d5c default tip
Guard against single operand multiplication when detecting like terms
This *shouldn't* happen, but it apparently does in some pathological cases
so guard against a single operand multiplication to prevent a crash.
author | William Astle <lost@l-w.ca> |
---|---|
date | Mon, 04 Nov 2024 23:48:23 -0700 |
parents | cde1a5a48636 |
children |
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 | 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); |
548
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
42 void do_output_ihex(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
|
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 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
|
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 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
|
47 |
2c24602be78f
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 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
|
49 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
|
50 { |
2c24602be78f
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 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
|
52 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
|
53 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
|
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 |
2c24602be78f
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 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
|
57 { |
2c24602be78f
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 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
|
59 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
|
60 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
|
61 |
2c24602be78f
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 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
|
63 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
|
64 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
|
65 |
523
1744f2d1a821
Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents:
425
diff
changeset
|
66 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
|
67 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
|
68 break; |
1744f2d1a821
Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents:
425
diff
changeset
|
69 |
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
|
70 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
|
71 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
|
72 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
|
73 |
148 | 74 case OUTPUT_OS9: |
75 do_output_os9(of); | |
76 break; | |
77 | |
344
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
78 case OUTPUT_SREC: |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
79 do_output_srec(of); |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
80 break; |
548
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
81 |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
82 case OUTPUT_IHEX: |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
83 do_output_ihex(of); |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
84 break; |
344
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
85 |
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
|
86 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
|
87 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
|
88 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
|
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 |
2c24602be78f
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 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
|
92 } |
2c24602be78f
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 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
|
95 { |
2c24602be78f
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 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
|
97 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
|
98 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
|
99 |
2c24602be78f
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 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
|
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 -> 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
|
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 // 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
|
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 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
|
108 { |
2c24602be78f
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 // 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
|
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 } |
2c24602be78f
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 // 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
|
114 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
|
115 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
|
116 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
|
117 { |
2c24602be78f
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 // 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
|
119 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
|
120 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
|
121 // 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
|
122 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
|
123 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
|
124 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
|
125 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
|
126 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
|
127 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
|
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 |
2c24602be78f
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 // 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
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 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
|
137 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
|
138 { |
2c24602be78f
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 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
|
140 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
|
141 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
|
142 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
|
143 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
|
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 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
|
146 } |
2c24602be78f
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 // 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 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
|
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 |
2c24602be78f
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 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
|
157 { |
523
1744f2d1a821
Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents:
425
diff
changeset
|
158 int sn; |
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 |
1744f2d1a821
Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents:
425
diff
changeset
|
161 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
|
162 { |
1744f2d1a821
Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents:
425
diff
changeset
|
163 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
|
164 { |
1744f2d1a821
Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents:
425
diff
changeset
|
165 // 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
|
166 continue; |
1744f2d1a821
Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents:
425
diff
changeset
|
167 } |
1744f2d1a821
Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents:
425
diff
changeset
|
168 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
|
169 } |
1744f2d1a821
Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents:
425
diff
changeset
|
170 } |
1744f2d1a821
Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents:
425
diff
changeset
|
171 |
1744f2d1a821
Change behaviour of --format=raw; old behaviour kept for --format=raw2
William Astle <lost@l-w.ca>
parents:
425
diff
changeset
|
172 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
|
173 { |
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
|
174 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
|
175 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
|
176 |
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
|
177 |
2c24602be78f
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 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
|
179 { |
2c24602be78f
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 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
|
181 { |
2c24602be78f
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 // 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
|
183 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
|
184 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
|
185 } |
2c24602be78f
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 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
|
187 { |
2c24602be78f
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 // 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
|
189 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
|
190 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
|
191 } |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
192 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
|
193 } |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
194 } |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
195 |
344
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
196 void do_output_srec(FILE *of) |
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 const int SRECLEN = 16; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
199 |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
200 int sn; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
201 int remainingcodebytes; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
202 |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
203 int codeaddr; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
204 int i; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
205 int recaddr = 0; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
206 int recdlen = 0; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
207 int recsum; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
208 int reccnt = -1; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
209 unsigned char* sectcode; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
210 // no header yet; unnecessary |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
211 |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
212 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
|
213 { |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
214 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
|
215 continue; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
216 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
|
217 continue; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
218 |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
219 recaddr = sectlist[sn].ptr -> loadaddress; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
220 remainingcodebytes = sectlist[sn].ptr -> codesize; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
221 sectcode = sectlist[sn].ptr -> code; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
222 |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
223 while (remainingcodebytes) |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
224 { |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
225 recdlen = (SRECLEN>remainingcodebytes)?remainingcodebytes:SRECLEN; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
226 recsum = recdlen + 3; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
227 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
|
228 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
|
229 for (i = 0; i < recdlen; i++) |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
230 { |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
231 fprintf(of, "%02X", sectcode[codeaddr+i]); |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
232 recsum += sectcode[codeaddr+i]; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
233 } |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
234 recsum += (recaddr >> 8) & 0xFF; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
235 recsum += recaddr & 0xFF; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
236 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
|
237 reccnt += 1; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
238 remainingcodebytes -= recdlen; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
239 recaddr += recdlen; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
240 } |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
241 } |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
242 // 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
|
243 recsum = 3; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
244 recsum += (linkscript.execaddr >> 8) & 0xFF; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
245 recsum += linkscript.execaddr & 0xFF; |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
246 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
|
247 } |
55c1f9a321e9
Add S-record output support to lwlink
William Astle <lost@l-w.ca>
parents:
320
diff
changeset
|
248 |
548
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
249 void do_output_ihex(FILE *of) |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
250 { |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
251 const int IRECLEN = 16; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
252 |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
253 int sn; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
254 int remainingcodebytes; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
255 |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
256 int codeaddr; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
257 int i; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
258 int recaddr = 0; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
259 int recdlen = 0; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
260 int recsum; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
261 int reccnt = -1; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
262 unsigned char* sectcode; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
263 // no header yet; unnecessary |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
264 |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
265 for (sn = 0; sn < nsects; sn++) // check all sections |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
266 { |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
267 if (sectlist[sn].ptr -> flags & SECTION_BSS) // ignore BSS sections |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
268 continue; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
269 if (sectlist[sn].ptr -> codesize == 0) // ignore empty sections |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
270 continue; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
271 |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
272 recaddr = sectlist[sn].ptr -> loadaddress; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
273 remainingcodebytes = sectlist[sn].ptr -> codesize; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
274 sectcode = sectlist[sn].ptr -> code; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
275 |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
276 while (remainingcodebytes) |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
277 { |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
278 recdlen = (IRECLEN>remainingcodebytes)?remainingcodebytes:IRECLEN; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
279 recsum = recdlen; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
280 codeaddr = recaddr - sectlist[sn].ptr -> loadaddress; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
281 fprintf(of, ":%02X%04X00", recdlen, recaddr & 0xffff); |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
282 for (i = 0; i < recdlen; i++) |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
283 { |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
284 fprintf(of, "%02X", sectcode[codeaddr+i]); |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
285 recsum += sectcode[codeaddr+i]; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
286 } |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
287 recsum += (recaddr >> 8) & 0xFF; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
288 recsum += recaddr & 0xFF; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
289 fprintf(of, "%02X\r\n", (unsigned char)(256 - recsum)); |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
290 reccnt += 1; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
291 remainingcodebytes -= recdlen; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
292 recaddr += recdlen; |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
293 } |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
294 } |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
295 if (reccnt > 0) |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
296 { |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
297 fprintf(of, ":00%04X01FF\r\n", linkscript.execaddr); |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
298 } |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
299 } |
cde1a5a48636
Add IHEX output format to lwlink
William Astle <lost@l-w.ca>
parents:
523
diff
changeset
|
300 |
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
|
301 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
|
302 { |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
303 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
|
304 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
|
305 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
|
306 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
|
307 |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
308 // 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
|
309 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
|
310 { |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
311 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
|
312 { |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
313 // 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
|
314 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
|
315 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
|
316 } |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
317 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
|
318 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
|
319 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
|
320 } |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
321 |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
322 // 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
|
323 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
|
324 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
|
325 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
|
326 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
|
327 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
|
328 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
|
329 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
|
330 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
|
331 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
|
332 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
|
333 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
|
334 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
|
335 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
|
336 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
|
337 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
|
338 |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
339 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
|
340 // 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
|
341 // NOTE: disjoint load addresses will not work correctly!!!!! |
180 | 342 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
|
343 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
|
344 { |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
345 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
|
346 { |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
347 // 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
|
348 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
|
349 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
|
350 } |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
351 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
|
352 { |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
353 // 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
|
354 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
|
355 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
|
356 } |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
357 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
|
358 } |
2c24602be78f
Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
lost@l-w.ca
parents:
diff
changeset
|
359 } |
148 | 360 |
361 void os9crc(unsigned char crc[3], unsigned char b) | |
362 { | |
363 b ^= crc[0]; | |
364 crc[0] = crc[1]; | |
365 crc[1] = crc[2]; | |
366 crc[1] ^= b >> 7; | |
367 crc[2] = b << 1; | |
368 crc[1] ^= b >> 2; | |
369 crc[2] ^= b << 6; | |
370 b ^= b << 1; | |
371 b ^= b << 2; | |
372 b ^= b << 4; | |
373 if (b & 0x80) | |
374 { | |
375 crc[0] ^= 0x80; | |
376 crc[2] ^= 0x21; | |
377 } | |
378 } | |
379 | |
380 | |
381 void do_output_os9(FILE *of) | |
382 { | |
383 int sn; | |
384 int codedatasize = 0; | |
385 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
|
386 int nameoff; |
148 | 387 int i; |
388 | |
389 unsigned char buf[16]; | |
390 unsigned char crc[3]; | |
391 | |
392 // calculate items for the file header | |
393 for (sn = 0; sn < nsects; sn++) | |
394 { | |
395 if (sectlist[sn].ptr -> flags & SECTION_BSS) | |
396 { | |
397 // no output for a BSS section | |
398 bsssize += sectlist[sn].ptr -> codesize; | |
399 continue; | |
400 } | |
401 codedatasize += sectlist[sn].ptr -> codesize; | |
402 } | |
258
ebda5c96665e
Improved stack handling for os9 target in lwlink
William Astle <lost@l-w.ca>
parents:
180
diff
changeset
|
403 bsssize += linkscript.stacksize; |
148 | 404 |
405 // now bss size is the data size for the module | |
406 // and codesize is the length of the module minus the module header | |
407 // and CRC | |
408 | |
151
3b58d76ea032
Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents:
148
diff
changeset
|
409 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
|
410 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
|
411 codedatasize += 3; // add in CRC |
148 | 412 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
|
413 if (linkscript.edition >= 0) |
7de7b14ebaee
Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents:
258
diff
changeset
|
414 codedatasize += 1; |
148 | 415 |
416 // output the file header | |
417 buf[0] = 0x87; | |
418 buf[1] = 0xCD; | |
419 buf[2] = (codedatasize >> 8) & 0xff; | |
420 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
|
421 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
|
422 buf[5] = nameoff & 0xff; |
148 | 423 buf[6] = (linkscript.modtype << 4) | (linkscript.modlang); |
424 buf[7] = (linkscript.modattr << 4) | (linkscript.modrev); | |
425 buf[8] = (~(buf[0] ^ buf[1] ^ buf[2] ^ buf[3] ^ buf[4] ^ buf[5] ^ buf[6] ^ buf[7])) & 0xff; | |
426 buf[9] = (linkscript.execaddr >> 8) & 0xff; | |
427 buf[10] = linkscript.execaddr & 0xff; | |
428 buf[11] = (bsssize >> 8) & 0xff; | |
429 buf[12] = bsssize & 0xff; | |
430 | |
431 crc[0] = 0xff; | |
432 crc[1] = 0xff; | |
433 crc[2] = 0xff; | |
434 | |
435 os9crc(crc, buf[0]); | |
436 os9crc(crc, buf[1]); | |
437 os9crc(crc, buf[2]); | |
438 os9crc(crc, buf[3]); | |
439 os9crc(crc, buf[4]); | |
440 os9crc(crc, buf[5]); | |
441 os9crc(crc, buf[6]); | |
442 os9crc(crc, buf[7]); | |
443 os9crc(crc, buf[8]); | |
444 os9crc(crc, buf[9]); | |
445 os9crc(crc, buf[10]); | |
446 os9crc(crc, buf[11]); | |
447 os9crc(crc, buf[12]); | |
448 | |
449 | |
450 writebytes(buf, 1, 13, of); | |
451 | |
452 // output the data | |
453 // NOTE: disjoint load addresses will not work correctly!!!!! | |
454 for (sn = 0; sn < nsects; sn++) | |
455 { | |
456 if (sectlist[sn].ptr -> flags & SECTION_BSS) | |
457 { | |
458 // no output for a BSS section | |
459 continue; | |
460 } | |
461 writebytes(sectlist[sn].ptr -> code, 1, sectlist[sn].ptr -> codesize, of); | |
462 for (i = 0; i < sectlist[sn].ptr -> codesize; i++) | |
463 os9crc(crc, sectlist[sn].ptr -> code[i]); | |
464 } | |
465 | |
151
3b58d76ea032
Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents:
148
diff
changeset
|
466 // output the name |
3b58d76ea032
Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents:
148
diff
changeset
|
467 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
|
468 { |
3b58d76ea032
Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents:
148
diff
changeset
|
469 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
|
470 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
|
471 } |
3b58d76ea032
Fix up some minor thinkos in handling the module layout and header
lost@l-w.ca
parents:
148
diff
changeset
|
472 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
|
473 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
|
474 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
|
475 |
262
7de7b14ebaee
Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents:
258
diff
changeset
|
476 if (linkscript.edition >= 0) |
7de7b14ebaee
Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents:
258
diff
changeset
|
477 { |
320
a640ff4ed95f
Fix dumbosity in outputting the edition byte in os9 target
William Astle <lost@l-w.ca>
parents:
264
diff
changeset
|
478 buf[0] = linkscript.edition & 0xff; |
262
7de7b14ebaee
Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents:
258
diff
changeset
|
479 writebytes(buf, 1, 1, of); |
7de7b14ebaee
Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents:
258
diff
changeset
|
480 os9crc(crc, buf[0]); |
7de7b14ebaee
Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents:
258
diff
changeset
|
481 } |
7de7b14ebaee
Add support for os9 module edition byte
William Astle <lost@l-w.ca>
parents:
258
diff
changeset
|
482 |
148 | 483 crc[0] ^= 0xff; |
484 crc[1] ^= 0xff; | |
485 crc[2] ^= 0xff; | |
486 writebytes(crc, 1, 3, of); | |
487 } |