Coverage Report

Created: 2023-08-28 06:31

/src/binutils-gdb/gas/output-file.c
Line
Count
Source (jump to first uncovered line)
1
/* output-file.c -  Deal with the output file
2
   Copyright (C) 1987-2023 Free Software Foundation, Inc.
3
4
   This file is part of GAS, the GNU Assembler.
5
6
   GAS is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3, or (at your option)
9
   any later version.
10
11
   GAS is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with GAS; see the file COPYING.  If not, write to
18
   the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19
   02110-1301, USA.  */
20
21
#include "as.h"
22
#include "subsegs.h"
23
#include "sb.h"
24
#include "macro.h"
25
#include "output-file.h"
26
27
#ifndef TARGET_MACH
28
#define TARGET_MACH 0
29
#endif
30
31
bfd *stdoutput;
32
33
void
34
output_file_create (const char *name)
35
1.15k
{
36
1.15k
  if (name[0] == '-' && name[1] == '\0')
37
0
    as_fatal (_("can't open a bfd on stdout %s"), name);
38
39
1.15k
  else if (!(stdoutput = bfd_openw (name, TARGET_FORMAT)))
40
0
    {
41
0
      bfd_error_type err = bfd_get_error ();
42
43
0
      if (err == bfd_error_invalid_target)
44
0
  as_fatal (_("selected target format '%s' unknown"), TARGET_FORMAT);
45
0
      else
46
0
  as_fatal (_("can't create %s: %s"), name, bfd_errmsg (err));
47
0
    }
48
49
1.15k
  bfd_set_format (stdoutput, bfd_object);
50
1.15k
  bfd_set_arch_mach (stdoutput, TARGET_ARCH, TARGET_MACH);
51
1.15k
  if (flag_traditional_format)
52
0
    stdoutput->flags |= BFD_TRADITIONAL_FORMAT;
53
1.15k
}
54
55
static void
56
stash_frchain_obs (asection *sec)
57
15.3k
{
58
15.3k
  segment_info_type *info = seg_info (sec);
59
15.3k
  if (info)
60
15.3k
    {
61
15.3k
      struct frchain *frchp;
62
29.3k
      for (frchp = info->frchainP; frchp; frchp = frchp->frch_next)
63
13.9k
  obstack_ptr_grow (&notes, &frchp->frch_obstack);
64
15.3k
      info->frchainP = NULL;
65
15.3k
    }
66
15.3k
}
67
68
void
69
output_file_close (void)
70
1.15k
{
71
1.15k
  bool res;
72
1.15k
  bfd *obfd = stdoutput;
73
1.15k
  struct obstack **obs;
74
1.15k
  asection *sec;
75
1.15k
  const char *filename;
76
77
1.15k
  if (obfd == NULL)
78
0
    return;
79
80
  /* Prevent an infinite loop - if the close failed we will call as_fatal
81
     which will call xexit() which may call this function again...  */
82
1.15k
  stdoutput = NULL;
83
84
  /* We can't free obstacks attached to the output bfd sections before
85
     closing the output bfd since data in those obstacks may need to
86
     be accessed, but we can't access anything in the output bfd after
87
     it is closed..  */
88
11.8k
  for (sec = obfd->sections; sec; sec = sec->next)
89
10.7k
    stash_frchain_obs (sec);
90
1.15k
  stash_frchain_obs (reg_section);
91
1.15k
  stash_frchain_obs (expr_section);
92
1.15k
  stash_frchain_obs (bfd_abs_section_ptr);
93
1.15k
  stash_frchain_obs (bfd_und_section_ptr);
94
1.15k
  obstack_ptr_grow (&notes, NULL);
95
1.15k
  obs = obstack_finish (&notes);
96
97
  /* Close the bfd.  */
98
1.15k
  if (!flag_always_generate_output && had_errors ())
99
1.15k
    res = bfd_close_all_done (obfd);
100
0
  else
101
0
    res = bfd_close (obfd);
102
1.15k
  now_seg = NULL;
103
1.15k
  now_subseg = 0;
104
105
1.15k
  filename = out_file_name;
106
1.15k
  out_file_name = NULL;
107
1.15k
  if (!keep_it && filename)
108
1.15k
    unlink_if_ordinary (filename);
109
110
1.15k
#ifdef md_end
111
1.15k
  md_end ();
112
1.15k
#endif
113
1.15k
#ifdef obj_end
114
1.15k
  obj_end ();
115
1.15k
#endif
116
1.15k
  macro_end ();
117
1.15k
  expr_end ();
118
1.15k
  read_end ();
119
1.15k
  symbol_end ();
120
1.15k
  subsegs_end (obs);
121
122
1.15k
  if (!res)
123
0
    as_fatal ("%s: %s", filename, bfd_errmsg (bfd_get_error ()));
124
1.15k
}