[Reland][Runtimes] Merge 'compile_commands.json' files from runtimes build (#116303)
[llvm-project.git] / compiler-rt / lib / sanitizer_common / sanitizer_getauxval.h
blob38439e44f611e6fe59da7a9de1650a8a98f619c9
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef SANITIZER_GETAUXVAL_H
17 #define SANITIZER_GETAUXVAL_H
19 #include "sanitizer_platform.h"
20 #include "sanitizer_glibc_version.h"
22 #if SANITIZER_LINUX || SANITIZER_FUCHSIA
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
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
41 #elif SANITIZER_NETBSD
43 #define SANITIZER_USE_GETAUXVAL 1
45 #include <dlfcn.h>
46 #include <elf.h>
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;
55 return 0;
58 #endif
60 #endif // SANITIZER_GETAUXVAL_H