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