Elf_External_Sym_Shndx *symshndxbuf;
/* Number of STT_FILE syms seen. */
size_t filesym_count;
+ /* Local symbol hash table. */
+ struct bfd_hash_table local_hash_table;
};
+struct local_hash_entry
+{
+ /* Base hash table entry structure. */
+ struct bfd_hash_entry root;
+ /* Size of the local symbol name. */
+ size_t size;
+ /* Number of the duplicated local symbol names. */
+ long count;
+};
+
+/* Create an entry in the local symbol hash table. */
+
+static struct bfd_hash_entry *
+local_hash_newfunc (struct bfd_hash_entry *entry,
+ struct bfd_hash_table *table,
+ const char *string)
+{
+
+ /* Allocate the structure if it has not already been allocated by a
+ subclass. */
+ if (entry == NULL)
+ {
+ entry = bfd_hash_allocate (table,
+ sizeof (struct local_hash_entry));
+ if (entry == NULL)
+ return entry;
+ }
+
+ /* Call the allocation method of the superclass. */
+ entry = bfd_hash_newfunc (entry, table, string);
+ if (entry != NULL)
+ {
+ ((struct local_hash_entry *) entry)->count = 0;
+ ((struct local_hash_entry *) entry)->size = 0;
+ }
+
+ return entry;
+}
+
/* This struct is used to pass information to elf_link_output_extsym. */
struct elf_outext_info
/* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize
to get the final offset for st_name. */
char *versioned_name = (char *) name;
- if (h != NULL && h->versioned == versioned && h->def_dynamic)
- {
- /* Keep only one '@' for versioned symbols defined in shared
- objects. */
- char *version = strrchr (name, ELF_VER_CHR);
- char *base_end = strchr (name, ELF_VER_CHR);
- if (version != base_end)
- {
- size_t base_len;
- size_t len = strlen (name);
- versioned_name = bfd_alloc (flinfo->output_bfd, len);
- if (versioned_name == NULL)
+ if (h != NULL)
+ {
+ if (h->versioned == versioned && h->def_dynamic)
+ {
+ /* Keep only one '@' for versioned symbols defined in
+ shared objects. */
+ char *version = strrchr (name, ELF_VER_CHR);
+ char *base_end = strchr (name, ELF_VER_CHR);
+ if (version != base_end)
+ {
+ size_t base_len;
+ size_t len = strlen (name);
+ versioned_name = bfd_alloc (flinfo->output_bfd, len);
+ if (versioned_name == NULL)
+ return 0;
+ base_len = base_end - name;
+ memcpy (versioned_name, name, base_len);
+ memcpy (versioned_name + base_len, version,
+ len - base_len);
+ }
+ }
+ }
+ else if (flinfo->info->unique_symbol
+ && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL)
+ {
+ struct local_hash_entry *lh;
+ switch (ELF_ST_TYPE (elfsym->st_info))
+ {
+ case STT_FILE:
+ case STT_SECTION:
+ break;
+ default:
+ lh = (struct local_hash_entry *) bfd_hash_lookup
+ (&flinfo->local_hash_table, name, TRUE, FALSE);
+ if (lh == NULL)
return 0;
- base_len = base_end - name;
- memcpy (versioned_name, name, base_len);
- memcpy (versioned_name + base_len, version,
- len - base_len);
+ if (lh->count)
+ {
+ /* Append ".COUNT" to duplicated local symbols. */
+ size_t count_len;
+ size_t base_len = lh->size;
+ char buf[30];
+ sprintf (buf, "%lx", lh->count);
+ if (!base_len)
+ {
+ base_len = strlen (name);
+ lh->size = base_len;
+ }
+ count_len = strlen (buf);
+ versioned_name = bfd_alloc (flinfo->output_bfd,
+ base_len + count_len + 2);
+ if (versioned_name == NULL)
+ return 0;
+ memcpy (versioned_name, name, base_len);
+ versioned_name[base_len] = '.';
+ memcpy (versioned_name + base_len + 1, buf,
+ count_len + 1);
+ }
+ lh->count++;
+ break;
}
}
elfsym->st_name
const char *std_attrs_section;
struct elf_link_hash_table *htab = elf_hash_table (info);
bfd_boolean sections_removed;
+ bfd_boolean ret;
if (!is_elf_hash_table (htab))
return FALSE;
emit_relocs = (bfd_link_relocatable (info)
|| info->emitrelocations);
+ memset (&flinfo, 0, sizeof (flinfo));
flinfo.info = info;
flinfo.output_bfd = abfd;
flinfo.symstrtab = _bfd_elf_strtab_init ();
/* Note that it is OK if symver_sec is NULL. */
}
- flinfo.contents = NULL;
- flinfo.external_relocs = NULL;
- flinfo.internal_relocs = NULL;
- flinfo.external_syms = NULL;
- flinfo.locsym_shndx = NULL;
- flinfo.internal_syms = NULL;
- flinfo.indices = NULL;
- flinfo.sections = NULL;
- flinfo.symshndxbuf = NULL;
- flinfo.filesym_count = 0;
+ if (info->unique_symbol
+ && !bfd_hash_table_init (&flinfo.local_hash_table,
+ local_hash_newfunc,
+ sizeof (struct local_hash_entry)))
+ return FALSE;
/* The object attributes have been merged. Remove the input
sections from the link, and set the contents of the output
}
}
+ ret = TRUE;
+
/* Output any global symbols that got converted to local in a
version script or due to symbol visibility. We do this in a
separate step since ELF requires all local symbols to appear
eoinfo.file_sym_done = FALSE;
bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
if (eoinfo.failed)
- return FALSE;
+ {
+ ret = FALSE;
+ goto return_local_hash_table;
+ }
/* If backend needs to output some local symbols not present in the hash
table, do it now. */
if (! ((*bed->elf_backend_output_arch_local_syms)
(abfd, info, &flinfo,
(out_sym_func) elf_link_output_symstrtab)))
- return FALSE;
+ {
+ ret = FALSE;
+ goto return_local_hash_table;
+ }
}
/* That wrote out all the local symbols. Finish up the symbol table
BFD_ASSERT (indx > 0);
sym.st_shndx = indx;
if (! check_dynsym (abfd, &sym))
- return FALSE;
+ {
+ ret = FALSE;
+ goto return_local_hash_table;
+ }
sym.st_value = s->vma;
dest = dynsym + dynindx * bed->s->sizeof_sym;
bed->s->swap_symbol_out (abfd, &sym, dest, 0);
sym.st_shndx =
elf_section_data (s->output_section)->this_idx;
if (! check_dynsym (abfd, &sym))
- return FALSE;
+ {
+ ret = FALSE;
+ goto return_local_hash_table;
+ }
sym.st_value = (s->output_section->vma
+ s->output_offset
+ e->isym.st_value);
eoinfo.flinfo = &flinfo;
bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
if (eoinfo.failed)
- return FALSE;
+ {
+ ret = FALSE;
+ goto return_local_hash_table;
+ }
/* If backend needs to output some symbols not present in the hash
table, do it now. */
if (! ((*bed->elf_backend_output_arch_syms)
(abfd, info, &flinfo,
(out_sym_func) elf_link_output_symstrtab)))
- return FALSE;
+ {
+ ret = FALSE;
+ goto return_local_hash_table;
+ }
}
/* Finalize the .strtab section. */
/* Swap out the .strtab section. */
if (!elf_link_swap_symbols_out (&flinfo))
- return FALSE;
+ {
+ ret = FALSE;
+ goto return_local_hash_table;
+ }
/* Now we know the size of the symtab section. */
if (bfd_get_symcount (abfd) > 0)
if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0
|| (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt))
- return FALSE;
+ {
+ ret = FALSE;
+ goto return_local_hash_table;
+ }
}
}
if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0
|| ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab))
- return FALSE;
+ {
+ ret = FALSE;
+ goto return_local_hash_table;
+ }
}
if (info->out_implib_bfd && !elf_output_implib (abfd, info))
{
_bfd_error_handler (_("%pB: failed to generate import library"),
info->out_implib_bfd);
- return FALSE;
+ ret = FALSE;
+ goto return_local_hash_table;
}
/* Adjust the relocs to have the correct symbol indices. */
sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o);
if (esdo->rel.hdr != NULL
&& !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info))
- return FALSE;
+ {
+ ret = FALSE;
+ goto return_local_hash_table;
+ }
if (esdo->rela.hdr != NULL
&& !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info))
- return FALSE;
+ {
+ ret = FALSE;
+ goto return_local_hash_table;
+ }
/* Set the reloc_count field to 0 to prevent write_relocs from
trying to swap the relocs out itself. */
{
bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size);
if (contents == NULL)
- return FALSE; /* Bail out and fail. */
+ {
+ /* Bail out and fail. */
+ ret = FALSE;
+ goto return_local_hash_table;
+ }
bfd_elf_set_obj_attr_contents (abfd, contents, attr_size);
bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size);
free (contents);
}
- return TRUE;
+ return_local_hash_table:
+ if (info->unique_symbol)
+ bfd_hash_table_free (&flinfo.local_hash_table);
+ return ret;
error_return:
elf_final_link_free (abfd, &flinfo);
- return FALSE;
+ ret = FALSE;
+ goto return_local_hash_table;
}
\f
/* Initialize COOKIE for input bfd ABFD. */
run_ld_link_exec_tests $array_tests_static $xfails
+run_cc_link_tests [list \
+ [list \
+ "Build pr26391-1" \
+ "-Wl,-z,unique-symbol" \
+ "-fno-function-sections" \
+ {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
+ {{nm "" pr26391.nd}} \
+ "pr26391-1" \
+ ] \
+ [list \
+ "Build pr26391-2" \
+ "-Wl,-z,unique-symbol" \
+ "-ffunction-sections" \
+ {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
+ {{nm "" pr26391.nd}} \
+ "pr26391-2" \
+ ] \
+ [list \
+ "Build pr26391-3" \
+ "-Wl,-z,unique-symbol,--emit-relocs" \
+ "-fno-function-sections" \
+ {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
+ {{nm "" pr26391.nd}} \
+ "pr26391-3" \
+ ] \
+ [list \
+ "Build pr26391-4" \
+ "-Wl,-z,unique-symbol,--emit-relocs" \
+ "-ffunction-sections" \
+ {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
+ {{nm "" pr26391.nd}} \
+ "pr26391-4" \
+ ] \
+]
+
+run_ld_link_tests [list \
+ [list \
+ "Build pr26391-5.o" \
+ "-z unique-symbol -r" \
+ "" \
+ "" \
+ {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
+ {{nm "" pr26391.nd}} \
+ "pr26391-5.o" \
+ "-fno-function-sections" \
+ ] \
+ [list \
+ "Build pr26391-6.o" \
+ "-z unique-symbol -r" \
+ "" \
+ "" \
+ {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
+ {{nm "" pr26391.nd}} \
+ "pr26391-6.o" \
+ "-ffunction-sections" \
+ ] \
+]
+
+run_ld_link_exec_tests [list \
+ [list \
+ "Run pr26391-1" \
+ "-Wl,-z,unique-symbol" \
+ "" \
+ {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
+ "pr26391-1" \
+ "pr26391.out" \
+ "-fno-function-sections" \
+ ] \
+ [list \
+ "Run pr26391-2" \
+ "-Wl,-z,unique-symbol" \
+ "" \
+ {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
+ "pr26391-2" \
+ "pr26391.out" \
+ "-ffunction-sections" \
+ ] \
+ [list \
+ "Run pr26391-3" \
+ "-Wl,-z,unique-symbol,--emit-relocs" \
+ "" \
+ {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
+ "pr26391-3" \
+ "pr26391.out" \
+ "-fno-function-sections" \
+ ] \
+ [list \
+ "Run pr26391-4" \
+ "-Wl,-z,unique-symbol,--emit-relocs" \
+ "" \
+ {pr26391a.c pr26391b.c pr26391c.c pr26391d.c} \
+ "pr26391-4" \
+ "pr26391.out" \
+ "-ffunction-sections" \
+ ] \
+ [list \
+ "Run pr26391-5" \
+ "-Wl,-z,unique-symbol" \
+ "" \
+ {dummy.c} \
+ "pr26391-5" \
+ "pr26391.out" \
+ "" \
+ "c" \
+ "" \
+ "tmpdir/pr26391-5.o" \
+ ] \
+ [list \
+ "Run pr26391-6" \
+ "-Wl,-z,unique-symbol" \
+ "" \
+ {dummy.c} \
+ "pr26391-6" \
+ "pr26391.out" \
+ "" \
+ "c" \
+ "" \
+ "tmpdir/pr26391-6.o" \
+ ] \
+]
+
catch "exec rm -f tmpdir/preinit tmpdir/init tmpdir/fini tmpdir/init-mixed" status