Coverage Report

Created: 2025-06-24 06:45

/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
28
{
38
28
  obstack_begin (&frchains, chunksize);
39
28
#if __GNUC__ >= 2
40
28
  obstack_alignment_mask (&frchains) = __alignof__ (frchainS) - 1;
41
28
#endif
42
43
28
  frchain_now = NULL;   /* Warn new_subseg() that we are booting.  */
44
28
  frag_now = &dummy_frag;
45
28
}
46
47
void
48
subsegs_end (struct obstack **obs)
49
28
{
50
441
  for (; *obs; obs++)
51
413
    _obstack_free (*obs, NULL);
52
28
  _obstack_free (&frchains, NULL);
53
28
  bfd_set_section_userdata (bfd_com_section_ptr, NULL);
54
28
  bfd_set_section_userdata (bfd_und_section_ptr, NULL);
55
28
  bfd_set_section_userdata (bfd_abs_section_ptr, NULL);
56
28
  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
28
  asymbol *global_syms = bfd_com_section_ptr->symbol;
62
28
  bfd_und_section_ptr->used_by_bfd = NULL;
63
28
  bfd_und_section_ptr->symbol = global_syms + (bfd_und_section_ptr
64
28
                 - bfd_com_section_ptr);
65
28
  bfd_abs_section_ptr->used_by_bfd = NULL;
66
28
  bfd_abs_section_ptr->symbol = global_syms + (bfd_abs_section_ptr
67
28
                 - bfd_com_section_ptr);
68
28
}
69

70
static void
71
alloc_seginfo (segT seg)
72
362
{
73
362
  segment_info_type *seginfo;
74
75
362
  seginfo = obstack_alloc (&notes, sizeof (*seginfo));
76
362
  memset (seginfo, 0, sizeof (*seginfo));
77
362
  bfd_set_section_userdata (seg, seginfo);
78
362
}
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
7.85k
{
92
7.85k
  now_seg = seg;
93
7.85k
  now_subseg = subseg;
94
95
7.85k
  if (!seg_info (seg))
96
0
    alloc_seginfo (seg);
97
7.85k
}
98

