[binutils-gdb] elf: Hide DT_INIT and DT_FINI functions

H.J. Lu hjl@sourceware.org
Fri Sep 11 01:00:22 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=873b163d69e367705884a96a27699d14bcecb880

commit 873b163d69e367705884a96a27699d14bcecb880
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Sep 11 06:19:36 2026 +0800

    elf: Hide DT_INIT and DT_FINI functions
    
    Hide special functions used by linker to define DT_INIT and DT_FINI in
    executable and shared library so that they won't be in dynamic symbol
    table.
    
    bfd/
    
            PR ld/34623
            * elflink.c (bfd_elf_size_dynamic_sections): Hide functions used
            to define DT_INIT and DT_FINI functions.
    
    ld/
    
            PR ld/34623
            * testsuite/ld-elf/pr34623.s: New file.
            * testsuite/ld-elf/pr34623a.rd: Likewise.
            * testsuite/ld-elf/pr34623b.rd: Likewise.
            * testsuite/ld-elf/shared.exp: Run PR ld/34623 tests.
    
    Signed-off-by: H.J. Lu <hjl.tools@gmail.com>

Diff:
---
 bfd/elflink.c                   |  2 ++
 ld/testsuite/ld-elf/pr34623.s   | 11 +++++++++++
 ld/testsuite/ld-elf/pr34623a.rd |  4 ++++
 ld/testsuite/ld-elf/pr34623b.rd |  4 ++++
 ld/testsuite/ld-elf/shared.exp  | 10 ++++++++++
 5 files changed, 31 insertions(+)

diff --git a/bfd/elflink.c b/bfd/elflink.c
index d5956b8bd2f..45a8ac36771 100644
--- a/bfd/elflink.c
+++ b/bfd/elflink.c
@@ -7715,6 +7715,7 @@ NOTE: This behaviour is deprecated and will be removed in a future version of th
 	{
 	  if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0))
 	    return false;
+	  obed->elf_backend_hide_symbol (info, h, true);
 	}
       h = (info->fini_function
 	   ? elf_link_hash_lookup (elf_hash_table (info),
@@ -7727,6 +7728,7 @@ NOTE: This behaviour is deprecated and will be removed in a future version of th
 	{
 	  if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0))
 	    return false;
+	  obed->elf_backend_hide_symbol (info, h, true);
 	}
 
       s = bfd_get_section_by_name (info->output_bfd, ".preinit_array");
diff --git a/ld/testsuite/ld-elf/pr34623.s b/ld/testsuite/ld-elf/pr34623.s
new file mode 100644
index 00000000000..08d1b31274e
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr34623.s
@@ -0,0 +1,11 @@
+	.section .fini,"ax",%progbits
+	.globl _fini
+	.type _fini, %function
+_fini:
+	.space 4
+	.section .init,"ax",%progbits
+	.globl _init
+	.type _init, %function
+_init:
+	.space 4
+	.section .note.GNU-stack,"",%progbits
diff --git a/ld/testsuite/ld-elf/pr34623a.rd b/ld/testsuite/ld-elf/pr34623a.rd
new file mode 100644
index 00000000000..4fe65120bee
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr34623a.rd
@@ -0,0 +1,4 @@
+#failif
+#...
+ +[0-9]+: +[0-9a-f]+ +[0-9]+ +FUNC +GLOBAL +DEFAULT +[0-9]+ +_init
+#pass
diff --git a/ld/testsuite/ld-elf/pr34623b.rd b/ld/testsuite/ld-elf/pr34623b.rd
new file mode 100644
index 00000000000..1a64ef19471
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr34623b.rd
@@ -0,0 +1,4 @@
+#failif
+#...
+ +[0-9]+: +[0-9a-f]+ +[0-9]+ +FUNC +GLOBAL +DEFAULT +[0-9]+ +_fini
+#pass
diff --git a/ld/testsuite/ld-elf/shared.exp b/ld/testsuite/ld-elf/shared.exp
index ae70ad2cc1a..8f582311039 100644
--- a/ld/testsuite/ld-elf/shared.exp
+++ b/ld/testsuite/ld-elf/shared.exp
@@ -102,6 +102,16 @@ run_ld_link_tests [list \
 	{} \
 	"pr22649-1.so" \
     ] \
+    [list \
+	"Build pr34623.so" \
+	"$LFLAGS -shared" \
+	"" \
+	"$AFLAGS_PIC" \
+	{pr34623.s} \
+	{{readelf {--dyn-syms --wide} pr34623a.rd} \
+	 {readelf {--dyn-syms --wide} pr34623b.rd}} \
+	"pr34623.so" \
+    ] \
 ]
 
 run_ld_link_tests [list \


More information about the Binutils-cvs mailing list