22cf85
diff --git a/dwflpp.cxx b/dwflpp.cxx
22cf85
index 636cd38..c31548d 100644
22cf85
--- a/dwflpp.cxx
22cf85
+++ b/dwflpp.cxx
22cf85
@@ -2272,7 +2272,15 @@ dwflpp::express_as_string (string prelude,
22cf85
 
22cf85
   fprintf(memstream, "{\n");
22cf85
   fprintf(memstream, "%s", prelude.c_str());
22cf85
-  bool deref = c_emit_location (memstream, head, 1);
22cf85
+
22cf85
+  unsigned int stack_depth;
22cf85
+  bool deref = c_emit_location (memstream, head, 1, &stack_depth);
22cf85
+
22cf85
+  // Ensure that DWARF keeps loc2c to a "reasonable" stack size
22cf85
+  // 32 intptr_t leads to max 256 bytes on the stack
22cf85
+  if (stack_depth > 32)
22cf85
+    throw semantic_error("oversized DWARF stack");
22cf85
+
22cf85
   fprintf(memstream, "%s", postlude.c_str());
22cf85
   fprintf(memstream, "  goto out;\n");
22cf85
 
22cf85
diff --git a/loc2c-test.c b/loc2c-test.c
22cf85
index 495a95f..ed7aa4b 100644
22cf85
--- a/loc2c-test.c
22cf85
+++ b/loc2c-test.c
22cf85
@@ -329,11 +329,14 @@ handle_variable (Dwarf_Die *lscopes, int lnscopes, int out,
22cf85
 	"{\n"
22cf85
 	"  intptr_t value;");
22cf85
 
22cf85
-  bool deref = c_emit_location (stdout, head, 1);
22cf85
+  unsigned int stack_depth;
22cf85
+  bool deref = c_emit_location (stdout, head, 1, &stack_depth);
22cf85
 
22cf85
   obstack_free (&pool, NULL);
22cf85
 
22cf85
-  puts (store ? " return;" :
22cf85
+  printf ("  /* max expression stack depth %u */\n", stack_depth);
22cf85
+
22cf85
+  puts (store ? "  return;" :
22cf85
 	"  printk (\" ---> %ld\\n\", (unsigned long) value);\n"
22cf85
 	"  return;");
22cf85
 
22cf85
diff --git a/loc2c.c b/loc2c.c
22cf85
index 5d6b549..0716c7d 100644
22cf85
--- a/loc2c.c
22cf85
+++ b/loc2c.c
22cf85
@@ -2071,7 +2071,8 @@ emit_loc_address (FILE *out, struct location *loc, unsigned int indent,
22cf85
    assign it to an address-sized value.  */
22cf85
 static void
22cf85
 emit_loc_value (FILE *out, struct location *loc, unsigned int indent,
22cf85
-		const char *target, bool declare)
22cf85
+		const char *target, bool declare,
22cf85
+		bool *used_deref, unsigned int *max_stack)
22cf85
 {
22cf85
   if (declare)
22cf85
     emit ("%*s%s %s;\n", indent * 2, "", STACK_TYPE, target);
22cf85
@@ -2091,6 +2092,9 @@ emit_loc_value (FILE *out, struct location *loc, unsigned int indent,
22cf85
     case loc_address:
22cf85
     case loc_value:
22cf85
       emit_loc_address (out, loc, indent, target);
22cf85
+      *used_deref = *used_deref || loc->address.used_deref;
22cf85
+      if (loc->address.stack_depth > *max_stack)
22cf85
+	*max_stack = loc->address.stack_depth;
22cf85
       break;
22cf85
     }
22cf85
 
22cf85
@@ -2098,7 +2102,8 @@ emit_loc_value (FILE *out, struct location *loc, unsigned int indent,
22cf85
 }
22cf85
 
22cf85
 bool
22cf85
-c_emit_location (FILE *out, struct location *loc, int indent)
22cf85
+c_emit_location (FILE *out, struct location *loc, int indent,
22cf85
+		 unsigned int *max_stack)
22cf85
 {
22cf85
   emit ("%*s{\n", indent * 2, "");
22cf85
 
22cf85
@@ -2134,9 +2139,11 @@ c_emit_location (FILE *out, struct location *loc, int indent)
22cf85
       }
22cf85
 
22cf85
   bool deref = false;
22cf85
+  *max_stack = 0;
22cf85
 
22cf85
   if (loc->frame_base != NULL)
22cf85
-    emit_loc_value (out, loc->frame_base, indent, "frame_base", true);
22cf85
+    emit_loc_value (out, loc->frame_base, indent, "frame_base", true,
22cf85
+		    &deref, max_stack);
22cf85
 
22cf85
   for (; loc->next != NULL; loc = loc->next)
22cf85
     switch (loc->type)
22cf85
@@ -2144,8 +2151,7 @@ c_emit_location (FILE *out, struct location *loc, int indent)
22cf85
       case loc_address:
22cf85
       case loc_value:
22cf85
 	/* Emit the program fragment to calculate the address.  */
22cf85
-	emit_loc_value (out, loc, indent + 1, "addr", false);
22cf85
-	deref = deref || loc->address.used_deref;
22cf85
+	emit_loc_value (out, loc, indent + 1, "addr", false, &deref, max_stack);
22cf85
 	break;
22cf85
 
22cf85
       case loc_fragment:
22cf85
@@ -2172,6 +2178,9 @@ c_emit_location (FILE *out, struct location *loc, int indent)
22cf85
 
22cf85
   emit ("%s%*s}\n", loc->address.program, indent * 2, "");
22cf85
 
22cf85
+  if (loc->address.stack_depth > *max_stack)
22cf85
+    *max_stack = loc->address.stack_depth;
22cf85
+
22cf85
   return deref || loc->address.used_deref;
22cf85
 }
22cf85
 
22cf85
diff --git a/loc2c.h b/loc2c.h
22cf85
index becf2d8..45d9382 100644
22cf85
--- a/loc2c.h
22cf85
+++ b/loc2c.h
22cf85
@@ -112,6 +112,7 @@ struct location *c_translate_argument (struct obstack *,
22cf85
 
22cf85
    Writes complete lines of C99, code forming a complete C block, to STREAM.
22cf85
    Return value is true iff that code uses the `deref' runtime macros.  */
22cf85
-bool c_emit_location (FILE *stream, struct location *loc, int indent);
22cf85
+bool c_emit_location (FILE *stream, struct location *loc, int indent,
22cf85
+		      unsigned int *max_stack);
22cf85
 
22cf85
 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
22cf85