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