1 /* Audit modules for tst-audit25a.
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 <stdio.h>
24 
25 #define AUDIT25_COOKIE     0x1
26 #define AUDIT25MOD1_COOKIE 0x2
27 #define AUDIT25MOD2_COOKIE 0x3
28 #define AUDIT25MOD3_COOKIE 0x2
29 #define AUDIT25MOD4_COOKIE 0x3
30 
31 #define TEST_NAME "tst-audit25"
32 #define TEST_MOD  "tst-audit25"
33 #define TEST_FUNC "tst_audit25"
34 
35 unsigned int
la_version(unsigned int version)36 la_version (unsigned int version)
37 {
38   return LAV_CURRENT;
39 }
40 
41 unsigned int
la_objopen(struct link_map * map,Lmid_t lmid,uintptr_t * cookie)42 la_objopen (struct link_map *map, Lmid_t lmid, uintptr_t *cookie)
43 {
44   const char *p = strrchr (map->l_name, '/');
45   const char *l_name = p == NULL ? TEST_NAME : p + 1;
46 
47   uintptr_t ck = -1;
48   if (strcmp (l_name, TEST_MOD "mod1.so") == 0)
49     ck = AUDIT25MOD1_COOKIE;
50   else if (strcmp (l_name, TEST_MOD "mod2.so") == 0)
51     ck = AUDIT25MOD2_COOKIE;
52   else if (strcmp (l_name, TEST_MOD "mod3.so") == 0)
53     ck = AUDIT25MOD3_COOKIE;
54   else if (strcmp (l_name, TEST_MOD "mod4.so") == 0)
55     ck = AUDIT25MOD4_COOKIE;
56   else if (strncmp (l_name, TEST_NAME, strlen (TEST_NAME)) == 0)
57     ck = AUDIT25_COOKIE;
58 
59   *cookie = ck;
60   return ck == -1 ? 0 : LA_FLG_BINDFROM | LA_FLG_BINDTO;
61 }
62 
63 #if __ELF_NATIVE_CLASS == 64
64 uintptr_t
la_symbind64(Elf64_Sym * sym,unsigned int ndx,uintptr_t * refcook,uintptr_t * defcook,unsigned int * flags,const char * symname)65 la_symbind64 (Elf64_Sym *sym, unsigned int ndx,
66 	      uintptr_t *refcook, uintptr_t *defcook,
67 	      unsigned int *flags, const char *symname)
68 #else
69 uintptr_t
70 la_symbind32 (Elf32_Sym *sym, unsigned int ndx,
71 	      uintptr_t *refcook, uintptr_t *defcook,
72 	      unsigned int *flags, const char *symname)
73 #endif
74 {
75   if (*refcook != -1 && *defcook != -1 && symname[0] != '\0'
76       && (*flags & LA_SYMB_DLSYM) == 0)
77     fprintf (stderr, "la_symbind: %s %u\n", symname,
78 	     *flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT) ? 1 : 0);
79   return sym->st_value;
80 }
81