1 /* Audit modules for tst-audit24a.
2    Copyright (C) 2022 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     0x1
26 #define AUDIT24MOD1_COOKIE 0x2
27 #define AUDIT24MOD2_COOKIE 0x3
28 
29 #ifndef TEST_NAME
30 # define TEST_NAME "tst-audit24a"
31 #endif
32 #ifndef TEST_MOD
33 # define TEST_MOD TEST_NAME
34 #endif
35 #ifndef TEST_FUNC
36 # define TEST_FUNC "tst_audit24a"
37 #endif
38 
39 unsigned int
la_version(unsigned int version)40 la_version (unsigned int version)
41 {
42   return LAV_CURRENT;
43 }
44 
45 unsigned int
la_objopen(struct link_map * map,Lmid_t lmid,uintptr_t * cookie)46 la_objopen (struct link_map *map, Lmid_t lmid, uintptr_t *cookie)
47 {
48   const char *p = strrchr (map->l_name, '/');
49   const char *l_name = p == NULL ? TEST_NAME : p + 1;
50 
51   uintptr_t ck = -1;
52   if (strcmp (l_name, TEST_MOD "mod1.so") == 0)
53     ck = AUDIT24MOD1_COOKIE;
54   else if (strcmp (l_name, TEST_MOD "mod2.so") == 0)
55     ck = AUDIT24MOD2_COOKIE;
56   else if (strcmp (l_name, TEST_NAME) == 0)
57     ck = AUDIT24_COOKIE;
58 
59   *cookie = ck;
60   return ck == -1 ? 0 : LA_FLG_BINDFROM | LA_FLG_BINDTO;
61 }
62 
63 static int
tst_func1(void)64 tst_func1 (void)
65 {
66   return 1;
67 }
68 
69 static int
tst_func2(void)70 tst_func2 (void)
71 {
72   return 10;
73 }
74 
75 #if __ELF_NATIVE_CLASS == 64
76 uintptr_t
la_symbind64(Elf64_Sym * sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,unsigned int * flags,const char * symname)77 la_symbind64 (Elf64_Sym *sym, unsigned int ndx,
78 	      uintptr_t *refcook, uintptr_t *defcook,
79 	      unsigned int *flags, const char *symname)
80 #else
81 uintptr_t
82 la_symbind32 (Elf32_Sym *sym, unsigned int ndx,
83 	      uintptr_t *refcook, uintptr_t *defcook,
84 	      unsigned int *flags, const char *symname)
85 #endif
86 {
87   if (*refcook == AUDIT24_COOKIE)
88     {
89       if (*defcook == AUDIT24MOD1_COOKIE)
90 	{
91 	  /* Check if bind-now symbols are advertised to not call the PLT
92 	     hooks.  */
93 	  test_symbind_flags (*flags);
94 
95 	  if (strcmp (symname, TEST_FUNC "mod1_func1") == 0)
96 	    return (uintptr_t) tst_func1;
97 	  else if (strcmp (symname, TEST_FUNC "mod1_func2") == 0)
98 	    return sym->st_value;
99 	  abort ();
100 	}
101       if (*defcook == AUDIT24MOD2_COOKIE
102 	  && (strcmp (symname, TEST_FUNC "mod2_func1") == 0))
103 	{
104 	  test_symbind_flags (*flags);
105 
106 	  return (uintptr_t) tst_func2;
107 	}
108 
109       /* malloc functions.  */
110       return sym->st_value;
111     }
112 
113   if (symname[0] != '\0')
114     abort ();
115   return sym->st_value;
116 }
117