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, 2002 Free Software Foundation, Inc.
4 Written by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
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--;
77 struct wildcard_spec wildcard;
78 struct wildcard_list *wildcard_list;
79 struct name_list *name_list;
81 union etree_union *etree;
86 union etree_union *at;
87 union etree_union *flags;
89 struct lang_nocrossref *nocrossref;
90 struct lang_output_section_phdr_list *section_phdr;
91 struct bfd_elf_version_deps *deflist;
92 struct bfd_elf_version_expr *versyms;
93 struct bfd_elf_version_tree *versnode;
96 %type <etree> exp opt_exp_with_type mustbe_exp opt_at phdr_type phdr_val
97 %type <etree> opt_exp_without_type
98 %type <fill> fill_opt fill_exp
99 %type <name_list> exclude_name_list
100 %type <wildcard_list> file_NAME_list
101 %type <name> memspec_opt casesymlist
102 %type <name> memspec_at_opt
103 %type <cname> wildcard_name
104 %type <wildcard> wildcard_spec
106 %token <name> NAME LNAME
107 %type <integer> length
108 %type <phdr> phdr_qualifiers
109 %type <nocrossref> nocrossref_list
110 %type <section_phdr> phdr_opt
111 %type <integer> opt_nocrossrefs
113 %right <token> PLUSEQ MINUSEQ MULTEQ DIVEQ '=' LSHIFTEQ RSHIFTEQ ANDEQ OREQ
114 %right <token> '?' ':'
121 %left <token> '<' '>' LE GE
122 %left <token> LSHIFT RSHIFT
124 %left <token> '+' '-'
125 %left <token> '*' '/' '%'
130 %token <token> ALIGN_K BLOCK BIND QUAD SQUAD LONG SHORT BYTE
131 %token SECTIONS PHDRS SORT DATA_SEGMENT_ALIGN DATA_SEGMENT_END
133 %token SIZEOF_HEADERS OUTPUT_FORMAT FORCE_COMMON_ALLOCATION OUTPUT_ARCH
134 %token INHIBIT_COMMON_ALLOCATION
135 %token SIZEOF_HEADERS
137 %token MEMORY DEFSYMEND
138 %token NOLOAD DSECT COPY INFO OVERLAY
139 %token NAME LNAME DEFINED TARGET_K SEARCH_DIR MAP ENTRY
140 %token <integer> NEXT
141 %token SIZEOF ADDR LOADADDR MAX_K MIN_K
142 %token STARTUP HLL SYSLIB FLOAT NOFLOAT NOCROSSREFS
144 %token LENGTH CREATE_OBJECT_SYMBOLS INPUT GROUP OUTPUT CONSTRUCTORS
145 %token ALIGNMOD AT PROVIDE
146 %type <token> assign_op atype attributes_opt
147 %type <name> filename
148 %token CHIP LIST SECT ABSOLUTE LOAD NEWLINE ENDWORD ORDER NAMEWORD ASSERT_K
149 %token FORMAT PUBLIC DEFSYMEND BASE ALIAS TRUNCATE REL
150 %token INPUT_SCRIPT INPUT_MRI_SCRIPT INPUT_DEFSYM CASE EXTERN START
151 %token <name> VERS_TAG VERS_IDENTIFIER
152 %token GLOBAL LOCAL VERSIONK INPUT_VERSION_SCRIPT
155 %type <versyms> vers_defns
156 %type <versnode> vers_tag
157 %type <deflist> verdep
162 INPUT_SCRIPT script_file
163 | INPUT_MRI_SCRIPT mri_script_file
164 | INPUT_VERSION_SCRIPT version_script_file
165 | INPUT_DEFSYM defsym_expr
177 lang_add_assignment(exp_assop($3,$2,$4));
180 /* SYNTAX WITHIN AN MRI SCRIPT FILE */
184 PUSH_ERROR (_("MRI style script"));
195 mri_script_lines mri_script_command NEWLINE
203 einfo(_("%P%F: unrecognised keyword in MRI style script '%s'\n"),$1);
206 config.map_filename = "-";
208 | ORDER ordernamelist
210 | PUBLIC NAME '=' exp
211 { mri_public($2, $4); }
212 | PUBLIC NAME ',' exp
213 { mri_public($2, $4); }
215 { mri_public($2, $3); }
219 { mri_output_section($2, $4);}
221 { mri_output_section($2, $3);}
223 { mri_output_section($2, $4);}
224 | ALIGN_K NAME '=' exp
225 { mri_align($2,$4); }
226 | ALIGN_K NAME ',' exp
227 { mri_align($2,$4); }
228 | ALIGNMOD NAME '=' exp
229 { mri_alignmod($2,$4); }
230 | ALIGNMOD NAME ',' exp
231 { mri_alignmod($2,$4); }
232 | ABSOLUTE mri_abs_name_list
233 | LOAD mri_load_name_list
236 | ALIAS NAME ',' NAME
237 { mri_alias($2,$4,0);}
239 { mri_alias ($2, 0, (int) $4.integer); }
243 { mri_truncate ((unsigned int) $2.integer); }
245 | EXTERN extern_name_list
247 { ldlex_script (); ldfile_open_command_file($2); }
249 { ldlex_popstate (); }
251 { lang_add_entry ($2, false); }
256 ordernamelist ',' NAME { mri_order($3); }
257 | ordernamelist NAME { mri_order($2); }
264 | mri_load_name_list ',' NAME { mri_load($3); }
269 { mri_only_load($1); }
270 | mri_abs_name_list ',' NAME
271 { mri_only_load($3); }
275 /* empty */ { $$ = NULL; }
277 | casesymlist ',' NAME
282 { ldlang_add_undef ($1); }
283 | extern_name_list NAME
284 { ldlang_add_undef ($2); }
285 | extern_name_list ',' NAME
286 { ldlang_add_undef ($3); }
314 | floating_point_support
318 | TARGET_K '(' NAME ')'
319 { lang_add_target($3); }
320 | SEARCH_DIR '(' filename ')'
321 { ldfile_add_library_path ($3, false); }
322 | OUTPUT '(' filename ')'
323 { lang_add_output($3, 1); }
324 | OUTPUT_FORMAT '(' NAME ')'
325 { lang_add_output_format ($3, (char *) NULL,
327 | OUTPUT_FORMAT '(' NAME ',' NAME ',' NAME ')'
328 { lang_add_output_format ($3, $5, $7, 1); }
329 | OUTPUT_ARCH '(' NAME ')'
330 { ldfile_set_output_arch($3); }
331 | FORCE_COMMON_ALLOCATION
332 { command_line.force_common_definition = true ; }
333 | INHIBIT_COMMON_ALLOCATION
334 { command_line.inhibit_common_definition = true ; }
335 | INPUT '(' input_list ')'
337 { lang_enter_group (); }
339 { lang_leave_group (); }
340 | MAP '(' filename ')'
341 { lang_add_map($3); }
343 { ldlex_script (); ldfile_open_command_file($2); }
345 { ldlex_popstate (); }
346 | NOCROSSREFS '(' nocrossref_list ')'
348 lang_add_nocrossref ($3);
350 | EXTERN '(' extern_name_list ')'
355 { lang_add_input_file($1,lang_input_file_is_search_file_enum,
357 | input_list ',' NAME
358 { lang_add_input_file($3,lang_input_file_is_search_file_enum,
361 { lang_add_input_file($2,lang_input_file_is_search_file_enum,
364 { lang_add_input_file($1,lang_input_file_is_l_enum,
366 | input_list ',' LNAME
367 { lang_add_input_file($3,lang_input_file_is_l_enum,
370 { lang_add_input_file($2,lang_input_file_is_l_enum,
375 SECTIONS '{' sec_or_group_p1 '}'
379 sec_or_group_p1 section
380 | sec_or_group_p1 statement_anywhere
386 { lang_add_entry ($3, false); }
390 /* The '*' and '?' cases are there because the lexer returns them as
391 separate tokens rather than as NAME. */
412 $$.exclude_name_list = NULL;
414 | EXCLUDE_FILE '(' exclude_name_list ')' wildcard_name
418 $$.exclude_name_list = $3;
420 | SORT '(' wildcard_name ')'
424 $$.exclude_name_list = NULL;
426 | SORT '(' EXCLUDE_FILE '(' exclude_name_list ')' wildcard_name ')'
430 $$.exclude_name_list = $5;
435 exclude_name_list wildcard_name
437 struct name_list *tmp;
438 tmp = (struct name_list *) xmalloc (sizeof *tmp);
446 struct name_list *tmp;
447 tmp = (struct name_list *) xmalloc (sizeof *tmp);
455 file_NAME_list opt_comma wildcard_spec
457 struct wildcard_list *tmp;
458 tmp = (struct wildcard_list *) xmalloc (sizeof *tmp);
466 struct wildcard_list *tmp;
467 tmp = (struct wildcard_list *) xmalloc (sizeof *tmp);
474 input_section_spec_no_keep:
477 struct wildcard_spec tmp;
479 tmp.exclude_name_list = NULL;
481 lang_add_wild (&tmp, NULL, ldgram_had_keep);
483 | '[' file_NAME_list ']'
485 lang_add_wild (NULL, $2, ldgram_had_keep);
487 | wildcard_spec '(' file_NAME_list ')'
489 lang_add_wild (&$1, $3, ldgram_had_keep);
494 input_section_spec_no_keep
496 { ldgram_had_keep = true; }
497 input_section_spec_no_keep ')'
498 { ldgram_had_keep = false; }
503 | CREATE_OBJECT_SYMBOLS
505 lang_add_attribute(lang_object_symbols_statement_enum);
511 lang_add_attribute(lang_constructors_statement_enum);
513 | SORT '(' CONSTRUCTORS ')'
515 constructors_sorted = true;
516 lang_add_attribute (lang_constructors_statement_enum);
519 | length '(' mustbe_exp ')'
521 lang_add_data ((int) $1, $3);
524 | FILL '(' fill_exp ')'
531 statement_list statement
556 $$ = exp_get_fill ($1,
559 lang_first_phase_enum);
566 | { $$ = (fill_type *) 0; }
596 lang_add_assignment (exp_assop ($2, $1, $3));
598 | NAME assign_op mustbe_exp
600 lang_add_assignment (exp_assop ('=', $1,
606 | PROVIDE '(' NAME '=' mustbe_exp ')'
608 lang_add_assignment (exp_provide ($3, $5));
618 MEMORY '{' memory_spec memory_spec_list '}'
622 memory_spec_list memory_spec
623 | memory_spec_list ',' memory_spec
629 { region = lang_memory_region_lookup($1); }
631 origin_spec opt_comma length_spec
636 ORIGIN '=' mustbe_exp
639 exp_get_vma($3, 0L,"origin", lang_first_phase_enum);
644 LENGTH '=' mustbe_exp
645 { region->length = exp_get_vma($3,
648 lang_first_phase_enum);
654 { /* dummy action to avoid bison 1.25 error message */ }
655 | '(' attributes_list ')'
660 | attributes_list attributes_string
665 { lang_set_flags (region, $1, 0); }
667 { lang_set_flags (region, $2, 1); }
671 STARTUP '(' filename ')'
672 { lang_startup($3); }
676 HLL '(' high_level_library_NAME_list ')'
678 { ldemul_hll((char *)NULL); }
681 high_level_library_NAME_list:
682 high_level_library_NAME_list opt_comma filename
690 SYSLIB '(' low_level_library_NAME_list ')'
691 ; low_level_library_NAME_list:
692 low_level_library_NAME_list opt_comma filename
693 { ldemul_syslib($3); }
697 floating_point_support:
699 { lang_float(true); }
701 { lang_float(false); }
709 | NAME nocrossref_list
711 struct lang_nocrossref *n;
713 n = (struct lang_nocrossref *) xmalloc (sizeof *n);
718 | NAME ',' nocrossref_list
720 struct lang_nocrossref *n;
722 n = (struct lang_nocrossref *) xmalloc (sizeof *n);
729 mustbe_exp: { ldlex_expression(); }
731 { ldlex_popstate(); $$=$2;}
736 { $$ = exp_unop('-', $2); }
739 | NEXT '(' exp ')' %prec UNARY
740 { $$ = exp_unop((int) $1,$3); }
741 | '!' exp %prec UNARY
742 { $$ = exp_unop('!', $2); }
743 | '+' exp %prec UNARY
745 | '~' exp %prec UNARY
746 { $$ = exp_unop('~', $2);}
749 { $$ = exp_binop('*', $1, $3); }
751 { $$ = exp_binop('/', $1, $3); }
753 { $$ = exp_binop('%', $1, $3); }
755 { $$ = exp_binop('+', $1, $3); }
757 { $$ = exp_binop('-' , $1, $3); }
759 { $$ = exp_binop(LSHIFT , $1, $3); }
761 { $$ = exp_binop(RSHIFT , $1, $3); }
763 { $$ = exp_binop(EQ , $1, $3); }
765 { $$ = exp_binop(NE , $1, $3); }
767 { $$ = exp_binop(LE , $1, $3); }
769 { $$ = exp_binop(GE , $1, $3); }
771 { $$ = exp_binop('<' , $1, $3); }
773 { $$ = exp_binop('>' , $1, $3); }
775 { $$ = exp_binop('&' , $1, $3); }
777 { $$ = exp_binop('^' , $1, $3); }
779 { $$ = exp_binop('|' , $1, $3); }
780 | exp '?' exp ':' exp
781 { $$ = exp_trinop('?' , $1, $3, $5); }
783 { $$ = exp_binop(ANDAND , $1, $3); }
785 { $$ = exp_binop(OROR , $1, $3); }
786 | DEFINED '(' NAME ')'
787 { $$ = exp_nameop(DEFINED, $3); }
789 { $$ = exp_bigintop ($1.integer, $1.str); }
791 { $$ = exp_nameop(SIZEOF_HEADERS,0); }
793 | SIZEOF '(' NAME ')'
794 { $$ = exp_nameop(SIZEOF,$3); }
796 { $$ = exp_nameop(ADDR,$3); }
797 | LOADADDR '(' NAME ')'
798 { $$ = exp_nameop(LOADADDR,$3); }
799 | ABSOLUTE '(' exp ')'
800 { $$ = exp_unop(ABSOLUTE, $3); }
801 | ALIGN_K '(' exp ')'
802 { $$ = exp_unop(ALIGN_K,$3); }
803 | DATA_SEGMENT_ALIGN '(' exp ',' exp ')'
804 { $$ = exp_binop (DATA_SEGMENT_ALIGN, $3, $5); }
805 | DATA_SEGMENT_END '(' exp ')'
806 { $$ = exp_unop(DATA_SEGMENT_END, $3); }
808 { $$ = exp_unop(ALIGN_K,$3); }
810 { $$ = exp_nameop(NAME,$1); }
811 | MAX_K '(' exp ',' exp ')'
812 { $$ = exp_binop (MAX_K, $3, $5 ); }
813 | MIN_K '(' exp ',' exp ')'
814 { $$ = exp_binop (MIN_K, $3, $5 ); }
815 | ASSERT_K '(' exp ',' NAME ')'
816 { $$ = exp_assert ($3, $5); }
821 AT '>' NAME { $$ = $3; }
822 | { $$ = "*default*"; }
826 AT '(' exp ')' { $$ = $3; }
830 section: NAME { ldlex_expression(); }
832 opt_at { ldlex_popstate (); ldlex_script (); }
835 lang_enter_output_section_statement($1, $3,
840 '}' { ldlex_popstate (); ldlex_expression (); }
841 memspec_opt memspec_at_opt phdr_opt fill_opt
844 lang_leave_output_section_statement ($14, $11, $13, $12);
848 { ldlex_expression (); }
849 opt_exp_without_type opt_nocrossrefs opt_at
850 { ldlex_popstate (); ldlex_script (); }
853 lang_enter_overlay ($3, $5, (int) $4);
857 { ldlex_popstate (); ldlex_expression (); }
858 memspec_opt memspec_at_opt phdr_opt fill_opt
861 lang_leave_overlay ($15, $12, $14, $13);
864 | /* The GROUP case is just enough to support the gcc
865 svr3.ifile script. It is not intended to be full
866 support. I'm not even sure what GROUP is supposed
868 GROUP { ldlex_expression (); }
872 lang_add_assignment (exp_assop ('=', ".", $3));
874 '{' sec_or_group_p1 '}'
878 NOLOAD { sectype = noload_section; }
879 | DSECT { sectype = dsect_section; }
880 | COPY { sectype = copy_section; }
881 | INFO { sectype = info_section; }
882 | OVERLAY { sectype = overlay_section; }
887 | /* EMPTY */ { sectype = normal_section; }
888 | '(' ')' { sectype = normal_section; }
892 exp atype ':' { $$ = $1; }
893 | atype ':' { $$ = (etree_type *)NULL; }
894 | /* The BIND cases are to support the gcc svr3.ifile
895 script. They aren't intended to implement full
896 support for the BIND keyword. I'm not even sure
897 what BIND is supposed to mean. */
898 BIND '(' exp ')' atype ':' { $$ = $3; }
899 | BIND '(' exp ')' BLOCK '(' exp ')' atype ':'
903 opt_exp_without_type:
905 | ':' { $$ = (etree_type *) NULL; }
918 | { $$ = "*default*"; }
928 struct lang_output_section_phdr_list *n;
930 n = ((struct lang_output_section_phdr_list *)
931 xmalloc (sizeof *n));
945 lang_enter_overlay_section ($2);
947 '{' statement_list_opt '}'
948 { ldlex_popstate (); ldlex_expression (); }
952 lang_leave_overlay_section ($9, $8);
958 PHDRS '{' phdr_list '}'
967 NAME { ldlex_expression (); }
968 phdr_type phdr_qualifiers { ldlex_popstate (); }
971 lang_new_phdr ($1, $3, $4.filehdr, $4.phdrs, $4.at,
981 if ($1->type.node_class == etree_name
982 && $1->type.node_code == NAME)
986 static const char * const phdr_types[] =
988 "PT_NULL", "PT_LOAD", "PT_DYNAMIC",
989 "PT_INTERP", "PT_NOTE", "PT_SHLIB",
995 i < sizeof phdr_types / sizeof phdr_types[0];
997 if (strcmp (s, phdr_types[i]) == 0)
1009 memset (&$$, 0, sizeof (struct phdr_info));
1011 | NAME phdr_val phdr_qualifiers
1014 if (strcmp ($1, "FILEHDR") == 0 && $2 == NULL)
1016 else if (strcmp ($1, "PHDRS") == 0 && $2 == NULL)
1018 else if (strcmp ($1, "FLAGS") == 0 && $2 != NULL)
1021 einfo (_("%X%P:%S: PHDRS syntax error at `%s'\n"), $1);
1023 | AT '(' exp ')' phdr_qualifiers
1041 /* This syntax is used within an external version script file. */
1043 version_script_file:
1045 ldlex_version_file ();
1046 PUSH_ERROR (_("VERSION script"));
1055 /* This is used within a normal linker script file. */
1059 ldlex_version_script ();
1061 VERSIONK '{' vers_nodes '}'
1069 | vers_nodes vers_node
1073 '{' vers_tag '}' ';'
1075 lang_register_vers_node (NULL, $2, NULL);
1077 | VERS_TAG '{' vers_tag '}' ';'
1079 lang_register_vers_node ($1, $3, NULL);
1081 | VERS_TAG '{' vers_tag '}' verdep ';'
1083 lang_register_vers_node ($1, $3, $5);
1090 $$ = lang_add_vers_depend (NULL, $1);
1094 $$ = lang_add_vers_depend ($1, $2);
1101 $$ = lang_new_vers_node (NULL, NULL);
1105 $$ = lang_new_vers_node ($1, NULL);
1107 | GLOBAL ':' vers_defns ';'
1109 $$ = lang_new_vers_node ($3, NULL);
1111 | LOCAL ':' vers_defns ';'
1113 $$ = lang_new_vers_node (NULL, $3);
1115 | GLOBAL ':' vers_defns ';' LOCAL ':' vers_defns ';'
1117 $$ = lang_new_vers_node ($3, $7);
1124 $$ = lang_new_vers_pattern (NULL, $1, ldgram_vers_current_lang);
1126 | vers_defns ';' VERS_IDENTIFIER
1128 $$ = lang_new_vers_pattern ($1, $3, ldgram_vers_current_lang);
1132 $<name>$ = ldgram_vers_current_lang;
1133 ldgram_vers_current_lang = $2;
1138 ldgram_vers_current_lang = $<name>4;
1147 if (ldfile_assumed_script)
1148 einfo (_("%P:%s: file format not recognized; treating as linker script\n"),
1149 ldfile_input_filename);
1150 if (error_index > 0 && error_index < ERROR_NAME_MAX)
1151 einfo ("%P%F:%S: %s in %s\n", arg, error_names[error_index-1]);
1153 einfo ("%P%F:%S: %s\n", arg);