📄 loadaoutlib.c
字号:
/* loadAoutLib.c - UNIX a.out object module loader *//* Copyright 1984-2002 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------06o,11may02,fmk SPR 77007 - improve common symbol support06n,28mar02,jn rewind to the beginning of the file before reading the header information (SPR 73145)06m,30nov98,dbt no longer clear seg.flags<xxx> after loadSegmentAllocate() call. (SPR #23553).06l,05oct98,pcn Initialize all the fields in the SEG_INFO structure.06k,16sep98,pcn Set to _ALLOC_ALIGN_SIZE the flags field in seg structure (SPR #21836).06j,17jul98,pcn Fixed SPR #21836: alignment mismatch between sections and target.06i,31oct96,elp Replaced symAdd() call by symSAdd() call (symtbls synchro).06h,07aug96,dbt call loadModuleGet with MODULE_A_OUT format (SPR #3006).06g,23feb95,caf fixed #endif.06f,12jun93,rrr vxsim.06e,30oct92,jwt #if for nonLongErrMsg[] for all CPU_FAMILYs but SPARC.06d,28oct92,yao added support for non-contiguous text/data/bss segments in jwt relSegmentSparc(); cleaned up warnings and relSegmentSparc().06c,16oct92,jmm fixed spr 1664 - local symbols always added (in RdSymtab())06b,31jul92,dnw changed to call CACHE_TEXT_UPDATE. doc tweak.06a,29jul92,jcf removed unnecessary forward declaration.05z,29jul92,elh Move boot routines to bootAoutLib.c05y,22jul92,jmm moduleSegAdd now called for all modules loadSegmentsAllocate now called to allocate memory05x,21jul92,rdc mods to support text segment write protection. changed the way aoutLdMdlAtSym allocates mem for segs (didn't really work before.)05w,14jul92,jmm added support for unloading, including calls to moduleLib permanent cache fix for MC68040 moved addSegNames to loadLib.c05v,04jul92,smb temporary cache fix for MC68040.05u,01jul92,jmm more cleanup of magic numbers05t,23jun92,rrr cleanup of some magic numbers05s,18jun92,ajm, made object module independant with split from loadLib.c jwt fixed masking bug in relSegmentSparc() as per SPARC 05i.05r,26may92,rrr the tree shuffle05q,10dec91,gae added includes for ANSI.05p,19nov91,rrr shut up some ansi warnings.05o,05oct91,ajm changed bad ifdef of CPU==MIPS to CPU_FAMILY in relSegmentR3k05n,04oct91,rrr passed through the ansification filter -changed functions to ansi style -changed includes to have absolute path from h/ -fixed #else and #endif -changed TINY and UTINY to INT8 and UINT8 -changed READ, WRITE and UPDATE to O_RDONLY O_WRONLY and ... -changed VOID to void -changed copyright notice05m,30aug91,ajm added MIPS support from 68k 05h, and mips 4.02 05q.05l,28aug91,shl added cache coherency calls for MC68040 support.05k,05apr91,jdi documentation -- removed header parens and x-ref numbers; doc review by dnw.05j,31mar91,del made file null for I960.05i,08feb91,jaa documentation cleanup.05h,05oct90,dnw made loadModuleAtSym() be NOMANUAL.05g,30jul90,dnw changed pathLastName() to pathLastNamePtr(). added forward declaration of void routines.05f,11may90,yao added missing modification history (05e) for the last checkin.05e,09may90,yao typecasted malloc to (char *).05d,22aug89,jcf updated to allow for separate symbol tables. symbol table type now a SYM_TYPE.05c,06jul89,ecs updated to track 4.0.2.05b,06jun89,ecs fixed bug in relSegmentSparc.05a,01may89,ecs split relSegment into relSegmentSparc & relSegment68k.04g,06jul89,llk changed call to pathLastName().04f,16jun89,llk made loadModuleAt add absolute symbols to symbol table. created SEG_INFO structure to reduce parameter passing.04e,09jun89,llk fixed bug so that symbols which reside in segments that have been located at a specific address now have the correct relocated address entered in the symbol table.04d,01may89,gae added empty N_ABS case to relSegment to stop useless warnings.04c,02oct88,hin used correct N_TYPE constant in 04b fix.04b,29sep88,gae fixed relative relocation bug with Greenhill's a.out, sort of introduced in 03y.04a,07sep88,gae applied dnw's documentation.03z,15aug88,jcf lint.03y,30jun88,dnw fixed bug of not relocating relative to data or bss correctly. llk03x,05jun88,dnw changed from ldLib to loadLib. removed pathTail.03w,30may88,dnw changed to v4 names.03v,28may88,dnw removed ldLoadAt.03u,19nov87,dnw made ldLoadModule tolerant of 0 length segments, strings, etc.03t,04nov87,rdc made ldLoadModule not create segment names if NO_SYMBOLS.03s,16oct87,gae removed ldLoadName, or ldLoadFile, and made ldLoadModule determine and add module name to symbol table. added pathTail().03r,29jul87,ecs added ldLoadFile, ldLoadModule. rdc fixed rdSymtab to zero out space malloc'd for common symbols. dnw fixed to require exact match with system symbol table instead of allowing matches with or without leading '_'. fixed to not link COMM symbols to existing symbols.03q,02jul87,ecs changed rdSymtab to call symFindType, instead of symFind.03p,02apr87,ecs added include of strLib.h03o,23mar87,jlf documentation.03n,20dec86,dnw changed to not get include files from default directories.03m,24nov86,llk deleted SYSTEM conditional compiles.03l,09oct86,dnw fixed handling of "pc relative" relocation commands. cleaned up rdSymtab. changed to allocate externals table based on size of symbol table in object module.03k,04sep86,jlf minor documentation.03j,27jul86,llk prints error messages to standard error03i,24jun86,rdc now throws away stabs and absolute symbols. Now dynamically allocates and frees necessary buffers. now searches symbol table before allocating new symbol for type N_COMM.03h,04jun86,dnw changed sstLib calls to symLib.03g,03mar86,jlf changed ioctrl calls to ioctl.03f,22oct85,dnw Fixed bug reading 4 bytes too many for BSD4.2 string table.03e,11oct85,jlf Made the string table buffer and the externals buffer be allocated rather than on the stack. Made new routine ldInit. Got rid of MAX_STRINGS and MAX_SYMBOLS, and replaced them with local variables which are initialize by ldInit. De-linted.03d,10oct85,jlf upped MAX_STRINGS form 20000 to 25000. Made all references use the #define, instead of raw numbers.03c,27aug85,rdc changed MAX_SYM_LEN to MAX_SYS_SYM_LEN.03b,12aug85,jlf fixed to not try to relocate segments that have already been relocated pc-relative in 4.2 version. Also, made it check relocation segment length, and print an error msg if it's not a long.03a,07aug85,jlf combined 4.2 and v7/sys5 version.02b,20jul85,jlf documentation.02a,24jun85,rdc modified for 4.2 a.out format.01e,19sep84,jlf cleaned up comments a little01d,09sep84,jlf added comments, copyright, got rid of GLOBAL.01c,10aug84,dnw changed load to not add special module symbols {T,D,B,E}name but also changed to add _etext, _edata, and _end as normal symbols. fixed rdSymtab to not set S_ldLib_UNDEFINED_SYMBOL and instead just leave it S_symLib_SYMBOL_NOT_FOUND. changed load to continue with relocation even if undefined symbols were found. changed call to 'fioOpen' to just 'open'. replaced 'load' and 'loadat' with 'ldLoad' and 'ldLoadAt', which now take an fd instead of filename.01b,02jul84,ecs changed format strings to be more unixlike.01a,27apr84,dnw written: some culled from old fioLib*//*DESCRIPTIONThis library provides an object module loading facility. Any UNIX BSD `a.out'format files may be loaded into memory, relocated properly, theirexternal references resolved, and their external definitions added tothe system symbol table for use by other modules and from the shell.Modules may be loaded from any I/O stream.EXAMPLE.CS fdX = open ("/devX/objFile", O_RDONLY); loadModule (fdX, ALL_SYMBOLS); close (fdX);.CEThis code fragment would load the `a.out' file "objFile" located ondevice "/devX/" into memory which would be allocated from the systemmemory pool. All external and static definitions from the file would beadded to the system symbol table.This could also have been accomplished from the shell, by typing:.CS -> ld (1) </devX/objFile.CEINCLUDE FILE: loadAoutLib.hSEE ALSO: usrLib, symLib, memLib,.pG "Basic OS"*//* LINTLIBRARY */#include "vxWorks.h"#include "a_out.h"#include "ioLib.h"#include "loadLib.h"#include "loadAoutLib.h"#include "stdlib.h"#include "pathLib.h"#include "string.h"#include "symLib.h"#include "sysSymTbl.h"#include "cacheLib.h"#include "fioLib.h"#include "logLib.h"#include "memLib.h"#include "errno.h"#include "stdio.h"#include "moduleLib.h"#include "private/vmLibP.h"#include "private/loadLibP.h"#if CPU_FAMILY==SIMSPARCSUNOS#undef CPU_FAMILY#define CPU_FAMILY SPARC#endif /* CPU_FAMILY==SIMSPARCSUNOS *//* The different systems use different names for the same info in some * structures. Make them the same here. */#define A_OUT_HDR exec#define TEXTSIZE a_text#define DATASIZE a_data#define BSSSIZE a_bss#define TRSIZE a_trsize#define DRSIZE a_drsize#define U_SYM_STRUCT nlist#define U_SYM_TYPE n_type#define U_SYM_VALUE n_value#define RTEXT 0#define RDATA 1#define RBSS 2LOCAL char stringMemErrMsg [] = "loadAoutLib error: insufficient memory for strings table (need %d bytes).\n";LOCAL char extMemErrMsg [] = "loadAoutLib error: insufficient memory for externals table (need %d bytes).\n";LOCAL char cantAddSymErrMsg [] = "loadAoutLib error: can't add '%s' to system symbol table - error = 0x%x.\n";#if (CPU_FAMILY != SPARC)LOCAL char nonLongErrMsg [] = "loadAoutLib error: attempt to relocate non-long address at 0x%x, code = %d.\n";#endifLOCAL char readStringsErrMsg [] = "loadAoutLib error: can't read string table - status = 0x%x\n";/* forward static functions */#if CPU_FAMILY == SPARCstatic STATUS relSegmentSparc (int fd, int nbytes, char *segAddress, SEG_INFO *pSeg, char ** externals);#elsestatic STATUS relSegment68k (int fd, int nbytes, char *segAddress, SEG_INFO *pSeg, char ** externals);#endif/* forward declarations */ LOCAL MODULE_ID aoutLdMdlAtSym (int fd, int symFlag, char **ppText, char **ppData, char **ppBss, SYMTAB_ID symTbl);LOCAL STATUS rdSymtab (int fd, int nbytes, char ** *externals, int max_symbols, int symFlag, SEG_INFO *pSeg, char *symStrings, SYMTAB_ID symTbl, UINT16 group);/* misc defines */#define STORE_MASKED_VALUE(address, mask, value) \ *(address) = ((*(address)) & ~(mask)) | ((value) & (mask))/********************************************************************************* loadAoutInit - initialize the system for aout load modules** This routine initialized VxWorks to use an extended coff format for* loading modules.** RETURNS:* OK, or* ERROR if XXX** SEE ALSO: loadModuleAt()*/STATUS loadAoutInit ( void ) { /* XXX check for installed ? */ loadRoutine = (FUNCPTR) aoutLdMdlAtSym; return (OK); }/******************************************************************************** aoutLdMdlAtSym - load object module into memory with specified symbol table** This routine is the underlying routine to loadModuleAtSym(). This interface* allows specification of the the symbol table used to resolve undefined* external references and to which to add new symbols.** RETURNS:* MODULE_ID, or* NULL if can't read file or not enough memory or illegal file format** NOMANUAL*/LOCAL MODULE_ID aoutLdMdlAtSym ( FAST int fd, /* fd from which to read module */ int symFlag, /* symbols to be added to table */ /* ([NO | GLOBAL | ALL]_SYMBOLS) */ char **ppText, /* load text segment at address pointed to by this */ /* pointer, return load address via this pointer */ char **ppData, /* load data segment at address pointed to by this */ /* pointer, return load address via this pointer */ char **ppBss, /* load bss segment at address pointed to by this */ /* pointer, return load address via this pointer */ SYMTAB_ID symTbl /* symbol table to use */ ) { struct A_OUT_HDR hdr; /* module's a.out header stored here */ SEG_INFO seg; int status; int nbytes; FAST int numExternals; UINT16 group; MODULE_ID moduleId; char * pText = (ppText == NULL) ? LD_NO_ADDRESS : *ppText; char * pData = (ppData == NULL) ? LD_NO_ADDRESS : *ppData; char * pBss = (ppBss == NULL) ? LD_NO_ADDRESS : *ppBss; char ** externalsBuf = NULL; /* buffer for reading externals */ FAST char * stringsBuf = NULL; char fileName[255]; /* Initialization */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -