void OMFLibWalk( libfile io, char *name, void (*rtn)( arch_header *arch, libfile io ) ) /*************************************************************************************/ { long pagelen; long offset; arch_header arch; char buff[ MAX_IMPORT_STRING ]; int len; unsigned_16 rec_len; unsigned_8 type; if( LibRead( io, &type, 1 ) != 1 ) return; // nyi - FALSE? if( LibRead( io, &rec_len, 2 ) != 2 ) return; offset = GET_LE_16( rec_len ); pagelen = offset + 3; if( Options.page_size == 0 ) { Options.page_size = pagelen; } LibSeek( io, offset, SEEK_CUR ); NewArchHeader( &arch, name ); offset = LibTell( io ); while( LibRead( io, &type, 1 ) == 1 && ( type == CMD_THEADR ) ) { LibSeek( io, 2, SEEK_CUR ); if( LibRead( io, &type, 1 ) != 1 ) break; len = type; if( LibRead( io, buff, len ) != len ) break; buff[ len ] = '\0'; arch.name = buff; LibSeek( io, offset, SEEK_SET ); rtn( &arch, io ); offset = LibTell( io ); offset = ( offset + pagelen - 1 ) & ~( pagelen - 1 ); LibSeek( io, offset, SEEK_SET ); } }
void LibWalk( libfile io, char *name, void (*rtn)( arch_header *, libfile io ) ) /******************************************************************************/ { ar_header ar; arch_header arch; file_offset bytes_read; // int dict_count; file_offset pos; // dict_count = 0; arch.fnametab = NULL; arch.ffnametab = NULL; while( (bytes_read = LibRead( io, &ar, AR_HEADER_SIZE )) != 0 ) { if( bytes_read != AR_HEADER_SIZE ) { BadLibrary( name ); } if( strncmp( ar.header_ident, AR_HEADER_IDENT, AR_HEADER_IDENT_LEN ) ) { BadLibrary( name ); } GetARHeaderValues( &ar, &arch ); pos = LibTell( io ); if( ar.name[ 0 ] == '/' && ar.name[ 1 ] == ' ' && ar.name[ 2 ] == ' ' ) { // Ignore symbol table. /* dict_count++; if( dict_count == 2 ) { error = readDict( &arch ); } else { error = MoveAheadFrom( &arch ); updateNewArchive( &arch ); } */ } else if( ar.name[ 0 ] == '/' && ar.name[ 1 ] == '/' && ar.name[ 2 ] == ' ' ) { AllocFNameTab( name, io, &arch ); } else if( ar.name[ 0 ] == '/' && ar.name[ 1 ] == '/' && ar.name[ 2 ] == '/' ) { AllocFFNameTab( name, io, &arch ); } else { arch.name = GetARName( io, &ar, &arch ); arch.ffname = GetFFName( &arch ); rtn( &arch, io ); MemFree( arch.name ); MemFree( arch.ffname ); } if( arch.size & 1 ) ++arch.size; LibSeek( io, pos + arch.size, SEEK_SET ); } MemFree( arch.fnametab ); MemFree( arch.ffnametab ); }
static long ObjSeek( void *fil, long pos, int where ) /***************************************************/ { obj_file *ofile = fil; switch( where ) { case SEEK_SET: pos += ofile->offset; break; case SEEK_CUR: break; } LibSeek( ofile->hdl, pos, where ); return( pos - ofile->offset ); }
static void ExtractObj( libfile io, char *name, arch_header *arch, char *newname ) { file_offset pos; libfile out; char *obj_name; obj_name = MakeObjOutputName( name, newname ); remove( obj_name ); out = LibOpen( obj_name, LIBOPEN_WRITE ); pos = LibTell( io ); CopyObj( io, out, arch ); LibSeek( io, pos, SEEK_SET ); LibClose( out ); if( Options.ar && Options.verbose ) { Message( "x - %s", obj_name ); } }
static void ProcessLibOrObj( char *name, objproc obj, void (*process)( arch_header *arch, libfile io ) ) { libfile io; unsigned char buff[ AR_IDENT_LEN ]; arch_header arch; NewArchHeader( &arch, name ); io = LibOpen( name, LIBOPEN_READ ); if( LibRead( io, buff, sizeof( buff ) ) != sizeof( buff ) ) { FatalError( ERR_CANT_READ, name, strerror( errno ) ); } if( memcmp( buff, AR_IDENT, sizeof( buff ) ) == 0 ) { // AR format AddInputLib( io, name ); LibWalk( io, name, process ); if( Options.libtype == WL_LTYPE_NONE ) { Options.libtype = WL_LTYPE_AR; } } else if( memcmp( buff, LIBMAG, LIBMAG_LEN ) == 0 ) { // MLIB format if( LibRead( io, buff, sizeof( buff ) ) != sizeof( buff ) ) { FatalError( ERR_CANT_READ, name, strerror( errno ) ); } if( memcmp( buff, LIB_CLASS_DATA_SHOULDBE, LIB_CLASS_LEN + LIB_DATA_LEN ) ) { BadLibrary( name ); } AddInputLib( io, name ); LibWalk( io, name, process ); if( Options.libtype == WL_LTYPE_NONE ) { Options.libtype = WL_LTYPE_MLIB; } } else if( AddImport( &arch, io ) ) { LibClose( io ); } else if( buff[ 0 ] == LIB_HEADER_REC && buff[ 1 ] != 0x01 ) { /* The buff[ 1 ] != 1 bit above is a bad hack to get around the fact that the coff cpu_type for PPC object files is 0x1f0. Really, we should be reading in the first object record and doing the checksum and seeing if it is actually a LIB_HEADER_REC. All file format designers who are too stupid to recognize the need for a signature should be beaten up with large blunt objects. */ // OMF format AddInputLib( io, name ); LibSeek( io, 0, SEEK_SET ); if( Options.libtype == WL_LTYPE_NONE ) { Options.libtype = WL_LTYPE_OMF; } OMFLibWalk( io, name, process ); } else if( obj == OBJ_PROCESS ) { // Object LibSeek( io, 0, SEEK_SET ); AddObjectSymbols( &arch, io, 0 ); LibClose( io ); } else if( obj == OBJ_ERROR ) { BadLibrary( name ); } else { LibClose( io ); } }