From patchwork Fri Feb 10 17:44:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ulf Samuelsson X-Patchwork-Id: 55504 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1089151wrn; Fri, 10 Feb 2023 09:47:49 -0800 (PST) X-Google-Smtp-Source: AK7set9XZp+AY8V0zO7mv8IBblGtKAE24F90V5cLEyp5pKOQW3P8nfdSLppE0dbay89mu1YoXNH5 X-Received: by 2002:a17:907:6d05:b0:87b:6bbb:11ac with SMTP id sa5-20020a1709076d0500b0087b6bbb11acmr21847897ejc.60.1676051269175; Fri, 10 Feb 2023 09:47:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676051269; cv=none; d=google.com; s=arc-20160816; b=qzEjK1TO6d1+oa/yKydLZjzKlWS7uNC2uOxutsQVfY2Yzs5rd7Mm1CI5Fvt5Kuc8wy lAS6C/zG0aeWEOlcBsXPTJW8eY7sMqIeOiU6INuqSTCE+obbPHdmbFR/QM+lfyhSI3QX ozfFGYp9R6lNafuS6+d84LNHE7ArZZlbaG+TGm5YubTPque+2ACOfmngZZlA/FXJ+3I5 N2XIi1LWKusWxaSrpqHse9g5ox7zNf6vPlbP8RvqQX/AFEpKBKhkYXmKjpYql5S7vqj6 Bb0MWaU0vfOcXaa4Gm22qxssGQqvmn+K4ulpWIh2lS1y5EWvDHQqAuU8+PvmGcX1XaRm 8Y6g== 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=DnyckHx7fDC4biH4WibasCp5gHNDs2p1jCuS/MLfrX6I6ageN2pgPp2Bt3HkI3oC3y Fm0iZrq3u0N1goueCG08S7tmPXufPvLbxQcDYXLC+l1dOcuz2iFl4f68cWyiurSVtebP YhlNsj4YD/kx1838b33M+jsK3Lh3mmEvaP2X4ce712RmgMhxZ9uYJxMSjYueEOXwHl19 khY2HOctEsHn2mnJFcPKChrEtkriy4jNVmQzd8QyozDVFDK13Xyn6Hs8Uj1g9Dg/f2oX TZIKeaePpnopIZ3uvFTvRWlOpK89J+nzBgzjcQKOrMY6J2tuYm7ZNcJxgeoTzMAocOX1 IFpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b="s5Pgt/9C"; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id 1-20020a17090600c100b0088dd72fa97dsi7697136eji.975.2023.02.10.09.47.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Feb 2023 09:47:49 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b="s5Pgt/9C"; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 785853887F64 for ; Fri, 10 Feb 2023 17:45:50 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 785853887F64 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1676051150; 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=s5Pgt/9Cm/UP2FsLMlLulsSus2LdjvoPO2/AQs9ub6iVocE6HoVT6M6Y8xvl7Fhg2 9MjbcUR9rZKylGlaUN306II/Yay4Bi7JkpVjsnTbdjzGolR7XbUjxPLr+HFQJuUsbm wuf99ydQI4T8T7aKt45fGhZYt55hjXwVclguHmn8= 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 0CA0F38432D6 for ; Fri, 10 Feb 2023 17:44:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0CA0F38432D6 Received: from valinor.ownit.se (84-55-68-216.customers.ownit.se [84.55.68.216]) by emagii.se (Postfix) with ESMTPSA id B14E41207ED; Fri, 10 Feb 2023 18:44:15 +0100 (CET) Received-SPF: pass (emagii.beebytevps.io: connection is authenticated) To: binutils@sourceware.org Cc: Ulf Samuelsson Subject: [PATCH 3/4] Parse ASCIZ command Date: Fri, 10 Feb 2023 18:44:03 +0100 Message-Id: <20230210174404.3763-4-binutils@emagii.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230210174404.3763-1-binutils@emagii.com> References: <20230210174404.3763-1-binutils@emagii.com> X-PPP-Message-ID: <167605105598.3627008.8247734203032683022@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?1757467136005600386?= X-GMAIL-MSGID: =?utf-8?q?1757467136005600386?= 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 *);