Coverage Report

Created: 2025-06-24 06:45

/src/binutils-gdb/gas/ehopt.c
Line
Count
Source (jump to first uncovered line)
1
/* ehopt.c--optimize gcc exception frame information.
2
   Copyright (C) 1998-2025 Free Software Foundation, Inc.
3
   Written by Ian Lance Taylor <ian@cygnus.com>.
4
5
   This file is part of GAS, the GNU Assembler.
6
7
   GAS is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3, or (at your option)
10
   any later version.
11
12
   GAS is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with GAS; see the file COPYING.  If not, write to the Free
19
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
20
   02110-1301, USA.  */
21
22
#include "as.h"
23
#include "subsegs.h"
24
25
/* We include this ELF file, even though we may not be assembling for
26
   ELF, since the exception frame information is always in a format
27
   derived from DWARF.  */
28
29
#include "dwarf2.h"
30
31
/* Try to optimize gcc 2.8 exception frame information.
32
33
   Exception frame information is emitted for every function in the
34
   .eh_frame or .debug_frame sections.  Simple information for a function
35
   with no exceptions looks like this:
36
37
__FRAME_BEGIN__:
38
  .4byte  .LLCIE1 / Length of Common Information Entry
39
.LSCIE1:
40
#if .eh_frame
41
  .4byte  0x0 / CIE Identifier Tag
42
#elif .debug_frame
43
  .4byte  0xffffffff / CIE Identifier Tag
44
#endif
45
  .byte 0x1 / CIE Version
46
  .byte 0x0 / CIE Augmentation (none)
47
  .byte 0x1 / ULEB128 0x1 (CIE Code Alignment Factor)
48
  .byte 0x7c  / SLEB128 -4 (CIE Data Alignment Factor)
49
  .byte 0x8 / CIE RA Column
50
  .byte 0xc / DW_CFA_def_cfa
51
  .byte 0x4 / ULEB128 0x4
52
  .byte 0x4 / ULEB128 0x4
53
  .byte 0x88  / DW_CFA_offset, column 0x8
54
  .byte 0x1 / ULEB128 0x1
55
  .align 4
56
.LECIE1:
57
  .set  .LLCIE1,.LECIE1-.LSCIE1 / CIE Length Symbol
58
  .4byte  .LLFDE1 / FDE Length
59
.LSFDE1:
60
  .4byte  .LSFDE1-__FRAME_BEGIN__ / FDE CIE offset
61
  .4byte  .LFB1 / FDE initial location
62
  .4byte  .LFE1-.LFB1 / FDE address range
63
  .byte 0x4 / DW_CFA_advance_loc4
64
  .4byte  .LCFI0-.LFB1
65
  .byte 0xe / DW_CFA_def_cfa_offset
66
  .byte 0x8 / ULEB128 0x8
67
  .byte 0x85  / DW_CFA_offset, column 0x5
68
  .byte 0x2 / ULEB128 0x2
69
  .byte 0x4 / DW_CFA_advance_loc4
70
  .4byte  .LCFI1-.LCFI0
71
  .byte 0xd / DW_CFA_def_cfa_register
72
  .byte 0x5 / ULEB128 0x5
73
  .byte 0x4 / DW_CFA_advance_loc4
74
  .4byte  .LCFI2-.LCFI1
75
  .byte 0x2e  / DW_CFA_GNU_args_size
76
  .byte 0x4 / ULEB128 0x4
77
  .byte 0x4 / DW_CFA_advance_loc4
78
  .4byte  .LCFI3-.LCFI2
79
  .byte 0x2e  / DW_CFA_GNU_args_size
80
  .byte 0x0 / ULEB128 0x0
81
  .align 4
82
.LEFDE1:
83
  .set  .LLFDE1,.LEFDE1-.LSFDE1 / FDE Length Symbol
84
85
   The immediate issue we can address in the assembler is the
86
   DW_CFA_advance_loc4 followed by a four byte value.  The value is
87
   the difference of two addresses in the function.  Since gcc does
88
   not know this value, it always uses four bytes.  We will know the
89
   value at the end of assembly, so we can do better.  */
