• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libunwind - a platform-independent unwind library
2 
3 This file is part of libunwind.
4 
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
23 
24 #if defined(HAVE_ELF_H)
25 # include <elf.h>
26 #elif defined(HAVE_SYS_ELF_H)
27 # include <sys/elf.h>
28 #endif
29 
30 #include "_UCD_lib.h"
31 #include "_UCD_internal.h"
32 
33 static int
get_unwind_info(struct UCD_info * ui,unw_addr_space_t as,unw_word_t ip)34 get_unwind_info(struct UCD_info *ui, unw_addr_space_t as, unw_word_t ip)
35 {
36   unsigned long segbase, mapoff;
37 
38 #if UNW_TARGET_IA64 && defined(__linux__)
39   if (!ui->edi.ktab.start_ip && _Uia64_get_kernel_table (&ui->edi.ktab) < 0)
40     return -UNW_ENOINFO;
41 
42   if (ui->edi.ktab.format != -1 && ip >= ui->edi.ktab.start_ip && ip < ui->edi.ktab.end_ip)
43     return 0;
44 #endif
45 
46   if ((ui->edi.di_cache.format != -1
47        && ip >= ui->edi.di_cache.start_ip && ip < ui->edi.di_cache.end_ip)
48 #if UNW_TARGET_ARM
49       || (ui->edi.di_debug.format != -1
50        && ip >= ui->edi.di_arm.start_ip && ip < ui->edi.di_arm.end_ip)
51 #endif
52       || (ui->edi.di_debug.format != -1
53        && ip >= ui->edi.di_debug.start_ip && ip < ui->edi.di_debug.end_ip))
54     return 0;
55 
56   invalidate_edi (&ui->edi);
57 
58   /* Used to be tdep_get_elf_image() in ptrace unwinding code */
59   coredump_phdr_t *phdr = _UCD_get_elf_image(ui, ip);
60   if (!phdr)
61     {
62       Debug(1, "returns error: _UCD_get_elf_image failed\n");
63       return -UNW_ENOINFO;
64     }
65   /* segbase: where it is mapped in virtual memory */
66   /* mapoff: offset in the file */
67   segbase = phdr->p_vaddr;
68   /*mapoff  = phdr->p_offset; WRONG! phdr->p_offset is the offset in COREDUMP file */
69   mapoff  = 0;
70 ///FIXME. text segment is USUALLY, not always, at offset 0 in the binary/.so file.
71 // ensure that at initialization.
72 
73   /* Here, SEGBASE is the starting-address of the (mmap'ped) segment
74      which covers the IP we're looking for.  */
75   if (tdep_find_unwind_table(&ui->edi, as, phdr->backing_filename, segbase, mapoff, ip) < 0)
76     {
77       Debug(1, "returns error: tdep_find_unwind_table failed\n");
78       return -UNW_ENOINFO;
79     }
80 
81   /* This can happen in corner cases where dynamically generated
82      code falls into the same page that contains the data-segment
83      and the page-offset of the code is within the first page of
84      the executable.  */
85   if (ui->edi.di_cache.format != -1
86       && (ip < ui->edi.di_cache.start_ip || ip >= ui->edi.di_cache.end_ip))
87      ui->edi.di_cache.format = -1;
88 
89   if (ui->edi.di_debug.format != -1
90       && (ip < ui->edi.di_debug.start_ip || ip >= ui->edi.di_debug.end_ip))
91      ui->edi.di_debug.format = -1;
92 
93   if (ui->edi.di_cache.format == -1
94 #if UNW_TARGET_ARM
95       && ui->edi.di_arm.format == -1
96 #endif
97       && ui->edi.di_debug.format == -1)
98   {
99     Debug(1, "returns error: all formats are -1\n");
100     return -UNW_ENOINFO;
101   }
102 
103   Debug(1, "returns success\n");
104   return 0;
105 }
106 
107 int
_UCD_find_proc_info(unw_addr_space_t as,unw_word_t ip,unw_proc_info_t * pi,int need_unwind_info,void * arg)108 _UCD_find_proc_info (unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
109                      int need_unwind_info, void *arg)
110 {
111   struct UCD_info *ui = arg;
112 
113   Debug(1, "entering\n");
114 
115   int ret = -UNW_ENOINFO;
116 
117   if (get_unwind_info(ui, as, ip) < 0) {
118     Debug(1, "returns error: get_unwind_info failed\n");
119     return -UNW_ENOINFO;
120   }
121 
122 #if UNW_TARGET_IA64
123   if (ui->edi.ktab.format != -1)
124     {
125       /* The kernel unwind table resides in local memory, so we have
126          to use the local address space to search it.  Since
127          _UCD_put_unwind_info() has no easy way of detecting this
128          case, we simply make a copy of the unwind-info, so
129          _UCD_put_unwind_info() can always free() the unwind-info
130          without ill effects.  */
131       ret = tdep_search_unwind_table (unw_local_addr_space, ip, &ui->edi.ktab, pi,
132                                       need_unwind_info, arg);
133       if (ret >= 0)
134         {
135           if (!need_unwind_info)
136             pi->unwind_info = NULL;
137           else
138             {
139               void *mem = malloc (pi->unwind_info_size);
140 
141               if (!mem)
142                 return -UNW_ENOMEM;
143               memcpy (mem, pi->unwind_info, pi->unwind_info_size);
144               pi->unwind_info = mem;
145             }
146         }
147     }
148 #endif
149 
150   if (ret == -UNW_ENOINFO && ui->edi.di_cache.format != -1)
151     ret = tdep_search_unwind_table (as, ip, &ui->edi.di_cache,
152                                     pi, need_unwind_info, arg);
153 
154 #if UNW_TARGET_ARM
155   if (ret == -UNW_ENOINFO && ui->edi.di_arm.format != -1)
156     ret = tdep_search_unwind_table (as, ip, &ui->edi.di_arm, pi,
157                                     need_unwind_info, arg);
158 #endif
159 
160   if (ret == -UNW_ENOINFO && ui->edi.di_debug.format != -1)
161     ret = tdep_search_unwind_table (as, ip, &ui->edi.di_debug, pi,
162                                     need_unwind_info, arg);
163 
164   Debug(1, "returns %d\n", ret);
165 
166   return ret;
167 }
168