comparison lwlink/trunk/src/link.c @ 120:36ce328df3c3

resolve incomplete references done
author lost
date Wed, 21 Jan 2009 06:14:08 +0000
parents 8bc00221ae89
children 7bc853cb2ca9
comparison
equal deleted inserted replaced
119:8bc00221ae89 120:36ce328df3c3
23 23
24 #ifdef HAVE_CONFIG_H 24 #ifdef HAVE_CONFIG_H
25 #include "config.h" 25 #include "config.h"
26 #endif 26 #endif
27 27
28 #include <stdio.h>
28 #include <stdlib.h> 29 #include <stdlib.h>
29 30
31 #include "expr.h"
30 #include "lwlink.h" 32 #include "lwlink.h"
31 #include "util.h" 33 #include "util.h"
32 34
33 struct section_list 35 struct section_list
34 { 36 {
138 } 140 }
139 } 141 }
140 142
141 // theoretically, all the base addresses are set now 143 // theoretically, all the base addresses are set now
142 } 144 }
145
146 // resolve all incomplete references now
147 // anything that is unresolvable at this stage will throw an error
148 // because we know the load address of every section now
149 lw_expr_stack_t *resolve_sym(char *sym, int symtype, void *state)
150 {
151 section_t *sect = state;
152 lw_expr_term_t *term;
153 int val = 0, i, fn;
154 lw_expr_stack_t *s;
155 symtab_t *se;
156
157 if (symtype == 1)
158 {
159 // local symbol
160 if (!sym)
161 {
162 val = sect -> loadaddress;
163 goto out;
164 }
165
166 // start with this section
167 for (se = sect -> localsyms; se; se = se -> next)
168 {
169 if (!strcmp(se -> sym, sym))
170 {
171 val = se -> offset + sect -> loadaddress;
172 goto out;
173 }
174 }
175 // not in this section - check all sections in this file
176 for (i = 0; i < sect -> file -> nsections; i++)
177 {
178 for (se = sect -> file -> sections[i].localsyms; se; se = se -> next)
179 {
180 if (!strcmp(se -> sym, sym))
181 {
182 val = se -> offset + sect -> file -> sections[i].loadaddress;
183 goto out;
184 }
185 }
186 }
187 // not found
188 fprintf(stderr, "Local symbol %s not found in %s:%s\n", sym, sect -> file -> filename, sect -> name);
189 exit(1);
190 }
191 else
192 {
193 // external symbol
194 // read all files in order until found (or not found)
195 for (fn = 0; fn < ninputfiles; fn++)
196 {
197 for (i = 0; i < inputfiles[fn] -> nsections; i++)
198 {
199 for (se = inputfiles[fn] -> sections[i].exportedsyms; se; se = se -> next)
200 {
201 if (!strcmp(sym, se -> sym))
202 {
203 val = se -> offset + inputfiles[fn] -> sections[i].loadaddress;
204 goto out;
205 }
206 }
207 }
208 }
209 fprintf(stderr, "External symbol %s not found in %s:%s\n", sym, sect -> file -> filename, sect -> name);
210 exit(1);
211 }
212 fprintf(stderr, "Shouldn't ever get here!!!\n");
213 exit(88);
214 out:
215 s = lw_expr_stack_create();
216 term = lw_expr_term_create_int(val & 0xffff);
217 lw_expr_stack_push(s, term);
218 lw_expr_term_free(term);
219 return s;
220 }
221
222 void resolve_references(void)
223 {
224 int sn;
225 reloc_t *rl;
226 int rval;
227
228 for (sn = 0; nsects; sn++)
229 {
230 for (rl = sectlist[sn].ptr -> incompletes; rl; rl = rl -> next)
231 {
232 // do a "simplify" on the expression
233 rval = lw_expr_reval(rl -> expr, resolve_sym, sectlist[sn].ptr);
234
235 // is it constant? error out if not
236 if (rval != 0 || !lw_expr_is_constant(rl -> expr))
237 {
238 fprintf(stderr, "Incomplete reference at %s:%s:%02X\n", sectlist[sn].ptr -> file -> filename, sectlist[sn].ptr -> name, rl -> offset);
239 exit(1);
240 }
241
242 // put the value into the relocation address
243 rval = lw_expr_get_value(rl -> expr);
244 sectlist[sn].ptr -> code[rl -> offset] = (rval >> 8) & 0xff;
245 sectlist[sn].ptr -> code[rl -> offset + 1] = rval & 0xff;
246 }
247 }
248 }