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