public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v4 0/7] ASCII output section command
@ 2024-07-08 17:49 binutils
  2024-07-08 17:49 ` [PATCH v4 1/7] ldlex.l: Add ASCII token binutils
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: binutils @ 2024-07-08 17:49 UTC (permalink / raw)
  To: binutils; +Cc: nickc

Implement the ASCII command for output sections

Introduce a command which adds a fixed size string.

   ASCII <size>  ',' <string>

Size specified in command. 
If the string is shorter than the allocated area, it is zero filled
If the string is longer than the allocated area, it is truncated
and a NUL character is added at the end.

Corrected ld.texi to use new syntax in index
Use hyphen instead of double quote in index, also for ASCIZ
More examples on misformed ASCII commands
Fix off-by-one error in lang_add_string
Fix whitespace error in ldgram.y and testsuite

Note: Will be merged into fewer patches after acceptance in final version

[PATCH v4 1/7] ldlex.l: Add ASCII token
[PATCH v4 2/7] ldgram.y: Add ASCII parsing
[PATCH v4 3/7] ldlang.*: process ASCII command
[PATCH v4 4/7] ld.texi: Add ASCII to info file
[PATCH v4 5/7] Add testsuite for ASCII command
[PATCH v4 6/7] NEWS: Add ASCII command
[PATCH v4 7/7] ChangeLog: Add ASCII command


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

* [PATCH v4 1/7] ldlex.l: Add ASCII token
  2024-07-08 17:49 [PATCH v4 0/7] ASCII output section command binutils
@ 2024-07-08 17:49 ` binutils
  2024-07-08 17:49 ` [PATCH v4 2/7] ldgram.y: Add ASCII parsing binutils
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: binutils @ 2024-07-08 17:49 UTC (permalink / raw)
  To: binutils; +Cc: nickc, Ulf Samuelsson

From: Ulf Samuelsson <ulf@emagii.com>

Signed-off-by: Ulf Samuelsson <ulf@emagii.com>
---
 ld/ldlex.l | 1 +
 1 file changed, 1 insertion(+)

diff --git a/ld/ldlex.l b/ld/ldlex.l
index aa613100db0..c67c83689d7 100644
--- a/ld/ldlex.l
+++ b/ld/ldlex.l
@@ -311,6 +311,7 @@ V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)*
 <WILD>"LONG"				{ RTOKEN(LONG); }
 <WILD>"SHORT"				{ RTOKEN(SHORT); }
 <WILD>"BYTE"				{ RTOKEN(BYTE); }
+<WILD>"ASCII"				{ RTOKEN(ASCII); }
 <WILD>"ASCIZ"				{ RTOKEN(ASCIZ); }
 <WILD>"LINKER_VERSION"			{ RTOKEN(LINKER_VERSION); }
 <SCRIPT>"NOFLOAT"			{ RTOKEN(NOFLOAT); }
-- 
2.17.1


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

* [PATCH v4 2/7] ldgram.y: Add ASCII parsing
  2024-07-08 17:49 [PATCH v4 0/7] ASCII output section command binutils
  2024-07-08 17:49 ` [PATCH v4 1/7] ldlex.l: Add ASCII token binutils
@ 2024-07-08 17:49 ` binutils
  2024-07-08 17:49 ` [PATCH v4 3/7] ldlang.*: process ASCII command binutils
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: binutils @ 2024-07-08 17:49 UTC (permalink / raw)
  To: binutils; +Cc: nickc, Ulf Samuelsson

From: Ulf Samuelsson <ulf@emagii.com>

Signed-off-by: Ulf Samuelsson <ulf@emagii.com>
---
 ld/ldgram.y | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/ld/ldgram.y b/ld/ldgram.y
index 7c04025f526..a6255969857 100644
--- a/ld/ldgram.y
+++ b/ld/ldgram.y
@@ -128,7 +128,7 @@ static void yyerror (const char *);
 %right UNARY
 %token END
 %left <token> '('
-%token <token> ALIGN_K BLOCK BIND QUAD SQUAD LONG SHORT BYTE ASCIZ
+%token <token> ALIGN_K BLOCK BIND QUAD SQUAD LONG SHORT BYTE ASCII ASCIZ
 %token SECTIONS PHDRS INSERT_K AFTER BEFORE LINKER_VERSION
 %token DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END DATA_SEGMENT_END
 %token SORT_BY_NAME SORT_BY_ALIGNMENT SORT_NONE
@@ -703,9 +703,18 @@ statement:
 		{
 		  lang_add_data ((int) $1, $3);
 		}
