objread.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 958 行 · 第 1/2 页

C
958
字号
    }
    Lnames = NULL;

}

static void PrintNames( void ) {
/******************************/
    Lnamelist   *entry;
    unsigned_16 k;
    size_t      col;

    if( Lnames != NULL ) {
        k = 0;
        col = Output( CRLF INDENT INDENT "List of Lnames:" CRLF );
        entry = Lnames;
        for( ;; ) {
           if( entry == NULL ) {
               break;
           }
           if( ( col > 40 ) || (entry->LnameLen > 40 ) ) {
               col = Output( CRLF );
           }
           k++;
           col = Output( "%u - '%s' %<", k, &(entry->Lname), 39u );
           entry = entry->next;
       }
       Output( CRLF );
    }

    if( Xnames != NULL ) {
        k = 0;
        col = Output( CRLF INDENT INDENT "List of Xnames:" CRLF );
        entry = Xnames;
        for( ;; ) {
           if( entry == NULL ) {
               break;
           }
           if( ( col > 40 ) || (entry->LnameLen > 40 ) ) {
               col = Output( CRLF );
           }
           k++;
           col = Output( "%u - '%s' %<", k, &(entry->Lname), 39u );
           entry = entry->next;
       }
       Output( CRLF );
    }
}

void AddXname( void ) {
/*********************/
    Lnamelist   *entry;

    entry = malloc( sizeof(Lnamelist) + NameLen );
    if( entry == NULL ) {
        OutputSetFH( stdout );
        Output( CRLF "**FATAL** Out of memory!" CRLF );
        leave( 21 );
    }
    if( Xnames == NULL ) {
        Xnames = entry;
    } else {
        lastXname->next = entry;
    }
    entry->next = NULL;
    entry->LnameLen = NameLen;
    memcpy( &(entry->Lname), NamePtr, NameLen );
    *(&(entry->Lname) + NameLen) = '\0';
    lastXname = entry;
}

char *GetXname( unsigned_16 idx ) {
/*********************************/
    Lnamelist   *entry;
    char        *name;
    int         k;

    if( (Xnames == NULL) || (idx == 0) ) {
        return( "" );
    }
    entry = Xnames;
    for( k = 1; k < idx; ++k) {
        entry = entry->next;
        if( entry == NULL ) {
            return( "" ); /* not found */
        }
    }
    name = &(entry->Lname);
    return( name );
}

static void FreeXnames( void ) {
/*******************************/
    Lnamelist   *entry;

    entry = Xnames;
    for( ;; ) {
        if( entry == NULL ) {
           break;
        }
        Xnames = entry->next;
        free( entry );
        entry = Xnames;
    }
    Xnames = NULL;

}

void AddSegdef( unsigned_16 idx ) {
/*********************************/
    Segdeflist  *entry;
    Segdeflist  *wkentry;

    entry = malloc( sizeof(Segdeflist) );
    if( entry == NULL ) {
        OutputSetFH( stdout );
        Output( CRLF "**FATAL** Out of memory!" CRLF );
        leave( 21 );
    }
    if( Segdefs == NULL ) {
        Segdefs = entry;
    } else {
        wkentry = Segdefs;
        for ( ;; ) {
            if( wkentry->next == NULL ) {
                break;
            }
            wkentry = wkentry->next;
        }
        wkentry->next = entry;
    }
    entry->next = NULL;
    entry->segind = idx;
}

Segdeflist  *GetSegdef( unsigned_16 idx ) {
/*****************************************/
    Segdeflist  *entry;
    int         k;

    if( (Segdefs == NULL) || (idx == 0) ) {
        return( NULL );
    }
    entry = Segdefs;
    for( k = 1; k < idx; ++k) {
        entry = entry->next;
        if( entry == NULL ) {
            return( NULL ); /* not found */
        }
    }
    return( entry );
}

static void FreeSegdefs( void ) {
/*******************************/
    Segdeflist   *entry;

    entry = Segdefs;
    for( ;; ) {
        if( entry == NULL ) {
           break;
        }
        Segdefs = entry->next;
        free( entry );
        entry = Segdefs;
    }
    Segdefs = NULL;
}


