2 /* Parser for linker scripts.
3 Copyright (C) 2001-2011 Red Hat, Inc.
4 This file is part of elfutils.
5 Written by Ulrich Drepper <drepper@redhat.com>, 2001.
7 This file is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 elfutils is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
36 /* The error handler. */
37 static void yyerror (const char *s);
39 /* Some helper functions we need to construct the data structures
40 describing information from the file. */
41 static struct expression *new_expr (int tag);
42 static struct input_section_name *new_input_section_name (const char *name,
44 static struct input_rule *new_input_rule (int tag);
45 static struct output_rule *new_output_rule (int tag);
46 static struct assignment *new_assignment (const char *variable,
47 struct expression *expression,
49 static void new_segment (int mode, struct output_rule *output_rule);
50 static struct filename_list *new_filename_listelem (const char *string);
51 static void add_inputfiles (struct filename_list *fnames);
52 static struct id_list *new_id_listelem (const char *str);
53 static struct filename_list *mark_as_needed (struct filename_list *listp);
54 static struct version *new_version (struct id_list *local,
55 struct id_list *global);
56 static struct version *merge_versions (struct version *one,
58 static void add_versions (struct version *versions);
60 extern int yylex (void);
65 enum expression_tag op;
67 struct expression *expr;
68 struct input_section_name *sectionname;
69 struct filemask_section_name *filemask_section_name;
70 struct input_rule *input_rule;
71 struct output_rule *output_rule;
72 struct assignment *assignment;
73 struct filename_list *filename_list;
74 struct version *version;
75 struct id_list *id_list;
83 %token <str> kFILENAME
99 %token kSIZEOF_HEADERS
102 %token kVERSION_SCRIPT
111 %type <str> filename_id
112 %type <str> filename_id_star
113 %type <str> exclude_opt
115 %type <sectionname> sort_opt_name
116 %type <filemask_section_name> sectionname
117 %type <input_rule> inputsection
118 %type <input_rule> inputsections
119 %type <output_rule> outputsection
120 %type <output_rule> outputsections
121 %type <assignment> assignment
122 %type <filename_list> filename_id_list
123 %type <filename_list> filename_id_listelem
124 %type <version> versionlist
125 %type <version> version
126 %type <version> version_stmt_list
127 %type <version> version_stmt
128 %type <id_list> filename_id_star_list
136 | kVERSION_SCRIPT versionlist
137 { add_versions ($2); }
144 content: kENTRY '(' kID ')' ';'
146 if (likely (ld_state.entry == NULL))
149 | kSEARCH_DIR '(' filename_id ')' ';'
151 ld_new_searchdir ($3);
153 | kPAGESIZE '(' kNUM ')' ';'
155 if (likely (ld_state.pagesize == 0))
156 ld_state.pagesize = $3;
158 | kINTERP '(' filename_id ')' ';'
160 if (likely (ld_state.interp == NULL)
161 && ld_state.file_type != dso_file_type)
162 ld_state.interp = $3;
164 | kSEGMENT kMODE '{' outputsections '}'
166 new_segment ($2, $4);
168 | kSEGMENT error '{' outputsections '}'
170 fputs_unlocked (gettext ("mode for segment invalid\n"),
174 | kGROUP '(' filename_id_list ')'
176 /* First little optimization. If there is only one
177 file in the group don't do anything. */
180 $3->next->group_start = 1;
185 | kINPUT '(' filename_id_list ')'
186 { add_inputfiles ($3); }
187 | kAS_NEEDED '(' filename_id_list ')'
188 { add_inputfiles (mark_as_needed ($3)); }
189 | kVERSION '{' versionlist '}'
190 { add_versions ($3); }
191 | kOUTPUT_FORMAT '(' filename_id ')'
195 outputsections: outputsections outputsection
204 outputsection: assignment ';'
206 $$ = new_output_rule (output_assignment);
207 $$->val.assignment = $1;
209 | kID '{' inputsections '}'
211 $$ = new_output_rule (output_section);
212 $$->val.section.name = $1;
213 $$->val.section.input = $3->next;
214 if (ld_state.strip == strip_debug
215 && ebl_debugscn_p (ld_state.ebl, $1))
216 $$->val.section.ignored = true;
218 $$->val.section.ignored = false;
223 /* This is a short cut for "ID { *(ID) }". */
224 $$ = new_output_rule (output_section);
225 $$->val.section.name = $1;
226 $$->val.section.input = new_input_rule (input_section);
227 $$->val.section.input->next = NULL;
228 $$->val.section.input->val.section =
229 (struct filemask_section_name *)
230 obstack_alloc (&ld_state.smem,
231 sizeof (struct filemask_section_name));
232 $$->val.section.input->val.section->filemask = NULL;
233 $$->val.section.input->val.section->excludemask = NULL;
234 $$->val.section.input->val.section->section_name =
235 new_input_section_name ($1, false);
236 $$->val.section.input->val.section->keep_flag = false;
237 if (ld_state.strip == strip_debug
238 && ebl_debugscn_p (ld_state.ebl, $1))
239 $$->val.section.ignored = true;
241 $$->val.section.ignored = false;
245 assignment: kID '=' expr
246 { $$ = new_assignment ($1, $3, false); }
247 | kPROVIDE '(' kID '=' expr ')'
248 { $$ = new_assignment ($3, $5, true); }
251 inputsections: inputsections inputsection
260 inputsection: sectionname
262 $$ = new_input_rule (input_section);
263 $$->val.section = $1;
265 | kKEEP '(' sectionname ')'
267 $3->keep_flag = true;
269 $$ = new_input_rule (input_section);
270 $$->val.section = $3;
274 $$ = new_input_rule (input_assignment);
275 $$->val.assignment = $1;
279 sectionname: filename_id_star '(' exclude_opt sort_opt_name ')'
281 $$ = (struct filemask_section_name *)
282 obstack_alloc (&ld_state.smem, sizeof (*$$));
284 $$->excludemask = $3;
285 $$->section_name = $4;
286 $$->keep_flag = false;
291 { $$ = new_input_section_name ($1, false); }
293 { $$ = new_input_section_name ($3, true); }
296 exclude_opt: kEXCLUDE_FILE '(' filename_id ')'
302 expr: kALIGN '(' expr ')'
304 $$ = new_expr (exp_align);
311 $$ = new_expr (exp_mult);
312 $$->val.binary.left = $1;
313 $$->val.binary.right = $3;
318 $$->val.binary.left = $1;
319 $$->val.binary.right = $3;
324 $$->val.binary.left = $1;
325 $$->val.binary.right = $3;
329 $$ = new_expr (exp_and);
330 $$->val.binary.left = $1;
331 $$->val.binary.right = $3;
335 $$ = new_expr (exp_or);
336 $$->val.binary.left = $1;
337 $$->val.binary.right = $3;
341 $$ = new_expr (exp_num);
346 $$ = new_expr (exp_id);
350 { $$ = new_expr (exp_sizeof_headers); }
352 { $$ = new_expr (exp_pagesize); }
355 filename_id_list: filename_id_list comma_opt filename_id_listelem
360 | filename_id_listelem
368 filename_id_listelem: kGROUP '(' filename_id_list ')'
370 /* First little optimization. If there is only one
371 file in the group don't do anything. */
374 $3->next->group_start = 1;
379 | kAS_NEEDED '(' filename_id_list ')'
380 { $$ = mark_as_needed ($3); }
382 { $$ = new_filename_listelem ($1); }
386 versionlist: versionlist version
395 version: '{' version_stmt_list '}' ';'
397 $2->versionname = "";
398 $2->parentname = NULL;
401 | filename_id '{' version_stmt_list '}' ';'
403 $3->versionname = $1;
404 $3->parentname = NULL;
407 | filename_id '{' version_stmt_list '}' filename_id ';'
409 $3->versionname = $1;
416 version_stmt_list version_stmt
417 { $$ = merge_versions ($1, $2); }
422 version_stmt: kGLOBAL filename_id_star_list
423 { $$ = new_version (NULL, $2); }
424 | kLOCAL filename_id_star_list
425 { $$ = new_version ($2, NULL); }
428 filename_id_star_list:
429 filename_id_star_list filename_id_star ';'
431 struct id_list *newp = new_id_listelem ($2);
432 newp->next = $1->next;
433 $$ = $1->next = newp;
435 | filename_id_star ';'
436 { $$ = new_id_listelem ($1); }
439 filename_id: kFILENAME
445 filename_id_star: filename_id
454 yyerror (const char *s)
456 error (0, 0, (ld_scan_version_script
457 ? gettext ("while reading version script '%s': %s at line %d")
458 : gettext ("while reading linker script '%s': %s at line %d")),
459 ldin_fname, gettext (s), ldlineno);
463 static struct expression *
466 struct expression *newp = (struct expression *)
467 obstack_alloc (&ld_state.smem, sizeof (*newp));
474 static struct input_section_name *
475 new_input_section_name (const char *name, bool sort_flag)
477 struct input_section_name *newp = (struct input_section_name *)
478 obstack_alloc (&ld_state.smem, sizeof (*newp));
481 newp->sort_flag = sort_flag;
486 static struct input_rule *
487 new_input_rule (int tag)
489 struct input_rule *newp = (struct input_rule *)
490 obstack_alloc (&ld_state.smem, sizeof (*newp));
498 static struct output_rule *
499 new_output_rule (int tag)
501 struct output_rule *newp = (struct output_rule *)
502 memset (obstack_alloc (&ld_state.smem, sizeof (*newp)),
503 '\0', sizeof (*newp));
511 static struct assignment *
512 new_assignment (const char *variable, struct expression *expression,
515 struct assignment *newp = (struct assignment *)
516 obstack_alloc (&ld_state.smem, sizeof (*newp));
518 newp->variable = variable;
519 newp->expression = expression;
521 newp->provide_flag = provide_flag;
523 /* Insert the symbol into a hash table. We will later have to matc*/
529 new_segment (int mode, struct output_rule *output_rule)
531 struct output_segment *newp;
534 = (struct output_segment *) obstack_alloc (&ld_state.smem, sizeof (*newp));
538 newp->output_rules = output_rule->next;
539 output_rule->next = NULL;
541 /* Enqueue the output segment description. */
542 if (ld_state.output_segments == NULL)
543 ld_state.output_segments = newp;
546 newp->next = ld_state.output_segments->next;
547 ld_state.output_segments = ld_state.output_segments->next = newp;
550 /* If the output file should be stripped of all symbol set the flag
551 in the structures of all output sections. */
552 if (mode == 0 && ld_state.strip == strip_all)
554 struct output_rule *runp;
556 for (runp = newp->output_rules; runp != NULL; runp = runp->next)
557 if (runp->tag == output_section)
558 runp->val.section.ignored = true;
563 static struct filename_list *
564 new_filename_listelem (const char *string)
566 struct filename_list *newp;
568 /* We use calloc and not the obstack since this object can be freed soon. */
569 newp = (struct filename_list *) xcalloc (1, sizeof (*newp));
576 static struct filename_list *
577 mark_as_needed (struct filename_list *listp)
579 struct filename_list *runp = listp;
582 runp->as_needed = true;
585 while (runp != listp);
592 add_inputfiles (struct filename_list *fnames)
594 assert (fnames != NULL);
596 if (ld_state.srcfiles == NULL)
597 ld_state.srcfiles = fnames;
600 struct filename_list *first = ld_state.srcfiles->next;
602 ld_state.srcfiles->next = fnames->next;
603 fnames->next = first;
604 ld_state.srcfiles->next = fnames;
610 special_char_p (const char *str)
614 if (__builtin_expect (*str == '*', 0)
615 || __builtin_expect (*str == '?', 0)
616 || __builtin_expect (*str == '[', 0))
626 static struct id_list *
627 new_id_listelem (const char *str)
629 struct id_list *newp;
631 newp = (struct id_list *) obstack_alloc (&ld_state.smem, sizeof (*newp));
633 newp->u.id_type = id_all;
634 else if (__builtin_expect (special_char_p (str), false))
635 newp->u.id_type = id_wild;
637 newp->u.id_type = id_str;
645 static struct version *
646 new_version (struct id_list *local, struct id_list *global)
648 struct version *newp;
650 newp = (struct version *) obstack_alloc (&ld_state.smem, sizeof (*newp));
652 newp->local_names = local;
653 newp->global_names = global;
654 newp->versionname = NULL;
655 newp->parentname = NULL;
661 static struct version *
662 merge_versions (struct version *one, struct version *two)
664 assert (two->local_names == NULL || two->global_names == NULL);
666 if (two->local_names != NULL)
668 if (one->local_names == NULL)
669 one->local_names = two->local_names;
672 two->local_names->next = one->local_names->next;
673 one->local_names = one->local_names->next = two->local_names;
678 if (one->global_names == NULL)
679 one->global_names = two->global_names;
682 two->global_names->next = one->global_names->next;
683 one->global_names = one->global_names->next = two->global_names;
692 add_id_list (const char *versionname, struct id_list *runp, _Bool local)
694 struct id_list *lastp = runp;
700 /* Convert into a simple single-linked list. */
702 assert (runp != NULL);
706 if (runp->u.id_type == id_str)
708 struct id_list *curp;
709 struct id_list *defp;
710 unsigned long int hval = elf_hash (runp->id);
715 defp = ld_version_str_tab_find (&ld_state.version_str_tab, hval, curp);
718 /* There is already a version definition for this symbol. */
719 while (strcmp (defp->u.s.versionname, versionname) != 0)
721 if (defp->next == NULL)
723 /* No version like this so far. */
725 curp->u.s.local = local;
726 curp->u.s.versionname = versionname;
735 if (defp != NULL && defp->u.s.local != local)
736 error (EXIT_FAILURE, 0, versionname[0] == '\0'
738 symbol '%s' is declared both local and global for unnamed version")
740 symbol '%s' is declared both local and global for version '%s'"),
741 runp->id, versionname);
745 /* This is the first version definition for this symbol. */
746 ld_version_str_tab_insert (&ld_state.version_str_tab, hval, curp);
748 curp->u.s.local = local;
749 curp->u.s.versionname = versionname;
753 else if (runp->u.id_type == id_all)
757 if (ld_state.default_bind_global)
758 error (EXIT_FAILURE, 0,
759 gettext ("default visibility set as local and global"));
760 ld_state.default_bind_local = true;
764 if (ld_state.default_bind_local)
765 error (EXIT_FAILURE, 0,
766 gettext ("default visibility set as local and global"));
767 ld_state.default_bind_global = true;
774 assert (runp->u.id_type == id_wild);
778 while (runp != NULL);
783 add_versions (struct version *versions)
785 struct version *lastp = versions;
787 if (versions == NULL)
790 /* Convert into a simple single-linked list. */
791 versions = versions->next;
792 assert (versions != NULL);
797 add_id_list (versions->versionname, versions->local_names, true);
798 add_id_list (versions->versionname, versions->global_names, false);
800 versions = versions->next;
802 while (versions != NULL);