mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 1 | /* libunwind - a platform-independent unwind library |
hp.com!davidm | 07b01ad | 2005-05-20 09:48:08 +0000 | [diff] [blame] | 2 | Copyright (C) 2003-2005 Hewlett-Packard Co |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 3 | Contributed by David Mosberger-Tang <davidm@hpl.hp.com> |
| 4 | |
| 5 | This file is part of libunwind. |
| 6 | |
| 7 | Permission is hereby granted, free of charge, to any person obtaining |
| 8 | a copy of this software and associated documentation files (the |
| 9 | "Software"), to deal in the Software without restriction, including |
| 10 | without limitation the rights to use, copy, modify, merge, publish, |
| 11 | distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | permit persons to whom the Software is furnished to do so, subject to |
| 13 | the following conditions: |
| 14 | |
| 15 | The above copyright notice and this permission notice shall be |
| 16 | included in all copies or substantial portions of the Software. |
| 17 | |
| 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 22 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 24 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 25 | |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 26 | #include <limits.h> |
| 27 | #include <stdio.h> |
| 28 | |
hp.com!davidm | 07b01ad | 2005-05-20 09:48:08 +0000 | [diff] [blame] | 29 | #include "libunwind_i.h" |
Christopher Ferris | 2553e59 | 2015-06-05 17:23:36 -0700 | [diff] [blame] | 30 | #include "libunwind-ptrace.h" |
Christopher Ferris | 7d46a21 | 2013-11-14 14:03:33 -0800 | [diff] [blame] | 31 | #include "map_info.h" |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 32 | #include "os-linux.h" |
| 33 | |
Christopher Ferris | 16b95a6 | 2014-01-22 16:07:26 -0800 | [diff] [blame] | 34 | /* ANDROID support update. */ |
Christopher Ferris | f4a8df5 | 2014-03-07 19:40:06 -0800 | [diff] [blame] | 35 | HIDDEN struct map_info * |
Christopher Ferris | 2553e59 | 2015-06-05 17:23:36 -0700 | [diff] [blame] | 36 | map_create_list (int map_create_type, pid_t pid) |
Christopher Ferris | 7d46a21 | 2013-11-14 14:03:33 -0800 | [diff] [blame] | 37 | { |
| 38 | struct map_iterator mi; |
| 39 | unsigned long start, end, offset, flags; |
| 40 | struct map_info *map_list = NULL; |
| 41 | struct map_info *cur_map; |
Christopher Ferris | 2553e59 | 2015-06-05 17:23:36 -0700 | [diff] [blame] | 42 | unw_addr_space_t as = NULL; |
| 43 | struct unw_addr_space local_as; |
| 44 | void* as_arg = NULL; |
Christopher Ferris | 7d46a21 | 2013-11-14 14:03:33 -0800 | [diff] [blame] | 45 | |
| 46 | if (maps_init (&mi, pid) < 0) |
| 47 | return NULL; |
| 48 | |
| 49 | while (maps_next (&mi, &start, &end, &offset, &flags)) |
| 50 | { |
Christopher Ferris | f4a8df5 | 2014-03-07 19:40:06 -0800 | [diff] [blame] | 51 | cur_map = map_alloc_info (); |
| 52 | if (cur_map == MAP_FAILED) |
Christopher Ferris | 7d46a21 | 2013-11-14 14:03:33 -0800 | [diff] [blame] | 53 | break; |
| 54 | cur_map->next = map_list; |
| 55 | cur_map->start = start; |
| 56 | cur_map->end = end; |
| 57 | cur_map->offset = offset; |
Christopher Ferris | 517b197 | 2014-10-17 10:18:40 -0700 | [diff] [blame] | 58 | cur_map->load_base = 0; |
Christopher Ferris | 7d46a21 | 2013-11-14 14:03:33 -0800 | [diff] [blame] | 59 | cur_map->flags = flags; |
Christopher Ferris | f4a8df5 | 2014-03-07 19:40:06 -0800 | [diff] [blame] | 60 | cur_map->path = strdup (mi.path); |
Christopher Ferris | b8627d9 | 2014-02-19 16:49:00 -0800 | [diff] [blame] | 61 | mutex_init (&cur_map->ei_lock); |
Christopher Ferris | cdf0d03 | 2015-05-04 11:01:39 -0700 | [diff] [blame] | 62 | cur_map->ei.valid = false; |
| 63 | cur_map->ei.load_attempted = false; |
| 64 | cur_map->ei.mapped = false; |
Christopher Ferris | 7d46a21 | 2013-11-14 14:03:33 -0800 | [diff] [blame] | 65 | |
Christopher Ferris | fac2c50 | 2014-09-10 16:26:12 -0700 | [diff] [blame] | 66 | /* Indicate mapped memory of devices is special and should not |
| 67 | be read or written. Use a special flag instead of zeroing the |
| 68 | flags to indicate that the maps do not need to be rebuilt if |
| 69 | any values ever wind up in these special maps. |
| 70 | /dev/ashmem/... maps are special and don't have any restrictions, |
| 71 | so don't mark them as device memory. */ |
| 72 | if (strncmp ("/dev/", cur_map->path, 5) == 0 |
| 73 | && strncmp ("ashmem/", cur_map->path + 5, 7) != 0) |
| 74 | cur_map->flags |= MAP_FLAGS_DEVICE_MEM; |
| 75 | |
Christopher Ferris | 2528bee | 2015-06-23 20:26:41 -0700 | [diff] [blame] | 76 | /* If this is a readable executable map, and not a stack map or an |
| 77 | empty map, find the load_base. */ |
| 78 | if (cur_map->path[0] != '\0' && strncmp ("[stack:", cur_map->path, 7) != 0 |
| 79 | && (flags & (PROT_EXEC | PROT_READ)) == (PROT_EXEC | PROT_READ) |
Christopher Ferris | 517b197 | 2014-10-17 10:18:40 -0700 | [diff] [blame] | 80 | && !(cur_map->flags & MAP_FLAGS_DEVICE_MEM)) |
| 81 | { |
Christopher Ferris | 901acdd | 2015-05-21 18:39:05 -0700 | [diff] [blame] | 82 | struct elf_image ei; |
Christopher Ferris | 2553e59 | 2015-06-05 17:23:36 -0700 | [diff] [blame] | 83 | // Do not map elf for local unwinds, it's faster to read |
| 84 | // from memory directly. |
| 85 | if (map_create_type == UNW_MAP_CREATE_REMOTE |
| 86 | && elf_map_image (&ei, cur_map->path)) |
Christopher Ferris | 517b197 | 2014-10-17 10:18:40 -0700 | [diff] [blame] | 87 | { |
| 88 | unw_word_t load_base; |
Christopher Ferris | 901acdd | 2015-05-21 18:39:05 -0700 | [diff] [blame] | 89 | if (elf_w (get_load_base) (&ei, offset, &load_base)) |
Christopher Ferris | 517b197 | 2014-10-17 10:18:40 -0700 | [diff] [blame] | 90 | cur_map->load_base = load_base; |
Christopher Ferris | 901acdd | 2015-05-21 18:39:05 -0700 | [diff] [blame] | 91 | munmap (ei.u.mapped.image, ei.u.mapped.size); |
Christopher Ferris | 517b197 | 2014-10-17 10:18:40 -0700 | [diff] [blame] | 92 | } |
Christopher Ferris | 2553e59 | 2015-06-05 17:23:36 -0700 | [diff] [blame] | 93 | else |
| 94 | { |
| 95 | // Create an address space right here with enough initialized |
| 96 | // to read data. |
| 97 | if (as == NULL) |
| 98 | { |
| 99 | if (map_create_type == UNW_MAP_CREATE_LOCAL) |
| 100 | { |
| 101 | as = &local_as; |
| 102 | unw_local_access_addr_space_init (as); |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | // For a remote unwind, create the address space |
| 107 | // and arg data the first time we need it. |
| 108 | // We'll reuse these values if we need to attempt |
| 109 | // to get elf data for another map. |
| 110 | as = unw_create_addr_space (&_UPT_accessors, 0); |
| 111 | if (as) |
| 112 | { |
| 113 | as_arg = (void*) _UPT_create (pid); |
| 114 | if (!as_arg) |
| 115 | { |
| 116 | unw_destroy_addr_space (as); |
| 117 | as = NULL; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | if (as) |
| 123 | { |
| 124 | ei.mapped = false; |
| 125 | ei.u.memory.map = cur_map; |
| 126 | ei.u.memory.as = as; |
| 127 | ei.u.memory.as_arg = as_arg; |
| 128 | ei.valid = elf_w (valid_object_memory) (&ei); |
| 129 | unw_word_t load_base; |
| 130 | if (ei.valid && elf_w (get_load_base) (&ei, cur_map->offset, &load_base)) |
| 131 | cur_map->load_base = load_base; |
| 132 | } |
| 133 | } |
Christopher Ferris | 517b197 | 2014-10-17 10:18:40 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Christopher Ferris | 7d46a21 | 2013-11-14 14:03:33 -0800 | [diff] [blame] | 136 | map_list = cur_map; |
| 137 | } |
| 138 | |
| 139 | maps_close (&mi); |
| 140 | |
Christopher Ferris | 2553e59 | 2015-06-05 17:23:36 -0700 | [diff] [blame] | 141 | if (as && map_create_type == UNW_MAP_CREATE_REMOTE) |
| 142 | { |
| 143 | unw_destroy_addr_space (as); |
| 144 | _UPT_destroy (as_arg); |
| 145 | } |
| 146 | |
Christopher Ferris | 7d46a21 | 2013-11-14 14:03:33 -0800 | [diff] [blame] | 147 | return map_list; |
| 148 | } |
Christopher Ferris | 16b95a6 | 2014-01-22 16:07:26 -0800 | [diff] [blame] | 149 | /* End of ANDROID update. */ |