void AddGrpdef( unsigned_16 grpidx, unsigned_16 segidx ) {
/********************************************************/
    Grpdeflist  *entry;
    Grpdeflist  *wkentry;
    int         k;

    if( segidx == 0 ) { /* start new grpdef */
        entry = malloc( sizeof(Grpdeflist) );
        if( entry == NULL ) {
            OutputSetFH( stdout );
            Output( CRLF "**FATAL** Out of memory!" CRLF );
            leave( 21 );
        }
        entry->next = NULL;
        entry->grpind = grpidx;
        for( k = 0; k < MAXGRPSEGS; ++k ) {
            entry->segidx[ k ] = 0; /* no members yet */
        }
        if( Grpdefs == NULL ) {
            Grpdefs = entry;
        } else {
            wkentry = Grpdefs;
            for ( ;; ) {
                if( wkentry->next == NULL ) {
                    break;
                }
                wkentry = wkentry->next;
            }
            wkentry->next = entry;
        }
    } else {               /* add member to grp*/
        if( Grpdefs == NULL ) {
            OutputSetFH( stdout );
            Output( CRLF "**FATAL** No grpdef entry!" CRLF );
            leave( 21 );
        } else {
            wkentry = Grpdefs;
            for ( ;; ) {
                if( wkentry->next == NULL ) {
                    break;
                }
                wkentry = wkentry->next;
            }
            for( k = 0; k < MAXGRPSEGS; ++k ) {
                if( wkentry->segidx[ k ] == 0 ) {
                    break;
                }
            }
            if( k < MAXGRPSEGS ) {
                wkentry->segidx[ k ] = segidx;
            }

        }
    }
}

Grpdeflist *GetGrpdef( unsigned_16 idx ) {
/****************************************/
    Grpdeflist  *entry;
    int         k;

    if( (Grpdefs == NULL) || (idx == 0) ) {
        return( NULL );
    }
    entry = Grpdefs;
    for( k = 1; k < idx; ++k) {
        entry = entry->next;
        if( entry == NULL ) {
            return( NULL ); /* not found */
        }
    }
    return( entry );
}

unsigned_16 GetGrpseg( unsigned_16 idx ) {
/****************************************/
    Segdeflist  *entry;

    if( (Grpdefs == NULL) || (idx == 0) ) {
        return( 0 );
    }
    entry = GetSegdef( idx );
    if( entry == NULL ) {
        return( 0 );
    } else {
       return( entry->segind );
    }
}

static void FreeGrpdefs( void ) {
/*******************************/
    Grpdeflist   *entry;

    entry = Grpdefs;
    for( ;; ) {
        if( entry == NULL ) {
           break;
        }
        Grpdefs = entry->next;
        free( entry );
        entry = Grpdefs;
    }
    Grpdefs = NULL;
}


