From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from emagii.se (www.emagii.com [185.133.207.17]) by sourceware.org (Postfix) with ESMTPS id 1B9313858298 for ; Mon, 13 Feb 2023 16:11:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1B9313858298 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=emagii.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=emagii.com Received: from valinor.ownit.se (84-55-68-216.customers.ownit.se [84.55.68.216]) by emagii.se (Postfix) with ESMTPSA id C9681120264; Mon, 13 Feb 2023 17:11:30 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=emagii.com; s=default; t=1676304691; bh=qZ2wWJ6S+HgVS14L20SJ6btOYz606ZkGGTw8jjqSzcE=; h=From:To:Subject; b=O8gKJ4g3v0w7jsNWyRo3dmrzYnkVylwcn2cL+7ed7NWAvdCoVBbca9ckPRJd3zVTq oimai8U5KN19Pa9RRDLjeQbPtOBdDxcLe9elszTumHyV7AHso8gIo7wjBXs598sJsO AljheSbe/kxaCSERCIyS00QZF0Q6ym7dar+5vyBA= Authentication-Results: emagii.beebytevps.io; spf=pass (sender IP is 84.55.68.216) smtp.mailfrom=binutils@emagii.com smtp.helo=valinor.ownit.se Received-SPF: pass (emagii.beebytevps.io: connection is authenticated) From: binutils@emagii.com To: binutils@sourceware.org Cc: nickc@redhat.com, Ulf Samuelsson Subject: [PATCH v3 6/6] Parse ASCIZ command Date: Mon, 13 Feb 2023 17:11:24 +0100 Message-Id: <20230213161124.15340-7-binutils@emagii.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230213161124.15340-1-binutils@emagii.com> References: <20230213161124.15340-1-binutils@emagii.com> X-PPP-Message-ID: <167630469114.3743017.17313898890616298041@localhost.localdomain> X-PPP-Vhost: emagii.com X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_FAIL,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Ulf Samuelsson Signed-off-by: Ulf Samuelsson --- 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