annotate lwlink/script.c @ 264:61d1db1dfe2a 2.6

Build manual for release
author lost
date Tue, 22 Dec 2009 05:27:32 +0000
parents f6af5791472c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
117
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
1 /*
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
2 script.c
118
d450943305a7 Fixed copyright year
lost
parents: 117
diff changeset
3 Copyright © 2009 William Astle
117
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
4
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
5 This file is part of LWLINK.
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
6
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
7 LWLINK is free software: you can redistribute it and/or modify it under the
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
10 version.
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
11
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
15 more details.
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
16
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
19
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
20
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
21 Read and parse linker scripts
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
22 */
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
23
212
bae1e3ecdce1 More preparation for gnulib integration
lost
parents: 187
diff changeset
24 #include <config.h>
117
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
25
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
26 #include <errno.h>
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
27 #include <stdio.h>
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
28 #include <stdlib.h>
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
29 #include <string.h>
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
30
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
31 #include "lwlink.h"
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
32 #include "util.h"
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
33
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
34 // the built-in DECB target linker script
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
35 static char *decb_script =
124
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 121
diff changeset
36 "section init load 2000\n"
117
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
37 "section code\n"
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
38 "section *,!bss\n"
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
39 "section *,bss\n"
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
40 "entry 2000\n"
117
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
41 ;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
42
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
43 // the built-in RAW target linker script
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
44 static char *raw_script =
124
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 121
diff changeset
45 "section init load 0000\n"
117
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
46 "section code\n"
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
47 "section *,!bss\n"
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
48 "section *,bss\n"
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
49 ;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
50
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
51 static char *lwex0_script =
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
52 "section init load 0100\n"
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
53 "section .text\n"
251
f6af5791472c Fixed default LWEX0 linkscript to include .ctors and .dtors sections
lost
parents: 247
diff changeset
54 "section .ctors_start\n"
f6af5791472c Fixed default LWEX0 linkscript to include .ctors and .dtors sections
lost
parents: 247
diff changeset
55 "section .ctors\n"
f6af5791472c Fixed default LWEX0 linkscript to include .ctors and .dtors sections
lost
parents: 247
diff changeset
56 "section .ctors_end\n"
f6af5791472c Fixed default LWEX0 linkscript to include .ctors and .dtors sections
lost
parents: 247
diff changeset
57 "section .dtors_start\n"
f6af5791472c Fixed default LWEX0 linkscript to include .ctors and .dtors sections
lost
parents: 247
diff changeset
58 "section .dtors\n"
f6af5791472c Fixed default LWEX0 linkscript to include .ctors and .dtors sections
lost
parents: 247
diff changeset
59 "section .dtors_end\n"
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
60 "section .data\n"
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
61 "section *,!bss\n"
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
62 "section *,bss\n"
219
402a035220ed Tweaked lwex0 linkscript to start at __start
lost
parents: 212
diff changeset
63 "entry __start\n"
246
c602f6eca3ed Changed default LWEX stack size to 1K
lost
parents: 219
diff changeset
64 "stacksize 0400\n" // default 1024 byte stack
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
65 ;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
66
117
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
67 // the "simple" script
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
68 static char *simple_script =
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
69 "section *,!bss\n"
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
70 "section *,bss\n"
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
71 ;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
72
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
73 linkscript_t linkscript = { 0, NULL, -1 };
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
74
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
75 void setup_script()
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
76 {
125
f9bfc2986023 Actually allow script file to be specified and fix segfault on parsing script file
lost
parents: 124
diff changeset
77 char *script, *oscript;
117
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
78 long size;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
79
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
80 // read the file if needed
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
81 if (scriptfile)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
82 {
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
83 FILE *f;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
84 long bread;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
85
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
86 f = fopen(scriptfile, "rb");
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
87 if (!f)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
88 {
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
89 fprintf(stderr, "Can't open file %s:", scriptfile);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
90 perror("");
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
91 exit(1);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
92 }
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
93 fseek(f, 0, SEEK_END);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
94 size = ftell(f);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
95 rewind(f);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
96
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
97 script = lw_malloc(size + 2);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
98
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
99 bread = fread(script, 1, size, f);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
100 if (bread < size)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
101 {
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
102 fprintf(stderr, "Short read on file %s (%ld/%ld):", scriptfile, bread, size);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
103 perror("");
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
104 exit(1);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
105 }
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
106 fclose(f);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
107
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
108 script[size] = '\n';
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
109 script[size + 1] = '\0';
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
110 }
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
111 else
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
112 {
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
113 // fetch defaults based on output mode
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
114 switch (outformat)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
115 {
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
116 case OUTPUT_RAW:
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
117 script = raw_script;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
118 break;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
119
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
120 case OUTPUT_DECB:
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
121 script = decb_script;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
122 break;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
123
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
124 case OUTPUT_LWEX0:
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
125 script = lwex0_script;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
126 break;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
127
117
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
128 default:
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
129 script = simple_script;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
130 break;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
131 }
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
132
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
133 size = strlen(script);
180
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
134 if (nscriptls)
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
135 {
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
136 char *rscript;
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
137 int i;
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
138 // prepend the "extra" script lines
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
139 for (i = 0; i < nscriptls; i++)
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
140 size += strlen(scriptls[i]) + 1;
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
141
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
142 rscript = lw_malloc(size + 1);
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
143 oscript = rscript;
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
144 for (i = 0; i < nscriptls; i++)
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
145 {
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
146 oscript += sprintf(oscript, "%s\n", scriptls[i]);
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
147 }
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
148 strcpy(oscript, script);
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
149 script = rscript;
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
150 }
117
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
151 }
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
152
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
153 if (outformat == OUTPUT_LWEX0)
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
154 linkscript.stacksize = 0x100;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
155
125
f9bfc2986023 Actually allow script file to be specified and fix segfault on parsing script file
lost
parents: 124
diff changeset
156 oscript = script;
117
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
157 // now parse the script file
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
158 while (*script)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
159 {
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
160 char *ptr, *ptr2, *line;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
161
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
162 for (ptr = script; *ptr && *ptr != '\n' && *ptr != '\r'; ptr++)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
163 /* do nothing */ ;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
164
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
165 line = lw_malloc(ptr - script + 1);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
166 memcpy(line, script, ptr - script);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
167 line[ptr - script] = '\0';
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
168
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
169 // skip line terms
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
170 for (script = ptr + 1; *script == '\n' || *script == '\r'; script++)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
171 /* do nothing */ ;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
172
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
173 // ignore leading whitespace
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
174 for (ptr = line; *ptr && isspace(*ptr); ptr++)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
175 /* do nothing */ ;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
176
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
177 // ignore blank lines
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
178 if (!*ptr)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
179 continue;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
180
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
181 for (ptr = line; *ptr && !isspace(*ptr); ptr++)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
182 /* do nothing */ ;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
183
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
184 // now ptr points to the char past the first word
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
185 // NUL it out
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
186 if (*ptr)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
187 *ptr++ = '\0';
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
188
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
189 // skip spaces after the first word
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
190 for ( ; *ptr && isspace(*ptr); ptr++)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
191 /* do nothing */ ;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
192
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
193 if (!strcmp(line, "pad"))
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
194 {
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
195 // padding
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
196 // parse the hex number and stow it
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
197 linkscript.padsize = strtol(ptr, NULL, 16);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
198 if (linkscript.padsize < 0)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
199 linkscript.padsize = 0;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
200 }
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
201 else if (!strcmp(line, "stacksize"))
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
202 {
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
203 // stack size for targets that support it
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
204 // parse the hex number and stow it
247
d8b5ff5c4ef9 Fixed LWEX stack size setting bug
lost
parents: 246
diff changeset
205 linkscript.stacksize = strtol(ptr, NULL, 16);
187
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
206 if (linkscript.stacksize < 0)
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
207 linkscript.stacksize = 0x100;
857cb407229e Added LWEX0 (LWOS simple binary) target to lwlink
lost
parents: 180
diff changeset
208 }
121
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
209 else if (!strcmp(line, "entry"))
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
210 {
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
211 int eaddr;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
212
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
213 eaddr = strtol(ptr, &ptr2, 16);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
214 if (*ptr2)
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
215 {
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
216 linkscript.execaddr = -1;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
217 linkscript.execsym = lw_strdup(ptr);
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
218 }
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
219 else
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
220 {
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
221 linkscript.execaddr = eaddr;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
222 linkscript.execsym = NULL;
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
223 }
7bc853cb2ca9 Added simplistic output module for DECB target
lost
parents: 119
diff changeset
224 }
117
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
225 else if (!strcmp(line, "section"))
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
226 {
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
227 // section
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
228 // parse out the section name and flags
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
229 for (ptr2 = ptr; *ptr2 && !isspace(*ptr2); ptr2++)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
230 /* do nothing */ ;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
231
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
232 if (*ptr2)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
233 *ptr2++ = '\0';
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
234
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
235 while (*ptr2 && isspace(*ptr2))
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
236 ptr2++;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
237
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
238 // ptr now points to the section name and flags and ptr2
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
239 // to the first non-space character following
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
240
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
241 // then look for "load <addr>" clause
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
242 if (*ptr2)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
243 {
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
244 if (!strncmp(ptr2, "load", 4))
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
245 {
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
246 ptr2 += 4;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
247 while (*ptr2 && isspace(*ptr2))
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
248 ptr2++;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
249
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
250 }
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
251 else
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
252 {
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
253 fprintf(stderr, "%s: bad script\n", scriptfile);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
254 exit(1);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
255 }
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
256 }
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
257
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
258 // now ptr2 points to the load address if there is one
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
259 // or NUL if not
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
260 linkscript.lines = lw_realloc(linkscript.lines, sizeof(struct scriptline_s) * (linkscript.nlines + 1));
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
261
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
262 linkscript.lines[linkscript.nlines].noflags = 0;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
263 linkscript.lines[linkscript.nlines].yesflags = 0;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
264 if (*ptr2)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
265 linkscript.lines[linkscript.nlines].loadat = strtol(ptr2, NULL, 16);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
266 else
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
267 linkscript.lines[linkscript.nlines].loadat = -1;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
268 for (ptr2 = ptr; *ptr2 && *ptr2 != ','; ptr2++)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
269 /* do nothing */ ;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
270 if (*ptr2)
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
271 {
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
272 *ptr2++ = '\0';
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
273 if (!strcmp(ptr2, "!bss"))
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
274 {
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
275 linkscript.lines[linkscript.nlines].noflags = SECTION_BSS;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
276 }
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
277 else if (!strcmp(ptr2, "bss"))
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
278 {
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
279 linkscript.lines[linkscript.nlines].yesflags = SECTION_BSS;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
280 }
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
281 else
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
282 {
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
283 fprintf(stderr, "%s: bad script\n", scriptfile);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
284 exit(1);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
285 }
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
286 }
119
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
287 if (ptr[0] == '*' && ptr[1] == '\0')
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
288 linkscript.lines[linkscript.nlines].sectname = NULL;
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
289 else
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
290 linkscript.lines[linkscript.nlines].sectname = lw_strdup(ptr);
117
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
291 linkscript.nlines++;
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
292 }
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
293 else
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
294 {
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
295 fprintf(stderr, "%s: bad script\n", scriptfile);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
296 exit(1);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
297 }
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
298 lw_free(line);
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
299 }
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
300
180
6ebb93b409ba Added library paths and --section-base
lost
parents: 139
diff changeset
301 if (scriptfile || nscriptls)
125
f9bfc2986023 Actually allow script file to be specified and fix segfault on parsing script file
lost
parents: 124
diff changeset
302 lw_free(oscript);
117
bb3cc989e84b parse linking scripts
lost
parents:
diff changeset
303 }