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 {
|
308
|
47 // printf("Linker script line %d: '%s', %04X, %d, %d\n", ln, linkscript.lines[ln].sectname, linkscript.lines[ln].loadat, linkscript.lines[ln].yesflags, linkscript.lines[ln].noflags);
|
301
|
48 if (linkscript.lines[ln].sectname)
|
|
49 {
|
|
50 int f = 0;
|
|
51 // named section
|
|
52 // look for all instances of a section by the specified name
|
|
53 // and resolve base addresses and add to the list
|
|
54 for (fn = 0; fn < ninputfiles; fn++)
|
|
55 {
|
|
56 for (sn = 0; sn < inputfiles[fn] -> nsections; sn++)
|
|
57 {
|
308
|
58 // printf(" Considering %s:%s\n", inputfiles[fn]->filename, inputfiles[fn]->sections[sn].name);
|
301
|
59 if (!strcmp(linkscript.lines[ln].sectname, inputfiles[fn] -> sections[sn].name))
|
|
60 {
|
|
61 // we have a match
|
|
62 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
|
|
63 sectlist[nsects].ptr = &(inputfiles[fn] -> sections[sn]);
|
|
64
|
|
65 inputfiles[fn] -> sections[sn].processed = 1;
|
|
66 if (!f && linkscript.lines[ln].loadat >= 0)
|
|
67 {
|
|
68 f = 1;
|
|
69 sectlist[nsects].forceaddr = 1;
|
|
70 laddr = linkscript.lines[ln].loadat;
|
|
71 }
|
|
72 else
|
|
73 {
|
|
74 sectlist[nsects].forceaddr = 0;
|
|
75 }
|
|
76 inputfiles[fn] -> sections[sn].loadaddress = laddr;
|
|
77 nsects++;
|
|
78 }
|
|
79 }
|
|
80 }
|
|
81 }
|
|
82 else
|
|
83 {
|
|
84 // wildcard section
|
|
85 // look for all sections not yet processed that match flags
|
|
86
|
|
87 int f = 0;
|
|
88 int fn0, sn0;
|
|
89 char *sname;
|
|
90
|
|
91 // named section
|
|
92 // look for all instances of a section by the specified name
|
|
93 // and resolve base addresses and add to the list
|
|
94 for (fn0 = 0; fn0 < ninputfiles; fn0++)
|
|
95 {
|
|
96 for (sn0 = 0; sn0 < inputfiles[fn0] -> nsections; sn0++)
|
|
97 {
|
|
98 // ignore if the "no flags" bit says to
|
|
99 if (linkscript.lines[ln].noflags && (inputfiles[fn0] -> sections[sn0].flags & linkscript.lines[ln].noflags))
|
|
100 continue;
|
|
101 // ignore unless the yes flags tell us not to
|
|
102 if (linkscript.lines[ln].yesflags && (inputfiles[fn0] -> sections[sn0].flags & linkscript.lines[ln].yesflags == 0))
|
|
103 continue;
|
|
104 if (inputfiles[fn0] -> sections[sn0].processed == 0)
|
|
105 {
|
|
106 sname = inputfiles[fn0] -> sections[sn0].name;
|
|
107 for (fn = 0; fn < ninputfiles; fn++)
|
|
108 {
|
|
109 for (sn = 0; sn < inputfiles[fn] -> nsections; sn++)
|
|
110 {
|
|
111 if (!strcmp(sname, inputfiles[fn] -> sections[sn].name))
|
|
112 {
|
|
113 // we have a match
|
|
114 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
|
|
115 sectlist[nsects].ptr = &(inputfiles[fn] -> sections[sn]);
|
|
116
|
|
117 inputfiles[fn] -> sections[sn].processed = 1;
|
|
118 if (!f && linkscript.lines[ln].loadat >= 0)
|
|
119 {
|
|
120 f = 1;
|
|
121 sectlist[nsects].forceaddr = 1;
|
|
122 laddr = linkscript.lines[ln].loadat;
|
|
123 }
|
|
124 else
|
|
125 {
|
|
126 sectlist[nsects].forceaddr = 0;
|
|
127 }
|
|
128 inputfiles[fn] -> sections[sn].loadaddress = laddr;
|
|
129 nsects++;
|
|
130 }
|
|
131 }
|
|
132 }
|
|
133 }
|
|
134 }
|
|
135 }
|
|
136 }
|
|
137 }
|
|
138
|
|
139 // theoretically, all the base addresses are set now
|
|
140 }
|
302
|
141
|
|
142 // resolve all incomplete references now
|
|
143 // anything that is unresolvable at this stage will throw an error
|
|
144 // because we know the load address of every section now
|
|
145 lw_expr_stack_t *resolve_sym(char *sym, int symtype, void *state)
|
|
146 {
|
|
147 section_t *sect = state;
|
|
148 lw_expr_term_t *term;
|
|
149 int val = 0, i, fn;
|
|
150 lw_expr_stack_t *s;
|
|
151 symtab_t *se;
|
|
152
|
|
153 if (symtype == 1)
|
|
154 {
|
|
155 // local symbol
|
|
156 if (!sym)
|
|
157 {
|
|
158 val = sect -> loadaddress;
|
|
159 goto out;
|
|
160 }
|
|
161
|
|
162 // start with this section
|
|
163 for (se = sect -> localsyms; se; se = se -> next)
|
|
164 {
|
|
165 if (!strcmp(se -> sym, sym))
|
|
166 {
|
|
167 val = se -> offset + sect -> loadaddress;
|
|
168 goto out;
|
|
169 }
|
|
170 }
|
|
171 // not in this section - check all sections in this file
|
|
172 for (i = 0; i < sect -> file -> nsections; i++)
|
|
173 {
|
|
174 for (se = sect -> file -> sections[i].localsyms; se; se = se -> next)
|
|
175 {
|
|
176 if (!strcmp(se -> sym, sym))
|
|
177 {
|
|
178 val = se -> offset + sect -> file -> sections[i].loadaddress;
|
|
179 goto out;
|
|
180 }
|
|
181 }
|
|
182 }
|
|
183 // not found
|
|
184 fprintf(stderr, "Local symbol %s not found in %s:%s\n", sym, sect -> file -> filename, sect -> name);
|
|
185 exit(1);
|
|
186 }
|
|
187 else
|
|
188 {
|
|
189 // external symbol
|
|
190 // read all files in order until found (or not found)
|
|
191 for (fn = 0; fn < ninputfiles; fn++)
|
|
192 {
|
|
193 for (i = 0; i < inputfiles[fn] -> nsections; i++)
|
|
194 {
|
|
195 for (se = inputfiles[fn] -> sections[i].exportedsyms; se; se = se -> next)
|
|
196 {
|
|
197 if (!strcmp(sym, se -> sym))
|
|
198 {
|
|
199 val = se -> offset + inputfiles[fn] -> sections[i].loadaddress;
|
|
200 goto out;
|
|
201 }
|
|
202 }
|
|
203 }
|
|
204 }
|
|
205 fprintf(stderr, "External symbol %s not found in %s:%s\n", sym, sect -> file -> filename, sect -> name);
|
|
206 exit(1);
|
|
207 }
|
|
208 fprintf(stderr, "Shouldn't ever get here!!!\n");
|
|
209 exit(88);
|
|
210 out:
|
|
211 s = lw_expr_stack_create();
|
|
212 term = lw_expr_term_create_int(val & 0xffff);
|
|
213 lw_expr_stack_push(s, term);
|
|
214 lw_expr_term_free(term);
|
|
215 return s;
|
|
216 }
|
|
217
|
|
218 void resolve_references(void)
|
|
219 {
|
|
220 int sn;
|
|
221 reloc_t *rl;
|
|
222 int rval;
|
303
|
223
|
|
224 // resolve entry point if required
|
|
225 // this must resolve to an *exported* symbol and will resolve to the
|
|
226 // first instance of that symbol
|
|
227 if (linkscript.execsym)
|
|
228 {
|
|
229 lw_expr_stack_t *s;
|
|
230
|
|
231 s = resolve_sym(linkscript.execsym, 0, NULL);
|
|
232 linkscript.execaddr = lw_expr_get_value(s);
|
|
233 lw_expr_stack_free(s);
|
|
234 }
|
302
|
235
|
306
|
236 for (sn = 0; sn < nsects; sn++)
|
302
|
237 {
|
|
238 for (rl = sectlist[sn].ptr -> incompletes; rl; rl = rl -> next)
|
|
239 {
|
|
240 // do a "simplify" on the expression
|
|
241 rval = lw_expr_reval(rl -> expr, resolve_sym, sectlist[sn].ptr);
|
|
242
|
|
243 // is it constant? error out if not
|
|
244 if (rval != 0 || !lw_expr_is_constant(rl -> expr))
|
|
245 {
|
|
246 fprintf(stderr, "Incomplete reference at %s:%s:%02X\n", sectlist[sn].ptr -> file -> filename, sectlist[sn].ptr -> name, rl -> offset);
|
|
247 exit(1);
|
|
248 }
|
|
249
|
|
250 // put the value into the relocation address
|
|
251 rval = lw_expr_get_value(rl -> expr);
|
|
252 sectlist[sn].ptr -> code[rl -> offset] = (rval >> 8) & 0xff;
|
|
253 sectlist[sn].ptr -> code[rl -> offset + 1] = rval & 0xff;
|
|
254 }
|
|
255 }
|
|
256 }
|