Commit | Line | Data |
---|---|---|
2fa0b342 DHW |
1 | %{ |
2 | /* Copyright (C) 1991 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GLD, the Gnu Linker. | |
5 | ||
6 | GLD is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 1, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GLD is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GLD; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | /* | |
21 | * $Id$ | |
7ca04d28 | 22 | |
2fa0b342 DHW |
23 | * |
24 | */ | |
25 | ||
26 | ||
27 | ||
28 | /*SUPPRESS 529*/ | |
29 | /*SUPPRESS 26*/ | |
30 | /*SUPPRESS 29*/ | |
1418c83b | 31 | #define LEXDEBUG 0 |
2fa0b342 DHW |
32 | #include "sysdep.h" |
33 | #include "bfd.h" | |
34 | ||
35 | #include <ctype.h> | |
36 | #include "ldlex.h" | |
37 | ||
38 | #include "ld.h" | |
39 | #include "ldexp.h" | |
40 | #include "ldgram.tab.h" | |
41 | #include "ldmisc.h" | |
42 | ||
43 | #undef input | |
44 | #undef unput | |
45 | #define input lex_input | |
46 | #define unput lex_unput | |
47 | int debug; | |
7ca04d28 SC |
48 | |
49 | extern boolean ldgram_in_expression; | |
50 | extern boolean ldgram_in_defsym; | |
1418c83b | 51 | extern boolean ldgram_in_script; |
2fa0b342 DHW |
52 | static char *command_line; |
53 | ||
54 | extern int fgetc(); | |
55 | extern int yyparse(); | |
56 | ||
57 | typedef struct { | |
58 | char *name; | |
59 | int value; | |
60 | } keyword_type; | |
61 | #define RTOKEN(x) { yylval.token = x; return x; } | |
62 | keyword_type keywords[] = | |
63 | { | |
64 | "MEMORY",MEMORY, | |
65 | "ORIGIN",ORIGIN, | |
66 | "BLOCK",BLOCK, | |
67 | "LENGTH",LENGTH, | |
68 | "ALIGN",ALIGN_K, | |
69 | "SUBSECTION_ALIGN",SUBSECTION_ALIGN, | |
70 | "ADDR",ADDR, | |
71 | "ENTRY",ENTRY, | |
1418c83b SC |
72 | "SCRIPT", SCRIPT, |
73 | "ENDSCRIPT", ENDSCRIPT, | |
2fa0b342 DHW |
74 | "NEXT",NEXT, |
75 | "MAP",MAP, | |
76 | "SIZEOF",SIZEOF, | |
77 | "TARGET",TARGET_K, | |
78 | "SEARCH_DIR",SEARCH_DIR, | |
79 | "OUTPUT",OUTPUT, | |
80 | "INPUT",INPUT, | |
81 | "DEFINED",DEFINED, | |
82 | "CREATE_OBJECT_SYMBOLS",CREATE_OBJECT_SYMBOLS, | |
fd846434 | 83 | "FORCE_COMMON_ALLOCATION",FORCE_COMMON_ALLOCATION, |
2fa0b342 DHW |
84 | "SECTIONS",SECTIONS, |
85 | "FILL",FILL, | |
86 | "STARTUP",STARTUP, | |
fd846434 | 87 | "OUTPUT_FORMAT",OUTPUT_FORMAT, |
2fa0b342 DHW |
88 | "HLL",HLL, |
89 | "SYSLIB",SYSLIB, | |
90 | "FLOAT",FLOAT, | |
91 | "LONG", LONG, | |
92 | "SHORT", SHORT, | |
93 | "BYTE", BYTE, | |
94 | "NOFLOAT",NOFLOAT, | |
95 | "o",ORIGIN, | |
96 | "org",ORIGIN, | |
97 | "l", LENGTH, | |
98 | "len", LENGTH, | |
99 | 0,0}; | |
100 | unsigned int lineno; | |
101 | extern boolean hex_mode; | |
102 | FILE *ldlex_input_stack; | |
103 | static unsigned int have_pushback; | |
104 | #define NPUSHBACK 10 | |
105 | int pushback[NPUSHBACK]; | |
106 | int thischar; | |
107 | extern char *ldfile_input_filename; | |
1418c83b | 108 | int donehash = 0; |
2fa0b342 DHW |
109 | int |
110 | lex_input() | |
111 | { | |
2fa0b342 | 112 | if (have_pushback > 0) |
1418c83b SC |
113 | { |
114 | have_pushback --; | |
115 | return thischar = pushback[have_pushback]; | |
116 | } | |
2fa0b342 DHW |
117 | if (ldlex_input_stack) { |
118 | thischar = fgetc(ldlex_input_stack); | |
119 | ||
120 | if (thischar == EOF) { | |
121 | fclose(ldlex_input_stack); | |
122 | ldlex_input_stack = (FILE *)NULL; | |
123 | ldfile_input_filename = (char *)NULL; | |
1418c83b SC |
124 | /* First char after script eof is a @ so that we can tell the grammer |
125 | that we've eft */ | |
126 | thischar = '@'; | |
2fa0b342 DHW |
127 | |
128 | } | |
129 | } | |
130 | else if (command_line && *command_line) { | |
131 | thischar = *(command_line++); | |
132 | } | |
1418c83b SC |
133 | else { |
134 | thischar = 0; | |
135 | } | |
2fa0b342 | 136 | if(thischar == '\t') thischar = ' '; |
7ca04d28 | 137 | if (thischar == '\n') { thischar = ' '; lineno++; } |
2fa0b342 DHW |
138 | return thischar ; |
139 | } | |
140 | ||
141 | void | |
142 | lex_unput(c) | |
143 | int c; | |
144 | { | |
145 | if (have_pushback > NPUSHBACK) { | |
146 | info("%F%P Too many pushbacks\n"); | |
147 | } | |
148 | ||
149 | pushback[have_pushback] = c; | |
150 | have_pushback ++; | |
151 | } | |
152 | ||
153 | ||
154 | int | |
155 | yywrap() | |
156 | { return 1; } | |
157 | /*VARARGS*/ | |
158 | ||
159 | void | |
160 | allprint(x) | |
161 | int x; | |
162 | { | |
163 | fprintf(yyout,"%d",x); | |
164 | } | |
165 | ||
166 | void | |
167 | sprint(x) | |
168 | char *x; | |
169 | { | |
170 | fprintf(yyout,"%s",x); | |
171 | } | |
172 | ||
173 | int thischar; | |
174 | ||
175 | void parse_line(arg) | |
176 | char *arg; | |
177 | { | |
178 | command_line = arg; | |
179 | have_pushback = 0; | |
180 | yyparse(); | |
181 | } | |
182 | ||
183 | ||
184 | ||
185 | void | |
186 | parse_args(ac, av) | |
187 | int ac; | |
188 | char **av; | |
189 | { | |
190 | char *p; | |
191 | int i; | |
192 | size_t size = 0; | |
193 | char *dst; | |
194 | debug = 1; | |
195 | for (i= 1; i < ac; i++) { | |
196 | size += strlen(av[i]) + 2; | |
197 | } | |
198 | dst = p = (char *)ldmalloc(size + 2); | |
199 | /* Put a space arount each option */ | |
200 | ||
201 | ||
202 | for (i =1; i < ac; i++) { | |
203 | ||
204 | unsigned int s = strlen(av[i]); | |
205 | *dst++ = ' '; | |
206 | memcpy(dst, av[i], s); | |
207 | dst[s] = ' '; | |
208 | dst += s + 1; | |
209 | } | |
210 | *dst = 0; | |
211 | parse_line(p); | |
212 | ||
213 | free(p); | |
214 | ||
215 | ||
216 | } | |
217 | ||
218 | long number(text, base) | |
219 | char *text; | |
220 | int base; | |
221 | { | |
1418c83b | 222 | unsigned long l = 0; |
2fa0b342 DHW |
223 | char *p; |
224 | for (p = text; *p != 0; p++) { | |
225 | if (*p == 'K') { | |
226 | l =l * 1024; | |
227 | } | |
228 | else if(*p== 'M') { | |
229 | l =l * 1024 * 1024; | |
230 | } | |
231 | else { | |
232 | l =l * base; | |
233 | if (isdigit(*p)) { | |
234 | l += *p - '0'; | |
235 | } | |
236 | else if (islower(*p)) { | |
237 | l += *p - 'a' + 10; | |
238 | } | |
239 | else { | |
240 | l += *p - 'A' + 10; | |
241 | } | |
242 | } | |
243 | } | |
244 | return l; | |
245 | } | |
246 | %} | |
247 | ||
248 | %a 4000 | |
249 | %o 5000 | |
1418c83b | 250 | FILENAMECHAR [a-zA-Z0-9\/\.\-\_\+\=] |
2fa0b342 DHW |
251 | FILENAME {FILENAMECHAR}+ |
252 | ||
253 | ||
254 | WHITE [ \t]+ | |
255 | ||
256 | %% | |
2fa0b342 | 257 | |
1418c83b SC |
258 | "@" { return ENDSCRIPT; } |
259 | "\ -defsym\ " { return OPTION_defsym; } | |
260 | "\ -noinhibit_exec\ " { return OPTION_noinhibit_exec; } | |
261 | "\ -format\ " { return OPTION_format; } | |
262 | "\ -n\ " { return OPTION_n; } | |
263 | "\ -r\ " { return OPTION_r; } | |
264 | "\ -Ur\ " { return OPTION_Ur; } | |
265 | "\ -o\ " { return OPTION_o; } | |
266 | "\ -g\ " { return OPTION_g; } | |
267 | "\ -e\ " { return OPTION_e; } | |
268 | "\ -b\ " { return OPTION_b; } | |
269 | "\ -dc\ " { return OPTION_dc; } | |
270 | "\ -dp\ " { return OPTION_dp; } | |
271 | "\ -d\ " { return OPTION_d; } | |
272 | "\ -v\ " { return OPTION_v; } | |
273 | "\ -M\ " { return OPTION_M; } | |
274 | "\ -t\ " { return OPTION_t; } | |
275 | "\ -X\ " { return OPTION_X; } | |
276 | "\ -x\ " { return OPTION_x; } | |
277 | "\ -c\ " { return OPTION_c; } | |
278 | "\ -R\ " { return OPTION_R; } | |
279 | "\ -u\ " { return OPTION_u; } | |
280 | "\ -s\ " { return OPTION_s; } | |
281 | "\ -S\ " { return OPTION_S; } | |
2fa0b342 DHW |
282 | "\ -l"{FILENAME} { |
283 | yylval.name = buystring(yytext+3); | |
284 | return OPTION_l; | |
285 | } | |
286 | ||
287 | "\ -L"{FILENAME} { | |
288 | yylval.name = buystring(yytext+3); | |
289 | return OPTION_L; | |
290 | } | |
1418c83b | 291 | "\ -Ttext\ " { |
2fa0b342 DHW |
292 | yylval.name = ".text"; |
293 | return OPTION_Texp; | |
294 | } | |
1418c83b | 295 | "\ -Tdata\ " { |
2fa0b342 DHW |
296 | yylval.name = ".data"; |
297 | return OPTION_Texp; | |
298 | } | |
1418c83b | 299 | "\ -Tbss\ " { |
2fa0b342 DHW |
300 | yylval.name = ".bss"; |
301 | return OPTION_Texp; | |
302 | } | |
303 | ||
304 | "\ -T"{FILENAME} { | |
305 | yylval.name = buystring(yytext+3); | |
306 | return OPTION_Tfile; | |
307 | } | |
1418c83b | 308 | "\ -T\ " { |
2fa0b342 DHW |
309 | return OPTION_T; |
310 | } | |
311 | ||
7ca04d28 SC |
312 | "\ -F"{FILENAME} { |
313 | return OPTION_F; | |
314 | } | |
1418c83b | 315 | "\ -F\ " { |
7ca04d28 SC |
316 | return OPTION_F; |
317 | } | |
318 | ||
2fa0b342 DHW |
319 | "\ -A"{FILENAME} { |
320 | yylval.name = buystring(yytext+3); | |
321 | return OPTION_Aarch; | |
322 | } | |
323 | " " { } | |
324 | "<<=" { RTOKEN(LSHIFTEQ);} | |
325 | ">>=" { RTOKEN(RSHIFTEQ);} | |
326 | "||" { RTOKEN(OROR);} | |
327 | "==" { RTOKEN(EQ);} | |
328 | "!=" { RTOKEN(NE);} | |
329 | ">=" { RTOKEN(GE);} | |
330 | "<=" { RTOKEN(LE);} | |
331 | "<<" { RTOKEN(LSHIFT);} | |
332 | ">>" { RTOKEN(RSHIFT);} | |
333 | "+=" { RTOKEN(PLUSEQ);} | |
334 | "-=" { RTOKEN(MINUSEQ);} | |
335 | "*=" { RTOKEN(MULTEQ);} | |
336 | "/=" { RTOKEN(DIVEQ);} | |
337 | "&=" { RTOKEN(ANDEQ);} | |
338 | "|=" { RTOKEN(OREQ);} | |
2fa0b342 DHW |
339 | "&&" { RTOKEN(ANDAND);} |
340 | ">" { RTOKEN('>');} | |
341 | "," { RTOKEN(',');} | |
342 | "&" { RTOKEN('&');} | |
343 | "|" { RTOKEN('|');} | |
344 | "~" { RTOKEN('~');} | |
345 | "!" { RTOKEN('!');} | |
346 | "?" { RTOKEN('?');} | |
347 | "*" { RTOKEN('*');} | |
348 | "%" { RTOKEN('%');} | |
349 | "<" { RTOKEN('<');} | |
2fa0b342 DHW |
350 | ">" { RTOKEN('>');} |
351 | "}" { RTOKEN('}') ; } | |
352 | "{" { RTOKEN('{'); } | |
353 | ")" { RTOKEN(')');} | |
354 | "(" { RTOKEN('(');} | |
355 | "]" { RTOKEN(']');} | |
356 | "[" { RTOKEN('[');} | |
357 | ":" { RTOKEN(':'); } | |
fd846434 | 358 | ";" { RTOKEN('\;');} |
2fa0b342 | 359 | "-" { RTOKEN('-');} |
1418c83b | 360 | |
2fa0b342 DHW |
361 | |
362 | ||
363 | "/*" { | |
364 | while (1) { | |
365 | int ch; | |
366 | ch = input(); | |
367 | while (ch != '*') { | |
2fa0b342 DHW |
368 | ch = input(); |
369 | } | |
370 | ||
371 | ||
372 | ||
373 | if (input() == '/') { | |
374 | break; | |
375 | } | |
376 | unput(yytext[yyleng-1]); | |
377 | } | |
378 | } | |
379 | ||
380 | "\""[^\"]*"\"" { | |
381 | ||
382 | yylval.name = buystring(yytext+1); | |
383 | yylval.name[yyleng-2] = 0; /* Fry final quote */ | |
384 | return NAME; | |
385 | } | |
386 | [0][0-7KM]* { | |
387 | ||
388 | yylval.integer = number(yytext+1, 8); | |
389 | return INT; | |
390 | } | |
391 | ||
392 | [0-9]+[KM]? { | |
070aa819 | 393 | if (hex_mode == true || ldgram_in_defsym == true) { |
2fa0b342 DHW |
394 | yylval.integer = number(yytext, 16); |
395 | } | |
396 | else { | |
397 | yylval.integer = number(yytext, 10); | |
398 | } | |
399 | return INT; | |
400 | } | |
401 | ||
402 | 0[Xx][0-9a-fA-FKM]+ { | |
403 | ||
404 | yylval.integer = number(yytext+2,16); | |
405 | return INT; | |
406 | } | |
407 | ||
408 | "\#"{WHITE}*{FILENAMECHAR}+ { | |
409 | char *p = yytext+1; | |
410 | while(*p ==' ' || *p == '\t') p++; | |
411 | yylval.name = buystring(p); | |
412 | return NAME; | |
413 | } | |
7ca04d28 | 414 | {FILENAMECHAR} { |
2fa0b342 | 415 | |
7ca04d28 SC |
416 | boolean loop = false; |
417 | /* | |
418 | Tokenize a name, this is really pain, since a name can be a | |
419 | filename or a symbol name. filenames have slashes and stuff whist | |
420 | in an expression those things are seperate tokens. We hack this by | |
1418c83b | 421 | setting ldlang_in_script when we are expecting a symbol, so that |
7ca04d28 SC |
422 | [/+-] get taken to be seperate tokens. An extra gotcha is |
423 | expressions after defsyms, we only allow +s and -s in a defsym | |
424 | expression, so -defsym foo=bar+9 /file.o is parsed ok. | |
425 | ||
1418c83b SC |
426 | The more I think about this the more I hate it. I've got a problem |
427 | now with the = sign, what should I do ? imagine: | |
428 | __start=.; | |
429 | You'd think that was pretty unambiguous wouldn't you. Well it's | |
430 | not since __start=. is (at the moment) a perfectly valid | |
431 | filename. And in some cases we don't know what's going on. I'm | |
432 | going to have to hack this. If we see a '/' before the = sign then | |
433 | we say we've got an = in a filename, otherwise it's an operator. | |
434 | (later) | |
435 | That's it, I've had enough. From now on, an =s on a command line | |
436 | will be taken to be part of a file name unless its in a defsym, | |
437 | and an = in a file will be taken to be an operator. | |
7ca04d28 SC |
438 | */ |
439 | int ch; | |
2fa0b342 | 440 | keyword_type *k; |
1418c83b SC |
441 | |
442 | if (hex_mode) { | |
443 | ch = yytext[0]; | |
444 | /* Then always read a number */ | |
445 | while (isxdigit(ch)) { | |
446 | yytext[yyleng++] = ch; | |
447 | ch = input(); | |
448 | } | |
449 | yytext[yyleng] = 0; | |
450 | unput(ch); | |
451 | ||
452 | yylval.integer = number(yytext,16); | |
453 | return INT; | |
454 | } | |
455 | ||
456 | if (ldfile_input_filename) { | |
457 | /* We're inside a file */ | |
458 | if (yytext[0]== '=') { | |
459 | RTOKEN('='); | |
460 | } | |
461 | } | |
462 | ||
463 | ||
464 | ||
465 | /* Otherwise we only notice special things if were in an | |
466 | expression */ | |
467 | ||
7ca04d28 SC |
468 | if (ldgram_in_expression) { |
469 | if (yytext[0] != '/' || ldgram_in_defsym == false) { | |
470 | switch (yytext[0]) { | |
471 | case '/': RTOKEN('/'); | |
1418c83b | 472 | case '=': RTOKEN('='); |
7ca04d28 SC |
473 | case '+': RTOKEN('+'); |
474 | case '-': RTOKEN('-'); | |
475 | } | |
2fa0b342 | 476 | } |
2fa0b342 DHW |
477 | } |
478 | ||
7ca04d28 SC |
479 | ch = input(); |
480 | while (true) | |
481 | { | |
1418c83b | 482 | if (isalpha(ch) || isdigit(ch) || ch == '.' || ch == '_' ) { |
7ca04d28 SC |
483 | yytext[yyleng++] = ch; |
484 | } | |
1418c83b SC |
485 | else if (ch == '=' && ldgram_in_script) { |
486 | /* An = within a script is always taken to be an operator */ | |
487 | break; | |
488 | } | |
489 | else if (ch == '+' || ch == '-' || ch == '/' || ch == '=') { | |
7ca04d28 SC |
490 | if (ldgram_in_expression) break; |
491 | yytext[yyleng++] = ch; | |
492 | } | |
493 | else | |
494 | break; | |
495 | ch = input(); | |
496 | } | |
497 | ||
2fa0b342 DHW |
498 | yytext[yyleng] = 0; |
499 | unput(ch); | |
1418c83b SC |
500 | /* Filenames of just =signs are tokens */ |
501 | if (yyleng == 1 && yytext[0] == '=') { | |
502 | RTOKEN('='); | |
503 | } | |
2fa0b342 DHW |
504 | for(k = keywords; k ->name != (char *)NULL; k++) { |
505 | ||
506 | if (strcmp(k->name, yytext)==0) { | |
507 | yylval.token = k->value; | |
508 | return k->value; | |
509 | } | |
510 | } | |
511 | yylval.name = buystring(yytext); | |
512 | return NAME; | |
513 | } | |
514 | ||
515 | ||
516 | ||
517 | ||
518 | ||
519 | %% |