Index: trunk/src/emx/src/lib/libc.smak
===================================================================
--- trunk/src/emx/src/lib/libc.smak	(revision 524)
+++ trunk/src/emx/src/lib/libc.smak	(revision 525)
@@ -51,5 +51,5 @@
 LIBC.OBJS       := $.omf/src/lib/startup/386/dll0.obj $.omf/src/lib/startup/dllinit.obj
 LIBC.LIBS       := $.omf/c.lib $.omf/c_app.lib
-LIBC.DEPS       := $.omf/os2stub.bin
+LIBC.DEPS       := $.omf/os2stub.bin $.omf/c_alias.lib
 LIBC.DLL.OBJS   :=
 LIBC.DIRS       := $(sort $(dir $(LIBC.OBJS) $(LIBC.DEPS) $(LIBC.DLL.OBJS)))
@@ -62,5 +62,12 @@
 LIBC.PRF.DIRS   := $(sort $(dir $(LIBC.PRF.OBJS) $(LIBC.PRF.DEPS)))
 
-TARGDIRS        += $(LIBC.DIRS) $(LIBC.PRF.DIRS)
+LIBC.ELH.DLL	:= $(LIBC.DLL:.dll=.elh)
+LIBC.ELH.DEF    := $.omf/libc.elh.def
+LIBC.ELH.OBJS   := $(LIBC.OBJS)
+LIBC.ELH.LIBS   := $(LIBC.LIBS)
+LIBC.ELH.DEPS   := $(LIBC.DEPS)
+LIBC.ELH.DIRS   :=
+						
+TARGDIRS        += $(LIBC.DIRS) $(LIBC.PRF.DIRS) $(LIBC.ELH.DIRS)
 
 			
@@ -78,5 +85,5 @@
 endif
 
-# LIBC .def file 
+# LIBC .def file
 $(LIBC.DEF): $(LIBC.LIBS) src/lib/libc.def
 	$(call RM,$@)
