4c1956
Added $(libdl) to $(objpfx)tst-audit-tlsdesc-dlopen in elf/Makefile
4c1956
since we still need $(libdl) in RHEL8.
4c1956
4c1956
commit d1b38173c9255b1a4ae00018ad9b35404a7c74d0
4c1956
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
4c1956
Date:   Wed Jun 30 15:51:31 2021 -0300
4c1956
4c1956
    elf: Add audit tests for modules with TLSDESC
4c1956
    
4c1956
    Checked on x86_64-linux-gnu, i686-linux-gnu, and aarch64-linux-gnu.
4c1956
    
4c1956
    Reviewed-by: Florian Weimer <fweimer@redhat.com>
4c1956
4c1956
diff --git a/elf/Makefile b/elf/Makefile
4c1956
index 0cc03ffe2984ee50..d8d9734df0fea9a8 100644
4c1956
--- a/elf/Makefile
4c1956
+++ b/elf/Makefile
4c1956
@@ -375,6 +375,22 @@ modules-names += tst-gnu2-tls1mod
4c1956
 $(objpfx)tst-gnu2-tls1: $(objpfx)tst-gnu2-tls1mod.so
4c1956
 tst-gnu2-tls1mod.so-no-z-defs = yes
4c1956
 CFLAGS-tst-gnu2-tls1mod.c += -mtls-dialect=gnu2
4c1956
+
4c1956
+tests += tst-audit-tlsdesc tst-audit-tlsdesc-dlopen
4c1956
+modules-names += tst-audit-tlsdesc-mod1 tst-audit-tlsdesc-mod2 tst-auditmod-tlsdesc
4c1956
+$(objpfx)tst-audit-tlsdesc: $(objpfx)tst-audit-tlsdesc-mod1.so \
4c1956
+			    $(objpfx)tst-audit-tlsdesc-mod2.so \
4c1956
+			    $(shared-thread-library)
4c1956
+CFLAGS-tst-audit-tlsdesc-mod1.c += -mtls-dialect=gnu2
4c1956
+CFLAGS-tst-audit-tlsdesc-mod2.c += -mtls-dialect=gnu2
4c1956
+$(objpfx)tst-audit-tlsdesc-dlopen: $(shared-thread-library) $(libdl)
4c1956
+$(objpfx)tst-audit-tlsdesc-dlopen.out: $(objpfx)tst-audit-tlsdesc-mod1.so \
4c1956
+				       $(objpfx)tst-audit-tlsdesc-mod2.so
4c1956
+$(objpfx)tst-audit-tlsdesc-mod1.so: $(objpfx)tst-audit-tlsdesc-mod2.so
4c1956
+$(objpfx)tst-audit-tlsdesc.out: $(objpfx)tst-auditmod-tlsdesc.so
4c1956
+tst-audit-tlsdesc-ENV = LD_AUDIT=$(objpfx)tst-auditmod-tlsdesc.so
4c1956
+$(objpfx)tst-audit-tlsdesc-dlopen.out: $(objpfx)tst-auditmod-tlsdesc.so
4c1956
+tst-audit-tlsdesc-dlopen-ENV = LD_AUDIT=$(objpfx)tst-auditmod-tlsdesc.so
4c1956
 endif
4c1956
 ifeq (yes,$(have-protected-data))
4c1956
 modules-names += tst-protected1moda tst-protected1modb
