sanitizer_getauxval.h revision 360784
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#  define SANITIZER_USE_GETAUXVAL 1
27# else
28#  define SANITIZER_USE_GETAUXVAL 0
29# endif
30
31# if SANITIZER_USE_GETAUXVAL
32#  include <sys/auxv.h>
33# else
34// The weak getauxval definition allows to check for the function at runtime.
35// This is useful for Android, when compiled at a lower API level yet running
36// on a more recent platform that offers the function.
37extern "C" SANITIZER_WEAK_ATTRIBUTE unsigned long getauxval(unsigned long type);
38# endif
39
40#elif SANITIZER_NETBSD
41
42#define SANITIZER_USE_GETAUXVAL 1
43
44#include <dlfcn.h>
45#include <elf.h>
46
47static inline decltype(AuxInfo::a_v) getauxval(decltype(AuxInfo::a_type) type) {
48  for (const AuxInfo *aux = (const AuxInfo *)_dlauxinfo();
49       aux->a_type != AT_NULL; ++aux) {
50    if (type == aux->a_type)
51      return aux->a_v;
52  }
53
54  return 0;
55}
56
57#endif
58
59#endif // SANITIZER_GETAUXVAL_H
60