+	| ASCII mustbe_exp ',' NAME
+		{
+		  /* 'value' is a memory leak, do we care?  */
+		  etree_type *value = $2;
+		  if (value->type.node_code == INT)
+		    lang_add_string (value->value.value, $4);
+		  else
+		    einfo (_("%X%P:%pS: ASCII expression must be an integer\n"), NULL);
+		}
 	| ASCIZ NAME
 		{
-		  lang_add_string ($2);
+		  lang_add_string (0, $2);
 		}
 	| FILL '(' fill_exp ')'
 		{
@@ -1175,10 +1184,10 @@ type:
 	|  COPY    { sectype = noalloc_section; }
 	|  INFO    { sectype = noalloc_section; }
 	|  OVERLAY { sectype = noalloc_section; }
-        |  READONLY '(' TYPE '=' exp ')' { sectype = typed_readonly_section; sectype_value = $5; }
+	|  READONLY '(' TYPE '=' exp ')' { sectype = typed_readonly_section; sectype_value = $5; }
 	|  READONLY { sectype = readonly_section; }
 	|  TYPE '=' exp { sectype = type_section; sectype_value = $3; }
-        ;
+	;
 
 atype:
 		'(' type ')'
-- 
2.17.1


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

* [PATCH v4 3/7] ldlang.*: process ASCII command
  2024-07-08 17:49 [PATCH v4 0/7] ASCII output section command binutils
  2024-07-08 17:49 ` [PATCH v4 1/7] ldlex.l: Add ASCII token binutils
  2024-07-08 17:49 ` [PATCH v4 2/7] ldgram.y: Add ASCII parsing binutils
@ 2024-07-08 17:49 ` binutils
  2024-07-08 17:49 ` [PATCH v4 4/7] ld.texi: Add ASCII to info file binutils
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: binutils @ 2024-07-08 17:49 UTC (permalink / raw)
  To: binutils; +Cc: nickc, Ulf Samuelsson

From: Ulf Samuelsson <ulf@emagii.com>

Signed-off-by: Ulf Samuelsson <ulf@emagii.com>
---
 ld/ldlang.c | 65 +++++++++++++++++++++++++++++++++++++++++------------
 ld/ldlang.h |  2 +-
 2 files changed, 52 insertions(+), 15 deletions(-)

diff --git a/ld/ldlang.c b/ld/ldlang.c
index 9e8cc224f4d..902e82ecf59 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -8663,15 +8663,20 @@ lang_add_data (int type, union etree_union *exp)
   new_stmt->type = type;
 }
 
-void
-lang_add_string (const char *s)
+/* Convert escape codes in S.
+   Supports \n, \r, \t and \NNN octals.
+   Returns a copy of S in a malloc'ed buffer.  */
+
+static char *
+convert_string (const char * s)
 {
-  bfd_vma  len = strlen (s);
-  bfd_vma  i;
-  bool     escape = false;
+  size_t  len = strlen (s);
+  size_t  i;
+  bool    escape = false;
+  char *  buffer = xmalloc (len + 1);
+  char *  b;
 
-  /* Add byte expressions until end of string.  */
-  for (i = 0 ; i < len; i++)
+  for (i = 0, b = buffer; i < len; i++)
     {
       char c = *s++;
 
@@ -8729,21 +8734,53 @@ lang_add_string (const char *s)
 	      }
 	      break;
 	    }
-
-	  lang_add_data (BYTE, exp_intop (c));
 	  escape = false;
 	}
       else
 	{
 	  if (c == '\\')
-	    escape = true;
-	  else
-	    lang_add_data (BYTE, exp_intop (c));
+	    {
+	      escape = true;
+	      continue;
+	    }
 	}
