* relax.c: added handling for new "padding" seclet type, used to
[external/binutils.git] / ld / ldlex.l
1 %{
2
3 /* Copyright (C) 1991 Free Software Foundation, Inc.
4
5 This file is part of GLD, the Gnu Linker.
6
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)
10 any later version.
11
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.
16
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.  */
20
21 /*
22 This was written by steve chamberlain
23                     sac@cygnus.com
24 */
25
26
27 typedef int bfd_vma;
28 #include <ansidecl.h>
29 #include "ldgram.h"
30
31 int ldgram_in_defsym;
32 int ldgram_had_equals;
33 int ldgram_in_script;
34
35 int hex_mode;
36 extern int fgetc();
37 extern int yyparse();
38
39
40
41 char *buystring();
42
43 int lineno = 1;
44 int old;
45
46 #undef YY_INPUT
47 #define YY_INPUT(buf,result,max_size) yy_input(buf, &result, max_size)
48 #undef YY_FATAL_ERROR
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;
53
54
55 /* STATES 
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
61         MRI             in an MRI script
62 */
63 #define RTOKEN(x)  {  yylval.token = x; return x; }
64 %}
65
66 %a 4000
67 %o 5000
68 FILENAMECHAR1   [_a-zA-Z\/\.\\]
69 SYMBOLCHARN     [_a-zA-Z\/\.\\0-9]
70 FILENAMECHAR    [_a-zA-Z0-9\/\.\-\_\+\=\$\:\[\]\\]
71 FILENAME        {FILENAMECHAR}+
72 WHITE           [ \t\n]+ 
73
74 %x COMMAND
75 %x SCRIPT
76 %x EXPRESSION
77 %x COMMENT
78 %x BOTH
79 %x DEFSYMEXP
80 %x MRI
81 %%
82   
83 <DEFSYMEXP>"-"                  { RTOKEN('-');}
84 <DEFSYMEXP>"+"                  { RTOKEN('+');}
85 <DEFSYMEXP>{FILENAMECHAR1}{SYMBOLCHARN}*   { yylval.name = buystring(yytext); return NAME; }
86 <DEFSYMEXP>[ \t]                { RTOKEN(DEFSYMEND); }
87 <DEFSYMEXP>"="                  { RTOKEN('='); }
88 <COMMAND>"-defsym"{WHITE}*      { return OPTION_defsym; }
89 <COMMAND>"-noinhibit_exec"      { return OPTION_noinhibit_exec; }
90 <COMMAND>"-noinhibit-exec"      { return OPTION_noinhibit_exec; }
91 <COMMAND>"-sort_common"         { return OPTION_sort_common;}
92 <COMMAND>"-sort-common"         { return OPTION_sort_common;}
93 <COMMAND>"-format"              { return OPTION_format; }
94 <COMMAND>"-n"                   { return OPTION_n; }
95 <COMMAND>"-N"                   { return OPTION_N; }
96 <COMMAND>"-r"                   { return OPTION_r; }
97 <COMMAND>"-relax"               { return OPTION_relax; }
98 <COMMAND>"-i"                   { return OPTION_r; }
99 <COMMAND>"-Ur"                  { return OPTION_Ur; }
100 <COMMAND>"-o"                   { return OPTION_o; }
101 <COMMAND>"-g"                   { return OPTION_g; }
102 <COMMAND>"-e"                   { return OPTION_e; }
103 <COMMAND>"-b"                   { return OPTION_b; }
104 <COMMAND>"-dc"                  { return OPTION_dc; }
105 <COMMAND>"-dp"                  { return OPTION_dp; }
106 <COMMAND>"-d"                   { return OPTION_d; }
107 <COMMAND>"-v"                   { return OPTION_v; }
108 <COMMAND>"-V"                   { return OPTION_V; }
109 <COMMAND>"-M"                   { return OPTION_M; }
110 <COMMAND>"-Map"                 { return OPTION_Map;}
111 <COMMAND>"-t"                   { return OPTION_t; }
112 <COMMAND>"-X"                   { return OPTION_X; }
113 <COMMAND>"-x"                   { return OPTION_x; }
114 <COMMAND>"-c"                   { return OPTION_c; }
115 <COMMAND>"-R"                   { return OPTION_R; }
116 <COMMAND>"-u"                   { return OPTION_u; }
117 <COMMAND>"-s"                   { return OPTION_s; }
118 <COMMAND>"-S"                   { return OPTION_S; }
119 <COMMAND>"-Bstat"               { return OPTION_Bstatic; }
120 <COMMAND>"-B"{FILENAME}         { /* Ignored */ }
121 <COMMAND>"-l"{FILENAME}         {
122                                         yylval.name = buystring(yytext+2);
123                                         return OPTION_l; 
124                                 }
125
126 <COMMAND>"-L"{FILENAME}         { 
127                                         yylval.name = buystring(yytext+2);
128                                         return OPTION_L; 
129                                 }
130 <COMMAND>"-Ttext"               {
131                                  yylval.name = ".text";
132                                  return OPTION_Texp;
133                                 }
134 <COMMAND>"-Tdata"               {
135                                  yylval.name = ".data";
136                                  return OPTION_Texp;
137                                }
138 <COMMAND>"-Tbss"                {
139                                  yylval.name = ".bss";
140                                  return OPTION_Texp;
141                                }
142 <COMMAND>"-O"{FILENAME}         {
143                                 yylval.name = buystring(yytext+2);
144                                 return OPTION_Texp;
145                                 }
146
147 <COMMAND>"-T"{FILENAME}         {
148                                  yylval.name = buystring(yytext+2);
149                                  return OPTION_Tfile;
150                                }
151 <COMMAND>"-T"                   {
152                                  return OPTION_T;
153                                }
154
155 <COMMAND>"-F"{FILENAME}         {
156                                  return OPTION_F;
157                                }
158 <COMMAND>"-F"                   {
159                                  return OPTION_F;
160                                }
161
162 <COMMAND>"-A"{FILENAME}         {
163                                         yylval.name = buystring(yytext+2);
164                                         return OPTION_Aarch;
165                                 }
166
167 <MRI,EXPRESSION>"$"([0-9A-Fa-f])+ {
168                                 yylval.integer = strtol(yytext+1, 0,16);        
169                                 return INT;
170                         }
171
172 <MRI,EXPRESSION>([0-9A-Fa-f])+(H|X|B|O|D)
173                                  {
174                                    int base ;
175                                    switch (yytext[yyleng-1]) {
176                                     case 'X': 
177                                     case 'H':
178                                      base = 16;
179                                      break;
180                                     case 'O':
181                                      base = 8;
182                                      break;
183                                     case 'B':
184                                      base = 2;
185                                      break;
186                                     default:
187                                      base = 10;
188                                    }
189                                    yylval.integer = strtol(yytext+1, 0, base);  
190                                    return INT;
191                                  }
192 <MRI,BOTH,EXPRESSION>"$"?"0x"?([0-9A-Fa-f])+(M|K|m|k)? {
193                                   yylval.integer = strtol(yytext,0,hex_mode);
194                                   if (yytext[yyleng-1]=='M'
195                                         || yytext[yyleng-1] == 'm') {
196                                       yylval.integer *= 1024*1024;
197                                     }   
198                                   if (yytext[yyleng-1]=='K' 
199                                 || yytext[yyleng-1]=='k') {
200                                       yylval.integer *= 1024;
201                                     }           
202                                   return INT;
203                                 }
204 <BOTH,SCRIPT,EXPRESSION>"]"             { RTOKEN(']');}
205 <BOTH,SCRIPT,EXPRESSION>"["             { RTOKEN('[');}
206 <BOTH,SCRIPT,EXPRESSION>"<<="   { RTOKEN(LSHIFTEQ);}
207 <BOTH,SCRIPT,EXPRESSION>">>="   { RTOKEN(RSHIFTEQ);}
208 <BOTH,SCRIPT,EXPRESSION>"||"    { RTOKEN(OROR);}
209 <BOTH,SCRIPT,EXPRESSION>"=="    { RTOKEN(EQ);}
210 <BOTH,SCRIPT,EXPRESSION>"!="    { RTOKEN(NE);}
211 <BOTH,SCRIPT,EXPRESSION>">="    { RTOKEN(GE);}
212 <BOTH,SCRIPT,EXPRESSION>"<="    { RTOKEN(LE);}
213 <BOTH,SCRIPT,EXPRESSION>"<<"    { RTOKEN(LSHIFT);}
214 <BOTH,SCRIPT,EXPRESSION>">>"    { RTOKEN(RSHIFT);}
215 <BOTH,SCRIPT,EXPRESSION>"+="    { RTOKEN(PLUSEQ);}
216 <BOTH,SCRIPT,EXPRESSION>"-="    { RTOKEN(MINUSEQ);}
217 <BOTH,SCRIPT,EXPRESSION>"*="    { RTOKEN(MULTEQ);}
218 <BOTH,SCRIPT,EXPRESSION>"/="    { RTOKEN(DIVEQ);}
219 <BOTH,SCRIPT,EXPRESSION>"&="    { RTOKEN(ANDEQ);}
220 <BOTH,SCRIPT,EXPRESSION>"|="    { RTOKEN(OREQ);}
221 <BOTH,SCRIPT,EXPRESSION>"&&"    { RTOKEN(ANDAND);}
222 <BOTH,SCRIPT,EXPRESSION>">"             { RTOKEN('>');}
223 <MRI,BOTH,SCRIPT,EXPRESSION>","         { RTOKEN(',');}
224 <BOTH,SCRIPT,EXPRESSION>"&"             { RTOKEN('&');}
225 <BOTH,SCRIPT,EXPRESSION>"|"             { RTOKEN('|');}
226 <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 <MRI,BOTH,SCRIPT,EXPRESSION>"="          { RTOKEN('=');}
237 <BOTH,SCRIPT,EXPRESSION>"}"                     { RTOKEN('}') ; }
238 <BOTH,SCRIPT,EXPRESSION>"{"                     { RTOKEN('{'); }
239 <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>"MEMORY"           { RTOKEN(MEMORY);}
248 <BOTH,SCRIPT>"ORIGIN"           { RTOKEN(ORIGIN);}
249 <BOTH,SCRIPT>"BLOCK"                    { RTOKEN(BLOCK);}
250 <BOTH,SCRIPT>"LENGTH"           { RTOKEN(LENGTH);}
251 <EXPRESSION,BOTH,SCRIPT>"ALIGN"                 { RTOKEN(ALIGN_K);}
252 <EXPRESSION,BOTH,SCRIPT>"ADDR"                  { RTOKEN(ADDR);}
253 <BOTH,SCRIPT>"ENTRY"                    { RTOKEN(ENTRY);}
254 <EXPRESSION,BOTH,SCRIPT>"NEXT"                  { RTOKEN(NEXT);}
255 <EXPRESSION,BOTH,SCRIPT>"sizeof_headers"        { RTOKEN(SIZEOF_HEADERS);}
256 <EXPRESSION,BOTH,SCRIPT>"SIZEOF_HEADERS"        { RTOKEN(SIZEOF_HEADERS);}
257 <BOTH,SCRIPT>"MAP"                      { RTOKEN(MAP);}
258 <EXPRESSION,BOTH,SCRIPT>"SIZEOF"                { RTOKEN(SIZEOF);}
259 <BOTH,SCRIPT>"TARGET"           { RTOKEN(TARGET_K);}
260 <BOTH,SCRIPT>"SEARCH_DIR"               { RTOKEN(SEARCH_DIR);}
261 <BOTH,SCRIPT>"OUTPUT"           { RTOKEN(OUTPUT);}
262 <BOTH,SCRIPT>"INPUT"                    { RTOKEN(INPUT);}
263 <BOTH,SCRIPT>"DEFINED"          { RTOKEN(DEFINED);}
264 <BOTH,SCRIPT>"CREATE_OBJECT_SYMBOLS"    { RTOKEN(CREATE_OBJECT_SYMBOLS);}
265 <BOTH,SCRIPT>"CONSTRUCTORS"             { RTOKEN( CONSTRUCTORS);}
266 <BOTH,SCRIPT>"FORCE_COMMON_ALLOCATION" { RTOKEN(FORCE_COMMON_ALLOCATION);}
267 <BOTH,SCRIPT>"SECTIONS"         { RTOKEN(SECTIONS);}
268 <BOTH,SCRIPT>"FILL"                     { RTOKEN(FILL);}
269 <BOTH,SCRIPT>"STARTUP"          { RTOKEN(STARTUP);}
270 <BOTH,SCRIPT>"OUTPUT_FORMAT"            { RTOKEN(OUTPUT_FORMAT);}
271 <BOTH,SCRIPT>"OUTPUT_ARCH"              { RTOKEN( OUTPUT_ARCH);}
272 <BOTH,SCRIPT>"HLL"                      { RTOKEN(HLL);}
273 <BOTH,SCRIPT>"SYSLIB"           { RTOKEN(SYSLIB);}
274 <BOTH,SCRIPT>"FLOAT"                    { RTOKEN(FLOAT);}
275 <BOTH,SCRIPT>"LONG"                     { RTOKEN( LONG);}
276 <BOTH,SCRIPT>"SHORT"                    { RTOKEN( SHORT);}
277 <BOTH,SCRIPT>"BYTE"                     { RTOKEN( BYTE);}
278 <BOTH,SCRIPT>"NOFLOAT"          { RTOKEN(NOFLOAT);}
279 <BOTH,SCRIPT>"NOLOAD"           { RTOKEN(NOLOAD);}
280 <BOTH,SCRIPT>"DSECT"                    { RTOKEN(DSECT);}
281 <BOTH,SCRIPT>"COPY"                     { RTOKEN(COPY);}
282 <BOTH,SCRIPT>"INFO"                     { RTOKEN(INFO);}
283 <BOTH,SCRIPT>"OVERLAY"          { RTOKEN(OVERLAY);}
284 <BOTH,SCRIPT>"o"                        { RTOKEN(ORIGIN);}
285 <BOTH,SCRIPT>"org"                      { RTOKEN(ORIGIN);}
286 <BOTH,SCRIPT>"l"                        { RTOKEN( LENGTH);}
287 <BOTH,SCRIPT>"len"                      { RTOKEN( LENGTH);}
288 <MRI>"\n"                       { ++ lineno;  RTOKEN(NEWLINE); }
289 <MRI>^"*".*                     { /* Mri comment line */ }
290 <MRI>"END"                      { RTOKEN(ENDWORD); }
291 <MRI>"CHIP"                     { RTOKEN(CHIP); }
292 <MRI>"LOAD"                     { RTOKEN(LOAD); }
293 <MRI>"ORDER"                    { RTOKEN(ORDER); }
294 <MRI>"NAME"                     { RTOKEN(NAMEWORD); }
295 <MRI>"FORMAT"                   { RTOKEN(FORMAT); }
296 <MRI>"LIST".*                   { RTOKEN(LIST); /* LIST and ignore to end of line */ }
297 <MRI>"SECT"                     { RTOKEN(SECT); }
298 <MRI>"ABSOLUTE"                 { RTOKEN(ABSOLUTE); }
299 <MRI>"end"                      { RTOKEN(ENDWORD); }
300 <MRI>"chip"                     { RTOKEN(CHIP); }
301 <MRI>"load"                     { RTOKEN(LOAD); }
302 <MRI>"order"                    { RTOKEN(ORDER); }
303 <MRI>"name"                     { RTOKEN(NAMEWORD); }
304 <MRI>"format"                   { RTOKEN(FORMAT); }
305 <MRI>"list".*                   { RTOKEN(LIST); /* LIST and ignore to end of line */ }
306 <MRI>"sect"                     { RTOKEN(SECT); }
307 <MRI>"absolute"                 { RTOKEN(ABSOLUTE); }
308
309 <MRI,BOTH,EXPRESSION>{FILENAMECHAR1}{FILENAMECHAR}*     {
310                                  yylval.name = buystring(yytext); 
311                                   return NAME;
312                                 }
313 <SCRIPT,COMMAND>{FILENAMECHAR}* { yylval.name = buystring(yytext); 
314                                   return NAME;
315                                 }
316
317 <EXPRESSION,BOTH,COMMAND,SCRIPT>"\""[^\"]*"\"" {
318                                         /* No matter the state, quotes
319                                            give what's inside */
320                                         yylval.name = buystring(yytext+1);
321                                         yylval.name[yyleng-2] = 0;
322                                         return NAME;
323                                 }
324 <BOTH,SCRIPT,EXPRESSION>"\n"            { lineno++;}
325 <MRI,COMMAND,BOTH,SCRIPT,EXPRESSION>[ \t]
326
327 "/*"                            { old = INITIAL; BEGIN(COMMENT); }
328 <COMMAND>"/*"                   { old = COMMAND; BEGIN(COMMENT); }
329 <BOTH>"/*"                      { old =BOTH; BEGIN(COMMENT); }
330 <SCRIPT>"/*"                    { old = EXPRESSION; BEGIN(COMMENT); }
331 <EXPRESSION>"/*"                { old = SCRIPT; BEGIN(COMMENT); }
332 "/*"                            { old = INITIAL; BEGIN(COMMENT); }
333 <COMMENT>[^*\\n]*
334 <COMMENT>"*"+[^*/\\n]*
335 <COMMENT>\\n                    { ++lineno;}
336 <COMMENT>"*"+"/"                { BEGIN(old); }
337
338
339 <<EOF>> {
340     include_stack_ptr--;
341     
342     if (include_stack_ptr == 0) 
343     {
344       yyterminate();
345     }
346     else 
347     {
348       yy_switch_to_buffer(include_stack[include_stack_ptr]);
349     }
350         BEGIN(COMMAND);
351         return END;
352   }
353     
354
355
356
357
358
359
360
361 %%
362
363 void
364 DEFUN(lex_push_file,(file),
365      FILE *file)
366 {
367   if (include_stack_ptr >= MAX_INCLUDE_DEPTH) 
368   {
369     einfo("%F:includes nested too deeply");
370   }
371   include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
372   yyin = file;
373   yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
374
375   BEGIN(SCRIPT);
376 }
377
378 YY_BUFFER_STATE 
379 DEFUN(yy_create_string_buffer,(string,  size),
380       CONST char *string AND
381       int size )
382 {
383   YY_BUFFER_STATE b;
384
385   b = (YY_BUFFER_STATE) malloc( sizeof( struct yy_buffer_state ) );
386   b->yy_input_file = 0;
387   
388   if ( ! b )
389    YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
390
391   b->yy_buf_size = size;
392
393   /* yy_ch_buf has to be 2 characters longer than the size given because
394    * we need to put in 2 end-of-buffer characters.
395    */
396   b->yy_ch_buf = (YY_CHAR *) malloc( (unsigned) (b->yy_buf_size + 3) );
397
398   if ( ! b->yy_ch_buf )
399    YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
400
401
402   b->yy_ch_buf[0] = '\n';
403   strcpy(b->yy_ch_buf+1,  string);
404   b->yy_ch_buf[size+1] = YY_END_OF_BUFFER_CHAR;
405   b->yy_ch_buf[size+2] = YY_END_OF_BUFFER_CHAR;
406     b->yy_n_chars = size+1;
407   b->yy_buf_pos = &b->yy_ch_buf[1];
408
409   b->yy_eof_status = EOF_NOT_SEEN;
410
411   return ( b );
412 }
413
414
415
416 void
417 DEFUN(lex_redirect,( string),
418     CONST  char *string)
419 {
420   YY_BUFFER_STATE tmp;
421
422   int len = strlen(string);
423 yy_init = 0  ;  
424   if (include_stack_ptr >= MAX_INCLUDE_DEPTH) 
425   {
426     einfo("%F: macros nested too deeply");
427   }
428   include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
429
430   tmp =   yy_create_string_buffer(string, len);
431   
432   yy_switch_to_buffer(tmp);
433   BEGIN(COMMAND);
434 yyout = stdout;
435 }
436
437 int state_stack[20];
438 int *state_stack_p = state_stack;
439
440 void
441 DEFUN_VOID(ldlex_script)
442 {
443 *(state_stack_p)++ = yy_start;
444
445 BEGIN(SCRIPT);
446 }
447
448
449 void
450 DEFUN_VOID(ldlex_mri_script)
451 {
452 *(state_stack_p)++ = yy_start;
453 BEGIN(MRI);
454 }
455
456 void
457 DEFUN_VOID(ldlex_defsym)
458 {
459 *(state_stack_p)++ = yy_start;
460 BEGIN(DEFSYMEXP);
461 }
462            
463 void
464 DEFUN_VOID(ldlex_expression)
465 {
466 *(state_stack_p)++ = yy_start;
467 BEGIN(EXPRESSION);
468
469 }
470 void
471 DEFUN_VOID(ldlex_both)
472 {
473 *(state_stack_p)++ = yy_start;
474 BEGIN(BOTH);
475 }
476 void
477 DEFUN_VOID(ldlex_command)
478 {
479 *(state_stack_p)++ = yy_start;
480 BEGIN(COMMAND);
481 }
482
483 void
484 DEFUN_VOID(ldlex_popstate)
485 {
486 yy_start = *(--state_stack_p);
487 }
488
489 yy_input(buf, result, max_size)
490 char *buf;
491 int *result;
492 int max_size;
493 {
494   *result = 0; 
495   if (yy_current_buffer->yy_input_file)
496   {
497     if (yyin)
498      if ( (*result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) 
499       YY_FATAL_ERROR( "read() in flex scanner failed" );
500   }
501 }
502