From cf95b909e2c29476525da29bfb4f0d2c8211e8b6 Mon Sep 17 00:00:00 2001 From: Alan Modra Date: Wed, 14 Feb 2024 14:34:47 +1030 Subject: [PATCH] elf_backend_finish_dynamic_symbol returning false Returning false from elf_backend_finish_dynamic_symbol will not result in an error being printed unless bfd_error is set but will result in the linker exiting with a non-zero status. If just bfd_error is set then a generic "final link failed" will result, which doesn't help a user much. So elf_backend_finish_dynamic_symbol should print its own error message whenever returning false, or use BFD_ASSERT or abort to print assertion failures for conditions that shouldn't occur. This patch does that, and removes unnecessary "htab != NULL" tests in elf_backend_finish_dynamic_symbol. Such tests aren't needed in a function only called via elf_backend_data. --- bfd/elf32-arc.c | 3 --- bfd/elf32-arm.c | 2 -- bfd/elf32-cris.c | 2 -- bfd/elf32-csky.c | 2 -- bfd/elf32-hppa.c | 2 -- bfd/elf32-i386.c | 2 -- bfd/elf32-lm32.c | 2 -- bfd/elf32-m32r.c | 2 -- bfd/elf32-microblaze.c | 2 -- bfd/elf32-or1k.c | 2 -- bfd/elf32-s390.c | 3 +-- bfd/elf32-score.c | 8 +++++++- bfd/elf32-score7.c | 8 +++++++- bfd/elf32-sh.c | 2 -- bfd/elf64-hppa.c | 2 -- bfd/elf64-ia64-vms.c | 2 -- bfd/elf64-ppc.c | 2 -- bfd/elf64-s390.c | 5 +---- bfd/elf64-x86-64.c | 5 +---- bfd/elfnn-aarch64.c | 6 ++---- bfd/elfnn-ia64.c | 2 -- bfd/elfnn-kvx.c | 3 +-- bfd/elfnn-riscv.c | 2 +- bfd/elfxx-mips.c | 9 ++++++++- 24 files changed, 29 insertions(+), 51 deletions(-) diff --git a/bfd/elf32-arc.c b/bfd/elf32-arc.c index 1061e54e9d7..5727bfbf889 100644 --- a/bfd/elf32-arc.c +++ b/bfd/elf32-arc.c @@ -2486,9 +2486,6 @@ elf_arc_finish_dynamic_symbol (bfd * output_bfd, { struct elf_arc_link_hash_table *arc_htab = elf_arc_hash_table (info); - if (arc_htab == NULL) - return false; - if (h->dynindx == -1 || (h->root.type != bfd_link_hash_defined && h->root.type != bfd_link_hash_defweak) diff --git a/bfd/elf32-arm.c b/bfd/elf32-arm.c index 4ad7c3542a7..1913be50cfe 100644 --- a/bfd/elf32-arm.c +++ b/bfd/elf32-arm.c @@ -17199,8 +17199,6 @@ elf32_arm_finish_dynamic_symbol (bfd * output_bfd, struct elf32_arm_link_hash_entry *eh; htab = elf32_arm_hash_table (info); - if (htab == NULL) - return false; eh = (struct elf32_arm_link_hash_entry *) h; diff --git a/bfd/elf32-cris.c b/bfd/elf32-cris.c index 188f1e1e8e8..e2e402a593e 100644 --- a/bfd/elf32-cris.c +++ b/bfd/elf32-cris.c @@ -2082,8 +2082,6 @@ elf_cris_finish_dynamic_symbol (bfd *output_bfd, const bfd_byte *plt_pic_entry = elf_cris_pic_plt_entry; htab = elf_cris_hash_table (info); - if (htab == NULL) - return false; /* Adjust the various PLT entry offsets. */ if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32) diff --git a/bfd/elf32-csky.c b/bfd/elf32-csky.c index 9479705d37e..87c37259fa1 100644 --- a/bfd/elf32-csky.c +++ b/bfd/elf32-csky.c @@ -2105,8 +2105,6 @@ csky_elf_finish_dynamic_symbol (bfd *output_bfd, struct csky_elf_link_hash_table *htab; htab = csky_elf_hash_table (info); - if (htab == NULL) - return false; /* Sanity check to make sure no unexpected symbol reaches here. This matches the test in csky_elf_relocate_section handling diff --git a/bfd/elf32-hppa.c b/bfd/elf32-hppa.c index 28aadfb8f9c..bb4bfdc00b8 100644 --- a/bfd/elf32-hppa.c +++ b/bfd/elf32-hppa.c @@ -4139,8 +4139,6 @@ elf32_hppa_finish_dynamic_symbol (bfd *output_bfd, bfd_byte *loc; htab = hppa_link_hash_table (info); - if (htab == NULL) - return false; if (eh->plt.offset != (bfd_vma) -1) { diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c index e2f88a11487..703a48c2c0a 100644 --- a/bfd/elf32-i386.c +++ b/bfd/elf32-i386.c @@ -3581,8 +3581,6 @@ elf_i386_finish_dynamic_symbol (bfd *output_bfd, bool use_plt_second; htab = elf_x86_hash_table (info, I386_ELF_DATA); - if (htab == NULL) - return false; plt_entry_size = htab->plt.plt_entry_size; diff --git a/bfd/elf32-lm32.c b/bfd/elf32-lm32.c index 80223e6941d..b0eaa06180f 100644 --- a/bfd/elf32-lm32.c +++ b/bfd/elf32-lm32.c @@ -1427,8 +1427,6 @@ lm32_elf_finish_dynamic_symbol (bfd *output_bfd, bfd_byte *loc; htab = lm32_elf_hash_table (info); - if (htab == NULL) - return false; if (h->plt.offset != (bfd_vma) -1) { diff --git a/bfd/elf32-m32r.c b/bfd/elf32-m32r.c index 41468a64c48..ab2f45a8946 100644 --- a/bfd/elf32-m32r.c +++ b/bfd/elf32-m32r.c @@ -2877,8 +2877,6 @@ m32r_elf_finish_dynamic_symbol (bfd *output_bfd, #endif htab = m32r_elf_hash_table (info); - if (htab == NULL) - return false; if (h->plt.offset != (bfd_vma) -1) { diff --git a/bfd/elf32-microblaze.c b/bfd/elf32-microblaze.c index 64198b8f1a6..fd2855d4f9f 100644 --- a/bfd/elf32-microblaze.c +++ b/bfd/elf32-microblaze.c @@ -3168,8 +3168,6 @@ microblaze_elf_finish_dynamic_symbol (bfd *output_bfd, struct elf32_mb_link_hash_entry *eh = elf32_mb_hash_entry(h); htab = elf32_mb_hash_table (info); - if (htab == NULL) - return false; if (h->plt.offset != (bfd_vma) -1) { diff --git a/bfd/elf32-or1k.c b/bfd/elf32-or1k.c index 81d63d45c29..358d2e3940f 100644 --- a/bfd/elf32-or1k.c +++ b/bfd/elf32-or1k.c @@ -2451,8 +2451,6 @@ or1k_elf_finish_dynamic_symbol (bfd *output_bfd, bfd_byte *loc; htab = or1k_elf_hash_table (info); - if (htab == NULL) - return false; if (h->plt.offset != (bfd_vma) -1) { diff --git a/bfd/elf32-s390.c b/bfd/elf32-s390.c index 1a2ade0414e..06d54e6fa2d 100644 --- a/bfd/elf32-s390.c +++ b/bfd/elf32-s390.c @@ -3505,8 +3505,7 @@ elf_s390_finish_dynamic_symbol (bfd *output_bfd, RELATIVE reloc. The entry in the global offset table will already have been initialized in the relocate_section function. */ - if (!(h->def_regular || ELF_COMMON_DEF_P (h))) - return false; + BFD_ASSERT (h->def_regular || ELF_COMMON_DEF_P (h)); BFD_ASSERT((h->got.offset & 1) != 0); rela.r_info = ELF32_R_INFO (0, R_390_RELATIVE); rela.r_addend = (h->root.u.def.value diff --git a/bfd/elf32-score.c b/bfd/elf32-score.c index 448aba87c86..0c4a29f336c 100644 --- a/bfd/elf32-score.c +++ b/bfd/elf32-score.c @@ -3485,7 +3485,13 @@ s3_bfd_score_elf_finish_dynamic_symbol (bfd *output_bfd, /* FIXME: Can h->dynindex be more than 64K? */ if (h->dynindx & 0xffff0000) - return false; + { + _bfd_error_handler + (_("%pB: cannot handle more than %d dynamic symbols"), + output_bfd, 0xffff); + bfd_set_error (bfd_error_bad_value); + return false; + } /* Fill the stub. */ score_bfd_put_32 (output_bfd, STUB_LW, stub); diff --git a/bfd/elf32-score7.c b/bfd/elf32-score7.c index c20fa7a3eca..d1155bfdb13 100644 --- a/bfd/elf32-score7.c +++ b/bfd/elf32-score7.c @@ -3296,7 +3296,13 @@ s7_bfd_score_elf_finish_dynamic_symbol (bfd *output_bfd, /* FIXME: Can h->dynindex be more than 64K? */ if (h->dynindx & 0xffff0000) - return false; + { + _bfd_error_handler + (_("%pB: cannot handle more than %d dynamic symbols"), + output_bfd, 0xffff); + bfd_set_error (bfd_error_bad_value); + return false; + } /* Fill the stub. */ bfd_put_32 (output_bfd, STUB_LW, stub); diff --git a/bfd/elf32-sh.c b/bfd/elf32-sh.c index 0f5117b6beb..bf4cb2a8242 100644 --- a/bfd/elf32-sh.c +++ b/bfd/elf32-sh.c @@ -5945,8 +5945,6 @@ sh_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info, struct elf_sh_link_hash_table *htab; htab = sh_elf_hash_table (info); - if (htab == NULL) - return false; if (h->plt.offset != (bfd_vma) -1) { diff --git a/bfd/elf64-hppa.c b/bfd/elf64-hppa.c index 59e79830548..bc91bc4c08a 100644 --- a/bfd/elf64-hppa.c +++ b/bfd/elf64-hppa.c @@ -1898,8 +1898,6 @@ elf64_hppa_finish_dynamic_symbol (bfd *output_bfd, struct elf64_hppa_link_hash_table *hppa_info; hppa_info = hppa_link_hash_table (info); - if (hppa_info == NULL) - return false; stub = hppa_info->stub_sec; splt = hppa_info->root.splt; diff --git a/bfd/elf64-ia64-vms.c b/bfd/elf64-ia64-vms.c index 2f37e90cc15..aa058a1793e 100644 --- a/bfd/elf64-ia64-vms.c +++ b/bfd/elf64-ia64-vms.c @@ -4003,8 +4003,6 @@ elf64_ia64_finish_dynamic_symbol (bfd *output_bfd, struct elf64_ia64_dyn_sym_info *dyn_i; ia64_info = elf64_ia64_hash_table (info); - if (ia64_info == NULL) - return false; dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false); diff --git a/bfd/elf64-ppc.c b/bfd/elf64-ppc.c index e95f9fbe651..508419bfedc 100644 --- a/bfd/elf64-ppc.c +++ b/bfd/elf64-ppc.c @@ -18135,8 +18135,6 @@ ppc64_elf_finish_dynamic_symbol (bfd *output_bfd, struct plt_entry *ent; htab = ppc_hash_table (info); - if (htab == NULL) - return false; if (!htab->opd_abi && !h->def_regular) for (ent = h->plt.plist; ent != NULL; ent = ent->next) diff --git a/bfd/elf64-s390.c b/bfd/elf64-s390.c index a7979604a6f..bef90f6ed84 100644 --- a/bfd/elf64-s390.c +++ b/bfd/elf64-s390.c @@ -3225,8 +3225,6 @@ elf_s390_finish_dynamic_symbol (bfd *output_bfd, struct elf_s390_link_hash_entry *eh = (struct elf_s390_link_hash_entry*)h; htab = elf_s390_hash_table (info); - if (htab == NULL) - return false; if (h->plt.offset != (bfd_vma) -1) { @@ -3373,8 +3371,7 @@ elf_s390_finish_dynamic_symbol (bfd *output_bfd, RELATIVE reloc. The entry in the global offset table will already have been initialized in the relocate_section function. */ - if (!(h->def_regular || ELF_COMMON_DEF_P (h))) - return false; + BFD_ASSERT (h->def_regular || ELF_COMMON_DEF_P (h)); BFD_ASSERT((h->got.offset & 1) != 0); rela.r_info = ELF64_R_INFO (0, R_390_RELATIVE); rela.r_addend = (h->root.u.def.value diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c index 2ed120af780..6cc70a7d4c3 100644 --- a/bfd/elf64-x86-64.c +++ b/bfd/elf64-x86-64.c @@ -4581,8 +4581,6 @@ elf_x86_64_finish_dynamic_symbol (bfd *output_bfd, bool local_undefweak; htab = elf_x86_hash_table (info, X86_64_ELF_DATA); - if (htab == NULL) - return false; /* Use the second PLT section only if there is .plt section. */ use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL; @@ -4923,8 +4921,7 @@ elf_x86_64_finish_dynamic_symbol (bfd *output_bfd, else if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL_P (info, h)) { - if (!SYMBOL_DEFINED_NON_SHARED_P (h)) - return false; + BFD_ASSERT (SYMBOL_DEFINED_NON_SHARED_P (h)); BFD_ASSERT((h->got.offset & 1) != 0); if (info->enable_dt_relr) generate_dynamic_reloc = false; diff --git a/bfd/elfnn-aarch64.c b/bfd/elfnn-aarch64.c index 109517db4aa..cf21f62d73a 100644 --- a/bfd/elfnn-aarch64.c +++ b/bfd/elfnn-aarch64.c @@ -9671,7 +9671,7 @@ elfNN_aarch64_finish_dynamic_symbol (bfd *output_bfd, || plt == NULL || gotplt == NULL || relplt == NULL) - return false; + abort (); elfNN_aarch64_create_small_pltn_entry (h, htab, output_bfd, info); if (!h->def_regular) @@ -9739,9 +9739,7 @@ elfNN_aarch64_finish_dynamic_symbol (bfd *output_bfd, } else if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h)) { - if (!(h->def_regular || ELF_COMMON_DEF_P (h))) - return false; - + BFD_ASSERT (h->def_regular || ELF_COMMON_DEF_P (h)); BFD_ASSERT ((h->got.offset & 1) != 0); rela.r_info = ELFNN_R_INFO (0, AARCH64_R (RELATIVE)); rela.r_addend = (h->root.u.def.value diff --git a/bfd/elfnn-ia64.c b/bfd/elfnn-ia64.c index 7081ba1bcbc..9951a47349b 100644 --- a/bfd/elfnn-ia64.c +++ b/bfd/elfnn-ia64.c @@ -4517,8 +4517,6 @@ elfNN_ia64_finish_dynamic_symbol (bfd *output_bfd, struct elfNN_ia64_dyn_sym_info *dyn_i; ia64_info = elfNN_ia64_hash_table (info); - if (ia64_info == NULL) - return false; dyn_i = get_dyn_sym_info (ia64_info, h, NULL, NULL, false); diff --git a/bfd/elfnn-kvx.c b/bfd/elfnn-kvx.c index ae5ed6bf3f7..00446c9a8d8 100644 --- a/bfd/elfnn-kvx.c +++ b/bfd/elfnn-kvx.c @@ -4479,8 +4479,7 @@ elfNN_kvx_finish_dynamic_symbol (bfd *output_bfd, if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h)) { - if (!h->def_regular) - return false; + BFD_ASSERT (h->def_regular); /* in case of PLT related GOT entry, it is not clear who is supposed to set the LSB of GOT entry... diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 8b27e3b8d6a..951b19a911d 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -3136,7 +3136,7 @@ riscv_elf_finish_dynamic_symbol (bfd *output_bfd, || plt == NULL || gotplt == NULL || relplt == NULL) - return false; + abort (); /* Calculate the address of the PLT header. */ header_address = sec_addr (plt); diff --git a/bfd/elfxx-mips.c b/bfd/elfxx-mips.c index b888e7622b7..47903069271 100644 --- a/bfd/elfxx-mips.c +++ b/bfd/elfxx-mips.c @@ -6842,6 +6842,7 @@ mips_elf_create_dynamic_relocation (bfd *output_bfd, indx = 0; else if (sec == NULL || sec->owner == NULL) { + BFD_ASSERT (0); bfd_set_error (bfd_error_bad_value); return false; } @@ -11186,7 +11187,13 @@ _bfd_mips_elf_finish_dynamic_symbol (bfd *output_bfd, sign extension at runtime in the stub, resulting in a negative index value. */ if (h->dynindx & ~0x7fffffff) - return false; + { + _bfd_error_handler + (_("%pB: cannot handle more than %d dynamic symbols"), + output_bfd, 0x7fffffff); + bfd_set_error (bfd_error_bad_value); + return false; + } /* Fill the stub. */ if (micromips_p) -- 2.39.5