public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v3 0/6] ASCIZ Command for output section
@ 2023-02-13 16:11 binutils
  2023-02-13 16:11 ` [PATCH v3 1/6] Document the ASCIZ command binutils
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: binutils @ 2023-02-13 16:11 UTC (permalink / raw)
  To: binutils; +Cc: nickc

Introduce an ASCIZ command for an output section
When generating a header for an embedded system
there is often a need to add text information.

There are arguments for generating the header in the linker
instead of compiling the header as part of the program.
The lack of support for strings makes this process a bit unwieldy.

The ASCIZ command allows you to specify a zero-terminated string as a parameter.

Example:

    ASCIZ "This is a string"

The string contains 16 characters, but a NUL character is added to the end,
so the areas reserved is 16+1 = 17 characters.

The string may contain '\n', '\r', '\t' and octals, but not hex characters.

Ideally, there should be a command which reserves a fixed size area.

I:E:

    ASCII 20, "This is a string"

but I have failed to get make this work in 'ld',
so this patch series is limited to ASCIZ.

[PATCH v3 1/6] Document the ASCIZ command
[PATCH v3 2/6] Add ASCIZ to NEWS
[PATCH v3 3/6] Add ASCIZ to testsuite
[PATCH v3 4/6] ldlex.l: Add ASCIZ token
[PATCH v3 5/6] ldgram.y: Add 'ASCIZ <string>' command
[PATCH v3 6/6] Parse ASCIZ command


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

* [PATCH v3 1/6] Document the ASCIZ command
  2023-02-13 16:11 [PATCH v3 0/6] ASCIZ Command for output section binutils
@ 2023-02-13 16:11 ` binutils
  2023-02-13 16:11 ` [PATCH v3 2/6] Add ASCIZ to NEWS binutils
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: binutils @ 2023-02-13 16:11 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 | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/ld/ld.texi b/ld/ld.texi
index 36005dc2b0d..0a3d4adfaa0 100644
--- a/ld/ld.texi
+++ b/ld/ld.texi
@@ -5308,6 +5308,7 @@ C identifiers because they contain a @samp{.} character.
 @cindex data
 @cindex section data
 @cindex output section data
+@kindex ASCIZ @var{string}
 @kindex BYTE(@var{expression})
 @kindex SHORT(@var{expression})
 @kindex LONG(@var{expression})
@@ -5338,6 +5339,16 @@ target are 32 bits, an expression is computed as 32 bits.  In this case
 @code{QUAD} stores a 32 bit value zero extended to 64 bits, and
 @code{SQUAD} stores a 32 bit value sign extended to 64 bits.
 
+You can include a zero-terminated string in an output section by using
+@code{ASCIZ} as an output section command. The keyword is followed by a string
+which is stored at the current value of the location counter adding a zero byte
+at the end.
+
+For example, this string of 16 characters will create a 17 byte area
+@smallexample
+ASCIZ     "This is 16 bytes"
+@end smallexample
+
 If the object file format of the output file has an explicit endianness,
 which is the normal case, the value will be stored in that endianness.
 When the object file format does not have an explicit endianness, as is
-- 
2.17.1


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

* [PATCH v3 2/6] Add ASCIZ to NEWS
  2023-02-13 16:11 [PATCH v3 0/6] ASCIZ Command for output section binutils
  2023-02-13 16:11 ` [PATCH v3 1/6] Document the ASCIZ command binutils
@ 2023-02-13 16:11 ` binutils
  2023-02-13 16:11 ` [PATCH v3 3/6] Add ASCIZ to testsuite binutils
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: binutils @ 2023-02-13 16:11 UTC (permalink / raw)
  To: binutils; +Cc: nickc, Ulf Samuelsson

From: Ulf Samuelsson <binutils@emagii.com>

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

diff --git a/ld/NEWS b/ld/NEWS
index 9982ad0168d..f5a1f1a1304 100644
--- a/ld/NEWS
+++ b/ld/NEWS
@@ -1,5 +1,11 @@
 -*- text -*-
 
