From 3f2e9699234ca31d083bc93ea6e03903f10baeaf Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Sat, 22 Aug 2020 08:31:53 -0700 Subject: [PATCH] elf: Keep only one '@' for undefined versioned symbols The symbol string table in the .symtab section is optional and cosmetic. Keep only one '@' for undefined versioned symbols, which are defined in shared objects, in the symbol string table. Update "nm -D" to display only one '@' for undefined versioned symbols. bfd/ PR ld/26382 * elflink.c (elf_link_output_symstrtab): Keep only one '@' for versioned symbols, which are defined in shared objects, in symbol string table. binutils/ PR ld/26382 * nm.c (print_symname): Display only one '@' for undefined versioned symbols. * doc/binutils.texi: Update nm version information. ld/ PR ld/26382 * testsuite/ld-elf/pr26302.nd: Updated. * testsuite/ld-elf/pr26302.rd: New file. * testsuite/ld-elf/shared.exp: Add a test for readelf -sW. --- bfd/ChangeLog | 7 +++++++ bfd/elflink.c | 22 +++++++++++++++++++++- binutils/ChangeLog | 7 +++++++ binutils/doc/binutils.texi | 8 +++++++- binutils/nm.c | 5 ++++- ld/ChangeLog | 7 +++++++ ld/testsuite/ld-elf/pr26302.nd | 2 +- ld/testsuite/ld-elf/pr26302.rd | 12 ++++++++++++ ld/testsuite/ld-elf/shared.exp | 3 ++- 9 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 ld/testsuite/ld-elf/pr26302.rd diff --git a/bfd/ChangeLog b/bfd/ChangeLog index d7853371c6a..b3c1c6a70ef 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,10 @@ +2020-08-22 H.J. Lu + + PR ld/26382 + * elflink.c (elf_link_output_symstrtab): Keep only one '@' for + versioned symbols, which are defined in shared objects, in + symbol string table. + 2020-08-21 Nick Clifton * elfnn-aarch64.c (_bfd_aarch64_erratum_835769_scan): Only sort diff --git a/bfd/elflink.c b/bfd/elflink.c index 0a7f5bb1528..17a423270ec 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -9642,9 +9642,29 @@ elf_link_output_symstrtab (struct elf_final_link_info *flinfo, { /* 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) + return 0; + base_len = base_end - name; + memcpy (versioned_name, name, base_len); + memcpy (versioned_name + base_len, version, + len - base_len); + } + } elfsym->st_name = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab, - name, FALSE); + versioned_name, FALSE); if (elfsym->st_name == (unsigned long) -1) return 0; } diff --git a/binutils/ChangeLog b/binutils/ChangeLog index 1f5bd36e7a9..ef4b8cb3dc0 100644 --- a/binutils/ChangeLog +++ b/binutils/ChangeLog @@ -1,3 +1,10 @@ +2020-08-22 H.J. Lu + + PR ld/26382 + * nm.c (print_symname): Display only one '@' for undefined + versioned symbols. + * doc/binutils.texi: Update nm version information. + 2020-08-21 Mark Wielaard * testsuite/binutils-all/readelf.exp (readelf_wi_test): Also diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi index 968da2cb28e..35a5a67041e 100644 --- a/binutils/doc/binutils.texi +++ b/binutils/doc/binutils.texi @@ -924,7 +924,13 @@ The symbol type is unknown, or object file format specific. @end table @item -The symbol name. +The symbol name. If a symbol has version information associated with it, +then the version information is displayed as well. If the versioned +symbol is undefined or hidden from linker, the version string is displayed +as a suffix to the symbol name, preceded by an @@ character. For example +@samp{foo@@VER_1}. If the version is the default version to be used when +resolving unversioned references to the symbol, then it is displayed as a +suffix preceded by two @@ characters. For example @samp{foo@@@@VER_2}. @end itemize @c man end diff --git a/binutils/nm.c b/binutils/nm.c index 69e697ae925..3501f48d29e 100644 --- a/binutils/nm.c +++ b/binutils/nm.c @@ -421,7 +421,10 @@ print_symname (const char *form, struct extended_symbol_info *info, = bfd_get_symbol_version_string (abfd, &info->elfinfo->symbol, FALSE, &hidden); if (version_string && version_string[0]) - printf ("%s%s", hidden ? "@" : "@@", version_string); + printf ("%s%s", + (hidden || bfd_is_und_section (info->elfinfo->symbol.section) + ? "@" : "@@"), + version_string); } } diff --git a/ld/ChangeLog b/ld/ChangeLog index 0e3a8593c35..4f57be86ded 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,10 @@ +2020-08-22 H.J. Lu + + PR ld/26382 + * testsuite/ld-elf/pr26302.nd: Updated. + * testsuite/ld-elf/pr26302.rd: New file. + * testsuite/ld-elf/shared.exp: Add a test for readelf -sW. + 2020-08-20 Nick Clifton PR 26428 diff --git a/ld/testsuite/ld-elf/pr26302.nd b/ld/testsuite/ld-elf/pr26302.nd index 1f2fbdf9a3f..bc9a675789a 100644 --- a/ld/testsuite/ld-elf/pr26302.nd +++ b/ld/testsuite/ld-elf/pr26302.nd @@ -1,3 +1,3 @@ #... - +U foo@@FOO + +U foo@FOO #pass diff --git a/ld/testsuite/ld-elf/pr26302.rd b/ld/testsuite/ld-elf/pr26302.rd new file mode 100644 index 00000000000..8f7b8a96e75 --- /dev/null +++ b/ld/testsuite/ld-elf/pr26302.rd @@ -0,0 +1,12 @@ +#... +Symbol table '\.dynsym' contains [0-9]+ entries: + +Num: +Value +Size Type +Bind +Vis +Ndx Name +#... + +[0-9]+: +[a-f0-9]+ +0 +FUNC +GLOBAL +DEFAULT +UND +foo@FOO \([0-9]+\) +#... + +Symbol table '\.symtab' contains [0-9]+ entries: + +Num: +Value +Size Type +Bind +Vis +Ndx Name +#... + +[0-9]+: +[a-f0-9]+ +0 +FUNC +GLOBAL +DEFAULT +UND +foo@FOO +#pass diff --git a/ld/testsuite/ld-elf/shared.exp b/ld/testsuite/ld-elf/shared.exp index 014937175fc..4de5b341834 100644 --- a/ld/testsuite/ld-elf/shared.exp +++ b/ld/testsuite/ld-elf/shared.exp @@ -856,7 +856,8 @@ run_cc_link_tests [list \ "-shared -Wl,--no-as-needed tmpdir/pr26302a.so" \ "-fPIC" \ {pr26302b.c} \ - {{nm {-u} pr26302.nd} \ + {{readelf {-sW} pr26302.rd} \ + {nm {-u} pr26302.nd} \ {nm {-u -D} pr26302.nd} \ {nm {-u -D --with-symbol-versions} pr26302.nd}} \ "pr26302b.so" \ -- 2.30.2