1 /* ehopt.c--optimize gcc exception frame information.
2 Copyright 1998, 2000, 2001, 2003, 2005, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
25 #include "struc-symbol.h"
27 /* We include this ELF file, even though we may not be assembling for
28 ELF, since the exception frame information is always in a format
29 derived from DWARF. */
33 /* Try to optimize gcc 2.8 exception frame information.
35 Exception frame information is emitted for every function in the
36 .eh_frame or .debug_frame sections. Simple information for a function
37 with no exceptions looks like this:
40 .4byte .LLCIE1 / Length of Common Information Entry
43 .4byte 0x0 / CIE Identifier Tag
45 .4byte 0xffffffff / CIE Identifier Tag
47 .byte 0x1 / CIE Version
48 .byte 0x0 / CIE Augmentation (none)
49 .byte 0x1 / ULEB128 0x1 (CIE Code Alignment Factor)
50 .byte 0x7c / SLEB128 -4 (CIE Data Alignment Factor)
51 .byte 0x8 / CIE RA Column
52 .byte 0xc / DW_CFA_def_cfa
53 .byte 0x4 / ULEB128 0x4
54 .byte 0x4 / ULEB128 0x4
55 .byte 0x88 / DW_CFA_offset, column 0x8
56 .byte 0x1 / ULEB128 0x1
59 .set .LLCIE1,.LECIE1-.LSCIE1 / CIE Length Symbol
60 .4byte .LLFDE1 / FDE Length
62 .4byte .LSFDE1-__FRAME_BEGIN__ / FDE CIE offset
63 .4byte .LFB1 / FDE initial location
64 .4byte .LFE1-.LFB1 / FDE address range
65 .byte 0x4 / DW_CFA_advance_loc4
67 .byte 0xe / DW_CFA_def_cfa_offset
68 .byte 0x8 / ULEB128 0x8
69 .byte 0x85 / DW_CFA_offset, column 0x5
70 .byte 0x2 / ULEB128 0x2
71 .byte 0x4 / DW_CFA_advance_loc4
73 .byte 0xd / DW_CFA_def_cfa_register
74 .byte 0x5 / ULEB128 0x5
75 .byte 0x4 / DW_CFA_advance_loc4
77 .byte 0x2e / DW_CFA_GNU_args_size
78 .byte 0x4 / ULEB128 0x4
79 .byte 0x4 / DW_CFA_advance_loc4
81 .byte 0x2e / DW_CFA_GNU_args_size
82 .byte 0x0 / ULEB128 0x0
85 .set .LLFDE1,.LEFDE1-.LSFDE1 / FDE Length Symbol
87 The immediate issue we can address in the assembler is the
88 DW_CFA_advance_loc4 followed by a four byte value. The value is
89 the difference of two addresses in the function. Since gcc does
90 not know this value, it always uses four bytes. We will know the
91 value at the end of assembly, so we can do better. */
95 unsigned code_alignment
;
99 static int get_cie_info (struct cie_info
*);
101 /* Extract information from the CIE. */
104 get_cie_info (struct cie_info
*info
)
110 char augmentation
[10];
112 int code_alignment
= 0;
114 /* We should find the CIE at the start of the section. */
116 f
= seg_info (now_seg
)->frchainP
->frch_root
;
117 fix
= seg_info (now_seg
)->frchainP
->fix_root
;
119 /* Look through the frags of the section to find the code alignment. */
121 /* First make sure that the CIE Identifier Tag is 0/-1. */
123 if (strncmp (segment_name (now_seg
), ".debug_frame", 12) == 0)
129 while (f
!= NULL
&& offset
>= f
->fr_fix
)
135 || f
->fr_fix
- offset
< 4
136 || f
->fr_literal
[offset
] != CIE_id
137 || f
->fr_literal
[offset
+ 1] != CIE_id
138 || f
->fr_literal
[offset
+ 2] != CIE_id
139 || f
->fr_literal
[offset
+ 3] != CIE_id
)
142 /* Next make sure the CIE version number is 1. */
145 while (f
!= NULL
&& offset
>= f
->fr_fix
)
151 || f
->fr_fix
- offset
< 1
152 || f
->fr_literal
[offset
] != 1)
155 /* Skip the augmentation (a null terminated string). */
161 while (f
!= NULL
&& offset
>= f
->fr_fix
)
169 while (offset
< f
->fr_fix
&& f
->fr_literal
[offset
] != '\0')
171 if ((size_t) iaug
< (sizeof augmentation
) - 1)
173 augmentation
[iaug
] = f
->fr_literal
[offset
];
178 if (offset
< f
->fr_fix
)
182 while (f
!= NULL
&& offset
>= f
->fr_fix
)
190 augmentation
[iaug
] = '\0';
191 if (augmentation
[0] == '\0')
193 /* No augmentation. */
195 else if (strcmp (augmentation
, "eh") == 0)
197 /* We have to skip a pointer. Unfortunately, we don't know how
198 large it is. We find out by looking for a matching fixup. */
200 && (fix
->fx_frag
!= f
|| fix
->fx_where
!= offset
))
205 offset
+= fix
->fx_size
;
206 while (f
!= NULL
&& offset
>= f
->fr_fix
)
214 else if (augmentation
[0] != 'z')
217 /* We're now at the code alignment factor, which is a ULEB128. If
218 it isn't a single byte, forget it. */
220 code_alignment
= f
->fr_literal
[offset
] & 0xff;
221 if ((code_alignment
& 0x80) != 0)
224 info
->code_alignment
= code_alignment
;
225 info
->z_augmentation
= (augmentation
[0] == 'z');
234 state_saw_cie_offset
,
236 state_seeing_aug_size
,
243 /* This function is called from emit_expr. It looks for cases which
246 Rather than try to parse all this information as we read it, we
247 look for a single byte DW_CFA_advance_loc4 followed by a 4 byte
248 difference. We turn that into a rs_cfa_advance frag, and handle
249 those frags at the end of the assembly. If the gcc output changes
250 somewhat, this optimization may stop working.
252 This function returns non-zero if it handled the expression and
253 emit_expr should not do anything, or zero otherwise. It can also
254 change *EXP and *PNBYTES. */
257 check_eh_frame (expressionS
*exp
, unsigned int *pnbytes
)
261 enum frame_state state
;
264 struct cie_info cie_info
;
266 symbolS
*size_end_sym
;
274 static struct frame_data eh_frame_data
;
275 static struct frame_data debug_frame_data
;
276 struct frame_data
*d
;
278 /* Don't optimize. */
279 if (flag_traditional_format
)
282 #ifdef md_allow_eh_opt
283 if (! md_allow_eh_opt
)
287 /* Select the proper section data. */
288 if (strncmp (segment_name (now_seg
), ".eh_frame", 9) == 0
289 && segment_name (now_seg
)[9] != '_')
291 else if (strncmp (segment_name (now_seg
), ".debug_frame", 12) == 0)
292 d
= &debug_frame_data
;
296 if (d
->state
>= state_saw_size
&& S_IS_DEFINED (d
->size_end_sym
))
298 /* We have come to the end of the CIE or FDE. See below where
299 we set saw_size. We must check this first because we may now
300 be looking at the next size. */
301 d
->state
= state_idle
;
309 /* This might be the size of the CIE or FDE. We want to know
310 the size so that we don't accidentally optimize across an FDE
311 boundary. We recognize the size in one of two forms: a
312 symbol which will later be defined as a difference, or a
313 subtraction of two symbols. Either way, we can tell when we
314 are at the end of the FDE because the symbol becomes defined
315 (in the case of a subtraction, the end symbol, from which the
316 start symbol is being subtracted). Other ways of describing
317 the size will not be optimized. */
318 if ((exp
->X_op
== O_symbol
|| exp
->X_op
== O_subtract
)
319 && ! S_IS_DEFINED (exp
->X_add_symbol
))
321 d
->state
= state_saw_size
;
322 d
->size_end_sym
= exp
->X_add_symbol
;
328 case state_saw_cie_offset
:
329 /* Assume whatever form it appears in, it appears atomically. */
330 d
->state
= (enum frame_state
) (d
->state
+ 1);
333 case state_saw_pc_begin
:
334 /* Decide whether we should see an augmentation. */
336 && ! (d
->cie_info_ok
= get_cie_info (&d
->cie_info
)))
337 d
->state
= state_error
;
338 else if (d
->cie_info
.z_augmentation
)
340 d
->state
= state_seeing_aug_size
;
345 d
->state
= state_wait_loc4
;
348 case state_seeing_aug_size
:
349 /* Bytes == -1 means this comes from an leb128 directive. */
350 if ((int)*pnbytes
== -1 && exp
->X_op
== O_constant
)
352 d
->aug_size
= exp
->X_add_number
;
353 d
->state
= state_skipping_aug
;
355 else if (*pnbytes
== 1 && exp
->X_op
== O_constant
)
357 unsigned char byte
= exp
->X_add_number
;
358 d
->aug_size
|= (byte
& 0x7f) << d
->aug_shift
;
360 if ((byte
& 0x80) == 0)
361 d
->state
= state_skipping_aug
;
364 d
->state
= state_error
;
365 if (d
->state
== state_skipping_aug
&& d
->aug_size
== 0)
366 d
->state
= state_wait_loc4
;
369 case state_skipping_aug
:
370 if ((int)*pnbytes
< 0)
371 d
->state
= state_error
;
374 int left
= (d
->aug_size
-= *pnbytes
);
376 d
->state
= state_wait_loc4
;
378 d
->state
= state_error
;
382 case state_wait_loc4
:
384 && exp
->X_op
== O_constant
385 && exp
->X_add_number
== DW_CFA_advance_loc4
)
387 /* This might be a DW_CFA_advance_loc4. Record the frag and the
388 position within the frag, so that we can change it later. */
390 d
->state
= state_saw_loc4
;
391 d
->loc4_frag
= frag_now
;
392 d
->loc4_fix
= frag_now_fix ();
397 d
->state
= state_wait_loc4
;
400 if (exp
->X_op
== O_constant
)
402 /* This is a case which we can optimize. The two symbols being
403 subtracted were in the same frag and the expression was
404 reduced to a constant. We can do the optimization entirely
406 if (exp
->X_add_number
< 0x40)
408 d
->loc4_frag
->fr_literal
[d
->loc4_fix
]
409 = DW_CFA_advance_loc
| exp
->X_add_number
;
410 /* No more bytes needed. */
413 else if (exp
->X_add_number
< 0x100)
415 d
->loc4_frag
->fr_literal
[d
->loc4_fix
] = DW_CFA_advance_loc1
;
418 else if (exp
->X_add_number
< 0x10000)
420 d
->loc4_frag
->fr_literal
[d
->loc4_fix
] = DW_CFA_advance_loc2
;
424 else if (exp
->X_op
== O_subtract
&& d
->cie_info
.code_alignment
== 1)
426 /* This is a case we can optimize. The expression was not
427 reduced, so we can not finish the optimization until the end
428 of the assembly. We set up a variant frag which we handle
430 frag_var (rs_cfa
, 4, 0, 1 << 3, make_expr_symbol (exp
),
431 d
->loc4_fix
, (char *) d
->loc4_frag
);
434 else if ((exp
->X_op
== O_divide
435 || exp
->X_op
== O_right_shift
)
436 && d
->cie_info
.code_alignment
> 1)
438 if (exp
->X_add_symbol
->bsym
439 && exp
->X_op_symbol
->bsym
440 && exp
->X_add_symbol
->sy_value
.X_op
== O_subtract
441 && exp
->X_op_symbol
->sy_value
.X_op
== O_constant
442 && ((exp
->X_op
== O_divide
443 ? exp
->X_op_symbol
->sy_value
.X_add_number
444 : (offsetT
) 1 << exp
->X_op_symbol
->sy_value
.X_add_number
)
445 == (offsetT
) d
->cie_info
.code_alignment
))
447 /* This is a case we can optimize as well. The expression was
448 not reduced, so we can not finish the optimization until the
449 end of the assembly. We set up a variant frag which we
451 frag_var (rs_cfa
, 4, 0, d
->cie_info
.code_alignment
<< 3,
452 make_expr_symbol (&exp
->X_add_symbol
->sy_value
),
453 d
->loc4_fix
, (char *) d
->loc4_frag
);
460 /* Just skipping everything. */
467 /* The function estimates the size of a rs_cfa variant frag based on
468 the current values of the symbols. It is called before the
469 relaxation loop. We set fr_subtype{0:2} to the expected length. */
472 eh_frame_estimate_size_before_relax (fragS
*frag
)
475 int ca
= frag
->fr_subtype
>> 3;
478 diff
= resolve_symbol_value (frag
->fr_symbol
);
484 else if (diff
< 0x100)
486 else if (diff
< 0x10000)
491 frag
->fr_subtype
= (frag
->fr_subtype
& ~7) | ret
;
496 /* This function relaxes a rs_cfa variant frag based on the current
497 values of the symbols. fr_subtype{0:2} is the current length of
498 the frag. This returns the change in frag length. */
501 eh_frame_relax_frag (fragS
*frag
)
503 int oldsize
, newsize
;
505 oldsize
= frag
->fr_subtype
& 7;
506 newsize
= eh_frame_estimate_size_before_relax (frag
);
507 return newsize
- oldsize
;
510 /* This function converts a rs_cfa variant frag into a normal fill
511 frag. This is called after all relaxation has been done.
512 fr_subtype{0:2} will be the desired length of the frag. */
515 eh_frame_convert_frag (fragS
*frag
)
521 loc4_frag
= (fragS
*) frag
->fr_opcode
;
522 loc4_fix
= (int) frag
->fr_offset
;
524 diff
= resolve_symbol_value (frag
->fr_symbol
);
526 ca
= frag
->fr_subtype
>> 3;
529 switch (frag
->fr_subtype
& 7)
532 gas_assert (diff
< 0x40);
533 loc4_frag
->fr_literal
[loc4_fix
] = DW_CFA_advance_loc
| diff
;
537 gas_assert (diff
< 0x100);
538 loc4_frag
->fr_literal
[loc4_fix
] = DW_CFA_advance_loc1
;
539 frag
->fr_literal
[frag
->fr_fix
] = diff
;
543 gas_assert (diff
< 0x10000);
544 loc4_frag
->fr_literal
[loc4_fix
] = DW_CFA_advance_loc2
;
545 md_number_to_chars (frag
->fr_literal
+ frag
->fr_fix
, diff
, 2);
549 md_number_to_chars (frag
->fr_literal
+ frag
->fr_fix
, diff
, 4);
553 frag
->fr_fix
+= frag
->fr_subtype
& 7;
554 frag
->fr_type
= rs_fill
;
555 frag
->fr_subtype
= 0;