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

📄 load.c

📁 ARM7的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
#include "sim.h"#include "bfd.h"#include "dis-asm.h"static char *default_target = "elf32-littlearm";	/* Default at runtime.  *//* Architecture to disassemble for, or default if NULL.  */static char *machine = "arm";/* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN.  */static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;static bfd_boolean disassemble_all;	/* -D *//* Target specific options to the disassembler.  */static char *disassembler_options = (char *) NULL;void parse_args PARAMS ((int, char **));static int dump_reloc_info;		/* -r */int exit_status = 0;static char *only = 0;			/* -j secname */#ifndef SKIP_ZEROES#define SKIP_ZEROES (8)#endif#ifndef SKIP_ZEROES_AT_END#define SKIP_ZEROES_AT_END (3)#endifstatic bfd_vma adjust_section_vma = 0;	/* --adjust-vma */static int with_line_numbers;		/* -l */static bfd_boolean with_source_code;	/* -S */static int show_raw_insn;		/* --show-raw-insn *//* Pseudo FILE object for strings.  */typedef struct{  char *buffer;  size_t size;  char *current;} SFILE;static intobjdump_sprintf VPARAMS ((SFILE *f, const char *format, ...)){  char *buf;  size_t n;  VA_OPEN (args, format);  VA_FIXEDARG (args, SFILE *, f);  VA_FIXEDARG (args, const char *, format);  vasprintf (&buf, format, args);  if (buf == NULL)    {      va_end (args);      fatal ("Out of virtual memory");    }  n = strlen (buf);  while ((size_t) ((f->buffer + f->size) - f->current) < n + 1)    {      size_t curroff;      curroff = f->current - f->buffer;      f->size *= 2;      f->buffer = xrealloc (f->buffer, f->size);      f->current = f->buffer + curroff;    }  memcpy (f->current, buf, n);  f->current += n;  f->current[0] = '\0';  free (buf);  VA_CLOSE (args);  return n;}/* Should perhaps share code and display with nm?  */static int disassemble_zeroes;		/* --disassemble-zeroes *//* The dynamic symbol table.  */static asymbol **dynsyms;/* Number of symbols in `dynsyms'.  */static long dynsymcount = 0;/* The symbol table.  */static asymbol **syms;/* Number of symbols in `syms'.  */static long symcount = 0;/* The sorted symbol table.  */static asymbol **sorted_syms;/* Number of symbols in `sorted_syms'.  */static long sorted_symcount = 0;/* Extra info to pass to the disassembler address printing function.  */struct objdump_disasm_info{  bfd *abfd;  asection *sec;  bfd_boolean require_sec;};/* Hold the last function name and the last line number we displayed   in a disassembly.  */static char *prev_functionname;static unsigned int prev_line;/* We keep a list of all files that we have seen when doing a   dissassembly with source, so that we know how much of the file to   display.  This can be important for inlined functions.  */struct print_file_list{  struct print_file_list *next;  char *filename;  unsigned int line;  FILE *f;};intmain (argc, argv)     int argc;     char **argv;{  char *target = default_target;  bfd_boolean seenflag = FALSE;  //parse_args(argc,argv);  bfd_init ();  //set_default_bfd_target ();  read_exe (argv[1], target);   return exit_status;}static bfd_vma start_address = (bfd_vma) -1; /* --start-address */static bfd_vma stop_address = (bfd_vma) -1;  /* --stop-address */static int wide_output;			/* -w */static int prefix_addresses;		/* --prefix-addresses *//* Print the name of a symbol.  */static voidobjdump_print_symname (abfd, info, sym)     bfd *abfd;     struct disassemble_info *info;     asymbol *sym;{  char *alloc;  const char *name;  alloc = NULL;  name = bfd_asymbol_name (sym);  //if (do_demangle && name[0] != '\0')  //  {      /* Demangle the name.  */  //    alloc = demangle (abfd, name);  //    name = alloc;  //  }  if (info != NULL)    (*info->fprintf_func) (info->stream, "%s", name);  else    printf ("%s", name);  if (alloc != NULL)    free (alloc);}/* Locate a symbol given a bfd, a section, and a VMA.  If REQUIRE_SEC   is TRUE, then always require the symbol to be in the section.  This   returns NULL if there is no suitable symbol.  If PLACE is not NULL,   then *PLACE is set to the index of the symbol in sorted_syms.  */static asymbol *find_symbol_for_address (abfd, sec, vma, require_sec, place)     bfd *abfd;     asection *sec;     bfd_vma vma;     bfd_boolean require_sec;     long *place;{  /* @@ Would it speed things up to cache the last two symbols returned,     and maybe their address ranges?  For many processors, only one memory     operand can be present at a time, so the 2-entry cache wouldn't be     constantly churned by code doing heavy memory accesses.  */  /* Indices in `sorted_syms'.  */  long min = 0;  long max = sorted_symcount;  long thisplace;  unsigned int opb = bfd_octets_per_byte (abfd);  if (sorted_symcount < 1)    return NULL;  /* Perform a binary search looking for the closest symbol to the     required value.  We are searching the range (min, max].  */  while (min + 1 < max)    {      asymbol *sym;      thisplace = (max + min) / 2;      sym = sorted_syms[thisplace];      if (bfd_asymbol_value (sym) > vma)	max = thisplace;      else if (bfd_asymbol_value (sym) < vma)	min = thisplace;      else	{	  min = thisplace;	  break;	}    }  /* The symbol we want is now in min, the low end of the range we     were searching.  If there are several symbols with the same     value, we want the first one.  */  thisplace = min;  while (thisplace > 0	 && (bfd_asymbol_value (sorted_syms[thisplace])	     == bfd_asymbol_value (sorted_syms[thisplace - 1])))    --thisplace;  /* If the file is relocateable, and the symbol could be from this     section, prefer a symbol from this section over symbols from     others, even if the other symbol's value might be closer.     Note that this may be wrong for some symbol references if the     sections have overlapping memory ranges, but in that case there's     no way to tell what's desired without looking at the relocation     table.  */  if (sorted_syms[thisplace]->section != sec      && (require_sec	  || ((abfd->flags & HAS_RELOC) != 0	      && vma >= bfd_get_section_vma (abfd, sec)	      && vma < (bfd_get_section_vma (abfd, sec)			+ bfd_section_size (abfd, sec) / opb))))    {      long i;      for (i = thisplace + 1; i < sorted_symcount; i++)	{	  if (bfd_asymbol_value (sorted_syms[i])	      != bfd_asymbol_value (sorted_syms[thisplace]))	    break;	}      --i;      for (; i >= 0; i--)	{	  if (sorted_syms[i]->section == sec	      && (i == 0		  || sorted_syms[i - 1]->section != sec		  || (bfd_asymbol_value (sorted_syms[i])		      != bfd_asymbol_value (sorted_syms[i - 1]))))	    {	      thisplace = i;	      break;	    }	}      if (sorted_syms[thisplace]->section != sec)	{	  /* We didn't find a good symbol with a smaller value.	     Look for one with a larger value.  */	  for (i = thisplace + 1; i < sorted_symcount; i++)	    {	      if (sorted_syms[i]->section == sec)		{		  thisplace = i;		  break;		}	    }	}      if (sorted_syms[thisplace]->section != sec	  && (require_sec	      || ((abfd->flags & HAS_RELOC) != 0		  && vma >= bfd_get_section_vma (abfd, sec)		  && vma < (bfd_get_section_vma (abfd, sec)			    + bfd_section_size (abfd, sec)))))	{	  /* There is no suitable symbol.  */	  return NULL;	}    }  if (place != NULL)    *place = thisplace;  return sorted_syms[thisplace];}/* Sort relocs into address order.  */static intcompare_relocs (ap, bp)     const PTR ap;     const PTR bp;{  const arelent *a = *(const arelent **)ap;  const arelent *b = *(const arelent **)bp;  if (a->address > b->address)    return 1;  else if (a->address < b->address)    return -1;  /* So that associated relocations tied to the same address show up     in the correct order, we don't do any further sorting.  */  if (a > b)    return 1;  else if (a < b)    return -1;  else    return 0;}/* Sort symbols into value order.  */static intcompare_symbols (ap, bp)     const PTR ap;     const PTR bp;{  const asymbol *a = *(const asymbol **)ap;  const asymbol *b = *(const asymbol **)bp;  const char *an, *bn;  size_t anl, bnl;  bfd_boolean af, bf;  flagword aflags, bflags;  if (bfd_asymbol_value (a) > bfd_asymbol_value (b))    return 1;  else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))    return -1;  if (a->section > b->section)    return 1;  else if (a->section < b->section)    return -1;  an = bfd_asymbol_name (a);  bn = bfd_asymbol_name (b);  anl = strlen (an);  bnl = strlen (bn);  /* The symbols gnu_compiled and gcc2_compiled convey no real     information, so put them after other symbols with the same value.  */  af = (strstr (an, "gnu_compiled") != NULL	|| strstr (an, "gcc2_compiled") != NULL);  bf = (strstr (bn, "gnu_compiled") != NULL	|| strstr (bn, "gcc2_compiled") != NULL);  if (af && ! bf)    return 1;  if (! af && bf)    return -1;  /* We use a heuristic for the file name, to try to sort it after     more useful symbols.  It may not work on non Unix systems, but it     doesn't really matter; the only difference is precisely which     symbol names get printed.  */#define file_symbol(s, sn, snl)			\  (((s)->flags & BSF_FILE) != 0			\   || ((sn)[(snl) - 2] == '.'			\       && ((sn)[(snl) - 1] == 'o'		\	   || (sn)[(snl) - 1] == 'a')))  af = file_symbol (a, an, anl);  bf = file_symbol (b, bn, bnl);  if (af && ! bf)    return 1;  if (! af && bf)    return -1;  /* Try to sort global symbols before local symbols before function     symbols before debugging symbols.  */  aflags = a->flags;  bflags = b->flags;  if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))    {      if ((aflags & BSF_DEBUGGING) != 0)	return 1;      else	return -1;    }  if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))    {      if ((aflags & BSF_FUNCTION) != 0)	return -1;      else	return 1;    }  if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))    {      if ((aflags & BSF_LOCAL) != 0)	return 1;      else	return -1;    }  if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))    {      if ((aflags & BSF_GLOBAL) != 0)	return -1;      else	return 1;    }  /* Symbols that start with '.' might be section names, so sort them     after symbols that don't start with '.'.  */  if (an[0] == '.' && bn[0] != '.')    return 1;  if (an[0] != '.' && bn[0] == '.')    return -1;  /* Finally, if we can't distinguish them in any other way, try to     get consistent results by sorting the symbols by name.  */  return strcmp (an, bn);}/* Print VMA to STREAM.  If SKIP_ZEROES is TRUE, omit leading zeroes.  */static voidobjdump_print_value (vma, info, skip_zeroes)     bfd_vma vma;     struct disassemble_info *info;     bfd_boolean skip_zeroes;{  char buf[30];  char *p;  struct objdump_disasm_info *aux    = (struct objdump_disasm_info *) info->application_data;  bfd_sprintf_vma (aux->abfd, buf, vma);  if (! skip_zeroes)    p = buf;  else    {      for (p = buf; *p == '0'; ++p)	;      if (*p == '\0')	--p;    }  (*info->fprintf_func) (info->stream, "%s", p);}/* Print an address to INFO symbolically.  */static voidobjdump_print_addr_with_sym (abfd, sec, sym, vma, info, skip_zeroes)     bfd *abfd;     asection *sec;     asymbol *sym;     bfd_vma vma;     struct disassemble_info *info;     bfd_boolean skip_zeroes;{  objdump_print_value (vma, info, skip_zeroes);  if (sym == NULL)    {      bfd_vma secaddr;      (*info->fprintf_func) (info->stream, " <%s",			     bfd_get_section_name (abfd, sec));      secaddr = bfd_get_section_vma (abfd, sec);      if (vma < secaddr)	{	  (*info->fprintf_func) (info->stream, "-0x");	  objdump_print_value (secaddr - vma, info, TRUE);	}      else if (vma > secaddr)	{	  (*info->fprintf_func) (info->stream, "+0x");	  objdump_print_value (vma - secaddr, info, TRUE);	}      (*info->fprintf_func) (info->stream, ">");    }  else    {      (*info->fprintf_func) (info->stream, " <");      objdump_print_symname (abfd, info, sym);      if (bfd_asymbol_value (sym) > vma)	{	  (*info->fprintf_func) (info->stream, "-0x");	  objdump_print_value (bfd_asymbol_value (sym) - vma, info, TRUE);	}      else if (vma > bfd_asymbol_value (sym))	{	  (*info->fprintf_func) (info->stream, "+0x");	  objdump_print_value (vma - bfd_asymbol_value (sym), info, TRUE);	}      (*info->fprintf_func) (info->stream, ">");    }}/* Print VMA to INFO, symbolically if possible.  If SKIP_ZEROES is   TRUE, don't output leading zeroes.  */static voidobjdump_print_addr (vma, info, skip_zeroes)     bfd_vma vma;     struct disassemble_info *info;     bfd_boolean skip_zeroes;{  struct objdump_disasm_info *aux;  asymbol *sym;  if (sorted_symcount < 1)    {      (*info->fprintf_func) (info->stream, "0x");      objdump_print_value (vma, info, skip_zeroes);      return;    }  aux = (struct objdump_disasm_info *) info->application_data;  sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec,				 (long *) NULL);  objdump_print_addr_with_sym (aux->abfd, aux->sec, sym, vma, info,			       skip_zeroes);}/* Print VMA to INFO.  This function is passed to the disassembler   routine.  */static voidobjdump_print_address (vma, info)     bfd_vma vma;     struct disassemble_info *info;{  objdump_print_addr (vma, info, ! prefix_addresses);}/* Determine of the given address has a symbol associated with it.  */static intobjdump_symbol_at_address (vma, info)     bfd_vma vma;     struct disassemble_info * info;{  struct objdump_disasm_info * aux;  asymbol * sym;  /* No symbols - do not bother checking.  */  if (sorted_symcount < 1)    return 0;  aux = (struct objdump_disasm_info *) info->application_data;  sym = find_symbol_for_address (aux->abfd, aux->sec, vma, aux->require_sec,				 (long *) NULL);  return (sym != NULL && (bfd_asymbol_value (sym) == vma));}static struct print_file_list *print_files;/* Filter out (in place) symbols that are useless for disassembly.   COUNT is the number of elements in SYMBOLS.   Return the number of useful symbols.  */static longremove_useless_symbols (symbols, count)     asymbol **symbols;     long count;{  register asymbol **in_ptr = symbols, **out_ptr = symbols;  while (--count >= 0)    {      asymbol *sym = *in_ptr++;      if (sym->name == NULL || sym->name[0] == '\0')	continue;      if (sym->flags & (BSF_DEBUGGING))	continue;      if (bfd_is_und_section (sym->section)	  || bfd_is_com_section (sym->section))	continue;      *out_ptr++ = sym;    }  return out_ptr - symbols;}static voidskip_to_line (p, line, show)     struct print_file_list *p;     unsigned int line;     bfd_boolean show;{  while (p->line < line)    {      char buf[100];      if (fgets (buf, sizeof buf, p->f) == NULL)	{	  fclose (p->f);	  p->f = NULL;	  break;	}      if (show)	printf ("%s", buf);      if (strchr (buf, '\n') != NULL)	++p->line;    }}

⌨️ 快捷键说明

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