📄 symlib.c
字号:
/********************************************************************************* symAlloc - allocate and initialize a symbol, including group number** Allocate and initialize a symbol. The symbol is not added to any symbol* table. To add a symbol to a symbol table use symAdd() or symTblAdd().* Space for the name is allocated along with the SYMBOL structure, thus the* name parameter need not be static.** RETURNS: Pointer to symbol, or NULL if out of memory.** NOMANUAL*/SYMBOL *symAlloc ( SYMTAB_ID symTblId, /* symbol table to allocate symbol for */ char *name, /* pointer to symbol name string */ char *value, /* symbol address */ SYM_TYPE type, /* symbol type */ UINT16 group /* symbol group */ ) { SYMBOL *pSymbol; char *symName; int length; if (OBJ_VERIFY (symTblId, symTblClassId) != OK) return (NULL); /* invalid symbol table ID */ if (name == NULL) return (NULL); /* null name */ length = strlen (name); /* figure out name length */ pSymbol = (SYMBOL *) memPartAlloc (symTblId->symPartId, (unsigned)(sizeof(SYMBOL) + length + 1)); if (pSymbol == NULL) /* out of memory */ return (NULL); /* copy name after symbol */ symName = (char *) ((unsigned) pSymbol + sizeof (SYMBOL)); symName[length] = EOS; /* null terminate string */ strncpy (symName, name, length); /* copy name into place */ symInit (pSymbol, symName, value, type, group); /* initialize symbol*/ return (pSymbol); /* return symbol ID */ }/********************************************************************************* symInit - initialize a symbol, including group number** Initialize a symbol. The symbol is not added to any symbol table. To add* a symbol to a symbol table use symAdd() or symTblAdd().** RETURNS: OK, or ERROR if symbol table could not be initialized.** NOMANUAL*/STATUS symInit ( SYMBOL *pSymbol, /* pointer to symbol */ char *name, /* pointer to symbol name string */ char *value, /* symbol address */ SYM_TYPE type, /* symbol type */ UINT16 group /* symbol group */ ) { /* fill in symbol */ pSymbol->name = name; /* symbol name */ pSymbol->value = value; /* symbol value */ pSymbol->type = type; /* symbol type */ pSymbol->group = group; /* symbol group */ return (OK); }/********************************************************************************* symFree - deallocate a symbol** Deallocate the specified symbol. This routine does not check if symbols* are still resident in symbol tables, so all calls to symFree should be* preceded by a call to symTblRemove.** RETURNS: OK, or ERROR if invalid symbol table.** NOMANUAL*/STATUS symFree ( SYMTAB_ID symTblId, /* symbol table semaphore */ SYMBOL *pSymbol /* pointer to symbol to delete */ ) { if (OBJ_VERIFY (symTblId, symTblClassId) != OK) return (ERROR); /* invalid symbol table ID */ return (memPartFree (symTblId->symPartId, (char *) pSymbol)); }/********************************************************************************* symSAdd - create and add a symbol to a symbol table, including a group number* * This routine behaves as symAdd() unless it does not check for synchronization* function pointers. Thus it can be used in loaders to prevent from trying to * independently synchronize each symbol of a module.** RETURNS: OK, or ERROR if the symbol table is invalid* or there is insufficient memory for the symbol to be allocated.** NOMANUAL*/STATUS symSAdd ( SYMTAB_ID symTblId, /* symbol table to add symbol to */ char *name, /* pointer to symbol name string */ char *value, /* symbol address */ SYM_TYPE type, /* symbol type */ UINT16 group /* symbol group */ ) { SYMBOL *pSymbol = symAlloc (symTblId, name, value, type, group); if (pSymbol == NULL) /* out of memory? */ return (ERROR); if (symTblAdd (symTblId, pSymbol) != OK) /* try to add symbol */ { symFree (symTblId, pSymbol); /* deallocate symbol if fail */ return (ERROR); } return (OK); }/********************************************************************************* symAdd - create and add a symbol to a symbol table, including a group number** This routine allocates a symbol <name> and adds it to a specified symbol* table <symTblId> with the specified parameters <value>, <type>, and <group>.* The <group> parameter specifies the group number assigned to a module when* it is loaded; see the manual entry for moduleLib.** RETURNS: OK, or ERROR if the symbol table is invalid* or there is insufficient memory for the symbol to be allocated.** SEE ALSO: moduleLib*/STATUS symAdd ( SYMTAB_ID symTblId, /* symbol table to add symbol to */ char *name, /* pointer to symbol name string */ char *value, /* symbol address */ SYM_TYPE type, /* symbol type */ UINT16 group /* symbol group */ ) { SYMBOL *pSymbol = symAlloc (symTblId, name, value, type, group); if (pSymbol == NULL) /* out of memory? */ return (ERROR); if (symTblAdd (symTblId, pSymbol) != OK) /* try to add symbol */ { symFree (symTblId, pSymbol); /* deallocate symbol if fail */ return (ERROR); } /* synchronize host symbol table if necessary */ if ((syncSymAddRtn != NULL) && (symTblId == sysSymTbl)) (* syncSymAddRtn) (name, value, type, group); return (OK); }/********************************************************************************* symTblAdd - add a symbol to a symbol table** This routine adds a symbol to a symbol table.** RETURNS: OK, or ERROR if invalid symbol table, or symbol couldn't be added.** INTERNAL* This is a lousy name, it should probably be symAddStatic ().** NOMANUAL*/STATUS symTblAdd ( SYMTAB_ID symTblId, /* symbol table to add symbol to */ SYMBOL *pSymbol /* pointer to symbol to add */ ) { if (OBJ_VERIFY (symTblId, symTblClassId) != OK) return (ERROR); /* invalid symbol table ID */ semTake (&symTblId->symMutex, WAIT_FOREVER); if ((!symTblId->sameNameOk) && (hashTblFind (symTblId->nameHashId, &pSymbol->nameHNode, SYM_MASK_ALL) != NULL)) { semGive (&symTblId->symMutex); /* release exclusion to table */ errno = S_symLib_NAME_CLASH; /* name clashed */ return (ERROR); } hashTblPut (symTblId->nameHashId, &pSymbol->nameHNode); symTblId->nsymbols ++; /* increment symbol count */ semGive (&symTblId->symMutex); /* release exclusion to table */ return (OK); }/********************************************************************************* symRemove - remove a symbol from a symbol table** This routine removes a symbol of matching name and type from a* specified symbol table. The symbol is deallocated if found.* Note that VxWorks symbols in a standalone VxWorks image (where the * symbol table is linked in) cannot be removed.** RETURNS: OK, or ERROR if the symbol is not found* or could not be deallocated.*/STATUS symRemove ( SYMTAB_ID symTblId, /* symbol tbl to remove symbol from */ char *name, /* name of symbol to remove */ SYM_TYPE type /* type of symbol to remove */ ) { SYMBOL *pSymbol; if (symFindSymbol (symTblId, name, type, SYM_MASK_ALL, &pSymbol) != OK) return (ERROR); if (symTblRemove (symTblId, pSymbol) != OK) return (ERROR); /* synchronize host symbol table if necessary */ if ((syncSymRemoveRtn != NULL) && (symTblId == sysSymTbl)) (* syncSymRemoveRtn) (name, type); return (symFree (symTblId, pSymbol)); }/********************************************************************************* symTblRemove - remove and terminate a symbol from a symbol table** This routine removes the specified symbol from the symbol table. The* symbol is not deallocated.** RETURNS: OK, or ERROR if symbol table invalid, or symbol not found.** NOMANUAL*/STATUS symTblRemove ( SYMTAB_ID symTblId, /* symbol table to remove symbol from */ SYMBOL *pSymbol /* pointer to symbol to remove */ ) { HASH_NODE *pNode; if (OBJ_VERIFY (symTblId, symTblClassId) != OK) return (ERROR); /* invalid symbol table ID */ semTake (&symTblId->symMutex, WAIT_FOREVER); pNode = hashTblFind (symTblId->nameHashId, &pSymbol->nameHNode, SYM_MASK_EXACT); if (pNode == NULL) { semGive (&symTblId->symMutex); /* release exclusion to table */ errnoSet (S_symLib_SYMBOL_NOT_FOUND); /* symbol wasn't found */ return (ERROR); } hashTblRemove (symTblId->nameHashId, pNode); symTblId->nsymbols--; /* one less symbol */ semGive (&symTblId->symMutex); /* release exclusion to table */ return (OK); }/********************************************************************************* symFindSymbol - find symbol in a symbol table of equivalent name and type** This routine finds a symbol of equivalent name and type in the specified* symbol table.** To search the global VxWorks symbol table, specify \f3sysSymTbl\f1* as <symTblId>.* * RETURNS: OK, or ERROR if symbol table invalid, or symbol not found.** NOMANUAL*/STATUS symFindSymbol ( SYMTAB_ID symTblId, /* symbol table ID */ char *name, /* name to search for */ SYM_TYPE type, /* symbol type */ SYM_TYPE mask, /* type bits that matter */ SYMBOL **ppSymbol /* where to return pointer to matching symbol */ ) { HASH_NODE *pNode; SYMBOL keySymbol; if (OBJ_VERIFY (symTblId, symTblClassId) != OK) return (ERROR); /* invalid symbol table ID */ /* fill in keySymbol */ keySymbol.name = name; /* match this name */ keySymbol.type = type; /* match this type */ semTake (&symTblId->symMutex, WAIT_FOREVER); pNode = hashTblFind (symTblId->nameHashId, &keySymbol.nameHNode, (int)mask); semGive (&symTblId->symMutex); /* release exclusion to table */ if (pNode == NULL) { errnoSet (S_symLib_SYMBOL_NOT_FOUND); /* couldn't find symbol */ return (ERROR); } *ppSymbol = (SYMBOL *) pNode; return (OK); }/********************************************************************************* symFindByName - look up a symbol by name** This routine searches a symbol table for a symbol matching a specified* name. If the symbol is found, its value and type are copied to <pValue>* and <pType>. If multiple symbols have the same name but differ in type,* the routine chooses the matching symbol most recently added to the symbol* table.** 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 cannot be found.*/STATUS symFindByName ( 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 */ ) { return (symFindByNameAndType (symTblId, name, pValue, pType, SYM_MASK_NONE, SYM_MASK_NONE)); }/********************************************************************************* symFindByCName - find a symbol in a symbol table, look for a '_'** Find a symbol in the table, by name. If the symbol isn't found,* try looking for it with a '_' prepended to it. If we find the* symbol, we fill in the type and the value of the symbol.** RETURNS: OK, or ERROR if symbol not found.** INTERNAL* The size of the symbol name is limited by MAX_SYS_SYM_LEN.** NOMANUAL*/STATUS symFindByCName ( 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 */ ) { char symBuf[MAX_SYS_SYM_LEN + 1]; if (symFindByName (symTblId, name, pValue, pType) == ERROR) { /* prepend a '_' and try again */ *symBuf = '_'; strcpy (&symBuf[1], name); return (symFindByName (symTblId, symBuf, pValue, pType)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -