Include bfd.h before sysdep.h, so ansidecl and PROTO() get defined first.
[deliverable/binutils-gdb.git] / ld / ldgram.y
1 %{
2 /*
3 * $Id$
4 *
5 *
6 */
7
8 /*
9 This is a YACC grammer intended to parse a superset of the AT&T
10 linker scripting languaue.
11
12
13 Written by Steve Chamberlain steve@cygnus.com
14 */
15
16
17 #define DONTDECLARE_MALLOC
18
19 #include "bfd.h"
20 #include "sysdep.h"
21 #include "ld.h"
22 #include "ldexp.h"
23 #include "ldver.h"
24 #include "ldlang.h"
25 #include "ldemul.h"
26 #include "ldfile.h"
27 #include "ldmisc.h"
28
29
30 #define YYDEBUG 1
31
32 boolean option_v;
33 extern unsigned int lineno;
34 extern boolean trace_files;
35 extern boolean write_map;
36
37 boolean hex_mode;
38
39 strip_symbols_type strip_symbols=STRIP_NONE;
40 discard_locals_type discard_locals=DISCARD_NONE;
41
42
43 lang_memory_region_type *region;
44
45
46 lang_memory_region_type *lang_memory_region_lookup();
47 lang_output_section_statement_type *lang_output_section_statement_lookup();
48
49 #ifdef __STDC__
50
51 void lang_add_data(int type, union etree_union *exp);
52 void lang_enter_output_section_statement(char *output_section_statement_name, etree_type *address_exp, int flags, bfd_vma block_value);
53
54 #else
55
56 void lang_add_data();
57 void lang_enter_output_section_statement();
58
59 #endif /* __STDC__ */
60
61 extern args_type command_line;
62 char *current_file;
63 boolean ldgram_want_filename = true;
64 boolean had_script = false;
65 boolean force_make_executable = false;
66
67 boolean ldgram_in_script = false;
68 boolean ldgram_had_equals = false;
69 /* LOCALS */
70
71
72
73
74 %}
75 %union {
76 bfd_vma integer;
77 int voidval;
78 char *name;
79 int token;
80 union etree_union *etree;
81 asection *section;
82 struct lang_output_section_statement_struct *output_section_statement;
83 union lang_statement_union **statement_ptr;
84 int lineno;
85 struct {
86 FILE *file;
87 char *name;
88 unsigned int lineno;
89 } state;
90
91
92 }
93
94 %type <etree> exp opt_exp
95 %type <integer> fill_opt opt_block opt_type
96 %type <name> memspec_opt
97 %token <integer> INT
98 %token <name> NAME
99 %type <integer> length
100
101 %right <token> PLUSEQ MINUSEQ MULTEQ DIVEQ '=' LSHIFTEQ RSHIFTEQ ANDEQ OREQ
102 %right <token> '?' ':'
103 %left <token> OROR
104 %left <token> ANDAND
105 %left <token> '|'
106 %left <token> '^'
107 %left <token> '&'
108 %left <token> EQ NE
109 %left <token> '<' '>' LE GE
110 %left <token> LSHIFT RSHIFT
111
112 %left <token> '+' '-'
113 %left <token> '*' '/' '%'
114
115 /*%token <token> '+' '-' '*' '/' '%'*/
116 %right UNARY
117 %left <token> '('
118 %token <token> ALIGN_K BLOCK LONG SHORT BYTE
119 %token SECTIONS
120 %token '{' '}'
121 %token SIZEOF_HEADERS OUTPUT_FORMAT FORCE_COMMON_ALLOCATION OUTPUT_ARCH
122 %token SIZEOF_HEADERS
123 %token MEMORY
124 %token NOLOAD DSECT COPY INFO OVERLAY
125 %token NAME DEFINED TARGET_K SEARCH_DIR MAP ENTRY
126 %token OPTION_e OPTION_c OPTION_noinhibit_exec OPTION_s OPTION_S OPTION_sort_common
127 %token OPTION_format OPTION_F OPTION_u OPTION_Bstatic OPTION_N
128 %token <integer> SIZEOF NEXT ADDR
129 %token OPTION_d OPTION_dc OPTION_dp OPTION_x OPTION_X OPTION_defsym
130 %token OPTION_v OPTION_M OPTION_t STARTUP HLL SYSLIB FLOAT NOFLOAT
131 %token OPTION_n OPTION_r OPTION_o OPTION_b OPTION_R
132 %token <name> OPTION_l OPTION_L OPTION_T OPTION_Aarch OPTION_Tfile OPTION_Texp
133 %token OPTION_Ur
134 %token ORIGIN FILL OPTION_g
135 %token LENGTH CREATE_OBJECT_SYMBOLS INPUT OUTPUT CONSTRUCTORS
136 %type <token> assign_op
137
138 %type <name> filename
139
140 %{
141 ld_config_type config;
142 %}
143
144 %%
145
146
147
148 file: command_line { lang_final(); };
149
150
151 filename:
152 NAME;
153
154 command_line:
155 command_line command_line_option
156 |
157 ;
158
159 command_line_option:
160 '{'
161 { ldgram_in_script = true; }
162 ifile_list
163 { ldgram_in_script = false; }
164 '}'
165 | OPTION_Bstatic { }
166 | OPTION_v
167 {
168 ldversion();
169 option_v = true;
170 }
171 | OPTION_t {
172 trace_files = true;
173 }
174 | OPTION_M {
175 write_map = true;
176 }
177 | OPTION_n {
178 config.magic_demand_paged = false;
179 config.text_read_only = true;
180 }
181 | OPTION_N {
182 config.magic_demand_paged = false;
183 }
184 | OPTION_s {
185 strip_symbols = STRIP_ALL;
186 }
187 | OPTION_S {
188 strip_symbols = STRIP_DEBUGGER;
189 }
190 | OPTION_u NAME {
191 ldlang_add_undef($2);
192 }
193
194 | OPTION_r {
195 config.relocateable_output = true;
196 config.build_constructors = false;
197 config.magic_demand_paged = false;
198 }
199 | OPTION_Ur {
200 config.relocateable_output = true;
201 config.build_constructors = true;
202 config.magic_demand_paged = false;
203 }
204 | OPTION_o filename
205 {
206 lang_add_output($2);
207 }
208 | OPTION_e NAME
209 { lang_add_entry($2);
210 }
211 | OPTION_X {
212 discard_locals = DISCARD_L;
213 }
214 | OPTION_x {
215 discard_locals = DISCARD_ALL;
216 }
217
218 | OPTION_noinhibit_exec
219 {
220 force_make_executable = true;
221 }
222 | OPTION_sort_common {
223 config.sort_common = true;
224 }
225 | OPTION_d {
226 command_line.force_common_definition = true;
227 }
228 | OPTION_dc
229 {
230 command_line.force_common_definition = true;
231 }
232 | OPTION_g
233 {
234 /* Ignored */
235 }
236 | OPTION_dp
237 {
238 command_line.force_common_definition = true;
239 }
240 | OPTION_format NAME
241 {
242 lang_add_target($2);
243 }
244 | OPTION_Texp
245 {
246 hex_mode =true;
247 }
248 INT
249 {
250 lang_section_start($1,exp_intop($3));
251 hex_mode = false;
252 }
253
254 | OPTION_Aarch
255 {
256 ldfile_add_arch($1);
257 }
258 | OPTION_b NAME
259 {
260 lang_add_target($2);
261 }
262 | OPTION_L
263 {
264 ldfile_add_library_path($1);
265 }
266 | OPTION_F
267 {
268 /* Ignore */
269 }
270 | NAME
271 { lang_add_input_file($1,lang_input_file_is_file_enum,
272 (char *)NULL); }
273 | OPTION_c filename
274 { ldfile_open_command_file($2); } script_file
275 | OPTION_Tfile
276 { ldfile_open_command_file($1); } script_file
277
278 | OPTION_T filename
279 { ldfile_open_command_file($2); } script_file
280
281 | OPTION_l
282 {
283 lang_add_input_file($1,
284 lang_input_file_is_l_enum,
285 (char *)NULL);
286 }
287 | OPTION_R filename
288 {
289 lang_add_input_file($2,
290 lang_input_file_is_symbols_only_enum,
291 (char *)NULL);
292 }
293 | OPTION_defsym
294 {
295 }
296 NAME '='
297 exp
298 {
299 lang_add_assignment(exp_assop($4,$3,$5));
300 }
301 | '-' NAME
302 { info("%P%F Unrecognised option -%s\n", $2); }
303
304 ;
305
306
307
308
309
310
311
312
313 script_file:
314 { ldgram_in_script = true; }
315 ifile_list '}'
316 { ldgram_in_script = false; }
317
318 ;
319
320
321 ifile_list:
322 ifile_list ifile_p1
323 |
324 ;
325
326
327
328 ifile_p1:
329 memory
330 | sections
331 | startup
332 | high_level_library
333 | low_level_library
334 | floating_point_support
335 | statement_anywhere
336 | ';'
337 | TARGET_K '(' NAME ')'
338 { lang_add_target($3); }
339 | SEARCH_DIR '(' filename ')'
340 { ldfile_add_library_path($3); }
341 | OUTPUT '(' filename ')'
342 { lang_add_output($3); }
343 | OUTPUT_FORMAT '(' NAME ')'
344 { lang_add_output_format($3); }
345 | OUTPUT_ARCH '(' NAME ')'
346 { ldfile_set_output_arch($3); }
347 | FORCE_COMMON_ALLOCATION
348 { command_line.force_common_definition = true ; }
349 | INPUT '(' input_list ')'
350 | MAP '(' filename ')'
351 { lang_add_map($3); }
352 ;
353
354 input_list:
355 NAME
356 { lang_add_input_file($1,lang_input_file_is_file_enum,
357 (char *)NULL); }
358 | input_list ',' NAME
359 { lang_add_input_file($3,lang_input_file_is_file_enum,
360 (char *)NULL); }
361 | input_list NAME
362 { lang_add_input_file($2, lang_input_file_is_file_enum,
363 (char *)NULL); }
364 ;
365
366 sections:
367 SECTIONS '{'sec_or_group_p1 '}'
368 ;
369
370 sec_or_group_p1:
371 sec_or_group_p1 section
372 | sec_or_group_p1 statement_anywhere
373 |
374 ;
375
376 statement_anywhere:
377 ENTRY '(' NAME ')'
378 { lang_add_entry($3); }
379 | assignment end
380 ;
381
382 file_NAME_list:
383 NAME
384 { lang_add_wild($1, current_file); }
385 | file_NAME_list opt_comma NAME
386 { lang_add_wild($3, current_file); }
387 ;
388
389 input_section_spec:
390 NAME
391 {
392 lang_add_wild((char *)NULL, $1);
393 }
394 | '['
395 {
396 current_file = (char *)NULL;
397 }
398 file_NAME_list
399 ']'
400 | NAME
401 {
402 current_file =$1;
403 }
404 '(' file_NAME_list ')'
405 | '*'
406 {
407 current_file = (char *)NULL;
408 }
409 '(' file_NAME_list ')'
410 ;
411
412 statement:
413 statement assignment end
414 | statement CREATE_OBJECT_SYMBOLS
415 {
416 lang_add_attribute(lang_object_symbols_statement_enum); }
417 | statement ';'
418 | statement CONSTRUCTORS
419 {
420 lang_add_attribute(lang_constructors_statement_enum); }
421
422 | statement input_section_spec
423 | statement length '(' exp ')'
424 {
425 lang_add_data($2,$4);
426 }
427
428 | statement FILL '(' exp ')'
429 {
430 lang_add_fill
431 (exp_get_value_int($4,
432 0,
433 "fill value",
434 lang_first_phase_enum));
435 }
436 |
437 ;
438
439 length:
440 LONG
441 { $$ = $1; }
442 | SHORT
443 { $$ = $1; }
444 | BYTE
445 { $$ = $1; }
446 ;
447
448 fill_opt:
449 '=' exp
450 {
451 $$ = exp_get_value_int($2,
452 0,
453 "fill value",
454 lang_first_phase_enum);
455 }
456 | { $$ = 0; }
457 ;
458
459
460
461 assign_op:
462 PLUSEQ
463 { $$ = '+'; }
464 | MINUSEQ
465 { $$ = '-'; }
466 | MULTEQ
467 { $$ = '*'; }
468 | DIVEQ
469 { $$ = '/'; }
470 | LSHIFTEQ
471 { $$ = LSHIFT; }
472 | RSHIFTEQ
473 { $$ = RSHIFT; }
474 | ANDEQ
475 { $$ = '&'; }
476 | OREQ
477 { $$ = '|'; }
478
479 ;
480
481 end: ';' | ','
482 ;
483
484
485 assignment:
486 NAME '=' exp
487 {
488 lang_add_assignment(exp_assop($2,$1,$3));
489 }
490 | NAME assign_op exp
491 {
492 lang_add_assignment(exp_assop('=',$1,exp_binop($2,exp_nameop(NAME,$1),$3)));
493 }
494
495 ;
496
497
498 opt_comma:
499 ',' | ;
500
501
502 memory:
503 MEMORY '{' memory_spec memory_spec_list '}'
504 ;
505
506 memory_spec_list:
507 memory_spec_list memory_spec
508 | memory_spec_list ',' memory_spec
509 |
510 ;
511
512
513 memory_spec:
514 NAME
515 { region = lang_memory_region_lookup($1); }
516 attributes_opt ':' origin_spec opt_comma length_spec
517
518 {
519
520
521 }
522 ;
523 origin_spec:
524 ORIGIN '=' exp
525 { region->current =
526 region->origin =
527 exp_get_vma($3, 0L,"origin", lang_first_phase_enum); }
528 ;
529 length_spec:
530 LENGTH '=' exp
531 { region->length = exp_get_vma($3,
532 ~((bfd_vma)0),
533 "length",
534 lang_first_phase_enum);
535 }
536
537
538 attributes_opt:
539 '(' NAME ')'
540 {
541 lang_set_flags(&region->flags, $2);
542 }
543 |
544
545 ;
546
547 startup:
548 STARTUP '(' filename ')'
549 { lang_startup($3); }
550 ;
551
552 high_level_library:
553 HLL '(' high_level_library_NAME_list ')'
554 | HLL '(' ')'
555 { ldemul_hll((char *)NULL); }
556 ;
557
558 high_level_library_NAME_list:
559 high_level_library_NAME_list opt_comma filename
560 { ldemul_hll($3); }
561 | filename
562 { ldemul_hll($1); }
563
564 ;
565
566 low_level_library:
567 SYSLIB '(' low_level_library_NAME_list ')'
568 ;
569 low_level_library_NAME_list:
570 low_level_library_NAME_list opt_comma filename
571 { ldemul_syslib($3); }
572 |
573 ;
574
575 floating_point_support:
576 FLOAT
577 { lang_float(true); }
578 | NOFLOAT
579 { lang_float(false); }
580 ;
581
582
583
584
585 exp :
586 '-' exp %prec UNARY
587 { $$ = exp_unop('-', $2); }
588 | '(' exp ')'
589 { $$ = $2; }
590 | NEXT '(' exp ')' %prec UNARY
591 { $$ = exp_unop($1,$3); }
592 | '!' exp %prec UNARY
593 { $$ = exp_unop('!', $2); }
594 | '+' exp %prec UNARY
595 { $$ = $2; }
596 | '~' exp %prec UNARY
597 { $$ = exp_unop('~', $2);}
598
599 | exp '*' exp
600 { $$ = exp_binop('*', $1, $3); }
601 | exp '/' exp
602 { $$ = exp_binop('/', $1, $3); }
603 | exp '%' exp
604 { $$ = exp_binop('%', $1, $3); }
605 | exp '+' exp
606 { $$ = exp_binop('+', $1, $3); }
607 | exp '-' exp
608 { $$ = exp_binop('-' , $1, $3); }
609 | exp LSHIFT exp
610 { $$ = exp_binop(LSHIFT , $1, $3); }
611 | exp RSHIFT exp
612 { $$ = exp_binop(RSHIFT , $1, $3); }
613 | exp EQ exp
614 { $$ = exp_binop(EQ , $1, $3); }
615 | exp NE exp
616 { $$ = exp_binop(NE , $1, $3); }
617 | exp LE exp
618 { $$ = exp_binop(LE , $1, $3); }
619 | exp GE exp
620 { $$ = exp_binop(GE , $1, $3); }
621 | exp '<' exp
622 { $$ = exp_binop('<' , $1, $3); }
623 | exp '>' exp
624 { $$ = exp_binop('>' , $1, $3); }
625 | exp '&' exp
626 { $$ = exp_binop('&' , $1, $3); }
627 | exp '^' exp
628 { $$ = exp_binop('^' , $1, $3); }
629 | exp '|' exp
630 { $$ = exp_binop('|' , $1, $3); }
631 | exp '?' exp ':' exp
632 { $$ = exp_trinop('?' , $1, $3, $5); }
633 | exp ANDAND exp
634 { $$ = exp_binop(ANDAND , $1, $3); }
635 | exp OROR exp
636 { $$ = exp_binop(OROR , $1, $3); }
637 | DEFINED '(' NAME ')'
638 { $$ = exp_nameop(DEFINED, $3); }
639 | INT
640 { $$ = exp_intop($1); }
641 | SIZEOF_HEADERS
642 { $$ = exp_nameop(SIZEOF_HEADERS,0); }
643
644 | SIZEOF '(' NAME ')'
645 { $$ = exp_nameop(SIZEOF,$3); }
646 | ADDR '(' NAME ')'
647 { $$ = exp_nameop(ADDR,$3); }
648 | ALIGN_K '(' exp ')'
649 { $$ = exp_unop(ALIGN_K,$3); }
650 | NAME
651 { $$ = exp_nameop(NAME,$1); }
652 ;
653
654
655
656
657 section: NAME opt_exp opt_type opt_block ':' opt_things'{'
658 {
659 lang_enter_output_section_statement($1,$2,$3,$4);
660 }
661 statement '}' fill_opt memspec_opt
662 {
663 lang_leave_output_section_statement($11, $12);
664 }
665
666 ;
667
668 opt_type:
669 '(' NOLOAD ')' { $$ = SEC_NO_FLAGS; }
670 | '(' DSECT ')' { $$ = 0; }
671 | '(' COPY ')' { $$ = 0; }
672 | '(' INFO ')' { $$ = 0; }
673 | '(' OVERLAY ')' { $$ = 0; }
674 | { $$ = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS; }
675 ;
676
677 opt_things:
678 {
679
680 }
681 ;
682
683
684
685
686
687 opt_exp:
688 exp
689 { $$ = $1; }
690 | { $$= (etree_type *)NULL; }
691 ;
692
693 opt_block:
694 BLOCK '(' exp ')'
695 { $$ = exp_get_value_int($3,
696 1L,
697 "block",
698 lang_first_phase_enum);
699 }
700 | { $$ = 1; }
701 ;
702
703 memspec_opt:
704 '>' NAME
705 { $$ = $2; }
706 | { $$ = "*default*"; }
707 ;
708
This page took 0.053885 seconds and 5 git commands to generate.