+* The linker has a new command for output sections.
+  ASCIZ "string"
+  This will insert the string at the current location adding a zero at the end.
+  The string may contain '\n', '\r', '\t' and octal numbers.
+  Hex numbers are not supported
+
 Changes in 2.40:
 
 * The linker has a new command line option to suppress the generation of any
-- 
2.17.1


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

* [PATCH v3 3/6] Add ASCIZ to testsuite
  2023-02-13 16:11 [PATCH v3 0/6] ASCIZ Command for output section binutils
  2023-02-13 16:11 ` [PATCH v3 1/6] Document the ASCIZ command binutils
  2023-02-13 16:11 ` [PATCH v3 2/6] Add ASCIZ to NEWS binutils
@ 2023-02-13 16:11 ` binutils
  2023-02-13 16:11 ` [PATCH v3 4/6] ldlex.l: Add ASCIZ token binutils
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: binutils @ 2023-02-13 16:11 UTC (permalink / raw)
  To: binutils; +Cc: nickc, Ulf Samuelsson

From: Ulf Samuelsson <binutils@emagii.com>

Signed-off-by: Ulf Samuelsson <binutils@emagii.com>
---
 ld/testsuite/ld-scripts/asciz.d | 14 ++++++++++++++
 ld/testsuite/ld-scripts/asciz.s |  6 ++++++
 ld/testsuite/ld-scripts/asciz.t | 23 +++++++++++++++++++++++
 3 files changed, 43 insertions(+)
 create mode 100644 ld/testsuite/ld-scripts/asciz.d
 create mode 100644 ld/testsuite/ld-scripts/asciz.s
 create mode 100644 ld/testsuite/ld-scripts/asciz.t

diff --git a/ld/testsuite/ld-scripts/asciz.d b/ld/testsuite/ld-scripts/asciz.d
new file mode 100644
index 00000000000..83cb61d6fb6
--- /dev/null
+++ b/ld/testsuite/ld-scripts/asciz.d
@@ -0,0 +1,14 @@
+#source: asciz.s
+#ld: -T asciz.t
+#objdump: -s -j .text
+#notarget: [is_aout_format]
+#xfail: tic4x-*-* tic54x-*-*
+
+.*:     file format .*
+
+Contents of section .text:
+ 10c0 434f4445 54686973 20697320 61207374  CODEThis is a st
+ 10d0 72696e67 00000000 00000000 00000000  ring............
+ 10e0 54686973 20697320 616e6f74 68657220  This is another 
+ 10f0 73747269 6e6700                      string.
+#pass
diff --git a/ld/testsuite/ld-scripts/asciz.s b/ld/testsuite/ld-scripts/asciz.s
new file mode 100644
index 00000000000..542dcd1a410
--- /dev/null
+++ b/ld/testsuite/ld-scripts/asciz.s
@@ -0,0 +1,6 @@
+	.section .text
+	.long 0x45444F43
+	.section .data
+	.long 0x9abcdef0
+	.section .bss
+	.long 0
diff --git a/ld/testsuite/ld-scripts/asciz.t b/ld/testsuite/ld-scripts/asciz.t
new file mode 100644
index 00000000000..986e00a1169
--- /dev/null
+++ b/ld/testsuite/ld-scripts/asciz.t
@@ -0,0 +1,23 @@
+MEMORY {
+  rom : ORIGIN = 0x00000, LENGTH = 0x10000
+  ram : ORIGIN = 0x10000, LENGTH = 0x10000
+}
+
+_start = 0x000000;
+SECTIONS
+{
+  . = 0x1000 + SIZEOF_HEADERS;
+  .text ALIGN (0x20) :
+    {
+      *(.text)
+      ASCIZ "This is a string"
+      . = ALIGN(0x20);
+      align_label = .;
+      ASCIZ "This is another string"
+      unalign_label = .;
+    }
+  .data : AT (0x10000) { *(.data) } >ram /* NO default AT>rom */
+  . = ALIGN(0x20);
+  .bss : { *(.bss) } >ram /* NO default AT>rom */
+  /DISCARD/ : { *(*) }
+}
-- 
2.17.1


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

