-y support
[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 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 "ld.h"    
31 #include "ldexp.h"
32 #include "ldver.h"
33 #include "ldlang.h"
34 #include "ldemul.h"
35 #include "ldfile.h"
36 #include "ldmisc.h"
37 #include "mri.h"
38
39 #define YYDEBUG 1
40
41 boolean option_v;
42 extern unsigned int lineno;
43 extern boolean trace_files;
44 extern boolean write_map;
45 extern boolean option_longmap;
46 boolean hex_mode;
47
48 strip_symbols_type strip_symbols=STRIP_NONE;
49 discard_locals_type discard_locals=DISCARD_NONE;
50
51
52 lang_memory_region_type *region;
53
54
55 lang_memory_region_type *lang_memory_region_lookup();
56 lang_output_section_statement_type *lang_output_section_statement_lookup();
57
58 #ifdef __STDC__
59
60 void lang_add_data(int type, union etree_union *exp);
61 void lang_enter_output_section_statement(char *output_section_statement_name, etree_type *address_exp, int flags, bfd_vma block_value);
62
63 #else
64
65 void lang_add_data();
66 void lang_enter_output_section_statement();
67
68 #endif /* __STDC__ */
69
70 extern args_type command_line;
71 char *current_file;
72 boolean ldgram_want_filename = true;
73 boolean had_script = false;
74 boolean force_make_executable = false;
75
76 boolean ldgram_in_script = false;
77 boolean ldgram_had_equals = false;
78 /* LOCALS */
79
80
81 #define ERROR_NAME_MAX 20
82 static char *error_names[ERROR_NAME_MAX];
83 static int error_index;
84 #define PUSH_ERROR(x) if (error_index < ERROR_NAME_MAX) error_names[error_index] = x; error_index++;
85 #define POP_ERROR()   error_index--;
86 %}
87 %union {
88   bfd_vma integer;
89   int voidval;
90   char *name;
91   int token;
92   union etree_union *etree;
93 struct sec *section;
94   struct lang_output_section_statement_struct *output_section_statement;
95   union  lang_statement_union **statement_ptr;
96   int lineno;
97   struct {
98     FILE *file;
99     char *name;
100     unsigned int lineno;
101   } state;
102
103   
104 }
105
106 %type <etree> exp  opt_exp  mustbe_exp
107 %type <integer> fill_opt opt_block opt_type
108 %type <name> memspec_opt
109 %token <integer> INT  
110 %token <name> NAME
111 %type  <integer> length
112
113 %right <token> PLUSEQ MINUSEQ MULTEQ DIVEQ  '=' LSHIFTEQ RSHIFTEQ   ANDEQ OREQ 
114 %right <token> '?' ':'
115 %left <token> OROR
116 %left <token>  ANDAND
117 %left <token> '|'
118 %left <token>  '^'
119 %left  <token> '&'
120 %left <token>  EQ NE
121 %left  <token> '<' '>' LE GE
122 %left  <token> LSHIFT RSHIFT
123
124 %left  <token> '+' '-'
125 %left  <token> '*' '/' '%'
126
127 /*%token <token> '+' '-' '*' '/' '%'*/
128 %right UNARY
129 %token END 
130 %left <token> '('
131 %token <token> ALIGN_K BLOCK LONG SHORT BYTE
132 %token SECTIONS  
133 %token '{' '}'
134 %token SIZEOF_HEADERS OUTPUT_FORMAT FORCE_COMMON_ALLOCATION OUTPUT_ARCH
135 %token SIZEOF_HEADERS
136 %token MEMORY  DEFSYMEND
137 %token NOLOAD DSECT COPY INFO OVERLAY
138 %token NAME DEFINED TARGET_K SEARCH_DIR MAP ENTRY 
139 %token OPTION_e OPTION_c OPTION_noinhibit_exec OPTION_s OPTION_S OPTION_sort_common
140 %token OPTION_format  OPTION_F OPTION_u OPTION_Bstatic OPTION_N
141 %token <integer> SIZEOF NEXT ADDR 
142 %token OPTION_d OPTION_dc OPTION_dp OPTION_x OPTION_X OPTION_defsym
143 %token OPTION_v OPTION_V OPTION_M OPTION_t STARTUP HLL SYSLIB FLOAT  NOFLOAT 
144 %token OPTION_Map
145 %token OPTION_n OPTION_r OPTION_o OPTION_b  OPTION_R OPTION_relax
146 %token <name> OPTION_l OPTION_L  OPTION_T OPTION_Aarch OPTION_Tfile  OPTION_Texp
147 %token OPTION_Ur 
148 %token ORIGIN FILL OPTION_g
149 %token LENGTH    CREATE_OBJECT_SYMBOLS INPUT OUTPUT  CONSTRUCTORS 
150 %type <token> assign_op 
151
152 %type <name>  filename
153
154
155 %token CHIP LIST SECT ABSOLUTE  LOAD NEWLINE ENDWORD ORDER NAMEWORD
156 %token FORMAT PUBLIC DEFSYMEND BASE ALIAS
157
158 %{
159 ld_config_type config;
160 %}
161
162 %%
163
164 file:   command_line  { lang_final(); };
165
166
167 filename:  NAME;
168
169
170 command_line:
171                 command_line command_line_option
172         |
173         ;
174
175 command_line_option:
176                 OPTION_Bstatic { }
177         |       OPTION_v
178                         {       
179                         ldversion(0);
180                         option_v = true;
181                         }
182         |       OPTION_V
183                         {       
184                         ldversion(1);
185                         option_v = true;
186                         }
187         |       OPTION_t {
188                         trace_files = true;
189                         }
190         |     OPTION_Map  NAME
191                 {
192                 write_map = true;
193                 config.map_filename = $2;
194                 }
195         |       OPTION_M {
196             config.map_filename = "-";
197                             
198                         }
199         |       OPTION_n {
200                         config.magic_demand_paged = false;
201                         }
202         |       OPTION_N {
203                         config.text_read_only = false;
204                         config.magic_demand_paged = false;
205                         }
206         |       OPTION_s {
207                         strip_symbols = STRIP_ALL;
208                         }
209         |       OPTION_S {
210                         strip_symbols = STRIP_DEBUGGER;
211                         }
212         |       OPTION_u NAME {
213                         ldlang_add_undef($2);
214                 }
215             
216         |       OPTION_r {
217                         config.relocateable_output = true;
218                         config.build_constructors = false;
219                         config.magic_demand_paged = false;
220                         config.text_read_only = false;
221                         }
222         |       OPTION_Ur {
223                         config.relocateable_output = true;
224                         config.build_constructors = true;
225                         config.magic_demand_paged = false;
226                         config.text_read_only = false;
227                       }             
228         |       OPTION_o filename
229                         {
230                         lang_add_output($2); 
231                         }
232         |       OPTION_e NAME
233                         { lang_add_entry($2); 
234                         }
235         |       OPTION_X {
236                         discard_locals = DISCARD_L;
237                 }
238         |       OPTION_x {
239                         discard_locals = DISCARD_ALL;
240                 }
241
242         |       OPTION_noinhibit_exec
243                         {
244                         force_make_executable = true;
245                         }
246         |      OPTION_sort_common {
247         config.sort_common = true;
248       }
249         |      OPTION_d {
250                           command_line.force_common_definition = true;
251                         }
252
253         |      OPTION_relax {
254                           command_line.relax = true;
255                         }
256         |      OPTION_dc
257                          {
258                           command_line.force_common_definition = true;
259                         }
260         |       OPTION_g
261                         {
262                         /* Ignored */
263                         }
264         |       OPTION_dp
265                          {
266                           command_line.force_common_definition = true;
267                         }
268         |       OPTION_format NAME
269                    {
270                           lang_add_target($2);
271                    }
272         |       OPTION_Texp 
273                 { ldlex_expression();
274                         hex_mode  = 16; 
275                 } 
276                 INT
277                 {  ldlex_popstate();
278                         lang_section_start($1,exp_intop($3));
279                         hex_mode = 0; 
280                 }
281         
282         |       OPTION_Aarch 
283                 { 
284                         ldfile_add_arch($1); 
285                 }
286         |        OPTION_b NAME
287                         {
288                         lang_add_target($2);
289                         }
290         |       OPTION_L
291                         {
292                         ldfile_add_library_path($1);
293                         }
294         |       OPTION_F
295                 {
296                 /* Ignore */
297                 }
298         |       NAME
299                 { lang_add_input_file($1,lang_input_file_is_file_enum,
300                                  (char *)NULL); }
301         |       OPTION_c filename 
302                         { ldfile_open_command_file($2); } mri_script_file  END {  ldlex_command();}
303
304         |       OPTION_Tfile 
305                         { ldfile_open_command_file($1); } script_file
306                         END {  ldlex_command();}
307
308         |       OPTION_T filename 
309                         { ldfile_open_command_file($2); } script_file
310 END {  ldlex_command();}
311
312         |       OPTION_l
313                         {
314                           lang_add_input_file($1,
315                                          lang_input_file_is_l_enum,
316                                          (char *)NULL);
317                         }
318         |       OPTION_R filename
319                         {
320                         lang_add_input_file($2,
321                                 lang_input_file_is_symbols_only_enum,
322                                 (char *)NULL);
323                         }
324
325         |       OPTION_defsym  { ldlex_defsym(); }
326                 NAME     '=' exp  DEFSYMEND { ldlex_popstate();
327                         lang_add_assignment(exp_assop($4,$3,$5));
328                         }       
329         | '-' NAME
330                  { info("%P%F Unrecognized option -%s\n", $2);  }
331
332         | '{' script_file '}'  
333         ;
334
335
336 /* SYNTAX WITHIN AN MRI SCRIPT FILE */  
337 mri_script_file:
338                 {       ldlex_mri_script();
339                         PUSH_ERROR("MRI style script");
340                 }
341              mri_script_lines
342                 {       ldlex_popstate(); 
343                         POP_ERROR();
344                 }
345         ;
346
347 mri_script_lines:
348                 mri_script_lines mri_script_command NEWLINE
349           |
350         ;
351
352 mri_script_command:
353                 CHIP  exp 
354         |       CHIP  exp ',' exp
355         |       NAME    {
356                         einfo("%P%F: unrecognised keyword in MRI style script '%s'\n",$1);
357                         }
358         |       LIST    {
359                         write_map = true;
360                         config.map_filename = "-";
361                         }
362         |       ORDER ordernamelist
363         |       ENDWORD 
364         |       PUBLIC NAME '=' exp 
365                         { mri_public($2, $4); }
366         |       PUBLIC NAME ',' exp 
367                         { mri_public($2, $4); }
368         |       PUBLIC NAME  exp 
369                         { mri_public($2, $3); }
370         |       FORMAT NAME
371                         { mri_format($2); }
372         |       SECT NAME ',' exp 
373                         { mri_output_section($2, $4);}
374         |       SECT NAME  exp
375                         { mri_output_section($2, $3);}
376         |       SECT NAME '=' exp
377                         { mri_output_section($2, $4);}
378         |       ABSOLUTE mri_abs_name_list
379         |       LOAD     mri_load_name_list
380         |       NAMEWORD NAME 
381                         { mri_name($2); }   
382         |       ALIAS NAME ',' NAME
383                         { mri_alias($2,$4,0);}
384         |       ALIAS NAME ',' INT
385                         { mri_alias($2,0,$4);}
386         |       BASE     exp
387                         { mri_base($2); }
388         |
389         ;
390
391 ordernamelist:
392               ordernamelist ',' NAME         { mri_order($3); }
393         |     ordernamelist  NAME         { mri_order($2); }
394         |
395         ;
396
397 mri_load_name_list:
398                 NAME
399                         { mri_load($1); }
400         |       mri_load_name_list ',' NAME { mri_load($3); }
401         ;
402
403 mri_abs_name_list:
404                 NAME
405                         { mri_only_load($1); }
406         |       mri_abs_name_list ','  NAME
407                         { mri_only_load($3); }
408         ;
409
410 script_file:
411         {
412          ldlex_both();
413         }
414        ifile_list
415         {
416         ldlex_popstate();
417         }
418         ;
419
420
421 ifile_list:
422        ifile_list ifile_p1
423         |
424         ;
425
426
427
428 ifile_p1:
429                 memory
430         |       sections
431         |       startup
432         |       high_level_library
433         |       low_level_library
434         |       floating_point_support
435         |       statement_anywhere
436         |        ';'
437         |       TARGET_K '(' NAME ')'
438                 { lang_add_target($3); }
439         |       SEARCH_DIR '(' filename ')'
440                 { ldfile_add_library_path($3); }
441         |       OUTPUT '(' filename ')'
442                 { lang_add_output($3); }
443         | OUTPUT_FORMAT '(' NAME ')'
444                   { lang_add_output_format($3); }
445         | OUTPUT_ARCH '(' NAME ')'
446                   { ldfile_set_output_arch($3); }
447         |       FORCE_COMMON_ALLOCATION
448                 { command_line.force_common_definition = true ; }
449         |       INPUT '(' input_list ')'
450         |       MAP '(' filename ')'
451                 { lang_add_map($3); }
452         ;
453
454 input_list:
455                 NAME
456                 { lang_add_input_file($1,lang_input_file_is_file_enum,
457                                  (char *)NULL); }
458         |       input_list ',' NAME
459                 { lang_add_input_file($3,lang_input_file_is_file_enum,
460                                  (char *)NULL); }
461         |       input_list NAME
462                 { lang_add_input_file($2,
463 lang_input_file_is_file_enum,
464                                  (char *)NULL); }
465         ;
466
467 sections:
468                 SECTIONS '{' sec_or_group_p1 '}'
469         ;
470
471 sec_or_group_p1:
472                 sec_or_group_p1 section
473         |       sec_or_group_p1 statement_anywhere
474         |
475         ;
476
477 statement_anywhere:
478                 ENTRY '(' NAME ')'
479                 { lang_add_entry($3); }
480         |       assignment end
481         ;
482
483 file_NAME_list:
484                 NAME
485                         { lang_add_wild($1, current_file); }
486         |       file_NAME_list opt_comma NAME
487                         { lang_add_wild($3, current_file); }
488         ;
489
490 input_section_spec:
491                 NAME
492                 {
493                 lang_add_wild((char *)NULL, $1);
494                 }
495         |       '['
496                         {
497                         current_file = (char *)NULL;
498                         }
499                         file_NAME_list
500                 ']'
501         |       NAME
502                         {
503                         current_file =$1;
504                         }
505                 '(' file_NAME_list ')'
506         |       '*'
507                         {
508                         current_file = (char *)NULL;
509                         }
510                 '(' file_NAME_list ')'
511         ;
512
513 statement:
514                 statement assignment end
515         |       statement CREATE_OBJECT_SYMBOLS
516                 {
517                 
518 lang_add_attribute(lang_object_symbols_statement_enum); }
519         |       statement ';'
520         |       statement CONSTRUCTORS
521                 {
522                 
523 lang_add_attribute(lang_constructors_statement_enum); }
524
525         |       statement input_section_spec
526         | statement length '(' exp ')'
527                         {
528                         lang_add_data($2,$4);
529                         }
530   
531         |       statement FILL '(' exp ')'
532                         {
533                           lang_add_fill
534                             (exp_get_value_int($4,
535                                                0,
536                                                "fill value",
537                                         
538 lang_first_phase_enum));
539                         }
540         |
541         ;
542
543 length:
544                 LONG
545                         { $$ = $1; }
546         |       SHORT
547                         { $$ = $1; }
548         |       BYTE
549                         { $$ = $1; }
550         ;
551
552 fill_opt:
553           '=' mustbe_exp
554                 {
555                   $$ =   exp_get_value_int($2,
556                                            0,
557                                            "fill value",
558                                            lang_first_phase_enum);
559                 }
560         |       { $$ = 0; }
561         ;
562
563                 
564
565 assign_op:
566                 PLUSEQ
567                         { $$ = '+'; }
568         |       MINUSEQ
569                         { $$ = '-'; }
570         |       MULTEQ
571                         { $$ = '*'; }
572         |       DIVEQ
573                         { $$ = '/'; }
574         |       LSHIFTEQ
575                         { $$ = LSHIFT; }
576         |       RSHIFTEQ
577                         { $$ = RSHIFT; }
578         |       ANDEQ
579                         { $$ = '&'; }
580         |       OREQ
581                         { $$ = '|'; }
582
583         ;
584
585 end:    ';' | ','
586         ;
587
588
589 assignment:
590                 NAME '=' mustbe_exp
591                 {
592                   lang_add_assignment(exp_assop($2,$1,$3));
593                 }
594         |       NAME assign_op mustbe_exp
595                 {
596                 
597 lang_add_assignment(exp_assop('=',$1,exp_binop($2,exp_nameop(NAME,$1),$3)));
598                 }
599                 
600         ;
601
602
603 opt_comma:
604                 ','     |       ;
605
606
607 memory:
608                 MEMORY '{' memory_spec memory_spec_list '}'
609         ;
610
611 memory_spec_list:
612                 memory_spec_list memory_spec
613         |       memory_spec_list ',' memory_spec
614         |
615         ;
616
617
618 memory_spec:            NAME
619                         { region = lang_memory_region_lookup($1); }
620                 attributes_opt ':'
621                 origin_spec opt_comma length_spec
622
623         ; origin_spec:
624         ORIGIN '=' mustbe_exp
625                 { region->current =
626                  region->origin =
627                  exp_get_vma($3, 0L,"origin", lang_first_phase_enum);
628 }
629         ; length_spec:
630              LENGTH '=' mustbe_exp
631                { region->length = exp_get_vma($3,
632                                                ~((bfd_vma)0),
633                                                "length",
634                                                lang_first_phase_enum);
635                 }
636         
637
638 attributes_opt:
639                   '(' NAME ')'
640                         {
641                         lang_set_flags(&region->flags, $2);
642                         }
643         |
644   
645         ;
646
647 startup:
648         STARTUP '(' filename ')'
649                 { lang_startup($3); }
650         ;
651
652 high_level_library:
653                 HLL '(' high_level_library_NAME_list ')'
654         |       HLL '(' ')'
655                         { ldemul_hll((char *)NULL); }
656         ;
657
658 high_level_library_NAME_list:
659                 high_level_library_NAME_list opt_comma filename
660                         { ldemul_hll($3); }
661         |       filename
662                         { ldemul_hll($1); }
663
664         ;
665
666 low_level_library:
667         SYSLIB '(' low_level_library_NAME_list ')'
668         ; low_level_library_NAME_list:
669                 low_level_library_NAME_list opt_comma filename
670                         { ldemul_syslib($3); }
671         |
672         ;
673
674 floating_point_support:
675                 FLOAT
676                         { lang_float(true); }
677         |       NOFLOAT
678                         { lang_float(false); }
679         ;
680                 
681
682 mustbe_exp:              { ldlex_expression(); }
683                 exp
684                          { ldlex_popstate(); $$=$2;}
685         ;
686
687 exp     :
688                 '-' exp %prec UNARY
689                         { $$ = exp_unop('-', $2); }
690         |       '(' exp ')'
691                         { $$ = $2; }
692         |       NEXT '(' exp ')' %prec UNARY
693                         { $$ = exp_unop($1,$3); }
694         |       '!' exp %prec UNARY
695                         { $$ = exp_unop('!', $2); }
696         |       '+' exp %prec UNARY
697                         { $$ = $2; }
698         |       '~' exp %prec UNARY
699                         { $$ = exp_unop('~', $2);}
700
701         |       exp '*' exp
702                         { $$ = exp_binop('*', $1, $3); }
703         |       exp '/' exp
704                         { $$ = exp_binop('/', $1, $3); }
705         |       exp '%' exp
706                         { $$ = exp_binop('%', $1, $3); }
707         |       exp '+' exp
708                         { $$ = exp_binop('+', $1, $3); }
709         |       exp '-' exp
710                         { $$ = exp_binop('-' , $1, $3); }
711         |       exp LSHIFT exp
712                         { $$ = exp_binop(LSHIFT , $1, $3); }
713         |       exp RSHIFT exp
714                         { $$ = exp_binop(RSHIFT , $1, $3); }
715         |       exp EQ exp
716                         { $$ = exp_binop(EQ , $1, $3); }
717         |       exp NE exp
718                         { $$ = exp_binop(NE , $1, $3); }
719         |       exp LE exp
720                         { $$ = exp_binop(LE , $1, $3); }
721         |       exp GE exp
722                         { $$ = exp_binop(GE , $1, $3); }
723         |       exp '<' exp
724                         { $$ = exp_binop('<' , $1, $3); }
725         |       exp '>' exp
726                         { $$ = exp_binop('>' , $1, $3); }
727         |       exp '&' exp
728                         { $$ = exp_binop('&' , $1, $3); }
729         |       exp '^' exp
730                         { $$ = exp_binop('^' , $1, $3); }
731         |       exp '|' exp
732                         { $$ = exp_binop('|' , $1, $3); }
733         |       exp '?' exp ':' exp
734                         { $$ = exp_trinop('?' , $1, $3, $5); }
735         |       exp ANDAND exp
736                         { $$ = exp_binop(ANDAND , $1, $3); }
737         |       exp OROR exp
738                         { $$ = exp_binop(OROR , $1, $3); }
739         |       DEFINED '(' NAME ')'
740                         { $$ = exp_nameop(DEFINED, $3); }
741         |       INT
742                         { $$ = exp_intop($1); }
743         |       SIZEOF_HEADERS
744                         { $$ = exp_nameop(SIZEOF_HEADERS,0); }
745
746         |       SIZEOF '(' NAME ')'
747                         { $$ = exp_nameop(SIZEOF,$3); }
748         |       ADDR '(' NAME ')'
749                         { $$ = exp_nameop(ADDR,$3); }
750         |       ABSOLUTE '(' exp ')'
751                         { $$ = exp_unop(ABSOLUTE, $3); }
752         |       ALIGN_K '(' exp ')'
753                         { $$ = exp_unop(ALIGN_K,$3); }
754         |       NAME
755                         { $$ = exp_nameop(NAME,$1); }
756         ;
757
758
759
760
761 section:        NAME            { ldlex_expression(); }
762                 opt_exp         { ldlex_popstate(); }
763                 opt_type opt_block ':' opt_things'{'
764                 {
765                 lang_enter_output_section_statement($1,$3,$5,$6);
766                 }
767                statement        '}' {ldlex_expression();} fill_opt memspec_opt
768                 {
769                   ldlex_popstate();
770                   lang_leave_output_section_statement($14, $15);
771                 }
772 opt_comma
773
774         ;
775
776 opt_type:
777            NOLOAD  { $$ = SEC_NEVER_LOAD; }
778         |  DSECT   { $$ = 0; }
779         |  COPY    { $$ = 0; }
780         |  INFO    { $$ = 0; }
781         |  OVERLAY { $$ = 0; }
782         | { $$ = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS; }
783         ;
784
785 opt_things:
786       {
787       };
788
789
790 opt_exp:
791                 exp
792                         { $$ = $1; }
793         |               { $$= (etree_type *)NULL; }
794         ;
795
796 opt_block:
797                 BLOCK '(' exp ')'
798                 { $$ = exp_get_value_int($3,
799                                          1L,
800                                          "block",
801                                          lang_first_phase_enum); 
802                 }
803         |       { $$  = 1; }
804         ;
805   
806 memspec_opt:
807                 '>' NAME
808                 { $$ = $2; }
809         |       { $$ = "*default*"; }
810         ;
811 %%
812 void
813 yyerror(arg) 
814 char *arg;
815
816   if (error_index> 0  && error_index < ERROR_NAME_MAX)
817      einfo("%P%F: %S syntax error in %s\n",error_names[error_index-1]);
818   else
819      einfo("%P%F: %S syntax error\n");
820 }