1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
|
%{
/* Parser for linker scripts.
Copyright (C) 2001-2011 Red Hat, Inc.
This file is part of Red Hat elfutils.
Written by Ulrich Drepper <drepper@redhat.com>, 2001.
Red Hat elfutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the
Free Software Foundation; version 2 of the License.
Red Hat elfutils is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with Red Hat elfutils; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
Red Hat elfutils is an included package of the Open Invention Network.
An included package of the Open Invention Network is a package for which
Open Invention Network licensees cross-license their patents. No patent
license is granted, either expressly or impliedly, by designation as an
included package. Should you wish to participate in the Open Invention
Network licensing program, please visit www.openinventionnetwork.com
<http://www.openinventionnetwork.com>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <assert.h>
#include <error.h>
#include <libintl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <system.h>
#include <ld.h>
/* The error handler. */
static void yyerror (const char *s);
/* Some helper functions we need to construct the data structures
describing information from the file. */
static struct expression *new_expr (int tag);
static struct input_section_name *new_input_section_name (const char *name,
bool sort_flag);
static struct input_rule *new_input_rule (int tag);
static struct output_rule *new_output_rule (int tag);
static struct assignment *new_assignment (const char *variable,
struct expression *expression,
bool provide_flag);
static void new_segment (int mode, struct output_rule *output_rule);
static struct filename_list *new_filename_listelem (const char *string);
static void add_inputfiles (struct filename_list *fnames);
static struct id_list *new_id_listelem (const char *str);
static struct filename_list *mark_as_needed (struct filename_list *listp);
static struct version *new_version (struct id_list *local,
struct id_list *global);
static struct version *merge_versions (struct version *one,
struct version *two);
static void add_versions (struct version *versions);
extern int yylex (void);
%}
%union {
uintmax_t num;
enum expression_tag op;
char *str;
struct expression *expr;
struct input_section_name *sectionname;
struct filemask_section_name *filemask_section_name;
struct input_rule *input_rule;
struct output_rule *output_rule;
struct assignment *assignment;
struct filename_list *filename_list;
struct version *version;
struct id_list *id_list;
}
%token kADD_OP
%token kALIGN
%token kAS_NEEDED
%token kENTRY
%token kEXCLUDE_FILE
%token <str> kFILENAME
%token kGLOBAL
%token kGROUP
%token <str> kID
%token kINPUT
%token kINTERP
%token kKEEP
%token kLOCAL
%token <num> kMODE
%token kMUL_OP
%token <num> kNUM
%token kOUTPUT_FORMAT
%token kPAGESIZE
%token kPROVIDE
%token kSEARCH_DIR
%token kSEGMENT
%token kSIZEOF_HEADERS
%token kSORT
%token kVERSION
%token kVERSION_SCRIPT
%left '|'
%left '&'
%left ADD_OP
%left MUL_OP '*'
%type <op> kADD_OP
%type <op> kMUL_OP
%type <str> filename_id
%type <str> filename_id_star
%type <str> exclude_opt
%type <expr> expr
%type <sectionname> sort_opt_name
%type <filemask_section_name> sectionname
%type <input_rule> inputsection
%type <input_rule> inputsections
%type <output_rule> outputsection
%type <output_rule> outputsections
%type <assignment> assignment
%type <filename_list> filename_id_list
%type <filename_list> filename_id_listelem
%type <version> versionlist
%type <version> version
%type <version> version_stmt_list
%type <version> version_stmt
%type <id_list> filename_id_star_list
%expect 16
%%
script_or_version:
file
| kVERSION_SCRIPT versionlist
{ add_versions ($2); }
;
file: file content
| content
;
content: kENTRY '(' kID ')' ';'
{
if (likely (ld_state.entry == NULL))
ld_state.entry = $3;
}
| kSEARCH_DIR '(' filename_id ')' ';'
{
ld_new_searchdir ($3);
}
| kPAGESIZE '(' kNUM ')' ';'
{
if (likely (ld_state.pagesize == 0))
ld_state.pagesize = $3;
}
| kINTERP '(' filename_id ')' ';'
{
if (likely (ld_state.interp == NULL)
&& ld_state.file_type != dso_file_type)
ld_state.interp = $3;
}
| kSEGMENT kMODE '{' outputsections '}'
{
new_segment ($2, $4);
}
| kSEGMENT error '{' outputsections '}'
{
fputs_unlocked (gettext ("mode for segment invalid\n"),
stderr);
new_segment (0, $4);
}
| kGROUP '(' filename_id_list ')'
{
/* First little optimization. If there is only one
file in the group don't do anything. */
if ($3 != $3->next)
{
$3->next->group_start = 1;
$3->group_end = 1;
}
add_inputfiles ($3);
}
| kINPUT '(' filename_id_list ')'
{ add_inputfiles ($3); }
| kAS_NEEDED '(' filename_id_list ')'
{ add_inputfiles (mark_as_needed ($3)); }
| kVERSION '{' versionlist '}'
{ add_versions ($3); }
| kOUTPUT_FORMAT '(' filename_id ')'
{ /* XXX TODO */ }
;
outputsections: outputsections outputsection
{
$2->next = $1->next;
$$ = $1->next = $2;
}
| outputsection
{ $$ = $1; }
;
outputsection: assignment ';'
{
$$ = new_output_rule (output_assignment);
$$->val.assignment = $1;
}
| kID '{' inputsections '}'
{
$$ = new_output_rule (output_section);
$$->val.section.name = $1;
$$->val.section.input = $3->next;
if (ld_state.strip == strip_debug
&& ebl_debugscn_p (ld_state.ebl, $1))
$$->val.section.ignored = true;
else
$$->val.section.ignored = false;
$3->next = NULL;
}
| kID ';'
{
/* This is a short cut for "ID { *(ID) }". */
$$ = new_output_rule (output_section);
$$->val.section.name = $1;
$$->val.section.input = new_input_rule (input_section);
$$->val.section.input->next = NULL;
$$->val.section.input->val.section =
(struct filemask_section_name *)
obstack_alloc (&ld_state.smem,
sizeof (struct filemask_section_name));
$$->val.section.input->val.section->filemask = NULL;
$$->val.section.input->val.section->excludemask = NULL;
$$->val.section.input->val.section->section_name =
new_input_section_name ($1, false);
$$->val.section.input->val.section->keep_flag = false;
if (ld_state.strip == strip_debug
&& ebl_debugscn_p (ld_state.ebl, $1))
$$->val.section.ignored = true;
else
$$->val.section.ignored = false;
}
;
assignment: kID '=' expr
{ $$ = new_assignment ($1, $3, false); }
| kPROVIDE '(' kID '=' expr ')'
{ $$ = new_assignment ($3, $5, true); }
;
inputsections: inputsections inputsection
{
$2->next = $1->next;
$$ = $1->next = $2;
}
| inputsection
{ $$ = $1; }
;
inputsection: sectionname
{
$$ = new_input_rule (input_section);
$$->val.section = $1;
}
| kKEEP '(' sectionname ')'
{
$3->keep_flag = true;
$$ = new_input_rule (input_section);
$$->val.section = $3;
}
| assignment ';'
{
$$ = new_input_rule (input_assignment);
$$->val.assignment = $1;
}
;
sectionname: filename_id_star '(' exclude_opt sort_opt_name ')'
{
$$ = (struct filemask_section_name *)
obstack_alloc (&ld_state.smem, sizeof (*$$));
$$->filemask = $1;
$$->excludemask = $3;
$$->section_name = $4;
$$->keep_flag = false;
}
;
sort_opt_name: kID
{ $$ = new_input_section_name ($1, false); }
| kSORT '(' kID ')'
{ $$ = new_input_section_name ($3, true); }
;
exclude_opt: kEXCLUDE_FILE '(' filename_id ')'
{ $$ = $3; }
|
{ $$ = NULL; }
;
expr: kALIGN '(' expr ')'
{
$$ = new_expr (exp_align);
$$->val.child = $3;
}
| '(' expr ')'
{ $$ = $2; }
| expr '*' expr
{
$$ = new_expr (exp_mult);
$$->val.binary.left = $1;
$$->val.binary.right = $3;
}
| expr kMUL_OP expr
{
$$ = new_expr ($2);
$$->val.binary.left = $1;
$$->val.binary.right = $3;
}
| expr kADD_OP expr
{
$$ = new_expr ($2);
$$->val.binary.left = $1;
$$->val.binary.right = $3;
}
| expr '&' expr
{
$$ = new_expr (exp_and);
$$->val.binary.left = $1;
$$->val.binary.right = $3;
}
| expr '|' expr
{
$$ = new_expr (exp_or);
$$->val.binary.left = $1;
$$->val.binary.right = $3;
}
| kNUM
{
$$ = new_expr (exp_num);
$$->val.num = $1;
}
| kID
{
$$ = new_expr (exp_id);
$$->val.str = $1;
}
| kSIZEOF_HEADERS
{ $$ = new_expr (exp_sizeof_headers); }
| kPAGESIZE
{ $$ = new_expr (exp_pagesize); }
;
filename_id_list: filename_id_list comma_opt filename_id_listelem
{
$3->next = $1->next;
$$ = $1->next = $3;
}
| filename_id_listelem
{ $$ = $1; }
;
comma_opt: ','
|
;
filename_id_listelem: kGROUP '(' filename_id_list ')'
{
/* First little optimization. If there is only one
file in the group don't do anything. */
if ($3 != $3->next)
{
$3->next->group_start = 1;
$3->group_end = 1;
}
$$ = $3;
}
| kAS_NEEDED '(' filename_id_list ')'
{ $$ = mark_as_needed ($3); }
| filename_id
{ $$ = new_filename_listelem ($1); }
;
versionlist: versionlist version
{
$2->next = $1->next;
$$ = $1->next = $2;
}
| version
{ $$ = $1; }
;
version: '{' version_stmt_list '}' ';'
{
$2->versionname = "";
$2->parentname = NULL;
$$ = $2;
}
| filename_id '{' version_stmt_list '}' ';'
{
$3->versionname = $1;
$3->parentname = NULL;
$$ = $3;
}
| filename_id '{' version_stmt_list '}' filename_id ';'
{
$3->versionname = $1;
$3->parentname = $5;
$$ = $3;
}
;
version_stmt_list:
version_stmt_list version_stmt
{ $$ = merge_versions ($1, $2); }
| version_stmt
{ $$ = $1; }
;
version_stmt: kGLOBAL filename_id_star_list
{ $$ = new_version (NULL, $2); }
| kLOCAL filename_id_star_list
{ $$ = new_version ($2, NULL); }
;
filename_id_star_list:
filename_id_star_list filename_id_star ';'
{
struct id_list *newp = new_id_listelem ($2);
newp->next = $1->next;
$$ = $1->next = newp;
}
| filename_id_star ';'
{ $$ = new_id_listelem ($1); }
;
filename_id: kFILENAME
{ $$ = $1; }
| kID
{ $$ = $1; }
;
filename_id_star: filename_id
{ $$ = $1; }
| '*'
{ $$ = NULL; }
;
%%
static void
yyerror (const char *s)
{
error (0, 0, (ld_scan_version_script
? gettext ("while reading version script '%s': %s at line %d")
: gettext ("while reading linker script '%s': %s at line %d")),
ldin_fname, gettext (s), ldlineno);
}
static struct expression *
new_expr (int tag)
{
struct expression *newp = (struct expression *)
obstack_alloc (&ld_state.smem, sizeof (*newp));
newp->tag = tag;
return newp;
}
static struct input_section_name *
new_input_section_name (const char *name, bool sort_flag)
{
struct input_section_name *newp = (struct input_section_name *)
obstack_alloc (&ld_state.smem, sizeof (*newp));
newp->name = name;
newp->sort_flag = sort_flag;
return newp;
}
static struct input_rule *
new_input_rule (int tag)
{
struct input_rule *newp = (struct input_rule *)
obstack_alloc (&ld_state.smem, sizeof (*newp));
newp->tag = tag;
newp->next = newp;
return newp;
}
static struct output_rule *
new_output_rule (int tag)
{
struct output_rule *newp = (struct output_rule *)
memset (obstack_alloc (&ld_state.smem, sizeof (*newp)),
'\0', sizeof (*newp));
newp->tag = tag;
newp->next = newp;
return newp;
}
static struct assignment *
new_assignment (const char *variable, struct expression *expression,
bool provide_flag)
{
struct assignment *newp = (struct assignment *)
obstack_alloc (&ld_state.smem, sizeof (*newp));
newp->variable = variable;
newp->expression = expression;
newp->sym = NULL;
newp->provide_flag = provide_flag;
/* Insert the symbol into a hash table. We will later have to matc*/
return newp;
}
static void
new_segment (int mode, struct output_rule *output_rule)
{
struct output_segment *newp;
newp
= (struct output_segment *) obstack_alloc (&ld_state.smem, sizeof (*newp));
newp->mode = mode;
newp->next = newp;
newp->output_rules = output_rule->next;
output_rule->next = NULL;
/* Enqueue the output segment description. */
if (ld_state.output_segments == NULL)
ld_state.output_segments = newp;
else
{
newp->next = ld_state.output_segments->next;
ld_state.output_segments = ld_state.output_segments->next = newp;
}
/* If the output file should be stripped of all symbol set the flag
in the structures of all output sections. */
if (mode == 0 && ld_state.strip == strip_all)
{
struct output_rule *runp;
for (runp = newp->output_rules; runp != NULL; runp = runp->next)
if (runp->tag == output_section)
runp->val.section.ignored = true;
}
}
static struct filename_list *
new_filename_listelem (const char *string)
{
struct filename_list *newp;
/* We use calloc and not the obstack since this object can be freed soon. */
newp = (struct filename_list *) xcalloc (1, sizeof (*newp));
newp->name = string;
newp->next = newp;
return newp;
}
static struct filename_list *
mark_as_needed (struct filename_list *listp)
{
struct filename_list *runp = listp;
do
{
runp->as_needed = true;
runp = runp->next;
}
while (runp != listp);
return listp;
}
static void
add_inputfiles (struct filename_list *fnames)
{
assert (fnames != NULL);
if (ld_state.srcfiles == NULL)
ld_state.srcfiles = fnames;
else
{
struct filename_list *first = ld_state.srcfiles->next;
ld_state.srcfiles->next = fnames->next;
fnames->next = first;
ld_state.srcfiles->next = fnames;
}
}
static _Bool
special_char_p (const char *str)
{
while (*str != '\0')
{
if (__builtin_expect (*str == '*', 0)
|| __builtin_expect (*str == '?', 0)
|| __builtin_expect (*str == '[', 0))
return true;
++str;
}
return false;
}
static struct id_list *
new_id_listelem (const char *str)
{
struct id_list *newp;
newp = (struct id_list *) obstack_alloc (&ld_state.smem, sizeof (*newp));
if (str == NULL)
newp->u.id_type = id_all;
else if (__builtin_expect (special_char_p (str), false))
newp->u.id_type = id_wild;
else
newp->u.id_type = id_str;
newp->id = str;
newp->next = newp;
return newp;
}
static struct version *
new_version (struct id_list *local, struct id_list *global)
{
struct version *newp;
newp = (struct version *) obstack_alloc (&ld_state.smem, sizeof (*newp));
newp->next = newp;
newp->local_names = local;
newp->global_names = global;
newp->versionname = NULL;
newp->parentname = NULL;
return newp;
}
static struct version *
merge_versions (struct version *one, struct version *two)
{
assert (two->local_names == NULL || two->global_names == NULL);
if (two->local_names != NULL)
{
if (one->local_names == NULL)
one->local_names = two->local_names;
else
{
two->local_names->next = one->local_names->next;
one->local_names = one->local_names->next = two->local_names;
}
}
else
{
if (one->global_names == NULL)
one->global_names = two->global_names;
else
{
two->global_names->next = one->global_names->next;
one->global_names = one->global_names->next = two->global_names;
}
}
return one;
}
static void
add_id_list (const char *versionname, struct id_list *runp, _Bool local)
{
struct id_list *lastp = runp;
if (runp == NULL)
/* Nothing to do. */
return;
/* Convert into a simple single-linked list. */
runp = runp->next;
assert (runp != NULL);
lastp->next = NULL;
do
if (runp->u.id_type == id_str)
{
struct id_list *curp;
struct id_list *defp;
unsigned long int hval = elf_hash (runp->id);
curp = runp;
runp = runp->next;
defp = ld_version_str_tab_find (&ld_state.version_str_tab, hval, curp);
if (defp != NULL)
{
/* There is already a version definition for this symbol. */
while (strcmp (defp->u.s.versionname, versionname) != 0)
{
if (defp->next == NULL)
{
/* No version like this so far. */
defp->next = curp;
curp->u.s.local = local;
curp->u.s.versionname = versionname;
curp->next = NULL;
defp = NULL;
break;
}
defp = defp->next;
}
if (defp != NULL && defp->u.s.local != local)
error (EXIT_FAILURE, 0, versionname[0] == '\0'
? gettext ("\
symbol '%s' is declared both local and global for unnamed version")
: gettext ("\
symbol '%s' is declared both local and global for version '%s'"),
runp->id, versionname);
}
else
{
/* This is the first version definition for this symbol. */
ld_version_str_tab_insert (&ld_state.version_str_tab, hval, curp);
curp->u.s.local = local;
curp->u.s.versionname = versionname;
curp->next = NULL;
}
}
else if (runp->u.id_type == id_all)
{
if (local)
{
if (ld_state.default_bind_global)
error (EXIT_FAILURE, 0,
gettext ("default visibility set as local and global"));
ld_state.default_bind_local = true;
}
else
{
if (ld_state.default_bind_local)
error (EXIT_FAILURE, 0,
gettext ("default visibility set as local and global"));
ld_state.default_bind_global = true;
}
runp = runp->next;
}
else
{
assert (runp->u.id_type == id_wild);
/* XXX TBI */
abort ();
}
while (runp != NULL);
}
static void
add_versions (struct version *versions)
{
struct version *lastp = versions;
if (versions == NULL)
return;
/* Convert into a simple single-linked list. */
versions = versions->next;
assert (versions != NULL);
lastp->next = NULL;
do
{
add_id_list (versions->versionname, versions->local_names, true);
add_id_list (versions->versionname, versions->global_names, false);
versions = versions->next;
}
while (versions != NULL);
}
|