1/* subsegs.c - subsegments -
2   Copyright (C) 1987-2020 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 the Free
18   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19   02110-1301, USA.  */
20
21/* Segments & sub-segments.  */
22
23#include "as.h"
24
25#include "subsegs.h"
26#include "obstack.h"
27
28frchainS *frchain_now;
29
30static struct obstack frchains;
31
32static fragS dummy_frag;
33
34
35void
36subsegs_begin (void)
37{
38  obstack_begin (&frchains, chunksize);
39#if __GNUC__ >= 2
40  obstack_alignment_mask (&frchains) = __alignof__ (frchainS) - 1;
41#endif
42
43  frchain_now = NULL;		/* Warn new_subseg() that we are booting.  */
44  frag_now = &dummy_frag;
45}
46
47/*
48 *			subseg_change()
49 *
50 * Change the subsegment we are in, BUT DO NOT MAKE A NEW FRAG for the
51 * subsegment. If we are already in the correct subsegment, change nothing.
52 * This is used eg as a worker for subseg_set [which does make a new frag_now]
53 * and for changing segments after we have read the source. We construct eg
54 * fixSs even after the source file is read, so we do have to keep the
55 * segment context correct.
56 */
57void
58subseg_change (segT seg, int subseg)
59{
60  segment_info_type *seginfo = seg_info (seg);
61  now_seg = seg;
62  now_subseg = subseg;
63
64  if (! seginfo)
65    {
66      seginfo = XCNEW (segment_info_type);
67      seginfo->bfd_section = seg;
68      bfd_set_section_userdata (seg, seginfo);
69    }
70}
71
72static void
73subseg_set_rest (segT seg, subsegT subseg)
74{
75  frchainS *frcP;		/* crawl frchain chain */
76  frchainS **lastPP;		/* address of last pointer */
77  frchainS *newP;		/* address of new frchain */
78  segment_info_type *seginfo;
79
80  mri_common_symbol = NULL;
81
82  if (frag_now && frchain_now)
83    frchain_now->frch_frag_now = frag_now;
84
85  gas_assert (frchain_now == 0
86	  || frchain_now->frch_last == frag_now);
87
88  subseg_change (seg, (int) subseg);
89
90  seginfo = seg_info (seg);
91
92  /* Attempt to find or make a frchain for that subsection.
93     We keep the list sorted by subsection number.  */
94  for (frcP = *(lastPP = &seginfo->frchainP);
95       frcP != NULL;
96       frcP = *(lastPP = &frcP->frch_next))
97    if (frcP->frch_subseg >= subseg)
98      break;
99
100  if (frcP == NULL || frcP->frch_subseg != subseg)
101    {
102      /* This should be the only code that creates a frchainS.  */
103
104      newP = (frchainS *) obstack_alloc (&frchains, sizeof (frchainS));
105      newP->frch_subseg = subseg;
106      newP->fix_root = NULL;
107      newP->fix_tail = NULL;
108      obstack_begin (&newP->frch_obstack, chunksize);
109#if __GNUC__ >= 2
110      obstack_alignment_mask (&newP->frch_obstack) = __alignof__ (fragS) - 1;
111#endif
112      newP->frch_frag_now = frag_alloc (&newP->frch_obstack);
113      newP->frch_frag_now->fr_type = rs_fill;
114      newP->frch_cfi_data = NULL;
115
116      newP->frch_root = newP->frch_last = newP->frch_frag_now;
117
118      *lastPP = newP;
119      newP->frch_next = frcP;
120      frcP = newP;
121    }
122
123  frchain_now = frcP;
124  frag_now = frcP->frch_frag_now;
125
126  gas_assert (frchain_now->frch_last == frag_now);
127}
128
129/*
130 *			subseg_set(segT, subsegT)
131 *
132 * If you attempt to change to the current subsegment, nothing happens.
133 *
134 * In:	segT, subsegT code for new subsegment.
135 *	frag_now -> incomplete frag for current subsegment.
136 *	If frag_now==NULL, then there is no old, incomplete frag, so
137 *	the old frag is not closed off.
138 *
139 * Out:	now_subseg, now_seg updated.
140 *	Frchain_now points to the (possibly new) struct frchain for this
141 *	sub-segment.
142 */
143
144segT
145subseg_get (const char *segname, int force_new)
146{
147  segT secptr;
148  segment_info_type *seginfo;
149  const char *now_seg_name = now_seg ? bfd_section_name (now_seg) : 0;
150
151  if (!force_new
152      && now_seg_name
153      && (now_seg_name == segname
154	  || !strcmp (now_seg_name, segname)))
155    return now_seg;
156
157  if (!force_new)
158    secptr = bfd_make_section_old_way (stdoutput, segname);
159  else
160    secptr = bfd_make_section_anyway (stdoutput, segname);
161
162  seginfo = seg_info (secptr);
163  if (! seginfo)
164    {
165      secptr->output_section = secptr;
166      seginfo = XCNEW (segment_info_type);
167      seginfo->bfd_section = secptr;
168      bfd_set_section_userdata (secptr, seginfo);
169    }
170  return secptr;
171}
172
173segT
174subseg_new (const char *segname, subsegT subseg)
175{
176  segT secptr;
177
178  secptr = subseg_get (segname, 0);
179  subseg_set_rest (secptr, subseg);
180  return secptr;
181}
182
183/* Like subseg_new, except a new section is always created, even if
184   a section with that name already exists.  */
185segT
186subseg_force_new (const char *segname, subsegT subseg)
187{
188  segT secptr;
189
190  secptr = subseg_get (segname, 1);
191  subseg_set_rest (secptr, subseg);
192  return secptr;
193}
194
195void
196subseg_set (segT secptr, subsegT subseg)
197{
198  if (! (secptr == now_seg && subseg == now_subseg))
199    subseg_set_rest (secptr, subseg);
200  mri_common_symbol = NULL;
201}
202
203#ifndef obj_sec_sym_ok_for_reloc
204#define obj_sec_sym_ok_for_reloc(SEC)	0
205#endif
206
207symbolS *
208section_symbol (segT sec)
209{
210  segment_info_type *seginfo = seg_info (sec);
211  symbolS *s;
212
213  if (seginfo == 0)
214    abort ();
215  if (seginfo->sym)
216    return seginfo->sym;
217
218#ifndef EMIT_SECTION_SYMBOLS
219#define EMIT_SECTION_SYMBOLS 1
220#endif
221
222  if (! EMIT_SECTION_SYMBOLS || symbol_table_frozen)
223    {
224      /* Here we know it won't be going into the symbol table.  */
225      s = symbol_create (sec->symbol->name, sec, 0, &zero_address_frag);
226    }
227  else
228    {
229      segT seg;
230      s = symbol_find (sec->symbol->name);
231      /* We have to make sure it is the right symbol when we
232	 have multiple sections with the same section name.  */
233      if (s == NULL
234	  || ((seg = S_GET_SEGMENT (s)) != sec
235	      && seg != undefined_section))
236	s = symbol_new (sec->symbol->name, sec, 0, &zero_address_frag);
237      else if (seg == undefined_section)
238	{
239	  S_SET_SEGMENT (s, sec);
240	  symbol_set_frag (s, &zero_address_frag);
241	}
242    }
243
244  S_CLEAR_EXTERNAL (s);
245
246  /* Use the BFD section symbol, if possible.  */
247  if (obj_sec_sym_ok_for_reloc (sec))
248    symbol_set_bfdsym (s, sec->symbol);
249  else
250    symbol_get_bfdsym (s)->flags |= BSF_SECTION_SYM;
251
252  seginfo->sym = s;
253  return s;
254}
255
256/* Return whether the specified segment is thought to hold text.  */
257
258int
259subseg_text_p (segT sec)
260{
261  return (bfd_section_flags (sec) & SEC_CODE) != 0;
262}
263
264/* Return non zero if SEC has at least one byte of data.  It is
265   possible that we'll return zero even on a non-empty section because
266   we don't know all the fragment types, and it is possible that an
267   fr_fix == 0 one still contributes data.  Think of this as
268   seg_definitely_not_empty_p.  */
269
270int
271seg_not_empty_p (segT sec ATTRIBUTE_UNUSED)
272{
273  segment_info_type *seginfo = seg_info (sec);
274  frchainS *chain;
275  fragS *frag;
276
277  if (!seginfo)
278    return 0;
279
280  for (chain = seginfo->frchainP; chain; chain = chain->frch_next)
281    {
282      for (frag = chain->frch_root; frag; frag = frag->fr_next)
283	if (frag->fr_fix)
284	  return 1;
285      if (obstack_next_free (&chain->frch_obstack)
286	  != chain->frch_last->fr_literal)
287	return 1;
288    }
289  return 0;
290}
291
292void
293subsegs_print_statistics (FILE *file)
294{
295  frchainS *frchp;
296  asection *s;
297
298  /* PR 20897 - check to see if the output bfd was actually created.  */
299  if (stdoutput == NULL)
300    return;
301
302  fprintf (file, "frag chains:\n");
303  for (s = stdoutput->sections; s; s = s->next)
304    {
305      segment_info_type *seginfo;
306
307      /* Skip gas-internal sections.  */
308      if (segment_name (s)[0] == '*')
309	continue;
310
311      seginfo = seg_info (s);
312      if (!seginfo)
313	continue;
314
315      for (frchp = seginfo->frchainP; frchp; frchp = frchp->frch_next)
316	{
317	  int count = 0;
318	  fragS *fragp;
319
320	  for (fragp = frchp->frch_root; fragp; fragp = fragp->fr_next)
321	    count++;
322
323	  fprintf (file, "\n");
324	  fprintf (file, "\t%p %-10s\t%10d frags\n", (void *) frchp,
325		   segment_name (s), count);
326	}
327    }
328}
329
330/* end of subsegs.c */
331