blob: ee6c8a5938ab003e2a37375e0939a1699ce82d19 [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>
Florian Mayer3f1f2e02018-10-23 15:56:28 +010018#include <string.h>
Christopher Ferris56d0e072018-10-17 10:57:53 -070019#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
29namespace unwindstack {
30
31Global::Global(std::shared_ptr<Memory>& memory) : memory_(memory) {}
32Global::Global(std::shared_ptr<Memory>& memory, std::vector<std::string>& search_libs)
33 : memory_(memory), search_libs_(search_libs) {}
34
Christopher Ferris4568f4b2018-10-23 17:42:41 -070035void Global::SetArch(ArchEnum arch) {
36 if (arch_ == ARCH_UNKNOWN) {
37 arch_ = arch;
38 ProcessArch();
39 }
40}
41
Christopher Ferrisdf683b72019-12-03 17:13:49 -080042bool Global::Searchable(const std::string& name) {
43 if (search_libs_.empty()) {
44 return true;
Christopher Ferris56d0e072018-10-17 10:57:53 -070045 }
46
Christopher Ferrisdf683b72019-12-03 17:13:49 -080047 if (name.empty()) {
48 return false;
Christopher Ferris56d0e072018-10-17 10:57:53 -070049 }
Christopher Ferrisdf683b72019-12-03 17:13:49 -080050
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 Ferris56d0e072018-10-17 10:57:53 -070058}
59
60void 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
Christopher Ferrisde5cd8c2020-01-21 17:56:25 -080073 // It is also possible to see empty maps after the read-only like so:
74 // f0000-f1000 0 r-- /system/lib/libc.so
75 // f1000-f2000 0 ---
76 // f2000-f3000 1000 r-x /system/lib/libc.so
77 // f3000-f4000 2000 rw- /system/lib/libc.so
78 MapInfo* map_zero = nullptr;
Florian Mayer3d67d342019-02-27 18:00:37 +000079 for (const auto& info : *maps) {
Christopher Ferrisde5cd8c2020-01-21 17:56:25 -080080 if (info->offset != 0 && (info->flags & (PROT_READ | PROT_WRITE)) == (PROT_READ | PROT_WRITE) &&
81 map_zero != nullptr && Searchable(info->name) && info->name == map_zero->name) {
82 Elf* elf = map_zero->GetElf(memory_, arch());
83 uint64_t ptr;
84 if (elf->GetGlobalVariableOffset(variable, &ptr) && ptr != 0) {
85 uint64_t offset_end = info->offset + info->end - info->start;
86 if (ptr >= info->offset && ptr < offset_end) {
87 ptr = info->start + ptr - info->offset;
88 if (ReadVariableData(ptr)) {
89 break;
Christopher Ferris56d0e072018-10-17 10:57:53 -070090 }
91 }
Christopher Ferris56d0e072018-10-17 10:57:53 -070092 }
Christopher Ferrisde5cd8c2020-01-21 17:56:25 -080093 } else if (info->offset == 0 && !info->name.empty()) {
94 map_zero = info.get();
Christopher Ferris56d0e072018-10-17 10:57:53 -070095 }
96 }
97}
98
99} // namespace unwindstack