1 /* Support for reading /etc/ld.so.cache files written by Linux ldconfig.
2    Copyright (C) 2003-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library.  If not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <ldconfig.h>
20 #include <assert.h>
21 
22 /* For now we only support the natural XLEN ABI length on all targets, so the
23    only bits that need to go into ld.so.cache are the FLEG ABI length.  */
24 #if defined __riscv_float_abi_double
25 # define _DL_CACHE_DEFAULT_ID    (FLAG_RISCV_FLOAT_ABI_DOUBLE | FLAG_ELF_LIBC6)
26 #else
27 # define _DL_CACHE_DEFAULT_ID    (FLAG_RISCV_FLOAT_ABI_SOFT | FLAG_ELF_LIBC6)
28 #endif
29 
30 #define _dl_cache_check_flags(flags)		    			\
31   ((flags) == _DL_CACHE_DEFAULT_ID)
32 
33 /* If given a path to one of our library directories, adds every library
34    directory via add_dir (), otherwise just adds the giver directory.  On
35    RISC-V, libraries can be found in paths ending in:
36      - /lib64/lp64d
37      - /lib64/lp64
38      - /lib32/ilp32d
39      - /lib32/ilp32
40      - /lib (only ld.so)
41    so this will add all of those paths.
42 
43    According to Joseph Myers:
44        My reasoning for that would be: generic autoconf-configured (etc.)
45        software may only know about using the lib directory, so you want the
46        lib directory to be searched regardless of the ABI - but it's also
47        useful to be able to e.g. list /usr/local/lib in /etc/ld.so.conf for all
48        architectures and have that automatically imply /usr/local/lib64/lp64d
49        etc. so that libraries can be found that come from software that does
50        use the ABI-specific directories.  */
51 #define add_system_dir(dir) 						\
52   do							    		\
53     {									\
54       static const char* lib_dirs[] = {					\
55 	"/lib64/lp64d",							\
56 	"/lib64/lp64",							\
57 	"/lib32/ilp32d",						\
58 	"/lib32/ilp32",							\
59 	NULL,								\
60       };								\
61       const size_t lib_len = sizeof ("/lib") - 1;			\
62       size_t len = strlen (dir);					\
63       char path[len + 10];						\
64       const char **ptr;							\
65 									\
66       memcpy (path, dir, len + 1);					\
67 									\
68       for (ptr = lib_dirs; *ptr != NULL; ptr++)				\
69 	{								\
70 	  const char *lib_dir = *ptr;					\
71 	  size_t dir_len = strlen (lib_dir);				\
72 									\
73 	  if (len >= dir_len						\
74 	      && !memcmp (path + len - dir_len, lib_dir, dir_len))	\
75 	    {								\
76 	      len -= dir_len - lib_len;					\
77 	      path[len] = '\0';						\
78 	      break;							\
79 	    }								\
80 	}								\
81       add_dir (path);							\
82       if (len >= lib_len						\
83 	  && !memcmp (path + len - lib_len, "/lib", lib_len))		\
84 	for (ptr = lib_dirs; *ptr != NULL; ptr++)			\
85 	  {								\
86 	    const char *lib_dir = *ptr;					\
87 	    size_t dir_len = strlen (lib_dir);				\
88 									\
89 	    assert (dir_len >= lib_len);				\
90 	    memcpy (path + len, lib_dir + lib_len,			\
91 		    dir_len - lib_len + 1);				\
92 	    add_dir (path);						\
93 	  }								\
94     } while (0)
95 
96 
97 #include_next <dl-cache.h>
98