mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 1 | /* libunwind - a platform-independent unwind library |
hp.com!davidm | 5724bee | 2005-05-20 09:48:08 +0000 | [diff] [blame] | 2 | Copyright (C) 2003, 2005 Hewlett-Packard Co |
David Mosberger-Tang | e6b9f35 | 2007-08-22 13:02:09 -0600 | [diff] [blame] | 3 | Copyright (C) 2007 David Mosberger-Tang |
| 4 | Contributed by David Mosberger-Tang <dmosberger@gmail.com> |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of libunwind. |
| 7 | |
| 8 | Permission is hereby granted, free of charge, to any person obtaining |
| 9 | a copy of this software and associated documentation files (the |
| 10 | "Software"), to deal in the Software without restriction, including |
| 11 | without limitation the rights to use, copy, modify, merge, publish, |
| 12 | distribute, sublicense, and/or sell copies of the Software, and to |
| 13 | permit persons to whom the Software is furnished to do so, subject to |
| 14 | the following conditions: |
| 15 | |
| 16 | The above copyright notice and this permission notice shall be |
| 17 | included in all copies or substantial portions of the Software. |
| 18 | |
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 23 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 24 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 25 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 26 | |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <sys/mman.h> |
mostang.com!davidm | 63d7003 | 2003-03-19 19:25:18 +0000 | [diff] [blame] | 31 | #include <sys/stat.h> |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 32 | |
Arun Sharma | d41a453 | 2013-05-19 00:04:15 -0700 | [diff] [blame] | 33 | #include "libunwind_i.h" |
Christopher Ferris | f4a8df5 | 2014-03-07 19:40:06 -0800 | [diff] [blame] | 34 | #include "map_info.h" |
Arun Sharma | d41a453 | 2013-05-19 00:04:15 -0700 | [diff] [blame] | 35 | |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 36 | #if ELF_CLASS == ELFCLASS32 |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 37 | # define ELF_W(x) ELF32_##x |
| 38 | # define Elf_W(x) Elf32_##x |
| 39 | # define elf_w(x) _Uelf32_##x |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 40 | #else |
hp.com!davidm | 4fafd8c | 2003-12-20 11:23:44 +0000 | [diff] [blame] | 41 | # define ELF_W(x) ELF64_##x |
| 42 | # define Elf_W(x) Elf64_##x |
| 43 | # define elf_w(x) _Uelf64_##x |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
Arun Sharma | a83e96c | 2011-01-23 17:55:55 -0800 | [diff] [blame] | 46 | extern int elf_w (get_proc_name) (unw_addr_space_t as, |
| 47 | pid_t pid, unw_word_t ip, |
| 48 | char *buf, size_t len, |
| 49 | unw_word_t *offp); |
| 50 | |
Arun Sharma | d276b7a | 2012-03-12 18:45:50 -0700 | [diff] [blame] | 51 | extern int elf_w (get_proc_name_in_image) (unw_addr_space_t as, |
| 52 | struct elf_image *ei, |
| 53 | unsigned long segbase, |
| 54 | unsigned long mapoff, |
| 55 | unw_word_t ip, |
| 56 | char *buf, size_t buf_len, unw_word_t *offp); |
| 57 | |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 58 | static inline int |
Arun Sharma | 3d08506 | 2012-02-12 19:53:38 -0800 | [diff] [blame] | 59 | elf_w (valid_object) (struct elf_image *ei) |
| 60 | { |
| 61 | if (ei->size <= EI_VERSION) |
| 62 | return 0; |
| 63 | |
| 64 | return (memcmp (ei->image, ELFMAG, SELFMAG) == 0 |
| 65 | && ((uint8_t *) ei->image)[EI_CLASS] == ELF_CLASS |
| 66 | && ((uint8_t *) ei->image)[EI_VERSION] != EV_NONE |
| 67 | && ((uint8_t *) ei->image)[EI_VERSION] <= EV_CURRENT); |
| 68 | } |
| 69 | |
| 70 | static inline int |
mostang.com!davidm | 9c23f9c | 2003-04-03 07:59:15 +0000 | [diff] [blame] | 71 | elf_map_image (struct elf_image *ei, const char *path) |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 72 | { |
| 73 | struct stat stat; |
| 74 | int fd; |
| 75 | |
| 76 | fd = open (path, O_RDONLY); |
| 77 | if (fd < 0) |
| 78 | return -1; |
| 79 | |
| 80 | if (fstat (fd, &stat) < 0) |
| 81 | { |
| 82 | close (fd); |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | ei->size = stat.st_size; |
| 87 | ei->image = mmap (NULL, ei->size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 88 | close (fd); |
| 89 | if (ei->image == MAP_FAILED) |
| 90 | return -1; |
| 91 | |
Arun Sharma | a83e96c | 2011-01-23 17:55:55 -0800 | [diff] [blame] | 92 | if (!elf_w (valid_object) (ei)) |
| 93 | { |
| 94 | munmap(ei->image, ei->size); |
| 95 | return -1; |
| 96 | } |
| 97 | |
mostang.com!davidm | 824d661 | 2003-02-08 10:10:59 +0000 | [diff] [blame] | 98 | return 0; |
| 99 | } |
Christopher Ferris | f4a8df5 | 2014-03-07 19:40:06 -0800 | [diff] [blame] | 100 | |
| 101 | /* ANDROID support update */ |
| 102 | static inline int |
| 103 | elf_map_cached_image (struct map_info *map, unw_word_t ip) |
| 104 | { |
| 105 | intrmask_t saved_mask; |
| 106 | int return_value = 0; |
| 107 | |
| 108 | /* Lock while loading the cached elf image. */ |
| 109 | lock_acquire (&map->ei_lock, saved_mask); |
| 110 | if (map->ei.image == NULL) |
| 111 | { |
| 112 | if (elf_map_image(&map->ei, map->path) < 0) |
| 113 | { |
| 114 | map->ei.image = NULL; |
| 115 | return_value = -1; |
| 116 | } |
| 117 | } |
| 118 | lock_release (&map->ei_lock, saved_mask); |
| 119 | return return_value; |
| 120 | } |
| 121 | /* End of ANDROID update */ |