public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "Maciej W. Rozycki" <macro@mips.com>
To: <libc-alpha@sourceware.org>
Cc: Alan Modra <amodra@gmail.com>
Subject: [PATCH 1/2] elf: Accept absolute (SHN_ABS) symbols whose value is zero [BZ #23307]
Date: Mon, 18 Jun 2018 18:08:00 -0000	[thread overview]
Message-ID: <alpine.DEB.2.00.1806181716230.20622@tp.orcam.me.uk> (raw)
In-Reply-To: <alpine.DEB.2.00.1806181533590.20622@tp.orcam.me.uk>

We have this condition in `check_match' (in elf/dl-lookup.c):

  if (__glibc_unlikely ((sym->st_value == 0 /* No value.  */
                         && stt != STT_TLS)
                        || ELF_MACHINE_SYM_NO_MATCH (sym)
                        || (type_class & (sym->st_shndx == SHN_UNDEF))))
    return NULL;

which causes all !STT_TLS symbols whose value is zero to be silently 
ignored in lookup.  This may make sense for regular symbols, however not 
for absolute (SHN_ABS) ones, where zero is like any value, there's no 
special meaning attached to it.

Consequently legitimate programs fail, for example taking the 
`elf/tst-absolute-sym' test case, substituting 0 for 0x55aa in 
`elf/tst-absolute-sym-lib.lds' and then trying to run the resulting 
program we get this:

$ .../elf/tst-absolute-sym
.../elf/tst-absolute-sym: symbol lookup error: .../elf/tst-absolute-sym-lib.so: undefined symbol: absolute
$ 

even though the symbol clearly is there:

$ readelf --dyn-syms .../elf/tst-absolute-sym-lib.so | grep '\babsolute\b'
     7: 00000000     0 NOTYPE  GLOBAL DEFAULT  ABS absolute
$ 

The check for the zero value has been there since forever or commit 
d66e34cd4234/08162fa88891 ("Implemented runtime dynamic linker to 
support ELF shared libraries.") dating back to May 2nd 1995, and the 
problem triggers regardless of commit e7feec374c63 ("elf: Correct 
absolute (SHN_ABS) symbol run-time calculation [BZ #19818]") being 
present or not.

Fix the issue then, by permitting `sym->st_value' to be 0 for SHN_ABS 
symbols in lookup.

	[BZ #23307]
	* elf/dl-lookup.c (check_match): Do not reject a symbol whose
	`st_value' is 0 if `st_shndx' is SHN_ABS.
	* elf/tst-absolute-zero.c: New file.
	* elf/tst-absolute-zero-lib.c: New file.
	* elf/tst-absolute-zero-lib.lds: New file.
	* elf/Makefile (tests): Add `tst-absolute-zero'.
	(modules-names): Add `tst-absolute-zero-lib'.
	(LDLIBS-tst-absolute-zero-lib.so): New variable.
	($(objpfx)tst-absolute-zero-lib.so): New dependency.
	($(objpfx)tst-absolute-zero: New dependency.
---
Hi,

 This has been regression-tested successfully with the `mips-linux-gnu' 
target and the o32 ABI, big endianness.  The new test case fails with an 
"undefined symbol" message where the fix included here to `check_match' 
has been removed whether commit e7feec374c63 ("elf: Correct absolute 
(SHN_ABS) symbol run-time calculation [BZ #19818]") is also present or 
not, and it passes with the fix applied.

 OK to apply?

  Maciej
---
 elf/Makefile                  |    8 ++++++--
 elf/dl-lookup.c               |    1 +
 elf/tst-absolute-zero-lib.c   |   25 +++++++++++++++++++++++++
 elf/tst-absolute-zero-lib.lds |    1 +
 elf/tst-absolute-zero.c       |   38 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 71 insertions(+), 2 deletions(-)

glibc-elf-shn-abs-zero.diff
Index: glibc/elf/Makefile
===================================================================
--- glibc.orig/elf/Makefile	2018-06-18 18:12:19.911942887 +0100
+++ glibc/elf/Makefile	2018-06-18 18:12:40.092259983 +0100
@@ -186,7 +186,7 @@ tests += restest1 preloadtest loadfail m
 	 tst-tlsalign tst-tlsalign-extern tst-nodelete-opened \
 	 tst-nodelete2 tst-audit11 tst-audit12 tst-dlsym-error tst-noload \
 	 tst-latepthread tst-tls-manydynamic tst-nodelete-dlclose \
-	 tst-debug1 tst-main1 tst-absolute-sym tst-big-note
+	 tst-debug1 tst-main1 tst-absolute-sym tst-absolute-zero tst-big-note
 #	 reldep9
 tests-internal += loadtest unload unload2 circleload1 \
 	 neededtest neededtest2 neededtest3 neededtest4 \
@@ -273,7 +273,7 @@ modules-names = testobj1 testobj2 testob
 		tst-latepthreadmod $(tst-tls-many-dynamic-modules) \
 		tst-nodelete-dlclose-dso tst-nodelete-dlclose-plugin \
 		tst-main1mod tst-libc_dlvsym-dso tst-absolute-sym-lib \
-		tst-big-note-lib
+		tst-absolute-zero-lib tst-big-note-lib
 
 ifeq (yes,$(have-mtls-dialect-gnu2))
 tests += tst-gnu2-tls1
@@ -1456,6 +1456,10 @@ LDLIBS-tst-absolute-sym-lib.so = tst-abs
 $(objpfx)tst-absolute-sym-lib.so: $(LDLIBS-tst-absolute-sym-lib.so)
 $(objpfx)tst-absolute-sym: $(objpfx)tst-absolute-sym-lib.so
 
+LDLIBS-tst-absolute-zero-lib.so = tst-absolute-zero-lib.lds
+$(objpfx)tst-absolute-zero-lib.so: $(LDLIBS-tst-absolute-zero-lib.so)
+$(objpfx)tst-absolute-zero: $(objpfx)tst-absolute-zero-lib.so
+
 # Both the main program and the DSO for tst-libc_dlvsym need to link
 # against libdl.
 $(objpfx)tst-libc_dlvsym: $(libdl)
Index: glibc/elf/dl-lookup.c
===================================================================
--- glibc.orig/elf/dl-lookup.c	2018-06-17 09:06:30.202407070 +0100
+++ glibc/elf/dl-lookup.c	2018-06-18 18:12:36.138114433 +0100
@@ -76,6 +76,7 @@ check_match (const char *const undef_nam
   unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
   assert (ELF_RTYPE_CLASS_PLT == 1);
   if (__glibc_unlikely ((sym->st_value == 0 /* No value.  */
+			 && sym->st_shndx != SHN_ABS
 			 && stt != STT_TLS)
 			|| ELF_MACHINE_SYM_NO_MATCH (sym)
 			|| (type_class & (sym->st_shndx == SHN_UNDEF))))
Index: glibc/elf/tst-absolute-zero-lib.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ glibc/elf/tst-absolute-zero-lib.c	2018-06-18 18:12:40.102324540 +0100
@@ -0,0 +1,25 @@
+/* BZ #xxxxx absolute zero symbol calculation shared module.
+   Copyright (C) 2018 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+extern char absolute;
+
+void *
+get_absolute (void)
+{
+  return &absolute;
+}
Index: glibc/elf/tst-absolute-zero-lib.lds
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ glibc/elf/tst-absolute-zero-lib.lds	2018-06-18 18:12:40.129495568 +0100
@@ -0,0 +1 @@
+"absolute" = 0;
Index: glibc/elf/tst-absolute-zero.c
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ glibc/elf/tst-absolute-zero.c	2018-06-18 18:12:40.147611912 +0100
@@ -0,0 +1,38 @@
+/* BZ #xxxxx absolute zero symbol calculation main executable.
+   Copyright (C) 2018 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#include <support/check.h>
+#include <support/support.h>
+#include <support/test-driver.h>
+
+void *get_absolute (void);
+
+static int
+do_test (void)
+{
+  void *ref = (void *) 0;
+  void *ptr;
+
+  ptr = get_absolute ();
+  if (ptr != ref)
+    FAIL_EXIT1 ("Got %p, expected %p\n", ptr, ref);
+
+  return 0;
+}
+
+#include <support/test-driver.c>

  parent reply	other threads:[~2018-06-18 18:08 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-18 18:06 [PATCH 0/2] Accept absolute " Maciej W. Rozycki
2018-06-18 18:08 ` [PATCH 2/2] libc-abis: Define ABSOLUTE ABI [BZ #19818][BZ #23307] Maciej W. Rozycki
2018-06-25 20:33   ` Florian Weimer
2018-06-27 22:08     ` Maciej W. Rozycki
2018-06-28  6:59       ` Florian Weimer
2018-06-28 13:49         ` Maciej W. Rozycki
2018-06-28 13:54           ` Florian Weimer
2018-06-28 14:54             ` Maciej W. Rozycki
2018-06-29 16:29               ` Maciej W. Rozycki
2018-07-03 13:53                 ` Carlos O'Donell
2018-07-05 15:42                   ` [PATCH v2 " Maciej W. Rozycki
2018-07-05 16:29                     ` Carlos O'Donell
2018-07-05 17:10                       ` Maciej W. Rozycki
2018-07-05 18:02                         ` Carlos O'Donell
2018-06-18 18:08 ` Maciej W. Rozycki [this message]
2018-06-25 20:25   ` [PATCH 1/2] elf: Accept absolute (SHN_ABS) symbols whose value is zero [BZ #23307] Florian Weimer
2018-06-29 16:13     ` [committed v2 " Maciej W. Rozycki
2018-06-29 17:41       ` Joseph Myers
2018-06-29 18:05         ` Maciej W. Rozycki
2018-06-25 19:14 ` [PING][PATCH 0/2] Accept absolute " Maciej W. Rozycki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.2.00.1806181716230.20622@tp.orcam.me.uk \
    --to=macro@mips.com \
    --cc=amodra@gmail.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).