⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cofflib.cpp

📁 TMS320F2808的完整驱动测试程序源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/*H***************************************************************************
*
* $Archive:: /TI/product/c2xxhll/CoffLib/CoffLib.c                           $
* $Revision:: 9                                                              $
* $Date:: 7/30/02 2:21p                                                      $
* $Author:: Tonyc                                                            $
*
* DESCRIPTION:
*    Generic coff reader.  
*
* USAGE/LIMITATIONS:
*
* NOTES:
*     This file does not currently handle relocations or ram model for
*     .cinit section.
*   
* (C) Copyright 1997 by Spectrum Digital Incorporated
* All rights reserved
*
*H***************************************************************************/

/*---- compilation control switches ----------------------------------------*/
#define CoffLib_cpp

/*****************************************************************************
* INCLUDE FILES
*****************************************************************************/
/*---- system and platform files -------------------------------------------*/

#include "windows.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

/*---- program files -------------------------------------------------------*/
#include "coff.h"
#include "coffdefs.h"
#include "CoffLib.h"

/*****************************************************************************
* EXTERNAL REFERENCE    
*****************************************************************************/
/*---- data declarations ---------------------------------------------------*/

/*---- function prototypes -------------------------------------------------*/

/*****************************************************************************
* PUBLIC DECLARATIONS 
*****************************************************************************/

/*---- data declarations ---------------------------------------------------*/

/*****************************************************************************
* PRIVATE DECLARATIONS  
*****************************************************************************/
/*---- context -------------------------------------------------------------*/

/*---- data declarations ---------------------------------------------------*/

/*---- function prototypes -------------------------------------------------*/


/*---- macros --------------------------------------------------------------*/

#define TRUE  1
#define FALSE 0

/*--------------------------------------------------------------------------*/
/* CONSTANTS, MACROS, VARIABLES, AND STRUCTURES FOR THE LOADER.             */
/*--------------------------------------------------------------------------*/
/* NOTE THAT I BELOW IS THE ACTUAL SECTION NUMBER    */
/* (1 THRU pCoffHndl->FileInfo->file_hdr.F_NSCNS)   */
#define SECT_HDR_PTR(i) ( (TI_SCNHDR *)pCoffHndl->FileInfo->sect_hdrs + ((i)-1))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))

/*****************************************************************************
* PUBLIC FUNCTION DEFINITIONS
*****************************************************************************/

