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 complete input section specification. */
64 struct Input_section_spec input_section_spec;
65 /* A list of wildcard specifications, with exclusions. */
66 struct Wildcard_sections wildcard_sections;
67 /* A single wildcard specification. */
68 struct Wildcard_section wildcard_section;
69 /* A list of strings. */
70 String_list_ptr string_list;
71 /* Used for version scripts and within VERSION {}. */
72 struct Version_dependency_list* deplist;
73 struct Version_expression_list* versyms;
74 struct Version_tree* versnode;
77 /* Operators, including a precedence table for expressions. */
79 %right PLUSEQ MINUSEQ MULTEQ DIVEQ '=' LSHIFTEQ RSHIFTEQ ANDEQ OREQ
92 /* A fake operator used to indicate unary operator precedence. */
97 %token <string> STRING
98 %token <string> QUOTED_STRING
99 %token <integer> INTEGER
101 /* Keywords. This list is taken from ldgram.y and ldlex.l in the old
102 GNU linker, with the keywords which only appear in MRI mode
103 removed. Not all these keywords are actually used in this grammar.
104 In most cases the keyword is recognized as the token name in upper
105 case. The comments indicate where this is not the case. */
109 %token ALIGN_K /* ALIGN */
111 %token ASSERT_K /* ASSERT */
119 %token CREATE_OBJECT_SYMBOLS
120 %token DATA_SEGMENT_ALIGN
121 %token DATA_SEGMENT_END
122 %token DATA_SEGMENT_RELRO_END
129 %token FORCE_COMMON_ALLOCATION
130 %token GLOBAL /* global */
134 %token INHIBIT_COMMON_ALLOCATION
137 %token LENGTH /* LENGTH, l, len */
139 %token LOCAL /* local */
142 %token MAX_K /* MAX */
144 %token MIN_K /* MIN */
150 %token ORIGIN /* ORIGIN, o, org */
157 %token PROVIDE_HIDDEN
164 %token SIZEOF_HEADERS /* SIZEOF_HEADERS, sizeof_headers */
165 %token SORT_BY_ALIGNMENT
172 %token TARGET_K /* TARGET */
174 %token VERSIONK /* VERSION */
176 /* Keywords, part 2. These are keywords that are unique to gold,
177 and not present in the old GNU linker. As before, unless the
178 comments say otherwise, the keyword is recognized as the token
179 name in upper case. */
183 /* Special tokens used to tell the grammar what type of tokens we are
184 parsing. The token stream always begins with one of these tokens.
185 We do this because version scripts can appear embedded within
186 linker scripts, and because --defsym uses the expression
188 %token PARSING_LINKER_SCRIPT
189 %token PARSING_VERSION_SCRIPT
190 %token PARSING_DEFSYM
192 /* Non-terminal types, where needed. */
194 %type <expr> parse_exp exp opt_address_and_section_type
195 %type <expr> opt_at opt_align opt_subalign opt_fill
196 %type <output_section_header> section_header
197 %type <output_section_trailer> section_trailer
198 %type <integer> data_length
199 %type <input_section_spec> input_section_no_keep
200 %type <wildcard_sections> wildcard_sections
201 %type <wildcard_section> wildcard_file wildcard_section
202 %type <string_list> exclude_names
203 %type <string> wildcard_name
204 %type <versyms> vers_defns
205 %type <versnode> vers_tag
206 %type <deplist> verdep
207 %type <string> string
211 /* Read the special token to see what to read next. */
213 PARSING_LINKER_SCRIPT linker_script
214 | PARSING_VERSION_SCRIPT version_script
215 | PARSING_DEFSYM defsym_expr
218 /* A file contains a list of commands. */
220 linker_script file_cmd
224 /* A command which may appear at top level of a linker script. */
227 { script_start_group(closure); }
229 { script_end_group(closure); }
230 | OPTION '(' string ')'
231 { script_parse_option(closure, $3.value, $3.length); }
233 { script_start_sections(closure); }
235 { script_finish_sections(closure); }
237 { script_push_lex_into_version_mode(closure); }
239 { script_pop_lex_mode(closure); }
240 | file_or_sections_cmd
244 /* Top level commands which we ignore. The GNU linker uses these to
245 select the output format, but we don't offer a choice. Ignoring
246 these is more-or-less OK since most scripts simply explicitly
247 choose the default. */
249 OUTPUT_FORMAT '(' string ')'
250 | OUTPUT_FORMAT '(' string ',' string ',' string ')'
251 | OUTPUT_ARCH '(' string ')'
254 /* A list of input file names. */
257 | input_list opt_comma input_list_element
260 /* An input file name. */
263 { script_add_file(closure, $1.value, $1.length); }
265 { script_start_as_needed(closure); }
267 { script_end_as_needed(closure); }
270 /* Commands in a SECTIONS block. */
272 sections_block section_block_cmd
276 /* A command which may appear within a SECTIONS block. */
279 | string section_header
280 { script_start_output_section(closure, $1.value, $1.length, &$2); }
281 '{' section_cmds '}' section_trailer
282 { script_finish_output_section(closure, &$7); }
285 /* The header of an output section in a SECTIONS block--everything
288 { script_push_lex_into_expression_mode(closure); }
289 opt_address_and_section_type opt_at opt_align opt_subalign
292 $$.load_address = $3;
295 script_pop_lex_mode(closure);
299 /* The optional address followed by the optional section type. This
300 is a separate nonterminal to avoid a shift/reduce conflict on
301 '(' in section_header. */
303 opt_address_and_section_type:
312 | exp '(' string ')' ':'
314 yyerror(closure, "section types are not supported");
319 /* The address at which an output section should be loaded. */
327 /* The alignment of an output section. */
331 | ALIGN_K '(' exp ')'
335 /* The input section alignment within an output section. */
339 | SUBALIGN '(' exp ')'
343 /* The trailer of an output section in a SECTIONS block. */
345 { script_push_lex_into_expression_mode(closure); }
346 opt_memspec opt_at_memspec opt_phdr opt_fill opt_comma
349 script_pop_lex_mode(closure);
353 /* A memory specification for an output section. */
356 { yyerror(closure, "memory regions are not supported"); }
360 /* A memory specification for where to load an output section. */
363 { yyerror(closure, "memory regions are not supported"); }
367 /* The program segment an output section should go into. */
370 { yyerror(closure, "program headers are not supported"); }
374 /* The value to use to fill an output section. FIXME: This does not
375 handle a string of arbitrary length. */
383 /* Commands which may appear within the description of an output
384 section in a SECTIONS block. */
387 | section_cmds section_cmd
390 /* A command which may appear within the description of an output
391 section in a SECTIONS block. */
395 | data_length '(' parse_exp ')'
396 { script_add_data(closure, $1, $3); }
397 | ASSERT_K '(' parse_exp ',' string ')'
398 { script_add_assertion(closure, $3, $5.value, $5.length); }
399 | FILL '(' parse_exp ')'
400 { script_add_fill(closure, $3); }
403 /* The GNU linker uses CONSTRUCTORS for the a.out object
404 file format. It does nothing when using ELF. Since
405 some ELF linker scripts use it although it does
406 nothing, we accept it and ignore it. */
411 /* The length of data which may appear within the description of an
412 output section in a SECTIONS block. */
426 /* An input section specification. This may appear within the
427 description of an output section in a SECTIONS block. */
429 input_section_no_keep
430 { script_add_input_section(closure, &$1, 0); }
431 | KEEP '(' input_section_no_keep ')'
432 { script_add_input_section(closure, &$3, 1); }
435 /* An input section specification within a KEEP clause. */
436 input_section_no_keep:
440 $$.file.sort = SORT_WILDCARD_NONE;
441 $$.input_sections.sections = NULL;
442 $$.input_sections.exclude = NULL;
444 | wildcard_file '(' wildcard_sections ')'
447 $$.input_sections = $3;
451 /* A wildcard file specification. */
456 $$.sort = SORT_WILDCARD_NONE;
458 | SORT_BY_NAME '(' wildcard_name ')'
461 $$.sort = SORT_WILDCARD_BY_NAME;
465 /* A list of wild card section specifications. */
467 wildcard_sections opt_comma wildcard_section
469 $$.sections = script_string_sort_list_add($1.sections, &$3);
470 $$.exclude = $1.exclude;
474 $$.sections = script_new_string_sort_list(&$1);
477 | wildcard_sections opt_comma EXCLUDE_FILE '(' exclude_names ')'
479 $$.sections = $1.sections;
480 $$.exclude = script_string_list_append($1.exclude, $5);
482 | EXCLUDE_FILE '(' exclude_names ')'
489 /* A single wild card specification. */
494 $$.sort = SORT_WILDCARD_NONE;
496 | SORT_BY_NAME '(' wildcard_section ')'
501 case SORT_WILDCARD_NONE:
502 $$.sort = SORT_WILDCARD_BY_NAME;
504 case SORT_WILDCARD_BY_NAME:
505 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
507 case SORT_WILDCARD_BY_ALIGNMENT:
508 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
509 $$.sort = SORT_WILDCARD_BY_NAME_BY_ALIGNMENT;
515 | SORT_BY_ALIGNMENT '(' wildcard_section ')'
520 case SORT_WILDCARD_NONE:
521 $$.sort = SORT_WILDCARD_BY_ALIGNMENT;
523 case SORT_WILDCARD_BY_ALIGNMENT:
524 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
526 case SORT_WILDCARD_BY_NAME:
527 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
528 $$.sort = SORT_WILDCARD_BY_ALIGNMENT_BY_NAME;
536 /* A list of file names to exclude. */
538 exclude_names opt_comma wildcard_name
539 { $$ = script_string_list_push_back($1, $3.value, $3.length); }
541 { $$ = script_new_string_list($1.value, $1.length); }
544 /* A single wildcard name. We recognize '*' and '?' specially since
545 they are expression tokens. */
561 /* A command which may appear at the top level of a linker script, or
562 within a SECTIONS block. */
563 file_or_sections_cmd:
565 { script_set_entry(closure, $3.value, $3.length); }
567 | ASSERT_K '(' parse_exp ',' string ')'
568 { script_add_assertion(closure, $3, $5.value, $5.length); }
571 /* Set a symbol to a value. */
574 { script_set_symbol(closure, $1.value, $1.length, $3, 0, 0); }
575 | string PLUSEQ parse_exp
577 Expression_ptr s = script_exp_string($1.value, $1.length);
578 Expression_ptr e = script_exp_binary_add(s, $3);
579 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
581 | string MINUSEQ parse_exp
583 Expression_ptr s = script_exp_string($1.value, $1.length);
584 Expression_ptr e = script_exp_binary_sub(s, $3);
585 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
587 | string MULTEQ parse_exp
589 Expression_ptr s = script_exp_string($1.value, $1.length);
590 Expression_ptr e = script_exp_binary_mult(s, $3);
591 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
593 | string DIVEQ parse_exp
595 Expression_ptr s = script_exp_string($1.value, $1.length);
596 Expression_ptr e = script_exp_binary_div(s, $3);
597 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
599 | string LSHIFTEQ parse_exp
601 Expression_ptr s = script_exp_string($1.value, $1.length);
602 Expression_ptr e = script_exp_binary_lshift(s, $3);
603 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
605 | string RSHIFTEQ parse_exp
607 Expression_ptr s = script_exp_string($1.value, $1.length);
608 Expression_ptr e = script_exp_binary_rshift(s, $3);
609 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
611 | string ANDEQ parse_exp
613 Expression_ptr s = script_exp_string($1.value, $1.length);
614 Expression_ptr e = script_exp_binary_bitwise_and(s, $3);
615 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
617 | string OREQ parse_exp
619 Expression_ptr s = script_exp_string($1.value, $1.length);
620 Expression_ptr e = script_exp_binary_bitwise_or(s, $3);
621 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
623 | PROVIDE '(' string '=' parse_exp ')'
624 { script_set_symbol(closure, $3.value, $3.length, $5, 1, 0); }
625 | PROVIDE_HIDDEN '(' string '=' parse_exp ')'
626 { script_set_symbol(closure, $3.value, $3.length, $5, 1, 1); }
629 /* Parse an expression, putting the lexer into the right mode. */
631 { script_push_lex_into_expression_mode(closure); }
634 script_pop_lex_mode(closure);
643 | '-' exp %prec UNARY
644 { $$ = script_exp_unary_minus($2); }
645 | '!' exp %prec UNARY
646 { $$ = script_exp_unary_logical_not($2); }
647 | '~' exp %prec UNARY
648 { $$ = script_exp_unary_bitwise_not($2); }
649 | '+' exp %prec UNARY
652 { $$ = script_exp_binary_mult($1, $3); }
654 { $$ = script_exp_binary_div($1, $3); }
656 { $$ = script_exp_binary_mod($1, $3); }
658 { $$ = script_exp_binary_add($1, $3); }
660 { $$ = script_exp_binary_sub($1, $3); }
662 { $$ = script_exp_binary_lshift($1, $3); }
664 { $$ = script_exp_binary_rshift($1, $3); }
666 { $$ = script_exp_binary_eq($1, $3); }
668 { $$ = script_exp_binary_ne($1, $3); }
670 { $$ = script_exp_binary_le($1, $3); }
672 { $$ = script_exp_binary_ge($1, $3); }
674 { $$ = script_exp_binary_lt($1, $3); }
676 { $$ = script_exp_binary_gt($1, $3); }
678 { $$ = script_exp_binary_bitwise_and($1, $3); }
680 { $$ = script_exp_binary_bitwise_xor($1, $3); }
682 { $$ = script_exp_binary_bitwise_or($1, $3); }
684 { $$ = script_exp_binary_logical_and($1, $3); }
686 { $$ = script_exp_binary_logical_or($1, $3); }
687 | exp '?' exp ':' exp
688 { $$ = script_exp_trinary_cond($1, $3, $5); }
690 { $$ = script_exp_integer($1); }
692 { $$ = script_exp_string($1.value, $1.length); }
693 | MAX_K '(' exp ',' exp ')'
694 { $$ = script_exp_function_max($3, $5); }
695 | MIN_K '(' exp ',' exp ')'
696 { $$ = script_exp_function_min($3, $5); }
697 | DEFINED '(' string ')'
698 { $$ = script_exp_function_defined($3.value, $3.length); }
700 { $$ = script_exp_function_sizeof_headers(); }
701 | ALIGNOF '(' string ')'
702 { $$ = script_exp_function_alignof($3.value, $3.length); }
703 | SIZEOF '(' string ')'
704 { $$ = script_exp_function_sizeof($3.value, $3.length); }
705 | ADDR '(' string ')'
706 { $$ = script_exp_function_addr($3.value, $3.length); }
707 | LOADADDR '(' string ')'
708 { $$ = script_exp_function_loadaddr($3.value, $3.length); }
709 | ORIGIN '(' string ')'
710 { $$ = script_exp_function_origin($3.value, $3.length); }
711 | LENGTH '(' string ')'
712 { $$ = script_exp_function_length($3.value, $3.length); }
713 | CONSTANT '(' string ')'
714 { $$ = script_exp_function_constant($3.value, $3.length); }
715 | ABSOLUTE '(' exp ')'
716 { $$ = script_exp_function_absolute($3); }
717 | ALIGN_K '(' exp ')'
718 { $$ = script_exp_function_align(script_exp_string(".", 1), $3); }
719 | ALIGN_K '(' exp ',' exp ')'
720 { $$ = script_exp_function_align($3, $5); }
722 { $$ = script_exp_function_align(script_exp_string(".", 1), $3); }
723 | DATA_SEGMENT_ALIGN '(' exp ',' exp ')'
724 { $$ = script_exp_function_data_segment_align($3, $5); }
725 | DATA_SEGMENT_RELRO_END '(' exp ',' exp ')'
726 { $$ = script_exp_function_data_segment_relro_end($3, $5); }
727 | DATA_SEGMENT_END '(' exp ')'
728 { $$ = script_exp_function_data_segment_end($3); }
729 | SEGMENT_START '(' string ',' exp ')'
731 $$ = script_exp_function_segment_start($3.value, $3.length, $5);
733 | ASSERT_K '(' exp ',' string ')'
734 { $$ = script_exp_function_assert($3, $5.value, $5.length); }
737 /* Handle the --defsym option. */
740 { script_set_symbol(closure, $1.value, $1.length, $3, 0, 0); }
743 /* A version script. */
750 | vers_nodes vers_node
756 script_register_vers_node (closure, NULL, 0, $2, NULL);
758 | string '{' vers_tag '}' ';'
760 script_register_vers_node (closure, $1.value, $1.length, $3,
763 | string '{' vers_tag '}' verdep ';'
765 script_register_vers_node (closure, $1.value, $1.length, $3, $5);
772 $$ = script_add_vers_depend (closure, NULL, $1.value, $1.length);
776 $$ = script_add_vers_depend (closure, $1, $2.value, $2.length);
782 { $$ = script_new_vers_node (closure, NULL, NULL); }
784 { $$ = script_new_vers_node (closure, $1, NULL); }
785 | GLOBAL ':' vers_defns ';'
786 { $$ = script_new_vers_node (closure, $3, NULL); }
787 | LOCAL ':' vers_defns ';'
788 { $$ = script_new_vers_node (closure, NULL, $3); }
789 | GLOBAL ':' vers_defns ';' LOCAL ':' vers_defns ';'
790 { $$ = script_new_vers_node (closure, $3, $7); }
793 /* Here is one of the rare places we care about the distinction
794 between STRING and QUOTED_STRING. For QUOTED_STRING, we do exact
795 matching on the pattern, so we pass in true for the exact_match
796 parameter. For STRING, we do glob matching and pass in false. */
800 $$ = script_new_vers_pattern (closure, NULL, $1.value,
805 $$ = script_new_vers_pattern (closure, NULL, $1.value,
808 | vers_defns ';' STRING
810 $$ = script_new_vers_pattern (closure, $1, $3.value,
813 | vers_defns ';' QUOTED_STRING
815 $$ = script_new_vers_pattern (closure, $1, $3.value,
818 | /* Push string on the language stack. */
820 { version_script_push_lang (closure, $2.value, $2.length); }
821 vers_defns opt_semicolon '}'
824 version_script_pop_lang(closure);
826 | /* Push string on the language stack. This is more complicated
827 than the other cases because we need to merge the linked-list
828 state from the pre-EXTERN defns and the post-EXTERN defns. */
829 vers_defns ';' EXTERN string '{'
830 { version_script_push_lang (closure, $4.value, $4.length); }
831 vers_defns opt_semicolon '}'
833 $$ = script_merge_expressions ($1, $7);
834 version_script_pop_lang(closure);
836 | EXTERN // "extern" as a symbol name
838 $$ = script_new_vers_pattern (closure, NULL, "extern",
839 sizeof("extern") - 1, 1);
841 | vers_defns ';' EXTERN
843 $$ = script_new_vers_pattern (closure, $1, "extern",
844 sizeof("extern") - 1, 1);
848 /* A string can be either a STRING or a QUOTED_STRING. Almost all the
849 time we don't care, and we use this rule. */
857 /* Some statements require a terminator, which may be a semicolon or a
864 /* An optional semicolon. */
870 /* An optional comma. */