1 /* libunwind - a platform-independent unwind library 2 Copyright (C) 2003, 2005 Hewlett-Packard Co 3 Copyright (C) 2007 David Mosberger-Tang 4 Contributed by David Mosberger-Tang <dmosberger (at) gmail.com> 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 27 #include <fcntl.h> 28 #include <unistd.h> 29 30 #include <sys/mman.h> 31 #include <sys/stat.h> 32 33 #include "libunwind_i.h" 34 #include "map_info.h" 35 36 #if ELF_CLASS == ELFCLASS32 37 # define ELF_W(x) ELF32_##x 38 # define Elf_W(x) Elf32_##x 39 # define elf_w(x) _Uelf32_##x 40 #else 41 # define ELF_W(x) ELF64_##x 42 # define Elf_W(x) Elf64_##x 43 # define elf_w(x) _Uelf64_##x 44 #endif 45 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 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 58 static inline int 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 71 elf_map_image (struct elf_image *ei, const char *path) 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 92 if (!elf_w (valid_object) (ei)) 93 { 94 munmap(ei->image, ei->size); 95 return -1; 96 } 97 98 return 0; 99 } 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 */ 122