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