annotate lwlink/lwlink.c @ 448:5cccf90bf838 3.0 tip

Fixed bug with complex external references generating invalid relocations in the object file
author lost@l-w.ca
date Fri, 05 Nov 2010 22:27:00 -0600
parents 54499b799779
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 lwlink.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
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 #include <config.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
25
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
26 #define __lwlink_c_seen__
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
27
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
28 #include <argp.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
29 #include <errno.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
30 #include <stdio.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
31 #include <stdlib.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
32 #include <string.h>
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
33
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
34 #include "lwlink.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
35 #include "util.h"
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
36
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
37 int debug_level = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
38 int outformat = OUTPUT_DECB;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
39 char *outfile = NULL;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
40 char *scriptfile = NULL;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
41 int symerr = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
42 char *map_file = NULL;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
43
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
44 fileinfo_t **inputfiles = NULL;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
45 int ninputfiles = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
46
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
47 int nlibdirs = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
48 char **libdirs = NULL;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
49
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
50 int nscriptls = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
51 char **scriptls = NULL;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
52
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
53 void add_input_file(char *fn)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
54 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
55 inputfiles = lw_realloc(inputfiles, sizeof(fileinfo_t *) * (ninputfiles + 1));
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
56 inputfiles[ninputfiles] = lw_malloc(sizeof(fileinfo_t));
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
57 memset(inputfiles[ninputfiles], 0, sizeof(fileinfo_t));
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
58 inputfiles[ninputfiles] -> forced = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
59 inputfiles[ninputfiles++] -> filename = lw_strdup(fn);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
60 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
61
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
62 void add_input_library(char *libname)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
63 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
64 inputfiles = lw_realloc(inputfiles, sizeof(fileinfo_t *) * (ninputfiles + 1));
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
65 inputfiles[ninputfiles] = lw_malloc(sizeof(fileinfo_t));
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
66 memset(inputfiles[ninputfiles], 0, sizeof(fileinfo_t));
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
67 inputfiles[ninputfiles] -> islib = 1;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
68 inputfiles[ninputfiles] -> forced = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
69 inputfiles[ninputfiles++] -> filename = lw_strdup(libname);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
70 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
71
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
72 void add_library_search(char *libdir)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
73 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
74 libdirs = lw_realloc(libdirs, sizeof(char*) * (nlibdirs + 1));
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
75 libdirs[nlibdirs] = lw_strdup(libdir);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
76 nlibdirs++;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
77 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
78
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
79 void add_section_base(char *sectspec)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
80 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
81 char *base;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
82 int baseaddr;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
83 char *t;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
84 int l;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
85
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
86 base = strchr(sectspec, '=');
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
87 if (!base)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
88 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
89 l = strlen(sectspec);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
90 baseaddr = 0;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
91 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
92 else
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
93 {
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
94 baseaddr = strtol(base + 1, NULL, 16);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
95 l = base - sectspec;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
96 *base = '\0';
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
97 }
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
98 baseaddr = baseaddr & 0xffff;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
99
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
100 t = lw_malloc(l + 25);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
101 sprintf(t, "section %s load %04X", sectspec, baseaddr);
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
102 if (base)
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
103 *base = '=';
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
104
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
105 scriptls = lw_realloc(scriptls, sizeof(char *) * (nscriptls + 1));
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
106 scriptls[nscriptls++] = t;
eb230fa7d28e Prepare for migration to hg
lost
parents:
diff changeset
107 }
395
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
108
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
109 char *sanitize_symbol(char *symbol)
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
110 {
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
111 static char symbuf[2048];
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
112 char *sym = symbol;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
113 char *tp = symbuf;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
114
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
115 for (; *sym; sym++)
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
116 {
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
117 int c1 = *sym;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
118 if (c1 == '\\')
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
119 {
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
120 *tp++ = '\\';
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
121 *tp++ = '\\';
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
122 continue;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
123 }
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
124 if (c1 < 32 || c1 > 126)
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
125 {
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
126 int c;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
127 *tp++ = '\\';
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
128 c = c1 >> 4;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
129 c += 48;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
130 if (c > 57)
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
131 c += 7;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
132 *tp++ = c;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
133 c = c1 & 15;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
134 c += 48;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
135 if (c > 57)
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
136 c += 7;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
137 *tp++ = c;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
138 continue;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
139 }
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
140 *tp++ = c1;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
141 }
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
142 *tp = 0;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
143 return symbuf;
54499b799779 Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents: 378
diff changeset
144 }