Coverage Report

Created: 2025-07-08 11:15

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