comparison lwlink/script.c @ 236:ce1fdc8d6568

Added ability to add padding after a section when linking. Added the ability for lwlink to automatically append padding bytes to the end of a section (once the section instances are merged). This behaviour is controlled by the link script. See the updated documentation for more information.
author William Astle <lost@l-w.ca>
date Sat, 11 Aug 2012 23:29:57 -0600
parents d389adbcc4ab
children ebda5c96665e
comparison
equal deleted inserted replaced
235:e3741cf53e00 236:ce1fdc8d6568
68 ; 68 ;
69 69
70 static char *lwex0_script = 70 static char *lwex0_script =
71 "define basesympat s_%s\n" 71 "define basesympat s_%s\n"
72 "define lensympat l_%s\n" 72 "define lensympat l_%s\n"
73 "sectopt .ctors padafter 00,00\n"
74 "sectopt .dtors padafter 00,00\n"
73 "section init load 0100\n" 75 "section init load 0100\n"
74 "section .text\n" 76 "section .text\n"
75 "section .data\n" 77 "section .data\n"
76 "section .ctors_start\n" 78 "section .ctors_start\n"
77 "section .ctors\n" 79 "section .ctors\n"
224 226
225 // skip spaces after the first word 227 // skip spaces after the first word
226 for ( ; *ptr && isspace(*ptr); ptr++) 228 for ( ; *ptr && isspace(*ptr); ptr++)
227 /* do nothing */ ; 229 /* do nothing */ ;
228 230
229 if (!strcmp(line, "define")) 231 if (!strcmp(line, "sectopt"))
232 {
233 char *sn;
234 char *ptr3;
235 sectopt_t *so;
236
237 for (ptr2 = ptr; *ptr && !isspace(*ptr2); ptr2++)
238 /* do nothing */ ;
239
240 if (*ptr2)
241 *ptr2++ = '\0';
242
243 while (*ptr2 && isspace(*ptr2))
244 ptr2++;
245
246 // section name is at ptr
247 // ptr2 is the option type
248 sn = ptr;
249
250 // now ptr2 points to the section option name
251 for (ptr3 = ptr2; *ptr3 && !isspace(*ptr3); ptr3++)
252 /* do nothing */ ;
253
254 if (*ptr3)
255 *ptr3++ = 0;
256
257 while (*ptr3 && isspace(*ptr3))
258 ptr3++;
259
260 // now ptr3 points to option value
261 for (so = section_opts; so; so = so -> next)
262 {
263 if (!strcmp(so -> name, sn))
264 break;
265 }
266
267 if (!so)
268 {
269 so = lw_alloc(sizeof(sectopt_t));
270 so -> name = lw_strdup(sn);
271 so -> aftersize = 0;
272 so -> afterbytes = NULL;
273 so -> next = section_opts;
274 section_opts = so;
275 }
276
277 if (!strcmp(ptr2, "padafter"))
278 {
279 if (so -> afterbytes)
280 lw_free(so -> afterbytes);
281 so -> aftersize = 0;
282
283 for (;;)
284 {
285 int v;
286 char *ptr4;
287
288 while (*ptr3 && isspace(*ptr3))
289 ptr3++;
290
291 if (!ptr3)
292 break;
293
294 v = strtoul(ptr3, &ptr4, 16);
295 if (ptr3 == ptr4)
296 break;
297
298
299 so -> afterbytes = lw_realloc(so -> afterbytes, so -> aftersize + 1);
300 so -> afterbytes[so -> aftersize] = v;
301 so -> aftersize++;
302 ptr3 = ptr4;
303 while (*ptr3 && isspace(*ptr3))
304 ptr3++;
305
306 if (*ptr3 != ',')
307 break;
308
309 ptr3++;
310 }
311 }
312 else
313 {
314 fprintf(stderr, "%s: bad script line: %s %s\n", scriptfile, line, ptr2);
315 exit(1);
316 }
317 }
318 else if (!strcmp(line, "define"))
230 { 319 {
231 // parse out the definition type 320 // parse out the definition type
232 for (ptr2 = ptr; *ptr2 && !isspace(*ptr2); ptr2++) 321 for (ptr2 = ptr; *ptr2 && !isspace(*ptr2); ptr2++)
233 /* do nothing */ ; 322 /* do nothing */ ;
234 323
251 lw_free(linkscript.lensympat); 340 lw_free(linkscript.lensympat);
252 linkscript.lensympat = lw_strdup(ptr2); 341 linkscript.lensympat = lw_strdup(ptr2);
253 } 342 }
254 else 343 else
255 { 344 {
256 fprintf(stderr, "%s: bad script line: %s\n", scriptfile, line);
257 exit(1);
258 } 345 }
259 } 346 }
260 else if (!strcmp(line, "pad")) 347 else if (!strcmp(line, "pad"))
261 { 348 {
262 // padding 349 // padding