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