1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2008 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
26 // C++ interface to lower levels of libuwind
29 #ifndef __ADDRESSSPACE_HPP__
30 #define __ADDRESSSPACE_HPP__
36 #include <mach-o/loader.h>
37 #include <mach-o/getsect.h>
38 #include <mach-o/dyld_priv.h>
39 #include <mach/i386/thread_status.h>
40 #include <Availability.h>
42 #include "FileAbstraction.hpp"
43 #include "libunwind.h"
44 #include "InternalMacros.h"
49 #if __i386__ || __x86_64__
50 // In 10.6 and later i386 and x86_64 don't have a __dyld section
51 // We need one to access private _dyld_func_lookup function.
53 struct __DATA__dyld { long lazy; int (*lookup)(const char*, void**); };
55 static volatile struct __DATA__dyld myDyldSection __attribute__ ((section ("__DATA,__dyld"))) = { 0, NULL };
58 static int my_dyld_func_lookup(const char* dyld_func_name, void **address)
60 return (*myDyldSection.lookup)(dyld_func_name, address);
63 #define my_dyld_func_lookup _dyld_func_lookup
67 bool _dyld_find_unwind_sections(void* addr, dyld_unwind_sections* info)
69 static void* (*p)(void*, dyld_unwind_sections*) = NULL;
72 my_dyld_func_lookup("__dyld_find_unwind_sections", (void**)&p);
82 /// LocalAddressSpace is used as a template parameter to UnwindCursor when unwinding a thread
83 /// in the same process. It compiles away and making local unwinds very fast.
85 class LocalAddressSpace
90 typedef uint64_t pint_t;
91 typedef int64_t sint_t;
93 typedef uint32_t pint_t;
94 typedef int32_t sint_t;
96 uint8_t get8(pint_t addr) { return *((uint8_t*)addr); }
97 uint16_t get16(pint_t addr) { return *((uint16_t*)addr); }
98 uint32_t get32(pint_t addr) { return *((uint32_t*)addr); }
99 uint64_t get64(pint_t addr) { return *((uint64_t*)addr); }
100 double getDouble(pint_t addr) { return *((double*)addr); }
101 v128 getVector(pint_t addr) { return *((v128*)addr); }
102 uintptr_t getP(pint_t addr);
103 static uint64_t getULEB128(pint_t& addr, pint_t end);
104 static int64_t getSLEB128(pint_t& addr, pint_t end);
106 pint_t getEncodedP(pint_t& addr, pint_t end, uint8_t encoding);
107 bool findFunctionName(pint_t addr, char* buf, size_t bufLen, unw_word_t* offset);
108 bool findUnwindSections(pint_t addr, pint_t& mh, pint_t& dwarfStart, pint_t& dwarfLen, pint_t& compactStart);
112 LocalAddressSpace sThisAddress;
114 inline uintptr_t LocalAddressSpace::getP(pint_t addr)
123 /* Read a ULEB128 into a 64-bit word. */
125 LocalAddressSpace::getULEB128(pint_t& addr, pint_t end)
127 const uint8_t* p = (uint8_t*)addr;
128 const uint8_t* pend = (uint8_t*)end;
135 ABORT("truncated uleb128 expression");
139 if (bit >= 64 || b << bit >> bit != b) {
140 ABORT("malformed uleb128 expression");
146 } while ( *p++ >= 0x80 );
151 /* Read a SLEB128 into a 64-bit word. */
153 LocalAddressSpace::getSLEB128(pint_t& addr, pint_t end)
155 const uint8_t* p = (uint8_t*)addr;
161 result |= ((byte & 0x7f) << bit);
163 } while (byte & 0x80);
164 // sign extend negative numbers
165 if ( (byte & 0x40) != 0 )
166 result |= (-1LL) << bit;
171 LocalAddressSpace::pint_t
172 LocalAddressSpace::getEncodedP(pint_t& addr, pint_t end, uint8_t encoding)
174 pint_t startAddr = addr;
175 const uint8_t* p = (uint8_t*)addr;
179 switch (encoding & 0x0F) {
185 case DW_EH_PE_uleb128:
186 result = getULEB128(addr, end);
188 case DW_EH_PE_udata2:
189 result = get16(addr);
193 case DW_EH_PE_udata4:
194 result = get32(addr);
198 case DW_EH_PE_udata8:
199 result = get64(addr);
203 case DW_EH_PE_sleb128:
204 result = getSLEB128(addr, end);
206 case DW_EH_PE_sdata2:
207 result = (int16_t)get16(addr);
211 case DW_EH_PE_sdata4:
212 result = (int32_t)get32(addr);
216 case DW_EH_PE_sdata8:
217 result = get64(addr);
222 ABORT("unknown pointer encoding");
225 // then add relative offset
226 switch ( encoding & 0x70 ) {
227 case DW_EH_PE_absptr:
233 case DW_EH_PE_textrel:
234 ABORT("DW_EH_PE_textrel pointer encoding not supported");
236 case DW_EH_PE_datarel:
237 ABORT("DW_EH_PE_datarel pointer encoding not supported");
239 case DW_EH_PE_funcrel:
240 ABORT("DW_EH_PE_funcrel pointer encoding not supported");
242 case DW_EH_PE_aligned:
243 ABORT("DW_EH_PE_aligned pointer encoding not supported");
246 ABORT("unknown pointer encoding");
250 if ( encoding & DW_EH_PE_indirect )
251 result = getP(result);
257 inline bool LocalAddressSpace::findUnwindSections(pint_t addr, pint_t& mh, pint_t& dwarfStart, pint_t& dwarfLen, pint_t& compactStart)
259 dyld_unwind_sections info;
260 if ( _dyld_find_unwind_sections((void*)addr, &info) ) {
261 mh = (pint_t)info.mh;
262 dwarfStart = (pint_t)info.dwarf_section;
263 dwarfLen = (pint_t)info.dwarf_section_length;
264 compactStart = (pint_t)info.compact_unwind_section;
271 inline bool LocalAddressSpace::findFunctionName(pint_t addr, char* buf, size_t bufLen, unw_word_t* offset)
274 if ( dladdr((void*)addr, &dyldInfo) ) {
275 if ( dyldInfo.dli_sname != NULL ) {
276 strlcpy(buf, dyldInfo.dli_sname, bufLen);
277 *offset = (addr - (pint_t)dyldInfo.dli_saddr);
289 /// OtherAddressSpace is used as a template parameter to UnwindCursor when unwinding a thread
290 /// in the another process. The other process can be a different endianness and a different
291 /// pointer size and is handled by the P template parameter.
293 template <typename P>
294 class OtherAddressSpace
297 OtherAddressSpace(task_t task) : fTask(task) {}
299 typedef typename P::uint_t pint_t;
301 uint8_t get8(pint_t addr);
302 uint16_t get16(pint_t addr);
303 uint32_t get32(pint_t addr);
304 uint64_t get64(pint_t addr);
305 pint_t getP(pint_t addr);
306 uint64_t getULEB128(pint_t& addr, pint_t end);
307 int64_t getSLEB128(pint_t& addr, pint_t end);
308 pint_t getEncodedP(pint_t& addr, pint_t end, uint8_t encoding);
309 bool findFunctionName(pint_t addr, char* buf, size_t bufLen, unw_word_t* offset);
310 bool findUnwindSections(pint_t addr, unwind_sections& info);
312 void* localCopy(pint_t addr);
319 template <typename P>
320 uint8_t OtherAddressSpace<P>::get8(pint_t addr)
322 return *((uint8_t*)localCopy(addr));
325 template <typename P>
326 uint16_t OtherAddressSpace<P>::get16(pint_t addr)
328 return P::E::get16(*(uint16_t*)localCopy(addr));
331 template <typename P>
332 uint32_t OtherAddressSpace<P>::get32(pint_t addr)
334 return P::E::get32(*(uint32_t*)localCopy(addr));
337 template <typename P>
338 uint64_t OtherAddressSpace<P>::get64(pint_t addr)
340 return P::E::get64(*(uint64_t*)localCopy(addr));
343 template <typename P>
344 typename P::uint_t OtherAddressSpace<P>::getP(pint_t addr)
346 return P::getP(*(uint64_t*)localCopy(addr));
349 template <typename P>
350 uint64_t OtherAddressSpace<P>::getULEB128(pint_t& addr, pint_t end)
352 uintptr_t size = (end - addr);
353 LocalAddressSpace::pint_t laddr = (LocalAddressSpace::pint_t)localCopy(addr);
354 LocalAddressSpace::pint_t sladdr = laddr;
355 uint64_t result = LocalAddressSpace::getULEB128(laddr, laddr+size);
356 addr += (laddr-sladdr);
360 template <typename P>
361 int64_t OtherAddressSpace<P>::getSLEB128(pint_t& addr, pint_t end)
363 uintptr_t size = (end - addr);
364 LocalAddressSpace::pint_t laddr = (LocalAddressSpace::pint_t)localCopy(addr);
365 LocalAddressSpace::pint_t sladdr = laddr;
366 uint64_t result = LocalAddressSpace::getSLEB128(laddr, laddr+size);
367 addr += (laddr-sladdr);
371 template <typename P>
372 void* OtherAddressSpace<P>::localCopy(pint_t addr)
377 template <typename P>
378 bool OtherAddressSpace<P>::findFunctionName(pint_t addr, char* buf, size_t bufLen, unw_word_t* offset)
386 /// unw_addr_space is the base class that abstract unw_addr_space_t type in libunwind.h points to.
388 struct unw_addr_space
396 /// unw_addr_space_i386 is the concrete instance that a unw_addr_space_t points to when examining
397 /// a 32-bit intel process.
399 struct unw_addr_space_i386 : public unw_addr_space
401 unw_addr_space_i386(task_t task) : oas(task) {}
402 OtherAddressSpace<Pointer32<LittleEndian> > oas;
407 /// unw_addr_space_x86_64 is the concrete instance that a unw_addr_space_t points to when examining
408 /// a 64-bit intel process.
410 struct unw_addr_space_x86_64 : public unw_addr_space
412 unw_addr_space_x86_64(task_t task) : oas(task) {}
413 OtherAddressSpace<Pointer64<LittleEndian> > oas;
418 /// unw_addr_space_ppc is the concrete instance that a unw_addr_space_t points to when examining
419 /// a 32-bit PowerPC process.
421 struct unw_addr_space_ppc : public unw_addr_space
423 unw_addr_space_ppc(task_t task) : oas(task) {}
424 OtherAddressSpace<Pointer32<BigEndian> > oas;
431 } // namespace libunwind
435 #endif // __ADDRESSSPACE_HPP__