void ProcFile( FILE *fp, bool is_intel )
/**************************************/
{
    byte        cksum;
    byte        hdr[ 3 ];
    unsigned_16 page_len;
    unsigned_32 offset;
    const char  *recname;
    unsigned_32 total_padding;
    int         raw_dump;
    int         i;

    IsPharLap = FALSE;
    IsMS386 = FALSE;
    IsIntel = is_intel;
    RecNum = 0;
    page_len = 0;
    RecBuff = NULL;
    RecMaxLen = 0;
    total_padding = 0;
    Lnames = NULL;
    Xnames = NULL;
    Segdefs = NULL;
    Grpdefs = NULL;
    for(;;) {
        raw_dump = DumpRaw;
        offset = ftell( fp );
        if( fread( hdr, 1, 3, fp ) != 3 ) break;
        cksum  = hdr[ 0 ];
        cksum += hdr[ 1 ];
        cksum += hdr[ 2 ];
        RecLen = hdr[ 1 ] | ( hdr[ 2 ] << 8 );
        ResizeBuff( RecLen );
        RecPtr = RecBuff;
        if( fread( RecBuff, RecLen, 1, fp ) == 0 ) {
            break;
        }
        cksum += checkSumBuff();
        IsMS386 = hdr[ 0 ] & 1;
        if( IsMS386 ) {
            IsIntel = FALSE;
        }
        no_disp = ( rec_count == 0 ) ? FALSE : TRUE;
        for( i = 0; i < rec_count; i++ ) {
            if( rec_type[ i ] == ( hdr[ 0 ] & ~1 )) {
                no_disp = FALSE;
                break;
            }
        }
        recname = RecNumberToName( hdr[ 0 ] );
        cksum = -( cksum - RecBuff[ RecLen - 1 ] );
        Output( CRLF "%s%s(%2) recnum:%u, offset:%X, len:%x, chksum:%b(%2)" CRLF,
            recname, IsMS386 ? "386" : "", hdr[ 0 ], ++RecNum, offset,
            RecLen, RecBuff[ RecLen - 1 ], cksum );
        RecLen--;
        if( setjmp( BailOutJmp ) == 0 ) {
            switch( hdr[ 0 ] & ~1 ) {
            case CMD_RHEADR:
                ProcRHeadr();
                break;
            case CMD_ENDREC:
                ProcEndRec();
                break;
            case CMD_THEADR:
                ProcTHeadr();
                break;
            case CMD_LHEADR:
                ProcLHeadr();
                break;
            case CMD_COMENT:
                ProcComent();
                break;
            case CMD_MODEND:
                ProcModEnd();
                if( page_len != 0 ) {
                    offset = ftell( fp );
                    offset = page_len - offset % page_len;
                    if( offset != page_len ) {
                        total_padding += offset;
                        fseek( fp, offset, SEEK_CUR );
                    }
                }
                break;
            case CMD_STATIC_EXTDEF:
                /* fall through */
            case CMD_EXTDEF:
                ProcExtNames();
                break;
            case CMD_STATIC_PUBDEF:
                /* fall through */
            case CMD_PUBDEF:
                ProcPubDefs();
                break;
            case CMD_LOCSYM:
                ProcLocSyms();
                break;
            case CMD_LINNUM:
                ProcLinNums();
                break;
            case CMD_LLNAME:
                /* fall through */
            case CMD_LNAMES:
                ProcLNames( &Nameindex );
                break;
            case CMD_SEGDEF:
                ProcSegDefs();
                break;
            case CMD_GRPDEF:
                ProcGrpDef();
                break;
            case CMD_FIXUP:
                ProcFixup();
                break;
            case CMD_LEDATA:
                ProcLedata();
                break;
            case CMD_LIDATA:
                ProcLidata();
                break;
            case CMD_LIBNAM:
                ProcNames( &Libindex );
                break;
            case CMD_STATIC_COMDEF:
                /* fall through */
            case CMD_COMDEF:
                ProcComDef();
                break;
            case CMD_BAKPAT:
                ProcBackPat();
                break;
            case CMD_CEXTDF:
                ProcComExtDef();
                break;
            case CMD_COMDAT:
                ProcComDat();
                break;
            case CMD_LINSYM:
                ProcLineSym();
                break;
            case CMD_ALIAS:
                ProcAlias();
                break;
            case CMD_NBKPAT:
                ProcNameBackPat();
                break;
            case CMD_VERNUM:
                ProcVerNum();
                break;
            case CMD_VENDEXT:
                ProcVendExt();
                break;
            case LIB_HEADER_REC:
                if( hdr[ 0 ] & 1 ) {
                    /* LIB_TRAILER_REC */
                    ProcLibTrailer( fp );
                    fseek( fp, 0L, SEEK_END );
                    page_len = 0;
                } else {
                    page_len = RecLen + 4;
                    ProcLibHeader();
                }
                break;
            default:
                if( !raw_dump ) {
                    OutputData( 0L, 0L );
                }
                break;
            }
        } else {
            /* something bailed out... */
            if( raw_dump ) {
                Output( INDENT "Error at offset %x" CRLF,
                        (unsigned_32)RecOffset );
            } else if( !EndRec() ) {
                Output( INDENT "Remainder of record follows:" CRLF );
                OutputData( (unsigned_32)RecOffset(), 0L );
            } else {
                Output( INDENT "End of record" CRLF );
            }
        }
        if( raw_dump ) {
            RecPtr = RecBuff;
            Output(
"====================RAW DUMP==============================================="
                CRLF );
            OutputData( 0L, 0L );
            Output(
"====================RAW DUMP==============================================="
                CRLF );
        }
    }
    if( total_padding > 0 ) {
        Output( CRLF "total padding=%X" CRLF, total_padding );
    }
    if( TranslateIndex) {
        PrintNames();
    }
    FreeGrpdefs();
    FreeSegdefs();
    FreeLnames();
    FreeXnames();
    free( RecBuff );
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?