Coverage Report

Created: 2026-04-04 08:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/gas/frags.c
Line
Count
Source
1
/* frags.c - manage frags -
2
   Copyright (C) 1987-2026 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
#include "as.h"
22
#include "subsegs.h"
23
#include "obstack.h"
24
25
extern fragS zero_address_frag;
26
extern fragS predefined_address_frag;
27
28
static unsigned int totalfrags;
29
30
unsigned int
31
get_frag_count (void)
32
0
{
33
0
  return totalfrags;
34
0
}
35
36
void
37
clear_frag_count (void)
38
0
{
39
0
  totalfrags = 0;
40
0
}
41

42
/* Initialization for frag routines.  */
43
44
void
45
frag_init (void)
46
567
{
47
567
  zero_address_frag.fr_type = rs_fill;
48
567
  predefined_address_frag.fr_type = rs_fill;
49
567
}
50

51
/* Check that we're not trying to assemble into a section that can't
52
   allocate frags (currently, this is only possible in the absolute
53
   section), or into an mri common.  */
54
55
static void
56
frag_alloc_check (const struct obstack *ob)
57
29.8k
{
58
29.8k
  if (ob->chunk_size == 0)
59
0
    {
60
0
      as_bad (_("attempt to allocate data in absolute section"));
61
0
      subseg_set (text_section, 0);
62
0
    }
63
64
29.8k
  if (mri_common_symbol != NULL)
65
2
    {
66
2
      as_bad (_("attempt to allocate data in common section"));
67
2
      mri_common_symbol = NULL;
68
2
    }
69
29.8k
}
70
71
/* Allocate a frag on the specified obstack.
72
   Call this routine every time a new frag is made, so that the
73
   alignment hackery can be done in just one place.  */
74
75
fragS *
76
frag_alloc (struct obstack *ob, size_t extra)
77
16.8k
{
78
16.8k
  fragS *ptr;
79
80
  /* This will align the obstack so the next struct we allocate on it
81
     will begin at a correct boundary.  */
82
16.8k
  (void) obstack_finish (ob);
83
  /* Do not use obstack_alloc here.  If you do, you'll need to turn
84
     off alignment as otherwise obstack_alloc will align the end of
85
     the frag (via obstack_finish adjusting obstack next_free
86
     pointer), making it seem like the frag already has contents in
87
     fr_literal.  */
88
16.8k
  obstack_blank (ob, extra + SIZEOF_STRUCT_FRAG);
89
16.8k
  ptr = obstack_base (ob);
90
16.8k
  memset (ptr, 0, SIZEOF_STRUCT_FRAG);
91
16.8k
  totalfrags++;
92
16.8k
  return ptr;
93
16.8k
}
94

95
/* Try to augment current frag by nchars chars.
96
   If there is no room, close off the current frag with a ".fill 0"
97
   and begin a new frag.  Then loop until the new frag has at least
98
   nchars chars available.  Does not set up any fields in frag_now.  */
99
100
void
101
frag_grow (size_t nchars)
102
29.0k
{
103
29.0k
  if (obstack_room (&frchain_now->frch_obstack) < nchars)
104
39
    {
105
39
      size_t oldc;
106
39
      size_t newc;
107
108
      /* Try to allocate a bit more than needed right now.  But don't do
109
         this if we would waste too much memory.  Especially necessary
110
         for extremely big (like 2GB initialized) frags.  */
111
39
      if (nchars < 0x10000)
112
39
        newc = 2 * nchars;
113
0
      else
114
0
        newc = nchars + 0x10000;
115
39
      newc += SIZEOF_STRUCT_FRAG;
116
117
      /* Check for possible overflow.  */
118
39
      if (newc < nchars)
119
0
  as_fatal (ngettext ("can't extend frag %lu char",
120
0
          "can't extend frag %lu chars",
121
0
          (unsigned long) nchars),
122
0
      (unsigned long) nchars);
123
124
      /* Force to allocate at least NEWC bytes, but not less than the
125
         default.  */
126
39
      oldc = obstack_chunk_size (&frchain_now->frch_obstack);
127
39
      if (newc > oldc)
128
0
  obstack_chunk_size (&frchain_now->frch_obstack) = newc;
129
130
78
      while (obstack_room (&frchain_now->frch_obstack) < nchars)
131
39
        {
132
          /* Not enough room in this frag.  Close it and start a new one.
133
             This must be done in a loop because the created frag may not
134
             be big enough if the current obstack chunk is used.  */
135
39
          frag_wane (frag_now);
136
39
          frag_new (0);
137
39
        }
138
139
      /* Restore the old chunk size.  */
140
39
      obstack_chunk_size (&frchain_now->frch_obstack) = oldc;
141
39
    }
142
29.0k
}
143

