Strange link script support
[external/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  
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 FORMAT
156
157 %{
158 ld_config_type config;
159 %}
160
161 %%
162
163 file:   command_line  { lang_final(); };
164
165
166 filename:  NAME;
167
168
169 command_line:
170                 command_line command_line_option
171         |
172         ;
173
174 command_line_option:
175                 OPTION_Bstatic { }
176         |       OPTION_v
177                         {       
178                         ldversion(0);
179                         option_v = true;
180                         }
181         |       OPTION_V
182                         {       
183                         ldversion(1);
184                         option_v = true;
185                         }
186         |       OPTION_t {
187                         trace_files = true;
188                         }
189         |     OPTION_Map  NAME
190                 {
191                 write_map = true;
192                 config.map_filename = $2;
193                 }
194         |       OPTION_M {
195             config.map_filename = "-";
196                             
197                         }
198         |       OPTION_n {
199                         config.magic_demand_paged = false;
200                         }
201         |       OPTION_N {
202                         config.text_read_only = false;
203                         config.magic_demand_paged = false;
204                         }
205         |       OPTION_s {
206                         strip_symbols = STRIP_ALL;
207                         }
208         |       OPTION_S {
209                         strip_symbols = STRIP_DEBUGGER;
210                         }
211         |       OPTION_u NAME {
212                         ldlang_add_undef($2);
213                 }
214             
215         |       OPTION_r {
216                         config.relocateable_output = true;
217                         config.build_constructors = false;
218                         config.magic_demand_paged = false;
219                         config.text_read_only = false;
220                         }
221         |       OPTION_Ur {
222                         config.relocateable_output = true;
223                         config.build_constructors = true;
224                         config.magic_demand_paged = false;
225                         config.text_read_only = false;
226                       }             
227         |       OPTION_o filename
228                         {
229                         lang_add_output($2); 
230                         }
231         |       OPTION_e NAME
232                         { lang_add_entry($2); 
233                         }
234         |       OPTION_X {
235                         discard_locals = DISCARD_L;
236                 }
237         |       OPTION_x {
238                         discard_locals = DISCARD_ALL;
239                 }
240
241         |       OPTION_noinhibit_exec
242                         {
243                         force_make_executable = true;
244                         }
245         |      OPTION_sort_common {
246         config.sort_common = true;
247       }
248         |      OPTION_d {
249                           command_line.force_common_definition = true;
250                         }
251
252         |      OPTION_relax {
253                           command_line.relax = true;
254                         }
255         |      OPTION_dc
256                          {
257                           command_line.force_common_definition = true;
258                         }
259         |       OPTION_g
260                         {
261                         /* Ignored */
262                         }
263         |       OPTION_dp
264                          {
265                           command_line.force_common_definition = true;
266                         }
267         |       OPTION_format NAME
268                    {
269                           lang_add_target($2);
270                    }
271         |       OPTION_Texp 
272                 { 
273                         hex_mode  = 16; 
274                 } 
275                 INT
276                 { 
277                         lang_section_start($1,exp_intop($3));
278                         hex_mode = 0; 
279                 }
280         
281         |       OPTION_Aarch 
282                 { 
283                         ldfile_add_arch($1); 
284                 }
285         |        OPTION_b NAME
286                         {
287                         lang_add_target($2);
288                         }
289         |       OPTION_L
290                         {
291                         ldfile_add_library_path($1);
292                         }
293         |       OPTION_F
294                 {
295                 /* Ignore */
296                 }
297         |       NAME
298                 { lang_add_input_file($1,lang_input_file_is_file_enum,
299                                  (char *)NULL); }
300         |       OPTION_c filename 
301                         { ldfile_open_command_file($2); } mri_script_file  END {  ldlex_command()};
302
303         |       OPTION_Tfile 
304                         { ldfile_open_command_file($1); } script_file
305                         END {  ldlex_command();}
306
307         |       OPTION_T filename 
308                         { ldfile_open_command_file($2); } script_file
309 END {  ldlex_command();}
310
311         |       OPTION_l
312                         {
313                           lang_add_input_file($1,
314                                          lang_input_file_is_l_enum,
315                                          (char *)NULL);
316                         }
317         |       OPTION_R filename
318                         {
319                         lang_add_input_file($2,
320                                 lang_input_file_is_symbols_only_enum,
321                                 (char *)NULL);
322                         }
323         |       OPTION_defsym  { ldlex_expression();
324   
325                         }
326                 NAME     '=' mustbe_exp  { ldlex_popstate();
327                         lang_add_assignment(exp_assop($4,$3,$5));
328                         }       
329         | '-' NAME
330                  { info("%P%F Unrecognised 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         |       FORMAT NAME
365                         { mri_format($2); }
366         |       SECT NAME ',' exp 
367                         { mri_output_section($2, $4);}
368         |       SECT NAME  exp
369                         { mri_output_section($2, $3);}
370         |       SECT NAME '=' exp
371                         { mri_output_section($2, $4);}
372         |       ABSOLUTE mri_abs_name_list
373         |       LOAD     mri_load_name_list
374         |       NAMEWORD NAME 
375                         { mri_name($2); }   
376         |
377         ;
378
379 ordernamelist:
380               ordernamelist ',' NAME         { mri_order($3); }
381         |     ordernamelist  NAME         { mri_order($2); }
382         |
383         ;
384
385 mri_load_name_list:
386                 NAME
387                         { mri_load($1); }
388         |       mri_load_name_list ',' NAME { mri_load($3); }
389         ;
390
391 mri_abs_name_list:
392                 NAME
393                         { mri_only_load($1); }
394         |       mri_abs_name_list ','  NAME
395                         { mri_only_load($3); }
396         ;
397
398 script_file:
399         {
400          ldlex_both();
401         }
402        ifile_list
403         {
404         ldlex_popstate();
405         }
406         ;
407
408
409 ifile_list:
410        ifile_list ifile_p1
411         |
412         ;
413
414
415
416 ifile_p1:
417                 memory
418         |       sections
419         |       startup
420         |       high_level_library
421         |       low_level_library
422         |       floating_point_support
423         |       statement_anywhere
424         |        ';'
425         |       TARGET_K '(' NAME ')'
426                 { lang_add_target($3); }
427         |       SEARCH_DIR '(' filename ')'
428                 { ldfile_add_library_path($3); }
429         |       OUTPUT '(' filename ')'
430                 { lang_add_output($3); }
431         | OUTPUT_FORMAT '(' NAME ')'
432                   { lang_add_output_format($3); }
433         | OUTPUT_ARCH '(' NAME ')'
434                   { ldfile_set_output_arch($3); }
435         |       FORCE_COMMON_ALLOCATION
436                 { command_line.force_common_definition = true ; }
437         |       INPUT '(' input_list ')'
438         |       MAP '(' filename ')'
439                 { lang_add_map($3); }
440         ;
441
442 input_list:
443                 NAME
444                 { lang_add_input_file($1,lang_input_file_is_file_enum,
445                                  (char *)NULL); }
446         |       input_list ',' NAME
447                 { lang_add_input_file($3,lang_input_file_is_file_enum,
448                                  (char *)NULL); }
449         |       input_list NAME
450                 { lang_add_input_file($2,
451 lang_input_file_is_file_enum,
452                                  (char *)NULL); }
453         ;
454
455 sections:
456                 SECTIONS '{' sec_or_group_p1 '}'
457         ;
458
459 sec_or_group_p1:
460                 sec_or_group_p1 section
461         |       sec_or_group_p1 statement_anywhere
462         |
463         ;
464
465 statement_anywhere:
466                 ENTRY '(' NAME ')'
467                 { lang_add_entry($3); }
468         |       assignment end
469         ;
470
471 file_NAME_list:
472                 NAME
473                         { lang_add_wild($1, current_file); }
474         |       file_NAME_list opt_comma NAME
475                         { lang_add_wild($3, current_file); }
476         ;
477
478 input_section_spec:
479                 NAME
480                 {
481                 lang_add_wild((char *)NULL, $1);
482                 }
483         |       '['
484                         {
485                         current_file = (char *)NULL;
486                         }
487                         file_NAME_list
488                 ']'
489         |       NAME
490                         {
491                         current_file =$1;
492                         }
493                 '(' file_NAME_list ')'
494         |       '*'
495                         {
496                         current_file = (char *)NULL;
497                         }
498                 '(' file_NAME_list ')'
499         ;
500
501 statement:
502                 statement assignment end
503         |       statement CREATE_OBJECT_SYMBOLS
504                 {
505                 
506 lang_add_attribute(lang_object_symbols_statement_enum); }
507         |       statement ';'
508         |       statement CONSTRUCTORS
509                 {
510                 
511 lang_add_attribute(lang_constructors_statement_enum); }
512
513         |       statement input_section_spec
514         | statement length '(' exp ')'
515                         {
516                         lang_add_data($2,$4);
517                         }
518   
519         |       statement FILL '(' exp ')'
520                         {
521                           lang_add_fill
522                             (exp_get_value_int($4,
523                                                0,
524                                                "fill value",
525                                         
526 lang_first_phase_enum));
527                         }
528         |
529         ;
530
531 length:
532                 LONG
533                         { $$ = $1; }
534         |       SHORT
535                         { $$ = $1; }
536         |       BYTE
537                         { $$ = $1; }
538         ;
539
540 fill_opt:
541           '=' mustbe_exp
542                 {
543                   $$ =   exp_get_value_int($2,
544                                            0,
545                                            "fill value",
546                                            lang_first_phase_enum);
547                 }
548         |       { $$ = 0; }
549         ;
550
551                 
552
553 assign_op:
554                 PLUSEQ
555                         { $$ = '+'; }
556         |       MINUSEQ
557                         { $$ = '-'; }
558         |       MULTEQ
559                         { $$ = '*'; }
560         |       DIVEQ
561                         { $$ = '/'; }
562         |       LSHIFTEQ
563                         { $$ = LSHIFT; }
564         |       RSHIFTEQ
565                         { $$ = RSHIFT; }
566         |       ANDEQ
567                         { $$ = '&'; }
568         |       OREQ
569                         { $$ = '|'; }
570
571         ;
572
573 end:    ';' | ','
574         ;
575
576
577 assignment:
578                 NAME '=' mustbe_exp
579                 {
580                   lang_add_assignment(exp_assop($2,$1,$3));
581                 }
582         |       NAME assign_op mustbe_exp
583                 {
584                 
585 lang_add_assignment(exp_assop('=',$1,exp_binop($2,exp_nameop(NAME,$1),$3)));
586                 }
587                 
588         ;
589
590
591 opt_comma:
592                 ','     |       ;
593
594
595 memory:
596                 MEMORY '{' memory_spec memory_spec_list '}'
597         ;
598
599 memory_spec_list:
600                 memory_spec_list memory_spec
601         |       memory_spec_list ',' memory_spec
602         |
603         ;
604
605
606 memory_spec:            NAME
607                         { region = lang_memory_region_lookup($1); }
608                 attributes_opt ':'
609                 origin_spec opt_comma length_spec
610
611         ; origin_spec:
612         ORIGIN '=' mustbe_exp
613                 { region->current =
614                  region->origin =
615                  exp_get_vma($3, 0L,"origin", lang_first_phase_enum);
616 }
617         ; length_spec:
618              LENGTH '=' mustbe_exp
619                { region->length = exp_get_vma($3,
620                                                ~((bfd_vma)0),
621                                                "length",
622                                                lang_first_phase_enum);
623                 }
624         
625
626 attributes_opt:
627                   '(' NAME ')'
628                         {
629                         lang_set_flags(&region->flags, $2);
630                         }
631         |
632   
633         ;
634
635 startup:
636         STARTUP '(' filename ')'
637                 { lang_startup($3); }
638         ;
639
640 high_level_library:
641                 HLL '(' high_level_library_NAME_list ')'
642         |       HLL '(' ')'
643                         { ldemul_hll((char *)NULL); }
644         ;
645
646 high_level_library_NAME_list:
647                 high_level_library_NAME_list opt_comma filename
648                         { ldemul_hll($3); }
649         |       filename
650                         { ldemul_hll($1); }
651
652         ;
653
654 low_level_library:
655         SYSLIB '(' low_level_library_NAME_list ')'
656         ; low_level_library_NAME_list:
657                 low_level_library_NAME_list opt_comma filename
658                         { ldemul_syslib($3); }
659         |
660         ;
661
662 floating_point_support:
663                 FLOAT
664                         { lang_float(true); }
665         |       NOFLOAT
666                         { lang_float(false); }
667         ;
668                 
669
670 mustbe_exp:              { ldlex_expression(); }
671                 exp
672                          { ldlex_popstate(); $$=$2;}
673         ;
674
675 exp     :
676                 '-' exp %prec UNARY
677                         { $$ = exp_unop('-', $2); }
678         |       '(' exp ')'
679                         { $$ = $2; }
680         |       NEXT '(' exp ')' %prec UNARY
681                         { $$ = exp_unop($1,$3); }
682         |       '!' exp %prec UNARY
683                         { $$ = exp_unop('!', $2); }
684         |       '+' exp %prec UNARY
685                         { $$ = $2; }
686         |       '~' exp %prec UNARY
687                         { $$ = exp_unop('~', $2);}
688
689         |       exp '*' exp
690                         { $$ = exp_binop('*', $1, $3); }
691         |       exp '/' exp
692                         { $$ = exp_binop('/', $1, $3); }
693         |       exp '%' exp
694                         { $$ = exp_binop('%', $1, $3); }
695         |       exp '+' exp
696                         { $$ = exp_binop('+', $1, $3); }
697         |       exp '-' exp
698                         { $$ = exp_binop('-' , $1, $3); }
699         |       exp LSHIFT exp
700                         { $$ = exp_binop(LSHIFT , $1, $3); }
701         |       exp RSHIFT exp
702                         { $$ = exp_binop(RSHIFT , $1, $3); }
703         |       exp EQ exp
704                         { $$ = exp_binop(EQ , $1, $3); }
705         |       exp NE exp
706                         { $$ = exp_binop(NE , $1, $3); }
707         |       exp LE exp
708                         { $$ = exp_binop(LE , $1, $3); }
709         |       exp GE exp
710                         { $$ = exp_binop(GE , $1, $3); }
711         |       exp '<' exp
712                         { $$ = exp_binop('<' , $1, $3); }
713         |       exp '>' exp
714                         { $$ = exp_binop('>' , $1, $3); }
715         |       exp '&' exp
716                         { $$ = exp_binop('&' , $1, $3); }
717         |       exp '^' exp
718                         { $$ = exp_binop('^' , $1, $3); }
719         |       exp '|' exp
720                         { $$ = exp_binop('|' , $1, $3); }
721         |       exp '?' exp ':' exp
722                         { $$ = exp_trinop('?' , $1, $3, $5); }
723         |       exp ANDAND exp
724                         { $$ = exp_binop(ANDAND , $1, $3); }
725         |       exp OROR exp
726                         { $$ = exp_binop(OROR , $1, $3); }
727         |       DEFINED '(' NAME ')'
728                         { $$ = exp_nameop(DEFINED, $3); }
729         |       INT
730                         { $$ = exp_intop($1); }
731         |       SIZEOF_HEADERS
732                         { $$ = exp_nameop(SIZEOF_HEADERS,0); }
733
734         |       SIZEOF '(' NAME ')'
735                         { $$ = exp_nameop(SIZEOF,$3); }
736         |       ADDR '(' NAME ')'
737                         { $$ = exp_nameop(ADDR,$3); }
738         |       ALIGN_K '(' exp ')'
739                         { $$ = exp_unop(ALIGN_K,$3); }
740         |       NAME
741                         { $$ = exp_nameop(NAME,$1); }
742         ;
743
744
745
746
747 section:        NAME            { ldlex_expression(); }
748                 opt_exp         { ldlex_popstate(); }
749                 opt_type opt_block ':' opt_things'{'
750                 {
751                 lang_enter_output_section_statement($1,$3,$5,$6);
752                 }
753                statement        '}'     fill_opt memspec_opt
754                 {
755                   lang_leave_output_section_statement($13, $14);
756                 }
757
758         ;
759
760 opt_type:
761           '(' NOLOAD ')' { $$ = SEC_NO_FLAGS; }
762         | '(' DSECT ')' { $$ = 0; }
763         | '(' COPY ')' { $$ = 0; }
764         | '(' INFO ')' { $$ = 0; }
765         | '(' OVERLAY ')' { $$ = 0; }
766         | { $$ = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS; }
767         ;
768
769 opt_things:
770       {
771       };
772
773
774 opt_exp:
775                 exp
776                         { $$ = $1; }
777         |               { $$= (etree_type *)NULL; }
778         ;
779
780 opt_block:
781                 BLOCK '(' exp ')'
782                 { $$ = exp_get_value_int($3,
783                                          1L,
784                                          "block",
785                                          lang_first_phase_enum); 
786                 }
787         |       { $$  = 1; }
788         ;
789   
790 memspec_opt:
791                 '>' NAME
792                 { $$ = $2; }
793         |       { $$ = "*default*"; }
794         ;
795 %%
796 void
797 yyerror(arg) 
798 char *arg;
799
800   if (error_index> 0  && error_index < ERROR_NAME_MAX)
801      einfo("%P%F: %S syntax error in %s\n",error_names[error_index-1]);
802   else
803      einfo("%P%F: %S syntax error\n");
804 }