@@ -118,4 +125,88 @@
 
 	
+# LIBC.ELH - Electric Heap version of libcXY.dll
+$(LIBC.ELH.DLL): $(LIBC.ELH.OBJS) $(LIBC.ELH.LIBS) $(LIBC.ELH.DEF) $(LIBC.ELH.DEPS)
+	$(LD) -o $@ -Zomf $(subst -s,,$(LDFLAGS.DLL)) -Zmap=$@.map -nostdlib  \
+	    $(LIBC.ELH.DEF) \
+		$(LIBC.ELH.OBJS) \
+		$(LIBC.ELH.LIBS) \
+		-L$.omf -los2 -lgcc -lgcc_eh -lc_alias -lend \
+		-Zlinker -STUB:$.omf/os2stub.bin
+	echo $@		
+
+# LIBC .def file (two steps)
+# Man! This sucks! Isn't there support for inline files (<<\nfile\n<<) in GNU MAKE?
+# Even M$/IBM nmake	have that.
+# And no, I couldn't care less about performance with this type of DLL.
+$(LIBC.ELH.DEF): $(LIBC.DEF) src/lib/libc.smak
+	$(call RM,$@)
+	cp $< $@
+	@$(call FECHO,$@,"")
+	@$(call FECHO,$@,"IMPORTS");
+	@$(call FECHO,$@,"  kHeapDbgException              = KLIBDBG.kHeapDbgException")	
+	@$(call FECHO,$@,"  __std_calloc                   = KLIBDBG._gcc__std_calloc")
+	@$(call FECHO,$@,"  __um_default_alloc             = KLIBDBG._gcc__um_default_alloc")
+	@$(call FECHO,$@,"  __um_default_expand            = KLIBDBG._gcc__um_default_expand")
+	@$(call FECHO,$@,"  __um_default_release           = KLIBDBG._gcc__um_default_release")
+	@$(call FECHO,$@,"  __um_default_shrink            = KLIBDBG._gcc__um_default_shrink")
+	@$(call FECHO,$@,"  __std_expand                   = KLIBDBG._gcc__std_expand")
+	@$(call FECHO,$@,"  __std_free                     = KLIBDBG._gcc__std_free")
+	@$(call FECHO,$@,"  __heapchk                      = KLIBDBG._gcc__heapchk")
+	@$(call FECHO,$@,"  __heapmin                      = KLIBDBG._gcc__heapmin")
+	@$(call FECHO,$@,"  __heapset                      = KLIBDBG._gcc__heapset")
+	@$(call FECHO,$@,"  __heap_walk                    = KLIBDBG._gcc__heap_walk")
+	@$(call FECHO,$@,"  __um_seg_addmem                = KLIBDBG._gcc__um_seg_addmem")
+	@$(call FECHO,$@,"  __um_seg_setmem                = KLIBDBG._gcc__um_seg_setmem")
+	@$(call FECHO,$@,"  __um_addmem_nolock             = KLIBDBG._gcc__um_addmem_nolock")
+	@$(call FECHO,$@,"  __um_crumb_alloc               = KLIBDBG._gcc__um_crumb_alloc")
+	@$(call FECHO,$@,"  __um_lump_alloc                = KLIBDBG._gcc__um_lump_alloc")
+	@$(call FECHO,$@,"  __um_alloc_no_lock             = KLIBDBG._gcc__um_alloc_no_lock")
+	@$(call FECHO,$@,"  __um_free_maybe_lock           = KLIBDBG._gcc__um_free_maybe_lock")
+	@$(call FECHO,$@,"  __um_crumb_free_maybe_lock     = KLIBDBG._gcc__um_crumb_free_maybe_lock")
+	@$(call FECHO,$@,"  __um_lump_free_maybe_lock      = KLIBDBG._gcc__um_lump_free_maybe_lock")
+	@$(call FECHO,$@,"  __um_find_bucket               = KLIBDBG._gcc__um_find_bucket")
+	@$(call FECHO,$@,"  __um_lump_unlink_bucket        = KLIBDBG._gcc__um_lump_unlink_bucket")
+	@$(call FECHO,$@,"  __um_lump_unlink_heap          = KLIBDBG._gcc__um_lump_unlink_heap")
+	@$(call FECHO,$@,"  __um_lump_link_heap            = KLIBDBG._gcc__um_lump_link_heap")
+	@$(call FECHO,$@,"  __um_lump_make_free            = KLIBDBG._gcc__um_lump_make_free")
+	@$(call FECHO,$@,"  __um_lump_coalesce_free        = KLIBDBG._gcc__um_lump_coalesce_free")
+	@$(call FECHO,$@,"  __um_init_default_regular_heap = KLIBDBG._gcc__um_init_default_regular_heap")
+	@$(call FECHO,$@,"  __um_init_default_tiled_heap   = KLIBDBG._gcc__um_init_default_tiled_heap")
+	@$(call FECHO,$@,"  __um_realloc                   = KLIBDBG._gcc__um_realloc")
+	@$(call FECHO,$@,"  __um_walk_error                = KLIBDBG._gcc__um_walk_error")
+	@$(call FECHO,$@,"  __um_walk_no_lock              = KLIBDBG._gcc__um_walk_no_lock")
+	@$(call FECHO,$@,"  __std_malloc                   = KLIBDBG._gcc__std_malloc")
+	@$(call FECHO,$@,"  __mheap                        = KLIBDBG._gcc__mheap")
+	@$(call FECHO,$@,"  __msize                        = KLIBDBG._gcc__msize")
+	@$(call FECHO,$@,"  __std_realloc                  = KLIBDBG._gcc__std_realloc")
+	@$(call FECHO,$@,"  __tcalloc                      = KLIBDBG._gcc__tcalloc")
+	@$(call FECHO,$@,"  __tfree                        = KLIBDBG._gcc__tfree")
+	@$(call FECHO,$@,"  __theapmin                     = KLIBDBG._gcc__theapmin")
+	@$(call FECHO,$@,"  __tmalloc                      = KLIBDBG._gcc__tmalloc")
+	@$(call FECHO,$@,"  __trealloc                     = KLIBDBG._gcc__trealloc")
+	@$(call FECHO,$@,"  __uaddmem                      = KLIBDBG._gcc__uaddmem")
+	@$(call FECHO,$@,"  __ucalloc                      = KLIBDBG._gcc__ucalloc")
+	@$(call FECHO,$@,"  __uclose                       = KLIBDBG._gcc__uclose")
+	@$(call FECHO,$@,"  __ucreate                      = KLIBDBG._gcc__ucreate")
+	@$(call FECHO,$@,"  __ucreate2                     = KLIBDBG._gcc__ucreate2")
+	@$(call FECHO,$@,"  __udefault                     = KLIBDBG._gcc__udefault")
+	@$(call FECHO,$@,"  __udestroy                     = KLIBDBG._gcc__udestroy")
+	@$(call FECHO,$@,"  __uheapchk                     = KLIBDBG._gcc__uheapchk")
+	@$(call FECHO,$@,"  __uheapmin                     = KLIBDBG._gcc__uheapmin")
+	@$(call FECHO,$@,"  __uheapset                     = KLIBDBG._gcc__uheapset")
+	@$(call FECHO,$@,"  __umalloc                      = KLIBDBG._gcc__umalloc")
+	@$(call FECHO,$@,"  __uopen                        = KLIBDBG._gcc__uopen")
+	@$(call FECHO,$@,"  __ustats                       = KLIBDBG._gcc__ustats")
+	@$(call FECHO,$@,"  __utcalloc                     = KLIBDBG._gcc__utcalloc")
+	@$(call FECHO,$@,"  __utdefault                    = KLIBDBG._gcc__utdefault")
+	@$(call FECHO,$@,"  __utmalloc                     = KLIBDBG._gcc__utmalloc")
+	@$(call FECHO,$@,"  __uheap_type                   = KLIBDBG._gcc__uheap_type")
+	@$(call FECHO,$@,"  __uheap_walk                   = KLIBDBG._gcc__uheap_walk")
+	@$(call FECHO,$@,"  __uheap_walk2                  = KLIBDBG._gcc__uheap_walk2")
+	@$(call FECHO,$@,"  ")
+	@$(call FECHO,$@,"  __um_regular_heap			   = KLIBDBG._gcc__um_regular_heap")
+	@$(call FECHO,$@,"  __um_tiled_heap                = KLIBDBG._gcc__um_tiled_heap")
+	
+					
 # Put libc into the big picture. (semihacks)
 DO.HELP.MODULES += $(call ECHO,    libc-dll - The dynamic C library (.dll))$(NL)