* [PATCH v3 4/6] ldlex.l: Add ASCIZ token
  2023-02-13 16:11 [PATCH v3 0/6] ASCIZ Command for output section binutils
                   ` (2 preceding siblings ...)
  2023-02-13 16:11 ` [PATCH v3 3/6] Add ASCIZ to testsuite binutils
@ 2023-02-13 16:11 ` binutils
  2023-02-13 16:11 ` [PATCH v3 5/6] ldgram.y: Add 'ASCIZ <string>' command binutils
  2023-02-13 16:11 ` [PATCH v3 6/6] Parse ASCIZ command binutils
  5 siblings, 0 replies; 7+ messages in thread
From: binutils @ 2023-02-13 16:11 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 cf596530b20..32336cf0be2 100644
--- a/ld/ldlex.l
+++ b/ld/ldlex.l
@@ -309,6 +309,7 @@ V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)*
 <WILD>"LONG"				{ RTOKEN(LONG); }
 <WILD>"SHORT"				{ RTOKEN(SHORT); }
 <WILD>"BYTE"				{ RTOKEN(BYTE); }
+<WILD>"ASCIZ"				{ RTOKEN(ASCIZ); }
 <SCRIPT>"NOFLOAT"			{ RTOKEN(NOFLOAT); }
 <SCRIPT,EXPRESSION>"NOCROSSREFS"	{ RTOKEN(NOCROSSREFS); }
 <SCRIPT,EXPRESSION>"NOCROSSREFS_TO"	{ RTOKEN(NOCROSSREFS_TO); }
-- 
2.17.1


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

* [PATCH v3 5/6] ldgram.y: Add 'ASCIZ <string>' command
  2023-02-13 16:11 [PATCH v3 0/6] ASCIZ Command for output section binutils
                   ` (3 preceding siblings ...)
  2023-02-13 16:11 ` [PATCH v3 4/6] ldlex.l: Add ASCIZ token binutils
@ 2023-02-13 16:11 ` binutils
  2023-02-13 16:11 ` [PATCH v3 6/6] Parse ASCIZ command binutils
  5 siblings, 0 replies; 7+ messages in thread
From: binutils @ 2023-02-13 16:11 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 | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/ld/ldgram.y b/ld/ldgram.y
index fa5f01fef1d..67e75d43067 100644
--- a/ld/ldgram.y
+++ b/ld/ldgram.y
@@ -125,7 +125,7 @@ static int error_index;
 %right UNARY
 %token END
 %left <token> '('
-%token <token> ALIGN_K BLOCK BIND QUAD SQUAD LONG SHORT BYTE
+%token <token> ALIGN_K BLOCK BIND QUAD SQUAD LONG SHORT BYTE ASCIZ
 %token SECTIONS PHDRS INSERT_K AFTER BEFORE
 %token DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END DATA_SEGMENT_END
 %token SORT_BY_NAME SORT_BY_ALIGNMENT SORT_NONE
@@ -668,7 +668,10 @@ statement:
 		{
 		  lang_add_data ((int) $1, $3);
 		}
-
+	| ASCIZ NAME
+		{
+		  lang_add_stringz($2);
+		}
 	| FILL '(' fill_exp ')'
 		{
 		  lang_add_fill ($3);
-- 
2.17.1


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

* [PATCH v3 6/6] Parse ASCIZ command
  2023-02-13 16:11 [PATCH v3 0/6] ASCIZ Command for output section binutils
                   ` (4 preceding siblings ...)
  2023-02-13 16:11 ` [PATCH v3 5/6] ldgram.y: Add 'ASCIZ <string>' command binutils
@ 2023-02-13 16:11 ` binutils
  5 siblings, 0 replies; 7+ messages in thread
