2 /* Copyright (C) 1991 Free Software Foundation, Inc.
4 This file is part of GLD, the Gnu Linker.
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)
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.
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. */
40 #include "ldgram.tab.h"
45 #define input lex_input
46 #define unput lex_unput
49 extern boolean ldgram_in_expression;
50 extern boolean ldgram_in_defsym;
51 extern boolean ldgram_in_script;
52 static char *command_line;
61 #define RTOKEN(x) { yylval.token = x; return x; }
62 keyword_type keywords[] =
69 "SUBSECTION_ALIGN",SUBSECTION_ALIGN,
73 "ENDSCRIPT", ENDSCRIPT,
78 "SEARCH_DIR",SEARCH_DIR,
82 "CREATE_OBJECT_SYMBOLS",CREATE_OBJECT_SYMBOLS,
99 extern boolean hex_mode;
100 FILE *ldlex_input_stack;
101 static unsigned int have_pushback;
103 int pushback[NPUSHBACK];
105 extern char *ldfile_input_filename;
110 if (have_pushback > 0)
113 return thischar = pushback[have_pushback];
115 if (ldlex_input_stack) {
116 thischar = fgetc(ldlex_input_stack);
118 if (thischar == EOF) {
119 fclose(ldlex_input_stack);
120 ldlex_input_stack = (FILE *)NULL;
121 ldfile_input_filename = (char *)NULL;
122 /* First char after script eof is a @ so that we can tell the grammer
128 else if (command_line && *command_line) {
129 thischar = *(command_line++);
134 if(thischar == '\t') thischar = ' ';
135 if (thischar == '\n') { thischar = ' '; lineno++; }
143 if (have_pushback > NPUSHBACK) {
144 info("%F%P Too many pushbacks\n");
147 pushback[have_pushback] = c;
161 fprintf(yyout,"%d",x);
168 fprintf(yyout,"%s",x);
193 for (i= 1; i < ac; i++) {
194 size += strlen(av[i]) + 2;
196 dst = p = (char *)ldmalloc(size + 2);
197 /* Put a space arount each option */
200 for (i =1; i < ac; i++) {
202 unsigned int s = strlen(av[i]);
204 memcpy(dst, av[i], s);
216 long number(text, base)
222 for (p = text; *p != 0; p++) {
234 else if (islower(*p)) {
248 FILENAMECHAR [a-zA-Z0-9\/\.\-\_\+\=]
249 FILENAME {FILENAMECHAR}+
256 "@" { return ENDSCRIPT; }
257 "\ -defsym\ " { return OPTION_defsym; }
258 "\ -noinhibit_exec\ " { return OPTION_noinhibit_exec; }
259 "\ -format\ " { return OPTION_format; }
260 "\ -n\ " { return OPTION_n; }
261 "\ -r\ " { return OPTION_r; }
262 "\ -Ur\ " { return OPTION_Ur; }
263 "\ -o\ " { return OPTION_o; }
264 "\ -g\ " { return OPTION_g; }
265 "\ -e\ " { return OPTION_e; }
266 "\ -b\ " { return OPTION_b; }
267 "\ -dc\ " { return OPTION_dc; }
268 "\ -dp\ " { return OPTION_dp; }
269 "\ -d\ " { return OPTION_d; }
270 "\ -v\ " { return OPTION_v; }
271 "\ -M\ " { return OPTION_M; }
272 "\ -t\ " { return OPTION_t; }
273 "\ -X\ " { return OPTION_X; }
274 "\ -x\ " { return OPTION_x; }
275 "\ -c\ " { return OPTION_c; }
276 "\ -R\ " { return OPTION_R; }
277 "\ -u\ " { return OPTION_u; }
278 "\ -s\ " { return OPTION_s; }
279 "\ -S\ " { return OPTION_S; }
281 yylval.name = buystring(yytext+3);
286 yylval.name = buystring(yytext+3);
290 yylval.name = ".text";
294 yylval.name = ".data";
298 yylval.name = ".bss";
303 yylval.name = buystring(yytext+3);
318 yylval.name = buystring(yytext+3);
322 "<<=" { RTOKEN(LSHIFTEQ);}
323 ">>=" { RTOKEN(RSHIFTEQ);}
324 "||" { RTOKEN(OROR);}
329 "<<" { RTOKEN(LSHIFT);}
330 ">>" { RTOKEN(RSHIFT);}
331 "+=" { RTOKEN(PLUSEQ);}
332 "-=" { RTOKEN(MINUSEQ);}
333 "*=" { RTOKEN(MULTEQ);}
334 "/=" { RTOKEN(DIVEQ);}
335 "&=" { RTOKEN(ANDEQ);}
336 "|=" { RTOKEN(OREQ);}
337 "&&" { RTOKEN(ANDAND);}
349 "}" { RTOKEN('}') ; }
371 if (input() == '/') {
374 unput(yytext[yyleng-1]);
380 yylval.name = buystring(yytext+1);
381 yylval.name[yyleng-2] = 0; /* Fry final quote */
386 yylval.integer = number(yytext+1, 8);
391 if (hex_mode == true) {
392 yylval.integer = number(yytext, 16);
395 yylval.integer = number(yytext, 10);
400 0[Xx][0-9a-fA-FKM]+ {
402 yylval.integer = number(yytext+2,16);
406 "\#"{WHITE}*{FILENAMECHAR}+ {
408 while(*p ==' ' || *p == '\t') p++;
409 yylval.name = buystring(p);
414 boolean loop = false;
416 Tokenize a name, this is really pain, since a name can be a
417 filename or a symbol name. filenames have slashes and stuff whist
418 in an expression those things are seperate tokens. We hack this by
419 setting ldlang_in_script when we are expecting a symbol, so that
420 [/+-] get taken to be seperate tokens. An extra gotcha is
421 expressions after defsyms, we only allow +s and -s in a defsym
422 expression, so -defsym foo=bar+9 /file.o is parsed ok.
424 The more I think about this the more I hate it. I've got a problem
425 now with the = sign, what should I do ? imagine:
427 You'd think that was pretty unambiguous wouldn't you. Well it's
428 not since __start=. is (at the moment) a perfectly valid
429 filename. And in some cases we don't know what's going on. I'm
430 going to have to hack this. If we see a '/' before the = sign then
431 we say we've got an = in a filename, otherwise it's an operator.
433 That's it, I've had enough. From now on, an =s on a command line
434 will be taken to be part of a file name unless its in a defsym,
435 and an = in a file will be taken to be an operator.
442 /* Then always read a number */
443 while (isxdigit(ch)) {
444 yytext[yyleng++] = ch;
450 yylval.integer = number(yytext,16);
454 if (ldfile_input_filename) {
455 /* We're inside a file */
456 if (yytext[0]== '=') {
463 /* Otherwise we only notice special things if were in an
466 if (ldgram_in_expression) {
467 if (yytext[0] != '/' || ldgram_in_defsym == false) {
469 case '/': RTOKEN('/');
470 case '=': RTOKEN('=');
471 case '+': RTOKEN('+');
472 case '-': RTOKEN('-');
480 if (isalpha(ch) || isdigit(ch) || ch == '.' || ch == '_' ) {
481 yytext[yyleng++] = ch;
483 else if (ch == '=' && ldgram_in_script) {
484 /* An = within a script is always taken to be an operator */
487 else if (ch == '+' || ch == '-' || ch == '/' || ch == '=') {
488 if (ldgram_in_expression) break;
489 yytext[yyleng++] = ch;
498 /* Filenames of just =signs are tokens */
499 if (yyleng == 1 && yytext[0] == '=') {
502 for(k = keywords; k ->name != (char *)NULL; k++) {
504 if (strcmp(k->name, yytext)==0) {
505 yylval.token = k->value;
509 yylval.name = buystring(yytext);