Mercurial > hg-old > index.cgi
annotate lwlink/script.c @ 180:6ebb93b409ba
Added library paths and --section-base
author | lost |
---|---|
date | Thu, 05 Mar 2009 02:23:25 +0000 |
parents | 106c2fe3c9d9 |
children | 857cb407229e |
rev | line source |
---|---|
117 | 1 /* |
2 script.c | |
118 | 3 Copyright © 2009 William Astle |
117 | 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 Read and parse linker scripts | |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include "config.h" | |
26 #endif | |
27 | |
28 #include <errno.h> | |
29 #include <stdio.h> | |
30 #include <stdlib.h> | |
31 #include <string.h> | |
32 | |
33 #include "lwlink.h" | |
34 #include "util.h" | |
35 | |
36 // the built-in DECB target linker script | |
37 static char *decb_script = | |
124 | 38 "section init load 2000\n" |
117 | 39 "section code\n" |
40 "section *,!bss\n" | |
41 "section *,bss\n" | |
121 | 42 "entry 2000\n" |
117 | 43 ; |
44 | |
45 // the built-in RAW target linker script | |
46 static char *raw_script = | |
124 | 47 "section init load 0000\n" |
117 | 48 "section code\n" |
49 "section *,!bss\n" | |
50 "section *,bss\n" | |
51 ; | |
52 | |
53 // the "simple" script | |
54 static char *simple_script = | |
55 "section *,!bss\n" | |
56 "section *,bss\n" | |
57 ; | |
58 | |
59 linkscript_t linkscript = { 0, NULL, -1 }; | |
60 | |
61 void setup_script() | |
62 { | |
125
f9bfc2986023
Actually allow script file to be specified and fix segfault on parsing script file
lost
parents:
124
diff
changeset
|
63 char *script, *oscript; |
117 | 64 long size; |
65 | |
66 // read the file if needed | |
67 if (scriptfile) | |
68 { | |
69 FILE *f; | |
70 long bread; | |
71 | |
72 f = fopen(scriptfile, "rb"); | |
73 if (!f) | |
74 { | |
75 fprintf(stderr, "Can't open file %s:", scriptfile); | |
76 perror(""); | |
77 exit(1); | |
78 } | |
79 fseek(f, 0, SEEK_END); | |
80 size = ftell(f); | |
81 rewind(f); | |
82 | |
83 script = lw_malloc(size + 2); | |
84 | |
85 bread = fread(script, 1, size, f); | |
86 if (bread < size) | |
87 { | |
88 fprintf(stderr, "Short read on file %s (%ld/%ld):", scriptfile, bread, size); | |
89 perror(""); | |
90 exit(1); | |
91 } | |
92 fclose(f); | |
93 | |
94 script[size] = '\n'; | |
95 script[size + 1] = '\0'; | |
96 } | |
97 else | |
98 { | |
99 // fetch defaults based on output mode | |
100 switch (outformat) | |
101 { | |
102 case OUTPUT_RAW: | |
103 script = raw_script; | |
104 break; | |
105 | |
106 case OUTPUT_DECB: | |
107 script = decb_script; | |
108 break; | |
109 | |
110 default: | |
111 script = simple_script; | |
112 break; | |
113 } | |
114 | |
115 size = strlen(script); | |
180 | 116 if (nscriptls) |
117 { | |
118 char *rscript; | |
119 int i; | |
120 // prepend the "extra" script lines | |
121 for (i = 0; i < nscriptls; i++) | |
122 size += strlen(scriptls[i]) + 1; | |
123 | |
124 rscript = lw_malloc(size + 1); | |
125 oscript = rscript; | |
126 for (i = 0; i < nscriptls; i++) | |
127 { | |
128 oscript += sprintf(oscript, "%s\n", scriptls[i]); | |
129 } | |
130 strcpy(oscript, script); | |
131 script = rscript; | |
132 } | |
117 | 133 } |
134 | |
125
f9bfc2986023
Actually allow script file to be specified and fix segfault on parsing script file
lost
parents:
124
diff
changeset
|
135 oscript = script; |
117 | 136 // now parse the script file |
137 while (*script) | |
138 { | |
139 char *ptr, *ptr2, *line; | |
140 | |
141 for (ptr = script; *ptr && *ptr != '\n' && *ptr != '\r'; ptr++) | |
142 /* do nothing */ ; | |
143 | |
144 line = lw_malloc(ptr - script + 1); | |
145 memcpy(line, script, ptr - script); | |
146 line[ptr - script] = '\0'; | |
147 | |
148 // skip line terms | |
149 for (script = ptr + 1; *script == '\n' || *script == '\r'; script++) | |
150 /* do nothing */ ; | |
151 | |
152 // ignore leading whitespace | |
153 for (ptr = line; *ptr && isspace(*ptr); ptr++) | |
154 /* do nothing */ ; | |
155 | |
156 // ignore blank lines | |
157 if (!*ptr) | |
158 continue; | |
159 | |
160 for (ptr = line; *ptr && !isspace(*ptr); ptr++) | |
161 /* do nothing */ ; | |
162 | |
163 // now ptr points to the char past the first word | |
164 // NUL it out | |
165 if (*ptr) | |
166 *ptr++ = '\0'; | |
167 | |
168 // skip spaces after the first word | |
169 for ( ; *ptr && isspace(*ptr); ptr++) | |
170 /* do nothing */ ; | |
171 | |
172 if (!strcmp(line, "pad")) | |
173 { | |
174 // padding | |
175 // parse the hex number and stow it | |
176 linkscript.padsize = strtol(ptr, NULL, 16); | |
177 if (linkscript.padsize < 0) | |
178 linkscript.padsize = 0; | |
179 } | |
121 | 180 else if (!strcmp(line, "entry")) |
181 { | |
182 int eaddr; | |
183 | |
184 eaddr = strtol(ptr, &ptr2, 16); | |
185 if (*ptr2) | |
186 { | |
187 linkscript.execaddr = -1; | |
188 linkscript.execsym = lw_strdup(ptr); | |
189 } | |
190 else | |
191 { | |
192 linkscript.execaddr = eaddr; | |
193 linkscript.execsym = NULL; | |
194 } | |
195 } | |
117 | 196 else if (!strcmp(line, "section")) |
197 { | |
198 // section | |
199 // parse out the section name and flags | |
200 for (ptr2 = ptr; *ptr2 && !isspace(*ptr2); ptr2++) | |
201 /* do nothing */ ; | |
202 | |
203 if (*ptr2) | |
204 *ptr2++ = '\0'; | |
205 | |
206 while (*ptr2 && isspace(*ptr2)) | |
207 ptr2++; | |
208 | |
209 // ptr now points to the section name and flags and ptr2 | |
210 // to the first non-space character following | |
211 | |
212 // then look for "load <addr>" clause | |
213 if (*ptr2) | |
214 { | |
215 if (!strncmp(ptr2, "load", 4)) | |
216 { | |
217 ptr2 += 4; | |
218 while (*ptr2 && isspace(*ptr2)) | |
219 ptr2++; | |
220 | |
221 } | |
222 else | |
223 { | |
224 fprintf(stderr, "%s: bad script\n", scriptfile); | |
225 exit(1); | |
226 } | |
227 } | |
228 | |
229 // now ptr2 points to the load address if there is one | |
230 // or NUL if not | |
231 linkscript.lines = lw_realloc(linkscript.lines, sizeof(struct scriptline_s) * (linkscript.nlines + 1)); | |
232 | |
233 linkscript.lines[linkscript.nlines].noflags = 0; | |
234 linkscript.lines[linkscript.nlines].yesflags = 0; | |
235 if (*ptr2) | |
236 linkscript.lines[linkscript.nlines].loadat = strtol(ptr2, NULL, 16); | |
237 else | |
238 linkscript.lines[linkscript.nlines].loadat = -1; | |
239 for (ptr2 = ptr; *ptr2 && *ptr2 != ','; ptr2++) | |
240 /* do nothing */ ; | |
241 if (*ptr2) | |
242 { | |
243 *ptr2++ = '\0'; | |
244 if (!strcmp(ptr2, "!bss")) | |
245 { | |
246 linkscript.lines[linkscript.nlines].noflags = SECTION_BSS; | |
247 } | |
248 else if (!strcmp(ptr2, "bss")) | |
249 { | |
250 linkscript.lines[linkscript.nlines].yesflags = SECTION_BSS; | |
251 } | |
252 else | |
253 { | |
254 fprintf(stderr, "%s: bad script\n", scriptfile); | |
255 exit(1); | |
256 } | |
257 } | |
119 | 258 if (ptr[0] == '*' && ptr[1] == '\0') |
259 linkscript.lines[linkscript.nlines].sectname = NULL; | |
260 else | |
261 linkscript.lines[linkscript.nlines].sectname = lw_strdup(ptr); | |
117 | 262 linkscript.nlines++; |
263 } | |
264 else | |
265 { | |
266 fprintf(stderr, "%s: bad script\n", scriptfile); | |
267 exit(1); | |
268 } | |
269 lw_free(line); | |
270 } | |
271 | |
180 | 272 if (scriptfile || nscriptls) |
125
f9bfc2986023
Actually allow script file to be specified and fix segfault on parsing script file
lost
parents:
124
diff
changeset
|
273 lw_free(oscript); |
117 | 274 } |