bool InitORL( void ) //****************** // Try and see if we will use ORL. Returns true if we'll use it. { orl_file_flags o_flags; orl_file_format o_format; orl_machine_type o_machine_type; ORLFileHnd = NULL; oFuncs.alloc = AllocMem; oFuncs.free = FreeMem; oFuncs.read = (void * (*) ( void *, int ))buffRead; oFuncs.seek = (long int (*) ( void *, long int, int ))buffSeek; ORLHnd = ORLInit( &oFuncs ); if( !ORLHnd ) { SysError( ERR_OUT_OF_MEM, false ); } initBuffer( &fileBuff, ObjFile ); o_format = ORLFileIdentify( ORLHnd, &fileBuff ); if( o_format != ORL_ELF && o_format != ORL_COFF ) { ORLFini( ORLHnd ); finiBuffer( &fileBuff ); ORLHnd = NULL; return( false ); // Will use ParseObjectOMF } ORLFileHnd = ORLFileInit( ORLHnd, &fileBuff, o_format ); if( !ORLFileHnd ) { ORLFini( ORLHnd ); finiBuffer( &fileBuff ); SysError( ERR_OUT_OF_MEM, false ); } o_machine_type = ORLFileGetMachineType( ORLFileHnd ); if( o_machine_type != ORL_MACHINE_TYPE_I386 ) { FiniORL(); Error( ERR_ORL_INV_MACHINE_TYPE, true ); exit( 1 ); } o_flags = ORLFileGetFlags( ORLFileHnd ); if( !(o_flags & ORL_FILE_FLAG_LITTLE_ENDIAN) ) { FiniORL(); Error( ERR_ORL_INV_BYTE_ORDER, true ); exit( 1 ); } UseORL = true; createHashTables(); return( true ); // Success: will use ORL }
/* * Given a set of object and library files, prepare to use fuzzy linking. * If symbols cannot be collected from a file, 'callback' will be called * with the name of the offending file. */ void InitFuzzy( const char *objs[], const char *libs[], const char *libpaths[], FuzzyInitCallback callback ) /******************************************************************/ { unsigned count; orl_handle o_hnd; int rc = 1; DllToolCallbacks dllcallbacks; ORLSetFuncs( orl_cli_funcs, obj_read, obj_seek, AllocMem, FreeMem ); /*** Create a hash table ***/ hashtable = InitHash( HASH_TABLE_SIZE, hash_symbol_name, hash_compare ); /*** Collect all external symbols from the specified object files ***/ if( objs != NULL ) { /*** Initialize ORL ***/ o_hnd = ORLInit( &orl_cli_funcs ); if( o_hnd == NULL ) { FatalError( "Got NULL orl_handle." ); } /*** Scan the file(s) ***/ for( count=0; objs[count]!=NULL; count++ ) { if( !handle_obj_file( objs[count], o_hnd ) ) { rc = (*callback)( objs[count] ); if( !rc ) break; } } /*** Tell ORL to go away ***/ if( ORLFini( o_hnd ) != ORL_OKAY ) { Warning( "Error calling ORLFini." ); } free_list( bufflist ); } if( !rc ) return; /*** Collect all external symbols from the specified library files ***/ if( libs != NULL ) { /*** Load the WLIB DLL ***/ dllcallbacks.printmessage = NULL; dllcallbacks.printmessageCRLF = print_message_crlf; dllcallbacks.printwithinfo2 = print_with_info2; dllcallbacks.printwithinfo = print_with_info; dllcallbacks.cookie = NULL; dllhandle = InitDllTool( DLLTOOL_WLIB, &dllcallbacks ); if( dllhandle == NULL ) { Warning( "Cannot load WLIB DLL -- fuzzy name matching may not work" ); } else { /*** Scan the file(s) ***/ for( count=0; libs[count]!=NULL; count++ ) { if( !handle_lib_file( libs[count], libpaths ) ) { rc = (*callback)( libs[count] ); if( !rc ) break; } } FiniDllTool( dllhandle ); } } }
void FiniORL( void ) //****************** { if( ORLFileHnd ) { ORLFileFini( ORLFileHnd ); ORLFini( ORLHnd ); } destroyHashTables(); finiBuffer( &fileBuff ); }
void FreeServicesUsed( void ) { orl_return status; DisFini( &DHnd ); status = ORLFini( ORLHnd ); if( status != ORL_OKAY ) { PrintErrorMsg( ERROR, WHERE_CLOSING_ORL ); } }
static return_val initORL( void ) { orl_file_flags flags; orl_machine_type machine_type; orl_return o_error = ORL_OKAY; orl_file_format type; bool byte_swap; ORLSetFuncs( orl_cli_funcs, objRead, objSeek, MemAlloc, MemFree ); ORLHnd = ORLInit( &orl_cli_funcs ); if( ORLHnd != ORL_NULL_HANDLE ) { type = ORLFileIdentify( ORLHnd, NULL ); if( type == ORL_UNRECOGNIZED_FORMAT ) { PrintErrorMsg( RC_OKAY, WHERE_NOT_COFF_ELF ); return( RC_ERROR ); } ObjFileHnd = ORLFileInit( ORLHnd, NULL, type ); if( ObjFileHnd != ORL_NULL_HANDLE ) { // check byte order flags = ORLFileGetFlags( ObjFileHnd ); byte_swap = false; #ifdef __BIG_ENDIAN__ if( flags & ORL_FILE_FLAG_LITTLE_ENDIAN ) { byte_swap = true; } #else if( flags & ORL_FILE_FLAG_BIG_ENDIAN ) { byte_swap = true; } #endif // check intended machine type machine_type = GetMachineType(); switch( machine_type ) { // If there's no machine specific code, the CPU we choose shouldn't // matter; there are some object files like this. case ORL_MACHINE_TYPE_NONE: case ORL_MACHINE_TYPE_ALPHA: if( DisInit( DISCPU_axp, &DHnd, byte_swap ) != DR_OK ) { ORLFini( ORLHnd ); PrintErrorMsg( RC_OKAY, WHERE_UNSUPPORTED_PROC ); return( RC_ERROR ); } break; case ORL_MACHINE_TYPE_PPC601: if( DisInit( DISCPU_ppc, &DHnd, byte_swap ) != DR_OK ) { ORLFini( ORLHnd ); PrintErrorMsg( RC_OKAY, WHERE_UNSUPPORTED_PROC ); return( RC_ERROR ); } // PAS assembler expects "", not \" for quotes. if( (Options & METAWARE_COMPATIBLE) == 0 ) { QuoteChar = '\"'; } break; case ORL_MACHINE_TYPE_R3000: case ORL_MACHINE_TYPE_R4000: if( DisInit( DISCPU_mips, &DHnd, byte_swap ) != DR_OK ) { ORLFini( ORLHnd ); PrintErrorMsg( RC_OKAY, WHERE_UNSUPPORTED_PROC ); return( RC_ERROR ); } break; case ORL_MACHINE_TYPE_I386: case ORL_MACHINE_TYPE_I8086: if( DisInit( DISCPU_x86, &DHnd, byte_swap ) != DR_OK ) { ORLFini( ORLHnd ); PrintErrorMsg( RC_OKAY, WHERE_UNSUPPORTED_PROC ); return( RC_ERROR ); } break; case ORL_MACHINE_TYPE_AMD64: if( DisInit( DISCPU_x64, &DHnd, byte_swap ) != DR_OK ) { ORLFini( ORLHnd ); PrintErrorMsg( RC_OKAY, WHERE_UNSUPPORTED_PROC ); return( RC_ERROR ); } break; case ORL_MACHINE_TYPE_SPARC: case ORL_MACHINE_TYPE_SPARCPLUS: if( DisInit( DISCPU_sparc, &DHnd, byte_swap ) != DR_OK ) { ORLFini( ORLHnd ); PrintErrorMsg( RC_OKAY, WHERE_UNSUPPORTED_PROC ); return( RC_ERROR ); } break; default: ORLFini( ORLHnd ); PrintErrorMsg( RC_OKAY, WHERE_UNSUPPORTED_PROC ); return( RC_ERROR ); } return( RC_OKAY ); } else { o_error = ORLGetError( ORLHnd ); ORLFini( ORLHnd ); // An "out of memory" error is not necessarily what it seems. // The ORL returns this error when encountering a bad or // unrecognized object file record. if( o_error == ORL_OUT_OF_MEMORY ) { PrintErrorMsg( RC_OUT_OF_MEMORY, WHERE_OPENING_ORL ); return( RC_OUT_OF_MEMORY ); } else { PrintErrorMsg( RC_ERROR, WHERE_OPENING_ORL ); return( RC_ERROR ); } } } else { return( RC_OUT_OF_MEMORY ); } }
void ObjORLFini( void ) /****************************/ { ORLFini( ORLHandle ); }
int main( int argc, char *argv[] ) /********************************/ { orl_handle o_hnd; orl_file_handle o_fhnd; orl_funcs funcs; int file; orl_file_flags file_flags; orl_machine_type machine_type; orl_file_type file_type; orl_file_format type; int c; int sep; char *secs[MAX_SECS]; int num_secs = 0; if( argc < 2 ) { printf( "Usage: objread [-ahrsSx] [-o<section>] <objfile>\n" ); printf( "Where <objfile> is a COFF, ELF or OMF object file\n" ); printf( "objread reads and dumps an object using ORL\n" ); printf( "Options: -a dumps all information (except hex dump)\n" ); printf( " -h dumps file header information\n" ); printf( " -r dumps relocation information\n" ); printf( " -s dumps symbol table\n" ); printf( " -S dumps section information\n" ); printf( " -x get hex dump of section content\n" ); printf( " -o only scan <section> for info\n" ); return( 1 ); } while( (c = getopt( argc, argv, "axhrsSo:" )) != EOF ) { switch( c ) { case 'a': dump.relocs++; dump.header++; dump.symbols++; dump.sections++; break; case 'x': dump.sec_contents++; break; case 'h': dump.header++; break; case 'r': dump.relocs++; break; case 's': dump.symbols++; break; case 'S': dump.sections++; break; case 'o': secs[num_secs++] = optarg; break; default: // error occured exit(1); }; }; if( optind != argc - 1 ) { fprintf( stderr, "must specify 1 filename\n" ); exit(1); } file = open( argv[optind], O_BINARY | O_RDONLY ); if( file == -1 ) { printf( "Error opening file.\n" ); return( 2 ); } TRMemOpen(); funcs.read = &objRead; funcs.seek = &objSeek; funcs.alloc = &TRMemAlloc; funcs.free = &TRMemFree; o_hnd = ORLInit( &funcs ); if( o_hnd == NULL ) { printf( "Got NULL orl_handle.\n" ); return( 2 ); } type = ORLFileIdentify( o_hnd, (void *)file ); if( type == ORL_UNRECOGNIZED_FORMAT ) { printf( "The object file is not in either ELF, COFF or OMF format." ); return( 1 ); } switch( type ) { case ORL_ELF: printf( "ELF" ); break; case ORL_COFF: printf( "COFF" ); break; case ORL_OMF: printf( "OMF" ); break; default: printf( "Unknown" ); break; } printf( " object file.\n" ); o_fhnd = ORLFileInit( o_hnd, (void *)file, type ); if( o_fhnd == NULL ) { printf( "Got NULL orl_file_handle.\n" ); return( 2 ); } if( dump.header ) { printf( "File %s:\n", argv[optind] ); machine_type = ORLFileGetMachineType( o_fhnd ); printf( "Machine Type: " ); if( machine_type >= ( sizeof( machType ) / sizeof( *machType ) ) ) { // We've probably added some new types? printf( "?(%d)", machine_type ); } else { printf( "%s", machType[ machine_type ] ); } file_type = ORLFileGetType( o_fhnd ); printf( " (" ); switch( file_type ) { case ORL_FILE_TYPE_NONE: printf( "file_type_none" ); break; case ORL_FILE_TYPE_OBJECT: printf( "object file" ); break; case ORL_FILE_TYPE_EXECUTABLE: printf( "executable" ); break; case ORL_FILE_TYPE_SHARED_OBJECT: printf( "shared object" ); break; case ORL_FILE_TYPE_DLL: printf( "DLL" ); break; default: printf( "unknown file type?" ); break; } printf( ")\n" ); file_flags = ORLFileGetFlags( o_fhnd ); //printf(" File flags=0x%x\n", file_flags ); sep = 0; if( file_flags & ORL_FILE_FLAG_LINE_NUMS_STRIPPED ) { printf( "line number info stripped" ); sep = 1; } if( file_flags & ORL_FILE_FLAG_RELOCS_STRIPPED ) { if( sep++ ) printf( ", " ); printf( "relocs stripped" ); } if( file_flags & ORL_FILE_FLAG_LOCAL_SYMS_STRIPPED ) { if( sep++ ) printf( ", " ); printf( "local symbols stripped" ); } if( file_flags & ORL_FILE_FLAG_DEBUG_STRIPPED ) { if( sep++ ) printf( ", " ); printf( "debug info stripped" ); } if( file_flags & ORL_FILE_FLAG_16BIT_MACHINE ) { if( sep++ ) printf( ", " ); printf( "for 16-bit machine" ); } if( file_flags & ORL_FILE_FLAG_32BIT_MACHINE ) { if( sep++ ) printf( ", " ); printf( "for 32-bit machine" ); } if( file_flags & ORL_FILE_FLAG_64BIT_MACHINE ) { if( sep++ ) printf( ", " ); printf( "for 64-bit machine" ); } if( file_flags & ORL_FILE_FLAG_LITTLE_ENDIAN ) { if( sep++ ) printf( ", " ); printf( "little-endian byte order" ); } if( file_flags & ORL_FILE_FLAG_BIG_ENDIAN ) { if( sep++ ) printf( ", " ); printf( "big-endian byte order" ); } if( file_flags & ORL_FILE_FLAG_SYSTEM ) { if( sep++ ) printf( ", " ); printf( "system file" ); } if( sep ) printf( "\n" ); } if( num_secs ) { for( c = 0; c < num_secs; c++ ) { sectionFound = 0; if( ORLFileScan( o_fhnd, secs[c], &PrintSecInfo ) != ORL_OKAY ) { printf( "Error occured in scanning section '%s'.\n", secs[c] ); } if( !sectionFound ) { printf( "Section '%s' not found in object.\n", secs[c] ); } } } else { if( ORLFileScan( o_fhnd, NULL, &PrintSecInfo ) != ORL_OKAY ) { printf( "Error occured in scanning file.\n" ); return( 2 ); } } if( ORLFileScan( o_fhnd, ".symtab", &PrintSymTable ) != ORL_OKAY ) { printf( "Error occured in scanning file for symbol table\n" ); return( 2 ); } if( ORLFileFini( o_fhnd ) != ORL_OKAY ) { printf( "Error calling ORLFileFini.\n" ); return( 2 ); } if( close( file ) == -1 ) { printf( "Error closing file.\n" ); return( 2 ); } if( ORLFini( o_hnd ) != ORL_OKAY ) { printf( "Error calling ORLFini.\n" ); } freeBuffList(); #ifdef TRMEM TRMemPrtList(); #endif TRMemClose(); return( 0 ); }
void FiniObj( void ) /******************/ { ORLFini( ORLHnd ); }
int main( int argc, char *argv[] ) /********************************/ { orl_handle o_hnd; orl_file_handle o_fhnd; orl_file_format type; orl_file_flags o_flags; FILE *fp; int c; char *secs[MAX_SECS]; int num_secs = 0; ORLSetFuncs( orl_cli_funcs, objRead, objSeek, TRMemAlloc, TRMemFree ); if( argc < 2 ) { printf( "Usage: dwdump <file>\n" ); printf( "Where <file> is a COFF, ELF or OMF object file\n" ); printf( "dwdump reads and dumps DWARF debugging information\n" ); return( EXIT_SUCCESS ); } dump.sections++; fp = fopen( argv[1], "rb" ); if( fp == NULL ) { printf( "Error opening file.\n" ); return( EXIT_FAILURE ); } TRMemOpen(); o_hnd = ORLInit( &orl_cli_funcs ); if( o_hnd == NULL ) { printf( "Got NULL orl_handle.\n" ); return( EXIT_FAILURE ); } type = ORLFileIdentify( o_hnd, fp ); if( type == ORL_UNRECOGNIZED_FORMAT ) { printf( "The object file is not in either ELF, COFF or OMF format." ); return( EXIT_FAILURE ); } switch( type ) { case ORL_OMF: printf( "OMF" ); break; case ORL_ELF: printf( "ELF" ); break; case ORL_COFF: printf( "COFF" ); break; default: printf( "Unknown" ); break; } printf( " object file.\n" ); o_fhnd = ORLFileInit( o_hnd, fp, type ); if( o_fhnd == NULL ) { printf( "Got NULL orl_file_handle.\n" ); return( EXIT_FAILURE ); } o_flags = ORLFileGetFlags( o_fhnd ); #ifdef __BIG_ENDIAN__ if( o_flags & ORL_FILE_FLAG_LITTLE_ENDIAN ) { byte_swap = true; } #else if( o_flags & ORL_FILE_FLAG_BIG_ENDIAN ) { byte_swap = true; } #endif if( num_secs ) { for( c = 0; c < num_secs; c++ ) { sectionFound = 0; if( ORLFileScan( o_fhnd, secs[c], &DoSection ) != ORL_OKAY ) { printf( "Error occured in scanning section '%s'.\n", secs[c] ); } if( !sectionFound ) { printf( "Section '%s' not found in object.\n", secs[c] ); } } } else { if( ORLFileScan( o_fhnd, NULL, &DoSection ) != ORL_OKAY ) { printf( "Error occured in scanning file.\n" ); return( EXIT_FAILURE ); } } setSects( o_fhnd ); if( ORLFileFini( o_fhnd ) != ORL_OKAY ) { printf( "Error calling ORLFileFini.\n" ); return( EXIT_FAILURE ); } if( fclose( fp ) ) { printf( "Error closing file.\n" ); return( EXIT_FAILURE ); } if( ORLFini( o_hnd ) != ORL_OKAY ) { printf( "Error calling ORLFini.\n" ); } DumpSections(); freeBuffList(); #ifdef TRMEM TRMemPrtList(); #endif TRMemClose(); return( EXIT_SUCCESS ); }