* ldgram.y (YYDEBUG): If not defined, define as 1.
[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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 #ifndef YYDEBUG
43 #define YYDEBUG 1
44 #endif
45
46 static int typebits;
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 LNAME
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 LNAME 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 GROUP OUTPUT CONSTRUCTORS
110 %token ALIGNMOD AT PROVIDE
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 CASE EXTERN START
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                         config.map_filename = "-";
161                         }
162         |       ORDER ordernamelist
163         |       ENDWORD 
164         |       PUBLIC NAME '=' exp
165                         { mri_public($2, $4); }
166         |       PUBLIC NAME ',' exp
167                         { mri_public($2, $4); }
168         |       PUBLIC NAME  exp 
169                         { mri_public($2, $3); }
170         |       FORMAT NAME
171                         { mri_format($2); }
172         |       SECT NAME ',' exp
173                         { mri_output_section($2, $4);}
174         |       SECT NAME  exp
175                         { mri_output_section($2, $3);}
176         |       SECT NAME '=' exp
177                         { mri_output_section($2, $4);}
178         |       ALIGN_K NAME '=' exp
179                         { mri_align($2,$4); }
180         |       ALIGNMOD NAME '=' exp
181                         { mri_alignmod($2,$4); }
182         |       ABSOLUTE mri_abs_name_list
183         |       LOAD     mri_load_name_list
184         |       NAMEWORD NAME 
185                         { mri_name($2); }   
186         |       ALIAS NAME ',' NAME
187                         { mri_alias($2,$4,0);}
188         |       ALIAS NAME ',' INT
189                         { mri_alias($2,0,(int) $4);}
190         |       BASE     exp
191                         { mri_base($2); }
192         |       TRUNCATE INT
193                 {  mri_truncate((unsigned int) $2); }
194         |       CASE casesymlist
195         |       EXTERN extern_name_list
196         |       INCLUDE filename
197                 { ldfile_open_command_file ($2); } mri_script_lines END
198         |       START NAME
199                 { lang_add_entry ($2, 0); }
200         |
201         ;
202
203 ordernamelist:
204               ordernamelist ',' NAME         { mri_order($3); }
205         |     ordernamelist  NAME         { mri_order($2); }
206         |
207         ;
208
209 mri_load_name_list:
210                 NAME
211                         { mri_load($1); }
212         |       mri_load_name_list ',' NAME { mri_load($3); }
213         ;
214
215 mri_abs_name_list:
216                 NAME
217                         { mri_only_load($1); }
218         |       mri_abs_name_list ','  NAME
219                         { mri_only_load($3); }
220         ;
221
222 casesymlist:
223           /* empty */
224         | NAME
225         | casesymlist ',' NAME
226         ;
227
228 extern_name_list:
229           NAME
230                         { ldlang_add_undef ($1); }
231         | extern_name_list ',' NAME
232                         { ldlang_add_undef ($3); }
233         ;
234
235 script_file:
236         {
237          ldlex_both();
238         }
239        ifile_list
240         {
241         ldlex_popstate();
242         }
243         ;
244
245
246 ifile_list:
247        ifile_list ifile_p1
248         |
249         ;
250
251
252
253 ifile_p1:
254                 memory
255         |       sections
256         |       startup
257         |       high_level_library
258         |       low_level_library
259         |       floating_point_support
260         |       statement_anywhere
261         |        ';'
262         |       TARGET_K '(' NAME ')'
263                 { lang_add_target($3); }
264         |       SEARCH_DIR '(' filename ')'
265                 { ldfile_add_library_path ($3, false); }
266         |       OUTPUT '(' filename ')'
267                 { lang_add_output($3, 1); }
268         |       OUTPUT_FORMAT '(' NAME ')'
269                   { lang_add_output_format ($3, (char *) NULL,
270                                             (char *) NULL, 1); }
271         |       OUTPUT_FORMAT '(' NAME ',' NAME ',' NAME ')'
272                   { lang_add_output_format ($3, $5, $7, 1); }
273         |       OUTPUT_ARCH '(' NAME ')'
274                   { ldfile_set_output_arch($3); }
275         |       FORCE_COMMON_ALLOCATION
276                 { command_line.force_common_definition = true ; }
277         |       INPUT '(' input_list ')'
278         |       GROUP
279                   { lang_enter_group (); }
280                     '(' input_list ')'
281                   { lang_leave_group (); }
282         |       MAP '(' filename ')'
283                 { lang_add_map($3); }
284         |       INCLUDE filename 
285                 { ldfile_open_command_file($2); } ifile_list END
286         ;
287
288 input_list:
289                 NAME
290                 { lang_add_input_file($1,lang_input_file_is_search_file_enum,
291                                  (char *)NULL); }
292         |       input_list ',' NAME
293                 { lang_add_input_file($3,lang_input_file_is_search_file_enum,
294                                  (char *)NULL); }
295         |       input_list NAME
296                 { lang_add_input_file($2,lang_input_file_is_search_file_enum,
297                                  (char *)NULL); }
298         |       LNAME
299                 { lang_add_input_file($1,lang_input_file_is_l_enum,
300                                  (char *)NULL); }
301         |       input_list ',' LNAME
302                 { lang_add_input_file($3,lang_input_file_is_l_enum,
303                                  (char *)NULL); }
304         |       input_list LNAME
305                 { lang_add_input_file($2,lang_input_file_is_l_enum,
306                                  (char *)NULL); }
307         ;
308
309 sections:
310                 SECTIONS '{' sec_or_group_p1 '}'
311         ;
312
313 sec_or_group_p1:
314                 sec_or_group_p1 section
315         |       sec_or_group_p1 statement_anywhere
316         |
317         ;
318
319 statement_anywhere:
320                 ENTRY '(' NAME ')'
321                 { lang_add_entry ($3, 0); }
322         |       assignment end
323         ;
324
325 file_NAME_list:
326                 NAME
327                         { lang_add_wild($1, current_file); }
328         |       file_NAME_list opt_comma NAME
329                         { lang_add_wild($3, current_file); }
330         ;
331
332 input_section_spec:
333                 NAME
334                 {
335                 lang_add_wild((char *)NULL, $1);
336                 }
337         |       '['
338                         {
339                         current_file = (char *)NULL;
340                         }
341                         file_NAME_list
342                 ']'
343         |       NAME
344                         {
345                         current_file =$1;
346                         }
347                 '(' file_NAME_list ')'
348         |       '*'
349                         {
350                         current_file = (char *)NULL;
351                         }
352                 '(' file_NAME_list ')'
353         ;
354
355 statement:
356                 assignment end
357         |       CREATE_OBJECT_SYMBOLS
358                 {
359                 lang_add_attribute(lang_object_symbols_statement_enum); 
360                 }
361         |       ';'
362         |       CONSTRUCTORS
363                 {
364                 
365                   lang_add_attribute(lang_constructors_statement_enum); 
366                 }
367         | input_section_spec
368         | length '(' exp ')'
369                         {
370                         lang_add_data((int) $1,$3);
371                         }
372   
373         | FILL '(' exp ')'
374                         {
375                           lang_add_fill
376                             (exp_get_value_int($3,
377                                                0,
378                                                "fill value",
379                                                lang_first_phase_enum));
380                         }
381         ;
382
383 statement_list:
384                 statement_list statement
385         |       statement
386         ;
387   
388 statement_list_opt:
389                 /* empty */
390         |       statement_list
391         ;
392
393 length:
394                 QUAD
395                         { $$ = $1; }
396         |       LONG
397                         { $$ = $1; }
398         |       SHORT
399                         { $$ = $1; }
400         |       BYTE
401                         { $$ = $1; }
402         ;
403
404 fill_opt:
405           '=' mustbe_exp
406                 {
407                   $$ =   exp_get_value_int($2,
408                                            0,
409                                            "fill value",
410                                            lang_first_phase_enum);
411                 }
412         |       { $$ = 0; }
413         ;
414
415                 
416
417 assign_op:
418                 PLUSEQ
419                         { $$ = '+'; }
420         |       MINUSEQ
421                         { $$ = '-'; }
422         |       MULTEQ
423                         { $$ = '*'; }
424         |       DIVEQ
425                         { $$ = '/'; }
426         |       LSHIFTEQ
427                         { $$ = LSHIFT; }
428         |       RSHIFTEQ
429                         { $$ = RSHIFT; }
430         |       ANDEQ
431                         { $$ = '&'; }
432         |       OREQ
433                         { $$ = '|'; }
434
435         ;
436
437 end:    ';' | ','
438         ;
439
440
441 assignment:
442                 NAME '=' mustbe_exp
443                 {
444                   lang_add_assignment (exp_assop ($2, $1, $3));
445                 }
446         |       NAME assign_op mustbe_exp
447                 {
448                   lang_add_assignment (exp_assop ('=', $1,
449                                                   exp_binop ($2,
450                                                              exp_nameop (NAME,
451                                                                          $1),
452                                                              $3)));
453                 }
454         |       PROVIDE '(' NAME '=' mustbe_exp ')'
455                 {
456                   lang_add_assignment (exp_provide ($3, $5));
457                 }
458         ;
459
460
461 opt_comma:
462                 ','     |       ;
463
464
465 memory:
466                 MEMORY '{' memory_spec memory_spec_list '}'
467         ;
468
469 memory_spec_list:
470                 memory_spec_list memory_spec
471         |       memory_spec_list ',' memory_spec
472         |
473         ;
474
475
476 memory_spec:            NAME
477                         { region = lang_memory_region_lookup($1); }
478                 attributes_opt ':'
479                 origin_spec opt_comma length_spec
480
481         ; origin_spec:
482         ORIGIN '=' mustbe_exp
483                 { region->current =
484                  region->origin =
485                  exp_get_vma($3, 0L,"origin", lang_first_phase_enum);
486 }
487         ; length_spec:
488              LENGTH '=' mustbe_exp
489                { region->length = exp_get_vma($3,
490                                                ~((bfd_vma)0),
491                                                "length",
492                                                lang_first_phase_enum);
493                 }
494         
495
496 attributes_opt:
497                   '(' NAME ')'
498                         {
499                         lang_set_flags(&region->flags, $2);
500                         }
501         |
502   
503         ;
504
505 startup:
506         STARTUP '(' filename ')'
507                 { lang_startup($3); }
508         ;
509
510 high_level_library:
511                 HLL '(' high_level_library_NAME_list ')'
512         |       HLL '(' ')'
513                         { ldemul_hll((char *)NULL); }
514         ;
515
516 high_level_library_NAME_list:
517                 high_level_library_NAME_list opt_comma filename
518                         { ldemul_hll($3); }
519         |       filename
520                         { ldemul_hll($1); }
521
522         ;
523
524 low_level_library:
525         SYSLIB '(' low_level_library_NAME_list ')'
526         ; low_level_library_NAME_list:
527                 low_level_library_NAME_list opt_comma filename
528                         { ldemul_syslib($3); }
529         |
530         ;
531
532 floating_point_support:
533                 FLOAT
534                         { lang_float(true); }
535         |       NOFLOAT
536                         { lang_float(false); }
537         ;
538                 
539
540 mustbe_exp:              { ldlex_expression(); }
541                 exp
542                          { ldlex_popstate(); $$=$2;}
543         ;
544
545 exp     :
546                 '-' exp %prec UNARY
547                         { $$ = exp_unop('-', $2); }
548         |       '(' exp ')'
549                         { $$ = $2; }
550         |       NEXT '(' exp ')' %prec UNARY
551                         { $$ = exp_unop((int) $1,$3); }
552         |       '!' exp %prec UNARY
553                         { $$ = exp_unop('!', $2); }
554         |       '+' exp %prec UNARY
555                         { $$ = $2; }
556         |       '~' exp %prec UNARY
557                         { $$ = exp_unop('~', $2);}
558
559         |       exp '*' exp
560                         { $$ = exp_binop('*', $1, $3); }
561         |       exp '/' exp
562                         { $$ = exp_binop('/', $1, $3); }
563         |       exp '%' exp
564                         { $$ = exp_binop('%', $1, $3); }
565         |       exp '+' exp
566                         { $$ = exp_binop('+', $1, $3); }
567         |       exp '-' exp
568                         { $$ = exp_binop('-' , $1, $3); }
569         |       exp LSHIFT exp
570                         { $$ = exp_binop(LSHIFT , $1, $3); }
571         |       exp RSHIFT exp
572                         { $$ = exp_binop(RSHIFT , $1, $3); }
573         |       exp EQ exp
574                         { $$ = exp_binop(EQ , $1, $3); }
575         |       exp NE exp
576                         { $$ = exp_binop(NE , $1, $3); }
577         |       exp LE exp
578                         { $$ = exp_binop(LE , $1, $3); }
579         |       exp GE exp
580                         { $$ = exp_binop(GE , $1, $3); }
581         |       exp '<' exp
582                         { $$ = exp_binop('<' , $1, $3); }
583         |       exp '>' exp
584                         { $$ = exp_binop('>' , $1, $3); }
585         |       exp '&' exp
586                         { $$ = exp_binop('&' , $1, $3); }
587         |       exp '^' exp
588                         { $$ = exp_binop('^' , $1, $3); }
589         |       exp '|' exp
590                         { $$ = exp_binop('|' , $1, $3); }
591         |       exp '?' exp ':' exp
592                         { $$ = exp_trinop('?' , $1, $3, $5); }
593         |       exp ANDAND exp
594                         { $$ = exp_binop(ANDAND , $1, $3); }
595         |       exp OROR exp
596                         { $$ = exp_binop(OROR , $1, $3); }
597         |       DEFINED '(' NAME ')'
598                         { $$ = exp_nameop(DEFINED, $3); }
599         |       INT
600                         { $$ = exp_intop($1); }
601         |       SIZEOF_HEADERS
602                         { $$ = exp_nameop(SIZEOF_HEADERS,0); }
603
604         |       SIZEOF '(' NAME ')'
605                         { $$ = exp_nameop(SIZEOF,$3); }
606         |       ADDR '(' NAME ')'
607                         { $$ = exp_nameop(ADDR,$3); }
608         |       ABSOLUTE '(' exp ')'
609                         { $$ = exp_unop(ABSOLUTE, $3); }
610         |       ALIGN_K '(' exp ')'
611                         { $$ = exp_unop(ALIGN_K,$3); }
612         |       BLOCK '(' exp ')'
613                         { $$ = exp_unop(ALIGN_K,$3); }
614         |       NAME
615                         { $$ = exp_nameop(NAME,$1); }
616         ;
617
618
619 opt_at:
620                 AT '(' exp ')' { $$ = $3; }
621         |       { $$ = 0; }
622         ;
623
624 section:        NAME            { ldlex_expression(); }
625                 opt_exp_with_type 
626                 opt_at          { ldlex_popstate(); }
627                 '{'
628                         {
629                         lang_enter_output_section_statement($1,$3,typebits,0,0,0,$4);
630                         }
631                 statement_list_opt      
632                 '}' {ldlex_expression();} memspec_opt fill_opt
633                 {
634                   ldlex_popstate();
635                   lang_leave_output_section_statement($12, $11);
636                 }
637 opt_comma
638
639         ;
640
641 type:
642            NOLOAD  { typebits = SEC_NEVER_LOAD; }
643         |  DSECT   { typebits = 0; }
644         |  COPY    { typebits = 0; }
645         |  INFO    { typebits = 0; }
646         |  OVERLAY { typebits = 0; }
647         | { typebits = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS; }
648         ;
649
650
651 opt_exp_with_type:
652                 exp ':'                 { $$ = $1; typebits =0;}
653         |       exp '(' type ')' ':'    { $$ = $1; }
654         |       ':'                     { $$= (etree_type *)NULL; typebits = 0; }
655         |       '(' type ')' ':'        { $$= (etree_type *)NULL;  }
656         ;
657
658 memspec_opt:
659                 '>' NAME
660                 { $$ = $2; }
661         |       { $$ = "*default*"; }
662         ;
663 %%
664 void
665 yyerror(arg) 
666      const char *arg;
667
668   if (ldfile_assumed_script)
669     einfo ("%P:%s: file format not recognized; treating as linker script\n",
670            ldfile_input_filename);
671   if (error_index > 0 && error_index < ERROR_NAME_MAX)
672      einfo ("%P%F:%S: %s in %s\n", arg, error_names[error_index-1]);
673   else
674      einfo ("%P%F:%S: %s\n", arg);
675 }