annotate lwlink/main.c @ 204:048ebb85f6ef

Added 8 bit external references for base page addressing mode
author lost
date Sun, 29 Mar 2009 14:52:28 +0000
parents 857cb407229e
children 42df94f30d82
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
1 /*
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
2 main.c
118
d450943305a7 Fixed copyright year
lost
parents: 117
diff changeset
3 Copyright © 2009 William Astle
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
4
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
5 This file is part of LWLINK.
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
6
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
7 LWLINK is free software: you can redistribute it and/or modify it under the
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
8 terms of the GNU General Public License as published by the Free Software
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
10 version.
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
11
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
15 more details.
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
16
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
17 You should have received a copy of the GNU General Public License along with
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
19
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
20
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
21 Implements the program startup code
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
22
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
23 */
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
24
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
25 #ifdef HAVE_CONFIG_H
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
26 #include "config.h"
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
27 #endif
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
28
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
29 #include <argp.h>
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
30 #include <errno.h>
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
31 #include <stdio.h>
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
32 #include <stdlib.h>
182
833d392fec82 Arranged for lwasm and lwlink to remove the output file in case they fail
lost
parents: 180
diff changeset
33 #include <unistd.h>
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
34
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
35 #include "lwlink.h"
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
36
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
37 // command line option handling
142
36ca3fa755e0 Autotools merge of packages done
lost
parents: 139
diff changeset
38 const char *argp_program_version = "LWLINK from " PACKAGE_STRING;
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
39 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
40
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
41 static error_t parse_opts(int key, char *arg, struct argp_state *state)
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
42 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
43 switch (key)
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
44 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
45 case 'o':
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
46 // output
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
47 outfile = arg;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
48 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
49
117
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
50 case 's':
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
51 // script file
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
52 scriptfile = arg;
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
53 break;
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
54
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
55 case 'd':
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
56 // debug
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
57 debug_level++;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
58 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
59
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
60 case 'b':
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
61 // decb output
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
62 outformat = OUTPUT_DECB;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
63 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
64
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
65 case 'r':
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
66 // raw binary output
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
67 outformat = OUTPUT_RAW;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
68 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
69
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
70 case 'f':
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
71 // output format
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
72 if (!strcasecmp(arg, "decb"))
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
73 outformat = OUTPUT_DECB;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
74 else if (!strcasecmp(arg, "raw"))
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
75 outformat = OUTPUT_RAW;
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 185
diff changeset
76 else if (!strcasecmp(arg, "lwex0") || !strcasecmp(arg, "lwex"))
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 185
diff changeset
77 outformat = OUTPUT_LWEX0;
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
78 else
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
79 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
80 fprintf(stderr, "Invalid output format: %s\n", arg);
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
81 exit(1);
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
82 }
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
83 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
84 case ARGP_KEY_END:
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
85 // done; sanity check
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
86 if (!outfile)
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
87 outfile = "a.out";
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
88 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
89
180
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
90 case 'l':
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
91 add_input_library(arg);
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
92 break;
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
93
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
94 case 'L':
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
95 add_library_search(arg);
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
96 break;
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
97
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
98 case 0x100:
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
99 add_section_base(arg);
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
100 break;
185
b89adfb0d174 Added support for outputting a linkmap
lost
parents: 182
diff changeset
101
b89adfb0d174 Added support for outputting a linkmap
lost
parents: 182
diff changeset
102 case 'm':
b89adfb0d174 Added support for outputting a linkmap
lost
parents: 182
diff changeset
103 map_file = arg;
b89adfb0d174 Added support for outputting a linkmap
lost
parents: 182
diff changeset
104 break;
b89adfb0d174 Added support for outputting a linkmap
lost
parents: 182
diff changeset
105
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
106 case ARGP_KEY_ARG:
114
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
107 add_input_file(arg);
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
108 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
109
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
110 default:
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
111 return ARGP_ERR_UNKNOWN;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
112 }
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
113 return 0;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
114 }
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
115
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
116 static struct argp_option options[] =
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
117 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
118 { "output", 'o', "FILE", 0,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
119 "Output to FILE"},
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
120 { "debug", 'd', 0, 0,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
121 "Set debug mode"},
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
122 { "format", 'f', "TYPE", 0,
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 185
diff changeset
123 "Select output format: decb, raw, lwex"},
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
124 { "decb", 'b', 0, 0,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
125 "Generate DECB .bin format output, equivalent of --format=decb"},
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
126 { "raw", 'r', 0, 0,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
127 "Generate raw binary format output, equivalent of --format=raw"},
125
f9bfc2986023 Actually allow script file to be specified and fix segfault on parsing script file
lost
parents: 121
diff changeset
128 { "script", 's', "FILE", 0,
180
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
129 "Specify the linking script (overrides the built in defaults)"},
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
130 { "library", 'l', "LIBSPEC", 0,
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
131 "Read library libLIBSPEC.a from the search path" },
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
132 { "library-path", 'L', "DIR", 0,
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
133 "Add DIR to the library search path" },
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
134 { "section-base", 0x100, "SECT=BASE", 0,
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
135 "Load section SECT at BASE" },
185
b89adfb0d174 Added support for outputting a linkmap
lost
parents: 182
diff changeset
136 { "map", 'm', "FILE", 0,
b89adfb0d174 Added support for outputting a linkmap
lost
parents: 182
diff changeset
137 "Output informaiton about the link" },
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
138 { 0 }
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
139 };
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
140
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
141 static struct argp argp =
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
142 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
143 options,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
144 parse_opts,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
145 "<input file> ...",
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
146 "LWLINK, a HD6309 and MC6809 cross-linker"
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
147 };
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
148
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
149 extern void read_files(void);
117
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
150 extern void setup_script(void);
119
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
151 extern void resolve_sections(void);
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
152 extern void resolve_references(void);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
153 extern void do_output(void);
185
b89adfb0d174 Added support for outputting a linkmap
lost
parents: 182
diff changeset
154 extern void display_map(void);
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
155
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
156 // main function; parse command line, set up assembler state, and run the
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
157 // assembler on the first file
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
158 int main(int argc, char **argv)
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
159 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
160 argp_parse(&argp, argc, argv, 0, 0, NULL);
114
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
161 if (ninputfiles == 0)
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
162 {
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
163 fprintf(stderr, "No input files\n");
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
164 exit(1);
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
165 }
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
166
182
833d392fec82 Arranged for lwasm and lwlink to remove the output file in case they fail
lost
parents: 180
diff changeset
167 unlink(outfile);
833d392fec82 Arranged for lwasm and lwlink to remove the output file in case they fail
lost
parents: 180
diff changeset
168
119
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
169 // handle the linker script
117
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
170 setup_script();
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
171
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
172 // read the input files
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
173 read_files();
114
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
174
119
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
175 // resolve section bases and section order
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
176 resolve_sections();
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
177
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
178 // resolve incomplete references
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
179 resolve_references();
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
180
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
181 // do the actual output
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
182 do_output();
185
b89adfb0d174 Added support for outputting a linkmap
lost
parents: 182
diff changeset
183
b89adfb0d174 Added support for outputting a linkmap
lost
parents: 182
diff changeset
184 // display/output the link map
b89adfb0d174 Added support for outputting a linkmap
lost
parents: 182
diff changeset
185 if (map_file)
b89adfb0d174 Added support for outputting a linkmap
lost
parents: 182
diff changeset
186 display_map();
b89adfb0d174 Added support for outputting a linkmap
lost
parents: 182
diff changeset
187
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
188 exit(0);
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
189 }