aboutsummaryrefslogtreecommitdiff
path: root/gcc/lto-wrapper.c
diff options
context:
space:
mode:
authorRichard Biener <rguenther@suse.de>2017-08-21 10:29:00 +0000
committerRichard Biener <rguenth@gcc.gnu.org>2017-08-21 10:29:00 +0000
commit1ea85365b430b5ade1d0b824e5406c006e6ffdb7 (patch)
tree3f66db94dd051305b43f1de3bf2077e236deb8a7 /gcc/lto-wrapper.c
parent9f33a5d9acbed950bf446849e9d6968cf22cb9a2 (diff)
downloadgcc-1ea85365b430b5ade1d0b824e5406c006e6ffdb7.zip
gcc-1ea85365b430b5ade1d0b824e5406c006e6ffdb7.tar.gz
gcc-1ea85365b430b5ade1d0b824e5406c006e6ffdb7.tar.bz2
re PR go/78628 (GO fails to build a translation unit decl)
2017-08-21 Richard Biener <rguenther@suse.de> include/ * simple-object.h (simple_object_copy_lto_debug_sections): New function. libiberty/ * simple-object-common.h (struct simple_object_functions): Add copy_lto_debug_sections hook. * simple-object.c: Include fcntl.h. (handle_lto_debug_sections): New helper function. (simple_object_copy_lto_debug_sections): New function copying early LTO debug sections to regular debug sections in a new file. (simple_object_start_write): Handle NULL segment_name. * simple-object-coff.c (simple_object_coff_functions): Adjust for not implemented copy_lto_debug_sections hook. * simple-object-mach-o.c (simple_object_mach_o_functions): Likewise. * simple-object-xcoff.c (simple_object_xcoff_functions): Likewise. * simple-object-elf.c (SHT_NULL, SHT_SYMTAB, SHT_RELA, SHT_REL, SHT_GROUP): Add various sectopn header types. (SHF_EXCLUDE): Add flag. (Elf32_External_Sym, Elf64_External_Sym): Add symbol struct. (ELF_ST_BIND, ELF_ST_TYPE, ELF_ST_INFO): Add accessors. (STT_OBJECT, STT_FUNC, STT_TLS, STT_GNU_IFUNC): Add Symbol types. (STV_DEFAULT): Add symbol visibility. (SHN_COMMON): Add special section index name. (struct simple_object_elf_write): New. (simple_object_elf_start_write): Adjust for new private data. (simple_object_elf_write_shdr): Pass in values for all fields we write. (simple_object_elf_write_to_file): Adjust. Copy from recorded section headers if requested. (simple_object_elf_release_write): Release private data. (simple_object_elf_copy_lto_debug_sections): Copy and rename sections as denoted by PFN and all their dependences, symbols and relocations to the empty destination file. (simple_object_elf_functions): Adjust for copy_lto_debug_sections hook. gcc/ * debug.h (struct gcc_debug_hooks): Add die_ref_for_decl and register_external_die hooks. (debug_false_tree_charstarstar_uhwistar): Declare. (debug_nothing_tree_charstar_uhwi): Likewise. * debug.c (do_nothing_debug_hooks): Adjust. (debug_false_tree_charstarstar_uhwistar): New do nothing. (debug_nothing_tree_charstar_uhwi): Likewise. * dbxout.c (dbx_debug_hooks): Adjust. (xcoff_debug_hooks): Likewise. * sdbout.c (sdb_debug_hooks): Likewise. * vmsdbgout.c (vmsdbg_debug_hooks): Likewise. * dwarf2out.c (macinfo_label_base): New global. (dwarf2out_register_external_die): New function for the register_external_die hook. (dwarf2out_die_ref_for_decl): Likewise for die_ref_for_decl. (dwarf2_debug_hooks): Use them. (dwarf2_lineno_debug_hooks): Adjust. (struct die_struct): Add with_offset flag. (DEBUG_LTO_DWO_INFO_SECTION, DEBUG_LTO_INFO_SECTION, DEBUG_LTO_DWO_ABBREV_SECTION, DEBUG_LTO_ABBREV_SECTION, DEBUG_LTO_DWO_MACINFO_SECTION, DEBUG_LTO_MACINFO_SECTION, DEBUG_LTO_DWO_MACRO_SECTION, DEBUG_LTO_MACRO_SECTION, DEBUG_LTO_LINE_SECTION, DEBUG_LTO_DWO_STR_OFFSETS_SECTION, DEBUG_LTO_STR_DWO_SECTION, DEBUG_STR_LTO_SECTION): New macros defining section names for the early LTO debug variants. (reset_indirect_string): New helper. (add_AT_external_die_ref): Helper for dwarf2out_register_external_die. (print_dw_val): Add support for offsetted symbol references. (get_ultimate_context): Split out from is_cxx. (is_cxx): Use get_ultimate_context. (is_fortran): Add decl overload. (compute_comp_unit_symbol): Split out worker from compute_section_prefix. (compute_section_prefix): Call compute_comp_unit_symbol and set comdat_type_p here. (output_die): Skip DIE symbol output for the LTO added one. Handle DIE symbol references with offset. (output_comp_unit): Guard section name mangling properly. For LTO debug sections emit a symbol at the section beginning which we use to refer to its DIEs. (add_abstract_origin_attribute): For DIEs registered via dwarf2out_register_external_die directly refer to the early DIE rather than indirectly through the shadow one we created. Remove obsolete call to dwarf2out_abstract_function for non-function/block origins. (gen_array_type_die): When generating early LTO debug do not emit DW_AT_string_length. (gen_formal_parameter_die): Do not re-create DIEs for PARM_DECLs late when in LTO. As suggested place a gcc_unreachable for the DECL_ABSTRACT_P case. (gen_subprogram_die): Avoid another specification DIE for early built declarations/definitions for the late LTO case. (gen_variable_die): Add type references for late duplicated VLA dies when in late LTO. (gen_inlined_subroutine_die): Do not call dwarf2out_abstract_function, we have the abstract instance already. (process_scope_var): Adjust decl DIE contexts in LTO which first puts them in limbo. (gen_decl_die): Do not generate type DIEs late apart from types for VLAs or for decls we do not yet have a DIE. Do not call dwarf2out_abstract_function late. (dwarf2out_early_global_decl): Make sure to create DIEs for abstract instances of a decl first. (dwarf2out_late_global_decl): Adjust comment. (output_macinfo_op): With multiple macro sections use macinfo_label_base to distinguish labels. (output_macinfo): Likewise. Update macinfo_label_base. Pass in the line info label. (note_variable_value_in_expr): When generating LTO resolve all variable values here by generating DIEs as needed. (init_sections_and_labels): Add early LTO debug flag parameter and generate different sections and names if set. Add generation counter for the labels so we can have multiple of them. (reset_dies): Helper to allow DIEs to be output multiple times. (dwarf2out_finish): When outputting DIEs to the fat part of an LTO object first reset DIEs. (dwarf2out_early_finish): Output early DIEs when generating LTO. (modified_type_die): Check for decl_ultimate_origin being self before recursing. (gen_type_die_with_usage): Likewise. (gen_typedef_die): Allow decl_ultimate_origin being self. (set_decl_abstract_flags): Remove. (set_block_abstract_flags): Likewise. (dwarf2out_abstract_function): Treat the early generated DIEs as the abstract copy and only add DW_AT_inline and DW_AT_artificial here and call set_decl_origin_self. If the DIE has an abstract origin don't do anything. * tree.c (free_lang_data): Build a dummy TRANSLATION_UNIT_DECL if we have none yet (Go fails to build one, PR78628). (variably_modified_type_p): Prevent endless recursion for Ada cyclic pointer types. * lto-streamer-in.c: Include debug.h. (dref_queue): New global. (lto_read_tree_1): Stream in DIE references. (lto_input_tree): Register DIE references. (input_function): Stream DECL_DEBUG_ARGS. * lto-streamer-out.c: Include debug.h. (lto_write_tree_1): Output DIE references. (DFS::DFS_write_tree_body): Follow DECL_ABSTRACT_ORIGIN. Force a TRANSLATION_UNIT_DECL DECL_CONTEXT for file-scope decls. (output_function): Stream DECL_DEBUG_ARGS. * tree-streamer-in.c (lto_input_ts_decl_common_tree_pointers): Stream DECL_ABSTRACT_ORIGIN. * tree-streamer-out.c (write_ts_decl_common_tree_pointers): Likewise. (write_ts_decl_minimal_tree_pointers): Force a TRANSLATION_UNIT_DECL DECL_CONTEXT for file-scope decls. * lto-streamer.h (struct dref_entry): Declare. (dref_queue): Likewise. * cfgexpand.c (pass_expand::execute): Do not call the outlining_inline_function hook here. * lto-wrapper.c (debug_obj): New global. (tool_cleanup): Unlink it if required. (debug_objcopy): New function. (run_gcc): Handle early debug sections in the IL files by extracting them to separate files, partially linkin them and feeding the result back as result to the linker. * config/darwin.h (DEBUG_LTO_INFO_SECTION, DEBUG_LTO_ABBREV_SECTION, DEBUG_LTO_MACINFO_SECTION, DEBUG_LTO_LINE_SECTION, DEBUG_STR_LTO_SECTION, DEBUG_LTO_MACRO_SECTION): Put early debug sections into a separate segment. * config/darwin.c (darwin_asm_named_section): Handle __GNU_DWARF_LTO segments. (darwin_asm_dwarf_section): Likewise. (darwin_asm_output_dwarf_offset): Likewise. * config/i386/i386.c (make_resolver_func): Set DECL_IGNORED_P. lto/ * lto.c (unify_scc): Truncate DIE reference queue for dropped SCCs. (lto_read_decls): Process TRANSLATION_UNIT_DECLs. Remove TYPE_DECL debug processing, register DIE references from prevailing SCCs with the debug machinery. (lto_section_with_id): Handle LTO debug sections. libstdc++/ * testsuite/libstdc++-prettyprinters/prettyprinters.exp: Run all tests with -flto as well if supported. testsuite/ * c-c++-common/asan/global-overflow-1.c: Adjust diagnostic location regex to handle the LTO case. * c-c++-common/asan/heap-overflow-1.c: Likewise. * c-c++-common/asan/misalign-1.c: Likewise. * c-c++-common/asan/misalign-2.c: Likewise. * c-c++-common/asan/null-deref-1.c: Likewise. * c-c++-common/asan/stack-overflow-1.c: Likewise. * c-c++-common/asan/strncpy-overflow-1.c: Likewise. * c-c++-common/asan/use-after-free-1.c: Likewise. * c-c++-common/asan/alloca_big_alignment.c: Likewise. * c-c++-common/asan/alloca_detect_custom_size.c: Likewise. * c-c++-common/asan/alloca_overflow_partial.c: Likewise. * c-c++-common/asan/alloca_overflow_right.c: Likewise. * c-c++-common/asan/alloca_underflow_left.c: Likewise. * g++.dg/asan/large-func-test-1.C: Likewise. * gfortran.dg/save_6.f90: Add -flto -g variant of save_5.f90. From-SVN: r251220
Diffstat (limited to 'gcc/lto-wrapper.c')
-rw-r--r--gcc/lto-wrapper.c179
1 files changed, 175 insertions, 4 deletions
diff --git a/gcc/lto-wrapper.c b/gcc/lto-wrapper.c
index 5e75a4f..841362a 100644
--- a/gcc/lto-wrapper.c
+++ b/gcc/lto-wrapper.c
@@ -70,6 +70,7 @@ static char **output_names;
static char **offload_names;
static char *offload_objects_file_name;
static char *makefile;
+static char *debug_obj;
const char tool_name[] = "lto-wrapper";
@@ -88,6 +89,8 @@ tool_cleanup (bool)
maybe_unlink (offload_objects_file_name);
if (makefile)
maybe_unlink (makefile);
+ if (debug_obj)
+ maybe_unlink (debug_obj);
for (i = 0; i < nr; ++i)
{
maybe_unlink (input_names[i]);
@@ -960,6 +963,67 @@ find_and_merge_options (int fd, off_t file_offset, const char *prefix,
return true;
}
+/* Copy early debug info sections from INFILE to a new file whose name
+ is returned. Return NULL on error. */
+
+const char *
+debug_objcopy (const char *infile)
+{
+ const char *outfile;
+ const char *errmsg;
+ int err;
+
+ const char *p;
+ off_t inoff = 0;
+ long loffset;
+ int consumed;
+ if ((p = strrchr (infile, '@'))
+ && p != infile
+ && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
+ && strlen (p) == (unsigned int) consumed)
+ {
+ char *fname = xstrdup (infile);
+ fname[p - infile] = '\0';
+ infile = fname;
+ inoff = (off_t) loffset;
+ }
+ int infd = open (infile, O_RDONLY);
+ if (infd == -1)
+ return NULL;
+ simple_object_read *inobj = simple_object_start_read (infd, inoff,
+ "__GNU_LTO",
+ &errmsg, &err);
+ if (!inobj)
+ return NULL;
+
+ off_t off, len;
+ if (simple_object_find_section (inobj, ".gnu.debuglto_.debug_info",
+ &off, &len, &errmsg, &err) != 1)
+ {
+ if (errmsg)
+ fatal_error (0, "%s: %s\n", errmsg, xstrerror (err));
+
+ simple_object_release_read (inobj);
+ close (infd);
+ return NULL;
+ }
+
+ outfile = make_temp_file ("debugobjtem");
+ errmsg = simple_object_copy_lto_debug_sections (inobj, outfile, &err);
+ if (errmsg)
+ {
+ unlink_if_ordinary (outfile);
+ fatal_error (0, "%s: %s\n", errmsg, xstrerror (err));
+ }
+
+ simple_object_release_read (inobj);
+ close (infd);
+
+ return outfile;
+}
+
+
+
/* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
static void
@@ -984,8 +1048,10 @@ run_gcc (unsigned argc, char *argv[])
int new_head_argc;
bool have_lto = false;
bool have_offload = false;
- unsigned lto_argc = 0;
- char **lto_argv;
+ unsigned lto_argc = 0, ltoobj_argc = 0;
+ char **lto_argv, **ltoobj_argv;
+ bool skip_debug = false;
+ unsigned n_debugobj;
/* Get the driver and options. */
collect_gcc = getenv ("COLLECT_GCC");
@@ -1004,6 +1070,7 @@ run_gcc (unsigned argc, char *argv[])
/* Allocate array for input object files with LTO IL,
and for possible preceding arguments. */
lto_argv = XNEWVEC (char *, argc);
+ ltoobj_argv = XNEWVEC (char *, argc);
/* Look at saved options in the IL files. */
for (i = 1; i < argc; ++i)
@@ -1046,7 +1113,7 @@ run_gcc (unsigned argc, char *argv[])
collect_gcc))
{
have_lto = true;
- lto_argv[lto_argc++] = argv[i];
+ ltoobj_argv[ltoobj_argc++] = argv[i];
}
close (fd);
}
@@ -1107,6 +1174,17 @@ run_gcc (unsigned argc, char *argv[])
}
}
+ /* Output lto-wrapper invocation command. */
+ if (verbose)
+ {
+ for (i = 0; i < argc; ++i)
+ {
+ fputs (argv[i], stderr);
+ fputc (' ', stderr);
+ }
+ fputc ('\n', stderr);
+ }
+
if (no_partition)
{
lto_mode = LTO_MODE_LTO;
@@ -1296,18 +1374,105 @@ cont1:
obstack_ptr_grow (&argv_obstack, "-fwpa");
}
- /* Append the input objects and possible preceding arguments. */
+ /* Append input arguments. */
for (i = 0; i < lto_argc; ++i)
obstack_ptr_grow (&argv_obstack, lto_argv[i]);
+ /* Append the input objects. */
+ for (i = 0; i < ltoobj_argc; ++i)
+ obstack_ptr_grow (&argv_obstack, ltoobj_argv[i]);
obstack_ptr_grow (&argv_obstack, NULL);
new_argv = XOBFINISH (&argv_obstack, const char **);
argv_ptr = &new_argv[new_head_argc];
fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true);
+ /* Handle early generated debug information. At compile-time
+ we output early DWARF debug info into .gnu.debuglto_ prefixed
+ sections. LTRANS object DWARF debug info refers to that.
+ So we need to transfer the .gnu.debuglto_ sections to the final
+ link. Ideally the linker plugin interface would allow us to
+ not claim those sections and instruct the linker to keep
+ them, renaming them in the process. For now we extract and
+ rename those sections via a simple-object interface to produce
+ regular objects containing only the early debug info. We
+ then partially link those to a single early debug info object
+ and pass that as additional output back to the linker plugin. */
+
+ /* Prepare the partial link to gather the compile-time generated
+ debug-info into a single input for the final link. */
+ debug_obj = make_temp_file ("debugobj");
+ obstack_ptr_grow (&argv_obstack, collect_gcc);
+ for (i = 1; i < decoded_options_count; ++i)
+ {
+ /* Retain linker choice and -B. */
+ if (decoded_options[i].opt_index == OPT_B
+ || decoded_options[i].opt_index == OPT_fuse_ld_bfd
+ || decoded_options[i].opt_index == OPT_fuse_ld_gold)
+ append_linker_options (&argv_obstack, &decoded_options[i-1], 2);
+ /* Retain all target options, this preserves -m32 for example. */
+ if (cl_options[decoded_options[i].opt_index].flags & CL_TARGET)
+ append_linker_options (&argv_obstack, &decoded_options[i-1], 2);
+ /* Recognize -g0. */
+ if (decoded_options[i].opt_index == OPT_g
+ && strcmp (decoded_options[i].arg, "0") == 0)
+ skip_debug = true;
+ }
+ obstack_ptr_grow (&argv_obstack, "-r");
+ obstack_ptr_grow (&argv_obstack, "-nostdlib");
+ obstack_ptr_grow (&argv_obstack, "-o");
+ obstack_ptr_grow (&argv_obstack, debug_obj);
+
+ /* Copy the early generated debug info from the objects to temporary
+ files and append those to the partial link commandline. */
+ n_debugobj = 0;
+ if (! skip_debug)
+ for (i = 0; i < ltoobj_argc; ++i)
+ {
+ const char *tem;
+ if ((tem = debug_objcopy (ltoobj_argv[i])))
+ {
+ obstack_ptr_grow (&argv_obstack, tem);
+ n_debugobj++;
+ }
+ }
+
+ /* Link them all into a single object. Ideally this would reduce
+ disk space usage mainly due to .debug_str merging but unfortunately
+ GNU ld doesn't perform this with -r. */
+ if (n_debugobj)
+ {
+ obstack_ptr_grow (&argv_obstack, NULL);
+ const char **debug_link_argv = XOBFINISH (&argv_obstack, const char **);
+ fork_execute (debug_link_argv[0],
+ CONST_CAST (char **, debug_link_argv), false);
+
+ /* And dispose the temporaries. */
+ for (i = 0; debug_link_argv[i]; ++i)
+ ;
+ for (--i; i > 0; --i)
+ {
+ if (strcmp (debug_link_argv[i], debug_obj) == 0)
+ break;
+ maybe_unlink (debug_link_argv[i]);
+ }
+ }
+ else
+ {
+ unlink_if_ordinary (debug_obj);
+ free (debug_obj);
+ debug_obj = NULL;
+ skip_debug = true;
+ }
+
if (lto_mode == LTO_MODE_LTO)
{
printf ("%s\n", flto_out);
+ if (!skip_debug)
+ {
+ printf ("%s\n", debug_obj);
+ free (debug_obj);
+ debug_obj = NULL;
+ }
free (flto_out);
flto_out = NULL;
}
@@ -1456,6 +1621,12 @@ cont:
for (i = 0; i < nr; ++i)
maybe_unlink (input_names[i]);
}
+ if (!skip_debug)
+ {
+ printf ("%s\n", debug_obj);
+ free (debug_obj);
+ debug_obj = NULL;
+ }
for (i = 0; i < nr; ++i)
{
fputs (output_names[i], stdout);