[10/14] add test-case for RTLD_NORELOCATE

Message ID 20230518082854.3903342-11-stsp2@yandex.ru
State New
Headers
Series implement RTLD_NORELOCATE api [BZ #30007] |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
redhat-pt-bot/TryBot-still_applies warning Patch no longer applies to master

Commit Message

stsp May 18, 2023, 8:28 a.m. UTC
  Makes sure that things work as before.
Makes sure that ctors are called upon dlsym().

The test-suite was run on x86_64/64 and showed no regressions.

Signed-off-by: Stas Sergeev <stsp2@yandex.ru>
---
 dlfcn/Makefile      |  6 ++--
 dlfcn/ctorlib1.c    | 36 ++++++++++++++++++++++
 dlfcn/tst-noreloc.c | 73 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 113 insertions(+), 2 deletions(-)
 create mode 100644 dlfcn/ctorlib1.c
 create mode 100644 dlfcn/tst-noreloc.c
  

Patch

diff --git a/dlfcn/Makefile b/dlfcn/Makefile
index 1fa7fea1ef..16b9401610 100644
--- a/dlfcn/Makefile
+++ b/dlfcn/Makefile
@@ -51,12 +51,12 @@  endif
 ifeq (yes,$(build-shared))
 tests = glrefmain failtest tst-dladdr default errmsg1 tstcxaatexit \
 	bug-dlopen1 bug-dlsym1 tst-dlinfo bug-atexit1 bug-atexit2 \
-	bug-atexit3 tstatexit bug-dl-leaf tst-rec-dlopen
+	bug-atexit3 tstatexit bug-dl-leaf tst-rec-dlopen tst-noreloc
 endif
 modules-names = glreflib1 glreflib2 glreflib3 failtestmod defaultmod1 \
 		defaultmod2 errmsg1mod modatexit modcxaatexit \
 		bug-dlsym1-lib1 bug-dlsym1-lib2 bug-atexit1-lib \
-		bug-atexit2-lib bug-dl-leaf-lib \
+		bug-atexit2-lib bug-dl-leaf-lib ctorlib1 \
 		bug-dl-leaf-lib-cb moddummy1 moddummy2
 
 failtestmod.so-no-z-defs = yes
@@ -102,6 +102,8 @@  $(objpfx)glrefmain.out: $(objpfx)glrefmain \
 $(objpfx)failtest.out: $(objpfx)failtestmod.so
 
 $(objpfx)tst-dladdr.out: $(objpfx)glreflib1.so
+$(objpfx)tst-noreloc.out: $(objpfx)ctorlib1.so
+LDFLAGS-tst-noreloc = $(LDFLAGS-rdynamic)
 
 $(objpfx)tst-dlinfo.out: $(objpfx)glreflib3.so
 LDFLAGS-glreflib3.so = -Wl,-rpath,:
diff --git a/dlfcn/ctorlib1.c b/dlfcn/ctorlib1.c
new file mode 100644
index 0000000000..cf7a9096ea
--- /dev/null
+++ b/dlfcn/ctorlib1.c
@@ -0,0 +1,36 @@ 
+/* Test for ctor calling.
+   Copyright (C) 2000-2023 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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <dlfcn.h>
+#include <assert.h>
+#include <stdio.h>
+
+extern int ref1 (void);
+int
+ref1 (void)
+{
+  return 42;
+}
+
+void __attribute__((constructor))
+ctor (void)
+{
+  int *ct = (int *) dlsym (RTLD_DEFAULT, "ctor_called");
+  assert (ct);
+  (*ct)++;
+}
diff --git a/dlfcn/tst-noreloc.c b/dlfcn/tst-noreloc.c
new file mode 100644
index 0000000000..78f5a7fb25
--- /dev/null
+++ b/dlfcn/tst-noreloc.c
@@ -0,0 +1,73 @@ 
+/* Tests for RTLD_NORELOCATE flag.
+   Copyright (C) 2000-2023 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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <dlfcn.h>
+#include <errno.h>
+#include <error.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <support/check.h>
+
+int ctor_called;
+
+static int
+do_test (void)
+{
+  void *handle;
+  int (*sym) (void);
+  Dl_info info;
+  int ret;
+
+  handle = dlopen ("ctorlib1.so", RTLD_NOW | RTLD_NORELOCATE);
+  if (handle == NULL)
+    error (EXIT_FAILURE, 0, "cannot load: ctorlib1.so");
+
+  TEST_COMPARE (ctor_called, 0);
+  sym = dlsym (handle, "ref1");
+  if (sym == NULL)
+    error (EXIT_FAILURE, 0, "dlsym failed");
+  TEST_COMPARE (ctor_called, 1);
+
+  memset (&info, 0, sizeof (info));
+  ret = dladdr (sym, &info);
+  if (ret == 0)
+    error (EXIT_FAILURE, 0, "dladdr failed");
+
+  printf ("ret = %d\n", ret);
+  printf ("info.dli_fname = %p (\"%s\")\n", info.dli_fname, info.dli_fname);
+  printf ("info.dli_fbase = %p\n", info.dli_fbase);
+  printf ("info.dli_sname = %p (\"%s\")\n", info.dli_sname, info.dli_sname);
+  printf ("info.dli_saddr = %p\n", info.dli_saddr);
+
+  if (info.dli_fname == NULL)
+    error (EXIT_FAILURE, 0, "dli_fname is NULL");
+  if (info.dli_fbase == NULL)
+    error (EXIT_FAILURE, 0, "dli_fbase is NULL");
+  if (info.dli_sname == NULL)
+    error (EXIT_FAILURE, 0, "dli_sname is NULL");
+  if (info.dli_saddr == NULL)
+    error (EXIT_FAILURE, 0, "dli_saddr is NULL");
+
+  dlclose (handle);
+
+  return 0;
+}
+
+
+#include <support/test-driver.c>