[PATCH] D11897: [libunwind] Make it possible to use libunwind without heap.

whitequark via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 10 01:22:59 PDT 2015


whitequark created this revision.
whitequark added reviewers: rengolin, kledzik.
whitequark added a subscriber: llvm-commits.

This patch allows to use libunwind on bare-metal systems that do not include malloc/free by conditionally turning off nonessential functionality that requires these functions.

The disabled functionality includes:
  * the .cfi_remember_state and .cfi_restore_state instructions;
  * the _unw_add_dynamic_fde, _unw_remove_dynamic_fde and
    unw_iterate_dwarf_unwind_cache APIs;
  * the DWARF FDE cache.

The .cfi_{remember,restore}_state instructions don't seem to be used by contemporary compilers. None of the LLVM backends emit it.

The _unw_{add,remove}_dynamic_fde is a user-facing API that such a system will have to avoid using. (It can still use dl_iterate_phdr to provide the FDE locations.)

The DWARF FDE cache is bypassed if _LIBUNWIND_NO_HEAP is defined. Specifically, entries are never added to it, so the search begins and ends at the statically allocated, empty initial cache.

Such heap-less libunwind on a bare metal system is successfully used in the [[ http://m-labs.hk/artiq | ARTIQ project ]].

http://reviews.llvm.org/D11897

Files:
  src/DwarfParser.hpp
  src/UnwindCursor.hpp
  src/libunwind.cpp

Index: src/libunwind.cpp
===================================================================
--- src/libunwind.cpp
+++ src/libunwind.cpp
@@ -303,17 +303,15 @@
 }
 #endif
 
-
-#if _LIBUNWIND_SUPPORT_DWARF_UNWIND
+#if !defined(_LIBUNWIND_NO_HEAP) && _LIBUNWIND_SUPPORT_DWARF_UNWIND
 /// SPI: walks cached dwarf entries
 _LIBUNWIND_EXPORT void unw_iterate_dwarf_unwind_cache(void (*func)(
     unw_word_t ip_start, unw_word_t ip_end, unw_word_t fde, unw_word_t mh)) {
   _LIBUNWIND_TRACE_API("unw_iterate_dwarf_unwind_cache(func=%p)\n",
                        reinterpret_cast<void *>(func));
   DwarfFDECache<LocalAddressSpace>::iterateCacheEntries(func);
 }
 
-
 /// IPI: for __register_frame()
 void _unw_add_dynamic_fde(unw_word_t fde) {
   CFI_Parser<LocalAddressSpace>::FDE_Info fdeInfo;
@@ -338,7 +336,7 @@
   // fde is own mh_group
   DwarfFDECache<LocalAddressSpace>::removeAllIn((LocalAddressSpace::pint_t)fde);
 }
-#endif // _LIBUNWIND_SUPPORT_DWARF_UNWIND
+#endif // !defined(_LIBUNWIND_NO_HEAP) && _LIBUNWIND_SUPPORT_DWARF_UNWIND
 
 
 
Index: src/UnwindCursor.hpp
===================================================================
--- src/UnwindCursor.hpp
+++ src/UnwindCursor.hpp
@@ -895,8 +895,12 @@
   #if _LIBUNWIND_SUPPORT_DWARF_INDEX
         if (sects.dwarf_index_section == 0)
   #endif
+  #ifdef _LIBUNWIND_NO_HEAP
+          ;
+  #else
         DwarfFDECache<A>::add(sects.dso_base, fdeInfo.pcStart, fdeInfo.pcEnd,
                               fdeInfo.fdeStart);
+  #endif
       }
       return true;
     }
Index: src/DwarfParser.hpp
===================================================================
--- src/DwarfParser.hpp
+++ src/DwarfParser.hpp
@@ -380,7 +380,9 @@
     uint64_t length;
     uint8_t opcode = addressSpace.get8(p);
     uint8_t operand;
+#ifndef _LIBUNWIND_NO_HEAP
     PrologInfoStackEntry *entry;
+#endif
     ++p;
     switch (opcode) {
     case DW_CFA_nop:
@@ -492,6 +494,7 @@
         fprintf(stderr, "DW_CFA_register(reg=%" PRIu64 ", reg2=%" PRIu64 ")\n",
                 reg, reg2);
       break;
+#ifndef _LIBUNWIND_NO_HEAP
     case DW_CFA_remember_state:
       // avoid operator new, because that would be an upward dependency
       entry = (PrologInfoStackEntry *)malloc(sizeof(PrologInfoStackEntry));
@@ -517,6 +520,7 @@
       if (logDwarf)
         fprintf(stderr, "DW_CFA_restore_state\n");
       break;
+#endif
     case DW_CFA_def_cfa:
       reg = addressSpace.getULEB128(p, instructionsEnd);
       offset = (int64_t)addressSpace.getULEB128(p, instructionsEnd);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D11897.31639.patch
Type: text/x-patch
Size: 2533 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150810/48b5a949/attachment.bin>


More information about the llvm-commits mailing list