]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/projects/libunwind/src/AddressSpace.hpp
MFV: r342867
[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   uint64_t        getRegister(pint_t addr);
152   static uint64_t getULEB128(pint_t &addr, pint_t end);
153   static int64_t  getSLEB128(pint_t &addr, pint_t end);
154
155   pint_t getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
156                      pint_t datarelBase = 0);
157   bool findFunctionName(pint_t addr, char *buf, size_t bufLen,
158                         unw_word_t *offset);
159   bool findUnwindSections(pint_t targetAddr, UnwindInfoSections &info);
160   bool findOtherFDE(pint_t targetAddr, pint_t &fde);
161
162   static LocalAddressSpace sThisAddressSpace;
163 };
164
165 inline uintptr_t LocalAddressSpace::getP(pint_t addr) {
166 #ifdef __LP64__
167   return get64(addr);
168 #else
169   return get32(addr);
170 #endif
171 }
172
173 inline uint64_t LocalAddressSpace::getRegister(pint_t addr) {
174 #if defined(__LP64__) || defined(__mips64)
175   return get64(addr);
176 #else
177   return get32(addr);
178 #endif
179 }
180
181 /// Read a ULEB128 into a 64-bit word.
182 inline uint64_t LocalAddressSpace::getULEB128(pint_t &addr, pint_t end) {
183   const uint8_t *p = (uint8_t *)addr;
184   const uint8_t *pend = (uint8_t *)end;
185   uint64_t result = 0;
186   int bit = 0;
187   do {
188     uint64_t b;
189
190     if (p == pend)
191       _LIBUNWIND_ABORT("truncated uleb128 expression");
192
193     b = *p & 0x7f;
194
195     if (bit >= 64 || b << bit >> bit != b) {
196       _LIBUNWIND_ABORT("malformed uleb128 expression");
197     } else {
198       result |= b << bit;
199       bit += 7;
200     }
201   } while (*p++ >= 0x80);
202   addr = (pint_t) p;
203   return result;
204 }
205
206 /// Read a SLEB128 into a 64-bit word.
207 inline int64_t LocalAddressSpace::getSLEB128(pint_t &addr, pint_t end) {
208   const uint8_t *p = (uint8_t *)addr;
209   const uint8_t *pend = (uint8_t *)end;
210   int64_t result = 0;
211   int bit = 0;
212   uint8_t byte;
213   do {
214     if (p == pend)
215       _LIBUNWIND_ABORT("truncated sleb128 expression");
216     byte = *p++;
217     result |= ((byte & 0x7f) << bit);
218     bit += 7;
219   } while (byte & 0x80);
220   // sign extend negative numbers
221   if ((byte & 0x40) != 0)
222     result |= (-1LL) << bit;
223   addr = (pint_t) p;
224   return result;
225 }
226
227 inline LocalAddressSpace::pint_t
228 LocalAddressSpace::getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
229                                pint_t datarelBase) {
230   pint_t startAddr = addr;
231   const uint8_t *p = (uint8_t *)addr;
232   pint_t result;
233
234   // first get value
235   switch (encoding & 0x0F) {
236   case DW_EH_PE_ptr:
237     result = getP(addr);
238     p += sizeof(pint_t);
239     addr = (pint_t) p;
240     break;
241   case DW_EH_PE_uleb128:
242     result = (pint_t)getULEB128(addr, end);
243     break;
244   case DW_EH_PE_udata2:
245     result = get16(addr);
246     p += 2;
247     addr = (pint_t) p;
248     break;
249   case DW_EH_PE_udata4:
250     result = get32(addr);
251     p += 4;
252     addr = (pint_t) p;
253     break;
254   case DW_EH_PE_udata8:
255     result = (pint_t)get64(addr);
256     p += 8;
257     addr = (pint_t) p;
258     break;
259   case DW_EH_PE_sleb128:
260     result = (pint_t)getSLEB128(addr, end);
261     break;
262   case DW_EH_PE_sdata2:
263     // Sign extend from signed 16-bit value.
264     result = (pint_t)(int16_t)get16(addr);
265     p += 2;
266     addr = (pint_t) p;
267     break;
268   case DW_EH_PE_sdata4:
269     // Sign extend from signed 32-bit value.
270     result = (pint_t)(int32_t)get32(addr);
271     p += 4;
272     addr = (pint_t) p;
273     break;
274   case DW_EH_PE_sdata8:
275     result = (pint_t)get64(addr);
276     p += 8;
277     addr = (pint_t) p;
278     break;
279   default:
280     _LIBUNWIND_ABORT("unknown pointer encoding");
281   }
282
283   // then add relative offset
284   switch (encoding & 0x70) {
285   case DW_EH_PE_absptr:
286     // do nothing
287     break;
288   case DW_EH_PE_pcrel:
289     result += startAddr;
290     break;
291   case DW_EH_PE_textrel:
292     _LIBUNWIND_ABORT("DW_EH_PE_textrel pointer encoding not supported");
293     break;
294   case DW_EH_PE_datarel:
295     // DW_EH_PE_datarel is only valid in a few places, so the parameter has a
296     // default value of 0, and we abort in the event that someone calls this
297     // function with a datarelBase of 0 and DW_EH_PE_datarel encoding.
298     if (datarelBase == 0)
299       _LIBUNWIND_ABORT("DW_EH_PE_datarel is invalid with a datarelBase of 0");
300     result += datarelBase;
301     break;
302   case DW_EH_PE_funcrel:
303     _LIBUNWIND_ABORT("DW_EH_PE_funcrel pointer encoding not supported");
304     break;
305   case DW_EH_PE_aligned:
306     _LIBUNWIND_ABORT("DW_EH_PE_aligned pointer encoding not supported");
307     break;
308   default:
309     _LIBUNWIND_ABORT("unknown pointer encoding");
310     break;
311   }
312
313   if (encoding & DW_EH_PE_indirect)
314     result = getP(result);
315
316   return result;
317 }
318
319 #ifdef __APPLE__ 
320   struct dyld_unwind_sections
321   {
322     const struct mach_header*   mh;
323     const void*                 dwarf_section;
324     uintptr_t                   dwarf_section_length;
325     const void*                 compact_unwind_section;
326     uintptr_t                   compact_unwind_section_length;
327   };
328   #if (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) \
329                                  && (__MAC_OS_X_VERSION_MIN_REQUIRED >= 1070)) \
330       || defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
331     // In 10.7.0 or later, libSystem.dylib implements this function.
332     extern "C" bool _dyld_find_unwind_sections(void *, dyld_unwind_sections *);
333   #else
334     // In 10.6.x and earlier, we need to implement this functionality.
335     static inline bool _dyld_find_unwind_sections(void* addr, 
336                                                     dyld_unwind_sections* info) {
337       // Find mach-o image containing address.
338       Dl_info dlinfo;
339       if (!dladdr(addr, &dlinfo))
340         return false;
341       const mach_header *mh = (const mach_header *)dlinfo.dli_saddr;
342       
343       // Find dwarf unwind section in that image.
344       unsigned long size;
345       const uint8_t *p = getsectiondata(mh, "__TEXT", "__eh_frame", &size);
346       if (!p)
347         return false;
348       
349       // Fill in return struct.
350       info->mh = mh;
351       info->dwarf_section = p;
352       info->dwarf_section_length = size;
353       info->compact_unwind_section = 0;
354       info->compact_unwind_section_length = 0;
355      
356       return true;
357     }
358   #endif
359 #endif
360
361 inline bool LocalAddressSpace::findUnwindSections(pint_t targetAddr,
362                                                   UnwindInfoSections &info) {
363 #ifdef __APPLE__
364   dyld_unwind_sections dyldInfo;
365   if (_dyld_find_unwind_sections((void *)targetAddr, &dyldInfo)) {
366     info.dso_base                      = (uintptr_t)dyldInfo.mh;
367  #if _LIBUNWIND_SUPPORT_DWARF_UNWIND
368     info.dwarf_section                 = (uintptr_t)dyldInfo.dwarf_section;
369     info.dwarf_section_length          = dyldInfo.dwarf_section_length;
370  #endif
371     info.compact_unwind_section        = (uintptr_t)dyldInfo.compact_unwind_section;
372     info.compact_unwind_section_length = dyldInfo.compact_unwind_section_length;
373     return true;
374   }
375 #elif _LIBUNWIND_ARM_EHABI
376  #ifdef _LIBUNWIND_IS_BAREMETAL
377   // Bare metal is statically linked, so no need to ask the dynamic loader
378   info.arm_section =        (uintptr_t)(&__exidx_start);
379   info.arm_section_length = (uintptr_t)(&__exidx_end - &__exidx_start);
380  #else
381   int length = 0;
382   info.arm_section = (uintptr_t) dl_unwind_find_exidx(
383       (_Unwind_Ptr) targetAddr, &length);
384   info.arm_section_length = (uintptr_t)length;
385  #endif
386   _LIBUNWIND_TRACE_UNWINDING("findUnwindSections: section %X length %x",
387                              info.arm_section, info.arm_section_length);
388   if (info.arm_section && info.arm_section_length)
389     return true;
390 #elif _LIBUNWIND_SUPPORT_DWARF_UNWIND
391 #if _LIBUNWIND_SUPPORT_DWARF_INDEX
392   struct dl_iterate_cb_data {
393     LocalAddressSpace *addressSpace;
394     UnwindInfoSections *sects;
395     uintptr_t targetAddr;
396   };
397
398   dl_iterate_cb_data cb_data = {this, &info, targetAddr};
399   int found = dl_iterate_phdr(
400       [](struct dl_phdr_info *pinfo, size_t, void *data) -> int {
401         auto cbdata = static_cast<dl_iterate_cb_data *>(data);
402         size_t object_length;
403         bool found_obj = false;
404         bool found_hdr = false;
405
406         assert(cbdata);
407         assert(cbdata->sects);
408
409         if (cbdata->targetAddr < pinfo->dlpi_addr) {
410           return false;
411         }
412
413 #if !defined(Elf_Half)
414         typedef ElfW(Half) Elf_Half;
415 #endif
416 #if !defined(Elf_Phdr)
417         typedef ElfW(Phdr) Elf_Phdr;
418 #endif
419
420         for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) {
421           const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i];
422           if (phdr->p_type == PT_LOAD) {
423             uintptr_t begin = pinfo->dlpi_addr + phdr->p_vaddr;
424             uintptr_t end = begin + phdr->p_memsz;
425             if (cbdata->targetAddr >= begin && cbdata->targetAddr < end) {
426               cbdata->sects->dso_base = begin;
427               object_length = phdr->p_memsz;
428               found_obj = true;
429             }
430           } else if (phdr->p_type == PT_GNU_EH_FRAME) {
431             EHHeaderParser<LocalAddressSpace>::EHHeaderInfo hdrInfo;
432             uintptr_t eh_frame_hdr_start = pinfo->dlpi_addr + phdr->p_vaddr;
433             cbdata->sects->dwarf_index_section = eh_frame_hdr_start;
434             cbdata->sects->dwarf_index_section_length = phdr->p_memsz;
435             EHHeaderParser<LocalAddressSpace>::decodeEHHdr(
436                 *cbdata->addressSpace, eh_frame_hdr_start, phdr->p_memsz,
437                 hdrInfo);
438             cbdata->sects->dwarf_section = hdrInfo.eh_frame_ptr;
439             found_hdr = true;
440           }
441         }
442
443         if (found_obj && found_hdr) {
444           cbdata->sects->dwarf_section_length = object_length;
445           return true;
446         } else {
447           return false;
448         }
449       },
450       &cb_data);
451   return static_cast<bool>(found);
452 #else
453 #error "_LIBUNWIND_SUPPORT_DWARF_UNWIND requires _LIBUNWIND_SUPPORT_DWARF_INDEX on this platform."
454 #endif
455 #endif
456
457   return false;
458 }
459
460
461 inline bool LocalAddressSpace::findOtherFDE(pint_t targetAddr, pint_t &fde) {
462 #ifdef __APPLE__
463   return checkKeyMgrRegisteredFDEs(targetAddr, *((void**)&fde));
464 #else
465   // TO DO: if OS has way to dynamically register FDEs, check that.
466   (void)targetAddr;
467   (void)fde;
468   return false;
469 #endif
470 }
471
472 inline bool LocalAddressSpace::findFunctionName(pint_t addr, char *buf,
473                                                 size_t bufLen,
474                                                 unw_word_t *offset) {
475 #ifndef _LIBUNWIND_IS_BAREMETAL
476   Dl_info dyldInfo;
477   if (dladdr((void *)addr, &dyldInfo)) {
478     if (dyldInfo.dli_sname != NULL) {
479       snprintf(buf, bufLen, "%s", dyldInfo.dli_sname);
480       *offset = (addr - (pint_t) dyldInfo.dli_saddr);
481       return true;
482     }
483   }
484 #endif
485   return false;
486 }
487
488
489
490 #ifdef UNW_REMOTE
491
492 /// OtherAddressSpace is used as a template parameter to UnwindCursor when
493 /// unwinding a thread in the another process.  The other process can be a
494 /// different endianness and a different pointer size which is handled by
495 /// the P template parameter.
496 template <typename P>
497 class OtherAddressSpace {
498 public:
499   OtherAddressSpace(task_t task) : fTask(task) {}
500
501   typedef typename P::uint_t pint_t;
502
503   uint8_t   get8(pint_t addr);
504   uint16_t  get16(pint_t addr);
505   uint32_t  get32(pint_t addr);
506   uint64_t  get64(pint_t addr);
507   pint_t    getP(pint_t addr);
508   uint64_t  getRegister(pint_t addr);
509   uint64_t  getULEB128(pint_t &addr, pint_t end);
510   int64_t   getSLEB128(pint_t &addr, pint_t end);
511   pint_t    getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
512                         pint_t datarelBase = 0);
513   bool      findFunctionName(pint_t addr, char *buf, size_t bufLen,
514                         unw_word_t *offset);
515   bool      findUnwindSections(pint_t targetAddr, UnwindInfoSections &info);
516   bool      findOtherFDE(pint_t targetAddr, pint_t &fde);
517 private:
518   void *localCopy(pint_t addr);
519
520   task_t fTask;
521 };
522
523 template <typename P> uint8_t OtherAddressSpace<P>::get8(pint_t addr) {
524   return *((uint8_t *)localCopy(addr));
525 }
526
527 template <typename P> uint16_t OtherAddressSpace<P>::get16(pint_t addr) {
528   return P::E::get16(*(uint16_t *)localCopy(addr));
529 }
530
531 template <typename P> uint32_t OtherAddressSpace<P>::get32(pint_t addr) {
532   return P::E::get32(*(uint32_t *)localCopy(addr));
533 }
534
535 template <typename P> uint64_t OtherAddressSpace<P>::get64(pint_t addr) {
536   return P::E::get64(*(uint64_t *)localCopy(addr));
537 }
538
539 template <typename P>
540 typename P::uint_t OtherAddressSpace<P>::getP(pint_t addr) {
541   return P::getP(*(uint64_t *)localCopy(addr));
542 }
543
544 template <typename P>
545 typename P::uint_t OtherAddressSpace<P>::getRegister(pint_t addr) {
546   return P::getRegister(*(uint64_t *)localCopy(addr));
547 }
548
549 template <typename P>
550 uint64_t OtherAddressSpace<P>::getULEB128(pint_t &addr, pint_t end) {
551   uintptr_t size = (end - addr);
552   LocalAddressSpace::pint_t laddr = (LocalAddressSpace::pint_t) localCopy(addr);
553   LocalAddressSpace::pint_t sladdr = laddr;
554   uint64_t result = LocalAddressSpace::getULEB128(laddr, laddr + size);
555   addr += (laddr - sladdr);
556   return result;
557 }
558
559 template <typename P>
560 int64_t OtherAddressSpace<P>::getSLEB128(pint_t &addr, pint_t end) {
561   uintptr_t size = (end - addr);
562   LocalAddressSpace::pint_t laddr = (LocalAddressSpace::pint_t) localCopy(addr);
563   LocalAddressSpace::pint_t sladdr = laddr;
564   uint64_t result = LocalAddressSpace::getSLEB128(laddr, laddr + size);
565   addr += (laddr - sladdr);
566   return result;
567 }
568
569 template <typename P> void *OtherAddressSpace<P>::localCopy(pint_t addr) {
570   // FIX ME
571 }
572
573 template <typename P>
574 bool OtherAddressSpace<P>::findFunctionName(pint_t addr, char *buf,
575                                             size_t bufLen, unw_word_t *offset) {
576   // FIX ME
577 }
578
579 /// unw_addr_space is the base class that abstract unw_addr_space_t type in
580 /// libunwind.h points to.
581 struct unw_addr_space {
582   cpu_type_t cpuType;
583   task_t taskPort;
584 };
585
586 /// unw_addr_space_i386 is the concrete instance that a unw_addr_space_t points
587 /// to when examining
588 /// a 32-bit intel process.
589 struct unw_addr_space_i386 : public unw_addr_space {
590   unw_addr_space_i386(task_t task) : oas(task) {}
591   OtherAddressSpace<Pointer32<LittleEndian> > oas;
592 };
593
594 /// unw_addr_space_x86_64 is the concrete instance that a unw_addr_space_t
595 /// points to when examining
596 /// a 64-bit intel process.
597 struct unw_addr_space_x86_64 : public unw_addr_space {
598   unw_addr_space_x86_64(task_t task) : oas(task) {}
599   OtherAddressSpace<Pointer64<LittleEndian> > oas;
600 };
601
602 /// unw_addr_space_ppc is the concrete instance that a unw_addr_space_t points
603 /// to when examining
604 /// a 32-bit PowerPC process.
605 struct unw_addr_space_ppc : public unw_addr_space {
606   unw_addr_space_ppc(task_t task) : oas(task) {}
607   OtherAddressSpace<Pointer32<BigEndian> > oas;
608 };
609
610 #endif // UNW_REMOTE
611
612 } // namespace libunwind
613
614 #endif // __ADDRESSSPACE_HPP__