Changeset 3940


Ignore:
Timestamp:
Nov 10, 2014, 9:16:06 PM (10 years ago)
Author:
bird
Message:

emxomfstrip: Debugged it.

Location:
trunk/emx/src/emxomf
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/emx/src/emxomf/emxomfld.c

    r3731 r3940  
    728728
    729729            if (   (   uBuf.OmfHdr.len >= sizeof(uBuf.OmfHdr)
    730                     && cbRead > sizeof(sizeof(uBuf.OmfHdr)) )
     730                    && cbRead > sizeof(uBuf.OmfHdr) )
    731731                    && (   uBuf.OmfHdr.type == THEADR
    732732                        /*|| uBuf.OmfHdr.type == LIBADR - doubt wlink can handle this */)
  • TabularUnified trunk/emx/src/emxomf/emxomfstrip.c

    r3939 r3940  
    1919#include <string.h>
    2020#include <sys/stat.h>
     21#include <sys/omflib.h>
    2122#if defined(_MSC_VER) || defined(__WATCOMC__)
    2223# include <io.h>
     24# define ftruncate _chsize
    2325#endif
     26
     27
    2428
    2529/*******************************************************************************
     
    5862        unsigned cbThis = cb < sizeof(abBuf) ? (unsigned)cb : sizeof(abBuf);
    5963        if (fread(abBuf, cbThis, 1, pSrc) != 1)
    60             return error("Read error (@%#lx) from '%s': %s\n", offSrc, pszSrcName, strerror(errno));
    61         if (fwrite(abBuf, cbThis, 1, pSrc) != 1)
    62             return error("Write error to '%s': %s\n", pszDstName, strerror(errno));
     64            return error("fread (@%#lx) failed on '%s': %s\n", offSrc, pszSrcName, strerror(errno));
     65        if (fwrite(abBuf, cbThis, 1, pDst) != 1)
     66            return error("fwrite failed on '%s': %s\n", pszDstName, strerror(errno));
    6367        cb -= cbThis;
    6468        offSrc += cbThis;
    6569    }
    6670    return 0;
     71}
     72
     73
     74static int isNbSignature(byte const *pbMagic)
     75{
     76    return pbMagic[0] == 'N'
     77        && pbMagic[1] == 'B'
     78        && pbMagic[2] >= '0' && pbMagic[2] <= '1'
     79        && pbMagic[3] >= '0' && pbMagic[3] <= '9';
     80}
     81
     82
     83static int isLxDebugInfoSignature(byte const *pbMagic)
     84{
     85    return isNbSignature(pbMagic);
    6786}
    6887
     
    89108        struct exe1p2_header mz;
    90109        struct os2_header lx;
     110        byte  abBuf[32];
    91111    } uHdr;
    92112    union
     
    120140            return error("Failed to read the header of '%s': %s\n", pOpts->pszInFile, strerror(errno));
    121141    }
     142    else if (   uHdr.abBuf[0] == THEADR
     143             && (uHdr.abBuf[1] > 3 || uHdr.abBuf[2]) )
     144        return error("%s: Stripping OMF object files is not currently supported!\n", pOpts->pszInFile);
     145    else if (   uHdr.abBuf[0] == LIBHDR
     146             && (uHdr.abBuf[1] > 3 || uHdr.abBuf[2]) )
     147        return error("%s: Stripping OMF library files is not currently supported!\n", pOpts->pszInFile);
    122148    else
    123149        return error("Header of '%s' is not recognized (%#x)\n", pOpts->pszInFile, uHdr.mz.e_magic);
     
    128154     */
    129155    if (   uHdr.lx.debug_offset != 0
    130         && uHdr.lx.debug_size != 0)
    131     {
    132         offDbg = uHdr.lx.debug_offset + offLX;
     156        && uHdr.lx.debug_size != 0 && 0)
     157    {
     158        offDbg = uHdr.lx.debug_offset;
    133159        cbDbg  = uHdr.lx.debug_size;
     160        if (   offDbg > StIn.st_size
     161            || cbDbg > StIn.st_size
     162            || (off_t)offDbg + cbDbg > StIn.st_size)
     163            return error("Debug info indicated by the LX header of '%s' makes no sense! (%#lx + %#lx = %#lx (file: %#x)\n",
     164                         pOpts->pszInFile, offDbg, cbDbg, offDbg + cbDbg, (unsigned long)StIn.st_size);
     165
     166        if (   fseek(pInFile, offDbg, SEEK_SET) != 0
     167            || fread(&uBuf, 4, 1, pInFile) != 1)
     168            return error("Error seeking/reading debug info header (@%#lx) in '%s': %s\n",
     169                         offDbg, pOpts->pszInFile, strerror(errno));
     170        if (!isLxDebugInfoSignature(uBuf.abBuf))
     171        {
     172            /* Try add the LX offset. */
     173            if (   fseek(pInFile, offDbg + offLX, SEEK_SET) == 0
     174                && fread(&uBuf.abBuf[4], 4, 1, pInFile) == 0
     175                && isLxDebugInfoSignature(&uBuf.abBuf[4]))
     176                offDbg += offLX;
     177            else
     178                return error("Debug info indicated by the LX header of '%s' is not recognized! (magic: %02x %02x %02x %02x)\n",
     179                             pOpts->pszInFile, uBuf.abBuf[0], uBuf.abBuf[1], uBuf.abBuf[2], uBuf.abBuf[3]);
     180        }
     181
    134182        if (offDbg + cbDbg != StIn.st_size) /* (This can be relaxed if we wish.) */
    135             return error("Debug info indicated by the LX header is not at the end of '%s'\n", pOpts->pszInFile);
     183            return error("Debug info indicated by the LX header is not at the end of '%s'! (%#lx + %#lx = %#lx (file: %#x)\n",
     184                         pOpts->pszInFile, offDbg, cbDbg, offDbg + cbDbg, (unsigned long)StIn.st_size);
    136185    }
    137186    /*
     
    144193        if (fread(&uBuf, 8, 1, pInFile) != 1)
    145194            return error("Error reading last 8 bytes of '%s': %s\n", pOpts->pszInFile, strerror(errno));
    146         if (   uBuf.abBuf[0] == 'N'
    147             && uBuf.abBuf[1] == 'B'
    148             && uBuf.abBuf[2] >= '0' && uBuf.abBuf[2] <= '9'
    149             && uBuf.abBuf[3] >= '0' && uBuf.abBuf[3] <= '9')
     195        if (isNbSignature(uBuf.abBuf))
    150196        {
    151197            int iVer = (uBuf.abBuf[2] - '0') * 10 + (uBuf.abBuf[3] - '0');
     
    159205                             pOpts->pszInFile, cbDbg, (unsigned long)StIn.st_size);
    160206            offDbg = (unsigned long)StIn.st_size - cbDbg;
    161         }
    162     }
    163 
    164     /*
    165      * Anything to strip?
     207
     208            if (   fseek(pInFile, offDbg, SEEK_SET) != 0
     209                || fread(&uBuf.abBuf[4], 4, 1, pInFile) != 1)
     210                return error("Error seeking/reading debug info header (@%#lx) in '%s': %s\n",
     211                             offDbg, pOpts->pszInFile, strerror(errno));
     212            if (uBuf.adwBuf[0] != uBuf.adwBuf[1])
     213                return error("Debug header does not match trailer in '%s': %02x %02x %02x %02x  !=   %02x %02x %02x %02x\n",
     214                             pOpts->pszInFile,
     215                             uBuf.abBuf[0], uBuf.abBuf[1], uBuf.abBuf[2], uBuf.abBuf[3],
     216                             uBuf.abBuf[4+0], uBuf.abBuf[4+1], uBuf.abBuf[4+2], uBuf.abBuf[4+3]);
     217        }
     218    }
     219
     220    /*
     221     * Did we find anything to strip? If not return immediately indicating this.
    166222     */
    167223    if (cbDbg == 0)
     
    205261         * Truncate the output file if it's the same as the input.
    206262         */
    207         if (rc == 0 && pOutFile != pInFile)
    208         {
    209 #if defined(_MSC_VER) || defined(__WATCOMC__)
    210             if (_chsize(fileno(pOutFile), offDbg) == 0)
    211 #else
    212             if (ftruncate(fileno(pOutFile), offDbg) == 0)
    213 #endif
     263        if (rc == 0 && pOutFile == pInFile)
     264        {
     265            if (ftruncate(fileno(pOutFile), offDbg) != 0)
    214266                return error("Error truncating '%s': %s\n", pOpts->pszInFile, strerror(errno));
    215267        }
     
    248300     * Parse the options.
    249301     */
    250     while ((ch = getopt_long(argc, argv, "dD:gsSxXvV", NULL, NULL)) != EOF)
     302    while ((ch = getopt_long(argc, argv, "dD:go:sSxXvV", NULL, NULL)) != EOF)
    251303    {
    252304        switch (ch)
     
    275327                printf(VERSION INNOTEK_VERSION "\n");
    276328                return 0;
     329
     330            default:
     331              return usage(stderr, 1);
    277332        }
    278333    }
     
    287342     * Process the files.
    288343     */
    289     while (optind < argc)
     344    while (   optind < argc
     345           && rcExit == 0)
    290346    {
    291347        /* Open the input and maybe output file. */
     
    302358            int   fNull = 0;
    303359            FILE *pOutFile;
    304             if (Opts.pszOutFile)
     360            if (!Opts.pszOutFile)
     361                pOutFile = pInFile;
     362            else
    305363            {
    306364                if (strcmp(Opts.pszOutFile, "/dev/null"))
     
    317375                FILE *pDbgFile = NULL;
    318376                if (Opts.pszDbgFile)
    319                     pDbgFile = fopen(Opts.pszOutFile, "wb");
     377                    pDbgFile = fopen(Opts.pszDbgFile, "wb");
    320378                if (pDbgFile || !Opts.pszDbgFile)
    321379                {
     
    337395                }
    338396                else
    339                     rcExit = error("Failed opening output file '%s': %s\n", Opts.pszDbgFile, strerror(errno));
     397                    rcExit = error("Failed opening debug info file '%s': %s\n", Opts.pszDbgFile, strerror(errno));
    340398                if (pOutFile && pOutFile != pInFile && fclose(pOutFile) != 0)
    341399                    rcExit = error("fclose failed on '%s': %s\n", Opts.pszOutFile, strerror(errno));
     
    348406        else
    349407            rcExit = error("Failed opening '%s': %s\n", Opts.pszInFile, strerror(errno));
     408        optind++;
    350409    }
    351410
Note: See TracChangeset for help on using the changeset viewer.