Index: trunk/src/emx/Makefile
===================================================================
--- trunk/src/emx/Makefile	(revision 155)
+++ trunk/src/emx/Makefile	(revision 156)
@@ -7,4 +7,5 @@
 #
 # Author: Andrew Zabolotny <zap@cobra.ru>
+# $Id$
 #
 # All Rights Reserved
@@ -67,4 +68,5 @@
 # Linker flags
 LDFLAGS = -Zmt $(LDFLAGS.$(MODE)) $(LDFLAGS.KIND)
+LDFLAGS.DLL = $(LDFLAGS) -Zdll
 # Linker flags for different build modes
 LDFLAGS.opt = -s
@@ -76,5 +78,16 @@
 LDFLAGS.KIND = $(foreach x,$(subst -, ,$(firstword $(subst /, ,$(subst $.,,$@)))),$(LDFLAGS.$x))
 # How to link a .exe file
-DO.LINK.exe = $(LD) $(strip $(LDFLAGS) $1) -o $@ $(^O) $(^DEF) $(^LIB)
+DO.LINK.exe = $(LD) $(strip $(LDFLAGS) $(filter-out -l%,$1)) -o $@ $(^O) $(^LIB) $(filter -l%,$1)
+# How to link a .dll file
+DO.LINK.dll = $(LD) $(strip $(LDFLAGS.DLL) $(filter-out -l%,$1)) -o $@ $(^O) $(^DEF) $(^LIB) $(filter -l%,$1)
+
+# emxbind tool
+EMXBIND = $(if $(wildcard $./$(TOOLFMT)/emxbind$E),$.$(TOOLFMT)/)emxbind$E
+# emxbind flags
+EMXBINDFLAGS = -q $(EMXBINDFLAGS.$(MODE))
+EMXBINDFLAGS.opt = -s
+EMXBINDFLAGS.dbg =
+# Flags for emxbind
+DO.EMXBIND = $(EMXBIND) $(EMXBINDFLAGS) -o $@ $1
 
 # The macro assembler
@@ -87,14 +100,15 @@
 AR = $(if $(findstring .lib,$@),emxomf)ar
 ARFLAGS = crs
-DO.LIBRARY = $(AR) $(ARFLAGS) $@ $(^O)
+DO.LIBRARY = $(call RM,$@); $(AR) $(ARFLAGS)$1 $@ $(^O)
+
+# The tool to extract exports from object files and archives
+EMXEXP = $(if $(wildcard $./$(TOOLFMT)/emxexp$E),$.$(TOOLFMT)/)emxexp$E
 
 # The tool to create import libraries
-IMPLIB = $(if $(wildcard $(IMPLIB.BUILT)),$(IMPLIB.BUILT),emximp$E)
-IMPLIB.BUILT = $./$(TOOLFMT)/emximp$E
-IMPLIBFLAGS.aout.prof = -m
-IMPLIBFLAGS.omf =
-IMPLIBFLAGS.KIND = $(foreach x, $(subst -, ,$(firstword $(subst /, ,$(subst $.,,$@)))) \
-  $(subst -,.,$(firstword $(subst /, ,$(subst $.,,$@)))),$(IMPLIBFLAGS.$x))
-DO.IMPLIB = $(IMPLIB) $(IMPLIBFLAGS.KIND) -o $@ $1
+IMPLIB = $(if $(wildcard $./$(TOOLFMT)/emximp$E),$.$(TOOLFMT)/)emximp$E
+IMPLIBFLAGS.prof = -m
+IMPLIBFLAGS.KIND = $(foreach x,$(subst -, ,$(firstword $(subst /, ,$(subst $.,,$@)))),$(IMPLIBFLAGS.$x))
+DO.IMPLIB = $(IMPLIB) -o $@ $(strip $1 $(IMPLIBFLAGS.KIND)) $(^I)\
+  $(if $(^O),$(NL)$(AR) rs $@ $(^O))
 
 # How to create dependencies
@@ -112,4 +126,6 @@
 # How to filter just the object files from $^
 ^O = $(filter %.o,$^)
+# Just the .imp files from $^
+^I = $(filter %.imp,$^)
 # How to filter just the .def files from $^
 ^DEF = $(filter %.def,$^)
@@ -177,5 +193,5 @@
 
 #------------ Submakefiles ------------
-SUBMAK := $(wildcard src/*/*.smak)
+SUBMAK := version.smak $(wildcard src/*/*.smak)
 
 # Include all submakefiles
@@ -189,5 +205,6 @@
 help:
 	@$(call ECHO,$(SEP))
-	@$(call ECHO,Welcome to GNU C for OS/2 runtime build system!)
+	@$(call ECHO,Welcome to $(PACKAGE) build system!)
+	@$(call ECHO,$(COPYRIGHT))
 	@$(call ECHO,To build something, type 'make {target} {vars}', where {target} is one of:)
 	@$(call ECHO,    all - build all available modules)
Index: trunk/src/emx/build.txt
===================================================================
--- trunk/src/emx/build.txt	(revision 155)
+++ trunk/src/emx/build.txt	(revision 156)
@@ -96,5 +96,6 @@
 
 .MDEP - (optional) module dependencies. This is useful if current module
-  requires another module to be built first (e.g. a library).
+  requires another module to be built first (e.g. a library). It usually
+  contains the name of another module (e.g. .MDEP := os2).
 
 .TARGET - The name of target (e.g. executable or library file name without dir).
Index: trunk/src/emx/comend.smak
===================================================================
--- trunk/src/emx/comend.smak	(revision 155)
+++ trunk/src/emx/comend.smak	(revision 156)
@@ -4,10 +4,8 @@
 # Clear local variables so that we don't incidentally
 # get a wrong value in a different module
+ifndef .TKEEP
 .OBJS :=
 .DIRS :=
 .TARG :=
-
-# Clear all target-specific variables
-ifndef .TKEEP
 .TARGET :=
 .TKIND :=