90
91
struct cie_info
92
{
93
  fragS *f;
94
  unsigned code_alignment;
95
  int z_augmentation;
96
};
97
98
/* Extract information from the CIE.  */
99
100
static bool
101
get_cie_info (struct cie_info *info)
102
0
{
103
0
  fragS *f;
104
0
  unsigned int offset;
105
0
  char CIE_id;
106
0
  char augmentation[10];
107
0
  int iaug;
108
0
  int code_alignment = 0;
109
110
  /* We should find the CIE at the start of the section.  */
111
112
0
  f = seg_info (now_seg)->frchainP->frch_root;
113
0
  while (f != NULL && f->fr_fix == 0)
114
0
    f = f->fr_next;
115
0
  if (f != info->f)
116
0
    return false;
117
118
  /* First make sure that the CIE Identifier Tag is 0/-1.  */
119
120
0
  if (startswith (segment_name (now_seg), ".debug_frame"))
121
0
    CIE_id = (char)0xff;
122
0
  else
123
0
    CIE_id = 0;
124
125
0
  offset = 4;
126
0
  while (f != NULL && offset >= f->fr_fix)
127
0
    {
128
0
      offset -= f->fr_fix;
129
0
      f = f->fr_next;
130
0
    }
131
0
  if (f == NULL
132
0
      || f->fr_fix - offset < 4
133
0
      || f->fr_literal[offset] != CIE_id
134
0
      || f->fr_literal[offset + 1] != CIE_id
135
0
      || f->fr_literal[offset + 2] != CIE_id
136
0
      || f->fr_literal[offset + 3] != CIE_id)
137
0
    return false;
138
139
  /* Next make sure the CIE version number is 1.  */
140
141
0
  offset += 4;
142
0
  while (f != NULL && offset >= f->fr_fix)
143
0
    {
144
0
      offset -= f->fr_fix;
145
0
      f = f->fr_next;
146
0
    }
147
0
  if (f == NULL
148
0
      || f->fr_fix - offset < 1
149
0
      || f->fr_literal[offset] != 1)
150
0
    return false;
151
152
  /* Skip the augmentation (a null terminated string).  */
153
154
0
  iaug = 0;
155
0
  ++offset;
156
0
  while (1)
157
0
    {
158
0
      while (f != NULL && offset >= f->fr_fix)
159
0
  {
160
0
    offset -= f->fr_fix;
161
0
    f = f->fr_next;
162
0
  }
163
0
      if (f == NULL)
164
0
  return false;
165
166
0
      while (offset < f->fr_fix && f->fr_literal[offset] != '\0')
167
0
  {
168
0
    if ((size_t) iaug < (sizeof augmentation) - 1)
169
0
      {
170
0
        augmentation[iaug] = f->fr_literal[offset];
171
0
        ++iaug;
172
0
      }
173
0
    ++offset;
174
0
  }
175
0
      if (offset < f->fr_fix)
176
0
  break;
177
0
    }
178
0
  ++offset;
179
0
  while (f != NULL && offset >= f->fr_fix)
180
0
    {
181
0
      offset -= f->fr_fix;
182
0
      f = f->fr_next;
183
0
    }
184
0
  if (f == NULL)
185
0
    return false;
186
187
0
  augmentation[iaug] = '\0';
188
0
  if (augmentation[0] == '\0')
189
0
    {
190
      /* No augmentation.  */
191
0
    }
192
0
  else if (strcmp (augmentation, "eh") == 0)
193
0
    {
194
      /* We have to skip a pointer.  Unfortunately, we don't know how
195
   large it is.  We find out by looking for a matching fixup.  */
196
0
      fixS *fix = seg_info (now_seg)->frchainP->fix_root;
197
0
      while (fix != NULL
198
0
       && (fix->fx_frag != f || fix->fx_where != offset))
199
0
  fix = fix->fx_next;
200
0
      if (fix == NULL)
201
0
  offset += 4;
202
0
      else
203
0
  offset += fix->fx_size;
204
0
      while (f != NULL && offset >= f->fr_fix)
205
0
  {
206
0
    offset -= f->fr_fix;
207
0
    f = f->fr_next;
208
0
  }
209
0
      if (f == NULL)
210
0
  return false;
211
0
    }
212
0
  else if (augmentation[0] != 'z')
213
0
    return false;
214
215
  /* We're now at the code alignment factor, which is a ULEB128.  If
216
     it isn't a single byte, forget it.  */
217
218
0
  code_alignment = f->fr_literal[offset] & 0xff;
219
0
  if ((code_alignment & 0x80) != 0)
220
0
    code_alignment = 0;
221
222
0
  info->code_alignment = code_alignment;
223
0
  info->z_augmentation = (augmentation[0] == 'z');
224
225
0
  return true;
226
0
}
227
228
enum frame_state
229
{
230
  state_idle,
231
  state_saw_size,
232
  state_saw_cie_offset,
233
  state_saw_pc_begin,
234
  state_seeing_aug_size,
235
  state_skipping_aug,
236
  state_wait_loc4,
237
  state_saw_loc4,
238
  state_error,
239
};
240
241
struct frame_data
242
{
243
  enum frame_state state;
244
245
  bool cie_info_ok;
246
  struct cie_info cie_info;
247
248
  symbolS *size_end_sym;
249
  fragS *loc4_frag;
250
  int loc4_fix;
251
252
  int aug_size;
253
  int aug_shift;
254
};
255
256
static struct eh_state
257
{
258
  struct frame_data eh_data;
259
  struct frame_data debug_data;
260
} frame;
261
262
/* This function is called from emit_expr.  It looks for cases which
263
   we can optimize.
264
265
   Rather than try to parse all this information as we read it, we
266
   look for a single byte DW_CFA_advance_loc4 followed by a 4 byte
267
   difference.  We turn that into a rs_cfa_advance frag, and handle
268
   those frags at the end of the assembly.  If the gcc output changes
269
   somewhat, this optimization may stop working.
270
271
   This function returns non-zero if it handled the expression and
272
   emit_expr should not do anything, or zero otherwise.  It can also
273
   change *EXP and *PNBYTES.  */
