⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 skyeye.c

📁 这是Skyeye 0.9 版本的源代码
💻 C
字号:
/* 
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

/**
 * \author chenyu <yuchen@tsinghua.edu.cn>
 *  teawater <c7code-uc@yahoo.com.cn> add elf load function in 2005.08.30
 */

#include "armdefs.h"
#include "armemu.h"
#include <signal.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>


char *skyeye_config_filename = NULL;
extern int skyeye_net_on;
struct ARMul_State *state;
static int verbosity;
int big_endian;
static int mem_size = (1 << 21);
static FILE *pf;

int global_argc;
char **global_argv;
int stop_simulator = 0;

ARMword
ARMul_Debug (ARMul_State * state, ARMword pc, ARMword instr)
{
}

void
ARMul_ConsolePrint (ARMul_State * state, const char *format, ...)
{
}
void
ARMul_CallCheck (ARMul_State * state, ARMword cur_pc, ARMword to_pc,
		 ARMword instr)
{
}

//chy 2005-08-01, borrow from wlm's 2005-07-26's change
static void
base_termios_exit (void)
{
  tcsetattr (STDIN_FILENO, TCSANOW, &(state->base_termios));
}

//chy 2005-08-01 ---------------------------------------------    

void
init ()
{
  static int done;
  if (!done)
    {
      ARMul_EmulateInit ();
      state = ARMul_NewState ();
//chy 2005-08-01, borrow from wlm's 2005-07-26's change
      if (tcgetattr (STDIN_FILENO, &(state->base_termios)))
	{
#ifndef __CYGWIN__
	  //tcgetattr & tcsetattr can't be used with gdb in cygwin
	  printf ("SKYEYE: tcgetattr error\n");
	  exit (-1);
#endif
	}
      if (atexit (base_termios_exit))
	{
	  printf ("SKYEYE: atexit error.\n");
	  exit (-1);
	}
      //it will be set in  skyeye_config.mach->mach_init(state, skyeye_config.mach);
      state->abort_model = 0;
//chy 2005-08-01 ---------------------------------------------    

      state->bigendSig = (big_endian ? HIGH : LOW);
      ARMul_MemoryInit (state, mem_size);
      ARMul_OSInit (state);
      state->verbose = verbosity;
      done = 1;
      /*some option should init before read config. e.g. uart option. */
//chy 2005-08-01, borrow from wlm's 2005-07-26's change
      initialize_all_devices ();
//chy 2005-08-01 ---------------------------------------------    
      skyeye_option_init (&skyeye_config);
      skyeye_read_config ();

//chy 2005-08-01 commit and disable ksh's energy estimantion, will be recover in the future
      /*added by ksh for energy estimation,in 2004-11-26 */
      state->energy.energy_prof = skyeye_config.energy.energy_prof;
//chy 2005-08-01 ---------------------------------------------    


      //chy 2003-01-14 seems another ARMul_Reset, the first is in ARMul_NewState
      //ARMul_Reset(state);
      //chy 2003-08-19 mach_init should call ARMul_SelectProcess
      skyeye_config.mach->mach_init (state, skyeye_config.mach);
      //chy:2003-08-19, after mach_init, because ARMul_Reset should after ARMul_SelectProcess
      ARMul_Reset (state);

      // add step disassemble code here :teawater
      state->disassemble = skyeye_config.can_step_disassemble;

      io_reset (state);		/*from ARMul_Reset. */
    }
}


void
sim_resume (int step)
{
  state->EndCondition = 0;
  stop_simulator = 0;

  if (step)
    {
      state->Reg[15] = ARMul_DoInstr (state);
//chy 2005-07-29, disable teawater for next_dis, future......
#if 0
//teawater add for next_dis 2004.10.27------------------------------------------
      if (state->disassemble)
	{
#ifdef MODET
	  if (TFLAG)
	    {
	      tea_print_insn (state, state->Reg[15], 1);
	    }
	  else
	    {
	      tea_print_insn (state, state->Reg[15], 0);
	    }
#else
	  tea_print_insn (state, state->Reg[15], 0);
#endif //MODET
	}
      //AJ2D------------------------------------------------------
#endif //chy---------------------------
      if (state->EndCondition == 0)
	{
	  //chy 20050729 ????
	  printf ("error in sim_resume for state->EndCondition");
	  exit (-1);
	  //state->EndCondition = RDIError_BreakpointReached;
	  //chy--------------------------------
	}
    }
  else
    {
#if 1				/* JGS */
      state->NextInstr = RESUME;	/* treat as PC change */
#endif
      state->Reg[15] = ARMul_DoProg (state);

//chy 2005-07-29, disable teawater for next_dis, future......
#if 0
//teawater add check thumb 2005.07.21-------------------------------------------
      if (state->trap == TRAP_UNPREDICTABLE)
	{
	  printf ("\nGet a unpredictable. Dbct don't support thumb now.\n");
	  printf ("addr:0x%08x  ", state->Reg[15]);
#ifdef MODET
	  if (TFLAG)
	    {
	      tea_print_insn (state, state->Reg[15], 1);
	    }
	  else
	    {
	      tea_print_insn (state, state->Reg[15], 0);
	    }
#else
	  tea_print_insn (state, state->Reg[15], 0);
#endif //MODET
	}
//AJ2D--------------------------------------------------------------------------
#endif //chy--------------------------------------------------------------------
    }

  FLUSHPIPE;
}

//teawater add for load elf 2005.07.31------------------------------------------
#include <bfd.h>

static inline void
tea_write (uint32_t addr, uint8_t * buffer, int size)
{
  int i;

  for (i = 0; i < size; i++)
    {
      ARMul_WriteByte (state, addr + i, buffer[i]);
    }
}

