public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v4 6/6] Parse ASCIZ command
@ 2023-02-14  5:06 binutils
  0 siblings, 0 replies; only message in thread
From: binutils @ 2023-02-14  5:06 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..04eaaf1fa66 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 - '0';
+        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] only message in thread

only message in thread, other threads:[~2023-02-14  5:07 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-14  5:06 [PATCH v4 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).