301
|
1 /*
|
|
2 link.c
|
|
3 Copyright © 2009 William Astle
|
|
4
|
|
5 This file is part of LWLINK.
|
|
6
|
|
7 LWLINK is free software: you can redistribute it and/or modify it under the
|
|
8 terms of the GNU General Public License as published by the Free Software
|
|
9 Foundation, either version 3 of the License, or (at your option) any later
|
|
10 version.
|
|
11
|
|
12 This program is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
15 more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License along with
|
|
18 this program. If not, see <http://www.gnu.org/licenses/>.
|
|
19
|
|
20
|
|
21 Resolve section and symbol addresses; handle incomplete references
|
|
22 */
|
|
23
|
|
24 #ifdef HAVE_CONFIG_H
|
|
25 #include "config.h"
|
|
26 #endif
|
|
27
|
302
|
28 #include <stdio.h>
|
301
|
29 #include <stdlib.h>
|
|
30
|
302
|
31 #include "expr.h"
|
301
|
32 #include "lwlink.h"
|
|
33 #include "util.h"
|
|
34
|
303
|
35 struct section_list *sectlist = NULL;
|
|
36 int nsects = 0;
|
301
|
37
|
|
38 // work out section load order and resolve base addresses for each section
|
|
39 // make a list of sections to load in order
|
|
40 void resolve_sections(void)
|
|
41 {
|
|
42 int laddr = 0;
|
|
43 int ln, sn, fn;
|
|
44
|
|
45 for (ln = 0; ln < linkscript.nlines; ln++)
|
|
46 {
|
|
47 if (linkscript.lines[ln].sectname)
|
|
48 {
|
|
49 int f = 0;
|
|
50 // named section
|
|
51 // look for all instances of a section by the specified name
|
|
52 // and resolve base addresses and add to the list
|
|
53 for (fn = 0; fn < ninputfiles; fn++)
|
|
54 {
|
|
55 for (sn = 0; sn < inputfiles[fn] -> nsections; sn++)
|
|
56 {
|
|
57 if (!strcmp(linkscript.lines[ln].sectname, inputfiles[fn] -> sections[sn].name))
|
|
58 {
|
|
59 // we have a match
|
|
60 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
|
|
61 sectlist[nsects].ptr = &(inputfiles[fn] -> sections[sn]);
|
|
62
|
|
63 inputfiles[fn] -> sections[sn].processed = 1;
|
|
64 if (!f && linkscript.lines[ln].loadat >= 0)
|
|
65 {
|
|
66 f = 1;
|
|
67 sectlist[nsects].forceaddr = 1;
|
|
68 laddr = linkscript.lines[ln].loadat;
|
|
69 }
|
|
70 else
|
|
71 {
|
|
72 sectlist[nsects].forceaddr = 0;
|
|
73 }
|
|
74 inputfiles[fn] -> sections[sn].loadaddress = laddr;
|
|
75 nsects++;
|
|
76 }
|
|
77 }
|
|
78 }
|
|
79 }
|
|
80 else
|
|
81 {
|
|
82 // wildcard section
|
|
83 // look for all sections not yet processed that match flags
|
|
84
|
|
85 int f = 0;
|
|
86 int fn0, sn0;
|
|
87 char *sname;
|
|
88
|
|
89 // named section
|
|
90 // look for all instances of a section by the specified name
|
|
91 // and resolve base addresses and add to the list
|
|
92 for (fn0 = 0; fn0 < ninputfiles; fn0++)
|
|
93 {
|
|
94 for (sn0 = 0; sn0 < inputfiles[fn0] -> nsections; sn0++)
|
|
95 {
|
|
96 // ignore if the "no flags" bit says to
|
|
97 if (linkscript.lines[ln].noflags && (inputfiles[fn0] -> sections[sn0].flags & linkscript.lines[ln].noflags))
|
|
98 continue;
|
|
99 // ignore unless the yes flags tell us not to
|
|
100 if (linkscript.lines[ln].yesflags && (inputfiles[fn0] -> sections[sn0].flags & linkscript.lines[ln].yesflags == 0))
|
|
101 continue;
|
|
102 if (inputfiles[fn0] -> sections[sn0].processed == 0)
|
|
103 {
|
|
104 sname = inputfiles[fn0] -> sections[sn0].name;
|
|
105 for (fn = 0; fn < ninputfiles; fn++)
|
|
106 {
|
|
107 for (sn = 0; sn < inputfiles[fn] -> nsections; sn++)
|
|
108 {
|
|
109 if (!strcmp(sname, inputfiles[fn] -> sections[sn].name))
|
|
110 {
|
|
111 // we have a match
|
|
112 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
|
|
113 sectlist[nsects].ptr = &(inputfiles[fn] -> sections[sn]);
|
|
114
|
|
115 inputfiles[fn] -> sections[sn].processed = 1;
|
|
116 if (!f && linkscript.lines[ln].loadat >= 0)
|
|
117 {
|
|
118 f = 1;
|
|
119 sectlist[nsects].forceaddr = 1;
|
|
120 laddr = linkscript.lines[ln].loadat;
|
|
121 }
|
|
122 else
|
|
123 {
|
|
124 sectlist[nsects].forceaddr = 0;
|
|
125 }
|
|
126 inputfiles[fn] -> sections[sn].loadaddress = laddr;
|
|
127 nsects++;
|
|
128 }
|
|
129 }
|
|
130 }
|
|
131 }
|
|
132 }
|
|
133 }
|
|
134 }
|
|
135 }
|
|
136
|
|
137 // theoretically, all the base addresses are set now
|
|
138 }
|
302
|
139
|
|
140 // resolve all incomplete references now
|
|
141 // anything that is unresolvable at this stage will throw an error
|
|
142 // because we know the load address of every section now
|
|
143 lw_expr_stack_t *resolve_sym(char *sym, int symtype, void *state)
|
|
144 {
|
|
145 section_t *sect = state;
|
|
146 lw_expr_term_t *term;
|
|
147 int val = 0, i, fn;
|
|
148 lw_expr_stack_t *s;
|
|
149 symtab_t *se;
|
|
150
|
|
151 if (symtype == 1)
|
|
152 {
|
|
153 // local symbol
|
|
154 if (!sym)
|
|
155 {
|
|
156 val = sect -> loadaddress;
|
|
157 goto out;
|
|
158 }
|
|
159
|
|
160 // start with this section
|
|
161 for (se = sect -> localsyms; se; se = se -> next)
|
|
162 {
|
|
163 if (!strcmp(se -> sym, sym))
|
|
164 {
|
|
165 val = se -> offset + sect -> loadaddress;
|
|
166 goto out;
|
|
167 }
|
|
168 }
|
|
169 // not in this section - check all sections in this file
|
|
170 for (i = 0; i < sect -> file -> nsections; i++)
|
|
171 {
|
|
172 for (se = sect -> file -> sections[i].localsyms; se; se = se -> next)
|
|
173 {
|
|
174 if (!strcmp(se -> sym, sym))
|
|
175 {
|
|
176 val = se -> offset + sect -> file -> sections[i].loadaddress;
|
|
177 goto out;
|
|
178 }
|
|
179 }
|
|
180 }
|
|
181 // not found
|
|
182 fprintf(stderr, "Local symbol %s not found in %s:%s\n", sym, sect -> file -> filename, sect -> name);
|
|
183 exit(1);
|
|
184 }
|
|
185 else
|
|
186 {
|
|
187 // external symbol
|
|
188 // read all files in order until found (or not found)
|
|
189 for (fn = 0; fn < ninputfiles; fn++)
|
|
190 {
|
|
191 for (i = 0; i < inputfiles[fn] -> nsections; i++)
|
|
192 {
|
|
193 for (se = inputfiles[fn] -> sections[i].exportedsyms; se; se = se -> next)
|
|
194 {
|
|
195 if (!strcmp(sym, se -> sym))
|
|
196 {
|
|
197 val = se -> offset + inputfiles[fn] -> sections[i].loadaddress;
|
|
198 goto out;
|
|
199 }
|
|
200 }
|
|
201 }
|
|
202 }
|
|
203 fprintf(stderr, "External symbol %s not found in %s:%s\n", sym, sect -> file -> filename, sect -> name);
|
|
204 exit(1);
|
|
205 }
|
|
206 fprintf(stderr, "Shouldn't ever get here!!!\n");
|
|
207 exit(88);
|
|
208 out:
|
|
209 s = lw_expr_stack_create();
|
|
210 term = lw_expr_term_create_int(val & 0xffff);
|
|
211 lw_expr_stack_push(s, term);
|
|
212 lw_expr_term_free(term);
|
|
213 return s;
|
|
214 }
|
|
215
|
|
216 void resolve_references(void)
|
|
217 {
|
|
218 int sn;
|
|
219 reloc_t *rl;
|
|
220 int rval;
|
303
|
221
|
|
222 // resolve entry point if required
|
|
223 // this must resolve to an *exported* symbol and will resolve to the
|
|
224 // first instance of that symbol
|
|
225 if (linkscript.execsym)
|
|
226 {
|
|
227 lw_expr_stack_t *s;
|
|
228
|
|
229 s = resolve_sym(linkscript.execsym, 0, NULL);
|
|
230 linkscript.execaddr = lw_expr_get_value(s);
|
|
231 lw_expr_stack_free(s);
|
|
232 }
|
302
|
233
|
|
234 for (sn = 0; nsects; sn++)
|
|
235 {
|
|
236 for (rl = sectlist[sn].ptr -> incompletes; rl; rl = rl -> next)
|
|
237 {
|
|
238 // do a "simplify" on the expression
|
|
239 rval = lw_expr_reval(rl -> expr, resolve_sym, sectlist[sn].ptr);
|
|
240
|
|
241 // is it constant? error out if not
|
|
242 if (rval != 0 || !lw_expr_is_constant(rl -> expr))
|
|
243 {
|
|
244 fprintf(stderr, "Incomplete reference at %s:%s:%02X\n", sectlist[sn].ptr -> file -> filename, sectlist[sn].ptr -> name, rl -> offset);
|
|
245 exit(1);
|
|
246 }
|
|
247
|
|
248 // put the value into the relocation address
|
|
249 rval = lw_expr_get_value(rl -> expr);
|
|
250 sectlist[sn].ptr -> code[rl -> offset] = (rval >> 8) & 0xff;
|
|
251 sectlist[sn].ptr -> code[rl -> offset + 1] = rval & 0xff;
|
|
252 }
|
|
253 }
|
|
254 }
|