xref: /freebsd/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_getauxval.h (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 //===-- sanitizer_getauxval.h -----------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Common getauxval() guards and definitions.
10 // getauxval() is not defined until glibc version 2.16, or until API level 21
11 // for Android.
12 // Implement the getauxval() compat function for NetBSD.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef SANITIZER_GETAUXVAL_H
17 #define SANITIZER_GETAUXVAL_H
18 
19 #include "sanitizer_platform.h"
20 #include "sanitizer_glibc_version.h"
21 
22 #if SANITIZER_LINUX || SANITIZER_FUCHSIA
23 
24 # if (__GLIBC_PREREQ(2, 16) || (SANITIZER_ANDROID && __ANDROID_API__ >= 21) || \
25       SANITIZER_FUCHSIA) &&                                                    \
26      !SANITIZER_GO
27 #  define SANITIZER_USE_GETAUXVAL 1
28 # else
29 #  define SANITIZER_USE_GETAUXVAL 0
30 # endif
31 
32 # if SANITIZER_USE_GETAUXVAL
33 #  include <sys/auxv.h>
34 # else
35 // The weak getauxval definition allows to check for the function at runtime.
36 // This is useful for Android, when compiled at a lower API level yet running
37 // on a more recent platform that offers the function.
38 extern "C" SANITIZER_WEAK_ATTRIBUTE unsigned long getauxval(unsigned long type);
39 # endif
40 
41 #elif SANITIZER_NETBSD
42 
43 #define SANITIZER_USE_GETAUXVAL 1
44 
45 #include <dlfcn.h>
46 #include <elf.h>
47 
48 static inline decltype(AuxInfo::a_v) getauxval(decltype(AuxInfo::a_type) type) {
49   for (const AuxInfo *aux = (const AuxInfo *)_dlauxinfo();
50        aux->a_type != AT_NULL; ++aux) {
51     if (type == aux->a_type)
52       return aux->a_v;
53   }
54 
55   return 0;
56 }
57 
58 #endif
59 
60 #endif // SANITIZER_GETAUXVAL_H
61