annotate lwlink/link.c @ 171:d610b8aef91b

Added LWAR skeleton
author lost
date Sun, 01 Mar 2009 19:37:03 +0000
parents 106c2fe3c9d9
children 0395e6fd67e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
119
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
1 /*
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
2 link.c
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
3 Copyright © 2009 William Astle
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
4
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
5 This file is part of LWLINK.
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
6
8bc00221ae89 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
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
10 version.
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
11
8bc00221ae89 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
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
15 more details.
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
16
8bc00221ae89 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
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
19
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
20
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
21 Resolve section and symbol addresses; handle incomplete references
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
22 */
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
23
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
25 #include "config.h"
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
26 #endif
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
27
120
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
28 #include <stdio.h>
119
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
29 #include <stdlib.h>
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
30
120
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
31 #include "expr.h"
119
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
32 #include "lwlink.h"
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
33 #include "util.h"
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
34
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
35 struct section_list *sectlist = NULL;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
36 int nsects = 0;
119
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
37
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
38 void check_section_name(char *name, int *base, fileinfo_t *fn)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
39 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
40 int sn;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
41 for (sn = 0; sn < fn -> nsections; sn++)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
42 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
43 if (!strcmp(name, fn -> sections[sn].name))
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
44 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
45 // we have a match
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
46 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
47 sectlist[nsects].ptr = &(fn -> sections[sn]);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
48
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
49 fn -> sections[sn].processed = 1;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
50 fn -> sections[sn].loadaddress = *base;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
51 *base += fn -> sections[sn].codesize;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
52 nsects++;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
53 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
54 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
55 for (sn = 0; sn < fn -> nsubs; fn++)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
56 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
57 check_section_name(name, base, fn -> subs[sn]);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
58 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
59 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
60
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
61 void add_matching_sections(char *name, int yesflags, int noflags, int *base);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
62 void check_section_flags(int yesflags, int noflags, int *base, fileinfo_t *fn)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
63 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
64 int sn;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
65 for (sn = 0; sn < fn -> nsections; sn++)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
66 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
67 // ignore if the noflags tell us to
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
68 if (noflags && (fn -> sections[sn].flags & noflags))
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
69 continue;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
70 // ignore unless the yesflags tell us not to
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
71 if (yesflags && (fn -> sections[sn].flags & yesflags == 0))
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
72 continue;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
73 // ignore it if already processed
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
74 if (fn -> sections[sn].processed)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
75 continue;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
76
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
77 // we have a match - now collect *all* sections of the same name!
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
78 add_matching_sections(fn -> sections[sn].name, 0, 0, base);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
79
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
80 // and then continue looking for sections
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
81 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
82 for (sn = 0; sn < fn -> nsubs; fn++)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
83 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
84 check_section_flags(yesflags, noflags, base, fn -> subs[sn]);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
85 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
86 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
87
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
88
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
89
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
90 void add_matching_sections(char *name, int yesflags, int noflags, int *base)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
91 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
92 int fn;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
93 if (name)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
94 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
95 // named section
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
96 // look for all instances of a section by the specified name
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
97 // and resolve base addresses and add to the list
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
98 for (fn = 0; fn < ninputfiles; fn++)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
99 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
100 check_section_name(name, base, inputfiles[fn]);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
101 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
102 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
103 else
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
104 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
105 // wildcard section
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
106 // named section
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
107 // look for all instances of a section by the specified name
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
108 // and resolve base addresses and add to the list
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
109 for (fn = 0; fn < ninputfiles; fn++)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
110 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
111 check_section_flags(yesflags, noflags, base, inputfiles[fn]);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
112 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
113 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
114 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
115
119
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
116 // work out section load order and resolve base addresses for each section
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
117 // make a list of sections to load in order
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
118 void resolve_sections(void)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
119 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
120 int laddr = 0;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
121 int ln, sn, fn;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
122
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
123 for (ln = 0; ln < linkscript.nlines; ln++)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
124 {
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
125 if (linkscript.lines[ln].loadat >= 0)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
126 laddr = linkscript.lines[ln].loadat;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
127 add_matching_sections(linkscript.lines[ln].sectname, linkscript.lines[ln].yesflags, linkscript.lines[ln].noflags, &laddr);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
128
119
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
129 if (linkscript.lines[ln].sectname)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
130 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
131 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
132 else
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
133 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
134 // wildcard section
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
135 // look for all sections not yet processed that match flags
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
136
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
137 int f = 0;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
138 int fn0, sn0;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
139 char *sname;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
140
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
141 // named section
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
142 // look for all instances of a section by the specified name
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
143 // and resolve base addresses and add to the list
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
144 for (fn0 = 0; fn0 < ninputfiles; fn0++)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
145 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
146 for (sn0 = 0; sn0 < inputfiles[fn0] -> nsections; sn0++)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
147 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
148 // ignore if the "no flags" bit says to
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
149 if (linkscript.lines[ln].noflags && (inputfiles[fn0] -> sections[sn0].flags & linkscript.lines[ln].noflags))
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
150 continue;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
151 // ignore unless the yes flags tell us not to
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
152 if (linkscript.lines[ln].yesflags && (inputfiles[fn0] -> sections[sn0].flags & linkscript.lines[ln].yesflags == 0))
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
153 continue;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
154 if (inputfiles[fn0] -> sections[sn0].processed == 0)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
155 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
156 sname = inputfiles[fn0] -> sections[sn0].name;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
157 for (fn = 0; fn < ninputfiles; fn++)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
158 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
159 for (sn = 0; sn < inputfiles[fn] -> nsections; sn++)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
160 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
161 if (!strcmp(sname, inputfiles[fn] -> sections[sn].name))
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
162 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
163 // we have a match
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
164 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
165 sectlist[nsects].ptr = &(inputfiles[fn] -> sections[sn]);
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
166
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
167 inputfiles[fn] -> sections[sn].processed = 1;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
168 if (!f && linkscript.lines[ln].loadat >= 0)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
169 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
170 f = 1;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
171 sectlist[nsects].forceaddr = 1;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
172 laddr = linkscript.lines[ln].loadat;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
173 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
174 else
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
175 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
176 sectlist[nsects].forceaddr = 0;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
177 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
178 inputfiles[fn] -> sections[sn].loadaddress = laddr;
131
5276565799bd Fixed load addresses of chained sections and subsections to actually increment
lost
parents: 130
diff changeset
179 laddr += inputfiles[fn] -> sections[sn].codesize;
119
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
180 nsects++;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
181 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
182 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
183 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
184 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
185 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
186 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
187 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
188 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
189
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
190 // theoretically, all the base addresses are set now
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
191 }
120
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
192
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
193 lw_expr_stack_t *find_external_sym_recurse(char *sym, fileinfo_t *fn)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
194 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
195 int sn;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
196 lw_expr_stack_t *r;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
197 lw_expr_term_t *term;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
198 symtab_t *se;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
199 int val;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
200
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
201 for (sn = 0; sn < fn -> nsections; sn++)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
202 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
203 for (se = fn -> sections[sn].exportedsyms; se; se = se -> next)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
204 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
205 if (!strcmp(sym, se -> sym))
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
206 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
207 val = se -> offset + fn -> sections[sn].loadaddress;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
208 r = lw_expr_stack_create();
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
209 term = lw_expr_term_create_int(val & 0xffff);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
210 lw_expr_stack_push(r, term);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
211 lw_expr_term_free(term);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
212 return r;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
213 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
214 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
215 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
216
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
217 for (sn = 0; sn < fn -> nsubs; sn++)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
218 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
219 r = find_external_sym_recurse(sym, fn -> subs[sn]);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
220 if (r)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
221 return r;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
222 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
223 return NULL;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
224 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
225
120
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
226 // resolve all incomplete references now
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
227 // anything that is unresolvable at this stage will throw an error
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
228 // because we know the load address of every section now
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
229 lw_expr_stack_t *resolve_sym(char *sym, int symtype, void *state)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
230 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
231 section_t *sect = state;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
232 lw_expr_term_t *term;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
233 int val = 0, i, fn;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
234 lw_expr_stack_t *s;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
235 symtab_t *se;
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
236 fileinfo_t *fp;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
237
120
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
238 if (symtype == 1)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
239 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
240 // local symbol
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
241 if (!sym)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
242 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
243 val = sect -> loadaddress;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
244 goto out;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
245 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
246
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
247 // start with this section
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
248 for (se = sect -> localsyms; se; se = se -> next)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
249 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
250 if (!strcmp(se -> sym, sym))
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
251 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
252 val = se -> offset + sect -> loadaddress;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
253 goto out;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
254 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
255 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
256 // not in this section - check all sections in this file
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
257 for (i = 0; i < sect -> file -> nsections; i++)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
258 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
259 for (se = sect -> file -> sections[i].localsyms; se; se = se -> next)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
260 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
261 if (!strcmp(se -> sym, sym))
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
262 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
263 val = se -> offset + sect -> file -> sections[i].loadaddress;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
264 goto out;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
265 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
266 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
267 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
268 // not found
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
269 fprintf(stderr, "Local symbol %s not found in %s:%s\n", sym, sect -> file -> filename, sect -> name);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
270 exit(1);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
271 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
272 else
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
273 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
274 // external symbol
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
275 // read all files in order until found (or not found)
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
276 for (fp = sect -> file; fp; fp = fp -> parent)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
277 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
278 s = find_external_sym_recurse(sym, fp);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
279 if (s)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
280 return s;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
281 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
282
120
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
283 for (fn = 0; fn < ninputfiles; fn++)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
284 {
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
285 s = find_external_sym_recurse(sym, inputfiles[fn]);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
286 if (s)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
287 return s;
120
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
288 }
130
8349048d67b3 Fixed crash when entry symbol is not exported
lost
parents: 126
diff changeset
289 if (sect)
8349048d67b3 Fixed crash when entry symbol is not exported
lost
parents: 126
diff changeset
290 {
8349048d67b3 Fixed crash when entry symbol is not exported
lost
parents: 126
diff changeset
291 fprintf(stderr, "External symbol %s not found in %s:%s\n", sym, sect -> file -> filename, sect -> name);
8349048d67b3 Fixed crash when entry symbol is not exported
lost
parents: 126
diff changeset
292 }
8349048d67b3 Fixed crash when entry symbol is not exported
lost
parents: 126
diff changeset
293 else
8349048d67b3 Fixed crash when entry symbol is not exported
lost
parents: 126
diff changeset
294 {
8349048d67b3 Fixed crash when entry symbol is not exported
lost
parents: 126
diff changeset
295 fprintf(stderr, "External symbol %s not found\n", sym);
8349048d67b3 Fixed crash when entry symbol is not exported
lost
parents: 126
diff changeset
296 }
120
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
297 exit(1);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
298 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
299 fprintf(stderr, "Shouldn't ever get here!!!\n");
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
300 exit(88);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
301 out:
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
302 s = lw_expr_stack_create();
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
303 term = lw_expr_term_create_int(val & 0xffff);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
304 lw_expr_stack_push(s, term);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
305 lw_expr_term_free(term);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
306 return s;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
307 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
308
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
309 void resolve_references(void)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
310 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
311 int sn;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
312 reloc_t *rl;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
313 int rval;
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
314
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
315 // resolve entry point if required
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
316 // this must resolve to an *exported* symbol and will resolve to the
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
317 // first instance of that symbol
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
318 if (linkscript.execsym)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
319 {
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
320 lw_expr_stack_t *s;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
321
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
322 s = resolve_sym(linkscript.execsym, 0, NULL);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
323 linkscript.execaddr = lw_expr_get_value(s);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
324 lw_expr_stack_free(s);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
325 }
120
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
326
124
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 121
diff changeset
327 for (sn = 0; sn < nsects; sn++)
120
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
328 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
329 for (rl = sectlist[sn].ptr -> incompletes; rl; rl = rl -> next)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
330 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
331 // do a "simplify" on the expression
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
332 rval = lw_expr_reval(rl -> expr, resolve_sym, sectlist[sn].ptr);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
333
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
334 // is it constant? error out if not
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
335 if (rval != 0 || !lw_expr_is_constant(rl -> expr))
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
336 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
337 fprintf(stderr, "Incomplete reference at %s:%s:%02X\n", sectlist[sn].ptr -> file -> filename, sectlist[sn].ptr -> name, rl -> offset);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
338 exit(1);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
339 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
340
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
341 // put the value into the relocation address
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
342 rval = lw_expr_get_value(rl -> expr);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
343 sectlist[sn].ptr -> code[rl -> offset] = (rval >> 8) & 0xff;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
344 sectlist[sn].ptr -> code[rl -> offset + 1] = rval & 0xff;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
345 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
346 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
347 }