public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] ld: Run ld-scripts/fill2 only for BFD64
@ 2023-12-30 14:55 H.J. Lu
  2024-01-01  0:50 ` Alan Modra
  0 siblings, 1 reply; 3+ messages in thread
From: H.J. Lu @ 2023-12-30 14:55 UTC (permalink / raw)
  To: binutils

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(-)

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.
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ld: Run ld-scripts/fill2 only for BFD64
  2023-12-30 14:55 [PATCH] ld: Run ld-scripts/fill2 only for BFD64 H.J. Lu
@ 2024-01-01  0:50 ` Alan Modra
  2024-01-05  2:35   ` Alan Modra
  0 siblings, 1 reply; 3+ messages in thread
From: Alan Modra @ 2024-01-01  0:50 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

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
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ld: Run ld-scripts/fill2 only for BFD64
  2024-01-01  0:50 ` Alan Modra
@ 2024-01-05  2:35   ` Alan Modra
  0 siblings, 0 replies; 3+ messages in thread
From: Alan Modra @ 2024-01-05  2:35 UTC (permalink / raw)
  To: H.J. Lu; +Cc: binutils

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.

-- 
Alan Modra
Australia Development Lab, IBM

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-01-05  2:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-30 14:55 [PATCH] ld: Run ld-scripts/fill2 only for BFD64 H.J. Lu
2024-01-01  0:50 ` Alan Modra
2024-01-05  2:35   ` Alan Modra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).