3 /* Copyright (C) 1991 Free Software Foundation, Inc.
5 This file is part of GLD, the Gnu Linker.
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 This was written by steve chamberlain
32 extern int ldgram_had_equals;
33 extern int ldgram_in_script;
43 unsigned int lineno = 1;
49 #define YY_INPUT(buf,result,max_size) yy_input(buf, &result, max_size)
51 #define YY_FATAL_ERROR ;
52 #define MAX_INCLUDE_DEPTH 10
53 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
54 char *file_name_stack[MAX_INCLUDE_DEPTH];
55 unsigned int include_stack_ptr = 0;
59 COMMAND on command line
60 COMMENT in a C comment
61 EXPRESSION definiatelyt in an expression
62 SCRIPT definately in a script
63 SOMEWHERE either EXPRESSION or SCRIPT
66 #define RTOKEN(x) { yylval.token = x; return x; }
72 CMDFILENAMECHAR [_a-zA-Z0-9\/\.\\_\+\$\:\[\]\\\,\=\&\!\<\>\-\~]
73 CMDFILENAMECHAR1 [_a-zA-Z0-9\/\.\\_\+\$\:\[\]\\\,\=\&\!\<\>\~]
74 FILENAMECHAR1 [_a-zA-Z\/\.\\\$\_\~]
75 SYMBOLCHARN [_a-zA-Z\/\.\\0-9]
76 FILENAMECHAR [_a-zA-Z0-9\/\.\-\_\+\=\$\:\[\]\\\,\~]
77 FILENAME {FILENAMECHAR}+
80 NOCFILENAMECHAR [_a-zA-Z0-9\/\.\-\_\+\$\:\[\]\\\~]
93 <BOTH,SCRIPT,EXPRESSION,COMMAND>"/*" { comment(); }
96 <DEFSYMEXP>"-" { RTOKEN('-');}
97 <DEFSYMEXP>"+" { RTOKEN('+');}
98 <DEFSYMEXP>{FILENAMECHAR1}{SYMBOLCHARN}* { yylval.name = buystring(yytext); return NAME; }
99 <DEFSYMEXP>[ \t] { RTOKEN(DEFSYMEND); }
100 <DEFSYMEXP>"=" { RTOKEN('='); }
101 <COMMAND>"--help" { return OPTION_help; }
102 <COMMAND>"--version" { return OPTION_version; }
103 <COMMAND>"-defsym"{WHITE}* { return OPTION_defsym; }
104 <COMMAND>"-format" { return OPTION_format; }
105 <COMMAND>"-noinhibit-exec" { return OPTION_noinhibit_exec; }
106 <COMMAND>"-noinhibit_exec" { return OPTION_noinhibit_exec; }
107 <COMMAND>"-oformat" { return OPTION_oformat; }
108 <COMMAND>"-sort-common" { return OPTION_sort_common;}
109 <COMMAND>"-sort_common" { return OPTION_sort_common;}
110 <COMMAND>"-warn-common" { return OPTION_warn_common;}
111 <COMMAND>"-n" { return OPTION_n; }
112 <COMMAND>"-N" { return OPTION_N; }
113 <COMMAND>"-r" { return OPTION_r; }
114 <COMMAND>"-relax" { return OPTION_relax; }
115 <COMMAND>"-i" { return OPTION_r; }
116 <COMMAND>"-Ur" { return OPTION_Ur; }
117 <COMMAND>"-o" { return OPTION_o; }
118 <COMMAND>"-g" { return OPTION_g; }
119 <COMMAND>"-e" { return OPTION_e; }
120 <COMMAND>"-b" { return OPTION_b; }
121 <COMMAND>"-dc" { return OPTION_dc; }
122 <COMMAND>"-dp" { return OPTION_dp; }
123 <COMMAND>"-d" { return OPTION_d; }
124 <COMMAND>"-v" { return OPTION_v; }
125 <COMMAND>"-V" { return OPTION_V; }
126 <COMMAND>"-m" { return OPTION_m; }
127 <COMMAND>"-m"{FILENAME} { return OPTION_memul; }
128 <COMMAND>"-M" { return OPTION_M; }
129 <COMMAND>"-Map" { return OPTION_Map;}
130 <COMMAND>"-t" { return OPTION_t; }
131 <COMMAND>"-X" { return OPTION_X; }
132 <COMMAND>"-x" { return OPTION_x; }
133 <COMMAND>"-c" { return OPTION_c; }
134 <COMMAND>"-R" { return OPTION_R; }
135 <COMMAND>"-u" { return OPTION_u; }
136 <COMMAND>"-s" { return OPTION_s; }
137 <COMMAND>"-S" { return OPTION_S; }
138 <COMMAND>"-Bstat" { return OPTION_Bstatic; }
139 <COMMAND>"-B"{FILENAME} { /* Ignored */ }
140 <COMMAND>"-l"{FILENAME} {
141 yylval.name = buystring(yytext+2);
145 <COMMAND>"-L"{FILENAME} {
146 yylval.name = buystring(yytext+2);
149 <COMMAND>"-L" { return OPTION_L; }
151 yylval.name = ".text";
155 yylval.name = ".data";
159 yylval.name = ".bss";
162 <COMMAND>"-O"{FILENAME} {
163 yylval.name = buystring(yytext+2);
167 <COMMAND>"-T"{FILENAME} {
168 yylval.name = buystring(yytext+2);
175 <COMMAND>"-F"{FILENAME} {
182 <COMMAND>"-y"{FILENAME} {
183 yylval.name = buystring(yytext+2);
187 <COMMAND>"-A"{FILENAME} {
188 yylval.name = buystring(yytext+2);
192 <COMMAND>"-retain-symbols-file" { return OPTION_RETAIN_SYMBOLS_FILE; }
203 <COMMAND>"-G"([0-9])+ {
204 yylval.integer = atoi (yytext + 2);
208 <COMMAND>"-Qy" { return OPTION_Qy; }
209 <COMMAND>"-dn" { return OPTION_dn; }
210 <COMMAND>"-Y" { return OPTION_Y; }
211 <COMMAND>"-YP,"{FILENAME} {
212 yylval.name = buystring (yytext+4);
216 <MRI,EXPRESSION>"$"([0-9A-Fa-f])+ {
217 yylval.integer = strtoul(yytext+1, 0,16);
221 <MRI,EXPRESSION>([0-9A-Fa-f])+(H|X|B|O|D) {
223 switch (yytext[yyleng-1]) {
237 yylval.integer = strtoul(yytext+1, 0, base);
240 <SCRIPT,DEFSYMEXP,MRI,BOTH,EXPRESSION>"$"?"0x"?([0-9A-Fa-f])+(M|K|m|k)? {
241 yylval.integer = strtoul(yytext,0,hex_mode);
242 if (yytext[yyleng-1]=='M'
243 || yytext[yyleng-1] == 'm') {
244 yylval.integer *= 1024*1024;
246 if (yytext[yyleng-1]=='K'
247 || yytext[yyleng-1]=='k') {
248 yylval.integer *= 1024;
252 <BOTH,SCRIPT,EXPRESSION>"]" { RTOKEN(']');}
253 <BOTH,SCRIPT,EXPRESSION>"[" { RTOKEN('[');}
254 <BOTH,SCRIPT,EXPRESSION>"<<=" { RTOKEN(LSHIFTEQ);}
255 <BOTH,SCRIPT,EXPRESSION>">>=" { RTOKEN(RSHIFTEQ);}
256 <BOTH,SCRIPT,EXPRESSION>"||" { RTOKEN(OROR);}
257 <BOTH,SCRIPT,EXPRESSION>"==" { RTOKEN(EQ);}
258 <BOTH,SCRIPT,EXPRESSION>"!=" { RTOKEN(NE);}
259 <BOTH,SCRIPT,EXPRESSION>">=" { RTOKEN(GE);}
260 <BOTH,SCRIPT,EXPRESSION>"<=" { RTOKEN(LE);}
261 <BOTH,SCRIPT,EXPRESSION>"<<" { RTOKEN(LSHIFT);}
262 <BOTH,SCRIPT,EXPRESSION>">>" { RTOKEN(RSHIFT);}
263 <BOTH,SCRIPT,EXPRESSION>"+=" { RTOKEN(PLUSEQ);}
264 <BOTH,SCRIPT,EXPRESSION>"-=" { RTOKEN(MINUSEQ);}
265 <BOTH,SCRIPT,EXPRESSION>"*=" { RTOKEN(MULTEQ);}
266 <BOTH,SCRIPT,EXPRESSION>"/=" { RTOKEN(DIVEQ);}
267 <BOTH,SCRIPT,EXPRESSION>"&=" { RTOKEN(ANDEQ);}
268 <BOTH,SCRIPT,EXPRESSION>"|=" { RTOKEN(OREQ);}
269 <BOTH,SCRIPT,EXPRESSION>"&&" { RTOKEN(ANDAND);}
270 <BOTH,SCRIPT,EXPRESSION>">" { RTOKEN('>');}
271 <MRI,BOTH,SCRIPT,EXPRESSION>"," { RTOKEN(',');}
272 <BOTH,SCRIPT,EXPRESSION>"&" { RTOKEN('&');}
273 <BOTH,SCRIPT,EXPRESSION>"|" { RTOKEN('|');}
274 <BOTH,SCRIPT,EXPRESSION>"~" { RTOKEN('~');}
275 <BOTH,SCRIPT,EXPRESSION>"!" { RTOKEN('!');}
276 <BOTH,SCRIPT,EXPRESSION>"?" { RTOKEN('?');}
277 <BOTH,SCRIPT,EXPRESSION>"*" { RTOKEN('*');}
278 <BOTH,SCRIPT,EXPRESSION>"+" { RTOKEN('+');}
279 <BOTH,SCRIPT,EXPRESSION>"-" { RTOKEN('-');}
280 <BOTH,SCRIPT,EXPRESSION>"/" { RTOKEN('/');}
281 <BOTH,SCRIPT,EXPRESSION>"%" { RTOKEN('%');}
282 <BOTH,SCRIPT,EXPRESSION>"<" { RTOKEN('<');}
283 <BOTH,SCRIPT,EXPRESSION>">" { RTOKEN('>');}
284 <MRI,BOTH,SCRIPT,EXPRESSION>"=" { RTOKEN('=');}
285 <BOTH,SCRIPT,EXPRESSION>"}" { RTOKEN('}') ; }
286 <BOTH,SCRIPT,EXPRESSION>"{" { RTOKEN('{'); }
287 <BOTH,SCRIPT,EXPRESSION>")" { RTOKEN(')');}
288 <BOTH,SCRIPT,EXPRESSION>"(" { RTOKEN('(');}
289 <BOTH,SCRIPT,EXPRESSION>"]" { RTOKEN(']');}
290 <BOTH,SCRIPT,EXPRESSION>"[" { RTOKEN('[');}
291 <BOTH,SCRIPT,EXPRESSION>":" { RTOKEN(':'); }
292 <BOTH,SCRIPT,EXPRESSION>";" { RTOKEN(';');}
293 <BOTH,SCRIPT,EXPRESSION>"-" { RTOKEN('-');}
294 <BOTH,SCRIPT,EXPRESSION>"/" { RTOKEN('/');}
295 <BOTH,SCRIPT>"MEMORY" { RTOKEN(MEMORY);}
296 <BOTH,SCRIPT>"ORIGIN" { RTOKEN(ORIGIN);}
297 <BOTH,SCRIPT>"BLOCK" { RTOKEN(BLOCK);}
298 <BOTH,SCRIPT>"LENGTH" { RTOKEN(LENGTH);}
299 <EXPRESSION,BOTH,SCRIPT>"ALIGN" { RTOKEN(ALIGN_K);}
300 <EXPRESSION,BOTH,SCRIPT>"ADDR" { RTOKEN(ADDR);}
301 <BOTH,SCRIPT>"ENTRY" { RTOKEN(ENTRY);}
302 <EXPRESSION,BOTH,SCRIPT>"NEXT" { RTOKEN(NEXT);}
303 <EXPRESSION,BOTH,SCRIPT>"sizeof_headers" { RTOKEN(SIZEOF_HEADERS);}
304 <EXPRESSION,BOTH,SCRIPT>"SIZEOF_HEADERS" { RTOKEN(SIZEOF_HEADERS);}
305 <BOTH,SCRIPT>"MAP" { RTOKEN(MAP);}
306 <EXPRESSION,BOTH,SCRIPT>"SIZEOF" { RTOKEN(SIZEOF);}
307 <BOTH,SCRIPT>"TARGET" { RTOKEN(TARGET_K);}
308 <BOTH,SCRIPT>"SEARCH_DIR" { RTOKEN(SEARCH_DIR);}
309 <BOTH,SCRIPT>"OUTPUT" { RTOKEN(OUTPUT);}
310 <BOTH,SCRIPT>"INPUT" { RTOKEN(INPUT);}
311 <EXPRESSION,BOTH,SCRIPT>"DEFINED" { RTOKEN(DEFINED);}
312 <BOTH,SCRIPT>"CREATE_OBJECT_SYMBOLS" { RTOKEN(CREATE_OBJECT_SYMBOLS);}
313 <BOTH,SCRIPT>"CONSTRUCTORS" { RTOKEN( CONSTRUCTORS);}
314 <BOTH,SCRIPT>"FORCE_COMMON_ALLOCATION" { RTOKEN(FORCE_COMMON_ALLOCATION);}
315 <BOTH,SCRIPT>"SECTIONS" { RTOKEN(SECTIONS);}
316 <BOTH,SCRIPT>"FILL" { RTOKEN(FILL);}
317 <BOTH,SCRIPT>"STARTUP" { RTOKEN(STARTUP);}
318 <BOTH,SCRIPT>"OUTPUT_FORMAT" { RTOKEN(OUTPUT_FORMAT);}
319 <BOTH,SCRIPT>"OUTPUT_ARCH" { RTOKEN( OUTPUT_ARCH);}
320 <BOTH,SCRIPT>"HLL" { RTOKEN(HLL);}
321 <BOTH,SCRIPT>"SYSLIB" { RTOKEN(SYSLIB);}
322 <BOTH,SCRIPT>"FLOAT" { RTOKEN(FLOAT);}
323 <BOTH,SCRIPT>"LONG" { RTOKEN( LONG);}
324 <BOTH,SCRIPT>"SHORT" { RTOKEN( SHORT);}
325 <BOTH,SCRIPT>"BYTE" { RTOKEN( BYTE);}
326 <BOTH,SCRIPT>"NOFLOAT" { RTOKEN(NOFLOAT);}
327 <EXPRESSION,BOTH,SCRIPT>"NOLOAD" { RTOKEN(NOLOAD);}
328 <BOTH,SCRIPT>"DSECT" { RTOKEN(DSECT);}
329 <BOTH,SCRIPT>"COPY" { RTOKEN(COPY);}
330 <BOTH,SCRIPT>"INFO" { RTOKEN(INFO);}
331 <BOTH,SCRIPT>"OVERLAY" { RTOKEN(OVERLAY);}
332 <BOTH,SCRIPT>"o" { RTOKEN(ORIGIN);}
333 <BOTH,SCRIPT>"org" { RTOKEN(ORIGIN);}
334 <BOTH,SCRIPT>"l" { RTOKEN( LENGTH);}
335 <BOTH,SCRIPT>"len" { RTOKEN( LENGTH);}
336 <BOTH,SCRIPT>"INCLUDE" { RTOKEN(INCLUDE);}
337 <EXPRESSION,BOTH,SCRIPT>"AT" { RTOKEN(AT);}
338 <MRI>"\n" { ++ lineno; RTOKEN(NEWLINE); }
339 <MRI>"*".* { /* Mri comment line */ }
340 <MRI>"END" { RTOKEN(ENDWORD); }
341 <MRI>"ALIGNMOD" { RTOKEN(ALIGNMOD);}
342 <MRI>"ALIGN" { RTOKEN(ALIGN_K);}
344 <MRI>"CHIP" { RTOKEN(CHIP); }
345 <MRI>"BASE" { RTOKEN(BASE); }
346 <MRI>"ALIAS" { RTOKEN(ALIAS); }
347 <MRI>"TRUNCATE" { RTOKEN(TRUNCATE); }
348 <MRI>"LOAD" { RTOKEN(LOAD); }
349 <MRI>"PUBLIC" { RTOKEN(PUBLIC); }
350 <MRI>"ORDER" { RTOKEN(ORDER); }
351 <MRI>"NAME" { RTOKEN(NAMEWORD); }
352 <MRI>"FORMAT" { RTOKEN(FORMAT); }
353 <MRI>"LIST".* { RTOKEN(LIST); /* LIST and ignore to end of line */ }
354 <MRI>"SECT" { RTOKEN(SECT); }
355 <EXPRESSION,BOTH,SCRIPT,MRI>"ABSOLUTE" { RTOKEN(ABSOLUTE); }
356 <MRI>"end" { RTOKEN(ENDWORD); }
357 <MRI>"chip" { RTOKEN(CHIP); }
358 <MRI>"load" { RTOKEN(LOAD); }
359 <MRI>"order" { RTOKEN(ORDER); }
360 <MRI>"name" { RTOKEN(NAMEWORD); }
361 <MRI>"format" { RTOKEN(FORMAT); }
362 <MRI>"list".* { RTOKEN(LIST); /* LIST and ignore to end of line */ }
363 <MRI>"sect" { RTOKEN(SECT); }
364 <EXPRESSION,BOTH,SCRIPT,MRI>"absolute" { RTOKEN(ABSOLUTE); }
366 <COMMAND>{CMDFILENAMECHAR1}{CMDFILENAMECHAR}* {
367 yylval.name = buystring(yytext);
372 <MRI>{FILENAMECHAR1}{NOCFILENAMECHAR}* {
373 /* Filename without commas, needed to parse mri stuff */
374 yylval.name = buystring(yytext);
379 <BOTH,EXPRESSION>{FILENAMECHAR1}{FILENAMECHAR}* {
380 yylval.name = buystring(yytext);
383 <SCRIPT,COMMAND>{FILENAMECHAR}* { yylval.name = buystring(yytext);
387 <EXPRESSION,BOTH,COMMAND,SCRIPT>"\""[^\"]*"\"" {
388 /* No matter the state, quotes
389 give what's inside */
390 yylval.name = buystring(yytext+1);
391 yylval.name[yyleng-2] = 0;
394 <BOTH,SCRIPT,EXPRESSION>"\n" { lineno++;}
395 <MRI,COMMAND,BOTH,SCRIPT,EXPRESSION>[ \t]
398 extern char *ldfile_input_filename;
401 if (include_stack_ptr == 0)
407 yy_switch_to_buffer(include_stack[include_stack_ptr]);
410 if (include_stack_ptr==1)
417 ldfile_input_filename = file_name_stack[include_stack_ptr-1];
426 lex_push_file (file, name)
430 if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
432 einfo("%F:includes nested too deeply");
434 file_name_stack[include_stack_ptr] = name;
435 include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
439 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
445 yy_create_string_buffer (string, size)
451 b = (YY_BUFFER_STATE) malloc( sizeof( struct yy_buffer_state ) );
452 b->yy_input_file = 0;
455 YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
457 b->yy_buf_size = size;
459 /* yy_ch_buf has to be 2 characters longer than the size given because
460 * we need to put in 2 end-of-buffer characters.
462 b->yy_ch_buf = (YY_CHAR *) malloc( (unsigned) (b->yy_buf_size + 3) );
464 if ( ! b->yy_ch_buf )
465 YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
468 b->yy_ch_buf[0] = '\n';
469 strcpy(b->yy_ch_buf+1, string);
470 b->yy_ch_buf[size+1] = YY_END_OF_BUFFER_CHAR;
471 b->yy_ch_buf[size+2] = YY_END_OF_BUFFER_CHAR;
472 b->yy_n_chars = size+1;
473 b->yy_buf_pos = &b->yy_ch_buf[1];
475 b->yy_eof_status = EOF_NOT_SEEN;
483 lex_redirect (string)
488 int len = strlen(string);
490 if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
492 einfo("%F: macros nested too deeply");
494 file_name_stack[include_stack_ptr] = "redirect";
495 include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
497 tmp = yy_create_string_buffer(string, len);
499 yy_switch_to_buffer(tmp);
505 int *state_stack_p = state_stack;
510 *(state_stack_p)++ = yy_start;
519 *(state_stack_p)++ = yy_start;
526 *(state_stack_p)++ = yy_start;
533 *(state_stack_p)++ = yy_start;
540 *(state_stack_p)++ = yy_start;
546 *(state_stack_p)++ = yy_start;
553 yy_start = *(--state_stack_p);
556 yy_input(buf, result, max_size)
562 if (yy_current_buffer->yy_input_file)
565 if ( (*result = read( fileno(yyin), (char *) buf, max_size )) < 0 )
566 YY_FATAL_ERROR( "read() in flex scanner failed" );
577 while (c !='*' && c != EOF)
579 if (c == '\n') lineno++;
590 break; /* found the end */
595 einfo( "%F%P :EOF in comment");