gcc/libiberty/simple-object.c
Richard Biener 1ea85365b4 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
2017-08-21 10:29:00 +00:00

523 lines
12 KiB
C

/* simple-object.c -- simple routines to read and write object files.
Copyright (C) 2010-2017 Free Software Foundation, Inc.
Written by Ian Lance Taylor, Google.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1301, USA. */
#include "config.h"
#include "libiberty.h"
#include "simple-object.h"
#include <errno.h>
#include <fcntl.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#ifndef SEEK_SET
#define SEEK_SET 0
#endif
#include "simple-object-common.h"
/* The known object file formats. */
static const struct simple_object_functions * const format_functions[] =
{
&simple_object_elf_functions,
&simple_object_mach_o_functions,
&simple_object_coff_functions,
&simple_object_xcoff_functions
};
/* Read data from a file using the simple_object error reporting
conventions. */
int
simple_object_internal_read (int descriptor, off_t offset,
unsigned char *buffer, size_t size,
const char **errmsg, int *err)
{
if (lseek (descriptor, offset, SEEK_SET) < 0)
{
*errmsg = "lseek";
*err = errno;
return 0;
}
do
{
ssize_t got = read (descriptor, buffer, size);
if (got == 0)
break;
else if (got > 0)
{
buffer += got;
size -= got;
}
else if (errno != EINTR)
{
*errmsg = "read";
*err = errno;
return 0;
}
}
while (size > 0);
if (size > 0)
{
*errmsg = "file too short";
*err = 0;
return 0;
}
return 1;
}
/* Write data to a file using the simple_object error reporting
conventions. */
int
simple_object_internal_write (int descriptor, off_t offset,
const unsigned char *buffer, size_t size,
const char **errmsg, int *err)
{
if (lseek (descriptor, offset, SEEK_SET) < 0)
{
*errmsg = "lseek";
*err = errno;
return 0;
}
do
{
ssize_t wrote = write (descriptor, buffer, size);
if (wrote == 0)
break;
else if (wrote > 0)
{
buffer += wrote;
size -= wrote;
}
else if (errno != EINTR)
{
*errmsg = "write";
*err = errno;
return 0;
}
}
while (size > 0);
if (size > 0)
{
*errmsg = "short write";
*err = 0;
return 0;
}
return 1;
}
/* Open for read. */
simple_object_read *
simple_object_start_read (int descriptor, off_t offset,
const char *segment_name, const char **errmsg,
int *err)
{
unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN];
size_t len, i;
if (!simple_object_internal_read (descriptor, offset, header,
SIMPLE_OBJECT_MATCH_HEADER_LEN,
errmsg, err))
return NULL;
len = sizeof (format_functions) / sizeof (format_functions[0]);
for (i = 0; i < len; ++i)
{
void *data;
data = format_functions[i]->match (header, descriptor, offset,
segment_name, errmsg, err);
if (data != NULL)
{
simple_object_read *ret;
ret = XNEW (simple_object_read);
ret->descriptor = descriptor;
ret->offset = offset;
ret->functions = format_functions[i];
ret->data = data;
return ret;
}
}
*errmsg = "file not recognized";
*err = 0;
return NULL;
}
/* Find all sections. */
const char *
simple_object_find_sections (simple_object_read *sobj,
int (*pfn) (void *, const char *, off_t, off_t),
void *data,
int *err)
{
return sobj->functions->find_sections (sobj, pfn, data, err);
}
/* Internal data passed to find_one_section. */
struct find_one_section_data
{
/* The section we are looking for. */
const char *name;
/* Where to store the section offset. */
off_t *offset;
/* Where to store the section length. */
off_t *length;
/* Set if the name is found. */
int found;
};
/* Internal function passed to find_sections. */
static int
find_one_section (void *data, const char *name, off_t offset, off_t length)
{
struct find_one_section_data *fosd = (struct find_one_section_data *) data;
if (strcmp (name, fosd->name) != 0)
return 1;
*fosd->offset = offset;
*fosd->length = length;
fosd->found = 1;
/* Stop iteration. */
return 0;
}
/* Find a section. */
int
simple_object_find_section (simple_object_read *sobj, const char *name,
off_t *offset, off_t *length,
const char **errmsg, int *err)
{
struct find_one_section_data fosd;
fosd.name = name;
fosd.offset = offset;
fosd.length = length;
fosd.found = 0;
*errmsg = simple_object_find_sections (sobj, find_one_section,
(void *) &fosd, err);
if (*errmsg != NULL)
return 0;
if (!fosd.found)
return 0;
return 1;
}
/* Callback to identify and rename LTO debug sections by name.
Returns 1 if NAME is a LTO debug section, 0 if not. */
static int
handle_lto_debug_sections (const char **name)
{
/* ??? So we can't use .gnu.lto_ prefixed sections as the assembler
complains about bogus section flags. Which means we need to arrange
for that to be fixed or .gnu.debuglto_ marked as SHF_EXCLUDE (to make
fat lto object tooling work for the fat part). */
/* ??? For now this handles both .gnu.lto_ and .gnu.debuglto_ prefixed
sections. */
/* Copy LTO debug sections and rename them to their non-LTO name. */
if (strncmp (*name, ".gnu.debuglto_", sizeof (".gnu.debuglto_") - 1) == 0)
{
*name = *name + sizeof (".gnu.debuglto_") - 1;
return 1;
}
else if (strncmp (*name, ".gnu.lto_.debug_", sizeof (".gnu.lto_.debug_") -1) == 0)
{
*name = *name + sizeof (".gnu.lto_") - 1;
return 1;
}
return 0;
}
/* Copy LTO debug sections. */
const char *
simple_object_copy_lto_debug_sections (simple_object_read *sobj,
const char *dest, int *err)
{
const char *errmsg;
simple_object_write *dest_sobj;
simple_object_attributes *attrs;
int outfd;
if (! sobj->functions->copy_lto_debug_sections)
{
*err = EINVAL;
return "simple_object_copy_lto_debug_sections not implemented";
}
attrs = simple_object_fetch_attributes (sobj, &errmsg, err);
if (! attrs)
return errmsg;
dest_sobj = simple_object_start_write (attrs, NULL, &errmsg, err);
simple_object_release_attributes (attrs);
if (! dest_sobj)
return errmsg;
errmsg = sobj->functions->copy_lto_debug_sections (sobj, dest_sobj,
handle_lto_debug_sections,
err);
if (errmsg)
{
simple_object_release_write (dest_sobj);
return errmsg;
}
outfd = creat (dest, 00777);
if (outfd == -1)
{
*err = errno;
simple_object_release_write (dest_sobj);
return "open failed";
}
errmsg = simple_object_write_to_file (dest_sobj, outfd, err);
close (outfd);
if (errmsg)
{
simple_object_release_write (dest_sobj);
return errmsg;
}
simple_object_release_write (dest_sobj);
return NULL;
}
/* Fetch attributes. */
simple_object_attributes *
simple_object_fetch_attributes (simple_object_read *sobj, const char **errmsg,
int *err)
{
void *data;
simple_object_attributes *ret;
data = sobj->functions->fetch_attributes (sobj, errmsg, err);
if (data == NULL)
return NULL;
ret = XNEW (simple_object_attributes);
ret->functions = sobj->functions;
ret->data = data;
return ret;
}
/* Release an simple_object_read. */
void
simple_object_release_read (simple_object_read *sobj)
{
sobj->functions->release_read (sobj->data);
XDELETE (sobj);
}
/* Merge attributes. */
const char *
simple_object_attributes_merge (simple_object_attributes *to,
simple_object_attributes *from,
int *err)
{
if (to->functions != from->functions)
{
*err = 0;
return "different object file format";
}
return to->functions->attributes_merge (to->data, from->data, err);
}
/* Release an attributes structure. */
void
simple_object_release_attributes (simple_object_attributes *attrs)
{
attrs->functions->release_attributes (attrs->data);
XDELETE (attrs);
}
/* Start creating an object file. */
simple_object_write *
simple_object_start_write (simple_object_attributes *attrs,
const char *segment_name, const char **errmsg,
int *err)
{
void *data;
simple_object_write *ret;
data = attrs->functions->start_write (attrs->data, errmsg, err);
if (data == NULL)
return NULL;
ret = XNEW (simple_object_write);
ret->functions = attrs->functions;
ret->segment_name = segment_name ? xstrdup (segment_name) : NULL;
ret->sections = NULL;
ret->last_section = NULL;
ret->data = data;
return ret;
}
/* Start creating a section. */
simple_object_write_section *
simple_object_write_create_section (simple_object_write *sobj, const char *name,
unsigned int align,
const char **errmsg ATTRIBUTE_UNUSED,
int *err ATTRIBUTE_UNUSED)
{
simple_object_write_section *ret;
ret = XNEW (simple_object_write_section);
ret->next = NULL;
ret->name = xstrdup (name);
ret->align = align;
ret->buffers = NULL;
ret->last_buffer = NULL;
if (sobj->last_section == NULL)
{
sobj->sections = ret;
sobj->last_section = ret;
}
else
{
sobj->last_section->next = ret;
sobj->last_section = ret;
}
return ret;
}
/* Add data to a section. */
const char *
simple_object_write_add_data (simple_object_write *sobj ATTRIBUTE_UNUSED,
simple_object_write_section *section,
const void *buffer,
size_t size, int copy,
int *err ATTRIBUTE_UNUSED)
{
struct simple_object_write_section_buffer *wsb;
wsb = XNEW (struct simple_object_write_section_buffer);
wsb->next = NULL;
wsb->size = size;
if (!copy)
{
wsb->buffer = buffer;
wsb->free_buffer = NULL;
}
else
{
wsb->free_buffer = (void *) XNEWVEC (char, size);
memcpy (wsb->free_buffer, buffer, size);
wsb->buffer = wsb->free_buffer;
}
if (section->last_buffer == NULL)
{
section->buffers = wsb;
section->last_buffer = wsb;
}
else
{
section->last_buffer->next = wsb;
section->last_buffer = wsb;
}
return NULL;
}
/* Write the complete object file. */
const char *
simple_object_write_to_file (simple_object_write *sobj, int descriptor,
int *err)
{
return sobj->functions->write_to_file (sobj, descriptor, err);
}
/* Release an simple_object_write. */
void
simple_object_release_write (simple_object_write *sobj)
{
simple_object_write_section *section;
free (sobj->segment_name);
section = sobj->sections;
while (section != NULL)
{
struct simple_object_write_section_buffer *buffer;
simple_object_write_section *next_section;
buffer = section->buffers;
while (buffer != NULL)
{
struct simple_object_write_section_buffer *next_buffer;
if (buffer->free_buffer != NULL)
XDELETEVEC (buffer->free_buffer);
next_buffer = buffer->next;
XDELETE (buffer);
buffer = next_buffer;
}
next_section = section->next;
free (section->name);
XDELETE (section);
section = next_section;
}
sobj->functions->release_write (sobj->data);
XDELETE (sobj);
}