/*F***************************************************************************
* NAME:   COFF_RET
*         COFFR_FileOpen( char       * pFilename, 
*                         COFF_OPTS  * pOptions, 
*                         COFF_HNDL  * pRetCoffHndl) 
*
* DESCRIPTION:  Open the COFF file, and read in all the various headers.
*
* INPUTS:
*   PARAMETERS:
*
*   GLOBALS:
*
* OUTPUTS:
*   PARAMETERS:
*       
*   GLOBALS:
*
*   RETURN:                     
*       
* NOTES:
*   String table and symbols are not read in.  The application must explicitly
*   request load of string and symbol tables.
*   
*F***************************************************************************/
COFF_RET
CoffLib::COFFR_FileOpen( const char * pFilename, 
                COFF_OPTS  * pOptions, 
                COFF_HNDL  * pRetCoffHndl)
{
    COFF_HNDL_INFO * pCoffHndl;
    int SectionHeaderSize,Error;
    short SectNum;

    /*- Set return to error value ------------------------------------------*/
    *pRetCoffHndl = (COFF_HNDL_INFO *)NULL;

    /*- Get space for handle; on error return, NULL the structure ----------*/
    pCoffHndl = (COFF_HNDL_INFO *)(malloc( sizeof(COFF_HNDL_INFO)));
    if ( pCoffHndl == (COFF_HNDL_INFO *)NULL )
        return (COFF_MALLOC_ERR);

    memset( pCoffHndl, 0, sizeof(COFF_HNDL_INFO));

    /*- Set Coff return handle ---------------------------------------------*/
    *pRetCoffHndl = pCoffHndl;

    /*- Open file; On error return space and return error ------------------*/
    pCoffHndl->FileHndl = fopen( pFilename, COFF_FILE_RD );
    if ( pCoffHndl->FileHndl == ( FILE *)NULL )
        return (COFF_FILE_GONE_ERR);

    /*- Set coff handle information  ---------------------------------------*/
    pCoffHndl->bss_downloaded = FALSE;

    /*- Initialize coff file information -----------------------------------*/
    pCoffHndl->options = (COFF_OPTS *)malloc( sizeof(COFF_OPTS) );
    if ( pCoffHndl->options == (COFF_OPTS *)NULL )
        return (COFF_MALLOC_ERR);
	memset( pCoffHndl->options,0,sizeof(COFF_OPTS));
    *pCoffHndl->options = *pOptions;

    /*- Initialze coff file information ------------------------------------*/
    pCoffHndl->FileInfo = (COFF_FILE_INFO *)malloc( sizeof( COFF_FILE_INFO ));
    if ( pCoffHndl->FileInfo == (COFF_FILE_INFO *)NULL )
        return (COFF_MALLOC_ERR);
	memset( pCoffHndl->FileInfo,0,sizeof(COFF_FILE_INFO));

    pCoffHndl->FileInfo->bss_sect     = -1;    /* Set to invalid section    */
    pCoffHndl->FileInfo->cinit_sect   = -1;    /* Set to invalid section    */
    pCoffHndl->FileInfo->coff_version = 0;     /* Set to invalid version    */
    pCoffHndl->FileInfo->device_id    = 0;     /* Set to invalid device id  */
    pCoffHndl->FileInfo->str_size     = 0;     /* Set to empty str table    */
    pCoffHndl->FileInfo->reloc_amount = NULL;  /* Set to invalid ptr        */
    pCoffHndl->FileInfo->sect_hdrs    = NULL;  /* Set to invalid ptr        */
    pCoffHndl->FileInfo->str_head     = NULL;  /* Set to invalid ptr        */    
    pCoffHndl->FileInfo->big_e_target = FALSE; /* Set to default            */
    pCoffHndl->FileInfo->byte_swapped = FALSE; /* Set to default            */
    pCoffHndl->FileInfo->tags_merged  = FALSE; /* Set to default            */

    /*- Read in file header ------------------------------------------------*/
    Error = COFFR_GetFileHdr(pCoffHndl, &pCoffHndl->FileInfo->file_hdr);
    if( Error != 0 )
        return (Error);

    /*- Read in file header ------------------------------------------------*/
    Error = COFFR_GetOptionalHdr(pCoffHndl,&pCoffHndl->FileInfo->o_filehdr);
    if( Error != 0 )
        return (Error);

    /*- Free existing section headers and malloc new space -----------------*/

    if (pCoffHndl->FileInfo->sect_hdrs) 
        free( pCoffHndl->FileInfo->sect_hdrs );

    SectionHeaderSize = pCoffHndl->FileInfo->file_hdr.f_nscns * TI_SCNHSZ;

    pCoffHndl->FileInfo->sect_hdrs = ( TI_SCNHDR *)malloc( SectionHeaderSize );

    if ( pCoffHndl->FileInfo->sect_hdrs == ( TI_SCNHDR *)NULL )
        return (COFF_MALLOC_ERR);
 
	memset( pCoffHndl->FileInfo->sect_hdrs, 0, SectionHeaderSize );

    /*- Get each section header --------------------------------------------*/
    pCoffHndl->FileInfo->largest_sect = 0;
    pCoffHndl->FileInfo->largest_sect_size = 0;

    for (  SectNum = 1; 
           SectNum <= (short)pCoffHndl->FileInfo->file_hdr.f_nscns;
           SectNum++ )
    {
        TI_SCNHDR * pSectHdr;

        pSectHdr = (TI_SCNHDR *)pCoffHndl->FileInfo->sect_hdrs + (SectNum-1);
        Error = COFFR_GetSectionHdr( pCoffHndl, SectNum, pSectHdr );
        if( Error != 0 )
            return (Error);

        /*- If section is loadable then find biggest section ---------------*/
        if (     ( pSectHdr->s_scnptr != 0 )
             &&  ( pSectHdr->s_size !=0 )
             && !( pSectHdr->s_flags & TI_STYP_DSECT )
             && !( pSectHdr->s_flags & TI_STYP_COPY )
             && !( pSectHdr->s_flags & TI_STYP_NOLOAD ))
        {
            long BytesInSection = LOCTOBYTE(pCoffHndl, pSectHdr->s_size );

            if( BytesInSection >  pCoffHndl->FileInfo->largest_sect_size)
            {
                pCoffHndl->FileInfo->largest_sect_size = BytesInSection;
                pCoffHndl->FileInfo->largest_sect      = SectNum;
            }
        }
    }

    /*- Get string table size, includes 4 bytes of string table count ------*/


    if( fseek( pCoffHndl->FileHndl, 
               ( pCoffHndl->FileInfo->file_hdr.f_symptr
                    +(pCoffHndl->FileInfo->file_hdr.f_nsyms*TI_SYMESZ)),
                 SEEK_SET))
    {
        return (COFF_FILE_ACCESS_ERR);
    }

    pCoffHndl->FileInfo->str_size = 0;  /* if no string table init to 0     */
    fread( &(pCoffHndl->FileInfo->str_size), sizeof(long),SIZE_ONE,
                    pCoffHndl->FileHndl);

    if ( pCoffHndl->FileInfo->byte_swapped)
        swap4byte(&pCoffHndl->FileInfo->str_size);

    return ( 0 );
}
/*F***************************************************************************
* NAME:  COFF_RET
*        COFFR_FileClose( COFF_HNDL pCoffHndl )    
*
* DESCRIPTION:  Close the coff file and release allocated memory.
*
* INPUTS:
*   PARAMETERS:
*
*   GLOBALS:
*
* OUTPUTS:
*   PARAMETERS:
*       
*   GLOBALS:
*
*   RETURN:                     
*       
* NOTES:
*   
*F***************************************************************************/

