Debian Codegraph @

extra/gperftools/gperftools-2.15/src/base/vdso_support.h

raw
1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 // Copyright (c) 2008, Google Inc.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // ---
32 // Author: Paul Pluzhnikov
33 //
34 // Allow dynamic symbol lookup in the kernel VDSO page.
35 //
36 // VDSO stands for "Virtual Dynamic Shared Object" -- a page of
37 // executable code, which looks like a shared library, but doesn't
38 // necessarily exist anywhere on disk, and which gets mmap()ed into
39 // every process by kernels which support VDSO, such as 2.6.x for 32-bit
40 // executables, and 2.6.24 and above for 64-bit executables.
41 //
42 // More details could be found here:
43 // http://www.trilithium.com/johan/2005/08/linux-gate/
44 //
45 // VDSOSupport -- a class representing kernel VDSO (if present).
46 //
47 // Example usage:
48 // VDSOSupport vdso;
49 // VDSOSupport::SymbolInfo info;
50 // typedef (*FN)(unsigned *, void *, void *);
51 // FN fn = NULL;
52 // if (vdso.LookupSymbol("__vdso_getcpu", "LINUX_2.6", STT_FUNC, &info)) {
53 // fn = reinterpret_cast<FN>(info.address);
54 // }
55
56 #ifndef BASE_VDSO_SUPPORT_H_
57 #define BASE_VDSO_SUPPORT_H_
58
59 #include <config.h>
60 #include "base/basictypes.h"
61 #include "base/elf_mem_image.h"
62
63 #ifdef HAVE_ELF_MEM_IMAGE
64
65 // Enable VDSO support only for the architectures/operating systems that
66 // support it.
67 #if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
68 #define HAVE_VDSO_SUPPORT 1
69 #endif
70
71 #include <stdlib.h> // for NULL
72
73 namespace base {
74
75 // NOTE: this class may be used from within tcmalloc, and can not
76 // use any memory allocation routines.
77 class VDSOSupport {
78 public:
79 VDSOSupport();
80
81 typedef ElfMemImage::SymbolInfo SymbolInfo;
82 typedef ElfMemImage::SymbolIterator SymbolIterator;
83
84 // Answers whether we have a vdso at all.
85 bool IsPresent() const { return image_.IsPresent(); }
86
87 // Allow to iterate over all VDSO symbols.
88 SymbolIterator begin() const { return image_.begin(); }
89 SymbolIterator end() const { return image_.end(); }
90
91 // Look up versioned dynamic symbol in the kernel VDSO.
92 // Returns false if VDSO is not present, or doesn't contain given
93 // symbol/version/type combination.
94 // If info_out != NULL, additional details are filled in.
95 bool LookupSymbol(const char *name, const char *version,
96 int symbol_type, SymbolInfo *info_out) const;
97
98 // Find info about symbol (if any) which overlaps given address.
99 // Returns true if symbol was found; false if VDSO isn't present
100 // or doesn't have a symbol overlapping given address.
101 // If info_out != NULL, additional details are filled in.
102 bool LookupSymbolByAddress(const void *address, SymbolInfo *info_out) const;
103
104 // Used only for testing. Replace real VDSO base with a mock.
105 // Returns previous value of vdso_base_. After you are done testing,
106 // you are expected to call SetBase() with previous value, in order to
107 // reset state to the way it was.
108 const void *SetBase(const void *s);
109
110 // Computes vdso_base_ and returns it. Should be called as early as
111 // possible; before any thread creation, chroot or setuid.
112 static const void *Init();
113
114 private:
115 // image_ represents VDSO ELF image in memory.
116 // image_.ehdr_ == NULL implies there is no VDSO.
117 ElfMemImage image_;
118
119 // Cached value of auxv AT_SYSINFO_EHDR, computed once.
120 // This is a tri-state:
121 // kInvalidBase => value hasn't been determined yet.
122 // 0 => there is no VDSO.
123 // else => vma of VDSO Elf{32,64}_Ehdr.
124 //
125 // When testing with mock VDSO, low bit is set.
126 // The low bit is always available because vdso_base_ is
127 // page-aligned.
128 static const void *vdso_base_;
129
130 DISALLOW_COPY_AND_ASSIGN(VDSOSupport);
131 };
132
133 } // namespace base
134
135 #endif // HAVE_ELF_MEM_IMAGE
136
137 #endif // BASE_VDSO_SUPPORT_H_