Index: trunk/src/emx/src/emxomf/emxomfld.c
===================================================================
--- trunk/src/emx/src/emxomf/emxomfld.c	(revision 534)
+++ trunk/src/emx/src/emxomf/emxomfld.c	(revision 535)
@@ -113,6 +113,12 @@
 static FILE *response_file = NULL;
 
+/* Force the use of a response file from the next put_arg(). */
+static int force_response_file = FALSE;
+
 /* Weak alias object file. */
 static char weakobj_fname[_MAX_PATH];
+
+/* Weak definition file (modified def_fname). */
+static char weakdef_fname[_MAX_PATH];
 
 /* Non-zero if debugging information is to be omitted.  Set by the -s
@@ -327,5 +333,6 @@
 
       max_len = (response_file == NULL ? 110 : 52);
-      if (line_len + len + 1 > max_len)
+      if (   line_len + len + 1 > max_len 
+          || (force_response_file && !response_file))
         {
 
@@ -385,4 +392,7 @@
               command_line[line_len++] = '@';
               strcpy (command_line+line_len, response_fname);
+
+              if (force_response_file)
+                force_response_file = FALSE;
             }
           else if (line_len != 0)
@@ -645,5 +655,5 @@
     if (    !strnicmp(pOpt->name, "/I", 2)
         ||  !strnicmp(pOpt->name, "-I", 2))
-        fFlags |= WLDC_VERBOSE;
+        fFlags = fFlags;                /* Ignore - require opt_t. */
     else
     if (    !strnicmp(pOpt->name, "/NOIN", 5)
@@ -704,7 +714,9 @@
       /* generate weak aliases. */
       if (!rc)
-        rc = wldGenerateWeakobj (pwld, weakobj_fname);
+        rc = wldGenerateWeakAliases (pwld, weakobj_fname, weakdef_fname);
       if (!rc && weakobj_fname[0])
         add_name_list (&add_obj_fnames, weakobj_fname);
+      if (!rc && weakdef_fname[0])
+        strcpy (def_fname, weakdef_fname);
 
       /* cleanup the linker */
@@ -740,4 +752,5 @@
   line_len = 0;
   response_flag = rsp;
+  force_response_file = FALSE;
 }
 
@@ -827,5 +840,5 @@
       if (!(argi % 32))
         argv = xrealloc(argv, sizeof(argv[0]) * (argi + 32 + 1));
-      argv[argi++] = xstrdup(psz);
+      argv[argi++] = psz;
 
       /* next */
@@ -839,4 +852,5 @@
     fprintf(stderr, "*** Return from %s is %d\n", pszwhat, rc);
 
+  free(argv);
   return rc;
 }
@@ -862,4 +876,9 @@
       remove (weakobj_fname);
       weakobj_fname[0] = '\0';
+    }
+  if (weakdef_fname[0] != '\0')
+    {
+      remove (weakdef_fname);
+      weakdef_fname[0] = '\0';
     }
 }
@@ -1259,5 +1278,6 @@
 
   /* Put the object file names onto the command line. */
-
+  
+  force_response_file = TRUE;           /* link386 workaround. */
   put_args (obj_fnames, TRUE);
   put_arg (",", FALSE, FALSE);
Index: trunk/src/emx/src/emxomf/weakld.c
===================================================================
--- trunk/src/emx/src/emxomf/weakld.c	(revision 534)
+++ trunk/src/emx/src/emxomf/weakld.c	(revision 535)
@@ -82,4 +82,7 @@
 #include <string.h>
 #include <sys/types.h>
+#include <sys/time.h>
+#include <sys/stat.h>
+#include <process.h>
 #include <sys/omflib.h>
 #include <sys/moddef.h>
@@ -564,5 +567,5 @@
     {
         pLib->phFile = fopen(pLib->pszLibName, "rb");
-        if (pLib->phFile)
+        if (!pLib->phFile)
             libErr(pLib, "Failed to open library.");
     }
