vdso_support.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #pragma once
  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. // Author: Paul Pluzhnikov
  32. //
  33. // Allow dynamic symbol lookup in the kernel VDSO page.
  34. //
  35. // VDSO stands for "Virtual Dynamic Shared Object" -- a page of
  36. // executable code, which looks like a shared library, but doesn't
  37. // necessarily exist anywhere on disk, and which gets mmap()ed into
  38. // every process by kernels which support VDSO, such as 2.6.x for 32-bit
  39. // executables, and 2.6.24 and above for 64-bit executables.
  40. //
  41. // More details could be found here:
  42. // http://www.trilithium.com/johan/2005/08/linux-gate/
  43. //
  44. // VDSOSupport -- a class representing kernel VDSO (if present).
  45. //
  46. // Example usage:
  47. // VDSOSupport vdso;
  48. // VDSOSupport::SymbolInfo info;
  49. // typedef (*FN)(unsigned *, void *, void *);
  50. // FN fn = NULL;
  51. // if (vdso.LookupSymbol("__vdso_getcpu", "LINUX_2.6", STT_FUNC, &info)) {
  52. // fn = reinterpret_cast<FN>(info.address);
  53. // }
  54. #ifndef BASE_VDSO_SUPPORT_H_
  55. #define BASE_VDSO_SUPPORT_H_
  56. #include "config.h"
  57. #include "elf_mem_image.h"
  58. #ifdef HAVE_ELF_MEM_IMAGE
  59. #define HAVE_VDSO_SUPPORT 1
  60. #include <stdlib.h> // for NULL
  61. namespace base {
  62. // NOTE: this class may be used from within tcmalloc, and can not
  63. // use any memory allocation routines.
  64. class VDSOSupport {
  65. public:
  66. VDSOSupport();
  67. typedef ElfMemImage::SymbolInfo SymbolInfo;
  68. typedef ElfMemImage::SymbolIterator SymbolIterator;
  69. // Answers whether we have a vdso at all.
  70. bool IsPresent() const { return image_.IsPresent(); }
  71. // Allow to iterate over all VDSO symbols.
  72. SymbolIterator begin() const { return image_.begin(); }
  73. SymbolIterator end() const { return image_.end(); }
  74. // Look up versioned dynamic symbol in the kernel VDSO.
  75. // Returns false if VDSO is not present, or doesn't contain given
  76. // symbol/version/type combination.
  77. // If info_out != NULL, additional details are filled in.
  78. bool LookupSymbol(const char *name, const char *version,
  79. int symbol_type, SymbolInfo *info_out) const;
  80. // Find info about symbol (if any) which overlaps given address.
  81. // Returns true if symbol was found; false if VDSO isn't present
  82. // or doesn't have a symbol overlapping given address.
  83. // If info_out != NULL, additional details are filled in.
  84. bool LookupSymbolByAddress(const void *address, SymbolInfo *info_out) const;
  85. // Used only for testing. Replace real VDSO base with a mock.
  86. // Returns previous value of vdso_base_. After you are done testing,
  87. // you are expected to call SetBase() with previous value, in order to
  88. // reset state to the way it was.
  89. const void *SetBase(const void *s);
  90. // Computes vdso_base_ and returns it. Should be called as early as
  91. // possible; before any thread creation, chroot or setuid.
  92. static const void *Init();
  93. private:
  94. // image_ represents VDSO ELF image in memory.
  95. // image_.ehdr_ == NULL implies there is no VDSO.
  96. ElfMemImage image_;
  97. // Cached value of auxv AT_SYSINFO_EHDR, computed once.
  98. // This is a tri-state:
  99. // 0 => value hasn't been determined yet.
  100. // kInvalidBase => there is no VDSO.
  101. // else => vma of VDSO Elf{32,64}_Ehdr.
  102. //
  103. // When testing with mock VDSO, low bit is set.
  104. // The low bit is always available because vdso_base_ is
  105. // page-aligned.
  106. static const void *vdso_base_;
  107. DISALLOW_COPY_AND_ASSIGN(VDSOSupport);
  108. };
  109. } // namespace base
  110. #endif // HAVE_ELF_MEM_IMAGE
  111. #endif // BASE_VDSO_SUPPORT_H_