Mercurial > hg-old > index.cgi
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 |
rev | line source |
---|---|
112 | 1 /* |
2 main.c | |
118 | 3 Copyright © 2009 William Astle |
112 | 4 |
5 This file is part of LWLINK. | |
6 | |
7 LWLINK is free software: you can redistribute it and/or modify it under the | |
8 terms of the GNU General Public License as published by the Free Software | |
9 Foundation, either version 3 of the License, or (at your option) any later | |
10 version. | |
11 | |
12 This program is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
15 more details. | |
16 | |
17 You should have received a copy of the GNU General Public License along with | |
18 this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 | |
21 Implements the program startup code | |
22 | |
23 */ | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
26 #include "config.h" | |
27 #endif | |
28 | |
29 #include <argp.h> | |
30 #include <errno.h> | |
31 #include <stdio.h> | |
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 | 34 |
35 #include "lwlink.h" | |
36 | |
37 // command line option handling | |
142 | 38 const char *argp_program_version = "LWLINK from " PACKAGE_STRING; |
112 | 39 const char *argp_program_bug_address = PACKAGE_BUGREPORT; |
40 | |
41 static error_t parse_opts(int key, char *arg, struct argp_state *state) | |
42 { | |
43 switch (key) | |
44 { | |
45 case 'o': | |
46 // output | |
47 outfile = arg; | |
48 break; | |
49 | |
117 | 50 case 's': |
51 // script file | |
52 scriptfile = arg; | |
53 break; | |
54 | |
112 | 55 case 'd': |
56 // debug | |
57 debug_level++; | |
58 break; | |
59 | |
60 case 'b': | |
61 // decb output | |
62 outformat = OUTPUT_DECB; | |
63 break; | |
64 | |
65 case 'r': | |
66 // raw binary output | |
67 outformat = OUTPUT_RAW; | |
68 break; | |
69 | |
70 case 'f': | |
71 // output format | |
72 if (!strcasecmp(arg, "decb")) | |
73 outformat = OUTPUT_DECB; | |
74 else if (!strcasecmp(arg, "raw")) | |
75 outformat = OUTPUT_RAW; | |
76 else | |
77 { | |
78 fprintf(stderr, "Invalid output format: %s\n", arg); | |
79 exit(1); | |
80 } | |
81 break; | |
82 case ARGP_KEY_END: | |
83 // done; sanity check | |
84 if (!outfile) | |
85 outfile = "a.out"; | |
86 break; | |
87 | |
180 | 88 case 'l': |
89 add_input_library(arg); | |
90 break; | |
91 | |
92 case 'L': | |
93 add_library_search(arg); | |
94 break; | |
95 | |
96 case 0x100: | |
97 add_section_base(arg); | |
98 break; | |
99 | |
112 | 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 | 102 break; |
103 | |
104 default: | |
105 return ARGP_ERR_UNKNOWN; | |
106 } | |
107 return 0; | |
108 } | |
109 | |
110 static struct argp_option options[] = | |
111 { | |
112 { "output", 'o', "FILE", 0, | |
113 "Output to FILE"}, | |
114 { "debug", 'd', 0, 0, | |
115 "Set debug mode"}, | |
116 { "format", 'f', "TYPE", 0, | |
149 | 117 "Select output format: decb, raw"}, |
112 | 118 { "decb", 'b', 0, 0, |
119 "Generate DECB .bin format output, equivalent of --format=decb"}, | |
120 { "raw", 'r', 0, 0, | |
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 | 123 "Specify the linking script (overrides the built in defaults)"}, |
124 { "library", 'l', "LIBSPEC", 0, | |
125 "Read library libLIBSPEC.a from the search path" }, | |
126 { "library-path", 'L', "DIR", 0, | |
127 "Add DIR to the library search path" }, | |
128 { "section-base", 0x100, "SECT=BASE", 0, | |
129 "Load section SECT at BASE" }, | |
112 | 130 { 0 } |
131 }; | |
132 | |
133 static struct argp argp = | |
134 { | |
135 options, | |
136 parse_opts, | |
137 "<input file> ...", | |
138 "LWLINK, a HD6309 and MC6809 cross-linker" | |
139 }; | |
140 | |
115 | 141 extern void read_files(void); |
117 | 142 extern void setup_script(void); |
119 | 143 extern void resolve_sections(void); |
121 | 144 extern void resolve_references(void); |
145 extern void do_output(void); | |
115 | 146 |
112 | 147 // main function; parse command line, set up assembler state, and run the |
148 // assembler on the first file | |
149 int main(int argc, char **argv) | |
150 { | |
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 | 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 | 160 // handle the linker script |
117 | 161 setup_script(); |
162 | |
115 | 163 // read the input files |
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 | 166 // resolve section bases and section order |
167 resolve_sections(); | |
168 | |
121 | 169 // resolve incomplete references |
170 resolve_references(); | |
171 | |
172 // do the actual output | |
173 do_output(); | |
174 | |
112 | 175 exit(0); |
176 } |