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 #include <elf.h> 25 26 #include "_UCD_lib.h" 27 #include "_UCD_internal.h" 28 29 static int 30 get_unwind_info(struct UCD_info *ui, unw_addr_space_t as, unw_word_t ip) 31 { 32 unsigned long segbase, mapoff; 33 34 #if UNW_TARGET_IA64 && defined(__linux) 35 if (!ui->edi.ktab.start_ip && _Uia64_get_kernel_table (&ui->edi.ktab) < 0) 36 return -UNW_ENOINFO; 37 38 if (ui->edi.ktab.format != -1 && ip >= ui->edi.ktab.start_ip && ip < ui->edi.ktab.end_ip) 39 return 0; 40 #endif 41 42 if ((ui->edi.di_cache.format != -1 43 && ip >= ui->edi.di_cache.start_ip && ip < ui->edi.di_cache.end_ip) 44 #if UNW_TARGET_ARM 45 || (ui->edi.di_debug.format != -1 46 && ip >= ui->edi.di_arm.start_ip && ip < ui->edi.di_arm.end_ip) 47 #endif 48 || (ui->edi.di_debug.format != -1 49 && ip >= ui->edi.di_debug.start_ip && ip < ui->edi.di_debug.end_ip)) 50 return 0; 51 52 invalidate_edi (&ui->edi); 53 54 /* Used to be tdep_get_elf_image() in ptrace unwinding code */ 55 coredump_phdr_t *phdr = _UCD_get_elf_image(ui, ip); 56 if (!phdr) 57 { 58 Debug(1, "returns error: _UCD_get_elf_image failed\n"); 59 return -UNW_ENOINFO; 60 } 61 /* segbase: where it is mapped in virtual memory */ 62 /* mapoff: offset in the file */ 63 segbase = phdr->p_vaddr; 64 /*mapoff = phdr->p_offset; WRONG! phdr->p_offset is the offset in COREDUMP file */ 65 mapoff = 0; 66 ///FIXME. text segment is USUALLY, not always, at offset 0 in the binary/.so file. 67 // ensure that at initialization. 68 69 /* Here, SEGBASE is the starting-address of the (mmap'ped) segment 70 which covers the IP we're looking for. */ 71 if (tdep_find_unwind_table(&ui->edi, as, phdr->backing_filename, segbase, mapoff, ip) < 0) 72 { 73 Debug(1, "returns error: tdep_find_unwind_table failed\n"); 74 return -UNW_ENOINFO; 75 } 76 77 /* This can happen in corner cases where dynamically generated 78 code falls into the same page that contains the data-segment 79 and the page-offset of the code is within the first page of 80 the executable. */ 81 if (ui->edi.di_cache.format != -1 82 && (ip < ui->edi.di_cache.start_ip || ip >= ui->edi.di_cache.end_ip)) 83 ui->edi.di_cache.format = -1; 84 85 if (ui->edi.di_debug.format != -1 86 && (ip < ui->edi.di_debug.start_ip || ip >= ui->edi.di_debug.end_ip)) 87 ui->edi.di_debug.format = -1; 88 89 if (ui->edi.di_cache.format == -1 90 #if UNW_TARGET_ARM 91 && ui->edi.di_arm.format == -1 92 #endif 93 && ui->edi.di_debug.format == -1) 94 { 95 Debug(1, "returns error: all formats are -1\n"); 96 return -UNW_ENOINFO; 97 } 98 99 Debug(1, "returns success\n"); 100 return 0; 101 } 102 103 int 104 _UCD_find_proc_info (unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi, 105 int need_unwind_info, void *arg) 106 { 107 struct UCD_info *ui = arg; 108 109 Debug(1, "entering\n"); 110 111 int ret = -UNW_ENOINFO; 112 113 if (get_unwind_info(ui, as, ip) < 0) { 114 Debug(1, "returns error: get_unwind_info failed\n"); 115 return -UNW_ENOINFO; 116 } 117 118 #if UNW_TARGET_IA64 119 if (ui->edi.ktab.format != -1) 120 { 121 /* The kernel unwind table resides in local memory, so we have 122 to use the local address space to search it. Since 123 _UCD_put_unwind_info() has no easy way of detecting this 124 case, we simply make a copy of the unwind-info, so 125 _UCD_put_unwind_info() can always free() the unwind-info 126 without ill effects. */ 127 ret = tdep_search_unwind_table (unw_local_addr_space, ip, &ui->edi.ktab, pi, 128 need_unwind_info, arg); 129 if (ret >= 0) 130 { 131 if (!need_unwind_info) 132 pi->unwind_info = NULL; 133 else 134 { 135 void *mem = malloc (pi->unwind_info_size); 136 137 if (!mem) 138 return -UNW_ENOMEM; 139 memcpy (mem, pi->unwind_info, pi->unwind_info_size); 140 pi->unwind_info = mem; 141 } 142 } 143 } 144 #endif 145 146 if (ret == -UNW_ENOINFO && ui->edi.di_cache.format != -1) 147 ret = tdep_search_unwind_table (as, ip, &ui->edi.di_cache, 148 pi, need_unwind_info, arg); 149 150 #if UNW_TARGET_ARM 151 if (ret == -UNW_ENOINFO && ui->edi.di_arm.format != -1) 152 ret = tdep_search_unwind_table (as, ip, &ui->edi.di_arm, pi, 153 need_unwind_info, arg); 154 #endif 155 156 if (ret == -UNW_ENOINFO && ui->edi.di_debug.format != -1) 157 ret = tdep_search_unwind_table (as, ip, &ui->edi.di_debug, pi, 158 need_unwind_info, arg); 159 160 Debug(1, "returns %d\n", ret); 161 162 return ret; 163 } 164