Handle symbol defined in IR and referenced in DSO

We need to make an IR symbol visible if it is defined in an IR object
and referenced in a dynamic object.  When --as-needed is used, since
linker removes the IR symbol reference of the dynamic object if the
dynamic object isn't needed in the first pass, the IR definition isn't
visible to the dynamic object even if the dynamic object becomes needed
in the second pass.  Add dynamic_ref_after_ir_def to bfd_link_hash_entry
to track IR symbol which is defined in an IR object and later referenced
in a dynamic object.  dynamic_ref_after_ir_def is preserved when restoring
the symbol table for unneeded dynamic object.

bfd/

	PR ld/21382
	* elflink.c (elf_link_add_object_symbols): Preserve
	dynamic_ref_after_ir_def when restoring the symbol table for
	unneeded dynamic object.

include/

	PR ld/21382
	* bfdlink.h (bfd_link_hash_entry): Add dynamic_ref_after_ir_def.

ld/

	PR ld/21382
	* plugin.c (is_visible_from_outside): Symbol may be visible
	from outside if dynamic_ref_after_ir_def is set.
	(plugin_notice): Set dynamic_ref_after_ir_def if the symbol is
	defined in an IR object and referenced in a dynamic object.
	* testsuite/ld-plugin/lto.exp: Run PR ld/21382 tests.
	* testsuite/ld-plugin/pr21382a.c: New file.
	* testsuite/ld-plugin/pr21382b.c: Likewise.
This commit is contained in:
H.J. Lu
2017-04-20 07:48:24 -07:00
parent c768868859
commit 59fa66c538
9 changed files with 83 additions and 6 deletions

View File

@@ -629,7 +629,9 @@ is_visible_from_outside (struct ld_plugin_symbol *lsym,
if (bfd_link_relocatable (&link_info))
return TRUE;
if (link_info.export_dynamic || bfd_link_dll (&link_info))
if (blhe->dynamic_ref_after_ir_def
|| link_info.export_dynamic
|| bfd_link_dll (&link_info))
{
/* Check if symbol is hidden by version script. */
if (bfd_hide_sym_by_version (link_info.version_info,
@@ -1316,12 +1318,21 @@ plugin_notice (struct bfd_link_info *info,
/* If this is a ref, set non_ir_ref. */
else if (bfd_is_und_section (section))
{
if (h->type == bfd_link_hash_defweak
|| h->type == bfd_link_hash_defined)
{
/* Check if the symbol is referenced in a dynamic object
after it has been defined in an IR object. */
if ((abfd->flags & DYNAMIC) != 0
&& is_ir_dummy_bfd (h->u.def.section->owner))
h->dynamic_ref_after_ir_def = TRUE;
}
/* Replace the undefined dummy bfd with the real one. */
if ((h->type == bfd_link_hash_undefined
|| h->type == bfd_link_hash_undefweak)
&& (h->u.undef.abfd == NULL
|| (h->u.undef.abfd->flags & BFD_PLUGIN) != 0))
h->u.undef.abfd = abfd;
else if ((h->type == bfd_link_hash_undefined
|| h->type == bfd_link_hash_undefweak)
&& (h->u.undef.abfd == NULL
|| (h->u.undef.abfd->flags & BFD_PLUGIN) != 0))
h->u.undef.abfd = abfd;
h->non_ir_ref = TRUE;
}