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 <integer> INTEGER
87 /* Keywords. This list is taken from ldgram.y and ldlex.l in the old
88 GNU linker, with the keywords which only appear in MRI mode
89 removed. Not all these keywords are actually used in this grammar.
90 In most cases the keyword is recognized as the token name in upper
91 case. The comments indicate where this is not the case. */
95 %token ALIGN_K /* ALIGN */
97 %token ASSERT_K /* ASSERT */
106 %token CREATE_OBJECT_SYMBOLS
107 %token DATA_SEGMENT_ALIGN
108 %token DATA_SEGMENT_END
109 %token DATA_SEGMENT_RELRO_END
117 %token FORCE_COMMON_ALLOCATION
118 %token GLOBAL /* global */
123 %token INHIBIT_COMMON_ALLOCATION
126 %token LENGTH /* LENGTH, l, len */
128 %token LOCAL /* local */
131 %token MAX_K /* MAX */
133 %token MIN_K /* MIN */
140 %token ORIGIN /* ORIGIN, o, org */
147 %token PROVIDE_HIDDEN
154 %token SIZEOF_HEADERS /* SIZEOF_HEADERS, sizeof_headers */
155 %token SORT_BY_ALIGNMENT
162 %token TARGET_K /* TARGET */
164 %token VERSIONK /* VERSION */
166 /* Keywords, part 2. These are keywords that are unique to gold,
167 and not present in the old GNU linker. As before, unless the
168 comments say otherwise, the keyword is recognized as the token
169 name in upper case. */
173 /* Special tokens used to tell the grammar what type of tokens we are
174 parsing. The token stream always begins with one of these tokens.
175 We do this because version scripts can appear embedded within
176 linker scripts, and because --defsym uses the expression
178 %token PARSING_LINKER_SCRIPT
179 %token PARSING_VERSION_SCRIPT
180 %token PARSING_DEFSYM
182 /* Non-terminal types, where needed. */
184 %type <expr> parse_exp exp
185 %type <versyms> vers_defns
186 %type <versnode> vers_tag
187 %type <deplist> verdep
191 /* Read the special token to see what to read next. */
193 PARSING_LINKER_SCRIPT linker_script
194 | PARSING_VERSION_SCRIPT version_script
195 | PARSING_DEFSYM defsym_expr
198 /* A file contains a list of commands. */
200 linker_script file_cmd
204 /* A command which may appear at top level of a linker script. */
207 { script_start_group(closure); }
209 { script_end_group(closure); }
210 | OPTION '(' STRING ')'
211 { script_parse_option(closure, $3.value, $3.length); }
213 { script_push_lex_into_version_mode(closure); }
215 { script_pop_lex_mode(closure); }
216 | file_or_sections_cmd
220 /* Top level commands which we ignore. The GNU linker uses these to
221 select the output format, but we don't offer a choice. Ignoring
222 these is more-or-less OK since most scripts simply explicitly
223 choose the default. */
225 OUTPUT_FORMAT '(' STRING ')'
226 | OUTPUT_FORMAT '(' STRING ',' STRING ',' STRING ')'
227 | OUTPUT_ARCH '(' STRING ')'
230 /* A list of input file names. */
233 | input_list opt_comma input_list_element
236 /* An input file name. */
239 { script_add_file(closure, $1.value, $1.length); }
241 { script_start_as_needed(closure); }
243 { script_end_as_needed(closure); }
246 /* A command which may appear at the top level of a linker script, or
247 within a SECTIONS block. */
248 file_or_sections_cmd:
250 { script_set_entry(closure, $3.value, $3.length); }
254 /* Set a symbol to a value. */
257 { script_set_symbol(closure, $1.value, $1.length, $3, 0, 0); }
258 | STRING PLUSEQ parse_exp
260 Expression_ptr s = script_exp_string($1.value, $1.length);
261 Expression_ptr e = script_exp_binary_add(s, $3);
262 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
264 | STRING MINUSEQ parse_exp
266 Expression_ptr s = script_exp_string($1.value, $1.length);
267 Expression_ptr e = script_exp_binary_sub(s, $3);
268 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
270 | STRING MULTEQ parse_exp
272 Expression_ptr s = script_exp_string($1.value, $1.length);
273 Expression_ptr e = script_exp_binary_mult(s, $3);
274 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
276 | STRING DIVEQ parse_exp
278 Expression_ptr s = script_exp_string($1.value, $1.length);
279 Expression_ptr e = script_exp_binary_div(s, $3);
280 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
282 | STRING LSHIFTEQ parse_exp
284 Expression_ptr s = script_exp_string($1.value, $1.length);
285 Expression_ptr e = script_exp_binary_lshift(s, $3);
286 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
288 | STRING RSHIFTEQ parse_exp
290 Expression_ptr s = script_exp_string($1.value, $1.length);
291 Expression_ptr e = script_exp_binary_rshift(s, $3);
292 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
294 | STRING ANDEQ parse_exp
296 Expression_ptr s = script_exp_string($1.value, $1.length);
297 Expression_ptr e = script_exp_binary_bitwise_and(s, $3);
298 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
300 | STRING OREQ parse_exp
302 Expression_ptr s = script_exp_string($1.value, $1.length);
303 Expression_ptr e = script_exp_binary_bitwise_or(s, $3);
304 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
306 | PROVIDE '(' STRING '=' parse_exp ')'
307 { script_set_symbol(closure, $3.value, $3.length, $5, 1, 0); }
308 | PROVIDE_HIDDEN '(' STRING '=' parse_exp ')'
309 { script_set_symbol(closure, $3.value, $3.length, $5, 1, 1); }
312 /* Parse an expression, putting the lexer into the right mode. */
314 { script_push_lex_into_expression_mode(closure); }
317 script_pop_lex_mode(closure);
326 | '-' exp %prec UNARY
327 { $$ = script_exp_unary_minus($2); }
328 | '!' exp %prec UNARY
329 { $$ = script_exp_unary_logical_not($2); }
330 | '~' exp %prec UNARY
331 { $$ = script_exp_unary_bitwise_not($2); }
332 | '+' exp %prec UNARY
335 { $$ = script_exp_binary_mult($1, $3); }
337 { $$ = script_exp_binary_div($1, $3); }
339 { $$ = script_exp_binary_mod($1, $3); }
341 { $$ = script_exp_binary_add($1, $3); }
343 { $$ = script_exp_binary_sub($1, $3); }
345 { $$ = script_exp_binary_lshift($1, $3); }
347 { $$ = script_exp_binary_rshift($1, $3); }
349 { $$ = script_exp_binary_eq($1, $3); }
351 { $$ = script_exp_binary_ne($1, $3); }
353 { $$ = script_exp_binary_le($1, $3); }
355 { $$ = script_exp_binary_ge($1, $3); }
357 { $$ = script_exp_binary_lt($1, $3); }
359 { $$ = script_exp_binary_gt($1, $3); }
361 { $$ = script_exp_binary_bitwise_and($1, $3); }
363 { $$ = script_exp_binary_bitwise_xor($1, $3); }
365 { $$ = script_exp_binary_bitwise_or($1, $3); }
367 { $$ = script_exp_binary_logical_and($1, $3); }
369 { $$ = script_exp_binary_logical_or($1, $3); }
370 | exp '?' exp ':' exp
371 { $$ = script_exp_trinary_cond($1, $3, $5); }
373 { $$ = script_exp_integer($1); }
375 { $$ = script_exp_string($1.value, $1.length); }
376 | MAX_K '(' exp ',' exp ')'
377 { $$ = script_exp_function_max($3, $5); }
378 | MIN_K '(' exp ',' exp ')'
379 { $$ = script_exp_function_min($3, $5); }
380 | DEFINED '(' STRING ')'
381 { $$ = script_exp_function_defined($3.value, $3.length); }
383 { $$ = script_exp_function_sizeof_headers(); }
384 | ALIGNOF '(' STRING ')'
385 { $$ = script_exp_function_alignof($3.value, $3.length); }
386 | SIZEOF '(' STRING ')'
387 { $$ = script_exp_function_sizeof($3.value, $3.length); }
388 | ADDR '(' STRING ')'
389 { $$ = script_exp_function_addr($3.value, $3.length); }
390 | LOADADDR '(' STRING ')'
391 { $$ = script_exp_function_loadaddr($3.value, $3.length); }
392 | ORIGIN '(' STRING ')'
393 { $$ = script_exp_function_origin($3.value, $3.length); }
394 | LENGTH '(' STRING ')'
395 { $$ = script_exp_function_length($3.value, $3.length); }
396 | CONSTANT '(' STRING ')'
397 { $$ = script_exp_function_constant($3.value, $3.length); }
398 | ABSOLUTE '(' exp ')'
399 { $$ = script_exp_function_absolute($3); }
400 | ALIGN_K '(' exp ')'
401 { $$ = script_exp_function_align(script_exp_string(".", 1), $3); }
402 | ALIGN_K '(' exp ',' exp ')'
403 { $$ = script_exp_function_align($3, $5); }
405 { $$ = script_exp_function_align(script_exp_string(".", 1), $3); }
406 | DATA_SEGMENT_ALIGN '(' exp ',' exp ')'
407 { $$ = script_exp_function_data_segment_align($3, $5); }
408 | DATA_SEGMENT_RELRO_END '(' exp ',' exp ')'
409 { $$ = script_exp_function_data_segment_relro_end($3, $5); }
410 | DATA_SEGMENT_END '(' exp ')'
411 { $$ = script_exp_function_data_segment_end($3); }
412 | SEGMENT_START '(' STRING ',' exp ')'
414 $$ = script_exp_function_segment_start($3.value, $3.length, $5);
416 | ASSERT_K '(' exp ',' STRING ')'
417 { $$ = script_exp_function_assert($3, $5.value, $5.length); }
420 /* Handle the --defsym option. */
423 { script_set_symbol(closure, $1.value, $1.length, $3, 0, 0); }
426 /* A version script. */
433 | vers_nodes vers_node
439 script_register_vers_node (closure, NULL, 0, $2, NULL);
441 | STRING '{' vers_tag '}' ';'
443 script_register_vers_node (closure, $1.value, $1.length, $3,
446 | STRING '{' vers_tag '}' verdep ';'
448 script_register_vers_node (closure, $1.value, $1.length, $3, $5);
455 $$ = script_add_vers_depend (closure, NULL, $1.value, $1.length);
459 $$ = script_add_vers_depend (closure, $1, $2.value, $2.length);
465 { $$ = script_new_vers_node (closure, NULL, NULL); }
467 { $$ = script_new_vers_node (closure, $1, NULL); }
468 | GLOBAL ':' vers_defns ';'
469 { $$ = script_new_vers_node (closure, $3, NULL); }
470 | LOCAL ':' vers_defns ';'
471 { $$ = script_new_vers_node (closure, NULL, $3); }
472 | GLOBAL ':' vers_defns ';' LOCAL ':' vers_defns ';'
473 { $$ = script_new_vers_node (closure, $3, $7); }
479 $$ = script_new_vers_pattern (closure, NULL, $1.value,
482 | vers_defns ';' STRING
484 $$ = script_new_vers_pattern (closure, $1, $3.value, $3.length);
486 | /* Push STRING on the language stack. */
488 { version_script_push_lang(closure, $2.value, $2.length); }
489 vers_defns opt_semicolon '}'
492 version_script_pop_lang(closure);
494 | EXTERN // "extern" as a symbol name
496 $$ = script_new_vers_pattern (closure, NULL, "extern",
497 sizeof("extern") - 1);
499 | vers_defns ';' EXTERN
501 $$ = script_new_vers_pattern (closure, $1, "extern",
502 sizeof("extern") - 1);
506 /* Some statements require a terminator, which may be a semicolon or a
513 /* An optional semicolon. */
519 /* An optional comma. */