Mercurial > hg-old > index.cgi
annotate lwlink/main.c @ 275:97630727000e 2.5
fixed expression 'slots' to not store null on a pass 1 store
author | lost |
---|---|
date | Mon, 31 Aug 2009 08:37:15 +0000 |
parents | bae1e3ecdce1 |
children |
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 | |
212 | 25 #include <config.h> |
112 | 26 |
27 #include <argp.h> | |
28 #include <errno.h> | |
29 #include <stdio.h> | |
30 #include <stdlib.h> | |
182
833d392fec82
Arranged for lwasm and lwlink to remove the output file in case they fail
lost
parents:
180
diff
changeset
|
31 #include <unistd.h> |
112 | 32 |
33 #include "lwlink.h" | |
34 | |
212 | 35 char *program_name; |
36 | |
112 | 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; | |
187 | 76 else if (!strcasecmp(arg, "lwex0") || !strcasecmp(arg, "lwex")) |
77 outformat = OUTPUT_LWEX0; | |
112 | 78 else |
79 { | |
80 fprintf(stderr, "Invalid output format: %s\n", arg); | |
81 exit(1); | |
82 } | |
83 break; | |
84 case ARGP_KEY_END: | |
85 // done; sanity check | |
86 if (!outfile) | |
87 outfile = "a.out"; | |
88 break; | |
89 | |
180 | 90 case 'l': |
91 add_input_library(arg); | |
92 break; | |
93 | |
94 case 'L': | |
95 add_library_search(arg); | |
96 break; | |
97 | |
98 case 0x100: | |
99 add_section_base(arg); | |
100 break; | |
185 | 101 |
102 case 'm': | |
103 map_file = arg; | |
104 break; | |
105 | |
112 | 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 | 108 break; |
109 | |
110 default: | |
111 return ARGP_ERR_UNKNOWN; | |
112 } | |
113 return 0; | |
114 } | |
115 | |
116 static struct argp_option options[] = | |
117 { | |
118 { "output", 'o', "FILE", 0, | |
119 "Output to FILE"}, | |
120 { "debug", 'd', 0, 0, | |
121 "Set debug mode"}, | |
122 { "format", 'f', "TYPE", 0, | |
187 | 123 "Select output format: decb, raw, lwex"}, |
112 | 124 { "decb", 'b', 0, 0, |
125 "Generate DECB .bin format output, equivalent of --format=decb"}, | |
126 { "raw", 'r', 0, 0, | |
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 | 129 "Specify the linking script (overrides the built in defaults)"}, |
130 { "library", 'l', "LIBSPEC", 0, | |
131 "Read library libLIBSPEC.a from the search path" }, | |
132 { "library-path", 'L', "DIR", 0, | |
133 "Add DIR to the library search path" }, | |
134 { "section-base", 0x100, "SECT=BASE", 0, | |
135 "Load section SECT at BASE" }, | |
185 | 136 { "map", 'm', "FILE", 0, |
137 "Output informaiton about the link" }, | |
112 | 138 { 0 } |
139 }; | |
140 | |
141 static struct argp argp = | |
142 { | |
143 options, | |
144 parse_opts, | |
145 "<input file> ...", | |
146 "LWLINK, a HD6309 and MC6809 cross-linker" | |
147 }; | |
148 | |
115 | 149 extern void read_files(void); |
117 | 150 extern void setup_script(void); |
205 | 151 extern void resolve_files(void); |
119 | 152 extern void resolve_sections(void); |
121 | 153 extern void resolve_references(void); |
154 extern void do_output(void); | |
185 | 155 extern void display_map(void); |
115 | 156 |
112 | 157 // main function; parse command line, set up assembler state, and run the |
158 // assembler on the first file | |
159 int main(int argc, char **argv) | |
160 { | |
212 | 161 program_name = argv[0]; |
162 | |
112 | 163 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
|
164 if (ninputfiles == 0) |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
112
diff
changeset
|
165 { |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
112
diff
changeset
|
166 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
|
167 exit(1); |
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
112
diff
changeset
|
168 } |
115 | 169 |
182
833d392fec82
Arranged for lwasm and lwlink to remove the output file in case they fail
lost
parents:
180
diff
changeset
|
170 unlink(outfile); |
833d392fec82
Arranged for lwasm and lwlink to remove the output file in case they fail
lost
parents:
180
diff
changeset
|
171 |
119 | 172 // handle the linker script |
117 | 173 setup_script(); |
174 | |
115 | 175 // read the input files |
176 read_files(); | |
205 | 177 |
178 // trace unresolved references and determine which non-forced | |
179 // objects must be included | |
180 resolve_files(); | |
114
c65fcec346cd
Handle input files on command line and add some memory management utility functions
lost
parents:
112
diff
changeset
|
181 |
119 | 182 // resolve section bases and section order |
183 resolve_sections(); | |
184 | |
121 | 185 // resolve incomplete references |
186 resolve_references(); | |
187 | |
188 // do the actual output | |
189 do_output(); | |
185 | 190 |
191 // display/output the link map | |
192 if (map_file) | |
193 display_map(); | |
194 | |
112 | 195 exit(0); |
196 } |