1 /* yyscript.y -- linker script grammar 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. */
39 /* We need to use a pure parser because we might be multi-threaded.
40 We pass some arguments through the parser to the lexer. */
44 %parse
-param
{void* closure
}
45 %lex
-param
{void* closure
}
47 /* Since we require bison anyhow, we take advantage of it. */
51 /* The values associated with tokens. */
55 struct Parser_string
string;
60 /* An output section header. */
61 struct Parser_output_section_header output_section_header
;
62 /* An output section trailer. */
63 struct Parser_output_section_trailer output_section_trailer
;
64 /* A section constraint. */
65 enum Section_constraint constraint
;
66 /* A complete input section specification. */
67 struct Input_section_spec input_section_spec
;
68 /* A list of wildcard specifications, with exclusions. */
69 struct Wildcard_sections wildcard_sections
;
70 /* A single wildcard specification. */
71 struct Wildcard_section wildcard_section
;
72 /* A list of strings. */
73 String_list_ptr string_list
;
74 /* Information for a program header. */
75 struct Phdr_info phdr_info
;
76 /* Used for version scripts and within VERSION {}. */
77 struct Version_dependency_list
* deplist
;
78 struct Version_expression_list
* versyms
;
79 struct Version_tree
* versnode
;
82 /* Operators, including a precedence table for expressions. */
84 %right PLUSEQ MINUSEQ MULTEQ DIVEQ
'=' LSHIFTEQ RSHIFTEQ ANDEQ OREQ
97 /* A fake operator used to indicate unary operator precedence. */
102 %token
<string> STRING
103 %token
<string> QUOTED_STRING
104 %token
<integer
> INTEGER
106 /* Keywords. This list is taken from ldgram.y and ldlex.l in the old
107 GNU linker, with the keywords which only appear in MRI mode
108 removed. Not all these keywords are actually used in this grammar.
109 In most cases the keyword is recognized as the token name in upper
110 case. The comments indicate where this is not the case. */
114 %token ALIGN_K
/* ALIGN */
116 %token ASSERT_K
/* ASSERT */
124 %token CREATE_OBJECT_SYMBOLS
125 %token DATA_SEGMENT_ALIGN
126 %token DATA_SEGMENT_END
127 %token DATA_SEGMENT_RELRO_END
134 %token FORCE_COMMON_ALLOCATION
135 %token GLOBAL
/* global */
139 %token INHIBIT_COMMON_ALLOCATION
142 %token LENGTH
/* LENGTH, l, len */
144 %token LOCAL
/* local */
147 %token MAX_K
/* MAX */
149 %token MIN_K
/* MIN */
155 %token ORIGIN
/* ORIGIN, o, org */
162 %token PROVIDE_HIDDEN
169 %token SIZEOF_HEADERS
/* SIZEOF_HEADERS, sizeof_headers */
170 %token SORT_BY_ALIGNMENT
177 %token TARGET_K
/* TARGET */
179 %token VERSIONK
/* VERSION */
181 /* Keywords, part 2. These are keywords that are unique to gold,
182 and not present in the old GNU linker. As before, unless the
183 comments say otherwise, the keyword is recognized as the token
184 name in upper case. */
188 /* Special tokens used to tell the grammar what type of tokens we are
189 parsing. The token stream always begins with one of these tokens.
190 We do this because version scripts can appear embedded within
191 linker scripts, and because --defsym uses the expression
193 %token PARSING_LINKER_SCRIPT
194 %token PARSING_VERSION_SCRIPT
195 %token PARSING_DEFSYM
197 /* Non-terminal types, where needed. */
199 %type
<expr
> parse_exp exp opt_address_and_section_type
200 %type
<expr
> opt_at opt_align opt_subalign opt_fill
201 %type
<output_section_header
> section_header
202 %type
<output_section_trailer
> section_trailer
203 %type
<constraint
> opt_constraint
204 %type
<string_list
> opt_phdr
205 %type
<integer
> data_length
206 %type
<input_section_spec
> input_section_no_keep
207 %type
<wildcard_sections
> wildcard_sections
208 %type
<wildcard_section
> wildcard_file wildcard_section
209 %type
<string_list
> exclude_names
210 %type
<string> wildcard_name
211 %type
<integer
> phdr_type
212 %type
<phdr_info
> phdr_info
213 %type
<versyms
> vers_defns
214 %type
<versnode
> vers_tag
215 %type
<deplist
> verdep
216 %type
<string> string
220 /* Read the special token to see what to read next. */
222 PARSING_LINKER_SCRIPT linker_script
223 | PARSING_VERSION_SCRIPT version_script
224 | PARSING_DEFSYM defsym_expr
227 /* A file contains a list of commands. */
229 linker_script file_cmd
233 /* A command which may appear at top level of a linker script. */
235 FORCE_COMMON_ALLOCATION
236 { script_set_common_allocation
(closure
, 1); }
238 { script_start_group
(closure
); }
240 { script_end_group
(closure
); }
241 | INHIBIT_COMMON_ALLOCATION
242 { script_set_common_allocation
(closure
, 0); }
243 | OPTION
'(' string ')'
244 { script_parse_option
(closure
, $3.value
, $3.length
); }
245 | PHDRS
'{' phdrs_defs
'}'
246 | SEARCH_DIR
'(' string ')'
247 { script_add_search_dir
(closure
, $3.value
, $3.length
); }
249 { script_start_sections
(closure
); }
251 { script_finish_sections
(closure
); }
253 { script_push_lex_into_version_mode
(closure
); }
255 { script_pop_lex_mode
(closure
); }
256 | file_or_sections_cmd
261 /* Top level commands which we ignore. The GNU linker uses these to
262 select the output format, but we don't offer a choice. Ignoring
263 these is more-or-less OK since most scripts simply explicitly
264 choose the default. */
266 OUTPUT_FORMAT
'(' string ')'
267 | OUTPUT_FORMAT
'(' string ',' string ',' string ')'
268 | OUTPUT_ARCH
'(' string ')'
271 /* A list of input file names. */
274 | input_list opt_comma input_list_element
277 /* An input file name. */
280 { script_add_file
(closure
, $1.value
, $1.length
); }
282 { script_start_as_needed
(closure
); }
284 { script_end_as_needed
(closure
); }
287 /* Commands in a SECTIONS block. */
289 sections_block section_block_cmd
293 /* A command which may appear within a SECTIONS block. */
296 |
string section_header
297 { script_start_output_section
(closure
, $1.value
, $1.length
, &$2); }
298 '{' section_cmds
'}' section_trailer
299 { script_finish_output_section
(closure
, &$7); }
302 /* The header of an output section in a SECTIONS block--everything
305 { script_push_lex_into_expression_mode
(closure
); }
306 opt_address_and_section_type opt_at opt_align opt_subalign
307 { script_pop_lex_mode
(closure
); }
311 $$.load_address
= $3;
318 /* The optional address followed by the optional section type. This
319 is a separate nonterminal to avoid a shift/reduce conflict on
320 '(' in section_header. */
322 opt_address_and_section_type:
331 | exp
'(' string ')' ':'
333 yyerror(closure
, "section types are not supported");
338 /* The address at which an output section should be loaded. */
346 /* The alignment of an output section. */
350 | ALIGN_K
'(' exp
')'
354 /* The input section alignment within an output section. */
358 | SUBALIGN
'(' exp
')'
362 /* A section constraint. */
365 { $$
= CONSTRAINT_NONE
; }
367 { $$
= CONSTRAINT_ONLY_IF_RO
; }
369 { $$
= CONSTRAINT_ONLY_IF_RW
; }
371 { $$
= CONSTRAINT_SPECIAL
; }
374 /* The trailer of an output section in a SECTIONS block. */
376 opt_memspec opt_at_memspec opt_phdr opt_fill opt_comma
383 /* A memory specification for an output section. */
386 { yyerror(closure
, "memory regions are not supported"); }
390 /* A memory specification for where to load an output section. */
393 { yyerror(closure
, "memory regions are not supported"); }
397 /* The program segment an output section should go into. */
400 { $$
= script_string_list_push_back
($1, $3.value
, $3.length
); }
405 /* The value to use to fill an output section. FIXME: This does not
406 handle a string of arbitrary length. */
414 /* Commands which may appear within the description of an output
415 section in a SECTIONS block. */
418 | section_cmds section_cmd
421 /* A command which may appear within the description of an output
422 section in a SECTIONS block. */
426 | data_length
'(' parse_exp
')'
427 { script_add_data
(closure
, $1, $3); }
428 | ASSERT_K
'(' parse_exp
',' string ')'
429 { script_add_assertion
(closure
, $3, $5.value
, $5.length
); }
430 | FILL
'(' parse_exp
')'
431 { script_add_fill
(closure
, $3); }
434 /* The GNU linker uses CONSTRUCTORS for the a.out object
435 file format. It does nothing when using ELF. Since
436 some ELF linker scripts use it although it does
437 nothing, we accept it and ignore it. */
439 | SORT_BY_NAME
'(' CONSTRUCTORS
')'
443 /* The length of data which may appear within the description of an
444 output section in a SECTIONS block. */
458 /* An input section specification. This may appear within the
459 description of an output section in a SECTIONS block. */
461 input_section_no_keep
462 { script_add_input_section
(closure
, &$1, 0); }
463 | KEEP
'(' input_section_no_keep
')'
464 { script_add_input_section
(closure
, &$3, 1); }
467 /* An input section specification within a KEEP clause. */
468 input_section_no_keep:
472 $$.file.sort
= SORT_WILDCARD_NONE
;
473 $$.input_sections.sections
= NULL
;
474 $$.input_sections.exclude
= NULL
;
476 | wildcard_file
'(' wildcard_sections
')'
479 $$.input_sections
= $3;
483 /* A wildcard file specification. */
488 $$.sort
= SORT_WILDCARD_NONE
;
490 | SORT_BY_NAME
'(' wildcard_name
')'
493 $$.sort
= SORT_WILDCARD_BY_NAME
;
497 /* A list of wild card section specifications. */
499 wildcard_sections opt_comma wildcard_section
501 $$.sections
= script_string_sort_list_add
($1.sections
, &$3);
502 $$.exclude
= $1.exclude
;
506 $$.sections
= script_new_string_sort_list
(&$1);
509 | wildcard_sections opt_comma EXCLUDE_FILE
'(' exclude_names
')'
511 $$.sections
= $1.sections
;
512 $$.exclude
= script_string_list_append
($1.exclude
, $5);
514 | EXCLUDE_FILE
'(' exclude_names
')'
521 /* A single wild card specification. */
526 $$.sort
= SORT_WILDCARD_NONE
;
528 | SORT_BY_NAME
'(' wildcard_section
')'
533 case SORT_WILDCARD_NONE
:
534 $$.sort
= SORT_WILDCARD_BY_NAME
;
536 case SORT_WILDCARD_BY_NAME
:
537 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
539 case SORT_WILDCARD_BY_ALIGNMENT
:
540 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
541 $$.sort
= SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
;
547 | SORT_BY_ALIGNMENT
'(' wildcard_section
')'
552 case SORT_WILDCARD_NONE
:
553 $$.sort
= SORT_WILDCARD_BY_ALIGNMENT
;
555 case SORT_WILDCARD_BY_ALIGNMENT
:
556 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
:
558 case SORT_WILDCARD_BY_NAME
:
559 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT
:
560 $$.sort
= SORT_WILDCARD_BY_ALIGNMENT_BY_NAME
;
568 /* A list of file names to exclude. */
570 exclude_names opt_comma wildcard_name
571 { $$
= script_string_list_push_back
($1, $3.value
, $3.length
); }
573 { $$
= script_new_string_list
($1.value
, $1.length
); }
576 /* A single wildcard name. We recognize '*' and '?' specially since
577 they are expression tokens. */
593 /* A command which may appear at the top level of a linker script, or
594 within a SECTIONS block. */
595 file_or_sections_cmd:
597 { script_set_entry
(closure
, $3.value
, $3.length
); }
599 | ASSERT_K
'(' parse_exp
',' string ')'
600 { script_add_assertion
(closure
, $3, $5.value
, $5.length
); }
603 /* A list of program header definitions. */
609 /* A program header definition. */
611 string phdr_type phdr_info
';'
612 { script_add_phdr
(closure
, $1.value
, $1.length
, $2, &$3); }
615 /* A program header type. The GNU linker accepts a general expression
616 here, but that would be a pain because we would have to dig into
617 the expression structure. It's unlikely that anybody uses anything
618 other than a string or a number here, so that is all we expect. */
621 { $$
= script_phdr_string_to_type
(closure
, $1.value
, $1.length
); }
626 /* Additional information for a program header. */
629 { memset
(&$$
, 0, sizeof
(struct Phdr_info
)); }
633 if
($1.length
== 7 && strncmp
($1.value
, "FILEHDR", 7) == 0)
634 $$.includes_filehdr
= 1;
636 yyerror(closure
, "PHDRS syntax error");
641 $$.includes_phdrs
= 1;
643 |
string '(' INTEGER
')' phdr_info
646 if
($1.length
== 5 && strncmp
($1.value
, "FLAGS", 5) == 0)
648 $$.is_flags_valid
= 1;
652 yyerror(closure
, "PHDRS syntax error");
654 | AT
'(' parse_exp
')' phdr_info
657 $$.load_address
= $3;
661 /* Set a symbol to a value. */
664 { script_set_symbol
(closure
, $1.value
, $1.length
, $3, 0, 0); }
665 |
string PLUSEQ parse_exp
667 Expression_ptr s
= script_exp_string
($1.value
, $1.length
);
668 Expression_ptr e
= script_exp_binary_add
(s
, $3);
669 script_set_symbol
(closure
, $1.value
, $1.length
, e
, 0, 0);
671 |
string MINUSEQ parse_exp
673 Expression_ptr s
= script_exp_string
($1.value
, $1.length
);
674 Expression_ptr e
= script_exp_binary_sub
(s
, $3);
675 script_set_symbol
(closure
, $1.value
, $1.length
, e
, 0, 0);
677 |
string MULTEQ parse_exp
679 Expression_ptr s
= script_exp_string
($1.value
, $1.length
);
680 Expression_ptr e
= script_exp_binary_mult
(s
, $3);
681 script_set_symbol
(closure
, $1.value
, $1.length
, e
, 0, 0);
683 |
string DIVEQ parse_exp
685 Expression_ptr s
= script_exp_string
($1.value
, $1.length
);
686 Expression_ptr e
= script_exp_binary_div
(s
, $3);
687 script_set_symbol
(closure
, $1.value
, $1.length
, e
, 0, 0);
689 |
string LSHIFTEQ parse_exp
691 Expression_ptr s
= script_exp_string
($1.value
, $1.length
);
692 Expression_ptr e
= script_exp_binary_lshift
(s
, $3);
693 script_set_symbol
(closure
, $1.value
, $1.length
, e
, 0, 0);
695 |
string RSHIFTEQ parse_exp
697 Expression_ptr s
= script_exp_string
($1.value
, $1.length
);
698 Expression_ptr e
= script_exp_binary_rshift
(s
, $3);
699 script_set_symbol
(closure
, $1.value
, $1.length
, e
, 0, 0);
701 |
string ANDEQ parse_exp
703 Expression_ptr s
= script_exp_string
($1.value
, $1.length
);
704 Expression_ptr e
= script_exp_binary_bitwise_and
(s
, $3);
705 script_set_symbol
(closure
, $1.value
, $1.length
, e
, 0, 0);
707 |
string OREQ parse_exp
709 Expression_ptr s
= script_exp_string
($1.value
, $1.length
);
710 Expression_ptr e
= script_exp_binary_bitwise_or
(s
, $3);
711 script_set_symbol
(closure
, $1.value
, $1.length
, e
, 0, 0);
713 | PROVIDE
'(' string '=' parse_exp
')'
714 { script_set_symbol
(closure
, $3.value
, $3.length
, $5, 1, 0); }
715 | PROVIDE_HIDDEN
'(' string '=' parse_exp
')'
716 { script_set_symbol
(closure
, $3.value
, $3.length
, $5, 1, 1); }
719 /* Parse an expression, putting the lexer into the right mode. */
721 { script_push_lex_into_expression_mode
(closure
); }
724 script_pop_lex_mode
(closure
);
733 |
'-' exp %prec UNARY
734 { $$
= script_exp_unary_minus
($2); }
735 |
'!' exp %prec UNARY
736 { $$
= script_exp_unary_logical_not
($2); }
737 |
'~' exp %prec UNARY
738 { $$
= script_exp_unary_bitwise_not
($2); }
739 |
'+' exp %prec UNARY
742 { $$
= script_exp_binary_mult
($1, $3); }
744 { $$
= script_exp_binary_div
($1, $3); }
746 { $$
= script_exp_binary_mod
($1, $3); }
748 { $$
= script_exp_binary_add
($1, $3); }
750 { $$
= script_exp_binary_sub
($1, $3); }
752 { $$
= script_exp_binary_lshift
($1, $3); }
754 { $$
= script_exp_binary_rshift
($1, $3); }
756 { $$
= script_exp_binary_eq
($1, $3); }
758 { $$
= script_exp_binary_ne
($1, $3); }
760 { $$
= script_exp_binary_le
($1, $3); }
762 { $$
= script_exp_binary_ge
($1, $3); }
764 { $$
= script_exp_binary_lt
($1, $3); }
766 { $$
= script_exp_binary_gt
($1, $3); }
768 { $$
= script_exp_binary_bitwise_and
($1, $3); }
770 { $$
= script_exp_binary_bitwise_xor
($1, $3); }
772 { $$
= script_exp_binary_bitwise_or
($1, $3); }
774 { $$
= script_exp_binary_logical_and
($1, $3); }
776 { $$
= script_exp_binary_logical_or
($1, $3); }
777 | exp
'?' exp
':' exp
778 { $$
= script_exp_trinary_cond
($1, $3, $5); }
780 { $$
= script_exp_integer
($1); }
782 { $$
= script_exp_string
($1.value
, $1.length
); }
783 | MAX_K
'(' exp
',' exp
')'
784 { $$
= script_exp_function_max
($3, $5); }
785 | MIN_K
'(' exp
',' exp
')'
786 { $$
= script_exp_function_min
($3, $5); }
787 | DEFINED
'(' string ')'
788 { $$
= script_exp_function_defined
($3.value
, $3.length
); }
790 { $$
= script_exp_function_sizeof_headers
(); }
791 | ALIGNOF
'(' string ')'
792 { $$
= script_exp_function_alignof
($3.value
, $3.length
); }
793 | SIZEOF
'(' string ')'
794 { $$
= script_exp_function_sizeof
($3.value
, $3.length
); }
795 | ADDR
'(' string ')'
796 { $$
= script_exp_function_addr
($3.value
, $3.length
); }
797 | LOADADDR
'(' string ')'
798 { $$
= script_exp_function_loadaddr
($3.value
, $3.length
); }
799 | ORIGIN
'(' string ')'
800 { $$
= script_exp_function_origin
($3.value
, $3.length
); }
801 | LENGTH
'(' string ')'
802 { $$
= script_exp_function_length
($3.value
, $3.length
); }
803 | CONSTANT
'(' string ')'
804 { $$
= script_exp_function_constant
($3.value
, $3.length
); }
805 | ABSOLUTE
'(' exp
')'
806 { $$
= script_exp_function_absolute
($3); }
807 | ALIGN_K
'(' exp
')'
808 { $$
= script_exp_function_align
(script_exp_string
(".", 1), $3); }
809 | ALIGN_K
'(' exp
',' exp
')'
810 { $$
= script_exp_function_align
($3, $5); }
812 { $$
= script_exp_function_align
(script_exp_string
(".", 1), $3); }
813 | DATA_SEGMENT_ALIGN
'(' exp
',' exp
')'
814 { $$
= script_exp_function_data_segment_align
($3, $5); }
815 | DATA_SEGMENT_RELRO_END
'(' exp
',' exp
')'
816 { $$
= script_exp_function_data_segment_relro_end
($3, $5); }
817 | DATA_SEGMENT_END
'(' exp
')'
818 { $$
= script_exp_function_data_segment_end
($3); }
819 | SEGMENT_START
'(' string ',' exp
')'
821 $$
= script_exp_function_segment_start
($3.value
, $3.length
, $5);
823 | ASSERT_K
'(' exp
',' string ')'
824 { $$
= script_exp_function_assert
($3, $5.value
, $5.length
); }
827 /* Handle the --defsym option. */
830 { script_set_symbol
(closure
, $1.value
, $1.length
, $3, 0, 0); }
833 /* A version script. */
840 | vers_nodes vers_node
846 script_register_vers_node
(closure
, NULL
, 0, $2, NULL
);
848 |
string '{' vers_tag
'}' ';'
850 script_register_vers_node
(closure
, $1.value
, $1.length
, $3,
853 |
string '{' vers_tag
'}' verdep
';'
855 script_register_vers_node
(closure
, $1.value
, $1.length
, $3, $5);
862 $$
= script_add_vers_depend
(closure
, NULL
, $1.value
, $1.length
);
866 $$
= script_add_vers_depend
(closure
, $1, $2.value
, $2.length
);
872 { $$
= script_new_vers_node
(closure
, NULL
, NULL
); }
874 { $$
= script_new_vers_node
(closure
, $1, NULL
); }
875 | GLOBAL
':' vers_defns
';'
876 { $$
= script_new_vers_node
(closure
, $3, NULL
); }
877 | LOCAL
':' vers_defns
';'
878 { $$
= script_new_vers_node
(closure
, NULL
, $3); }
879 | GLOBAL
':' vers_defns
';' LOCAL
':' vers_defns
';'
880 { $$
= script_new_vers_node
(closure
, $3, $7); }
883 /* Here is one of the rare places we care about the distinction
884 between STRING and QUOTED_STRING. For QUOTED_STRING, we do exact
885 matching on the pattern, so we pass in true for the exact_match
886 parameter. For STRING, we do glob matching and pass in false. */
890 $$
= script_new_vers_pattern
(closure
, NULL
, $1.value
,
895 $$
= script_new_vers_pattern
(closure
, NULL
, $1.value
,
898 | vers_defns
';' STRING
900 $$
= script_new_vers_pattern
(closure
, $1, $3.value
,
903 | vers_defns
';' QUOTED_STRING
905 $$
= script_new_vers_pattern
(closure
, $1, $3.value
,
908 |
/* Push string on the language stack. */
910 { version_script_push_lang
(closure
, $2.value
, $2.length
); }
911 vers_defns opt_semicolon
'}'
914 version_script_pop_lang
(closure
);
916 |
/* Push string on the language stack. This is more complicated
917 than the other cases because we need to merge the linked-list
918 state from the pre-EXTERN defns and the post-EXTERN defns. */
919 vers_defns
';' EXTERN
string '{'
920 { version_script_push_lang
(closure
, $4.value
, $4.length
); }
921 vers_defns opt_semicolon
'}'
923 $$
= script_merge_expressions
($1, $7);
924 version_script_pop_lang
(closure
);
926 | EXTERN
// "extern" as a symbol name
928 $$
= script_new_vers_pattern
(closure
, NULL
, "extern",
929 sizeof
("extern") - 1, 1);
931 | vers_defns
';' EXTERN
933 $$
= script_new_vers_pattern
(closure
, $1, "extern",
934 sizeof
("extern") - 1, 1);
938 /* A string can be either a STRING or a QUOTED_STRING. Almost all the
939 time we don't care, and we use this rule. */
947 /* Some statements require a terminator, which may be a semicolon or a
954 /* An optional semicolon. */
960 /* An optional comma. */