274
275
int
276
check_eh_frame (expressionS *exp, unsigned int *pnbytes)
277
5.41k
{
278
5.41k
  struct frame_data *d;
279
280
  /* Don't optimize.  */
281
5.41k
  if (flag_traditional_format)
282
0
    return 0;
283
284
#ifdef md_allow_eh_opt
285
  if (! md_allow_eh_opt)
286
    return 0;
287
#endif
288
289
  /* Select the proper section data.  */
290
5.41k
  if (startswith (segment_name (now_seg), ".eh_frame")
291
5.41k
      && segment_name (now_seg)[9] != '_')
292
0
    d = &frame.eh_data;
293
5.41k
  else if (startswith (segment_name (now_seg), ".debug_frame"))
294
0
    d = &frame.debug_data;
295
5.41k
  else
296
5.41k
    return 0;
297
298
0
  if (d->state >= state_saw_size && S_IS_DEFINED (d->size_end_sym))
299
0
    {
300
      /* We have come to the end of the CIE or FDE.  See below where
301
         we set saw_size.  We must check this first because we may now
302
         be looking at the next size.  */
303
0
      d->state = state_idle;
304
0
    }
305
306
0
  switch (d->state)
307
0
    {
308
0
    case state_idle:
309
0
      if (*pnbytes == 4)
310
0
  {
311
    /* This might be the size of the CIE or FDE.  We want to know
312
       the size so that we don't accidentally optimize across an FDE
313
       boundary.  We recognize the size in one of two forms: a
314
       symbol which will later be defined as a difference, or a
315
       subtraction of two symbols.  Either way, we can tell when we
316
       are at the end of the FDE because the symbol becomes defined
317
       (in the case of a subtraction, the end symbol, from which the
318
       start symbol is being subtracted).  Other ways of describing
319
       the size will not be optimized.  */
320
0
    if ((exp->X_op == O_symbol || exp->X_op == O_subtract)
321
0
        && ! S_IS_DEFINED (exp->X_add_symbol))
322
0
      {
323
0
        d->state = state_saw_size;
324
0
        d->size_end_sym = exp->X_add_symbol;
325
0
        if (!d->cie_info.f)
326
0
    d->cie_info.f = frag_now;
327
0
      }
328
0
  }
329
0
      break;
330
331
0
    case state_saw_size:
332
0
    case state_saw_cie_offset:
333
0
      if (!(*pnbytes == 4 || *pnbytes == 8))
334
  /* Stop scanning if we don't see the expected FDE fields.  */
335
0
  d->state = state_error;
336
0
      else
337
0
  d->state = (enum frame_state) (d->state + 1);
338
0
      break;
339
340
0
    case state_saw_pc_begin:
341
      /* Decide whether we should see an augmentation.  */
342
0
      if (!(*pnbytes == 4 || *pnbytes == 8))
343
0
  d->state = state_error;
344
0
      else if (!d->cie_info_ok
345
0
         && !(d->cie_info_ok = get_cie_info (&d->cie_info)))
346
0
  d->state = state_error;
347
0
      else if (d->cie_info.z_augmentation)
348
0
  {
349
0
    d->state = state_seeing_aug_size;
350
0
    d->aug_size = 0;
351
0
    d->aug_shift = 0;
352
0
  }
353
0
      else
354
0
  d->state = state_wait_loc4;
355
0
      break;
356
357
0
    case state_seeing_aug_size:
358
      /* Bytes == -1 means this comes from an leb128 directive.  */
359
0
      if ((int) *pnbytes == -1 && exp->X_op == O_constant)
360
0
  {
361
0
    d->aug_size = exp->X_add_number;
362
0
    d->state = state_skipping_aug;
363
0
  }
364
0
      else if (*pnbytes == 1 && exp->X_op == O_constant)
365
0
  {
366
0
    unsigned char byte = exp->X_add_number;
367
0
    d->aug_size |= (byte & 0x7f) << d->aug_shift;
368
0
    d->aug_shift += 7;
369
0
    if ((byte & 0x80) == 0)
370
0
      d->state = state_skipping_aug;
371
0
  }
372
0
      else
373
0
  d->state = state_error;
374
0
      if (d->state == state_skipping_aug && d->aug_size == 0)
375
0
  d->state = state_wait_loc4;
376
0
      break;
377
378
0
    case state_skipping_aug:
379
0
      if ((int) *pnbytes < 0)
380
0
  d->state = state_error;
381
0
      else
382
0
  {
383
0
    int left = (d->aug_size -= *pnbytes);
384
0
    if (left == 0)
385
0
      d->state = state_wait_loc4;
386
0
    else if (left < 0)
387
0
      d->state = state_error;
388
0
  }
389
0
      break;
390
391
0
    case state_wait_loc4:
392
0
      if (*pnbytes == 1
393
0
    && exp->X_op == O_constant
394
0
    && exp->X_add_number == DW_CFA_advance_loc4)
395
0
  {
396
    /* This might be a DW_CFA_advance_loc4.  Record the frag and the
397
       position within the frag, so that we can change it later.  */
398
0
    frag_grow (1 + 4);
399
0
    d->state = state_saw_loc4;
400
0
    d->loc4_frag = frag_now;
401
0
    d->loc4_fix = frag_now_fix ();
402
0
  }
403
0
      break;
404
405
0
    case state_saw_loc4:
406
0
      d->state = state_wait_loc4;
407
0
      if (*pnbytes != 4)
408
0
  break;
409
0
      if (exp->X_op == O_constant)
410
0
  {
411
    /* This is a case which we can optimize.  The two symbols being
412
       subtracted were in the same frag and the expression was
413
       reduced to a constant.  We can do the optimization entirely
414
       in this function.  */
415
0
    if (exp->X_add_number < 0x40)
416
0
      {
417
0
        d->loc4_frag->fr_literal[d->loc4_fix]
418
0
    = DW_CFA_advance_loc | exp->X_add_number;
419
        /* No more bytes needed.  */
420
0
        return 1;
421
0
      }
422
0
    else if (exp->X_add_number < 0x100)
423
0
      {
424
0
        d->loc4_frag->fr_literal[d->loc4_fix] = DW_CFA_advance_loc1;
425
0
        *pnbytes = 1;
426
0
      }
427
0
    else if (exp->X_add_number < 0x10000)
428
0
      {
429
0
        d->loc4_frag->fr_literal[d->loc4_fix] = DW_CFA_advance_loc2;
430
0
        *pnbytes = 2;
431
0
      }
432
0
  }
433
0
      else if (exp->X_op == O_subtract && d->cie_info.code_alignment == 1)
434
0
  {
435
    /* This is a case we can optimize.  The expression was not
436
       reduced, so we can not finish the optimization until the end
437
       of the assembly.  We set up a variant frag which we handle
438
       later.  */
439
0
    frag_var (rs_cfa, 4, 0, 1 << 3, make_expr_symbol (exp),
440
0
        d->loc4_fix, (char *) d->loc4_frag);
441
0
    return 1;
442
0
  }
443
0
      else if ((exp->X_op == O_divide
444
0
    || exp->X_op == O_right_shift)
445
0
         && d->cie_info.code_alignment > 1)
446
0
  {
447
0
    if (symbol_symbolS (exp->X_add_symbol)
448
0
        && symbol_constant_p (exp->X_op_symbol)
449
0
        && S_GET_SEGMENT (exp->X_op_symbol) == absolute_section
450
0
        && ((exp->X_op == O_divide
451
0
       ? *symbol_X_add_number (exp->X_op_symbol)
452
0
       : (offsetT) 1 << *symbol_X_add_number (exp->X_op_symbol))
453
0
      == (offsetT) d->cie_info.code_alignment))
454
0
      {
455
0
        expressionS *symval;
456
457
0
        symval = symbol_get_value_expression (exp->X_add_symbol);
458
0
        if (symval->X_op == O_subtract)
459
0
    {
460
      /* This is a case we can optimize as well.  The
461
         expression was not reduced, so we can not finish
462
         the optimization until the end of the assembly.
463
         We set up a variant frag which we handle later.  */
464
0
      frag_var (rs_cfa, 4, 0, d->cie_info.code_alignment << 3,
465
0
          make_expr_symbol (symval),
466
0
          d->loc4_fix, (char *) d->loc4_frag);
467
0
      return 1;
468
0
    }
469
0
      }
470
0
  }
471
0
      break;
472
473
0
    case state_error:
474
      /* Just skipping everything.  */
475
0
      break;
476
0
    }
477
478
0
  return 0;
479
0
}
480
481
/* The function estimates the size of a rs_cfa variant frag based on
482
   the current values of the symbols.  It is called before the
483
   relaxation loop.  We set fr_subtype{0:2} to the expected length.  */
