1 /* A YACC grammer to parse a superset of the AT&T linker scripting languaue.
2 Copyright (C) 1991, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
5 This file is part of GNU ld.
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.
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.
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. */
26 #define DONTDECLARE_MALLOC
46 static enum section_type sectype;
48 lang_memory_region_type *region;
52 boolean ldgram_want_filename = true;
53 boolean had_script = false;
54 boolean force_make_executable = false;
56 boolean ldgram_in_script = false;
57 boolean ldgram_had_equals = false;
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--;
70 union etree_union *etree;
75 union etree_union *at;
76 union etree_union *flags;
78 struct lang_nocrossref *nocrossref;
81 %type <etree> exp opt_exp_with_type mustbe_exp opt_at phdr_type phdr_val
82 %type <integer> fill_opt
83 %type <name> memspec_opt casesymlist
85 %token <name> NAME LNAME
86 %type <integer> length
87 %type <phdr> phdr_qualifiers
88 %type <nocrossref> nocrossref_list
90 %right <token> PLUSEQ MINUSEQ MULTEQ DIVEQ '=' LSHIFTEQ RSHIFTEQ ANDEQ OREQ
91 %right <token> '?' ':'
98 %left <token> '<' '>' LE GE
99 %left <token> LSHIFT RSHIFT
101 %left <token> '+' '-'
102 %left <token> '*' '/' '%'
107 %token <token> ALIGN_K BLOCK BIND QUAD LONG SHORT BYTE
108 %token SECTIONS PHDRS
110 %token SIZEOF_HEADERS OUTPUT_FORMAT FORCE_COMMON_ALLOCATION OUTPUT_ARCH
111 %token SIZEOF_HEADERS
113 %token MEMORY DEFSYMEND
114 %token NOLOAD DSECT COPY INFO OVERLAY
115 %token NAME LNAME DEFINED TARGET_K SEARCH_DIR MAP ENTRY
116 %token <integer> SIZEOF NEXT ADDR
117 %token STARTUP HLL SYSLIB FLOAT NOFLOAT NOCROSSREFS
119 %token LENGTH CREATE_OBJECT_SYMBOLS INPUT GROUP OUTPUT CONSTRUCTORS
120 %token ALIGNMOD AT PROVIDE
121 %type <token> assign_op atype
122 %type <name> filename
123 %token CHIP LIST SECT ABSOLUTE LOAD NEWLINE ENDWORD ORDER NAMEWORD
124 %token FORMAT PUBLIC DEFSYMEND BASE ALIAS TRUNCATE REL
125 %token INPUT_SCRIPT INPUT_MRI_SCRIPT INPUT_DEFSYM CASE EXTERN START
130 INPUT_SCRIPT script_file
131 | INPUT_MRI_SCRIPT mri_script_file
132 | INPUT_DEFSYM defsym_expr
144 lang_add_assignment(exp_assop($3,$2,$4));
147 /* SYNTAX WITHIN AN MRI SCRIPT FILE */
151 PUSH_ERROR ("MRI style script");
162 mri_script_lines mri_script_command NEWLINE
170 einfo("%P%F: unrecognised keyword in MRI style script '%s'\n",$1);
173 config.map_filename = "-";
175 | ORDER ordernamelist
177 | PUBLIC NAME '=' exp
178 { mri_public($2, $4); }
179 | PUBLIC NAME ',' exp
180 { mri_public($2, $4); }
182 { mri_public($2, $3); }
186 { mri_output_section($2, $4);}
188 { mri_output_section($2, $3);}
190 { mri_output_section($2, $4);}
191 | ALIGN_K NAME '=' exp
192 { mri_align($2,$4); }
193 | ALIGN_K NAME ',' exp
194 { mri_align($2,$4); }
195 | ALIGNMOD NAME '=' exp
196 { mri_alignmod($2,$4); }
197 | ALIGNMOD NAME ',' exp
198 { mri_alignmod($2,$4); }
199 | ABSOLUTE mri_abs_name_list
200 | LOAD mri_load_name_list
203 | ALIAS NAME ',' NAME
204 { mri_alias($2,$4,0);}
206 { mri_alias($2,0,(int) $4);}
210 { mri_truncate((unsigned int) $2); }
212 | EXTERN extern_name_list
214 { ldfile_open_command_file ($2); } mri_script_lines END
216 { lang_add_entry ($2, false); }
221 ordernamelist ',' NAME { mri_order($3); }
222 | ordernamelist NAME { mri_order($2); }
229 | mri_load_name_list ',' NAME { mri_load($3); }
234 { mri_only_load($1); }
235 | mri_abs_name_list ',' NAME
236 { mri_only_load($3); }
240 /* empty */ { $$ = NULL; }
242 | casesymlist ',' NAME
247 { ldlang_add_undef ($1); }
248 | extern_name_list ',' NAME
249 { ldlang_add_undef ($3); }
277 | floating_point_support
280 | TARGET_K '(' NAME ')'
281 { lang_add_target($3); }
282 | SEARCH_DIR '(' filename ')'
283 { ldfile_add_library_path ($3, false); }
284 | OUTPUT '(' filename ')'
285 { lang_add_output($3, 1); }
286 | OUTPUT_FORMAT '(' NAME ')'
287 { lang_add_output_format ($3, (char *) NULL,
289 | OUTPUT_FORMAT '(' NAME ',' NAME ',' NAME ')'
290 { lang_add_output_format ($3, $5, $7, 1); }
291 | OUTPUT_ARCH '(' NAME ')'
292 { ldfile_set_output_arch($3); }
293 | FORCE_COMMON_ALLOCATION
294 { command_line.force_common_definition = true ; }
295 | INPUT '(' input_list ')'
297 { lang_enter_group (); }
299 { lang_leave_group (); }
300 | MAP '(' filename ')'
301 { lang_add_map($3); }
303 { ldfile_open_command_file($2); } ifile_list END
304 | NOCROSSREFS '(' nocrossref_list ')'
306 lang_add_nocrossref ($3);
312 { lang_add_input_file($1,lang_input_file_is_search_file_enum,
314 | input_list ',' NAME
315 { lang_add_input_file($3,lang_input_file_is_search_file_enum,
318 { lang_add_input_file($2,lang_input_file_is_search_file_enum,
321 { lang_add_input_file($1,lang_input_file_is_l_enum,
323 | input_list ',' LNAME
324 { lang_add_input_file($3,lang_input_file_is_l_enum,
327 { lang_add_input_file($2,lang_input_file_is_l_enum,
332 SECTIONS '{' sec_or_group_p1 '}'
336 sec_or_group_p1 section
337 | sec_or_group_p1 statement_anywhere
343 { lang_add_entry ($3, false); }
347 /* The '*' and '?' cases are there because the lexer returns them as
348 separate tokens rather than as NAME. */
351 { lang_add_wild ($1, current_file); }
353 { lang_add_wild ("*", current_file); }
355 { lang_add_wild ("?", current_file); }
356 | file_NAME_list opt_comma NAME
357 { lang_add_wild ($3, current_file); }
358 | file_NAME_list opt_comma '*'
359 { lang_add_wild ("*", current_file); }
360 | file_NAME_list opt_comma '?'
361 { lang_add_wild ("?", current_file); }
367 lang_add_wild((char *)NULL, $1);
371 current_file = (char *)NULL;
379 '(' file_NAME_list ')'
381 /* This case is needed because the lexer returns a
382 single question mark as '?' rather than NAME. */
386 '(' file_NAME_list ')'
389 current_file = (char *)NULL;
391 '(' file_NAME_list ')'
396 | CREATE_OBJECT_SYMBOLS
398 lang_add_attribute(lang_object_symbols_statement_enum);
404 lang_add_attribute(lang_constructors_statement_enum);
407 | length '(' mustbe_exp ')'
409 lang_add_data((int) $1,$3);
412 | FILL '(' mustbe_exp ')'
415 (exp_get_value_int($3,
418 lang_first_phase_enum));
423 statement_list statement
446 $$ = exp_get_value_int($2,
449 lang_first_phase_enum);
483 lang_add_assignment (exp_assop ($2, $1, $3));
485 | NAME assign_op mustbe_exp
487 lang_add_assignment (exp_assop ('=', $1,
493 | PROVIDE '(' NAME '=' mustbe_exp ')'
495 lang_add_assignment (exp_provide ($3, $5));
505 MEMORY '{' memory_spec memory_spec_list '}'
509 memory_spec_list memory_spec
510 | memory_spec_list ',' memory_spec
516 { region = lang_memory_region_lookup($1); }
518 origin_spec opt_comma length_spec
521 ORIGIN '=' mustbe_exp
524 exp_get_vma($3, 0L,"origin", lang_first_phase_enum);
527 LENGTH '=' mustbe_exp
528 { region->length = exp_get_vma($3,
531 lang_first_phase_enum);
538 lang_set_flags(®ion->flags, $2);
545 STARTUP '(' filename ')'
546 { lang_startup($3); }
550 HLL '(' high_level_library_NAME_list ')'
552 { ldemul_hll((char *)NULL); }
555 high_level_library_NAME_list:
556 high_level_library_NAME_list opt_comma filename
564 SYSLIB '(' low_level_library_NAME_list ')'
565 ; low_level_library_NAME_list:
566 low_level_library_NAME_list opt_comma filename
567 { ldemul_syslib($3); }
571 floating_point_support:
573 { lang_float(true); }
575 { lang_float(false); }
583 | NAME nocrossref_list
585 struct lang_nocrossref *n;
587 n = (struct lang_nocrossref *) xmalloc (sizeof *n);
592 | NAME ',' nocrossref_list
594 struct lang_nocrossref *n;
596 n = (struct lang_nocrossref *) xmalloc (sizeof *n);
603 mustbe_exp: { ldlex_expression(); }
605 { ldlex_popstate(); $$=$2;}
610 { $$ = exp_unop('-', $2); }
613 | NEXT '(' exp ')' %prec UNARY
614 { $$ = exp_unop((int) $1,$3); }
615 | '!' exp %prec UNARY
616 { $$ = exp_unop('!', $2); }
617 | '+' exp %prec UNARY
619 | '~' exp %prec UNARY
620 { $$ = exp_unop('~', $2);}
623 { $$ = exp_binop('*', $1, $3); }
625 { $$ = exp_binop('/', $1, $3); }
627 { $$ = exp_binop('%', $1, $3); }
629 { $$ = exp_binop('+', $1, $3); }
631 { $$ = exp_binop('-' , $1, $3); }
633 { $$ = exp_binop(LSHIFT , $1, $3); }
635 { $$ = exp_binop(RSHIFT , $1, $3); }
637 { $$ = exp_binop(EQ , $1, $3); }
639 { $$ = exp_binop(NE , $1, $3); }
641 { $$ = exp_binop(LE , $1, $3); }
643 { $$ = exp_binop(GE , $1, $3); }
645 { $$ = exp_binop('<' , $1, $3); }
647 { $$ = exp_binop('>' , $1, $3); }
649 { $$ = exp_binop('&' , $1, $3); }
651 { $$ = exp_binop('^' , $1, $3); }
653 { $$ = exp_binop('|' , $1, $3); }
654 | exp '?' exp ':' exp
655 { $$ = exp_trinop('?' , $1, $3, $5); }
657 { $$ = exp_binop(ANDAND , $1, $3); }
659 { $$ = exp_binop(OROR , $1, $3); }
660 | DEFINED '(' NAME ')'
661 { $$ = exp_nameop(DEFINED, $3); }
663 { $$ = exp_intop($1); }
665 { $$ = exp_nameop(SIZEOF_HEADERS,0); }
667 | SIZEOF '(' NAME ')'
668 { $$ = exp_nameop(SIZEOF,$3); }
670 { $$ = exp_nameop(ADDR,$3); }
671 | ABSOLUTE '(' exp ')'
672 { $$ = exp_unop(ABSOLUTE, $3); }
673 | ALIGN_K '(' exp ')'
674 { $$ = exp_unop(ALIGN_K,$3); }
676 { $$ = exp_unop(ALIGN_K,$3); }
678 { $$ = exp_nameop(NAME,$1); }
683 AT '(' exp ')' { $$ = $3; }
687 section: NAME { ldlex_expression(); }
689 opt_at { ldlex_popstate (); ldlex_script (); }
692 lang_enter_output_section_statement($1, $3,
697 '}' { ldlex_popstate (); ldlex_expression (); }
698 memspec_opt phdr_opt fill_opt
701 lang_leave_output_section_statement($13, $11);
704 | /* The GROUP case is just enough to support the gcc
705 svr3.ifile script. It is not intended to be full
706 support. I'm not even sure what GROUP is supposed
708 GROUP { ldlex_expression (); }
712 lang_add_assignment (exp_assop ('=', ".", $3));
714 '{' sec_or_group_p1 '}'
718 NOLOAD { sectype = noload_section; }
719 | DSECT { sectype = dsect_section; }
720 | COPY { sectype = copy_section; }
721 | INFO { sectype = info_section; }
722 | OVERLAY { sectype = overlay_section; }
727 | /* EMPTY */ { sectype = normal_section; }
731 exp atype ':' { $$ = $1; }
732 | atype ':' { $$ = (etree_type *)NULL; }
733 | /* The BIND cases are to support the gcc svr3.ifile
734 script. They aren't intended to implement full
735 support for the BIND keyword. I'm not even sure
736 what BIND is supposed to mean. */
737 BIND '(' exp ')' atype ':' { $$ = $3; }
738 | BIND '(' exp ')' BLOCK '(' exp ')' atype ':'
745 | { $$ = "*default*"; }
752 lang_section_in_phdr ($3);
757 PHDRS '{' phdr_list '}'
766 NAME { ldlex_expression (); }
767 phdr_type phdr_qualifiers { ldlex_popstate (); }
770 lang_new_phdr ($1, $3, $4.filehdr, $4.phdrs, $4.at,
780 if ($1->type.node_class == etree_name
781 && $1->type.node_code == NAME)
785 static const char * const phdr_types[] =
787 "PT_NULL", "PT_LOAD", "PT_DYNAMIC",
788 "PT_INTERP", "PT_NOTE", "PT_SHLIB",
794 i < sizeof phdr_types / sizeof phdr_types[0];
796 if (strcmp (s, phdr_types[i]) == 0)
808 memset (&$$, 0, sizeof (struct phdr_info));
810 | NAME phdr_val phdr_qualifiers
813 if (strcmp ($1, "FILEHDR") == 0 && $2 == NULL)
815 else if (strcmp ($1, "PHDRS") == 0 && $2 == NULL)
817 else if (strcmp ($1, "FLAGS") == 0 && $2 != NULL)
820 einfo ("%X%P:%S: PHDRS syntax error at `%s'\n", $1);
822 | AT '(' exp ')' phdr_qualifiers
845 if (ldfile_assumed_script)
846 einfo ("%P:%s: file format not recognized; treating as linker script\n",
847 ldfile_input_filename);
848 if (error_index > 0 && error_index < ERROR_NAME_MAX)
849 einfo ("%P%F:%S: %s in %s\n", arg, error_names[error_index-1]);
851 einfo ("%P%F:%S: %s\n", arg);