Ticket #220: emxomf-03-fix-symbol-too-long.diff

File emxomf-03-fix-symbol-too-long.diff, 7.9 KB (added by dmik, 14 years ago)
  • emxomf.c

    old new  
    796796    return psz;
    797797}
    798798
     799/* Put the string pszName into the given buffer pOutBuf (which must be at least
     800   256 bytes long). The string length must not exceed 255 bytes (OMF format
     801   requirement); if it is too long, display a warning and truncate the string.
     802   The string returned in pOutBuf is zero-terminated. The function returns the
     803   length of the resulting string. */
    799804
    800 
    801 
    802 
    803 /* Put the string pszName into the current OMF record.
    804    In the OMF record, the string is preceded by a length byte.  The
    805    string length must not exceed 255; if it is too long, display a
    806    warning and truncate the string.  Moreover, there must be enough
    807    space left in the OMF record; if there isn't, display an error
    808    message and abort. */
    809 
    810 static void put_nstr(const char *pszName, size_t cch)
     805static int make_nstr (const char *pszName, size_t cch, char *pOutBuf)
    811806{
    812807    if (    cch > SYMBOL_MAX_LENGTH
    813808        &&  !strstr(pszName + SYMBOL_MAX_LENGTH - SYMBOL_WEAK_LENGTH, "!_")
     
    825820        psz = format_u64((uint32_t)uHash, &szHash[2], 62, 6);
    826821        assert(psz - &szHash[0] == SYMBOL_HASH_LENGTH);
    827822
    828         if (!fits(1 + SYMBOL_MAX_LENGTH + SYMBOL_HASH_LENGTH))
    829             doesn_fit();
    830         out_data[out_idx++] = SYMBOL_MAX_LENGTH + SYMBOL_HASH_LENGTH;
    831         memcpy(out_data + out_idx, pszName, SYMBOL_MAX_LENGTH);
    832         out_idx += SYMBOL_MAX_LENGTH;
    833         memcpy(out_data + out_idx, szHash, SYMBOL_HASH_LENGTH);
    834         out_idx += SYMBOL_HASH_LENGTH;
     823        memcpy(pOutBuf, pszName, SYMBOL_MAX_LENGTH);
     824        memcpy(pOutBuf + SYMBOL_MAX_LENGTH, szHash, SYMBOL_HASH_LENGTH);
     825        pOutBuf[SYMBOL_MAX_LENGTH + SYMBOL_HASH_LENGTH] = '\0';
     826
     827        if (!quiet)
     828            warning ("Truncated symbol '%.*s' to '%.*s%s'", cch, pszName, SYMBOL_MAX_LENGTH, pszName, szHash);
    835829
    836         warning ("Truncated symbol '%.*s' to '%.*s%s'", cch, pszName, SYMBOL_MAX_LENGTH, pszName, szHash);
     830        return SYMBOL_MAX_LENGTH + SYMBOL_HASH_LENGTH;
    837831    }
    838832    else
    839833    {
    840834        assert(cch <= 0xff);
    841835        cch &= 0xff;
    842         if (!fits(1+cch))
    843             doesn_fit();
    844         out_data[out_idx++] = (byte)cch;
    845         memcpy(out_data+out_idx, pszName, cch);
    846         out_idx += cch;
     836        memcpy(pOutBuf, pszName, cch);
     837        pOutBuf[cch] = '\0';
     838        return cch;
    847839    }
    848840}
    849841
     842/* Put the string pszName into the current OMF record. In the OMF record, the
     843   string is preceded by a length byte.  The string length must not exceed 255
     844   (which is guaranteed by make_nstr()). Moreover, there must be enough space
     845   left in the OMF record; if there isn't, display an error message and abort.*/
     846
     847static void put_nstr(const char *pszName, size_t cch)
     848{
     849    char szName[256];
     850
     851    make_nstr(pszName, cch, szName);
     852    cch = strlen(szName);
     853
     854    if (!fits(1 + cch))
     855        doesn_fit();
     856    out_data[out_idx++] = (byte)cch;
     857    memcpy(out_data+out_idx, szName, cch);
     858    out_idx += cch;
     859}
     860
    850861/* Put the null-terminated string pszName into the current OMF record.
    851862   See put_nstr() for full details on string handling. */
    852863
     
    11821193                pachName[SYMBOL_MAX_LENGTH + 1] = '_';
    11831194                format_u64(u32Hash, &pachName[SYMBOL_MAX_LENGTH + 2], 62, 6);
    11841195                memcpy(&pachName[SYMBOL_MAX_LENGTH + SYMBOL_HASH_LENGTH], weak_marker, SYMBOL_WEAK_LENGTH + 1);
    1185                 warning("Truncated symbol '%s' to '%s' (weak)", pszOrgName, pachName);
     1196                if (!quiet)
     1197                    warning("Truncated symbol '%s' to '%s' (weak)", pszOrgName, pachName);
    11861198            }
    11871199
    11881200            return pachName;
     
    12011213{
    12021214  int i;
    12031215  const char *pub_name;
     1216  char szPubName[256];
     1217  int cchPubName;
    12041218
    12051219  for (i = 0; i < sym_count - 1; ++i)
    12061220    if (sym_ptr[i].n_type == (N_INDR|N_EXT) && sym_ptr[i+1].n_type == N_EXT)
    12071221      {
     1222        pub_name = (const char *)(str_ptr + sym_ptr[i].n_un.n_strx);
     1223        if (strip_underscore (pub_name))
     1224          ++pub_name;
     1225        cchPubName = make_nstr (pub_name, strlen(pub_name), szPubName);
     1226
    12081227        init_rec (ALIAS);
    1209         put_sym ((const char *)(str_ptr + sym_ptr[i].n_un.n_strx));
     1228        put_8 (cchPubName);
     1229        put_mem (szPubName, cchPubName);
    12101230        put_sym ((const char *)(str_ptr + sym_ptr[i+1].n_un.n_strx));
    12111231        write_rec ();
    12121232
    12131233        if (out_lib != NULL)
    12141234          {
    1215             pub_name = (const char *)(str_ptr + sym_ptr[i].n_un.n_strx);
    1216             if (strip_underscore (pub_name))
    1217               ++pub_name;
    1218             if (omflib_add_pub (out_lib, pub_name, mod_page, lib_errmsg) != 0)
     1235            if (omflib_add_pub (out_lib, szPubName, mod_page, lib_errmsg) != 0)
    12191236              error (lib_errmsg);
    12201237          }
    12211238      }
     
    12491266  const char *name, *pub_name;
    12501267  dword address;
    12511268  char  szName[256];
     1269  char szPubName[256];
     1270  int cchPubName;
    12521271
    12531272  started = FALSE;
    12541273  for (i = 0; i < sym_count; ++i)
     
    12671286                /* for weaksymbols we may have to decorate the name */
    12681287                if ( sym_ptr[i].n_type >= N_WEAKU && sym_ptr[i].n_type <= N_WEAKB )
    12691288                    name = weak_process_name(&sym_ptr[i], name, szName, sizeof(szName));
     1289                pub_name = name;
     1290                if (strip_underscore (pub_name))
     1291                  ++pub_name;
     1292                cchPubName = make_nstr (pub_name, strlen(pub_name), szPubName);
    12701293
    12711294                if (    out_lib != NULL
    12721295                    &&  (   (   (sym_ptr[i].n_type & N_EXT)
     
    12751298                         ||  sym_ptr[i].n_type == N_WEAKT
    12761299                         ||  sym_ptr[i].n_type == N_WEAKD ) )
    12771300                  {
    1278                     pub_name = name;
    1279                     if (strip_underscore (pub_name))
    1280                       ++pub_name;
    1281                     if (omflib_add_pub (out_lib, pub_name, mod_page,
     1301                    if (omflib_add_pub (out_lib, szPubName, mod_page,
    12821302                                        lib_errmsg) != 0)
    12831303                      error (lib_errmsg);
    12841304                  }
     
    12971317                        put_16 (0);
    12981318                    started = TRUE;
    12991319                  }
    1300                 put_sym (name);
     1320                put_8 (cchPubName);
     1321                put_mem (szPubName, cchPubName);
    13011322                if (big)
    13021323                  put_32 (address);
    13031324                else
     
    13301351              int big = ((address >= 0x10000 || force_big) == big);
    13311352
    13321353              name++; /* skip the underscore */
     1354
     1355              /* Note: no need to call make_nstr() as main definitely fits 255 bytes */
     1356
    13331357              if (out_lib != NULL)
    13341358                {
    13351359                  if (omflib_add_pub (out_lib, name, mod_page, lib_errmsg) != 0)
     
    24132437static void make_import (const char *pub_name, const char *proc_name,
    24142438                         long ord, const char *mod)
    24152439{
     2440  char szPubName[256];
     2441  int cchPubName;
     2442
    24162443  /* Skip a leading underscore character if present. */
    24172444
    24182445  if (strip_underscore (pub_name))
    24192446    ++pub_name;
    24202447
     2448  cchPubName = make_nstr (pub_name, strlen(pub_name), szPubName);
     2449
    24212450  /* Make the symbol public in the output library. */
    24222451
    2423   if (omflib_add_pub (out_lib, pub_name, mod_page, lib_errmsg) != 0)
     2452  if (omflib_add_pub (out_lib, szPubName, mod_page, lib_errmsg) != 0)
    24242453    error (lib_errmsg);
    24252454
    24262455  /* Write the import definition record. */
     
    24362465    put_16 (ord);
    24372466  else if (strcmp (proc_name, pub_name) == 0)
    24382467    put_8 (0);
    2439   else
    2440     put_str (proc_name);
     2468  else {
     2469    put_8 (cchPubName);
     2470    put_mem (szPubName, cchPubName);
     2471  }
    24412472  write_rec ();
    24422473  init_rec (MODEND|REC32);
    24432474  put_8 (0x00);                 /* Non-main module without start address */