Mercurial > hg-old > index.cgi
annotate lwlink/script.c @ 211:51511cf1c9f8
Prepare for gnulib integration
author | lost |
---|---|
date | Fri, 24 Apr 2009 06:27:00 +0000 |
parents | 857cb407229e |
children | bae1e3ecdce1 |
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 | |
187 | 53 static char *lwex0_script = |
54 "section init load 0100\n" | |
55 "section .text\n" | |
56 "section .data\n" | |
57 "section *,!bss\n" | |
58 "section *,bss\n" | |
59 "entry _start\n" | |
60 "stacksize 0100\n" // default 256 byte stack | |
61 ; | |
62 | |
117 | 63 // the "simple" script |
64 static char *simple_script = | |
65 "section *,!bss\n" | |
66 "section *,bss\n" | |
67 ; | |
68 | |
69 linkscript_t linkscript = { 0, NULL, -1 }; | |
70 | |
71 void setup_script() | |
72 { | |
125
f9bfc2986023
Actually allow script file to be specified and fix segfault on parsing script file
lost
parents:
124
diff
changeset
|
73 char *script, *oscript; |
117 | 74 long size; |
75 | |
76 // read the file if needed | |
77 if (scriptfile) | |
78 { | |
79 FILE *f; | |
80 long bread; | |
81 | |
82 f = fopen(scriptfile, "rb"); | |
83 if (!f) | |
84 { | |
85 fprintf(stderr, "Can't open file %s:", scriptfile); | |
86 perror(""); | |
87 exit(1); | |
88 } | |
89 fseek(f, 0, SEEK_END); | |
90 size = ftell(f); | |
91 rewind(f); | |
92 | |
93 script = lw_malloc(size + 2); | |
94 | |
95 bread = fread(script, 1, size, f); | |
96 if (bread < size) | |
97 { | |
98 fprintf(stderr, "Short read on file %s (%ld/%ld):", scriptfile, bread, size); | |
99 perror(""); | |
100 exit(1); | |
101 } | |
102 fclose(f); | |
103 | |
104 script[size] = '\n'; | |
105 script[size + 1] = '\0'; | |
106 } | |
107 else | |
108 { | |
109 // fetch defaults based on output mode | |
110 switch (outformat) | |
111 { | |
112 case OUTPUT_RAW: | |
113 script = raw_script; | |
114 break; | |
115 | |
116 case OUTPUT_DECB: | |
117 script = decb_script; | |
118 break; | |
119 | |
187 | 120 case OUTPUT_LWEX0: |
121 script = lwex0_script; | |
122 break; | |
123 | |
117 | 124 default: |
125 script = simple_script; | |
126 break; | |
127 } | |
128 | |
129 size = strlen(script); | |
180 | 130 if (nscriptls) |
131 { | |
132 char *rscript; | |
133 int i; | |
134 // prepend the "extra" script lines | |
135 for (i = 0; i < nscriptls; i++) | |
136 size += strlen(scriptls[i]) + 1; | |
137 | |
138 rscript = lw_malloc(size + 1); | |
139 oscript = rscript; | |
140 for (i = 0; i < nscriptls; i++) | |
141 { | |
142 oscript += sprintf(oscript, "%s\n", scriptls[i]); | |
143 } | |
144 strcpy(oscript, script); | |
145 script = rscript; | |
146 } | |
117 | 147 } |
148 | |
187 | 149 if (outformat == OUTPUT_LWEX0) |
150 linkscript.stacksize = 0x100; | |
151 | |
125
f9bfc2986023
Actually allow script file to be specified and fix segfault on parsing script file
lost
parents:
124
diff
changeset
|
152 oscript = script; |
117 | 153 // now parse the script file |
154 while (*script) | |
155 { | |
156 char *ptr, *ptr2, *line; | |
157 | |
158 for (ptr = script; *ptr && *ptr != '\n' && *ptr != '\r'; ptr++) | |
159 /* do nothing */ ; | |
160 | |
161 line = lw_malloc(ptr - script + 1); | |
162 memcpy(line, script, ptr - script); | |
163 line[ptr - script] = '\0'; | |
164 | |
165 // skip line terms | |
166 for (script = ptr + 1; *script == '\n' || *script == '\r'; script++) | |
167 /* do nothing */ ; | |
168 | |
169 // ignore leading whitespace | |
170 for (ptr = line; *ptr && isspace(*ptr); ptr++) | |
171 /* do nothing */ ; | |
172 | |
173 // ignore blank lines | |
174 if (!*ptr) | |
175 continue; | |
176 | |
177 for (ptr = line; *ptr && !isspace(*ptr); ptr++) | |
178 /* do nothing */ ; | |
179 | |
180 // now ptr points to the char past the first word | |
181 // NUL it out | |
182 if (*ptr) | |
183 *ptr++ = '\0'; | |
184 | |
185 // skip spaces after the first word | |
186 for ( ; *ptr && isspace(*ptr); ptr++) | |
187 /* do nothing */ ; | |
188 | |
189 if (!strcmp(line, "pad")) | |
190 { | |
191 // padding | |
192 // parse the hex number and stow it | |
193 linkscript.padsize = strtol(ptr, NULL, 16); | |
194 if (linkscript.padsize < 0) | |
195 linkscript.padsize = 0; | |
196 } | |
187 | 197 else if (!strcmp(line, "stacksize")) |
198 { | |
199 // stack size for targets that support it | |
200 // parse the hex number and stow it | |
201 linkscript.padsize = strtol(ptr, NULL, 16); | |
202 if (linkscript.stacksize < 0) | |
203 linkscript.stacksize = 0x100; | |
204 } | |
121 | 205 else if (!strcmp(line, "entry")) |
206 { | |
207 int eaddr; | |
208 | |
209 eaddr = strtol(ptr, &ptr2, 16); | |
210 if (*ptr2) | |
211 { | |
212 linkscript.execaddr = -1; | |
213 linkscript.execsym = lw_strdup(ptr); | |
214 } | |
215 else | |
216 { | |
217 linkscript.execaddr = eaddr; | |
218 linkscript.execsym = NULL; | |
219 } | |
220 } | |
117 | 221 else if (!strcmp(line, "section")) |
222 { | |
223 // section | |
224 // parse out the section name and flags | |
225 for (ptr2 = ptr; *ptr2 && !isspace(*ptr2); ptr2++) | |
226 /* do nothing */ ; | |
227 | |
228 if (*ptr2) | |
229 *ptr2++ = '\0'; | |
230 | |
231 while (*ptr2 && isspace(*ptr2)) | |
232 ptr2++; | |
233 | |
234 // ptr now points to the section name and flags and ptr2 | |
235 // to the first non-space character following | |
236 | |
237 // then look for "load <addr>" clause | |
238 if (*ptr2) | |
239 { | |
240 if (!strncmp(ptr2, "load", 4)) | |
241 { | |
242 ptr2 += 4; | |
243 while (*ptr2 && isspace(*ptr2)) | |
244 ptr2++; | |
245 | |
246 } | |
247 else | |
248 { | |
249 fprintf(stderr, "%s: bad script\n", scriptfile); | |
250 exit(1); | |
251 } | |
252 } | |
253 | |
254 // now ptr2 points to the load address if there is one | |
255 // or NUL if not | |
256 linkscript.lines = lw_realloc(linkscript.lines, sizeof(struct scriptline_s) * (linkscript.nlines + 1)); | |
257 | |
258 linkscript.lines[linkscript.nlines].noflags = 0; | |
259 linkscript.lines[linkscript.nlines].yesflags = 0; | |
260 if (*ptr2) | |
261 linkscript.lines[linkscript.nlines].loadat = strtol(ptr2, NULL, 16); | |
262 else | |
263 linkscript.lines[linkscript.nlines].loadat = -1; | |
264 for (ptr2 = ptr; *ptr2 && *ptr2 != ','; ptr2++) | |
265 /* do nothing */ ; | |
266 if (*ptr2) | |
267 { | |
268 *ptr2++ = '\0'; | |
269 if (!strcmp(ptr2, "!bss")) | |
270 { | |
271 linkscript.lines[linkscript.nlines].noflags = SECTION_BSS; | |
272 } | |
273 else if (!strcmp(ptr2, "bss")) | |
274 { | |
275 linkscript.lines[linkscript.nlines].yesflags = SECTION_BSS; | |
276 } | |
277 else | |
278 { | |
279 fprintf(stderr, "%s: bad script\n", scriptfile); | |
280 exit(1); | |
281 } | |
282 } | |
119 | 283 if (ptr[0] == '*' && ptr[1] == '\0') |
284 linkscript.lines[linkscript.nlines].sectname = NULL; | |
285 else | |
286 linkscript.lines[linkscript.nlines].sectname = lw_strdup(ptr); | |
117 | 287 linkscript.nlines++; |
288 } | |
289 else | |
290 { | |
291 fprintf(stderr, "%s: bad script\n", scriptfile); | |
292 exit(1); | |
293 } | |
294 lw_free(line); | |
295 } | |
296 | |
180 | 297 if (scriptfile || nscriptls) |
125
f9bfc2986023
Actually allow script file to be specified and fix segfault on parsing script file
lost
parents:
124
diff
changeset
|
298 lw_free(oscript); |
117 | 299 } |