dwarf2out: reset generation count in toplev::finalize [PR98751]

PR debug/98751 reports an issue in which most of libgccjit's tests
fails in DWARF 5 handling with
  `.Ldebug_loc2' is already defined"
asm errors.

The bogus label is being emitted at the 3rd in-process iteration, at:
  31673	      ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
which on the initial iteration emits:

 145   │ .Ldebug_loc0:

on the 2nd iteration:
 145   │ .Ldebug_loc1:

and on the 3rd iteration:
 145   │ .Ldebug_loc2:

which is a duplicate of a label emitted earlier:
 138   │     .section    .debug_loclists,"",@progbits
 139   │     .long   .Ldebug_loc3-.Ldebug_loc2
 140   │ .Ldebug_loc2:
 141   │     .value  0x5
 142   │     .byte   0x8
 143   │     .byte   0
 144   │     .long   0
 145   │ .Ldebug_loc2:

The issue seems to be that init_sections_and_labels creates the label
  ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
			       generation);

where "generation" is a static local to init_sections_and_labels that
increments, and thus eventually hits the duplicate value.

It appears that this value is intended to be either 0 or 1, but in
the libgccjit case the compilation code can be invoked an arbitrary
number of times in-process, and hence can eventually lead to a
label name collision.

This patch adds code to dwarf2out_c_finalize (called by
toplev::finalize in libgccjit) to reset the generation counts,
fixing the issue.

gcc/ChangeLog:
	PR debug/98751
	* dwarf2out.c (output_line_info): Rename static variable
	"generation", moving it out of the function to...
	(output_line_info_generation): New.
	(init_sections_and_labels): Likewise, renaming the variable to...
	(init_sections_and_labels_generation): New.
	(dwarf2out_c_finalize): Reset the new variables.
This commit is contained in:
David Malcolm 2021-01-19 19:58:23 -05:00
parent f35a4f9637
commit b83604c75f

View File

@ -12709,22 +12709,27 @@ output_one_line_info_table (dw_line_info_table *table)
dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
}
static unsigned int output_line_info_generation;
/* Output the source line number correspondence information. This
information goes into the .debug_line section. */
static void
output_line_info (bool prologue_only)
{
static unsigned int generation;
char l1[MAX_ARTIFICIAL_LABEL_BYTES], l2[MAX_ARTIFICIAL_LABEL_BYTES];
char p1[MAX_ARTIFICIAL_LABEL_BYTES], p2[MAX_ARTIFICIAL_LABEL_BYTES];
bool saw_one = false;
int opc;
ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, generation);
ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, generation);
ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, generation);
ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, generation++);
ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL,
output_line_info_generation);
ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL,
output_line_info_generation);
ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL,
output_line_info_generation);
ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL,
output_line_info_generation++);
if (!XCOFF_DEBUGGING_INFO)
{
@ -28589,6 +28594,10 @@ output_macinfo (const char *debug_line_label, bool early_lto_debug)
macinfo_label_base += macinfo_label_base_adj;
}
/* As init_sections_and_labels may get called multiple times, have a
generation count for labels. */
static unsigned init_sections_and_labels_generation;
/* Initialize the various sections and labels for dwarf output and prefix
them with PREFIX if non-NULL. Returns the generation (zero based
number of times function was called). */
@ -28596,10 +28605,6 @@ output_macinfo (const char *debug_line_label, bool early_lto_debug)
static unsigned
init_sections_and_labels (bool early_lto_debug)
{
/* As we may get called multiple times have a generation count for
labels. */
static unsigned generation = 0;
if (early_lto_debug)
{
if (!dwarf_split_debug_info)
@ -28634,7 +28639,7 @@ init_sections_and_labels (bool early_lto_debug)
SECTION_DEBUG | SECTION_EXCLUDE, NULL);
ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
DEBUG_SKELETON_ABBREV_SECTION_LABEL,
generation);
init_sections_and_labels_generation);
/* Somewhat confusing detail: The skeleton_[abbrev|info] sections
stay in the main .o, but the skeleton_line goes into the split
@ -28644,14 +28649,14 @@ init_sections_and_labels (bool early_lto_debug)
SECTION_DEBUG | SECTION_EXCLUDE, NULL);
ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
DEBUG_SKELETON_LINE_SECTION_LABEL,
generation);
init_sections_and_labels_generation);
debug_str_offsets_section
= get_section (DEBUG_LTO_DWO_STR_OFFSETS_SECTION,
SECTION_DEBUG | SECTION_EXCLUDE,
NULL);
ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
DEBUG_SKELETON_INFO_SECTION_LABEL,
generation);
init_sections_and_labels_generation);
debug_str_dwo_section = get_section (DEBUG_LTO_STR_DWO_SECTION,
DEBUG_STR_DWO_SECTION_FLAGS,
NULL);
@ -28667,7 +28672,8 @@ init_sections_and_labels (bool early_lto_debug)
debug_line_section = get_section (DEBUG_LTO_LINE_SECTION,
SECTION_DEBUG | SECTION_EXCLUDE, NULL);
ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
DEBUG_LINE_SECTION_LABEL, generation);
DEBUG_LINE_SECTION_LABEL,
init_sections_and_labels_generation);
debug_str_section = get_section (DEBUG_LTO_STR_SECTION,
DEBUG_STR_SECTION_FLAGS
@ -28711,7 +28717,7 @@ init_sections_and_labels (bool early_lto_debug)
SECTION_DEBUG, NULL);
ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
DEBUG_SKELETON_ABBREV_SECTION_LABEL,
generation);
init_sections_and_labels_generation);
/* Somewhat confusing detail: The skeleton_[abbrev|info] sections
stay in the main .o, but the skeleton_line goes into the
@ -28721,13 +28727,13 @@ init_sections_and_labels (bool early_lto_debug)
SECTION_DEBUG | SECTION_EXCLUDE, NULL);
ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
DEBUG_SKELETON_LINE_SECTION_LABEL,
generation);
init_sections_and_labels_generation);
debug_str_offsets_section
= get_section (DEBUG_DWO_STR_OFFSETS_SECTION,
SECTION_DEBUG | SECTION_EXCLUDE, NULL);
ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
DEBUG_SKELETON_INFO_SECTION_LABEL,
generation);
init_sections_and_labels_generation);
debug_loc_section = get_section (dwarf_version >= 5
? DEBUG_DWO_LOCLISTS_SECTION
: DEBUG_DWO_LOC_SECTION,
@ -28767,31 +28773,37 @@ init_sections_and_labels (bool early_lto_debug)
}
ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
DEBUG_ABBREV_SECTION_LABEL, generation);
DEBUG_ABBREV_SECTION_LABEL,
init_sections_and_labels_generation);
ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
DEBUG_INFO_SECTION_LABEL, generation);
DEBUG_INFO_SECTION_LABEL,
init_sections_and_labels_generation);
info_section_emitted = false;
ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
DEBUG_LINE_SECTION_LABEL, generation);
DEBUG_LINE_SECTION_LABEL,
init_sections_and_labels_generation);
/* There are up to 4 unique ranges labels per generation.
See also output_rnglists. */
ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
DEBUG_RANGES_SECTION_LABEL, generation * 4);
DEBUG_RANGES_SECTION_LABEL,
init_sections_and_labels_generation * 4);
if (dwarf_version >= 5 && dwarf_split_debug_info)
ASM_GENERATE_INTERNAL_LABEL (ranges_base_label,
DEBUG_RANGES_SECTION_LABEL,
1 + generation * 4);
1 + init_sections_and_labels_generation * 4);
ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
DEBUG_ADDR_SECTION_LABEL, generation);
DEBUG_ADDR_SECTION_LABEL,
init_sections_and_labels_generation);
ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
(dwarf_strict && dwarf_version < 5)
? DEBUG_MACINFO_SECTION_LABEL
: DEBUG_MACRO_SECTION_LABEL, generation);
: DEBUG_MACRO_SECTION_LABEL,
init_sections_and_labels_generation);
ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL,
generation);
init_sections_and_labels_generation);
++generation;
return generation - 1;
++init_sections_and_labels_generation;
return init_sections_and_labels_generation - 1;
}
/* Set up for Dwarf output at the start of compilation. */
@ -32379,6 +32391,8 @@ dwarf2out_c_finalize (void)
base_types.release ();
XDELETEVEC (producer_string);
producer_string = NULL;
output_line_info_generation = 0;
init_sections_and_labels_generation = 0;
}
#include "gt-dwarf2out.h"