--- emxomf.c.bak	2010-03-12 02:00:22.000000000 +0300
+++ emxomf.c	2010-07-30 21:33:14.000000000 +0400
@@ -796,18 +796,13 @@
     return psz;
 }
 
+/* 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. */
 
-
-
-
-/* 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)
+static int make_nstr (const char *pszName, size_t cch, char *pOutBuf)
 {
     if (    cch > SYMBOL_MAX_LENGTH
         &&  !strstr(pszName + SYMBOL_MAX_LENGTH - SYMBOL_WEAK_LENGTH, "!_")
@@ -825,28 +820,44 @@
         psz = format_u64((uint32_t)uHash, &szHash[2], 62, 6);
         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(pOutBuf, pszName, SYMBOL_MAX_LENGTH);
+        memcpy(pOutBuf + SYMBOL_MAX_LENGTH, szHash, SYMBOL_HASH_LENGTH);
+        pOutBuf[SYMBOL_MAX_LENGTH + SYMBOL_HASH_LENGTH] = '\0';
+
+        if (!quiet)
+            warning ("Truncated symbol '%.*s' to '%.*s%s'", cch, pszName, SYMBOL_MAX_LENGTH, pszName, szHash);
 
-        warning ("Truncated symbol '%.*s' to '%.*s%s'", cch, pszName, SYMBOL_MAX_LENGTH, pszName, szHash);
+        return SYMBOL_MAX_LENGTH + SYMBOL_HASH_LENGTH;
     }
     else
     {
         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(pOutBuf, pszName, cch);
+        pOutBuf[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
+   (which is guaranteed by make_nstr()). 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];
+
+    make_nstr(pszName, cch, szName);
+    cch = strlen(szName);
+
+    if (!fits(1 + cch))
+        doesn_fit();
+    out_data[out_idx++] = (byte)cch;
+    memcpy(out_data+out_idx, szName, cch);
+    out_idx += cch;
+}
+
 /* Put the null-terminated string pszName into the current OMF record.
    See put_nstr() for full details on string handling. */
 
@@ -1182,7 +1193,8 @@
                 pachName[SYMBOL_MAX_LENGTH + 1] = '_';
                 format_u64(u32Hash, &pachName[SYMBOL_MAX_LENGTH + 2], 62, 6);
                 memcpy(&pachName[SYMBOL_MAX_LENGTH + SYMBOL_HASH_LENGTH], weak_marker, SYMBOL_WEAK_LENGTH + 1);
-                warning("Truncated symbol '%s' to '%s' (weak)", pszOrgName, pachName);
+                if (!quiet)
+                    warning("Truncated symbol '%s' to '%s' (weak)", pszOrgName, pachName);
             }
 
             return pachName;
@@ -1201,21 +1213,26 @@
 {
   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 ();
 
         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);
           }
       }
@@ -1249,6 +1266,8 @@
   const char *name, *pub_name;
   dword address;
   char  szName[256];
+  char szPubName[256];
+  int cchPubName;
 
   started = FALSE;
   for (i = 0; i < sym_count; ++i)
@@ -1267,6 +1286,10 @@
                 /* for weaksymbols we may have to decorate the name */
                 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
                     &&  (   (   (sym_ptr[i].n_type & N_EXT)
@@ -1275,10 +1298,7 @@
                          ||  sym_ptr[i].n_type == N_WEAKT
                          ||  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);
                   }
@@ -1297,7 +1317,8 @@
                         put_16 (0);
                     started = TRUE;
                   }
-                put_sym (name);
+                put_8 (cchPubName);
+                put_mem (szPubName, cchPubName);
                 if (big)
                   put_32 (address);
                 else
@@ -1330,6 +1351,9 @@
               int big = ((address >= 0x10000 || force_big) == big);
 
               name++; /* skip the underscore */
+
+              /* Note: no need to call make_nstr() as main definitely fits 255 bytes */
+
               if (out_lib != NULL)
                 {
                   if (omflib_add_pub (out_lib, name, mod_page, lib_errmsg) != 0)
@@ -2413,14 +2437,19 @@
 static void make_import (const char *pub_name, const char *proc_name,
                          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);
 
   /* Write the import definition record. */
@@ -2436,8 +2465,10 @@
     put_16 (ord);
   else if (strcmp (proc_name, pub_name) == 0)
     put_8 (0);
-  else
-    put_str (proc_name);
+  else {
+    put_8 (cchPubName);
+    put_mem (szPubName, cchPubName);
+  }
   write_rec ();
   init_rec (MODEND|REC32);
   put_8 (0x00);                 /* Non-main module without start address */