144
/* Call this to close off a completed frag, and start up a new (empty)
145
   frag, in the same subsegment as the old frag.
146
   [frchain_now remains the same but frag_now is updated.]
147
   Because this calculates the correct value of fr_fix by
148
   looking at the obstack 'frags', it needs to know how many
149
   characters at the end of the old frag belong to the maximal
150
   variable part;  The rest must belong to fr_fix.
151
   It doesn't actually set up the old frag's fr_var.  You may have
152
   set fr_var == 1, but allocated 10 chars to the end of the frag;
153
   In this case you pass old_frags_var_max_size == 10.
154
   In fact, you may use fr_var for something totally unrelated to the
155
   size of the variable part of the frag;  None of the generic frag
156
   handling code makes use of fr_var.
157
158
   Make a new frag, initialising some components. Link new frag at end
159
   of frchain_now.  */
160
161
void
162
frag_new (size_t old_frags_var_max_size
163
    /* Number of chars (already allocated on obstack frags) in
164
       variable_length part of frag.  */)
165
9.69k
{
166
9.69k
  fragS *former_last_fragP;
167
9.69k
  frchainS *frchP;
168
169
9.69k
  gas_assert (frchain_now->frch_last == frag_now);
170
171
  /* Fix up old frag's fr_fix.  */
172
9.69k
  frag_now->fr_fix = frag_now_fix_octets ();
173
9.69k
  gas_assert (frag_now->fr_fix >= old_frags_var_max_size
174
9.69k
        || now_seg == absolute_section);
175
9.69k
  frag_now->fr_fix -= old_frags_var_max_size;
176
  /* Make sure its type is valid.  */
177
9.69k
  gas_assert (frag_now->fr_type != 0);
178
179
9.69k
  frchP = frchain_now;
180
9.69k
  know (frchP);
181
9.69k
  former_last_fragP = frchP->frch_last;
182
9.69k
  gas_assert (former_last_fragP != 0);
183
9.69k
  gas_assert (former_last_fragP == frag_now);
184
9.69k
  frag_now = frag_alloc (&frchP->frch_obstack, 0);
185
186
9.69k
  frag_now->fr_file = as_where (&frag_now->fr_line);
187
188
  /* Generally, frag_now->points to an address rounded up to next
189
     alignment.  However, characters will add to obstack frags
190
     IMMEDIATELY after the struct frag, even if they are not starting
191
     at an alignment address.  */
192
9.69k
  former_last_fragP->fr_next = frag_now;
193
9.69k
  frchP->frch_last = frag_now;
194
195
9.69k
#ifndef NO_LISTING
196
9.69k
  frag_now->line = listing_tail;
197
9.69k
#endif
198
199
9.69k
  gas_assert (frchain_now->frch_last == frag_now);
200
201
9.69k
  frag_now->fr_next = NULL;
202
9.69k
}
203

204
/* Start a new frag unless we have n more chars of room in the current frag.
205
   Close off the old frag with a .fill 0.
206
207
   Return the address of the 1st char to write into. Advance
208
   frag_now_growth past the new chars.  */
209
210
char *
211
frag_more (size_t nchars)
212
16.5k
{
213
16.5k
  char *retval;
214
215
16.5k
  frag_alloc_check (&frchain_now->frch_obstack);
216
16.5k
  frag_grow (nchars);
217
16.5k
  retval = obstack_next_free (&frchain_now->frch_obstack);
218
16.5k
  obstack_blank_fast (&frchain_now->frch_obstack, nchars);
219
16.5k
  return retval;
220
16.5k
}
221

222
/* Close the current frag, setting its fields for a relaxable frag.  Start a
223
   new frag.  */
224
225
static void
226
frag_var_init (relax_stateT type, size_t max_chars, size_t var,
227
         relax_substateT subtype, symbolS *symbol, offsetT offset,
228
               char *opcode)
