| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 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 |  | 
|  | 17 | #include <stdint.h> | 
| Florian Mayer | 3f1f2e0 | 2018-10-23 15:56:28 +0100 | [diff] [blame] | 18 | #include <string.h> | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 19 | #include <sys/mman.h> | 
|  | 20 |  | 
|  | 21 | #include <string> | 
|  | 22 | #include <vector> | 
|  | 23 |  | 
|  | 24 | #include <unwindstack/Global.h> | 
|  | 25 | #include <unwindstack/MapInfo.h> | 
|  | 26 | #include <unwindstack/Maps.h> | 
|  | 27 | #include <unwindstack/Memory.h> | 
|  | 28 |  | 
|  | 29 | namespace unwindstack { | 
|  | 30 |  | 
|  | 31 | Global::Global(std::shared_ptr<Memory>& memory) : memory_(memory) {} | 
|  | 32 | Global::Global(std::shared_ptr<Memory>& memory, std::vector<std::string>& search_libs) | 
|  | 33 | : memory_(memory), search_libs_(search_libs) {} | 
|  | 34 |  | 
| Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 35 | void Global::SetArch(ArchEnum arch) { | 
|  | 36 | if (arch_ == ARCH_UNKNOWN) { | 
|  | 37 | arch_ = arch; | 
|  | 38 | ProcessArch(); | 
|  | 39 | } | 
|  | 40 | } | 
|  | 41 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 42 | bool Global::Searchable(const std::string& name) { | 
|  | 43 | if (search_libs_.empty()) { | 
|  | 44 | return true; | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 47 | if (name.empty()) { | 
|  | 48 | return false; | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 49 | } | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 50 |  | 
|  | 51 | const char* base_name = basename(name.c_str()); | 
|  | 52 | for (const std::string& lib : search_libs_) { | 
|  | 53 | if (base_name == lib) { | 
|  | 54 | return true; | 
|  | 55 | } | 
|  | 56 | } | 
|  | 57 | return false; | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 58 | } | 
|  | 59 |  | 
|  | 60 | void Global::FindAndReadVariable(Maps* maps, const char* var_str) { | 
|  | 61 | std::string variable(var_str); | 
|  | 62 | // When looking for global variables, do not arbitrarily search every | 
|  | 63 | // readable map. Instead look for a specific pattern that must exist. | 
|  | 64 | // The pattern should be a readable map, followed by a read-write | 
|  | 65 | // map with a non-zero offset. | 
|  | 66 | // For example: | 
|  | 67 | //   f0000-f1000 0 r-- /system/lib/libc.so | 
|  | 68 | //   f1000-f2000 1000 r-x /system/lib/libc.so | 
|  | 69 | //   f2000-f3000 2000 rw- /system/lib/libc.so | 
|  | 70 | // This also works: | 
|  | 71 | //   f0000-f2000 0 r-- /system/lib/libc.so | 
|  | 72 | //   f2000-f3000 2000 rw- /system/lib/libc.so | 
|  | 73 | MapInfo* map_start = nullptr; | 
| Florian Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame] | 74 | for (const auto& info : *maps) { | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 75 | if (map_start != nullptr && map_start->name == info->name) { | 
|  | 76 | if (info->offset != 0 && | 
|  | 77 | (info->flags & (PROT_READ | PROT_WRITE)) == (PROT_READ | PROT_WRITE)) { | 
|  | 78 | Elf* elf = map_start->GetElf(memory_, arch()); | 
|  | 79 | uint64_t ptr; | 
|  | 80 | if (elf->GetGlobalVariableOffset(variable, &ptr) && ptr != 0) { | 
|  | 81 | uint64_t offset_end = info->offset + info->end - info->start; | 
|  | 82 | if (ptr >= info->offset && ptr < offset_end) { | 
|  | 83 | ptr = info->start + ptr - info->offset; | 
|  | 84 | if (ReadVariableData(ptr)) { | 
|  | 85 | break; | 
|  | 86 | } | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 87 | } | 
|  | 88 | } | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 89 | map_start = nullptr; | 
|  | 90 | } | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 91 | } else { | 
|  | 92 | map_start = nullptr; | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 93 | } | 
|  | 94 | if (map_start == nullptr && (info->flags & PROT_READ) && info->offset == 0 && | 
| Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 95 | Searchable(info->name)) { | 
| Florian Mayer | 3d67d34 | 2019-02-27 18:00:37 +0000 | [diff] [blame] | 96 | map_start = info.get(); | 
| Christopher Ferris | 56d0e07 | 2018-10-17 10:57:53 -0700 | [diff] [blame] | 97 | } | 
|  | 98 | } | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | }  // namespace unwindstack |