4c1956
diff --git a/elf/tst-audit-tlsdesc-dlopen.c b/elf/tst-audit-tlsdesc-dlopen.c
4c1956
new file mode 100644
4c1956
index 0000000000000000..9c16bb087aca1b77
4c1956
--- /dev/null
4c1956
+++ b/elf/tst-audit-tlsdesc-dlopen.c
4c1956
@@ -0,0 +1,67 @@
4c1956
+/* DT_AUDIT with modules with TLSDESC.
4c1956
+   Copyright (C) 2021 Free Software Foundation, Inc.
4c1956
+   This file is part of the GNU C Library.
4c1956
+
4c1956
+   The GNU C Library is free software; you can redistribute it and/or
4c1956
+   modify it under the terms of the GNU Lesser General Public
4c1956
+   License as published by the Free Software Foundation; either
4c1956
+   version 2.1 of the License, or (at your option) any later version.
4c1956
+
4c1956
+   The GNU C Library is distributed in the hope that it will be useful,
4c1956
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
4c1956
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c1956
+   Lesser General Public License for more details.
4c1956
+
4c1956
+   You should have received a copy of the GNU Lesser General Public
4c1956
+   License along with the GNU C Library; if not, see
4c1956
+   <https://www.gnu.org/licenses/>.  */
4c1956
+
4c1956
+#include <support/check.h>
4c1956
+#include <support/xthread.h>
4c1956
+#include <support/xdlfcn.h>
4c1956
+
4c1956
+static void *
4c1956
+thr_func (void *mod)
4c1956
+{
4c1956
+  int* (*get_global1)(void) = xdlsym (mod, "get_global1");
4c1956
+  int* (*get_global2)(void) = xdlsym (mod, "get_global2");
4c1956
+  void (*set_global2)(int) = xdlsym (mod, "set_global2");
4c1956
+  int* (*get_local1)(void) = xdlsym (mod, "get_local1");
4c1956
+  int* (*get_local2)(void) = xdlsym (mod, "get_local2");
4c1956
+
4c1956
+  int *global1 = get_global1 ();
4c1956
+  TEST_COMPARE (*global1, 0);
4c1956
+  ++*global1;
4c1956
+
4c1956
+  int *global2 = get_global2 ();
4c1956
+  TEST_COMPARE (*global2, 0);
4c1956
+  ++*global2;
4c1956
+  TEST_COMPARE (*global2, 1);
4c1956
+
4c1956
+  set_global2 (10);
4c1956
+  TEST_COMPARE (*global2, 10);
4c1956
+
4c1956
+  int *local1 = get_local1 ();
4c1956
+  TEST_COMPARE (*local1, 0);
4c1956
+  ++*local1;
4c1956
+
4c1956
+  int *local2 = get_local2 ();
4c1956
+  TEST_COMPARE (*local2, 0);
4c1956
+  ++*local2;
4c1956
+
4c1956
+  return 0;
4c1956
+}
4c1956
+
4c1956
+static int
4c1956
+do_test (void)
4c1956
+{
4c1956
+  void *mod = xdlopen ("tst-audit-tlsdesc-mod1.so", RTLD_LAZY);
4c1956
+
4c1956
+  pthread_t thr = xpthread_create (NULL, thr_func, mod);
4c1956
+  void *r = xpthread_join (thr);
4c1956
+  TEST_VERIFY (r == NULL);
4c1956
+
4c1956
+  return 0;
4c1956
+}
4c1956
+
4c1956
+#include <support/test-driver.c>
4c1956
diff --git a/elf/tst-audit-tlsdesc-mod1.c b/elf/tst-audit-tlsdesc-mod1.c
4c1956
new file mode 100644
4c1956
index 0000000000000000..61c7dd99a2fb5e28
4c1956
--- /dev/null
4c1956
+++ b/elf/tst-audit-tlsdesc-mod1.c
4c1956
@@ -0,0 +1,41 @@
4c1956
+/* DT_AUDIT with modules with TLSDESC.
4c1956
+   Copyright (C) 2021 Free Software Foundation, Inc.
4c1956
+   This file is part of the GNU C Library.
4c1956
+
4c1956
+   The GNU C Library is free software; you can redistribute it and/or
4c1956
+   modify it under the terms of the GNU Lesser General Public
4c1956
+   License as published by the Free Software Foundation; either
4c1956
+   version 2.1 of the License, or (at your option) any later version.
4c1956
+
4c1956
+   The GNU C Library is distributed in the hope that it will be useful,
4c1956
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
4c1956
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c1956
+   Lesser General Public License for more details.
4c1956
+
4c1956
+   You should have received a copy of the GNU Lesser General Public
4c1956
+   License along with the GNU C Library; if not, see
4c1956
+   <https://www.gnu.org/licenses/>.  */
4c1956
+
4c1956
+__thread int global1;
4c1956
+
4c1956
+int *
4c1956
+get_global1 (void)
4c1956
+{
4c1956
+  return &global1;
4c1956
+}
4c1956
+
4c1956
+static __thread int local1;
4c1956
+
4c1956
+void *
4c1956
+get_local1 (void)
4c1956
+{
4c1956
+  return &local1;
4c1956
+}
4c1956
+
4c1956
+extern __thread int global2;
4c1956
+
4c1956
+void
4c1956
+set_global2 (int v)
4c1956
+{
4c1956
+  global2 = v;
4c1956
+}
4c1956
diff --git a/elf/tst-audit-tlsdesc-mod2.c b/elf/tst-audit-tlsdesc-mod2.c
4c1956
new file mode 100644
4c1956
index 0000000000000000..28aef635f688ee03
4c1956
--- /dev/null
4c1956
+++ b/elf/tst-audit-tlsdesc-mod2.c
4c1956
@@ -0,0 +1,33 @@
4c1956
+/* DT_AUDIT with modules with TLSDESC.
4c1956
+   Copyright (C) 2021 Free Software Foundation, Inc.
4c1956
+   This file is part of the GNU C Library.
4c1956
+
4c1956
+   The GNU C Library is free software; you can redistribute it and/or
4c1956
+   modify it under the terms of the GNU Lesser General Public
4c1956
+   License as published by the Free Software Foundation; either
4c1956
+   version 2.1 of the License, or (at your option) any later version.
4c1956
+
4c1956
+   The GNU C Library is distributed in the hope that it will be useful,
4c1956
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
4c1956
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c1956
+   Lesser General Public License for more details.
4c1956
+
4c1956
+   You should have received a copy of the GNU Lesser General Public
4c1956
+   License along with the GNU C Library; if not, see
4c1956
+   <https://www.gnu.org/licenses/>.  */
4c1956
+
4c1956
+__thread int global2;
4c1956
+
4c1956
+int *
4c1956
+get_global2 (void)
4c1956
+{
4c1956
+  return &global2;
4c1956
+}
4c1956
+
4c1956
+static __thread int local2;
4c1956
+
4c1956
+void *
4c1956
+get_local2 (void)
4c1956
+{
4c1956
+  return &local2;
4c1956
+}
4c1956
diff --git a/elf/tst-audit-tlsdesc.c b/elf/tst-audit-tlsdesc.c
4c1956
new file mode 100644
4c1956
index 0000000000000000..3c8be81c95528f47
4c1956
--- /dev/null
4c1956
+++ b/elf/tst-audit-tlsdesc.c
4c1956
@@ -0,0 +1,60 @@
4c1956
+/* DT_AUDIT with modules with TLSDESC.
4c1956
+   Copyright (C) 2021 Free Software Foundation, Inc.
4c1956
+   This file is part of the GNU C Library.
4c1956
+
4c1956
+   The GNU C Library is free software; you can redistribute it and/or
4c1956
+   modify it under the terms of the GNU Lesser General Public
4c1956
+   License as published by the Free Software Foundation; either
4c1956
+   version 2.1 of the License, or (at your option) any later version.
4c1956
+
4c1956
+   The GNU C Library is distributed in the hope that it will be useful,
4c1956
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
4c1956
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c1956
+   Lesser General Public License for more details.
4c1956
+
4c1956
+   You should have received a copy of the GNU Lesser General Public
4c1956
+   License along with the GNU C Library; if not, see
4c1956
+   <https://www.gnu.org/licenses/>.  */
4c1956
+
4c1956
+#include <support/check.h>
4c1956
+#include <support/xthread.h>
4c1956
+
4c1956
+extern __thread int global1;
4c1956
+extern __thread int global2;
4c1956
+void *get_local1 (void);
4c1956
+void set_global2 (int v);
4c1956
+void *get_local2 (void);
4c1956
+
4c1956
+static void *
4c1956
+thr_func (void *clousure)
4c1956
+{
4c1956
+  TEST_COMPARE (global1, 0);
4c1956
+  ++global1;
4c1956
+  TEST_COMPARE (global2, 0);
4c1956
+  ++global2;
4c1956
+  TEST_COMPARE (global2, 1);
4c1956
+
4c1956
+  set_global2 (10);
4c1956
+  TEST_COMPARE (global2, 10);
4c1956
+
4c1956
+  int *local1 = get_local1 ();
4c1956
+  TEST_COMPARE (*local1, 0);
4c1956
+  ++*local1;
4c1956
+
4c1956
+  int *local2 = get_local2 ();
4c1956
+  TEST_COMPARE (*local2, 0);
4c1956
+  ++*local2;
4c1956
+
4c1956
+  return 0;
4c1956
+}
4c1956
+
4c1956
+static int
4c1956
+do_test (void)
4c1956
+{
4c1956
+  pthread_t thr = xpthread_create (NULL, thr_func, NULL);
4c1956
+  void *r = xpthread_join (thr);
4c1956
+  TEST_VERIFY (r == NULL);
4c1956
+  return 0;
4c1956
+}
4c1956
+
4c1956
+#include <support/test-driver.c>
4c1956
diff --git a/elf/tst-auditmod-tlsdesc.c b/elf/tst-auditmod-tlsdesc.c
4c1956
new file mode 100644
4c1956
index 0000000000000000..e4b835d1f1fb6f73
4c1956
--- /dev/null
4c1956
+++ b/elf/tst-auditmod-tlsdesc.c
4c1956
@@ -0,0 +1,25 @@
4c1956
+/* DT_AUDIT with modules with TLSDESC.
4c1956
+   Copyright (C) 2021 Free Software Foundation, Inc.
4c1956
+   This file is part of the GNU C Library.
4c1956
+
4c1956
+   The GNU C Library is free software; you can redistribute it and/or
4c1956
+   modify it under the terms of the GNU Lesser General Public
4c1956
+   License as published by the Free Software Foundation; either
4c1956
+   version 2.1 of the License, or (at your option) any later version.
4c1956
+
4c1956
+   The GNU C Library is distributed in the hope that it will be useful,
4c1956
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
4c1956
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c1956
+   Lesser General Public License for more details.
4c1956
+
4c1956
+   You should have received a copy of the GNU Lesser General Public
4c1956
+   License along with the GNU C Library; if not, see
4c1956
+   <https://www.gnu.org/licenses/>.  */
4c1956
+
4c1956
+#include <link.h>
4c1956
+
4c1956
+unsigned int
4c1956
+la_version (unsigned int version)
4c1956
+{
4c1956
+  return LAV_CURRENT;
4c1956
+}