X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=bfd%2Felf32-nds32.c;h=01ea277426fb345bb5aad237edc899ab86369bfd;hb=d49e5065ed43ec88627fd8cc6ab9e45fcc0e538a;hp=27ffea2e69284b12c63725cdb6ac77dcb8ab5d4e;hpb=cc364be6ff45669150077387f22723de33d5f1b1;p=deliverable%2Fbinutils-gdb.git diff --git a/bfd/elf32-nds32.c b/bfd/elf32-nds32.c index 27ffea2e69..01ea277426 100644 --- a/bfd/elf32-nds32.c +++ b/bfd/elf32-nds32.c @@ -1,5 +1,5 @@ /* NDS32-specific support for 32-bit ELF. - Copyright (C) 2012-2019 Free Software Foundation, Inc. + Copyright (C) 2012-2020 Free Software Foundation, Inc. Contributed by Andes Technology Corporation. This file is part of BFD, the Binary File Descriptor library. @@ -32,6 +32,9 @@ #include "opcode/cgen.h" #include "../opcodes/nds32-opc.h" +/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */ +#define OCTETS_PER_BYTE(ABFD, SEC) 1 + /* Relocation HOWTO functions. */ static bfd_reloc_status_type nds32_elf_ignore_reloc (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); @@ -196,9 +199,6 @@ struct elf_nds32_link_hash_entry { struct elf_link_hash_entry root; - /* Track dynamic relocs copied for this symbol. */ - struct elf_dyn_relocs *dyn_relocs; - /* For checking relocation type. */ enum elf_nds32_tls_type tls_type; @@ -2526,7 +2526,9 @@ nds32_insertion_sort (void *base, size_t nmemb, size_t size, { char *ptr = (char *) base; int i, j; - char *tmp = xmalloc (size); + char tmp[sizeof (Elf_Internal_Rela)]; + + BFD_ASSERT (size <= sizeof (tmp)); /* If i is less than j, i is inserted before j. @@ -2550,7 +2552,6 @@ nds32_insertion_sort (void *base, size_t nmemb, size_t size, memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size); memcpy (ptr + j * size, tmp, size); } - free (tmp); } /* Sort relocation by r_offset. @@ -3665,7 +3666,6 @@ nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry, struct elf_nds32_link_hash_entry *eh; eh = (struct elf_nds32_link_hash_entry *) ret; - eh->dyn_relocs = NULL; eh->tls_type = GOT_UNKNOWN; eh->offset_to_gp = 0; } @@ -3680,7 +3680,7 @@ nds32_elf_link_hash_table_create (bfd *abfd) { struct elf_nds32_link_hash_table *ret; - bfd_size_type amt = sizeof (struct elf_nds32_link_hash_table); + size_t amt = sizeof (struct elf_nds32_link_hash_table); ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt); if (ret == NULL) @@ -3723,11 +3723,11 @@ create_got_section (bfd *dynobj, struct bfd_link_info *info) /* _bfd_elf_create_got_section will create it for us. */ ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); if (ehtab->srelgot == NULL - || !bfd_set_section_flags (dynobj, ehtab->srelgot, + || !bfd_set_section_flags (ehtab->srelgot, (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED | SEC_READONLY)) - || !bfd_set_section_alignment (dynobj, ehtab->srelgot, 2)) + || !bfd_set_section_alignment (ehtab->srelgot, 2)) return FALSE; return TRUE; @@ -3769,8 +3769,8 @@ nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) s = bfd_make_section (abfd, ".plt"); ehtab->splt = s; if (s == NULL - || !bfd_set_section_flags (abfd, s, pltflags) - || !bfd_set_section_alignment (abfd, s, bed->plt_alignment)) + || !bfd_set_section_flags (s, pltflags) + || !bfd_set_section_alignment (s, bed->plt_alignment)) return FALSE; if (bed->want_plt_sym) @@ -3798,8 +3798,8 @@ nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) bed->default_use_rela_p ? ".rela.plt" : ".rel.plt"); ehtab->srelplt = s; if (s == NULL - || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY) - || !bfd_set_section_alignment (abfd, s, ptralign)) + || !bfd_set_section_flags (s, flags | SEC_READONLY) + || !bfd_set_section_alignment (s, ptralign)) return FALSE; if (ehtab->sgot == NULL && !create_got_section (abfd, info)) @@ -3807,11 +3807,11 @@ nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) for (sec = abfd->sections; sec; sec = sec->next) { - secflags = bfd_get_section_flags (abfd, sec); + secflags = bfd_section_flags (sec); if ((secflags & (SEC_DATA | SEC_LINKER_CREATED)) || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS)) continue; - secname = bfd_get_section_name (abfd, sec); + secname = bfd_section_name (sec); relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6); strcpy (relname, ".rela"); strcat (relname, secname); @@ -3819,8 +3819,8 @@ nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) continue; s = bfd_make_section (abfd, relname); if (s == NULL - || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY) - || !bfd_set_section_alignment (abfd, s, ptralign)) + || !bfd_set_section_flags (s, flags | SEC_READONLY) + || !bfd_set_section_alignment (s, ptralign)) return FALSE; } @@ -3835,7 +3835,7 @@ nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) s = bfd_make_section (abfd, ".dynbss"); htab->sdynbss = s; if (s == NULL - || !bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED)) + || !bfd_set_section_flags (s, SEC_ALLOC | SEC_LINKER_CREATED)) return FALSE; /* The .rel[a].bss section holds copy relocs. This section is not normally needed. We need to create it here, though, so that the @@ -3854,8 +3854,8 @@ nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) ? ".rela.bss" : ".rel.bss")); htab->srelbss = s; if (s == NULL - || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY) - || !bfd_set_section_alignment (abfd, s, ptralign)) + || !bfd_set_section_flags (s, flags | SEC_READONLY) + || !bfd_set_section_alignment (s, ptralign)) return FALSE; } } @@ -3874,40 +3874,6 @@ nds32_elf_copy_indirect_symbol (struct bfd_link_info *info, edir = (struct elf_nds32_link_hash_entry *) dir; eind = (struct elf_nds32_link_hash_entry *) ind; - if (eind->dyn_relocs != NULL) - { - if (edir->dyn_relocs != NULL) - { - struct elf_dyn_relocs **pp; - struct elf_dyn_relocs *p; - - if (ind->root.type == bfd_link_hash_indirect) - abort (); - - /* Add reloc counts against the weak sym to the strong sym - list. Merge any entries against the same section. */ - for (pp = &eind->dyn_relocs; (p = *pp) != NULL;) - { - struct elf_dyn_relocs *q; - - for (q = edir->dyn_relocs; q != NULL; q = q->next) - if (q->sec == p->sec) - { - q->pc_count += p->pc_count; - q->count += p->count; - *pp = p->next; - break; - } - if (q == NULL) - pp = &p->next; - } - *pp = edir->dyn_relocs; - } - - edir->dyn_relocs = eind->dyn_relocs; - eind->dyn_relocs = NULL; - } - if (ind->root.type == bfd_link_hash_indirect) { if (dir->got.refcount <= 0) @@ -3920,24 +3886,6 @@ nds32_elf_copy_indirect_symbol (struct bfd_link_info *info, _bfd_elf_link_hash_copy_indirect (info, dir, ind); } - -/* Find dynamic relocs for H that apply to read-only sections. */ - -static asection * -readonly_dynrelocs (struct elf_link_hash_entry *h) -{ - struct elf_dyn_relocs *p; - - for (p = elf32_nds32_hash_entry (h)->dyn_relocs; p != NULL; p = p->next) - { - asection *s = p->sec->output_section; - - if (s != NULL && (s->flags & SEC_READONLY) != 0) - return p->sec; - } - return NULL; -} - /* Adjust a symbol defined by a dynamic object and referenced by a regular object. The current definition is in some section of the dynamic object, but we're not including those sections. We have to @@ -4023,7 +3971,7 @@ nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info, /* If we don't find any dynamic relocs in read-only sections, then we'll be keeping the dynamic relocs and avoiding the copy reloc. */ - if (!readonly_dynrelocs (h)) + if (!_bfd_elf_readonly_dynrelocs (h)) { h->non_got_ref = 0; return TRUE; @@ -4065,9 +4013,9 @@ nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info, /* Apply the required alignment. */ s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two)); - if (power_of_two > bfd_get_section_alignment (dynobj, s)) + if (power_of_two > bfd_section_alignment (s)) { - if (!bfd_set_section_alignment (dynobj, s, power_of_two)) + if (!bfd_set_section_alignment (s, power_of_two)) return FALSE; } @@ -4090,7 +4038,6 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) struct bfd_link_info *info; struct elf_link_hash_table *ehtab; struct elf_nds32_link_hash_table *htab; - struct elf_nds32_link_hash_entry *eh; struct elf_dyn_relocs *p; if (h->root.type == bfd_link_hash_indirect) @@ -4102,16 +4049,12 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) if (h->root.type == bfd_link_hash_warning) h = (struct elf_link_hash_entry *) h->root.u.i.link; - eh = (struct elf_nds32_link_hash_entry *) h; - info = (struct bfd_link_info *) inf; ehtab = elf_hash_table (info); htab = nds32_elf_hash_table (info); if (htab == NULL) return FALSE; - eh = (struct elf_nds32_link_hash_entry *) h; - if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC) && h->plt.refcount > 0 && !(bfd_link_pie (info) && h->def_regular)) @@ -4223,7 +4166,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) else h->got.offset = (bfd_vma)-1; - if (eh->dyn_relocs == NULL) + if (h->dyn_relocs == NULL) return TRUE; /* In the shared -Bsymbolic case, discard space allocated for @@ -4238,7 +4181,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) { struct elf_dyn_relocs **pp; - for (pp = &eh->dyn_relocs; (p = *pp) != NULL;) + for (pp = &h->dyn_relocs; (p = *pp) != NULL;) { p->count -= p->pc_count; p->pc_count = 0; @@ -4275,13 +4218,13 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) goto keep; } - eh->dyn_relocs = NULL; + h->dyn_relocs = NULL; -keep:; + keep:; } /* Finally, allocate space. */ - for (p = eh->dyn_relocs; p != NULL; p = p->next) + for (p = h->dyn_relocs; p != NULL; p = p->next) { asection *sreloc = elf_section_data (p->sec)->sreloc; sreloc->size += p->count * sizeof (Elf32_External_Rela); @@ -4309,33 +4252,6 @@ elf32_nds32_add_dynreloc (bfd *output_bfd, bfd_elf32_swap_reloca_out (output_bfd, rel, loc); } -/* Set DF_TEXTREL if we find any dynamic relocs that apply to - read-only sections. */ - -static bfd_boolean -maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p) -{ - asection *sec; - - if (h->root.type == bfd_link_hash_indirect) - return TRUE; - - sec = readonly_dynrelocs (h); - if (sec != NULL) - { - struct bfd_link_info *info = (struct bfd_link_info *) info_p; - - info->flags |= DF_TEXTREL; - info->callbacks->minfo - (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"), - sec->owner, h->root.root.string, sec); - - /* Not an error, just cut short the traversal. */ - return FALSE; - } - return TRUE; -} - /* Set the sizes of the dynamic sections. */ static bfd_boolean @@ -4525,7 +4441,7 @@ nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED, { got_size += s->size; } - else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0) + else if (strncmp (bfd_section_name (s), ".rela", 5) == 0) { if (s->size != 0 && s != elf_hash_table (info)->srelplt) relocs = TRUE; @@ -4609,7 +4525,8 @@ nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED, /* If any dynamic relocs apply to a read-only section, then we need a DT_TEXTREL entry. */ if ((info->flags & DF_TEXTREL) == 0) - elf_link_hash_traverse (&htab->root, maybe_set_textrel, + elf_link_hash_traverse (&htab->root, + _bfd_elf_maybe_set_textrel, (void *) info); if ((info->flags & DF_TEXTREL) != 0) @@ -4858,7 +4775,7 @@ nds32_elf_output_symbol_hook (struct bfd_link_info *info, if (bfd_is_const_section (input_sec)) source = input_sec->name; else - source = input_sec->owner->filename; + source = bfd_get_filename (input_sec->owner); fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n", h->root.root.string, @@ -5043,7 +4960,7 @@ patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd) if (!rz) { - printf ("%s: %s @ 0x%08x\n", __func__, ibfd->filename, + printf ("%s: %s @ 0x%08x\n", __func__, bfd_get_filename (ibfd), (int) rel->r_offset); BFD_ASSERT(0); /* Unsupported pattern. */ } @@ -5571,8 +5488,7 @@ nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED, return FALSE; BFD_ASSERT (strncmp (name, ".rela", 5) == 0 - && strcmp (bfd_get_section_name (input_bfd, - input_section), + && strcmp (bfd_section_name (input_section), name + 5) == 0); sreloc = bfd_get_section_by_name (dynobj, name); @@ -5824,7 +5740,7 @@ nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED, case R_NDS32_SDA15S0_RELA: case R_NDS32_SDA15S0: align = 0x0; -handle_sda: + handle_sda: BFD_ASSERT (sec != NULL); /* If the symbol is in the abs section, the out_bfd will be null. @@ -6152,7 +6068,7 @@ handle_sda: break; } -check_reloc: + check_reloc: if (r != bfd_reloc_ok) { @@ -6166,7 +6082,7 @@ check_reloc: name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, sym->st_name); if (name == NULL || *name == '\0') - name = bfd_section_name (input_bfd, sec); + name = bfd_section_name (sec); } if (errmsg != NULL) @@ -6201,7 +6117,7 @@ check_reloc: errmsg = _("internal error: unknown error"); /* Fall through. */ -common_error: + common_error: (*info->callbacks->warning) (info, errmsg, name, input_bfd, input_section, offset); break; @@ -6485,7 +6401,7 @@ nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info) goto get_vma; case DT_JMPREL: s = ehtab->srelplt->output_section; -get_vma: + get_vma: BFD_ASSERT (s != NULL); dyn.d_un.d_ptr = s->vma; bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); @@ -6814,6 +6730,10 @@ nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info) flagword out_fpu_config; flagword in_fpu_config; + /* FIXME: What should be checked when linking shared libraries? */ + if ((ibfd->flags & DYNAMIC) != 0) + return TRUE; + /* TODO: Revise to use object-attributes instead. */ if (!nds32_check_vec_size (ibfd)) return FALSE; @@ -7390,7 +7310,7 @@ nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, return FALSE; BFD_ASSERT (strncmp (name, ".rela", 5) == 0 - && strcmp (bfd_get_section_name (abfd, sec), + && strcmp (bfd_section_name (sec), name + 5) == 0); sreloc = bfd_get_section_by_name (dynobj, name); @@ -7404,8 +7324,8 @@ nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, if ((sec->flags & SEC_ALLOC) != 0) flags |= SEC_ALLOC | SEC_LOAD; if (sreloc == NULL - || !bfd_set_section_flags (dynobj, sreloc, flags) - || !bfd_set_section_alignment (dynobj, sreloc, 2)) + || !bfd_set_section_flags (sreloc, flags) + || !bfd_set_section_alignment (sreloc, 2)) return FALSE; elf_section_type (sreloc) = SHT_RELA; @@ -7416,7 +7336,7 @@ nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, /* If this is a global symbol, we count the number of relocations we need for this symbol. */ if (h != NULL) - head = &((struct elf_nds32_link_hash_entry *) h)->dyn_relocs; + head = &h->dyn_relocs; else { asection *s; @@ -7439,7 +7359,7 @@ nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, p = *head; if (p == NULL || p->sec != sec) { - bfd_size_type amt = sizeof (*p); + size_t amt = sizeof (*p); p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt); if (p == NULL) return FALSE; @@ -8278,7 +8198,7 @@ nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16, goto done; } -done: + done: /* Bit-15 of insn16 should be set for a valid instruction. */ if ((insn16 & 0x8000) == 0) return 0; @@ -8600,7 +8520,7 @@ nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn) goto done; } -done: + done: if (insn & 0x80000000) return 0; @@ -9445,7 +9365,7 @@ nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec, if (p < endp) *p |= 0x80; } -done_adjust_diff: + done_adjust_diff: if (sec == sect) { @@ -12564,21 +12484,19 @@ nds32_elf_relax_section (bfd *abfd, asection *sec, clean_nds32_elf_blank (); } -finish: - if (internal_relocs != NULL - && elf_section_data (sec)->relocs != internal_relocs) + finish: + if (elf_section_data (sec)->relocs != internal_relocs) free (internal_relocs); - if (contents != NULL - && elf_section_data (sec)->this_hdr.contents != contents) + if (elf_section_data (sec)->this_hdr.contents != contents) free (contents); - if (isymbuf != NULL && symtab_hdr->contents != (bfd_byte *) isymbuf) + if (symtab_hdr->contents != (bfd_byte *) isymbuf) free (isymbuf); return result; -error_return: + error_return: result = FALSE; goto finish; } @@ -12590,6 +12508,18 @@ static struct bfd_elf_special_section const nds32_elf_special_sections[] = {NULL, 0, 0, 0, 0} }; +static bfd_boolean +nds32_elf_section_flags (const Elf_Internal_Shdr *hdr) +{ + const char *name = hdr->bfd_section->name; + + if (strncmp (name, ".sbss", 5) == 0 + || strncmp (name, ".sdata", 6) == 0) + hdr->bfd_section->flags |= SEC_SMALL_DATA; + + return TRUE; +} + static bfd_boolean nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED, struct bfd_link_info *info, @@ -13149,7 +13079,7 @@ nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec, goto error_return; } -finish: + finish: if (relax_blank_list) { nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list); @@ -13157,7 +13087,7 @@ finish: } return result; -error_return: + error_return: result = FALSE; goto finish; } @@ -13220,7 +13150,8 @@ nds32_elf_get_relocated_section_contents (bfd *abfd, = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL, "unused", FALSE, 0, 0, FALSE); - off = (*parent)->address * bfd_octets_per_byte (input_bfd); + off = (*parent)->address * OCTETS_PER_BYTE (input_bfd, + input_section); _bfd_clear_contents ((*parent)->howto, input_bfd, input_section, data, off); (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; @@ -13287,7 +13218,7 @@ nds32_elf_get_relocated_section_contents (bfd *abfd, free (reloc_vector); return data; -error_return: + error_return: free (reloc_vector); return NULL; } @@ -13453,7 +13384,7 @@ elf32_nds32_check_relax_group (bfd *abfd, asection *asec) } while (FALSE); - if ((relocs != NULL) && (elf_section_data (asec)->relocs != relocs)) + if (elf_section_data (asec)->relocs != relocs) free (relocs); if ((min_id != relax_group_ptr->min_id) @@ -13592,7 +13523,7 @@ elf32_nds32_unify_relax_group (bfd *abfd, asection *asec) } while (FALSE); - if (relocs != NULL && elf_section_data (asec)->relocs != relocs) + if (elf_section_data (asec)->relocs != relocs) free (relocs); return result; @@ -14014,19 +13945,17 @@ nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents, #endif } -finish: + finish: if (incontents) contents = NULL; - if (internal_relocs != NULL - && elf_section_data (insec)->relocs != internal_relocs) + if (elf_section_data (insec)->relocs != internal_relocs) free (internal_relocs); - if (contents != NULL - && elf_section_data (insec)->this_hdr.contents != contents) + if (elf_section_data (insec)->this_hdr.contents != contents) free (contents); - if (local_syms != NULL && symtab_hdr->contents != (bfd_byte *) local_syms) + if (symtab_hdr->contents != (bfd_byte *) local_syms) free (local_syms); if (chain.next) @@ -14043,7 +13972,7 @@ finish: return result; -error_return: + error_return: result = FALSE; goto finish; } @@ -14089,6 +14018,7 @@ error_return: #define elf_backend_object_p nds32_elf_object_p #define elf_backend_final_write_processing nds32_elf_final_write_processing #define elf_backend_special_sections nds32_elf_special_sections +#define elf_backend_section_flags nds32_elf_section_flags #define bfd_elf32_bfd_get_relocated_section_contents \ nds32_elf_get_relocated_section_contents #define bfd_elf32_bfd_is_target_special_symbol nds32_elf_is_target_special_symbol