More changes for the benefit of kbuild.

diff --git a/cp-to-kernel.sh b/cp-to-kernel.sh
new file mode 100644
index 0000000..bc9f915
--- /dev/null
+++ b/cp-to-kernel.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+# Copy klibc to kernel - integrating klibc with the kbuild infrastructure
+# Most files are located in the usr/ directory structure in the kernel
+
+# fetch kernel directory
+if [ -z $1 ]; then
+	echo "Copy klibc to kernel"
+	echo "$0: path-to-kernel-src"
+	exit 1
+fi
+
+kernel=$1
+if [ ! -d $kernel ]; then
+	echo "$kernel is not a kernel directory"
+	exit 1
+fi
+
+if [ -z $2 ]; then
+	echo "Copying source files"
+	# 1) Copy all klibc source files
+	if [ ! -d $kernel/usr/klibc ]; then
+		mkdir $kernel/usr/klibc
+	fi
+	cp -R klibc/* $kernel/usr/klibc
+
+	echo "Copying header files"
+	# 2) And the include files
+	if [ ! -d $kernel/usr/include ]; then
+		mkdir $kernel/usr/include
+	fi
+	cp -R include/* $kernel/usr/include
+fi
+
+echo "Copying kbuild files"
+cp kernel/Kbuild.*          $kernel/scripts
+cp kernel/Kbuild            $kernel/usr
+cp klibc/Kbuild             $kernel/usr/klibc
+cp klibc/syscalls/Kbuild    $kernel/usr/klibc/syscalls
+cp klibc/socketcalls/Kbuild $kernel/usr/klibc/socketcalls
+
+
diff --git a/kernel/Kbuild b/kernel/Kbuild
new file mode 100644
index 0000000..9df200a
--- /dev/null
+++ b/kernel/Kbuild
@@ -0,0 +1,72 @@
+#
+# kbuild file for usr/ - including initramfs image and klibc
+#
+
+obj-y        := initramfs_data.o
+always       := klibc/libc.a
+hostprogs-y  := gen_init_cpio
+clean-files  := initramfs_data.cpio.gz initramfs_list
+
+# Build klibc library
+$(obj)/klibc/libc.a: FORCE
+	$(Q)$(MAKE) -rR -f $(srctree)/scripts/Kbuild.klibc obj=$(obj)/klibc
+
+# initramfs_data.o contains the initramfs_data.cpio.gz image.
+# The image is included using .incbin, a dependency which is not
+# tracked automatically.
+$(obj)/initramfs_data.o: $(obj)/initramfs_data.cpio.gz FORCE
+
+ifdef CONFIG_INITRAMFS_ROOT_UID
+gen_initramfs_args += -u $(CONFIG_INITRAMFS_ROOT_UID)
+endif
+
+ifdef CONFIG_INITRAMFS_ROOT_GID
+gen_initramfs_args += -g $(CONFIG_INITRAMFS_ROOT_GID)
+endif
+
+# The $(shell echo $(CONFIG_INITRAMFS_SOURCE)) is to remove the
+# gratuitous begin and end quotes from the Kconfig string type.
+# Internal, escaped quotes in the Kconfig string will loose the
+# escape and become active quotes.
+quotefixed_initramfs_source := $(shell echo $(CONFIG_INITRAMFS_SOURCE))
+
+filechk_initramfs_list = $(CONFIG_SHELL) \
+  $(srctree)/scripts/gen_initramfs_list.sh $(gen_initramfs_args) \
+                                           $(quotefixed_initramfs_source)
+
+$(obj)/initramfs_list: FORCE
+	$(call filechk,initramfs_list)
+
+quiet_cmd_cpio = CPIO    $@
+      cmd_cpio = ./$< $(obj)/initramfs_list > $@
+
+
+# Check if the INITRAMFS_SOURCE is a cpio archive
+ifneq (,$(findstring .cpio,$(quotefixed_initramfs_source)))
+
+# INITRAMFS_SOURCE has a cpio archive - verify that it's a single file
+ifneq (1,$(words $(quotefixed_initramfs_source)))
+$(error Only a single file may be specified in CONFIG_INITRAMFS_SOURCE (="$(quotefixed_initramfs_source)") when a cpio archive is directly specified.)
+endif
+# Now use the cpio archive directly
+initramfs_data_cpio = $(quotefixed_initramfs_source)
+targets += $(quotefixed_initramfs_source)
+
+else
+
+# INITRAMFS_SOURCE is not a cpio archive - create one
+$(obj)/initramfs_data.cpio: $(obj)/gen_init_cpio \
+                            $(initramfs-y) $(obj)/initramfs_list FORCE
+	$(call if_changed,cpio)
+
+targets += initramfs_data.cpio
+initramfs_data_cpio = $(obj)/initramfs_data.cpio
+
+endif
+
+
+$(obj)/initramfs_data.cpio.gz: $(initramfs_data_cpio) FORCE
+	$(call if_changed,gzip)
+
+targets += initramfs_data.cpio.gz
+
diff --git a/kernel/Kbuild.include b/kernel/Kbuild.include
new file mode 100644
index 0000000..e917947
--- /dev/null
+++ b/kernel/Kbuild.include
@@ -0,0 +1,52 @@
+#
+# Generic kbuild routines
+#
+
+# The temporary file to save gcc -MD generated dependencies must not
+# contain a comma
+depfile = $(subst $(comma),_,$(@D)/.$(@F).d)
+
+
+ifneq ($(KBUILD_NOCMDDEP),1)
+# Check if both arguments has same arguments. Result in empty string if equal
+# User may override this check using make KBUILD_NOCMDDEP=1
+arg-check = $(strip $(filter-out $(1), $(2)) $(filter-out $(2), $(1)) )
+
+endif
+
+# If quiet is set, only print short version of command
+cmd = @$(if $($(quiet)cmd_$(1)),\
+      echo '  $(subst ','\'',$($(quiet)cmd_$(1)))' &&) $(cmd_$(1))
+
+
+# echo command. Short version is $(quiet) equals quiet, otherwise full command
+echo-cmd = $(if $($(quiet)cmd_$(1)), \
+	echo '  $(subst ','\'',$($(quiet)cmd_$(1)))';)
+
+# function to only execute the passed command if necessary
+# >'< substitution is for echo to work,
+# >$< substitution to preserve $ when reloading .cmd file
+# note: when using inline perl scripts [perl -e '...$$t=1;...'] in
+#       $(cmd_xxx) double $$ your perl vars
+# 
+if_changed = $(if $(strip $? $(call arg-check, $(cmd_$(1)), $(cmd_$@)) ), \
+	@set -e; \
+	$(echo-cmd) \
+	$(cmd_$(1)); \
+	echo 'cmd_$@ := $(subst $$,$$$$,$(subst ','\'',$(cmd_$(1))))' \
+	  > $(@D)/.$(@F).cmd)
+
+
+# execute the command and also postprocess generated .d dependencies
+# file
+if_changed_dep = $(if $(strip $? $(filter-out FORCE $(wildcard $^),$^) \
+	$(call arg-check, $(cmd_$(1)), $(cmd_$@)) ),                   \
+	@set -e; \
+	$(echo-cmd) \
+	$(cmd_$(1)); \
+	scripts/basic/fixdep $(depfile) $@ \
+	  '$(subst $$,$$$$,$(subst ','\'',$(cmd_$(1))))' > $(@D)/.$(@F).tmp; \
+	rm -f $(depfile); \
+	mv -f $(@D)/.$(@F).tmp $(@D)/.$(@F).cmd)
+
+
diff --git a/kernel/Kbuild.klibc b/kernel/Kbuild.klibc
new file mode 100644
index 0000000..39b8d32
--- /dev/null
+++ b/kernel/Kbuild.klibc
@@ -0,0 +1,281 @@
+# ==========================================================================
+# Support for building klibc and related programs
+# ==========================================================================
+#
+# To create a kbuild file for a userspace program do the following:
+#
+# Kbuild:
+# 
+# user-progs := cat
+#
+# This will compile a file named cat.c -> the executable 'cat'
+# 
+# If the user space program consist of more files do the following:
+# Kbuild:
+#
+# user-progs := ipconfig
+# ipconfig-y := main.o netdev.c
+
+src := $(obj)
+
+.phony: __build
+__build:
+
+# Generic Kbuild routines
+include $(srctree)/scripts/Kbuild.include
+
+# Defines used when compiling early userspace (klibc programs)
+# ---------------------------------------------------------------------------
+KLIBSRC         := usr/klibc
+
+# Arch specific definitions for klibc
+include $(KLIBSRC)/arch/$(ARCH)/MCONFIG
+
+USERWARNFLAGS   := -Wall -Wpointer-arith -Wwrite-strings                 \
+                   -Wstrict-prototypes -Winline
+
+USERCROSS       := $(CROSS_COMPILE)
+
+USERLD          := $(USERCROSS)ld
+USERCC          := $(USERCROSS)gcc
+USERAR          := $(USERCROSS)ar
+USERRANLIB      := $(USERCROSS)ranlib
+USERSTRIP       := $(USERCROSS)strip
+USERNM          := $(USERCROSS)nm
+
+USERCPPFLAGS    := -I$(srctree)/usr/include/arch/$(ARCH)                 \
+                   -I$(srctree)/usr/include/bits$(BITSIZE)               \
+                   -I$(srctree)/usr/include                              \
+                   -Iinclude                                             \
+                   $(if $(KBUILD_SRC),-Iinclude2 -I$(srctree)/include)   \
+                   -D__KLIBC__ -DBITSIZE=$(BITSIZE)
+USERCFLAGS      := $(USERCPPFLAGS) $(ARCHREQFLAGS) $(OPTFLAGS)           \
+                   $(USERWARNFLAGS)
+USERAFLAGS      := -D__ASSEMBLY__ $(USERCPPFLAGS)
+USERSTRIPFLAGS  := --strip-all -R .comment -R .note
+
+USERLIBGCC      := $(shell $(USERCC) --print-libgcc)
+USERSHAREDFLAGS := $(SHAREDFLAGS)
+USERCRT0        := $(objtree)/$(KLIBSRC)/arch/$(ARCH)/crt0.o
+USERLIBC        := $(objtree)/$(KLIBSRC)/libc.a
+
+#
+# This indicates the location of the final version of the shared library.
+# THIS MUST BE AN ABSOLUTE PATH WITH NO FINAL SLASH.
+# Leave this empty to make it the root.
+#
+SHLIBDIR = /lib
+
+export USERLD USERCC USERAR USERSTRIP USERNM
+export USERCFLAGS USERAFLAGS USERLIBGCC USERSHAREDFLAGS USERSTRIPFLAGS
+export USERCRT0 USERLIBC SHLIBDIR
+
+# kernel configuration
+include .config
+
+# Add $(obj)/ for paths that is not absolute
+objectify = $(foreach o,$(1),$(if $(filter /%,$(o)),$(o),$(obj)/$(o)))
+
+# Kbuild file in the directory that is being build
+include $(obj)/Kbuild
+
+#####
+# user-progs := handling
+
+# user-progs based on a single .c file (with same name)
+user-single := $(foreach p, $(user-progs), $(if $($(p)-y),,$(p)))
+
+# user-progs which is based on several .o files
+user-multi  := $(foreach p, $(user-progs), $(if $($(p)-y),$(p)))
+# objects used for user-progs with more then one .o file
+user-multi-objs := $(foreach p, $(user-multi), $($(p)-y))
+# objects build in this dir
+user-real-objs := $(patsubst %/,,$(user-multi-objs))
+# Directories we need to visit before user-multi-obs are up-to-date
+user-dirs :=  $(patsubst %/,%,$(filter %/, $(user-multi-objs))) 
+# replace all dir/ with dir/built-in.o
+user-multi-objs := $(patsubst %/, %/built-in.o, $(user-multi-objs))
+
+# $(output-dirs) are a list of directories that contain object files
+output-dirs := $(dir $(user-dirs))
+output-dirs += $(foreach f, $(hostprogs-y) $(targets), \
+               $(if $(dir $(f)), $(dir $(f))))
+output-dirs := $(strip $(sort $(filter-out ./,$(output-dirs))))
+
+# prefix so we get full dir
+user-progs      := $(addprefix $(obj)/,$(user-progs))
+user-single     := $(addprefix $(obj)/,$(user-single))
+user-multi      := $(addprefix $(obj)/,$(user-multi))
+user-multi-objs := $(addprefix $(obj)/,$(user-multi-objs))
+user-real-objs  := $(addprefix $(obj)/,$(user-real-objs))
+output-dirs     := $(addprefix $(obj)/,$(output-dirs))
+user-dirs       := $(addprefix $(obj)/,$(user-dirs))
+subdir-y        := $(addprefix $(obj)/,$(subdir-y))
+always          := $(addprefix $(obj)/,$(always))
+targets         := $(addprefix $(obj)/,$(targets))
+
+
+_usercflags    = $(USERCFLAGS) $(EXTRA_USERCFLAGS) $(USERCFLAGS_$(*F).o)
+_useraflags    = $(USERAFLAGS) $(EXTRA_USERAFLAGS) $(USERAFLAGS_$(*F).o)
+
+usercflags     = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(_usercflags)
+useraflags     = -Wp,-MD,$(depfile) $(NOSTDINC_FLAGS) $(_useraflags)
+
+ifneq ($(KBUILD_SRC),)
+# Create output directory if not already present
+_dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj))
+
+# Create directories for object files if directory does not exist
+# Needed when obj-y := dir/file.o syntax is used
+_dummy := $(foreach d,$(output-dirs), $(shell [ -d $(d) ] || mkdir -p $(d)))
+endif
+
+# Do we have to make a built-in.o in this dir?
+ifneq ($(strip $(obj-y) $(obj-n) $(obj-)),)
+builtin-target := $(obj)/built-in.o
+endif
+
+__build: $(subdir-y) $(builtin-target) $(always)
+	@:
+
+# Compile C sources (.c)
+# ---------------------------------------------------------------------------
+
+quiet_cmd_cc_s_c = USERCC  $@
+      cmd_cc_s_c = $(USERCC) $(usercflags) -S -o $@ $< 
+
+%.s: %.c FORCE
+	$(call if_changed_dep,cc_s_c)
+
+quiet_cmd_cc_i_c = USERCPP $@
+      cmd_cc_i_c = $(USERCPP) $(usercflags)   -o $@ $<
+
+%.i: %.c FORCE
+	$(call if_changed_dep,cc_i_c)
+
+quiet_cmd_cc_o_c = USERCC  $@
+      cmd_cc_o_c = $(USERCC) $(usercflags) -c -o $@ $<
+
+%.o: %.c FORCE
+	$(call if_changed_dep,cc_o_c)
+
+# Compile assembler sources (.S)
+# ---------------------------------------------------------------------------
+
+quiet_cmd_as_s_S = USERCPP $@
+      cmd_as_s_S = $(USERCPP) $(useraflags)   -o $@ $< 
+
+%.s: %.S FORCE
+	$(call if_changed_dep,as_s_S)
+
+quiet_cmd_as_o_S = USERAS  $@
+      cmd_as_o_S = $(USERCC) $(useraflags) -c -o $@ $<
+
+%.o: %.S FORCE
+	$(call if_changed_dep,as_o_S)
+
+targets += $(real-objs-y)
+
+#
+# Rule to compile a set of .o files into one .o file
+#
+ifdef builtin-target
+quiet_cmd_link_o_target = LD      $@
+# If the list of objects to link is empty, just create an empty built-in.o
+cmd_link_o_target = $(if $(strip $(obj-y)),\
+                    $(USERLD) $(USERLDFLAGS) -r -o $@ $(filter $(obj-y), $^),\
+                    rm -f $@; $(AR) rcs $@)
+
+$(builtin-target): $(obj-y) FORCE
+	$(call if_changed,link_o_target)
+targets += $(builtin-target)
+endif # builtin-target
+
+
+ifdef user-progs
+# Compile userspace programs for the target
+# ===========================================================================
+
+__build : $(user-dirs) $(user-progs)
+
+# Descend if needed
+$(sort $(addsuffix /built-in.o,$(user-dirs))): $(user-dirs) ;
+
+quiet_cmd_user-ld-single = USERLD  $@
+      cmd_user-ld-single = $(USERLD) $(USERLDFLAGS) -o $@      \
+                           $(USERCRT0) $<                      \
+                           $(filter-out FORCE,$^)              \
+                           $(USERLIBC) $(USERLIBGCC)           \
+                           $(USERLIBGCC);                      \
+                           $(USERSTRIP) $(USERSTRIPFLAGS) $@
+
+$(user-single): %: %.o $(USERCRT0) $(USERLIBC) FORCE
+	$(call if_changed,user-ld-single)
+
+targets += $(user-single) $(user-single:=.o)
+
+multi-deps = $($(subst $(obj)/,,$@-y))
+link-multi-deps = $(addprefix $(obj)/, \
+                    $(patsubst %/, %/built-in.o, $(multi-deps)))
+
+quiet_cmd_user-ld-multi = USERLD  $@
+      cmd_user-ld-multi = $(USERLD) $(USERLDFLAGS) -o $@       \
+                           $(USERCRT0)                         \
+                           $(link-multi-deps)                  \
+                           $(USERLIBC) $(USERLIBGCC)           \
+                           $(USERLIBGCC);                      \
+                           $(USERSTRIP) $(USERSTRIPFLAGS) $@
+
+$(user-multi): $(user-multi-objs) FORCE
+	$(call if_changed,user-ld-multi)
+
+targets += $(user-multi) $(user-real-objs)
+endif
+
+
+# Compile programs on the host
+# ===========================================================================
+ifdef hostprogs-y
+include $(srctree)/scripts/Makefile.host
+endif
+
+# Descending
+# ---------------------------------------------------------------------------
+
+.PHONY: $(subdir-y) $(user-dirs)
+$(subdir-y) $(user-dirs):
+	$(Q)$(MAKE) $(klibc)=$@
+
+# Add FORCE to the prequisites of a target to force it to be always rebuilt.
+# ---------------------------------------------------------------------------
+
+.PHONY: FORCE
+
+FORCE:
+
+# Linking
+# Create a reloctable composite object file
+# ---------------------------------------------------------------------------
+quiet_cmd_userld = USERLD  $@
+      cmd_userld = $(USERLD) -r $(USERLDFLAGS) \
+                                $(EXTRA_USERLDFLAGS) $(USERLDFLAGS_$(@F)) \
+                                $(filter-out FORCE,$^) -o $@
+
+
+# Read all saved command lines and dependencies for the $(targets) we
+# may be building above, using $(if_changed{,_dep}). As an
+# optimization, we don't need to read them if the target does not
+# exist, we will rebuild anyway in that case.
+
+targets := $(wildcard $(sort $(targets)))
+cmd_files := $(wildcard $(foreach f,$(targets),$(dir $(f)).$(notdir $(f)).cmd))
+
+ifneq ($(cmd_files),)
+  include $(cmd_files)
+endif
+
+# Shorthand for $(Q)$(MAKE) -f scripts/Kbuild.klibc obj
+# Usage:
+# $(Q)$(MAKE) $(klibc)=dir
+klibc := -rR -f $(if $(KBUILD_SRC),$(srctree)/)scripts/Kbuild.klibc obj
+
diff --git a/klibc/Kbuild b/klibc/Kbuild
new file mode 100644
index 0000000..be239a6
--- /dev/null
+++ b/klibc/Kbuild
@@ -0,0 +1,149 @@
+#
+# Kbuild file for klibc
+#
+
+libc-y := vsnprintf.o snprintf.o vsprintf.o sprintf.o \
+	  asprintf.o vasprintf.o \
+	  vsscanf.o sscanf.o ctypes.o \
+	  strntoumax.o strntoimax.o \
+	  atoi.o atol.o atoll.o \
+	  strtol.o strtoll.o strtoul.o strtoull.o \
+	  strtoimax.o strtoumax.o \
+	  globals.o exitc.o atexit.o onexit.o \
+	  execl.o execle.o execv.o execvpe.o execvp.o execlp.o execlpe.o \
+	  fork.o wait.o wait3.o waitpid.o system.o setpgrp.o getpgrp.o \
+	  daemon.o \
+	  printf.o vprintf.o fprintf.o vfprintf.o perror.o \
+	  statfs.o fstatfs.o umount.o \
+	  open.o fopen.o fread.o fread2.o fgetc.o fgets.o \
+	  fwrite.o fwrite2.o fputc.o fputs.o puts.o putchar.o \
+	  sleep.o usleep.o strtotimespec.o strtotimeval.o \
+	  raise.o abort.o assert.o alarm.o pause.o \
+	  __signal.o sysv_signal.o bsd_signal.o siglist.o siglongjmp.o \
+	  sigaction.o sigpending.o sigprocmask.o sigsuspend.o \
+	  brk.o sbrk.o malloc.o realloc.o calloc.o mmap.o \
+	  memcpy.o memcmp.o memset.o memccpy.o memmem.o memswap.o \
+	  memmove.o memchr.o memrchr.o \
+	  strcasecmp.o strncasecmp.o strndup.o strerror.o \
+	  strcat.o strchr.o strcmp.o strcpy.o strdup.o strlen.o strnlen.o \
+	  strncat.o strlcpy.o strlcat.o \
+	  strstr.o strncmp.o strncpy.o strrchr.o \
+	  strxspn.o strspn.o strcspn.o strpbrk.o strsep.o strtok.o \
+	  gethostname.o getdomainname.o getcwd.o \
+	  seteuid.o setegid.o \
+	  getenv.o setenv.o putenv.o __put_env.o unsetenv.o \
+	  getopt.o readdir.o \
+	  syslog.o closelog.o pty.o getpt.o isatty.o reboot.o \
+	  time.o utime.o llseek.o nice.o getpriority.o \
+	  qsort.o \
+	  lrand48.o jrand48.o mrand48.o nrand48.o srand48.o seed48.o \
+	  inet/inet_ntoa.o inet/inet_aton.o inet/inet_addr.o \
+	  inet/inet_ntop.o inet/inet_pton.o inet/bindresvport.o \
+	  send.o recv.o
+
+libc-$(CONFIG_KLIBC_ERRLIST) += errlist.o
+
+libc-$(CONFIG_KLIBC_ZLIB)    += \
+	zlib/adler32.o zlib/compress.o zlib/crc32.o zlib/gzio.o \
+	zlib/uncompr.o zlib/deflate.o zlib/trees.o zlib/zutil.o \
+	zlib/inflate.o zlib/infback.o zlib/inftrees.o zlib/inffast.o
+
+#####
+# Add any architecture-specific rules
+include $(obj)/arch/$(ARCH)/Makefile.inc
+
+#####
+# Shared definitions
+LIB      := libc.a
+SOLIB    := libc.so
+SOHASH   := klibc.so
+CRT0     := arch/$(ARCH)/crt0.o
+INTERP_O := interp.o
+
+always   := $(CRT0) $(LIB) $(SOLIB) $(SOHASH) $(INTERP_O)
+LIB      := $(call objectify,$(LIB))
+SOLIB    := $(call objectify,$(SOLIB))
+SOHASH   := $(call objectify,$(SOHASH))
+CRT0     := $(call objectify,$(CRT0))
+INTERP_O := $(call objectify,$(INTERP_O))
+
+targets  := arch/$(ARCH)/crt0.o
+targets  += $(libc-y) $(ARCHOBJS)
+
+# Generate syscall stubs
+subdir-y += syscalls
+# Generate socket calls stubs
+subdir-y += socketcalls
+
+# Tell make to descend before building libs
+$(obj)/syscalls/syscalls.o: $(obj)/syscalls
+$(obj)/socketcalls/socketcalls.o: $(obj)/socketcalls
+
+#####
+# Readable errormessages extracted from src..
+targets += errlist.c
+quiet_cmd_errlist = GEN     $@
+      cmd_errlist = $(PERL) $< $(LINUXINCLUDE) -errlist > $@ || rm -f $@
+
+$(obj)/errlist.c: $(srctree)/$(src)/makeerrlist.pl
+	$(call cmd,errlist)
+
+# full list of dependencies for klibc
+libc-deps = $(call objectify, $(libc-y) $(ARCHOBJS)) \
+            $(call objectify, syscalls/syscalls.o socketcalls/socketcalls.o)
+
+######
+# Build static library: libc.a
+targets += libc.a __static_init.o
+quiet_cmd_libc = USERAR  $@
+      cmd_libc = rm -f $@; \
+                 $(USERAR) cq $@ $(filter-out FORCE,$^); \
+                 $(USERRANLIB) $@
+
+$(LIB): $(call objectify,__static_init.o) $(libc-deps) FORCE
+	$(call if_changed,libc)
+
+######
+# Build shared library
+targets += libc.so __shared_init.o
+
+quiet_cmd_libcso = LD      $@
+      cmd_libcso = $(USERLD) $(USERLDFLAGS) $(USERSHAREDFLAGS) \
+                             -o $@ $(filter-out FORCE,$^) $(USERLIBGCC)
+
+$(SOLIB): $(CRT0) $(call objectify,__shared_init.o) $(libc-deps) FORCE
+	$(call if_changed,libcso)
+
+
+#####
+# Build sha1 hash values
+targets     += klibc.so libc.so.hash
+hostprogs-y := sha1hash
+
+quiet_cmd_solibhash = HASH    $@
+      cmd_solibhash = $(USERNM) $< | egrep '^[0-9a-fA-F]+ [ADRTW] ' | \
+                                        sort | $(obj)/sha1hash > $@
+$(SOLIB).hash: $(SOLIB) $(obj)/sha1hash FORCE
+	$(call if_changed,solibhash)
+
+quiet_cmd_sohash = GEN     $@
+      cmd_sohash = cat $< > $@;                                           \
+                     $(USERSTRIP) $(USERSTRIPFLAGS) $@;                   \
+                     rm -f $(obj)/klibc-??????????????????????.so;        \
+                     ln -f $@ $(obj)/klibc-`cat $(SOLIB).hash`.so
+$(SOHASH): $(SOLIB) $(SOLIB).hash
+	$(call cmd,sohash)
+
+
+#####
+# build interp.o
+targets += interp.o
+
+quiet_cmd_interp = BUILD  $@
+      cmd_interp = $(USERCC) $(usercflags) -D__ASSEMBLY__     \
+                             -DLIBDIR=\"$(SHLIBDIR)\"         \
+			     -DSOHASH=\"`cat $(SOLIB).hash`\" \
+			     -c -o $@ $<
+
+$(INTERP_O): $(obj)/interp.S $(SOLIB).hash
+	$(call if_changed,interp)
diff --git a/klibc/Makefile b/klibc/Makefile
index 181a226..dff813f 100644
--- a/klibc/Makefile
+++ b/klibc/Makefile
@@ -139,8 +139,8 @@
 	$(CC) $(CFLAGS) -Wp,-dM -x c -E -o $@ $<
 
 syscalls.dir: SYSCALLS.i syscalls.pl arch/$(ARCH)/sysstub.ph syscommon.h syscalls.nrs
-	rm -rf syscalls
-	mkdir syscalls
+	rm -f syscalls/*.[Ssco] syscalls/*.obj
+	mkdir -p syscalls
 	$(PERL) syscalls.pl SYSCALLS.i arch/$(ARCH)/sysstub.ph $(ARCH) \
 		$(BITSIZE) syscalls.nrs \
 		syscalls ../include/klibc/havesyscall.h
@@ -150,8 +150,8 @@
 	: Generated by side effect
 
 socketcalls.dir: SOCKETCALLS.def socketcalls.pl socketcommon.h
-	rm -rf socketcalls
-	mkdir socketcalls
+	rm -f socketcalls/*.[Ssco] socketcalls/*.obj
+	mkdir -p socketcalls
 	$(PERL) socketcalls.pl SOCKETCALLS.def $(ARCH) socketcalls
 	touch $@
 
@@ -167,8 +167,8 @@
 	find . -type f -a \( -name \*.[isoa] -o -name \*.l[iso] \) -print0 | xargs -0rt rm -f
 	rm -f *.a *.so *.hash *.syms *.stripped
 	rm -f $(TESTS) tests/*.stripped
-	rm -rf syscalls syscalls.dir
-	rm -rf socketcalls socketcalls.dir
+	rm -f syscalls/*.[Ssco] syscalls/*.obj syscalls.dir
+	rm -f socketcalls/*.[Ssco] socketcalls/*.obj socketcalls.dir
 	rm -f sha1hash errlist.c
 
 spotless: clean
diff --git a/klibc/socketcalls.pl b/klibc/socketcalls.pl
index 024483f..68f0fa1 100644
--- a/klibc/socketcalls.pl
+++ b/klibc/socketcalls.pl
@@ -40,7 +40,7 @@
 	    push(@cargs, "$arg a".$i++);
 	}
 	$nargs = $i;
-	print " \\\n\tsocketcalls/${name}.o";
+	print " \\\n\t${name}.o";
 
 	if ( $arch eq 'i386' ) {
 	    open(OUT, '>', "${outputdir}/${name}.S")
@@ -61,7 +61,7 @@
 	    open(OUT, '>', "${outputdir}/${name}.c")
 		or die "$0: Cannot open ${outputdir}/${name}.c\n";
 
-	    print OUT "#include \"socketcommon.h\"\n";
+	    print OUT "#include \"../socketcommon.h\"\n";
 	    print OUT "\n";
 	    print OUT "#ifndef __NR_${name}\n\n";
 
diff --git a/klibc/syscalls.pl b/klibc/syscalls.pl
index 9cea630..0575fa6 100644
--- a/klibc/syscalls.pl
+++ b/klibc/syscalls.pl
@@ -107,7 +107,7 @@
 	@args = split(/\s*\,\s*/, $argv);
 
 	print HAVESYS "#define _KLIBC_HAVE_SYSCALL_${fname} ${sname}\n";
-	print " \\\n\tsyscalls/${fname}.o";
+	print " \\\n\t${fname}.o";
 	make_sysstub($outputdir, $fname, $type, $sname, $stype, @args);
     } else {
 	die "$file:$.: Could not parse input: \"$line\"\n";