• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 <elf.h>
18 #include <stdint.h>
19 
20 #include <string>
21 
22 #include <unwindstack/Memory.h>
23 
24 #include "Check.h"
25 #include "Symbols.h"
26 
27 namespace unwindstack {
28 
Symbols(uint64_t offset,uint64_t size,uint64_t entry_size,uint64_t str_offset,uint64_t str_size)29 Symbols::Symbols(uint64_t offset, uint64_t size, uint64_t entry_size, uint64_t str_offset,
30                  uint64_t str_size)
31     : cur_offset_(offset),
32       offset_(offset),
33       end_(offset + size),
34       entry_size_(entry_size),
35       str_offset_(str_offset),
36       str_end_(str_offset_ + str_size) {}
37 
GetInfoFromCache(uint64_t addr)38 const Symbols::Info* Symbols::GetInfoFromCache(uint64_t addr) {
39   // Binary search the table.
40   size_t first = 0;
41   size_t last = symbols_.size();
42   while (first < last) {
43     size_t current = first + (last - first) / 2;
44     const Info* info = &symbols_[current];
45     if (addr < info->start_offset) {
46       last = current;
47     } else if (addr < info->end_offset) {
48       return info;
49     } else {
50       first = current + 1;
51     }
52   }
53   return nullptr;
54 }
55 
56 template <typename SymType>
GetName(uint64_t addr,uint64_t load_bias,Memory * elf_memory,std::string * name,uint64_t * func_offset)57 bool Symbols::GetName(uint64_t addr, uint64_t load_bias, Memory* elf_memory, std::string* name,
58                       uint64_t* func_offset) {
59   addr += load_bias;
60 
61   if (symbols_.size() != 0) {
62     const Info* info = GetInfoFromCache(addr);
63     if (info) {
64       CHECK(addr >= info->start_offset && addr <= info->end_offset);
65       *func_offset = addr - info->start_offset;
66       return elf_memory->ReadString(info->str_offset, name, str_end_ - info->str_offset);
67     }
68   }
69 
70   bool symbol_added = false;
71   bool return_value = false;
72   while (cur_offset_ + entry_size_ <= end_) {
73     SymType entry;
74     if (!elf_memory->ReadFully(cur_offset_, &entry, sizeof(entry))) {
75       // Stop all processing, something looks like it is corrupted.
76       cur_offset_ = UINT64_MAX;
77       return false;
78     }
79     cur_offset_ += entry_size_;
80 
81     if (entry.st_shndx != SHN_UNDEF && ELF32_ST_TYPE(entry.st_info) == STT_FUNC) {
82       // Treat st_value as virtual address.
83       uint64_t start_offset = entry.st_value;
84       if (entry.st_shndx != SHN_ABS) {
85         start_offset += load_bias;
86       }
87       uint64_t end_offset = start_offset + entry.st_size;
88 
89       // Cache the value.
90       symbols_.emplace_back(start_offset, end_offset, str_offset_ + entry.st_name);
91       symbol_added = true;
92 
93       if (addr >= start_offset && addr < end_offset) {
94         *func_offset = addr - start_offset;
95         uint64_t offset = str_offset_ + entry.st_name;
96         if (offset < str_end_) {
97           return_value = elf_memory->ReadString(offset, name, str_end_ - offset);
98         }
99         break;
100       }
101     }
102   }
103 
104   if (symbol_added) {
105     std::sort(symbols_.begin(), symbols_.end(),
106               [](const Info& a, const Info& b) { return a.start_offset < b.start_offset; });
107   }
108   return return_value;
109 }
110 
111 template <typename SymType>
GetGlobal(Memory * elf_memory,const std::string & name,uint64_t * memory_address)112 bool Symbols::GetGlobal(Memory* elf_memory, const std::string& name, uint64_t* memory_address) {
113   uint64_t cur_offset = offset_;
114   while (cur_offset + entry_size_ <= end_) {
115     SymType entry;
116     if (!elf_memory->ReadFully(cur_offset, &entry, sizeof(entry))) {
117       return false;
118     }
119     cur_offset += entry_size_;
120 
121     if (entry.st_shndx != SHN_UNDEF && ELF32_ST_TYPE(entry.st_info) == STT_OBJECT &&
122         ELF32_ST_BIND(entry.st_info) == STB_GLOBAL) {
123       uint64_t str_offset = str_offset_ + entry.st_name;
124       if (str_offset < str_end_) {
125         std::string symbol;
126         if (elf_memory->ReadString(str_offset, &symbol, str_end_ - str_offset) && symbol == name) {
127           *memory_address = entry.st_value;
128           return true;
129         }
130       }
131     }
132   }
133   return false;
134 }
135 
136 // Instantiate all of the needed template functions.
137 template bool Symbols::GetName<Elf32_Sym>(uint64_t, uint64_t, Memory*, std::string*, uint64_t*);
138 template bool Symbols::GetName<Elf64_Sym>(uint64_t, uint64_t, Memory*, std::string*, uint64_t*);
139 
140 template bool Symbols::GetGlobal<Elf32_Sym>(Memory*, const std::string&, uint64_t*);
141 template bool Symbols::GetGlobal<Elf64_Sym>(Memory*, const std::string&, uint64_t*);
142 }  // namespace unwindstack
143