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;
47 #define YY_INPUT(buf,result,max_size) yy_input(buf, &result, max_size)
49 #define YY_FATAL_ERROR ;
50 #define MAX_INCLUDE_DEPTH 10
51 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
52 unsigned int include_stack_ptr = 0;
56 COMMAND on command line
57 COMMENT in a C comment
58 EXPRESSION definiatelyt in an expression
59 SCRIPT definately in a script
60 SOMEWHERE either EXPRESSION or SCRIPT
63 #define RTOKEN(x) { yylval.token = x; return x; }
69 CMDFILENAMECHAR [_a-zA-Z0-9\/\.\\_\+\$\:\[\]\\\,\=\&\!\<\>\-]
70 CMDFILENAMECHAR1 [_a-zA-Z0-9\/\.\\_\+\$\:\[\]\\\,\=\&\!\<\>]
71 FILENAMECHAR1 [_a-zA-Z\/\.\\]
72 SYMBOLCHARN [_a-zA-Z\/\.\\0-9]
73 FILENAMECHAR [_a-zA-Z0-9\/\.\-\_\+\=\$\:\[\]\\]
74 FILENAME {FILENAMECHAR}+
86 <DEFSYMEXP>"-" { RTOKEN('-');}
87 <DEFSYMEXP>"+" { RTOKEN('+');}
88 <DEFSYMEXP>{FILENAMECHAR1}{SYMBOLCHARN}* { yylval.name = buystring(yytext); return NAME; }
89 <DEFSYMEXP>[ \t] { RTOKEN(DEFSYMEND); }
90 <DEFSYMEXP>"=" { RTOKEN('='); }
91 <COMMAND>"-defsym"{WHITE}* { return OPTION_defsym; }
92 <COMMAND>"-noinhibit_exec" { return OPTION_noinhibit_exec; }
93 <COMMAND>"-noinhibit-exec" { return OPTION_noinhibit_exec; }
94 <COMMAND>"-sort_common" { return OPTION_sort_common;}
95 <COMMAND>"-sort-common" { return OPTION_sort_common;}
96 <COMMAND>"-format" { return OPTION_format; }
97 <COMMAND>"-n" { return OPTION_n; }
98 <COMMAND>"-N" { return OPTION_N; }
99 <COMMAND>"-r" { return OPTION_r; }
100 <COMMAND>"-relax" { return OPTION_relax; }
101 <COMMAND>"-i" { return OPTION_r; }
102 <COMMAND>"-Ur" { return OPTION_Ur; }
103 <COMMAND>"-o" { return OPTION_o; }
104 <COMMAND>"-g" { return OPTION_g; }
105 <COMMAND>"-e" { return OPTION_e; }
106 <COMMAND>"-b" { return OPTION_b; }
107 <COMMAND>"-dc" { return OPTION_dc; }
108 <COMMAND>"-dp" { return OPTION_dp; }
109 <COMMAND>"-d" { return OPTION_d; }
110 <COMMAND>"-v" { return OPTION_v; }
111 <COMMAND>"-V" { return OPTION_V; }
112 <COMMAND>"-M" { return OPTION_M; }
113 <COMMAND>"-Map" { return OPTION_Map;}
114 <COMMAND>"-t" { return OPTION_t; }
115 <COMMAND>"-X" { return OPTION_X; }
116 <COMMAND>"-x" { return OPTION_x; }
117 <COMMAND>"-c" { return OPTION_c; }
118 <COMMAND>"-R" { return OPTION_R; }
119 <COMMAND>"-u" { return OPTION_u; }
120 <COMMAND>"-s" { return OPTION_s; }
121 <COMMAND>"-S" { return OPTION_S; }
122 <COMMAND>"-Bstat" { return OPTION_Bstatic; }
123 <COMMAND>"-B"{FILENAME} { /* Ignored */ }
124 <COMMAND>"-l"{FILENAME} {
125 yylval.name = buystring(yytext+2);
129 <COMMAND>"-L"{FILENAME} {
130 yylval.name = buystring(yytext+2);
134 yylval.name = ".text";
138 yylval.name = ".data";
142 yylval.name = ".bss";
145 <COMMAND>"-O"{FILENAME} {
146 yylval.name = buystring(yytext+2);
150 <COMMAND>"-T"{FILENAME} {
151 yylval.name = buystring(yytext+2);
158 <COMMAND>"-F"{FILENAME} {
165 <COMMAND>"-A"{FILENAME} {
166 yylval.name = buystring(yytext+2);
170 <MRI,EXPRESSION>"$"([0-9A-Fa-f])+ {
171 yylval.integer = strtoul(yytext+1, 0,16);
175 <MRI,EXPRESSION>([0-9A-Fa-f])+(H|X|B|O|D)
178 switch (yytext[yyleng-1]) {
192 yylval.integer = strtoul(yytext+1, 0, base);
195 <DEFSYMEXP,MRI,BOTH,EXPRESSION>"$"?"0x"?([0-9A-Fa-f])+(M|K|m|k)? {
196 yylval.integer = strtoul(yytext,0,hex_mode);
197 if (yytext[yyleng-1]=='M'
198 || yytext[yyleng-1] == 'm') {
199 yylval.integer *= 1024*1024;
201 if (yytext[yyleng-1]=='K'
202 || yytext[yyleng-1]=='k') {
203 yylval.integer *= 1024;
207 <BOTH,SCRIPT,EXPRESSION>"]" { RTOKEN(']');}
208 <BOTH,SCRIPT,EXPRESSION>"[" { RTOKEN('[');}
209 <BOTH,SCRIPT,EXPRESSION>"<<=" { RTOKEN(LSHIFTEQ);}
210 <BOTH,SCRIPT,EXPRESSION>">>=" { RTOKEN(RSHIFTEQ);}
211 <BOTH,SCRIPT,EXPRESSION>"||" { RTOKEN(OROR);}
212 <BOTH,SCRIPT,EXPRESSION>"==" { RTOKEN(EQ);}
213 <BOTH,SCRIPT,EXPRESSION>"!=" { RTOKEN(NE);}
214 <BOTH,SCRIPT,EXPRESSION>">=" { RTOKEN(GE);}
215 <BOTH,SCRIPT,EXPRESSION>"<=" { RTOKEN(LE);}
216 <BOTH,SCRIPT,EXPRESSION>"<<" { RTOKEN(LSHIFT);}
217 <BOTH,SCRIPT,EXPRESSION>">>" { RTOKEN(RSHIFT);}
218 <BOTH,SCRIPT,EXPRESSION>"+=" { RTOKEN(PLUSEQ);}
219 <BOTH,SCRIPT,EXPRESSION>"-=" { RTOKEN(MINUSEQ);}
220 <BOTH,SCRIPT,EXPRESSION>"*=" { RTOKEN(MULTEQ);}
221 <BOTH,SCRIPT,EXPRESSION>"/=" { RTOKEN(DIVEQ);}
222 <BOTH,SCRIPT,EXPRESSION>"&=" { RTOKEN(ANDEQ);}
223 <BOTH,SCRIPT,EXPRESSION>"|=" { RTOKEN(OREQ);}
224 <BOTH,SCRIPT,EXPRESSION>"&&" { RTOKEN(ANDAND);}
225 <BOTH,SCRIPT,EXPRESSION>">" { RTOKEN('>');}
226 <MRI,BOTH,SCRIPT,EXPRESSION>"," { RTOKEN(',');}
227 <BOTH,SCRIPT,EXPRESSION>"&" { RTOKEN('&');}
228 <BOTH,SCRIPT,EXPRESSION>"|" { RTOKEN('|');}
229 <BOTH,SCRIPT,EXPRESSION>"~" { RTOKEN('~');}
230 <BOTH,SCRIPT,EXPRESSION>"!" { RTOKEN('!');}
231 <BOTH,SCRIPT,EXPRESSION>"?" { RTOKEN('?');}
232 <BOTH,SCRIPT,EXPRESSION>"*" { RTOKEN('*');}
233 <BOTH,SCRIPT,EXPRESSION>"+" { RTOKEN('+');}
234 <BOTH,SCRIPT,EXPRESSION>"-" { RTOKEN('-');}
235 <BOTH,SCRIPT,EXPRESSION>"/" { RTOKEN('/');}
236 <BOTH,SCRIPT,EXPRESSION>"%" { RTOKEN('%');}
237 <BOTH,SCRIPT,EXPRESSION>"<" { RTOKEN('<');}
238 <BOTH,SCRIPT,EXPRESSION>">" { RTOKEN('>');}
239 <MRI,BOTH,SCRIPT,EXPRESSION>"=" { RTOKEN('=');}
240 <BOTH,SCRIPT,EXPRESSION>"}" { RTOKEN('}') ; }
241 <BOTH,SCRIPT,EXPRESSION>"{" { RTOKEN('{'); }
242 <BOTH,SCRIPT,EXPRESSION>")" { RTOKEN(')');}
243 <BOTH,SCRIPT,EXPRESSION>"(" { RTOKEN('(');}
244 <BOTH,SCRIPT,EXPRESSION>"]" { RTOKEN(']');}
245 <BOTH,SCRIPT,EXPRESSION>"[" { RTOKEN('[');}
246 <BOTH,SCRIPT,EXPRESSION>":" { RTOKEN(':'); }
247 <BOTH,SCRIPT,EXPRESSION>";" { RTOKEN(';');}
248 <BOTH,SCRIPT,EXPRESSION>"-" { RTOKEN('-');}
249 <BOTH,SCRIPT,EXPRESSION>"/" { RTOKEN('/');}
250 <BOTH,SCRIPT>"MEMORY" { RTOKEN(MEMORY);}
251 <BOTH,SCRIPT>"ORIGIN" { RTOKEN(ORIGIN);}
252 <BOTH,SCRIPT>"BLOCK" { RTOKEN(BLOCK);}
253 <BOTH,SCRIPT>"LENGTH" { RTOKEN(LENGTH);}
254 <EXPRESSION,BOTH,SCRIPT>"ALIGN" { RTOKEN(ALIGN_K);}
255 <EXPRESSION,BOTH,SCRIPT>"ADDR" { RTOKEN(ADDR);}
256 <BOTH,SCRIPT>"ENTRY" { RTOKEN(ENTRY);}
257 <EXPRESSION,BOTH,SCRIPT>"NEXT" { RTOKEN(NEXT);}
258 <EXPRESSION,BOTH,SCRIPT>"sizeof_headers" { RTOKEN(SIZEOF_HEADERS);}
259 <EXPRESSION,BOTH,SCRIPT>"SIZEOF_HEADERS" { RTOKEN(SIZEOF_HEADERS);}
260 <BOTH,SCRIPT>"MAP" { RTOKEN(MAP);}
261 <EXPRESSION,BOTH,SCRIPT>"SIZEOF" { RTOKEN(SIZEOF);}
262 <BOTH,SCRIPT>"TARGET" { RTOKEN(TARGET_K);}
263 <BOTH,SCRIPT>"SEARCH_DIR" { RTOKEN(SEARCH_DIR);}
264 <BOTH,SCRIPT>"OUTPUT" { RTOKEN(OUTPUT);}
265 <BOTH,SCRIPT>"INPUT" { RTOKEN(INPUT);}
266 <BOTH,SCRIPT>"DEFINED" { RTOKEN(DEFINED);}
267 <BOTH,SCRIPT>"CREATE_OBJECT_SYMBOLS" { RTOKEN(CREATE_OBJECT_SYMBOLS);}
268 <BOTH,SCRIPT>"CONSTRUCTORS" { RTOKEN( CONSTRUCTORS);}
269 <BOTH,SCRIPT>"FORCE_COMMON_ALLOCATION" { RTOKEN(FORCE_COMMON_ALLOCATION);}
270 <BOTH,SCRIPT>"SECTIONS" { RTOKEN(SECTIONS);}
271 <BOTH,SCRIPT>"FILL" { RTOKEN(FILL);}
272 <BOTH,SCRIPT>"STARTUP" { RTOKEN(STARTUP);}
273 <BOTH,SCRIPT>"OUTPUT_FORMAT" { RTOKEN(OUTPUT_FORMAT);}
274 <BOTH,SCRIPT>"OUTPUT_ARCH" { RTOKEN( OUTPUT_ARCH);}
275 <BOTH,SCRIPT>"HLL" { RTOKEN(HLL);}
276 <BOTH,SCRIPT>"SYSLIB" { RTOKEN(SYSLIB);}
277 <BOTH,SCRIPT>"FLOAT" { RTOKEN(FLOAT);}
278 <BOTH,SCRIPT>"LONG" { RTOKEN( LONG);}
279 <BOTH,SCRIPT>"SHORT" { RTOKEN( SHORT);}
280 <BOTH,SCRIPT>"BYTE" { RTOKEN( BYTE);}
281 <BOTH,SCRIPT>"NOFLOAT" { RTOKEN(NOFLOAT);}
282 <BOTH,SCRIPT>"NOLOAD" { RTOKEN(NOLOAD);}
283 <BOTH,SCRIPT>"DSECT" { RTOKEN(DSECT);}
284 <BOTH,SCRIPT>"COPY" { RTOKEN(COPY);}
285 <BOTH,SCRIPT>"INFO" { RTOKEN(INFO);}
286 <BOTH,SCRIPT>"OVERLAY" { RTOKEN(OVERLAY);}
287 <BOTH,SCRIPT>"o" { RTOKEN(ORIGIN);}
288 <BOTH,SCRIPT>"org" { RTOKEN(ORIGIN);}
289 <BOTH,SCRIPT>"l" { RTOKEN( LENGTH);}
290 <BOTH,SCRIPT>"len" { RTOKEN( LENGTH);}
291 <MRI>"\n" { ++ lineno; RTOKEN(NEWLINE); }
292 <MRI>^"*".* { /* Mri comment line */ }
293 <MRI>"END" { RTOKEN(ENDWORD); }
294 <MRI>"CHIP" { RTOKEN(CHIP); }
295 <MRI>"LOAD" { RTOKEN(LOAD); }
296 <MRI>"ORDER" { RTOKEN(ORDER); }
297 <MRI>"NAME" { RTOKEN(NAMEWORD); }
298 <MRI>"FORMAT" { RTOKEN(FORMAT); }
299 <MRI>"LIST".* { RTOKEN(LIST); /* LIST and ignore to end of line */ }
300 <MRI>"SECT" { RTOKEN(SECT); }
301 <MRI>"ABSOLUTE" { RTOKEN(ABSOLUTE); }
302 <MRI>"end" { RTOKEN(ENDWORD); }
303 <MRI>"chip" { RTOKEN(CHIP); }
304 <MRI>"load" { RTOKEN(LOAD); }
305 <MRI>"order" { RTOKEN(ORDER); }
306 <MRI>"name" { RTOKEN(NAMEWORD); }
307 <MRI>"format" { RTOKEN(FORMAT); }
308 <MRI>"list".* { RTOKEN(LIST); /* LIST and ignore to end of line */ }
309 <MRI>"sect" { RTOKEN(SECT); }
310 <MRI>"absolute" { RTOKEN(ABSOLUTE); }
312 <COMMAND>{CMDFILENAMECHAR1}{CMDFILENAMECHAR}* {
313 yylval.name = buystring(yytext);
318 <MRI,BOTH,EXPRESSION>{FILENAMECHAR1}{FILENAMECHAR}* {
319 yylval.name = buystring(yytext);
322 <SCRIPT,COMMAND>{FILENAMECHAR}* { yylval.name = buystring(yytext);
326 <EXPRESSION,BOTH,COMMAND,SCRIPT>"\""[^\"]*"\"" {
327 /* No matter the state, quotes
328 give what's inside */
329 yylval.name = buystring(yytext+1);
330 yylval.name[yyleng-2] = 0;
333 <BOTH,SCRIPT,EXPRESSION>"\n" { lineno++;}
334 <MRI,COMMAND,BOTH,SCRIPT,EXPRESSION>[ \t]
336 "/*" { old = INITIAL; BEGIN(COMMENT); }
337 <COMMAND>"/*" { old = COMMAND; BEGIN(COMMENT); }
338 <BOTH>"/*" { old =BOTH; BEGIN(COMMENT); }
339 <SCRIPT>"/*" { old = EXPRESSION; BEGIN(COMMENT); }
340 <EXPRESSION>"/*" { old = SCRIPT; BEGIN(COMMENT); }
341 "/*" { old = INITIAL; BEGIN(COMMENT); }
343 <COMMENT>"*"+[^*/\\n]*
344 <COMMENT>\\n { ++lineno;}
345 <COMMENT>"*"+"/" { BEGIN(old); }
351 if (include_stack_ptr == 0)
357 yy_switch_to_buffer(include_stack[include_stack_ptr]);
373 DEFUN(lex_push_file,(file),
376 if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
378 einfo("%F:includes nested too deeply");
380 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
382 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
388 DEFUN(yy_create_string_buffer,(string, size),
389 CONST char *string AND
394 b = (YY_BUFFER_STATE) malloc( sizeof( struct yy_buffer_state ) );
395 b->yy_input_file = 0;
398 YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
400 b->yy_buf_size = size;
402 /* yy_ch_buf has to be 2 characters longer than the size given because
403 * we need to put in 2 end-of-buffer characters.
405 b->yy_ch_buf = (YY_CHAR *) malloc( (unsigned) (b->yy_buf_size + 3) );
407 if ( ! b->yy_ch_buf )
408 YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
411 b->yy_ch_buf[0] = '\n';
412 strcpy(b->yy_ch_buf+1, string);
413 b->yy_ch_buf[size+1] = YY_END_OF_BUFFER_CHAR;
414 b->yy_ch_buf[size+2] = YY_END_OF_BUFFER_CHAR;
415 b->yy_n_chars = size+1;
416 b->yy_buf_pos = &b->yy_ch_buf[1];
418 b->yy_eof_status = EOF_NOT_SEEN;
426 DEFUN(lex_redirect,( string),
431 int len = strlen(string);
433 if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
435 einfo("%F: macros nested too deeply");
437 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
439 tmp = yy_create_string_buffer(string, len);
441 yy_switch_to_buffer(tmp);
447 int *state_stack_p = state_stack;
450 DEFUN_VOID(ldlex_script)
452 *(state_stack_p)++ = yy_start;
459 DEFUN_VOID(ldlex_mri_script)
461 *(state_stack_p)++ = yy_start;
466 DEFUN_VOID(ldlex_defsym)
468 *(state_stack_p)++ = yy_start;
473 DEFUN_VOID(ldlex_expression)
475 *(state_stack_p)++ = yy_start;
480 DEFUN_VOID(ldlex_both)
482 *(state_stack_p)++ = yy_start;
486 DEFUN_VOID(ldlex_command)
488 *(state_stack_p)++ = yy_start;
493 DEFUN_VOID(ldlex_popstate)
495 yy_start = *(--state_stack_p);
498 yy_input(buf, result, max_size)
504 if (yy_current_buffer->yy_input_file)
507 if ( (*result = read( fileno(yyin), (char *) buf, max_size )) < 0 )
508 YY_FATAL_ERROR( "read() in flex scanner failed" );