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 int ldgram_had_equals;
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>"-defsym"{WHITE}* { return OPTION_defsym; }
102 <COMMAND>"-noinhibit_exec" { return OPTION_noinhibit_exec; }
103 <COMMAND>"-noinhibit-exec" { return OPTION_noinhibit_exec; }
104 <COMMAND>"-sort_common" { return OPTION_sort_common;}
105 <COMMAND>"-sort-common" { return OPTION_sort_common;}
106 <COMMAND>"-format" { return OPTION_format; }
107 <COMMAND>"-n" { return OPTION_n; }
108 <COMMAND>"-N" { return OPTION_N; }
109 <COMMAND>"-r" { return OPTION_r; }
110 <COMMAND>"-relax" { return OPTION_relax; }
111 <COMMAND>"-i" { return OPTION_r; }
112 <COMMAND>"-Ur" { return OPTION_Ur; }
113 <COMMAND>"-o" { return OPTION_o; }
114 <COMMAND>"-g" { return OPTION_g; }
115 <COMMAND>"-e" { return OPTION_e; }
116 <COMMAND>"-b" { return OPTION_b; }
117 <COMMAND>"-dc" { return OPTION_dc; }
118 <COMMAND>"-dp" { return OPTION_dp; }
119 <COMMAND>"-d" { return OPTION_d; }
120 <COMMAND>"-v" { return OPTION_v; }
121 <COMMAND>"-V" { return OPTION_V; }
122 <COMMAND>"-M" { return OPTION_M; }
123 <COMMAND>"-Map" { return OPTION_Map;}
124 <COMMAND>"-t" { return OPTION_t; }
125 <COMMAND>"-X" { return OPTION_X; }
126 <COMMAND>"-x" { return OPTION_x; }
127 <COMMAND>"-c" { return OPTION_c; }
128 <COMMAND>"-R" { return OPTION_R; }
129 <COMMAND>"-u" { return OPTION_u; }
130 <COMMAND>"-s" { return OPTION_s; }
131 <COMMAND>"-S" { return OPTION_S; }
132 <COMMAND>"-Bstat" { return OPTION_Bstatic; }
133 <COMMAND>"-B"{FILENAME} { /* Ignored */ }
134 <COMMAND>"-l"{FILENAME} {
135 yylval.name = buystring(yytext+2);
139 <COMMAND>"-L"{FILENAME} {
140 yylval.name = buystring(yytext+2);
144 yylval.name = ".text";
148 yylval.name = ".data";
152 yylval.name = ".bss";
155 <COMMAND>"-O"{FILENAME} {
156 yylval.name = buystring(yytext+2);
160 <COMMAND>"-T"{FILENAME} {
161 yylval.name = buystring(yytext+2);
168 <COMMAND>"-F"{FILENAME} {
175 <COMMAND>"-y"{FILENAME} {
176 yylval.name = buystring(yytext+2);
180 <COMMAND>"-A"{FILENAME} {
181 yylval.name = buystring(yytext+2);
185 <COMMAND>"-retain-symbols-file" { return OPTION_RETAIN_SYMBOLS_FILE; }
193 <MRI,EXPRESSION>"$"([0-9A-Fa-f])+ {
194 yylval.integer = strtoul(yytext+1, 0,16);
198 <MRI,EXPRESSION>([0-9A-Fa-f])+(H|X|B|O|D) {
200 switch (yytext[yyleng-1]) {
214 yylval.integer = strtoul(yytext+1, 0, base);
217 <SCRIPT,DEFSYMEXP,MRI,BOTH,EXPRESSION>"$"?"0x"?([0-9A-Fa-f])+(M|K|m|k)? {
218 yylval.integer = strtoul(yytext,0,hex_mode);
219 if (yytext[yyleng-1]=='M'
220 || yytext[yyleng-1] == 'm') {
221 yylval.integer *= 1024*1024;
223 if (yytext[yyleng-1]=='K'
224 || yytext[yyleng-1]=='k') {
225 yylval.integer *= 1024;
229 <BOTH,SCRIPT,EXPRESSION>"]" { RTOKEN(']');}
230 <BOTH,SCRIPT,EXPRESSION>"[" { RTOKEN('[');}
231 <BOTH,SCRIPT,EXPRESSION>"<<=" { RTOKEN(LSHIFTEQ);}
232 <BOTH,SCRIPT,EXPRESSION>">>=" { RTOKEN(RSHIFTEQ);}
233 <BOTH,SCRIPT,EXPRESSION>"||" { RTOKEN(OROR);}
234 <BOTH,SCRIPT,EXPRESSION>"==" { RTOKEN(EQ);}
235 <BOTH,SCRIPT,EXPRESSION>"!=" { RTOKEN(NE);}
236 <BOTH,SCRIPT,EXPRESSION>">=" { RTOKEN(GE);}
237 <BOTH,SCRIPT,EXPRESSION>"<=" { RTOKEN(LE);}
238 <BOTH,SCRIPT,EXPRESSION>"<<" { RTOKEN(LSHIFT);}
239 <BOTH,SCRIPT,EXPRESSION>">>" { RTOKEN(RSHIFT);}
240 <BOTH,SCRIPT,EXPRESSION>"+=" { RTOKEN(PLUSEQ);}
241 <BOTH,SCRIPT,EXPRESSION>"-=" { RTOKEN(MINUSEQ);}
242 <BOTH,SCRIPT,EXPRESSION>"*=" { RTOKEN(MULTEQ);}
243 <BOTH,SCRIPT,EXPRESSION>"/=" { RTOKEN(DIVEQ);}
244 <BOTH,SCRIPT,EXPRESSION>"&=" { RTOKEN(ANDEQ);}
245 <BOTH,SCRIPT,EXPRESSION>"|=" { RTOKEN(OREQ);}
246 <BOTH,SCRIPT,EXPRESSION>"&&" { RTOKEN(ANDAND);}
247 <BOTH,SCRIPT,EXPRESSION>">" { RTOKEN('>');}
248 <MRI,BOTH,SCRIPT,EXPRESSION>"," { RTOKEN(',');}
249 <BOTH,SCRIPT,EXPRESSION>"&" { RTOKEN('&');}
250 <BOTH,SCRIPT,EXPRESSION>"|" { RTOKEN('|');}
251 <BOTH,SCRIPT,EXPRESSION>"~" { RTOKEN('~');}
252 <BOTH,SCRIPT,EXPRESSION>"!" { RTOKEN('!');}
253 <BOTH,SCRIPT,EXPRESSION>"?" { RTOKEN('?');}
254 <BOTH,SCRIPT,EXPRESSION>"*" { RTOKEN('*');}
255 <BOTH,SCRIPT,EXPRESSION>"+" { RTOKEN('+');}
256 <BOTH,SCRIPT,EXPRESSION>"-" { RTOKEN('-');}
257 <BOTH,SCRIPT,EXPRESSION>"/" { RTOKEN('/');}
258 <BOTH,SCRIPT,EXPRESSION>"%" { RTOKEN('%');}
259 <BOTH,SCRIPT,EXPRESSION>"<" { RTOKEN('<');}
260 <BOTH,SCRIPT,EXPRESSION>">" { RTOKEN('>');}
261 <MRI,BOTH,SCRIPT,EXPRESSION>"=" { RTOKEN('=');}
262 <BOTH,SCRIPT,EXPRESSION>"}" { RTOKEN('}') ; }
263 <BOTH,SCRIPT,EXPRESSION>"{" { RTOKEN('{'); }
264 <BOTH,SCRIPT,EXPRESSION>")" { RTOKEN(')');}
265 <BOTH,SCRIPT,EXPRESSION>"(" { RTOKEN('(');}
266 <BOTH,SCRIPT,EXPRESSION>"]" { RTOKEN(']');}
267 <BOTH,SCRIPT,EXPRESSION>"[" { RTOKEN('[');}
268 <BOTH,SCRIPT,EXPRESSION>":" { RTOKEN(':'); }
269 <BOTH,SCRIPT,EXPRESSION>";" { RTOKEN(';');}
270 <BOTH,SCRIPT,EXPRESSION>"-" { RTOKEN('-');}
271 <BOTH,SCRIPT,EXPRESSION>"/" { RTOKEN('/');}
272 <BOTH,SCRIPT>"MEMORY" { RTOKEN(MEMORY);}
273 <BOTH,SCRIPT>"ORIGIN" { RTOKEN(ORIGIN);}
274 <BOTH,SCRIPT>"BLOCK" { RTOKEN(BLOCK);}
275 <BOTH,SCRIPT>"LENGTH" { RTOKEN(LENGTH);}
276 <EXPRESSION,BOTH,SCRIPT>"ALIGN" { RTOKEN(ALIGN_K);}
277 <EXPRESSION,BOTH,SCRIPT>"ADDR" { RTOKEN(ADDR);}
278 <BOTH,SCRIPT>"ENTRY" { RTOKEN(ENTRY);}
279 <EXPRESSION,BOTH,SCRIPT>"NEXT" { RTOKEN(NEXT);}
280 <EXPRESSION,BOTH,SCRIPT>"sizeof_headers" { RTOKEN(SIZEOF_HEADERS);}
281 <EXPRESSION,BOTH,SCRIPT>"SIZEOF_HEADERS" { RTOKEN(SIZEOF_HEADERS);}
282 <BOTH,SCRIPT>"MAP" { RTOKEN(MAP);}
283 <EXPRESSION,BOTH,SCRIPT>"SIZEOF" { RTOKEN(SIZEOF);}
284 <BOTH,SCRIPT>"TARGET" { RTOKEN(TARGET_K);}
285 <BOTH,SCRIPT>"SEARCH_DIR" { RTOKEN(SEARCH_DIR);}
286 <BOTH,SCRIPT>"OUTPUT" { RTOKEN(OUTPUT);}
287 <BOTH,SCRIPT>"INPUT" { RTOKEN(INPUT);}
288 <EXPRESSION,BOTH,SCRIPT>"DEFINED" { RTOKEN(DEFINED);}
289 <BOTH,SCRIPT>"CREATE_OBJECT_SYMBOLS" { RTOKEN(CREATE_OBJECT_SYMBOLS);}
290 <BOTH,SCRIPT>"CONSTRUCTORS" { RTOKEN( CONSTRUCTORS);}
291 <BOTH,SCRIPT>"FORCE_COMMON_ALLOCATION" { RTOKEN(FORCE_COMMON_ALLOCATION);}
292 <BOTH,SCRIPT>"SECTIONS" { RTOKEN(SECTIONS);}
293 <BOTH,SCRIPT>"FILL" { RTOKEN(FILL);}
294 <BOTH,SCRIPT>"STARTUP" { RTOKEN(STARTUP);}
295 <BOTH,SCRIPT>"OUTPUT_FORMAT" { RTOKEN(OUTPUT_FORMAT);}
296 <BOTH,SCRIPT>"OUTPUT_ARCH" { RTOKEN( OUTPUT_ARCH);}
297 <BOTH,SCRIPT>"HLL" { RTOKEN(HLL);}
298 <BOTH,SCRIPT>"SYSLIB" { RTOKEN(SYSLIB);}
299 <BOTH,SCRIPT>"FLOAT" { RTOKEN(FLOAT);}
300 <BOTH,SCRIPT>"LONG" { RTOKEN( LONG);}
301 <BOTH,SCRIPT>"SHORT" { RTOKEN( SHORT);}
302 <BOTH,SCRIPT>"BYTE" { RTOKEN( BYTE);}
303 <BOTH,SCRIPT>"NOFLOAT" { RTOKEN(NOFLOAT);}
304 <EXPRESSION,BOTH,SCRIPT>"NOLOAD" { RTOKEN(NOLOAD);}
305 <BOTH,SCRIPT>"DSECT" { RTOKEN(DSECT);}
306 <BOTH,SCRIPT>"COPY" { RTOKEN(COPY);}
307 <BOTH,SCRIPT>"INFO" { RTOKEN(INFO);}
308 <BOTH,SCRIPT>"OVERLAY" { RTOKEN(OVERLAY);}
309 <BOTH,SCRIPT>"o" { RTOKEN(ORIGIN);}
310 <BOTH,SCRIPT>"org" { RTOKEN(ORIGIN);}
311 <BOTH,SCRIPT>"l" { RTOKEN( LENGTH);}
312 <BOTH,SCRIPT>"len" { RTOKEN( LENGTH);}
313 <BOTH,SCRIPT>"INCLUDE" { RTOKEN(INCLUDE);}
314 <EXPRESSION,BOTH,SCRIPT>"AT" { RTOKEN(AT);}
315 <MRI>"\n" { ++ lineno; RTOKEN(NEWLINE); }
316 <MRI>"*".* { /* Mri comment line */ }
317 <MRI>"END" { RTOKEN(ENDWORD); }
318 <MRI>"ALIGNMOD" { RTOKEN(ALIGNMOD);}
319 <MRI>"ALIGN" { RTOKEN(ALIGN_K);}
321 <MRI>"CHIP" { RTOKEN(CHIP); }
322 <MRI>"BASE" { RTOKEN(BASE); }
323 <MRI>"ALIAS" { RTOKEN(ALIAS); }
324 <MRI>"TRUNCATE" { RTOKEN(TRUNCATE); }
325 <MRI>"LOAD" { RTOKEN(LOAD); }
326 <MRI>"PUBLIC" { RTOKEN(PUBLIC); }
327 <MRI>"ORDER" { RTOKEN(ORDER); }
328 <MRI>"NAME" { RTOKEN(NAMEWORD); }
329 <MRI>"FORMAT" { RTOKEN(FORMAT); }
330 <MRI>"LIST".* { RTOKEN(LIST); /* LIST and ignore to end of line */ }
331 <MRI>"SECT" { RTOKEN(SECT); }
332 <EXPRESSION,BOTH,SCRIPT,MRI>"ABSOLUTE" { RTOKEN(ABSOLUTE); }
333 <MRI>"end" { RTOKEN(ENDWORD); }
334 <MRI>"chip" { RTOKEN(CHIP); }
335 <MRI>"load" { RTOKEN(LOAD); }
336 <MRI>"order" { RTOKEN(ORDER); }
337 <MRI>"name" { RTOKEN(NAMEWORD); }
338 <MRI>"format" { RTOKEN(FORMAT); }
339 <MRI>"list".* { RTOKEN(LIST); /* LIST and ignore to end of line */ }
340 <MRI>"sect" { RTOKEN(SECT); }
341 <EXPRESSION,BOTH,SCRIPT,MRI>"absolute" { RTOKEN(ABSOLUTE); }
343 <COMMAND>{CMDFILENAMECHAR1}{CMDFILENAMECHAR}* {
344 yylval.name = buystring(yytext);
349 <MRI>{FILENAMECHAR1}{NOCFILENAMECHAR}* {
350 /* Filename without commas, needed to parse mri stuff */
351 yylval.name = buystring(yytext);
356 <BOTH,EXPRESSION>{FILENAMECHAR1}{FILENAMECHAR}* {
357 yylval.name = buystring(yytext);
360 <SCRIPT,COMMAND>{FILENAMECHAR}* { yylval.name = buystring(yytext);
364 <EXPRESSION,BOTH,COMMAND,SCRIPT>"\""[^\"]*"\"" {
365 /* No matter the state, quotes
366 give what's inside */
367 yylval.name = buystring(yytext+1);
368 yylval.name[yyleng-2] = 0;
371 <BOTH,SCRIPT,EXPRESSION>"\n" { lineno++;}
372 <MRI,COMMAND,BOTH,SCRIPT,EXPRESSION>[ \t]
375 extern char *ldfile_input_filename;
378 if (include_stack_ptr == 0)
384 yy_switch_to_buffer(include_stack[include_stack_ptr]);
387 if (include_stack_ptr==1)
394 ldfile_input_filename = file_name_stack[include_stack_ptr-1];
403 DEFUN(lex_push_file,(file,name),
407 if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
409 einfo("%F:includes nested too deeply");
411 file_name_stack[include_stack_ptr] = name;
412 include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
416 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
422 DEFUN(yy_create_string_buffer,(string, size),
423 CONST char *string AND
428 b = (YY_BUFFER_STATE) malloc( sizeof( struct yy_buffer_state ) );
429 b->yy_input_file = 0;
432 YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
434 b->yy_buf_size = size;
436 /* yy_ch_buf has to be 2 characters longer than the size given because
437 * we need to put in 2 end-of-buffer characters.
439 b->yy_ch_buf = (YY_CHAR *) malloc( (unsigned) (b->yy_buf_size + 3) );
441 if ( ! b->yy_ch_buf )
442 YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
445 b->yy_ch_buf[0] = '\n';
446 strcpy(b->yy_ch_buf+1, string);
447 b->yy_ch_buf[size+1] = YY_END_OF_BUFFER_CHAR;
448 b->yy_ch_buf[size+2] = YY_END_OF_BUFFER_CHAR;
449 b->yy_n_chars = size+1;
450 b->yy_buf_pos = &b->yy_ch_buf[1];
452 b->yy_eof_status = EOF_NOT_SEEN;
460 DEFUN(lex_redirect,( string),
465 int len = strlen(string);
467 if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
469 einfo("%F: macros nested too deeply");
471 file_name_stack[include_stack_ptr] = "redirect";
472 include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
474 tmp = yy_create_string_buffer(string, len);
476 yy_switch_to_buffer(tmp);
482 int *state_stack_p = state_stack;
485 DEFUN_VOID(ldlex_script)
487 *(state_stack_p)++ = yy_start;
494 DEFUN_VOID(ldlex_mri_script)
496 *(state_stack_p)++ = yy_start;
501 DEFUN_VOID(ldlex_defsym)
503 *(state_stack_p)++ = yy_start;
508 DEFUN_VOID(ldlex_expression)
510 *(state_stack_p)++ = yy_start;
515 DEFUN_VOID(ldlex_both)
517 *(state_stack_p)++ = yy_start;
521 DEFUN_VOID(ldlex_command)
523 *(state_stack_p)++ = yy_start;
528 DEFUN_VOID(ldlex_popstate)
530 yy_start = *(--state_stack_p);
533 yy_input(buf, result, max_size)
539 if (yy_current_buffer->yy_input_file)
542 if ( (*result = read( fileno(yyin), (char *) buf, max_size )) < 0 )
543 YY_FATAL_ERROR( "read() in flex scanner failed" );
554 while (c !='*' && c != EOF)
556 if (c == '\n') lineno++;
567 break; /* found the end */
572 einfo( "%F%P :EOF in comment");