annotate lwlink/main.c @ 158:f0527dc3804d

Added support for .globl <sym>, .area <section>, .word, .byte, .blkb, and a .module directive that does nothing
author lost
date Sat, 31 Jan 2009 05:30:06 +0000
parents 3706ede361ea
children 6ebb93b409ba
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
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
87 case ARGP_KEY_ARG:
114
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
88 add_input_file(arg);
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
89 break;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
90
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
91 default:
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
92 return ARGP_ERR_UNKNOWN;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
93 }
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
94 return 0;
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
95 }
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
96
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
97 static struct argp_option options[] =
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
98 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
99 { "output", 'o', "FILE", 0,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
100 "Output to FILE"},
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
101 { "debug", 'd', 0, 0,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
102 "Set debug mode"},
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
103 { "format", 'f', "TYPE", 0,
149
3706ede361ea Fixed help message for --format
lost
parents: 142
diff changeset
104 "Select output format: decb, raw"},
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
105 { "decb", 'b', 0, 0,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
106 "Generate DECB .bin format output, equivalent of --format=decb"},
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
107 { "raw", 'r', 0, 0,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
108 "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
109 { "script", 's', "FILE", 0,
117
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
110 "Specify the linking script (overrides the build in defaults)"},
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
111 { 0 }
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
112 };
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
113
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
114 static struct argp argp =
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
115 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
116 options,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
117 parse_opts,
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
118 "<input file> ...",
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
119 "LWLINK, a HD6309 and MC6809 cross-linker"
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
120 };
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
121
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
122 extern void read_files(void);
117
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
123 extern void setup_script(void);
119
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
124 extern void resolve_sections(void);
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
125 extern void resolve_references(void);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
126 extern void do_output(void);
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
127
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
128 // main function; parse command line, set up assembler state, and run the
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
129 // assembler on the first file
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
130 int main(int argc, char **argv)
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
131 {
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
132 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
133 if (ninputfiles == 0)
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
134 {
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
135 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
136 exit(1);
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
137 }
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
138
119
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
139 // handle the linker script
117
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
140 setup_script();
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
141
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
142 // read the input files
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
143 read_files();
114
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
144
119
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
145 // resolve section bases and section order
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
146 resolve_sections();
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
147
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
148 // resolve incomplete references
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
149 resolve_references();
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
150
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
151 // do the actual output
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
152 do_output();
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
153
112
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
154 exit(0);
a567dbb3f1d4 command line options
lost
parents: 111
diff changeset
155 }