From 81d2389acc88dd716c42c5197d03b937b4022c21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Chigot?= Date: Tue, 16 Feb 2021 10:38:52 +0100 Subject: [PATCH 3/6] aix: remove handlers for negative r_symndx in relocate_section MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit r_symndx can't be negative AFAIK. Thus all the checks are useless. I still let a single BFD_ASSERT if it even happens. In this case, revert this commit and maybe adds some checks in new relocation functions. bfd/ChangeLog: 2020-10-29 Clément Chigot * coff-rs6000.c (xcoff_reloc_type_toc): Remove negative rel->r_symndx handler. (xcoff_reloc_type_br): Likewize. (xcoff_ppc_relocate_section): Add BFD_ASSERT to prevent negative rel->r_symndx instead of a single if. * coff64-rs6000.c (xcoff64_reloc_type_br): Remove negative rel->r_symndx handler. (xcoff64_ppc_relocate_section): Addd BFD_ASSERT to prevent negative rel->r_symndx. --- bfd/coff-rs6000.c | 117 ++++++++++++++++++++------------------------ bfd/coff64-rs6000.c | 107 +++++++++++++++++++--------------------- 2 files changed, 103 insertions(+), 121 deletions(-) diff --git a/bfd/coff-rs6000.c b/bfd/coff-rs6000.c index fbc1aed311..194846794a 100644 --- a/bfd/coff-rs6000.c +++ b/bfd/coff-rs6000.c @@ -2863,9 +2863,6 @@ xcoff_reloc_type_toc (bfd *input_bfd, { struct xcoff_link_hash_entry *h; - if (0 > rel->r_symndx) - return FALSE; - h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx]; if (h != NULL && h->smclas != XMC_TD) @@ -2925,9 +2922,6 @@ xcoff_reloc_type_br (bfd *input_bfd, struct xcoff_link_hash_entry *h; bfd_vma section_offset; - if (0 > rel->r_symndx) - return FALSE; - h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx]; section_offset = rel->r_vaddr - input_section->vma; @@ -3362,6 +3356,7 @@ xcoff_ppc_relocate_section (bfd *output_bfd, bfd_vma value_to_relocate; bfd_vma address; bfd_byte *location; + asection *sec; /* Relocation type R_REF is a special relocation type which is merely used to prevent garbage collection from occurring for @@ -3369,6 +3364,11 @@ xcoff_ppc_relocate_section (bfd *output_bfd, if (rel->r_type == R_REF) continue; + /* Previous version of the code was handling negative r_symndx. + But AFAIK, it can't appear except in .loader relocations which + aren't handled by the function. */ + BFD_ASSERT(0 <= rel->r_symndx); + /* howto */ howto.type = rel->r_type; howto.rightshift = 0; @@ -3392,62 +3392,57 @@ xcoff_ppc_relocate_section (bfd *output_bfd, sym = NULL; symndx = rel->r_symndx; - if (-1 != symndx) - { - asection *sec; - - h = obj_xcoff_sym_hashes (input_bfd)[symndx]; - sym = syms + symndx; - addend = - sym->n_value; + h = obj_xcoff_sym_hashes (input_bfd)[symndx]; + sym = syms + symndx; + addend = - sym->n_value; - if (NULL == h) + if (NULL == h) + { + sec = sections[symndx]; + /* Hack to make sure we use the right TOC anchor value + if this reloc is against the TOC anchor. */ + if (sec->name[3] == '0' + && strcmp (sec->name, ".tc0") == 0) + val = xcoff_data (output_bfd)->toc; + else + val = (sec->output_section->vma + + sec->output_offset + + sym->n_value + - sec->vma); + } + else + { + if (info->unresolved_syms_in_objects != RM_IGNORE + && (h->flags & XCOFF_WAS_UNDEFINED) != 0) + (*info->callbacks->undefined_symbol) + (info, h->root.root.string, + input_bfd, input_section, + rel->r_vaddr - input_section->vma, + info->unresolved_syms_in_objects == RM_DIAGNOSE && + !info->warn_unresolved_syms); + + if (h->root.type == bfd_link_hash_defined + || h->root.type == bfd_link_hash_defweak) { - sec = sections[symndx]; - /* Hack to make sure we use the right TOC anchor value - if this reloc is against the TOC anchor. */ - if (sec->name[3] == '0' - && strcmp (sec->name, ".tc0") == 0) - val = xcoff_data (output_bfd)->toc; - else - val = (sec->output_section->vma - + sec->output_offset - + sym->n_value - - sec->vma); + sec = h->root.u.def.section; + val = (h->root.u.def.value + + sec->output_section->vma + + sec->output_offset); } - else + else if (h->root.type == bfd_link_hash_common) { - if (info->unresolved_syms_in_objects != RM_IGNORE - && (h->flags & XCOFF_WAS_UNDEFINED) != 0) - (*info->callbacks->undefined_symbol) - (info, h->root.root.string, - input_bfd, input_section, - rel->r_vaddr - input_section->vma, - info->unresolved_syms_in_objects == RM_DIAGNOSE && - !info->warn_unresolved_syms); - - if (h->root.type == bfd_link_hash_defined - || h->root.type == bfd_link_hash_defweak) - { - sec = h->root.u.def.section; - val = (h->root.u.def.value - + sec->output_section->vma - + sec->output_offset); - } - else if (h->root.type == bfd_link_hash_common) - { - sec = h->root.u.c.p->section; - val = (sec->output_section->vma - + sec->output_offset); + sec = h->root.u.c.p->section; + val = (sec->output_section->vma + + sec->output_offset); - } - else - { - BFD_ASSERT (bfd_link_relocatable (info) - || (info->static_link - && (h->flags & XCOFF_WAS_UNDEFINED) != 0) - || (h->flags & XCOFF_DEF_DYNAMIC) != 0 - || (h->flags & XCOFF_IMPORT) != 0); - } + } + else + { + BFD_ASSERT (bfd_link_relocatable (info) + || (info->static_link + && (h->flags & XCOFF_WAS_UNDEFINED) != 0) + || (h->flags & XCOFF_DEF_DYNAMIC) != 0 + || (h->flags & XCOFF_IMPORT) != 0); } } @@ -3484,14 +3479,8 @@ xcoff_ppc_relocate_section (bfd *output_bfd, char buf[SYMNMLEN + 1]; char reloc_type_name[10]; - if (symndx == -1) - { - name = "*ABS*"; - } - else if (h != NULL) - { + if (h != NULL) name = NULL; - } else { name = _bfd_coff_internal_syment_name (input_bfd, sym, buf); diff --git a/bfd/coff64-rs6000.c b/bfd/coff64-rs6000.c index 962f4bdbdb..bc2810b126 100644 --- a/bfd/coff64-rs6000.c +++ b/bfd/coff64-rs6000.c @@ -725,9 +725,6 @@ xcoff64_reloc_type_br (bfd *input_bfd, struct xcoff_link_hash_entry *h; bfd_vma section_offset; - if (0 > rel->r_symndx) - return FALSE; - h = obj_xcoff_sym_hashes (input_bfd)[rel->r_symndx]; section_offset = rel->r_vaddr - input_section->vma; @@ -845,12 +842,19 @@ xcoff64_ppc_relocate_section (bfd *output_bfd, bfd_vma address; bfd_byte *location; + asection *sec; + /* Relocation type R_REF is a special relocation type which is merely used to prevent garbage collection from occurring for the csect including the symbol which it references. */ if (rel->r_type == R_REF) continue; + /* Previous version of the code was handling negative r_symndx. + But AFAIK, it can't appear except in .loader relocations which + aren't handled by the function. */ + BFD_ASSERT(0 <= rel->r_symndx); + /* howto */ howto.type = rel->r_type; howto.rightshift = 0; @@ -874,58 +878,53 @@ xcoff64_ppc_relocate_section (bfd *output_bfd, sym = NULL; symndx = rel->r_symndx; - if (-1 != symndx) - { - asection *sec; + h = obj_xcoff_sym_hashes (input_bfd)[symndx]; + sym = syms + symndx; + addend = - sym->n_value; - h = obj_xcoff_sym_hashes (input_bfd)[symndx]; - sym = syms + symndx; - addend = - sym->n_value; - - if (NULL == h) + if (NULL == h) + { + sec = sections[symndx]; + /* Hack to make sure we use the right TOC anchor value + if this reloc is against the TOC anchor. */ + if (sec->name[3] == '0' + && strcmp (sec->name, ".tc0") == 0) + val = xcoff_data (output_bfd)->toc; + else + val = (sec->output_section->vma + + sec->output_offset + + sym->n_value + - sec->vma); + } + else + { + if (info->unresolved_syms_in_objects != RM_IGNORE + && (h->flags & XCOFF_WAS_UNDEFINED) != 0) + info->callbacks->undefined_symbol + (info, h->root.root.string, input_bfd, input_section, + rel->r_vaddr - input_section->vma, + info->unresolved_syms_in_objects == RM_DIAGNOSE + && !info->warn_unresolved_syms); + + if (h->root.type == bfd_link_hash_defined + || h->root.type == bfd_link_hash_defweak) + { + sec = h->root.u.def.section; + val = (h->root.u.def.value + + sec->output_section->vma + + sec->output_offset); + } + else if (h->root.type == bfd_link_hash_common) { - sec = sections[symndx]; - /* Hack to make sure we use the right TOC anchor value - if this reloc is against the TOC anchor. */ - if (sec->name[3] == '0' - && strcmp (sec->name, ".tc0") == 0) - val = xcoff_data (output_bfd)->toc; - else - val = (sec->output_section->vma - + sec->output_offset - + sym->n_value - - sec->vma); + sec = h->root.u.c.p->section; + val = (sec->output_section->vma + + sec->output_offset); } else { - if (info->unresolved_syms_in_objects != RM_IGNORE - && (h->flags & XCOFF_WAS_UNDEFINED) != 0) - info->callbacks->undefined_symbol - (info, h->root.root.string, input_bfd, input_section, - rel->r_vaddr - input_section->vma, - info->unresolved_syms_in_objects == RM_DIAGNOSE - && !info->warn_unresolved_syms); - - if (h->root.type == bfd_link_hash_defined - || h->root.type == bfd_link_hash_defweak) - { - sec = h->root.u.def.section; - val = (h->root.u.def.value - + sec->output_section->vma - + sec->output_offset); - } - else if (h->root.type == bfd_link_hash_common) - { - sec = h->root.u.c.p->section; - val = (sec->output_section->vma - + sec->output_offset); - } - else - { - BFD_ASSERT (bfd_link_relocatable (info) - || (h->flags & XCOFF_DEF_DYNAMIC) != 0 - || (h->flags & XCOFF_IMPORT) != 0); - } + BFD_ASSERT (bfd_link_relocatable (info) + || (h->flags & XCOFF_DEF_DYNAMIC) != 0 + || (h->flags & XCOFF_IMPORT) != 0); } } @@ -964,14 +963,8 @@ xcoff64_ppc_relocate_section (bfd *output_bfd, char buf[SYMNMLEN + 1]; char reloc_type_name[10]; - if (symndx == -1) - { - name = "*ABS*"; - } - else if (h != NULL) - { + if (h != NULL) name = NULL; - } else { name = _bfd_coff_internal_syment_name (input_bfd, sym, buf); -- 2.25.1