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