vdso_support.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (c) 2008, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // ---
  30. // Author: Paul Pluzhnikov
  31. //
  32. // Allow dynamic symbol lookup in the kernel VDSO page.
  33. //
  34. // VDSO stands for "Virtual Dynamic Shared Object" -- a page of
  35. // executable code, which looks like a shared library, but doesn't
  36. // necessarily exist anywhere on disk, and which gets mmap()ed into
  37. // every process by kernels which support VDSO, such as 2.6.x for 32-bit
  38. // executables, and 2.6.24 and above for 64-bit executables.
  39. //
  40. // More details could be found here:
  41. // http://www.trilithium.com/johan/2005/08/linux-gate/
  42. //
  43. // VDSOSupport -- a class representing kernel VDSO (if present).
  44. //
  45. // Example usage:
  46. // VDSOSupport vdso;
  47. // VDSOSupport::SymbolInfo info;
  48. // typedef (*FN)(unsigned *, void *, void *);
  49. // FN fn = NULL;
  50. // if (vdso.LookupSymbol("__vdso_getcpu", "LINUX_2.6", STT_FUNC, &info)) {
  51. // fn = reinterpret_cast<FN>(info.address);
  52. // }
  53. #ifndef BASE_VDSO_SUPPORT_H_
  54. #define BASE_VDSO_SUPPORT_H_
  55. #include <config.h>
  56. #include "base/basictypes.h"
  57. #include "base/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. // kInvalidBase => value hasn't been determined yet.
  100. // 0 => 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_