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

📄 loadpecofflib.c

📁 vxworks源码源码解读是学习vxworks的最佳途径
💻 C
📖 第 1 页 / 共 4 页
字号:
                vxSymType = VX_BSS;                }            else if (scnFlags & STYP_DRECTVE)                {                continue;                }            else                {                printErr (cantConvSymbolType, name, errnoGet());                continue;                }            if (symsAbsolute)	        {		bias = pScnHdr->s_vaddr + imageBase;		}            /*  Determine if symbol should be put into symbol table.             */            if (((symFlag & LOAD_LOCAL_SYMBOLS) && !PECOFF_EXT(symbol))                || ((symFlag & LOAD_GLOBAL_SYMBOLS) && PECOFF_EXT(symbol)))                {                if (PECOFF_EXT(symbol))                    vxSymType |= VX_EXT;                /*  Add symbol to symbol table.                 */                /* cut out Local tags */                if ( name[0] != '$')    		    if (symSAdd (symTbl, name, symbol->U_SYM_VALUE + bias,			     vxSymType, group) != OK)		        printErr (cantAddSymErrMsg, name, errnoGet());                }            /* Add symbol address to externals table.             * For PECOFF, we add all symbol addresses into the externals             * table, not only those symbols saved in the vxWorks symbol             * table.             */            (*externals) [symNum] = symbol->U_SYM_VALUE + bias;	    }	else	    {	    /* Undefined external symbol or "common" symbol             *	     *   A common symbol type is denoted by undefined external             *   with its value set to non-zero.	     */	    /* if symbol is a common, then allocate space and add to	     * symbol table as BSS	     */	    /* common symbol - create new symbol */            if (PECOFF_COMM(symbol))		{		if (symFlag == LOAD_NO_SYMBOLS)		    ;		else		    {		    /* 		     *  common symbol - create new symbol 		     *		     *  Common symbols are now tacked to the end of the bss		     *  section.  This is done to accomodate systems that have		     *  multiple boards sharing memory with common symbols		     *  over the system bus.  		     *  This portion of code reads the symbol value		     *  (which contains the symbol size) and places the symbol		     *  in the bss section.  The function dataAlign uses		     *  the next possible address for a common symbol to determine		     *  the proper alignment.		     */		    		    adrs = pNextCommon;		    bssAlignment = dataAlign (symbol->U_SYM_VALUE, (ULONG) adrs);		    adrs += bssAlignment;		    pNextCommon += (symbol->U_SYM_VALUE + bssAlignment);		    		    if (symSAdd (symTbl, name, adrs, (VX_BSS | VX_EXT), group) != OK)		    printErr (cantAddSymErrMsg, name, errnoGet());		    		    }		}	    /* look up undefined external symbol in symbol table */	    else if (symFindByNameAndType (symTbl, name, &adrs, &vxSymType,					   VX_EXT, VX_EXT) != OK)                {                /* symbol not found in symbol table */                printErr ("undefined symbol: %s\n", name);                adrs = NULL;                status = ERROR;                }	    /* add symbol address to externals table             */	    (*externals) [symNum] = adrs;	    }	}    return (status);    }/********************************************************************************* pecoffRelSegmentI386 - perform relocation for the I386 family** This routine reads the specified relocation command segment and performs* all the relocation specified therein.* Absuolute symbol addresses are looked up in the 'externals' table.** This function handles the following types of relocation commands for the* I386 family of processor:* 	IMAGE_REL_I386_DIR32  - direct 32 bit relocation* 	IMAGE_REL_I386_REL32  - relative 32b bit relocation** RETURNS: OK or ERROR*/LOCAL STATUS pecoffRelSegmentI386    (    RELOC *	pRelCmds,	/* list of relocation commands */    SCNHDR *	pScnHdrArray, 	/* array of section headers */    char **	pExternals,	/* array of absolute symbols values */    SYMENT *	pExtSyms,	/* pointer to object file symbols */     SEG_INFO * 	pSeg,		/* section addresses and sizes */    int         section,        /* section number -1 for relocation */    SYMTAB_ID   symTbl          /* symbol table to use */    )    {    long *      pAdrs;          /* relocation address */    int         nCmds;          /* # reloc commands for seg */    RELOC       relocCmd;       /* relocation structure */    SCNHDR *    pScnHdr;        /* section header for relocation */    STATUS      status = OK;    pScnHdr = pScnHdrArray + section;    for (nCmds = pScnHdr->s_nreloc; nCmds > 0; nCmds--)        {        /* read relocation command */        relocCmd = *pRelCmds++;        /*         * Calculate actual address in memory that needs relocation.         */#ifdef GNU_LD_ADDS_VMA        pAdrs = (long *) ((long) pSeg->addrText +                                 (relocCmd.r_vaddr - pScnHdr->s_vaddr));#else        pAdrs = (long *) ((long) pSeg->addrText + relocCmd.r_vaddr);#endif        switch (relocCmd.r_type)            {            /* Do Relocations */	    case IMAGE_REL_I386_DIR32:		/*		 * This relocation is preformed by adding the absolute address		 * of the symbol to the relocation value in the code.		 */		*pAdrs += (INT32) pExternals [relocCmd.r_symndx];  		break;	    case IMAGE_REL_I386_REL32:		/*		 * Call near, displacement relative to the next instruction.		 * First, find the next instruction addr, then subtract it from		 * the addr of the found symbol to obtain the relocation addr.		 */	                *pAdrs = pExternals[relocCmd.r_symndx] - ((UINT32)pAdrs + 4);                break;	    default:		printf("Unknown Relocation Error\n");		status = ERROR;		break;            }        }    return(status);    }/********************************************************************************* pecoffHdrRead - read in PECOFF header and swap if necessary* * Note:  To maintain code portability, we can not just read sizeof(FILHDR) *	bytes.  Compilers pad structures differently,*	resulting in different structure sizes.*	So, we read the structure in an element at a time, using the size*	of each element.*/LOCAL STATUS pecoffHdrRead    (    int fd,    FILHDR *pHdr,    BOOL *pSwap    )    {    int status;    int signature;    short int i;    char tmp;    struct dosheader dhead;        *pSwap = 0;    if (fioRead (fd, (char *) &(pHdr->f_magic), sizeof (pHdr->f_magic) )		!= sizeof (pHdr->f_magic))	{/*	errnoSet ((int) S_loadLib_FILE_READ_ERROR); */winOut("Header Error #e\n");	return (ERROR);	}    switch (pHdr->f_magic)	{	case (SWAB_SHORT(IMAGE_DOS_SIGNATURE)):            *pSwap = TRUE;	case (IMAGE_DOS_SIGNATURE):	    /* Remove DOS EXE header */            fioRead(fd,(char *)&dhead + 2,sizeof(dhead)-2);	    lseek (fd, dhead.e_lfanew, SEEK_SET);            /* Check for NT SIGNATURE */            fioRead (fd, &i, 4);            if ( i != IMAGE_NT_SIGNATURE )                {                printf("Bad Image Signature %x %x\n",i,IMAGE_NT_SIGNATURE);                return(ERROR);                }            fioRead (fd, &pHdr->f_magic, 2);	    break;	case (SWAB_SHORT(IMAGE_FILE_MACHINE_I386)):            *pSwap = TRUE;	    break;        case (IMAGE_FILE_MACHINE_I386):	    *pSwap = FALSE;	    break;	    	default:#ifndef MULTIPLE_LOADERS	    printErr (fileTypeUnsupported, pHdr->f_magic);#endif/*	    errnoSet (S_loadLib_FILE_ENDIAN_ERROR); */winOut("Header Error #8\n");	    return (ERROR);	    break;	}    status = fileRead (fd, &pHdr->f_nscns, sizeof(pHdr->f_nscns), *pSwap);    status |= fileRead (fd, &pHdr->f_timdat, sizeof(pHdr->f_timdat), *pSwap);    status |= fileRead (fd, &pHdr->f_symptr, sizeof(pHdr->f_symptr), *pSwap);    status |= fileRead (fd, &pHdr->f_nsyms, sizeof(pHdr->f_nsyms), *pSwap);    status |= fileRead (fd, &pHdr->f_opthdr, sizeof(pHdr->f_opthdr), *pSwap);    status |= fileRead (fd, &pHdr->f_flags, sizeof(pHdr->f_flags), *pSwap);    max_scns = pHdr->f_nscns;    return (status);    }/********************************************************************************* pecoffOpthdrRead - read in PECOFF optional header and swap if necessary* */LOCAL STATUS pecoffOpthdrRead    (    int     	fd,    PEOPTION 	*pOptHdr,    BOOL    	swapTables    )    {    int status;    status = fileRead(fd, &pOptHdr->magic, sizeof(pOptHdr->magic),                            swapTables);    status |= fileRead(fd, &pOptHdr->vstamp, sizeof(pOptHdr->vstamp),                            swapTables);    status |= fileRead(fd, &pOptHdr->tsize, sizeof(pOptHdr->tsize),                            swapTables);    status |= fileRead(fd, &pOptHdr->dsize, sizeof(pOptHdr->dsize),                            swapTables);    status |= fileRead(fd, &pOptHdr->bsize, sizeof(pOptHdr->bsize),                            swapTables);    status |= fileRead(fd, &pOptHdr->entry, sizeof(pOptHdr->entry),                            swapTables);    status |= fileRead(fd, &pOptHdr->text_start, sizeof(pOptHdr->text_start),                            swapTables);    status |= fileRead(fd, &pOptHdr->data_start, sizeof(pOptHdr->data_start),                            swapTables);    status |= fileRead(fd, &pOptHdr->image_base, sizeof(pOptHdr->image_base),                            swapTables);    status |= fileRead(fd, &pOptHdr->section_align, sizeof(pOptHdr->section_align),                            swapTables);    status |= fileRead(fd, &pOptHdr->file_align, sizeof(pOptHdr->file_align),                            swapTables);    return (status);    }/********************************************************************************* pecoffSecHdrRead - read in PECOFF section header and swap if necessary* */LOCAL STATUS pecoffSecHdrRead    (    int    fd,    SCNHDR *pScnHdr,    FILHDR *pHdr,    BOOL   swapTables    )    {    int ix;    int status = 0;    /* check for correct section count */    if (pHdr->f_nscns > MAX_SCNS)	{	return (ERROR);	}    for (ix = 0; ix < pHdr->f_nscns; ix++)	{        status = fileRead(fd, pScnHdr->s_name, sizeof(pScnHdr->s_name),                          FALSE);        status |= fileRead(fd, &pScnHdr->s_paddr, sizeof(pScnHdr->s_paddr),                           swapTables);        status |= fileRead(fd, &pScnHdr->s_vaddr, sizeof(pScnHdr->s_vaddr),                            swapTables);        status |= fileRead(fd, &pScnHdr->s_size, sizeof(pScnHdr->s_size),                            swapTables);        status |= fileRead(fd, &pScnHdr->s_scnptr, sizeof(pScnHdr->s_scnptr),                            swapTables);        status |= fileRead(fd, &pScnHdr->s_relptr, sizeof(pScnHdr->s_relptr),                            swapTables);        status |= fileRead(fd, &pScnHdr->s_lnnoptr, sizeof(pScnHdr->s_lnnoptr),                            swapTables);        status |= fileRead(fd, &pScnHdr->s_nreloc, sizeof(pScnHdr->s_nreloc),                            swapTables);        status |= fileRead(fd, &pScnHdr->s_nlnno, sizeof(pScnHdr->s_nlnno),                            swapTables);        status |= fileRead(fd, &pScnHdr->s_flags, sizeof(pScnHdr->s_flags),                            swapTables);	pScnHdr++;	}    return (status);    }/********************************************************************************* pecoffReadRelocInfo - read in PECOFF relocation information and swap if necessary* * Assumptions:  The file pointer is positioned at the start of the relocation*		records.**		The relocation records are ordered by section.*/LOCAL STATUS pecoffReadRelocInfo    (    int     fd,    SCNHDR  *pScnHdr,    RELOC   **pRelsPtr,    BOOL    swapTables    )    {    int ix;    int iy;    int relocSize;    int status = OK;    RELOC *pRels;                       /* pointer to single reloc entry */    for (ix = 0; ix < max_scns; ix++)	{        if (pScnHdr->s_nreloc > 0)	    {	    relocSize = (int) pScnHdr->s_nreloc * RELSZ;	    if ((*pRelsPtr = (RELOC *)malloc (relocSize)) == NULL)		{		return (ERROR);		}	if (lseek (fd, pScnHdr->s_relptr, SEEK_SET) == ERROR)	    {	    return (ERROR);	    }            for (iy = 0, pRels = *pRelsPtr;                 (iy < (int) pScnHdr->s_nreloc) && (status == OK);                 iy++, pRels++)                {                status = fileRead(fd, &pRels->r_vaddr,                                   sizeof(pRels->r_vaddr), swapTables);                status |= fileRead(fd, &pRels->r_symndx,                                   sizeof(pRels->r_symndx), swapTables);                status |= fileRead(fd, &pRels->r_type,                                   sizeof(pRels->r_type), swapTables);                }            if (status != OK)                return(status);	    }	pScnHdr++;	pRelsPtr++;        }    return (OK);    }/********************************************************************************* pecoffReadSym - read one PECOFF symbol entry* */LOCAL STATUS pecoffReadSym    (    int     fd,    SYMENT *pSym,                       /* where to read symbol entry */    BOOL    swapTables    )    {    int status;    status = fioRead (fd, pSym->n_name, sizeof(pSym->n_name));    if(status != sizeof(pSym->n_name))        {        printf(" Status %x %x : %x %x %x %x\n",status,sizeof(pSym->n_name),            pSym->n_name[0],             pSym->n_name[1],             pSym->n_name[2],             pSym->n_name[3]);    status = fioRead (fd, pSym->n_name, sizeof(pSym->n_name));        printf(" Status  #2 %x : %x %x %x %x\n",status,            pSym->n_name[0],             pSym->n_name[1],             pSym->n_name[2],             pSym->n_name[3]);	status = ERROR;	        }    else        status = OK;    if (swapTables && (status == OK))        {

⌨️ 快捷键说明

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