@@ -125,10 +216,12 @@
 libc-dll: emxbind emxexp app alias libc os2 \
 			$(LIBC.DIRS) $(LIBC.DLL) $(LIBC.IMPLIB) \
-			$(LIBC.PRF.DIRS) $(LIBC.PRF.DLL)
+			$(LIBC.PRF.DIRS) $(LIBC.PRF.DLL) \
+			$(LIBC.ELH.DIRS) $(LIBC.ELH.DLL)
 
 # Installation.
 INS.FILES       += $(INS)lib/$(notdir $(LIBC.DLL)) \
   $(addprefix $(INS)lib/,$(notdir $(LIBC.IMPLIB))) \
-  $(INS)lib/$(notdir $(LIBC.PRF.DLL))
+  $(INS)lib/$(notdir $(LIBC.PRF.DLL)) \
+  $(INS)lib/$(notdir $(LIBC.ELH.DLL))
 
 $(INS)lib/$(notdir $(LIBC.DLL)): $(LIBC.DLL)
@@ -138,4 +231,7 @@
 	$(call CP,$<,$@)
 
+$(INS)lib/$(notdir $(LIBC.ELH.DLL)): $(LIBC.ELH.DLL)
+	$(call CP,$<,$@)
+						
 ifeq ($(MODE),opt)
 INS.FILES       += $(INS)lib/$(notdir $(LIBC.DLL:.dll=.dbg))
Index: trunk/src/emx/src/lib/sys/__init.c
===================================================================
--- trunk/src/emx/src/lib/sys/__init.c	(revision 524)
+++ trunk/src/emx/src/lib/sys/__init.c	(revision 525)
@@ -356,4 +356,12 @@
 }
 
+/* from kLib/kHeapDbg.h */
+typedef enum { enmRead, enmWrite, enmUnknown } ENMACCESS;
+extern BOOL _Optlink kHeapDbgException(void *    pvAccess,
+                                       ENMACCESS enmAccess,
+                                       void *    pvIP,
+                                       void *    pvOS);
+/* Weak on _Optlink and _System doesn't work because of underscoring. */
+asm (".weak kHeapDbgException");
 
 ULONG _sys_exception (PEXCEPTIONREPORTRECORD report,
@@ -379,4 +387,30 @@
       break;
     case XCPT_ACCESS_VIOLATION:
+      /* If we're linking libc01.elh or someone is linking static libc
+         together with kLib the electric fence heap will get the opportunity
+         to check if any access violation was caused by someone touching any
+         of the electric fences. */
+      if (    kHeapDbgException
+          &&  (   report->ExceptionInfo[0] == XCPT_READ_ACCESS
+               || report->ExceptionInfo[0] == XCPT_WRITE_ACCESS
+               || report->ExceptionInfo[0] == XCPT_EXECUTE_ACCESS
+               || report->ExceptionInfo[0] == XCPT_UNKNOWN_ACCESS)
+          )
+        {
+          ENMACCESS enmAccess = enmRead;
+          switch (report->ExceptionInfo[0])
+          {
+            case XCPT_WRITE_ACCESS:     enmAccess = enmWrite; break;
+            case XCPT_UNKNOWN_ACCESS:   enmAccess = enmUnknown; break;
+          }
+          /* This call returns true if the page was commited in order
+             to workaround the immediate problem. If it returns false
+             the default action should be taken. */
+          if (kHeapDbgException((void*)report->ExceptionInfo[1],
+                                enmAccess,
+                                report->ExceptionAddress,
+                                report))
+                  return XCPT_CONTINUE_EXECUTION;
+        }
     case XCPT_DATATYPE_MISALIGNMENT:
       return _sys_trap (SIGSEGV);
