From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1062) id 066F8385703F; Mon, 13 Feb 2023 10:36:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 066F8385703F Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Alan Modra To: bfd-cvs@sourceware.org Subject: [binutils-gdb] stabs.c static state X-Act-Checkin: binutils-gdb X-Git-Author: Alan Modra X-Git-Refname: refs/heads/master X-Git-Oldrev: 103357fb75c93ba8195166f65719b03aecce497e X-Git-Newrev: 24b7e8b18ae6410e0b2445475cd21503a2c38922 Message-Id: <20230213103631.066F8385703F@sourceware.org> Date: Mon, 13 Feb 2023 10:36:31 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 13 Feb 2023 10:36:31 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D24b7e8b18ae6= 410e0b2445475cd21503a2c38922 commit 24b7e8b18ae6410e0b2445475cd21503a2c38922 Author: Alan Modra Date: Mon Feb 13 20:28:41 2023 +1030 stabs.c static state =20 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. =20 * 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: --- gas/read.c | 2 ++ gas/read.h | 2 ++ gas/stabs.c | 113 +++++++++++++++++++++++++++++++++++---------------------= ---- 3 files changed, 71 insertions(+), 46 deletions(-) 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) =20 if (flag_mri) lex_type['?'] =3D 3; + stabs_begin (); } =20 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 =20 -/* 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 =3D false; - -/* Label at start of current function if in_dot_func_p !=3D 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; =20 +/* 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; =20 - static segT cached_sec; - dot =3D frag_now_fix (); =20 #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 =3D file; const char *file_endp =3D file + strlen (file); char *bufp; =20 - if (last_file !=3D NULL - && filename_cmp (last_file, file) =3D=3D 0) + if (last_asm_file !=3D NULL + && filename_cmp (last_asm_file, file) =3D=3D 0) return; =20 /* 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; =20 /* 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) =20 colon (sym); =20 - free (last_file); - last_file =3D xstrdup (file); + free (last_asm_file); + last_asm_file =3D xstrdup (file); =20 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 =3D -1; - static char *prev_file =3D NULL; =20 /* 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 =3D as_where (&lineno); =20 /* Don't emit sequences of stabs for the same line. */ - if (prev_file =3D=3D NULL) + if (prev_line_file !=3D NULL + && filename_cmp (file, prev_line_file) =3D=3D 0) { - /* First time through. */ - prev_file =3D xstrdup (file); - prev_lineno =3D lineno; - } - else if (lineno =3D=3D prev_lineno - && filename_cmp (file, prev_file) =3D=3D 0) - { - /* Same file/line as last time. */ - return; + if (lineno =3D=3D prev_lineno) + /* Same file/line as last time. */ + return; } else { /* Remember file/line for next time. */ - prev_lineno =3D lineno; - if (filename_cmp (file, prev_file) !=3D 0) - { - free (prev_file); - prev_file =3D xstrdup (file); - } + free (prev_line_file); + prev_line_file =3D xstrdup (file); } =20 + prev_lineno =3D lineno; + /* Let the world know that we are in the middle of generating a piece of stabs line debugging information. */ outputting_stabs_line_debug =3D 1; =20 generate_asm_file (N_SOL, file); =20 - sprintf (sym, "%sL%d", FAKE_LABEL_NAME, label_count); - ++label_count; + sprintf (sym, "%sL%d", FAKE_LABEL_NAME, line_label_count); + ++line_label_count; =20 - if (in_dot_func_p) + if (current_function_label) { buf =3D 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 =3D false; char *buf; unsigned int lineno; =20 @@ -674,8 +674,8 @@ stabs_generate_asm_func (const char *funcname, const ch= ar *startlabname) restore_ilp (); free (buf); =20 + free ((char *) current_function_label); current_function_label =3D xstrdup (startlabname); - in_dot_func_p =3D true; } =20 /* 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]; =20 - 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); =20 if (asprintf (&buf, "\"\",%d,0,0,%s-%s", N_FUN, sym, startlabname) =3D= =3D -1) @@ -700,6 +699,28 @@ stabs_generate_asm_endfunc (const char *funcname ATTRI= BUTE_UNUSED, restore_ilp (); free (buf); =20 - in_dot_func_p =3D false; + free ((char *) current_function_label); current_function_label =3D NULL; } + +void +stabs_begin (void) +{ + current_function_label =3D NULL; + cached_sec =3D NULL; + last_asm_file =3D NULL; + file_label_count =3D 0; + line_label_count =3D 0; + prev_lineno =3D -1u; + prev_line_file =3D NULL; + void_emitted_p =3D false; + endfunc_label_count =3D 0; +} + +void +stabs_end (void) +{ + free ((char *) current_function_label); + free (last_asm_file); + free (prev_line_file); +}