Mark _init and _fini as hidden [BZ #23145]

Message ID 20180506144340.GA82831@intel.com
State New, archived
Headers

Commit Message

Lu, Hongjiu May 6, 2018, 2:43 p.m. UTC
  _init and _fini are special functions provided by glibc for linker to
define DT_INIT and DT_FINI in executable and shared library.  They
should never be put in dynamic symbol table.  This patch marks them as
hidden to remove them from dynamic symbol table.

Tested with build-many-glibcs.py.

Any comments?


H.J.
---
	[BZ #23145]
	* elf/Makefile (tests-special): Add $(objpfx)check-initfini.out.
	($(all-built-dso:=.dynsym): New target.
	(common-generated): Add $(all-built-dso:$(common-objpfx)%=%.dynsym).
	($(objpfx)check-initfini.out): New target.
	(generated): Add check-initfini.out.
	* scripts/check-initfini.awk: New file.
	* sysdeps/aarch64/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/alpha/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/arm/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/hppa/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/i386/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/ia64/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/m68k/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/microblaze/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/mips/mips32/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/mips/mips64/n32/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/mips/mips64/n64/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/nios2/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/powerpc/powerpc32/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/powerpc/powerpc64/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/s390/s390-32/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/s390/s390-64/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/sh/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/sparc/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
	* sysdeps/x86_64/crti.S (_init): Mark as hidden.
	(_fini): Likewise.
---
 elf/Makefile                     | 15 ++++++++++-
 scripts/check-initfini.awk       | 45 ++++++++++++++++++++++++++++++++
 sysdeps/aarch64/crti.S           |  2 ++
 sysdeps/alpha/crti.S             |  2 ++
 sysdeps/arm/crti.S               |  2 ++
 sysdeps/hppa/crti.S              |  2 ++
 sysdeps/i386/crti.S              |  2 ++
 sysdeps/ia64/crti.S              |  2 ++
 sysdeps/m68k/crti.S              |  2 ++
 sysdeps/microblaze/crti.S        |  2 ++
 sysdeps/mips/mips32/crti.S       |  2 ++
 sysdeps/mips/mips64/n32/crti.S   |  2 ++
 sysdeps/mips/mips64/n64/crti.S   |  2 ++
 sysdeps/nios2/crti.S             |  2 ++
 sysdeps/powerpc/powerpc32/crti.S |  2 ++
 sysdeps/powerpc/powerpc64/crti.S |  2 ++
 sysdeps/s390/s390-32/crti.S      |  2 ++
 sysdeps/s390/s390-64/crti.S      |  2 ++
 sysdeps/sh/crti.S                |  2 ++
 sysdeps/sparc/crti.S             |  2 ++
 sysdeps/x86_64/crti.S            |  2 ++
 21 files changed, 97 insertions(+), 1 deletion(-)
 create mode 100644 scripts/check-initfini.awk
  

Comments

Joseph Myers May 8, 2018, 2:52 p.m. UTC | #1
On Sun, 6 May 2018, H.J. Lu wrote:

> diff --git a/scripts/check-initfini.awk b/scripts/check-initfini.awk
> new file mode 100644
> index 0000000000..e4a2355e02
> --- /dev/null
> +++ b/scripts/check-initfini.awk
> @@ -0,0 +1,45 @@
> +# This awk script expects to get command-line files that are each
> +# the output of 'readelf -d' on a single shared object.
> +# It exits successfully (0) if none contained any TEXTREL markers.
> +# It fails (1) if any did contain a TEXTREL marker.
> +# It fails (2) if the input did not take the expected form.

This comment is clearly inapplicable to this file, since this test is not 
about TEXTREL, and you need a copyright / license notice on this file.
  
H.J. Lu May 8, 2018, 8:50 p.m. UTC | #2
On Tue, May 8, 2018 at 7:52 AM, Joseph Myers <joseph@codesourcery.com> wrote:
> On Sun, 6 May 2018, H.J. Lu wrote:
>
>> diff --git a/scripts/check-initfini.awk b/scripts/check-initfini.awk
>> new file mode 100644
>> index 0000000000..e4a2355e02
>> --- /dev/null
>> +++ b/scripts/check-initfini.awk
>> @@ -0,0 +1,45 @@
>> +# This awk script expects to get command-line files that are each
>> +# the output of 'readelf -d' on a single shared object.
>> +# It exits successfully (0) if none contained any TEXTREL markers.
>> +# It fails (1) if any did contain a TEXTREL marker.
>> +# It fails (2) if the input did not take the expected form.
>
> This comment is clearly inapplicable to this file, since this test is not

Fixed.

> about TEXTREL, and you need a copyright / license notice on this file.
>

check-initfini.awk is based on check-textrel.awk which doesn't have
a copyright / license notice.  What should I put in check-initfini.awk?
  
Joseph Myers May 8, 2018, 9:04 p.m. UTC | #3
On Tue, 8 May 2018, H.J. Lu wrote:

> > about TEXTREL, and you need a copyright / license notice on this file.
> >
> 
> check-initfini.awk is based on check-textrel.awk which doesn't have
> a copyright / license notice.  What should I put in check-initfini.awk?

It looks like the correct dates for check-textrel.awk are 2012-2018 (and 
the usual license notice).
  

Patch

diff --git a/elf/Makefile b/elf/Makefile
index 2dcd2b88e0..f221422de3 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -348,7 +348,7 @@  ifeq ($(run-built-tests),yes)
 tests-special += $(objpfx)tst-pathopt.out $(objpfx)tst-rtld-load-self.out
 endif
 tests-special += $(objpfx)check-textrel.out $(objpfx)check-execstack.out \
-		 $(objpfx)check-localplt.out
+		 $(objpfx)check-localplt.out $(objpfx)check-initfini.out
 endif
 
 ifeq ($(run-built-tests),yes)
@@ -1136,6 +1136,19 @@  $(objpfx)check-localplt.out: $(..)scripts/check-localplt.awk \
 	$(evaluate-test)
 endif
 
+$(all-built-dso:=.dynsym): %.dynsym: %
+	@rm -f $@T
+	LC_ALL=C $(READELF) -W --dyn-syms $< > $@T
+	test -s $@T
+	mv -f $@T $@
+common-generated += $(all-built-dso:$(common-objpfx)%=%.dynsym)
+
+$(objpfx)check-initfini.out: $(..)scripts/check-initfini.awk \
+			    $(all-built-dso:=.dynsym)
+	LC_ALL=C $(AWK) -f $^ > $@; \
+	$(evaluate-test)
+generated += check-initfini.out
+
 $(objpfx)tst-dlopenrpathmod.so: $(libdl)
 $(objpfx)tst-dlopenrpath: $(objpfx)tst-dlopenrpathmod.so $(libdl)
 CFLAGS-tst-dlopenrpath.c += -DPFX=\"$(objpfx)\"
diff --git a/scripts/check-initfini.awk b/scripts/check-initfini.awk
new file mode 100644
index 0000000000..e4a2355e02
--- /dev/null
+++ b/scripts/check-initfini.awk
@@ -0,0 +1,45 @@ 
+# This awk script expects to get command-line files that are each
+# the output of 'readelf -d' on a single shared object.
+# It exits successfully (0) if none contained any TEXTREL markers.
+# It fails (1) if any did contain a TEXTREL marker.
+# It fails (2) if the input did not take the expected form.
+
+BEGIN { result = _init = _fini = sanity = 0 }
+
+function check_one(name) {
+  if (!sanity) {
+    print name ": *** input did not look like readelf -d output";
+    result = 2;
+  } else {
+    ok = 1;
+    if (_init) {
+      print name ": *** _init is in dynamic symbol table";
+      result = result ? result : 1;
+      ok = 0;
+    }
+    if (_fini) {
+      print name ": *** _fini is in dynamic symbol table";
+      result = result ? result : 1;
+      ok = 0;
+    }
+    if (ok)
+      print name ": OK";
+  }
+
+  _init = _fini = sanity = 0
+}
+
+FILENAME != lastfile {
+  if (lastfile)
+    check_one(lastfile);
+  lastfile = FILENAME;
+}
+
+$1 == "Symbol" && $2 == "table" && $3 == "'.dynsym'" { sanity = 1 }
+$8 == "_init" { _init = 1 }
+$8 == "_fini" { _fini = 1 }
+
+END {
+  check_one(lastfile);
+  exit(result);
+}
diff --git a/sysdeps/aarch64/crti.S b/sysdeps/aarch64/crti.S
index 366b837d55..2b213758b2 100644
--- a/sysdeps/aarch64/crti.S
+++ b/sysdeps/aarch64/crti.S
@@ -72,6 +72,7 @@  call_weak_fn:
 	.section .init,"ax",%progbits
 	.align	2
 	.global	_init
+	.hidden	_init
 	.type	_init, %function
 _init:
 	stp	x29, x30, [sp, -16]!
@@ -85,6 +86,7 @@  _init:
 	.section	.fini,"ax",%progbits
 	.align	2
 	.global	_fini
+	.hidden	_fini
 	.type	_fini, %function
 _fini:
 	stp	x29, x30, [sp, -16]!
diff --git a/sysdeps/alpha/crti.S b/sysdeps/alpha/crti.S
index b00ae203f5..bcce1e9055 100644
--- a/sysdeps/alpha/crti.S
+++ b/sysdeps/alpha/crti.S
@@ -67,6 +67,7 @@ 
 
 	.section .init, "ax", @progbits
 	.globl	_init
+	.hidden	_init
 	.type	_init, @function
 	.usepv	_init, std
 _init:
@@ -89,6 +90,7 @@  _init:
 
 	.section .fini, "ax", @progbits
 	.globl	_fini
+	.hidden	_fini
 	.type	_fini,@function
 	.usepv	_fini,std
 _fini:
diff --git a/sysdeps/arm/crti.S b/sysdeps/arm/crti.S
index e645d467d3..a1424d0333 100644
--- a/sysdeps/arm/crti.S
+++ b/sysdeps/arm/crti.S
@@ -78,6 +78,7 @@  call_weak_fn:
 	.section .init,"ax",%progbits
 	.p2align 2
 	.globl _init
+	.hidden	_init
 	.type _init, %function
 _init:
 	push	{r3, lr}
@@ -90,6 +91,7 @@  _init:
 	.section .fini,"ax",%progbits
 	.p2align 2
 	.globl _fini
+	.hidden	_fini
 	.type _fini, %function
 _fini:
 	push	{r3, lr}
diff --git a/sysdeps/hppa/crti.S b/sysdeps/hppa/crti.S
index 4566d9f7da..28550e5758 100644
--- a/sysdeps/hppa/crti.S
+++ b/sysdeps/hppa/crti.S
@@ -142,6 +142,7 @@  gmon_initializer:
 	.section .init, "ax", %progbits
 	.align 4
 	.globl _init
+	.hidden	_init
 	.type _init,@function
 _init:
 	stw	%rp,-20(%sp)
@@ -152,6 +153,7 @@  _init:
         .section .fini,"ax",%progbits
 	.align 4
 	.globl _fini
+	.hidden	_fini
 	.type _fini,@function
 _fini:
 	stw	%rp,-20(%sp)
diff --git a/sysdeps/i386/crti.S b/sysdeps/i386/crti.S
index c0422f9ce3..e647067a33 100644
--- a/sysdeps/i386/crti.S
+++ b/sysdeps/i386/crti.S
@@ -58,6 +58,7 @@ 
 	.section .init,"ax",@progbits
 	.p2align 2
 	.globl _init
+	.hidden	_init
 	.type _init, @function
 _init:
 	pushl %ebx
@@ -77,6 +78,7 @@  _init:
 	.section .fini,"ax",@progbits
 	.p2align 2
 	.globl _fini
+	.hidden	_fini
 	.type _fini, @function
 _fini:
 	pushl %ebx
diff --git a/sysdeps/ia64/crti.S b/sysdeps/ia64/crti.S
index 867683b5ef..c46e297814 100644
--- a/sysdeps/ia64/crti.S
+++ b/sysdeps/ia64/crti.S
@@ -129,6 +129,7 @@  gmon_initializer:
 
 	.section .init,"ax",@progbits
 	.global _init#
+	.hidden	_init#
 	.proc _init#
 _init:
 	.prologue
@@ -145,6 +146,7 @@  _init:
 
 	.section .fini,"ax",@progbits
 	.global _fini#
+	.hidden	_fini#
 	.proc _fini#
 _fini:
 	.prologue
diff --git a/sysdeps/m68k/crti.S b/sysdeps/m68k/crti.S
index b81076357c..e5f2157cd6 100644
--- a/sysdeps/m68k/crti.S
+++ b/sysdeps/m68k/crti.S
@@ -58,6 +58,7 @@ 
 	.section .init,"ax",@progbits
 	.align	2
 	.globl	_init
+	.hidden	_init
 	.type	_init, @function
 _init:
 	link.w %fp, #0
@@ -75,6 +76,7 @@  _init:
 	.section .fini,"ax",@progbits
 	.align	2
 	.globl	_fini
+	.hidden	_fini
 	.type	_fini, @function
 _fini:
 	link.w %fp, #0
diff --git a/sysdeps/microblaze/crti.S b/sysdeps/microblaze/crti.S
index c407487b45..09c8cd7ccd 100644
--- a/sysdeps/microblaze/crti.S
+++ b/sysdeps/microblaze/crti.S
@@ -58,6 +58,7 @@ 
 	.section .init,"ax",@progbits
 	.align	2
 	.globl	_init
+	.hidden	_init
 	.type	_init, @function
 _init:
 	addik	r1,r1,-32
@@ -79,6 +80,7 @@  $Lno_weak_fn:
 	.section .fini,"ax",@progbits
 	.align	2
 	.globl	_fini
+	.hidden	_fini
 	.type	_fini, @function
 _fini:
 	addik	r1,r1,-32
diff --git a/sysdeps/mips/mips32/crti.S b/sysdeps/mips/mips32/crti.S
index 139f8bb4ed..b3e1c4e0d5 100644
--- a/sysdeps/mips/mips32/crti.S
+++ b/sysdeps/mips/mips32/crti.S
@@ -65,6 +65,7 @@ 
 	.section .init,"ax",@progbits
 	.p2align 2
 	.globl _init
+	.hidden	_init
 	.type _init, @function
 _init:
 	.set noreorder
@@ -90,6 +91,7 @@  _init:
 	.section .fini,"ax",@progbits
 	.p2align 2
 	.globl _fini
+	.hidden	_fini
 	.type _fini, @function
 _fini:
 	.set noreorder
diff --git a/sysdeps/mips/mips64/n32/crti.S b/sysdeps/mips/mips64/n32/crti.S
index 23ad90b59b..59a09d5763 100644
--- a/sysdeps/mips/mips64/n32/crti.S
+++ b/sysdeps/mips/mips64/n32/crti.S
@@ -65,6 +65,7 @@ 
 	.section .init,"ax",@progbits
 	.p2align 2
 	.globl _init
+	.hidden _init
 	.type _init, @function
 _init:
 	addiu $sp,$sp,-16
@@ -90,6 +91,7 @@  _init:
 	.section .fini,"ax",@progbits
 	.p2align 2
 	.globl _fini
+	.hidden _fini
 	.type _fini, @function
 _fini:
 	addiu $sp,$sp,-16
diff --git a/sysdeps/mips/mips64/n64/crti.S b/sysdeps/mips/mips64/n64/crti.S
index 24b47b5cf3..fd468f0467 100644
--- a/sysdeps/mips/mips64/n64/crti.S
+++ b/sysdeps/mips/mips64/n64/crti.S
@@ -65,6 +65,7 @@ 
 	.section .init,"ax",@progbits
 	.p2align 2
 	.globl _init
+	.hidden _init
 	.type _init, @function
 _init:
 	daddiu $sp,$sp,-16
@@ -90,6 +91,7 @@  _init:
 	.section .fini,"ax",@progbits
 	.p2align 2
 	.globl _fini
+	.hidden _fini
 	.type _fini, @function
 _fini:
 	daddiu $sp,$sp,-16
diff --git a/sysdeps/nios2/crti.S b/sysdeps/nios2/crti.S
index a3b42a4cc2..46ab896ac0 100644
--- a/sysdeps/nios2/crti.S
+++ b/sysdeps/nios2/crti.S
@@ -57,6 +57,7 @@ 
 	.section .init,"ax",@progbits
 	.align	2
 	.global	_init
+	.hidden	_init
 	.type	_init, @function
 _init:
 	addi	sp, sp, -8
@@ -84,6 +85,7 @@  _init:
 	.section .fini,"ax",@progbits
 	.align	2
 	.global	_fini
+	.hidden	_fini
 	.type	_fini, @function
 _fini:
 	addi	sp, sp, -8
diff --git a/sysdeps/powerpc/powerpc32/crti.S b/sysdeps/powerpc/powerpc32/crti.S
index a30e33a75e..e4ee8dd484 100644
--- a/sysdeps/powerpc/powerpc32/crti.S
+++ b/sysdeps/powerpc/powerpc32/crti.S
@@ -58,6 +58,7 @@ 
 	.section .init,"ax",@progbits
 	.align	2
 	.globl	_init
+	.hidden	_init
 	.type	_init, @function
 _init:
 	stwu r1, -16(r1)
@@ -80,6 +81,7 @@  _init:
 	.section .fini,"ax",@progbits
 	.align	2
 	.globl	_fini
+	.hidden	_fini
 	.type	_fini, @function
 _fini:
 	stwu r1, -16(r1)
diff --git a/sysdeps/powerpc/powerpc64/crti.S b/sysdeps/powerpc/powerpc64/crti.S
index 61b1409a33..2242deb3dd 100644
--- a/sysdeps/powerpc/powerpc64/crti.S
+++ b/sysdeps/powerpc/powerpc64/crti.S
@@ -62,6 +62,7 @@ 
 #endif
 	.section ".init", "ax", @progbits
 	ENTRY_2(_init)
+	.hidden _init
 	.align ALIGNARG (2)
 BODY_LABEL (_init):
 	LOCALENTRY(_init)
@@ -80,6 +81,7 @@  BODY_LABEL (_init):
 
 	.section ".fini", "ax", @progbits
 	ENTRY_2(_fini)
+	.hidden _fini
 	.align ALIGNARG (2)
 BODY_LABEL (_fini):
 	LOCALENTRY(_fini)
diff --git a/sysdeps/s390/s390-32/crti.S b/sysdeps/s390/s390-32/crti.S
index cb0c967de1..44b1a704fd 100644
--- a/sysdeps/s390/s390-32/crti.S
+++ b/sysdeps/s390/s390-32/crti.S
@@ -57,6 +57,7 @@ 
 
 	.section .init,"ax",@progbits
 	.globl	_init
+	.hidden	_init
 	.type	_init,@function
 	.align	4
 _init:
@@ -88,6 +89,7 @@  _init:
 
 	.section .fini,"ax",@progbits
 	.globl	_fini
+	.hidden	_fini
 	.type	_fini,@function
 	.align	4
 _fini:
diff --git a/sysdeps/s390/s390-64/crti.S b/sysdeps/s390/s390-64/crti.S
index 613bac217a..f676eb5259 100644
--- a/sysdeps/s390/s390-64/crti.S
+++ b/sysdeps/s390/s390-64/crti.S
@@ -59,6 +59,7 @@ 
 	.section .init,"ax",@progbits
 	.align 4
 	.globl	_init
+	.hidden	_init
 	.type	_init,@function
 _init:
 	stmg	%r6,%r15,48(%r15)
@@ -81,6 +82,7 @@  _init:
 	.section .fini,"ax",@progbits
 	.align 4
 	.globl	_fini
+	.hidden	_fini
 	.type	_fini,@function
 _fini:
 	stmg	%r6,%r15,48(%r15)
diff --git a/sysdeps/sh/crti.S b/sysdeps/sh/crti.S
index 48f04a48b6..c0707406f5 100644
--- a/sysdeps/sh/crti.S
+++ b/sysdeps/sh/crti.S
@@ -58,6 +58,7 @@ 
 	.section	.init,"ax",@progbits
 	.align 5
 	.global	_init
+	.hidden	_init
 	.type	_init, @function
 _init:
 	mov.l	r12,@-r15
@@ -103,6 +104,7 @@  _init:
 	.section	.fini,"ax",@progbits
 	.align 5
 	.global	_fini
+	.hidden	_fini
 	.type	_fini, @function
 _fini:
 	mov.l	r12,@-r15
diff --git a/sysdeps/sparc/crti.S b/sysdeps/sparc/crti.S
index 69aabe7e3b..a7d1a08acc 100644
--- a/sysdeps/sparc/crti.S
+++ b/sysdeps/sparc/crti.S
@@ -66,6 +66,7 @@ 
 	.section	.init,"ax",@progbits
 	.p2align	2
 	.globl		_init
+	.hidden		_init
 	.type		_init, @function
 _init:
 	save		%sp, -STACKFRAME_SIZE, %sp
@@ -88,6 +89,7 @@  _init:
 	.section	.fini,"ax",@progbits
 	.p2align	2
 	.globl		_fini
+	.hidden		_fini
 	.type		_fini, @function
 _fini:
 	save		%sp, -STACKFRAME_SIZE, %sp
diff --git a/sysdeps/x86_64/crti.S b/sysdeps/x86_64/crti.S
index 619effac3a..f26915e956 100644
--- a/sysdeps/x86_64/crti.S
+++ b/sysdeps/x86_64/crti.S
@@ -58,6 +58,7 @@ 
 	.section .init,"ax",@progbits
 	.p2align 2
 	.globl _init
+	.hidden	_init
 	.type _init, @function
 _init:
 	/* Maintain 16-byte stack alignment for called functions.  */
@@ -75,6 +76,7 @@  _init:
 	.section .fini,"ax",@progbits
 	.p2align 2
 	.globl _fini
+	.hidden	_fini
 	.type _fini, @function
 _fini:
 	subq $8, %rsp