annotate old-trunk/lwlink/main.c @ 348:11a95c6414b4

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