@@ -16,4 +14,5 @@
 .TCF :=
 .TDEP :=
+.IMPS :=
 endif
 
Index: trunk/src/emx/common.smak
===================================================================
--- trunk/src/emx/common.smak	(revision 155)
+++ trunk/src/emx/common.smak	(revision 156)
@@ -28,4 +28,5 @@
 .TKIND := $(.tmp)
 .TKIND.DIR := $(subst $(SPACE),-,$(.TKIND))/
+.DIRS :=
 
 ifdef .TARGET
@@ -33,4 +34,5 @@
 # .TARG is same as .TARGET except that it has a st/ or mt/ prefix
 .TARG := $(.TKIND.DIR)$(.TARGET)
+.DIRS += $.$(.TKIND.DIR)
 
 ifdef .INSDIR
@@ -61,6 +63,5 @@
 
 .OBJS := $(addprefix $.,$(call OBJFILE,$(.TSRC)))
-.DIRS := $(sort $(dir $(.OBJS)))
-TARGDIRS += $(.DIRS)
+.DIRS += $(sort $(dir $(.OBJS)))
 
 # Remove the names of files for which we already generated build rules
@@ -97,9 +98,16 @@
 endif # def .TSRC
 
+# Add target directories to module dependencies and to overall list of dirs
+ifneq ($(.DIRS),)
+TARGDIRS += $(.DIRS)
+ifdef .MODULE
+$(.MODULE): $(.DIRS)
+endif # def .MODULE
+endif # neq ($(.DIRS),)
+
+# Add the target to the list of module dependencies
 ifdef .TARGET
-ifdef .MODULE
-$(.MODULE): $(.DIRS) $.$(.TARG)
-endif
-endif
+$(.MODULE): $.$(.TARG)
+endif # def .TARGET
 
 # Replace the special sequence in .TDEP @O@ with $.$(.TKIND.DIR)
Index: trunk/src/emx/include/os2thunk.h
===================================================================
--- trunk/src/emx/include/os2thunk.h	(revision 155)
+++ trunk/src/emx/include/os2thunk.h	(revision 156)
@@ -19,5 +19,5 @@
 #define _THUNK_PASCAL_FLAT(ARG)     _THUNK_PASCAL_LONG (_emx_32to16 (ARG))
 #define _THUNK_PASCAL_FAR16(ARG)    _THUNK_PASCAL_LONG (ARG)
-#define _THUNK_PASCAL_FUNCTION(FUN) _16_##FUN
+#define _THUNK_PASCAL_FUNCTION(FUN) APIENTRY _16_##FUN
 #define _THUNK_PASCAL_CALL(FUN)     _emx_thunk1 (_tb, (void *)(_16_##FUN)); })
 #define _THUNK_PASCAL_CALLI(FUN)    _emx_thunk1 (_tb, (void *)(FUN)); })
Index: trunk/src/emx/mklib.smak
===================================================================
--- trunk/src/emx/mklib.smak	(revision 155)
+++ trunk/src/emx/mklib.smak	(revision 156)
@@ -10,27 +10,4 @@
 	$(call FECHO,_@,	$$(DO.LIBRARY))$(NL)
 
-# The rule for building the OMF variant of library from a.out format
-ifneq ($(findstring aout,$(.TKIND)),)
-# Remove the .a suffix from library name
-.TARGET := $(patsubst %$A,%,$(.TARGET))
-# Replace 'aout' with 'omf' in .TKIND
-.TKIND := $(subst aout,omf,$(.TKIND))
-# Compute the new output file directory
-.TKIND.DIR := $(subst $(SPACE),-,$(.TKIND))/
-# Append the .lib suffix to target name
-.TARGET := $(.TARGET)$A
-# Compute the name of target file to build
-.TARG@OMF := $(.TKIND.DIR)$(.TARGET)
-# The rule for building the xxx@omf variant of target
-.PHONY: $(.MODULE)@omf
-MODULES += $(.MODULE)@omf
-DO.INSTALL += $(call CP,$.$(.TARG@OMF),$(INS)$(.INSDIR)$(.TARGET))$(NL)
-.DIRS@OMF := $(dir $.$(.TARG@OMF))
-TARGDIRS += $(.DIRS@OMF)
-$(.MODULE)@omf: $(.MODULE) $(.DIRS@OMF) $.$(.TARG@OMF)
-# bird: Add a dependency rule to help make
-RULES += \
-	$(call FECHO,_@,$.$(.TARG@OMF): $.$(.TARG))$(NL)
-endif # neq ($(findstring aout,$(.TKIND)),)
-
+include mkomflib.smak
 include comend.smak
Index: trunk/src/emx/src/emxbind/emxbind.h
===================================================================
--- trunk/src/emx/src/emxbind/emxbind.h	(revision 155)
+++ trunk/src/emx/src/emxbind/emxbind.h	(revision 156)
@@ -488,5 +488,5 @@
 
 void build_sym_hash_table (void);
-struct nlist *find_symbol (const char *name, int underscore);
+struct nlist *find_symbol (const char *name);
 void sort_fixup (void);
 void create_fixup (const struct fixup *fp, int neg);
