output-file.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* output-file.c - Deal with the output file
  2. Copyright (C) 1987-2022 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS 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, or (at your option)
  7. any later version.
  8. GAS 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 GAS; see the file COPYING. If not, write to
  14. the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "as.h"
  17. #include "output-file.h"
  18. #ifndef TARGET_MACH
  19. #define TARGET_MACH 0
  20. #endif
  21. bfd *stdoutput;
  22. void
  23. output_file_create (const char *name)
  24. {
  25. if (name[0] == '-' && name[1] == '\0')
  26. as_fatal (_("can't open a bfd on stdout %s"), name);
  27. else if (!(stdoutput = bfd_openw (name, TARGET_FORMAT)))
  28. {
  29. bfd_error_type err = bfd_get_error ();
  30. if (err == bfd_error_invalid_target)
  31. as_fatal (_("selected target format '%s' unknown"), TARGET_FORMAT);
  32. else
  33. as_fatal (_("can't create %s: %s"), name, bfd_errmsg (err));
  34. }
  35. bfd_set_format (stdoutput, bfd_object);
  36. bfd_set_arch_mach (stdoutput, TARGET_ARCH, TARGET_MACH);
  37. if (flag_traditional_format)
  38. stdoutput->flags |= BFD_TRADITIONAL_FORMAT;
  39. }
  40. void
  41. output_file_close (const char *filename)
  42. {
  43. bool res;
  44. if (stdoutput == NULL)
  45. return;
  46. /* Close the bfd. */
  47. if (!flag_always_generate_output && had_errors ())
  48. res = bfd_cache_close_all ();
  49. else
  50. res = bfd_close (stdoutput);
  51. /* Prevent an infinite loop - if the close failed we will call as_fatal
  52. which will call xexit() which may call this function again... */
  53. stdoutput = NULL;
  54. if (! res)
  55. as_fatal ("%s: %s", filename, bfd_errmsg (bfd_get_error ()));
  56. }