* ldgram.y: Add SPECIAL token.
[external/binutils.git] / ld / ldlex.l
1 %{
2
3 /* Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4    2000, 2001, 2002, 2003, 2004, 2005  Free Software Foundation, Inc.
5
6    This file is part of GLD, the Gnu Linker.
7
8    GLD is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    GLD is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GLD; see the file COPYING.  If not, write to the Free
20    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21    02111-1307, USA.  */
22
23 /*
24 This was written by steve chamberlain
25                     sac@cygnus.com
26 */
27
28
29 #include <stdio.h>
30
31 #include "bfd.h"
32 #include "sysdep.h"
33 #include "safe-ctype.h"
34 #include "bfdlink.h"
35 #include "ld.h"
36 #include "ldmisc.h"
37 #include "ldexp.h"
38 #include "ldlang.h"
39 #include <ldgram.h>
40 #include "ldfile.h"
41 #include "ldlex.h"
42 #include "ldmain.h"
43 #include "libiberty.h"
44
45 /* The type of top-level parser input.
46    yylex and yyparse (indirectly) both check this.  */
47 input_type parser_input;
48
49 /* Line number in the current input file.
50    (FIXME Actually, it doesn't appear to get reset for each file?)  */
51 unsigned int lineno = 1;
52
53 /* The string we are currently lexing, or NULL if we are reading a
54    file.  */
55 const char *lex_string = NULL;
56
57 /* Support for flex reading from more than one input file (stream).
58    `include_stack' is flex's input state for each open file;
59    `file_name_stack' is the file names.  `lineno_stack' is the current
60    line numbers.
61
62    If `include_stack_ptr' is 0, we haven't started reading anything yet.
63    Otherwise, stack elements 0 through `include_stack_ptr - 1' are valid.  */
64
65 #undef YY_INPUT
66 #define YY_INPUT(buf,result,max_size) yy_input (buf, &result, max_size)
67
68 #define YY_NO_UNPUT
69
70 #define MAX_INCLUDE_DEPTH 10
71 static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
72 static const char *file_name_stack[MAX_INCLUDE_DEPTH];
73 static unsigned int lineno_stack[MAX_INCLUDE_DEPTH];
74 static unsigned int include_stack_ptr = 0;
75 static int vers_node_nesting = 0;
76
77 static void yy_input (char *, int *, int);
78 static void comment (void);
79 static void lex_warn_invalid (char *where, char *what);
80
81 /* STATES
82         EXPRESSION      definitely in an expression
83         SCRIPT          definitely in a script
84         BOTH            either EXPRESSION or SCRIPT
85         DEFSYMEXP       in an argument to -defsym
86         MRI             in an MRI script
87         VERS_START      starting a Sun style mapfile
88         VERS_SCRIPT     a Sun style mapfile
89         VERS_NODE       a node within a Sun style mapfile
90 */
91 #define RTOKEN(x)  {  yylval.token = x; return x; }
92
93 /* Some versions of flex want this.  */
94 #ifndef yywrap
95 int yywrap (void) { return 1; }
96 #endif
97 %}
98
99 %a 4000
100 %o 5000
101
102 CMDFILENAMECHAR   [_a-zA-Z0-9\/\.\\_\+\$\:\[\]\\\,\=\&\!\<\>\-\~]
103 CMDFILENAMECHAR1  [_a-zA-Z0-9\/\.\\_\+\$\:\[\]\\\,\=\&\!\<\>\~]
104 FILENAMECHAR1   [_a-zA-Z\/\.\\\$\_\~]
105 SYMBOLCHARN     [_a-zA-Z\/\.\\\$\_\~0-9]
106 FILENAMECHAR    [_a-zA-Z0-9\/\.\-\_\+\=\$\:\[\]\\\,\~]
107 WILDCHAR        [_a-zA-Z0-9\/\.\-\_\+\=\$\:\[\]\\\,\~\?\*]
108 WHITE           [ \t\n\r]+
109
110 NOCFILENAMECHAR [_a-zA-Z0-9\/\.\-\_\+\$\:\[\]\\\~]
111
112 V_TAG [.$_a-zA-Z][._a-zA-Z0-9]*
113 V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)*
114
115 %s SCRIPT
116 %s EXPRESSION
117 %s BOTH
118 %s DEFSYMEXP
119 %s MRI
120 %s VERS_START
121 %s VERS_SCRIPT
122 %s VERS_NODE
123 %%
124
125   if (parser_input != input_selected)
126     {
127       /* The first token of the input determines the initial parser state.  */
128       input_type t = parser_input;
129       parser_input = input_selected;
130       switch (t)
131         {
132         case input_script: return INPUT_SCRIPT; break;
133         case input_mri_script: return INPUT_MRI_SCRIPT; break;
134         case input_version_script: return INPUT_VERSION_SCRIPT; break;
135         case input_defsym: return INPUT_DEFSYM; break;
136         default: abort ();
137         }
138     }
139
140 <BOTH,SCRIPT,EXPRESSION,VERS_START,VERS_NODE,VERS_SCRIPT>"/*"   { comment (); }
141
142
143 <DEFSYMEXP>"-"                  { RTOKEN('-');}
144 <DEFSYMEXP>"+"                  { RTOKEN('+');}
145 <DEFSYMEXP>{FILENAMECHAR1}{SYMBOLCHARN}*   { yylval.name = xstrdup (yytext); return NAME; }
146 <DEFSYMEXP>"="                  { RTOKEN('='); }
147
148 <MRI,EXPRESSION>"$"([0-9A-Fa-f])+ {
149                                 yylval.integer = bfd_scan_vma (yytext + 1, 0, 16);
150                                 yylval.bigint.str = NULL;
151                                 return INT;
152                         }
153
154 <MRI,EXPRESSION>([0-9A-Fa-f])+(H|h|X|x|B|b|O|o|D|d) {
155                                    int ibase ;
156                                    switch (yytext[yyleng - 1]) {
157                                     case 'X':
158                                     case 'x':
159                                     case 'H':
160                                     case 'h':
161                                      ibase = 16;
162                                      break;
163                                     case 'O':
164                                     case 'o':
165                                      ibase = 8;
166                                      break;
167                                     case 'B':
168                                     case 'b':
169                                      ibase = 2;
170                                      break;
171                                     default:
172                                      ibase = 10;
173                                    }
174                                    yylval.integer = bfd_scan_vma (yytext, 0,
175                                                                   ibase);
176                                    yylval.bigint.str = NULL;
177                                    return INT;
178                                  }
179 <SCRIPT,DEFSYMEXP,MRI,BOTH,EXPRESSION>((("$"|0[xX])([0-9A-Fa-f])+)|(([0-9])+))(M|K|m|k)? {
180                                   char *s = yytext;
181                                   int ibase = 0;
182
183                                   if (*s == '$')
184                                     {
185                                       ++s;
186                                       ibase = 16;
187                                     }
188                                   yylval.integer = bfd_scan_vma (s, 0, ibase);
189                                   yylval.bigint.str = NULL;
190                                   if (yytext[yyleng - 1] == 'M'
191                                       || yytext[yyleng - 1] == 'm')
192                                     {
193                                       yylval.integer *= 1024 * 1024;
194                                     }
195                                   else if (yytext[yyleng - 1] == 'K'
196                                       || yytext[yyleng - 1]=='k')
197                                     {
198                                       yylval.integer *= 1024;
199                                     }
200                                   else if (yytext[0] == '0'
201                                            && (yytext[1] == 'x'
202                                                || yytext[1] == 'X'))
203                                     {
204                                       yylval.bigint.str = xstrdup (yytext + 2);
205                                     }
206                                   return INT;
207                                 }
208 <BOTH,SCRIPT,EXPRESSION,MRI>"]"         { RTOKEN(']');}
209 <BOTH,SCRIPT,EXPRESSION,MRI>"["         { RTOKEN('[');}
210 <BOTH,SCRIPT,EXPRESSION,MRI>"<<="       { RTOKEN(LSHIFTEQ);}
211 <BOTH,SCRIPT,EXPRESSION,MRI>">>="       { RTOKEN(RSHIFTEQ);}
212 <BOTH,SCRIPT,EXPRESSION,MRI>"||"        { RTOKEN(OROR);}
213 <BOTH,SCRIPT,EXPRESSION,MRI>"=="        { RTOKEN(EQ);}
214 <BOTH,SCRIPT,EXPRESSION,MRI>"!="        { RTOKEN(NE);}
215 <BOTH,SCRIPT,EXPRESSION,MRI>">="        { RTOKEN(GE);}
216 <BOTH,SCRIPT,EXPRESSION,MRI>"<="        { RTOKEN(LE);}
217 <BOTH,SCRIPT,EXPRESSION,MRI>"<<"        { RTOKEN(LSHIFT);}
218 <BOTH,SCRIPT,EXPRESSION,MRI>">>"        { RTOKEN(RSHIFT);}
219 <BOTH,SCRIPT,EXPRESSION,MRI>"+="        { RTOKEN(PLUSEQ);}
220 <BOTH,SCRIPT,EXPRESSION,MRI>"-="        { RTOKEN(MINUSEQ);}
221 <BOTH,SCRIPT,EXPRESSION,MRI>"*="        { RTOKEN(MULTEQ);}
222 <BOTH,SCRIPT,EXPRESSION,MRI>"/="        { RTOKEN(DIVEQ);}
223 <BOTH,SCRIPT,EXPRESSION,MRI>"&="        { RTOKEN(ANDEQ);}
224 <BOTH,SCRIPT,EXPRESSION,MRI>"|="        { RTOKEN(OREQ);}
225 <BOTH,SCRIPT,EXPRESSION,MRI>"&&"        { RTOKEN(ANDAND);}
226 <BOTH,SCRIPT,EXPRESSION,MRI>">"         { RTOKEN('>');}
227 <BOTH,SCRIPT,EXPRESSION,MRI>","         { RTOKEN(',');}
228 <BOTH,SCRIPT,EXPRESSION,MRI>"&"         { RTOKEN('&');}
229 <BOTH,SCRIPT,EXPRESSION,MRI>"|"         { RTOKEN('|');}
230 <BOTH,SCRIPT,EXPRESSION,MRI>"~"         { RTOKEN('~');}
231 <BOTH,SCRIPT,EXPRESSION,MRI>"!"         { RTOKEN('!');}
232 <BOTH,SCRIPT,EXPRESSION,MRI>"?"         { RTOKEN('?');}
233 <BOTH,SCRIPT,EXPRESSION,MRI>"*"         { RTOKEN('*');}
234 <BOTH,SCRIPT,EXPRESSION,MRI>"+"         { RTOKEN('+');}
235 <BOTH,SCRIPT,EXPRESSION,MRI>"-"         { RTOKEN('-');}
236 <BOTH,SCRIPT,EXPRESSION,MRI>"/"         { RTOKEN('/');}
237 <BOTH,SCRIPT,EXPRESSION,MRI>"%"         { RTOKEN('%');}
238 <BOTH,SCRIPT,EXPRESSION,MRI>"<"         { RTOKEN('<');}
239 <BOTH,SCRIPT,EXPRESSION,MRI>"="         { RTOKEN('=');}
240 <BOTH,SCRIPT,EXPRESSION,MRI>"}"         { RTOKEN('}') ; }
241 <BOTH,SCRIPT,EXPRESSION,MRI>"{"         { RTOKEN('{'); }
242 <BOTH,SCRIPT,EXPRESSION,MRI>")"         { RTOKEN(')');}
243 <BOTH,SCRIPT,EXPRESSION,MRI>"("         { RTOKEN('(');}
244 <BOTH,SCRIPT,EXPRESSION,MRI>":"         { RTOKEN(':'); }
245 <BOTH,SCRIPT,EXPRESSION,MRI>";"         { RTOKEN(';');}
246 <BOTH,SCRIPT>"MEMORY"                   { RTOKEN(MEMORY);}
247 <BOTH,SCRIPT,EXPRESSION>"ORIGIN"        { RTOKEN(ORIGIN);}
248 <BOTH,SCRIPT>"VERSION"                  { RTOKEN(VERSIONK);}
249 <EXPRESSION,BOTH,SCRIPT>"BLOCK"         { RTOKEN(BLOCK);}
250 <EXPRESSION,BOTH,SCRIPT>"BIND"          { RTOKEN(BIND);}
251 <BOTH,SCRIPT,EXPRESSION>"LENGTH"        { RTOKEN(LENGTH);}
252 <EXPRESSION,BOTH,SCRIPT>"ALIGN"         { RTOKEN(ALIGN_K);}
253 <EXPRESSION,BOTH,SCRIPT>"DATA_SEGMENT_ALIGN"    { RTOKEN(DATA_SEGMENT_ALIGN);}
254 <EXPRESSION,BOTH,SCRIPT>"DATA_SEGMENT_RELRO_END"        { RTOKEN(DATA_SEGMENT_RELRO_END);}
255 <EXPRESSION,BOTH,SCRIPT>"DATA_SEGMENT_END"      { RTOKEN(DATA_SEGMENT_END);}
256 <EXPRESSION,BOTH,SCRIPT>"ADDR"          { RTOKEN(ADDR);}
257 <EXPRESSION,BOTH,SCRIPT>"LOADADDR"      { RTOKEN(LOADADDR);}
258 <EXPRESSION,BOTH>"MAX"                  { RTOKEN(MAX_K); }
259 <EXPRESSION,BOTH>"MIN"                  { RTOKEN(MIN_K); }
260 <EXPRESSION,BOTH>"ASSERT"               { RTOKEN(ASSERT_K); }
261 <BOTH,SCRIPT>"ENTRY"                    { RTOKEN(ENTRY);}
262 <BOTH,SCRIPT,MRI>"EXTERN"               { RTOKEN(EXTERN);}
263 <EXPRESSION,BOTH,SCRIPT>"NEXT"          { RTOKEN(NEXT);}
264 <EXPRESSION,BOTH,SCRIPT>"sizeof_headers"        { RTOKEN(SIZEOF_HEADERS);}
265 <EXPRESSION,BOTH,SCRIPT>"SIZEOF_HEADERS"        { RTOKEN(SIZEOF_HEADERS);}
266 <EXPRESSION,BOTH,SCRIPT>"SEGMENT_START" { RTOKEN(SEGMENT_START);}
267 <BOTH,SCRIPT>"MAP"                      { RTOKEN(MAP);}
268 <EXPRESSION,BOTH,SCRIPT>"SIZEOF"        { RTOKEN(SIZEOF);}
269 <BOTH,SCRIPT>"TARGET"                   { RTOKEN(TARGET_K);}
270 <BOTH,SCRIPT>"SEARCH_DIR"               { RTOKEN(SEARCH_DIR);}
271 <BOTH,SCRIPT>"OUTPUT"                   { RTOKEN(OUTPUT);}
272 <BOTH,SCRIPT>"INPUT"                    { RTOKEN(INPUT);}
273 <EXPRESSION,BOTH,SCRIPT>"GROUP"         { RTOKEN(GROUP);}
274 <EXPRESSION,BOTH,SCRIPT>"AS_NEEDED"     { RTOKEN(AS_NEEDED);}
275 <EXPRESSION,BOTH,SCRIPT>"DEFINED"       { RTOKEN(DEFINED);}
276 <BOTH,SCRIPT>"CREATE_OBJECT_SYMBOLS"    { RTOKEN(CREATE_OBJECT_SYMBOLS);}
277 <BOTH,SCRIPT>"CONSTRUCTORS"             { RTOKEN( CONSTRUCTORS);}
278 <BOTH,SCRIPT>"FORCE_COMMON_ALLOCATION"  { RTOKEN(FORCE_COMMON_ALLOCATION);}
279 <BOTH,SCRIPT>"INHIBIT_COMMON_ALLOCATION" { RTOKEN(INHIBIT_COMMON_ALLOCATION);}
280 <BOTH,SCRIPT>"SECTIONS"                 { RTOKEN(SECTIONS);}
281 <BOTH,SCRIPT>"FILL"                     { RTOKEN(FILL);}
282 <BOTH,SCRIPT>"STARTUP"                  { RTOKEN(STARTUP);}
283 <BOTH,SCRIPT>"OUTPUT_FORMAT"            { RTOKEN(OUTPUT_FORMAT);}
284 <BOTH,SCRIPT>"OUTPUT_ARCH"              { RTOKEN( OUTPUT_ARCH);}
285 <BOTH,SCRIPT>"HLL"                      { RTOKEN(HLL);}
286 <BOTH,SCRIPT>"SYSLIB"                   { RTOKEN(SYSLIB);}
287 <BOTH,SCRIPT>"FLOAT"                    { RTOKEN(FLOAT);}
288 <BOTH,SCRIPT>"QUAD"                     { RTOKEN( QUAD);}
289 <BOTH,SCRIPT>"SQUAD"                    { RTOKEN( SQUAD);}
290 <BOTH,SCRIPT>"LONG"                     { RTOKEN( LONG);}
291 <BOTH,SCRIPT>"SHORT"                    { RTOKEN( SHORT);}
292 <BOTH,SCRIPT>"BYTE"                     { RTOKEN( BYTE);}
293 <BOTH,SCRIPT>"NOFLOAT"                  { RTOKEN(NOFLOAT);}
294 <EXPRESSION,BOTH,SCRIPT>"NOCROSSREFS"   { RTOKEN(NOCROSSREFS);}
295 <BOTH,SCRIPT>"OVERLAY"                  { RTOKEN(OVERLAY); }
296 <BOTH,SCRIPT>"SORT_BY_NAME"             { RTOKEN(SORT_BY_NAME); }
297 <BOTH,SCRIPT>"SORT_BY_ALIGNMENT"        { RTOKEN(SORT_BY_ALIGNMENT); }
298 <BOTH,SCRIPT>"SORT"                     { RTOKEN(SORT_BY_NAME); }
299 <EXPRESSION,BOTH,SCRIPT>"NOLOAD"        { RTOKEN(NOLOAD);}
300 <EXPRESSION,BOTH,SCRIPT>"DSECT"         { RTOKEN(DSECT);}
301 <EXPRESSION,BOTH,SCRIPT>"COPY"          { RTOKEN(COPY);}
302 <EXPRESSION,BOTH,SCRIPT>"INFO"          { RTOKEN(INFO);}
303 <EXPRESSION,BOTH,SCRIPT>"OVERLAY"       { RTOKEN(OVERLAY);}
304 <EXPRESSION,BOTH,SCRIPT>"ONLY_IF_RO"    { RTOKEN(ONLY_IF_RO); }
305 <EXPRESSION,BOTH,SCRIPT>"ONLY_IF_RW"    { RTOKEN(ONLY_IF_RW); }
306 <EXPRESSION,BOTH,SCRIPT>"SPECIAL"       { RTOKEN(SPECIAL); }
307 <BOTH,SCRIPT>"o"                        { RTOKEN(ORIGIN);}
308 <BOTH,SCRIPT>"org"                      { RTOKEN(ORIGIN);}
309 <BOTH,SCRIPT>"l"                        { RTOKEN( LENGTH);}
310 <BOTH,SCRIPT>"len"                      { RTOKEN( LENGTH);}
311 <BOTH,SCRIPT>"INCLUDE"                  { RTOKEN(INCLUDE);}
312 <BOTH,SCRIPT>"PHDRS"                    { RTOKEN (PHDRS); }
313 <EXPRESSION,BOTH,SCRIPT>"AT"            { RTOKEN(AT);}
314 <EXPRESSION,BOTH,SCRIPT>"SUBALIGN"      { RTOKEN(SUBALIGN);}
315 <EXPRESSION,BOTH,SCRIPT>"PROVIDE"       { RTOKEN(PROVIDE); }
316 <EXPRESSION,BOTH,SCRIPT>"KEEP"          { RTOKEN(KEEP); }
317 <EXPRESSION,BOTH,SCRIPT>"EXCLUDE_FILE"  { RTOKEN(EXCLUDE_FILE); }
318 <MRI>"#".*\n?                   { ++ lineno; }
319 <MRI>"\n"                       { ++ lineno;  RTOKEN(NEWLINE); }
320 <MRI>"*".*                      { /* Mri comment line */ }
321 <MRI>";".*                      { /* Mri comment line */ }
322 <MRI>"END"                      { RTOKEN(ENDWORD); }
323 <MRI>"ALIGNMOD"                 { RTOKEN(ALIGNMOD);}
324 <MRI>"ALIGN"                    { RTOKEN(ALIGN_K);}
325 <MRI>"CHIP"                     { RTOKEN(CHIP); }
326 <MRI>"BASE"                     { RTOKEN(BASE); }
327 <MRI>"ALIAS"                    { RTOKEN(ALIAS); }
328 <MRI>"TRUNCATE"                 { RTOKEN(TRUNCATE); }
329 <MRI>"LOAD"                     { RTOKEN(LOAD); }
330 <MRI>"PUBLIC"                   { RTOKEN(PUBLIC); }
331 <MRI>"ORDER"                    { RTOKEN(ORDER); }
332 <MRI>"NAME"                     { RTOKEN(NAMEWORD); }
333 <MRI>"FORMAT"                   { RTOKEN(FORMAT); }
334 <MRI>"CASE"                     { RTOKEN(CASE); }
335 <MRI>"START"                    { RTOKEN(START); }
336 <MRI>"LIST".*                   { RTOKEN(LIST); /* LIST and ignore to end of line */ }
337 <MRI>"SECT"                     { RTOKEN(SECT); }
338 <EXPRESSION,BOTH,SCRIPT,MRI>"ABSOLUTE"                  { RTOKEN(ABSOLUTE); }
339 <MRI>"end"                      { RTOKEN(ENDWORD); }
340 <MRI>"alignmod"                 { RTOKEN(ALIGNMOD);}
341 <MRI>"align"                    { RTOKEN(ALIGN_K);}
342 <MRI>"chip"                     { RTOKEN(CHIP); }
343 <MRI>"base"                     { RTOKEN(BASE); }
344 <MRI>"alias"                    { RTOKEN(ALIAS); }
345 <MRI>"truncate"                 { RTOKEN(TRUNCATE); }
346 <MRI>"load"                     { RTOKEN(LOAD); }
347 <MRI>"public"                   { RTOKEN(PUBLIC); }
348 <MRI>"order"                    { RTOKEN(ORDER); }
349 <MRI>"name"                     { RTOKEN(NAMEWORD); }
350 <MRI>"format"                   { RTOKEN(FORMAT); }
351 <MRI>"case"                     { RTOKEN(CASE); }
352 <MRI>"extern"                   { RTOKEN(EXTERN); }
353 <MRI>"start"                    { RTOKEN(START); }
354 <MRI>"list".*                   { RTOKEN(LIST); /* LIST and ignore to end of line */ }
355 <MRI>"sect"                     { RTOKEN(SECT); }
356 <EXPRESSION,BOTH,SCRIPT,MRI>"absolute"                  { RTOKEN(ABSOLUTE); }
357
358 <MRI>{FILENAMECHAR1}{NOCFILENAMECHAR}*  {
359 /* Filename without commas, needed to parse mri stuff */
360                                  yylval.name = xstrdup (yytext);
361                                   return NAME;
362                                 }
363
364
365 <BOTH,EXPRESSION>{FILENAMECHAR1}{FILENAMECHAR}* {
366                                  yylval.name = xstrdup (yytext);
367                                   return NAME;
368                                 }
369 <BOTH,EXPRESSION>"-l"{FILENAMECHAR}+ {
370                                   yylval.name = xstrdup (yytext + 2);
371                                   return LNAME;
372                                 }
373 <SCRIPT>{WILDCHAR}* {
374                 /* Annoyingly, this pattern can match comments, and we have
375                    longest match issues to consider.  So if the first two
376                    characters are a comment opening, put the input back and
377                    try again.  */
378                 if (yytext[0] == '/' && yytext[1] == '*')
379                   {
380                     yyless (2);
381                     comment ();
382                   }
383                 else
384                   {
385                     yylval.name = xstrdup (yytext);
386                     return NAME;
387                   }
388         }
389
390 <EXPRESSION,BOTH,SCRIPT,VERS_NODE>"\""[^\"]*"\"" {
391                                         /* No matter the state, quotes
392                                            give what's inside */
393                                         yylval.name = xstrdup (yytext + 1);
394                                         yylval.name[yyleng - 2] = 0;
395                                         return NAME;
396                                 }
397 <BOTH,SCRIPT,EXPRESSION>"\n"            { lineno++;}
398 <MRI,BOTH,SCRIPT,EXPRESSION>[ \t\r]+    { }
399
400 <VERS_NODE,VERS_SCRIPT>[:,;]    { return *yytext; }
401
402 <VERS_NODE>global               { RTOKEN(GLOBAL); }
403
404 <VERS_NODE>local                { RTOKEN(LOCAL); }
405
406 <VERS_NODE>extern               { RTOKEN(EXTERN); }
407
408 <VERS_NODE>{V_IDENTIFIER}       { yylval.name = xstrdup (yytext);
409                                   return VERS_IDENTIFIER; }
410
411 <VERS_SCRIPT>{V_TAG}            { yylval.name = xstrdup (yytext);
412                                   return VERS_TAG; }
413
414 <VERS_START>"{"                 { BEGIN(VERS_SCRIPT); return *yytext; }
415
416 <VERS_SCRIPT>"{"                { BEGIN(VERS_NODE);
417                                   vers_node_nesting = 0;
418                                   return *yytext;
419                                 }
420 <VERS_SCRIPT>"}"                { return *yytext; }
421 <VERS_NODE>"{"                  { vers_node_nesting++; return *yytext; }
422 <VERS_NODE>"}"                  { if (--vers_node_nesting < 0)
423                                     BEGIN(VERS_SCRIPT);
424                                   return *yytext;
425                                 }
426
427 <VERS_START,VERS_NODE,VERS_SCRIPT>[\n]          { lineno++; }
428
429 <VERS_START,VERS_NODE,VERS_SCRIPT>#.*           { /* Eat up comments */ }
430
431 <VERS_START,VERS_NODE,VERS_SCRIPT>[ \t\r]+      { /* Eat up whitespace */ }
432
433 <<EOF>> {
434   include_stack_ptr--;
435
436   if (include_stack_ptr == 0)
437   {
438     yyterminate ();
439   }
440   else
441   {
442     yy_switch_to_buffer (include_stack[include_stack_ptr]);
443   }
444
445   ldfile_input_filename = file_name_stack[include_stack_ptr - 1];
446   lineno = lineno_stack[include_stack_ptr];
447
448   return END;
449 }
450
451 <SCRIPT,MRI,VERS_START,VERS_SCRIPT,VERS_NODE>.  lex_warn_invalid (" in script", yytext);
452 <EXPRESSION,DEFSYMEXP,BOTH>.    lex_warn_invalid (" in expression", yytext);
453
454 %%
455 \f
456
457 /* Switch flex to reading script file NAME, open on FILE,
458    saving the current input info on the include stack.  */
459
460 void
461 lex_push_file (FILE *file, const char *name)
462 {
463   if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
464     {
465       einfo ("%F:includes nested too deeply\n");
466     }
467   file_name_stack[include_stack_ptr] = name;
468   lineno_stack[include_stack_ptr] = lineno;
469   include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
470
471   include_stack_ptr++;
472   lineno = 1;
473   yyin = file;
474   yy_switch_to_buffer (yy_create_buffer (yyin, YY_BUF_SIZE));
475 }
476
477 /* Return a newly created flex input buffer containing STRING,
478    which is SIZE bytes long.  */
479
480 static YY_BUFFER_STATE
481 yy_create_string_buffer (const char *string, size_t size)
482 {
483   YY_BUFFER_STATE b;
484
485   /* Calls to m-alloc get turned by sed into xm-alloc.  */
486   b = malloc (sizeof (struct yy_buffer_state));
487   b->yy_input_file = 0;
488   b->yy_buf_size = size;
489
490   /* yy_ch_buf has to be 2 characters longer than the size given because
491      we need to put in 2 end-of-buffer characters.  */
492   b->yy_ch_buf = malloc ((unsigned) (b->yy_buf_size + 3));
493
494   b->yy_ch_buf[0] = '\n';
495   strcpy (b->yy_ch_buf+1, string);
496   b->yy_ch_buf[size+1] = YY_END_OF_BUFFER_CHAR;
497   b->yy_ch_buf[size+2] = YY_END_OF_BUFFER_CHAR;
498   b->yy_n_chars = size+1;
499   b->yy_buf_pos = &b->yy_ch_buf[1];
500
501   b->yy_is_our_buffer = 1;
502   b->yy_is_interactive = 0;
503   b->yy_at_bol = 1;
504   b->yy_fill_buffer = 0;
505
506   /* flex 2.4.7 changed the interface.  FIXME: We should not be using
507      a flex internal interface in the first place!  */
508 #ifdef YY_BUFFER_NEW
509   b->yy_buffer_status = YY_BUFFER_NEW;
510 #else
511   b->yy_eof_status = EOF_NOT_SEEN;
512 #endif
513
514   return b;
515 }
516
517 /* Switch flex to reading from STRING, saving the current input info
518    on the include stack.  */
519
520 void
521 lex_redirect (const char *string)
522 {
523   YY_BUFFER_STATE tmp;
524
525   yy_init = 0;
526   if (include_stack_ptr >= MAX_INCLUDE_DEPTH)
527     {
528       einfo("%F: macros nested too deeply\n");
529     }
530   file_name_stack[include_stack_ptr] = "redirect";
531   lineno_stack[include_stack_ptr] = lineno;
532   include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
533   include_stack_ptr++;
534   lineno = 1;
535   tmp = yy_create_string_buffer (string, strlen (string));
536   yy_switch_to_buffer (tmp);
537 }
538 \f
539 /* Functions to switch to a different flex start condition,
540    saving the current start condition on `state_stack'.  */
541
542 static int state_stack[MAX_INCLUDE_DEPTH * 2];
543 static int *state_stack_p = state_stack;
544
545 void
546 ldlex_script (void)
547 {
548   *(state_stack_p)++ = yy_start;
549   BEGIN (SCRIPT);
550 }
551
552 void
553 ldlex_mri_script (void)
554 {
555   *(state_stack_p)++ = yy_start;
556   BEGIN (MRI);
557 }
558
559 void
560 ldlex_version_script (void)
561 {
562   *(state_stack_p)++ = yy_start;
563   BEGIN (VERS_START);
564 }
565
566 void
567 ldlex_version_file (void)
568 {
569   *(state_stack_p)++ = yy_start;
570   BEGIN (VERS_SCRIPT);
571 }
572
573 void
574 ldlex_defsym (void)
575 {
576   *(state_stack_p)++ = yy_start;
577   BEGIN (DEFSYMEXP);
578 }
579
580 void
581 ldlex_expression (void)
582 {
583   *(state_stack_p)++ = yy_start;
584   BEGIN (EXPRESSION);
585 }
586
587 void
588 ldlex_both (void)
589 {
590   *(state_stack_p)++ = yy_start;
591   BEGIN (BOTH);
592 }
593
594 void
595 ldlex_popstate (void)
596 {
597   yy_start = *(--state_stack_p);
598 }
599 \f
600
601 /* Place up to MAX_SIZE characters in BUF and return in *RESULT
602    either the number of characters read, or 0 to indicate EOF.  */
603
604 static void
605 yy_input (char *buf, int *result, int max_size)
606 {
607   *result = 0;
608   if (YY_CURRENT_BUFFER->yy_input_file)
609     {
610       if (yyin)
611         {
612           *result = fread (buf, 1, max_size, yyin);
613           if (*result < max_size && ferror (yyin))
614             einfo ("%F%P: read in flex scanner failed\n");
615         }
616     }
617 }
618
619 /* Eat the rest of a C-style comment.  */
620
621 static void
622 comment (void)
623 {
624   int c;
625
626   while (1)
627   {
628     c = input();
629     while (c != '*' && c != EOF)
630     {
631       if (c == '\n')
632         lineno++;
633       c = input();
634     }
635
636     if (c == '*')
637     {
638       c = input();
639       while (c == '*')
640        c = input();
641       if (c == '/')
642        break;                   /* found the end */
643     }
644
645     if (c == '\n')
646       lineno++;
647
648     if (c == EOF)
649     {
650       einfo( "%F%P: EOF in comment\n");
651       break;
652     }
653   }
654 }
655
656 /* Warn the user about a garbage character WHAT in the input
657    in context WHERE.  */
658
659 static void
660 lex_warn_invalid (char *where, char *what)
661 {
662   char buf[5];
663
664   /* If we have found an input file whose format we do not recognize,
665      and we are therefore treating it as a linker script, and we find
666      an invalid character, then most likely this is a real object file
667      of some different format.  Treat it as such.  */
668   if (ldfile_assumed_script)
669     {
670       bfd_set_error (bfd_error_file_not_recognized);
671       einfo ("%F%s: file not recognized: %E\n", ldfile_input_filename);
672     }
673
674   if (! ISPRINT (*what))
675     {
676       sprintf (buf, "\\%03o", (unsigned int) *what);
677       what = buf;
678     }
679
680   einfo ("%P:%S: ignoring invalid character `%s'%s\n", what, where);
681 }