public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Stas Sergeev <stsp2@yandex.ru>
To: libc-alpha@sourceware.org
Cc: Stas Sergeev <stsp2@yandex.ru>
Subject: [PATCH 10/14] add test-case for RTLD_NORELOCATE
Date: Thu, 18 May 2023 13:28:50 +0500	[thread overview]
Message-ID: <20230518082854.3903342-11-stsp2@yandex.ru> (raw)
In-Reply-To: <20230518082854.3903342-1-stsp2@yandex.ru>

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

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>
-- 
2.39.2


  parent reply	other threads:[~2023-05-18  8:29 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-18  8:28 [PATCH 00/14] implement RTLD_NORELOCATE api [BZ #30007] Stas Sergeev
2023-05-18  8:28 ` [PATCH 01/14] elf: switch _dl_map_segment() to anonymous mapping Stas Sergeev
2023-05-18  8:28 ` [PATCH 02/14] use initial mmap also for ET_EXEC Stas Sergeev
2023-05-18  8:28 ` [PATCH 03/14] rework maphole Stas Sergeev
2023-05-18  8:28 ` [PATCH 04/14] split do_reloc_1() from dl_open_worker_begin() Stas Sergeev
2023-05-18  8:28 ` [PATCH 05/14] split do_reloc_2() out of do_open_worker() Stas Sergeev
2023-05-18  8:28 ` [PATCH 06/14] move relocation into _dl_object_reloc() func Stas Sergeev
2023-05-18  8:28 ` [PATCH 07/14] split out _dl_finalize_segments() Stas Sergeev
2023-05-18  8:28 ` [PATCH 08/14] finalize elf segments on a relocation step Stas Sergeev
2023-05-18  8:28 ` [PATCH 09/14] implement RTLD_NORELOCATE dlopen() flag Stas Sergeev
2023-05-18  8:28 ` Stas Sergeev [this message]
2023-05-18  8:28 ` [PATCH 11/14] implement dlrelocate() function Stas Sergeev
2023-05-18  8:28 ` [PATCH 12/14] implement RTLD_DI_MAPINFO dlinfo() request Stas Sergeev
2023-05-18  8:28 ` [PATCH 13/14] implement dlset_object_base() function Stas Sergeev
2023-05-18  8:28 ` [PATCH 14/14] implement RTLD_DI_DEPLIST dlinfo() request Stas Sergeev

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=20230518082854.3903342-11-stsp2@yandex.ru \
    --to=stsp2@yandex.ru \
    --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).