]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/projects/libunwind/src/AddressSpace.hpp
MFC r345805: Unify SCSI_STATUS_BUSY retry handling with other cases.
[FreeBSD/FreeBSD.git] / contrib / llvm / projects / libunwind / src / AddressSpace.hpp
1 //===------------------------- AddressSpace.hpp ---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //
9 // Abstracts accessing local vs remote address spaces.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef __ADDRESSSPACE_HPP__
14 #define __ADDRESSSPACE_HPP__
15
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #ifndef _LIBUNWIND_IS_BAREMETAL
22 #include <dlfcn.h>
23 #endif
24
25 #ifdef __APPLE__
26 #include <mach-o/getsect.h>
27 namespace libunwind {
28    bool checkKeyMgrRegisteredFDEs(uintptr_t targetAddr, void *&fde);
29 }
30 #endif
31
32 #include "libunwind.h"
33 #include "config.h"
34 #include "dwarf2.h"
35 #include "Registers.hpp"
36
37 #if _LIBUNWIND_ARM_EHABI
38 #if defined(__FreeBSD__) || defined(__NetBSD__)
39
40 #include <sys/link_elf.h>
41 typedef void *_Unwind_Ptr;
42
43 #elif defined(__linux__)
44
45 typedef long unsigned int *_Unwind_Ptr;
46 extern "C" _Unwind_Ptr __gnu_Unwind_Find_exidx(_Unwind_Ptr addr, int *len);
47
48 // Emulate the BSD dl_unwind_find_exidx API when on a GNU libdl system.
49 #define dl_unwind_find_exidx __gnu_Unwind_Find_exidx
50
51 #elif !defined(_LIBUNWIND_IS_BAREMETAL)
52 #include <link.h>
53 #else // !defined(_LIBUNWIND_IS_BAREMETAL)
54 // When statically linked on bare-metal, the symbols for the EH table are looked
55 // up without going through the dynamic loader.
56 struct EHTEntry {
57   uint32_t functionOffset;
58   uint32_t unwindOpcodes;
59 };
60 extern EHTEntry __exidx_start;
61 extern EHTEntry __exidx_end;
62 #endif // !defined(_LIBUNWIND_IS_BAREMETAL)
63 #endif // _LIBUNWIND_ARM_EHABI
64
65 #if defined(__CloudABI__) || defined(__FreeBSD__) || defined(__linux__) ||      \
66     defined(__NetBSD__)
67 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND && _LIBUNWIND_SUPPORT_DWARF_INDEX
68 #include <link.h>
69 // Macro for machine-independent access to the ELF program headers. This
70 // macro is not available on some systems (e.g., FreeBSD). On these
71 // systems the data structures are just called Elf_XXX. Define ElfW()
72 // locally.
73 #if !defined(ElfW)
74 #define ElfW(type) Elf_##type
75 #endif
76 #include "EHHeaderParser.hpp"
77 #endif
78 #endif
79
80 namespace libunwind {
81
82 /// Used by findUnwindSections() to return info about needed sections.
83 struct UnwindInfoSections {
84 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND || _LIBUNWIND_SUPPORT_DWARF_INDEX ||       \
85     _LIBUNWIND_SUPPORT_COMPACT_UNWIND
86   // No dso_base for ARM EHABI.
87   uintptr_t       dso_base;
88 #endif
89 #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
90   uintptr_t       dwarf_section;
91   uintptr_t       dwarf_section_length;
92 #endif
93 #if _LIBUNWIND_SUPPORT_DWARF_INDEX
94   uintptr_t       dwarf_index_section;
95   uintptr_t       dwarf_index_section_length;
96 #endif
97 #if _LIBUNWIND_SUPPORT_COMPACT_UNWIND
98   uintptr_t       compact_unwind_section;
99   uintptr_t       compact_unwind_section_length;
100 #endif
101 #if _LIBUNWIND_ARM_EHABI
102   uintptr_t       arm_section;
103   uintptr_t       arm_section_length;
104 #endif
105 };
106
107
108 /// LocalAddressSpace is used as a template parameter to UnwindCursor when
109 /// unwinding a thread in the same process.  The wrappers compile away,
110 /// making local unwinds fast.
111 class __attribute__((visibility("hidden"))) LocalAddressSpace {
112 public:
113 #ifdef __LP64__
114   typedef uint64_t pint_t;
115   typedef int64_t  sint_t;
116 #else
117   typedef uint32_t pint_t;
118   typedef int32_t  sint_t;
119 #endif
120   uint8_t         get8(pint_t addr) {
121     uint8_t val;
122     memcpy(&val, (void *)addr, sizeof(val));
123     return val;
124   }
125   uint16_t         get16(pint_t addr) {
126     uint16_t val;
127     memcpy(&val, (void *)addr, sizeof(val));
128     return val;
129   }
130   uint32_t         get32(pint_t addr) {
131     uint32_t val;
132     memcpy(&val, (void *)addr, sizeof(val));
133     return val;
134   }
135   uint64_t         get64(pint_t addr) {
136     uint64_t val;
137     memcpy(&val, (void *)addr, sizeof(val));
138     return val;
139   }
140   double           getDouble(pint_t addr) {
141     double val;
142     memcpy(&val, (void *)addr, sizeof(val));
143     return val;
144   }
145   v128             getVector(pint_t addr) {
146     v128 val;
147     memcpy(&val, (void *)addr, sizeof(val));
148     return val;
149   }
150   uintptr_t       getP(pint_t addr);
151   static uint64_t getULEB128(pint_t &addr, pint_t end);
152   static int64_t  getSLEB128(pint_t &addr, pint_t end);
153
154   pint_t getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
155                      pint_t datarelBase = 0);
156   bool findFunctionName(pint_t addr, char *buf, size_t bufLen,
157                         unw_word_t *offset);
158   bool findUnwindSections(pint_t targetAddr, UnwindInfoSections &info);
159   bool findOtherFDE(pint_t targetAddr, pint_t &fde);
160
161   static LocalAddressSpace sThisAddressSpace;
162 };
163
164 inline uintptr_t LocalAddressSpace::getP(pint_t addr) {
165 #ifdef __LP64__
166   return get64(addr);
167 #else
168   return get32(addr);
169 #endif
170 }
171
172 /// Read a ULEB128 into a 64-bit word.
173 inline uint64_t LocalAddressSpace::getULEB128(pint_t &addr, pint_t end) {
174   const uint8_t *p = (uint8_t *)addr;
175   const uint8_t *pend = (uint8_t *)end;
176   uint64_t result = 0;
177   int bit = 0;
178   do {
179     uint64_t b;
180
181     if (p == pend)
182       _LIBUNWIND_ABORT("truncated uleb128 expression");
183
184     b = *p & 0x7f;
185
186     if (bit >= 64 || b << bit >> bit != b) {
187       _LIBUNWIND_ABORT("malformed uleb128 expression");
188     } else {
189       result |= b << bit;
190       bit += 7;
191     }
192   } while (*p++ >= 0x80);
193   addr = (pint_t) p;
194   return result;
195 }
196
197 /// Read a SLEB128 into a 64-bit word.
198 inline int64_t LocalAddressSpace::getSLEB128(pint_t &addr, pint_t end) {
199   const uint8_t *p = (uint8_t *)addr;
200   const uint8_t *pend = (uint8_t *)end;
201   int64_t result = 0;
202   int bit = 0;
203   uint8_t byte;
204   do {
205     if (p == pend)
206       _LIBUNWIND_ABORT("truncated sleb128 expression");
207     byte = *p++;
208     result |= ((byte & 0x7f) << bit);
209     bit += 7;
210   } while (byte & 0x80);
211   // sign extend negative numbers
212   if ((byte & 0x40) != 0)
213     result |= (-1LL) << bit;
214   addr = (pint_t) p;
215   return result;
216 }
217
218 inline LocalAddressSpace::pint_t
219 LocalAddressSpace::getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
220                                pint_t datarelBase) {
221   pint_t startAddr = addr;
222   const uint8_t *p = (uint8_t *)addr;
223   pint_t result;
224
225   // first get value
226   switch (encoding & 0x0F) {
227   case DW_EH_PE_ptr:
228     result = getP(addr);
229     p += sizeof(pint_t);
230     addr = (pint_t) p;
231     break;
232   case DW_EH_PE_uleb128:
233     result = (pint_t)getULEB128(addr, end);
234     break;
235   case DW_EH_PE_udata2:
236     result = get16(addr);
237     p += 2;
238     addr = (pint_t) p;
239     break;
240   case DW_EH_PE_udata4:
241     result = get32(addr);
242     p += 4;
243     addr = (pint_t) p;
244     break;
245   case DW_EH_PE_udata8:
246     result = (pint_t)get64(addr);
247     p += 8;
248     addr = (pint_t) p;
249     break;
250   case DW_EH_PE_sleb128:
251     result = (pint_t)getSLEB128(addr, end);
252     break;
253   case DW_EH_PE_sdata2:
254     // Sign extend from signed 16-bit value.
255     result = (pint_t)(int16_t)get16(addr);
256     p += 2;
257     addr = (pint_t) p;
258     break;
259   case DW_EH_PE_sdata4:
260     // Sign extend from signed 32-bit value.
261     result = (pint_t)(int32_t)get32(addr);
262     p += 4;
263     addr = (pint_t) p;
264     break;
265   case DW_EH_PE_sdata8:
266     result = (pint_t)get64(addr);
267     p += 8;
268     addr = (pint_t) p;
269     break;
270   default:
271     _LIBUNWIND_ABORT("unknown pointer encoding");
272   }
273
274   // then add relative offset
275   switch (encoding & 0x70) {
276   case DW_EH_PE_absptr:
277     // do nothing
278     break;
279   case DW_EH_PE_pcrel:
280     result += startAddr;
281     break;
282   case DW_EH_PE_textrel:
283     _LIBUNWIND_ABORT("DW_EH_PE_textrel pointer encoding not supported");
284     break;
285   case DW_EH_PE_datarel:
286     // DW_EH_PE_datarel is only valid in a few places, so the parameter has a
287     // default value of 0, and we abort in the event that someone calls this
288     // function with a datarelBase of 0 and DW_EH_PE_datarel encoding.
289     if (datarelBase == 0)
290       _LIBUNWIND_ABORT("DW_EH_PE_datarel is invalid with a datarelBase of 0");
291     result += datarelBase;
292     break;
293   case DW_EH_PE_funcrel:
294     _LIBUNWIND_ABORT("DW_EH_PE_funcrel pointer encoding not supported");
295     break;
296   case DW_EH_PE_aligned:
297     _LIBUNWIND_ABORT("DW_EH_PE_aligned pointer encoding not supported");
298     break;
299   default:
300     _LIBUNWIND_ABORT("unknown pointer encoding");
301     break;
302   }
303
304   if (encoding & DW_EH_PE_indirect)
305     result = getP(result);
306
307   return result;
308 }
309
310 #ifdef __APPLE__ 
311   struct dyld_unwind_sections
312   {
313     const struct mach_header*   mh;
314     const void*                 dwarf_section;
315     uintptr_t                   dwarf_section_length;
316     const void*                 compact_unwind_section;
317     uintptr_t                   compact_unwind_section_length;
318   };
319   #if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) \
320                                  && (__MAC_OS_X_VERSION_MIN_REQUIRED >= 1070)) \
321       || defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
322     // In 10.7.0 or later, libSystem.dylib implements this function.
323     extern "C" bool _dyld_find_unwind_sections(void *, dyld_unwind_sections *);
324   #else
325     // In 10.6.x and earlier, we need to implement this functionality.
326     static inline bool _dyld_find_unwind_sections(void* addr, 
327                                                     dyld_unwind_sections* info) {
328       // Find mach-o image containing address.
329       Dl_info dlinfo;
330       if (!dladdr(addr, &dlinfo))
331         return false;
332       const mach_header *mh = (const mach_header *)dlinfo.dli_saddr;
333       
334       // Find dwarf unwind section in that image.
335       unsigned long size;
336       const uint8_t *p = getsectiondata(mh, "__TEXT", "__eh_frame", &size);
337       if (!p)
338         return false;
339       
340       // Fill in return struct.
341       info->mh = mh;
342       info->dwarf_section = p;
343       info->dwarf_section_length = size;
344       info->compact_unwind_section = 0;
345       info->compact_unwind_section_length = 0;
346      
347       return true;
348     }
349   #endif
350 #endif
351
352 inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
353                                                   UnwindInfoSections &info) {
354 #ifdef __APPLE__
355   dyld_unwind_sections dyldInfo;
356   if (_dyld_find_unwind_sections((void *)targetAddr, &dyldInfo)) {
357     info.dso_base                      = (uintptr_t)dyldInfo.mh;
358  #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
359     info.dwarf_section                 = (uintptr_t)dyldInfo.dwarf_section;
360     info.dwarf_section_length          = dyldInfo.dwarf_section_length;
361  #endif
362     info.compact_unwind_section        = (uintptr_t)dyldInfo.compact_unwind_section;
363     info.compact_unwind_section_length = dyldInfo.compact_unwind_section_length;
364     return true;
365   }
366 #elif _LIBUNWIND_ARM_EHABI
367  #ifdef _LIBUNWIND_IS_BAREMETAL
368   // Bare metal is statically linked, so no need to ask the dynamic loader
369   info.arm_section =        (uintptr_t)(&__exidx_start);
370   info.arm_section_length = (uintptr_t)(&__exidx_end - &__exidx_start);
371  #else
372   int length = 0;
373   info.arm_section = (uintptr_t) dl_unwind_find_exidx(
374       (_Unwind_Ptr) targetAddr, &length);
375   info.arm_section_length = (uintptr_t)length;
376  #endif
377   _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %X length %x",
378                              info.arm_section, info.arm_section_length);
379   if (info.arm_section && info.arm_section_length)
380     return true;
381 #elif _LIBUNWIND_SUPPORT_DWARF_UNWIND
382 #if _LIBUNWIND_SUPPORT_DWARF_INDEX
383   struct dl_iterate_cb_data {
384     LocalAddressSpace *addressSpace;
385     UnwindInfoSections *sects;
386     uintptr_t targetAddr;
387   };
388
389   dl_iterate_cb_data cb_data = {this, &info, targetAddr};
390   int found = dl_iterate_phdr(
391       [](struct dl_phdr_info *pinfo, size_t, void *data) -> int {
392         auto cbdata = static_cast<dl_iterate_cb_data *>(data);
393         size_t object_length;
394         bool found_obj = false;
395         bool found_hdr = false;
396
397         assert(cbdata);
398         assert(cbdata->sects);
399
400         if (cbdata->targetAddr < pinfo->dlpi_addr) {
401           return false;
402         }
403
404 #if !defined(Elf_Half)
405         typedef ElfW(Half) Elf_Half;
406 #endif
407 #if !defined(Elf_Phdr)
408         typedef ElfW(Phdr) Elf_Phdr;
409 #endif
410
411         for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) {
412           const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i];
413           if (phdr->p_type == PT_LOAD) {
414             uintptr_t begin = pinfo->dlpi_addr + phdr->p_vaddr;
415             uintptr_t end = begin + phdr->p_memsz;
416             if (cbdata->targetAddr >= begin && cbdata->targetAddr < end) {
417               cbdata->sects->dso_base = begin;
418               object_length = phdr->p_memsz;
419               found_obj = true;
420             }
421           } else if (phdr->p_type == PT_GNU_EH_FRAME) {
422             EHHeaderParser<LocalAddressSpace>::EHHeaderInfo hdrInfo;
423             uintptr_t eh_frame_hdr_start = pinfo->dlpi_addr + phdr->p_vaddr;
424             cbdata->sects->dwarf_index_section = eh_frame_hdr_start;
425             cbdata->sects->dwarf_index_section_length = phdr->p_memsz;
426             EHHeaderParser<LocalAddressSpace>::decodeEHHdr(
427                 *cbdata->addressSpace, eh_frame_hdr_start, phdr->p_memsz,
428                 hdrInfo);
429             cbdata->sects->dwarf_section = hdrInfo.eh_frame_ptr;
430             found_hdr = true;
431           }
432         }
433
434         if (found_obj && found_hdr) {
435           cbdata->sects->dwarf_section_length = object_length;
436           return true;
437         } else {
438           return false;
439         }
440       },
441       &cb_data);
442   return static_cast<bool>(found);
443 #else
444 #error "_LIBUNWIND_SUPPORT_DWARF_UNWIND requires _LIBUNWIND_SUPPORT_DWARF_INDEX on this platform."
445 #endif
446 #endif
447
448   return false;
449 }
450
451
452 inline bool LocalAddressSpace::findOtherFDE(pint_t targetAddr, pint_t &fde) {
453 #ifdef __APPLE__
454   return checkKeyMgrRegisteredFDEs(targetAddr, *((void**)&fde));
455 #else
456   // TO DO: if OS has way to dynamically register FDEs, check that.
457   (void)targetAddr;
458   (void)fde;
459   return false;
460 #endif
461 }
462
463 inline bool LocalAddressSpace::findFunctionName(pint_t addr, char *buf,
464                                                 size_t bufLen,
465                                                 unw_word_t *offset) {
466 #ifndef _LIBUNWIND_IS_BAREMETAL
467   Dl_info dyldInfo;
468   if (dladdr((void *)addr, &dyldInfo)) {
469     if (dyldInfo.dli_sname != NULL) {
470       snprintf(buf, bufLen, "%s", dyldInfo.dli_sname);
471       *offset = (addr - (pint_t) dyldInfo.dli_saddr);
472       return true;
473     }
474   }
475 #endif
476   return false;
477 }
478
479
480
481 #ifdef UNW_REMOTE
482
483 /// OtherAddressSpace is used as a template parameter to UnwindCursor when
484 /// unwinding a thread in the another process.  The other process can be a
485 /// different endianness and a different pointer size which is handled by
486 /// the P template parameter.
487 template <typename P>
488 class OtherAddressSpace {
489 public:
490   OtherAddressSpace(task_t task) : fTask(task) {}
491
492   typedef typename P::uint_t pint_t;
493
494   uint8_t   get8(pint_t addr);
495   uint16_t  get16(pint_t addr);
496   uint32_t  get32(pint_t addr);
497   uint64_t  get64(pint_t addr);
498   pint_t    getP(pint_t addr);
499   uint64_t  getULEB128(pint_t &addr, pint_t end);
500   int64_t   getSLEB128(pint_t &addr, pint_t end);
501   pint_t    getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
502                         pint_t datarelBase = 0);
503   bool      findFunctionName(pint_t addr, char *buf, size_t bufLen,
504                         unw_word_t *offset);
505   bool      findUnwindSections(pint_t targetAddr, UnwindInfoSections &info);
506   bool      findOtherFDE(pint_t targetAddr, pint_t &fde);
507 private:
508   void *localCopy(pint_t addr);
509
510   task_t fTask;
511 };
512
513 template <typename P> uint8_t OtherAddressSpace<P>::get8(pint_t addr) {
514   return *((uint8_t *)localCopy(addr));
515 }
516
517 template <typename P> uint16_t OtherAddressSpace<P>::get16(pint_t addr) {
518   return P::E::get16(*(uint16_t *)localCopy(addr));
519 }
520
521 template <typename P> uint32_t OtherAddressSpace<P>::get32(pint_t addr) {
522   return P::E::get32(*(uint32_t *)localCopy(addr));
523 }
524
525 template <typename P> uint64_t OtherAddressSpace<P>::get64(pint_t addr) {
526   return P::E::get64(*(uint64_t *)localCopy(addr));
527 }
528
529 template <typename P>
530 typename P::uint_t OtherAddressSpace<P>::getP(pint_t addr) {
531   return P::getP(*(uint64_t *)localCopy(addr));
532 }
533
534 template <typename P>
535 uint64_t OtherAddressSpace<P>::getULEB128(pint_t &addr, pint_t end) {
536   uintptr_t size = (end - addr);
537   LocalAddressSpace::pint_t laddr = (LocalAddressSpace::pint_t) localCopy(addr);
538   LocalAddressSpace::pint_t sladdr = laddr;
539   uint64_t result = LocalAddressSpace::getULEB128(laddr, laddr + size);
540   addr += (laddr - sladdr);
541   return result;
542 }
543
544 template <typename P>
545 int64_t OtherAddressSpace<P>::getSLEB128(pint_t &addr, pint_t end) {
546   uintptr_t size = (end - addr);
547   LocalAddressSpace::pint_t laddr = (LocalAddressSpace::pint_t) localCopy(addr);
548   LocalAddressSpace::pint_t sladdr = laddr;
549   uint64_t result = LocalAddressSpace::getSLEB128(laddr, laddr + size);
550   addr += (laddr - sladdr);
551   return result;
552 }
553
554 template <typename P> void *OtherAddressSpace<P>::localCopy(pint_t addr) {
555   // FIX ME
556 }
557
558 template <typename P>
559 bool OtherAddressSpace<P>::findFunctionName(pint_t addr, char *buf,
560                                             size_t bufLen, unw_word_t *offset) {
561   // FIX ME
562 }
563
564 /// unw_addr_space is the base class that abstract unw_addr_space_t type in
565 /// libunwind.h points to.
566 struct unw_addr_space {
567   cpu_type_t cpuType;
568   task_t taskPort;
569 };
570
571 /// unw_addr_space_i386 is the concrete instance that a unw_addr_space_t points
572 /// to when examining
573 /// a 32-bit intel process.
574 struct unw_addr_space_i386 : public unw_addr_space {
575   unw_addr_space_i386(task_t task) : oas(task) {}
576   OtherAddressSpace<Pointer32<LittleEndian> > oas;
577 };
578
579 /// unw_addr_space_x86_64 is the concrete instance that a unw_addr_space_t
580 /// points to when examining
581 /// a 64-bit intel process.
582 struct unw_addr_space_x86_64 : public unw_addr_space {
583   unw_addr_space_x86_64(task_t task) : oas(task) {}
584   OtherAddressSpace<Pointer64<LittleEndian> > oas;
585 };
586
587 /// unw_addr_space_ppc is the concrete instance that a unw_addr_space_t points
588 /// to when examining
589 /// a 32-bit PowerPC process.
590 struct unw_addr_space_ppc : public unw_addr_space {
591   unw_addr_space_ppc(task_t task) : oas(task) {}
592   OtherAddressSpace<Pointer32<BigEndian> > oas;
593 };
594
595 #endif // UNW_REMOTE
596
597 } // namespace libunwind
598
599 #endif // __ADDRESSSPACE_HPP__