1//
2// Copyright 2017 The Abseil Authors.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// https://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17// Allow dynamic symbol lookup in the kernel VDSO page.
18//
19// VDSO stands for "Virtual Dynamic Shared Object" -- a page of
20// executable code, which looks like a shared library, but doesn't
21// necessarily exist anywhere on disk, and which gets mmap()ed into
22// every process by kernels which support VDSO, such as 2.6.x for 32-bit
23// executables, and 2.6.24 and above for 64-bit executables.
24//
25// More details could be found here:
26// http://www.trilithium.com/johan/2005/08/linux-gate/
27//
28// VDSOSupport -- a class representing kernel VDSO (if present).
29//
30// Example usage:
31// VDSOSupport vdso;
32// VDSOSupport::SymbolInfo info;
33// typedef (*FN)(unsigned *, void *, void *);
34// FN fn = nullptr;
35// if (vdso.LookupSymbol("__vdso_getcpu", "LINUX_2.6", STT_FUNC, &info)) {
36// fn = reinterpret_cast<FN>(info.address);
37// }
38
39#ifndef ABSL_DEBUGGING_INTERNAL_VDSO_SUPPORT_H_
40#define ABSL_DEBUGGING_INTERNAL_VDSO_SUPPORT_H_
41
42#include <atomic>
43
44#include "absl/base/attributes.h"
45#include "absl/debugging/internal/elf_mem_image.h"
46
47#ifdef ABSL_HAVE_ELF_MEM_IMAGE
48
49#ifdef ABSL_HAVE_VDSO_SUPPORT
50#error ABSL_HAVE_VDSO_SUPPORT cannot be directly set
51#else
52#define ABSL_HAVE_VDSO_SUPPORT 1
53#endif
54
55namespace absl {
56namespace debugging_internal {
57
58// NOTE: this class may be used from within tcmalloc, and can not
59// use any memory allocation routines.
60class VDSOSupport {
61 public:
62 VDSOSupport();
63
64 typedef ElfMemImage::SymbolInfo SymbolInfo;
65 typedef ElfMemImage::SymbolIterator SymbolIterator;
66
67 // On PowerPC64 VDSO symbols can either be of type STT_FUNC or STT_NOTYPE
68 // depending on how the kernel is built. The kernel is normally built with
69 // STT_NOTYPE type VDSO symbols. Let's make things simpler first by using a
70 // compile-time constant.
71#ifdef __powerpc64__
72 enum { kVDSOSymbolType = STT_NOTYPE };
73#else
74 enum { kVDSOSymbolType = STT_FUNC };
75#endif
76
77 // Answers whether we have a vdso at all.
78 bool IsPresent() const { return image_.IsPresent(); }
79
80 // Allow to iterate over all VDSO symbols.
81 SymbolIterator begin() const { return image_.begin(); }
82 SymbolIterator end() const { return image_.end(); }
83
84 // Look up versioned dynamic symbol in the kernel VDSO.
85 // Returns false if VDSO is not present, or doesn't contain given
86 // symbol/version/type combination.
87 // If info_out != nullptr, additional details are filled in.
88 bool LookupSymbol(const char *name, const char *version,
89 int symbol_type, SymbolInfo *info_out) const;
90
91 // Find info about symbol (if any) which overlaps given address.
92 // Returns true if symbol was found; false if VDSO isn't present
93 // or doesn't have a symbol overlapping given address.
94 // If info_out != nullptr, additional details are filled in.
95 bool LookupSymbolByAddress(const void *address, SymbolInfo *info_out) const;
96
97 // Used only for testing. Replace real VDSO base with a mock.
98 // Returns previous value of vdso_base_. After you are done testing,
99 // you are expected to call SetBase() with previous value, in order to
100 // reset state to the way it was.
101 const void *SetBase(const void *s);
102
103 // Computes vdso_base_ and returns it. Should be called as early as
104 // possible; before any thread creation, chroot or setuid.
105 static const void *Init();
106
107 private:
108 // image_ represents VDSO ELF image in memory.
109 // image_.ehdr_ == nullptr implies there is no VDSO.
110 ElfMemImage image_;
111
112 // Cached value of auxv AT_SYSINFO_EHDR, computed once.
113 // This is a tri-state:
114 // kInvalidBase => value hasn't been determined yet.
115 // 0 => there is no VDSO.
116 // else => vma of VDSO Elf{32,64}_Ehdr.
117 //
118 // When testing with mock VDSO, low bit is set.
119 // The low bit is always available because vdso_base_ is
120 // page-aligned.
121 static std::atomic<const void *> vdso_base_;
122
123 // NOLINT on 'long' because these routines mimic kernel api.
124 // The 'cache' parameter may be used by some versions of the kernel,
125 // and should be nullptr or point to a static buffer containing at
126 // least two 'long's.
127 static long InitAndGetCPU(unsigned *cpu, void *cache, // NOLINT 'long'.
128 void *unused);
129 static long GetCPUViaSyscall(unsigned *cpu, void *cache, // NOLINT 'long'.
130 void *unused);
131 typedef long (*GetCpuFn)(unsigned *cpu, void *cache, // NOLINT 'long'.
132 void *unused);
133
134 // This function pointer may point to InitAndGetCPU,
135 // GetCPUViaSyscall, or __vdso_getcpu at different stages of initialization.
136 ABSL_CONST_INIT static std::atomic<GetCpuFn> getcpu_fn_;
137
138 friend int GetCPU(void); // Needs access to getcpu_fn_.
139
140 VDSOSupport(const VDSOSupport&) = delete;
141 VDSOSupport& operator=(const VDSOSupport&) = delete;
142};
143
144// Same as sched_getcpu() on later glibc versions.
145// Return current CPU, using (fast) __vdso_getcpu@LINUX_2.6 if present,
146// otherwise use syscall(SYS_getcpu,...).
147// May return -1 with errno == ENOSYS if the kernel doesn't
148// support SYS_getcpu.
149int GetCPU();
150
151} // namespace debugging_internal
152} // namespace absl
153
154#endif // ABSL_HAVE_ELF_MEM_IMAGE
155
156#endif // ABSL_DEBUGGING_INTERNAL_VDSO_SUPPORT_H_
157