DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T o

⟦9683ef9ee⟧ TextFile

    Length: 4110 (0x100e)
    Types: TextFile
    Names: »objfile.c«

Derivation

└─⟦9ae75bfbd⟧ Bits:30007242 EUUGD3: Starter Kit
    └─⟦3f75c1919⟧ »EurOpenD3/utils/decomp.tar.Z« 
        └─⟦510c4d5ee⟧ 
            └─⟦this⟧ »decomp/objfile.c« 

TextFile

/*
 * Module: objfile.c
 *
 * Author: J. Reuter
 *
 * This module contains code that reads the various parts of object
 * and a.out symbol tables.  These "parts" include the symbol table
 * proper, the "string" table that contains the symbol names, and
 * the relocation table, if it exists.
 */

#include "defs.h"
#include "main.h"
#include <sys/types.h>
#include <sys/stat.h>

char *strtab;

struct nlist *symtab;
int nsym;

struct relocation_info *rel;
int nrel;

char *datatab;
int ndata;
int datoff;

sym_read()
{
    struct stat statbuf;
    int stab_size;

    /* read the string table */

    fseek( objfile, N_STROFF( obj_header ), 0 );

    fstat( fileno( objfile ), &statbuf );

    stab_size = statbuf.st_size - N_STROFF( obj_header );

    strtab = (char *)malloc( stab_size );
    if ( strtab == NULL ) {
	fprintf( stderr, "Out of memory\n" );
	exit( 1 );
    }
    fread( strtab, 1, stab_size, objfile );

    /* read the symbol table proper */

    fseek( objfile, N_SYMOFF( obj_header ), 0 );
    nsym = obj_header.a_syms;
    symtab = (struct nlist *) malloc( nsym );
    if ( symtab == NULL ) {
	fprintf( stderr, "Out of memory\n" );
	exit( 1 );
    }
    fread( symtab, 1, nsym, objfile );

    /* read the relocaion table */

    fseek( objfile, N_TRLOFF( obj_header ), 0 );
    nrel = obj_header.a_trsize + obj_header.a_drsize;
    rel = (struct relocation_info *) malloc( nrel );
    if ( rel == NULL ) {
	fprintf( stderr, "Out of memory\n" );
	exit( 1 );
    }
    fread( rel, 1, nrel, objfile );

    /* read initialized data */

    datoff = N_DATOFF( obj_header );
    fseek( objfile, datoff, 0 );
    ndata = N_TRLOFF( obj_header) - datoff;
    datatab = malloc( ndata );
    if ( datatab == NULL ) {
	fprintf( stderr, "Out of memory\n" );
	exit( 1 );
    }
    fread( datatab, 1, ndata, objfile );
    datoff -= N_TXTOFF( obj_header ); /* offset by header size */
}

struct relocation_info *relo_cur;

relo_first()
{
    relo_cur = rel;
}

struct relocation_info *
relo_next()
{
    if ( relo_cur - rel >= nrel )
	return NULL;
    else
	return relo_cur++;
}

sym_find( addr )
address addr;
{
    int i;

    for ( i=0; i < nsym/sizeof(struct nlist); i++ )
	if ( (symtab+i)->n_value == addr )
	    return i;

    return -1;
}

char *
prname( name )
char *name;
{
    if ( name[0] == '_' )
	return name + 1;
    else
	return name;
}

#ifdef debug

symdump()
{
    register int i;
    register struct nlist *s;

    printf( "Name            Type Other   Desc      Value\n" );
    for ( i=0; i<nsym/sizeof(struct nlist); i++ ) {
	s = symtab+i;
	printf( "%-15s %3d  %4d  %6x  %8d\n",
	       &strtab[s->n_un.n_strx], s->n_type, s->n_other,
	       s->n_desc, s->n_value );
    }
}

reldump( start, count )
int start;
int count;
{
    int i;
    struct nlist *s;

    start /= sizeof( struct relocation_info );
    count /= sizeof( struct relocation_info );

    printf( " Address   Pcrel Length Extrn Stype  Sval  Sname\n" );
    for ( i=start; i < count+start; i++ ) {
	s = &symtab[(rel+i)->r_symbolnum];
	printf( "%8d %5d %6d  %3d    %3d %6d   %-30s\n",
	       (rel+i)->r_address,
	       (rel+i)->r_pcrel, (rel+i)->r_length, (rel+i)->r_extern,
	       s->n_type, s->n_value, &strtab[s->n_un.n_strx] );
    }
}
#endif debug

/*
 * The following routines allow reading of the code section of the
 * object file.
 */

static long current_pos = 0;

char
get_byte( addr )
address addr;
{
    char byte;

    if ( addr != current_pos ) {
	fseek( objfile, offset + addr, 0 );
	current_pos = addr;
    }

    fread( &byte, sizeof(byte), 1, objfile );
    current_pos += 1;
    return byte;
}

short
get_word( addr )
address addr;
{
    short word;

    if ( addr != current_pos ) {
	fseek( objfile, offset + addr, 0 );
	current_pos = addr;
    }

    fread( &word, sizeof(word), 1, objfile );
    current_pos += 2;
    return word;
}

long
get_long( addr )
address addr;
{
    long longword;

    if ( addr != current_pos ) {
	fseek( objfile, offset + addr, 0 );
	current_pos = addr;
    }

    fread( &longword, sizeof(longword), 1, objfile );
    current_pos += 4;
    return longword;
}