tweak __ehdr_start visibility and flags for check_relocs

bfd/
	* elf-bfd.h (UNDEFWEAK_NO_DYNAMIC_RELOC): Test linker_def.
ld/
	* ldelf.c (ldelf_before_allocation): Don't force __ehdr_start
	local and hidden here..
	* ldlang.c (lang_symbol_tweaks): ..do so here instead and set
	def_regular and linker_def for check_relocs.  New function
	extracted from lang_process.
This commit is contained in:
Alan Modra 2022-01-12 23:42:23 +10:30
parent 1f95afb889
commit 97da0e2677
3 changed files with 42 additions and 11 deletions

View file

@ -291,9 +291,12 @@ weakdef (struct elf_link_hash_entry *h)
_bfd_elf_symbol_refs_local_p (H, INFO, 1) _bfd_elf_symbol_refs_local_p (H, INFO, 1)
/* Whether an undefined weak symbol should resolve to its link-time /* Whether an undefined weak symbol should resolve to its link-time
value, even in PIC or PIE objects. */ value, even in PIC or PIE objects. The linker_def test is to
handle symbols like __ehdr_start that may be undefweak in early
stages of linking but are guaranteed to be defined later. */
#define UNDEFWEAK_NO_DYNAMIC_RELOC(INFO, H) \ #define UNDEFWEAK_NO_DYNAMIC_RELOC(INFO, H) \
((H)->root.type == bfd_link_hash_undefweak \ ((H)->root.type == bfd_link_hash_undefweak \
&& !(H)->root.linker_def \
&& (ELF_ST_VISIBILITY ((H)->other) != STV_DEFAULT \ && (ELF_ST_VISIBILITY ((H)->other) != STV_DEFAULT \
|| (INFO)->dynamic_undefined_weak == 0)) || (INFO)->dynamic_undefined_weak == 0))

View file

@ -1607,11 +1607,6 @@ ldelf_before_allocation (char *audit, char *depaudit,
|| h->root.type == bfd_link_hash_undefweak || h->root.type == bfd_link_hash_undefweak
|| h->root.type == bfd_link_hash_common)) || h->root.type == bfd_link_hash_common))
{ {
const struct elf_backend_data *bed;
bed = get_elf_backend_data (link_info.output_bfd);
(*bed->elf_backend_hide_symbol) (&link_info, h, true);
if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
/* Don't leave the symbol undefined. Undefined hidden /* Don't leave the symbol undefined. Undefined hidden
symbols typically won't have dynamic relocations, but symbols typically won't have dynamic relocations, but
we most likely will need dynamic relocations for we most likely will need dynamic relocations for

View file

@ -6994,6 +6994,43 @@ lang_finalize_start_stop (void)
foreach_start_stop (set_start_stop); foreach_start_stop (set_start_stop);
} }
static void
lang_symbol_tweaks (void)
{
/* Give initial values for __start and __stop symbols, so that ELF
gc_sections will keep sections referenced by these symbols. Must
be done before lang_do_assignments. */
if (config.build_constructors)
lang_init_start_stop ();
/* Make __ehdr_start hidden, and set def_regular even though it is
likely undefined at this stage. For lang_check_relocs. */
if (is_elf_hash_table (link_info.hash)
&& !bfd_link_relocatable (&link_info))
{
struct elf_link_hash_entry *h = (struct elf_link_hash_entry *)
bfd_link_hash_lookup (link_info.hash, "__ehdr_start",
false, false, true);
/* Only adjust the export class if the symbol was referenced
and not defined, otherwise leave it alone. */
if (h != NULL
&& (h->root.type == bfd_link_hash_new
|| h->root.type == bfd_link_hash_undefined
|| h->root.type == bfd_link_hash_undefweak
|| h->root.type == bfd_link_hash_common))
{
const struct elf_backend_data *bed;
bed = get_elf_backend_data (link_info.output_bfd);
(*bed->elf_backend_hide_symbol) (&link_info, h, true);
if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
h->def_regular = 1;
h->root.linker_def = 1;
}
}
}
static void static void
lang_end (void) lang_end (void)
{ {
@ -8210,11 +8247,7 @@ lang_process (void)
files. */ files. */
ldctor_build_sets (); ldctor_build_sets ();
/* Give initial values for __start and __stop symbols, so that ELF lang_symbol_tweaks ();
gc_sections will keep sections referenced by these symbols. Must
be done before lang_do_assignments below. */
if (config.build_constructors)
lang_init_start_stop ();
/* PR 13683: We must rerun the assignments prior to running garbage /* PR 13683: We must rerun the assignments prior to running garbage
collection in order to make sure that all symbol aliases are resolved. */ collection in order to make sure that all symbol aliases are resolved. */