bfdtest2.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* A program to test BFD.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. This file is part of the GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. static void
  19. die (const char *s)
  20. {
  21. printf ("oops: %s\n", s);
  22. exit (1);
  23. }
  24. static void *
  25. iovec_open (struct bfd *nbfd ATTRIBUTE_UNUSED, void *open_closure)
  26. {
  27. return open_closure;
  28. }
  29. static file_ptr iovec_read (struct bfd *nbfd ATTRIBUTE_UNUSED,
  30. void *stream, void *buf, file_ptr nbytes,
  31. file_ptr offset)
  32. {
  33. FILE* file = (FILE*) stream;
  34. if (fseek(file, offset, SEEK_SET) != 0)
  35. die ("fseek error");
  36. return fread (buf, 1, nbytes, file);
  37. }
  38. static int
  39. iovec_stat (struct bfd *abfd ATTRIBUTE_UNUSED,
  40. void *stream, struct stat *sb)
  41. {
  42. return fstat (fileno ((FILE*) stream), sb);
  43. }
  44. static bool
  45. check_format_any (struct bfd *abfd, bfd_format format)
  46. {
  47. char** targets = NULL;
  48. if (bfd_check_format_matches (abfd, format, &targets))
  49. return true;
  50. if (targets)
  51. {
  52. bfd_find_target (targets[0], abfd);
  53. return bfd_check_format (abfd, format);
  54. }
  55. return false;
  56. }
  57. int
  58. main (int argc, const char** argv)
  59. {
  60. FILE* file;
  61. bfd *abfd, *mbfd;
  62. if (argc < 2)
  63. die ("Usage: test archivefile");
  64. file = fopen(argv[1], "rb");
  65. if (!file)
  66. die ("file not found");
  67. abfd = bfd_openr_iovec (argv[1], 0, iovec_open, file,
  68. iovec_read, NULL, iovec_stat);
  69. if (!abfd)
  70. die ("error opening file");
  71. if (!check_format_any (abfd, bfd_archive))
  72. die ("not an archive");
  73. mbfd = bfd_openr_next_archived_file (abfd, 0);
  74. if (!mbfd)
  75. die ("error opening archive member");
  76. if (!bfd_close (mbfd))
  77. die ("error closing archive member");
  78. if (!bfd_close (abfd))
  79. die ("error closing archive");
  80. return 0;
  81. }