annotate lwlink/main.c @ 184:220a760ec654

Make lwlink display all undefined references instead of bailing after the first one
author lost
date Sat, 21 Mar 2009 17:39:45 +0000
parents 833d392fec82
children b89adfb0d174
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;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
76 else
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
77 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
78 fprintf(stderr, "Invalid output format: %s\n", arg);
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
79 exit(1);
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
80 }
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
81 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
82 case ARGP_KEY_END:
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
83 // done; sanity check
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
84 if (!outfile)
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
85 outfile = "a.out";
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
86 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
87
180
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
88 case 'l':
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
89 add_input_library(arg);
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
90 break;
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
91
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
92 case 'L':
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
93 add_library_search(arg);
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
94 break;
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
95
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
96 case 0x100:
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
97 add_section_base(arg);
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
98 break;
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
99
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
100 case ARGP_KEY_ARG:
114
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
101 add_input_file(arg);
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
102 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
103
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
104 default:
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
105 return ARGP_ERR_UNKNOWN;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
106 }
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
107 return 0;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
108 }
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
109
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
110 static struct argp_option options[] =
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
111 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
112 { "output", 'o', "FILE", 0,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
113 "Output to FILE"},
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
114 { "debug", 'd', 0, 0,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
115 "Set debug mode"},
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
116 { "format", 'f', "TYPE", 0,
149
3706ede361ea Fixed help message for --format
lost
parents: 142
diff changeset
117 "Select output format: decb, raw"},
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
118 { "decb", 'b', 0, 0,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
119 "Generate DECB .bin format output, equivalent of --format=decb"},
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
120 { "raw", 'r', 0, 0,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
121 "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
122 { "script", 's', "FILE", 0,
180
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
123 "Specify the linking script (overrides the built in defaults)"},
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
124 { "library", 'l', "LIBSPEC", 0,
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
125 "Read library libLIBSPEC.a from the search path" },
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
126 { "library-path", 'L', "DIR", 0,
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
127 "Add DIR to the library search path" },
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
128 { "section-base", 0x100, "SECT=BASE", 0,
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
129 "Load section SECT at BASE" },
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
130 { 0 }
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
131 };
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
132
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
133 static struct argp argp =
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
134 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
135 options,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
136 parse_opts,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
137 "<input file> ...",
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
138 "LWLINK, a HD6309 and MC6809 cross-linker"
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
139 };
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
140
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
141 extern void read_files(void);
117
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
142 extern void setup_script(void);
119
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
143 extern void resolve_sections(void);
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
144 extern void resolve_references(void);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
145 extern void do_output(void);
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
146
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
147 // main function; parse command line, set up assembler state, and run the
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
148 // assembler on the first file
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
149 int main(int argc, char **argv)
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
150 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
151 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
152 if (ninputfiles == 0)
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
153 {
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
154 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
155 exit(1);
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
156 }
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
157
182
833d392fec82 Arranged for lwasm and lwlink to remove the output file in case they fail
lost
parents: 180
diff changeset
158 unlink(outfile);
833d392fec82 Arranged for lwasm and lwlink to remove the output file in case they fail
lost
parents: 180
diff changeset
159
119
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
160 // handle the linker script
117
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
161 setup_script();
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
162
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
163 // read the input files
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
164 read_files();
114
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
165
119
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
166 // resolve section bases and section order
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
167 resolve_sections();
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
168
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
169 // resolve incomplete references
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
170 resolve_references();
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
171
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
172 // do the actual output
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
173 do_output();
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
174
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
175 exit(0);
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
176 }