484
485
int
486
eh_frame_estimate_size_before_relax (fragS *frag)
487
0
{
488
0
  offsetT diff;
489
0
  int ca = frag->fr_subtype >> 3;
490
0
  int ret;
491
492
0
  diff = resolve_symbol_value (frag->fr_symbol);
493
494
0
  gas_assert (ca > 0);
495
0
  diff /= ca;
496
0
  if (diff == 0)
497
0
    ret = -1;
498
0
  else if (diff < 0x40)
499
0
    ret = 0;
500
0
  else if (diff < 0x100)
501
0
    ret = 1;
502
0
  else if (diff < 0x10000)
503
0
    ret = 2;
504
0
  else
505
0
    ret = 4;
506
507
0
  frag->fr_subtype = (frag->fr_subtype & ~7) | (ret & 7);
508
509
0
  return ret;
510
0
}
511
512
/* This function relaxes a rs_cfa variant frag based on the current
513
   values of the symbols.  fr_subtype{0:2} is the current length of
514
   the frag.  This returns the change in frag length.  */
515
516
int
517
eh_frame_relax_frag (fragS *frag)
518
0
{
519
0
  int oldsize, newsize;
520
521
0
  oldsize = frag->fr_subtype & 7;
522
0
  if (oldsize == 7)
523
0
    oldsize = -1;
524
0
  newsize = eh_frame_estimate_size_before_relax (frag);
525
0
  return newsize - oldsize;
526
0
}
527
528
/* This function converts a rs_cfa variant frag into a normal fill
529
   frag.  This is called after all relaxation has been done.
530
   fr_subtype{0:2} will be the desired length of the frag.  */
