1 /* yyscript.y -- linker script grammer for gold. */
3 /* Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <iant@google.com>.
6 This file is part of gold.
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 3 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., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 /* This is a bison grammar to parse a subset of the original GNU ld
24 linker script language. */
38 /* We need to use a pure parser because we might be multi-threaded.
39 We pass some arguments through the parser to the lexer. */
43 %parse-param {void* closure}
44 %lex-param {void* closure}
46 /* Since we require bison anyhow, we take advantage of it. */
50 /* The values associated with tokens. */
54 struct Parser_string string;
59 /* An output section header. */
60 struct Parser_output_section_header output_section_header;
61 /* An output section trailer. */
62 struct Parser_output_section_trailer output_section_trailer;
63 /* A section constraint. */
64 enum Section_constraint constraint;
65 /* A complete input section specification. */
66 struct Input_section_spec input_section_spec;
67 /* A list of wildcard specifications, with exclusions. */
68 struct Wildcard_sections wildcard_sections;
69 /* A single wildcard specification. */
70 struct Wildcard_section wildcard_section;
71 /* A list of strings. */
72 String_list_ptr string_list;
73 /* Used for version scripts and within VERSION {}. */
74 struct Version_dependency_list* deplist;
75 struct Version_expression_list* versyms;
76 struct Version_tree* versnode;
79 /* Operators, including a precedence table for expressions. */
81 %right PLUSEQ MINUSEQ MULTEQ DIVEQ '=' LSHIFTEQ RSHIFTEQ ANDEQ OREQ
94 /* A fake operator used to indicate unary operator precedence. */
99 %token <string> STRING
100 %token <string> QUOTED_STRING
101 %token <integer> INTEGER
103 /* Keywords. This list is taken from ldgram.y and ldlex.l in the old
104 GNU linker, with the keywords which only appear in MRI mode
105 removed. Not all these keywords are actually used in this grammar.
106 In most cases the keyword is recognized as the token name in upper
107 case. The comments indicate where this is not the case. */
111 %token ALIGN_K /* ALIGN */
113 %token ASSERT_K /* ASSERT */
121 %token CREATE_OBJECT_SYMBOLS
122 %token DATA_SEGMENT_ALIGN
123 %token DATA_SEGMENT_END
124 %token DATA_SEGMENT_RELRO_END
131 %token FORCE_COMMON_ALLOCATION
132 %token GLOBAL /* global */
136 %token INHIBIT_COMMON_ALLOCATION
139 %token LENGTH /* LENGTH, l, len */
141 %token LOCAL /* local */
144 %token MAX_K /* MAX */
146 %token MIN_K /* MIN */
152 %token ORIGIN /* ORIGIN, o, org */
159 %token PROVIDE_HIDDEN
166 %token SIZEOF_HEADERS /* SIZEOF_HEADERS, sizeof_headers */
167 %token SORT_BY_ALIGNMENT
174 %token TARGET_K /* TARGET */
176 %token VERSIONK /* VERSION */
178 /* Keywords, part 2. These are keywords that are unique to gold,
179 and not present in the old GNU linker. As before, unless the
180 comments say otherwise, the keyword is recognized as the token
181 name in upper case. */
185 /* Special tokens used to tell the grammar what type of tokens we are
186 parsing. The token stream always begins with one of these tokens.
187 We do this because version scripts can appear embedded within
188 linker scripts, and because --defsym uses the expression
190 %token PARSING_LINKER_SCRIPT
191 %token PARSING_VERSION_SCRIPT
192 %token PARSING_DEFSYM
194 /* Non-terminal types, where needed. */
196 %type <expr> parse_exp exp opt_address_and_section_type
197 %type <expr> opt_at opt_align opt_subalign opt_fill
198 %type <output_section_header> section_header
199 %type <output_section_trailer> section_trailer
200 %type <constraint> opt_constraint
201 %type <integer> data_length
202 %type <input_section_spec> input_section_no_keep
203 %type <wildcard_sections> wildcard_sections
204 %type <wildcard_section> wildcard_file wildcard_section
205 %type <string_list> exclude_names
206 %type <string> wildcard_name
207 %type <versyms> vers_defns
208 %type <versnode> vers_tag
209 %type <deplist> verdep
210 %type <string> string
214 /* Read the special token to see what to read next. */
216 PARSING_LINKER_SCRIPT linker_script
217 | PARSING_VERSION_SCRIPT version_script
218 | PARSING_DEFSYM defsym_expr
221 /* A file contains a list of commands. */
223 linker_script file_cmd
227 /* A command which may appear at top level of a linker script. */
230 { script_start_group(closure); }
232 { script_end_group(closure); }
233 | OPTION '(' string ')'
234 { script_parse_option(closure, $3.value, $3.length); }
235 | SEARCH_DIR '(' string ')'
236 { script_add_search_dir(closure, $3.value, $3.length); }
238 { script_start_sections(closure); }
240 { script_finish_sections(closure); }
242 { script_push_lex_into_version_mode(closure); }
244 { script_pop_lex_mode(closure); }
245 | file_or_sections_cmd
250 /* Top level commands which we ignore. The GNU linker uses these to
251 select the output format, but we don't offer a choice. Ignoring
252 these is more-or-less OK since most scripts simply explicitly
253 choose the default. */
255 OUTPUT_FORMAT '(' string ')'
256 | OUTPUT_FORMAT '(' string ',' string ',' string ')'
257 | OUTPUT_ARCH '(' string ')'
260 /* A list of input file names. */
263 | input_list opt_comma input_list_element
266 /* An input file name. */
269 { script_add_file(closure, $1.value, $1.length); }
271 { script_start_as_needed(closure); }
273 { script_end_as_needed(closure); }
276 /* Commands in a SECTIONS block. */
278 sections_block section_block_cmd
282 /* A command which may appear within a SECTIONS block. */
285 | string section_header
286 { script_start_output_section(closure, $1.value, $1.length, &$2); }
287 '{' section_cmds '}' section_trailer
288 { script_finish_output_section(closure, &$7); }
291 /* The header of an output section in a SECTIONS block--everything
294 { script_push_lex_into_expression_mode(closure); }
295 opt_address_and_section_type opt_at opt_align opt_subalign
296 { script_pop_lex_mode(closure); }
300 $$.load_address = $3;
307 /* The optional address followed by the optional section type. This
308 is a separate nonterminal to avoid a shift/reduce conflict on
309 '(' in section_header. */
311 opt_address_and_section_type:
320 | exp '(' string ')' ':'
322 yyerror(closure, "section types are not supported");
327 /* The address at which an output section should be loaded. */
335 /* The alignment of an output section. */
339 | ALIGN_K '(' exp ')'
343 /* The input section alignment within an output section. */
347 | SUBALIGN '(' exp ')'
351 /* A section constraint. */
354 { $$ = CONSTRAINT_NONE; }
356 { $$ = CONSTRAINT_ONLY_IF_RO; }
358 { $$ = CONSTRAINT_ONLY_IF_RW; }
360 { $$ = CONSTRAINT_SPECIAL; }
363 /* The trailer of an output section in a SECTIONS block. */
365 opt_memspec opt_at_memspec opt_phdr opt_fill opt_comma
371 /* A memory specification for an output section. */
374 { yyerror(closure, "memory regions are not supported"); }
378 /* A memory specification for where to load an output section. */
381 { yyerror(closure, "memory regions are not supported"); }
385 /* The program segment an output section should go into. */
388 { yyerror(closure, "program headers are not supported"); }
392 /* The value to use to fill an output section. FIXME: This does not
393 handle a string of arbitrary length. */
401 /* Commands which may appear within the description of an output
402 section in a SECTIONS block. */
405 | section_cmds section_cmd
408 /* A command which may appear within the description of an output
409 section in a SECTIONS block. */
413 | data_length '(' parse_exp ')'
414 { script_add_data(closure, $1, $3); }
415 | ASSERT_K '(' parse_exp ',' string ')'
416 { script_add_assertion(closure, $3, $5.value, $5.length); }
417 | FILL '(' parse_exp ')'
418 { script_add_fill(closure, $3); }
421 /* The GNU linker uses CONSTRUCTORS for the a.out object
422 file format. It does nothing when using ELF. Since
423 some ELF linker scripts use it although it does
424 nothing, we accept it and ignore it. */
426 | SORT_BY_NAME '(' CONSTRUCTORS ')'
430 /* The length of data which may appear within the description of an
431 output section in a SECTIONS block. */
445 /* An input section specification. This may appear within the
446 description of an output section in a SECTIONS block. */
448 input_section_no_keep
449 { script_add_input_section(closure, &$1, 0); }
450 | KEEP '(' input_section_no_keep ')'
451 { script_add_input_section(closure, &$3, 1); }
454 /* An input section specification within a KEEP clause. */
455 input_section_no_keep:
459 $$.file.sort = SORT_WILDCARD_NONE;
460 $$.input_sections.sections = NULL;
461 $$.input_sections.exclude = NULL;
463 | wildcard_file '(' wildcard_sections ')'
466 $$.input_sections = $3;
470 /* A wildcard file specification. */
475 $$.sort = SORT_WILDCARD_NONE;
477 | SORT_BY_NAME '(' wildcard_name ')'
480 $$.sort = SORT_WILDCARD_BY_NAME;
484 /* A list of wild card section specifications. */
486 wildcard_sections opt_comma wildcard_section
488 $$.sections = script_string_sort_list_add($1.sections, &$3);
489 $$.exclude = $1.exclude;
493 $$.sections = script_new_string_sort_list(&$1);
496 | wildcard_sections opt_comma EXCLUDE_FILE '(' exclude_names ')'
498 $$.sections = $1.sections;
499 $$.exclude = script_string_list_append($1.exclude, $5);
501 | EXCLUDE_FILE '(' exclude_names ')'
508 /* A single wild card specification. */
513 $$.sort = SORT_WILDCARD_NONE;
515 | SORT_BY_NAME '(' wildcard_section ')'
520 case SORT_WILDCARD_NONE:
521 $$.sort = SORT_WILDCARD_BY_NAME;
523 case SORT_WILDCARD_BY_NAME:
524 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
526 case SORT_WILDCARD_BY_ALIGNMENT:
527 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
528 $$.sort = SORT_WILDCARD_BY_NAME_BY_ALIGNMENT;
534 | SORT_BY_ALIGNMENT '(' wildcard_section ')'
539 case SORT_WILDCARD_NONE:
540 $$.sort = SORT_WILDCARD_BY_ALIGNMENT;
542 case SORT_WILDCARD_BY_ALIGNMENT:
543 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
545 case SORT_WILDCARD_BY_NAME:
546 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
547 $$.sort = SORT_WILDCARD_BY_ALIGNMENT_BY_NAME;
555 /* A list of file names to exclude. */
557 exclude_names opt_comma wildcard_name
558 { $$ = script_string_list_push_back($1, $3.value, $3.length); }
560 { $$ = script_new_string_list($1.value, $1.length); }
563 /* A single wildcard name. We recognize '*' and '?' specially since
564 they are expression tokens. */
580 /* A command which may appear at the top level of a linker script, or
581 within a SECTIONS block. */
582 file_or_sections_cmd:
584 { script_set_entry(closure, $3.value, $3.length); }
586 | ASSERT_K '(' parse_exp ',' string ')'
587 { script_add_assertion(closure, $3, $5.value, $5.length); }
590 /* Set a symbol to a value. */
593 { script_set_symbol(closure, $1.value, $1.length, $3, 0, 0); }
594 | string PLUSEQ parse_exp
596 Expression_ptr s = script_exp_string($1.value, $1.length);
597 Expression_ptr e = script_exp_binary_add(s, $3);
598 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
600 | string MINUSEQ parse_exp
602 Expression_ptr s = script_exp_string($1.value, $1.length);
603 Expression_ptr e = script_exp_binary_sub(s, $3);
604 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
606 | string MULTEQ parse_exp
608 Expression_ptr s = script_exp_string($1.value, $1.length);
609 Expression_ptr e = script_exp_binary_mult(s, $3);
610 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
612 | string DIVEQ parse_exp
614 Expression_ptr s = script_exp_string($1.value, $1.length);
615 Expression_ptr e = script_exp_binary_div(s, $3);
616 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
618 | string LSHIFTEQ parse_exp
620 Expression_ptr s = script_exp_string($1.value, $1.length);
621 Expression_ptr e = script_exp_binary_lshift(s, $3);
622 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
624 | string RSHIFTEQ parse_exp
626 Expression_ptr s = script_exp_string($1.value, $1.length);
627 Expression_ptr e = script_exp_binary_rshift(s, $3);
628 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
630 | string ANDEQ parse_exp
632 Expression_ptr s = script_exp_string($1.value, $1.length);
633 Expression_ptr e = script_exp_binary_bitwise_and(s, $3);
634 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
636 | string OREQ parse_exp
638 Expression_ptr s = script_exp_string($1.value, $1.length);
639 Expression_ptr e = script_exp_binary_bitwise_or(s, $3);
640 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
642 | PROVIDE '(' string '=' parse_exp ')'
643 { script_set_symbol(closure, $3.value, $3.length, $5, 1, 0); }
644 | PROVIDE_HIDDEN '(' string '=' parse_exp ')'
645 { script_set_symbol(closure, $3.value, $3.length, $5, 1, 1); }
648 /* Parse an expression, putting the lexer into the right mode. */
650 { script_push_lex_into_expression_mode(closure); }
653 script_pop_lex_mode(closure);
662 | '-' exp %prec UNARY
663 { $$ = script_exp_unary_minus($2); }
664 | '!' exp %prec UNARY
665 { $$ = script_exp_unary_logical_not($2); }
666 | '~' exp %prec UNARY
667 { $$ = script_exp_unary_bitwise_not($2); }
668 | '+' exp %prec UNARY
671 { $$ = script_exp_binary_mult($1, $3); }
673 { $$ = script_exp_binary_div($1, $3); }
675 { $$ = script_exp_binary_mod($1, $3); }
677 { $$ = script_exp_binary_add($1, $3); }
679 { $$ = script_exp_binary_sub($1, $3); }
681 { $$ = script_exp_binary_lshift($1, $3); }
683 { $$ = script_exp_binary_rshift($1, $3); }
685 { $$ = script_exp_binary_eq($1, $3); }
687 { $$ = script_exp_binary_ne($1, $3); }
689 { $$ = script_exp_binary_le($1, $3); }
691 { $$ = script_exp_binary_ge($1, $3); }
693 { $$ = script_exp_binary_lt($1, $3); }
695 { $$ = script_exp_binary_gt($1, $3); }
697 { $$ = script_exp_binary_bitwise_and($1, $3); }
699 { $$ = script_exp_binary_bitwise_xor($1, $3); }
701 { $$ = script_exp_binary_bitwise_or($1, $3); }
703 { $$ = script_exp_binary_logical_and($1, $3); }
705 { $$ = script_exp_binary_logical_or($1, $3); }
706 | exp '?' exp ':' exp
707 { $$ = script_exp_trinary_cond($1, $3, $5); }
709 { $$ = script_exp_integer($1); }
711 { $$ = script_exp_string($1.value, $1.length); }
712 | MAX_K '(' exp ',' exp ')'
713 { $$ = script_exp_function_max($3, $5); }
714 | MIN_K '(' exp ',' exp ')'
715 { $$ = script_exp_function_min($3, $5); }
716 | DEFINED '(' string ')'
717 { $$ = script_exp_function_defined($3.value, $3.length); }
719 { $$ = script_exp_function_sizeof_headers(); }
720 | ALIGNOF '(' string ')'
721 { $$ = script_exp_function_alignof($3.value, $3.length); }
722 | SIZEOF '(' string ')'
723 { $$ = script_exp_function_sizeof($3.value, $3.length); }
724 | ADDR '(' string ')'
725 { $$ = script_exp_function_addr($3.value, $3.length); }
726 | LOADADDR '(' string ')'
727 { $$ = script_exp_function_loadaddr($3.value, $3.length); }
728 | ORIGIN '(' string ')'
729 { $$ = script_exp_function_origin($3.value, $3.length); }
730 | LENGTH '(' string ')'
731 { $$ = script_exp_function_length($3.value, $3.length); }
732 | CONSTANT '(' string ')'
733 { $$ = script_exp_function_constant($3.value, $3.length); }
734 | ABSOLUTE '(' exp ')'
735 { $$ = script_exp_function_absolute($3); }
736 | ALIGN_K '(' exp ')'
737 { $$ = script_exp_function_align(script_exp_string(".", 1), $3); }
738 | ALIGN_K '(' exp ',' exp ')'
739 { $$ = script_exp_function_align($3, $5); }
741 { $$ = script_exp_function_align(script_exp_string(".", 1), $3); }
742 | DATA_SEGMENT_ALIGN '(' exp ',' exp ')'
743 { $$ = script_exp_function_data_segment_align($3, $5); }
744 | DATA_SEGMENT_RELRO_END '(' exp ',' exp ')'
745 { $$ = script_exp_function_data_segment_relro_end($3, $5); }
746 | DATA_SEGMENT_END '(' exp ')'
747 { $$ = script_exp_function_data_segment_end($3); }
748 | SEGMENT_START '(' string ',' exp ')'
750 $$ = script_exp_function_segment_start($3.value, $3.length, $5);
752 | ASSERT_K '(' exp ',' string ')'
753 { $$ = script_exp_function_assert($3, $5.value, $5.length); }
756 /* Handle the --defsym option. */
759 { script_set_symbol(closure, $1.value, $1.length, $3, 0, 0); }
762 /* A version script. */
769 | vers_nodes vers_node
775 script_register_vers_node (closure, NULL, 0, $2, NULL);
777 | string '{' vers_tag '}' ';'
779 script_register_vers_node (closure, $1.value, $1.length, $3,
782 | string '{' vers_tag '}' verdep ';'
784 script_register_vers_node (closure, $1.value, $1.length, $3, $5);
791 $$ = script_add_vers_depend (closure, NULL, $1.value, $1.length);
795 $$ = script_add_vers_depend (closure, $1, $2.value, $2.length);
801 { $$ = script_new_vers_node (closure, NULL, NULL); }
803 { $$ = script_new_vers_node (closure, $1, NULL); }
804 | GLOBAL ':' vers_defns ';'
805 { $$ = script_new_vers_node (closure, $3, NULL); }
806 | LOCAL ':' vers_defns ';'
807 { $$ = script_new_vers_node (closure, NULL, $3); }
808 | GLOBAL ':' vers_defns ';' LOCAL ':' vers_defns ';'
809 { $$ = script_new_vers_node (closure, $3, $7); }
812 /* Here is one of the rare places we care about the distinction
813 between STRING and QUOTED_STRING. For QUOTED_STRING, we do exact
814 matching on the pattern, so we pass in true for the exact_match
815 parameter. For STRING, we do glob matching and pass in false. */
819 $$ = script_new_vers_pattern (closure, NULL, $1.value,
824 $$ = script_new_vers_pattern (closure, NULL, $1.value,
827 | vers_defns ';' STRING
829 $$ = script_new_vers_pattern (closure, $1, $3.value,
832 | vers_defns ';' QUOTED_STRING
834 $$ = script_new_vers_pattern (closure, $1, $3.value,
837 | /* Push string on the language stack. */
839 { version_script_push_lang (closure, $2.value, $2.length); }
840 vers_defns opt_semicolon '}'
843 version_script_pop_lang(closure);
845 | /* Push string on the language stack. This is more complicated
846 than the other cases because we need to merge the linked-list
847 state from the pre-EXTERN defns and the post-EXTERN defns. */
848 vers_defns ';' EXTERN string '{'
849 { version_script_push_lang (closure, $4.value, $4.length); }
850 vers_defns opt_semicolon '}'
852 $$ = script_merge_expressions ($1, $7);
853 version_script_pop_lang(closure);
855 | EXTERN // "extern" as a symbol name
857 $$ = script_new_vers_pattern (closure, NULL, "extern",
858 sizeof("extern") - 1, 1);
860 | vers_defns ';' EXTERN
862 $$ = script_new_vers_pattern (closure, $1, "extern",
863 sizeof("extern") - 1, 1);
867 /* A string can be either a STRING or a QUOTED_STRING. Almost all the
868 time we don't care, and we use this rule. */
876 /* Some statements require a terminator, which may be a semicolon or a
883 /* An optional semicolon. */
889 /* An optional comma. */