From: binutils @ 2023-02-13 16:11 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 | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 ld/ldlang.h |  4 +++
 2 files changed, 76 insertions(+)

diff --git a/ld/ldlang.c b/ld/ldlang.c
index 84a2914fc26..588a27034ba 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -8360,6 +8360,78 @@ lang_add_data (int type, union etree_union *exp)
   new_stmt->type = type;
 }
 
+void
+lang_add_string (bfd_vma size, char *s)
+{
+  bfd_vma stringlen = strlen(s) + 1;    /* Add one for terminating '\0' */
+  bfd_vma fill_len = 0;
+  int     escape = 0;
+
+  if (size == 0) {  /* Zero terminated string */
+    size = stringlen;
+  } else if (size > stringlen) {    /* Fix Size string */
+    fill_len = size - stringlen;
+  } else if (size > stringlen) {
+    /* We have an error */
+    einfo (_("%P:%pS: warning: string does not fit \"%s\"\n"), NULL, s);
+  }
+  /* Add byte expressions until end of string */
+  for (bfd_vma i = 0 ; i < size ; i++) {
+    if (escape) {
+      char *p = &s[i];
+      char c = *p;
+      if (c == 't') {
+        *p = '\t';
+      } else if (c == 'n') {
+        *p = '\n';
+      } else if (c == 'r') {
+        *p = '\r';
+      } else if ((c >= '0') && (c <= '7')) {
+        int value = c;
+        c = p[1];
+        if ((c >= '0') && (c <= '7')) {
+          value <<= 3;
+          value += (c - '0');
+          i++;
+          c = p[2];
+          if ((c >= '0') && (c <= '7')) {
+            value <<= 3;
+            value += (c - '0');
+            i++;
+          }
+        }
+        if (value <= 0xff) {
+          s[i] = value;
+        } else {  /* octal: \777 is treated as '\077' + '7' */
+          value >>= 3;
+          i--;
+          s[i] = value;
+        }
+      } else {
+        /* whatever we have */
+      }
+      lang_add_data (BYTE, exp_intop(s[i]));
+      escape = 0;
+    } else {
+      if (s[i] == '\\') {
+        escape = 1;
+      } else {
+        lang_add_data (BYTE, exp_intop(s[i]));
+      }
+    }
+  }
+  /* Add byte expressions for filling to the end of the string */
+  for (bfd_vma i = 0 ; i < fill_len ; i++) {
+    lang_add_data (BYTE, exp_intop(s[i]));
+  }
+}
+
+void
+lang_add_stringz (char *s)
+{
+    lang_add_string (0, s);
+}
+
 /* Create a new reloc statement.  RELOC is the BFD relocation type to
    generate.  HOWTO is the corresponding howto structure (we could
    look this up, but the caller has already done so).  SECTION is the
diff --git a/ld/ldlang.h b/ld/ldlang.h
index 24c42f48218..a77a4802e3e 100644
--- a/ld/ldlang.h
+++ b/ld/ldlang.h
@@ -646,6 +646,10 @@ extern void pop_stat_ptr
   (void);
 extern void lang_add_data
   (int type, union etree_union *);
+extern void
+lang_add_string (bfd_vma size, char *s);
+extern void
+lang_add_stringz (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] 7+ messages in thread

end of thread, other threads:[~2023-02-13 16:11 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-13 16:11 [PATCH v3 0/6] ASCIZ Command for output section binutils
2023-02-13 16:11 ` [PATCH v3 1/6] Document the ASCIZ command binutils
2023-02-13 16:11 ` [PATCH v3 2/6] Add ASCIZ to NEWS binutils
2023-02-13 16:11 ` [PATCH v3 3/6] Add ASCIZ to testsuite binutils
2023-02-13 16:11 ` [PATCH v3 4/6] ldlex.l: Add ASCIZ token binutils
2023-02-13 16:11 ` [PATCH v3 5/6] ldgram.y: Add 'ASCIZ <string>' command binutils
2023-02-13 16:11 ` [PATCH v3 6/6] Parse ASCIZ command 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).