@@ -576,5 +579,5 @@
 static void         libClose(PWLDLIB pLib)
 {
-    if (!pLib->phFile)
+    if (pLib->phFile)
     {
         fclose(pLib->phFile);
@@ -2342,4 +2345,5 @@
     {
         void *pv = pObj;   
+        modClose(pObj);
         pObj = pObj->pNext;
         free(pv);
@@ -2348,4 +2352,5 @@
     {
         void *pv = pObj;   
+        modClose(pObj);
         pObj = pObj->pNext;
         free(pv);
@@ -2356,4 +2361,5 @@
     {
         void *pv = pLib;   
+        libClose(pLib);
         pLib = pLib->pNext;
         free(pv);
@@ -2411,4 +2417,5 @@
         pWld->ppObjsAdd = &pMod->pNext;
         rc = pass1ReadOMFMod(pWld, pMod, 0);
+        modClose(pMod);
     }
     else if (OmfRec.chType == LIBHDR)
@@ -2435,4 +2442,5 @@
                 }
                 rc = pass1ReadOMFMod(pWld, pMod, 0);
+                pMod->phFile = NULL;
                 if (rc)
                     break;
@@ -2447,7 +2455,9 @@
             fread(&OmfRec, sizeof(OmfRec), 1, phFile);
         }
+        fclose(phFile);
     }
     else
     {
+        fclose(phFile);
         fprintf(stderr, "weakld: invalid object file '%s'.\n", pszName);
         rc = -1;
@@ -2826,4 +2836,108 @@
 
 
+
+/**
+ * Callback function for doing weak aliasing of a module definition file.
+ *
+ * @returns 0 on success.
+ * @returns -1 to stop the parsing.
+ * @param   pMD     Pointer to module definition file handle.
+ * @param   pStmt   Statement we're processing.
+ * @param   eToken  Token we're processing.
+ * @param   pvArg   Pointer to file stream for the output file.
+ *                  
+ */
+static int wldDefCallbackGenerateWeakAliases(struct _md *pMD, const _md_stmt *pStmt, _md_token eToken, void *pvArg)
+{
+    unsigned        fFlags;
+    unsigned        uOrdinal;
+    unsigned        cWords;
+    PWLDSYM         pSym;
+    FILE *          phFile = (PHFILE)pvArg;
+
+    switch (eToken)
+    {
+        /*
+         * One export.
+         */
+        case _MD_EXPORTS:
+            fFlags = uOrdinal = cWords = 0;
+            if (pStmt->export.flags & _MDEP_ORDINAL)
+                uOrdinal = pStmt->export.ordinal;
+            if (pStmt->export.flags & _MDEP_RESIDENTNAME)
+                fFlags |= WLDSEF_NONRESIDENT;
+            else if (pStmt->export.flags & _MDEP_NONAME)
+                fFlags |= WLDSEF_NONAME;
+            else
+                fFlags |= WLDSEF_DEFAULT;
+            if (pStmt->export.flags & _MDEP_NODATA)
+                fFlags |= WLDSEF_NODATA;
+            if (pStmt->export.flags & _MDEP_PWORDS)
+                cWords = pStmt->export.pwords;
+            pSym = symAddExport(pParam->pWld, pParam->pMod, 0,
+                                fFlags, cWords,
+                                pStmt->export.entryname, -1,
+                                pStmt->export.internalname, -1,
+                                uOrdinal);
+            if (!pSym)
+            {
+                pParam->rc = -4;
+                return -4;
+            }
+            break;
+
+        /*
+         * Parse error.
+         */
+        case _MD_parseerror:
+            wldErr(pWld, "Parse error %d on line %d. (errorcode=%d stmt=%d)",
+                   _md_get_linenumber(pMD), pStmt->error.code, pStmt->error.stmt);
+            pParam->rc = -2;
+            break;
+
+        /*
+         * Everything else is passed thru line by line.
+         */
+        default:
+            pMD->
+            break;
+    }
+    return 0;
+}
+
+
+/**
+ * Generates an unique temporary file.
+ *
+ * @returns 0 on success.
+ * @returns -1 on failure.
+ * @param   pWld        Linker instance.
+ * @param   pszFile     Where to put the filename.
+ * @param   pszPrefix   Prefix.
+ * @param   pszSuffix   Suffix.
+ */
+static int      wldTempFile(PWLD pWld, char *pszFile, const char *pszPrefix, const char *pszSuffix)
+{
+    struct stat     s;
+    unsigned        c = 0;
+    pid_t           pid = getpid();
+    const char *    pszTmp = getenv("TMP");
+    if (!pszTmp)    pszTmp = getenv("TMPDIR");
+    if (!pszTmp)    pszTmp = getenv("TEMP");
+    if (!pszTmp)    pszTmp = ".";
+
+    do
+    {
+        struct timeval  tv = {0,0};
+        if (c++ >= 200)
+            return -1;
+        gettimeofday(&tv, NULL);
+        sprintf(pszFile, "%s\\%s%x%lx%d%lx%s", pszTmp, pszPrefix, pid, tv.tv_sec, c, tv.tv_usec, pszSuffix);
+    } while (!stat(pszFile, &s));
+    
+    return 0;
+}
+
+
 /**
  * Generates a object file containing alias for resolving the weak
@@ -2834,9 +2948,11 @@
  * @returns 0 on success.
  * @returns some unexplainable randomly picked number on failure.
- * @param   pWld    Linker instance to destroy.
- * @param   pszName Where to put the name of the generated object file.
- *                  This is an empty string if no weak symbols were found!
- */
-int     wldGenerateWeakobj(PWLD pWld, char *pszName)
+ * @param   pWld        Linker instance.
+ * @param   pszObjName  Where to put the name of the generated object file.
+ *                      This is an empty string if no weak symbols were found!
+ * @param   pszDefName  Where to put the name of the modified definition file.
+ *                      This is an empty string if changes was required!
+ */
+int     wldGenerateWeakAliases(PWLD pWld, char *pszObjName, char *pszDefName)
 {
     char *      psz;
@@ -2844,20 +2960,17 @@
     int         rc;
 
-    /* generate the file */
-    *pszName = '\0';
-    psz = _tempnam(NULL, "weako");
-    if (psz)
-    {
-        strcpy(pszName, psz);
-        free(psz);
-    }
-    else
-        tmpnam(pszName);
-    strcat(pszName, "wk.obj");
-    WLDINFO(pWld, ("Generating weakobj object file '%s'.", pszName));
-
-
-    /* open the file */
-    phFile = fopen(pszName, "wr");
+    /* zero returns */
+    *pszObjName = '\0';
+    if (pszDefName)
+        *pszDefName = '\0';
+
+    /* generate the object  file */
+    rc = wldTempFile(pWld, pszObjName, "wk", ".obj");
+    if (rc)
+        return wldErr(pWld, "Failed to generate temporary object file for weak aliases.");
+    WLDINFO(pWld, ("Generating object file '%s' for weak aliases.", pszObjName));
+
+    /* open it */
+    phFile = fopen(pszObjName, "wr");
     if (phFile)
     {
@@ -2893,6 +3006,6 @@
                 WLDINFO(pWld, ("No weak alias needed, deleting file."));
             fclose(phFile);
-            remove(pszName);
-            *pszName = '\0';
+            remove(pszObjName);
+            *pszObjName = '\0';
         }
         else
@@ -2906,9 +3019,9 @@
            else
            {
-               wldErr(pWld, "Failed to write to '%s'.", pszName);
+               wldErr(pWld, "Failed to write to '%s'.", pszObjName);
                rc = -1;
                fclose(phFile);
-               remove(pszName);
-               *pszName = '\0';
+               remove(pszObjName);
+               *pszObjName = '\0';
            }
         }
@@ -2916,6 +3029,60 @@
     else
     {
-        wldErr(pWld, "Failed to open '%s' for writing.", pszName);
-        *pszName = '\0';
+        wldErr(pWld, "Failed to open '%s' for writing.", pszObjName);
+        *pszObjName = '\0';
+    }
+
+    /* do the definition file */
+    if (!rc && pWld->pDef && pszDefName)
+    {
+        rc = wldTempFile(pWld, pszDefName, "wk", ".def");
+        if (!rc)
+        {
+            WLDINFO(pWld, ("Generating definition file '%s' for weak exports.", pszDefName));
+
+            /* open it */
+            phFile = fopen(pszDefName, "w");
+            if (phFile)
+            {
+                FILE *phOrgDef = fopen(pWld->pDef->pszModName, "r");
+                if (phOrgDef)
+                {
+                    char *pachOrgDef = fsize
+                    struct _md *pMD;
+
+                    /* open original def file */
+                    pMD = _md_open(pWld->pDef->pszModName);
+                    if (pMD)
+                    {
+                        /* parse it */
+                        _md_next_token(pMD);
+                        rc = _md_parse(pMD, wldDefCallbackGenerateWeakAliases, phFile);
+                        _md_close(pMD);
+                    }
+                    else
+                        rc = wldErr(pWld, "_md_open on '%s'.\n", pszDefName);
+                    fclose(phOrgFile);
+                }
+                else
+                    rc = wldErr(pWld, "Failed to open '%s' for reading.\n", pszDefName);
+                fclose(phFile);
+            }
+            else
+                rc = wldErr(pWld, "Failed to open '%s' for writing.\n", pszDefName);
+        }
+        else
+            rc = wldErr(pWld, "Failed to generate temporary definition file for weak aliases.");
+    }
+
+    /* cleanup */
+    if (rc && pszDefName && *pszDefName)
+    {
+        remove(pszDefName);
+        *pszDefName = '\0';
+    }
+    if (rc && pszObjName && *pszObjName)
+    {
+        remove(pszObjName);
+        *pszObjName = '\0';
     }
 
Index: trunk/src/emx/src/emxomf/weakld.h
===================================================================
--- trunk/src/emx/src/emxomf/weakld.h	(revision 534)
+++ trunk/src/emx/src/emxomf/weakld.h	(revision 535)
@@ -56,5 +56,5 @@
 int     wldAddLibrary(PWLD pWld, FILE *phFile, const char *pszName);
 int     wldPass1(PWLD pWld);
-int     wldGenerateWeakobj(PWLD pwld, char *pszName);
+int     wldGenerateWeakAliases(PWLD pwld, char *pszObjName, char *pszDefName);
 int     wldDestroy(PWLD pWld);
 /** @} */
