blob: edc21875624c2719bc1e58734355ecb9be0ee549 [file] [log] [blame]
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -07001/*
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
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070017#include <elf.h>
18#include <stdint.h>
19
20#include <string>
21
Christopher Ferris94167032017-06-28 18:56:52 -070022#include "Check.h"
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070023#include "Memory.h"
24#include "Symbols.h"
25
26Symbols::Symbols(uint64_t offset, uint64_t size, uint64_t entry_size, uint64_t str_offset,
27 uint64_t str_size)
28 : cur_offset_(offset),
29 offset_(offset),
30 end_(offset + size),
31 entry_size_(entry_size),
32 str_offset_(str_offset),
33 str_end_(str_offset_ + str_size) {}
34
35const Symbols::Info* Symbols::GetInfoFromCache(uint64_t addr) {
36 // Binary search the table.
37 size_t first = 0;
38 size_t last = symbols_.size();
39 while (first < last) {
40 size_t current = first + (last - first) / 2;
41 const Info* info = &symbols_[current];
42 if (addr < info->start_offset) {
43 last = current;
44 } else if (addr < info->end_offset) {
45 return info;
46 } else {
47 first = current + 1;
48 }
49 }
50 return nullptr;
51}
52
53template <typename SymType>
54bool Symbols::GetName(uint64_t addr, uint64_t load_bias, Memory* elf_memory, std::string* name,
55 uint64_t* func_offset) {
56 addr += load_bias;
57
58 if (symbols_.size() != 0) {
59 const Info* info = GetInfoFromCache(addr);
60 if (info) {
Christopher Ferris94167032017-06-28 18:56:52 -070061 CHECK(addr >= info->start_offset && addr <= info->end_offset);
Christopher Ferrise7ba4cc2017-04-04 14:06:58 -070062 *func_offset = addr - info->start_offset;
63 return elf_memory->ReadString(info->str_offset, name, str_end_ - info->str_offset);
64 }
65 }
66
67 bool symbol_added = false;
68 bool return_value = false;
69 while (cur_offset_ + entry_size_ <= end_) {
70 SymType entry;
71 if (!elf_memory->Read(cur_offset_, &entry, sizeof(entry))) {
72 // Stop all processing, something looks like it is corrupted.
73 cur_offset_ = UINT64_MAX;
74 return false;
75 }
76 cur_offset_ += entry_size_;
77
78 if (entry.st_shndx != SHN_UNDEF && ELF32_ST_TYPE(entry.st_info) == STT_FUNC) {
79 // Treat st_value as virtual address.
80 uint64_t start_offset = entry.st_value;
81 if (entry.st_shndx != SHN_ABS) {
82 start_offset += load_bias;
83 }
84 uint64_t end_offset = start_offset + entry.st_size;
85
86 // Cache the value.
87 symbols_.emplace_back(start_offset, end_offset, str_offset_ + entry.st_name);
88 symbol_added = true;
89
90 if (addr >= start_offset && addr < end_offset) {
91 *func_offset = addr - start_offset;
92 uint64_t offset = str_offset_ + entry.st_name;
93 if (offset < str_end_) {
94 return_value = elf_memory->ReadString(offset, name, str_end_ - offset);
95 }
96 break;
97 }
98 }
99 }
100
101 if (symbol_added) {
102 std::sort(symbols_.begin(), symbols_.end(),
103 [](const Info& a, const Info& b) { return a.start_offset < b.start_offset; });
104 }
105 return return_value;
106}
107
108// Instantiate all of the needed template functions.
109template bool Symbols::GetName<Elf32_Sym>(uint64_t, uint64_t, Memory*, std::string*, uint64_t*);
110template bool Symbols::GetName<Elf64_Sym>(uint64_t, uint64_t, Memory*, std::string*, uint64_t*);