299
|
1 /*
|
|
2 script.c
|
300
|
3 Copyright © 2009 William Astle
|
299
|
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 =
|
|
38 "section init load at 2000\n"
|
|
39 "section code\n"
|
|
40 "section *,!bss\n"
|
|
41 "section *,bss\n"
|
303
|
42 "entry 2000\n"
|
299
|
43 ;
|
|
44
|
|
45 // the built-in RAW target linker script
|
|
46 static char *raw_script =
|
|
47 "section init load at 0000\n"
|
|
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 {
|
|
63 char *script;
|
|
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);
|
|
116 }
|
|
117
|
|
118 // now parse the script file
|
|
119 while (*script)
|
|
120 {
|
|
121 char *ptr, *ptr2, *line;
|
|
122
|
|
123 for (ptr = script; *ptr && *ptr != '\n' && *ptr != '\r'; ptr++)
|
|
124 /* do nothing */ ;
|
|
125
|
|
126 line = lw_malloc(ptr - script + 1);
|
|
127 memcpy(line, script, ptr - script);
|
|
128 line[ptr - script] = '\0';
|
|
129
|
|
130 // skip line terms
|
|
131 for (script = ptr + 1; *script == '\n' || *script == '\r'; script++)
|
|
132 /* do nothing */ ;
|
|
133
|
|
134 // ignore leading whitespace
|
|
135 for (ptr = line; *ptr && isspace(*ptr); ptr++)
|
|
136 /* do nothing */ ;
|
|
137
|
|
138 // ignore blank lines
|
|
139 if (!*ptr)
|
|
140 continue;
|
|
141
|
|
142 for (ptr = line; *ptr && !isspace(*ptr); ptr++)
|
|
143 /* do nothing */ ;
|
|
144
|
|
145 // now ptr points to the char past the first word
|
|
146 // NUL it out
|
|
147 if (*ptr)
|
|
148 *ptr++ = '\0';
|
|
149
|
|
150 // skip spaces after the first word
|
|
151 for ( ; *ptr && isspace(*ptr); ptr++)
|
|
152 /* do nothing */ ;
|
|
153
|
|
154 if (!strcmp(line, "pad"))
|
|
155 {
|
|
156 // padding
|
|
157 // parse the hex number and stow it
|
|
158 linkscript.padsize = strtol(ptr, NULL, 16);
|
|
159 if (linkscript.padsize < 0)
|
|
160 linkscript.padsize = 0;
|
|
161 }
|
303
|
162 else if (!strcmp(line, "entry"))
|
|
163 {
|
|
164 int eaddr;
|
|
165
|
|
166 eaddr = strtol(ptr, &ptr2, 16);
|
|
167 if (*ptr2)
|
|
168 {
|
|
169 linkscript.execaddr = -1;
|
|
170 linkscript.execsym = lw_strdup(ptr);
|
|
171 }
|
|
172 else
|
|
173 {
|
|
174 linkscript.execaddr = eaddr;
|
|
175 linkscript.execsym = NULL;
|
|
176 }
|
|
177 }
|
299
|
178 else if (!strcmp(line, "section"))
|
|
179 {
|
|
180 // section
|
|
181 // parse out the section name and flags
|
|
182 for (ptr2 = ptr; *ptr2 && !isspace(*ptr2); ptr2++)
|
|
183 /* do nothing */ ;
|
|
184
|
|
185 if (*ptr2)
|
|
186 *ptr2++ = '\0';
|
|
187
|
|
188 while (*ptr2 && isspace(*ptr2))
|
|
189 ptr2++;
|
|
190
|
|
191 // ptr now points to the section name and flags and ptr2
|
|
192 // to the first non-space character following
|
|
193
|
|
194 // then look for "load <addr>" clause
|
|
195 if (*ptr2)
|
|
196 {
|
|
197 if (!strncmp(ptr2, "load", 4))
|
|
198 {
|
|
199 ptr2 += 4;
|
|
200 while (*ptr2 && isspace(*ptr2))
|
|
201 ptr2++;
|
|
202
|
|
203 }
|
|
204 else
|
|
205 {
|
|
206 fprintf(stderr, "%s: bad script\n", scriptfile);
|
|
207 exit(1);
|
|
208 }
|
|
209 }
|
|
210
|
|
211 // now ptr2 points to the load address if there is one
|
|
212 // or NUL if not
|
|
213 linkscript.lines = lw_realloc(linkscript.lines, sizeof(struct scriptline_s) * (linkscript.nlines + 1));
|
|
214
|
|
215 linkscript.lines[linkscript.nlines].noflags = 0;
|
|
216 linkscript.lines[linkscript.nlines].yesflags = 0;
|
|
217 if (*ptr2)
|
|
218 linkscript.lines[linkscript.nlines].loadat = strtol(ptr2, NULL, 16);
|
|
219 else
|
|
220 linkscript.lines[linkscript.nlines].loadat = -1;
|
|
221 for (ptr2 = ptr; *ptr2 && *ptr2 != ','; ptr2++)
|
|
222 /* do nothing */ ;
|
|
223 if (*ptr2)
|
|
224 {
|
|
225 *ptr2++ = '\0';
|
|
226 if (!strcmp(ptr2, "!bss"))
|
|
227 {
|
|
228 linkscript.lines[linkscript.nlines].noflags = SECTION_BSS;
|
|
229 }
|
|
230 else if (!strcmp(ptr2, "bss"))
|
|
231 {
|
|
232 linkscript.lines[linkscript.nlines].yesflags = SECTION_BSS;
|
|
233 }
|
|
234 else
|
|
235 {
|
|
236 fprintf(stderr, "%s: bad script\n", scriptfile);
|
|
237 exit(1);
|
|
238 }
|
|
239 }
|
301
|
240 if (ptr[0] == '*' && ptr[1] == '\0')
|
|
241 linkscript.lines[linkscript.nlines].sectname = NULL;
|
|
242 else
|
|
243 linkscript.lines[linkscript.nlines].sectname = lw_strdup(ptr);
|
299
|
244 linkscript.nlines++;
|
|
245 }
|
|
246 else
|
|
247 {
|
|
248 fprintf(stderr, "%s: bad script\n", scriptfile);
|
|
249 exit(1);
|
|
250 }
|
|
251 lw_free(line);
|
|
252 }
|
|
253
|
|
254 if (scriptfile)
|
|
255 lw_free(script);
|
|
256 }
|