Index: trunk/emx/src/emxomf/emxomf.c
===================================================================
--- trunk/emx/src/emxomf/emxomf.c	(revision 3728)
+++ trunk/emx/src/emxomf/emxomf.c	(revision 3729)
@@ -795,16 +795,11 @@
 }
 
-
-
-
-
-/* Put the string pszName into the current OMF record.
-   In the OMF record, the string is preceded by a length byte.  The
-   string length must not exceed 255; if it is too long, display a
-   warning and truncate the string.  Moreover, there must be enough
-   space left in the OMF record; if there isn't, display an error
-   message and abort. */
-
-static void put_nstr(const char *pszName, size_t cch)
+/* Put the string pszName into the given buffer pOutBuf (which must be at least
+   256 bytes long). The string length must not exceed 255 bytes (OMF format
+   requirement); if it is too long, display a warning and truncate the string.
+   The string returned in pOutBuf is zero-terminated. The function returns the
+   length of the resulting string. */
+
+static int make_nstr (const char *pszName, size_t cch, char *pszOutBuf)
 {
     if (    cch > SYMBOL_MAX_LENGTH
@@ -824,13 +819,11 @@
         assert(psz - &szHash[0] == SYMBOL_HASH_LENGTH);
 
-        if (!fits(1 + SYMBOL_MAX_LENGTH + SYMBOL_HASH_LENGTH))
-            doesn_fit();
-        out_data[out_idx++] = SYMBOL_MAX_LENGTH + SYMBOL_HASH_LENGTH;
-        memcpy(out_data + out_idx, pszName, SYMBOL_MAX_LENGTH);
-        out_idx += SYMBOL_MAX_LENGTH;
-        memcpy(out_data + out_idx, szHash, SYMBOL_HASH_LENGTH);
-        out_idx += SYMBOL_HASH_LENGTH;
+        memcpy(pszOutBuf, pszName, SYMBOL_MAX_LENGTH);
+        memcpy(pszOutBuf + SYMBOL_MAX_LENGTH, szHash, SYMBOL_HASH_LENGTH);
+        pszOutBuf[SYMBOL_MAX_LENGTH + SYMBOL_HASH_LENGTH] = '\0';
 
         warning ("Truncated symbol '%.*s' to '%.*s%s'", cch, pszName, SYMBOL_MAX_LENGTH, pszName, szHash);
+
+        cch = SYMBOL_MAX_LENGTH + SYMBOL_HASH_LENGTH;
     }
     else
@@ -838,10 +831,28 @@
         assert(cch <= 0xff);
         cch &= 0xff;
-        if (!fits(1+cch))
-            doesn_fit();
-        out_data[out_idx++] = (byte)cch;
-        memcpy(out_data+out_idx, pszName, cch);
-        out_idx += cch;
-    }
+        memcpy(pszOutBuf, pszName, cch);
+        pszOutBuf[cch] = '\0';
+    }
+    return cch;
+}
+
+/* Put the string pszName into the current OMF record.
+   In the OMF record, the string is preceded by a length byte.  The
+   string length must not exceed 255; if it is too long, display a
+   warning and truncate the string.  Moreover, there must be enough
+   space left in the OMF record; if there isn't, display an error
+   message and abort. */
+
+static void put_nstr(const char *pszName, size_t cch)
+{
+    char szName[256];
+
+    cch = make_nstr(pszName, cch, szName);
+    
+    if (!fits(1 + cch))
+        doesn_fit();
+    out_data[out_idx++] = (byte)cch;
+    memcpy(out_data+out_idx, szName, cch);
+    out_idx += cch;
 }
 
@@ -1200,10 +1211,18 @@
   int i;
   const char *pub_name;
