Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 29 | #include <ctype.h> |
Christopher Ferris | 70b6e1d | 2015-07-16 14:49:17 -0700 | [diff] [blame^] | 30 | #include <elf.h> |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 31 | #include <inttypes.h> |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <string.h> |
| 34 | #include <stdlib.h> |
| 35 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 36 | #include "debug_mapinfo.h" |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 37 | #include "malloc_debug_disable.h" |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 38 | |
Christopher Ferris | 70b6e1d | 2015-07-16 14:49:17 -0700 | [diff] [blame^] | 39 | #if defined(__LP64__) |
| 40 | #define Elf_W(x) Elf64_##x |
| 41 | #else |
| 42 | #define Elf_W(x) Elf32_##x |
| 43 | #endif |
| 44 | |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 45 | // Format of /proc/<PID>/maps: |
| 46 | // 6f000000-6f01e000 rwxp 00000000 00:0c 16389419 /system/lib/libcomposer.so |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 47 | static mapinfo_t* parse_maps_line(char* line) { |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 48 | uintptr_t start; |
| 49 | uintptr_t end; |
Christopher Ferris | 70b6e1d | 2015-07-16 14:49:17 -0700 | [diff] [blame^] | 50 | uintptr_t offset; |
| 51 | char permissions[4]; |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 52 | int name_pos; |
Christopher Ferris | 70b6e1d | 2015-07-16 14:49:17 -0700 | [diff] [blame^] | 53 | if (sscanf(line, "%" PRIxPTR "-%" PRIxPTR " %4s %" PRIxPTR " %*x:%*x %*d%n", &start, |
| 54 | &end, permissions, &offset, &name_pos) < 2) { |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 55 | return NULL; |
| 56 | } |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 57 | |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 58 | while (isspace(line[name_pos])) { |
| 59 | name_pos += 1; |
| 60 | } |
| 61 | const char* name = line + name_pos; |
| 62 | size_t name_len = strlen(name); |
| 63 | if (name_len && name[name_len - 1] == '\n') { |
| 64 | name_len -= 1; |
| 65 | } |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 66 | |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 67 | mapinfo_t* mi = reinterpret_cast<mapinfo_t*>(calloc(1, sizeof(mapinfo_t) + name_len + 1)); |
| 68 | if (mi) { |
| 69 | mi->start = start; |
| 70 | mi->end = end; |
Christopher Ferris | 70b6e1d | 2015-07-16 14:49:17 -0700 | [diff] [blame^] | 71 | mi->offset = offset; |
| 72 | if (permissions[0] != 'r') { |
| 73 | // Any unreadable map will just get a zero load base. |
| 74 | mi->load_base = 0; |
| 75 | mi->load_base_read = true; |
| 76 | } else { |
| 77 | mi->load_base_read = false; |
| 78 | } |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 79 | memcpy(mi->name, name, name_len); |
| 80 | mi->name[name_len] = '\0'; |
| 81 | } |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 82 | return mi; |
| 83 | } |
| 84 | |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 85 | __LIBC_HIDDEN__ mapinfo_t* mapinfo_create(pid_t pid) { |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 86 | ScopedDisableDebugCalls disable; |
| 87 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 88 | struct mapinfo_t* milist = NULL; |
| 89 | char data[1024]; // Used to read lines as well as to construct the filename. |
| 90 | snprintf(data, sizeof(data), "/proc/%d/maps", pid); |
Elliott Hughes | c674edb | 2014-08-26 15:56:54 -0700 | [diff] [blame] | 91 | FILE* fp = fopen(data, "re"); |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 92 | if (fp != NULL) { |
| 93 | while (fgets(data, sizeof(data), fp) != NULL) { |
| 94 | mapinfo_t* mi = parse_maps_line(data); |
| 95 | if (mi) { |
| 96 | mi->next = milist; |
| 97 | milist = mi; |
| 98 | } |
| 99 | } |
| 100 | fclose(fp); |
| 101 | } |
| 102 | return milist; |
| 103 | } |
| 104 | |
| 105 | __LIBC_HIDDEN__ void mapinfo_destroy(mapinfo_t* mi) { |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 106 | ScopedDisableDebugCalls disable; |
| 107 | |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 108 | while (mi != NULL) { |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 109 | mapinfo_t* del = mi; |
| 110 | mi = mi->next; |
Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 111 | free(del); |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Christopher Ferris | 70b6e1d | 2015-07-16 14:49:17 -0700 | [diff] [blame^] | 115 | template<typename T> |
| 116 | static inline bool get_val(mapinfo_t* mi, uintptr_t addr, T* store) { |
| 117 | if (addr < mi->start || addr + sizeof(T) > mi->end) { |
| 118 | return false; |
| 119 | } |
| 120 | // Make sure the address is aligned properly. |
| 121 | if (addr & (sizeof(T)-1)) { |
| 122 | return false; |
| 123 | } |
| 124 | *store = *reinterpret_cast<T*>(addr); |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | __LIBC_HIDDEN__ void mapinfo_read_loadbase(mapinfo_t* mi) { |
| 129 | mi->load_base = 0; |
| 130 | mi->load_base_read = true; |
| 131 | uintptr_t addr = mi->start; |
| 132 | Elf_W(Ehdr) ehdr; |
| 133 | if (!get_val<Elf_W(Half)>(mi, addr + offsetof(Elf_W(Ehdr), e_phnum), &ehdr.e_phnum)) { |
| 134 | return; |
| 135 | } |
| 136 | if (!get_val<Elf_W(Off)>(mi, addr + offsetof(Elf_W(Ehdr), e_phoff), &ehdr.e_phoff)) { |
| 137 | return; |
| 138 | } |
| 139 | addr += ehdr.e_phoff; |
| 140 | for (size_t i = 0; i < ehdr.e_phnum; i++) { |
| 141 | Elf_W(Phdr) phdr; |
| 142 | if (!get_val<Elf_W(Word)>(mi, addr + offsetof(Elf_W(Phdr), p_type), &phdr.p_type)) { |
| 143 | return; |
| 144 | } |
| 145 | if (!get_val<Elf_W(Off)>(mi, addr + offsetof(Elf_W(Phdr), p_offset), &phdr.p_offset)) { |
| 146 | return; |
| 147 | } |
| 148 | if (phdr.p_type == PT_LOAD && phdr.p_offset == mi->offset) { |
| 149 | if (!get_val<Elf_W(Addr)>(mi, addr + offsetof(Elf_W(Phdr), p_vaddr), &phdr.p_vaddr)) { |
| 150 | return; |
| 151 | } |
| 152 | mi->load_base = phdr.p_vaddr; |
| 153 | return; |
| 154 | } |
| 155 | addr += sizeof(phdr); |
| 156 | } |
| 157 | } |
| 158 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 159 | // Find the containing map info for the PC. |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 160 | __LIBC_HIDDEN__ const mapinfo_t* mapinfo_find(mapinfo_t* mi, uintptr_t pc, uintptr_t* rel_pc) { |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 161 | for (; mi != NULL; mi = mi->next) { |
| 162 | if ((pc >= mi->start) && (pc < mi->end)) { |
Christopher Ferris | 70b6e1d | 2015-07-16 14:49:17 -0700 | [diff] [blame^] | 163 | if (!mi->load_base_read) { |
| 164 | mapinfo_read_loadbase(mi); |
| 165 | } |
| 166 | *rel_pc = pc - mi->start + mi->load_base; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 167 | return mi; |
| 168 | } |
| 169 | } |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 170 | *rel_pc = pc; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 171 | return NULL; |
| 172 | } |