comparison lwlink/main.c @ 0:2c24602be78f

Initial import from lwtools 3.0.1 version, with new hand built build system and file reorganization
author lost@l-w.ca
date Wed, 19 Jan 2011 22:27:17 -0700
parents
children 7317fbe024af
comparison
equal deleted inserted replaced
-1:000000000000 0:2c24602be78f
1 /*
2 main.c
3 Copyright © 2009 William Astle
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 #include <argp.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30
31 #include "lwlink.h"
32
33 char *program_name;
34
35 // command line option handling
36 const char *argp_program_version = "LWLINK from " PACKAGE_STRING;
37 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
38
39 static error_t parse_opts(int key, char *arg, struct argp_state *state)
40 {
41 switch (key)
42 {
43 case 'o':
44 // output
45 outfile = arg;
46 break;
47
48 case 's':
49 // script file
50 scriptfile = arg;
51 break;
52
53 case 'd':
54 // debug
55 debug_level++;
56 break;
57
58 case 'b':
59 // decb output
60 outformat = OUTPUT_DECB;
61 break;
62
63 case 'r':
64 // raw binary output
65 outformat = OUTPUT_RAW;
66 break;
67
68 case 'f':
69 // output format
70 if (!strcasecmp(arg, "decb"))
71 outformat = OUTPUT_DECB;
72 else if (!strcasecmp(arg, "raw"))
73 outformat = OUTPUT_RAW;
74 else if (!strcasecmp(arg, "lwex0") || !strcasecmp(arg, "lwex"))
75 outformat = OUTPUT_LWEX0;
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
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
100 case 'm':
101 map_file = arg;
102 break;
103
104 case ARGP_KEY_ARG:
105 add_input_file(arg);
106 break;
107
108 default:
109 return ARGP_ERR_UNKNOWN;
110 }
111 return 0;
112 }
113
114 static struct argp_option options[] =
115 {
116 { "output", 'o', "FILE", 0,
117 "Output to FILE"},
118 { "debug", 'd', 0, 0,
119 "Set debug mode"},
120 { "format", 'f', "TYPE", 0,
121 "Select output format: decb, raw, lwex"},
122 { "decb", 'b', 0, 0,
123 "Generate DECB .bin format output, equivalent of --format=decb"},
124 { "raw", 'r', 0, 0,
125 "Generate raw binary format output, equivalent of --format=raw"},
126 { "script", 's', "FILE", 0,
127 "Specify the linking script (overrides the built in defaults)"},
128 { "library", 'l', "LIBSPEC", 0,
129 "Read library libLIBSPEC.a from the search path" },
130 { "library-path", 'L', "DIR", 0,
131 "Add DIR to the library search path" },
132 { "section-base", 0x100, "SECT=BASE", 0,
133 "Load section SECT at BASE" },
134 { "map", 'm', "FILE", 0,
135 "Output informaiton about the link" },
136 { 0 }
137 };
138
139 static struct argp argp =
140 {
141 options,
142 parse_opts,
143 "<input file> ...",
144 "LWLINK, a HD6309 and MC6809 cross-linker"
145 };
146
147 extern void read_files(void);
148 extern void setup_script(void);
149 extern void resolve_files(void);
150 extern void resolve_sections(void);
151 extern void resolve_references(void);
152 extern void do_output(void);
153 extern void display_map(void);
154
155 // main function; parse command line, set up assembler state, and run the
156 // assembler on the first file
157 int main(int argc, char **argv)
158 {
159 program_name = argv[0];
160
161 argp_parse(&argp, argc, argv, 0, 0, NULL);
162 if (ninputfiles == 0)
163 {
164 fprintf(stderr, "No input files\n");
165 exit(1);
166 }
167
168 unlink(outfile);
169
170 // handle the linker script
171 setup_script();
172
173 // read the input files
174 read_files();
175
176 // trace unresolved references and determine which non-forced
177 // objects must be included
178 resolve_files();
179
180 // resolve section bases and section order
181 resolve_sections();
182
183 // resolve incomplete references
184 resolve_references();
185
186 // do the actual output
187 do_output();
188
189 // display/output the link map
190 if (map_file)
191 display_map();
192
193 exit(0);
194 }