Index: trunk/src/emx/src/emxbind/emxbind.smak
===================================================================
--- trunk/src/emx/src/emxbind/emxbind.smak	(revision 155)
+++ trunk/src/emx/src/emxbind/emxbind.smak	(revision 156)
@@ -8,3 +8,14 @@
 .TSRC   := $(wildcard src/emxbind/*.c)
 .TLDF   := -lmoddef
+.TKEEP	:= 1
 include mkexe.smak
+
+.TARG := $(TOOLFMT)/os2stub.bin
+DO.INSTALL += $(call CP,$.$(.TARG),$(INS)$(.INSDIR))$(NL)
+
+emxbind: $.$(.TARG)
+
+$.$(.TARG): src/emxbind/os2stub.bin
+	$(call CP,$< $@)
+
+include comend.smak
Index: trunk/src/emx/src/emxbind/export.c
===================================================================
--- trunk/src/emx/src/emxbind/export.c	(revision 155)
+++ trunk/src/emx/src/emxbind/export.c	(revision 156)
@@ -85,46 +85,4 @@
   put_grow (table, &ord16, 2);
 }
-
-#if 0
-
-/* Find the symbol NAME in the a.out symbol table of the input
-   executable.  If UNDERSCORE is true, an underscore is prepended to
-   NAME.  If the symbol is found, find_symbol() returns a pointer to
-   the symbol table entry.  Otherwise, NULL is returned. */
-
-static struct nlist *find_symbol (const char *name, int underscore)
-{
-  int i, j, n, len, ok, t;
-  const byte *s;
-
-  i = 4; len = strlen (name);
-  while (i < a_in_str_size)
-    {
-      ok = TRUE; s = name;
-      if (underscore)
-	{
-	  if (str_image[i] == '_')
-	    ++i;
-	  else
-	    ok = FALSE;
-	}
-      if (ok && memcmp (name, str_image+i, len+1) == 0)
-	{
-	  n = a_in_h.sym_size / sizeof (struct nlist);
-	  if (underscore) --i;
-	  for (j = 0; j < n; ++j)
-	    if (sym_image[j].string == i)
-	      {
-		t = sym_image[j].type & ~N_EXT;
-		if (t == N_TEXT || t == N_DATA || t == N_BSS)
-		  return sym_image+j;
-	      }
-	}
-      i += strlen (str_image+i) + 1;
-    }
-  return NULL;
-}
-
-#endif
 
 /* Compare two entries of an int array for qsort().  This function is
@@ -190,5 +148,5 @@
   for (i = 0; i < export_len; ++i)
     {
-      nl = find_symbol (export_data[i].internalname, TRUE);
+      nl = find_symbol (export_data[i].internalname);
       if (nl == NULL)
         error ("symbol %s undefined (EXPORTS)", export_data[i].internalname);
Index: trunk/src/emx/src/emxbind/fixup.c
===================================================================
--- trunk/src/emx/src/emxbind/fixup.c	(revision 155)
+++ trunk/src/emx/src/emxbind/fixup.c	(revision 156)
@@ -98,10 +98,9 @@
 
 
-/* Find the symbol NAME in the a.out symbol table of the input
-   executable.  If UNDERSCORE is true, an underscore is prepended to
-   NAME.  If the symbol is found, find_symbol() returns a pointer to
-   the symbol table entry.  Otherwise, NULL is returned. */
-
-struct nlist *find_symbol (const char *name, int underscore)
+/* Find the symbol NAME in the a.out symbol table of the input executable.
+   If the symbol is found, find_symbol() returns a pointer to the symbol
+   table entry.  Otherwise, NULL is returned. */
+
+struct nlist *find_symbol (const char *name)
 {
   int j, len;
@@ -109,10 +108,4 @@
 
   len = strlen (name);
-  if (underscore)
-  {
-    name1 = alloca (len + 2);
-    ((char *)name1)[0] = '_';
-    memcpy (((char *)name1) + 1, name, len + 1);
-  }
 
   for (j = sym_hash_table[sym_hash (name1)]; j != -1;
@@ -499,5 +492,5 @@
     }
   mod_idx = find_module (mod, NO_ADDR);
-  if (memcmp ("__16_", name1, 5) == 0)
+  if (memcmp ("_16_", name1, 4) == 0)
     {
       if (r->pcrel)
Index: trunk/src/emx/src/emxbind/map.c
===================================================================
--- trunk/src/emx/src/emxbind/map.c	(revision 155)
+++ trunk/src/emx/src/emxbind/map.c	(revision 156)
@@ -160,6 +160,4 @@
 {
   int i;
-
-  if (*sym_name == '_') ++sym_name;
 
   /* TODO: Use hashing. */
@@ -218,5 +216,4 @@
               grow_map_sym_table ();
               name = sym_image[i].string + str_image;
-              if (*name == '_') ++name;
               map_sym_table[map_sym_count].seg = seg;
               map_sym_table[map_sym_count].addr = addr;
Index: trunk/src/emx/src/emxexp/emxexp.c
===================================================================
--- trunk/src/emx/src/emxexp/emxexp.c	(revision 155)
+++ trunk/src/emx/src/emxexp/emxexp.c	(revision 156)
@@ -206,6 +206,4 @@
       {
         name = str_ptr + sym_ptr[i].string;
-        if (*name == '_')
-          ++name;
         export (name);
       }
@@ -215,6 +213,4 @@
       {
         name = str_ptr + sym_ptr[i].string;
-        if (*name == '_')
-          ++name;
         export_bss (name);
       }
Index: trunk/src/emx/src/emxload/emxload.smak
===================================================================
--- trunk/src/emx/src/emxload/emxload.smak	(revision 155)
+++ trunk/src/emx/src/emxload/emxload.smak	(revision 156)
@@ -1,4 +1,5 @@
 .MODULE := emxload
 .MDESC  := Fast loader daemon
+.MDEP	:= os2
 .INSDIR := bin/
 
@@ -6,4 +7,4 @@
 .TKIND	:= $(TOOLFMT)
 .TSRC   := $(wildcard src/emxload/*.c)
-.TLDF   := 
+.TDEP	:= @O@os2$A
 include mkexe.smak
Index: trunk/src/emx/src/emxomf/emxomf.c
===================================================================
--- trunk/src/emx/src/emxomf/emxomf.c	(revision 155)
+++ trunk/src/emx/src/emxomf/emxomf.c	(revision 156)
@@ -1083,5 +1083,5 @@
         name = str_ptr + sym_ptr[i].string;
         sym_more[i].index = sym_index++;
-        if (memcmp (name, "__16_", 5) == 0)
+        if (memcmp (name, "_16_", 4) == 0)
           sym_more[i].flags |= SF_FAR16;
         add_extdef (&started, name);
@@ -1334,5 +1334,5 @@
         name = str_ptr + sym_ptr[i].string;
         sym_more[i].index = sym_index++;
-        if (memcmp (name, "__16_", 5) == 0)
+        if (memcmp (name, "_16_", 4) == 0)
           sym_more[i].flags |= SF_FAR16;
         size = sym_ptr[i].value;
Index: trunk/src/emx/src/emxomf/emxomf.smak
===================================================================
--- trunk/src/emx/src/emxomf/emxomf.smak	(revision 155)
+++ trunk/src/emx/src/emxomf/emxomf.smak	(revision 156)
@@ -2,5 +2,5 @@
 .MDESC	:= a.out to OMF object file conversion utility
 .INSDIR	:= bin/
-.MDEP	:= omflib@omf moddef@omf
+.MDEP	:= omflib@omf moddef@omf os2
 
 .TARGET	:= emxomf.exe
@@ -19,5 +19,5 @@
 .TKIND	:= $(TOOLFMT)
 .TSRC	:= src/emxomf/emxomfld.c
-.TDEP	:= @O@moddef$A
+.TDEP	:= @O@moddef$A @O@os2$A
 include mkexe.smak
 
Index: trunk/src/emx/src/ld/ld.c
===================================================================
--- trunk/src/emx/src/ld/ld.c	(revision 155)
+++ trunk/src/emx/src/ld/ld.c	(revision 156)
@@ -82,9 +82,6 @@
 
 #define FIXSETBUG
-#ifdef _System
-extern int _System DosCopy (char *, char *, int);
-#else
-extern int DosCopy (char *, char *, int);
-#endif
+/* gcc will warn on __attribute__((system)) but still will compile */
+extern int __attribute__((system)) DosCopy (char *, char *, int);
 #endif /* EMX */
 
Index: trunk/src/emx/src/ld/ld.smak
===================================================================
--- trunk/src/emx/src/ld/ld.smak	(revision 155)
+++ trunk/src/emx/src/ld/ld.smak	(revision 156)
@@ -3,4 +3,5 @@
 .MODULE := ld
 .MDESC  := Old GNU ld, used for a.out linking
+.MDEP	:= os2
 .INSDIR := bin/
 
@@ -9,4 +10,5 @@
 .TSRC   := $(wildcard src/ld/*.c)
 .TCF	:= -DEMX
+.TDEP	:= @O@os2$A
 .TLDF   := -liberty
 include mkexe.smak
Index: trunk/src/emx/src/lib/alias/alias.smak
===================================================================
--- trunk/src/emx/src/lib/alias/alias.smak	(revision 155)
+++ trunk/src/emx/src/lib/alias/alias.smak	(revision 156)
@@ -8,8 +8,8 @@
 .MODULE	:= alias
 .MDESC	:= A library of underscored aliases for most C library functions
-.MDEP	:= libc sys app
+.MDEP	:= libc app
 .TKIND	:= aout
 .INSDIR := lib/
-.TARGET := c_alias$A
+.TARGET := c_alias.a
 include common.smak
 
@@ -25,5 +25,5 @@
 # For every aliased function create a .o file containing the alias
 # and add it to the target library.
-$.$(.TARG): $.$(.TKIND.DIR)c.a $.$(.TKIND.DIR)sys.a $.$(.TKIND.DIR)c_app.a
+$.$(.TARG): $.$(.TKIND.DIR)c.a $.$(.TKIND.DIR)c_app.a
 	$(call RM,$@)
 	@$(foreach x,$(ALIAS.FUNCS),\
Index: trunk/src/emx/src/lib/alias/aliasfuncs.awk
===================================================================
--- trunk/src/emx/src/lib/alias/aliasfuncs.awk	(revision 155)
+++ trunk/src/emx/src/lib/alias/aliasfuncs.awk	(revision 156)
@@ -1,4 +1,4 @@
 #
-# A simple script to collect all symbols from c.a and sys.a that needs
+# A simple script to collect all symbols from c.a and c_app.a that needs
 # aliasing. All symbols beginning with _std_ get an alias without _std_
 # and an alias starting with just a '_' instead of _std_.
@@ -36,5 +36,4 @@
 BEGIN {
   gulp("out/dbg/aout/c.a");
-  gulp("out/dbg/aout/sys.a");
   gulp("out/dbg/aout/c_app.a");
 
Index: trunk/src/emx/src/lib/app/app.smak
===================================================================
--- trunk/src/emx/src/lib/app/app.smak	(revision 155)
+++ trunk/src/emx/src/lib/app/app.smak	(revision 156)
@@ -6,6 +6,6 @@
 
 .TKIND	:= aout
-.TARGET	:= c_app$A
-.TSRC.386 := $(wildcard src/lib/app/386/*.s)
+.TARGET	:= c_app.a
+.TSRC.386 := src/lib/app/386/crt2.s
 .TSRC	:= $(.TSRC.$(CPU)) $(wildcard src/lib/app/*.c)
 .TCF.src/lib/app/stdio.c := -D_NFILES=40
@@ -15,5 +15,8 @@
 include mklib.smak
 
+# Save the list of object files to link them later into libc.dll
+LIBC.DLL.OBJS += $(.OBJS)
+
 .TKIND	:= aout prof
-.TARGET	:= c_app_p$A
+.TARGET	:= c_app_p.a
 include mklib.smak
Index: trunk/src/emx/src/lib/app/stdio.c
===================================================================
--- trunk/src/emx/src/lib/app/stdio.c	(revision 155)
+++ trunk/src/emx/src/lib/app/stdio.c	(revision 156)
@@ -125,5 +125,5 @@
 void _exit_streams (void)
 {
-  _flushall ();
+  flushall ();
   _rmtmp ();
 }
Index: trunk/src/emx/src/lib/end/end.smak
===================================================================
--- trunk/src/emx/src/lib/end/end.smak	(revision 155)
+++ trunk/src/emx/src/lib/end/end.smak	(revision 156)
@@ -2,6 +2,6 @@
 .MDESC	:= The C runtime library that should be linked last
 
+.TARGET	:= end.lib
 .TKIND	:= omf
-.TARGET	:= end$A
 .TSRC.386 := $(wildcard src/lib/end/386/*.asm)
 .TSRC	:= $(.TSRC.$(CPU))
Index: trunk/src/emx/src/lib/io/io.smak
===================================================================
--- trunk/src/emx/src/lib/io/io.smak	(revision 155)
+++ trunk/src/emx/src/lib/io/io.smak	(revision 156)
@@ -18,4 +18,4 @@
 include comend.smak
 
-# bird: added dependency to help make make omf version.
+# Additional dependency to help make make omf version.
 $.omf/src/lib/io/386/binmode.obj: $.aout/src/lib/io/386/binmode.o
Index: trunk/src/emx/src/lib/libc.smak
===================================================================
--- trunk/src/emx/src/lib/libc.smak	(revision 155)
+++ trunk/src/emx/src/lib/libc.smak	(revision 156)
@@ -4,4 +4,7 @@
 # Creates four versions of object file name for all kinds of outputs.
 .MVER = $(foreach x,aout aout-prof,$.$x/src/lib/$1)
+
+# This is the list of object files for libc.dll (appended by some submakefiles)
+LIBC.DLL.OBJS :=
 
 # First of all, include all second-level libc submakefiles
@@ -14,6 +17,6 @@
 .MDESC	:= The C library
 
-.TKIND	:= aout
-.TARGET	:= c$A
+.TARGET	:= c_p.a
+.TKIND	:= aout prof
 .TSRC	:= $(libc.TSRC.$(CPU)) $(libc.TSRC)
 .TDEP	:= $(libc.TDEP.$(CPU)) $(libc.TDEP)
@@ -22,9 +25,40 @@
 include mklib.smak
 
-.TKIND	:= aout prof
-.TARGET	:= c_p$A
+# Build c.a last since LIBC.DLL needs .OBJS from c.a
+.TARGET	:= c.a
+.TKIND	:= aout
+.TKEEP	:= 1
 include mklib.smak
 
+#------------------------------------------------------
+# Now define the rules for building libc$(VERSION).dll
+#------------------------------------------------------
+LIBC.DLL = $.aout/libc$(VH)$(VM).dll
+
+DO.HELP.MODULES += $(call ECHO,    libc-dll - The dynamic C library (.dll))$(NL)
+
+DO.INSTALL += $(call CP,$(LIBC.DLL),$(INS)dll/)$(NL)
+INSDIRS += $(INS)dll/
+
+.PHONY: libc-dll
+all: libc-dll
+libc-dll: emxbind emxexp app alias libc os2 $(LIBC.DLL)
+
+$(LIBC.DLL): $.aout/src/lib/startup/386/dll0.o $(.OBJS) $(.DEPS) \
+		$(LIBC.DLL.OBJS) $.aout/libc.def
+	$(LD) $(LDFLAGS.DLL) -nostdlib -o __libc__ $^ \
+	  -L$.aout -los2 -lgcc -lc_alias
+	$(call DO.EMXBIND,-d$.aout/libc.def __libc__)
+	@$(call RM,__libc__)
+
+$.aout/libc.def: $.aout/c.a $.aout/c_app.a
+	$(call RM,$@)
+	@$(call FECHO,$@,LIBRARY libc$(VH)$(VM))
+	@$(call FECHO,$@,DESCRIPTION \"GNU C runtime library version $(VERSION)\")
+	@$(call FECHO,$@,EXPORTS)
+	$(EMXEXP) -u $^ | sed -e "/^$$/d" -e "/^ *;/d" | sort -d >>$@
+
 # Forget temporary variables
+include comend.smak
 .MVER :=
 libc.TSRC.$(CPU) :=
Index: trunk/src/emx/src/lib/misc/core.c
===================================================================
--- trunk/src/emx/src/lib/misc/core.c	(revision 155)
+++ 	(revision )
@@ -1,9 +1,0 @@
-/* core.c (emx+gcc) -- Copyright (c) 1990-1996 by Eberhard Mattes */
-
-#include <stdlib.h>
-#include <emx/syscalls.h>
-
-int _core (int handle)
-{
-  return __core (handle);
-}
Index: trunk/src/emx/src/lib/moddef/moddef.smak
===================================================================
--- trunk/src/emx/src/lib/moddef/moddef.smak	(revision 155)
+++ trunk/src/emx/src/lib/moddef/moddef.smak	(revision 156)
@@ -2,6 +2,6 @@
 .MDESC	:= A library for working with .DEF files
 
+.TARGET	:= moddef.a
 .TKIND	:= aout
-.TARGET	:= moddef$A
 .TSRC	:= $(wildcard src/lib/moddef/*.c)
 .INSDIR	= lib/
Index: trunk/src/emx/src/lib/omflib/omflib.smak
===================================================================
--- trunk/src/emx/src/lib/omflib/omflib.smak	(revision 155)
+++ trunk/src/emx/src/lib/omflib/omflib.smak	(revision 156)
@@ -2,6 +2,6 @@
 .MDESC	:= A library for reading/writing OMF object files
 
+.TARGET	:= omflib.a
 .TKIND	:= aout
-.TARGET	:= omflib$A
 .TSRC	:= $(wildcard src/lib/omflib/*.c)
 .INSDIR	= lib/
Index: trunk/src/emx/src/lib/os2.smak
===================================================================
--- trunk/src/emx/src/lib/os2.smak	(revision 155)
+++ 	(revision )
@@ -1,64 +1,0 @@
-# $Id$
-include comend.smak
-.MODULE := os2
-.MDESC  := OS/2 import library (similar to os2386.lib)
-.INSDIR := lib/
-.MDEP   := $(IMPLIB.BUILT)
-
-.IMPS := \
-src/lib/os2_386/ctls.imp \
-src/lib/os2_386/mou16.imp \
-src/lib/os2_386/rexxapi.imp \
-src/lib/os2_386/dos.imp \
-src/lib/os2_386/msg.imp  \
-src/lib/os2_386/ses.imp     \
-src/lib/os2_386/dos16.imp \
-src/lib/os2_386/nls.imp \
-src/lib/os2_386/shl.imp     \
-src/lib/os2_386/drag.imp \
-src/lib/os2_386/pic.imp \
-src/lib/os2_386/som.imp     \
-src/lib/os2_386/gpi.imp \
-src/lib/os2_386/pmmerge.imp \
-src/lib/os2_386/spl.imp     \
-src/lib/os2_386/help.imp \
-src/lib/os2_386/pmviop16.imp \
-src/lib/os2_386/vio16.imp   \
-src/lib/os2_386/kbd16.imp \
-src/lib/os2_386/que.imp \
-src/lib/os2_386/win.imp     \
-src/lib/os2_386/mon16.imp \
-src/lib/os2_386/rexx.imp \
-src/lib/os2_386/wp.imp
-
-.TKIND	:= aout
-.TARGET := os2.a
-include common.smak
-RULES += \
-	$(call FECHO,_@,$$.$(.TKIND.DIR)os2.a: $(.IMPS))$(NL)\
-	$(call FECHO,_@,	$$(call DO.IMPLIB,$$^))$(NL)
-
-.TKIND   := aout prof
-.TARGET  := os2_p.a
-include common.smak
-RULES += \
-	$(call FECHO,_@,$$.$(.TKIND.DIR)os2_p.a: $(.IMPS))$(NL)\
-	$(call FECHO,_@,	$$(call DO.IMPLIB,$$^))$(NL)
-
-.TKIND   := omf
-.TARGET  := os2.lib
-include common.smak
-RULES += \
-	$(call FECHO,_@,$$.$(.TKIND.DIR)os2.lib: $(.IMPS))$(NL)\
-	$(call FECHO,_@,	$$(call DO.IMPLIB,$$^))$(NL)
-
-.TKIND   := omf prof
-.TARGET  := os2_p.lib
-include common.smak
-RULES += \
-	$(call FECHO,_@,$$.$(.TKIND.DIR)os2_p.lib: $(.IMPS))$(NL)\
-	$(call FECHO,_@,	$$(call DO.IMPLIB,$$^))$(NL)
-				
-include comend.smak
-.IMPS :=
-
Index: trunk/src/emx/src/lib/os2_386/code2.s
===================================================================
--- trunk/src/emx/src/lib/os2_386/code2.s	(revision 155)
+++ trunk/src/emx/src/lib/os2_386/code2.s	(revision 156)
@@ -3,5 +3,5 @@
 #include <emx/asm386.h>
 
-        .globl  _DosGetMessage
+        .globl  DosGetMessage
         .globl  __msgseg32
 
@@ -14,10 +14,10 @@
         .align  2, 0x90
 
-_DosGetMessage:
+DosGetMessage:
         PROFILE_NOFRAME
         popl    %ecx                    /* return address */
         pushl   $__msgseg32
         pushl   %ecx
-        jmp     _DosTrueGetMessage
+        jmp     DosTrueGetMessage
 
 L_tab:  .short  0x0000
Index: trunk/src/emx/src/lib/os2_386/code3.s
===================================================================
--- trunk/src/emx/src/lib/os2_386/code3.s	(revision 155)
+++ trunk/src/emx/src/lib/os2_386/code3.s	(revision 156)
@@ -3,7 +3,7 @@
 #include <emx/asm386.h>
 
-        .globl  _DosQueryMessageCP
+        .globl  DosQueryMessageCP
 
-_DosQueryMessageCP:
+DosQueryMessageCP:
         PROFILE_NOFRAME
         pushl   0(%esp)
@@ -14,3 +14,3 @@
         xchgl   8(%esp), %eax
         movl    %eax, 4(%esp)
-        jmp     _DosIQueryMessageCP
+        jmp     DosIQueryMessageCP
Index: trunk/src/emx/src/lib/os2_386/os2.smak
===================================================================
--- trunk/src/emx/src/lib/os2_386/os2.smak	(revision 156)
+++ trunk/src/emx/src/lib/os2_386/os2.smak	(revision 156)
@@ -0,0 +1,19 @@
+# $Id$
+
+.MODULE := os2
+.MDESC  := OS/2 import library (similar to os2386.lib)
+.MDEP   := emximp
+.INSDIR := lib/
+
+.TARGET := os2.a
+.TKIND	:= aout
+.TSRC.386 := $(wildcard src/lib/os2_386/*.s)
+.TSRC	:= $(.TSRC.$(CPU)) $(wildcard src/lib/os2_386/*.c)
+.TCF	:= -Wno-unused
+.IMPS	:= $(wildcard src/lib/os2_386/*.imp)
+.TKEEP	:= 1
+include mkimplib.smak
+
+.TARGET := os2_p.a
+.TKIND	:= aout prof
+include mkimplib.smak
Index: trunk/src/emx/src/lib/startup/startup.smak
===================================================================
--- trunk/src/emx/src/lib/startup/startup.smak	(revision 155)
+++ trunk/src/emx/src/lib/startup/startup.smak	(revision 156)
@@ -34,5 +34,5 @@
 	$(call DO.AOUT2OMF,-m__text)
 
-# bird: dependencies to help stupid make.
+# Additional dependencies to help stupid make.
 $.omf/src/lib/startup/mcrt0.obj: $.aout/src/lib/startup/mcrt0.o
 $.omf/src/lib/startup/gcrt0.obj: $.aout/src/lib/startup/gcrt0.o
Index: trunk/src/emx/src/lib/sys/386/thunk0.s
===================================================================
--- trunk/src/emx/src/lib/sys/386/thunk0.s	(revision 155)
+++ trunk/src/emx/src/lib/sys/386/thunk0.s	(revision 156)
@@ -6,7 +6,7 @@
 		.p2align 2
 __emx_32to16:	movl	4(%esp),%eax
-		jmp	_DosFlatToSel
+		jmp	DosFlatToSel
 
 		.p2align 2
 __emx_16to32:	movl	4(%esp),%eax
-		jmp	_DosSelToFlat
+		jmp	DosSelToFlat
Index: trunk/src/emx/src/lib/sys/386/thunk1.s
===================================================================
--- trunk/src/emx/src/lib/sys/386/thunk1.s	(revision 155)
+++ trunk/src/emx/src/lib/sys/386/thunk1.s	(revision 156)
@@ -68,5 +68,5 @@
 
 		movl	$Lthunk16_call, %eax
-		call	_DosFlatToSel
+		call	DosFlatToSel
 		movzwl	%ax, %ecx		# %ecx = offset
 		shrl	$16, %eax		# %eax = segment
@@ -75,5 +75,5 @@
 
 		movl	%esp, %eax
-		call	_DosFlatToSel
+		call	DosFlatToSel
 		movzwl	%ax, %ecx		# %ecx = offset
 		shrl	$16, %eax		# %eax = segment
Index: trunk/src/emx/src/lib/sys/386/unwind.s
===================================================================
--- trunk/src/emx/src/lib/sys/386/unwind.s	(revision 155)
+++ trunk/src/emx/src/lib/sys/386/unwind.s	(revision 156)
@@ -10,5 +10,5 @@
         pushl   $L_cont
         pushl   %eax
-        call    _DosUnwindException
+        call    DosUnwindException
 L_cont: addl    $3*4, %esp
         ret
Index: trunk/src/emx/src/lib/sys/__fork.c
===================================================================
--- trunk/src/emx/src/lib/sys/__fork.c	(revision 156)
+++ trunk/src/emx/src/lib/sys/__fork.c	(revision 156)
@@ -0,0 +1,8 @@
+#include <stdlib.h>
+#include <emx/syscalls.h>
+
+int __fork ()
+{
+  perror ("fork() not implemented");
+  return -1;
+}
Index: trunk/src/emx/src/lib/sys/__select.c
===================================================================
--- trunk/src/emx/src/lib/sys/__select.c	(revision 156)
+++ trunk/src/emx/src/lib/sys/__select.c	(revision 156)
@@ -0,0 +1,9 @@
+#include <stdlib.h>
+#include <emx/syscalls.h>
+
+int __select (struct _select *args)
+{
+  (void)args;
+  perror ("select() not implemented");
+  return 0;
+}
Index: trunk/src/emx/src/lib/sys/__settime.c
===================================================================
--- trunk/src/emx/src/lib/sys/__settime.c	(revision 156)
+++ trunk/src/emx/src/lib/sys/__settime.c	(revision 156)
@@ -0,0 +1,63 @@
+#include <time.h>
+#include <sys/time.h>
+#include <errno.h>
+#include <emx/time.h>
+#include <emx/syscalls.h>
+#define INCL_DOS
+#include <os2.h>
+
+/* Convert a Unix time value to a DATETIME structure. */
+
+static void unix2time (DATETIME *dst, ULONG t)
+{
+  ULONG lo, hi, i;
+  const USHORT *p;
+
+  dst->seconds = t % 60; t /= 60;
+  dst->minutes = t % 60; t /= 60;
+  dst->hours = t % 24; t /= 24;
+
+  /* Find an i such that _year_day[i] <= t < _year_day[i+1]. */
+
+  lo = 0; hi = _YEARS;
+  for (;;)
+    {
+      i = (lo + hi) / 2;
+      if (_year_day[i] > (int)t)
+        hi = i - 1;
+      else if (_year_day[i+1] <= (int)t)
+        lo = i + 1;
+      else
+        break;
+    }
+  dst->year = i + 1970;
+  t -= _year_day[i];
+
+  p = (_leap_year (dst->year) ? _month_day_leap : _month_day_non_leap);
+  for (i = 0; t >= p[i+1]; ++i)
+    ;
+  dst->month = i + 1;
+  dst->day = t - p[i] + 1;
+}
+
+int __settime (const struct timeval *tp)
+{
+  DATETIME dt;
+
+  /* Preserve the value of DATETIME.timezone. */
+
+  if (DosGetDateTime (&dt))
+    return -1;
+  
+  unix2time (&dt, tp->tv_sec + tp->tv_usec / 1000000);
+
+  /* Keep dt.timezone! */
+
+  dt.weekday = 0;
+  dt.hundredths = (tp->tv_usec / 10000) % 100;
+
+  if (DosSetDateTime (&dt))
+    return -1;
+
+  return 0;
+}
Index: trunk/src/emx/src/lib/sys/__ttyname.c
===================================================================
--- trunk/src/emx/src/lib/sys/__ttyname.c	(revision 156)
+++ trunk/src/emx/src/lib/sys/__ttyname.c	(revision 156)
@@ -0,0 +1,9 @@
+#include <stdlib.h>
+#include <emx/syscalls.h>
+
+int __ttyname (int handle, char *buf, size_t buf_size)
+{
+  (void)handle; (void)buf; (void)buf_size;
+  perror ("ttyname() not implemented");
+  return 0;
+}
Index: trunk/src/emx/src/lib/sys/core.c
===================================================================
--- trunk/src/emx/src/lib/sys/core.c	(revision 156)
+++ trunk/src/emx/src/lib/sys/core.c	(revision 156)
@@ -0,0 +1,11 @@
+/* core.c (emx+gcc) -- Copyright (c) 1990-1996 by Eberhard Mattes */
+
+#include <stdlib.h>
+#include <emx/syscalls.h>
+
+int _core (int handle)
+{
+  (void)handle;
+  /* Not implemented */
+  return -1;
+}
Index: trunk/src/emx/src/lib/sys/sys.smak
===================================================================
--- trunk/src/emx/src/lib/sys/sys.smak	(revision 155)
+++ trunk/src/emx/src/lib/sys/sys.smak	(revision 156)
@@ -1,18 +1,4 @@
-# Build two kinds of library: aout and aout-profile.
-# The OMF libraries will be derived from them.
+libc.TSRC.386 += $(wildcard src/lib/sys/386/*.s)
+libc.TSRC += $(wildcard src/lib/sys/*.c)
 
-.MODULE	:= sys
-.MDESC	:= System-dependent libc backend
-
-.TKIND	:= aout
-.TARGET	:= sys$A
-.TSRC.386 := $(wildcard src/lib/sys/386/*.s)
-.TSRC	:= $(.TSRC.$(CPU)) $(wildcard src/lib/sys/*.c)
 .TCF.src/lib/sys/heapsize.c := -DHEAPSIZE=0x2000000
-.INSDIR	= lib/
-.TKEEP	:= 1
-include mklib.smak
-
-.TKIND	:= aout prof
-.TARGET	:= sys_p$A
-include mklib.smak
Index: trunk/src/emx/src/libend/end.smak
===================================================================
--- trunk/src/emx/src/libend/end.smak	(revision 155)
+++ trunk/src/emx/src/libend/end.smak	(revision 156)
@@ -2,6 +2,6 @@
 .MDESC	:= The C runtime library that should be linked last
 
+.TARGET	:= end.lib
 .TKIND	:= omf
-.TARGET	:= end$A
 .TSRC.386 := $(wildcard src/lib/end/386/*.asm)
 .TSRC	:= $(.TSRC.$(CPU))
Index: trunk/src/emx/src/libmoddef/moddef.smak
===================================================================
--- trunk/src/emx/src/libmoddef/moddef.smak	(revision 155)
+++ trunk/src/emx/src/libmoddef/moddef.smak	(revision 156)
@@ -2,6 +2,6 @@
 .MDESC	:= A library for working with .DEF files
 
+.TARGET	:= moddef.a
 .TKIND	:= aout
-.TARGET	:= moddef$A
 .TSRC	:= $(wildcard src/lib/moddef/*.c)
 .INSDIR	= lib/
Index: trunk/src/emx/src/libomflib/omflib.smak
===================================================================
--- trunk/src/emx/src/libomflib/omflib.smak	(revision 155)
+++ trunk/src/emx/src/libomflib/omflib.smak	(revision 156)
@@ -2,6 +2,6 @@
 .MDESC	:= A library for reading/writing OMF object files
 
+.TARGET	:= omflib.a
 .TKIND	:= aout
-.TARGET	:= omflib$A
 .TSRC	:= $(wildcard src/lib/omflib/*.c)
 .INSDIR	= lib/
Index: trunk/src/emx/src/libos2/code2.s
===================================================================
--- trunk/src/emx/src/libos2/code2.s	(revision 155)
+++ trunk/src/emx/src/libos2/code2.s	(revision 156)
@@ -3,5 +3,5 @@
 #include <emx/asm386.h>
 
-        .globl  _DosGetMessage
+        .globl  DosGetMessage
         .globl  __msgseg32
 
@@ -14,10 +14,10 @@
         .align  2, 0x90
 
-_DosGetMessage:
+DosGetMessage:
         PROFILE_NOFRAME
         popl    %ecx                    /* return address */
         pushl   $__msgseg32
         pushl   %ecx
-        jmp     _DosTrueGetMessage
+        jmp     DosTrueGetMessage
 
 L_tab:  .short  0x0000
Index: trunk/src/emx/src/libos2/code3.s
===================================================================
--- trunk/src/emx/src/libos2/code3.s	(revision 155)
+++ trunk/src/emx/src/libos2/code3.s	(revision 156)
@@ -3,7 +3,7 @@
 #include <emx/asm386.h>
 
-        .globl  _DosQueryMessageCP
+        .globl  DosQueryMessageCP
 
-_DosQueryMessageCP:
+DosQueryMessageCP:
         PROFILE_NOFRAME
         pushl   0(%esp)
@@ -14,3 +14,3 @@
         xchgl   8(%esp), %eax
         movl    %eax, 4(%esp)
-        jmp     _DosIQueryMessageCP
+        jmp     DosIQueryMessageCP
Index: trunk/src/emx/src/libos2/os2.smak
===================================================================
--- trunk/src/emx/src/libos2/os2.smak	(revision 156)
+++ trunk/src/emx/src/libos2/os2.smak	(revision 156)
@@ -0,0 +1,19 @@
+# $Id$
+
+.MODULE := os2
+.MDESC  := OS/2 import library (similar to os2386.lib)
+.MDEP   := emximp
+.INSDIR := lib/
+
+.TARGET := os2.a
+.TKIND	:= aout
+.TSRC.386 := $(wildcard src/lib/os2_386/*.s)
+.TSRC	:= $(.TSRC.$(CPU)) $(wildcard src/lib/os2_386/*.c)
+.TCF	:= -Wno-unused
+.IMPS	:= $(wildcard src/lib/os2_386/*.imp)
+.TKEEP	:= 1
+include mkimplib.smak
+
+.TARGET := os2_p.a
+.TKIND	:= aout prof
+include mkimplib.smak
Index: trunk/src/emx/src/regexp/regexp.smak
===================================================================
--- trunk/src/emx/src/regexp/regexp.smak	(revision 155)
+++ trunk/src/emx/src/regexp/regexp.smak	(revision 156)
@@ -3,5 +3,6 @@
 .INSDIR := lib/
 
-.TARGET := regexp$A
+.TARGET := regexp.a
+.TKIND	:= aout
 .TSRC   := $(wildcard src/regexp/*.c)
 include mklib.smak
