binemul.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* Binutils emulation layer.
  2. Copyright (C) 2002-2022 Free Software Foundation, Inc.
  3. Written by Tom Rix, Red Hat Inc.
  4. This file is part of GNU Binutils.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "binemul.h"
  18. extern bin_emulation_xfer_type bin_dummy_emulation;
  19. void
  20. ar_emul_usage (FILE *fp)
  21. {
  22. if (bin_dummy_emulation.ar_usage)
  23. bin_dummy_emulation.ar_usage (fp);
  24. }
  25. void
  26. ar_emul_default_usage (FILE *fp)
  27. {
  28. AR_EMUL_USAGE_PRINT_OPTION_HEADER (fp);
  29. /* xgettext:c-format */
  30. fprintf (fp, _(" No emulation specific options\n"));
  31. }
  32. bool
  33. ar_emul_append (bfd **after_bfd, char *file_name, const char *target,
  34. bool verbose, bool flatten)
  35. {
  36. bfd *new_bfd;
  37. new_bfd = bfd_openr (file_name, target);
  38. AR_EMUL_ELEMENT_CHECK (new_bfd, file_name);
  39. if (bin_dummy_emulation.ar_append)
  40. return bin_dummy_emulation.ar_append (after_bfd, new_bfd,
  41. verbose, flatten);
  42. return false;
  43. }
  44. bool
  45. ar_emul_append_bfd (bfd **after_bfd, bfd *new_bfd,
  46. bool verbose, bool flatten)
  47. {
  48. if (bin_dummy_emulation.ar_append)
  49. return bin_dummy_emulation.ar_append (after_bfd, new_bfd,
  50. verbose, flatten);
  51. return false;
  52. }
  53. static bool
  54. any_ok (bfd *new_bfd ATTRIBUTE_UNUSED)
  55. {
  56. return true;
  57. }
  58. bool
  59. do_ar_emul_append (bfd **after_bfd, bfd *new_bfd,
  60. bool verbose, bool flatten,
  61. bool (*check) (bfd *))
  62. {
  63. /* When flattening, add the members of an archive instead of the
  64. archive itself. */
  65. if (flatten && bfd_check_format (new_bfd, bfd_archive))
  66. {
  67. bfd *elt;
  68. bool added = false;
  69. for (elt = bfd_openr_next_archived_file (new_bfd, NULL);
  70. elt;
  71. elt = bfd_openr_next_archived_file (new_bfd, elt))
  72. {
  73. if (do_ar_emul_append (after_bfd, elt, verbose, true, check))
  74. {
  75. added = true;
  76. after_bfd = &((*after_bfd)->archive_next);
  77. }
  78. }
  79. return added;
  80. }
  81. if (!check (new_bfd))
  82. return false;
  83. AR_EMUL_APPEND_PRINT_VERBOSE (verbose, bfd_get_filename (new_bfd));
  84. new_bfd->archive_next = *after_bfd;
  85. *after_bfd = new_bfd;
  86. return true;
  87. }
  88. bool
  89. ar_emul_default_append (bfd **after_bfd, bfd *new_bfd,
  90. bool verbose, bool flatten)
  91. {
  92. return do_ar_emul_append (after_bfd, new_bfd, verbose, flatten, any_ok);
  93. }
  94. bool
  95. ar_emul_replace (bfd **after_bfd, char *file_name, const char *target,
  96. bool verbose)
  97. {
  98. bfd *new_bfd;
  99. new_bfd = bfd_openr (file_name, target);
  100. AR_EMUL_ELEMENT_CHECK (new_bfd, file_name);
  101. if (bin_dummy_emulation.ar_replace)
  102. return bin_dummy_emulation.ar_replace (after_bfd, new_bfd,
  103. verbose);
  104. return false;
  105. }
  106. bool
  107. ar_emul_replace_bfd (bfd **after_bfd, bfd *new_bfd,
  108. bool verbose)
  109. {
  110. if (bin_dummy_emulation.ar_replace)
  111. return bin_dummy_emulation.ar_replace (after_bfd, new_bfd,
  112. verbose);
  113. return false;
  114. }
  115. bool
  116. ar_emul_default_replace (bfd **after_bfd, bfd *new_bfd,
  117. bool verbose)
  118. {
  119. AR_EMUL_REPLACE_PRINT_VERBOSE (verbose, bfd_get_filename (new_bfd));
  120. new_bfd->archive_next = *after_bfd;
  121. *after_bfd = new_bfd;
  122. return true;
  123. }
  124. bool
  125. ar_emul_parse_arg (char *arg)
  126. {
  127. if (bin_dummy_emulation.ar_parse_arg)
  128. return bin_dummy_emulation.ar_parse_arg (arg);
  129. return false;
  130. }
  131. bool
  132. ar_emul_default_parse_arg (char *arg ATTRIBUTE_UNUSED)
  133. {
  134. return false;
  135. }