static int
tea_load_exec (const char *file)
{
  int ret = -1;
  bfd *tmp_bfd = NULL;
  asection *s;
  char *tmp_str = NULL;

  //open
  tmp_bfd = bfd_openr (file, NULL);
  if (tmp_bfd == NULL)
    {
      fprintf (stderr, "open %s error: %s\n", file,
	       bfd_errmsg (bfd_get_error ()));
      goto out;
    }
  if (!bfd_check_format (tmp_bfd, bfd_object))
    {
      /*FIXME:In freebsd, if bfd_errno is bfd_error_file_ambiguously_recognized,
       * though bfd can't recognize this format, we should try to load file.*/
      if (bfd_get_error() != bfd_error_file_ambiguously_recognized) {
      	fprintf (stderr, "check format of %s error: %s\n", file,
		bfd_errmsg (bfd_get_error ()));
        goto out;
      }
    }
  printf ("exec file \"%s\"'s format is %s.\n", file, tmp_bfd->xvec->name);

  //load
  for (s = tmp_bfd->sections; s; s = s->next)
    {
      if (bfd_get_section_flags (tmp_bfd, s) & (SEC_LOAD))
	{
	  printf ("load section %s: addr = 0x%08x  size = 0x%08x.\n",
		  bfd_section_name (tmp_bfd, s),
		  (unsigned int) bfd_section_vma (tmp_bfd, s),
		  (unsigned int) bfd_section_size (tmp_bfd, s));
	  if (bfd_section_size (tmp_bfd, s) > 0)
	    {
	      tmp_str = (char *) malloc (bfd_section_size (tmp_bfd, s));
	      if (!tmp_str)
		{
		  fprintf (stderr, "alloc memory to load session %s error.\n",
			   bfd_section_name (tmp_bfd, s));
		  goto out;
		}
	      if (!bfd_get_section_contents
		  (tmp_bfd, s, tmp_str, 0, bfd_section_size (tmp_bfd, s)))
		{
		  fprintf (stderr, "get session %s content error: %s\n",
			   bfd_section_name (tmp_bfd, s),
			   bfd_errmsg (bfd_get_error ()));
		  goto out;
		}
	      tea_write (bfd_section_vma (tmp_bfd, s), tmp_str,
			 bfd_section_size (tmp_bfd, s));
	      free (tmp_str);
	      tmp_str = NULL;
	    }
	}
      else
	{
	  printf ("not load section %s: addr = 0x%08x  size = 0x%08x .\n",
		  bfd_section_name (tmp_bfd, s),
		  (unsigned int) bfd_section_vma (tmp_bfd, s),
		  (unsigned int) bfd_section_size (tmp_bfd, s));
	}
    }

  //set strat address
  if (skyeye_config.start_address == 0)
    {
      skyeye_config.start_address = bfd_get_start_address (tmp_bfd);
      printf ("start addr is set to 0x%08x by exec file.\n",
	      (unsigned int) skyeye_config.start_address);
    }

  ret = 0;
out:
  if (tmp_str)
    free (tmp_str);
  if (tmp_bfd)
    bfd_close (tmp_bfd);
  return (ret);
}

//AJ2D--------------------------------------------------------------------------

static void
usage ()
{
  fprintf (stderr, "------------------------- SkyEye -V1.0 ---------------------------\n");
  fprintf (stderr, "Usage: SkyEye [options] program [program args]\n");
  fprintf (stderr, "Default mode is STANDALONE mode\n");
  fprintf (stderr, "------------------------------------------------------------------\n");
  fprintf (stderr, "Options:\n");
//teawater add for load elf 2005.07.31------------------------------------------
  fprintf (stderr, "-e exec-file        the (ELF executable format)kernel file name.\n");
//AJ2D--------------------------------------------------------------------------
  fprintf (stderr,
	   "-d                  in GDB Server mode (can be connected by GDB).\n");
  fprintf (stderr, "-c config-file      the skyeye configure file name.\n");
  fprintf (stderr, "-h                  This Help Display\n");
  fprintf (stderr, "------------------------------------------------------------------\n");
}


int
main (int argc, char **argv)
{
  int c;
  int index;
  int debugmode = 0;
  opterr = 0;
//teawater add for load elf 2005.07.31------------------------------------------
  char *exec_file = NULL;

  while ((c = getopt (argc, argv, "e:dc:h")) != -1)
//AJ2D--------------------------------------------------------------------------
    switch (c)
      {
//teawater add for load elf 2005.07.31------------------------------------------
      case 'e':
	exec_file = optarg;
	break;
//AJ2D--------------------------------------------------------------------------
      case 'd':
	debugmode = 1;
	break;
      case 'h':
	usage ();
	exit (0);
      case 'c':
	skyeye_config_filename = optarg;
	break;
      case '?':
	if (isprint (optopt))
	  fprintf (stderr, "Unknown option `-%c'.\n", optopt);
	else
	  fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
	return 1;
      default:
	abort ();
      }
  usage();
  printf ("debugmode= %d, filename = %s, server TCP port is 12345\n",
	  debugmode, skyeye_config_filename);

  for (index = optind; index < argc; index++)
    printf ("Non-option argument %s\n", argv[index]);


  init ();
//teawater add for load elf 2005.07.31------------------------------------------
  if (exec_file)
    {
      if (tea_load_exec (exec_file))
	{
	  fprintf (stderr, "load \"%s\" error\n", exec_file);
	}
    }
//AJ2D--------------------------------------------------------------------------
  state->NextInstr = 0;
  state->Emulate = 3;
  if (skyeye_config.start_address != 0)
    ARMul_SetPC (state, skyeye_config.start_address);

  if (debugmode == 0)
    sim_resume (0);
  else
    sim_debug ();

  return 0;
}

⌨️ 快捷键说明

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