public inbox for binutils-cvs@sourceware.org
 help / color / mirror / Atom feed
* [binutils-gdb] ld: =fillexp different behaviors for hexidecimal literal
@ 2023-11-06 14:00 Nick Clifton
  0 siblings, 0 replies; only message in thread
From: Nick Clifton @ 2023-11-06 14:00 UTC (permalink / raw)
  To: bfd-cvs

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=f1837d9c3f2dc2a5c232f4eae4c4bd5a7f77baf8

commit f1837d9c3f2dc2a5c232f4eae4c4bd5a7f77baf8
Author: Nick Clifton <nickc@redhat.com>
Date:   Mon Nov 6 13:59:53 2023 +0000

    ld: =fillexp different behaviors for hexidecimal literal
    
      PR 30865
      * ld.texi: Update description of the FILL command.
      * testsuite/ld-scripts/fill2.d: New test.
      * testsuite/ld-scripts/fill2.t: New test source.
      * testsuite/ld-scripts/data.exp: Run the new test.

Diff:
---
 ld/ChangeLog                     |  8 ++++++++
 ld/ld.texi                       | 40 +++++++++++++++++++++++++++++++++++++++-
 ld/testsuite/ld-scripts/data.exp |  1 +
 ld/testsuite/ld-scripts/fill2.d  | 12 ++++++++++++
 ld/testsuite/ld-scripts/fill2.t  | 29 +++++++++++++++++++++++++++++
 5 files changed, 89 insertions(+), 1 deletion(-)

diff --git a/ld/ChangeLog b/ld/ChangeLog
index f567606a570..bc74287e73e 100644
--- a/ld/ChangeLog
+++ b/ld/ChangeLog
@@ -1,3 +1,11 @@
+2023-11-06  Nick Clifton  <nickc@redhat.com>
+
+	PR 30865
+	* ld.texi: Update description of the FILL command.
+	* testsuite/ld-scripts/fill2.d: New test.
+	* testsuite/ld-scripts/fill2.t: New test source.
+	* testsuite/ld-scripts/data.exp: Run the new test.
+
 2023-11-02  Nick Clifton  <nickc@redhat.com>
 
 	* testsuite/ld-x86-64/property-3.r: Update regexp to allow for
diff --git a/ld/ld.texi b/ld/ld.texi
index 7ae01b121d7..f710c02c3d4 100644
--- a/ld/ld.texi
+++ b/ld/ld.texi
@@ -5552,6 +5552,36 @@ entire section.  If both are used, the @code{FILL} command takes
 precedence.  @xref{Output Section Fill}, for details on the fill
 expression.
 
+Note - normally the value of @code{expression} is zero extended to 4
+bytes when used to fill gaps.  Thus @samp{FILL(144)} will fill a
+region with repeats of the pattern @samp{0 0 0 144}.  The value is
+treated as a big-endian number, so for example
+@samp{FILL(22 * 256 + 23)} will fill the region with repeats of the
+pattern @samp {0 0 22 23}.  If the expression results in a value with
+more than 4 significant bytes only the least 4 bytes of the value will
+be used.
+
+The above rules do not apply when the @code{expression} is a simple
+hexadecimal number.  In this case zero extension is not performed and
+all bytes are significant.  So @samp{FILL(0x90)} will fill a region with
+repeats of @samp{0x90} with no zero bytes, and @samp{FILL(0x9192)}
+will fill the region with repeats of @samp{0x91 0x92}.  Zero bytes
+in a hexadecimal expression are significant even at the start, so
+@samp{FILL(0x0090)} will fill a region with repeats of @samp{0x00 0x90}.
+
+Hexadecimal numbers can be longer than 4 bytes, and all of the bytes
+are significant, so @samp{FILL(0x123456789a)} will fill a region with
+repeats of the 5 byte sequence @samp{0x12 0x34 0x56 0x78 0x9a}.
+Excess bytes in a hexadecimal value beyond the size of a region will 
+be silently ignored.
+
+The above only applies to hexadecimal numbers specified as
+@samp{0x[0-9][a-f][A-F]}.  Hexadecimal numbers specified with a
+@samp{$} prefix, or a @samp{h}, @samp{H}, @samp{x} or @samp{X} suffix
+will follow the normal fill value rules.  This also applies to
+expressions that involve hexadecimal numbers, and hexadecimal numbers
+that have a magnitude suffix.
+
 @kindex LINKER_VERSION
 @cindex LINKER_VERSION
 The @code{LINKER_VERSION} command inserts a string containing the
