1/* libunwind - a platform-independent unwind library
2   Copyright (C) 2003-2004 Hewlett-Packard Co
3	Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5This file is part of libunwind.
6
7Permission is hereby granted, free of charge, to any person obtaining
8a copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, sublicense, and/or sell copies of the Software, and to
12permit persons to whom the Software is furnished to do so, subject to
13the following conditions:
14
15The above copyright notice and this permission notice shall be
16included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
25
26#include <elf.h>
27#include <fcntl.h>
28#include <string.h>
29#include <unistd.h>
30
31#include <sys/mman.h>
32
33#include "_UPT_internal.h"
34
35static int
36get_unwind_info (struct elf_dyn_info *edi, pid_t pid, unw_addr_space_t as, unw_word_t ip, void *as_arg)
37{
38  /* ANDROID support update. */
39  unsigned long segbase, mapoff;
40  struct elf_image ei;
41  int ret;
42  char *path = NULL;
43  /* End of ANDROID update. */
44
45#if UNW_TARGET_IA64 && defined(__linux)
46  if (!edi->ktab.start_ip && _Uia64_get_kernel_table (&edi->ktab) < 0)
47    return -UNW_ENOINFO;
48
49  if (edi->ktab.format != -1 && ip >= edi->ktab.start_ip && ip < edi->ktab.end_ip)
50    return 0;
51#endif
52
53  if ((edi->di_cache.format != -1
54       && ip >= edi->di_cache.start_ip && ip < edi->di_cache.end_ip)
55#if UNW_TARGET_ARM
56      || (edi->di_debug.format != -1
57       && ip >= edi->di_arm.start_ip && ip < edi->di_arm.end_ip)
58#endif
59      || (edi->di_debug.format != -1
60       && ip >= edi->di_debug.start_ip && ip < edi->di_debug.end_ip))
61    return 0;
62
63  invalidate_edi(edi);
64
65  /* ANDROID support update. */
66  if (tdep_get_elf_image (as, &ei, pid, ip, &segbase, &mapoff, &path, as_arg) < 0)
67    return -UNW_ENOINFO;
68
69  ret = tdep_find_unwind_table (edi, &ei, as, path, segbase, mapoff, ip);
70  free(path);
71  if (ret < 0)
72    return ret;
73  /* End of ANDROID update. */
74
75  /* This can happen in corner cases where dynamically generated
76     code falls into the same page that contains the data-segment
77     and the page-offset of the code is within the first page of
78     the executable.  */
79  if (edi->di_cache.format != -1
80      && (ip < edi->di_cache.start_ip || ip >= edi->di_cache.end_ip))
81     edi->di_cache.format = -1;
82
83  if (edi->di_debug.format != -1
84      && (ip < edi->di_debug.start_ip || ip >= edi->di_debug.end_ip))
85     edi->di_debug.format = -1;
86
87  if (edi->di_cache.format == -1
88#if UNW_TARGET_ARM
89      && edi->di_arm.format == -1
90#endif
91      && edi->di_debug.format == -1)
92    return -UNW_ENOINFO;
93
94  return 0;
95}
96
97int
98_UPT_find_proc_info (unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
99		     int need_unwind_info, void *arg)
100{
101  struct UPT_info *ui = arg;
102  int ret = -UNW_ENOINFO;
103
104  if (get_unwind_info (&ui->edi, ui->pid, as, ip, arg) < 0)
105    return -UNW_ENOINFO;
106
107#if UNW_TARGET_IA64
108  if (ui->edi.ktab.format != -1)
109    {
110      /* The kernel unwind table resides in local memory, so we have
111	 to use the local address space to search it.  Since
112	 _UPT_put_unwind_info() has no easy way of detecting this
113	 case, we simply make a copy of the unwind-info, so
114	 _UPT_put_unwind_info() can always free() the unwind-info
115	 without ill effects.  */
116      ret = tdep_search_unwind_table (unw_local_addr_space, ip, &ui->edi.ktab, pi,
117				      need_unwind_info, arg);
118      if (ret >= 0)
119	{
120	  if (!need_unwind_info)
121	    pi->unwind_info = NULL;
122	  else
123	    {
124	      void *mem = malloc (pi->unwind_info_size);
125
126	      if (!mem)
127		return -UNW_ENOMEM;
128	      memcpy (mem, pi->unwind_info, pi->unwind_info_size);
129	      pi->unwind_info = mem;
130	    }
131	}
132    }
133#endif
134
135  if (ret == -UNW_ENOINFO && ui->edi.di_cache.format != -1)
136    ret = tdep_search_unwind_table (as, ip, &ui->edi.di_cache,
137				    pi, need_unwind_info, arg);
138
139#if UNW_TARGET_ARM
140  if (ret == -UNW_ENOINFO && ui->edi.di_arm.format != -1)
141    ret = tdep_search_unwind_table (as, ip, &ui->edi.di_arm, pi,
142                                    need_unwind_info, arg);
143#endif
144
145  if (ret == -UNW_ENOINFO && ui->edi.di_debug.format != -1)
146    ret = tdep_search_unwind_table (as, ip, &ui->edi.di_debug, pi,
147				    need_unwind_info, arg);
148
149  return ret;
150}
151