annotate src/script.c @ 305:10cc87f4b16c

Added basic raw output module
author lost
date Thu, 22 Jan 2009 02:14:52 +0000
parents 13272197d278
children be1abc4ac114
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
299
460d96987670 parse linking scripts
lost
parents:
diff changeset
1 /*
460d96987670 parse linking scripts
lost
parents:
diff changeset
2 script.c
300
48945dac8178 Fixed copyright year
lost
parents: 299
diff changeset
3 Copyright © 2009 William Astle
299
460d96987670 parse linking scripts
lost
parents:
diff changeset
4
460d96987670 parse linking scripts
lost
parents:
diff changeset
5 This file is part of LWLINK.
460d96987670 parse linking scripts
lost
parents:
diff changeset
6
460d96987670 parse linking scripts
lost
parents:
diff changeset
7 LWLINK is free software: you can redistribute it and/or modify it under the
460d96987670 parse linking scripts
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
460d96987670 parse linking scripts
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
460d96987670 parse linking scripts
lost
parents:
diff changeset
10 version.
460d96987670 parse linking scripts
lost
parents:
diff changeset
11
460d96987670 parse linking scripts
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
460d96987670 parse linking scripts
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
460d96987670 parse linking scripts
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
460d96987670 parse linking scripts
lost
parents:
diff changeset
15 more details.
460d96987670 parse linking scripts
lost
parents:
diff changeset
16
460d96987670 parse linking scripts
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
460d96987670 parse linking scripts
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
460d96987670 parse linking scripts
lost
parents:
diff changeset
19
460d96987670 parse linking scripts
lost
parents:
diff changeset
20
460d96987670 parse linking scripts
lost
parents:
diff changeset
21 Read and parse linker scripts
460d96987670 parse linking scripts
lost
parents:
diff changeset
22 */
460d96987670 parse linking scripts
lost
parents:
diff changeset
23
460d96987670 parse linking scripts
lost
parents:
diff changeset
24 #ifdef HAVE_CONFIG_H
460d96987670 parse linking scripts
lost
parents:
diff changeset
25 #include "config.h"
460d96987670 parse linking scripts
lost
parents:
diff changeset
26 #endif
460d96987670 parse linking scripts
lost
parents:
diff changeset
27
460d96987670 parse linking scripts
lost
parents:
diff changeset
28 #include <errno.h>
460d96987670 parse linking scripts
lost
parents:
diff changeset
29 #include <stdio.h>
460d96987670 parse linking scripts
lost
parents:
diff changeset
30 #include <stdlib.h>
460d96987670 parse linking scripts
lost
parents:
diff changeset
31 #include <string.h>
460d96987670 parse linking scripts
lost
parents:
diff changeset
32
460d96987670 parse linking scripts
lost
parents:
diff changeset
33 #include "lwlink.h"
460d96987670 parse linking scripts
lost
parents:
diff changeset
34 #include "util.h"
460d96987670 parse linking scripts
lost
parents:
diff changeset
35
460d96987670 parse linking scripts
lost
parents:
diff changeset
36 // the built-in DECB target linker script
460d96987670 parse linking scripts
lost
parents:
diff changeset
37 static char *decb_script =
460d96987670 parse linking scripts
lost
parents:
diff changeset
38 "section init load at 2000\n"
460d96987670 parse linking scripts
lost
parents:
diff changeset
39 "section code\n"
460d96987670 parse linking scripts
lost
parents:
diff changeset
40 "section *,!bss\n"
460d96987670 parse linking scripts
lost
parents:
diff changeset
41 "section *,bss\n"
303
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
42 "entry 2000\n"
299
460d96987670 parse linking scripts
lost
parents:
diff changeset
43 ;
460d96987670 parse linking scripts
lost
parents:
diff changeset
44
460d96987670 parse linking scripts
lost
parents:
diff changeset
45 // the built-in RAW target linker script
460d96987670 parse linking scripts
lost
parents:
diff changeset
46 static char *raw_script =
460d96987670 parse linking scripts
lost
parents:
diff changeset
47 "section init load at 0000\n"
460d96987670 parse linking scripts
lost
parents:
diff changeset
48 "section code\n"
460d96987670 parse linking scripts
lost
parents:
diff changeset
49 "section *,!bss\n"
460d96987670 parse linking scripts
lost
parents:
diff changeset
50 "section *,bss\n"
460d96987670 parse linking scripts
lost
parents:
diff changeset
51 ;
460d96987670 parse linking scripts
lost
parents:
diff changeset
52
460d96987670 parse linking scripts
lost
parents:
diff changeset
53 // the "simple" script
460d96987670 parse linking scripts
lost
parents:
diff changeset
54 static char *simple_script =
460d96987670 parse linking scripts
lost
parents:
diff changeset
55 "section *,!bss\n"
460d96987670 parse linking scripts
lost
parents:
diff changeset
56 "section *,bss\n"
460d96987670 parse linking scripts
lost
parents:
diff changeset
57 ;
460d96987670 parse linking scripts
lost
parents:
diff changeset
58
460d96987670 parse linking scripts
lost
parents:
diff changeset
59 linkscript_t linkscript = { 0, NULL, -1 };
460d96987670 parse linking scripts
lost
parents:
diff changeset
60
460d96987670 parse linking scripts
lost
parents:
diff changeset
61 void setup_script()
460d96987670 parse linking scripts
lost
parents:
diff changeset
62 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
63 char *script;
460d96987670 parse linking scripts
lost
parents:
diff changeset
64 long size;
460d96987670 parse linking scripts
lost
parents:
diff changeset
65
460d96987670 parse linking scripts
lost
parents:
diff changeset
66 // read the file if needed
460d96987670 parse linking scripts
lost
parents:
diff changeset
67 if (scriptfile)
460d96987670 parse linking scripts
lost
parents:
diff changeset
68 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
69 FILE *f;
460d96987670 parse linking scripts
lost
parents:
diff changeset
70 long bread;
460d96987670 parse linking scripts
lost
parents:
diff changeset
71
460d96987670 parse linking scripts
lost
parents:
diff changeset
72 f = fopen(scriptfile, "rb");
460d96987670 parse linking scripts
lost
parents:
diff changeset
73 if (!f)
460d96987670 parse linking scripts
lost
parents:
diff changeset
74 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
75 fprintf(stderr, "Can't open file %s:", scriptfile);
460d96987670 parse linking scripts
lost
parents:
diff changeset
76 perror("");
460d96987670 parse linking scripts
lost
parents:
diff changeset
77 exit(1);
460d96987670 parse linking scripts
lost
parents:
diff changeset
78 }
460d96987670 parse linking scripts
lost
parents:
diff changeset
79 fseek(f, 0, SEEK_END);
460d96987670 parse linking scripts
lost
parents:
diff changeset
80 size = ftell(f);
460d96987670 parse linking scripts
lost
parents:
diff changeset
81 rewind(f);
460d96987670 parse linking scripts
lost
parents:
diff changeset
82
460d96987670 parse linking scripts
lost
parents:
diff changeset
83 script = lw_malloc(size + 2);
460d96987670 parse linking scripts
lost
parents:
diff changeset
84
460d96987670 parse linking scripts
lost
parents:
diff changeset
85 bread = fread(script, 1, size, f);
460d96987670 parse linking scripts
lost
parents:
diff changeset
86 if (bread < size)
460d96987670 parse linking scripts
lost
parents:
diff changeset
87 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
88 fprintf(stderr, "Short read on file %s (%ld/%ld):", scriptfile, bread, size);
460d96987670 parse linking scripts
lost
parents:
diff changeset
89 perror("");
460d96987670 parse linking scripts
lost
parents:
diff changeset
90 exit(1);
460d96987670 parse linking scripts
lost
parents:
diff changeset
91 }
460d96987670 parse linking scripts
lost
parents:
diff changeset
92 fclose(f);
460d96987670 parse linking scripts
lost
parents:
diff changeset
93
460d96987670 parse linking scripts
lost
parents:
diff changeset
94 script[size] = '\n';
460d96987670 parse linking scripts
lost
parents:
diff changeset
95 script[size + 1] = '\0';
460d96987670 parse linking scripts
lost
parents:
diff changeset
96 }
460d96987670 parse linking scripts
lost
parents:
diff changeset
97 else
460d96987670 parse linking scripts
lost
parents:
diff changeset
98 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
99 // fetch defaults based on output mode
460d96987670 parse linking scripts
lost
parents:
diff changeset
100 switch (outformat)
460d96987670 parse linking scripts
lost
parents:
diff changeset
101 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
102 case OUTPUT_RAW:
460d96987670 parse linking scripts
lost
parents:
diff changeset
103 script = raw_script;
460d96987670 parse linking scripts
lost
parents:
diff changeset
104 break;
460d96987670 parse linking scripts
lost
parents:
diff changeset
105
460d96987670 parse linking scripts
lost
parents:
diff changeset
106 case OUTPUT_DECB:
460d96987670 parse linking scripts
lost
parents:
diff changeset
107 script = decb_script;
460d96987670 parse linking scripts
lost
parents:
diff changeset
108 break;
460d96987670 parse linking scripts
lost
parents:
diff changeset
109
460d96987670 parse linking scripts
lost
parents:
diff changeset
110 default:
460d96987670 parse linking scripts
lost
parents:
diff changeset
111 script = simple_script;
460d96987670 parse linking scripts
lost
parents:
diff changeset
112 break;
460d96987670 parse linking scripts
lost
parents:
diff changeset
113 }
460d96987670 parse linking scripts
lost
parents:
diff changeset
114
460d96987670 parse linking scripts
lost
parents:
diff changeset
115 size = strlen(script);
460d96987670 parse linking scripts
lost
parents:
diff changeset
116 }
460d96987670 parse linking scripts
lost
parents:
diff changeset
117
460d96987670 parse linking scripts
lost
parents:
diff changeset
118 // now parse the script file
460d96987670 parse linking scripts
lost
parents:
diff changeset
119 while (*script)
460d96987670 parse linking scripts
lost
parents:
diff changeset
120 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
121 char *ptr, *ptr2, *line;
460d96987670 parse linking scripts
lost
parents:
diff changeset
122
460d96987670 parse linking scripts
lost
parents:
diff changeset
123 for (ptr = script; *ptr && *ptr != '\n' && *ptr != '\r'; ptr++)
460d96987670 parse linking scripts
lost
parents:
diff changeset
124 /* do nothing */ ;
460d96987670 parse linking scripts
lost
parents:
diff changeset
125
460d96987670 parse linking scripts
lost
parents:
diff changeset
126 line = lw_malloc(ptr - script + 1);
460d96987670 parse linking scripts
lost
parents:
diff changeset
127 memcpy(line, script, ptr - script);
460d96987670 parse linking scripts
lost
parents:
diff changeset
128 line[ptr - script] = '\0';
460d96987670 parse linking scripts
lost
parents:
diff changeset
129
460d96987670 parse linking scripts
lost
parents:
diff changeset
130 // skip line terms
460d96987670 parse linking scripts
lost
parents:
diff changeset
131 for (script = ptr + 1; *script == '\n' || *script == '\r'; script++)
460d96987670 parse linking scripts
lost
parents:
diff changeset
132 /* do nothing */ ;
460d96987670 parse linking scripts
lost
parents:
diff changeset
133
460d96987670 parse linking scripts
lost
parents:
diff changeset
134 // ignore leading whitespace
460d96987670 parse linking scripts
lost
parents:
diff changeset
135 for (ptr = line; *ptr && isspace(*ptr); ptr++)
460d96987670 parse linking scripts
lost
parents:
diff changeset
136 /* do nothing */ ;
460d96987670 parse linking scripts
lost
parents:
diff changeset
137
460d96987670 parse linking scripts
lost
parents:
diff changeset
138 // ignore blank lines
460d96987670 parse linking scripts
lost
parents:
diff changeset
139 if (!*ptr)
460d96987670 parse linking scripts
lost
parents:
diff changeset
140 continue;
460d96987670 parse linking scripts
lost
parents:
diff changeset
141
460d96987670 parse linking scripts
lost
parents:
diff changeset
142 for (ptr = line; *ptr && !isspace(*ptr); ptr++)
460d96987670 parse linking scripts
lost
parents:
diff changeset
143 /* do nothing */ ;
460d96987670 parse linking scripts
lost
parents:
diff changeset
144
460d96987670 parse linking scripts
lost
parents:
diff changeset
145 // now ptr points to the char past the first word
460d96987670 parse linking scripts
lost
parents:
diff changeset
146 // NUL it out
460d96987670 parse linking scripts
lost
parents:
diff changeset
147 if (*ptr)
460d96987670 parse linking scripts
lost
parents:
diff changeset
148 *ptr++ = '\0';
460d96987670 parse linking scripts
lost
parents:
diff changeset
149
460d96987670 parse linking scripts
lost
parents:
diff changeset
150 // skip spaces after the first word
460d96987670 parse linking scripts
lost
parents:
diff changeset
151 for ( ; *ptr && isspace(*ptr); ptr++)
460d96987670 parse linking scripts
lost
parents:
diff changeset
152 /* do nothing */ ;
460d96987670 parse linking scripts
lost
parents:
diff changeset
153
460d96987670 parse linking scripts
lost
parents:
diff changeset
154 if (!strcmp(line, "pad"))
460d96987670 parse linking scripts
lost
parents:
diff changeset
155 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
156 // padding
460d96987670 parse linking scripts
lost
parents:
diff changeset
157 // parse the hex number and stow it
460d96987670 parse linking scripts
lost
parents:
diff changeset
158 linkscript.padsize = strtol(ptr, NULL, 16);
460d96987670 parse linking scripts
lost
parents:
diff changeset
159 if (linkscript.padsize < 0)
460d96987670 parse linking scripts
lost
parents:
diff changeset
160 linkscript.padsize = 0;
460d96987670 parse linking scripts
lost
parents:
diff changeset
161 }
303
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
162 else if (!strcmp(line, "entry"))
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
163 {
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
164 int eaddr;
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
165
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
166 eaddr = strtol(ptr, &ptr2, 16);
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
167 if (*ptr2)
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
168 {
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
169 linkscript.execaddr = -1;
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
170 linkscript.execsym = lw_strdup(ptr);
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
171 }
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
172 else
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
173 {
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
174 linkscript.execaddr = eaddr;
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
175 linkscript.execsym = NULL;
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
176 }
13272197d278 Added simplistic output module for DECB target
lost
parents: 301
diff changeset
177 }
299
460d96987670 parse linking scripts
lost
parents:
diff changeset
178 else if (!strcmp(line, "section"))
460d96987670 parse linking scripts
lost
parents:
diff changeset
179 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
180 // section
460d96987670 parse linking scripts
lost
parents:
diff changeset
181 // parse out the section name and flags
460d96987670 parse linking scripts
lost
parents:
diff changeset
182 for (ptr2 = ptr; *ptr2 && !isspace(*ptr2); ptr2++)
460d96987670 parse linking scripts
lost
parents:
diff changeset
183 /* do nothing */ ;
460d96987670 parse linking scripts
lost
parents:
diff changeset
184
460d96987670 parse linking scripts
lost
parents:
diff changeset
185 if (*ptr2)
460d96987670 parse linking scripts
lost
parents:
diff changeset
186 *ptr2++ = '\0';
460d96987670 parse linking scripts
lost
parents:
diff changeset
187
460d96987670 parse linking scripts
lost
parents:
diff changeset
188 while (*ptr2 && isspace(*ptr2))
460d96987670 parse linking scripts
lost
parents:
diff changeset
189 ptr2++;
460d96987670 parse linking scripts
lost
parents:
diff changeset
190
460d96987670 parse linking scripts
lost
parents:
diff changeset
191 // ptr now points to the section name and flags and ptr2
460d96987670 parse linking scripts
lost
parents:
diff changeset
192 // to the first non-space character following
460d96987670 parse linking scripts
lost
parents:
diff changeset
193
460d96987670 parse linking scripts
lost
parents:
diff changeset
194 // then look for "load <addr>" clause
460d96987670 parse linking scripts
lost
parents:
diff changeset
195 if (*ptr2)
460d96987670 parse linking scripts
lost
parents:
diff changeset
196 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
197 if (!strncmp(ptr2, "load", 4))
460d96987670 parse linking scripts
lost
parents:
diff changeset
198 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
199 ptr2 += 4;
460d96987670 parse linking scripts
lost
parents:
diff changeset
200 while (*ptr2 && isspace(*ptr2))
460d96987670 parse linking scripts
lost
parents:
diff changeset
201 ptr2++;
460d96987670 parse linking scripts
lost
parents:
diff changeset
202
460d96987670 parse linking scripts
lost
parents:
diff changeset
203 }
460d96987670 parse linking scripts
lost
parents:
diff changeset
204 else
460d96987670 parse linking scripts
lost
parents:
diff changeset
205 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
206 fprintf(stderr, "%s: bad script\n", scriptfile);
460d96987670 parse linking scripts
lost
parents:
diff changeset
207 exit(1);
460d96987670 parse linking scripts
lost
parents:
diff changeset
208 }
460d96987670 parse linking scripts
lost
parents:
diff changeset
209 }
460d96987670 parse linking scripts
lost
parents:
diff changeset
210
460d96987670 parse linking scripts
lost
parents:
diff changeset
211 // now ptr2 points to the load address if there is one
460d96987670 parse linking scripts
lost
parents:
diff changeset
212 // or NUL if not
460d96987670 parse linking scripts
lost
parents:
diff changeset
213 linkscript.lines = lw_realloc(linkscript.lines, sizeof(struct scriptline_s) * (linkscript.nlines + 1));
460d96987670 parse linking scripts
lost
parents:
diff changeset
214
460d96987670 parse linking scripts
lost
parents:
diff changeset
215 linkscript.lines[linkscript.nlines].noflags = 0;
460d96987670 parse linking scripts
lost
parents:
diff changeset
216 linkscript.lines[linkscript.nlines].yesflags = 0;
460d96987670 parse linking scripts
lost
parents:
diff changeset
217 if (*ptr2)
460d96987670 parse linking scripts
lost
parents:
diff changeset
218 linkscript.lines[linkscript.nlines].loadat = strtol(ptr2, NULL, 16);
460d96987670 parse linking scripts
lost
parents:
diff changeset
219 else
460d96987670 parse linking scripts
lost
parents:
diff changeset
220 linkscript.lines[linkscript.nlines].loadat = -1;
460d96987670 parse linking scripts
lost
parents:
diff changeset
221 for (ptr2 = ptr; *ptr2 && *ptr2 != ','; ptr2++)
460d96987670 parse linking scripts
lost
parents:
diff changeset
222 /* do nothing */ ;
460d96987670 parse linking scripts
lost
parents:
diff changeset
223 if (*ptr2)
460d96987670 parse linking scripts
lost
parents:
diff changeset
224 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
225 *ptr2++ = '\0';
460d96987670 parse linking scripts
lost
parents:
diff changeset
226 if (!strcmp(ptr2, "!bss"))
460d96987670 parse linking scripts
lost
parents:
diff changeset
227 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
228 linkscript.lines[linkscript.nlines].noflags = SECTION_BSS;
460d96987670 parse linking scripts
lost
parents:
diff changeset
229 }
460d96987670 parse linking scripts
lost
parents:
diff changeset
230 else if (!strcmp(ptr2, "bss"))
460d96987670 parse linking scripts
lost
parents:
diff changeset
231 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
232 linkscript.lines[linkscript.nlines].yesflags = SECTION_BSS;
460d96987670 parse linking scripts
lost
parents:
diff changeset
233 }
460d96987670 parse linking scripts
lost
parents:
diff changeset
234 else
460d96987670 parse linking scripts
lost
parents:
diff changeset
235 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
236 fprintf(stderr, "%s: bad script\n", scriptfile);
460d96987670 parse linking scripts
lost
parents:
diff changeset
237 exit(1);
460d96987670 parse linking scripts
lost
parents:
diff changeset
238 }
460d96987670 parse linking scripts
lost
parents:
diff changeset
239 }
301
376cc55361c7 section order and address resolution done
lost
parents: 300
diff changeset
240 if (ptr[0] == '*' && ptr[1] == '\0')
376cc55361c7 section order and address resolution done
lost
parents: 300
diff changeset
241 linkscript.lines[linkscript.nlines].sectname = NULL;
376cc55361c7 section order and address resolution done
lost
parents: 300
diff changeset
242 else
376cc55361c7 section order and address resolution done
lost
parents: 300
diff changeset
243 linkscript.lines[linkscript.nlines].sectname = lw_strdup(ptr);
299
460d96987670 parse linking scripts
lost
parents:
diff changeset
244 linkscript.nlines++;
460d96987670 parse linking scripts
lost
parents:
diff changeset
245 }
460d96987670 parse linking scripts
lost
parents:
diff changeset
246 else
460d96987670 parse linking scripts
lost
parents:
diff changeset
247 {
460d96987670 parse linking scripts
lost
parents:
diff changeset
248 fprintf(stderr, "%s: bad script\n", scriptfile);
460d96987670 parse linking scripts
lost
parents:
diff changeset
249 exit(1);
460d96987670 parse linking scripts
lost
parents:
diff changeset
250 }
460d96987670 parse linking scripts
lost
parents:
diff changeset
251 lw_free(line);
460d96987670 parse linking scripts
lost
parents:
diff changeset
252 }
460d96987670 parse linking scripts
lost
parents:
diff changeset
253
460d96987670 parse linking scripts
lost
parents:
diff changeset
254 if (scriptfile)
460d96987670 parse linking scripts
lost
parents:
diff changeset
255 lw_free(script);
460d96987670 parse linking scripts
lost
parents:
diff changeset
256 }