@@ -5966,7 +5996,15 @@ an arbitrarily long sequence of hex digits can be used to specify the
 fill pattern;  Leading zeros become part of the pattern too.  For all
 other cases, including extra parentheses or a unary @code{+}, the fill
 pattern is the four least significant bytes of the value of the
-expression.  In all cases, the number is big-endian.
+expression.  If the value is less than four bytes in size then it will
+be zero extended to four bytes.  In all cases, the number is big-endian.
+
+@smallexample
+Fill Value     Fill Pattern
+0x90           90 90 90 90
+0x0090         00 90 00 90
+144            00 00 00 90
+@end smallexample
 
 You can also change the fill value with a @code{FILL} command in the
 output section commands; (@pxref{Output Section Data}).
diff --git a/ld/testsuite/ld-scripts/data.exp b/ld/testsuite/ld-scripts/data.exp
index 3de32279b97..c3198a01ad2 100644
--- a/ld/testsuite/ld-scripts/data.exp
+++ b/ld/testsuite/ld-scripts/data.exp
@@ -29,6 +29,7 @@ if { [is_pecoff_format] } then {
 run_dump_test data
 run_dump_test fill
 run_dump_test fill16
+run_dump_test fill2
 run_dump_test pr27100
 
 set LDFLAGS $old_LDFLAGS
diff --git a/ld/testsuite/ld-scripts/fill2.d b/ld/testsuite/ld-scripts/fill2.d
new file mode 100644
index 00000000000..f913a82a017
--- /dev/null
+++ b/ld/testsuite/ld-scripts/fill2.d
@@ -0,0 +1,12 @@
+#source: fill_0.s
+#ld: -T fill2.t
+#readelf: -x.foo
+#notarget: ![is_elf_format]
+# See PR 30865 - a fill value expressed as a simple hexadecimal
+# number behaves differently from other fill values.
+
+Hex dump of section '.foo':
+  0x00000000 00000000 00000090 91919191 00000092 ................
+  0x00000010 00000093 00025000 00969500 00000097 ................
+  0x00000020 00010203 04050607 04050607 04050607 ................
+  0x00000030 08090a0b ffffffff .*
diff --git a/ld/testsuite/ld-scripts/fill2.t b/ld/testsuite/ld-scripts/fill2.t
new file mode 100644
index 00000000000..1ad9e46bc60
--- /dev/null
+++ b/ld/testsuite/ld-scripts/fill2.t
@@ -0,0 +1,29 @@
+SECTIONS {
+  .foo :
+  {
+    . += 4;
+    FILL (144)      # Decimal values zero extend to 4 bytes.  Fills with: 00 00 00 90
+    . += 4;
+    FILL (0x91)     # Hex values do not zero extend.  Fills with: 91 91 91 91
+    . += 4;
+    FILL ($92)      # A dollar prefix indicates a hex values that does zero extend.  Fills with: 00 00 00 92
+    . += 4;
+    FILL (93H)      # An H suffix does the same.  Fills with: 00 00 00 93
+    . += 4;
+    FILL (0x94K)    # A hex value with a manitude suffix zero extends.  Fills with: 00 02 50 00
+    . += 4;
+    FILL (0x009695) # Zeros in hex values are significant.  Values are big-endian.  Fills with: 00 96 95 00
+    . += 4;
+    FILL (0x90+0x7) # An expression containing hex values also zero extends.  Fills with: 00 00 00 97
+    . += 4;
+    FILL (0x0001020304050607) # Hex values can be used to specify fills with more than 4 bytes.  Fills with: 00 01 02 03 04 05 06 07
+    . += 8;
+    FILL ($0001020304050607)  # But non-hex or $-hex or suffix-hex values cannot.  Fills with 04 05 06 07 04 05 06 07
+    . += 8;
+    FILL (0x08090a0b0c0d0e0f) # Extra bytes at the end of a value are silently ignored.  Fills with 08 09 0a 0b
+    . += 4;
+    LONG(0xffffffff)
+  } =0
+
+  /DISCARD/ : { *(*) }  
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-11-06 14:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-06 14:00 [binutils-gdb] ld: =fillexp different behaviors for hexidecimal literal Nick Clifton

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