Commit | Line | Data |
---|---|---|
dbe717ef ILT |
1 | /* yyscript.y -- linker script grammer for gold. */ |
2 | ||
e5756efb | 3 | /* Copyright 2006, 2007, 2008 Free Software Foundation, Inc. |
6cb15b7f ILT |
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 | ||
dbe717ef ILT |
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 { | |
e5756efb ILT |
52 | /* A string. */ |
53 | struct Parser_string string; | |
54 | /* A number. */ | |
55 | uint64_t integer; | |
56 | /* An expression. */ | |
57 | Expression_ptr expr; | |
09124467 ILT |
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; | |
dbe717ef ILT |
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 | ||
e5756efb ILT |
79 | /* A fake operator used to indicate unary operator precedence. */ |
80 | %right UNARY | |
81 | ||
dbe717ef ILT |
82 | /* Constants. */ |
83 | ||
84 | %token <string> STRING | |
85 | %token <integer> INTEGER | |
86 | ||
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. */ | |
92 | ||
93 | %token ABSOLUTE | |
94 | %token ADDR | |
95 | %token ALIGN_K /* ALIGN */ | |
e5756efb | 96 | %token ALIGNOF |
dbe717ef ILT |
97 | %token ASSERT_K /* ASSERT */ |
98 | %token AS_NEEDED | |
99 | %token AT | |
100 | %token BIND | |
101 | %token BLOCK | |
102 | %token BYTE | |
103 | %token CONSTANT | |
104 | %token CONSTRUCTORS | |
105 | %token COPY | |
106 | %token CREATE_OBJECT_SYMBOLS | |
107 | %token DATA_SEGMENT_ALIGN | |
108 | %token DATA_SEGMENT_END | |
109 | %token DATA_SEGMENT_RELRO_END | |
110 | %token DEFINED | |
111 | %token DSECT | |
112 | %token ENTRY | |
113 | %token EXCLUDE_FILE | |
114 | %token EXTERN | |
115 | %token FILL | |
116 | %token FLOAT | |
117 | %token FORCE_COMMON_ALLOCATION | |
118 | %token GLOBAL /* global */ | |
119 | %token GROUP | |
120 | %token HLL | |
121 | %token INCLUDE | |
122 | %token INFO | |
123 | %token INHIBIT_COMMON_ALLOCATION | |
124 | %token INPUT | |
125 | %token KEEP | |
126 | %token LENGTH /* LENGTH, l, len */ | |
127 | %token LOADADDR | |
128 | %token LOCAL /* local */ | |
129 | %token LONG | |
130 | %token MAP | |
131 | %token MAX_K /* MAX */ | |
132 | %token MEMORY | |
133 | %token MIN_K /* MIN */ | |
134 | %token NEXT | |
135 | %token NOCROSSREFS | |
136 | %token NOFLOAT | |
137 | %token NOLOAD | |
138 | %token ONLY_IF_RO | |
139 | %token ONLY_IF_RW | |
140 | %token ORIGIN /* ORIGIN, o, org */ | |
141 | %token OUTPUT | |
142 | %token OUTPUT_ARCH | |
143 | %token OUTPUT_FORMAT | |
144 | %token OVERLAY | |
145 | %token PHDRS | |
146 | %token PROVIDE | |
147 | %token PROVIDE_HIDDEN | |
148 | %token QUAD | |
149 | %token SEARCH_DIR | |
150 | %token SECTIONS | |
151 | %token SEGMENT_START | |
152 | %token SHORT | |
153 | %token SIZEOF | |
154 | %token SIZEOF_HEADERS /* SIZEOF_HEADERS, sizeof_headers */ | |
155 | %token SORT_BY_ALIGNMENT | |
156 | %token SORT_BY_NAME | |
157 | %token SPECIAL | |
158 | %token SQUAD | |
159 | %token STARTUP | |
160 | %token SUBALIGN | |
161 | %token SYSLIB | |
162 | %token TARGET_K /* TARGET */ | |
163 | %token TRUNCATE | |
164 | %token VERSIONK /* VERSION */ | |
165 | ||
195e7dc6 ILT |
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. */ | |
170 | ||
171 | %token OPTION | |
172 | ||
e5756efb ILT |
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 | |
177 | parser. */ | |
178 | %token PARSING_LINKER_SCRIPT | |
179 | %token PARSING_VERSION_SCRIPT | |
180 | %token PARSING_DEFSYM | |
181 | ||
182 | /* Non-terminal types, where needed. */ | |
183 | ||
184 | %type <expr> parse_exp exp | |
09124467 ILT |
185 | %type <versyms> vers_defns |
186 | %type <versnode> vers_tag | |
187 | %type <deplist> verdep | |
e5756efb | 188 | |
dbe717ef ILT |
189 | %% |
190 | ||
e5756efb ILT |
191 | /* Read the special token to see what to read next. */ |
192 | top: | |
193 | PARSING_LINKER_SCRIPT linker_script | |
194 | | PARSING_VERSION_SCRIPT version_script | |
195 | | PARSING_DEFSYM defsym_expr | |
196 | ; | |
197 | ||
d391083d | 198 | /* A file contains a list of commands. */ |
e5756efb ILT |
199 | linker_script: |
200 | linker_script file_cmd | |
dbe717ef ILT |
201 | | /* empty */ |
202 | ; | |
203 | ||
d391083d | 204 | /* A command which may appear at top level of a linker script. */ |
dbe717ef | 205 | file_cmd: |
2dd3e587 | 206 | GROUP |
dbe717ef ILT |
207 | { script_start_group(closure); } |
208 | '(' input_list ')' | |
209 | { script_end_group(closure); } | |
195e7dc6 | 210 | | OPTION '(' STRING ')' |
e5756efb | 211 | { script_parse_option(closure, $3.value, $3.length); } |
09124467 ILT |
212 | | VERSIONK '{' |
213 | { script_push_lex_into_version_mode(closure); } | |
214 | version_script '}' | |
215 | { script_pop_lex_mode(closure); } | |
d391083d | 216 | | file_or_sections_cmd |
2dd3e587 ILT |
217 | | ignore_cmd |
218 | ; | |
219 | ||
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. */ | |
224 | ignore_cmd: | |
225 | OUTPUT_FORMAT '(' STRING ')' | |
226 | | OUTPUT_FORMAT '(' STRING ',' STRING ',' STRING ')' | |
227 | | OUTPUT_ARCH '(' STRING ')' | |
dbe717ef ILT |
228 | ; |
229 | ||
d391083d | 230 | /* A list of input file names. */ |
dbe717ef ILT |
231 | input_list: |
232 | input_list_element | |
233 | | input_list opt_comma input_list_element | |
234 | ; | |
235 | ||
d391083d | 236 | /* An input file name. */ |
dbe717ef ILT |
237 | input_list_element: |
238 | STRING | |
e5756efb | 239 | { script_add_file(closure, $1.value, $1.length); } |
dbe717ef ILT |
240 | | AS_NEEDED |
241 | { script_start_as_needed(closure); } | |
242 | '(' input_list ')' | |
243 | { script_end_as_needed(closure); } | |
244 | ; | |
245 | ||
d391083d ILT |
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: | |
249 | ENTRY '(' STRING ')' | |
e5756efb ILT |
250 | { script_set_entry(closure, $3.value, $3.length); } |
251 | | assignment end | |
252 | ; | |
253 | ||
254 | /* Set a symbol to a value. */ | |
255 | assignment: | |
256 | STRING '=' parse_exp | |
257 | { script_set_symbol(closure, $1.value, $1.length, $3, 0, 0); } | |
258 | | STRING PLUSEQ parse_exp | |
259 | { | |
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); | |
263 | } | |
264 | | STRING MINUSEQ parse_exp | |
265 | { | |
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); | |
269 | } | |
270 | | STRING MULTEQ parse_exp | |
271 | { | |
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); | |
275 | } | |
276 | | STRING DIVEQ parse_exp | |
277 | { | |
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); | |
281 | } | |
282 | | STRING LSHIFTEQ parse_exp | |
283 | { | |
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); | |
287 | } | |
288 | | STRING RSHIFTEQ parse_exp | |
289 | { | |
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); | |
293 | } | |
294 | | STRING ANDEQ parse_exp | |
295 | { | |
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); | |
299 | } | |
300 | | STRING OREQ parse_exp | |
301 | { | |
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); | |
305 | } | |
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); } | |
310 | ; | |
311 | ||
312 | /* Parse an expression, putting the lexer into the right mode. */ | |
313 | parse_exp: | |
314 | { script_push_lex_into_expression_mode(closure); } | |
315 | exp | |
316 | { | |
317 | script_pop_lex_mode(closure); | |
318 | $$ = $2; | |
319 | } | |
320 | ; | |
321 | ||
322 | /* An expression. */ | |
323 | exp: | |
324 | '(' exp ')' | |
325 | { $$ = $2; } | |
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 | |
333 | { $$ = $2; } | |
334 | | exp '*' exp | |
335 | { $$ = script_exp_binary_mult($1, $3); } | |
336 | | exp '/' exp | |
337 | { $$ = script_exp_binary_div($1, $3); } | |
338 | | exp '%' exp | |
339 | { $$ = script_exp_binary_mod($1, $3); } | |
340 | | exp '+' exp | |
341 | { $$ = script_exp_binary_add($1, $3); } | |
342 | | exp '-' exp | |
343 | { $$ = script_exp_binary_sub($1, $3); } | |
344 | | exp LSHIFT exp | |
345 | { $$ = script_exp_binary_lshift($1, $3); } | |
346 | | exp RSHIFT exp | |
347 | { $$ = script_exp_binary_rshift($1, $3); } | |
348 | | exp EQ exp | |
349 | { $$ = script_exp_binary_eq($1, $3); } | |
350 | | exp NE exp | |
351 | { $$ = script_exp_binary_ne($1, $3); } | |
352 | | exp LE exp | |
353 | { $$ = script_exp_binary_le($1, $3); } | |
354 | | exp GE exp | |
355 | { $$ = script_exp_binary_ge($1, $3); } | |
356 | | exp '<' exp | |
357 | { $$ = script_exp_binary_lt($1, $3); } | |
358 | | exp '>' exp | |
359 | { $$ = script_exp_binary_gt($1, $3); } | |
360 | | exp '&' exp | |
361 | { $$ = script_exp_binary_bitwise_and($1, $3); } | |
362 | | exp '^' exp | |
363 | { $$ = script_exp_binary_bitwise_xor($1, $3); } | |
364 | | exp '|' exp | |
365 | { $$ = script_exp_binary_bitwise_or($1, $3); } | |
366 | | exp ANDAND exp | |
367 | { $$ = script_exp_binary_logical_and($1, $3); } | |
368 | | exp OROR exp | |
369 | { $$ = script_exp_binary_logical_or($1, $3); } | |
370 | | exp '?' exp ':' exp | |
371 | { $$ = script_exp_trinary_cond($1, $3, $5); } | |
372 | | INTEGER | |
373 | { $$ = script_exp_integer($1); } | |
374 | | STRING | |
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); } | |
382 | | SIZEOF_HEADERS | |
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); } | |
404 | | BLOCK '(' exp ')' | |
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 ')' | |
413 | { | |
414 | $$ = script_exp_function_segment_start($3.value, $3.length, $5); | |
415 | } | |
416 | | ASSERT_K '(' exp ',' STRING ')' | |
417 | { $$ = script_exp_function_assert($3, $5.value, $5.length); } | |
418 | ; | |
419 | ||
420 | /* Handle the --defsym option. */ | |
421 | defsym_expr: | |
422 | STRING '=' parse_exp | |
423 | { script_set_symbol(closure, $1.value, $1.length, $3, 0, 0); } | |
424 | ; | |
425 | ||
09124467 | 426 | /* A version script. */ |
e5756efb | 427 | version_script: |
09124467 ILT |
428 | vers_nodes |
429 | ; | |
430 | ||
431 | vers_nodes: | |
432 | vers_node | |
433 | | vers_nodes vers_node | |
434 | ; | |
435 | ||
436 | vers_node: | |
437 | '{' vers_tag '}' ';' | |
438 | { | |
439 | script_register_vers_node (closure, NULL, 0, $2, NULL); | |
440 | } | |
441 | | STRING '{' vers_tag '}' ';' | |
442 | { | |
443 | script_register_vers_node (closure, $1.value, $1.length, $3, | |
444 | NULL); | |
445 | } | |
446 | | STRING '{' vers_tag '}' verdep ';' | |
447 | { | |
448 | script_register_vers_node (closure, $1.value, $1.length, $3, $5); | |
449 | } | |
450 | ; | |
451 | ||
452 | verdep: | |
453 | STRING | |
454 | { | |
455 | $$ = script_add_vers_depend (closure, NULL, $1.value, $1.length); | |
456 | } | |
457 | | verdep STRING | |
458 | { | |
459 | $$ = script_add_vers_depend (closure, $1, $2.value, $2.length); | |
460 | } | |
461 | ; | |
462 | ||
463 | vers_tag: | |
464 | /* empty */ | |
465 | { $$ = script_new_vers_node (closure, NULL, NULL); } | |
466 | | vers_defns ';' | |
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); } | |
474 | ; | |
475 | ||
476 | vers_defns: | |
477 | STRING | |
478 | { | |
479 | $$ = script_new_vers_pattern (closure, NULL, $1.value, | |
480 | $1.length); | |
481 | } | |
482 | | vers_defns ';' STRING | |
483 | { | |
484 | $$ = script_new_vers_pattern (closure, $1, $3.value, $3.length); | |
485 | } | |
486 | | /* Push STRING on the language stack. */ | |
487 | EXTERN STRING '{' | |
488 | { version_script_push_lang(closure, $2.value, $2.length); } | |
489 | vers_defns opt_semicolon '}' | |
490 | { | |
491 | $$ = $5; | |
492 | version_script_pop_lang(closure); | |
493 | } | |
494 | | EXTERN // "extern" as a symbol name | |
495 | { | |
496 | $$ = script_new_vers_pattern (closure, NULL, "extern", | |
497 | sizeof("extern") - 1); | |
498 | } | |
499 | | vers_defns ';' EXTERN | |
500 | { | |
501 | $$ = script_new_vers_pattern (closure, $1, "extern", | |
502 | sizeof("extern") - 1); | |
503 | } | |
e5756efb ILT |
504 | ; |
505 | ||
506 | /* Some statements require a terminator, which may be a semicolon or a | |
507 | comma. */ | |
508 | end: | |
509 | ';' | |
510 | | ',' | |
d391083d ILT |
511 | ; |
512 | ||
09124467 ILT |
513 | /* An optional semicolon. */ |
514 | opt_semicolon: | |
515 | ';' | |
516 | | /* empty */ | |
517 | ; | |
518 | ||
d391083d | 519 | /* An optional comma. */ |
dbe717ef ILT |
520 | opt_comma: |
521 | ',' | |
522 | | /* empty */ | |
523 | ; | |
524 | ||
525 | %% |