531
532
void
533
eh_frame_convert_frag (fragS *frag)
534
0
{
535
0
  offsetT diff;
536
0
  fragS *loc4_frag;
537
0
  int loc4_fix, ca;
538
539
0
  loc4_frag = (fragS *) frag->fr_opcode;
540
0
  loc4_fix = (int) frag->fr_offset;
541
542
0
  diff = resolve_symbol_value (frag->fr_symbol);
543
544
0
  ca = frag->fr_subtype >> 3;
545
0
  gas_assert (ca > 0);
546
0
  diff /= ca;
547
0
  switch (frag->fr_subtype & 7)
548
0
    {
549
0
    case 0:
550
0
      gas_assert (diff < 0x40);
551
0
      loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc | diff;
552
0
      break;
553
554
0
    case 1:
555
0
      gas_assert (diff < 0x100);
556
0
      loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc1;
557
0
      frag->fr_literal[frag->fr_fix] = diff;
558
0
      break;
559
560
0
    case 2:
561
0
      gas_assert (diff < 0x10000);
562
0
      loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc2;
563
0
      md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 2);
564
0
      break;
565
566
0
    case 4:
567
0
      md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 4);
568
0
      break;
569
570
0
    case 7:
571
0
      gas_assert (diff == 0);
572
0
      frag->fr_fix -= 8;
573
0
      break;
574
575
0
    default:
576
0
      abort ();
577
0
    }
578
579
0
  frag->fr_fix += frag->fr_subtype & 7;
580
0
  frag->fr_type = rs_fill;
581
0
  frag->fr_subtype = 0;
582
0
  frag->fr_offset = 0;
583
0
}
584
585
void
586
eh_begin (void)
587
28
{
588
28
  memset (&frame, 0, sizeof (frame));
589
28
}