Beispiel #1
0
static return_val registerSec( orl_sec_handle shnd, section_type type )
{
    section_ptr         sec;
    return_val          error;

    sec = MemAlloc( sizeof( section_struct ) );
    if( sec ) {
        error = HashTableInsert( HandleToSectionTable, (hash_value) shnd, (hash_data) sec );
        if( error == OKAY ) {
            memset( sec, 0, sizeof( section_struct ) );
            sec->shnd = shnd;
            sec->name = ORLSecGetName( shnd );
            sec->type = type;
            sec->next = NULL;
            if( Sections.first ) {
                Sections.last->next = sec;
                Sections.last = sec;
            } else {
                Sections.first = sec;
                Sections.last = sec;
            }
        } else {
            MemFree( sec );
            return( OUT_OF_MEMORY );
        }
    } else {
        return( OUT_OF_MEMORY );
    }
    return( OKAY );
}
Beispiel #2
0
static return_val registerSec( orl_sec_handle shnd, section_type type )
{
    section_ptr         section;
    return_val          error;
    hash_entry_data     key_entry;

    section = MemAlloc( sizeof( section_struct ) );
    if( section != NULL ) {
        key_entry.key.u.sec_handle = shnd;
        key_entry.data.u.section = section;
        error = HashTableInsert( HandleToSectionTable, &key_entry );
        if( error == RC_OKAY ) {
            memset( section, 0, sizeof( section_struct ) );
            section->shnd = shnd;
            section->name = ORLSecGetName( shnd );
            section->type = type;
            section->next = NULL;
            if( Sections.first != NULL ) {
                Sections.last->next = section;
                Sections.last = section;
            } else {
                Sections.first = section;
                Sections.last = section;
            }
        } else {
            MemFree( section );
            return( RC_OUT_OF_MEMORY );
        }
    } else {
        return( RC_OUT_OF_MEMORY );
    }
    return( RC_OKAY );
}
Beispiel #3
0
static void registerSegment( orl_sec_handle o_shnd )
//**************************************************
{
    orl_sec_flags               sec_flags;
    orl_sec_handle              reloc_section;
    orl_sec_alignment           alignment;
    char *                      content;
    int                         ctr;
    segment                     *seg;

    seg = NewSegment();
    seg->name = ORLSecGetName( o_shnd );
    seg->size = ORLSecGetSize( o_shnd );
    seg->start = 0;
    seg->use_32 = 1;            // only 32-bit object files use ORL
    seg->attr = ( 2 << 2 );     // (?) combine public
    alignment = ORLSecGetAlignment( o_shnd );
    // FIXME: Need better alignment translation.
    switch( alignment ) {
    case 0:
        seg->attr |= ( 1 << 5 ); break;
    case 1:
        seg->attr |= ( 2 << 5 ); break;
    case 3:
    case 4:
        seg->attr |= ( 3 << 5 ); break;
    case 8:
        seg->attr |= ( 4 << 5 ); break;
    case 2:
        seg->attr |= ( 5 << 5 ); break;
    case 12:
        seg->attr |= ( 6 << 5 ); break;
    default:
        // fprintf( stderr, "NOTE! 'Strange' alignment (%d) found. Using byte alignment.\n", alignment );
        seg->attr |= ( 1 << 5 ); break;
    }
    sec_flags = ORLSecGetFlags( o_shnd );
    if( !( sec_flags & ORL_SEC_FLAG_EXEC ) ) {
        seg->data_seg = true;
    }
    if( seg->size > 0 && ORLSecGetContents( o_shnd, &content ) == ORL_OKAY ) {
        Segment = seg;
        // Putting contents into segment struct.
        for( ctr = 0; ctr < seg->size; ctr++ ) {
            PutSegByte( ctr, content[ctr] );
        }
    }
    if( !HashTableInsert( SectionToSegmentTable, (hash_value)o_shnd, (hash_data)seg ) ) {
        SysError( ERR_OUT_OF_MEM, false );
    }
    reloc_section = ORLSecGetRelocTable( o_shnd );
    if( reloc_section ) {
        if( !addRelocSection( reloc_section ) ) {
            SysError( ERR_OUT_OF_MEM, false );
        }
    }
}
Beispiel #4
0
static orl_return DeclareSegment( orl_sec_handle sec )
/****************************************************/
// declare the "segment"
{
    segdata             *sdata;
    segnode             *snode;
    char                *name;
    unsigned_32 _WCUNALIGNED *contents;
    size_t              len;
    orl_sec_flags       flags;
    orl_sec_type        type;
    unsigned            numlines;
    unsigned            segidx;

    type = ORLSecGetType( sec );
    if( type != ORL_SEC_TYPE_NO_BITS && type != ORL_SEC_TYPE_PROG_BITS ) {
         return( ORL_OKAY );
    }
    flags = ORLSecGetFlags( sec );
    name = ORLSecGetName( sec );
    sdata = AllocSegData();
    segidx = ORLCvtSecHdlToIdx( sec );
    snode = AllocNodeIdx( SegNodes, segidx );
    snode->entry = sdata;
    snode->handle = sec;
    sdata->iscdat = (flags & ORL_SEC_FLAG_COMDAT) != 0;
    len = sizeof( CoffIDataSegName ) - 1;
    if( strnicmp( CoffIDataSegName, name, len ) == 0 ) {
        SeenDLLRecord();
        CurrMod->modinfo |= MOD_IMPORT_LIB;
        if( name[len + 1] == '6' ) {    // it is the segment containg the name
            ORLSecGetContents( sec, (unsigned_8 **)&ImpExternalName );
            ImpExternalName += 2;
        } else if( name[len + 1] == '4' ) {     // it is an import by ordinal
            ORLSecGetContents( sec, (void *) &contents );
            ImpOrdinal = *contents;
        }
        sdata->isdead = true;
        sdata->isidata = true;
    }
    sdata->combine = COMBINE_ADD;
    if( flags & ORL_SEC_FLAG_NO_PADDING ) {
        sdata->align = 0;
    } else {
        sdata->align = ORLSecGetAlignment( sec );
    }
    sdata->is32bit = true;
    sdata->length = ORLSecGetSize( sec );
    sdata->u.name = name;
    if( flags & ORL_SEC_FLAG_EXEC ) {
        sdata->iscode = true;
    } else if( flags & ORL_SEC_FLAG_UNINITIALIZED_DATA ) {
        sdata->isuninit = true;
#ifdef _DEVELOPMENT
    } else {
        unsigned namelen;

        namelen = strlen(name);
        if( namelen >= 3 && memicmp(name + namelen - 3, "bss", 3) == 0 ) {
            LnkMsg( ERR+MSG_INTERNAL, "s", "Initialized BSS found" );
        }
#endif
    }
    numlines = ORLSecGetNumLines( sec );
    if( numlines > 0 ) {
        numlines *= sizeof(orl_linnum);
        DBIAddLines( sdata, ORLSecGetLines( sec ), numlines, true );
    }
    return( ORL_OKAY );
}
Beispiel #5
0
orl_return PrintSymbolInfo( orl_symbol_handle symbol )
/****************************************************/
{
    orl_symbol_type                     type;
    char                                *name;
    orl_sec_handle                      sec;

    name = ORLSymbolGetName( symbol );
    //printf( "handle = %x", symbol );
    printf( "%-25s:", name ? name : "" );
    printf( " %8.8llx ", ORLSymbolGetValue( symbol ) );
    switch( ORLSymbolGetBinding( symbol ) ) {
        case ORL_SYM_BINDING_NONE:
            printf( "n/a " );
            break;
        case ORL_SYM_BINDING_LOCAL:
            printf( "locl" );
            break;
        case ORL_SYM_BINDING_WEAK:
            printf( "weak" );
            break;
        case ORL_SYM_BINDING_GLOBAL:
            printf( "glbl" );
            break;
        case ORL_SYM_BINDING_LAZY:
            printf( "lazy" );
            break;
        case ORL_SYM_BINDING_ALIAS:
            printf( "alis" );
            break;
    }
    printf( " " );
    type = ORLSymbolGetType( symbol );
    if( type & ORL_SYM_TYPE_DEBUG ) {
        printf( "debug" );
    } else if( type & ORL_SYM_TYPE_UNDEFINED ) {
        printf( "undef" );
    } else if( type & ORL_SYM_TYPE_COMMON ) {
        printf( "comm " );
    } else if( type & ORL_SYM_TYPE_ABSOLUTE ) {
        printf( "abs  " );
    } else {
        printf( "     " );
    }
    printf( " " );
    if( type & ORL_SYM_TYPE_OBJECT ) {
        printf( "obj " );
    } else if( type & ORL_SYM_TYPE_FUNCTION ) {
        printf( "func" );
    } else if( type & ORL_SYM_TYPE_SECTION ) {
        printf( "sect" );
    } else if( type & ORL_SYM_TYPE_FILE ) {
        printf( "file" );
    } else {
        printf( "none" );
    }
    sec = ORLSymbolGetSecHandle( symbol );
    if( sec ) {
        printf( " (in '%s')", ORLSecGetName( sec ) );
    }
    printf( "\n" );
    return( ORL_OKAY );
}
Beispiel #6
0
orl_return PrintSecInfo( orl_sec_handle o_shnd )
/**********************************************/
{
    char                        *buf;
    int                         loop;
    int                         loop2;
    orl_sec_size                size;
    orl_sec_type                sec_type;
    orl_sec_flags               sec_flags;
    orl_sec_handle              reloc_section;
    orl_sec_handle              symbol_table;
    orl_sec_handle              string_table;
    int                         sep;
    int                         segname_printed = 0;

    size = 0;   // just because gcc is a little retarded
    sectionFound = 1;
    if( dump.sections || dump.sec_contents ) {
        buf = ORLSecGetName( o_shnd );
        printf( "[%s]\n", buf );
        segname_printed = 1;
        size = ORLSecGetSize( o_shnd );
    }
    if( dump.sections ) {
        //printf( "\nSection Handle:\t0x%x\n", o_shnd );
        printf( "Size=%8.8x ", size );
        sec_type = ORLSecGetType( o_shnd );
        printf( "Align=%4.4x ", ORLSecGetAlignment( o_shnd ) );
        //printf( "Section Type:\t%d\n", sec_type );
        printf( "(" );
        switch( sec_type ) {
            case ORL_SEC_TYPE_NONE:
                printf( "sec_type_none" );
                break;
            case ORL_SEC_TYPE_NO_BITS:
                printf( "no bits" );
                break;
            case ORL_SEC_TYPE_PROG_BITS:
                printf( "program bits" );
                break;
            case ORL_SEC_TYPE_SYM_TABLE:
                printf( "symbol table" );
                break;
            case ORL_SEC_TYPE_DYN_SYM_TABLE:
                printf( "dynamic symbol table" );
                break;
            case ORL_SEC_TYPE_STR_TABLE:
                printf( "string table" );
                break;
            case ORL_SEC_TYPE_RELOCS:
                printf( "relocs" );
                break;
            case ORL_SEC_TYPE_RELOCS_EXPADD:
                printf( "relocs with explicit addends" );
                break;
            case ORL_SEC_TYPE_HASH:
                printf( "hash table" );
                break;
            case ORL_SEC_TYPE_DYNAMIC:
                printf( "dynamic linking information" );
                break;
            case ORL_SEC_TYPE_NOTE:
                printf( "note or comment" );
                break;
            default:
                printf( "unknown type? %s", sec_type );
                break;
        }
        printf( ") " );
        sec_flags = ORLSecGetFlags( o_shnd );
        printf( "ORL_flags=0x%x\n", sec_flags );
        sep = 0;
        if( sec_flags ) printf( " " );
        if( sec_flags & ORL_SEC_FLAG_EXEC ) {
            printf( "executable code" );
            sep++;
        }
        if( sec_flags & ORL_SEC_FLAG_INITIALIZED_DATA ) {
            if( sep++ ) printf( ", " );
            printf( "initialized data" );
        }
        if( sec_flags & ORL_SEC_FLAG_UNINITIALIZED_DATA ) {
            if( sep++ ) printf( ", " );
            printf( "uninitialized data" );
        }
        if( sec_flags & ORL_SEC_FLAG_GROUPED ) {
            if( sep++ ) printf( ", " );
            printf( "grouped section" );
        }
        if( sec_flags & ORL_SEC_FLAG_NO_PADDING ) {
            if( sep++ ) printf( ", " );
            printf( "no padding to next boundary" );
        }
        if( sec_flags & ORL_SEC_FLAG_OVERLAY ) {
            if( sep++ ) printf( ", " );
            printf( "contains an overlay" );
        }
        if( sec_flags & ORL_SEC_FLAG_REMOVE ) {
            if( sep++ ) printf( ", " );
            printf( "remove at link-time" );
        }
        if( sec_flags & ORL_SEC_FLAG_COMDAT ) {
            if( sep++ ) printf( ", " );
            printf( "communal data" );
        }
        if( sec_flags & ORL_SEC_FLAG_DISCARDABLE ) {
            if( sep++ ) printf( ", " );
            printf( "discardable" );
        }
        if( sec_flags & ORL_SEC_FLAG_NOT_CACHED ) {
            if( sep++ ) printf( ", " );
            printf( "cannot be cached" );
        }
        if( sec_flags & ORL_SEC_FLAG_NOT_PAGEABLE) {
            if( sep++ ) printf( ", " );
            printf( "not pageable" );
        }
        if( sec_flags & ORL_SEC_FLAG_SHARED ) {
            if( sep++ ) printf( ", " );
            printf( "shared in memory" );
        }
        if( sec_flags & ORL_SEC_FLAG_EXECUTE_PERMISSION ) {
            if( sep++ ) printf( ", " );
            printf( "execute permission" );
        }
        if( sec_flags & ORL_SEC_FLAG_READ_PERMISSION ) {
            if( sep++ ) printf( ", " );
            printf( "read permission" );
        }
        if( sec_flags & ORL_SEC_FLAG_WRITE_PERMISSION) {
            if( sep++ ) printf( ", " );
            printf( "write permission" );
        }
        if( sep ) printf( "\n" );
    }
    if( dump.sec_contents ) {
        if( ORLSecGetContents( o_shnd, (unsigned_8 **)&buf ) == ORL_OKAY ) {
            printf( "Contents:\n" );
            for( loop = 0; loop < size; loop += 16 ) {
                printf( "0x%8.8x: ", loop );
                for( loop2 = 0; loop2 < 16; loop2++ ) {
                    if( loop + loop2 < size ) {
                        printf( "%2.2x ", buf[loop+loop2] );
                    } else {
                        printf( "   " );
                    }
                    if( loop2 == 7 ) {
                        printf( " " );
                    }
                }
                printf( " " );
                for( loop2 = 0; loop2 < 16 && loop + loop2 < size; loop2++ ) {
                    if( buf[loop+loop2] >= 32 && buf[loop+loop2] <= 122 ) {
                        printf( "%c", buf[loop+loop2] );
                    } else {
                        printf( "." );
                    }
                    if( loop2 == 7 ) {
                        printf( " " );
                    }
                }
                printf( "\n" );
            }
        }
    }
    if( dump.relocs ) {
        reloc_section = ORLSecGetRelocTable( o_shnd );
        if( reloc_section ) {
            if( !segname_printed++ ) {
                buf = ORLSecGetName( o_shnd );
                printf( "[%s]\n", buf );
            }
            printf( "Relocs in [%s], ", ORLSecGetName( reloc_section ) );
            symbol_table = ORLSecGetSymbolTable( reloc_section );
            if( symbol_table ) {
                printf( "symtab='%s', ", ORLSecGetName( symbol_table ) );
                string_table = ORLSecGetStringTable( symbol_table );
                if( string_table ) {
                    printf( "strtab='%s'.\n", ORLSecGetName( string_table ) );
                } else {
                    printf( "strtab=none.\n" );
                }
            } else {
                printf( "symtab=none, strtab=none.\n" );
            }
            //printf( "Relocs:\n" );
            ORLRelocSecScan( reloc_section, &PrintRelocInfo );
        }
    }
    return( ORL_OKAY );
}
Beispiel #7
0
orl_return CreateNamedLabel( orl_symbol_handle sym_hnd )
{
    hash_data *         data_ptr;
    label_list          sec_label_list;
    label_entry         entry;
    orl_symbol_type     type;
    orl_symbol_type     primary_type;
    orl_sec_handle      sec;
    char *              SourceName;
    char *              LabName;
    unsigned_64         val64;

    type = ORLSymbolGetType( sym_hnd );
    primary_type = type & 0xFF;
    switch( primary_type ) {
// No harm in including these since elf generates relocs to these.
//      case ORL_SYM_TYPE_NONE:
//      case ORL_SYM_TYPE_FUNC_INFO:
//          return( ORL_OKAY );
        case ORL_SYM_TYPE_FILE:
            SourceName = ORLSymbolGetName( sym_hnd );
            if( (SourceName != NULL) && (SourceFileInObject == NULL) ) {
                SourceFileInObject = SourceName;
            }
            return( ORL_OKAY );
    }
    entry = MemAlloc( sizeof( label_entry_struct ) );
    if( !entry ) return( ORL_OUT_OF_MEMORY );
    val64 = ORLSymbolGetValue( sym_hnd );
    entry->offset = val64.u._32[I64LO32];
    // all symbols from the object file will have names
    entry->shnd = ORLSymbolGetSecHandle( sym_hnd );
    if( primary_type == ORL_SYM_TYPE_SECTION ) {
        entry->type = LTYP_SECTION;
    } else if( primary_type == ORL_SYM_TYPE_GROUP ) {
        entry->type = LTYP_GROUP;
    } else if( entry->shnd == 0 ) {
        entry->type = LTYP_EXTERNAL_NAMED;
    } else if( primary_type == ORL_SYM_TYPE_FUNC_INFO ){
        entry->type = LTYP_FUNC_INFO;
    } else {
        entry->type = LTYP_NAMED;
    }
    entry->binding = ORLSymbolGetBinding( sym_hnd );
    LabName = ORLSymbolGetName( sym_hnd );
    if( LabName == NULL ) {
        sec = ORLSymbolGetSecHandle( sym_hnd );
        if( sec ) {
            LabName = ORLSecGetName( sec );
        } else {
            MemFree( entry );
            return( ORL_OKAY );
        }
    }

    // Demangle the name, if necessary
    if( !((Options & NODEMANGLE_NAMES) || (DFormat & DFF_ASM)) ) {
        entry->label.name = MemAlloc( MAX_LINE_LEN + 3 );
        __demangle_l( LabName, 0, &(entry->label.name[2]), MAX_LINE_LEN );
    } else {
        entry->label.name = MemAlloc( strlen( LabName )+8 );
        strcpy( &(entry->label.name[2]), LabName );
    }

    entry->label.name[0]=0;
    entry->label.name[1]=0;
    LabName = &(entry->label.name[2]);
    if( NeedsQuoting( LabName ) ) {
        // entry->label.name[-1] will be 1 if we have added a quote,
        // 0 otherwise.  This is helpful when freeing the memory.
        entry->label.name[0] = 1;
        entry->label.name[1] = '`';
        entry->label.name += 1;
        LabName += strlen( LabName );
        LabName[0] = '`';
        LabName[1] = '\0';
    } else {
        entry->label.name += 2;
    }
    data_ptr = HashTableQuery( HandleToLabelListTable, (hash_value) entry->shnd );
    if( data_ptr ) {
        sec_label_list = (label_list) *data_ptr;
        entry = addLabel( sec_label_list, entry, sym_hnd );
        if( Options & PRINT_PUBLICS && entry->shnd != 0 &&
                primary_type != ORL_SYM_TYPE_SECTION &&
                entry->binding != ORL_SYM_BINDING_LOCAL ) {
            Publics.number++;
        }
    } else {
        // error!!!! the label list should have been created
        MemFree( entry );
        return( ORL_ERROR );
    }
    return( ORL_OKAY );
}