[libunwind] r252452 - Make it possible to use libunwind without heap.

Peter Zotov via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 8 22:57:30 PST 2015


Author: whitequark
Date: Mon Nov  9 00:57:29 2015
New Revision: 252452

URL: http://llvm.org/viewvc/llvm-project?rev=252452&view=rev
Log:
Make it possible to use libunwind without heap.

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 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 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 ARTIQ project[1], and it is my hope that it will be useful
elsewhere.

[1]: http://m-labs.hk/artiq

Differential Revision: http://reviews.llvm.org/D11897

Modified:
    libunwind/trunk/src/DwarfParser.hpp
    libunwind/trunk/src/UnwindCursor.hpp

Modified: libunwind/trunk/src/DwarfParser.hpp
URL: http://llvm.org/viewvc/llvm-project/libunwind/trunk/src/DwarfParser.hpp?rev=252452&r1=252451&r2=252452&view=diff
==============================================================================
--- libunwind/trunk/src/DwarfParser.hpp (original)
+++ libunwind/trunk/src/DwarfParser.hpp Mon Nov  9 00:57:29 2015
@@ -380,7 +380,9 @@ bool CFI_Parser<A>::parseInstructions(A
     uint64_t length;
     uint8_t opcode = addressSpace.get8(p);
     uint8_t operand;
+#if !defined(_LIBUNWIND_NO_HEAP)
     PrologInfoStackEntry *entry;
+#endif
     ++p;
     switch (opcode) {
     case DW_CFA_nop:
@@ -492,6 +494,7 @@ bool CFI_Parser<A>::parseInstructions(A
         fprintf(stderr, "DW_CFA_register(reg=%" PRIu64 ", reg2=%" PRIu64 ")\n",
                 reg, reg2);
       break;
+#if !defined(_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 @@ bool CFI_Parser<A>::parseInstructions(A
       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);

Modified: libunwind/trunk/src/UnwindCursor.hpp
URL: http://llvm.org/viewvc/llvm-project/libunwind/trunk/src/UnwindCursor.hpp?rev=252452&r1=252451&r2=252452&view=diff
==============================================================================
--- libunwind/trunk/src/UnwindCursor.hpp (original)
+++ libunwind/trunk/src/UnwindCursor.hpp Mon Nov  9 00:57:29 2015
@@ -114,6 +114,7 @@ typename A::pint_t DwarfFDECache<A>::fin
 template <typename A>
 void DwarfFDECache<A>::add(pint_t mh, pint_t ip_start, pint_t ip_end,
                            pint_t fde) {
+#if !defined(_LIBUNWIND_NO_HEAP)
   _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_wrlock(&_lock));
   if (_bufferUsed >= _bufferEnd) {
     size_t oldSize = (size_t)(_bufferEnd - _buffer);
@@ -139,6 +140,7 @@ void DwarfFDECache<A>::add(pint_t mh, pi
   }
 #endif
   _LIBUNWIND_LOG_NON_ZERO(::pthread_rwlock_unlock(&_lock));
+#endif
 }
 
 template <typename A>




More information about the cfe-commits mailing list