public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: binutils@emagii.com
To: binutils@sourceware.org
Cc: nickc@redhat.com, Ulf Samuelsson <ulf@emagii.com>
Subject: [PATCH v3 6/6] Parse ASCIZ command
Date: Mon, 13 Feb 2023 17:11:24 +0100	[thread overview]
Message-ID: <20230213161124.15340-7-binutils@emagii.com> (raw)
In-Reply-To: <20230213161124.15340-1-binutils@emagii.com>

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


      parent reply	other threads:[~2023-02-13 16:11 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` binutils [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230213161124.15340-7-binutils@emagii.com \
    --to=binutils@emagii.com \
    --cc=binutils@sourceware.org \
    --cc=nickc@redhat.com \
    --cc=ulf@emagii.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).