public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v2 0/4] ASCIZ Command for output section
@ 2023-02-13 12:22 binutils
  2023-02-13 12:22 ` [PATCH v2 1/5] ldlex.l: Add ASCIZ token binutils
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: binutils @ 2023-02-13 12:22 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.

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 1/5] ldlex.l: Add ASCIZ token
[PATCH 2/5] ldgram.y: Add 'ASCIZ <string>' command
[PATCH 3/5] Parse ASCIZ command
[PATCH 4/5] Document the ASCIZ command
[PATCH 5/5] Use lang_add_data


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

* [PATCH v2 1/5] ldlex.l: Add ASCIZ token
  2023-02-13 12:22 [PATCH v2 0/4] ASCIZ Command for output section binutils
@ 2023-02-13 12:22 ` binutils
  2023-02-13 12:22 ` [PATCH v2 2/5] ldgram.y: Add 'ASCIZ <string>' command binutils
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: binutils @ 2023-02-13 12:22 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 v2 2/5] ldgram.y: Add 'ASCIZ <string>' command
  2023-02-13 12:22 [PATCH v2 0/4] ASCIZ Command for output section binutils
  2023-02-13 12:22 ` [PATCH v2 1/5] ldlex.l: Add ASCIZ token binutils
@ 2023-02-13 12:22 ` binutils
  2023-02-13 12:22 ` [PATCH v2 3/5] Parse ASCIZ command binutils
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: binutils @ 2023-02-13 12:22 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 v2 3/5] Parse ASCIZ command
  2023-02-13 12:22 [PATCH v2 0/4] ASCIZ Command for output section binutils
  2023-02-13 12:22 ` [PATCH v2 1/5] ldlex.l: Add ASCIZ token binutils
  2023-02-13 12:22 ` [PATCH v2 2/5] ldgram.y: Add 'ASCIZ <string>' command binutils
@ 2023-02-13 12:22 ` binutils
  2023-02-13 12:29   ` Ulf Samuelsson
  2023-02-13 12:22 ` [PATCH v2 4/5] Document the " binutils
  2023-02-13 12:22 ` [PATCH v2 5/5] Use lang_add_data binutils
  4 siblings, 1 reply; 7+ messages in thread
From: binutils @ 2023-02-13 12:22 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 | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 ld/ldlang.h |  4 +++
 2 files changed, 77 insertions(+)

diff --git a/ld/ldlang.c b/ld/ldlang.c
index 84a2914fc26..010e6e0fe22 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -8360,6 +8360,79 @@ lang_add_data (int type, union etree_union *exp)
   new_stmt->type = type;
 }
 
+void
+lang_add_string (bfd_vma size, char *s)
+{
+  lang_data_statement_type *new_stmt;
+  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 <= '3')) {
+        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++;
+          }
+        }
+          s[i] = value;
+      } else {
+        /* whatever we have */
+      }
+      new_stmt = new_stat (lang_data_statement, stat_ptr);
+      new_stmt->exp = exp_intop(s[i]);
+      new_stmt->type = BYTE;
+      escape = 0;
+    } else {
+      if (s[i] == '\\') {
+        escape = 1;
+      } else {
+        new_stmt = new_stat (lang_data_statement, stat_ptr);
+        new_stmt->exp = exp_intop(s[i]);
+        new_stmt->type = BYTE;
+      }
+    }
+  }
+  /* Add byte expressions for filling to the end of the string */
+  for (bfd_vma i = 0 ; i < fill_len ; i++) {
+    new_stmt = new_stat (lang_data_statement, stat_ptr);
+    new_stmt->exp = exp_intop(0);
+    new_stmt->type = BYTE;
+  }
+}
+
+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

* [PATCH v2 4/5] Document the ASCIZ command
  2023-02-13 12:22 [PATCH v2 0/4] ASCIZ Command for output section binutils
                   ` (2 preceding siblings ...)
  2023-02-13 12:22 ` [PATCH v2 3/5] Parse ASCIZ command binutils
@ 2023-02-13 12:22 ` binutils
  2023-02-13 12:22 ` [PATCH v2 5/5] Use lang_add_data binutils
  4 siblings, 0 replies; 7+ messages in thread
From: binutils @ 2023-02-13 12:22 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 v2 5/5] Use lang_add_data
  2023-02-13 12:22 [PATCH v2 0/4] ASCIZ Command for output section binutils
                   ` (3 preceding siblings ...)
  2023-02-13 12:22 ` [PATCH v2 4/5] Document the " binutils
@ 2023-02-13 12:22 ` binutils
  4 siblings, 0 replies; 7+ messages in thread
From: binutils @ 2023-02-13 12:22 UTC (permalink / raw)
  To: binutils; +Cc: nickc, Ulf Samuelsson

From: Ulf Samuelsson <binutils@emagii.com>

Signed-off-by: Ulf Samuelsson <binutils@emagii.com>
---
 ld/ldlang.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)

diff --git a/ld/ldlang.c b/ld/ldlang.c
index 010e6e0fe22..b6d75fc446c 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -8363,7 +8363,6 @@ lang_add_data (int type, union etree_union *exp)
 void
 lang_add_string (bfd_vma size, char *s)
 {
-  lang_data_statement_type *new_stmt;
   bfd_vma stringlen = strlen(s) + 1;    /* Add one for terminating '\0' */
   bfd_vma fill_len = 0;
   int     escape = 0;
@@ -8405,25 +8404,19 @@ lang_add_string (bfd_vma size, char *s)
       } else {
         /* whatever we have */
       }
-      new_stmt = new_stat (lang_data_statement, stat_ptr);
-      new_stmt->exp = exp_intop(s[i]);
-      new_stmt->type = BYTE;
+      lang_add_data (BYTE, exp_intop(s[i]));
       escape = 0;
     } else {
       if (s[i] == '\\') {
         escape = 1;
       } else {
-        new_stmt = new_stat (lang_data_statement, stat_ptr);
-        new_stmt->exp = exp_intop(s[i]);
-        new_stmt->type = BYTE;
+        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++) {
-    new_stmt = new_stat (lang_data_statement, stat_ptr);
-    new_stmt->exp = exp_intop(0);
-    new_stmt->type = BYTE;
+    lang_add_data (BYTE, exp_intop(s[i]));
   }
 }
 
-- 
2.17.1


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

* Re: [PATCH v2 3/5] Parse ASCIZ command
  2023-02-13 12:22 ` [PATCH v2 3/5] Parse ASCIZ command binutils
@ 2023-02-13 12:29   ` Ulf Samuelsson
  0 siblings, 0 replies; 7+ messages in thread
From: Ulf Samuelsson @ 2023-02-13 12:29 UTC (permalink / raw)
  To: binutils

Comment on:

einfo (_("%P:%pS: warning: string does not fit \"%s\"\n"), NULL, s);

This output command is needed once there is an
ASCII <size>, <string>
command.

The ASCIZ command supported in this patchset will never trigger this.

I guess this will need translation once the ASCII command is added.

What is the procedure when you want to add strings like this?
I certainly do not know how to translate to Mandarin ;-)

Best Regards
Ulf Samuelsson


Den 2023-02-13 kl. 13:22, skrev Ulf Samuelsson via Binutils:
> einfo (_("%P:%pS: warning: string does not fit \"%s\"\n"), NULL, s);

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

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

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