1 /* ehopt.c--optimize gcc exception frame information.
2 Copyright (C) 1998 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 section. Simple information for a function with no
35 exceptions looks like this:
38 .4byte .LLCIE1 / Length of Common Information Entry
40 .4byte 0x0 / CIE Identifier Tag
41 .byte 0x1 / CIE Version
42 .byte 0x0 / CIE Augmentation (none)
43 .byte 0x1 / ULEB128 0x1 (CIE Code Alignment Factor)
44 .byte 0x7c / SLEB128 -4 (CIE Data Alignment Factor)
45 .byte 0x8 / CIE RA Column
46 .byte 0xc / DW_CFA_def_cfa
47 .byte 0x4 / ULEB128 0x4
48 .byte 0x4 / ULEB128 0x4
49 .byte 0x88 / DW_CFA_offset, column 0x8
50 .byte 0x1 / ULEB128 0x1
53 .set .LLCIE1,.LECIE1-.LSCIE1 / CIE Length Symbol
54 .4byte .LLFDE1 / FDE Length
56 .4byte .LSFDE1-__FRAME_BEGIN__ / FDE CIE offset
57 .4byte .LFB1 / FDE initial location
58 .4byte .LFE1-.LFB1 / FDE address range
59 .byte 0x4 / DW_CFA_advance_loc4
61 .byte 0xe / DW_CFA_def_cfa_offset
62 .byte 0x8 / ULEB128 0x8
63 .byte 0x85 / DW_CFA_offset, column 0x5
64 .byte 0x2 / ULEB128 0x2
65 .byte 0x4 / DW_CFA_advance_loc4
67 .byte 0xd / DW_CFA_def_cfa_register
68 .byte 0x5 / ULEB128 0x5
69 .byte 0x4 / DW_CFA_advance_loc4
71 .byte 0x2e / DW_CFA_GNU_args_size
72 .byte 0x4 / ULEB128 0x4
73 .byte 0x4 / DW_CFA_advance_loc4
75 .byte 0x2e / DW_CFA_GNU_args_size
76 .byte 0x0 / ULEB128 0x0
79 .set .LLFDE1,.LEFDE1-.LSFDE1 / FDE Length Symbol
81 The immediate issue we can address in the assembler is the
82 DW_CFA_advance_loc4 followed by a four byte value. The value is
83 the difference of two addresses in the function. Since gcc does
84 not know this value, it always uses four bytes. We will know the
85 value at the end of assembly, so we can do better. */
87 static int eh_frame_code_alignment
PARAMS ((void));
89 /* Get the code alignment factor from the CIE. */
92 eh_frame_code_alignment ()
94 static int code_alignment
;
96 subsegT current_subseg
;
100 if (code_alignment
!= 0)
101 return code_alignment
;
103 /* We should find the CIE at the start of the .eh_frame section. */
105 current_seg
= now_seg
;
106 current_subseg
= now_subseg
;
107 subseg_new (".eh_frame", 0);
108 f
= seg_info (now_seg
)->frchainP
->frch_root
;
109 subseg_set (current_seg
, current_subseg
);
111 /* Look through the frags of the section to find the code alignment. */
113 /* First make sure that the CIE Identifier Tag is 0. */
116 while (f
!= NULL
&& offset
>= f
->fr_fix
)
122 || f
->fr_fix
- offset
< 4
123 || f
->fr_literal
[offset
] != 0
124 || f
->fr_literal
[offset
+ 1] != 0
125 || f
->fr_literal
[offset
+ 2] != 0
126 || f
->fr_literal
[offset
+ 3] != 0)
132 /* Next make sure the CIE version number is 1. */
135 while (f
!= NULL
&& offset
>= f
->fr_fix
)
141 || f
->fr_fix
- offset
< 1
142 || f
->fr_literal
[offset
] != 1)
148 /* Skip the augmentation (a null terminated string). */
153 while (f
!= NULL
&& offset
>= f
->fr_fix
)
163 while (offset
< f
->fr_fix
&& f
->fr_literal
[offset
] != '\0')
165 if (offset
< f
->fr_fix
)
169 while (f
!= NULL
&& offset
>= f
->fr_fix
)
180 /* We're now at the code alignment factor, which is a ULEB128. If
181 it isn't a single byte, forget it. */
183 code_alignment
= f
->fr_literal
[offset
] & 0xff;
184 if ((code_alignment
& 0x80) != 0 || code_alignment
== 0)
190 return code_alignment
;
193 /* This function is called from emit_expr. It looks for cases which
196 Rather than try to parse all this information as we read it, we
197 look for a single byte DW_CFA_advance_loc4 followed by a 4 byte
198 difference. We turn that into a rs_cfa_advance frag, and handle
199 those frags at the end of the assembly. If the gcc output changes
200 somewhat, this optimization may stop working.
202 This function returns non-zero if it handled the expression and
203 emit_expr should not do anything, or zero otherwise. It can also
204 change *EXP and *PNBYTES. */
207 check_eh_frame (exp
, pnbytes
)
209 unsigned int *pnbytes
;
211 static int saw_advance_loc4
;
212 static fragS
*loc4_frag
;
215 if (flag_traditional_format
)
217 /* Don't optimize. */
219 else if (strcmp (segment_name (now_seg
), ".eh_frame") != 0)
220 saw_advance_loc4
= 0;
221 else if (*pnbytes
== 1
222 && exp
->X_op
== O_constant
223 && exp
->X_add_number
== DW_CFA_advance_loc4
)
225 /* This might be a DW_CFA_advance_loc4. Record the frag and the
226 position within the frag, so that we can change it later. */
227 saw_advance_loc4
= 1;
229 loc4_frag
= frag_now
;
230 loc4_fix
= frag_now_fix ();
232 else if (saw_advance_loc4
234 && exp
->X_op
== O_constant
)
238 /* This is a case which we can optimize. The two symbols being
239 subtracted were in the same frag and the expression was
240 reduced to a constant. We can do the optimization entirely
243 saw_advance_loc4
= 0;
245 ca
= eh_frame_code_alignment ();
248 /* Don't optimize. */
250 else if (exp
->X_add_number
% ca
== 0
251 && exp
->X_add_number
/ ca
< 0x40)
253 loc4_frag
->fr_literal
[loc4_fix
]
254 = DW_CFA_advance_loc
| (exp
->X_add_number
/ ca
);
255 /* No more bytes needed. */
258 else if (exp
->X_add_number
< 0x100)
260 loc4_frag
->fr_literal
[loc4_fix
] = DW_CFA_advance_loc1
;
263 else if (exp
->X_add_number
< 0x10000)
265 loc4_frag
->fr_literal
[loc4_fix
] = DW_CFA_advance_loc2
;
269 else if (saw_advance_loc4
271 && exp
->X_op
== O_subtract
)
274 /* This is a case we can optimize. The expression was not
275 reduced, so we can not finish the optimization until the end
276 of the assembly. We set up a variant frag which we handle
279 saw_advance_loc4
= 0;
281 frag_var (rs_cfa
, 4, 0, 0, make_expr_symbol (exp
),
282 loc4_fix
, (char *) loc4_frag
);
285 saw_advance_loc4
= 0;
290 /* The function estimates the size of a rs_cfa variant frag based on
291 the current values of the symbols. It is called before the
292 relaxation loop. We set fr_subtype to the expected length. */
295 eh_frame_estimate_size_before_relax (frag
)
302 ca
= eh_frame_code_alignment ();
303 diff
= resolve_symbol_value (frag
->fr_symbol
, 0);
307 else if (diff
% ca
== 0 && diff
/ ca
< 0x40)
309 else if (diff
< 0x100)
311 else if (diff
< 0x10000)
316 frag
->fr_subtype
= ret
;
321 /* This function relaxes a rs_cfa variant frag based on the current
322 values of the symbols. fr_subtype is the current length of the
323 frag. This returns the change in frag length. */
326 eh_frame_relax_frag (frag
)
329 int oldsize
, newsize
;
331 oldsize
= frag
->fr_subtype
;
332 newsize
= eh_frame_estimate_size_before_relax (frag
);
333 return newsize
- oldsize
;
336 /* This function converts a rs_cfa variant frag into a normal fill
337 frag. This is called after all relaxation has been done.
338 fr_subtype will be the desired length of the frag. */
341 eh_frame_convert_frag (frag
)
348 loc4_frag
= (fragS
*) frag
->fr_opcode
;
349 loc4_fix
= (int) frag
->fr_offset
;
351 diff
= resolve_symbol_value (frag
->fr_symbol
, 1);
353 if (frag
->fr_subtype
== 0)
357 ca
= eh_frame_code_alignment ();
358 assert (ca
> 0 && diff
% ca
== 0 && diff
/ ca
< 0x40);
359 loc4_frag
->fr_literal
[loc4_fix
] = DW_CFA_advance_loc
| (diff
/ ca
);
361 else if (frag
->fr_subtype
== 1)
363 assert (diff
< 0x100);
364 loc4_frag
->fr_literal
[loc4_fix
] = DW_CFA_advance_loc1
;
365 frag
->fr_literal
[frag
->fr_fix
] = diff
;
367 else if (frag
->fr_subtype
== 2)
369 assert (diff
< 0x10000);
370 loc4_frag
->fr_literal
[loc4_fix
] = DW_CFA_advance_loc2
;
371 md_number_to_chars (frag
->fr_literal
+ frag
->fr_fix
, diff
, 2);
374 md_number_to_chars (frag
->fr_literal
+ frag
->fr_fix
, diff
, 4);
376 frag
->fr_fix
+= frag
->fr_subtype
;
377 frag
->fr_type
= rs_fill
;
This page took 0.040077 seconds and 5 git commands to generate.