示例#1
0
文件: bfd.c 项目: chrisglass/ufoai
static int init_bfd_ctx (struct bfd_ctx *bc, const char * procname, struct output_buffer *ob)
{
	bfd *b;
	void *symbol_table;
	unsigned dummy = 0;
	char **matching = NULL;

	bc->handle = NULL;
	bc->symbol = NULL;

	b = bfd_openr(procname, 0);
	if (!b) {
		output_print(ob, "Failed to open bfd from (%s)\n", procname);
		return 1;
	}

	if (bfd_check_format(b, bfd_archive)) {
		output_print(ob, "Cannot get addresses from archive (%s)\n", b->filename);
		bfd_close(b);
		return -1;
	}

	if (!bfd_check_format_matches(b, bfd_object, &matching)) {
		const char *errmsg = bfd_errmsg(bfd_get_error());
		output_print(ob, "%s (%s)\n", errmsg, b->filename);
		if (bfd_get_error() == bfd_error_file_ambiguously_recognized) {
			list_matching_formats(ob, b->filename, matching);
			free(matching);
		}
		bfd_close(b);
		return -1;
	}

	if ((bfd_get_file_flags(b) & HAS_SYMS) == 0) {
		const char *errmsg = bfd_errmsg(bfd_get_error());
		output_print(ob, "Failed to get file flags from (%s) %s\n", b->filename, errmsg);
		bfd_close(b);
		return 1;
	}

	if (bfd_read_minisymbols(b, FALSE, &symbol_table, &dummy) == 0) {
		if (bfd_read_minisymbols(b, TRUE, &symbol_table, &dummy) < 0) {
			const char *errmsg = bfd_errmsg(bfd_get_error());
			output_print(ob, "Failed to read symbols from (%s): %s\n", b->filename, errmsg);
			free(symbol_table);
			bfd_close(b);
			return 1;
		}
	}

	bc->handle = b;
	bc->symbol = symbol_table;

	return 0;
}
示例#2
0
int
main(int ac, char **av)
{
  bfd *abfd;
  struct coff_ofile *tree;
  char **matching;
  char *input_file = NULL;
  int opt;
  static struct option long_options[] =
    {
      { "help", no_argument, 0, 'h' },
      { "version", no_argument, 0, 'V' },
      { NULL, no_argument, 0, 0 }
    };

#if defined(HAVE_SETLOCALE) && defined(HAVE_LC_MESSAGES)
  setlocale(LC_MESSAGES, "");
#endif /* HAVE_SETLOCALE && HAVE_LC_MESSAGES */
#if defined(HAVE_SETLOCALE)
  setlocale(LC_CTYPE, "");
#endif /* HAVE_SETLOCALE */
  bindtextdomain(PACKAGE, LOCALEDIR);
  textdomain(PACKAGE);

  program_name = av[0];
  xmalloc_set_program_name (program_name);

  while ((opt = getopt_long(ac, av, "HhVv", long_options,
                            (int *)NULL)) != EOF)
    {
      switch (opt)
	{
	case 'H':
	case 'h':
	  show_usage(stdout, 0);
	  break;
	case 'v':
	case 'V':
	  print_version("coffdump");
	  xexit(0);
	case 0:
	  break;
	default:
	  show_usage(stderr, 1);
	  break;
	}
    }

  if (optind < ac)
    {
      input_file = av[optind];
    }

  if (!input_file)
    fatal(_("no input file specified"));

  abfd = bfd_openr(input_file, 0);

  if (!abfd)
    bfd_fatal(input_file);

  if (! bfd_check_format_matches(abfd, bfd_object, &matching))
    {
      bfd_nonfatal(input_file);

      if (bfd_get_error() == bfd_error_file_ambiguously_recognized)
	{
	  list_matching_formats(matching);
	  free(matching);
	}
      xexit(1);
    }

  tree = coff_grok(abfd);

  coff_dump(tree);
  printf("\n");

  return 0;
}