COFF_RET
CoffLib::COFFR_FileClose( COFF_HNDL pCoffHndl )
{

    if ( pCoffHndl == NULL )
        return( 0 );
   
    /*- Free coff file info ------------------------------------------------*/
    if( pCoffHndl->FileInfo != (COFF_FILE_INFO *)NULL )
    {

        /*- Free strings ---------------------------------------------------*/
        if (pCoffHndl->FileInfo->str_head != ( STRTAB *)NULL)
            FreeStrings( pCoffHndl );
        pCoffHndl->FileInfo->str_head = ( STRTAB *)NULL;

        /*- Free section headers -------------------------------------------*/
        if ( pCoffHndl->FileInfo->sect_hdrs != (TI_SCNHDR *)NULL ) 
            free( pCoffHndl->FileInfo->sect_hdrs );
        pCoffHndl->FileInfo->sect_hdrs = ( TI_SCNHDR *)NULL;

        free( pCoffHndl->FileInfo );
        pCoffHndl->FileInfo = (COFF_FILE_INFO *)NULL;
    }

    /*- Free coff options  -------------------------------------------------*/
    if ( pCoffHndl->options != (COFF_OPTS *)NULL )
        free( pCoffHndl->options );
    pCoffHndl->options = (COFF_OPTS *)NULL;

    /*-- Close the file ----------------------------------------------------*/
    if ( pCoffHndl->FileHndl != ( FILE *)NULL )
        fclose ( pCoffHndl->FileHndl );
    pCoffHndl->FileHndl = NULL;

    /*- Free coff handle ---------------------------------------------------*/
    free( pCoffHndl );
    return ( 0 );
}
/*F***************************************************************************
* NAME: COFF_RET
*       COFFR_GetFileHdr( COFF_HNDL pCoffHndl, FILHDR * pFileHdr )   
*
* DESCRIPTION: Read in the file header information.
*
* INPUTS:
*   PARAMETERS:
*
*   GLOBALS:
*
* OUTPUTS:
*   PARAMETERS:
*       
*   GLOBALS:
*
*   RETURN:                     
*       
* NOTES:
*   
*F***************************************************************************/

