From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by sourceware.org (Postfix) with ESMTPS id CFAFE3857BAB for ; Thu, 11 Aug 2022 04:53:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CFAFE3857BAB Received: by mail-pj1-x1034.google.com with SMTP id t2-20020a17090a4e4200b001f21572f3a4so4106866pjl.0 for ; Wed, 10 Aug 2022 21:53:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc; bh=adVwM9xfidhYRFiNUhMa4QU79vwqABh3chnHVU+QG5o=; b=gHQVLFRFpmbu4CLEzNSceRwiEB5no/zN8yEL/pXAT22PWkWTgmJORrX3V+zewDcILr 93HvVmsiWP8n69XwtINqh/R6l9N8IrMnmM7J6oqydPU8ILKrfj9tqN0mVMmwS8nCyU6F Dd3jEeYTxJyf553LYE1ZwVD2rYst+6a1mo1TlfLJwkULJTRKkGx99c5RxnVkTDvb+Z1l fAwCqaU/biHKpRhR90+eHr/aueORv2ykiCyl1XwKYSMAFrPLA6XzweZZtKEB3rVqzB73 bRr577ooxhMl3tubfficyXtFS0dnpms1V0ZLKrTx5rArMwpsPeavTyQ7O5TK3ED9X2R/ 2dpQ== X-Gm-Message-State: ACgBeo2UNDw6wZaYKF5Ctnqmif7vLAIi7CsFju840aSxGUlqRktV7cl2 xRQghPGnpqJXwnm+iico8Y3YMehLTVA= X-Google-Smtp-Source: AA6agR5NITX9zTV0LX4+fB0zq3h41XEg9cNZMAJarEiYYfyQsYVIsgQuCmPSBnndu/sv1BF7McXeGQ== X-Received: by 2002:a17:90b:3944:b0:1f5:4cc8:d46 with SMTP id oe4-20020a17090b394400b001f54cc80d46mr6895539pjb.95.1660193607555; Wed, 10 Aug 2022 21:53:27 -0700 (PDT) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id a8-20020a170902ecc800b0016c5306917fsm14078628plh.53.2022.08.10.21.53.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Aug 2022 21:53:27 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 50C371142E95; Thu, 11 Aug 2022 14:23:24 +0930 (ACST) Date: Thu, 11 Aug 2022 14:23:24 +0930 From: Alan Modra To: binutils@sourceware.org Subject: PR29466, APP/NO_APP with .linefile Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3036.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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-List-Received-Date: Thu, 11 Aug 2022 04:53:30 -0000 Commit 53f2b36a54b9 exposed a bug in sb_scrub_and_add_sb that could result in losing input. If scrubbing results in expansion past the holding capacity of do_scrub_chars output buffer, then do_scrub_chars stashes the extra input for the next call. That call never came because sb_scrub_and_add_sb wrongly decided it was done. Fix that by allowing sb_scrub_and_add_sb to see whether there is pending input. Also allow a little extra space so that in most cases we won't need to resize the output buffer. sb_scrub_and_add_sb also limited output to the size of the input, rather than the actual output buffer size. Fixing that resulted in a fail of gas/testsuite/macros/dot with an extra warning: "end of file not at end of a line; newline inserted". OK, so the macro in dot.s really does finish without end-of-line. Apparently the macro expansion code relied on do_scrub_chars returning early. So fix that too by adding a newline if needed in macro_expand_body. PR 29466 * app.c (do_scrub_pending): New function. * as.h: Declare it. * input-scrub.c (input_scrub_include_sb): Add extra space for two .linefile directives. * sb.c (sb_scrub_and_add_sb): Take into account pending input. Allow output to max. * macro.c (macro_expand_body): Add terminating newline. * testsuite/config/default.exp (SIZE, SIZEFLAGS): Define. * testsuite/gas/macros/app5.d, * testsuite/gas/macros/app5.s: New test. * testsuite/gas/macros/macros.exp: Run it. diff --git a/gas/app.c b/gas/app.c index a3ad146625c..096829fd541 100644 --- a/gas/app.c +++ b/gas/app.c @@ -1537,3 +1537,16 @@ do_scrub_chars (size_t (*get) (char *, size_t), char *tostart, size_t tolen) last_char = to[-1]; return to - tostart; } + +/* Return amount of pending input. */ + +size_t +do_scrub_pending (void) +{ + size_t len = 0; + if (saved_input) + len += saved_input_len; + if (state == -1) + len += strlen (out_string); + return len; +} diff --git a/gas/as.h b/gas/as.h index ff665c75812..730e134dce6 100644 --- a/gas/as.h +++ b/gas/as.h @@ -471,6 +471,7 @@ void input_scrub_insert_file (char *); char * input_scrub_new_file (const char *); char * input_scrub_next_buffer (char **bufp); size_t do_scrub_chars (size_t (*get) (char *, size_t), char *, size_t); +size_t do_scrub_pending (void); bool scan_for_multibyte_characters (const unsigned char *, const unsigned char *, bool); int gen_to_words (LITTLENUM_TYPE *, int, long); int had_err (void); diff --git a/gas/input-scrub.c b/gas/input-scrub.c index ec0b007c77a..44e4bdca521 100644 --- a/gas/input-scrub.c +++ b/gas/input-scrub.c @@ -278,9 +278,11 @@ input_scrub_include_sb (sb *from, char *position, enum expansion expansion) next_saved_file = input_scrub_push (position); - /* Allocate sufficient space: from->len + optional newline. */ + /* Allocate sufficient space: from->len plus optional newline + plus two ".linefile " directives, plus a little more for other + expansion. */ newline = from->len >= 1 && from->ptr[0] != '\n'; - sb_build (&from_sb, from->len + newline); + sb_build (&from_sb, from->len + newline + 2 * sizeof (".linefile") + 30); if (expansion == expanding_repeat && from_sb_expansion >= expanding_macro) expansion = expanding_nested; from_sb_expansion = expansion; diff --git a/gas/macro.c b/gas/macro.c index d799ba586e5..c2a47684b15 100644 --- a/gas/macro.c +++ b/gas/macro.c @@ -1046,6 +1046,8 @@ macro_expand_body (sb *in, sb *out, formal_entry *formals, loclist = f; } + if (!err && (out->len == 0 || out->ptr[out->len - 1] != '\n')) + sb_add_char (out, '\n'); return err; } diff --git a/gas/sb.c b/gas/sb.c index c44016a0338..e83a5f524f8 100644 --- a/gas/sb.c +++ b/gas/sb.c @@ -119,11 +119,12 @@ sb_scrub_and_add_sb (sb *ptr, sb *s) So we loop until the input S is consumed. */ while (1) { - size_t copy = s->len - (scrub_position - s->ptr); + size_t copy = s->len - (scrub_position - s->ptr) + do_scrub_pending (); if (copy == 0) break; sb_check (ptr, copy); - ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len, copy); + ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len, + ptr->max - ptr->len); } sb_to_scrub = 0; diff --git a/gas/testsuite/config/default.exp b/gas/testsuite/config/default.exp index 5a3dda915f7..21859d961d9 100644 --- a/gas/testsuite/config/default.exp +++ b/gas/testsuite/config/default.exp @@ -52,6 +52,14 @@ if ![info exists NMFLAGS] then { set NMFLAGS {} } +if ![info exists SIZE] then { + set SIZE [findfile $base_dir/size] +} + +if ![info exists SIZEFLAGS] then { + set SIZEFLAGS "" +} + if ![info exists OBJCOPY] then { set OBJCOPY [findfile $base_dir/../../binutils/objcopy] } diff --git a/gas/testsuite/gas/macros/app5.d b/gas/testsuite/gas/macros/app5.d new file mode 100644 index 00000000000..0a1483ccc3a --- /dev/null +++ b/gas/testsuite/gas/macros/app5.d @@ -0,0 +1,6 @@ +#name: APP with linefile +#xfail: tic30-*-* +#size: -G +# pr29466 just check that the test assembles + +#pass diff --git a/gas/testsuite/gas/macros/app5.s b/gas/testsuite/gas/macros/app5.s new file mode 100644 index 00000000000..a960ae223ba --- /dev/null +++ b/gas/testsuite/gas/macros/app5.s @@ -0,0 +1,5 @@ +#NO_APP +#APP +# 5 "foo.c" 1 +# 0 "" 2 +#NO_APP diff --git a/gas/testsuite/gas/macros/macros.exp b/gas/testsuite/gas/macros/macros.exp index 915c45d74f6..7a45af4a308 100644 --- a/gas/testsuite/gas/macros/macros.exp +++ b/gas/testsuite/gas/macros/macros.exp @@ -70,6 +70,7 @@ run_dump_test app2 run_dump_test app3 remote_download host "$srcdir/$subdir/app4b.s" run_dump_test app4 +run_dump_test app5 run_list_test badarg "" -- Alan Modra Australia Development Lab, IBM