+  char szPubName[256];
+  int cchPubName;
 
   for (i = 0; i < sym_count - 1; ++i)
     if (sym_ptr[i].n_type == (N_INDR|N_EXT) && sym_ptr[i+1].n_type == N_EXT)
       {
+        pub_name = (const char *)(str_ptr + sym_ptr[i].n_un.n_strx);
+        if (strip_underscore (pub_name))
+          ++pub_name;
+        cchPubName = make_nstr (pub_name, strlen(pub_name), szPubName);
+
         init_rec (ALIAS);
-        put_sym ((const char *)(str_ptr + sym_ptr[i].n_un.n_strx));
+        put_8 (cchPubName);
+        put_mem (szPubName, cchPubName);
         put_sym ((const char *)(str_ptr + sym_ptr[i+1].n_un.n_strx));
         write_rec ();
@@ -1211,8 +1230,5 @@
         if (out_lib != NULL)
           {
-            pub_name = (const char *)(str_ptr + sym_ptr[i].n_un.n_strx);
-            if (strip_underscore (pub_name))
-              ++pub_name;
-            if (omflib_add_pub (out_lib, pub_name, mod_page, lib_errmsg) != 0)
+            if (omflib_add_pub (out_lib, szPubName, mod_page, lib_errmsg) != 0)
               error (lib_errmsg);
           }
@@ -1248,4 +1264,6 @@
   dword address;
   char  szName[256];
+  char  szPubName[256];
+  int   cchPubName;
 
   started = FALSE;
@@ -1266,4 +1284,8 @@
                 if ( sym_ptr[i].n_type >= N_WEAKU && sym_ptr[i].n_type <= N_WEAKB )
                     name = weak_process_name(&sym_ptr[i], name, szName, sizeof(szName));
+                pub_name = name;
+                if (strip_underscore (pub_name))
+                  ++pub_name;
+                cchPubName = make_nstr (pub_name, strlen (pub_name), szPubName);
 
                 if (    out_lib != NULL
@@ -1274,8 +1296,5 @@
                          ||  sym_ptr[i].n_type == N_WEAKD ) )
                   {
-                    pub_name = name;
-                    if (strip_underscore (pub_name))
-                      ++pub_name;
-                    if (omflib_add_pub (out_lib, pub_name, mod_page,
+                    if (omflib_add_pub (out_lib, szPubName, mod_page,
                                         lib_errmsg) != 0)
                       error (lib_errmsg);
@@ -1296,5 +1315,6 @@
                     started = TRUE;
                   }
-                put_sym (name);
+                put_8 (cchPubName);
+                put_mem (szPubName, cchPubName);
                 if (big)
                   put_32 (address);
@@ -1331,4 +1351,5 @@
               if (out_lib != NULL)
                 {
+                  /* Note! make_nstr is not needed here, main is too short. */
                   if (omflib_add_pub (out_lib, name, mod_page, lib_errmsg) != 0)
                     error (lib_errmsg);
@@ -2412,12 +2433,16 @@
                          long ord, const char *mod)
 {
+  char szPubName[256];
+  int  cchPubName;
+
   /* Skip a leading underscore character if present. */
 
   if (strip_underscore (pub_name))
     ++pub_name;
+  cchPubName = make_nstr (pub_name, strlen (pub_name), szPubName);
 
   /* Make the symbol public in the output library. */
 
-  if (omflib_add_pub (out_lib, pub_name, mod_page, lib_errmsg) != 0)
+  if (omflib_add_pub (out_lib, szPubName, mod_page, lib_errmsg) != 0)
     error (lib_errmsg);
 
@@ -2429,5 +2454,6 @@
   put_8 (IMPDEF_SUBTYPE);
   put_8 (proc_name == NULL ? 0x01 : 0x00); /* Import by ordinal or by name */
-  put_str (pub_name);           /* Underscore already removed above */
+  put_8 (cchPubName);
+  put_mem (szPubName, cchPubName);
   put_str (mod);
   if (proc_name == NULL)
@@ -2435,6 +2461,7 @@
   else if (strcmp (proc_name, pub_name) == 0)
     put_8 (0);
-  else
+  else {
     put_str (proc_name);
+  }
   write_rec ();
   init_rec (MODEND|REC32);
