]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
elf: Generalize name-based DSO recognition in ldconfig
authorFlorian Weimer <fweimer@redhat.com>
Mon, 28 Jun 2021 06:33:57 +0000 (08:33 +0200)
committerFlorian Weimer <fweimer@redhat.com>
Mon, 28 Jun 2021 06:33:57 +0000 (08:33 +0200)
This introduces <dl-is_dso.h> and the _dl_is_dso function.  A
test ensures that the official names of libc.so, ld.so, and their
versioned names are recognized.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>
Tested-by: Carlos O'Donell <carlos@redhat.com>
elf/Makefile
elf/dl-is_dso.h [new file with mode: 0644]
elf/ldconfig.c
elf/tst-dl-is_dso.c [new file with mode: 0644]

index 38d08e03b8979a81544a8296c8022ba1c0f99dd7..62f7e8a22544ab9d1fcdab101f9e6bc3ed292e18 100644 (file)
@@ -223,7 +223,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
         tst-single_threaded tst-single_threaded-pthread \
         tst-tls-ie tst-tls-ie-dlmopen argv0test \
         tst-glibc-hwcaps tst-glibc-hwcaps-prepend tst-glibc-hwcaps-mask \
-        tst-tls20 tst-tls21 tst-dlmopen-dlerror tst-dlmopen-gethostbyname
+        tst-tls20 tst-tls21 tst-dlmopen-dlerror tst-dlmopen-gethostbyname \
+        tst-dl-is_dso
 #       reldep9
 tests-internal += loadtest unload unload2 circleload1 \
         neededtest neededtest2 neededtest3 neededtest4 \
diff --git a/elf/dl-is_dso.h b/elf/dl-is_dso.h
new file mode 100644 (file)
index 0000000..94e0096
--- /dev/null
@@ -0,0 +1,33 @@
+/* Heuristic for recognizing DSO file names.
+   Copyright (C) 2021 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 <stdbool.h>
+#include <string.h>
+
+/* Returns true if the file name looks like a DSO name.  */
+static bool
+_dl_is_dso (const char *name)
+{
+  /* Recognize lib*.so*, ld-*.so*, ld.so.*, ld64.so.*.  ld-*.so*
+     matches both platform dynamic linker names like ld-linux.so.2,
+     and versioned dynamic loader names like ld-2.12.so.  */
+  return (((strncmp (name, "lib", 3) == 0 || strncmp (name, "ld-", 3) == 0)
+           && strstr (name, ".so") != NULL)
+          || strncmp (name, "ld.so.", 6) == 0
+          || strncmp (name, "ld64.so.", 8) == 0);
+}
index 96bf7700b2efd37a3ace84e971168d4a64c5eb3a..1037e8d0cf8d28b6ab681360169ed43eba88f362 100644 (file)
@@ -43,6 +43,7 @@
 #include <ldconfig.h>
 #include <dl-cache.h>
 #include <dl-hwcaps.h>
+#include <dl-is_dso.h>
 
 #include <dl-procinfo.h>
 
@@ -842,9 +843,7 @@ search_dir (const struct dir_entry *entry)
         subdirectory (if not already processing a glibc-hwcaps
         subdirectory)?  The dynamic linker is also considered as
         shared library.  */
-      if (((strncmp (direntry->d_name, "lib", 3) != 0
-           && strncmp (direntry->d_name, "ld-", 3) != 0)
-          || strstr (direntry->d_name, ".so") == NULL)
+      if (!_dl_is_dso (direntry->d_name)
          && (direntry->d_type == DT_REG
              || (entry->hwcaps == NULL
                  && !is_hwcap_platform (direntry->d_name))))
diff --git a/elf/tst-dl-is_dso.c b/elf/tst-dl-is_dso.c
new file mode 100644 (file)
index 0000000..48d2cc9
--- /dev/null
@@ -0,0 +1,35 @@
+/* Test heuristic for recognizing DSO file names.
+   Copyright (C) 2021 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 <dl-is_dso.h>
+#include <gnu/lib-names.h>
+#include <support/check.h>
+
+static int
+do_test (void)
+{
+  /* Official ABI names.  */
+  TEST_VERIFY (_dl_is_dso (LIBC_SO));
+  TEST_VERIFY (_dl_is_dso (LD_SO));
+  /* Version-based names.  The version number does not matter.  */
+  TEST_VERIFY (_dl_is_dso ("libc-2.12.so"));
+  TEST_VERIFY (_dl_is_dso ("ld-2.12.so"));
+  return 0;
+}
+
+#include <support/test-driver.c>