From patchwork Mon Feb 13 12:22:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulf Samuelsson X-Patchwork-Id: 56263 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2312692wrn; Mon, 13 Feb 2023 04:24:54 -0800 (PST) X-Google-Smtp-Source: AK7set+Zx/z0A+ofd1joAlYspuUPWuTfw9J3/1qz3HzSL4+nnTt9Z8sfK3TGFHCQ+HpKENXN4wrO X-Received: by 2002:a17:906:608a:b0:888:4e73:b71d with SMTP id t10-20020a170906608a00b008884e73b71dmr23035148ejj.47.1676291094430; Mon, 13 Feb 2023 04:24:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676291094; cv=none; d=google.com; s=arc-20160816; b=jY2htYpoC7tWtvEo71dOJef13wcbJ0MGZw1+wfhJdrf5QugNwfBMOD6bHvqpWvzBk3 BxkygTMQvHLnYSGQa6I4+kgDhsWxel3e8ZrNB4eaXcx7AOjxiFTjvkkGT8AEYUAz1267 WZKUL1dD3TnK1+apJe5beNSoU8K1FqRASUo0yIdPLqCnA7GK7U0Ubht+mYZKHYQIU/Cq HaOZd28PI33dB2Mln45z+e/bTJm3sP3eGMnP6aEBLhEG7JG4dW4lbZ9ZSRnngLzFOVeM jpZeZXP1P3yRHdtlgG+u84zfMaNxm3GBVGUWUjCgTMfUW94BS6u2bQ2HZfyP+XuD4tKc mf/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:references :in-reply-to:message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=is4z5kJzf8R2GdUIdTRnIZIa8s2kzCFJkaWfmzCmUbs=; b=sHB00WdXZEGWszOSYGXIGHb4y08arcNCHmH30r8d6nfyS6ia9IXgLUmEulkoEj64MW TLdon0oIGG3dTZ/ZD0nQTOVXYzSxB4tUWZhKpZtpC4qWMro+3cFx/EcSg1vS8oEvzM3b 5D+RnIvuhb9FI13r6v57f25e0g8/vv9b7s6HPWdjdWdomJVUoxZUzcq7Y9L7NlTA1Sms jfJpatAx8W+4sLVJmYq4vICjN3c+cJZFD3bONgNPrtX2BJ+cg+Wo4i8Njtvua3HDihTk ai/yAcOo1DX4Jdo2ET+ol+rPKdlI9gRWkmlJRbCPWF+9R02QJ3joZPSfTc+jVV3tTTaV /X7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=toRShi3y; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id zu21-20020a17090708d500b00882423d0b7fsi12431011ejb.779.2023.02.13.04.24.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Feb 2023 04:24:54 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=toRShi3y; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 78B28382E6BE for ; Mon, 13 Feb 2023 12:24:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 78B28382E6BE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1676291047; bh=is4z5kJzf8R2GdUIdTRnIZIa8s2kzCFJkaWfmzCmUbs=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=toRShi3yY7284ou7UPdS+QHUJSkgSdcsSvh82i1UW3fxVtRTEY1a9MzDIAChHSewz F5Q7a7qv1wf/6NMsEzQ/8kDoTKaol5X2J4xX80NKV+Xy7D8B598MkbeTTMfazQ6Njr ymanQ5UI6dbvFYg3Myi0fbV0DoMNDYnAXdvMjNjs= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from emagii.se (www.emagii.com [185.133.207.17]) by sourceware.org (Postfix) with ESMTPS id C1859384F031 for ; Mon, 13 Feb 2023 12:22:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C1859384F031 Received: from valinor.ownit.se (84-55-68-216.customers.ownit.se [84.55.68.216]) by emagii.se (Postfix) with ESMTPSA id 5D973120245; Mon, 13 Feb 2023 13:22:57 +0100 (CET) Received-SPF: pass (emagii.beebytevps.io: connection is authenticated) To: binutils@sourceware.org Cc: nickc@redhat.com, Ulf Samuelsson Subject: [PATCH v2 3/5] Parse ASCIZ command Date: Mon, 13 Feb 2023 13:22:39 +0100 Message-Id: <20230213122241.6144-4-binutils@emagii.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230213122241.6144-1-binutils@emagii.com> References: <20230213122241.6144-1-binutils@emagii.com> X-PPP-Message-ID: <167629097767.3735107.2880836267046477415@localhost.localdomain> X-PPP-Vhost: emagii.com X-Spam-Status: No, score=-11.6 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 X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Ulf Samuelsson via Binutils From: Ulf Samuelsson Reply-To: binutils@emagii.com Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757718610543551990?= X-GMAIL-MSGID: =?utf-8?q?1757718610543551990?= From: Ulf Samuelsson Signed-off-by: Ulf Samuelsson --- 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 *);