Esempio n. 1
0
int main() {
  int retval;
  Memory_word one[5], two[5];
  
  retval= relocate_program(one, two, -5, 4);
  if (retval != 0) {
    printf("relocate_program() returned %d!\n", retval);
    printf("Test failed\n");
    return -1;
  }

  printf("Test finished!\n");
  return 0;
}
Esempio n. 2
0
TosProgram *load_tos_program( char *filename )
{
  TosProgram * prog;
  TosExecHeader *hdr;
  TosBasepage *bp;
  void *prg, *buf;
  ulong size;
  FILE *fp;
  char *env, *src, *dest;
  char ascii_num[ 4 ];

  /* Allocate a new program structure */
  prog = new_program ();

  /* Set up the MiNT emulation flag */
  DDEBUG( "Environment: %s\n", Opt_environment );
  prog->emulate_mint = Opt_MiNT;

  /* Set up GEM flag, if program name ends on .app or .prg
	 and Opt_GEM is not set to "no" */
  /* Use case insensitive compare */
  if(Opt_GEM && (!strncasecmp(filename+strlen(filename)-4, ".app", 4) ||
				 !strncasecmp(filename+strlen(filename)-4, ".prg", 4)))
	prog->gem = 1;
  else
	prog->gem = 0;
  
  /* Set up the environment */
  if( strcmp( Opt_environment, "-" ) == 0 ) {
	env = NULL;
  }
  else
  {
	env = (char *)mymalloc( strlen( Opt_environment ) + 2 );
	for( src = Opt_environment, dest = env ; *src ; src++ ) {
	  if( *src == '\\' ) {
		src++;
		if( *src >= '0' && *src <= '9' ) {
		  ascii_num[ 0 ] = *src++;
		  ascii_num[ 1 ] = *src++;
		  ascii_num[ 2 ] = *src;
		  ascii_num[ 3 ] = 0;
		  *dest++ = strtol( ascii_num, NULL, 8 );
		}
		else {
		  switch( *src ) {
		  case 'n':
			*dest++ = '\n';
			break;
		  case 'r':
			*dest++ = '\r';
			break;
		  default:
			*dest++ = *src;
			break;
		  }
		}
	  }
	  else if( *src == ',' ) {
		*dest++ = '\0';
	  }
	  else {
		*dest++ = *src;
	  }
	}
	*dest++ = 0;
	*dest++ = 0;
  }  

  /* Set up the trace flag */
  prog->trace = Opt_trace;

  if( (fp = fopen( filename, "r" )) == NULL ){
    fprintf( stderr, "Can't open %s: %s\n", filename, strerror(errno) );
    return NULL;
  }

  /* read the header */
  hdr = (TosExecHeader *)mymalloc( sizeof(TosExecHeader) );
  if (fread( hdr, sizeof(TosExecHeader), 1, fp ) != 1 ||
	  ntohs(hdr->magic) != TOS_PROGRAM_MAGIC) {
	fprintf( stderr, "%s: no GEMDOS executable\n", filename );
	free( hdr );
	fclose( fp );
	return NULL;
  }

  /* calculate size of TPA */
  size = sizeof(TosBasepage) + ntohl(hdr->tsize) + ntohl(hdr->dsize) +
    ntohl(hdr->bsize) + Opt_extra_mem*1024;
  buf = (void *)mymalloc( size );
  bp = (TosBasepage *)buf;
  prg = (void *)(bp + 1);

  /* read program text + data */
  if(fread(prg, sizeof(char), ntohl(hdr->tsize) + ntohl(hdr->dsize), fp ) !=
     ntohl(hdr->tsize) + ntohl(hdr->dsize))
  {
	  fprintf( stderr, "%s: short executable!\n", filename );
	err_ret:
	  free( buf );
	  free( hdr );
	  fclose( fp );
	  return NULL;
  }

  /* initialize basepage */
  bp->lowtpa = htonl((UInt32)bp);
  bp->hitpa = htonl((UInt32)buf + size);
  bp->tbase = htonl(TEXT_SEGMENT(hdr,prg));
  bp->tlen = hdr->tsize;
  bp->dbase = htonl(DATA_SEGMENT(hdr,prg));
  bp->dlen = hdr->dsize;
  bp->bbase = htonl(BSS_SEGMENT(hdr,prg));
  bp->blen = hdr->bsize;
  bp->parent = (Ptr32)NULL;
  bp->env = (char *)htonl((UInt32)env);
  bp->cmdlin[ 0 ] = 0;

  /*  patch_program( hdr ); */
  if (relocate_program( bp, hdr, fp ) < 0) {
	  fprintf( stderr, "%s: relocation failed\n", filename );
	  goto err_ret;
  }
  fclose( fp );
  
  /* clear BSS and maybe rest of TPA */
  memset((void *)BSS_SEGMENT(hdr,prg), 0,
         ntohl(hdr->bsize) +
         ((ntohl(hdr->prgflags) & TOS_PRGFLAG_FASTLOAD) ? 0 : Opt_extra_mem*1024) );

  prog->text = TEXT_SEGMENT(hdr,prg);
  prog->data = DATA_SEGMENT(hdr,prg);
  prog->bss = BSS_SEGMENT(hdr,prg);
  prog->size = size;

  prog->basepage = bp;
  prog->basepage->dta = (Dta *)ntohl((UInt32)prog->basepage->cmdlin);
  prog->dta = (Dta *)ntohl((UInt32)prog->basepage->dta);

  /* The program starts in user mode */
  prog->super = 0;
  
  /* The program starts in the TOS domain */
  prog->domain = 0;

#ifdef DEBUG_STACK
  start_stack = BSS_SEGMENT(hdr,prg) + ntohl(hdr->bsize);
#endif

  free(hdr);
  
  return prog;
}