Index: trunk/src/emx/src/emximp/emximp.c
===================================================================
--- trunk/src/emx/src/emximp/emximp.c	(revision 2232)
+++ trunk/src/emx/src/emximp/emximp.c	(revision 2233)
@@ -1250,5 +1250,5 @@
  * Reads a LX nametable.
  */
-static void dll_read_names(FILE *phFile, off_t off, int fResident, int fOrdinal0Only, const char *pszFilename)
+static void dll_read_names(FILE *phFile, off_t off, int cb, int fResident, int fOrdinal0Only, const char *pszFilename)
 {
     char            szName[256];
@@ -1259,30 +1259,15 @@
         error("`%s': Bad LX format\n", pszFilename);
 
-
-    /* first name is module name / description. */
-    cchName = (unsigned)fgetc(phFile);
-    if (!cchName)
-        error("`%s': Bad LX format\n", pszFilename);
-    iOrd = 0;
-    if (    fread(szName, 1, cchName, phFile) != cchName
-        ||  fread(&iOrd, 1, 2, phFile) != 2)
-        error("`%s': Bad LX format\n", pszFilename);
-    if (iOrd != 0)
-        error("`%s': Bad LX format - expect 0 as first ordinal, found %d\n", pszFilename, iOrd);
-
-    szName[cchName] = '\0';
     if (fResident)
-        module_name = xstrdup(szName);
+        module_name = NULL;
     else
-        description = xstrdup(szName);
-
-    if (fOrdinal0Only)
-        return;
-
-    /* The rest of the names are export names. */
-    for (;;)
+        description = NULL;
+
+    /* The names doesn't have to be in order, so we'll have to read
+     * until we find the 0 ordinal if that's what we require. */
+    while (cb > 0)
     {
         cchName = (unsigned)fgetc(phFile);
-        if (!cchName)
+        if (!cchName || cb < cchName + 2)
             break;
         iOrd = 0;
@@ -1291,37 +1276,50 @@
             error("`%s': Bad LX format\n", pszFilename);
         szName[cchName] = '\0';
-
-        switch (mode)
+        cb -= 1 + cchName + 2;
+
+        if (iOrd == 0)
         {
-            case M_DLL_TO_IMP:
-                if (!fResident)
-                    fprintf(out_file, "%-23s %-8s %3u ?\n",   szName, module_name, iOrd);
-                else
-                    fprintf(out_file, "%-23s %-8s %-23s ?\n", szName, module_name, szName);
-                if (ferror(out_file))
-                    write_error(out_fname);
-                break;
-            case M_DLL_TO_A:
-                if (!fResident)
-                    write_a_import(szName, module_name, iOrd, NULL);
-                else
-                    write_a_import(szName, module_name, 0, szName);
-                break;
-            case M_DLL_TO_LIB:
-                if (!fResident)
-                    write_lib_import(szName, module_name, iOrd, NULL);
-                else
-                    write_lib_import(szName, module_name, 0, szName);
-                break;
-            case M_DLL_TO_DEF:
-                if (!fResident)
-                    fprintf(out_file, "  \"%s\" @%d\n", szName, iOrd);
-                else
-                    fprintf(out_file, "  \"%s\"\n", szName);
-                if (ferror(out_file))
-                    write_error(out_fname);
-                break;
-            default:
-                abort ();
+            if (fResident)
+                module_name = xstrdup(szName);
+            else
+                description = xstrdup(szName);
+            if (fOrdinal0Only)
+                return;
+        }
+        else if (!fOrdinal0Only)
+        {
+            switch (mode)
+            {
+                case M_DLL_TO_IMP:
+                    if (!fResident)
+                        fprintf(out_file, "%-23s %-8s %3u ?\n",   szName, module_name, iOrd);
+                    else
+                        fprintf(out_file, "%-23s %-8s %-23s ?\n", szName, module_name, szName);
+                    if (ferror(out_file))
+                        write_error(out_fname);
+                    break;
+                case M_DLL_TO_A:
+                    if (!fResident)
+                        write_a_import(szName, module_name, iOrd, NULL);
+                    else
+                        write_a_import(szName, module_name, 0, szName);
+                    break;
+                case M_DLL_TO_LIB:
+                    if (!fResident)
+                        write_lib_import(szName, module_name, iOrd, NULL);
+                    else
+                        write_lib_import(szName, module_name, 0, szName);
+                    break;
+                case M_DLL_TO_DEF:
+                    if (!fResident)
+                        fprintf(out_file, "  \"%s\" @%d\n", szName, iOrd);
+                    else
+                        fprintf(out_file, "  \"%s\"\n", szName);
+                    if (ferror(out_file))
+                        write_error(out_fname);
+                    break;
+                default:
+                    abort ();
+            }
         }
     } /* read restable loop */
@@ -1443,6 +1441,6 @@
     if (mode == M_DLL_TO_DEF && !first_module)
     {
-        dll_read_names(phFile, os2hdr.e32_restab + offLX, 1, 1, pszFilename);
-        dll_read_names(phFile, os2hdr.e32_nrestab, 0, 1, pszFilename);
+        dll_read_names(phFile, os2hdr.e32_restab + offLX, 0x7ffffff, 1, 1, pszFilename);
+        dll_read_names(phFile, os2hdr.e32_nrestab, os2hdr.e32_cbnrestab, 0, 1, pszFilename);
 
         first_module = xstrdup(module_name);
@@ -1457,7 +1455,7 @@
           write_error(out_fname);
     }
-    dll_read_names(phFile, os2hdr.e32_restab + offLX, 1, 0, pszFilename);
+    dll_read_names(phFile, os2hdr.e32_restab + offLX, 0x7ffffff, 1, 0, pszFilename);
     if (os2hdr.e32_nrestab)
-        dll_read_names(phFile, os2hdr.e32_nrestab, 0, 0, pszFilename);
+        dll_read_names(phFile, os2hdr.e32_nrestab, os2hdr.e32_cbnrestab, 0, 0, pszFilename);
 
     fclose(phFile);
