ld: Run ld-scripts/fill2 only for BFD64

Message ID 20231230145506.416432-1-hjl.tools@gmail.com
State New
Headers
Series ld: Run ld-scripts/fill2 only for BFD64 |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Testing passed

Commit Message

H.J. Lu Dec. 30, 2023, 2:55 p.m. UTC
  FILL ($0001020304050607) in fil2.t has the value read by strtoul (via
scan_bfd_vma in ldlex.l), and strtoul returns -1 on overflow when BFD64
isn't defined.  Add is_bfd64 and don't run ld-scripts/fill2 if is_bfd64
returns 0.

	PR ld/31120
	* testsuite/config/default.exp (is_bfd64): New.
	* testsuite/ld-scripts/fill2.d (notarget): Add ![is_bfd64].
---
 ld/testsuite/config/default.exp | 23 +++++++++++++++++++++++
 ld/testsuite/ld-scripts/fill2.d |  2 +-
 2 files changed, 24 insertions(+), 1 deletion(-)
  

Comments

Alan Modra Jan. 1, 2024, 12:50 a.m. UTC | #1
On Sat, Dec 30, 2023 at 06:55:06AM -0800, H.J. Lu wrote:
> FILL ($0001020304050607) in fil2.t has the value read by strtoul (via
> scan_bfd_vma in ldlex.l), and strtoul returns -1 on overflow when BFD64
> isn't defined.  Add is_bfd64 and don't run ld-scripts/fill2 if is_bfd64
> returns 0.
> 
> 	PR ld/31120
> 	* testsuite/config/default.exp (is_bfd64): New.
> 	* testsuite/ld-scripts/fill2.d (notarget): Add ![is_bfd64].

Here is an alternate approach.  I'm inclined to go this way because it
removes another host difference (the size of bfd_vma depends on host
and target), and in any case the ld lexer converts strings to integers
without overflow checking.  So I don't think there is any problem in
truncating an integer that exceeds the size of a 32-bit bfd_vma rather
than using (bfd_vma) -1.

Does anyone have a reason to not do this?

	PR 31120
	* ldlex.l: Don't use bfd_scan_vma for integer conversion, use
	strtoull.

diff --git a/ld/ldlex.l b/ld/ldlex.l
index 101f5271b94..ed68be82e3f 100644
--- a/ld/ldlex.l
+++ b/ld/ldlex.l
@@ -133,7 +133,7 @@ V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)*
 				comment (); }
 
 <MRI,EXPRESSION>"$"([0-9A-Fa-f])+ {
-				yylval.integer = bfd_scan_vma (yytext + 1, 0, 16);
+				yylval.integer = strtoull (yytext + 1, 0, 16);
 				yylval.bigint.str = NULL;
 				return INT;
 			}
@@ -158,8 +158,7 @@ V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)*
 				    default:
 				     ibase = 10;
 				   }
-				   yylval.integer = bfd_scan_vma (yytext, 0,
-								  ibase);
+				   yylval.integer = strtoull (yytext, 0, ibase);
 				   yylval.bigint.str = NULL;
 				   return INT;
 				 }
@@ -172,7 +171,7 @@ V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)*
 				      ++s;
 				      ibase = 16;
 				    }
-				  yylval.integer = bfd_scan_vma (s, 0, ibase);
+				  yylval.integer = strtoull (s, 0, ibase);
 				  yylval.bigint.str = NULL;
 				  if (yytext[yyleng - 1] == 'M'
 				      || yytext[yyleng - 1] == 'm')
  
Alan Modra Jan. 5, 2024, 2:35 a.m. UTC | #2
On Mon, Jan 01, 2024 at 11:20:44AM +1030, Alan Modra wrote:
> 	PR 31120
> 	* ldlex.l: Don't use bfd_scan_vma for integer conversion, use
> 	strtoull.

Pushed.
  

Patch

diff --git a/ld/testsuite/config/default.exp b/ld/testsuite/config/default.exp
index 5c925476e23..4b3e4706316 100644
--- a/ld/testsuite/config/default.exp
+++ b/ld/testsuite/config/default.exp
@@ -512,3 +512,26 @@  if {[file exists .libs/libdep.so]} {
 } else {
     set dep_plug_opt ""
 }
+
+# Return 1 if BFD64 is defined.
+proc is_bfd64 { } {
+    global IS_BFD64
+    if { ![info exists IS_BFD64] } then {
+	global CC
+	set fn "cs[pid].c"
+	set rfno "cs[pid].o"
+	set f [open $fn "w"]
+	puts $f "#include <config.h>"
+	puts $f "#include <bfd.h>"
+	puts $f "#ifndef BFD64"
+	puts $f "# error Failed"
+	puts $f "#endif"
+	close $f
+	set rfn [remote_download host $fn]
+	set IS_BFD64 [run_host_cmd_yesno "$CC" "-I../bfd -c $rfn -o $rfno"]
+	remote_file host delete $rfno
+	remote_file host delete $rfn
+	file delete $fn
+    }
+    return $IS_BFD64
+}
diff --git a/ld/testsuite/ld-scripts/fill2.d b/ld/testsuite/ld-scripts/fill2.d
index f913a82a017..05383b98fad 100644
--- a/ld/testsuite/ld-scripts/fill2.d
+++ b/ld/testsuite/ld-scripts/fill2.d
@@ -1,7 +1,7 @@ 
 #source: fill_0.s
 #ld: -T fill2.t
 #readelf: -x.foo
-#notarget: ![is_elf_format]
+#notarget: ![is_elf_format] ![is_bfd64]
 # See PR 30865 - a fill value expressed as a simple hexadecimal
 # number behaves differently from other fill values.