99
static void
100
subseg_set_rest (segT seg, subsegT subseg)
101
7.85k
{
102
7.85k
  frchainS *frcP;   /* crawl frchain chain */
103
7.85k
  frchainS **lastPP;    /* address of last pointer */
104
7.85k
  frchainS *newP;   /* address of new frchain */
105
7.85k
  segment_info_type *seginfo;
106
107
7.85k
  mri_common_symbol = NULL;
108
109
7.85k
  if (frag_now && frchain_now)
110
7.82k
    frchain_now->frch_frag_now = frag_now;
111
112
7.85k
  gas_assert (frchain_now == 0
113
7.85k
    || frchain_now->frch_last == frag_now);
114
115
7.85k
  subseg_change (seg, (int) subseg);
116
117
7.85k
  seginfo = seg_info (seg);
118
119
  /* Should the section symbol be kept?  */
120
7.85k
  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
7.85k
  for (frcP = *(lastPP = &seginfo->frchainP);
126
8.63k
       frcP != NULL;
127
7.85k
       frcP = *(lastPP = &frcP->frch_next))
128
8.24k
    if (frcP->frch_subseg >= subseg)
129
7.46k
      break;
130
131
7.85k
  if (frcP == NULL || frcP->frch_subseg != subseg)
132
413
    {
133
      /* This should be the only code that creates a frchainS.  */
134
135
413
      newP = (frchainS *) obstack_alloc (&frchains, sizeof (frchainS));
136
413
      newP->frch_subseg = subseg;
137
413
      newP->fix_root = NULL;
138
413
      newP->fix_tail = NULL;
139
413
      obstack_begin (&newP->frch_obstack, chunksize);
140
413
#if __GNUC__ >= 2
141
413
      obstack_alignment_mask (&newP->frch_obstack) = __alignof__ (fragS) - 1;
142
413
#endif
143
413
      newP->frch_frag_now = frag_alloc (&newP->frch_obstack, 0);
144
413
      newP->frch_frag_now->fr_type = rs_fill;
145
413
      newP->frch_cfi_data = NULL;
146
413
      newP->frch_ginsn_data = NULL;
147
148
413
      newP->frch_root = newP->frch_last = newP->frch_frag_now;
149
150
413
      *lastPP = newP;
151
413
      newP->frch_next = frcP;
152
413
      frcP = newP;
153
413
    }
154
155
7.85k
  frchain_now = frcP;
156
7.85k
  frag_now = frcP->frch_frag_now;
157
158
7.85k
  gas_assert (frchain_now->frch_last == frag_now);
159
7.85k
}
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
3.31k
{
179
3.31k
  segT secptr;
180
3.31k
  const char *now_seg_name = now_seg ? bfd_section_name (now_seg) : 0;
181
182
3.31k
  if (!force_new
183
3.31k
      && now_seg_name
184
3.31k
      && (now_seg_name == segname
185
3.24k
    || !strcmp (now_seg_name, segname)))
186
0
    return now_seg;
187
188
3.31k
  if (!force_new)
189
3.27k
    secptr = bfd_make_section_old_way (stdoutput, segname);
190
45
  else
191
45
    secptr = bfd_make_section_anyway (stdoutput, segname);
192
193
3.31k
  if (!seg_info (secptr))
194
362
    {
195
362
      secptr->output_section = secptr;
196
362
      alloc_seginfo (secptr);
197
362
    }
198
3.31k
  return secptr;
199
3.31k
}
200
201
segT
202
subseg_new (const char *segname, subsegT subseg)
203
3.27k
{
204
3.27k
  segT secptr;
205
206
3.27k
  secptr = subseg_get (segname, 0);
207
3.27k
  subseg_set_rest (secptr, subseg);
208
3.27k
  return secptr;
209
3.27k
}
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
45
{
216
45
  segT secptr;
217
218
45
  secptr = subseg_get (segname, 1);
219
45
  subseg_set_rest (secptr, subseg);
220
45
  return secptr;
221
45
}
222
223
void
224
subseg_set (segT secptr, subsegT subseg)
225
5.80k
{
226
5.80k
  if (! (secptr == now_seg && subseg == now_subseg))
227
4.53k
    subseg_set_rest (secptr, subseg);
228
5.80k
  mri_common_symbol = NULL;
229
5.80k
}
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
530
{
238
530
  segment_info_type *seginfo = seg_info (sec);
239
530
  symbolS *s;
240
241
530
  if (seginfo == 0)
242
0
    abort ();
243
530
  if (seginfo->sym)
244
369
    return seginfo->sym;
245
246
161
#ifndef EMIT_SECTION_SYMBOLS
247
322
#define EMIT_SECTION_SYMBOLS 1
248
161
#endif
249
250
161
  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
161
  else
256
161
    {
257
161
      segT seg;
258
161
      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
161
      if (s == NULL
262
161
    || ((seg = S_GET_SEGMENT (s)) != sec
263
3
        && seg != undefined_section))
264
158
  s = symbol_new (sec->symbol->name, sec, &zero_address_frag, 0);
265
3
      else if (seg == undefined_section)
266
2
  {
267
2
    S_SET_SEGMENT (s, sec);
268
2
    symbol_set_frag (s, &zero_address_frag);
269
2
  }
270
161
    }
271
272
161
  S_CLEAR_EXTERNAL (s);
273
274
  /* Use the BFD section symbol, if possible.  */
275
161
  if (obj_sec_sym_ok_for_reloc (sec))
276
160
    symbol_set_bfdsym (s, sec->symbol);
277
1
  else
278
1
    symbol_get_bfdsym (s)->flags |= BSF_SECTION_SYM;
279
280
161
  seginfo->sym = s;
281
161
  return s;
282
530
}
283
284
/* Return whether the specified segment is thought to hold text.  */
285
286
int
287
subseg_text_p (segT sec)
288
482
{
289
482
  return (bfd_section_flags (sec) & SEC_CODE) != 0;
290
482
}
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
1
{
301
1
  segment_info_type *seginfo = seg_info (sec);
302
1
  frchainS *chain;
303
1
  fragS *frag;
304
305
1
  if (!seginfo)
306
0
    return 0;
307
308
1
  for (chain = seginfo->frchainP; chain; chain = chain->frch_next)
309
1
    {
310
1
      for (frag = chain->frch_root; frag; frag = frag->fr_next)
311
1
  if (frag->fr_fix)
312
1
    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
1
}
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 */