This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
[PATCH 3/7] ld: New filename_spec rule in grammar file
Add new filename_spec rule to the linker grammar instead of reusing the
wildcard_spec rule that matches section wildcards.
There are now many patterns / keywords that can be used around section
patterns that are not valid around filenames, for example, neither of
these is valid:
SORT_BY_ALIGNMENT (*) (.text)
SORT_BY_INIT_PRIORITY (*) (.text)
but both are currently accepted by the linker, though they have no
effect.
By creating a new rule 'filename_spec' I limit the filename wildcard
matching to only accept those things that are actually valid for the
filename part, this is SORT_BY_NAME or SORT_NONE (where SORT_NONE is the
same as having no sort specifier at all).
ld/ChangeLog:
* ldgram.y (filename_spec): New rule.
(input_section_spec_no_keep): Use filename_spec.
(wildcard_maybe_exclude): New rule.
* testsuite/ld-scripts/align.exp: Run new tests.
* testsuite/ld-scripts/align3.d: New file.
* testsuite/ld-scripts/align3.t: New file.
* testsuite/ld-scripts/align4.d: New file.
* testsuite/ld-scripts/align4.t: New file.
* testsuite/ld-scripts/align5.d: New file.
* testsuite/ld-scripts/align5.t: New file.
* NEWS: Mention change.
---
ld/ChangeLog | 14 ++++++++++++++
ld/NEWS | 4 ++++
ld/ldgram.y | 37 ++++++++++++++++++++++++++++++++++---
ld/testsuite/ld-scripts/align.exp | 3 +++
ld/testsuite/ld-scripts/align3.d | 3 +++
ld/testsuite/ld-scripts/align3.t | 10 ++++++++++
ld/testsuite/ld-scripts/align4.d | 3 +++
ld/testsuite/ld-scripts/align4.t | 10 ++++++++++
ld/testsuite/ld-scripts/align5.d | 7 +++++++
ld/testsuite/ld-scripts/align5.t | 11 +++++++++++
10 files changed, 99 insertions(+), 3 deletions(-)
create mode 100644 ld/testsuite/ld-scripts/align3.d
create mode 100644 ld/testsuite/ld-scripts/align3.t
create mode 100644 ld/testsuite/ld-scripts/align4.d
create mode 100644 ld/testsuite/ld-scripts/align4.t
create mode 100644 ld/testsuite/ld-scripts/align5.d
create mode 100644 ld/testsuite/ld-scripts/align5.t
diff --git a/ld/NEWS b/ld/NEWS
index 1f5060c6cbe..4486029ddb0 100644
--- a/ld/NEWS
+++ b/ld/NEWS
@@ -1,5 +1,9 @@
-*- text -*-
+* Tighten linker script grammar around file name specifiers to prevent the use
+ of SORT_BY_ALIGNMENT and SORT_BY_INIT_PRIORITY on filenames. These would
+ previously be accepted but had no effect.
+
Changes in 2.29:
* Support for -z shstk in the x86 ELF linker to generate
diff --git a/ld/ldgram.y b/ld/ldgram.y
index 425f1bf2ed3..87c75c5c453 100644
--- a/ld/ldgram.y
+++ b/ld/ldgram.y
@@ -98,7 +98,7 @@ static int error_index;
%type <name> memspec_opt casesymlist
%type <name> memspec_at_opt
%type <cname> wildcard_name
-%type <wildcard> wildcard_spec
+%type <wildcard> wildcard_spec filename_spec wildcard_maybe_exclude
%token <bigint> INT
%token <name> NAME LNAME
%type <integer> length
@@ -447,6 +447,37 @@ wildcard_name:
}
;
+wildcard_maybe_exclude:
+ wildcard_name
+ {
+ $$.name = $1;
+ $$.sorted = none;
+ $$.exclude_name_list = NULL;
+ $$.section_flag_list = NULL;
+ }
+ | EXCLUDE_FILE '(' exclude_name_list ')' wildcard_name
+ {
+ $$.name = $5;
+ $$.sorted = none;
+ $$.exclude_name_list = $3;
+ $$.section_flag_list = NULL;
+ }
+ ;
+
+filename_spec:
+ wildcard_maybe_exclude
+ | SORT_BY_NAME '(' wildcard_maybe_exclude ')'
+ {
+ $$ = $3;
+ $$.sorted = by_name;
+ }
+ | SORT_NONE '(' wildcard_maybe_exclude ')'
+ {
+ $$ = $3;
+ $$.sorted = by_none;
+ }
+ ;
+
wildcard_spec:
wildcard_name
{
@@ -650,11 +681,11 @@ input_section_spec_no_keep:
tmp.section_flag_list = $1;
lang_add_wild (&tmp, $3, ldgram_had_keep);
}
- | wildcard_spec '(' section_NAME_list ')'
+ | filename_spec '(' section_NAME_list ')'
{
lang_add_wild (&$1, $3, ldgram_had_keep);
}
- | sect_flags wildcard_spec '(' section_NAME_list ')'
+ | sect_flags filename_spec '(' section_NAME_list ')'
{
$2.section_flag_list = $1;
lang_add_wild (&$2, $4, ldgram_had_keep);
diff --git a/ld/testsuite/ld-scripts/align.exp b/ld/testsuite/ld-scripts/align.exp
index a41db721b89..ba229af74b1 100644
--- a/ld/testsuite/ld-scripts/align.exp
+++ b/ld/testsuite/ld-scripts/align.exp
@@ -47,6 +47,9 @@ if ![ld_link $ld tmpdir/align "$LDFLAGS -T $srcdir/$subdir/align.t tmpdir/align.
if ![is_aout_format] {
run_dump_test align2a
run_dump_test align2b
+ run_dump_test align3
+ run_dump_test align4
+ run_dump_test align5
}
run_dump_test align2c
set LDFLAGS "$saved_LDFLAGS"
diff --git a/ld/testsuite/ld-scripts/align3.d b/ld/testsuite/ld-scripts/align3.d
new file mode 100644
index 00000000000..384d91a9e1b
--- /dev/null
+++ b/ld/testsuite/ld-scripts/align3.d
@@ -0,0 +1,3 @@
+# source: align2a.s
+# ld: -T align3.t
+# error: .*:4: syntax error
diff --git a/ld/testsuite/ld-scripts/align3.t b/ld/testsuite/ld-scripts/align3.t
new file mode 100644
index 00000000000..5c9e5bfff96
--- /dev/null
+++ b/ld/testsuite/ld-scripts/align3.t
@@ -0,0 +1,10 @@
+SECTIONS
+{
+ .text : {
+ SORT_BY_ALIGNMENT (*) (.text .text.*)
+ }
+
+ .data : {
+ SORT_BY_ALIGNMENT (*) (.data .data.*)
+ }
+}
\ No newline at end of file
diff --git a/ld/testsuite/ld-scripts/align4.d b/ld/testsuite/ld-scripts/align4.d
new file mode 100644
index 00000000000..661738aa21e
--- /dev/null
+++ b/ld/testsuite/ld-scripts/align4.d
@@ -0,0 +1,3 @@
+# source: align2a.s
+# ld: -T align4.t
+# error: .*:4: syntax error
diff --git a/ld/testsuite/ld-scripts/align4.t b/ld/testsuite/ld-scripts/align4.t
new file mode 100644
index 00000000000..fc809057004
--- /dev/null
+++ b/ld/testsuite/ld-scripts/align4.t
@@ -0,0 +1,10 @@
+SECTIONS
+{
+ .text : {
+ SORT_BY_INIT_PRIORITY (*) (.text .text.*)
+ }
+
+ .data : {
+ SORT_BY_INIT_PRIORITY (*) (.data .data.*)
+ }
+}
\ No newline at end of file
diff --git a/ld/testsuite/ld-scripts/align5.d b/ld/testsuite/ld-scripts/align5.d
new file mode 100644
index 00000000000..880b6fbb014
--- /dev/null
+++ b/ld/testsuite/ld-scripts/align5.d
@@ -0,0 +1,7 @@
+# source: align2a.s
+# ld: -T align5.t
+# nm: -n
+
+#...
+.*foo
+#...
\ No newline at end of file
diff --git a/ld/testsuite/ld-scripts/align5.t b/ld/testsuite/ld-scripts/align5.t
new file mode 100644
index 00000000000..7feafaf2dba
--- /dev/null
+++ b/ld/testsuite/ld-scripts/align5.t
@@ -0,0 +1,11 @@
+SECTIONS
+{
+ .text : {
+ SORT_NONE (*) (.text .text.*)
+ }
+
+ .data : {
+ SORT_NONE (*) (.data .data.*)
+ foo = .;
+ }
+}
\ No newline at end of file
--
2.13.3