229
9.64k
{
230
9.64k
  frag_now->fr_var = var;
231
9.64k
  frag_now->fr_type = type;
232
9.64k
  frag_now->fr_subtype = subtype;
233
9.64k
  frag_now->fr_symbol = symbol;
234
9.64k
  frag_now->fr_offset = offset;
235
9.64k
  frag_now->fr_opcode = opcode;
236
#ifdef USING_CGEN
237
  frag_now->fr_cgen.insn = 0;
238
  frag_now->fr_cgen.opindex = 0;
239
  frag_now->fr_cgen.opinfo = 0;
240
#endif
241
9.64k
#ifdef TC_FRAG_INIT
242
9.64k
  TC_FRAG_INIT (frag_now, max_chars);
243
9.64k
#endif
244
9.64k
  frag_now->fr_file = as_where (&frag_now->fr_line);
245
246
9.64k
  frag_new (max_chars);
247
9.64k
}
248
249
/* Start a new frag unless we have max_chars more chars of room in the
250
   current frag.  Close off the old frag with a .fill 0.
251
252
   Set up a machine_dependent relaxable frag, then start a new frag.
253
   Return the address of the 1st char of the var part of the old frag
254
   to write into.  */
255
256
char *
257
frag_var (relax_stateT type, size_t max_chars, size_t var,
258
    relax_substateT subtype, symbolS *symbol, offsetT offset,
259
    char *opcode)
260
9.64k
{
261
9.64k
  char *retval;
262
263
9.64k
  frag_grow (max_chars);
264
9.64k
  retval = obstack_next_free (&frchain_now->frch_obstack);
265
9.64k
  obstack_blank_fast (&frchain_now->frch_obstack, max_chars);
266
9.64k
  frag_var_init (type, max_chars, var, subtype, symbol, offset, opcode);
267
9.64k
  return retval;
268
9.64k
}
269

270
/* OVE: This variant of frag_var assumes that space for the tail has been
271
  allocated by caller.
272
  No call to frag_grow is done.  */
273
274
char *
275
frag_variant (relax_stateT type, size_t max_chars, size_t var,
276
        relax_substateT subtype, symbolS *symbol, offsetT offset,
277
        char *opcode)
278
0
{
279
0
  char *retval;
280
281
0
  retval = obstack_next_free (&frchain_now->frch_obstack);
282
0
  frag_var_init (type, max_chars, var, subtype, symbol, offset, opcode);
283
284
0
  return retval;
285
0
}
286

287
/* Reduce the variable end of a frag to a harmless state.  */
288
289
void
290
frag_wane (fragS *fragP)
291
47
{
292
47
  fragP->fr_type = rs_fill;
293
47
  fragP->fr_offset = 0;
294
47
  fragP->fr_var = 0;
295
47
}
296

297
/* Return the number of bytes by which the current frag can be grown.  */
298
299
size_t
300
frag_room (void)
301
0
{
302
0
  return obstack_room (&frchain_now->frch_obstack);
303
0
}
304

305
/* Make an alignment frag.  The size of this frag will be adjusted to
306
   force the next frag to have the appropriate alignment.  ALIGNMENT
307
   is the power of two to which to align.  FILL_CHARACTER is the
308
   character to use to fill in any bytes which are skipped.  MAX is
309
   the maximum number of characters to skip when doing the alignment,
310
   or 0 if there is no maximum.  */
311
312
void
313
frag_align (int alignment, int fill_character, int max)
314
1.13k
{
315
1.13k
  if (now_seg == absolute_section)
316
327
    {
317
327
      addressT new_off;
318
327
      addressT mask;
319
320
327
      mask = (~(addressT) 0) << alignment;
321
327
      new_off = (abs_section_offset + ~mask) & mask;
322
327
      if (max == 0 || new_off - abs_section_offset <= (addressT) max)
323
327
  abs_section_offset = new_off;
324
327
    }
325
811
  else
326
811
    {
327
811
      char *p;
328
329
811
      p = frag_var (rs_align, 1, 1, max, NULL, alignment, NULL);
330
811
      *p = fill_character;
331
811
    }
332
1.13k
}
333
334
/* Make an alignment frag like frag_align, but fill with a repeating
335
   pattern rather than a single byte.  ALIGNMENT is the power of two
336
   to which to align.  FILL_PATTERN is the fill pattern to repeat in
337
   the bytes which are skipped.  N_FILL is the number of bytes in
338
   FILL_PATTERN.  MAX is the maximum number of characters to skip when
339
   doing the alignment, or 0 if there is no maximum.  */
