annotate lwlink/lwlink.c @ 254:c7a41b4c89b3 2.x

Added struct support to LWASM
author lost
date Sat, 19 Dec 2009 06:38:43 +0000
parents bae1e3ecdce1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
112
a567dbb3f1d4 command line options
lost
parents:
diff changeset
1 /*
a567dbb3f1d4 command line options
lost
parents:
diff changeset
2 lwlink.c
118
d450943305a7 Fixed copyright year
lost
parents: 117
diff changeset
3 Copyright © 2009 William Astle
112
a567dbb3f1d4 command line options
lost
parents:
diff changeset
4
a567dbb3f1d4 command line options
lost
parents:
diff changeset
5 This file is part of LWLINK.
a567dbb3f1d4 command line options
lost
parents:
diff changeset
6
a567dbb3f1d4 command line options
lost
parents:
diff changeset
7 LWLINK is free software: you can redistribute it and/or modify it under the
a567dbb3f1d4 command line options
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
a567dbb3f1d4 command line options
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
a567dbb3f1d4 command line options
lost
parents:
diff changeset
10 version.
a567dbb3f1d4 command line options
lost
parents:
diff changeset
11
a567dbb3f1d4 command line options
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
a567dbb3f1d4 command line options
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a567dbb3f1d4 command line options
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
a567dbb3f1d4 command line options
lost
parents:
diff changeset
15 more details.
a567dbb3f1d4 command line options
lost
parents:
diff changeset
16
a567dbb3f1d4 command line options
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
a567dbb3f1d4 command line options
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
a567dbb3f1d4 command line options
lost
parents:
diff changeset
19
a567dbb3f1d4 command line options
lost
parents:
diff changeset
20
a567dbb3f1d4 command line options
lost
parents:
diff changeset
21
a567dbb3f1d4 command line options
lost
parents:
diff changeset
22 */
a567dbb3f1d4 command line options
lost
parents:
diff changeset
23
212
bae1e3ecdce1 More preparation for gnulib integration
lost
parents: 205
diff changeset
24 #include <config.h>
112
a567dbb3f1d4 command line options
lost
parents:
diff changeset
25
a567dbb3f1d4 command line options
lost
parents:
diff changeset
26 #define __lwlink_c_seen__
a567dbb3f1d4 command line options
lost
parents:
diff changeset
27
a567dbb3f1d4 command line options
lost
parents:
diff changeset
28 #include <argp.h>
a567dbb3f1d4 command line options
lost
parents:
diff changeset
29 #include <errno.h>
a567dbb3f1d4 command line options
lost
parents:
diff changeset
30 #include <stdio.h>
a567dbb3f1d4 command line options
lost
parents:
diff changeset
31 #include <stdlib.h>
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
32 #include <string.h>
112
a567dbb3f1d4 command line options
lost
parents:
diff changeset
33
a567dbb3f1d4 command line options
lost
parents:
diff changeset
34 #include "lwlink.h"
114
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
35 #include "util.h"
112
a567dbb3f1d4 command line options
lost
parents:
diff changeset
36
a567dbb3f1d4 command line options
lost
parents:
diff changeset
37 int debug_level = 0;
a567dbb3f1d4 command line options
lost
parents:
diff changeset
38 int outformat = OUTPUT_DECB;
a567dbb3f1d4 command line options
lost
parents:
diff changeset
39 char *outfile = NULL;
117
bb3cc989e84b parse linking scripts
lost
parents: 115
diff changeset
40 char *scriptfile = NULL;
184
220a760ec654 Make lwlink display all undefined references instead of bailing after the first one
lost
parents: 180
diff changeset
41 int symerr = 0;
185
b89adfb0d174 Added support for outputting a linkmap
lost
parents: 184
diff changeset
42 char *map_file = NULL;
112
a567dbb3f1d4 command line options
lost
parents:
diff changeset
43
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
44 fileinfo_t **inputfiles = NULL;
114
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
45 int ninputfiles = 0;
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
46
180
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
47 int nlibdirs = 0;
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
48 char **libdirs = NULL;
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
49
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
50 int nscriptls = 0;
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
51 char **scriptls = NULL;
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
52
114
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
53 void add_input_file(char *fn)
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
54 {
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
55 inputfiles = lw_realloc(inputfiles, sizeof(fileinfo_t *) * (ninputfiles + 1));
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
56 inputfiles[ninputfiles] = lw_malloc(sizeof(fileinfo_t));
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
57 memset(inputfiles[ninputfiles], 0, sizeof(fileinfo_t));
205
42df94f30d82 checkpoint
lost
parents: 185
diff changeset
58 inputfiles[ninputfiles] -> forced = 1;
115
776d8bea5b46 implement reading files
lost
parents: 114
diff changeset
59 inputfiles[ninputfiles++] -> filename = lw_strdup(fn);
114
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
60 }
c65fcec346cd Handle input files on command line and add some memory management utility functions
lost
parents: 112
diff changeset
61
180
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
62 void add_input_library(char *libname)
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
63 {
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
64 inputfiles = lw_realloc(inputfiles, sizeof(fileinfo_t *) * (ninputfiles + 1));
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
65 inputfiles[ninputfiles] = lw_malloc(sizeof(fileinfo_t));
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
66 memset(inputfiles[ninputfiles], 0, sizeof(fileinfo_t));
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
67 inputfiles[ninputfiles] -> islib = 1;
205
42df94f30d82 checkpoint
lost
parents: 185
diff changeset
68 inputfiles[ninputfiles] -> forced = 0;
180
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
69 inputfiles[ninputfiles++] -> filename = lw_strdup(libname);
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
70 }
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
71
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
72 void add_library_search(char *libdir)
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
73 {
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
74 libdirs = lw_realloc(libdirs, sizeof(char*) * (nlibdirs + 1));
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
75 libdirs[nlibdirs] = lw_strdup(libdir);
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
76 nlibdirs++;
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
77 }
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
78
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
79 void add_section_base(char *sectspec)
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
80 {
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
81 char *base;
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
82 int baseaddr;
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
83 char *t;
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
84 int l;
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
85
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
86 base = strchr(sectspec, '=');
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
87 if (!base)
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
88 {
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
89 l = strlen(sectspec);
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
90 baseaddr = 0;
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
91 }
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
92 else
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
93 {
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
94 baseaddr = strtol(base + 1, NULL, 16);
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
95 l = base - sectspec;
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
96 *base = '\0';
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
97 }
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
98 baseaddr = baseaddr & 0xffff;
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
99
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
100 t = lw_malloc(l + 25);
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
101 sprintf(t, "section %s load %04X", sectspec, baseaddr);
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
102 if (base)
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
103 *base = '=';
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
104
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
105 scriptls = lw_realloc(scriptls, sizeof(char *) * (nscriptls + 1));
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
106 scriptls[nscriptls++] = t;
6ebb93b409ba Added library paths and --section-base
lost
parents: 171
diff changeset
107 }