039a07db896ebd3716d1839d33ec995145f59c6e
[deliverable/binutils-gdb.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
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
82
83 %}
84 %union {
85 bfd_vma integer;
86 int voidval;
87 char *name;
88 int token;
89 union etree_union *etree;
90 asection *section;
91 struct lang_output_section_statement_struct *output_section_statement;
92 union lang_statement_union **statement_ptr;
93 int lineno;
94 struct {
95 FILE *file;
96 char *name;
97 unsigned int lineno;
98 } state;
99
100
101 }
102
103 %type <etree> exp opt_exp
104 %type <integer> fill_opt opt_block opt_type
105 %type <name> memspec_opt
106 %token <integer> INT
107 %token <name> NAME
108 %type <integer> length
109
110 %right <token> PLUSEQ MINUSEQ MULTEQ DIVEQ '=' LSHIFTEQ RSHIFTEQ ANDEQ OREQ
111 %right <token> '?' ':'
112 %left <token> OROR
113 %left <token> ANDAND
114 %left <token> '|'
115 %left <token> '^'
116 %left <token> '&'
117 %left <token> EQ NE
118 %left <token> '<' '>' LE GE
119 %left <token> LSHIFT RSHIFT
120
121 %left <token> '+' '-'
122 %left <token> '*' '/' '%'
123
124 /*%token <token> '+' '-' '*' '/' '%'*/
125 %right UNARY
126 %left <token> '('
127 %token <token> ALIGN_K BLOCK LONG SHORT BYTE
128 %token SECTIONS
129 %token '{' '}'
130 %token SIZEOF_HEADERS OUTPUT_FORMAT FORCE_COMMON_ALLOCATION OUTPUT_ARCH
131 %token SIZEOF_HEADERS
132 %token MEMORY
133 %token NOLOAD DSECT COPY INFO OVERLAY
134 %token NAME DEFINED TARGET_K SEARCH_DIR MAP ENTRY
135 %token OPTION_e OPTION_c OPTION_noinhibit_exec OPTION_s OPTION_S OPTION_sort_common
136 %token OPTION_format OPTION_F OPTION_u OPTION_Bstatic OPTION_N
137 %token <integer> SIZEOF NEXT ADDR
138 %token OPTION_d OPTION_dc OPTION_dp OPTION_x OPTION_X OPTION_defsym
139 %token OPTION_v OPTION_V OPTION_M OPTION_t STARTUP HLL SYSLIB FLOAT NOFLOAT
140 %token OPTION_Map
141 %token OPTION_n OPTION_r OPTION_o OPTION_b OPTION_R OPTION_relax
142 %token <name> OPTION_l OPTION_L OPTION_T OPTION_Aarch OPTION_Tfile OPTION_Texp
143 %token OPTION_Ur
144 %token ORIGIN FILL OPTION_g
145 %token LENGTH CREATE_OBJECT_SYMBOLS INPUT OUTPUT CONSTRUCTORS
146 %type <token> assign_op
147
148 %type <name> filename
149
150 %{
151 ld_config_type config;
152 %}
153
154 %%
155
156
157
158 file: command_line { lang_final(); };
159
160
161 filename:
162 NAME;
163
164 command_line:
165 command_line command_line_option
166 |
167 ;
168
169 command_line_option:
170 '{'
171 { ldgram_in_script = true; }
172 ifile_list
173 { ldgram_in_script = false; }
174 '}'
175 | OPTION_Bstatic { }
176 | OPTION_v
177 {
178 ldversion();
179 option_v = true;
180 }
181 | OPTION_V
182 {
183 ldversion();
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
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 {
274 hex_mode =true;
275 }
276 INT
277 {
278 lang_section_start($1,exp_intop($3));
279 hex_mode = false;
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); } script_file
303 | OPTION_Tfile
304 { ldfile_open_command_file($1); } script_file
305
306 | OPTION_T filename
307 { ldfile_open_command_file($2); } script_file
308
309 | OPTION_l
310 {
311 lang_add_input_file($1,
312 lang_input_file_is_l_enum,
313 (char *)NULL);
314 }
315 | OPTION_R filename
316 {
317 lang_add_input_file($2,
318 lang_input_file_is_symbols_only_enum,
319 (char *)NULL);
320 }
321 | OPTION_defsym
322 {
323 }
324 NAME '='
325 exp
326 {
327 lang_add_assignment(exp_assop($4,$3,$5));
328 }
329 | '-' NAME
330 { info("%P%F Unrecognised option -%s\n", $2); }
331
332 ;
333
334
335
336
337
338
339
340
341 script_file:
342 { ldgram_in_script = true; }
343 ifile_list '}'
344 { ldgram_in_script = false; }
345
346 ;
347
348
349 ifile_list:
350 ifile_list ifile_p1
351 |
352 ;
353
354
355
356 ifile_p1:
357 memory
358 | sections
359 | startup
360 | high_level_library
361 | low_level_library
362 | floating_point_support
363 | statement_anywhere
364 | ';'
365 | TARGET_K '(' NAME ')'
366 { lang_add_target($3); }
367 | SEARCH_DIR '(' filename ')'
368 { ldfile_add_library_path($3); }
369 | OUTPUT '(' filename ')'
370 { lang_add_output($3); }
371 | OUTPUT_FORMAT '(' NAME ')'
372 { lang_add_output_format($3); }
373 | OUTPUT_ARCH '(' NAME ')'
374 { ldfile_set_output_arch($3); }
375 | FORCE_COMMON_ALLOCATION
376 { command_line.force_common_definition = true ; }
377 | INPUT '(' input_list ')'
378 | MAP '(' filename ')'
379 { lang_add_map($3); }
380 ;
381
382 input_list:
383 NAME
384 { lang_add_input_file($1,lang_input_file_is_file_enum,
385 (char *)NULL); }
386 | input_list ',' NAME
387 { lang_add_input_file($3,lang_input_file_is_file_enum,
388 (char *)NULL); }
389 | input_list NAME
390 { lang_add_input_file($2, lang_input_file_is_file_enum,
391 (char *)NULL); }
392 ;
393
394 sections:
395 SECTIONS '{'sec_or_group_p1 '}'
396 ;
397
398 sec_or_group_p1:
399 sec_or_group_p1 section
400 | sec_or_group_p1 statement_anywhere
401 |
402 ;
403
404 statement_anywhere:
405 ENTRY '(' NAME ')'
406 { lang_add_entry($3); }
407 | assignment end
408 ;
409
410 file_NAME_list:
411 NAME
412 { lang_add_wild($1, current_file); }
413 | file_NAME_list opt_comma NAME
414 { lang_add_wild($3, current_file); }
415 ;
416
417 input_section_spec:
418 NAME
419 {
420 lang_add_wild((char *)NULL, $1);
421 }
422 | '['
423 {
424 current_file = (char *)NULL;
425 }
426 file_NAME_list
427 ']'
428 | NAME
429 {
430 current_file =$1;
431 }
432 '(' file_NAME_list ')'
433 | '*'
434 {
435 current_file = (char *)NULL;
436 }
437 '(' file_NAME_list ')'
438 ;
439
440 statement:
441 statement assignment end
442 | statement CREATE_OBJECT_SYMBOLS
443 {
444 lang_add_attribute(lang_object_symbols_statement_enum); }
445 | statement ';'
446 | statement CONSTRUCTORS
447 {
448 lang_add_attribute(lang_constructors_statement_enum); }
449
450 | statement input_section_spec
451 | statement length '(' exp ')'
452 {
453 lang_add_data($2,$4);
454 }
455
456 | statement FILL '(' exp ')'
457 {
458 lang_add_fill
459 (exp_get_value_int($4,
460 0,
461 "fill value",
462 lang_first_phase_enum));
463 }
464 |
465 ;
466
467 length:
468 LONG
469 { $$ = $1; }
470 | SHORT
471 { $$ = $1; }
472 | BYTE
473 { $$ = $1; }
474 ;
475
476 fill_opt:
477 '=' exp
478 {
479 $$ = exp_get_value_int($2,
480 0,
481 "fill value",
482 lang_first_phase_enum);
483 }
484 | { $$ = 0; }
485 ;
486
487
488
489 assign_op:
490 PLUSEQ
491 { $$ = '+'; }
492 | MINUSEQ
493 { $$ = '-'; }
494 | MULTEQ
495 { $$ = '*'; }
496 | DIVEQ
497 { $$ = '/'; }
498 | LSHIFTEQ
499 { $$ = LSHIFT; }
500 | RSHIFTEQ
501 { $$ = RSHIFT; }
502 | ANDEQ
503 { $$ = '&'; }
504 | OREQ
505 { $$ = '|'; }
506
507 ;
508
509 end: ';' | ','
510 ;
511
512
513 assignment:
514 NAME '=' exp
515 {
516 lang_add_assignment(exp_assop($2,$1,$3));
517 }
518 | NAME assign_op exp
519 {
520 lang_add_assignment(exp_assop('=',$1,exp_binop($2,exp_nameop(NAME,$1),$3)));
521 }
522
523 ;
524
525
526 opt_comma:
527 ',' | ;
528
529
530 memory:
531 MEMORY '{' memory_spec memory_spec_list '}'
532 ;
533
534 memory_spec_list:
535 memory_spec_list memory_spec
536 | memory_spec_list ',' memory_spec
537 |
538 ;
539
540
541 memory_spec:
542 NAME
543 { region = lang_memory_region_lookup($1); }
544 attributes_opt ':' origin_spec opt_comma length_spec
545
546 {
547
548
549 }
550 ;
551 origin_spec:
552 ORIGIN '=' exp
553 { region->current =
554 region->origin =
555 exp_get_vma($3, 0L,"origin", lang_first_phase_enum); }
556 ;
557 length_spec:
558 LENGTH '=' exp
559 { region->length = exp_get_vma($3,
560 ~((bfd_vma)0),
561 "length",
562 lang_first_phase_enum);
563 }
564
565
566 attributes_opt:
567 '(' NAME ')'
568 {
569 lang_set_flags(&region->flags, $2);
570 }
571 |
572
573 ;
574
575 startup:
576 STARTUP '(' filename ')'
577 { lang_startup($3); }
578 ;
579
580 high_level_library:
581 HLL '(' high_level_library_NAME_list ')'
582 | HLL '(' ')'
583 { ldemul_hll((char *)NULL); }
584 ;
585
586 high_level_library_NAME_list:
587 high_level_library_NAME_list opt_comma filename
588 { ldemul_hll($3); }
589 | filename
590 { ldemul_hll($1); }
591
592 ;
593
594 low_level_library:
595 SYSLIB '(' low_level_library_NAME_list ')'
596 ;
597 low_level_library_NAME_list:
598 low_level_library_NAME_list opt_comma filename
599 { ldemul_syslib($3); }
600 |
601 ;
602
603 floating_point_support:
604 FLOAT
605 { lang_float(true); }
606 | NOFLOAT
607 { lang_float(false); }
608 ;
609
610
611
612
613 exp :
614 '-' exp %prec UNARY
615 { $$ = exp_unop('-', $2); }
616 | '(' exp ')'
617 { $$ = $2; }
618 | NEXT '(' exp ')' %prec UNARY
619 { $$ = exp_unop($1,$3); }
620 | '!' exp %prec UNARY
621 { $$ = exp_unop('!', $2); }
622 | '+' exp %prec UNARY
623 { $$ = $2; }
624 | '~' exp %prec UNARY
625 { $$ = exp_unop('~', $2);}
626
627 | exp '*' exp
628 { $$ = exp_binop('*', $1, $3); }
629 | exp '/' exp
630 { $$ = exp_binop('/', $1, $3); }
631 | exp '%' exp
632 { $$ = exp_binop('%', $1, $3); }
633 | exp '+' exp
634 { $$ = exp_binop('+', $1, $3); }
635 | exp '-' exp
636 { $$ = exp_binop('-' , $1, $3); }
637 | exp LSHIFT exp
638 { $$ = exp_binop(LSHIFT , $1, $3); }
639 | exp RSHIFT exp
640 { $$ = exp_binop(RSHIFT , $1, $3); }
641 | exp EQ exp
642 { $$ = exp_binop(EQ , $1, $3); }
643 | exp NE exp
644 { $$ = exp_binop(NE , $1, $3); }
645 | exp LE exp
646 { $$ = exp_binop(LE , $1, $3); }
647 | exp GE exp
648 { $$ = exp_binop(GE , $1, $3); }
649 | exp '<' exp
650 { $$ = exp_binop('<' , $1, $3); }
651 | exp '>' exp
652 { $$ = exp_binop('>' , $1, $3); }
653 | exp '&' exp
654 { $$ = exp_binop('&' , $1, $3); }
655 | exp '^' exp
656 { $$ = exp_binop('^' , $1, $3); }
657 | exp '|' exp
658 { $$ = exp_binop('|' , $1, $3); }
659 | exp '?' exp ':' exp
660 { $$ = exp_trinop('?' , $1, $3, $5); }
661 | exp ANDAND exp
662 { $$ = exp_binop(ANDAND , $1, $3); }
663 | exp OROR exp
664 { $$ = exp_binop(OROR , $1, $3); }
665 | DEFINED '(' NAME ')'
666 { $$ = exp_nameop(DEFINED, $3); }
667 | INT
668 { $$ = exp_intop($1); }
669 | SIZEOF_HEADERS
670 { $$ = exp_nameop(SIZEOF_HEADERS,0); }
671
672 | SIZEOF '(' NAME ')'
673 { $$ = exp_nameop(SIZEOF,$3); }
674 | ADDR '(' NAME ')'
675 { $$ = exp_nameop(ADDR,$3); }
676 | ALIGN_K '(' exp ')'
677 { $$ = exp_unop(ALIGN_K,$3); }
678 | NAME
679 { $$ = exp_nameop(NAME,$1); }
680 ;
681
682
683
684
685 section: NAME opt_exp opt_type opt_block ':' opt_things'{'
686 {
687 lang_enter_output_section_statement($1,$2,$3,$4);
688 }
689 statement '}' fill_opt memspec_opt
690 {
691 lang_leave_output_section_statement($11, $12);
692 }
693
694 ;
695
696 opt_type:
697 '(' NOLOAD ')' { $$ = SEC_NO_FLAGS; }
698 | '(' DSECT ')' { $$ = 0; }
699 | '(' COPY ')' { $$ = 0; }
700 | '(' INFO ')' { $$ = 0; }
701 | '(' OVERLAY ')' { $$ = 0; }
702 | { $$ = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS; }
703 ;
704
705 opt_things:
706 {
707
708 }
709 ;
710
711
712
713
714
715 opt_exp:
716 exp
717 { $$ = $1; }
718 | { $$= (etree_type *)NULL; }
719 ;
720
721 opt_block:
722 BLOCK '(' exp ')'
723 { $$ = exp_get_value_int($3,
724 1L,
725 "block",
726 lang_first_phase_enum);
727 }
728 | { $$ = 1; }
729 ;
730
731 memspec_opt:
732 '>' NAME
733 { $$ = $2; }
734 | { $$ = "*default*"; }
735 ;
736
This page took 0.045474 seconds and 4 git commands to generate.