1 //===-- sanitizer_getauxval.h -----------------------------------*- C++ -*-===// 2 // 3 // This file is distributed under the University of Illinois Open Source 4 // License. See LICENSE.TXT for details. 5 // 6 //===----------------------------------------------------------------------===// 7 // 8 // Common getauxval() guards and definitions. 9 // getauxval() is not defined until glibc version 2.16, or until API level 21 10 // for Android. 11 // Implement the getauxval() compat function for NetBSD. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef SANITIZER_GETAUXVAL_H 16 #define SANITIZER_GETAUXVAL_H 17 18 #include "sanitizer_platform.h" 19 20 #if SANITIZER_LINUX || SANITIZER_FUCHSIA 21 22 # include <features.h> 23 24 # ifndef __GLIBC_PREREQ 25 # define __GLIBC_PREREQ(x, y) 0 26 # endif 27 28 # if __GLIBC_PREREQ(2, 16) || (SANITIZER_ANDROID && __ANDROID_API__ >= 21) || \ 29 SANITIZER_FUCHSIA 30 # define SANITIZER_USE_GETAUXVAL 1 31 # else 32 # define SANITIZER_USE_GETAUXVAL 0 33 # endif 34 35 # if SANITIZER_USE_GETAUXVAL 36 # include <sys/auxv.h> 37 # else 38 // The weak getauxval definition allows to check for the function at runtime. 39 // This is useful for Android, when compiled at a lower API level yet running 40 // on a more recent platform that offers the function. 41 extern "C" SANITIZER_WEAK_ATTRIBUTE 42 unsigned long getauxval(unsigned long type); // NOLINT 43 # endif 44 45 #elif SANITIZER_NETBSD 46 47 #define SANITIZER_USE_GETAUXVAL 1 48 49 #include <dlfcn.h> 50 #include <elf.h> 51 getauxval(decltype (AuxInfo::a_type)type)52static inline decltype(AuxInfo::a_v) getauxval(decltype(AuxInfo::a_type) type) { 53 for (const AuxInfo *aux = (const AuxInfo *)_dlauxinfo(); 54 aux->a_type != AT_NULL; ++aux) { 55 if (type == aux->a_type) 56 return aux->a_v; 57 } 58 59 return 0; 60 } 61 62 #endif 63 64 #endif // SANITIZER_GETAUXVAL_H 65