Index: /trunk/Config.kmk
===================================================================
--- /trunk/Config.kmk	(revision 2522)
+++ /trunk/Config.kmk	(revision 2523)
@@ -327,4 +327,7 @@
  endif
 endif
+if1of ($(KBUILD_TARGET), darwin win)
+ TEMPLATE_BIN_LD_DEBUG = split
+endif
 
 #
Index: /trunk/kBuild/footer-misc.kmk
===================================================================
--- /trunk/kBuild/footer-misc.kmk	(revision 2522)
+++ /trunk/kBuild/footer-misc.kmk	(revision 2523)
@@ -68,6 +68,16 @@
 # DIRECTORIES
 #
-_DIR_ALL := $(sort $(addsuffix /,$(patsubst %/,%,$(_DIRS))) $(dir $(_OUT_FILES) $(_OBJS) $(_INSTALLS_FILES) $(_STAGE_FILES)))
-$(foreach directory,$(_INSTALLS_DIRS) $(_STAGE_DIRS), $(eval _DIR_ALL := $(filter-out $(directory),$(_DIR_ALL))))
+_DIR_ALL := $(sort \
+    $(addsuffix /,$(patsubst %/,%,$(_DIRS) )) \
+    $(dir $(_OUT_FILES) $(_OBJS) $(_INSTALLS_FILES) $(_DEBUG_INSTALL_FILES) $(_STAGE_FILES) $(_DEBUG_STAGE_FILES) ) \
+    $(foreach path, $(KBUILD_INST_PATHS), $(PATH_INS)/$(INST_$(path)) $(PATH_STAGE)/$(STAGE_$(path)) ) \
+    $(dir $(patsubst %/,%, $(_INSTALLS_DIRS) $(_STAGE_DIRS) $(_DEBUG_INSTALL_DIRS) $(_DEBUG_STAGE_DIRS)) ) \
+    )
+$(foreach directory, \
+    $(_INSTALLS_DIRS) \
+    $(_STAGE_DIRS) \
+    $(_DEBUG_INSTALL_DIRS) \
+    $(_DEBUG_STAGE_DIRS) \
+    , $(eval _DIR_ALL := $(filter-out $(directory),$(_DIR_ALL))) )
 
 
Index: /trunk/kBuild/footer-pass1.kmk
===================================================================
--- /trunk/kBuild/footer-pass1.kmk	(revision 2522)
+++ /trunk/kBuild/footer-pass1.kmk	(revision 2523)
@@ -85,11 +85,12 @@
 # Staging and install directory target variables.
 local insttype := $(firstword \
-	$($(target)_INSTTYPE) \
-	$($(target)_INSTTYPE.$(bld_trg)) \
+	$($(target)_INSTTYPE.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
 	$($(target)_INSTTYPE.$(bld_trg).$(bld_trg_arch)) \
-	$($(target)_INSTTYPE.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
+	$($(target)_INSTTYPE.$(bld_trg).$(bld_type)) \
 	$($(target)_INSTTYPE.$(bld_trg_arch)) \
 	$($(target)_INSTTYPE.$(bld_trg_cpu)) \
+	$($(target)_INSTTYPE.$(bld_trg)) \
 	$($(target)_INSTTYPE.$(bld_type)) \
+	$($(target)_INSTTYPE) \
 	)
 ifeq ($(insttype),)
@@ -113,20 +114,25 @@
 
 if1of ($(insttype), stage both)
+ local inst := $(strip $(firstdefined \
+	$(target)_INST.$(bld_trg).$(bld_trg_arch).$(bld_type) \
+	$(target)_INST.$(bld_trg).$(bld_trg_arch) \
+	$(target)_INST.$(bld_trg).$(bld_type) \
+	$(target)_INST.$(bld_trg_arch) \
+	$(target)_INST.$(bld_trg_cpu) \
+	$(target)_INST.$(bld_trg) \
+	$(target)_INST.$(bld_type) \
+	$(target)_INST \
+	definst \
+	,value))
  local stage := $(strip $(firstdefined \
 	$(target)_STAGE.$(bld_trg).$(bld_trg_arch).$(bld_type) \
-	$(target)_INST.$(bld_trg).$(bld_trg_arch).$(bld_type) \
 	$(target)_STAGE.$(bld_trg).$(bld_trg_arch) \
-	$(target)_INST.$(bld_trg).$(bld_trg_arch) \
 	$(target)_STAGE.$(bld_trg).$(bld_type) \
-	$(target)_INST.$(bld_trg).$(bld_type) \
 	$(target)_STAGE.$(bld_trg_arch) \
-	$(target)_INST.$(bld_trg_arch) \
+	$(target)_STAGE.$(bld_trg_cpu) \
 	$(target)_STAGE.$(bld_trg) \
-	$(target)_INST.$(bld_trg) \
 	$(target)_STAGE.$(bld_type) \
-	$(target)_INST.$(bld_type) \
 	$(target)_STAGE \
-	$(target)_INST \
-	definst \
+	inst \
 	,value))
  $(target)_1_STAGE := $(stage)
@@ -145,14 +151,4 @@
 
 if1of ($(insttype), both)
- local inst := $(strip $(firstdefined \
-	$(target)_INST.$(bld_trg).$(bld_trg_arch).$(bld_type) \
-	$(target)_INST.$(bld_trg).$(bld_trg_arch) \
-	$(target)_INST.$(bld_trg).$(bld_type) \
-	$(target)_INST.$(bld_trg_arch) \
-	$(target)_INST.$(bld_trg) \
-	$(target)_INST.$(bld_type) \
-	$(target)_INST \
-	definst \
-	,value))
  $(target)_1_INST := $(inst)
  if "$(substr $(inst),-1,1)" == "/" # Multicast support requires addprefix/suffix.
@@ -167,4 +163,79 @@
  $(target)_1_INST_TARGET :=
 endif
+
+# Debug info related stuff.
+local debug_insttype := $(firstword \
+	$($(target)_DEBUG_INSTTYPE.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
+	$($(target)_DEBUG_INSTTYPE.$(bld_trg).$(bld_trg_arch)) \
+	$($(target)_DEBUG_INSTTYPE.$(bld_trg).$(bld_type)) \
+	$($(target)_DEBUG_INSTTYPE.$(bld_trg_arch)) \
+	$($(target)_DEBUG_INSTTYPE.$(bld_trg_cpu)) \
+	$($(target)_DEBUG_INSTTYPE.$(bld_trg)) \
+	$($(target)_DEBUG_INSTTYPE.$(bld_type)) \
+	$($(target)_DEBUG_INSTTYPE) \
+	$(insttype) )
+ifn1of ($(debug_insttype), none both stage)
+ $(error kBuild: Unknown value '$(debug_insttype)' for '$(target)_DEBUG_INSTTYPE'. Valid values are 'none', 'both' and 'stage'.)
+endif
+$(target)_1_DEBUG_INSTTYPE := $(debug_insttype)
+
+if1of ($(debug_insttype), stage both)
+ ifn1of ($(insttype), stage both)
+  $(error kBuild: DEBUG_INSTTYPE=$(debug_insttype) and INSTTYPE=$(insttype) for target '$(target)' are not compatible)
+ endif
+ local debug_stage := $(firstdefined \
+	$(target)_DEBUG_STAGE.$(bld_trg).$(bld_trg_arch).$(bld_type) \
+	$(target)_DEBUG_STAGE.$(bld_trg).$(bld_trg_arch) \
+	$(target)_DEBUG_STAGE.$(bld_trg).$(bld_type) \
+	$(target)_DEBUG_STAGE.$(bld_trg_arch) \
+	$(target)_DEBUG_STAGE.$(bld_trg_cpu) \
+	$(target)_DEBUG_STAGE.$(bld_trg) \
+	$(target)_DEBUG_STAGE.$(bld_type) \
+	$(target)_DEBUG_STAGE \
+	$(target)_DEBUG_INST.$(bld_trg).$(bld_trg_arch).$(bld_type) \
+	$(target)_DEBUG_INST.$(bld_trg).$(bld_trg_arch) \
+	$(target)_DEBUG_INST.$(bld_trg).$(bld_type) \
+	$(target)_DEBUG_INST.$(bld_trg_arch) \
+	$(target)_DEBUG_INST.$(bld_trg_cpu) \
+	$(target)_DEBUG_INST.$(bld_trg) \
+	$(target)_DEBUG_INST.$(bld_type) \
+	$(target)_DEBUG_INST)
+ ifneq ($(debug_stage),)
+  $(target)_1_DEBUG_STAGE := $($(debug_stage))
+ else
+  $(target)_1_DEBUG_STAGE := $(addprefix $(STAGE_DEBUG),$(stage))
+ endif
+ ifndef $(target)_1_DEBUG_STAGE
+  $(target)_1_DEBUG_STAGE := ./
+ endif
+else
+ $(target)_1_DEBUG_STAGE :=
+endif
+
+if1of ($(debug_insttype), both)
+ ifn1of ($(insttype), both)
+  $(error kBuild: DEBUG_INSTTYPE=$(debug_insttype) and INSTTYPE=$(insttype) for target '$(target)' are not compatible)
+ endif
+ local debug_inst := $(firstdefined \
+	$(target)_DEBUG_INST.$(bld_trg).$(bld_trg_arch).$(bld_type) \
+	$(target)_DEBUG_INST.$(bld_trg).$(bld_trg_arch) \
+	$(target)_DEBUG_INST.$(bld_trg).$(bld_type) \
+	$(target)_DEBUG_INST.$(bld_trg_arch) \
+	$(target)_DEBUG_INST.$(bld_trg_cpu) \
+	$(target)_DEBUG_INST.$(bld_trg) \
+	$(target)_DEBUG_INST.$(bld_type) \
+	$(target)_DEBUG_INST)
+ ifneq ($(debug_inst),)
+  $(target)_1_DEBUG_INST := $($(debug_inst))
+ else
+  $(target)_1_DEBUG_INST := $(addprefix $(INST_DEBUG),$(inst))
+ endif
+ ifndef $(target)_1_DEBUG_INST
+  $(target)_1_DEBUG_INST := ./
+ endif
+else
+ $(target)_1_DEBUG_INST :=
+endif
+#$(warning $(NLTAB)$(target)_1_DEBUG_INST=$($(target)_1_DEBUG_INST)$(NLTAB)$(target)_1_DEBUG_STAGE=$($(target)_1_DEBUG_STAGE)$(NLTAB)insttype=$(insttype)$(NLTAB)debug_insttype=$(debug_insttype))
 
 endef # def_pass1_link_common
@@ -292,11 +363,12 @@
 # Determine and set 1_INSTTYPE.
 local insttype := $(firstword \
-	$($(target)_INSTTYPE) \
-	$($(target)_INSTTYPE.$(bld_trg)) \
+	$($(target)_INSTTYPE.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
 	$($(target)_INSTTYPE.$(bld_trg).$(bld_trg_arch)) \
-	$($(target)_INSTTYPE.$(bld_trg).$(bld_trg_arch).$(bld_type)) \
+	$($(target)_INSTTYPE.$(bld_trg).$(bld_type)) \
 	$($(target)_INSTTYPE.$(bld_trg_arch)) \
 	$($(target)_INSTTYPE.$(bld_trg_cpu)) \
+	$($(target)_INSTTYPE.$(bld_trg)) \
 	$($(target)_INSTTYPE.$(bld_type)) \
+	$($(target)_INSTTYPE) \
 	)
 ifeq ($(insttype),)
@@ -323,16 +395,18 @@
  local stage := $(strip $(firstdefined \
 	$(target)_STAGE.$(bld_trg).$(bld_trg_arch).$(bld_type) \
+	$(target)_STAGE.$(bld_trg).$(bld_trg_arch) \
+	$(target)_STAGE.$(bld_trg).$(bld_type) \
+	$(target)_STAGE.$(bld_trg_arch) \
+	$(target)_STAGE.$(bld_trg_cpu) \
+	$(target)_STAGE.$(bld_trg) \
+	$(target)_STAGE.$(bld_type) \
+	$(target)_STAGE \
 	$(target)_INST.$(bld_trg).$(bld_trg_arch).$(bld_type) \
-	$(target)_STAGE.$(bld_trg).$(bld_trg_arch) \
 	$(target)_INST.$(bld_trg).$(bld_trg_arch) \
-	$(target)_STAGE.$(bld_trg).$(bld_type) \
 	$(target)_INST.$(bld_trg).$(bld_type) \
-	$(target)_STAGE.$(bld_trg_arch) \
 	$(target)_INST.$(bld_trg_arch) \
-	$(target)_STAGE.$(bld_trg) \
+	$(target)_INST.$(bld_trg_cpu) \
 	$(target)_INST.$(bld_trg) \
-	$(target)_STAGE.$(bld_type) \
 	$(target)_INST.$(bld_type) \
-	$(target)_STAGE \
 	$(target)_INST \
 	definst \
@@ -353,4 +427,5 @@
 	$(target)_INST.$(bld_trg).$(bld_type) \
 	$(target)_INST.$(bld_trg_arch) \
+	$(target)_INST.$(bld_trg_cpu) \
 	$(target)_INST.$(bld_trg) \
 	$(target)_INST.$(bld_type) \
Index: /trunk/kBuild/footer-pass2-compiling-targets.kmk
===================================================================
--- /trunk/kBuild/footer-pass2-compiling-targets.kmk	(revision 2522)
+++ /trunk/kBuild/footer-pass2-compiling-targets.kmk	(revision 2523)
@@ -137,5 +137,9 @@
 $(out): .MUST_MAKE = $$(comp-cmds-ex $$($(target)_CMDS_PREV_),$$(commands $$@),FORCE)
 endif
-$(out) + $($(target)_2_OUTPUT) +| $($(target)_2_OUTPUT_MAYBE) : \
+$(out) \
++ $($(target)_2_OUTPUT) \
++ $($(target)_2_OUTPUT_DEBUG_FILES) \
++ $($(target)_2_OUTPUT_DEBUG_DIRS) \
++| $($(target)_2_OUTPUT_MAYBE) : \
 		$$$$($(target)_2_DEPEND) \
 		$(value _DEP_LINK_CMDS) \
@@ -143,5 +147,8 @@
 		$$$$($(target)_2_DEPORD)
 	%$$(call MSG_LINK,$(target),$$@,$(tool_do))
-	$$(QUIET)$$(RM) -f -- $(dep) $(out) $($(target)_2_OUTPUT) $($(target)_2_OUTPUT_MAYBE)
+	$$(QUIET)$$(RM) -f -- $(dep) $(out) $($(target)_2_OUTPUT) $($(target)_2_OUTPUT_MAYBE) $($(target)_2_OUTPUT_DEBUG_FILES)
+ifdef $(target)_2_OUTPUT_DEBUG_DIRS
+	$$(QUIET)$$(RM) -Rf -- $($(target)_2_OUTPUT_DEBUG_DIRS)
+endif
 
 $(cmds)
@@ -281,39 +288,143 @@
 #
 # @param   1            The property name.
+# @param   2            The default property name, empty if none.
+# @param   3            What to return if all variables are empty. (optional)
 # @note    Implicit parameters: target, bld_trg, bld_trg_arch, bld_trg_cpu, bld_type.
 # @returns Expanded property value.
 #
-## @todo bld_trg_cpu is missing here.
 define def_fn_prop_get_first_defined
-ifdef $(target)_$(1).$(bld_trg).$(bld_trg_arch).$(bld_type)
- local .RETURN := $($(target)_$(1).$(bld_trg).$(bld_trg_arch).$(bld_type))
-else ifdef $(target)_$(1).$(bld_trg).$(bld_trg_arch)
- local .RETURN := $($(target)_$(1).$(bld_trg).$(bld_trg_arch))
-else ifdef $(target)_$(1).$(bld_trg).$(bld_type)
- local .RETURN := $($(target)_$(1).$(bld_trg).$(bld_type))
-else ifdef $(target)_$(1).$(bld_trg_arch)
- local .RETURN := $($(target)_$(1).$(bld_trg_arch))
-else ifdef $(target)_$(1).$(bld_trg)
- local .RETURN := $($(target)_$(1).$(bld_trg))
-else ifdef $(target)_$(1).$(bld_type)
- local .RETURN := $($(target)_$(1).$(bld_type))
-else ifdef $(target)_$(1)
- local .RETURN := $($(target)_$(1))
-else ifdef $(1).$(bld_trg).$(bld_trg_arch).$(bld_type)
- local .RETURN := $($(1).$(bld_trg).$(bld_trg_arch).$(bld_type))
-else ifdef $(1).$(bld_trg).$(bld_trg_arch)
- local .RETURN := $($(1).$(bld_trg).$(bld_trg_arch))
-else ifdef $(1).$(bld_trg).$(bld_type)
- local .RETURN := $($(1).$(bld_trg).$(bld_type))
-else ifdef $(1).$(bld_trg_arch)
- local .RETURN := $($(1).$(bld_trg_arch))
-else ifdef $(1).$(bld_trg)
- local .RETURN := $($(1).$(bld_trg))
-else ifdef $(1).$(bld_type)
- local .RETURN := $($(1).$(bld_type))
+local .RETURN := $(firstdefined \
+    $(target)_$(1).$(bld_trg).$(bld_trg_arch).$(bld_type) \
+    $(target)_$(1).$(bld_trg).$(bld_trg_arch) \
+    $(target)_$(1).$(bld_trg).$(bld_type) \
+    $(target)_$(1).$(bld_trg_cpu) \
+    $(target)_$(1).$(bld_trg_arch) \
+    $(target)_$(1).$(bld_trg) \
+    $(target)_$(1).$(bld_type) \
+    $(target)_$(1) \
+)
+ifeq ($(.RETURN),)
+ ifneq ($(2),)
+  local .RETURN := $(firstdefined \
+    $(2).$(bld_trg).$(bld_trg_arch).$(bld_type) \
+    $(2).$(bld_trg).$(bld_trg_arch) \
+    $(2).$(bld_trg).$(bld_type) \
+    $(2).$(bld_trg_cpu) \
+    $(2).$(bld_trg_arch) \
+    $(2).$(bld_trg) \
+    $(2).$(bld_type) \
+    $(2) \
+    3 \
+  )
+ else
+  local .RETURN := $(firstdefined \
+    kBuildGlobalDefaults_$(1).$(bld_trg).$(bld_trg_arch).$(bld_type) \
+    kBuildGlobalDefaults_$(1).$(bld_trg).$(bld_trg_arch) \
+    kBuildGlobalDefaults_$(1).$(bld_trg).$(bld_type) \
+    kBuildGlobalDefaults_$(1).$(bld_trg_cpu) \
+    kBuildGlobalDefaults_$(1).$(bld_trg_arch) \
+    kBuildGlobalDefaults_$(1).$(bld_trg) \
+    kBuildGlobalDefaults_$(1).$(bld_type) \
+    kBuildGlobalDefaults_$(1) \
+    3 \
+  )
+ endif
+endif
+local .RETURN := $(strip $($(.RETURN)))
+endef # def_fn_prop_get_first_defined
+
+##
+# Function for getting the first property value (ignoring empty defines).
+#
+# @param   1            The property name.
+# @param   2            The default property name, empty if none.
+# @param   3            What to return if all variables are empty. (optional)
+# @note    Implicit parameters: target, bld_trg, bld_trg_arch, bld_trg_cpu, bld_type.
+# @returns First word.
+#
+define def_fn_prop_get_first_word
+local .RETURN := $(firstword \
+    $($(target)_$(1).$(bld_trg).$(bld_trg_arch).$(bld_type)) \
+    $($(target)_$(1).$(bld_trg).$(bld_trg_arch)) \
+    $($(target)_$(1).$(bld_trg).$(bld_type)) \
+    $($(target)_$(1).$(bld_trg_cpu)) \
+    $($(target)_$(1).$(bld_trg_arch)) \
+    $($(target)_$(1).$(bld_trg)) \
+    $($(target)_$(1).$(bld_type)) \
+    $($(target)_$(1)) \
+)
+ifeq ($(.RETURN),)
+ ifneq ($(2),)
+  local .RETURN := $(firstword \
+    $($(2).$(bld_trg).$(bld_trg_arch).$(bld_type)) \
+    $($(2).$(bld_trg).$(bld_trg_arch)) \
+    $($(2).$(bld_trg).$(bld_type)) \
+    $($(2).$(bld_trg_cpu)) \
+    $($(2).$(bld_trg_arch)) \
+    $($(2).$(bld_trg)) \
+    $($(2).$(bld_type)) \
+    $($(2)) \
+    $(3) \
+  )
+ else
+  local .RETURN := $(firstword \
+    $(kBuildGlobalDefaults_$(1).$(bld_trg).$(bld_trg_arch).$(bld_type)) \
+    $(kBuildGlobalDefaults_$(1).$(bld_trg).$(bld_trg_arch)) \
+    $(kBuildGlobalDefaults_$(1).$(bld_trg).$(bld_type)) \
+    $(kBuildGlobalDefaults_$(1).$(bld_trg_cpu)) \
+    $(kBuildGlobalDefaults_$(1).$(bld_trg_arch)) \
+    $(kBuildGlobalDefaults_$(1).$(bld_trg)) \
+    $(kBuildGlobalDefaults_$(1).$(bld_type)) \
+    $(kBuildGlobalDefaults_$(1)) \
+    $(3) \
+  )
+ endif
+endif
+endef # def_fn_prop_get_first_word
+
+##
+# Install a debug directory.
+# @param    debug_dir           The directory name.
+define def_link_install_debug_dir_rule
+local dir := $(debug_inst_path)/$(debug_inst)$(debug_dir)
+$$(dir): | $$$$(dir $$$$(patsubst %/,%,$$$$@))
+	%$$(call MSG_INST_DIR,$$@)
+	$$(QUIET)$$(MKDIR) -p -- $$@
+$(target)_2_DEBUG_$(debug_var)_TARGET_DIRS += $$(dir)
+endef # def_link_install_debug_dir_rule
+
+##
+# Install a debug file. 
+# @param    debug_file          Src=>Dst file pair.
+define def_link_install_debug_file_rule
+local dst := $(debug_inst_path)/$(debug_inst)$(word 2, $(subst =>,$(SP),$(debug_file)))
+$$(dst): $(word 1, $(subst =>,$(SP),$(debug_file))) | $$$$(dir $$$$@)
+	%$$(call MSG_INST_FILE,$$@,$$<)
+	$$(QUIET)$(debug_install_cmd) $(if $(mode),-m $(mode)) $(if $(uid),-o $(uid)) $(if $(gid),-g $(gid)) -- $$< $$@
+$(target)_2_DEBUG_$(debug_var)_TARGET_FILES += $$(dst)
+endef # def_link_install_debug_file_rule
+
+##
+# Install debug info to $(debug_inst), where debug_inst can be a directory or 
+# file (just like $(inst) and $(stage).  Used for both staging and installing.
+define def_target_install_only_debug
+if "$(substr $(debug_inst),-1,1)" == "/"
+ if "$(debug_inst)" == "./"
+  local debug_inst := 
+ endif
+ local debug_dirs  := $(patsubst $($(target)_0_OUTDIR)/%,%,$($(target)_2_OUTPUT_DEBUG_DIRS))
+ ifneq ($(strip $(root $(debug_dirs))),)
+  $(error kBuild: Bad tool? debug_dirs='$(debug_dirs)', expected all to be under '$(outbase)')
+ endif
+ local debug_files := $(foreach file, $(patsubst $($(target)_0_OUTDIR)/%,%,$($(target)_2_OUTPUT_DEBUG_FILES)) \
+                        ,$($(target)_0_OUTDIR)/$(file)=>$(file))
 else
- local .RETURN := $($(1))
-endif
-endef # def_fn_prop_get_first_defined
+ local debug_files := $(call TOOL_$(tool)_$(tool_do)_DEBUG_INSTALL_FN,$(out),$(outbase),$(debug_inst))
+ local debug_dirs  := $(filter     %/,$(debug_files))
+ local debug_files := $(filter-out %/,$(debug_files))
+endif
+$(foreach debug_dir, $(debug_dirs), $(eval $(def_link_install_debug_dir_rule)))
+$(foreach debug_file, $(debug_files), $(eval $(def_link_install_debug_file_rule)))
+endef
 
 
@@ -365,4 +476,42 @@
  $(typevar) += $(out)
 endif # INSTTYPE == none
+
+# Install debug info.
+$(target)_2_DEBUG_STAGE_TARGET_DIRS :=
+$(target)_2_DEBUG_STAGE_TARGET_FILES :=
+$(target)_2_DEBUG_INSTALL_TARGET_DIRS :=
+$(target)_2_DEBUG_INSTALL_TARGET_FILES :=
+
+if  (   defined($(target)_2_OUTPUT_DEBUG_DIRS) \
+     || defined($(target)_2_OUTPUT_DEBUG_FILES) ) \
+ && "$($(target)_1_DEBUG_INSTTYPE)" != "none"
+ local mode := $(if $(mode),$(mode)$(COMMA)a-x,0644)
+
+ ifeq ($($(target)_1_DEBUG_INSTTYPE),both)
+  local debug_inst_path   := $(PATH_INS)
+  local debug_install_cmd := $(INSTALL)
+  local debug_var         := INSTALL
+  $(foreach debug_inst, $($(target)_1_DEBUG_INST), $(evalval def_target_install_only_debug))
+ endif
+
+ local debug_inst_path    := $(PATH_STAGE)
+ local debug_install_cmd  := $(INSTALL_STAGING)
+ local debug_var          := STAGE
+ if1of ($($(target)_1_DEBUG_INSTTYPE), stage both)
+  $(foreach debug_inst, $($(target)_1_DEBUG_STAGE), $(evalval def_target_install_only_debug))
+ endif
+ if1of ($($(target)_1_INSTTYPE), stage both)
+  ifndef debug_nostage
+   $(foreach debug_inst,$($(target)_1_STAGE), $(evalval def_target_install_only_debug))
+  endif
+ endif
+
+ _DEBUG_STAGE_DIRS    += $($(target)_2_DEBUG_STAGE_TARGET_DIRS)
+ _DEBUG_STAGE_FILES   += $($(target)_2_DEBUG_STAGE_TARGET_FILES)
+ _DEBUG_INSTALL_DIRS  += $($(target)_2_DEBUG_INSTALL_TARGET_DIRS)
+ _DEBUG_INSTALL_FILES += $($(target)_2_DEBUG_INSTALL_TARGET_FILES)
+
+ $(basename $(notdir $(out))):: $($(target)_2_DEBUG_STAGE_TARGET_DIRS) $($(target)_2_DEBUG_STAGE_TARGET_FILES)
+endif
 
 endef # def_target_install_only
@@ -534,10 +683,10 @@
 
 # call the tool
-local cmds := $(TOOL_$(tool)_LINK_LIBRARY_CMDS)
+local  cmds := $(TOOL_$(tool)_LINK_LIBRARY_CMDS)
 ifneq ($(pre_cmds),)
  local cmds := $(TAB)$(pre_cmds)$(NL)$(TAB)$(cmds)
 endif
 ifneq ($(post_cmds),)
- local cmds  := $(cmds)$(NL)$(TAB)$(post_cmds)
+ local cmds := $(cmds)$(NL)$(TAB)$(post_cmds)
 endif
 $(target)_2_OUTPUT := $(TOOL_$(tool)_LINK_LIBRARY_OUTPUT)
@@ -813,14 +962,16 @@
 endif
 
+# Debug info.
+local ld_debug      := $(evalcall def_fn_prop_get_first_defined,LD_DEBUG)
+local debug_inst    := $(evalcall def_fn_prop_get_first_defined,DEBUG_INST)
+local debug_stage   := $(evalcall def_fn_prop_get_first_defined,DEBUG_STAGE)
+local debug_nostage := $(evalcall def_fn_prop_get_first_word,DEBUG_NOSTAGE)
+
 # Custom pre/post-link actions.
-local pre_cmds  := $(evalcall def_fn_prop_get_first_defined,PRE_CMDS)
-local post_cmds := $(evalcall def_fn_prop_get_first_defined,POST_CMDS)
+local pre_cmds      := $(evalcall def_fn_prop_get_first_defined,PRE_CMDS)
+local post_cmds     := $(evalcall def_fn_prop_get_first_defined,POST_CMDS)
 
 # eliminate this guy?
 local objs = $($(target)_2_OBJS)
-
-# installation targets
-local target_type_mode :=
-$(evalval def_target_install_only)
 
 # dependency file
@@ -861,12 +1012,22 @@
  local cmds  := $(cmds)$(NL)$(TAB)$(post_cmds)
 endif
-$(target)_2_OUTPUT := $(TOOL_$(tool)_$(tool_do)_OUTPUT)
-$(target)_2_OUTPUT_MAYBE := $(TOOL_$(tool)_$(tool_do)_OUTPUT_MAYBE)
-$(target)_2_DEPEND := $(TOOL_$(tool)_$(tool_do)_DEPEND) $(deps) $($(target)_2_OBJS)
-$(target)_2_DEPORD := $(TOOL_$(tool)_$(tool_do)_DEPORD) $(dirdep) $(orderdeps)
+$(target)_2_OUTPUT              := $(TOOL_$(tool)_$(tool_do)_OUTPUT)
+$(target)_2_OUTPUT_MAYBE        := $(TOOL_$(tool)_$(tool_do)_OUTPUT_MAYBE)
+if1of ($(ld_debug), split)
+ $(target)_2_OUTPUT_DEBUG_FILES := $(filter-out %/,$(TOOL_$(tool)_$(tool_do)_OUTPUT_DEBUG))
+ $(target)_2_OUTPUT_DEBUG_DIRS  := $(filter     %/,$(TOOL_$(tool)_$(tool_do)_OUTPUT_DEBUG))
+else
+ $(target)_2_OUTPUT_DEBUG_FILES :=
+ $(target)_2_OUTPUT_DEBUG_DIRS  :=
+endif
+$(target)_2_DEPEND              := $(TOOL_$(tool)_$(tool_do)_DEPEND) $(deps) $($(target)_2_OBJS)
+$(target)_2_DEPORD              := $(TOOL_$(tool)_$(tool_do)_DEPORD) $(dirdep) $(orderdeps)
 
 # generate the link rule.
 $(eval $(def_link_rule))
 
+# installation targets
+local target_type_mode :=
+$(evalval def_target_install_only)
 
 # Update globals.
Index: /trunk/kBuild/footer.kmk
===================================================================
--- /trunk/kBuild/footer.kmk	(revision 2522)
+++ /trunk/kBuild/footer.kmk	(revision 2523)
@@ -126,4 +126,8 @@
 _INSTALLS_FILES :=
 _INSTALLS_DIRS :=
+_DEBUG_STAGE_FILES :=
+_DEBUG_STAGE_DIRS :=
+_DEBUG_INSTALL_FILES :=
+_DEBUG_INSTALL_DIRS :=
 _OTHERS   :=
 _PACKING  :=
@@ -242,4 +246,6 @@
 installs:  $$(_INSTALLS_DIRS) $$(_INSTALLS_FILES)
 
+install-debug:  $$(_DEBUG_INSTALL_DIRS) $$(_DEBUG_INSTALL_FILES)
+
 
 
Index: /trunk/kBuild/header.kmk
===================================================================
--- /trunk/kBuild/header.kmk	(revision 2522)
+++ /trunk/kBuild/header.kmk	(revision 2523)
@@ -914,5 +914,5 @@
 PROPS_SINGLE := $(PROPS_TOOLS) TEMPLATE INSTTYPE INST STAGE NOINST BLD_TYPE BLD_TRG BLD_TRG_ARCH BLD_TRG_CPU FETCHDIR \
 	OBJSUFF COBJSUFF CXXOBJSUFF OBJCOBJSUFF OBJCXXOBJSUFF ASOBJSUFF RCOBJSUFF SYSSUFF BINSUFF EXESUFF DLLSUFF LIBSUFF ARLIBSUFF \
-	MODE UID GID
+	MODE UID GID LD_DEBUG DEBUG_INSTTYPE DEBUG_INST DEBUG_STAGE
 ## PROPS_SINGLE_LNK
 # Subset of PROPS_SINGLE which applies to all linkable targets.
@@ -920,5 +920,5 @@
 	INST NOINST BLD_TYPE BLD_TRG BLD_TRG_ARCH BLD_TRG_CPU \
 	OBJSUFF COBJSUFF CXXOBJSUFF OBJCOBJSUFF OBJCXXOBJSUFF ASOBJSUFF RCOBJSUFF \
-	MODE UID GID
+	MODE UID GID LD_DEBUG DEBUG_INSTTYPE DEBUG_INST DEBUG_STAGE
 
 ## PROPS_DEFERRED
@@ -1146,5 +1146,5 @@
 PASS_STAGING        := Staging
 PASS_STAGING_trgs   :=
-PASS_STAGING_vars   := _STAGING_DIRS _INSTALLS _STAGING_FILES
+PASS_STAGING_vars   := _STAGE_DIRS _INSTALLS _STAGE_FILES _DEBUG_STAGE_DIRS _DEBUG_STAGE_FILES
 PASS_STAGING_pass   := staging
 
@@ -1154,5 +1154,5 @@
 PASS_INSTALLS       := Install
 PASS_INSTALLS_trgs  :=
-PASS_INSTALLS_vars  := _INSTALLS_DIRS _INSTALLS_FILES
+PASS_INSTALLS_vars  := _INSTALLS_DIRS _INSTALLS_FILES _DEBUG_INSTALL_DIRS _DEBUG_INSTALL_FILES
 PASS_INSTALLS_pass  := installs
 
Index: /trunk/kBuild/tools/GCC4MACHO.kmk
===================================================================
--- /trunk/kBuild/tools/GCC4MACHO.kmk	(revision 2522)
+++ /trunk/kBuild/tools/GCC4MACHO.kmk	(revision 2523)
@@ -51,4 +51,5 @@
 TOOL_GCC4MACHO_LDFLAGS.sysmod ?= -r
 #TOOL_GCC4MACHO_LD_SONAME = -Wl,-dylib_install_name $(firstword $($(1)_SONAME.$(KBUILD_TARGET).$(KBUILD_TYPE)) $($(1)_SONAME.$(KBUILD_TARGET)) $($(1)_SONAME.$(KBUILD_TYPE)) $($(1)_SONAME) $(notdir $(2)))
+TOOL_GCC4MACHO_DSYMUTIL  ?= dsymutil
 
 ifdef SLKRUNS
@@ -104,4 +105,30 @@
 TOOL_GCC4MACHO_LDFLAGS.debug    ?= -g
 TOOL_GCC4MACHO_LDFLAGS.profile  ?= -g
+
+
+##
+# Calculate the files in the debug bundle.
+# @param    1           The whole output filename.
+# @param    2   	The output filename sans suffix. 
+TOOL_GCC4MACHO_DEBUG_BUNDLE_FN = \
+    $(1).dSYM/ \
+    $(1).dSYM/Contents/ \
+    $(1).dSYM/Contents/Resources/ \
+    $(1).dSYM/Contents/Resources/DWARF/ \
+    $(1).dSYM/Contents/Info.plist \
+    $(1).dSYM/Contents/Resources/DWARF/$(notdir $(1))
+
+##
+# Calculate the files in the debug bundle.
+# @param    1           The whole linker output filename.
+# @param    2   	The linker output filename sans suffix. 
+# @param    3           The desired install name (no dir slash).
+TOOL_GCC4MACHO_DEBUG_INSTALL_FN= \
+    $(3).dSYM/ \
+    $(3).dSYM/Contents/ \
+    $(3).dSYM/Contents/Resources/ \
+    $(3).dSYM/Contents/Resources/DWARF/ \
+    $(1).dSYM/Contents/Info.plist=>$(3).dSYM/Contents/Info.plist \
+    $(1).dSYM/Contents/Resources/DWARF/$(notdir $(1))=>$(3).dSYM/Contents/Resources/DWARF/$(notdir $(1))
 
 
@@ -350,4 +377,6 @@
 # @param    $(outbase)  Output basename (full). Use this for list files and such.
 TOOL_GCC4MACHO_LINK_PROGRAM_OUTPUT = $(outbase).rsp
+TOOL_GCC4MACHO_LINK_PROGRAM_OUTPUT_DEBUG = $(call TOOL_GCC4MACHO_DEBUG_BUNDLE_FN,$(out))
+TOOL_GCC4MACHO_LINK_PROGRAM_DEBUG_INSTALL_FN = $(TOOL_GCC4MACHO_DEBUG_INSTALL_FN)
 TOOL_GCC4MACHO_LINK_PROGRAM_DEPEND = $(foreach lib,$(libs),$(if $(findstring $(lib),$(subst /,x,$(lib))),, $(lib)))
 TOOL_GCC4MACHO_LINK_PROGRAM_DEPORD =
@@ -358,5 +387,9 @@
 		$(foreach p,$(libpath), -L$(p))\
 		$(foreach lib,$(libs), $(if $(findstring $(lib),$(subst /,x,$(lib))), -l$(patsubst lib%,%,$(lib)), $(lib)))
-endef
+ ifeq ($(ld_debug),split)
+	$(QUIET)$(TOOL_GCC4MACHO_DSYMUTIL) --verbose -o $(out).dSYM/ $(out)
+ endif
+endef
+
 
 
@@ -375,4 +408,6 @@
 # @param    $(outbase)  Output basename (full). Use this for list files and such.
 TOOL_GCC4MACHO_LINK_DLL_OUTPUT = $(outbase).rsp
+TOOL_GCC4MACHO_LINK_DLL_OUTPUT_DEBUG = $(call TOOL_GCC4MACHO_DEBUG_BUNDLE_FN,$(out))
+TOOL_GCC4MACHO_LINK_DLL_DEBUG_INSTALL_FN = $(TOOL_GCC4MACHO_DEBUG_INSTALL_FN)
 TOOL_GCC4MACHO_LINK_DLL_DEPEND = $(foreach lib,$(libs),$(if $(findstring $(lib),$(subst /,x,$(lib))),, $(lib)))
 TOOL_GCC4MACHO_LINK_DLL_DEPORD =
@@ -384,4 +419,7 @@
 		$(foreach p,$(libpath), -L$(p))\
 		$(foreach lib,$(libs), $(if $(findstring $(lib),$(subst /,x,$(lib))), -l$(patsubst lib%,%,$(lib)), $(lib)))
+ ifeq ($(ld_debug),split)
+	$(QUIET)$(TOOL_GCC4MACHO_DSYMUTIL) --verbose -o $(out).dSYM/ $(out)
+ endif
 endef
 
@@ -401,4 +439,6 @@
 # @param    $(outbase)  Output basename (full). Use this for list files and such.
 TOOL_GCC4MACHO_LINK_SYSMOD_OUTPUT = $(outbase).rsp
+TOOL_GCC4MACHO_LINK_SYSMOD_OUTPUT_DEBUG = $(call TOOL_GCC4MACHO_DEBUG_BUNDLE_FN,$(out))
+TOOL_GCC4MACHO_LINK_SYSMOD_DEBUG_INSTALL_FN = $(TOOL_GCC4MACHO_DEBUG_INSTALL_FN)
 TOOL_GCC4MACHO_LINK_SYSMOD_DEPEND = $(foreach lib,$(libs),$(if $(findstring $(lib),$(subst /,x,$(lib))),, $(lib)))
 TOOL_GCC4MACHO_LINK_SYSMOD_DEPORD =
@@ -409,4 +449,7 @@
 		$(foreach p,$(libpath), -L$(p))\
 		$(foreach lib,$(libs), $(if $(findstring $(lib),$(subst /,x,$(lib))), -l$(patsubst lib%,%,$(lib)), $(lib)))
-endef
-
+ ifeq ($(ld_debug),split)
+	$(QUIET)$(TOOL_GCC4MACHO_DSYMUTIL) --verbose -o $(out).dSYM/ $(out)
+ endif
+endef
+
Index: /trunk/kBuild/tools/GXX4MACHO.kmk
===================================================================
--- /trunk/kBuild/tools/GXX4MACHO.kmk	(revision 2522)
+++ /trunk/kBuild/tools/GXX4MACHO.kmk	(revision 2523)
@@ -51,4 +51,5 @@
 TOOL_GXX4MACHO_LDFLAGS.sysmod ?= -r
 #TOOL_GXX4MACHO_LD_SONAME = -Wl,-dylib_install_name $(firstword $($(1)_SONAME.$(KBUILD_TARGET).$(KBUILD_TYPE)) $($(1)_SONAME.$(KBUILD_TARGET)) $($(1)_SONAME.$(KBUILD_TYPE)) $($(1)_SONAME) $(notdir $(2)))
+TOOL_GXX4MACHO_DSYMUTIL  ?= dsymutil
 
 ifdef SLKRUNS
@@ -192,5 +193,5 @@
 	$(QUIET)$(APPEND) -n "$(dep)" "" "$(source):" ""
 endef
-endif # !KUSE_OBJCACHE
+endif # !KBUILD_USE_KOBJCACHE
 
 
@@ -236,5 +237,5 @@
 	$(QUIET)$(APPEND) -n "$(dep)" "" "$(source):" ""
 endef
-endif # !KUSE_OBJCACHE
+endif # !KBUILD_USE_KOBJCACHE
 
 
@@ -280,5 +281,5 @@
 	$(QUIET)$(APPEND) -n "$(dep)" "" "$(source):" ""
 endef
-endif # !KUSE_OBJCACHE
+endif # !KBUILD_USE_KOBJCACHE
 
 
@@ -350,4 +351,5 @@
 # @param    $(outbase)  Output basename (full). Use this for list files and such.
 TOOL_GXX4MACHO_LINK_PROGRAM_OUTPUT = $(outbase).rsp
+TOOL_GXX4MACHO_LINK_PROGRAM_OUTPUT_DEBUG = $(out).dSYM/
 TOOL_GXX4MACHO_LINK_PROGRAM_DEPEND = $(foreach lib,$(libs),$(if $(findstring $(lib),$(subst /,x,$(lib))),, $(lib)))
 TOOL_GXX4MACHO_LINK_PROGRAM_DEPORD =
@@ -358,4 +360,7 @@
 		$(foreach p,$(libpath), -L$(p))\
 		$(foreach lib,$(libs), $(if $(findstring $(lib),$(subst /,x,$(lib))), -l$(patsubst lib%,%,$(lib)), $(lib)))
+ ifeq ($(ld_debug),split)
+	$(QUIET)$(TOOL_GXX4MACHO_DSYMUTIL) --verbose -o $(out).dSYM/ $(out)
+ endif
 endef
 
@@ -375,4 +380,5 @@
 # @param    $(outbase)  Output basename (full). Use this for list files and such.
 TOOL_GXX4MACHO_LINK_DLL_OUTPUT = $(outbase).rsp
+TOOL_GXX4MACHO_LINK_DLL_OUTPUT_DEBUG = $(out).dSYM/
 TOOL_GXX4MACHO_LINK_DLL_DEPEND = $(foreach lib,$(libs),$(if $(findstring $(lib),$(subst /,x,$(lib))),, $(lib)))
 TOOL_GXX4MACHO_LINK_DLL_DEPORD =
@@ -384,4 +390,7 @@
 		$(foreach p,$(libpath), -L$(p))\
 		$(foreach lib,$(libs), $(if $(findstring $(lib),$(subst /,x,$(lib))), -l$(patsubst lib%,%,$(lib)), $(lib)))
+ ifeq ($(ld_debug),split)
+	$(QUIET)$(TOOL_GXX4MACHO_DSYMUTIL) --verbose -o $(out).dSYM/ $(out)
+ endif
 endef
 
@@ -401,4 +410,5 @@
 # @param    $(outbase)  Output basename (full). Use this for list files and such.
 TOOL_GXX4MACHO_LINK_SYSMOD_OUTPUT = $(outbase).rsp
+TOOL_GXX4MACHO_LINK_SYSMOD_OUTPUT_DEBUG = $(out).dSYM/
 TOOL_GXX4MACHO_LINK_SYSMOD_DEPEND = $(foreach lib,$(libs),$(if $(findstring $(lib),$(subst /,x,$(lib))),, $(lib)))
 TOOL_GXX4MACHO_LINK_SYSMOD_DEPORD =
@@ -409,4 +419,7 @@
 		$(foreach p,$(libpath), -L$(p))\
 		$(foreach lib,$(libs), $(if $(findstring $(lib),$(subst /,x,$(lib))), -l$(patsubst lib%,%,$(lib)), $(lib)))
-endef
-
+ ifeq ($(ld_debug),split)
+	$(QUIET)$(TOOL_GXX4MACHO_DSYMUTIL) --verbose -o $(out).dSYM/ $(out)
+ endif
+endef
+