340
341
void
342
frag_align_pattern (int alignment, const char *fill_pattern,
343
        size_t n_fill, int max)
344
0
{
345
0
  char *p;
346
347
0
  p = frag_var (rs_align, n_fill, n_fill, max, NULL, alignment, NULL);
348
0
  memcpy (p, fill_pattern, n_fill);
349
0
}
350
351
/* The NOP_OPCODE is for the alignment fill value.  Fill it with a nop
352
   instruction so that the disassembler does not choke on it.  */
353
#ifndef NOP_OPCODE
354
#define NOP_OPCODE 0x00
355
#endif
356
357
/* Use this to specify the amount of memory allocated for representing
358
   the alignment code.  Needs to be large enough to hold any fixed size
359
   prologue plus the replicating portion.  */
360
#ifndef MAX_MEM_FOR_RS_ALIGN_CODE
361
# define MAX_MEM_FOR_RS_ALIGN_CODE(p2align, max) 1
362
#endif
363
364
void
365
frag_align_code (int alignment, int max)
366
390
{
367
390
  char *p;
368
369
390
  p = frag_var (rs_align_code, MAX_MEM_FOR_RS_ALIGN_CODE (alignment, max),
370
390
    1, max, NULL, alignment, NULL);
371
390
  *p = NOP_OPCODE;
372
390
}
373
374
addressT
375
frag_now_fix_octets (void)
376
604k
{
377
604k
  if (now_seg == absolute_section)
378
18.4k
    return abs_section_offset;
379
380
586k
  return ((char *) obstack_next_free (&frchain_now->frch_obstack)
381
586k
    - frag_now->fr_literal);
382
604k
}
383
384
addressT
385
frag_now_fix (void)
386
592k
{
387
  /* Symbols whose section has SEC_ELF_OCTETS set,
388
     resolve to octets instead of target bytes.  */
389
592k
  if (now_seg->flags & SEC_OCTETS)
390
1.91k
    return frag_now_fix_octets ();
391
590k
  else
392
590k
    return frag_now_fix_octets () / OCTETS_PER_BYTE;
393
592k
}
394
395
void
396
frag_append_1_char (int datum)
397
13.3k
{
398
13.3k
  frag_alloc_check (&frchain_now->frch_obstack);
399
13.3k
  if (obstack_room (&frchain_now->frch_obstack) <= 1)
400
8
    {
401
8
      frag_wane (frag_now);
402
8
      frag_new (0);
403
8
    }
404
13.3k
  obstack_1grow (&frchain_now->frch_obstack, datum);
405
13.3k
}
406
407
/* Return TRUE if FRAG1 and FRAG2 have a fixed relationship between
408
   their start addresses.  Set OFFSET to the difference in address
409
   not already accounted for in the frag FR_ADDRESS.  */
410
411
bool
412
frag_offset_fixed_p (const fragS *frag1, const fragS *frag2, offsetT *offset)
413
3.25k
{
414
3.25k
  const fragS *frag;
415
3.25k
  offsetT off;
416
417
  /* Start with offset initialised to difference between the two frags.
418
     Prior to assigning frag addresses this will be zero.  */
419
3.25k
  off = frag1->fr_address - frag2->fr_address;
420
3.25k
  if (frag1 == frag2)
421
3.21k
    {
422
3.21k
      *offset = off;
423
3.21k
      return true;
424
3.21k
    }
425
426
  /* Maybe frag2 is after frag1.  */
427
43
  frag = frag1;
428
64
  while (frag->fr_type == rs_fill)
429
50
    {
430
50
      off += frag->fr_fix + frag->fr_offset * frag->fr_var;
431
50
      frag = frag->fr_next;
432
50
      if (frag == NULL)
433
21
  break;
434
29
      if (frag == frag2)
435
8
  {
436
8
    *offset = off;
437
8
    return true;
438
8
  }
439
29
    }
440
441
  /* Maybe frag1 is after frag2.  */
442
35
  off = frag1->fr_address - frag2->fr_address;
443
35
  frag = frag2;
444
53
  while (frag->fr_type == rs_fill)
445
45
    {
446
45
      off -= frag->fr_fix + frag->fr_offset * frag->fr_var;
447
45
      frag = frag->fr_next;
448
45
      if (frag == NULL)
449
21
  break;
450
24
      if (frag == frag1)
451
6
  {
452
6
    *offset = off;
453
6
    return true;
454
6
  }
455
24
    }
456
457
29
  return false;
458
35
}
459
460
/* Return TRUE if FRAG2 follows FRAG1 with a fixed relationship
461
   between the two assuming alignment frags do nothing.  Set OFFSET to
462
   the difference in address not already accounted for in the frag
463
   FR_ADDRESS.  */
