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. */
37 /* We need to use a pure parser because we might be multi-threaded.
38 We pass some arguments through the parser to the lexer. */
42 %parse-param {void* closure}
43 %lex-param {void* closure}
45 /* Since we require bison anyhow, we take advantage of it. */
49 /* The values associated with tokens. */
53 struct Parser_string string;
58 // Used for version scripts and within VERSION {}
59 struct Version_dependency_list* deplist;
60 struct Version_expression_list* versyms;
61 struct Version_tree* versnode;
64 /* Operators, including a precedence table for expressions. */
66 %right PLUSEQ MINUSEQ MULTEQ DIVEQ '=' LSHIFTEQ RSHIFTEQ ANDEQ OREQ
79 /* A fake operator used to indicate unary operator precedence. */
84 %token <string> STRING
85 %token <string> QUOTED_STRING
86 %token <integer> INTEGER
88 /* Keywords. This list is taken from ldgram.y and ldlex.l in the old
89 GNU linker, with the keywords which only appear in MRI mode
90 removed. Not all these keywords are actually used in this grammar.
91 In most cases the keyword is recognized as the token name in upper
92 case. The comments indicate where this is not the case. */
96 %token ALIGN_K /* ALIGN */
98 %token ASSERT_K /* ASSERT */
107 %token CREATE_OBJECT_SYMBOLS
108 %token DATA_SEGMENT_ALIGN
109 %token DATA_SEGMENT_END
110 %token DATA_SEGMENT_RELRO_END
118 %token FORCE_COMMON_ALLOCATION
119 %token GLOBAL /* global */
124 %token INHIBIT_COMMON_ALLOCATION
127 %token LENGTH /* LENGTH, l, len */
129 %token LOCAL /* local */
132 %token MAX_K /* MAX */
134 %token MIN_K /* MIN */
141 %token ORIGIN /* ORIGIN, o, org */
148 %token PROVIDE_HIDDEN
155 %token SIZEOF_HEADERS /* SIZEOF_HEADERS, sizeof_headers */
156 %token SORT_BY_ALIGNMENT
163 %token TARGET_K /* TARGET */
165 %token VERSIONK /* VERSION */
167 /* Keywords, part 2. These are keywords that are unique to gold,
168 and not present in the old GNU linker. As before, unless the
169 comments say otherwise, the keyword is recognized as the token
170 name in upper case. */
174 /* Special tokens used to tell the grammar what type of tokens we are
175 parsing. The token stream always begins with one of these tokens.
176 We do this because version scripts can appear embedded within
177 linker scripts, and because --defsym uses the expression
179 %token PARSING_LINKER_SCRIPT
180 %token PARSING_VERSION_SCRIPT
181 %token PARSING_DEFSYM
183 /* Non-terminal types, where needed. */
185 %type <expr> parse_exp exp
186 %type <versyms> vers_defns
187 %type <versnode> vers_tag
188 %type <deplist> verdep
189 %type <string> string
193 /* Read the special token to see what to read next. */
195 PARSING_LINKER_SCRIPT linker_script
196 | PARSING_VERSION_SCRIPT version_script
197 | PARSING_DEFSYM defsym_expr
200 /* A file contains a list of commands. */
202 linker_script file_cmd
206 /* A command which may appear at top level of a linker script. */
209 { script_start_group(closure); }
211 { script_end_group(closure); }
212 | OPTION '(' string ')'
213 { script_parse_option(closure, $3.value, $3.length); }
215 { script_push_lex_into_version_mode(closure); }
217 { script_pop_lex_mode(closure); }
218 | file_or_sections_cmd
222 /* Top level commands which we ignore. The GNU linker uses these to
223 select the output format, but we don't offer a choice. Ignoring
224 these is more-or-less OK since most scripts simply explicitly
225 choose the default. */
227 OUTPUT_FORMAT '(' string ')'
228 | OUTPUT_FORMAT '(' string ',' string ',' string ')'
229 | OUTPUT_ARCH '(' string ')'
232 /* A list of input file names. */
235 | input_list opt_comma input_list_element
238 /* An input file name. */
241 { script_add_file(closure, $1.value, $1.length); }
243 { script_start_as_needed(closure); }
245 { script_end_as_needed(closure); }
248 /* A command which may appear at the top level of a linker script, or
249 within a SECTIONS block. */
250 file_or_sections_cmd:
252 { script_set_entry(closure, $3.value, $3.length); }
256 /* Set a symbol to a value. */
259 { script_set_symbol(closure, $1.value, $1.length, $3, 0, 0); }
260 | string PLUSEQ parse_exp
262 Expression_ptr s = script_exp_string($1.value, $1.length);
263 Expression_ptr e = script_exp_binary_add(s, $3);
264 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
266 | string MINUSEQ parse_exp
268 Expression_ptr s = script_exp_string($1.value, $1.length);
269 Expression_ptr e = script_exp_binary_sub(s, $3);
270 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
272 | string MULTEQ parse_exp
274 Expression_ptr s = script_exp_string($1.value, $1.length);
275 Expression_ptr e = script_exp_binary_mult(s, $3);
276 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
278 | string DIVEQ parse_exp
280 Expression_ptr s = script_exp_string($1.value, $1.length);
281 Expression_ptr e = script_exp_binary_div(s, $3);
282 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
284 | string LSHIFTEQ parse_exp
286 Expression_ptr s = script_exp_string($1.value, $1.length);
287 Expression_ptr e = script_exp_binary_lshift(s, $3);
288 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
290 | string RSHIFTEQ parse_exp
292 Expression_ptr s = script_exp_string($1.value, $1.length);
293 Expression_ptr e = script_exp_binary_rshift(s, $3);
294 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
296 | string ANDEQ parse_exp
298 Expression_ptr s = script_exp_string($1.value, $1.length);
299 Expression_ptr e = script_exp_binary_bitwise_and(s, $3);
300 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
302 | string OREQ parse_exp
304 Expression_ptr s = script_exp_string($1.value, $1.length);
305 Expression_ptr e = script_exp_binary_bitwise_or(s, $3);
306 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
308 | PROVIDE '(' string '=' parse_exp ')'
309 { script_set_symbol(closure, $3.value, $3.length, $5, 1, 0); }
310 | PROVIDE_HIDDEN '(' string '=' parse_exp ')'
311 { script_set_symbol(closure, $3.value, $3.length, $5, 1, 1); }
314 /* Parse an expression, putting the lexer into the right mode. */
316 { script_push_lex_into_expression_mode(closure); }
319 script_pop_lex_mode(closure);
328 | '-' exp %prec UNARY
329 { $$ = script_exp_unary_minus($2); }
330 | '!' exp %prec UNARY
331 { $$ = script_exp_unary_logical_not($2); }
332 | '~' exp %prec UNARY
333 { $$ = script_exp_unary_bitwise_not($2); }
334 | '+' exp %prec UNARY
337 { $$ = script_exp_binary_mult($1, $3); }
339 { $$ = script_exp_binary_div($1, $3); }
341 { $$ = script_exp_binary_mod($1, $3); }
343 { $$ = script_exp_binary_add($1, $3); }
345 { $$ = script_exp_binary_sub($1, $3); }
347 { $$ = script_exp_binary_lshift($1, $3); }
349 { $$ = script_exp_binary_rshift($1, $3); }
351 { $$ = script_exp_binary_eq($1, $3); }
353 { $$ = script_exp_binary_ne($1, $3); }
355 { $$ = script_exp_binary_le($1, $3); }
357 { $$ = script_exp_binary_ge($1, $3); }
359 { $$ = script_exp_binary_lt($1, $3); }
361 { $$ = script_exp_binary_gt($1, $3); }
363 { $$ = script_exp_binary_bitwise_and($1, $3); }
365 { $$ = script_exp_binary_bitwise_xor($1, $3); }
367 { $$ = script_exp_binary_bitwise_or($1, $3); }
369 { $$ = script_exp_binary_logical_and($1, $3); }
371 { $$ = script_exp_binary_logical_or($1, $3); }
372 | exp '?' exp ':' exp
373 { $$ = script_exp_trinary_cond($1, $3, $5); }
375 { $$ = script_exp_integer($1); }
377 { $$ = script_exp_string($1.value, $1.length); }
379 { $$ = script_exp_string($1.value, $1.length); }
380 | MAX_K '(' exp ',' exp ')'
381 { $$ = script_exp_function_max($3, $5); }
382 | MIN_K '(' exp ',' exp ')'
383 { $$ = script_exp_function_min($3, $5); }
384 | DEFINED '(' string ')'
385 { $$ = script_exp_function_defined($3.value, $3.length); }
387 { $$ = script_exp_function_sizeof_headers(); }
388 | ALIGNOF '(' string ')'
389 { $$ = script_exp_function_alignof($3.value, $3.length); }
390 | SIZEOF '(' string ')'
391 { $$ = script_exp_function_sizeof($3.value, $3.length); }
392 | ADDR '(' string ')'
393 { $$ = script_exp_function_addr($3.value, $3.length); }
394 | LOADADDR '(' string ')'
395 { $$ = script_exp_function_loadaddr($3.value, $3.length); }
396 | ORIGIN '(' string ')'
397 { $$ = script_exp_function_origin($3.value, $3.length); }
398 | LENGTH '(' string ')'
399 { $$ = script_exp_function_length($3.value, $3.length); }
400 | CONSTANT '(' string ')'
401 { $$ = script_exp_function_constant($3.value, $3.length); }
402 | ABSOLUTE '(' exp ')'
403 { $$ = script_exp_function_absolute($3); }
404 | ALIGN_K '(' exp ')'
405 { $$ = script_exp_function_align(script_exp_string(".", 1), $3); }
406 | ALIGN_K '(' exp ',' exp ')'
407 { $$ = script_exp_function_align($3, $5); }
409 { $$ = script_exp_function_align(script_exp_string(".", 1), $3); }
410 | DATA_SEGMENT_ALIGN '(' exp ',' exp ')'
411 { $$ = script_exp_function_data_segment_align($3, $5); }
412 | DATA_SEGMENT_RELRO_END '(' exp ',' exp ')'
413 { $$ = script_exp_function_data_segment_relro_end($3, $5); }
414 | DATA_SEGMENT_END '(' exp ')'
415 { $$ = script_exp_function_data_segment_end($3); }
416 | SEGMENT_START '(' string ',' exp ')'
418 $$ = script_exp_function_segment_start($3.value, $3.length, $5);
420 | ASSERT_K '(' exp ',' string ')'
421 { $$ = script_exp_function_assert($3, $5.value, $5.length); }
424 /* Handle the --defsym option. */
427 { script_set_symbol(closure, $1.value, $1.length, $3, 0, 0); }
430 /* A version script. */
437 | vers_nodes vers_node
443 script_register_vers_node (closure, NULL, 0, $2, NULL);
445 | string '{' vers_tag '}' ';'
447 script_register_vers_node (closure, $1.value, $1.length, $3,
450 | string '{' vers_tag '}' verdep ';'
452 script_register_vers_node (closure, $1.value, $1.length, $3, $5);
459 $$ = script_add_vers_depend (closure, NULL, $1.value, $1.length);
463 $$ = script_add_vers_depend (closure, $1, $2.value, $2.length);
469 { $$ = script_new_vers_node (closure, NULL, NULL); }
471 { $$ = script_new_vers_node (closure, $1, NULL); }
472 | GLOBAL ':' vers_defns ';'
473 { $$ = script_new_vers_node (closure, $3, NULL); }
474 | LOCAL ':' vers_defns ';'
475 { $$ = script_new_vers_node (closure, NULL, $3); }
476 | GLOBAL ':' vers_defns ';' LOCAL ':' vers_defns ';'
477 { $$ = script_new_vers_node (closure, $3, $7); }
480 /* Here is one of the rare places we care about the distinction
481 between STRING and QUOTED_STRING. For QUOTED_STRING, we do exact
482 matching on the pattern, so we pass in true for the exact_match
483 parameter. For STRING, we do glob matching and pass in false. */
487 $$ = script_new_vers_pattern (closure, NULL, $1.value,
492 $$ = script_new_vers_pattern (closure, NULL, $1.value,
495 | vers_defns ';' STRING
497 $$ = script_new_vers_pattern (closure, $1, $3.value,
500 | vers_defns ';' QUOTED_STRING
502 $$ = script_new_vers_pattern (closure, $1, $3.value,
505 | /* Push string on the language stack. */
507 { version_script_push_lang (closure, $2.value, $2.length); }
508 vers_defns opt_semicolon '}'
511 version_script_pop_lang(closure);
513 | /* Push string on the language stack. This is more complicated
514 than the other cases because we need to merge the linked-list
515 state from the pre-EXTERN defns and the post-EXTERN defns. */
516 vers_defns ';' EXTERN string '{'
517 { version_script_push_lang (closure, $4.value, $4.length); }
518 vers_defns opt_semicolon '}'
520 $$ = script_merge_expressions ($1, $7);
521 version_script_pop_lang(closure);
523 | EXTERN // "extern" as a symbol name
525 $$ = script_new_vers_pattern (closure, NULL, "extern",
526 sizeof("extern") - 1, 1);
528 | vers_defns ';' EXTERN
530 $$ = script_new_vers_pattern (closure, $1, "extern",
531 sizeof("extern") - 1, 1);
535 /* A string can be either a STRING or a QUOTED_STRING. Almost all the
536 time we don't care, and we use this rule. */
544 /* Some statements require a terminator, which may be a semicolon or a
551 /* An optional semicolon. */
557 /* An optional comma. */