* config/tc-mips.c (md_begin): If mips_cpu is set, then use it as
[deliverable/binutils-gdb.git] / gas / ehopt.c
CommitLineData
ffd652c3
ILT
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>.
4
5This file is part of GAS, the GNU Assembler.
6
7GAS is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GAS is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GAS; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, 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 "elf/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 section. Simple information for a function with no
35 exceptions looks like this:
36
37__FRAME_BEGIN__:
38 .4byte .LLCIE1 / Length of Common Information Entry
39.LSCIE1:
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
51 .align 4
52.LECIE1:
53 .set .LLCIE1,.LECIE1-.LSCIE1 / CIE Length Symbol
54 .4byte .LLFDE1 / FDE Length
55.LSFDE1:
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
60 .4byte .LCFI0-.LFB1
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
66 .4byte .LCFI1-.LCFI0
67 .byte 0xd / DW_CFA_def_cfa_register
68 .byte 0x5 / ULEB128 0x5
69 .byte 0x4 / DW_CFA_advance_loc4
70 .4byte .LCFI2-.LCFI1
71 .byte 0x2e / DW_CFA_GNU_args_size
72 .byte 0x4 / ULEB128 0x4
73 .byte 0x4 / DW_CFA_advance_loc4
74 .4byte .LCFI3-.LCFI2
75 .byte 0x2e / DW_CFA_GNU_args_size
76 .byte 0x0 / ULEB128 0x0
77 .align 4
78.LEFDE1:
79 .set .LLFDE1,.LEFDE1-.LSFDE1 / FDE Length Symbol
80
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. */
86
87static int eh_frame_code_alignment PARAMS ((void));
88
89/* Get the code alignment factor from the CIE. */
90
91static int
92eh_frame_code_alignment ()
93{
94 static int code_alignment;
95 segT current_seg;
96 subsegT current_subseg;
97 fragS *f;
1430b6ed 98 fixS *fix;
ffd652c3 99 int offset;
1430b6ed 100 int eh_state;
ffd652c3
ILT
101
102 if (code_alignment != 0)
103 return code_alignment;
104
105 /* We should find the CIE at the start of the .eh_frame section. */
106
107 current_seg = now_seg;
108 current_subseg = now_subseg;
109 subseg_new (".eh_frame", 0);
110 f = seg_info (now_seg)->frchainP->frch_root;
1430b6ed 111 fix = seg_info (now_seg)->frchainP->fix_root;
ffd652c3
ILT
112 subseg_set (current_seg, current_subseg);
113
114 /* Look through the frags of the section to find the code alignment. */
115
116 /* First make sure that the CIE Identifier Tag is 0. */
117
118 offset = 4;
119 while (f != NULL && offset >= f->fr_fix)
120 {
121 offset -= f->fr_fix;
122 f = f->fr_next;
123 }
124 if (f == NULL
125 || f->fr_fix - offset < 4
126 || f->fr_literal[offset] != 0
127 || f->fr_literal[offset + 1] != 0
128 || f->fr_literal[offset + 2] != 0
129 || f->fr_literal[offset + 3] != 0)
130 {
131 code_alignment = -1;
132 return -1;
133 }
134
135 /* Next make sure the CIE version number is 1. */
136
137 offset += 4;
138 while (f != NULL && offset >= f->fr_fix)
139 {
140 offset -= f->fr_fix;
141 f = f->fr_next;
142 }
143 if (f == NULL
144 || f->fr_fix - offset < 1
145 || f->fr_literal[offset] != 1)
146 {
147 code_alignment = -1;
148 return -1;
149 }
150
151 /* Skip the augmentation (a null terminated string). */
152
153 ++offset;
1430b6ed 154 eh_state = 0;
ffd652c3
ILT
155 while (1)
156 {
157 while (f != NULL && offset >= f->fr_fix)
158 {
159 offset -= f->fr_fix;
160 f = f->fr_next;
161 }
162 if (f == NULL)
163 {
164 code_alignment = -1;
165 return -1;
166 }
167 while (offset < f->fr_fix && f->fr_literal[offset] != '\0')
1430b6ed
ILT
168 {
169 switch (eh_state)
170 {
171 case 0:
172 if (f->fr_literal[offset] == 'e')
173 eh_state = 1;
174 break;
175 case 1:
176 if (f->fr_literal[offset] == 'h')
177 eh_state = 2;
178 break;
179 default:
180 eh_state = 3;
181 break;
182 }
183 ++offset;
184 }
ffd652c3
ILT
185 if (offset < f->fr_fix)
186 break;
187 }
188 ++offset;
189 while (f != NULL && offset >= f->fr_fix)
190 {
191 offset -= f->fr_fix;
192 f = f->fr_next;
193 }
194 if (f == NULL)
195 {
196 code_alignment = -1;
197 return -1;
198 }
199
1430b6ed
ILT
200 /* If the augmentation field is "eh", then we have to skip a
201 pointer. Unfortunately, we don't know how large it is. We find
202 out by looking for a matching fixup. */
203 if (eh_state == 2)
204 {
205 while (fix != NULL
206 && (fix->fx_frag != f || fix->fx_where != offset))
207 fix = fix->fx_next;
208 if (fix == NULL)
209 offset += 4;
210 else
211 offset += fix->fx_size;
212 while (f != NULL && offset >= f->fr_fix)
213 {
214 offset -= f->fr_fix;
215 f = f->fr_next;
216 }
217 if (f == NULL)
218 {
219 code_alignment = -1;
220 return -1;
221 }
222 }
223
ffd652c3
ILT
224 /* We're now at the code alignment factor, which is a ULEB128. If
225 it isn't a single byte, forget it. */
226
227 code_alignment = f->fr_literal[offset] & 0xff;
228 if ((code_alignment & 0x80) != 0 || code_alignment == 0)
229 {
230 code_alignment = -1;
231 return -1;
232 }
233
234 return code_alignment;
235}
236
237/* This function is called from emit_expr. It looks for cases which
238 we can optimize.
239
240 Rather than try to parse all this information as we read it, we
241 look for a single byte DW_CFA_advance_loc4 followed by a 4 byte
242 difference. We turn that into a rs_cfa_advance frag, and handle
243 those frags at the end of the assembly. If the gcc output changes
244 somewhat, this optimization may stop working.
245
246 This function returns non-zero if it handled the expression and
247 emit_expr should not do anything, or zero otherwise. It can also
248 change *EXP and *PNBYTES. */
249
250int
251check_eh_frame (exp, pnbytes)
252 expressionS *exp;
253 unsigned int *pnbytes;
254{
255 static int saw_advance_loc4;
256 static fragS *loc4_frag;
257 static int loc4_fix;
258
259 if (flag_traditional_format)
260 {
261 /* Don't optimize. */
262 }
263 else if (strcmp (segment_name (now_seg), ".eh_frame") != 0)
264 saw_advance_loc4 = 0;
265 else if (*pnbytes == 1
266 && exp->X_op == O_constant
267 && exp->X_add_number == DW_CFA_advance_loc4)
268 {
269 /* This might be a DW_CFA_advance_loc4. Record the frag and the
270 position within the frag, so that we can change it later. */
271 saw_advance_loc4 = 1;
272 frag_grow (1);
273 loc4_frag = frag_now;
274 loc4_fix = frag_now_fix ();
275 }
276 else if (saw_advance_loc4
277 && *pnbytes == 4
278 && exp->X_op == O_constant)
279 {
280 int ca;
281
282 /* This is a case which we can optimize. The two symbols being
283 subtracted were in the same frag and the expression was
284 reduced to a constant. We can do the optimization entirely
285 in this function. */
286
287 saw_advance_loc4 = 0;
288
289 ca = eh_frame_code_alignment ();
290 if (ca < 0)
291 {
292 /* Don't optimize. */
293 }
294 else if (exp->X_add_number % ca == 0
1430b6ed 295 && exp->X_add_number / ca < 0x40)
ffd652c3
ILT
296 {
297 loc4_frag->fr_literal[loc4_fix]
298 = DW_CFA_advance_loc | (exp->X_add_number / ca);
299 /* No more bytes needed. */
300 return 1;
301 }
302 else if (exp->X_add_number < 0x100)
303 {
304 loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc1;
305 *pnbytes = 1;
306 }
307 else if (exp->X_add_number < 0x10000)
308 {
309 loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc2;
310 *pnbytes = 2;
311 }
312 }
313 else if (saw_advance_loc4
314 && *pnbytes == 4
315 && exp->X_op == O_subtract)
316 {
317
318 /* This is a case we can optimize. The expression was not
319 reduced, so we can not finish the optimization until the end
320 of the assembly. We set up a variant frag which we handle
321 later. */
322
323 saw_advance_loc4 = 0;
324
325 frag_var (rs_cfa, 4, 0, 0, make_expr_symbol (exp),
326 loc4_fix, (char *) loc4_frag);
1430b6ed
ILT
327
328 return 1;
ffd652c3
ILT
329 }
330 else
331 saw_advance_loc4 = 0;
332
333 return 0;
334}
335
336/* The function estimates the size of a rs_cfa variant frag based on
337 the current values of the symbols. It is called before the
338 relaxation loop. We set fr_subtype to the expected length. */
339
340int
341eh_frame_estimate_size_before_relax (frag)
342 fragS *frag;
343{
344 int ca;
345 offsetT diff;
346 int ret;
347
348 ca = eh_frame_code_alignment ();
349 diff = resolve_symbol_value (frag->fr_symbol, 0);
350
351 if (ca < 0)
352 ret = 4;
353 else if (diff % ca == 0 && diff / ca < 0x40)
354 ret = 0;
355 else if (diff < 0x100)
356 ret = 1;
357 else if (diff < 0x10000)
358 ret = 2;
359 else
360 ret = 4;
361
362 frag->fr_subtype = ret;
363
364 return ret;
365}
366
367/* This function relaxes a rs_cfa variant frag based on the current
368 values of the symbols. fr_subtype is the current length of the
369 frag. This returns the change in frag length. */
370
371int
372eh_frame_relax_frag (frag)
373 fragS *frag;
374{
375 int oldsize, newsize;
376
377 oldsize = frag->fr_subtype;
378 newsize = eh_frame_estimate_size_before_relax (frag);
379 return newsize - oldsize;
380}
381
382/* This function converts a rs_cfa variant frag into a normal fill
383 frag. This is called after all relaxation has been done.
384 fr_subtype will be the desired length of the frag. */
385
386void
387eh_frame_convert_frag (frag)
388 fragS *frag;
389{
390 offsetT diff;
391 fragS *loc4_frag;
392 int loc4_fix;
393
394 loc4_frag = (fragS *) frag->fr_opcode;
395 loc4_fix = (int) frag->fr_offset;
396
397 diff = resolve_symbol_value (frag->fr_symbol, 1);
398
399 if (frag->fr_subtype == 0)
400 {
401 int ca;
402
403 ca = eh_frame_code_alignment ();
404 assert (ca > 0 && diff % ca == 0 && diff / ca < 0x40);
405 loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc | (diff / ca);
406 }
407 else if (frag->fr_subtype == 1)
408 {
409 assert (diff < 0x100);
410 loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc1;
411 frag->fr_literal[frag->fr_fix] = diff;
412 }
413 else if (frag->fr_subtype == 2)
414 {
415 assert (diff < 0x10000);
416 loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc2;
417 md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 2);
418 }
419 else
420 md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 4);
421
422 frag->fr_fix += frag->fr_subtype;
423 frag->fr_type = rs_fill;
1430b6ed 424 frag->fr_offset = 0;
ffd652c3 425}
This page took 0.037225 seconds and 4 git commands to generate.