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