From patchwork Mon Feb 13 10:36:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 56164 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2272268wrn; Mon, 13 Feb 2023 02:36:46 -0800 (PST) X-Google-Smtp-Source: AK7set+i2ea92lVLnB0TLaAHE2nZc/RQnpyM3LWXaodU+DjsUkoyaeng06SsCjEZMVMtp6vmfn6t X-Received: by 2002:a50:cd02:0:b0:4ac:d2cd:81c7 with SMTP id z2-20020a50cd02000000b004acd2cd81c7mr219149edi.5.1676284605949; Mon, 13 Feb 2023 02:36:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676284605; cv=none; d=google.com; s=arc-20160816; b=J+znIx0RDopq5C0nsGAAHhm34ljKLoSolK0AD0nN1L4iR4swOFMTOm5kK7PWBgnRkt fvutLH8zZrxNKed7m81qy6ypYOrOXxqKaWEuqm4ikT/dELouKQglfgIgZfk2h/xDHuTo SQIuQRmXl+TafQMsRLYzik7NHjUf2DAEi5vB7Pgh8c5RSzIpegyiODDx1kLnR0DTR4k+ YMLMGmEkARGbBqLeXaLgwfb8IBXa1NL3Cg0DRpQDgJ9kjbsQA2QDCMwEJBrMr+ucl/TD F0jloblhcPBPPf5zLmq7B9Qh8K+EmUhMjA8ds1cVLqXwjYg4nXXnYwqEhIt0psboGBbE f+Fg== 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 :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=AcFYbY+utB39vsmZjuVWRYnHYyfCDCcPHFTJTLaodvc=; b=de96JJqQMH3BCgc2uJcnXhCSU6MO79UHQQErbDArFjyOpmMD89cwTNME4ErMkRvFxg YiRPo4cZ4IKxZtvxmCo0zeLMdD5AMXif2IwrsmdscqZbRs9r5JqruzRNDZck5ZNqKjnD X7AFkiea6VD50m6V8Pr1H96pDJ82emZUMY97Vza2Ld7UD1bBwJN2D/YbF8QPJN0kUb42 OsgAuMCR1k7bB0ym6phEDt1uL/99WqKoAi8BReXEnw2PCDQRsneYYYngOxSk1Fy3yvXm yTwQcYbQTtZ3E22YKk1LpahK4lLrvWPQHsk78vHg2SpeVXTzeXTuMpB7dWj9W668x+KS bk3g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=h6YmeZvC; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id c19-20020a056402101300b004ab250bcee2si11657977edu.644.2023.02.13.02.36.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Feb 2023 02:36:45 -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=h6YmeZvC; 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 CC2E638493CD for ; Mon, 13 Feb 2023 10:36:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CC2E638493CD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1676284597; bh=AcFYbY+utB39vsmZjuVWRYnHYyfCDCcPHFTJTLaodvc=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=h6YmeZvCN810TFx3GrfkQV4l0NcvCB6LJ4bevN3NVT8bnAoNA0vh5IN+eY8JbhX3D RmIhc1exGItd/IUVFvcPv9lYF+VH0SihIQAi7iuLf7jTUK0f7+e7jsVgt9LFugEj95 JqwHXutmaQRXIFw4pf3jTBAtCpuRvapkBWzatZa0= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by sourceware.org (Postfix) with ESMTPS id 63DB43858022 for ; Mon, 13 Feb 2023 10:36:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 63DB43858022 Received: by mail-pl1-x629.google.com with SMTP id ja21so5458429plb.13 for ; Mon, 13 Feb 2023 02:36:06 -0800 (PST) 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:subject:date:message-id:reply-to; bh=AcFYbY+utB39vsmZjuVWRYnHYyfCDCcPHFTJTLaodvc=; b=tX8FErHc38YRvZD53vqWJFKZ7p3wjX+MmJYEzSVZJ12qBRXWef4xVpO0a8FqK1IB1A 7v2as94ID0eDT1sTMAALo3xGv7gA80tx0tYkFCGDiGAVc+mMa1QLfqIJTHOp12xKfyS9 P5BGgJ0StxOsaStuUZclGPvPBIvMoIJzdniUkABHDQYimF7EqJ8CBWXpqZ4XWC25vnKN rqI2AyFManja2ampwj6bLLaKhDgzP02VQhupFNspdCIzfJbNwwLyqSa4y/hFrHaQ8vtF sxFTYgZ39HvW16QTKpNerBJED3LG0WJC7EuIHlf+dtigmUbrImONT7BcRjH30gjhtWpd rk9w== X-Gm-Message-State: AO0yUKURxPYV21ILirMqdxtCPQz6KDUEyxzOLBXDgJ0DUcrwW7SWwPra LFs7zg2d4f3CQ4znZ0LNLl4rl5PzziA= X-Received: by 2002:a05:6a20:1449:b0:be:b137:9d2d with SMTP id a9-20020a056a20144900b000beb1379d2dmr29703537pzi.58.1676284565216; Mon, 13 Feb 2023 02:36:05 -0800 (PST) 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 s3-20020a637703000000b004e8f7f23c4bsm2308544pgc.76.2023.02.13.02.36.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Feb 2023 02:36:04 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 817921142CA6; Mon, 13 Feb 2023 21:06:02 +1030 (ACDT) Date: Mon, 13 Feb 2023 21:06:02 +1030 To: binutils@sourceware.org Subject: stabs.c static state Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3035.6 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 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: Alan Modra via Binutils From: Alan Modra Reply-To: Alan Modra 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?1757711806706547148?= X-GMAIL-MSGID: =?utf-8?q?1757711806706547148?= Move all the function local static state variables to file scope, in order to tidy memory on exit and to reinit everything for that annoying oss-fuzz. Also fix a couple memory leaks. * read.h (read_begin, read_end): Declare. * read.c (read_begin): Call stabs_begin. (read_end): Call stabs_end. * stabs.c (stabs_begin, stabs_end): New functions. (in_dot_func_p): Delete, use current_function_label instead. (cached_sec): Move from s_stab_generic. (last_asm_file, file_label_count): Move from generate_asm_file. (line_label_count, prev_lineno, prev_line_file): Move from stabs_generate_asm_lineno. (void_emitted_p): Move from stabs_generate_asm_func. (endfunc_label_count): Move from stabs_generate_asm_endfunc. (stabs_generate_asm_lineno): Simplify setting of prev_line_file. (stabs_generate_asm_func): Don't leak current_function_label. (stabs_generate_asm_endfunc): Likewise. diff --git a/gas/read.c b/gas/read.c index c5a477f9e08..9f6a7007ebf 100644 --- a/gas/read.c +++ b/gas/read.c @@ -274,11 +274,13 @@ read_begin (void) if (flag_mri) lex_type['?'] = 3; + stabs_begin (); } void read_end (void) { + stabs_end (); poend (); _obstack_free (&cond_obstack, NULL); } diff --git a/gas/read.h b/gas/read.h index baac5910c51..406b026d76f 100644 --- a/gas/read.h +++ b/gas/read.h @@ -149,6 +149,8 @@ extern void stabs_generate_asm_file (void); extern void stabs_generate_asm_lineno (void); extern void stabs_generate_asm_func (const char *, const char *); extern void stabs_generate_asm_endfunc (const char *, const char *); +extern void stabs_begin (void); +extern void stabs_end (void); extern void do_repeat (size_t, const char *, const char *, const char *); extern void end_repeat (int); extern void do_parse_cons_expression (expressionS *, int); diff --git a/gas/stabs.c b/gas/stabs.c index e4817484834..f650a48ae88 100644 --- a/gas/stabs.c +++ b/gas/stabs.c @@ -45,15 +45,31 @@ static void generate_asm_file (int, const char *); #define STAB_STRING_SECTION_NAME ".stabstr" #endif -/* True if we're in the middle of a .func function, in which case - stabs_generate_asm_lineno emits function relative line number stabs. - Otherwise it emits line number stabs with absolute addresses. Note that - both cases only apply to assembler code assembled with -gstabs. */ -static bool in_dot_func_p = false; - -/* Label at start of current function if in_dot_func_p != FALSE. */ +/* Label at start of current function if we're in the middle of a + .func function, in which case stabs_generate_asm_lineno emits + function relative line number stabs. Otherwise it emits line + number stabs with absolute addresses. Note that both cases only + apply to assembler code assembled with -gstabs. */ static const char *current_function_label; +/* Current stab section when SEPARATE_STAB_SECTIONS. */ +static segT cached_sec; + +/* State used by generate_asm_file. */ +static char *last_asm_file; +static int file_label_count; + +/* State used by stabs_generate_asm_lineno. */ +static int line_label_count; +static unsigned int prev_lineno; +static char *prev_line_file; + +/* State used by stabs_generate_asm_func. */ +static bool void_emitted_p; + +/* State used by stabs_generate_asm_endfunc. */ +static int endfunc_label_count; + /* * Handle .stabX directives, which used to be open-coded. * So much creeping featurism overloaded the semantics that we decided @@ -314,8 +330,6 @@ s_stab_generic (int what, unsigned int stroff; char *p; - static segT cached_sec; - dot = frag_now_fix (); #ifdef md_flush_pending_output @@ -512,24 +526,22 @@ stabs_generate_asm_file (void) static void generate_asm_file (int type, const char *file) { - static char *last_file; - static int label_count; char sym[30]; char *buf; const char *tmp = file; const char *file_endp = file + strlen (file); char *bufp; - if (last_file != NULL - && filename_cmp (last_file, file) == 0) + if (last_asm_file != NULL + && filename_cmp (last_asm_file, file) == 0) return; /* Rather than try to do this in some efficient fashion, we just generate a string and then parse it again. That lets us use the existing stabs hook, which expect to see a string, rather than inventing new ones. */ - sprintf (sym, "%sF%d", FAKE_LABEL_NAME, label_count); - ++label_count; + sprintf (sym, "%sF%d", FAKE_LABEL_NAME, file_label_count); + ++file_label_count; /* Allocate enough space for the file name (possibly extended with doubled up backslashes), the symbol name, and the other characters @@ -563,8 +575,8 @@ generate_asm_file (int type, const char *file) colon (sym); - free (last_file); - last_file = xstrdup (file); + free (last_asm_file); + last_asm_file = xstrdup (file); free (buf); } @@ -575,14 +587,10 @@ generate_asm_file (int type, const char *file) void stabs_generate_asm_lineno (void) { - static int label_count; const char *file; unsigned int lineno; char *buf; char sym[30]; - /* Remember the last file/line and avoid duplicates. */ - static unsigned int prev_lineno = -1; - static char *prev_file = NULL; /* Rather than try to do this in some efficient fashion, we just generate a string and then parse it again. That lets us use the @@ -592,39 +600,32 @@ stabs_generate_asm_lineno (void) file = as_where (&lineno); /* Don't emit sequences of stabs for the same line. */ - if (prev_file == NULL) + if (prev_line_file != NULL + && filename_cmp (file, prev_line_file) == 0) { - /* First time through. */ - prev_file = xstrdup (file); - prev_lineno = lineno; - } - else if (lineno == prev_lineno - && filename_cmp (file, prev_file) == 0) - { - /* Same file/line as last time. */ - return; + if (lineno == prev_lineno) + /* Same file/line as last time. */ + return; } else { /* Remember file/line for next time. */ - prev_lineno = lineno; - if (filename_cmp (file, prev_file) != 0) - { - free (prev_file); - prev_file = xstrdup (file); - } + free (prev_line_file); + prev_line_file = xstrdup (file); } + prev_lineno = lineno; + /* Let the world know that we are in the middle of generating a piece of stabs line debugging information. */ outputting_stabs_line_debug = 1; generate_asm_file (N_SOL, file); - sprintf (sym, "%sL%d", FAKE_LABEL_NAME, label_count); - ++label_count; + sprintf (sym, "%sL%d", FAKE_LABEL_NAME, line_label_count); + ++line_label_count; - if (in_dot_func_p) + if (current_function_label) { buf = XNEWVEC (char, 100 + strlen (current_function_label)); sprintf (buf, "%d,0,%d,%s-%s\n", N_SLINE, lineno, @@ -652,7 +653,6 @@ stabs_generate_asm_lineno (void) void stabs_generate_asm_func (const char *funcname, const char *startlabname) { - static bool void_emitted_p = false; char *buf; unsigned int lineno; @@ -674,8 +674,8 @@ stabs_generate_asm_func (const char *funcname, const char *startlabname) restore_ilp (); free (buf); + free ((char *) current_function_label); current_function_label = xstrdup (startlabname); - in_dot_func_p = true; } /* Emit a stab to record the end of a function. */ @@ -684,12 +684,11 @@ void stabs_generate_asm_endfunc (const char *funcname ATTRIBUTE_UNUSED, const char *startlabname) { - static int label_count; char *buf; char sym[30]; - sprintf (sym, "%sendfunc%d", FAKE_LABEL_NAME, label_count); - ++label_count; + sprintf (sym, "%sendfunc%d", FAKE_LABEL_NAME, endfunc_label_count); + ++endfunc_label_count; colon (sym); if (asprintf (&buf, "\"\",%d,0,0,%s-%s", N_FUN, sym, startlabname) == -1) @@ -700,6 +699,28 @@ stabs_generate_asm_endfunc (const char *funcname ATTRIBUTE_UNUSED, restore_ilp (); free (buf); - in_dot_func_p = false; + free ((char *) current_function_label); current_function_label = NULL; } + +void +stabs_begin (void) +{ + current_function_label = NULL; + cached_sec = NULL; + last_asm_file = NULL; + file_label_count = 0; + line_label_count = 0; + prev_lineno = -1u; + prev_line_file = NULL; + void_emitted_p = false; + endfunc_label_count = 0; +} + +void +stabs_end (void) +{ + free ((char *) current_function_label); + free (last_asm_file); + free (prev_line_file); +}