From Craig Silverstein: Handle quoted strings differently in version
[deliverable/binutils-gdb.git] / gold / yyscript.y
1 /* yyscript.y -- linker script grammer for gold. */
2
3 /* Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <iant@google.com>.
5
6 This file is part of gold.
7
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.
12
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.
17
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. */
22
23 /* This is a bison grammar to parse a subset of the original GNU ld
24 linker script language. */
25
26 %{
27
28 #include "config.h"
29
30 #include <stddef.h>
31 #include <stdint.h>
32
33 #include "script-c.h"
34
35 %}
36
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. */
39
40 %pure-parser
41
42 %parse-param {void* closure}
43 %lex-param {void* closure}
44
45 /* Since we require bison anyhow, we take advantage of it. */
46
47 %error-verbose
48
49 /* The values associated with tokens. */
50
51 %union {
52 /* A string. */
53 struct Parser_string string;
54 /* A number. */
55 uint64_t integer;
56 /* An expression. */
57 Expression_ptr expr;
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;
62 }
63
64 /* Operators, including a precedence table for expressions. */
65
66 %right PLUSEQ MINUSEQ MULTEQ DIVEQ '=' LSHIFTEQ RSHIFTEQ ANDEQ OREQ
67 %right '?' ':'
68 %left OROR
69 %left ANDAND
70 %left '|'
71 %left '^'
72 %left '&'
73 %left EQ NE
74 %left '<' '>' LE GE
75 %left LSHIFT RSHIFT
76 %left '+' '-'
77 %left '*' '/' '%'
78
79 /* A fake operator used to indicate unary operator precedence. */
80 %right UNARY
81
82 /* Constants. */
83
84 %token <string> STRING
85 %token <string> QUOTED_STRING
86 %token <integer> INTEGER
87
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. */
93
94 %token ABSOLUTE
95 %token ADDR
96 %token ALIGN_K /* ALIGN */
97 %token ALIGNOF
98 %token ASSERT_K /* ASSERT */
99 %token AS_NEEDED
100 %token AT
101 %token BIND
102 %token BLOCK
103 %token BYTE
104 %token CONSTANT
105 %token CONSTRUCTORS
106 %token COPY
107 %token CREATE_OBJECT_SYMBOLS
108 %token DATA_SEGMENT_ALIGN
109 %token DATA_SEGMENT_END
110 %token DATA_SEGMENT_RELRO_END
111 %token DEFINED
112 %token DSECT
113 %token ENTRY
114 %token EXCLUDE_FILE
115 %token EXTERN
116 %token FILL
117 %token FLOAT
118 %token FORCE_COMMON_ALLOCATION
119 %token GLOBAL /* global */
120 %token GROUP
121 %token HLL
122 %token INCLUDE
123 %token INFO
124 %token INHIBIT_COMMON_ALLOCATION
125 %token INPUT
126 %token KEEP
127 %token LENGTH /* LENGTH, l, len */
128 %token LOADADDR
129 %token LOCAL /* local */
130 %token LONG
131 %token MAP
132 %token MAX_K /* MAX */
133 %token MEMORY
134 %token MIN_K /* MIN */
135 %token NEXT
136 %token NOCROSSREFS
137 %token NOFLOAT
138 %token NOLOAD
139 %token ONLY_IF_RO
140 %token ONLY_IF_RW
141 %token ORIGIN /* ORIGIN, o, org */
142 %token OUTPUT
143 %token OUTPUT_ARCH
144 %token OUTPUT_FORMAT
145 %token OVERLAY
146 %token PHDRS
147 %token PROVIDE
148 %token PROVIDE_HIDDEN
149 %token QUAD
150 %token SEARCH_DIR
151 %token SECTIONS
152 %token SEGMENT_START
153 %token SHORT
154 %token SIZEOF
155 %token SIZEOF_HEADERS /* SIZEOF_HEADERS, sizeof_headers */
156 %token SORT_BY_ALIGNMENT
157 %token SORT_BY_NAME
158 %token SPECIAL
159 %token SQUAD
160 %token STARTUP
161 %token SUBALIGN
162 %token SYSLIB
163 %token TARGET_K /* TARGET */
164 %token TRUNCATE
165 %token VERSIONK /* VERSION */
166
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. */
171
172 %token OPTION
173
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
178 parser. */
179 %token PARSING_LINKER_SCRIPT
180 %token PARSING_VERSION_SCRIPT
181 %token PARSING_DEFSYM
182
183 /* Non-terminal types, where needed. */
184
185 %type <expr> parse_exp exp
186 %type <versyms> vers_defns
187 %type <versnode> vers_tag
188 %type <deplist> verdep
189 %type <string> string
190
191 %%
192
193 /* Read the special token to see what to read next. */
194 top:
195 PARSING_LINKER_SCRIPT linker_script
196 | PARSING_VERSION_SCRIPT version_script
197 | PARSING_DEFSYM defsym_expr
198 ;
199
200 /* A file contains a list of commands. */
201 linker_script:
202 linker_script file_cmd
203 | /* empty */
204 ;
205
206 /* A command which may appear at top level of a linker script. */
207 file_cmd:
208 GROUP
209 { script_start_group(closure); }
210 '(' input_list ')'
211 { script_end_group(closure); }
212 | OPTION '(' string ')'
213 { script_parse_option(closure, $3.value, $3.length); }
214 | VERSIONK '{'
215 { script_push_lex_into_version_mode(closure); }
216 version_script '}'
217 { script_pop_lex_mode(closure); }
218 | file_or_sections_cmd
219 | ignore_cmd
220 ;
221
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. */
226 ignore_cmd:
227 OUTPUT_FORMAT '(' string ')'
228 | OUTPUT_FORMAT '(' string ',' string ',' string ')'
229 | OUTPUT_ARCH '(' string ')'
230 ;
231
232 /* A list of input file names. */
233 input_list:
234 input_list_element
235 | input_list opt_comma input_list_element
236 ;
237
238 /* An input file name. */
239 input_list_element:
240 string
241 { script_add_file(closure, $1.value, $1.length); }
242 | AS_NEEDED
243 { script_start_as_needed(closure); }
244 '(' input_list ')'
245 { script_end_as_needed(closure); }
246 ;
247
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:
251 ENTRY '(' string ')'
252 { script_set_entry(closure, $3.value, $3.length); }
253 | assignment end
254 ;
255
256 /* Set a symbol to a value. */
257 assignment:
258 string '=' parse_exp
259 { script_set_symbol(closure, $1.value, $1.length, $3, 0, 0); }
260 | string PLUSEQ parse_exp
261 {
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);
265 }
266 | string MINUSEQ parse_exp
267 {
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);
271 }
272 | string MULTEQ parse_exp
273 {
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);
277 }
278 | string DIVEQ parse_exp
279 {
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);
283 }
284 | string LSHIFTEQ parse_exp
285 {
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);
289 }
290 | string RSHIFTEQ parse_exp
291 {
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);
295 }
296 | string ANDEQ parse_exp
297 {
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);
301 }
302 | string OREQ parse_exp
303 {
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);
307 }
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); }
312 ;
313
314 /* Parse an expression, putting the lexer into the right mode. */
315 parse_exp:
316 { script_push_lex_into_expression_mode(closure); }
317 exp
318 {
319 script_pop_lex_mode(closure);
320 $$ = $2;
321 }
322 ;
323
324 /* An expression. */
325 exp:
326 '(' exp ')'
327 { $$ = $2; }
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
335 { $$ = $2; }
336 | exp '*' exp
337 { $$ = script_exp_binary_mult($1, $3); }
338 | exp '/' exp
339 { $$ = script_exp_binary_div($1, $3); }
340 | exp '%' exp
341 { $$ = script_exp_binary_mod($1, $3); }
342 | exp '+' exp
343 { $$ = script_exp_binary_add($1, $3); }
344 | exp '-' exp
345 { $$ = script_exp_binary_sub($1, $3); }
346 | exp LSHIFT exp
347 { $$ = script_exp_binary_lshift($1, $3); }
348 | exp RSHIFT exp
349 { $$ = script_exp_binary_rshift($1, $3); }
350 | exp EQ exp
351 { $$ = script_exp_binary_eq($1, $3); }
352 | exp NE exp
353 { $$ = script_exp_binary_ne($1, $3); }
354 | exp LE exp
355 { $$ = script_exp_binary_le($1, $3); }
356 | exp GE exp
357 { $$ = script_exp_binary_ge($1, $3); }
358 | exp '<' exp
359 { $$ = script_exp_binary_lt($1, $3); }
360 | exp '>' exp
361 { $$ = script_exp_binary_gt($1, $3); }
362 | exp '&' exp
363 { $$ = script_exp_binary_bitwise_and($1, $3); }
364 | exp '^' exp
365 { $$ = script_exp_binary_bitwise_xor($1, $3); }
366 | exp '|' exp
367 { $$ = script_exp_binary_bitwise_or($1, $3); }
368 | exp ANDAND exp
369 { $$ = script_exp_binary_logical_and($1, $3); }
370 | exp OROR exp
371 { $$ = script_exp_binary_logical_or($1, $3); }
372 | exp '?' exp ':' exp
373 { $$ = script_exp_trinary_cond($1, $3, $5); }
374 | INTEGER
375 { $$ = script_exp_integer($1); }
376 | STRING
377 { $$ = script_exp_string($1.value, $1.length); }
378 | QUOTED_STRING
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); }
386 | SIZEOF_HEADERS
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); }
408 | BLOCK '(' exp ')'
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 ')'
417 {
418 $$ = script_exp_function_segment_start($3.value, $3.length, $5);
419 }
420 | ASSERT_K '(' exp ',' string ')'
421 { $$ = script_exp_function_assert($3, $5.value, $5.length); }
422 ;
423
424 /* Handle the --defsym option. */
425 defsym_expr:
426 string '=' parse_exp
427 { script_set_symbol(closure, $1.value, $1.length, $3, 0, 0); }
428 ;
429
430 /* A version script. */
431 version_script:
432 vers_nodes
433 ;
434
435 vers_nodes:
436 vers_node
437 | vers_nodes vers_node
438 ;
439
440 vers_node:
441 '{' vers_tag '}' ';'
442 {
443 script_register_vers_node (closure, NULL, 0, $2, NULL);
444 }
445 | string '{' vers_tag '}' ';'
446 {
447 script_register_vers_node (closure, $1.value, $1.length, $3,
448 NULL);
449 }
450 | string '{' vers_tag '}' verdep ';'
451 {
452 script_register_vers_node (closure, $1.value, $1.length, $3, $5);
453 }
454 ;
455
456 verdep:
457 string
458 {
459 $$ = script_add_vers_depend (closure, NULL, $1.value, $1.length);
460 }
461 | verdep string
462 {
463 $$ = script_add_vers_depend (closure, $1, $2.value, $2.length);
464 }
465 ;
466
467 vers_tag:
468 /* empty */
469 { $$ = script_new_vers_node (closure, NULL, NULL); }
470 | vers_defns ';'
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); }
478 ;
479
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. */
484 vers_defns:
485 STRING
486 {
487 $$ = script_new_vers_pattern (closure, NULL, $1.value,
488 $1.length, 0);
489 }
490 | QUOTED_STRING
491 {
492 $$ = script_new_vers_pattern (closure, NULL, $1.value,
493 $1.length, 1);
494 }
495 | vers_defns ';' STRING
496 {
497 $$ = script_new_vers_pattern (closure, $1, $3.value,
498 $3.length, 0);
499 }
500 | vers_defns ';' QUOTED_STRING
501 {
502 $$ = script_new_vers_pattern (closure, $1, $3.value,
503 $3.length, 1);
504 }
505 | /* Push string on the language stack. */
506 EXTERN string '{'
507 { version_script_push_lang (closure, $2.value, $2.length); }
508 vers_defns opt_semicolon '}'
509 {
510 $$ = $5;
511 version_script_pop_lang(closure);
512 }
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 '}'
519 {
520 $$ = script_merge_expressions ($1, $7);
521 version_script_pop_lang(closure);
522 }
523 | EXTERN // "extern" as a symbol name
524 {
525 $$ = script_new_vers_pattern (closure, NULL, "extern",
526 sizeof("extern") - 1, 1);
527 }
528 | vers_defns ';' EXTERN
529 {
530 $$ = script_new_vers_pattern (closure, $1, "extern",
531 sizeof("extern") - 1, 1);
532 }
533 ;
534
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. */
537 string:
538 STRING
539 { $$ = $1; }
540 | QUOTED_STRING
541 { $$ = $1; }
542 ;
543
544 /* Some statements require a terminator, which may be a semicolon or a
545 comma. */
546 end:
547 ';'
548 | ','
549 ;
550
551 /* An optional semicolon. */
552 opt_semicolon:
553 ';'
554 | /* empty */
555 ;
556
557 /* An optional comma. */
558 opt_comma:
559 ','
560 | /* empty */
561 ;
562
563 %%
This page took 0.041088 seconds and 5 git commands to generate.