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