annotate lwlink/main.c @ 180:6ebb93b409ba

Added library paths and --section-base
author lost
date Thu, 05 Mar 2009 02:23:25 +0000
parents 3706ede361ea
children 833d392fec82
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>
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
33
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
34 #include "lwlink.h"
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
35
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
36 // command line option handling
142
36ca3fa755e0 Autotools merge of packages done
lost
parents: 139
diff changeset
37 const char *argp_program_version = "LWLINK from " PACKAGE_STRING;
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
38 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
39
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
40 static error_t parse_opts(int key, char *arg, struct argp_state *state)
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
41 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
42 switch (key)
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
43 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
44 case 'o':
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
45 // output
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
46 outfile = arg;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
47 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
48
117
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
49 case 's':
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
50 // script file
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
51 scriptfile = arg;
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
52 break;
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
53
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
54 case 'd':
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
55 // debug
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
56 debug_level++;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
57 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
58
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
59 case 'b':
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
60 // decb output
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
61 outformat = OUTPUT_DECB;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
62 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
63
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
64 case 'r':
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
65 // raw binary output
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
66 outformat = OUTPUT_RAW;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
67 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
68
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
69 case 'f':
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
70 // output format
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
71 if (!strcasecmp(arg, "decb"))
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
72 outformat = OUTPUT_DECB;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
73 else if (!strcasecmp(arg, "raw"))
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
74 outformat = OUTPUT_RAW;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
75 else
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
76 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
77 fprintf(stderr, "Invalid output format: %s\n", arg);
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
78 exit(1);
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
79 }
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
80 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
81 case ARGP_KEY_END:
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
82 // done; sanity check
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
83 if (!outfile)
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
84 outfile = "a.out";
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
85 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
86
180
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
87 case 'l':
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
88 add_input_library(arg);
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
89 break;
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
90
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
91 case 'L':
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
92 add_library_search(arg);
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
93 break;
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
94
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
95 case 0x100:
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
96 add_section_base(arg);
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
97 break;
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
98
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
99 case ARGP_KEY_ARG:
114
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
100 add_input_file(arg);
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
101 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
102
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
103 default:
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
104 return ARGP_ERR_UNKNOWN;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
105 }
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
106 return 0;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
107 }
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
108
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
109 static struct argp_option options[] =
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
110 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
111 { "output", 'o', "FILE", 0,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
112 "Output to FILE"},
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
113 { "debug", 'd', 0, 0,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
114 "Set debug mode"},
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
115 { "format", 'f', "TYPE", 0,
149
3706ede361ea Fixed help message for --format
lost
parents: 142
diff changeset
116 "Select output format: decb, raw"},
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
117 { "decb", 'b', 0, 0,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
118 "Generate DECB .bin format output, equivalent of --format=decb"},
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
119 { "raw", 'r', 0, 0,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
120 "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
121 { "script", 's', "FILE", 0,
180
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
122 "Specify the linking script (overrides the built in defaults)"},
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
123 { "library", 'l', "LIBSPEC", 0,
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
124 "Read library libLIBSPEC.a from the search path" },
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
125 { "library-path", 'L', "DIR", 0,
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
126 "Add DIR to the library search path" },
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
127 { "section-base", 0x100, "SECT=BASE", 0,
6ebb93b409ba Added library paths and --section-base
lost
parents: 149
diff changeset
128 "Load section SECT at BASE" },
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
129 { 0 }
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
130 };
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
131
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
132 static struct argp argp =
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
133 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
134 options,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
135 parse_opts,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
136 "<input file> ...",
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
137 "LWLINK, a HD6309 and MC6809 cross-linker"
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
138 };
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
139
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
140 extern void read_files(void);
117
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
141 extern void setup_script(void);
119
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
142 extern void resolve_sections(void);
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
143 extern void resolve_references(void);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
144 extern void do_output(void);
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
145
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
146 // main function; parse command line, set up assembler state, and run the
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
147 // assembler on the first file
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
148 int main(int argc, char **argv)
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
149 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
150 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
151 if (ninputfiles == 0)
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
152 {
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
153 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
154 exit(1);
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
155 }
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
156
119
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
157 // handle the linker script
117
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
158 setup_script();
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
159
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
160 // read the input files
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
161 read_files();
114
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
162
119
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
163 // resolve section bases and section order
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
164 resolve_sections();
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
165
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
166 // resolve incomplete references
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
167 resolve_references();
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
168
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
169 // do the actual output
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
170 do_output();
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
171
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
172 exit(0);
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
173 }