annotate lwlink/link.c @ 146:6c0a30278982

Made development version of LWASM be 2.1, not 3.0, because the next release will be an incremental feature release
author lost
date Thu, 29 Jan 2009 06:14:04 +0000
parents 106c2fe3c9d9
children d610b8aef91b
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
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
38 // work out section load order and resolve base addresses for each section
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
39 // make a list of sections to load in order
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
40 void resolve_sections(void)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
41 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
42 int laddr = 0;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
43 int ln, sn, fn;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
44
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
45 for (ln = 0; ln < linkscript.nlines; ln++)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
46 {
126
269ef87192ad Fixed stupid logic problem reading input files
lost
parents: 124
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);
119
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
48 if (linkscript.lines[ln].sectname)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
49 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
50 int f = 0;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
51 // named section
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
52 // look for all instances of a section by the specified name
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
53 // and resolve base addresses and add to the list
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
54 for (fn = 0; fn < ninputfiles; fn++)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
55 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
56 for (sn = 0; sn < inputfiles[fn] -> nsections; sn++)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
57 {
126
269ef87192ad Fixed stupid logic problem reading input files
lost
parents: 124
diff changeset
58 // printf(" Considering %s:%s\n", inputfiles[fn]->filename, inputfiles[fn]->sections[sn].name);
119
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
59 if (!strcmp(linkscript.lines[ln].sectname, inputfiles[fn] -> sections[sn].name))
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
60 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
61 // we have a match
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
62 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
63 sectlist[nsects].ptr = &(inputfiles[fn] -> sections[sn]);
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
64
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
65 inputfiles[fn] -> sections[sn].processed = 1;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
66 if (!f && linkscript.lines[ln].loadat >= 0)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
67 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
68 f = 1;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
69 sectlist[nsects].forceaddr = 1;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
70 laddr = linkscript.lines[ln].loadat;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
71 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
72 else
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
73 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
74 sectlist[nsects].forceaddr = 0;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
75 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
76 inputfiles[fn] -> sections[sn].loadaddress = laddr;
131
5276565799bd Fixed load addresses of chained sections and subsections to actually increment
lost
parents: 130
diff changeset
77 laddr += inputfiles[fn] -> sections[sn].codesize;
119
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
78 nsects++;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
79 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
80 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
81 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
82 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
83 else
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
84 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
85 // wildcard section
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
86 // look for all sections not yet processed that match flags
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
87
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
88 int f = 0;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
89 int fn0, sn0;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
90 char *sname;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
91
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
92 // named section
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
93 // look for all instances of a section by the specified name
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
94 // and resolve base addresses and add to the list
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
95 for (fn0 = 0; fn0 < ninputfiles; fn0++)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
96 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
97 for (sn0 = 0; sn0 < inputfiles[fn0] -> nsections; sn0++)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
98 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
99 // ignore if the "no flags" bit says to
8bc00221ae89 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))
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
101 continue;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
102 // ignore unless the yes flags tell us not to
8bc00221ae89 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))
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
104 continue;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
105 if (inputfiles[fn0] -> sections[sn0].processed == 0)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
106 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
107 sname = inputfiles[fn0] -> sections[sn0].name;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
108 for (fn = 0; fn < ninputfiles; fn++)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
109 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
110 for (sn = 0; sn < inputfiles[fn] -> nsections; sn++)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
111 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
112 if (!strcmp(sname, inputfiles[fn] -> sections[sn].name))
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
113 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
114 // we have a match
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
115 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
116 sectlist[nsects].ptr = &(inputfiles[fn] -> sections[sn]);
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
117
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
118 inputfiles[fn] -> sections[sn].processed = 1;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
119 if (!f && linkscript.lines[ln].loadat >= 0)
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
120 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
121 f = 1;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
122 sectlist[nsects].forceaddr = 1;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
123 laddr = linkscript.lines[ln].loadat;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
124 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
125 else
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
126 {
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
127 sectlist[nsects].forceaddr = 0;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
128 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
129 inputfiles[fn] -> sections[sn].loadaddress = laddr;
131
5276565799bd Fixed load addresses of chained sections and subsections to actually increment
lost
parents: 130
diff changeset
130 laddr += inputfiles[fn] -> sections[sn].codesize;
119
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
131 nsects++;
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
132 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
133 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
134 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
135 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
136 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
137 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
138 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
139 }
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
140
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
141 // theoretically, all the base addresses are set now
8bc00221ae89 section order and address resolution done
lost
parents:
diff changeset
142 }
120
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
143
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
144 // resolve all incomplete references now
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
145 // anything that is unresolvable at this stage will throw an error
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
146 // because we know the load address of every section now
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
147 lw_expr_stack_t *resolve_sym(char *sym, int symtype, void *state)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
148 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
149 section_t *sect = state;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
150 lw_expr_term_t *term;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
151 int val = 0, i, fn;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
152 lw_expr_stack_t *s;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
153 symtab_t *se;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
154
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
155 if (symtype == 1)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
156 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
157 // local symbol
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
158 if (!sym)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
159 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
160 val = sect -> loadaddress;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
161 goto out;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
162 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
163
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
164 // start with this section
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
165 for (se = sect -> localsyms; se; se = se -> next)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
166 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
167 if (!strcmp(se -> sym, sym))
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
168 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
169 val = se -> offset + sect -> loadaddress;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
170 goto out;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
171 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
172 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
173 // not in this section - check all sections in this file
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
174 for (i = 0; i < sect -> file -> nsections; i++)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
175 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
176 for (se = sect -> file -> sections[i].localsyms; se; se = se -> next)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
177 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
178 if (!strcmp(se -> sym, sym))
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
179 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
180 val = se -> offset + sect -> file -> sections[i].loadaddress;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
181 goto out;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
182 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
183 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
184 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
185 // not found
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
186 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
187 exit(1);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
188 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
189 else
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
190 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
191 // external symbol
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
192 // read all files in order until found (or not found)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
193 for (fn = 0; fn < ninputfiles; fn++)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
194 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
195 for (i = 0; i < inputfiles[fn] -> nsections; i++)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
196 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
197 for (se = inputfiles[fn] -> sections[i].exportedsyms; se; se = se -> next)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
198 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
199 if (!strcmp(sym, se -> sym))
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
200 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
201 val = se -> offset + inputfiles[fn] -> sections[i].loadaddress;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
202 goto out;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
203 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
204 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
205 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
206 }
130
8349048d67b3 Fixed crash when entry symbol is not exported
lost
parents: 126
diff changeset
207 if (sect)
8349048d67b3 Fixed crash when entry symbol is not exported
lost
parents: 126
diff changeset
208 {
8349048d67b3 Fixed crash when entry symbol is not exported
lost
parents: 126
diff changeset
209 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
210 }
8349048d67b3 Fixed crash when entry symbol is not exported
lost
parents: 126
diff changeset
211 else
8349048d67b3 Fixed crash when entry symbol is not exported
lost
parents: 126
diff changeset
212 {
8349048d67b3 Fixed crash when entry symbol is not exported
lost
parents: 126
diff changeset
213 fprintf(stderr, "External symbol %s not found\n", sym);
8349048d67b3 Fixed crash when entry symbol is not exported
lost
parents: 126
diff changeset
214 }
120
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
215 exit(1);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
216 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
217 fprintf(stderr, "Shouldn't ever get here!!!\n");
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
218 exit(88);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
219 out:
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
220 s = lw_expr_stack_create();
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
221 term = lw_expr_term_create_int(val & 0xffff);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
222 lw_expr_stack_push(s, term);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
223 lw_expr_term_free(term);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
224 return s;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
225 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
226
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
227 void resolve_references(void)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
228 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
229 int sn;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
230 reloc_t *rl;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
231 int rval;
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
232
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
233 // resolve entry point if required
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
234 // 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
235 // first instance of that symbol
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
236 if (linkscript.execsym)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
237 {
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
238 lw_expr_stack_t *s;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
239
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
240 s = resolve_sym(linkscript.execsym, 0, NULL);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
241 linkscript.execaddr = lw_expr_get_value(s);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
242 lw_expr_stack_free(s);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 120
diff changeset
243 }
120
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
244
124
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 121
diff changeset
245 for (sn = 0; sn < nsects; sn++)
120
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
246 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
247 for (rl = sectlist[sn].ptr -> incompletes; rl; rl = rl -> next)
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
248 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
249 // do a "simplify" on the expression
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
250 rval = lw_expr_reval(rl -> expr, resolve_sym, sectlist[sn].ptr);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
251
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
252 // is it constant? error out if not
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
253 if (rval != 0 || !lw_expr_is_constant(rl -> expr))
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
254 {
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
255 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
256 exit(1);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
257 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
258
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
259 // put the value into the relocation address
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
260 rval = lw_expr_get_value(rl -> expr);
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
261 sectlist[sn].ptr -> code[rl -> offset] = (rval >> 8) & 0xff;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
262 sectlist[sn].ptr -> code[rl -> offset + 1] = rval & 0xff;
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
263 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
264 }
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
265 }