blob: ce2b999959449cc84d73fb09bbd1bb5025ad135d [file] [log] [blame]
Christopher Ferris63860cb2015-11-16 17:30:32 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <ctype.h>
30#include <elf.h>
31#include <inttypes.h>
32#include <link.h>
33#include <stdio.h>
34#include <string.h>
35#include <stdlib.h>
36
37#include <vector>
38
39#include "debug_disable.h"
40#include "MapData.h"
41
42// Format of /proc/<PID>/maps:
43// 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so
44static MapEntry* parse_line(char* line) {
45 uintptr_t start;
46 uintptr_t end;
47 uintptr_t offset;
48 char permissions[4];
49 int name_pos;
50 if (sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %4s %" PRIxPTR " %*x:%*x %*d %n", &start,
51 &end, permissions, &offset, &name_pos) < 2) {
52 return nullptr;
53 }
54
55 const char* name = line + name_pos;
56 size_t name_len = strlen(name);
57 if (name_len && name[name_len - 1] == '\n') {
58 name_len -= 1;
59 }
60
61 MapEntry* entry = new MapEntry(start, end, offset, name, name_len);
62 if (permissions[0] != 'r') {
63 // Any unreadable map will just get a zero load base.
64 entry->load_base = 0;
65 entry->load_base_read = true;
66 }
67 return entry;
68}
69
70template<typename T>
71static inline bool get_val(MapEntry* entry, uintptr_t addr, T* store) {
72 if (addr < entry->start || addr + sizeof(T) > entry->end) {
73 return false;
74 }
75 // Make sure the address is aligned properly.
76 if (addr & (sizeof(T)-1)) {
77 return false;
78 }
79 *store = *reinterpret_cast<T*>(addr);
80 return true;
81}
82
83static void read_loadbase(MapEntry* entry) {
84 entry->load_base = 0;
85 entry->load_base_read = true;
86 uintptr_t addr = entry->start;
87 ElfW(Ehdr) ehdr;
88 if (!get_val<ElfW(Half)>(entry, addr + offsetof(ElfW(Ehdr), e_phnum), &ehdr.e_phnum)) {
89 return;
90 }
91 if (!get_val<ElfW(Off)>(entry, addr + offsetof(ElfW(Ehdr), e_phoff), &ehdr.e_phoff)) {
92 return;
93 }
94 addr += ehdr.e_phoff;
95 for (size_t i = 0; i < ehdr.e_phnum; i++) {
96 ElfW(Phdr) phdr;
97 if (!get_val<ElfW(Word)>(entry, addr + offsetof(ElfW(Phdr), p_type), &phdr.p_type)) {
98 return;
99 }
100 if (!get_val<ElfW(Off)>(entry, addr + offsetof(ElfW(Phdr), p_offset), &phdr.p_offset)) {
101 return;
102 }
103 if (phdr.p_type == PT_LOAD && phdr.p_offset == entry->offset) {
104 if (!get_val<ElfW(Addr)>(entry, addr + offsetof(ElfW(Phdr), p_vaddr), &phdr.p_vaddr)) {
105 return;
106 }
107 entry->load_base = phdr.p_vaddr;
108 return;
109 }
110 addr += sizeof(phdr);
111 }
112}
113
114bool MapData::Initialize() {
115 ScopedDisableDebugCalls disable;
116
117 FILE* fp = fopen("/proc/self/maps", "re");
118 if (fp == nullptr) {
119 return false;
120 }
121
122 std::vector<char> buffer(1024);
123 while (fgets(buffer.data(), buffer.size(), fp) != nullptr) {
124 MapEntry* entry = parse_line(buffer.data());
125 if (entry == nullptr) {
126 return false;
127 }
128 entries_.push_back(entry);
129 }
130 fclose(fp);
131 return true;
132}
133
134MapData* MapData::Create() {
135 MapData* maps = new MapData();
136 if (!maps->Initialize()) {
137 delete maps;
138 return nullptr;
139 }
140 return maps;
141}
142
143MapData::~MapData() {
144 ScopedDisableDebugCalls disable;
145
146 for (auto* entry : entries_) {
147 delete entry;
148 }
149 entries_.clear();
150}
151
152// Find the containing map info for the PC.
153const MapEntry* MapData::find(uintptr_t pc, uintptr_t* rel_pc) {
154 for (auto* entry : entries_) {
155 if ((pc >= entry->start) && (pc < entry->end)) {
156 if (!entry->load_base_read) {
157 read_loadbase(entry);
158 }
159 if (rel_pc) {
160 *rel_pc = pc - entry->start + entry->load_base;
161 }
162 return entry;
163 }
164 }
165 if (rel_pc) {
166 *rel_pc = pc;
167 }
168 return nullptr;
169}