464
465
bool
466
frag_offset_ignore_align_p (const fragS *frag1, const fragS *frag2,
467
          offsetT *offset)
468
625
{
469
625
  const fragS *frag;
470
625
  offsetT off;
471
472
  /* Start with offset initialised to difference between the two frags.
473
     Prior to assigning frag addresses this will be zero.  */
474
625
  off = frag1->fr_address - frag2->fr_address;
475
625
  if (frag1 == frag2)
476
625
    {
477
625
      *offset = off;
478
625
      return true;
479
625
    }
480
481
0
  frag = frag1;
482
0
  while (frag->fr_type == rs_fill
483
0
   || frag->fr_type == rs_align
484
0
   || frag->fr_type == rs_align_code
485
0
   || frag->fr_type == rs_align_test)
486
0
    {
487
0
      if (frag->fr_type == rs_fill)
488
0
  off += frag->fr_fix + frag->fr_offset * frag->fr_var;
489
0
      frag = frag->fr_next;
490
0
      if (frag == NULL)
491
0
  break;
492
0
      if (frag == frag2)
493
0
  {
494
0
    *offset = off;
495
0
    return true;
496
0
  }
497
0
    }
498
499
0
  return false;
500
0
}
501
502
/* Return TRUE if we can determine whether FRAG2 OFF2 appears after
503
   (strict >, not >=) FRAG1 OFF1, assuming it is not before.  Set
504
   *OFFSET so that resolve_expression will resolve an O_gt operation
505
   between them to false (0) if they are guaranteed to be at the same
506
   location, or to true (-1) if they are guaranteed to be at different
507
   locations.  Return FALSE conservatively, e.g. if neither result can
508
   be guaranteed (yet).
509
510
   They are known to be in the same segment, and not the same frag
511
   (this is a fallback for frag_offset_fixed_p, that always takes care
512
   of this case), and it is expected (from the uses this is designed
513
   to simplify, namely location view increments) that frag2 is
514
   reachable from frag1 following the fr_next links, rather than the
515
   other way round.  */
516
517
bool
518
frag_gtoffset_p (valueT off2, const fragS *frag2,
519
     valueT off1, const fragS *frag1, offsetT *offset)
520
23
{
521
  /* Insanity check.  */
522
23
  if (frag2 == frag1 || off1 > frag1->fr_fix)
523
19
    return false;
524
525
  /* If the first symbol offset is at the end of the first frag and
526
     the second symbol offset at the beginning of the second frag then
527
     it is possible they are at the same address.  Go looking for a
528
     non-zero fr_fix in any frag between these frags.  If found then
529
     we can say the O_gt result will be true.  If no such frag is
530
     found we assume that frag1 or any of the following frags might
531
     have a variable tail and thus the answer is unknown.  This isn't
532
     strictly true; some frags don't have a variable tail, but it
533
     doesn't seem worth optimizing for those cases.  */
534
4
  const fragS *frag = frag1;
535
4
  offsetT delta = off2 - off1;
536
4
  for (;;)
537
6
    {
538
6
      delta += frag->fr_fix;
539
6
      frag = frag->fr_next;
540
6
      if (frag == frag2)
541
2
  {
542
2
    if (delta == 0)
543
2
      return false;
544
0
    break;
545
2
  }
546
      /* If we run off the end of the frag chain then we have a case
547
   where frag2 is not after frag1, ie. an O_gt expression not
548
   created for .loc view.  */
549
4
      if (frag == NULL)
550
2
  return false;
551
4
    }
552
553
0
  *offset = (off2 - off1 - delta) * OCTETS_PER_BYTE;
554
  return true;
555
4
}