COFF_RET
CoffLib::COFFR_GetFileHdr( COFF_HNDL pCoffHndl, TI_FILHDR * pFileHdr )
{  

    int filshz = TI_FILHSZ;
    
    if (  (fseek(pCoffHndl->FileHndl, 0L, SEEK_SET))
       || (fread(pFileHdr, TI_FILHSZ, SIZE_ONE, pCoffHndl->FileHndl)!=SIZE_ONE))
    {
         return (COFF_FILE_ACCESS_ERR);
    }

    /*----------------------------------------------------------------------*/
    /* MAKE SURE THIS IS REALLY A COFF FILE. CHECK FOR SWAPPED FILES.       */
    /* DETERMINE BYTE ORDERING OF OBJECT DATA.                              */
    /*----------------------------------------------------------------------*/
    if (!TI_ISCOFF2(pFileHdr->f_magic))
    {
        swap2byte(&pFileHdr->f_magic);
        if (!TI_ISCOFF2(pFileHdr->f_magic))
        {
            return (COFF_BAD_MAGIC_ERR);
        }

        pCoffHndl->FileInfo->byte_swapped = TRUE;

        swap2byte(&pFileHdr->f_nscns);
        swap4byte(&pFileHdr->f_timdat);
        swap4byte(&pFileHdr->f_symptr);
        swap4byte(&pFileHdr->f_nsyms);
        swap2byte(&pFileHdr->f_opthdr);
        swap2byte(&pFileHdr->f_flags);
        swap2byte(&pFileHdr->f_target_id); 
    }

    pCoffHndl->FileInfo->big_e_target = ((pFileHdr->f_flags & TI_F_BIG) != 0);
    pCoffHndl->FileInfo->tags_merged  = ((pFileHdr->f_flags & TI_F_SYMMERGE) != 0);

    /*----------------------------------------------------------------------*/
    /* DETERMINE VERSION OF COFF BEING USED, CHECK TARGET ID IF NEEDED.     */
    /*----------------------------------------------------------------------*/
    if ( TI_ISCOFF_1(pFileHdr->f_magic) || TI_ISCOFF_2(pFileHdr->f_magic) )
    {
        if (!TI_ISMAGIC_ANY(pFileHdr->f_target_id))
        {
            return (COFF_BAD_MAGIC_ERR);
        }

        pCoffHndl->FileInfo->coff_version = pFileHdr->f_magic;
        pCoffHndl->FileInfo->device_id    = pFileHdr->f_target_id;
    } 
    else
    {
        pCoffHndl->FileInfo->coff_version = TI_COFF_MAGIC_0;
        pCoffHndl->FileInfo->device_id    = pFileHdr->f_magic;
    }
    return ( 0 );
}
/*F***************************************************************************
* NAME:  COFF_RET
*        COFFR_GetOptionalHdr( COFF_HNDL pCoffHndl, AOUTHDR * pOptHdr )  
*
* DESCRIPTION: Read in the optional section information.
*
* INPUTS:
*   PARAMETERS:
*
*   GLOBALS:
*
* OUTPUTS:
*   PARAMETERS:
*       
*   GLOBALS:
*
*   RETURN:                     
*       
* NOTES:
*   
*F***************************************************************************/
COFF_RET
CoffLib::COFFR_GetOptionalHdr( COFF_HNDL pCoffHndl, TI_AOUTHDR * pOptHdr )
{

    size_t ReadSize = TI_AOUTSZ;

    /*----------------------------------------------------------------------*/
    /* READ IN OPTIONAL HEADER, IF THERE IS ONE, AND SWAP IF NEEDED.        */
    /*----------------------------------------------------------------------*/
    if (pCoffHndl->FileInfo->file_hdr.f_opthdr == TI_AOUTSZ)
    {
        if (  (fseek( pCoffHndl->FileHndl,
                      TI_FILHSZ_IN(pCoffHndl->FileInfo->coff_version),

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -