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
|
|
35 struct section_list
|
|
36 {
|
|
37 section_t *ptr; // ptr to section structure
|
|
38 int forceaddr; // was this force to an address by the link script?
|
|
39 };
|
|
40
|
|
41 static struct section_list *sectlist = NULL;
|
|
42 static int nsects = 0;
|
|
43
|
|
44 // work out section load order and resolve base addresses for each section
|
|
45 // make a list of sections to load in order
|
|
46 void resolve_sections(void)
|
|
47 {
|
|
48 int laddr = 0;
|
|
49 int ln, sn, fn;
|
|
50
|
|
51 for (ln = 0; ln < linkscript.nlines; ln++)
|
|
52 {
|
|
53 if (linkscript.lines[ln].sectname)
|
|
54 {
|
|
55 int f = 0;
|
|
56 // named section
|
|
57 // look for all instances of a section by the specified name
|
|
58 // and resolve base addresses and add to the list
|
|
59 for (fn = 0; fn < ninputfiles; fn++)
|
|
60 {
|
|
61 for (sn = 0; sn < inputfiles[fn] -> nsections; sn++)
|
|
62 {
|
|
63 if (!strcmp(linkscript.lines[ln].sectname, inputfiles[fn] -> sections[sn].name))
|
|
64 {
|
|
65 // we have a match
|
|
66 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
|
|
67 sectlist[nsects].ptr = &(inputfiles[fn] -> sections[sn]);
|
|
68
|
|
69 inputfiles[fn] -> sections[sn].processed = 1;
|
|
70 if (!f && linkscript.lines[ln].loadat >= 0)
|
|
71 {
|
|
72 f = 1;
|
|
73 sectlist[nsects].forceaddr = 1;
|
|
74 laddr = linkscript.lines[ln].loadat;
|
|
75 }
|
|
76 else
|
|
77 {
|
|
78 sectlist[nsects].forceaddr = 0;
|
|
79 }
|
|
80 inputfiles[fn] -> sections[sn].loadaddress = laddr;
|
|
81 nsects++;
|
|
82 }
|
|
83 }
|
|
84 }
|
|
85 }
|
|
86 else
|
|
87 {
|
|
88 // wildcard section
|
|
89 // look for all sections not yet processed that match flags
|
|
90
|
|
91 int f = 0;
|
|
92 int fn0, sn0;
|
|
93 char *sname;
|
|
94
|
|
95 // named section
|
|
96 // look for all instances of a section by the specified name
|
|
97 // and resolve base addresses and add to the list
|
|
98 for (fn0 = 0; fn0 < ninputfiles; fn0++)
|
|
99 {
|
|
100 for (sn0 = 0; sn0 < inputfiles[fn0] -> nsections; sn0++)
|
|
101 {
|
|
102 // ignore if the "no flags" bit says to
|
|
103 if (linkscript.lines[ln].noflags && (inputfiles[fn0] -> sections[sn0].flags & linkscript.lines[ln].noflags))
|
|
104 continue;
|
|
105 // ignore unless the yes flags tell us not to
|
|
106 if (linkscript.lines[ln].yesflags && (inputfiles[fn0] -> sections[sn0].flags & linkscript.lines[ln].yesflags == 0))
|
|
107 continue;
|
|
108 if (inputfiles[fn0] -> sections[sn0].processed == 0)
|
|
109 {
|
|
110 sname = inputfiles[fn0] -> sections[sn0].name;
|
|
111 for (fn = 0; fn < ninputfiles; fn++)
|
|
112 {
|
|
113 for (sn = 0; sn < inputfiles[fn] -> nsections; sn++)
|
|
114 {
|
|
115 if (!strcmp(sname, inputfiles[fn] -> sections[sn].name))
|
|
116 {
|
|
117 // we have a match
|
|
118 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
|
|
119 sectlist[nsects].ptr = &(inputfiles[fn] -> sections[sn]);
|
|
120
|
|
121 inputfiles[fn] -> sections[sn].processed = 1;
|
|
122 if (!f && linkscript.lines[ln].loadat >= 0)
|
|
123 {
|
|
124 f = 1;
|
|
125 sectlist[nsects].forceaddr = 1;
|
|
126 laddr = linkscript.lines[ln].loadat;
|
|
127 }
|
|
128 else
|
|
129 {
|
|
130 sectlist[nsects].forceaddr = 0;
|
|
131 }
|
|
132 inputfiles[fn] -> sections[sn].loadaddress = laddr;
|
|
133 nsects++;
|
|
134 }
|
|
135 }
|
|
136 }
|
|
137 }
|
|
138 }
|
|
139 }
|
|
140 }
|
|
141 }
|
|
142
|
|
143 // theoretically, all the base addresses are set now
|
|
144 }
|
302
|
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 }
|