📄 symlib.c
字号:
return (OK); }/********************************************************************************* symFindByNameAndType - look up a symbol by name and type** This routine searches a symbol table for a symbol matching both name and* type (<name> and <sType>). If the symbol is found, its value and type are* copied to <pValue> and <pType>. The <mask> parameter can be used to match* sub-classes of type.** To search the global VxWorks symbol table, specify \f3sysSymTbl\f1* as <symTblId>.* * RETURNS: OK, or ERROR if the symbol table ID is invalid* or the symbol is not found.*/STATUS symFindByNameAndType ( SYMTAB_ID symTblId, /* ID of symbol table to look in */ char *name, /* symbol name to look for */ char **pValue, /* where to put symbol value */ SYM_TYPE *pType, /* where to put symbol type */ SYM_TYPE sType, /* symbol type to look for */ SYM_TYPE mask /* bits in <sType> to pay attention to */ ) { SYMBOL *pSymbol; SYMBOL keySymbol; if (symTblId == NULL) return (ERROR); if (OBJ_VERIFY (symTblId, symTblClassId) != OK) return (ERROR); /* invalid symbol table ID */ /* fill in keySymbol */ keySymbol.name = name; /* match this name */ keySymbol.type = sType; /* match this type */ semTake (&symTblId->symMutex, WAIT_FOREVER); if ((pSymbol = (SYMBOL *) hashTblFind (symTblId->nameHashId, &keySymbol.nameHNode,(int) mask)) != NULL) { *pValue = pSymbol->value; /* fill in value */ *pType = pSymbol->type; /* fill in type */ } else { semGive (&symTblId->symMutex); /* release exclusion to table */ errnoSet (S_symLib_SYMBOL_NOT_FOUND); /* couldn't find symbol */ return (ERROR); } semGive (&symTblId->symMutex); /* release exclusion to table */ return (OK); }/********************************************************************************* symFindByValue - look up a symbol by value** This routine searches a symbol table for a symbol matching a specified* value. If there is no matching entry, it chooses the table entry with the* next lower value. The symbol name (with terminating EOS), the actual* value, and the type are copied to <name>, <pValue>, and <pType>.** For the <name> buffer, allocate MAX_SYS_SYM_LEN + 1 bytes. The value* MAX_SYS_SYM_LEN is defined in sysSymTbl.h.** To search the global VxWorks symbol table, specify \f3sysSymTbl\f1* as <symTblId>.* * RETURNS: OK, or ERROR if <value> is less than the lowest value in the table.*/STATUS symFindByValue ( SYMTAB_ID symTblId, /* ID of symbol table to look in */ UINT value, /* value of symbol to find */ char * name, /* where to put symbol name string */ int * pValue, /* where to put symbol value */ SYM_TYPE * pType /* where to put symbol type */ ) { return (symFindByValueAndType (symTblId, value, name, pValue, pType, SYM_MASK_NONE, SYM_MASK_NONE)); }/********************************************************************************* symFindByValueAndType - look up a symbol by value and type** This routine searches a symbol table for a symbol matching both value and* type (<value> and <sType>). If there is no matching entry, it chooses the* table entry with the next lower value. The symbol name (with terminating* EOS), the actual value, and the type are copied to <name>, <pValue>, and* <pType>. The <mask> parameter can be used to match sub-classes of type.** For the <name> buffer, allocate MAX_SYS_SYM_LEN + 1 bytes. The value* MAX_SYS_SYM_LEN is defined in sysSymTbl.h.** To search the global VxWorks symbol table, specify \f3sysSymTbl\f1* as <symTblId>.* * INTERNAL* This routine contains a weird hack designed to deal with the additional* symbols the loader puts in the symbol table. The loader adds three symbols* to the symbol table: <filename>_text, <filename>_data, <filename>_bss.* These symbols may have the same address (i.e. value in the symbol table)* as real routine or variable names. When looking up a symbol for display* it is desirable to find the real routine or variable names in preference* to these made up names. For example, loading "demo.o" will cause a* symbol "demo.o_text" to be added to the symbol table with the same value* as the real symbol "_demo". In a disassembly or "i" printout, etc, we* would rather see "_demo". So the test inside the loop in this routine* that normally terminates the search if we find an exact match, has been* changed to keep searching if the match it finds ends in "_text", "_data",* or "_bss". ** If no other exact match is found, the special symbols will be returned* anyway. Thus this routine simply has a "bias" against such symbols, but* will not return an erroneous result in any event. This nonsense should be* removed when the loader is changed to not add the special symbols.** The GNU toolkit adds symbols of the form "gcc2_compiled." and "xxx.o".* These symbols are also biased against. ** RETURNS: OK, or ERROR if <value> is less than the lowest value in the table.*/STATUS symFindByValueAndType ( SYMTAB_ID symTblId, /* ID of symbol table to look in */ FAST UINT value, /* value of symbol to find */ char * name, /* where to put symbol name string */ int * pValue, /* where to put symbol value */ SYM_TYPE * pType, /* where to put symbol type */ FAST SYM_TYPE sType, /* symbol type to look for */ FAST SYM_TYPE mask /* bits in <sType> to pay attention to */ ) { int index; FAST SYMBOL * pSymbol; FAST SYMBOL * pBestSymbol = NULL; FAST char * pUnder; UINT bestValue = 0; sType &= mask; if (symTblId == NULL) return (ERROR); if (OBJ_VERIFY (symTblId, symTblClassId) != OK) return (ERROR); /* invalid symbol table ID */ semTake (&symTblId->symMutex, WAIT_FOREVER); for (index = 0; index < symTblId->nameHashId->elements; index++) { pSymbol = (SYMBOL *) SLL_FIRST(&symTblId->nameHashId->pHashTbl [index]); while (pSymbol != NULL) /* list empty */ { if (((pSymbol->type & mask) == sType) && ((int)pSymbol->value == value) && (((pUnder = rindex (pSymbol->name, '_')) == NULL) || ((strcmp (pUnder, "_text") != 0) && (strcmp (pUnder, "_data") != 0) && (strcmp (pUnder, "_bss") != 0) && (strcmp (pUnder, "_compiled.") != 0))) && (((pUnder = rindex (pSymbol->name, '.')) == NULL) || ((strcmp (pUnder, ".o") != 0)))) { /* We've found the entry. Return it. */ strcpy (name, pSymbol->name); /* copy the name out */ *pValue = (int) pSymbol->value; /* fill in the value */ *pType = pSymbol->type; /* fill in type */ semGive (&symTblId->symMutex); return (OK); } else if (((pSymbol->type & mask) == sType) && (((int)pSymbol->value <= value) && ((int)pSymbol->value > bestValue))) { /* this symbol is of correct type and closer than last one */ bestValue = (int) pSymbol->value; pBestSymbol = pSymbol; } pSymbol = (SYMBOL *) SLL_NEXT (&pSymbol->nameHNode); } } if (bestValue == 0 || pBestSymbol == NULL) /* any closer symbol? */ { semGive (&symTblId->symMutex); /* release exclusion to table */ errnoSet (S_symLib_SYMBOL_NOT_FOUND); return (ERROR); } strcpy (name, pBestSymbol->name); /* copy the name out */ *pValue = (int) pBestSymbol->value; /* fill in the value */ *pType = pBestSymbol->type; /* fill in type */ semGive (&symTblId->symMutex); /* release exclusion to table */ return (OK); }/********************************************************************************* symEach - call a routine to examine each entry in a symbol table** This routine calls a user-supplied routine to examine each entry in the* symbol table; it calls the specified routine once for each entry. The* routine should be declared as follows:* .CS* BOOL routine* (* char *name, /@ entry name @/* int val, /@ value associated with entry @/* SYM_TYPE type, /@ entry type @/* int arg, /@ arbitrary user-supplied arg @/* UINT16 group /@ group number @/* )* .CE* The user-supplied routine should return TRUE if symEach() is to continue* calling it for each entry, or FALSE if it is done and symEach() can exit.** RETURNS: A pointer to the last symbol reached,* or NULL if all symbols are reached.** INTERNAL* In addition to the parameters given, it also passes a pointer to a symbol* as the last arguement.**/SYMBOL *symEach ( SYMTAB_ID symTblId, /* pointer to symbol table */ FUNCPTR routine, /* func to call for each tbl entry */ int routineArg /* arbitrary user-supplied arg */ ) { SYMBOL *pSymbol; RTN_DESC rtnDesc; if (OBJ_VERIFY (symTblId, symTblClassId) != OK) return (NULL); /* invalid symbol table ID */ /* fill in a routine descriptor with the routine and argument to call */ rtnDesc.routine = routine; rtnDesc.routineArg = routineArg; semTake (&symTblId->symMutex, WAIT_FOREVER); pSymbol = (SYMBOL *) hashTblEach (symTblId->nameHashId, symEachRtn, (int) &rtnDesc); semGive (&symTblId->symMutex); /* release exclusion to table */ return (pSymbol); /* symbol we stopped on */ }/********************************************************************************* symEachRtn - call a user routine for a hashed symbol** This routine supports hashTblEach(), by unpackaging the routine descriptor* and calling the user routine specified to symEach() with the right calling* sequence.** RETURNS: Boolean result of user specified symEach routine.** NOMANUAL*/LOCAL BOOL symEachRtn ( SYMBOL *pSymbol, /* ptr to symbol */ RTN_DESC *pRtnDesc /* ptr to a routine descriptor */ ) { return ((* pRtnDesc->routine) (pSymbol->name, (int) pSymbol->value, pSymbol->type, pRtnDesc->routineArg, pSymbol->group, pSymbol)); }/********************************************************************************* symHFuncName - symbol name hash function** This routine checksums the name and applies a multiplicative hashing function* provided by hashFuncMultiply().** RETURNS: An integer between 0 and (elements - 1).*/LOCAL int symHFuncName ( int elements, /* no. of elements in hash table */ SYMBOL *pSymbol, /* pointer to symbol */ int seed /* seed to be used as scalar */ ) { FAST int hash; FAST char *tkey; FAST int key = 0; /* checksum the string and use a multiplicative hashing function */ for (tkey = pSymbol->name; *tkey != '\0'; tkey++) key = key + (unsigned int) *tkey; hash = key * seed; /* multiplicative hash func */ hash = hash >> (33 - ffsMsb (elements)); /* take only the leading bits */ return (hash & (elements - 1)); /* mask hash to (0,elements-1)*/ }/********************************************************************************* symKeyCmpName - compare two symbol's names for equivalence** This routine returns TRUE if the match symbol's type masked by the specified* symbol mask, is equivalent to the second symbol's type also masked by the* symbol mask, and if the symbol's names agree.** RETURNS: TRUE if symbols match, FALSE if they differ.*/LOCAL BOOL symKeyCmpName ( SYMBOL *pMatchSymbol, /* pointer to match criteria symbol */ SYMBOL *pSymbol, /* pointer to symbol */ int maskArg /* symbol type bits than matter (int) */ ) { SYM_TYPE mask; /* symbol type bits than matter (char)*/ /* * If maskArg is equal to SYM_MASK_EXACT, then check to see if the pointers * match exactly. */ if (maskArg == SYM_MASK_EXACT) return (pMatchSymbol == pSymbol ? TRUE : FALSE); mask = (SYM_TYPE) maskArg; return (((pSymbol->type & mask) == (pMatchSymbol->type & mask)) && (strcmp (pMatchSymbol->name, pSymbol->name) == 0)); }/********************************************************************************* symName - get the name associated with a symbol value** This routine returns a pointer to the name of the symbol of specified value.** RETURNS: A pointer to the symbol name, or* NULL if symbol cannot be found or <symTbl> doesn't exist.** NOMANUAL*/char *symName ( SYMTAB_ID symTbl, /* symbol table */ char *value /* symbol value */ ) { SYMBOL sym; sym.value = value; /* initialize symbol */ sym.name = NULL; (void)symEach (symTbl, (FUNCPTR) symNameValueCmp, (int) (&sym)); return (sym.name); }/********************************************************************************* symNameValueCmp - compares a symbol value with a name** This routine was written to be invoked by symEach().* <pSym> is a pointer to a symbol which contains a value that this routine* compares to the passed symbol value <val>. If the two values match, then the* name field in <pSym>'s name field is set to point to the symbol name in the* symbol table.** RETURNS: FALSE if <val> matches <pSym>->value and sets <pSym>->name,* or TRUE otherwise.*/LOCAL BOOL symNameValueCmp ( char *name, /* symbol name */ int val, /* symbol value */ SYM_TYPE type, /* symbol type -- not used */ int pSym /* pointer to symbol trying to be matched */ ) { if (val == (int) (((SYMBOL *)pSym)->value)) { ((SYMBOL *)pSym)->name = name; return (FALSE); } return (TRUE); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -