annotate src/main.c @ 318:be4d471f3de5 lwlink-1.0

Moved old tags and branches for lwlink to global tags/branches dirs
author lost
date Wed, 28 Jan 2009 05:54:43 +0000
parents 87c4e65074f0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
294
0f488febdc2b command line options
lost
parents: 293
diff changeset
1 /*
0f488febdc2b command line options
lost
parents: 293
diff changeset
2 main.c
300
48945dac8178 Fixed copyright year
lost
parents: 299
diff changeset
3 Copyright © 2009 William Astle
294
0f488febdc2b command line options
lost
parents: 293
diff changeset
4
0f488febdc2b command line options
lost
parents: 293
diff changeset
5 This file is part of LWLINK.
0f488febdc2b command line options
lost
parents: 293
diff changeset
6
0f488febdc2b command line options
lost
parents: 293
diff changeset
7 LWLINK is free software: you can redistribute it and/or modify it under the
0f488febdc2b command line options
lost
parents: 293
diff changeset
8 terms of the GNU General Public License as published by the Free Software
0f488febdc2b command line options
lost
parents: 293
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
0f488febdc2b command line options
lost
parents: 293
diff changeset
10 version.
0f488febdc2b command line options
lost
parents: 293
diff changeset
11
0f488febdc2b command line options
lost
parents: 293
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
0f488febdc2b command line options
lost
parents: 293
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0f488febdc2b command line options
lost
parents: 293
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
0f488febdc2b command line options
lost
parents: 293
diff changeset
15 more details.
0f488febdc2b command line options
lost
parents: 293
diff changeset
16
0f488febdc2b command line options
lost
parents: 293
diff changeset
17 You should have received a copy of the GNU General Public License along with
0f488febdc2b command line options
lost
parents: 293
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
0f488febdc2b command line options
lost
parents: 293
diff changeset
19
0f488febdc2b command line options
lost
parents: 293
diff changeset
20
0f488febdc2b command line options
lost
parents: 293
diff changeset
21 Implements the program startup code
0f488febdc2b command line options
lost
parents: 293
diff changeset
22
0f488febdc2b command line options
lost
parents: 293
diff changeset
23 */
0f488febdc2b command line options
lost
parents: 293
diff changeset
24
0f488febdc2b command line options
lost
parents: 293
diff changeset
25 #ifdef HAVE_CONFIG_H
0f488febdc2b command line options
lost
parents: 293
diff changeset
26 #include "config.h"
0f488febdc2b command line options
lost
parents: 293
diff changeset
27 #endif
0f488febdc2b command line options
lost
parents: 293
diff changeset
28
0f488febdc2b command line options
lost
parents: 293
diff changeset
29 #include <argp.h>
0f488febdc2b command line options
lost
parents: 293
diff changeset
30 #include <errno.h>
0f488febdc2b command line options
lost
parents: 293
diff changeset
31 #include <stdio.h>
0f488febdc2b command line options
lost
parents: 293
diff changeset
32 #include <stdlib.h>
0f488febdc2b command line options
lost
parents: 293
diff changeset
33
0f488febdc2b command line options
lost
parents: 293
diff changeset
34 #include "lwlink.h"
0f488febdc2b command line options
lost
parents: 293
diff changeset
35
0f488febdc2b command line options
lost
parents: 293
diff changeset
36 // command line option handling
0f488febdc2b command line options
lost
parents: 293
diff changeset
37 const char *argp_program_version = PACKAGE_STRING;
0f488febdc2b command line options
lost
parents: 293
diff changeset
38 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
0f488febdc2b command line options
lost
parents: 293
diff changeset
39
0f488febdc2b command line options
lost
parents: 293
diff changeset
40 static error_t parse_opts(int key, char *arg, struct argp_state *state)
0f488febdc2b command line options
lost
parents: 293
diff changeset
41 {
0f488febdc2b command line options
lost
parents: 293
diff changeset
42 switch (key)
0f488febdc2b command line options
lost
parents: 293
diff changeset
43 {
0f488febdc2b command line options
lost
parents: 293
diff changeset
44 case 'o':
0f488febdc2b command line options
lost
parents: 293
diff changeset
45 // output
0f488febdc2b command line options
lost
parents: 293
diff changeset
46 outfile = arg;
0f488febdc2b command line options
lost
parents: 293
diff changeset
47 break;
0f488febdc2b command line options
lost
parents: 293
diff changeset
48
299
460d96987670 parse linking scripts
lost
parents: 297
diff changeset
49 case 's':
460d96987670 parse linking scripts
lost
parents: 297
diff changeset
50 // script file
460d96987670 parse linking scripts
lost
parents: 297
diff changeset
51 scriptfile = arg;
460d96987670 parse linking scripts
lost
parents: 297
diff changeset
52 break;
460d96987670 parse linking scripts
lost
parents: 297
diff changeset
53
294
0f488febdc2b command line options
lost
parents: 293
diff changeset
54 case 'd':
0f488febdc2b command line options
lost
parents: 293
diff changeset
55 // debug
0f488febdc2b command line options
lost
parents: 293
diff changeset
56 debug_level++;
0f488febdc2b command line options
lost
parents: 293
diff changeset
57 break;
0f488febdc2b command line options
lost
parents: 293
diff changeset
58
0f488febdc2b command line options
lost
parents: 293
diff changeset
59 case 'b':
0f488febdc2b command line options
lost
parents: 293
diff changeset
60 // decb output
0f488febdc2b command line options
lost
parents: 293
diff changeset
61 outformat = OUTPUT_DECB;
0f488febdc2b command line options
lost
parents: 293
diff changeset
62 break;
0f488febdc2b command line options
lost
parents: 293
diff changeset
63
0f488febdc2b command line options
lost
parents: 293
diff changeset
64 case 'r':
0f488febdc2b command line options
lost
parents: 293
diff changeset
65 // raw binary output
0f488febdc2b command line options
lost
parents: 293
diff changeset
66 outformat = OUTPUT_RAW;
0f488febdc2b command line options
lost
parents: 293
diff changeset
67 break;
0f488febdc2b command line options
lost
parents: 293
diff changeset
68
0f488febdc2b command line options
lost
parents: 293
diff changeset
69 case 'f':
0f488febdc2b command line options
lost
parents: 293
diff changeset
70 // output format
0f488febdc2b command line options
lost
parents: 293
diff changeset
71 if (!strcasecmp(arg, "decb"))
0f488febdc2b command line options
lost
parents: 293
diff changeset
72 outformat = OUTPUT_DECB;
0f488febdc2b command line options
lost
parents: 293
diff changeset
73 else if (!strcasecmp(arg, "raw"))
0f488febdc2b command line options
lost
parents: 293
diff changeset
74 outformat = OUTPUT_RAW;
0f488febdc2b command line options
lost
parents: 293
diff changeset
75 else
0f488febdc2b command line options
lost
parents: 293
diff changeset
76 {
0f488febdc2b command line options
lost
parents: 293
diff changeset
77 fprintf(stderr, "Invalid output format: %s\n", arg);
0f488febdc2b command line options
lost
parents: 293
diff changeset
78 exit(1);
0f488febdc2b command line options
lost
parents: 293
diff changeset
79 }
0f488febdc2b command line options
lost
parents: 293
diff changeset
80 break;
0f488febdc2b command line options
lost
parents: 293
diff changeset
81 case ARGP_KEY_END:
0f488febdc2b command line options
lost
parents: 293
diff changeset
82 // done; sanity check
0f488febdc2b command line options
lost
parents: 293
diff changeset
83 if (!outfile)
0f488febdc2b command line options
lost
parents: 293
diff changeset
84 outfile = "a.out";
0f488febdc2b command line options
lost
parents: 293
diff changeset
85 break;
0f488febdc2b command line options
lost
parents: 293
diff changeset
86
0f488febdc2b command line options
lost
parents: 293
diff changeset
87 case ARGP_KEY_ARG:
296
14d835cf02d9 Handle input files on command line and add some memory management utility functions
lost
parents: 294
diff changeset
88 add_input_file(arg);
294
0f488febdc2b command line options
lost
parents: 293
diff changeset
89 break;
0f488febdc2b command line options
lost
parents: 293
diff changeset
90
0f488febdc2b command line options
lost
parents: 293
diff changeset
91 default:
0f488febdc2b command line options
lost
parents: 293
diff changeset
92 return ARGP_ERR_UNKNOWN;
0f488febdc2b command line options
lost
parents: 293
diff changeset
93 }
0f488febdc2b command line options
lost
parents: 293
diff changeset
94 return 0;
0f488febdc2b command line options
lost
parents: 293
diff changeset
95 }
0f488febdc2b command line options
lost
parents: 293
diff changeset
96
0f488febdc2b command line options
lost
parents: 293
diff changeset
97 static struct argp_option options[] =
0f488febdc2b command line options
lost
parents: 293
diff changeset
98 {
0f488febdc2b command line options
lost
parents: 293
diff changeset
99 { "output", 'o', "FILE", 0,
0f488febdc2b command line options
lost
parents: 293
diff changeset
100 "Output to FILE"},
0f488febdc2b command line options
lost
parents: 293
diff changeset
101 { "debug", 'd', 0, 0,
0f488febdc2b command line options
lost
parents: 293
diff changeset
102 "Set debug mode"},
0f488febdc2b command line options
lost
parents: 293
diff changeset
103 { "format", 'f', "TYPE", 0,
0f488febdc2b command line options
lost
parents: 293
diff changeset
104 "Select output format: decb, raw, obj"},
0f488febdc2b command line options
lost
parents: 293
diff changeset
105 { "decb", 'b', 0, 0,
0f488febdc2b command line options
lost
parents: 293
diff changeset
106 "Generate DECB .bin format output, equivalent of --format=decb"},
0f488febdc2b command line options
lost
parents: 293
diff changeset
107 { "raw", 'r', 0, 0,
0f488febdc2b command line options
lost
parents: 293
diff changeset
108 "Generate raw binary format output, equivalent of --format=raw"},
307
87c4e65074f0 Actually allow script file to be specified and fix segfault on parsing script file
lost
parents: 303
diff changeset
109 { "script", 's', "FILE", 0,
299
460d96987670 parse linking scripts
lost
parents: 297
diff changeset
110 "Specify the linking script (overrides the build in defaults)"},
294
0f488febdc2b command line options
lost
parents: 293
diff changeset
111 { 0 }
0f488febdc2b command line options
lost
parents: 293
diff changeset
112 };
0f488febdc2b command line options
lost
parents: 293
diff changeset
113
0f488febdc2b command line options
lost
parents: 293
diff changeset
114 static struct argp argp =
0f488febdc2b command line options
lost
parents: 293
diff changeset
115 {
0f488febdc2b command line options
lost
parents: 293
diff changeset
116 options,
0f488febdc2b command line options
lost
parents: 293
diff changeset
117 parse_opts,
0f488febdc2b command line options
lost
parents: 293
diff changeset
118 "<input file> ...",
0f488febdc2b command line options
lost
parents: 293
diff changeset
119 "LWLINK, a HD6309 and MC6809 cross-linker"
0f488febdc2b command line options
lost
parents: 293
diff changeset
120 };
0f488febdc2b command line options
lost
parents: 293
diff changeset
121
297
c52ad3135bd3 implement reading files
lost
parents: 296
diff changeset
122 extern void read_files(void);
299
460d96987670 parse linking scripts
lost
parents: 297
diff changeset
123 extern void setup_script(void);
301
376cc55361c7 section order and address resolution done
lost
parents: 300
diff changeset
124 extern void resolve_sections(void);
303
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
125 extern void resolve_references(void);
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
126 extern void do_output(void);
297
c52ad3135bd3 implement reading files
lost
parents: 296
diff changeset
127
294
0f488febdc2b command line options
lost
parents: 293
diff changeset
128 // main function; parse command line, set up assembler state, and run the
0f488febdc2b command line options
lost
parents: 293
diff changeset
129 // assembler on the first file
0f488febdc2b command line options
lost
parents: 293
diff changeset
130 int main(int argc, char **argv)
0f488febdc2b command line options
lost
parents: 293
diff changeset
131 {
0f488febdc2b command line options
lost
parents: 293
diff changeset
132 argp_parse(&argp, argc, argv, 0, 0, NULL);
296
14d835cf02d9 Handle input files on command line and add some memory management utility functions
lost
parents: 294
diff changeset
133 if (ninputfiles == 0)
14d835cf02d9 Handle input files on command line and add some memory management utility functions
lost
parents: 294
diff changeset
134 {
14d835cf02d9 Handle input files on command line and add some memory management utility functions
lost
parents: 294
diff changeset
135 fprintf(stderr, "No input files\n");
14d835cf02d9 Handle input files on command line and add some memory management utility functions
lost
parents: 294
diff changeset
136 exit(1);
14d835cf02d9 Handle input files on command line and add some memory management utility functions
lost
parents: 294
diff changeset
137 }
297
c52ad3135bd3 implement reading files
lost
parents: 296
diff changeset
138
301
376cc55361c7 section order and address resolution done
lost
parents: 300
diff changeset
139 // handle the linker script
299
460d96987670 parse linking scripts
lost
parents: 297
diff changeset
140 setup_script();
460d96987670 parse linking scripts
lost
parents: 297
diff changeset
141
297
c52ad3135bd3 implement reading files
lost
parents: 296
diff changeset
142 // read the input files
c52ad3135bd3 implement reading files
lost
parents: 296
diff changeset
143 read_files();
296
14d835cf02d9 Handle input files on command line and add some memory management utility functions
lost
parents: 294
diff changeset
144
301
376cc55361c7 section order and address resolution done
lost
parents: 300
diff changeset
145 // resolve section bases and section order
376cc55361c7 section order and address resolution done
lost
parents: 300
diff changeset
146 resolve_sections();
376cc55361c7 section order and address resolution done
lost
parents: 300
diff changeset
147
303
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
148 // resolve incomplete references
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
149 resolve_references();
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
150
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
151 // do the actual output
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
152 do_output();
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
153
294
0f488febdc2b command line options
lost
parents: 293
diff changeset
154 exit(0);
0f488febdc2b command line options
lost
parents: 293
diff changeset
155 }