blob: 7a3de01d2c1587fa630ec8e4b0295bfd218ed030 [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
Christopher Ferris4568f4b2018-10-23 17:42:41 -070034void Global::SetArch(ArchEnum arch) {
35 if (arch_ == ARCH_UNKNOWN) {
36 arch_ = arch;
37 ProcessArch();
38 }
39}
40
Christopher Ferris56d0e072018-10-17 10:57:53 -070041uint64_t Global::GetVariableOffset(MapInfo* info, const std::string& variable) {
42 if (!search_libs_.empty()) {
43 bool found = false;
44 const char* lib = basename(info->name.c_str());
45 for (const std::string& name : search_libs_) {
46 if (name == lib) {
47 found = true;
48 break;
49 }
50 }
51 if (!found) {
52 return 0;
53 }
54 }
55
Christopher Ferris4568f4b2018-10-23 17:42:41 -070056 Elf* elf = info->GetElf(memory_, arch());
Christopher Ferris56d0e072018-10-17 10:57:53 -070057 uint64_t ptr;
58 // Find first non-empty list (libraries might be loaded multiple times).
59 if (elf->GetGlobalVariable(variable, &ptr) && ptr != 0) {
60 return ptr + info->start;
61 }
62 return 0;
63}
64
65void Global::FindAndReadVariable(Maps* maps, const char* var_str) {
66 std::string variable(var_str);
67 // When looking for global variables, do not arbitrarily search every
68 // readable map. Instead look for a specific pattern that must exist.
69 // The pattern should be a readable map, followed by a read-write
70 // map with a non-zero offset.
71 // For example:
72 // f0000-f1000 0 r-- /system/lib/libc.so
73 // f1000-f2000 1000 r-x /system/lib/libc.so
74 // f2000-f3000 2000 rw- /system/lib/libc.so
75 // This also works:
76 // f0000-f2000 0 r-- /system/lib/libc.so
77 // f2000-f3000 2000 rw- /system/lib/libc.so
78 MapInfo* map_start = nullptr;
79 for (MapInfo* info : *maps) {
80 if (map_start != nullptr) {
81 if (map_start->name == info->name) {
82 if (info->offset != 0 &&
83 (info->flags & (PROT_READ | PROT_WRITE)) == (PROT_READ | PROT_WRITE)) {
84 uint64_t ptr = GetVariableOffset(map_start, variable);
85 if (ptr != 0 && ReadVariableData(ptr)) {
86 break;
87 } else {
88 // Failed to find the global variable, do not bother trying again.
89 map_start = nullptr;
90 }
91 }
92 } else {
93 map_start = nullptr;
94 }
95 }
96 if (map_start == nullptr && (info->flags & PROT_READ) && info->offset == 0 &&
97 !info->name.empty()) {
98 map_start = info;
99 }
100 }
101}
102
103} // namespace unwindstack