annotate src/link.c @ 313:c275e7e328d4

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