1 /* A YACC grammar to parse a superset of the AT&T linker scripting language.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001 Free Software Foundation, Inc.
6 This file is part of GNU ld.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #define DONTDECLARE_MALLOC
48 static enum section_type sectype;
50 lang_memory_region_type *region;
52 boolean ldgram_want_filename = true;
53 FILE * saved_script_handle = NULL;
54 boolean force_make_executable = false;
56 boolean ldgram_in_script = false;
57 boolean ldgram_had_equals = false;
58 boolean ldgram_had_keep = false;
59 char *ldgram_vers_current_lang = NULL;
61 #define ERROR_NAME_MAX 20
62 static char *error_names[ERROR_NAME_MAX];
63 static int error_index;
64 #define PUSH_ERROR(x) if (error_index < ERROR_NAME_MAX) error_names[error_index] = x; error_index++;
65 #define POP_ERROR() error_index--;
71 struct wildcard_spec wildcard;
72 struct wildcard_list *wildcard_list;
73 struct name_list *name_list;
75 union etree_union *etree;
80 union etree_union *at;
81 union etree_union *flags;
83 struct lang_nocrossref *nocrossref;
84 struct lang_output_section_phdr_list *section_phdr;
85 struct bfd_elf_version_deps *deflist;
86 struct bfd_elf_version_expr *versyms;
87 struct bfd_elf_version_tree *versnode;
90 %type <etree> exp opt_exp_with_type mustbe_exp opt_at phdr_type phdr_val
91 %type <etree> opt_exp_without_type
92 %type <integer> fill_opt
93 %type <name_list> exclude_name_list
94 %type <wildcard_list> file_NAME_list
95 %type <name> memspec_opt casesymlist
96 %type <name> memspec_at_opt
97 %type <cname> wildcard_name
98 %type <wildcard> wildcard_spec
100 %token <name> NAME LNAME
101 %type <integer> length
102 %type <phdr> phdr_qualifiers
103 %type <nocrossref> nocrossref_list
104 %type <section_phdr> phdr_opt
105 %type <integer> opt_nocrossrefs
107 %right <token> PLUSEQ MINUSEQ MULTEQ DIVEQ '=' LSHIFTEQ RSHIFTEQ ANDEQ OREQ
108 %right <token> '?' ':'
115 %left <token> '<' '>' LE GE
116 %left <token> LSHIFT RSHIFT
118 %left <token> '+' '-'
119 %left <token> '*' '/' '%'
124 %token <token> ALIGN_K BLOCK BIND QUAD SQUAD LONG SHORT BYTE
125 %token SECTIONS PHDRS SORT
127 %token SIZEOF_HEADERS OUTPUT_FORMAT FORCE_COMMON_ALLOCATION OUTPUT_ARCH
128 %token INHIBIT_COMMON_ALLOCATION
129 %token SIZEOF_HEADERS
131 %token MEMORY DEFSYMEND
132 %token NOLOAD DSECT COPY INFO OVERLAY
133 %token NAME LNAME DEFINED TARGET_K SEARCH_DIR MAP ENTRY
134 %token <integer> NEXT
135 %token SIZEOF ADDR LOADADDR MAX_K MIN_K
136 %token STARTUP HLL SYSLIB FLOAT NOFLOAT NOCROSSREFS
138 %token LENGTH CREATE_OBJECT_SYMBOLS INPUT GROUP OUTPUT CONSTRUCTORS
139 %token ALIGNMOD AT PROVIDE
140 %type <token> assign_op atype attributes_opt
141 %type <name> filename
142 %token CHIP LIST SECT ABSOLUTE LOAD NEWLINE ENDWORD ORDER NAMEWORD ASSERT_K
143 %token FORMAT PUBLIC DEFSYMEND BASE ALIAS TRUNCATE REL
144 %token INPUT_SCRIPT INPUT_MRI_SCRIPT INPUT_DEFSYM CASE EXTERN START
145 %token <name> VERS_TAG VERS_IDENTIFIER
146 %token GLOBAL LOCAL VERSIONK INPUT_VERSION_SCRIPT
149 %type <versyms> vers_defns
150 %type <versnode> vers_tag
151 %type <deflist> verdep
156 INPUT_SCRIPT script_file
157 | INPUT_MRI_SCRIPT mri_script_file
158 | INPUT_VERSION_SCRIPT version_script_file
159 | INPUT_DEFSYM defsym_expr
171 lang_add_assignment(exp_assop($3,$2,$4));
174 /* SYNTAX WITHIN AN MRI SCRIPT FILE */
178 PUSH_ERROR (_("MRI style script"));
189 mri_script_lines mri_script_command NEWLINE
197 einfo(_("%P%F: unrecognised keyword in MRI style script '%s'\n"),$1);
200 config.map_filename = "-";
202 | ORDER ordernamelist
204 | PUBLIC NAME '=' exp
205 { mri_public($2, $4); }
206 | PUBLIC NAME ',' exp
207 { mri_public($2, $4); }
209 { mri_public($2, $3); }
213 { mri_output_section($2, $4);}
215 { mri_output_section($2, $3);}
217 { mri_output_section($2, $4);}
218 | ALIGN_K NAME '=' exp
219 { mri_align($2,$4); }
220 | ALIGN_K NAME ',' exp
221 { mri_align($2,$4); }
222 | ALIGNMOD NAME '=' exp
223 { mri_alignmod($2,$4); }
224 | ALIGNMOD NAME ',' exp
225 { mri_alignmod($2,$4); }
226 | ABSOLUTE mri_abs_name_list
227 | LOAD mri_load_name_list
230 | ALIAS NAME ',' NAME
231 { mri_alias($2,$4,0);}
233 { mri_alias($2,0,(int) $4);}
237 { mri_truncate((unsigned int) $2); }
239 | EXTERN extern_name_list
241 { ldfile_open_command_file ($2); } mri_script_lines END
243 { lang_add_entry ($2, false); }
248 ordernamelist ',' NAME { mri_order($3); }
249 | ordernamelist NAME { mri_order($2); }
256 | mri_load_name_list ',' NAME { mri_load($3); }
261 { mri_only_load($1); }
262 | mri_abs_name_list ',' NAME
263 { mri_only_load($3); }
267 /* empty */ { $$ = NULL; }
269 | casesymlist ',' NAME
274 { ldlang_add_undef ($1); }
275 | extern_name_list NAME
276 { ldlang_add_undef ($2); }
277 | extern_name_list ',' NAME
278 { ldlang_add_undef ($3); }
306 | floating_point_support
310 | TARGET_K '(' NAME ')'
311 { lang_add_target($3); }
312 | SEARCH_DIR '(' filename ')'
313 { ldfile_add_library_path ($3, false); }
314 | OUTPUT '(' filename ')'
315 { lang_add_output($3, 1); }
316 | OUTPUT_FORMAT '(' NAME ')'
317 { lang_add_output_format ($3, (char *) NULL,
319 | OUTPUT_FORMAT '(' NAME ',' NAME ',' NAME ')'
320 { lang_add_output_format ($3, $5, $7, 1); }
321 | OUTPUT_ARCH '(' NAME ')'
322 { ldfile_set_output_arch($3); }
323 | FORCE_COMMON_ALLOCATION
324 { command_line.force_common_definition = true ; }
325 | INHIBIT_COMMON_ALLOCATION
326 { command_line.inhibit_common_definition = true ; }
327 | INPUT '(' input_list ')'
329 { lang_enter_group (); }
331 { lang_leave_group (); }
332 | MAP '(' filename ')'
333 { lang_add_map($3); }
335 { ldfile_open_command_file($2); } ifile_list END
336 | NOCROSSREFS '(' nocrossref_list ')'
338 lang_add_nocrossref ($3);
340 | EXTERN '(' extern_name_list ')'
345 { lang_add_input_file($1,lang_input_file_is_search_file_enum,
347 | input_list ',' NAME
348 { lang_add_input_file($3,lang_input_file_is_search_file_enum,
351 { lang_add_input_file($2,lang_input_file_is_search_file_enum,
354 { lang_add_input_file($1,lang_input_file_is_l_enum,
356 | input_list ',' LNAME
357 { lang_add_input_file($3,lang_input_file_is_l_enum,
360 { lang_add_input_file($2,lang_input_file_is_l_enum,
365 SECTIONS '{' sec_or_group_p1 '}'
369 sec_or_group_p1 section
370 | sec_or_group_p1 statement_anywhere
376 { lang_add_entry ($3, false); }
380 /* The '*' and '?' cases are there because the lexer returns them as
381 separate tokens rather than as NAME. */
402 $$.exclude_name_list = NULL;
404 | EXCLUDE_FILE '(' exclude_name_list ')' wildcard_name
408 $$.exclude_name_list = $3;
410 | SORT '(' wildcard_name ')'
414 $$.exclude_name_list = NULL;
416 | SORT '(' EXCLUDE_FILE '(' exclude_name_list ')' wildcard_name ')'
420 $$.exclude_name_list = $5;
425 exclude_name_list wildcard_name
427 struct name_list *tmp;
428 tmp = (struct name_list *) xmalloc (sizeof *tmp);
436 struct name_list *tmp;
437 tmp = (struct name_list *) xmalloc (sizeof *tmp);
445 file_NAME_list opt_comma wildcard_spec
447 struct wildcard_list *tmp;
448 tmp = (struct wildcard_list *) xmalloc (sizeof *tmp);
456 struct wildcard_list *tmp;
457 tmp = (struct wildcard_list *) xmalloc (sizeof *tmp);
464 input_section_spec_no_keep:
467 struct wildcard_spec tmp;
469 tmp.exclude_name_list = NULL;
471 lang_add_wild (&tmp, NULL, ldgram_had_keep);
473 | '[' file_NAME_list ']'
475 lang_add_wild (NULL, $2, ldgram_had_keep);
477 | wildcard_spec '(' file_NAME_list ')'
479 lang_add_wild (&$1, $3, ldgram_had_keep);
484 input_section_spec_no_keep
486 { ldgram_had_keep = true; }
487 input_section_spec_no_keep ')'
488 { ldgram_had_keep = false; }
493 | CREATE_OBJECT_SYMBOLS
495 lang_add_attribute(lang_object_symbols_statement_enum);
501 lang_add_attribute(lang_constructors_statement_enum);
503 | SORT '(' CONSTRUCTORS ')'
505 constructors_sorted = true;
506 lang_add_attribute (lang_constructors_statement_enum);
509 | length '(' mustbe_exp ')'
511 lang_add_data((int) $1,$3);
514 | FILL '(' mustbe_exp ')'
517 (exp_get_value_int($3,
520 lang_first_phase_enum));
525 statement_list statement
550 $$ = exp_get_value_int($2,
553 lang_first_phase_enum);
587 lang_add_assignment (exp_assop ($2, $1, $3));
589 | NAME assign_op mustbe_exp
591 lang_add_assignment (exp_assop ('=', $1,
597 | PROVIDE '(' NAME '=' mustbe_exp ')'
599 lang_add_assignment (exp_provide ($3, $5));
609 MEMORY '{' memory_spec memory_spec_list '}'
613 memory_spec_list memory_spec
614 | memory_spec_list ',' memory_spec
620 { region = lang_memory_region_lookup($1); }
622 origin_spec opt_comma length_spec
627 ORIGIN '=' mustbe_exp
630 exp_get_vma($3, 0L,"origin", lang_first_phase_enum);
635 LENGTH '=' mustbe_exp
636 { region->length = exp_get_vma($3,
639 lang_first_phase_enum);
645 { /* dummy action to avoid bison 1.25 error message */ }
646 | '(' attributes_list ')'
651 | attributes_list attributes_string
656 { lang_set_flags (region, $1, 0); }
658 { lang_set_flags (region, $2, 1); }
662 STARTUP '(' filename ')'
663 { lang_startup($3); }
667 HLL '(' high_level_library_NAME_list ')'
669 { ldemul_hll((char *)NULL); }
672 high_level_library_NAME_list:
673 high_level_library_NAME_list opt_comma filename
681 SYSLIB '(' low_level_library_NAME_list ')'
682 ; low_level_library_NAME_list:
683 low_level_library_NAME_list opt_comma filename
684 { ldemul_syslib($3); }
688 floating_point_support:
690 { lang_float(true); }
692 { lang_float(false); }
700 | NAME nocrossref_list
702 struct lang_nocrossref *n;
704 n = (struct lang_nocrossref *) xmalloc (sizeof *n);
709 | NAME ',' nocrossref_list
711 struct lang_nocrossref *n;
713 n = (struct lang_nocrossref *) xmalloc (sizeof *n);
720 mustbe_exp: { ldlex_expression(); }
722 { ldlex_popstate(); $$=$2;}
727 { $$ = exp_unop('-', $2); }
730 | NEXT '(' exp ')' %prec UNARY
731 { $$ = exp_unop((int) $1,$3); }
732 | '!' exp %prec UNARY
733 { $$ = exp_unop('!', $2); }
734 | '+' exp %prec UNARY
736 | '~' exp %prec UNARY
737 { $$ = exp_unop('~', $2);}
740 { $$ = exp_binop('*', $1, $3); }
742 { $$ = exp_binop('/', $1, $3); }
744 { $$ = exp_binop('%', $1, $3); }
746 { $$ = exp_binop('+', $1, $3); }
748 { $$ = exp_binop('-' , $1, $3); }
750 { $$ = exp_binop(LSHIFT , $1, $3); }
752 { $$ = exp_binop(RSHIFT , $1, $3); }
754 { $$ = exp_binop(EQ , $1, $3); }
756 { $$ = exp_binop(NE , $1, $3); }
758 { $$ = exp_binop(LE , $1, $3); }
760 { $$ = exp_binop(GE , $1, $3); }
762 { $$ = exp_binop('<' , $1, $3); }
764 { $$ = exp_binop('>' , $1, $3); }
766 { $$ = exp_binop('&' , $1, $3); }
768 { $$ = exp_binop('^' , $1, $3); }
770 { $$ = exp_binop('|' , $1, $3); }
771 | exp '?' exp ':' exp
772 { $$ = exp_trinop('?' , $1, $3, $5); }
774 { $$ = exp_binop(ANDAND , $1, $3); }
776 { $$ = exp_binop(OROR , $1, $3); }
777 | DEFINED '(' NAME ')'
778 { $$ = exp_nameop(DEFINED, $3); }
780 { $$ = exp_intop($1); }
782 { $$ = exp_nameop(SIZEOF_HEADERS,0); }
784 | SIZEOF '(' NAME ')'
785 { $$ = exp_nameop(SIZEOF,$3); }
787 { $$ = exp_nameop(ADDR,$3); }
788 | LOADADDR '(' NAME ')'
789 { $$ = exp_nameop(LOADADDR,$3); }
790 | ABSOLUTE '(' exp ')'
791 { $$ = exp_unop(ABSOLUTE, $3); }
792 | ALIGN_K '(' exp ')'
793 { $$ = exp_unop(ALIGN_K,$3); }
795 { $$ = exp_unop(ALIGN_K,$3); }
797 { $$ = exp_nameop(NAME,$1); }
798 | MAX_K '(' exp ',' exp ')'
799 { $$ = exp_binop (MAX_K, $3, $5 ); }
800 | MIN_K '(' exp ',' exp ')'
801 { $$ = exp_binop (MIN_K, $3, $5 ); }
802 | ASSERT_K '(' exp ',' NAME ')'
803 { $$ = exp_assert ($3, $5); }
808 AT '>' NAME { $$ = $3; }
809 | { $$ = "*default*"; }
813 AT '(' exp ')' { $$ = $3; }
817 section: NAME { ldlex_expression(); }
819 opt_at { ldlex_popstate (); ldlex_script (); }
822 lang_enter_output_section_statement($1, $3,
827 '}' { ldlex_popstate (); ldlex_expression (); }
828 memspec_opt memspec_at_opt phdr_opt fill_opt
831 lang_leave_output_section_statement ($14, $11, $13, $12);
835 { ldlex_expression (); }
836 opt_exp_without_type opt_nocrossrefs opt_at
837 { ldlex_popstate (); ldlex_script (); }
840 lang_enter_overlay ($3, $5, (int) $4);
844 { ldlex_popstate (); ldlex_expression (); }
845 memspec_opt memspec_at_opt phdr_opt fill_opt
848 lang_leave_overlay ($15, $12, $14, $13);
851 | /* The GROUP case is just enough to support the gcc
852 svr3.ifile script. It is not intended to be full
853 support. I'm not even sure what GROUP is supposed
855 GROUP { ldlex_expression (); }
859 lang_add_assignment (exp_assop ('=', ".", $3));
861 '{' sec_or_group_p1 '}'
865 NOLOAD { sectype = noload_section; }
866 | DSECT { sectype = dsect_section; }
867 | COPY { sectype = copy_section; }
868 | INFO { sectype = info_section; }
869 | OVERLAY { sectype = overlay_section; }
874 | /* EMPTY */ { sectype = normal_section; }
875 | '(' ')' { sectype = normal_section; }
879 exp atype ':' { $$ = $1; }
880 | atype ':' { $$ = (etree_type *)NULL; }
881 | /* The BIND cases are to support the gcc svr3.ifile
882 script. They aren't intended to implement full
883 support for the BIND keyword. I'm not even sure
884 what BIND is supposed to mean. */
885 BIND '(' exp ')' atype ':' { $$ = $3; }
886 | BIND '(' exp ')' BLOCK '(' exp ')' atype ':'
890 opt_exp_without_type:
892 | ':' { $$ = (etree_type *) NULL; }
905 | { $$ = "*default*"; }
915 struct lang_output_section_phdr_list *n;
917 n = ((struct lang_output_section_phdr_list *)
918 xmalloc (sizeof *n));
932 lang_enter_overlay_section ($2);
934 '{' statement_list_opt '}'
935 { ldlex_popstate (); ldlex_expression (); }
939 lang_leave_overlay_section ($9, $8);
945 PHDRS '{' phdr_list '}'
954 NAME { ldlex_expression (); }
955 phdr_type phdr_qualifiers { ldlex_popstate (); }
958 lang_new_phdr ($1, $3, $4.filehdr, $4.phdrs, $4.at,
968 if ($1->type.node_class == etree_name
969 && $1->type.node_code == NAME)
973 static const char * const phdr_types[] =
975 "PT_NULL", "PT_LOAD", "PT_DYNAMIC",
976 "PT_INTERP", "PT_NOTE", "PT_SHLIB",
982 i < sizeof phdr_types / sizeof phdr_types[0];
984 if (strcmp (s, phdr_types[i]) == 0)
996 memset (&$$, 0, sizeof (struct phdr_info));
998 | NAME phdr_val phdr_qualifiers
1001 if (strcmp ($1, "FILEHDR") == 0 && $2 == NULL)
1003 else if (strcmp ($1, "PHDRS") == 0 && $2 == NULL)
1005 else if (strcmp ($1, "FLAGS") == 0 && $2 != NULL)
1008 einfo (_("%X%P:%S: PHDRS syntax error at `%s'\n"), $1);
1010 | AT '(' exp ')' phdr_qualifiers
1028 /* This syntax is used within an external version script file. */
1030 version_script_file:
1032 ldlex_version_file ();
1033 PUSH_ERROR (_("VERSION script"));
1042 /* This is used within a normal linker script file. */
1046 ldlex_version_script ();
1048 VERSIONK '{' vers_nodes '}'
1056 | vers_nodes vers_node
1060 VERS_TAG '{' vers_tag '}' ';'
1062 lang_register_vers_node ($1, $3, NULL);
1064 | VERS_TAG '{' vers_tag '}' verdep ';'
1066 lang_register_vers_node ($1, $3, $5);
1073 $$ = lang_add_vers_depend (NULL, $1);
1077 $$ = lang_add_vers_depend ($1, $2);
1084 $$ = lang_new_vers_node (NULL, NULL);
1088 $$ = lang_new_vers_node ($1, NULL);
1090 | GLOBAL ':' vers_defns ';'
1092 $$ = lang_new_vers_node ($3, NULL);
1094 | LOCAL ':' vers_defns ';'
1096 $$ = lang_new_vers_node (NULL, $3);
1098 | GLOBAL ':' vers_defns ';' LOCAL ':' vers_defns ';'
1100 $$ = lang_new_vers_node ($3, $7);
1107 $$ = lang_new_vers_regex (NULL, $1, ldgram_vers_current_lang);
1109 | vers_defns ';' VERS_IDENTIFIER
1111 $$ = lang_new_vers_regex ($1, $3, ldgram_vers_current_lang);
1115 $<name>$ = ldgram_vers_current_lang;
1116 ldgram_vers_current_lang = $2;
1121 ldgram_vers_current_lang = $<name>4;
1130 if (ldfile_assumed_script)
1131 einfo (_("%P:%s: file format not recognized; treating as linker script\n"),
1132 ldfile_input_filename);
1133 if (error_index > 0 && error_index < ERROR_NAME_MAX)
1134 einfo ("%P%F:%S: %s in %s\n", arg, error_names[error_index-1]);
1136 einfo ("%P%F:%S: %s\n", arg);