Changeset 4358 for trunk/tools


Ignore:
Timestamp:
Oct 2, 2000, 6:01:40 AM (25 years ago)
Author:
bird
Message:

Added some limited dump capabilities for PE executables.

Location:
trunk/tools/common
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/common/kFile.cpp

    r4129 r4358  
    1 /* $Id: kFile.cpp,v 1.4 2000-08-31 03:00:12 bird Exp $
     1/* $Id: kFile.cpp,v 1.5 2000-10-02 04:01:39 bird Exp $
    22 *
    33 * kFile - Simple (for the time being) file class.
    44 *
    5  * Copyright (c) 2000 knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
     5 * Copyright (c) 2000 knut st. osmundsen (knut.stange.osmundsen@mynd.no)
    66 *
    77 * Project Odin Software License can be found in LICENSE.TXT
     
    2626#include <stdio.h>
    2727
    28 #include <kFile.h>
     28#include "kFile.h"
     29
     30/*******************************************************************************
     31*   Global Variables                                                           *
     32*******************************************************************************/
     33kFile kFile::StdIn((HFILE)0, TRUE);
     34kFile kFile::StdOut((HFILE)1, FALSE);
     35kFile kFile::StdErr((HFILE)2, FALSE);
    2936
    3037
     
    3744BOOL    kFile::refreshFileStatus()
    3845{
     46    if (fStdDev)
     47        return fStatusClean = TRUE;
     48
    3949    if (!fStatusClean)
    4050    {
     
    7787}
    7888
     89/**
     90 * Creates a kFile object for a file that is opened allready.
     91 *  Intended use for the three standard handles only.
     92 *
     93 * @returns     <object> with state updated.
     94 * @param       pszFilename     Filename.
     95 * @param       fReadOnly       TRUE:  Open the file readonly.
     96 *                              FALSE: Open the file readwrite appending
     97 *                                     existing files.
     98 * @author      knut st. osmundsen (knut.stange.osmundsen@mynd.no)
     99 */
     100kFile::kFile(HFILE hFile, BOOL fReadOnly)
     101:   fReadOnly(fReadOnly),
     102    fStatusClean(FALSE),
     103    fThrowErrors(FALSE),
     104    offVirtual(0),
     105    offReal(0),
     106    pszFilename(NULL),
     107    hFile(hFile),
     108    fStdDev(TRUE)
     109{
     110    if (!refreshFileStatus())
     111        throw ((int)rc);
     112    this->pszFilename = strdup("");
     113}
     114
    79115
    80116/**
     
    86122 *                              FALSE: Open the file readwrite appending
    87123 *                                     existing files.
    88  * @author      knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
     124 * @author      knut st. osmundsen (knut.stange.osmundsen@mynd.no)
    89125 */
    90126kFile::kFile(const char *pszFilename, BOOL fReadOnly/*=TRUE*/)
     
    94130    offVirtual(0),
    95131    offReal(0),
    96     pszFilename(NULL)
     132    pszFilename(NULL),
     133    fStdDev(FALSE)
    97134{
    98135    ULONG   fulOpenFlags;
  • trunk/tools/common/kFile.h

    r4129 r4358  
    1 /* $Id: kFile.h,v 1.4 2000-08-31 03:00:13 bird Exp $
     1/* $Id: kFile.h,v 1.5 2000-10-02 04:01:39 bird Exp $
    22 *
    33 * kFile - Simple (for the time being) file class.
     
    2626    HFILE           hFile;              /* Pointer to stdio filehandle */
    2727    BOOL            fReadOnly;          /* True if readonly access, False is readwrite. */
     28    BOOL            fStdDev;            /* True if stdio, stderr or stdin. Filestatus is invalid with this is set.*/
    2829    APIRET          rc;                 /* Last error (return code). */
    2930    FILESTATUS4     filestatus;         /* Filestatus data. */
     
    4243    BOOL            position() throw(int);
    4344
     45    /** @cat constructors */
     46private:
     47    kFile(HFILE hFile, BOOL fReadOnly);
    4448public:
    45     /** @cat constructor */
    4649    kFile(const char *pszFilename, BOOL fReadOnly = TRUE) throw(int);
    4750    ~kFile();
     
    7578    BOOL            setFailOnErrors();
    7679    int             getLastError() const;
     80
     81    /** standard files */
     82    static kFile    StdOut;
     83    static kFile    StdIn;
     84    static kFile    StdErr;
    7785};
    7886
  • trunk/tools/common/kFileFormatBase.cpp

    r3246 r4358  
    1 /* $Id: kFileFormatBase.cpp,v 1.1 2000-03-27 10:18:40 bird Exp $
     1/* $Id: kFileFormatBase.cpp,v 1.2 2000-10-02 04:01:39 bird Exp $
    22 *
    33 * kFileFormatBase - Base class for kFile<format> classes.
     
    7171    return pvFile;
    7272}
     73
     74/**
     75 * Dump function.
     76 * @returns Successindicator.
     77 * @param   pOut    Output file.
     78 */
     79BOOL   kFileFormatBase::dump(kFile *pOut)
     80{
     81    pOut = pOut;
     82    return FALSE;
     83}
     84
  • trunk/tools/common/kFileFormatBase.h

    r4129 r4358  
    1 /* $Id: kFileFormatBase.h,v 1.3 2000-08-31 03:00:13 bird Exp $
     1/* $Id: kFileFormatBase.h,v 1.4 2000-10-02 04:01:39 bird Exp $
    22 *
    33 * kFileFormatBase - Base class for kFile<format> classes.
     
    2323******************************************************************************/
    2424#pragma pack(4)
     25
     26class kFile;
    2527
    2628/**
     
    5759
    5860    static void * readfile(const char *pszFilename);
     61    virtual BOOL  dump(kFile *pOut);
    5962};
    6063
  • trunk/tools/common/kFilePE.cpp

    r824 r4358  
    3434#include <assert.h>
    3535#include <peexe.h>
     36#include "kFile.h"
    3637#include "kFileFormatBase.h"
    3738#include "kFilePe.h"
     
    4546kFilePE::kFilePE(FILE *phFile) throw(int) : pvBase(NULL),
    4647    pDosHdr(NULL), pFileHdr(NULL), pOptHdr(NULL), paDataDir(NULL), paSectionHdr(NULL),
    47     pExportDir(NULL)
     48    pExportDir(NULL),
     49    pImportDir(NULL),
     50    pRsrcDir(NULL),
     51    pBRelocDir(NULL),
     52    pDebugDir(NULL),
     53    pCopyright(NULL),
     54    pulGlobalPtr(NULL),
     55    pTLSDir(NULL),
     56    pLoadConfigDir(NULL),
     57    pBoundImportDir(NULL),
     58    pIATDir(NULL),
     59    pDelayImportDir(NULL)
    4860{
    4961    IMAGE_DOS_HEADER doshdr;
     
    93105
    94106                        cbSection = min(pSectionHdr->Misc.VirtualSize, pSectionHdr->SizeOfRawData);
    95                         if (fseek(phFile, pSectionHdr->PointerToRawData, SEEK_SET)
    96                             ||
    97                             fread((void*)((ULONG)pvBase + pSectionHdr->VirtualAddress), (size_t)cbSection, 1, phFile) != 1
     107                        if (cbSection
     108                            &&
     109                             (fseek(phFile, pSectionHdr->PointerToRawData, SEEK_SET)
     110                             ||
     111                             fread((void*)((ULONG)pvBase + pSectionHdr->VirtualAddress), (size_t)cbSection, 1, phFile) != 1
     112                             )
    98113                            )
    99114                        {
     
    120135                                                           pOptHdr->NumberOfRvaAndSizes*sizeof(*paDataDir));
    121136
    122                     if (paDataDir[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress)
    123                         pExportDir = (PIMAGE_EXPORT_DIRECTORY)((int)pvBase + paDataDir[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
     137                    //if (paDataDir[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress)
     138                    //    pExportDir = (PIMAGE_EXPORT_DIRECTORY)((int)pvBase + paDataDir[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
     139                    for (i = 0; i < pOptHdr->NumberOfRvaAndSizes && i < 16; i++)
     140                    {
     141                        if (paDataDir[i].VirtualAddress != 0)
     142                        {
     143                            if (paDataDir[i].VirtualAddress < pOptHdr->SizeOfImage)
     144                                ((PULONG)&this->pExportDir)[i] = (int)pvBase + paDataDir[i].VirtualAddress;
     145                            #ifdef DEBUG
     146                            else
     147                                fprintf(stderr, "bad directory pointer %d\n", i);
     148                            #endif
     149                        }
     150                    }
    124151                }
    125152                else
     
    227254
    228255
     256/**
     257 * Mini dump function.
     258 */
     259BOOL  kFilePE::dump(kFile *pOut)
     260{
     261    int i,j,k;
     262    int c;
     263
     264    /*
     265     * Dump sections.
     266     */
     267    pOut->printf("Sections\n"
     268                 "--------\n");
     269    for (i = 0; i < pFileHdr->NumberOfSections; i++)
     270    {
     271        pOut->printf("%2d  %-8.8s  VirtSize: 0x%08x  RVA: 0x%08x\n"
     272                     "    RawSize:  0x%08x  FileOffset: 0x%08x\n"
     273                     "    #Relocs:  0x%08x  FileOffset: 0x%08x\n"
     274                     "    #LineNbr: 0x%08x  FileOffset: 0x%08x\n"
     275                     "    Characteristics: 0x%08x\n",
     276                     i,
     277                     paSectionHdr[i].Name,
     278                     paSectionHdr[i].Misc.VirtualSize,
     279                     paSectionHdr[i].VirtualAddress,
     280                     paSectionHdr[i].PointerToRawData,
     281                     paSectionHdr[i].PointerToRawData,
     282                     paSectionHdr[i].NumberOfRelocations,
     283                     paSectionHdr[i].PointerToRelocations,
     284                     paSectionHdr[i].NumberOfLinenumbers,
     285                     paSectionHdr[i].PointerToLinenumbers,
     286                     paSectionHdr[i].Characteristics
     287                     );
     288
     289    }
     290
     291
     292    /*
     293     * Dump the directories.
     294     */
     295    pOut->printf("Data Directory\n"
     296                 "--------------\n");
     297    for (i = 0; i < pOptHdr->NumberOfRvaAndSizes; i++)
     298    {
     299        static const char * apszDirectoryNames[] =
     300        {
     301            "Export",
     302            "Import",
     303            "Resource",
     304            "Exception",
     305            "Security",
     306            "Base Reloc",
     307            "Debug",
     308            "Copyright",
     309            "Global Ptr",
     310            "TLS",
     311            "Load Config",
     312            "Bound Import",
     313            "IAT",
     314            "Delay Import",
     315            "COM Descriptor",
     316            "unknown",
     317            "unknown"
     318        };
     319
     320        pOut->printf("%2d  %-16s  Size: 0x%08x  RVA: 0x%08x\n",
     321                     i,
     322                     apszDirectoryNames[i],
     323                     pOptHdr->DataDirectory[i].Size,
     324                     pOptHdr->DataDirectory[i].VirtualAddress);
     325    }
     326    pOut->printf("\n");
     327
     328
     329    /*
     330     * Dump TLS directory if present
     331     */
     332    if (pTLSDir)
     333    {
     334        pOut->printf("TLS Directory\n"
     335                     "-------------\n");
     336        if (pOptHdr->DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].Size % sizeof(IMAGE_TLS_DIRECTORY))
     337            pOut->printf(" Warning! The size directory isn't a multiple of the directory struct!");
     338
     339        c = (int)(pOptHdr->DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS].Size / sizeof(IMAGE_TLS_DIRECTORY));
     340        for (i = 0; i < c; i++)
     341        {
     342
     343            pOut->printf("%2d  StartAddressOfRawData %p\n"
     344                         "    EndAddressOfRawData   %p\n"
     345                         "    AddressOfIndex        %p\n"
     346                         "    AddressOfCallBacks    %p\n"
     347                         "    SizeOfZeroFill        %p\n"
     348                         "    Characteristics       %p\n",
     349                         i,
     350                         pTLSDir[i].StartAddressOfRawData,
     351                         pTLSDir[i].EndAddressOfRawData,
     352                         pTLSDir[i].AddressOfIndex,
     353                         pTLSDir[i].AddressOfCallBacks,
     354                         pTLSDir[i].SizeOfZeroFill,
     355                         pTLSDir[i].Characteristics);
     356
     357            /* Print Callbacks */
     358            if (pTLSDir[i].AddressOfCallBacks)
     359            {
     360                PULONG  paulIndex;
     361                PULONG  paulCallback;
     362                ULONG   ulBorlandRVAFix = 0UL;
     363
     364                /* Check if the addresses in the TLSDir is RVAs or real addresses */
     365                if (pTLSDir[i].StartAddressOfRawData > pOptHdr->ImageBase)
     366                    ulBorlandRVAFix = pOptHdr->ImageBase;
     367
     368                j = 0;
     369                paulIndex    = (PULONG)((ULONG)pTLSDir[i].AddressOfIndex - ulBorlandRVAFix + (ULONG)this->pvBase);
     370                paulCallback = (PULONG)((ULONG)pTLSDir[i].AddressOfCallBacks - ulBorlandRVAFix + (ULONG)this->pvBase);
     371                if (*paulCallback)
     372                {
     373                    pOut->printf("    Callbacks:\n");
     374                    for (j = 0; paulCallback[j] != 0; j++)
     375                    {
     376                        pOut->printf("      %02d  Address: 0x%08x  Index: 0x%08x\n",
     377                                     paulIndex[j],
     378                                     paulCallback[j]);
     379                    }
     380                }
     381                else
     382                    pOut->printf("    (Empty callback array!)\n");
     383            }
     384
     385        }
     386
     387        pOut->printf("\n");
     388    }
     389
     390
     391    return TRUE;
     392}
     393
  • trunk/tools/common/kFilePE.h

    r824 r4358  
    77
    88#ifndef _kFilePE_h_
    9 #define  _kFilePE_h_
     9#define _kFilePE_h_
    1010
    1111/*******************************************************************************
    1212*   Defined Constants                                                          *
    1313*******************************************************************************/
     14
    1415#if !defined(__WINE_PEEXE_H) && !defined(_WINNT_)
     16#if 0
    1517    #define PIMAGE_DOS_HEADER           void*
    1618    #define PIMAGE_FILE_HEADER          void*
     
    1820    #define PIMAGE_DATA_DIRECTORY       void*
    1921    #define PIMAGE_SECTION_HEADER       void*
    20     #define PIMAGE_EXPORT_DIRECTORY     void*
     22    #define PIMAGE_EXPORT_DIRECTORY          void*
     23    #define PIMAGE_IMPORT_DESCRIPTOR         void*
     24    #define PIMAGE_RESOURCE_DIRECTORY        void*
     25    #define PIMAGE_BASE_RELOCATION           void*
     26    #define PIMAGE_DEBUG_DIRECTORY           void*
     27    #define PSZ                              void*
     28    #define PULONG                           void*
     29    #define PIMAGE_TLS_DIRECTORY             void*
     30    #define PIMAGE_LOAD_CONFIG_DIRECTORY     void*
     31    #define PIMAGE_IMPORT_DESCRIPTOR         void*
     32    #define PIMAGE_THUNK_DATA                void*
     33    #define PIMAGE_IMPORT_DESCRIPTOR         void*
     34#else
     35#include <peexe.h>
     36#endif
    2137#endif
    2238
     
    4359        PIMAGE_SECTION_HEADER   paSectionHdr;
    4460
    45         /* directories */
    46         PIMAGE_EXPORT_DIRECTORY pExportDir;
     61        /** @cat
     62         * Directory pointers.
     63         */
     64        PIMAGE_EXPORT_DIRECTORY         pExportDir;     /* 0 */
     65        PIMAGE_IMPORT_DESCRIPTOR        pImportDir;     /* 1 */
     66        PIMAGE_RESOURCE_DIRECTORY       pRsrcDir;       /* 2 */
     67        PVOID                           pExcpDir;       /* 3 */
     68        PVOID                           pSecDir;        /* 4 */
     69        PIMAGE_BASE_RELOCATION          pBRelocDir;     /* 5 */
     70        PIMAGE_DEBUG_DIRECTORY          pDebugDir;      /* 6 */
     71        PSZ                             pCopyright;     /* 7 */
     72        PULONG                          pulGlobalPtr;   /* 8 */  //is this the correct pointer type?
     73        PIMAGE_TLS_DIRECTORY            pTLSDir;        /* 9 */
     74        PIMAGE_LOAD_CONFIG_DIRECTORY    pLoadConfigDir; /* 10 */
     75        PIMAGE_IMPORT_DESCRIPTOR        pBoundImportDir;/* 11 */ //is this the correct pointer type?
     76        PIMAGE_THUNK_DATA               pIATDir;        /* 12 */ //is this the correct pointer type?
     77        PIMAGE_IMPORT_DESCRIPTOR        pDelayImportDir;/* 13 */
     78        PVOID                           pComDir;        /* 14 */
     79        PVOID                           pv15;           /* 15 */
    4780
    4881    public:
     
    5588        BOOL  findNextExport(PEXPORTENTRY pExport);
    5689        BOOL  isPe() const   { return TRUE;}
     90
     91        BOOL  dump(kFile *pOut);
    5792};
    5893
  • trunk/tools/common/makefile

    r3534 r4358  
    1 # $Id: makefile,v 1.5 2000-05-13 16:04:43 bird Exp $
     1# $Id: makefile,v 1.6 2000-10-02 04:01:40 bird Exp $
    22
    33#
     
    1818#
    1919
    20 PDWIN32_INCLUDE = ..\..\include
    21 PDWIN32_TOOLS   = ..\bin
    22 !include $(PDWIN32_INCLUDE)\pdwin32.mk
    23 
    24 
    25 !ifdef DEBUG
    26 MAKE_CMD = $(MAKE) -nologo DEBUG=1
    27 !else
    28 MAKE_CMD = $(MAKE) -nologo
    29 !endif
     20!include ..\..\include\pdwin32.mk
    3021
    3122
     
    4839    @$(MAKE_CMD) OMF=1 -f makefile.gcc
    4940
     41kDump.exe: dummy
     42    -@echo $@
     43    @$(MAKE_CMD) -f makefile.icc kDump.exe
     44
    5045
    5146#a simple hack to make nmake process the target.
  • trunk/tools/common/makefile.icc

    r3592 r4358  
    1 # $Id: makefile.icc,v 1.10 2000-05-23 18:23:04 bird Exp $
     1# $Id: makefile.icc,v 1.11 2000-10-02 04:01:40 bird Exp $
    22
    33#
     
    5252
    5353
     54# CommonICC
    5455commonicc.lib: $(OBJDIR)\commonicc.lib
    5556    $(CP) $** $@
     
    5859    $(RM) $@
    5960    $(ILIB) /nobackup $@ $(OBJS: = +), NUL;
     61
     62# kDump
     63kDump.exe: $(OBJDIR)\kDump.exe
     64    $(CP) $** $@
     65
     66$(OBJDIR)\kDump.exe: commonicc.lib $(OBJDIR)\kDump.obj
     67    $(LD) $(LDFLAGS) -Fe$@ $** $(RTLLIB) OS2386.LIB
    6068
    6169
Note: See TracChangeset for help on using the changeset viewer.