Use getopt instead of lex and yacc to parse the command line.
[platform/upstream/binutils.git] / ld / ldgram.y
1 /* A YACC grammer to parse a superset of the AT&T linker scripting languaue.
2    Copyright (C) 1991, 1993 Free Software Foundation, Inc.
3    Written by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
4
5 This file is part of GNU ld.
6
7 This program 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 of the License, or
10 (at your option) any later version.
11
12 This program 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 this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 %{
22 /*
23
24  */
25
26 #define DONTDECLARE_MALLOC
27
28 #include "bfd.h"
29 #include "sysdep.h"
30 #include "bfdlink.h"
31 #include "ld.h"    
32 #include "ldexp.h"
33 #include "ldver.h"
34 #include "ldlang.h"
35 #include "ldemul.h"
36 #include "ldfile.h"
37 #include "ldmisc.h"
38 #include "ldmain.h"
39 #include "mri.h"
40 #include "ldlex.h"
41
42 #define YYDEBUG 1
43
44 static int typebits;
45
46 static char *dirlist_ptr;
47
48 lang_memory_region_type *region;
49
50
51 char *current_file;
52 boolean ldgram_want_filename = true;
53 boolean had_script = false;
54 boolean force_make_executable = false;
55
56 boolean ldgram_in_script = false;
57 boolean ldgram_had_equals = false;
58
59
60 #define ERROR_NAME_MAX 20
61 static char *error_names[ERROR_NAME_MAX];
62 static int error_index;
63 #define PUSH_ERROR(x) if (error_index < ERROR_NAME_MAX) error_names[error_index] = x; error_index++;
64 #define POP_ERROR()   error_index--;
65 %}
66 %union {
67   bfd_vma integer;
68   char *name;
69   int token;
70   union etree_union *etree;
71 }
72
73 %type <etree> exp  opt_exp_with_type  mustbe_exp opt_at
74 %type <integer> fill_opt
75 %type <name> memspec_opt
76 %token <integer> INT  
77 %token <name> NAME
78 %type  <integer> length
79
80 %right <token> PLUSEQ MINUSEQ MULTEQ DIVEQ  '=' LSHIFTEQ RSHIFTEQ   ANDEQ OREQ 
81 %right <token> '?' ':'
82 %left <token> OROR
83 %left <token>  ANDAND
84 %left <token> '|'
85 %left <token>  '^'
86 %left  <token> '&'
87 %left <token>  EQ NE
88 %left  <token> '<' '>' LE GE
89 %left  <token> LSHIFT RSHIFT
90
91 %left  <token> '+' '-'
92 %left  <token> '*' '/' '%'
93
94 %right UNARY
95 %token END 
96 %left <token> '('
97 %token <token> ALIGN_K BLOCK QUAD LONG SHORT BYTE
98 %token SECTIONS
99 %token '{' '}'
100 %token SIZEOF_HEADERS OUTPUT_FORMAT FORCE_COMMON_ALLOCATION OUTPUT_ARCH
101 %token SIZEOF_HEADERS
102 %token INCLUDE
103 %token MEMORY DEFSYMEND
104 %token NOLOAD DSECT COPY INFO OVERLAY
105 %token NAME DEFINED TARGET_K SEARCH_DIR MAP ENTRY
106 %token <integer> SIZEOF NEXT ADDR
107 %token STARTUP HLL SYSLIB FLOAT NOFLOAT
108 %token ORIGIN FILL
109 %token LENGTH CREATE_OBJECT_SYMBOLS INPUT OUTPUT CONSTRUCTORS
110 %token ALIGNMOD AT
111 %type <token> assign_op 
112 %type <name>  filename
113 %token CHIP LIST SECT ABSOLUTE  LOAD NEWLINE ENDWORD ORDER NAMEWORD
114 %token FORMAT PUBLIC DEFSYMEND BASE ALIAS TRUNCATE REL
115 %token INPUT_SCRIPT INPUT_MRI_SCRIPT INPUT_DEFSYM
116
117 %%
118
119 file:   
120                 INPUT_SCRIPT script_file
121         |       INPUT_MRI_SCRIPT mri_script_file
122         |       INPUT_DEFSYM defsym_expr
123         ;
124
125
126 filename:  NAME;
127
128
129 defsym_expr:
130                 { ldlex_defsym(); }
131                 NAME '=' exp
132                 {
133                   ldlex_popstate();
134                   lang_add_assignment(exp_assop($3,$2,$4));
135                 }
136
137 /* SYNTAX WITHIN AN MRI SCRIPT FILE */  
138 mri_script_file:
139                 {       ldlex_mri_script();
140                         PUSH_ERROR("MRI style script");
141                 }
142              mri_script_lines
143                 {       ldlex_popstate(); 
144                         POP_ERROR();
145                 }
146         ;
147
148 mri_script_lines:
149                 mri_script_lines mri_script_command NEWLINE
150           |
151         ;
152
153 mri_script_command:
154                 CHIP  exp 
155         |       CHIP  exp ',' exp
156         |       NAME    {
157                         einfo("%P%F: unrecognised keyword in MRI style script '%s'\n",$1);
158                         }
159         |       LIST    {
160                         write_map = true;
161                         config.map_filename = "-";
162                         }
163         |       ORDER ordernamelist
164         |       ENDWORD 
165         |       PUBLIC NAME '=' exp 
166                         { mri_public($2, $4); }
167         |       PUBLIC NAME ',' exp 
168                         { mri_public($2, $4); }
169         |       PUBLIC NAME  exp 
170                         { mri_public($2, $3); }
171         |       FORMAT NAME
172                         { mri_format($2); }
173         |       SECT NAME ',' exp 
174                         { mri_output_section($2, $4);}
175         |       SECT NAME  exp
176                         { mri_output_section($2, $3);}
177         |       SECT NAME '=' exp
178                         { mri_output_section($2, $4);}
179         |       ALIGN_K NAME '=' exp
180                         { mri_align($2,$4); }
181         |       ALIGNMOD NAME '=' exp
182                         { mri_alignmod($2,$4); }
183         |       ABSOLUTE mri_abs_name_list
184         |       LOAD     mri_load_name_list
185         |       NAMEWORD NAME 
186                         { mri_name($2); }   
187         |       ALIAS NAME ',' NAME
188                         { mri_alias($2,$4,0);}
189         |       ALIAS NAME ',' INT
190                         { mri_alias($2,0,(int) $4);}
191         |       BASE     exp
192                         { mri_base($2); }
193         |       TRUNCATE INT
194                 {  mri_truncate((unsigned int) $2); }
195         |
196         ;
197
198 ordernamelist:
199               ordernamelist ',' NAME         { mri_order($3); }
200         |     ordernamelist  NAME         { mri_order($2); }
201         |
202         ;
203
204 mri_load_name_list:
205                 NAME
206                         { mri_load($1); }
207         |       mri_load_name_list ',' NAME { mri_load($3); }
208         ;
209
210 mri_abs_name_list:
211                 NAME
212                         { mri_only_load($1); }
213         |       mri_abs_name_list ','  NAME
214                         { mri_only_load($3); }
215         ;
216
217 script_file:
218         {
219          ldlex_both();
220         }
221        ifile_list
222         {
223         ldlex_popstate();
224         }
225         ;
226
227
228 ifile_list:
229        ifile_list ifile_p1
230         |
231         ;
232
233
234
235 ifile_p1:
236                 memory
237         |       sections
238         |       startup
239         |       high_level_library
240         |       low_level_library
241         |       floating_point_support
242         |       statement_anywhere
243         |        ';'
244         |       TARGET_K '(' NAME ')'
245                 { lang_add_target($3); }
246         |       SEARCH_DIR '(' filename ')'
247                 { ldfile_add_library_path($3); }
248         |       OUTPUT '(' filename ')'
249                 { lang_add_output($3, 1); }
250         |       OUTPUT_FORMAT '(' NAME ')'
251                   { lang_add_output_format($3, 1); }
252         |       OUTPUT_ARCH '(' NAME ')'
253                   { ldfile_set_output_arch($3); }
254         |       FORCE_COMMON_ALLOCATION
255                 { command_line.force_common_definition = true ; }
256         |       INPUT '(' input_list ')'
257         |       MAP '(' filename ')'
258                 { lang_add_map($3); }
259         |       INCLUDE filename 
260                 { ldfile_open_command_file($2); } ifile_list END
261         ;
262
263 input_list:
264                 NAME
265                 { lang_add_input_file($1,lang_input_file_is_search_file_enum,
266                                  (char *)NULL); }
267         |       input_list ',' NAME
268                 { lang_add_input_file($3,lang_input_file_is_search_file_enum,
269                                  (char *)NULL); }
270         |       input_list NAME
271                 { lang_add_input_file($2,lang_input_file_is_search_file_enum,
272                                  (char *)NULL); }
273         ;
274
275 sections:
276                 SECTIONS '{' sec_or_group_p1 '}'
277         ;
278
279 sec_or_group_p1:
280                 sec_or_group_p1 section
281         |       sec_or_group_p1 statement_anywhere
282         |
283         ;
284
285 statement_anywhere:
286                 ENTRY '(' NAME ')'
287                 { lang_add_entry($3); }
288         |       assignment end
289         ;
290
291 file_NAME_list:
292                 NAME
293                         { lang_add_wild($1, current_file); }
294         |       file_NAME_list opt_comma NAME
295                         { lang_add_wild($3, current_file); }
296         ;
297
298 input_section_spec:
299                 NAME
300                 {
301                 lang_add_wild((char *)NULL, $1);
302                 }
303         |       '['
304                         {
305                         current_file = (char *)NULL;
306                         }
307                         file_NAME_list
308                 ']'
309         |       NAME
310                         {
311                         current_file =$1;
312                         }
313                 '(' file_NAME_list ')'
314         |       '*'
315                         {
316                         current_file = (char *)NULL;
317                         }
318                 '(' file_NAME_list ')'
319         ;
320
321 statement:
322                 assignment end
323         |       CREATE_OBJECT_SYMBOLS
324                 {
325                 lang_add_attribute(lang_object_symbols_statement_enum); 
326                 }
327         |       ';'
328         |       CONSTRUCTORS
329                 {
330                 
331                   lang_add_attribute(lang_constructors_statement_enum); 
332                 }
333         | input_section_spec
334         | length '(' exp ')'
335                         {
336                         lang_add_data((int) $1,$3);
337                         }
338   
339         | FILL '(' exp ')'
340                         {
341                           lang_add_fill
342                             (exp_get_value_int($3,
343                                                0,
344                                                "fill value",
345                                                lang_first_phase_enum));
346                         }
347         ;
348
349 statement_list:
350                 statement_list statement
351         |       statement
352         ;
353   
354 statement_list_opt:
355                 /* empty */
356         |       statement_list
357         ;
358
359 length:
360                 QUAD
361                         { $$ = $1; }
362         |       LONG
363                         { $$ = $1; }
364         |       SHORT
365                         { $$ = $1; }
366         |       BYTE
367                         { $$ = $1; }
368         ;
369
370 fill_opt:
371           '=' mustbe_exp
372                 {
373                   $$ =   exp_get_value_int($2,
374                                            0,
375                                            "fill value",
376                                            lang_first_phase_enum);
377                 }
378         |       { $$ = 0; }
379         ;
380
381                 
382
383 assign_op:
384                 PLUSEQ
385                         { $$ = '+'; }
386         |       MINUSEQ
387                         { $$ = '-'; }
388         |       MULTEQ
389                         { $$ = '*'; }
390         |       DIVEQ
391                         { $$ = '/'; }
392         |       LSHIFTEQ
393                         { $$ = LSHIFT; }
394         |       RSHIFTEQ
395                         { $$ = RSHIFT; }
396         |       ANDEQ
397                         { $$ = '&'; }
398         |       OREQ
399                         { $$ = '|'; }
400
401         ;
402
403 end:    ';' | ','
404         ;
405
406
407 assignment:
408                 NAME '=' mustbe_exp
409                 {
410                   lang_add_assignment(exp_assop($2,$1,$3));
411                 }
412         |       NAME assign_op mustbe_exp
413                 {
414                 
415 lang_add_assignment(exp_assop('=',$1,exp_binop($2,exp_nameop(NAME,$1),$3)));
416                 }
417                 
418         ;
419
420
421 opt_comma:
422                 ','     |       ;
423
424
425 memory:
426                 MEMORY '{' memory_spec memory_spec_list '}'
427         ;
428
429 memory_spec_list:
430                 memory_spec_list memory_spec
431         |       memory_spec_list ',' memory_spec
432         |
433         ;
434
435
436 memory_spec:            NAME
437                         { region = lang_memory_region_lookup($1); }
438                 attributes_opt ':'
439                 origin_spec opt_comma length_spec
440
441         ; origin_spec:
442         ORIGIN '=' mustbe_exp
443                 { region->current =
444                  region->origin =
445                  exp_get_vma($3, 0L,"origin", lang_first_phase_enum);
446 }
447         ; length_spec:
448              LENGTH '=' mustbe_exp
449                { region->length = exp_get_vma($3,
450                                                ~((bfd_vma)0),
451                                                "length",
452                                                lang_first_phase_enum);
453                 }
454         
455
456 attributes_opt:
457                   '(' NAME ')'
458                         {
459                         lang_set_flags(&region->flags, $2);
460                         }
461         |
462   
463         ;
464
465 startup:
466         STARTUP '(' filename ')'
467                 { lang_startup($3); }
468         ;
469
470 high_level_library:
471                 HLL '(' high_level_library_NAME_list ')'
472         |       HLL '(' ')'
473                         { ldemul_hll((char *)NULL); }
474         ;
475
476 high_level_library_NAME_list:
477                 high_level_library_NAME_list opt_comma filename
478                         { ldemul_hll($3); }
479         |       filename
480                         { ldemul_hll($1); }
481
482         ;
483
484 low_level_library:
485         SYSLIB '(' low_level_library_NAME_list ')'
486         ; low_level_library_NAME_list:
487                 low_level_library_NAME_list opt_comma filename
488                         { ldemul_syslib($3); }
489         |
490         ;
491
492 floating_point_support:
493                 FLOAT
494                         { lang_float(true); }
495         |       NOFLOAT
496                         { lang_float(false); }
497         ;
498                 
499
500 mustbe_exp:              { ldlex_expression(); }
501                 exp
502                          { ldlex_popstate(); $$=$2;}
503         ;
504
505 exp     :
506                 '-' exp %prec UNARY
507                         { $$ = exp_unop('-', $2); }
508         |       '(' exp ')'
509                         { $$ = $2; }
510         |       NEXT '(' exp ')' %prec UNARY
511                         { $$ = exp_unop((int) $1,$3); }
512         |       '!' exp %prec UNARY
513                         { $$ = exp_unop('!', $2); }
514         |       '+' exp %prec UNARY
515                         { $$ = $2; }
516         |       '~' exp %prec UNARY
517                         { $$ = exp_unop('~', $2);}
518
519         |       exp '*' exp
520                         { $$ = exp_binop('*', $1, $3); }
521         |       exp '/' exp
522                         { $$ = exp_binop('/', $1, $3); }
523         |       exp '%' exp
524                         { $$ = exp_binop('%', $1, $3); }
525         |       exp '+' exp
526                         { $$ = exp_binop('+', $1, $3); }
527         |       exp '-' exp
528                         { $$ = exp_binop('-' , $1, $3); }
529         |       exp LSHIFT exp
530                         { $$ = exp_binop(LSHIFT , $1, $3); }
531         |       exp RSHIFT exp
532                         { $$ = exp_binop(RSHIFT , $1, $3); }
533         |       exp EQ exp
534                         { $$ = exp_binop(EQ , $1, $3); }
535         |       exp NE exp
536                         { $$ = exp_binop(NE , $1, $3); }
537         |       exp LE exp
538                         { $$ = exp_binop(LE , $1, $3); }
539         |       exp GE exp
540                         { $$ = exp_binop(GE , $1, $3); }
541         |       exp '<' exp
542                         { $$ = exp_binop('<' , $1, $3); }
543         |       exp '>' exp
544                         { $$ = exp_binop('>' , $1, $3); }
545         |       exp '&' exp
546                         { $$ = exp_binop('&' , $1, $3); }
547         |       exp '^' exp
548                         { $$ = exp_binop('^' , $1, $3); }
549         |       exp '|' exp
550                         { $$ = exp_binop('|' , $1, $3); }
551         |       exp '?' exp ':' exp
552                         { $$ = exp_trinop('?' , $1, $3, $5); }
553         |       exp ANDAND exp
554                         { $$ = exp_binop(ANDAND , $1, $3); }
555         |       exp OROR exp
556                         { $$ = exp_binop(OROR , $1, $3); }
557         |       DEFINED '(' NAME ')'
558                         { $$ = exp_nameop(DEFINED, $3); }
559         |       INT
560                         { $$ = exp_intop($1); }
561         |       SIZEOF_HEADERS
562                         { $$ = exp_nameop(SIZEOF_HEADERS,0); }
563
564         |       SIZEOF '(' NAME ')'
565                         { $$ = exp_nameop(SIZEOF,$3); }
566         |       ADDR '(' NAME ')'
567                         { $$ = exp_nameop(ADDR,$3); }
568         |       ABSOLUTE '(' exp ')'
569                         { $$ = exp_unop(ABSOLUTE, $3); }
570         |       ALIGN_K '(' exp ')'
571                         { $$ = exp_unop(ALIGN_K,$3); }
572         |       NAME
573                         { $$ = exp_nameop(NAME,$1); }
574         ;
575
576
577 opt_at:
578                 AT '(' exp ')' { $$ = $3; }
579         |       { $$ = 0; }
580         ;
581
582 section:        NAME            { ldlex_expression(); }
583                 opt_exp_with_type 
584                 opt_at          { ldlex_popstate(); }
585                 '{'
586                         {
587                         lang_enter_output_section_statement($1,$3,typebits,0,0,0,$4);
588                         }
589                 statement_list_opt      
590                 '}' {ldlex_expression();} fill_opt memspec_opt
591                 {
592                   ldlex_popstate();
593                   lang_leave_output_section_statement($11, $12);
594                 }
595 opt_comma
596
597         ;
598
599 type:
600            NOLOAD  { typebits = SEC_NEVER_LOAD; }
601         |  DSECT   { typebits = 0; }
602         |  COPY    { typebits = 0; }
603         |  INFO    { typebits = 0; }
604         |  OVERLAY { typebits = 0; }
605         | { typebits = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS; }
606         ;
607
608
609 opt_exp_with_type:
610                 exp ':'                 { $$ = $1; typebits =0;}
611         |       exp '(' type ')' ':'    { $$ = $1; }
612         |       ':'                     { $$= (etree_type *)NULL; typebits = 0; }
613         |       '(' type ')' ':'        { $$= (etree_type *)NULL;  }
614         ;
615
616 memspec_opt:
617                 '>' NAME
618                 { $$ = $2; }
619         |       { $$ = "*default*"; }
620         ;
621 %%
622 void
623 yyerror(arg) 
624      const char *arg;
625
626   if (error_index > 0 && error_index < ERROR_NAME_MAX)
627      einfo("%P%F: %S %s in %s\n", arg, error_names[error_index-1]);
628   else
629      einfo("%P%F: %S %s\n", arg);
630 }