rtld: properly handle root directory in load path (bug 30435)
[platform/upstream/glibc.git] / elf / tst-auditmod24d.c
1 /* Audit module for tst-audit24d.
2    Copyright (C) 2022-2023 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 <link.h>
20 #include <inttypes.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <tst-auditmod24.h>
24
25 #define AUDIT24_COOKIE     0x0
26 #define AUDIT24MOD1_COOKIE 0x1
27 #define AUDIT24MOD2_COOKIE 0x2
28 #define AUDIT24MOD3_COOKIE 0x3
29 #define AUDIT24MOD4_COOKIE 0x4
30
31 unsigned int
32 la_version (unsigned int version)
33 {
34   return LAV_CURRENT;
35 }
36
37 unsigned int
38 la_objopen (struct link_map *map, Lmid_t lmid, uintptr_t *cookie)
39 {
40   const char *p = strrchr (map->l_name, '/');
41   const char *l_name = p == NULL ? "tst-audit24d" : p + 1;
42
43   uintptr_t ck = -1;
44   if (strcmp (l_name, "tst-audit24dmod1.so") == 0)
45     ck = AUDIT24MOD1_COOKIE;
46   else if (strcmp (l_name, "tst-audit24dmod2.so") == 0)
47     ck = AUDIT24MOD2_COOKIE;
48   else if (strcmp (l_name, "tst-audit24dmod3.so") == 0)
49     ck = AUDIT24MOD3_COOKIE;
50   else if (strcmp (l_name, "tst-audit24dmod.so") == 0)
51     ck = AUDIT24MOD4_COOKIE;
52   else if (strcmp (l_name, "tst-audit24d") == 0)
53     ck = AUDIT24_COOKIE;
54
55   *cookie = ck;
56   return ck == -1 ? 0 : LA_FLG_BINDFROM | LA_FLG_BINDTO;
57 }
58
59 static int
60 tst_audit24dmod1_func1 (void)
61 {
62   return 1;
63 }
64
65 static int
66 tst_audit24dmod2_func1 (void)
67 {
68   return 10;
69 }
70
71 static int
72 tst_audit24dmod3_func1 (void)
73 {
74   return 30;
75 }
76
77 #include <stdio.h>
78
79 #if __ELF_NATIVE_CLASS == 64
80 uintptr_t
81 la_symbind64 (Elf64_Sym *sym, unsigned int ndx,
82               uintptr_t *refcook, uintptr_t *defcook,
83               unsigned int *flags, const char *symname)
84 #else
85 uintptr_t
86 la_symbind32 (Elf32_Sym *sym, unsigned int ndx,
87               uintptr_t *refcook, uintptr_t *defcook,
88               unsigned int *flags, const char *symname)
89 #endif
90 {
91   if (*refcook == AUDIT24_COOKIE)
92     {
93       if (*defcook == AUDIT24MOD1_COOKIE)
94           {
95             if (strcmp (symname, "tst_audit24dmod1_func1") == 0)
96               return (uintptr_t) tst_audit24dmod1_func1;
97             else if (strcmp (symname, "tst_audit24dmod1_func2") == 0)
98               return sym->st_value;
99             abort ();
100           }
101       if (*defcook == AUDIT24MOD2_COOKIE
102           && (strcmp (symname, "tst_audit24dmod2_func1") == 0))
103         return (uintptr_t) tst_audit24dmod2_func1;
104
105       /* malloc functions.  */
106       return sym->st_value;
107     }
108   else if (*refcook == AUDIT24MOD1_COOKIE)
109     {
110       if (*defcook == AUDIT24MOD3_COOKIE
111           && strcmp (symname, "tst_audit24dmod3_func1") == 0)
112         {
113           test_symbind_flags (*flags);
114
115           return (uintptr_t) tst_audit24dmod3_func1;
116         }
117     }
118
119   if (symname[0] != '\0')
120     abort ();
121   return sym->st_value;
122 }