Global.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (C) 2018 The Android Open Source Project
  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. * http://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. #include <stdint.h>
  17. #include <string.h>
  18. #include <sys/mman.h>
  19. #include <string>
  20. #include <vector>
  21. #include <unwindstack/Global.h>
  22. #include <unwindstack/MapInfo.h>
  23. #include <unwindstack/Maps.h>
  24. #include <unwindstack/Memory.h>
  25. namespace unwindstack {
  26. Global::Global(std::shared_ptr<Memory>& memory) : memory_(memory) {}
  27. Global::Global(std::shared_ptr<Memory>& memory, std::vector<std::string>& search_libs)
  28. : memory_(memory), search_libs_(search_libs) {}
  29. void Global::SetArch(ArchEnum arch) {
  30. if (arch_ == ARCH_UNKNOWN) {
  31. arch_ = arch;
  32. ProcessArch();
  33. }
  34. }
  35. uint64_t Global::GetVariableOffset(MapInfo* info, const std::string& variable) {
  36. if (!search_libs_.empty()) {
  37. bool found = false;
  38. const char* lib = basename(info->name.c_str());
  39. for (const std::string& name : search_libs_) {
  40. if (name == lib) {
  41. found = true;
  42. break;
  43. }
  44. }
  45. if (!found) {
  46. return 0;
  47. }
  48. }
  49. Elf* elf = info->GetElf(memory_, arch());
  50. uint64_t ptr;
  51. // Find first non-empty list (libraries might be loaded multiple times).
  52. if (elf->GetGlobalVariable(variable, &ptr) && ptr != 0) {
  53. return ptr + info->start;
  54. }
  55. return 0;
  56. }
  57. void Global::FindAndReadVariable(Maps* maps, const char* var_str) {
  58. std::string variable(var_str);
  59. // When looking for global variables, do not arbitrarily search every
  60. // readable map. Instead look for a specific pattern that must exist.
  61. // The pattern should be a readable map, followed by a read-write
  62. // map with a non-zero offset.
  63. // For example:
  64. // f0000-f1000 0 r-- /system/lib/libc.so
  65. // f1000-f2000 1000 r-x /system/lib/libc.so
  66. // f2000-f3000 2000 rw- /system/lib/libc.so
  67. // This also works:
  68. // f0000-f2000 0 r-- /system/lib/libc.so
  69. // f2000-f3000 2000 rw- /system/lib/libc.so
  70. MapInfo* map_start = nullptr;
  71. for (const auto& info : *maps) {
  72. if (map_start != nullptr) {
  73. if (map_start->name == info->name) {
  74. if (info->offset != 0 &&
  75. (info->flags & (PROT_READ | PROT_WRITE)) == (PROT_READ | PROT_WRITE)) {
  76. uint64_t ptr = GetVariableOffset(map_start, variable);
  77. if (ptr != 0 && ReadVariableData(ptr)) {
  78. break;
  79. } else {
  80. // Failed to find the global variable, do not bother trying again.
  81. map_start = nullptr;
  82. }
  83. }
  84. } else {
  85. map_start = nullptr;
  86. }
  87. }
  88. if (map_start == nullptr && (info->flags & PROT_READ) && info->offset == 0 &&
  89. !info->name.empty()) {
  90. map_start = info.get();
  91. }
  92. }
  93. }
  94. } // namespace unwindstack