+
+      * b ++ = c;
+    }
+
+  * b = 0;
+  return buffer;
+}
+
+void
+lang_add_string (size_t size, const char *s)
+{
+  size_t  len;
+  size_t  i;
+  char *  string;
+
+  string = convert_string (s);
+  len = strlen (string);
+
+  /* Check if it is ASCIZ command (len == 0) */
+  if (size == 0)
+    /* Make sure that we include the terminating NUL byte.  */
+    size = len + 1;
+  else if (len > size)
+    {
+      len = size - 1;
+
+      einfo (_("%P:%pS: warning: ASCII string does not fit in allocated space,"
+		" truncated, terminating with NUL.\n"), NULL);
     }
 
-  /* Remeber to terminate the string.  */
-  lang_add_data (BYTE, exp_intop (0));
+  for (i = 0; i < len; i++)
+    lang_add_data (BYTE, exp_intop (string[i]));
+
+  while (i++ < size)
+    lang_add_data (BYTE, exp_intop ('\0'));
+
+  free (string);
 }
 
 /* Create a new reloc statement.  RELOC is the BFD relocation type to
diff --git a/ld/ldlang.h b/ld/ldlang.h
index f36e04c586a..bf1b602799c 100644
--- a/ld/ldlang.h
+++ b/ld/ldlang.h
@@ -656,7 +656,7 @@ extern void pop_stat_ptr
 extern void lang_add_data
   (int, union etree_union *);
 extern void lang_add_string
-  (const char *);
+  (size_t, const char *s);
 extern void lang_add_reloc
   (bfd_reloc_code_real_type, reloc_howto_type *, asection *, const char *,
    union etree_union *);
-- 
2.17.1


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

* [PATCH v4 4/7] ld.texi: Add ASCII to info file
  2024-07-08 17:49 [PATCH v4 0/7] ASCII output section command binutils
                   ` (2 preceding siblings ...)
  2024-07-08 17:49 ` [PATCH v4 3/7] ldlang.*: process ASCII command binutils
@ 2024-07-08 17:49 ` binutils
  2024-07-08 17:49 ` [PATCH v4 5/7] Add testsuite for ASCII command binutils
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: binutils @ 2024-07-08 17:49 UTC (permalink / raw)
  To: binutils; +Cc: nickc, Ulf Samuelsson

From: Ulf Samuelsson <ulf@emagii.com>

Signed-off-by: Ulf Samuelsson <ulf@emagii.com>
---
 ld/ld.texi | 42 ++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 40 insertions(+), 2 deletions(-)

diff --git a/ld/ld.texi b/ld/ld.texi
index 89e3913317a..70ff492ab2b 100644
--- a/ld/ld.texi
+++ b/ld/ld.texi
@@ -5642,7 +5642,8 @@ C identifiers because they contain a @samp{.} character.
 @cindex data
 @cindex section data
 @cindex output section data
-@kindex ASCIZ ``@var{string}''
+@kindex ASCIZ "@var{string}"
+@kindex ASCII @var{expression}, "@var{string}"
 @kindex BYTE(@var{expression})
 @kindex SHORT(@var{expression})
 @kindex LONG(@var{expression})
@@ -5681,7 +5682,7 @@ endianness of the first input object file.
 
 You can include a zero-terminated string in an output section by using
 @code{ASCIZ}.  The keyword is followed by a string which is stored at
-the current value of the location counter adding a zero byte at the
+the current value of the location counter adding a NUL byte at the
 end.  If the string includes spaces it must be enclosed in double
 quotes.  The string may contain '\n', '\r', '\t' and octal numbers.
 Hex numbers are not supported.
@@ -5691,6 +5692,43 @@ For example, this string of 16 characters will create a 17 byte area
   ASCIZ "This is 16 bytes"
 @end smallexample
 
+You can include a fixed size string in an output section by using
+@code{ASCII}.  The keyword is followed by a size and a string
+separated by a comma.  The string is stored at the current
+value of the location counter. If the string is shorted than the allocated
+size, NUL character are added at the end to fill up to the specified size.
+Note the fill value is ignored for this padding.
+If the string includes spaces it must be enclosed in double
+quotes.  The string may contain '\n', '\r', '\t' and octal numbers.
+Hex numbers are not supported.
+
+If the string is too long, a warning is issued and the string is
+truncated.  The string will still be zero-terminated in this case.
+
+If the expression evaluates to zero then the directive will be treated
+as if it were @code{ASCIZ} instead.
+
+Example: This is string of 16 characters and will create a 16 byte
+area, not zero-terminated.
+@smallexample
+  ASCII 16, "This is 16 bytes"
+@end smallexample
+
+
+Example: This is string of 10 characters in a 16 byte area, unused locations are filled with NUL characters.
+@smallexample
+  ASCII 16, "This is 16"
+@end smallexample
+
+
+Example: This is string of 15 characters in a 16 byte area, ending with a NUL character.
+@smallexample
+  ASCII 16, "01234567890123456789"
+@end smallexample
+The last character of the string will be "4".
+A warning will be issued to indicate that the string does not fit.
+
+
 Note---these commands only work inside a section description and not
 between them, so the following will produce an error from the linker:
 @smallexample
-- 
2.17.1


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

* [PATCH v4 5/7] Add testsuite for ASCII command
  2024-07-08 17:49 [PATCH v4 0/7] ASCII output section command binutils
                   ` (3 preceding siblings ...)
  2024-07-08 17:49 ` [PATCH v4 4/7] ld.texi: Add ASCII to info file binutils
@ 2024-07-08 17:49 ` binutils
  2024-07-08 17:49 ` [PATCH v4 6/7] NEWS: Add " binutils
  2024-07-08 17:49 ` [PATCH v4 7/7] ChangeLog: " binutils
  6 siblings, 0 replies; 8+ messages in thread
From: binutils @ 2024-07-08 17:49 UTC (permalink / raw)
  To: binutils; +Cc: nickc, Ulf Samuelsson

From: Ulf Samuelsson <ulf@emagii.com>

Signed-off-by: Ulf Samuelsson <ulf@emagii.com>
---
 ld/testsuite/ld-scripts/ascii.d    | 25 ++++++++++++++++++++
 ld/testsuite/ld-scripts/ascii.s    | 11 +++++++++
 ld/testsuite/ld-scripts/ascii.t    | 37 ++++++++++++++++++++++++++++++
 ld/testsuite/ld-scripts/script.exp |  1 +
 4 files changed, 74 insertions(+)
 create mode 100644 ld/testsuite/ld-scripts/ascii.d
 create mode 100644 ld/testsuite/ld-scripts/ascii.s
 create mode 100644 ld/testsuite/ld-scripts/ascii.t

diff --git a/ld/testsuite/ld-scripts/ascii.d b/ld/testsuite/ld-scripts/ascii.d
new file mode 100644
index 00000000000..ff3bd328c15
--- /dev/null
+++ b/ld/testsuite/ld-scripts/ascii.d
@@ -0,0 +1,25 @@
+#source: ascii.s
+#ld: -T ascii.t
+#objdump: -s -j .header
+#target: [is_elf_format] [is_coff_format]
+#notarget: tic4x-*-* tic54x-*-*
+
+.*:     file format .*
+
+Contents of section .header:
+ .... 70726f67 72616d20 6e616d65 00000000  program name....
+ .... 656d7074 79000000 00000000 00000000  empty...........
+ .... 00000000 00000000 00000000 00000000  ................
+ .... 00000000 00000000 00000000 00000000  ................
+ .... 00000000 00000000 00000000 00000000  ................
+ .... 636f6d6d 656e7420 310a0000 00000000  comment 1.......
+ .... 00000000 00000000 00000000 00000000  ................
+ .... 636f6d6d 656e7420 320a0000 00000000  comment 2.......
+ .... 00000000 00000000 00000000 00000000  ................
+ .... 636f6d6d 656e7420 330a0000 00000000  comment 3.......
+ .... 00000000 00000000 00000000 00000000  ................
+ .... 636f6d6d 656e7420 340a0000 00000000  comment 4.......
+ .... 00000000 00000000 49206d65 616e7420  ........I meant 
+ .... 746f2073 61793a20 54686973 20697320  to say: This is 
+ .... 77617920 746f6f20 6c6f6e67 00000000  way too long....
+#pass
diff --git a/ld/testsuite/ld-scripts/ascii.s b/ld/testsuite/ld-scripts/ascii.s
new file mode 100644
index 00000000000..af1022dea63
--- /dev/null
+++ b/ld/testsuite/ld-scripts/ascii.s
@@ -0,0 +1,11 @@
+	.extern ecc_start
+	.section .text
+main:
+	.long 0x45444F43
+	.long 0x12345678
+
+	.section .data
+	.long 0x9abcdef0
+
+	.section .bss
+	.long 0
diff --git a/ld/testsuite/ld-scripts/ascii.t b/ld/testsuite/ld-scripts/ascii.t
new file mode 100644
index 00000000000..93bac748834
--- /dev/null
+++ b/ld/testsuite/ld-scripts/ascii.t
@@ -0,0 +1,37 @@
+_start = 0x000000;
+
+SECTIONS
+{
+  . = 0x1000 + SIZEOF_HEADERS;
+
+  .header ALIGN (0x100) (READONLY) :
+    {
+      ASCII 16, "program name"
+      ASCII 64, "empty"
+      ASCII 4 * 8, "comment 1\n"
+      ASCII 32, "comment 2\n"
+      ASCII 32, "comment 3\n"
+      ASCII 24, "comment 4\n"
+      ASCII 64, "I meant to say: This is way too long"
+    }
+
+  .text ALIGN (0x100) :
+  {
+      entry = .;
+      *(.text)
+  }
+
+  .data : AT (0x400000)
+  {
+	*(.data)
+  }
+
+  . = ALIGN(0x20);
+
+  .bss :
+  {
+	*(.bss)
+  }
+
+  /DISCARD/ : { *(*) }
+}
diff --git a/ld/testsuite/ld-scripts/script.exp b/ld/testsuite/ld-scripts/script.exp
index 213383de962..bc3fffe92d8 100644
--- a/ld/testsuite/ld-scripts/script.exp
+++ b/ld/testsuite/ld-scripts/script.exp
@@ -228,6 +228,7 @@ foreach test_script $test_script_list {
 }
 
 run_dump_test "asciz"
+run_dump_test "ascii"
 run_dump_test "align-with-input"
 run_dump_test "pr20302"
 run_dump_test "output-section-types"
-- 
2.17.1


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

* [PATCH v4 6/7] NEWS: Add ASCII command
  2024-07-08 17:49 [PATCH v4 0/7] ASCII output section command binutils
                   ` (4 preceding siblings ...)
  2024-07-08 17:49 ` [PATCH v4 5/7] Add testsuite for ASCII command binutils
@ 2024-07-08 17:49 ` binutils
  2024-07-08 17:49 ` [PATCH v4 7/7] ChangeLog: " binutils
  6 siblings, 0 replies; 8+ messages in thread
From: binutils @ 2024-07-08 17:49 UTC (permalink / raw)
  To: binutils; +Cc: nickc, Ulf Samuelsson

From: Ulf Samuelsson <ulf@emagii.com>

Signed-off-by: Ulf Samuelsson <ulf@emagii.com>
---
 ld/NEWS | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/ld/NEWS b/ld/NEWS
index e0b9341a8ef..58144afd7a4 100644
--- a/ld/NEWS
+++ b/ld/NEWS
@@ -1,5 +1,9 @@
 -*- text -*-
 
+* The linker script syntax has a new command for output sections: ASCII (<size>) "string"
+  This will insert a string of the specified length at the current location.
+  It will be zero-filled if the string is shorter than <size> and truncated if longer.
+
 * Add -z isa-level-report=[none|all|needed|used] to the x86 ELF linker
   to report needed and used x86-64 ISA levels.
 
-- 
2.17.1


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

* [PATCH v4 7/7] ChangeLog: Add ASCII command
  2024-07-08 17:49 [PATCH v4 0/7] ASCII output section command binutils
                   ` (5 preceding siblings ...)
  2024-07-08 17:49 ` [PATCH v4 6/7] NEWS: Add " binutils
@ 2024-07-08 17:49 ` binutils
  6 siblings, 0 replies; 8+ messages in thread
From: binutils @ 2024-07-08 17:49 UTC (permalink / raw)
  To: binutils; +Cc: nickc, Ulf Samuelsson

From: Ulf Samuelsson <ulf@emagii.com>

Signed-off-by: Ulf Samuelsson <ulf@emagii.com>
---
 ld/ChangeLog | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/ld/ChangeLog b/ld/ChangeLog
index ab881707e11..0d7fc62a984 100644
--- a/ld/ChangeLog
+++ b/ld/ChangeLog
@@ -1,3 +1,18 @@
+2024-06-28  Ulf Samuelsson <binutils@emagii.com>
+	    Nick Clifton  <nickc@redhat.com>
+
+	* ldlex.l: Add ASCII token.
+	* ldgram.y: Add parsing of the ASCII command.
+	* ldlang.c (lang_add_string): Add maximum size parameter.  Move
+	escape character handling code into separate function.
+	* ldlang.h (lang_add_string): Update prototype.
+	* NEWS: Mention the new feature.
+	* ld.texi (Output Section Data): Document the new directives.
+	* testsuite/ld-scripts/ascii.d: New test driver.
+	* testsuite/ld-scripts/ascii.s: New test assembler source.
+	* testsuite/ld-scripts/ascii.t: New test script.
+	* testsuite/ld-scripts/script.exp: Run the new test.
+
 2024-01-15  Nick Clifton  <nickc@redhat.com>
 
 	* configure: Regenerate.
-- 
2.17.1


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

end of thread, other threads:[~2024-07-08 17:49 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-08 17:49 [PATCH v4 0/7] ASCII output section command binutils
2024-07-08 17:49 ` [PATCH v4 1/7] ldlex.l: Add ASCII token binutils
2024-07-08 17:49 ` [PATCH v4 2/7] ldgram.y: Add ASCII parsing binutils
2024-07-08 17:49 ` [PATCH v4 3/7] ldlang.*: process ASCII command binutils
2024-07-08 17:49 ` [PATCH v4 4/7] ld.texi: Add ASCII to info file binutils
2024-07-08 17:49 ` [PATCH v4 5/7] Add testsuite for ASCII command binutils
2024-07-08 17:49 ` [PATCH v4 6/7] NEWS: Add " binutils
2024-07-08 17:49 ` [PATCH v4 7/7] ChangeLog: " binutils

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