sanitizer_getauxval.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #ifndef SANITIZER_GETAUXVAL_H
  16. #define SANITIZER_GETAUXVAL_H
  17. #include "sanitizer_platform.h"
  18. #include "sanitizer_glibc_version.h"
  19. #if SANITIZER_LINUX || SANITIZER_FUCHSIA
  20. # if (__GLIBC_PREREQ(2, 16) || (SANITIZER_ANDROID && __ANDROID_API__ >= 21) || \
  21. SANITIZER_FUCHSIA) && \
  22. !SANITIZER_GO
  23. # define SANITIZER_USE_GETAUXVAL 1
  24. # else
  25. # define SANITIZER_USE_GETAUXVAL 0
  26. # endif
  27. # if SANITIZER_USE_GETAUXVAL
  28. # include <sys/auxv.h>
  29. # else
  30. // The weak getauxval definition allows to check for the function at runtime.
  31. // This is useful for Android, when compiled at a lower API level yet running
  32. // on a more recent platform that offers the function.
  33. extern "C" SANITIZER_WEAK_ATTRIBUTE unsigned long getauxval(unsigned long type);
  34. # endif
  35. #elif SANITIZER_NETBSD
  36. #define SANITIZER_USE_GETAUXVAL 1
  37. #include <dlfcn.h>
  38. #include <elf.h>
  39. static inline decltype(AuxInfo::a_v) getauxval(decltype(AuxInfo::a_type) type) {
  40. for (const AuxInfo *aux = (const AuxInfo *)_dlauxinfo();
  41. aux->a_type != AT_NULL; ++aux) {
  42. if (type == aux->a_type)
  43. return aux->a_v;
  44. }
  45. return 0;
  46. }
  47. #endif
  48. #endif // SANITIZER_GETAUXVAL_H