1 /* ehopt.c--optimize gcc exception frame information.
2 Copyright 1998, 2000, 2001, 2003 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>.
5 This file is part of GAS, the GNU Assembler.
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 2, or (at your option)
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.
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, 59 Temple Place - Suite 330, Boston, MA
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. */
29 #include "elf/dwarf2.h"
31 /* Try to optimize gcc 2.8 exception frame information.
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:
38 .4byte .LLCIE1 / Length of Common Information Entry
41 .4byte 0x0 / CIE Identifier Tag
43 .4byte 0xffffffff / CIE Identifier Tag
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
57 .set .LLCIE1,.LECIE1-.LSCIE1 / CIE Length Symbol
58 .4byte .LLFDE1 / FDE Length
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
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
71 .byte 0xd / DW_CFA_def_cfa_register
72 .byte 0x5 / ULEB128 0x5
73 .byte 0x4 / DW_CFA_advance_loc4
75 .byte 0x2e / DW_CFA_GNU_args_size
76 .byte 0x4 / ULEB128 0x4
77 .byte 0x4 / DW_CFA_advance_loc4
79 .byte 0x2e / DW_CFA_GNU_args_size
80 .byte 0x0 / ULEB128 0x0
83 .set .LLFDE1,.LEFDE1-.LSFDE1 / FDE Length Symbol
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. */
93 unsigned code_alignment
;
97 static int get_cie_info (struct cie_info
*);
99 /* Extract information from the CIE. */
102 get_cie_info (struct cie_info
*info
)
108 char augmentation
[10];
110 int code_alignment
= 0;
112 /* We should find the CIE at the start of the section. */
114 #if defined (BFD_ASSEMBLER) || defined (MANY_SEGMENTS)
115 f
= seg_info (now_seg
)->frchainP
->frch_root
;
117 f
= frchain_now
->frch_root
;
120 fix
= seg_info (now_seg
)->frchainP
->fix_root
;
122 fix
= *seg_fix_rootP
;
125 /* Look through the frags of the section to find the code alignment. */
127 /* First make sure that the CIE Identifier Tag is 0/-1. */
129 if (strcmp (segment_name (now_seg
), ".debug_frame") == 0)
135 while (f
!= NULL
&& offset
>= f
->fr_fix
)
141 || f
->fr_fix
- offset
< 4
142 || f
->fr_literal
[offset
] != CIE_id
143 || f
->fr_literal
[offset
+ 1] != CIE_id
144 || f
->fr_literal
[offset
+ 2] != CIE_id
145 || f
->fr_literal
[offset
+ 3] != CIE_id
)
148 /* Next make sure the CIE version number is 1. */
151 while (f
!= NULL
&& offset
>= f
->fr_fix
)
157 || f
->fr_fix
- offset
< 1
158 || f
->fr_literal
[offset
] != 1)
161 /* Skip the augmentation (a null terminated string). */
167 while (f
!= NULL
&& offset
>= f
->fr_fix
)
175 while (offset
< f
->fr_fix
&& f
->fr_literal
[offset
] != '\0')
177 if ((size_t) iaug
< (sizeof augmentation
) - 1)
179 augmentation
[iaug
] = f
->fr_literal
[offset
];
184 if (offset
< f
->fr_fix
)
188 while (f
!= NULL
&& offset
>= f
->fr_fix
)
196 augmentation
[iaug
] = '\0';
197 if (augmentation
[0] == '\0')
199 /* No augmentation. */
201 else if (strcmp (augmentation
, "eh") == 0)
203 /* We have to skip a pointer. Unfortunately, we don't know how
204 large it is. We find out by looking for a matching fixup. */
206 && (fix
->fx_frag
!= f
|| fix
->fx_where
!= offset
))
211 offset
+= fix
->fx_size
;
212 while (f
!= NULL
&& offset
>= f
->fr_fix
)
220 else if (augmentation
[0] != 'z')
223 /* We're now at the code alignment factor, which is a ULEB128. If
224 it isn't a single byte, forget it. */
226 code_alignment
= f
->fr_literal
[offset
] & 0xff;
227 if ((code_alignment
& 0x80) != 0)
230 info
->code_alignment
= code_alignment
;
231 info
->z_augmentation
= (augmentation
[0] == 'z');
236 /* This function is called from emit_expr. It looks for cases which
239 Rather than try to parse all this information as we read it, we
240 look for a single byte DW_CFA_advance_loc4 followed by a 4 byte
241 difference. We turn that into a rs_cfa_advance frag, and handle
242 those frags at the end of the assembly. If the gcc output changes
243 somewhat, this optimization may stop working.
245 This function returns non-zero if it handled the expression and
246 emit_expr should not do anything, or zero otherwise. It can also
247 change *EXP and *PNBYTES. */
250 check_eh_frame (expressionS
*exp
, unsigned int *pnbytes
)
258 state_saw_cie_offset
,
260 state_seeing_aug_size
,
268 struct cie_info cie_info
;
270 symbolS
*size_end_sym
;
278 static struct frame_data eh_frame_data
;
279 static struct frame_data debug_frame_data
;
280 struct frame_data
*d
;
282 /* Don't optimize. */
283 if (flag_traditional_format
)
286 /* Select the proper section data. */
287 if (strcmp (segment_name (now_seg
), ".eh_frame") == 0)
289 else if (strcmp (segment_name (now_seg
), ".debug_frame") == 0)
290 d
= &debug_frame_data
;
294 if (d
->state
>= state_saw_size
&& S_IS_DEFINED (d
->size_end_sym
))
296 /* We have come to the end of the CIE or FDE. See below where
297 we set saw_size. We must check this first because we may now
298 be looking at the next size. */
299 d
->state
= state_idle
;
307 /* This might be the size of the CIE or FDE. We want to know
308 the size so that we don't accidentally optimize across an FDE
309 boundary. We recognize the size in one of two forms: a
310 symbol which will later be defined as a difference, or a
311 subtraction of two symbols. Either way, we can tell when we
312 are at the end of the FDE because the symbol becomes defined
313 (in the case of a subtraction, the end symbol, from which the
314 start symbol is being subtracted). Other ways of describing
315 the size will not be optimized. */
316 if ((exp
->X_op
== O_symbol
|| exp
->X_op
== O_subtract
)
317 && ! S_IS_DEFINED (exp
->X_add_symbol
))
319 d
->state
= state_saw_size
;
320 d
->size_end_sym
= exp
->X_add_symbol
;
326 case state_saw_cie_offset
:
327 /* Assume whatever form it appears in, it appears atomically. */
331 case state_saw_pc_begin
:
332 /* Decide whether we should see an augmentation. */
334 && ! (d
->cie_info_ok
= get_cie_info (&d
->cie_info
)))
335 d
->state
= state_error
;
336 else if (d
->cie_info
.z_augmentation
)
338 d
->state
= state_seeing_aug_size
;
343 d
->state
= state_wait_loc4
;
346 case state_seeing_aug_size
:
347 /* Bytes == -1 means this comes from an leb128 directive. */
348 if ((int)*pnbytes
== -1 && exp
->X_op
== O_constant
)
350 d
->aug_size
= exp
->X_add_number
;
351 d
->state
= state_skipping_aug
;
353 else if (*pnbytes
== 1 && exp
->X_op
== O_constant
)
355 unsigned char byte
= exp
->X_add_number
;
356 d
->aug_size
|= (byte
& 0x7f) << d
->aug_shift
;
358 if ((byte
& 0x80) == 0)
359 d
->state
= state_skipping_aug
;
362 d
->state
= state_error
;
363 if (d
->state
== state_skipping_aug
&& d
->aug_size
== 0)
364 d
->state
= state_wait_loc4
;
367 case state_skipping_aug
:
368 if ((int)*pnbytes
< 0)
369 d
->state
= state_error
;
372 int left
= (d
->aug_size
-= *pnbytes
);
374 d
->state
= state_wait_loc4
;
376 d
->state
= state_error
;
380 case state_wait_loc4
:
382 && exp
->X_op
== O_constant
383 && exp
->X_add_number
== DW_CFA_advance_loc4
)
385 /* This might be a DW_CFA_advance_loc4. Record the frag and the
386 position within the frag, so that we can change it later. */
388 d
->state
= state_saw_loc4
;
389 d
->loc4_frag
= frag_now
;
390 d
->loc4_fix
= frag_now_fix ();
395 d
->state
= state_wait_loc4
;
398 if (exp
->X_op
== O_constant
)
400 /* This is a case which we can optimize. The two symbols being
401 subtracted were in the same frag and the expression was
402 reduced to a constant. We can do the optimization entirely
404 if (d
->cie_info
.code_alignment
> 0
405 && exp
->X_add_number
% d
->cie_info
.code_alignment
== 0
406 && exp
->X_add_number
/ d
->cie_info
.code_alignment
< 0x40)
408 d
->loc4_frag
->fr_literal
[d
->loc4_fix
]
410 | (exp
->X_add_number
/ d
->cie_info
.code_alignment
);
411 /* No more bytes needed. */
414 else if (exp
->X_add_number
< 0x100)
416 d
->loc4_frag
->fr_literal
[d
->loc4_fix
] = DW_CFA_advance_loc1
;
419 else if (exp
->X_add_number
< 0x10000)
421 d
->loc4_frag
->fr_literal
[d
->loc4_fix
] = DW_CFA_advance_loc2
;
425 else if (exp
->X_op
== O_subtract
)
427 /* This is a case we can optimize. The expression was not
428 reduced, so we can not finish the optimization until the end
429 of the assembly. We set up a variant frag which we handle
433 if (d
->cie_info
.code_alignment
> 0)
434 fr_subtype
= d
->cie_info
.code_alignment
<< 3;
438 frag_var (rs_cfa
, 4, 0, fr_subtype
, make_expr_symbol (exp
),
439 d
->loc4_fix
, (char *) d
->loc4_frag
);
445 /* Just skipping everything. */
452 /* The function estimates the size of a rs_cfa variant frag based on
453 the current values of the symbols. It is called before the
454 relaxation loop. We set fr_subtype{0:2} to the expected length. */
457 eh_frame_estimate_size_before_relax (fragS
*frag
)
460 int ca
= frag
->fr_subtype
>> 3;
463 diff
= resolve_symbol_value (frag
->fr_symbol
);
465 if (ca
> 0 && diff
% ca
== 0 && diff
/ ca
< 0x40)
467 else if (diff
< 0x100)
469 else if (diff
< 0x10000)
474 frag
->fr_subtype
= (frag
->fr_subtype
& ~7) | ret
;
479 /* This function relaxes a rs_cfa variant frag based on the current
480 values of the symbols. fr_subtype{0:2} is the current length of
481 the frag. This returns the change in frag length. */
484 eh_frame_relax_frag (fragS
*frag
)
486 int oldsize
, newsize
;
488 oldsize
= frag
->fr_subtype
& 7;
489 newsize
= eh_frame_estimate_size_before_relax (frag
);
490 return newsize
- oldsize
;
493 /* This function converts a rs_cfa variant frag into a normal fill
494 frag. This is called after all relaxation has been done.
495 fr_subtype{0:2} will be the desired length of the frag. */
498 eh_frame_convert_frag (fragS
*frag
)
504 loc4_frag
= (fragS
*) frag
->fr_opcode
;
505 loc4_fix
= (int) frag
->fr_offset
;
507 diff
= resolve_symbol_value (frag
->fr_symbol
);
509 switch (frag
->fr_subtype
& 7)
513 int ca
= frag
->fr_subtype
>> 3;
514 assert (ca
> 0 && diff
% ca
== 0 && diff
/ ca
< 0x40);
515 loc4_frag
->fr_literal
[loc4_fix
] = DW_CFA_advance_loc
| (diff
/ ca
);
520 assert (diff
< 0x100);
521 loc4_frag
->fr_literal
[loc4_fix
] = DW_CFA_advance_loc1
;
522 frag
->fr_literal
[frag
->fr_fix
] = diff
;
526 assert (diff
< 0x10000);
527 loc4_frag
->fr_literal
[loc4_fix
] = DW_CFA_advance_loc2
;
528 md_number_to_chars (frag
->fr_literal
+ frag
->fr_fix
, diff
, 2);
532 md_number_to_chars (frag
->fr_literal
+ frag
->fr_fix
, diff
, 4);
536 frag
->fr_fix
+= frag
->fr_subtype
& 7;
537 frag
->fr_type
= rs_fill
;
538 frag
->fr_subtype
= 0;