aboutsummaryrefslogtreecommitdiff
path: root/ld
diff options
context:
space:
mode:
authorH.J. Lu <hjl.tools@gmail.com>2020-09-12 05:37:30 -0700
committerH.J. Lu <hjl.tools@gmail.com>2020-09-12 05:37:43 -0700
commit496afd17055aeb7d8f45e01715c475664f2b73bd (patch)
tree221e0db337f288cbced1e5e4189463c23733a1a1 /ld
parente71774ed24cc6890285b174cb95caf79a7622466 (diff)
downloadgdb-496afd17055aeb7d8f45e01715c475664f2b73bd.zip
gdb-496afd17055aeb7d8f45e01715c475664f2b73bd.tar.gz
gdb-496afd17055aeb7d8f45e01715c475664f2b73bd.tar.bz2
elf: Add -z unique-symbol to avoid duplicated local symbol names
The symbol string table in the .symtab section is optional and cosmetic. The contents of the .symtab section have no impact on run-time execution. The symbol names in the symbol string table help distinguish addresses at different locations. Add a linker option, -z unique-symbol, to avoid duplicated local symbol names in the symbol string table. This feature was well received by the livepatch maintainers. It not only solves the duplicated local symbol name problem, but also would allow livepatch to more precisely locate duplicate symbols in general for patching. bfd/ PR ld/26391 * elflink.c (elf_final_link_info): Add local_hash_table. (local_hash_entry): New. (local_hash_newfunc): Likewise. (elf_link_output_symstrtab): Append ".COUNT" to duplicated local symbols. (bfd_elf_final_link): Initialize and free local_hash_table for "-z unique-symbol". include/ PR ld/26391 * bfdlink.h (bfd_link_info): Add unique_symbol. ld/ PR ld/26391 * NEWS: Mention "-z unique-symbol". * emultempl/elf.em (gld${EMULATION_NAME}_handle_option): Handle "-z unique-symbol" and "-z nounique-symbol". * ld.texi: Document "-z unique-symbol" and "-z nounique-symbol". * lexsup.c (elf_static_list_options): Add "-z unique-symbol" and "-z nounique-symbol". * testsuite/ld-elf/elf.exp: Add PR ld/26391 tests. * testsuite/ld-elf/pr26391.nd: New file. * testsuite/ld-elf/pr26391.out: Likewise. * testsuite/ld-elf/pr26391a.c: Likewise. * testsuite/ld-elf/pr26391b.c: Likewise. * testsuite/ld-elf/pr26391c.c: Likewise. * testsuite/ld-elf/pr26391d.c: Likewise.
Diffstat (limited to 'ld')
-rw-r--r--ld/ChangeLog17
-rw-r--r--ld/NEWS2
-rw-r--r--ld/emultempl/elf.em4
-rw-r--r--ld/ld.texi6
-rw-r--r--ld/lexsup.c4
-rw-r--r--ld/testsuite/ld-elf/elf.exp121
-rw-r--r--ld/testsuite/ld-elf/pr26391.nd7
-rw-r--r--ld/testsuite/ld-elf/pr26391.out3
-rw-r--r--ld/testsuite/ld-elf/pr26391a.c18
-rw-r--r--ld/testsuite/ld-elf/pr26391b.c13
-rw-r--r--ld/testsuite/ld-elf/pr26391c.c13
-rw-r--r--ld/testsuite/ld-elf/pr26391d.c13
12 files changed, 221 insertions, 0 deletions
diff --git a/ld/ChangeLog b/ld/ChangeLog
index d81ebcc..8d63f66 100644
--- a/ld/ChangeLog
+++ b/ld/ChangeLog
@@ -1,3 +1,20 @@
+2020-09-12 H.J. Lu <hongjiu.lu@intel.com>
+
+ PR ld/26391
+ * NEWS: Mention "-z unique-symbol".
+ * emultempl/elf.em (gld${EMULATION_NAME}_handle_option): Handle
+ "-z unique-symbol" and "-z nounique-symbol".
+ * ld.texi: Document "-z unique-symbol" and "-z nounique-symbol".
+ * lexsup.c (elf_static_list_options): Add "-z unique-symbol" and
+ "-z nounique-symbol".
+ * testsuite/ld-elf/elf.exp: Add PR ld/26391 tests.
+ * testsuite/ld-elf/pr26391.nd: New file.
+ * testsuite/ld-elf/pr26391.out: Likewise.
+ * testsuite/ld-elf/pr26391a.c: Likewise.
+ * testsuite/ld-elf/pr26391b.c: Likewise.
+ * testsuite/ld-elf/pr26391c.c: Likewise.
+ * testsuite/ld-elf/pr26391d.c: Likewise.
+
2020-09-11 Jeremy Drake <sourceware-bugzilla@jdrake.com>
PR 26588
diff --git a/ld/NEWS b/ld/NEWS
index 6953481..e4ae43b 100644
--- a/ld/NEWS
+++ b/ld/NEWS
@@ -1,5 +1,7 @@
-*- text -*-
+* Add -z unique-symbol to avoid duplicated local symbol names.
+
* The creation of PE format DLLs now defaults to using a more secure set of DLL
characteristics.
diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em
index 356f345..59eed70 100644
--- a/ld/emultempl/elf.em
+++ b/ld/emultempl/elf.em
@@ -751,6 +751,10 @@ fragment <<EOF
link_info.noexecstack = TRUE;
link_info.execstack = FALSE;
}
+ else if (strcmp (optarg, "unique-symbol") == 0)
+ link_info.unique_symbol = TRUE;
+ else if (strcmp (optarg, "nounique-symbol") == 0)
+ link_info.unique_symbol = FALSE;
else if (strcmp (optarg, "globalaudit") == 0)
{
link_info.flags_1 |= DF_1_GLOBAUDIT;
diff --git a/ld/ld.texi b/ld/ld.texi
index 7d961c3..ee592df 100644
--- a/ld/ld.texi
+++ b/ld/ld.texi
@@ -1378,6 +1378,12 @@ specifies a memory segment that should contain only instructions and must
be in wholly disjoint pages from any other data. Don't create separate
code @code{PT_LOAD} segment if @samp{noseparate-code} is used.
+@item unique-symbol
+@itemx nounique-symbol
+Avoid duplicated local symbol names in the symbol string table. Append
+".@code{number}" to duplicated local symbol names if @samp{unique-symbol}
+is used. @option{nounique-symbol} is the default.
+
@item shstk
Generate GNU_PROPERTY_X86_FEATURE_1_SHSTK in .note.gnu.property section
to indicate compatibility with Intel Shadow Stack. Supported for
diff --git a/ld/lexsup.c b/ld/lexsup.c
index b9cc8a1..b8f0667 100644
--- a/ld/lexsup.c
+++ b/ld/lexsup.c
@@ -2050,6 +2050,10 @@ elf_static_list_options (FILE *file)
fprintf (file, _("\
-z noexecstack Mark executable as not requiring executable stack\n"));
fprintf (file, _("\
+ -z unique-symbol Avoid duplicated local symbol names\n"));
+ fprintf (file, _("\
+ -z nounique-symbol Keep duplicated local symbol names (default)\n"));
+ fprintf (file, _("\
-z globalaudit Mark executable requiring global auditing\n"));
}
diff --git a/ld/testsuite/ld-elf/elf.exp b/ld/testsuite/ld-elf/elf.exp
index c0d67d8..f2ff039 100644
--- a/ld/testsuite/ld-elf/elf.exp
+++ b/ld/testsuite/ld-elf/elf.exp
@@ -359,4 +359,125 @@ if { [istarget *-*-linux*]
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
diff --git a/ld/testsuite/ld-elf/pr26391.nd b/ld/testsuite/ld-elf/pr26391.nd
new file mode 100644
index 0000000..8dd48d9
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr26391.nd
@@ -0,0 +1,7 @@
+#...
+[0-9a-z]+ t _?bar
+#...
+[0-9a-z]+ t _?bar.1
+#...
+[0-9a-z]+ t _?bar.2
+#pass
diff --git a/ld/testsuite/ld-elf/pr26391.out b/ld/testsuite/ld-elf/pr26391.out
new file mode 100644
index 0000000..73654ee
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr26391.out
@@ -0,0 +1,3 @@
+bar 1
+bar 2
+bar 3
diff --git a/ld/testsuite/ld-elf/pr26391a.c b/ld/testsuite/ld-elf/pr26391a.c
new file mode 100644
index 0000000..7356d9d
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr26391a.c
@@ -0,0 +1,18 @@
+typedef void (*func_p) (void);
+
+extern func_p bar1_p (void);
+extern func_p bar2_p (void);
+extern func_p bar3_p (void);
+
+int
+main ()
+{
+ func_p f;
+ f = bar1_p ();
+ f ();
+ f = bar2_p ();
+ f ();
+ f = bar3_p ();
+ f ();
+ return 0;
+}
diff --git a/ld/testsuite/ld-elf/pr26391b.c b/ld/testsuite/ld-elf/pr26391b.c
new file mode 100644
index 0000000..8f716a5
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr26391b.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+static void
+bar (void)
+{
+ printf ("bar 1\n");
+}
+
+void *
+bar1_p (void)
+{
+ return bar;
+}
diff --git a/ld/testsuite/ld-elf/pr26391c.c b/ld/testsuite/ld-elf/pr26391c.c
new file mode 100644
index 0000000..e5bf1c1
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr26391c.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+static void
+bar (void)
+{
+ printf ("bar 2\n");
+}
+
+void *
+bar2_p (void)
+{
+ return bar;
+}
diff --git a/ld/testsuite/ld-elf/pr26391d.c b/ld/testsuite/ld-elf/pr26391d.c
new file mode 100644
index 0000000..6e388f8
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr26391d.c
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+static void
+bar (void)
+{
+ printf ("bar 3\n");
+}
+
+void *
+bar3_p (void)
+{
+ return bar;
+}