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