diff --git a/Makefile.am b/Makefile.am
index 1e5688c1be05c863fb5f7cb630304832e655c062..85202e543afa99713acd1e3eb04c1d9b5ad598f8 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -33,6 +33,7 @@ print_libstubs_LDFLAGS = -ldl
 
 libcapsule_la_SOURCES  = capsule/capsule-dlmopen.c  \
                          capsule/capsule-relocate.c \
+                         capsule/capsule-init.c     \
                          utils/utils.c utils/dump.c \
                          utils/process-pt-dynamic.c \
                          utils/mmap-info.c
diff --git a/capsule/capsule-init.c b/capsule/capsule-init.c
new file mode 100644
index 0000000000000000000000000000000000000000..d6659379208349fe35caac45f2a7314218ab120c
--- /dev/null
+++ b/capsule/capsule-init.c
@@ -0,0 +1,10 @@
+#include "capsule.h"
+#include "utils/utils.h"
+#include <stdlib.h>
+
+void
+capsule_init ()
+{
+    set_debug_flags( secure_getenv("CAPSULE_DEBUG") );
+}
+
diff --git a/capsule/capsule.h b/capsule/capsule.h
index 129de50a818758e15e566f0c57bb725d329eda1d..fd2071f464f42b2a091a93ed28e48ec7368a68c9 100644
--- a/capsule/capsule.h
+++ b/capsule/capsule.h
@@ -26,6 +26,8 @@ typedef struct
     ElfW(Addr) real;
 } capsule_item_t;
 
+void capsule_init (void);
+
 int capsule_relocate (const char *target,
                       void *source,
                       int debug,
diff --git a/generate-stublib.sh b/generate-stublib.sh
index f86da78237166d1688c2759c12282da5a78fbd2b..538cde1cd0deb3d8fc53113bdb97fe5e174bbaaf 100755
--- a/generate-stublib.sh
+++ b/generate-stublib.sh
@@ -139,6 +139,8 @@ cat - <<EOF
      symbol_ns = LM_ID_NEWLM;
      prefix = "/host";
 
+     capsule_init();
+
      dso = capsule_dlmopen( "$proxied_dso", prefix, &symbol_ns, wrappers,
                             0, exclude, &capsule_errno, &capsule_error );
 
diff --git a/utils/debug.h b/utils/debug.h
new file mode 100644
index 0000000000000000000000000000000000000000..188a82a0d892f2164c85e394c8dd587010124937
--- /dev/null
+++ b/utils/debug.h
@@ -0,0 +1,26 @@
+#pragma once
+
+enum
+{
+    DEBUG_NONE       = 0,
+    DEBUG_PATH       = 0x1,
+    DEBUG_SEARCH     = 0x1 << 1,
+    DEBUG_LDCACHE    = 0x1 << 2,
+    DEBUG_CAPSULE    = 0x1 << 3,
+    DEBUG_MPROTECT   = 0x1 << 4,
+    DEBUG_WRAPPERS   = 0x1 << 5,
+    DEBUG_RELOCS     = 0x1 << 6,
+    DEBUG_ALL        = 0xffff,
+};
+
+#ifdef DEBUG
+#define debug(fmt, args...) \
+    fprintf( stderr, "%s:" fmt "\n", __PRETTY_FUNCTION__, ##args )
+#else
+#define debug(fmt, args...) \
+
+#endif
+
+extern unsigned long debug_context;
+extern unsigned long debug_flags;
+void  set_debug_flags (const char *control);
diff --git a/utils/utils.c b/utils/utils.c
index 25910dbf5397780a2e5d193587eeea3206df5ee8..9a7e3a54d405f17407de6945418d6d6487d4fb92 100644
--- a/utils/utils.c
+++ b/utils/utils.c
@@ -20,6 +20,10 @@
 #include <fcntl.h>
 #include <string.h>
 #include <stdio.h>
+#include "debug.h"
+
+unsigned long debug_flags;
+unsigned long debug_context;
 
 char *safe_strncpy (char *dest, const char *src, size_t n)
 {
@@ -108,6 +112,5 @@ void set_debug_flags (const char *control)
             (debug_flags & DEBUG_CAPSULE ) ? 'Y' : 'n' ,
             (debug_flags & DEBUG_MPROTECT) ? 'Y' : 'n' ,
             (debug_flags & DEBUG_WRAPPERS) ? 'Y' : 'n' ,
-            (debug_flags & DEBUG_RELOCS  ) ? 'Y' : 'n' ,
-            (debug_flags & DEBUG_ALL     ) ? 'Y' : 'n' )
+            (debug_flags & DEBUG_RELOCS  ) ? 'Y' : 'n' );
 }
diff --git a/utils/utils.h b/utils/utils.h
index 77d6f28559c625543dc2ada780e22fb7f6ad895b..068e8dd4af1d4434450be456f78d94d68b94c51c 100644
--- a/utils/utils.h
+++ b/utils/utils.h
@@ -17,30 +17,7 @@
 
 #pragma once
 
-enum
-{
-    DEBUG_NONE       = 0,
-    DEBUG_PATH       = 0x1,
-    DEBUG_SEARCH     = 0x1 << 1,
-    DEBUG_LDCACHE    = 0x1 << 2,
-    DEBUG_CAPSULE    = 0x1 << 3,
-    DEBUG_MPROTECT   = 0x1 << 4,
-    DEBUG_WRAPPERS   = 0x1 << 5,
-    DEBUG_RELOCS     = 0x1 << 6,
-    DEBUG_ALL        = 0xffff,
-};
-
-#ifdef DEBUG
-#define debug(fmt, args...) \
-    fprintf( stderr, "%s:" fmt "\n", __PRETTY_FUNCTION__, ##args )
-#else
-#define debug(fmt, args...) \
-
-#endif
-
-extern unsigned long debug_context;
-extern unsigned long debug_flags;
+#include "debug.h"
 
 char *safe_strncpy (char *dest, const char *src, size_t n);
 int   resolve_link (const char *prefix, char *path, char *dir);
-void  set_debug_flags (const char *control);