]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/yyscript.y
* script-sections.cc (Script_sections::create_segments): Don't put
[thirdparty/binutils-gdb.git] / gold / yyscript.y
CommitLineData
bd52eafb 1/* yyscript.y -- linker script grammar for gold. */
dbe717ef 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>
494e05f4 32#include <stdlib.h>
1c4f3631 33#include <string.h>
dbe717ef
ILT
34
35#include "script-c.h"
36
37%}
38
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. */
41
42%pure-parser
43
44%parse-param {void* closure}
45%lex-param {void* closure}
46
47/* Since we require bison anyhow, we take advantage of it. */
48
49%error-verbose
50
51/* The values associated with tokens. */
52
53%union {
e5756efb
ILT
54 /* A string. */
55 struct Parser_string string;
56 /* A number. */
57 uint64_t integer;
58 /* An expression. */
59 Expression_ptr expr;
494e05f4
ILT
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;
3802b2dd
ILT
64 /* A section constraint. */
65 enum Section_constraint constraint;
494e05f4
ILT
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;
1c4f3631
ILT
74 /* Information for a program header. */
75 struct Phdr_info phdr_info;
494e05f4 76 /* Used for version scripts and within VERSION {}. */
09124467
ILT
77 struct Version_dependency_list* deplist;
78 struct Version_expression_list* versyms;
79 struct Version_tree* versnode;
dbe717ef
ILT
80}
81
82/* Operators, including a precedence table for expressions. */
83
84%right PLUSEQ MINUSEQ MULTEQ DIVEQ '=' LSHIFTEQ RSHIFTEQ ANDEQ OREQ
85%right '?' ':'
86%left OROR
87%left ANDAND
88%left '|'
89%left '^'
90%left '&'
91%left EQ NE
92%left '<' '>' LE GE
93%left LSHIFT RSHIFT
94%left '+' '-'
95%left '*' '/' '%'
96
e5756efb
ILT
97/* A fake operator used to indicate unary operator precedence. */
98%right UNARY
99
dbe717ef
ILT
100/* Constants. */
101
102%token <string> STRING
10600224 103%token <string> QUOTED_STRING
dbe717ef
ILT
104%token <integer> INTEGER
105
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. */
111
112%token ABSOLUTE
113%token ADDR
114%token ALIGN_K /* ALIGN */
e5756efb 115%token ALIGNOF
dbe717ef
ILT
116%token ASSERT_K /* ASSERT */
117%token AS_NEEDED
118%token AT
119%token BIND
120%token BLOCK
121%token BYTE
122%token CONSTANT
123%token CONSTRUCTORS
dbe717ef
ILT
124%token CREATE_OBJECT_SYMBOLS
125%token DATA_SEGMENT_ALIGN
126%token DATA_SEGMENT_END
127%token DATA_SEGMENT_RELRO_END
128%token DEFINED
dbe717ef
ILT
129%token ENTRY
130%token EXCLUDE_FILE
131%token EXTERN
132%token FILL
133%token FLOAT
134%token FORCE_COMMON_ALLOCATION
135%token GLOBAL /* global */
136%token GROUP
137%token HLL
138%token INCLUDE
dbe717ef
ILT
139%token INHIBIT_COMMON_ALLOCATION
140%token INPUT
141%token KEEP
142%token LENGTH /* LENGTH, l, len */
143%token LOADADDR
144%token LOCAL /* local */
145%token LONG
146%token MAP
147%token MAX_K /* MAX */
148%token MEMORY
149%token MIN_K /* MIN */
150%token NEXT
151%token NOCROSSREFS
152%token NOFLOAT
dbe717ef
ILT
153%token ONLY_IF_RO
154%token ONLY_IF_RW
155%token ORIGIN /* ORIGIN, o, org */
156%token OUTPUT
157%token OUTPUT_ARCH
158%token OUTPUT_FORMAT
159%token OVERLAY
160%token PHDRS
161%token PROVIDE
162%token PROVIDE_HIDDEN
163%token QUAD
164%token SEARCH_DIR
165%token SECTIONS
166%token SEGMENT_START
167%token SHORT
168%token SIZEOF
169%token SIZEOF_HEADERS /* SIZEOF_HEADERS, sizeof_headers */
170%token SORT_BY_ALIGNMENT
171%token SORT_BY_NAME
172%token SPECIAL
173%token SQUAD
174%token STARTUP
175%token SUBALIGN
176%token SYSLIB
177%token TARGET_K /* TARGET */
178%token TRUNCATE
179%token VERSIONK /* VERSION */
180
195e7dc6
ILT
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. */
185
186%token OPTION
187
e5756efb
ILT
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
192 parser. */
193%token PARSING_LINKER_SCRIPT
194%token PARSING_VERSION_SCRIPT
195%token PARSING_DEFSYM
c82fbeee 196%token PARSING_DYNAMIC_LIST
e5756efb
ILT
197
198/* Non-terminal types, where needed. */
199
494e05f4
ILT
200%type <expr> parse_exp exp opt_address_and_section_type
201%type <expr> opt_at opt_align opt_subalign opt_fill
202%type <output_section_header> section_header
203%type <output_section_trailer> section_trailer
3802b2dd 204%type <constraint> opt_constraint
1c4f3631 205%type <string_list> opt_phdr
494e05f4
ILT
206%type <integer> data_length
207%type <input_section_spec> input_section_no_keep
208%type <wildcard_sections> wildcard_sections
209%type <wildcard_section> wildcard_file wildcard_section
210%type <string_list> exclude_names
211%type <string> wildcard_name
1c4f3631
ILT
212%type <integer> phdr_type
213%type <phdr_info> phdr_info
09124467
ILT
214%type <versyms> vers_defns
215%type <versnode> vers_tag
216%type <deplist> verdep
10600224 217%type <string> string
e5756efb 218
dbe717ef
ILT
219%%
220
e5756efb
ILT
221/* Read the special token to see what to read next. */
222top:
223 PARSING_LINKER_SCRIPT linker_script
224 | PARSING_VERSION_SCRIPT version_script
225 | PARSING_DEFSYM defsym_expr
c82fbeee 226 | PARSING_DYNAMIC_LIST dynamic_list_expr
e5756efb
ILT
227 ;
228
d391083d 229/* A file contains a list of commands. */
e5756efb
ILT
230linker_script:
231 linker_script file_cmd
dbe717ef
ILT
232 | /* empty */
233 ;
234
d391083d 235/* A command which may appear at top level of a linker script. */
dbe717ef 236file_cmd:
afc06bb8
ILT
237 EXTERN '(' extern_name_list ')'
238 | FORCE_COMMON_ALLOCATION
0dfbdef4
ILT
239 { script_set_common_allocation(closure, 1); }
240 | GROUP
dbe717ef
ILT
241 { script_start_group(closure); }
242 '(' input_list ')'
243 { script_end_group(closure); }
0dfbdef4
ILT
244 | INHIBIT_COMMON_ALLOCATION
245 { script_set_common_allocation(closure, 0); }
fbc558e1 246 | INPUT '(' input_list ')'
10600224 247 | OPTION '(' string ')'
e5756efb 248 { script_parse_option(closure, $3.value, $3.length); }
15f8229b
ILT
249 | OUTPUT_FORMAT '(' string ')'
250 {
251 if (!script_check_output_format(closure, $3.value, $3.length,
252 NULL, 0, NULL, 0))
253 YYABORT;
254 }
255 | OUTPUT_FORMAT '(' string ',' string ',' string ')'
256 {
257 if (!script_check_output_format(closure, $3.value, $3.length,
258 $5.value, $5.length,
259 $7.value, $7.length))
260 YYABORT;
261 }
1c4f3631 262 | PHDRS '{' phdrs_defs '}'
3802b2dd
ILT
263 | SEARCH_DIR '(' string ')'
264 { script_add_search_dir(closure, $3.value, $3.length); }
494e05f4
ILT
265 | SECTIONS '{'
266 { script_start_sections(closure); }
267 sections_block '}'
268 { script_finish_sections(closure); }
09124467
ILT
269 | VERSIONK '{'
270 { script_push_lex_into_version_mode(closure); }
271 version_script '}'
272 { script_pop_lex_mode(closure); }
d391083d 273 | file_or_sections_cmd
2dd3e587 274 | ignore_cmd
3802b2dd 275 | ';'
2dd3e587
ILT
276 ;
277
278/* Top level commands which we ignore. The GNU linker uses these to
279 select the output format, but we don't offer a choice. Ignoring
280 these is more-or-less OK since most scripts simply explicitly
281 choose the default. */
282ignore_cmd:
15f8229b 283 OUTPUT_ARCH '(' string ')'
dbe717ef
ILT
284 ;
285
afc06bb8
ILT
286/* A list of external undefined symbols. We put the lexer into
287 expression mode so that commas separate names; this is what the GNU
288 linker does. */
289
290extern_name_list:
291 { script_push_lex_into_expression_mode(closure); }
292 extern_name_list_body
293 { script_pop_lex_mode(closure); }
294 ;
295
296extern_name_list_body:
297 string
298 { script_add_extern(closure, $1.value, $1.length); }
299 | extern_name_list_body string
300 { script_add_extern(closure, $2.value, $2.length); }
301 | extern_name_list_body ',' string
302 { script_add_extern(closure, $3.value, $3.length); }
303 ;
304
d391083d 305/* A list of input file names. */
dbe717ef
ILT
306input_list:
307 input_list_element
308 | input_list opt_comma input_list_element
309 ;
310
d391083d 311/* An input file name. */
dbe717ef 312input_list_element:
10600224 313 string
e5756efb 314 { script_add_file(closure, $1.value, $1.length); }
dbe717ef
ILT
315 | AS_NEEDED
316 { script_start_as_needed(closure); }
317 '(' input_list ')'
318 { script_end_as_needed(closure); }
319 ;
320
494e05f4
ILT
321/* Commands in a SECTIONS block. */
322sections_block:
323 sections_block section_block_cmd
324 | /* empty */
325 ;
326
327/* A command which may appear within a SECTIONS block. */
328section_block_cmd:
329 file_or_sections_cmd
e4967d85 330 | string section_header
494e05f4
ILT
331 { script_start_output_section(closure, $1.value, $1.length, &$2); }
332 '{' section_cmds '}' section_trailer
333 { script_finish_output_section(closure, &$7); }
334 ;
335
336/* The header of an output section in a SECTIONS block--everything
337 after the name. */
338section_header:
339 { script_push_lex_into_expression_mode(closure); }
340 opt_address_and_section_type opt_at opt_align opt_subalign
3802b2dd
ILT
341 { script_pop_lex_mode(closure); }
342 opt_constraint
494e05f4
ILT
343 {
344 $$.address = $2;
345 $$.load_address = $3;
346 $$.align = $4;
347 $$.subalign = $5;
3802b2dd 348 $$.constraint = $7;
494e05f4
ILT
349 }
350 ;
351
352/* The optional address followed by the optional section type. This
353 is a separate nonterminal to avoid a shift/reduce conflict on
354 '(' in section_header. */
355
356opt_address_and_section_type:
357 ':'
358 { $$ = NULL; }
359 | '(' ')' ':'
360 { $$ = NULL; }
361 | exp ':'
362 { $$ = $1; }
363 | exp '(' ')' ':'
364 { $$ = $1; }
e4967d85 365 | exp '(' string ')' ':'
494e05f4
ILT
366 {
367 yyerror(closure, "section types are not supported");
368 $$ = $1;
369 }
370 ;
371
372/* The address at which an output section should be loaded. */
373opt_at:
374 /* empty */
375 { $$ = NULL; }
376 | AT '(' exp ')'
377 { $$ = $3; }
378 ;
379
380/* The alignment of an output section. */
381opt_align:
382 /* empty */
383 { $$ = NULL; }
384 | ALIGN_K '(' exp ')'
385 { $$ = $3; }
386 ;
387
388/* The input section alignment within an output section. */
389opt_subalign:
390 /* empty */
391 { $$ = NULL; }
392 | SUBALIGN '(' exp ')'
393 { $$ = $3; }
394 ;
395
3802b2dd
ILT
396/* A section constraint. */
397opt_constraint:
398 /* empty */
399 { $$ = CONSTRAINT_NONE; }
400 | ONLY_IF_RO
401 { $$ = CONSTRAINT_ONLY_IF_RO; }
402 | ONLY_IF_RW
403 { $$ = CONSTRAINT_ONLY_IF_RW; }
404 | SPECIAL
405 { $$ = CONSTRAINT_SPECIAL; }
406 ;
407
494e05f4
ILT
408/* The trailer of an output section in a SECTIONS block. */
409section_trailer:
494e05f4
ILT
410 opt_memspec opt_at_memspec opt_phdr opt_fill opt_comma
411 {
3802b2dd 412 $$.fill = $4;
1c4f3631 413 $$.phdrs = $3;
494e05f4
ILT
414 }
415 ;
416
417/* A memory specification for an output section. */
418opt_memspec:
e4967d85 419 '>' string
494e05f4
ILT
420 { yyerror(closure, "memory regions are not supported"); }
421 | /* empty */
422 ;
423
424/* A memory specification for where to load an output section. */
425opt_at_memspec:
e4967d85 426 AT '>' string
494e05f4
ILT
427 { yyerror(closure, "memory regions are not supported"); }
428 | /* empty */
429 ;
430
431/* The program segment an output section should go into. */
432opt_phdr:
e4967d85 433 opt_phdr ':' string
1c4f3631 434 { $$ = script_string_list_push_back($1, $3.value, $3.length); }
494e05f4 435 | /* empty */
1c4f3631 436 { $$ = NULL; }
494e05f4
ILT
437 ;
438
a445fddf
ILT
439/* The value to use to fill an output section. FIXME: This does not
440 handle a string of arbitrary length. */
494e05f4 441opt_fill:
3802b2dd 442 '=' parse_exp
494e05f4
ILT
443 { $$ = $2; }
444 | /* empty */
445 { $$ = NULL; }
446 ;
447
448/* Commands which may appear within the description of an output
449 section in a SECTIONS block. */
450section_cmds:
451 /* empty */
452 | section_cmds section_cmd
453 ;
454
455/* A command which may appear within the description of an output
456 section in a SECTIONS block. */
457section_cmd:
458 assignment end
459 | input_section_spec
460 | data_length '(' parse_exp ')'
461 { script_add_data(closure, $1, $3); }
e4967d85 462 | ASSERT_K '(' parse_exp ',' string ')'
494e05f4
ILT
463 { script_add_assertion(closure, $3, $5.value, $5.length); }
464 | FILL '(' parse_exp ')'
465 { script_add_fill(closure, $3); }
466 | CONSTRUCTORS
467 {
468 /* The GNU linker uses CONSTRUCTORS for the a.out object
469 file format. It does nothing when using ELF. Since
470 some ELF linker scripts use it although it does
471 nothing, we accept it and ignore it. */
472 }
3802b2dd 473 | SORT_BY_NAME '(' CONSTRUCTORS ')'
494e05f4
ILT
474 | ';'
475 ;
476
477/* The length of data which may appear within the description of an
478 output section in a SECTIONS block. */
479data_length:
480 QUAD
481 { $$ = QUAD; }
482 | SQUAD
483 { $$ = SQUAD; }
484 | LONG
485 { $$ = LONG; }
486 | SHORT
487 { $$ = SHORT; }
488 | BYTE
489 { $$ = BYTE; }
490 ;
491
492/* An input section specification. This may appear within the
493 description of an output section in a SECTIONS block. */
494input_section_spec:
495 input_section_no_keep
496 { script_add_input_section(closure, &$1, 0); }
497 | KEEP '(' input_section_no_keep ')'
498 { script_add_input_section(closure, &$3, 1); }
499 ;
500
501/* An input section specification within a KEEP clause. */
502input_section_no_keep:
e4967d85 503 string
494e05f4
ILT
504 {
505 $$.file.name = $1;
506 $$.file.sort = SORT_WILDCARD_NONE;
507 $$.input_sections.sections = NULL;
508 $$.input_sections.exclude = NULL;
509 }
510 | wildcard_file '(' wildcard_sections ')'
511 {
512 $$.file = $1;
513 $$.input_sections = $3;
514 }
515 ;
516
517/* A wildcard file specification. */
518wildcard_file:
519 wildcard_name
520 {
521 $$.name = $1;
522 $$.sort = SORT_WILDCARD_NONE;
523 }
524 | SORT_BY_NAME '(' wildcard_name ')'
525 {
526 $$.name = $3;
527 $$.sort = SORT_WILDCARD_BY_NAME;
528 }
529 ;
530
531/* A list of wild card section specifications. */
532wildcard_sections:
533 wildcard_sections opt_comma wildcard_section
534 {
535 $$.sections = script_string_sort_list_add($1.sections, &$3);
536 $$.exclude = $1.exclude;
537 }
538 | wildcard_section
539 {
540 $$.sections = script_new_string_sort_list(&$1);
541 $$.exclude = NULL;
542 }
543 | wildcard_sections opt_comma EXCLUDE_FILE '(' exclude_names ')'
544 {
545 $$.sections = $1.sections;
546 $$.exclude = script_string_list_append($1.exclude, $5);
547 }
548 | EXCLUDE_FILE '(' exclude_names ')'
549 {
550 $$.sections = NULL;
551 $$.exclude = $3;
552 }
553 ;
554
555/* A single wild card specification. */
556wildcard_section:
557 wildcard_name
558 {
559 $$.name = $1;
560 $$.sort = SORT_WILDCARD_NONE;
561 }
562 | SORT_BY_NAME '(' wildcard_section ')'
563 {
564 $$.name = $3.name;
565 switch ($3.sort)
566 {
567 case SORT_WILDCARD_NONE:
568 $$.sort = SORT_WILDCARD_BY_NAME;
569 break;
570 case SORT_WILDCARD_BY_NAME:
571 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
572 break;
573 case SORT_WILDCARD_BY_ALIGNMENT:
574 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
575 $$.sort = SORT_WILDCARD_BY_NAME_BY_ALIGNMENT;
576 break;
577 default:
578 abort();
579 }
580 }
581 | SORT_BY_ALIGNMENT '(' wildcard_section ')'
582 {
583 $$.name = $3.name;
584 switch ($3.sort)
585 {
586 case SORT_WILDCARD_NONE:
587 $$.sort = SORT_WILDCARD_BY_ALIGNMENT;
588 break;
589 case SORT_WILDCARD_BY_ALIGNMENT:
590 case SORT_WILDCARD_BY_ALIGNMENT_BY_NAME:
591 break;
592 case SORT_WILDCARD_BY_NAME:
593 case SORT_WILDCARD_BY_NAME_BY_ALIGNMENT:
594 $$.sort = SORT_WILDCARD_BY_ALIGNMENT_BY_NAME;
595 break;
596 default:
597 abort();
598 }
599 }
600 ;
601
602/* A list of file names to exclude. */
603exclude_names:
604 exclude_names opt_comma wildcard_name
605 { $$ = script_string_list_push_back($1, $3.value, $3.length); }
606 | wildcard_name
607 { $$ = script_new_string_list($1.value, $1.length); }
608 ;
609
610/* A single wildcard name. We recognize '*' and '?' specially since
611 they are expression tokens. */
612wildcard_name:
e4967d85 613 string
494e05f4
ILT
614 { $$ = $1; }
615 | '*'
616 {
617 $$.value = "*";
618 $$.length = 1;
619 }
620 | '?'
621 {
622 $$.value = "?";
623 $$.length = 1;
624 }
625 ;
626
d391083d
ILT
627/* A command which may appear at the top level of a linker script, or
628 within a SECTIONS block. */
629file_or_sections_cmd:
10600224 630 ENTRY '(' string ')'
e5756efb
ILT
631 { script_set_entry(closure, $3.value, $3.length); }
632 | assignment end
e4967d85 633 | ASSERT_K '(' parse_exp ',' string ')'
494e05f4 634 { script_add_assertion(closure, $3, $5.value, $5.length); }
e5756efb
ILT
635 ;
636
1c4f3631
ILT
637/* A list of program header definitions. */
638phdrs_defs:
639 phdrs_defs phdr_def
640 | /* empty */
641 ;
642
643/* A program header definition. */
644phdr_def:
645 string phdr_type phdr_info ';'
646 { script_add_phdr(closure, $1.value, $1.length, $2, &$3); }
647 ;
648
649/* A program header type. The GNU linker accepts a general expression
650 here, but that would be a pain because we would have to dig into
651 the expression structure. It's unlikely that anybody uses anything
652 other than a string or a number here, so that is all we expect. */
653phdr_type:
654 string
655 { $$ = script_phdr_string_to_type(closure, $1.value, $1.length); }
656 | INTEGER
657 { $$ = $1; }
658 ;
659
660/* Additional information for a program header. */
661phdr_info:
662 /* empty */
663 { memset(&$$, 0, sizeof(struct Phdr_info)); }
664 | string phdr_info
665 {
666 $$ = $2;
667 if ($1.length == 7 && strncmp($1.value, "FILEHDR", 7) == 0)
668 $$.includes_filehdr = 1;
669 else
670 yyerror(closure, "PHDRS syntax error");
671 }
672 | PHDRS phdr_info
673 {
674 $$ = $2;
675 $$.includes_phdrs = 1;
676 }
677 | string '(' INTEGER ')' phdr_info
678 {
679 $$ = $5;
680 if ($1.length == 5 && strncmp($1.value, "FLAGS", 5) == 0)
681 {
682 $$.is_flags_valid = 1;
683 $$.flags = $3;
684 }
685 else
686 yyerror(closure, "PHDRS syntax error");
687 }
688 | AT '(' parse_exp ')' phdr_info
689 {
690 $$ = $5;
691 $$.load_address = $3;
692 }
693 ;
694
e5756efb
ILT
695/* Set a symbol to a value. */
696assignment:
10600224 697 string '=' parse_exp
e5756efb 698 { script_set_symbol(closure, $1.value, $1.length, $3, 0, 0); }
10600224 699 | string PLUSEQ parse_exp
e5756efb
ILT
700 {
701 Expression_ptr s = script_exp_string($1.value, $1.length);
702 Expression_ptr e = script_exp_binary_add(s, $3);
703 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
704 }
10600224 705 | string MINUSEQ parse_exp
e5756efb
ILT
706 {
707 Expression_ptr s = script_exp_string($1.value, $1.length);
708 Expression_ptr e = script_exp_binary_sub(s, $3);
709 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
710 }
10600224 711 | string MULTEQ parse_exp
e5756efb
ILT
712 {
713 Expression_ptr s = script_exp_string($1.value, $1.length);
714 Expression_ptr e = script_exp_binary_mult(s, $3);
715 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
716 }
10600224 717 | string DIVEQ parse_exp
e5756efb
ILT
718 {
719 Expression_ptr s = script_exp_string($1.value, $1.length);
720 Expression_ptr e = script_exp_binary_div(s, $3);
721 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
722 }
10600224 723 | string LSHIFTEQ parse_exp
e5756efb
ILT
724 {
725 Expression_ptr s = script_exp_string($1.value, $1.length);
726 Expression_ptr e = script_exp_binary_lshift(s, $3);
727 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
728 }
10600224 729 | string RSHIFTEQ parse_exp
e5756efb
ILT
730 {
731 Expression_ptr s = script_exp_string($1.value, $1.length);
732 Expression_ptr e = script_exp_binary_rshift(s, $3);
733 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
734 }
10600224 735 | string ANDEQ parse_exp
e5756efb
ILT
736 {
737 Expression_ptr s = script_exp_string($1.value, $1.length);
738 Expression_ptr e = script_exp_binary_bitwise_and(s, $3);
739 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
740 }
10600224 741 | string OREQ parse_exp
e5756efb
ILT
742 {
743 Expression_ptr s = script_exp_string($1.value, $1.length);
744 Expression_ptr e = script_exp_binary_bitwise_or(s, $3);
745 script_set_symbol(closure, $1.value, $1.length, e, 0, 0);
746 }
10600224 747 | PROVIDE '(' string '=' parse_exp ')'
e5756efb 748 { script_set_symbol(closure, $3.value, $3.length, $5, 1, 0); }
10600224 749 | PROVIDE_HIDDEN '(' string '=' parse_exp ')'
e5756efb
ILT
750 { script_set_symbol(closure, $3.value, $3.length, $5, 1, 1); }
751 ;
752
753/* Parse an expression, putting the lexer into the right mode. */
754parse_exp:
755 { script_push_lex_into_expression_mode(closure); }
756 exp
757 {
758 script_pop_lex_mode(closure);
759 $$ = $2;
760 }
761 ;
762
763/* An expression. */
764exp:
765 '(' exp ')'
766 { $$ = $2; }
767 | '-' exp %prec UNARY
768 { $$ = script_exp_unary_minus($2); }
769 | '!' exp %prec UNARY
770 { $$ = script_exp_unary_logical_not($2); }
771 | '~' exp %prec UNARY
772 { $$ = script_exp_unary_bitwise_not($2); }
773 | '+' exp %prec UNARY
774 { $$ = $2; }
775 | exp '*' exp
776 { $$ = script_exp_binary_mult($1, $3); }
777 | exp '/' exp
778 { $$ = script_exp_binary_div($1, $3); }
779 | exp '%' exp
780 { $$ = script_exp_binary_mod($1, $3); }
781 | exp '+' exp
782 { $$ = script_exp_binary_add($1, $3); }
783 | exp '-' exp
784 { $$ = script_exp_binary_sub($1, $3); }
785 | exp LSHIFT exp
786 { $$ = script_exp_binary_lshift($1, $3); }
787 | exp RSHIFT exp
788 { $$ = script_exp_binary_rshift($1, $3); }
789 | exp EQ exp
790 { $$ = script_exp_binary_eq($1, $3); }
791 | exp NE exp
792 { $$ = script_exp_binary_ne($1, $3); }
793 | exp LE exp
794 { $$ = script_exp_binary_le($1, $3); }
795 | exp GE exp
796 { $$ = script_exp_binary_ge($1, $3); }
797 | exp '<' exp
798 { $$ = script_exp_binary_lt($1, $3); }
799 | exp '>' exp
800 { $$ = script_exp_binary_gt($1, $3); }
801 | exp '&' exp
802 { $$ = script_exp_binary_bitwise_and($1, $3); }
803 | exp '^' exp
804 { $$ = script_exp_binary_bitwise_xor($1, $3); }
805 | exp '|' exp
806 { $$ = script_exp_binary_bitwise_or($1, $3); }
807 | exp ANDAND exp
808 { $$ = script_exp_binary_logical_and($1, $3); }
809 | exp OROR exp
810 { $$ = script_exp_binary_logical_or($1, $3); }
811 | exp '?' exp ':' exp
812 { $$ = script_exp_trinary_cond($1, $3, $5); }
813 | INTEGER
814 { $$ = script_exp_integer($1); }
e4967d85 815 | string
10600224 816 { $$ = script_exp_string($1.value, $1.length); }
e5756efb
ILT
817 | MAX_K '(' exp ',' exp ')'
818 { $$ = script_exp_function_max($3, $5); }
819 | MIN_K '(' exp ',' exp ')'
820 { $$ = script_exp_function_min($3, $5); }
10600224 821 | DEFINED '(' string ')'
e5756efb
ILT
822 { $$ = script_exp_function_defined($3.value, $3.length); }
823 | SIZEOF_HEADERS
824 { $$ = script_exp_function_sizeof_headers(); }
10600224 825 | ALIGNOF '(' string ')'
e5756efb 826 { $$ = script_exp_function_alignof($3.value, $3.length); }
10600224 827 | SIZEOF '(' string ')'
e5756efb 828 { $$ = script_exp_function_sizeof($3.value, $3.length); }
10600224 829 | ADDR '(' string ')'
e5756efb 830 { $$ = script_exp_function_addr($3.value, $3.length); }
10600224 831 | LOADADDR '(' string ')'
e5756efb 832 { $$ = script_exp_function_loadaddr($3.value, $3.length); }
10600224 833 | ORIGIN '(' string ')'
e5756efb 834 { $$ = script_exp_function_origin($3.value, $3.length); }
10600224 835 | LENGTH '(' string ')'
e5756efb 836 { $$ = script_exp_function_length($3.value, $3.length); }
10600224 837 | CONSTANT '(' string ')'
e5756efb
ILT
838 { $$ = script_exp_function_constant($3.value, $3.length); }
839 | ABSOLUTE '(' exp ')'
840 { $$ = script_exp_function_absolute($3); }
841 | ALIGN_K '(' exp ')'
842 { $$ = script_exp_function_align(script_exp_string(".", 1), $3); }
843 | ALIGN_K '(' exp ',' exp ')'
844 { $$ = script_exp_function_align($3, $5); }
845 | BLOCK '(' exp ')'
846 { $$ = script_exp_function_align(script_exp_string(".", 1), $3); }
847 | DATA_SEGMENT_ALIGN '(' exp ',' exp ')'
2d924fd9
ILT
848 {
849 script_data_segment_align(closure);
850 $$ = script_exp_function_data_segment_align($3, $5);
851 }
e5756efb 852 | DATA_SEGMENT_RELRO_END '(' exp ',' exp ')'
2d924fd9
ILT
853 {
854 script_data_segment_relro_end(closure);
855 $$ = script_exp_function_data_segment_relro_end($3, $5);
856 }
e5756efb
ILT
857 | DATA_SEGMENT_END '(' exp ')'
858 { $$ = script_exp_function_data_segment_end($3); }
10600224 859 | SEGMENT_START '(' string ',' exp ')'
e5756efb
ILT
860 {
861 $$ = script_exp_function_segment_start($3.value, $3.length, $5);
862 }
10600224 863 | ASSERT_K '(' exp ',' string ')'
e5756efb
ILT
864 { $$ = script_exp_function_assert($3, $5.value, $5.length); }
865 ;
866
867/* Handle the --defsym option. */
868defsym_expr:
10600224 869 string '=' parse_exp
e5756efb
ILT
870 { script_set_symbol(closure, $1.value, $1.length, $3, 0, 0); }
871 ;
872
c82fbeee
CS
873/* Handle the --dynamic-list option. A dynamic list has the format
874 { sym1; sym2; extern "C++" { namespace::sym3 }; };
875 We store the symbol we see in the "local" list; that is where
876 Command_line::in_dynamic_list() will look to do its check.
877 TODO(csilvers): More than one of these brace-lists can appear, and
878 should just be merged and treated as a single list. */
879dynamic_list_expr: dynamic_list_nodes ;
880
881dynamic_list_nodes:
882 dynamic_list_node
883 | dynamic_list_nodes dynamic_list_node
884 ;
885
886dynamic_list_node:
887 '{' vers_defns ';' '}' ';'
888 { script_new_vers_node (closure, NULL, $2); }
889 ;
890
09124467 891/* A version script. */
e5756efb 892version_script:
09124467
ILT
893 vers_nodes
894 ;
895
896vers_nodes:
897 vers_node
898 | vers_nodes vers_node
899 ;
900
901vers_node:
902 '{' vers_tag '}' ';'
903 {
904 script_register_vers_node (closure, NULL, 0, $2, NULL);
905 }
10600224 906 | string '{' vers_tag '}' ';'
09124467
ILT
907 {
908 script_register_vers_node (closure, $1.value, $1.length, $3,
909 NULL);
910 }
10600224 911 | string '{' vers_tag '}' verdep ';'
09124467
ILT
912 {
913 script_register_vers_node (closure, $1.value, $1.length, $3, $5);
914 }
915 ;
916
917verdep:
10600224 918 string
09124467
ILT
919 {
920 $$ = script_add_vers_depend (closure, NULL, $1.value, $1.length);
921 }
10600224 922 | verdep string
09124467
ILT
923 {
924 $$ = script_add_vers_depend (closure, $1, $2.value, $2.length);
925 }
926 ;
927
928vers_tag:
929 /* empty */
930 { $$ = script_new_vers_node (closure, NULL, NULL); }
931 | vers_defns ';'
932 { $$ = script_new_vers_node (closure, $1, NULL); }
933 | GLOBAL ':' vers_defns ';'
934 { $$ = script_new_vers_node (closure, $3, NULL); }
935 | LOCAL ':' vers_defns ';'
936 { $$ = script_new_vers_node (closure, NULL, $3); }
937 | GLOBAL ':' vers_defns ';' LOCAL ':' vers_defns ';'
938 { $$ = script_new_vers_node (closure, $3, $7); }
939 ;
940
10600224
ILT
941/* Here is one of the rare places we care about the distinction
942 between STRING and QUOTED_STRING. For QUOTED_STRING, we do exact
943 matching on the pattern, so we pass in true for the exact_match
944 parameter. For STRING, we do glob matching and pass in false. */
09124467
ILT
945vers_defns:
946 STRING
947 {
948 $$ = script_new_vers_pattern (closure, NULL, $1.value,
10600224
ILT
949 $1.length, 0);
950 }
951 | QUOTED_STRING
952 {
953 $$ = script_new_vers_pattern (closure, NULL, $1.value,
954 $1.length, 1);
09124467
ILT
955 }
956 | vers_defns ';' STRING
957 {
10600224
ILT
958 $$ = script_new_vers_pattern (closure, $1, $3.value,
959 $3.length, 0);
960 }
961 | vers_defns ';' QUOTED_STRING
962 {
963 $$ = script_new_vers_pattern (closure, $1, $3.value,
964 $3.length, 1);
09124467 965 }
10600224
ILT
966 | /* Push string on the language stack. */
967 EXTERN string '{'
968 { version_script_push_lang (closure, $2.value, $2.length); }
09124467
ILT
969 vers_defns opt_semicolon '}'
970 {
971 $$ = $5;
972 version_script_pop_lang(closure);
973 }
10600224
ILT
974 | /* Push string on the language stack. This is more complicated
975 than the other cases because we need to merge the linked-list
976 state from the pre-EXTERN defns and the post-EXTERN defns. */
977 vers_defns ';' EXTERN string '{'
978 { version_script_push_lang (closure, $4.value, $4.length); }
979 vers_defns opt_semicolon '}'
980 {
981 $$ = script_merge_expressions ($1, $7);
982 version_script_pop_lang(closure);
983 }
09124467
ILT
984 | EXTERN // "extern" as a symbol name
985 {
986 $$ = script_new_vers_pattern (closure, NULL, "extern",
10600224 987 sizeof("extern") - 1, 1);
09124467
ILT
988 }
989 | vers_defns ';' EXTERN
990 {
991 $$ = script_new_vers_pattern (closure, $1, "extern",
10600224 992 sizeof("extern") - 1, 1);
09124467 993 }
e5756efb
ILT
994 ;
995
10600224
ILT
996/* A string can be either a STRING or a QUOTED_STRING. Almost all the
997 time we don't care, and we use this rule. */
998string:
999 STRING
1000 { $$ = $1; }
1001 | QUOTED_STRING
1002 { $$ = $1; }
1003 ;
1004
e5756efb
ILT
1005/* Some statements require a terminator, which may be a semicolon or a
1006 comma. */
1007end:
1008 ';'
1009 | ','
d391083d
ILT
1010 ;
1011
09124467
ILT
1012/* An optional semicolon. */
1013opt_semicolon:
1014 ';'
1015 | /* empty */
1016 ;
1017
d391083d 1018/* An optional comma. */
dbe717ef
ILT
1019opt_comma:
1020 ','
1021 | /* empty */
1022 ;
1023
1024%%