1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2014 The Android Open Source Project
3 
4 This file is part of libunwind.
5 
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13 
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
24 
25 
26 #include "libunwind_i.h"
27 #include "map_info.h"
28 
29 extern int local_get_elf_image (unw_addr_space_t as, struct elf_image *,
30                                 unw_word_t, unsigned long *, unsigned long *,
31                                 char **, void *);
32 
33 PROTECTED int
tdep_get_elf_image(unw_addr_space_t as,struct elf_image * ei,pid_t pid,unw_word_t ip,unsigned long * segbase,unsigned long * mapoff,char ** path,void * as_arg)34 tdep_get_elf_image (unw_addr_space_t as, struct elf_image *ei,
35                     pid_t pid, unw_word_t ip,
36                     unsigned long *segbase, unsigned long *mapoff, char **path,
37                     void *as_arg)
38 {
39   struct map_info *map;
40 
41   if (pid == getpid())
42     return local_get_elf_image (as, ei, ip, segbase, mapoff, path, as_arg);
43 
44   map = map_find_from_addr (as->map_list, ip);
45   if (!map)
46     return -UNW_ENOINFO;
47 
48   if (!elf_map_cached_image (as, as_arg, map, ip, false))
49     return -UNW_ENOINFO;
50 
51   *ei = map->ei;
52   *segbase = map->start;
53   if (ei->mapped)
54     *mapoff = map->offset;
55   else
56     /* Always use zero as the map offset for in memory maps. The
57      * dlopen of a shared library from an APK will result in a
58      * non-zero offset so it won't match the elf data and cause
59      * unwinds to fail. Currently, only in memory unwinds of an APK
60      * are possible, so only modify this path.
61      */
62     *mapoff = 0;
63 
64   if (path != NULL)
65     {
66       *path = strdup (map->path);
67     }
68   return 0;
69 }
70