daily update
[deliverable/binutils-gdb.git] / gas / ehopt.c
CommitLineData
252b5132 1/* ehopt.c--optimize gcc exception frame information.
395e8345
JJ
2 Copyright 1998, 2000, 2001, 2003, 2005, 2007, 2008
3 Free Software Foundation, Inc.
252b5132
RH
4 Written by Ian Lance Taylor <ian@cygnus.com>.
5
ec2655a6 6 This file is part of GAS, the GNU Assembler.
252b5132 7
ec2655a6
NC
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)
11 any later version.
252b5132 12
ec2655a6
NC
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.
252b5132 17
ec2655a6
NC
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
21 02110-1301, USA. */
252b5132
RH
22
23#include "as.h"
24#include "subsegs.h"
395e8345 25#include "struc-symbol.h"
252b5132
RH
26
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. */
30
31#include "elf/dwarf2.h"
32
33/* Try to optimize gcc 2.8 exception frame information.
34
35 Exception frame information is emitted for every function in the
6d2cf69f
RH
36 .eh_frame or .debug_frame sections. Simple information for a function
37 with no exceptions looks like this:
252b5132
RH
38
39__FRAME_BEGIN__:
40 .4byte .LLCIE1 / Length of Common Information Entry
41.LSCIE1:
6d2cf69f 42#if .eh_frame
252b5132 43 .4byte 0x0 / CIE Identifier Tag
6d2cf69f
RH
44#elif .debug_frame
45 .4byte 0xffffffff / CIE Identifier Tag
46#endif
252b5132
RH
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
57 .align 4
58.LECIE1:
59 .set .LLCIE1,.LECIE1-.LSCIE1 / CIE Length Symbol
60 .4byte .LLFDE1 / FDE Length
61.LSFDE1:
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
66 .4byte .LCFI0-.LFB1
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
72 .4byte .LCFI1-.LCFI0
73 .byte 0xd / DW_CFA_def_cfa_register
74 .byte 0x5 / ULEB128 0x5
75 .byte 0x4 / DW_CFA_advance_loc4
76 .4byte .LCFI2-.LCFI1
77 .byte 0x2e / DW_CFA_GNU_args_size
78 .byte 0x4 / ULEB128 0x4
79 .byte 0x4 / DW_CFA_advance_loc4
80 .4byte .LCFI3-.LCFI2
81 .byte 0x2e / DW_CFA_GNU_args_size
82 .byte 0x0 / ULEB128 0x0
83 .align 4
84.LEFDE1:
85 .set .LLFDE1,.LEFDE1-.LSFDE1 / FDE Length Symbol
86
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. */
92
67a659f6
RH
93struct cie_info
94{
95 unsigned code_alignment;
96 int z_augmentation;
97};
98
dd625418 99static int get_cie_info (struct cie_info *);
252b5132 100
67a659f6 101/* Extract information from the CIE. */
252b5132
RH
102
103static int
dd625418 104get_cie_info (struct cie_info *info)
252b5132 105{
252b5132
RH
106 fragS *f;
107 fixS *fix;
108 int offset;
6d2cf69f 109 char CIE_id;
252b5132
RH
110 char augmentation[10];
111 int iaug;
67a659f6 112 int code_alignment = 0;
6d2cf69f
RH
113
114 /* We should find the CIE at the start of the section. */
252b5132 115
252b5132 116 f = seg_info (now_seg)->frchainP->frch_root;
252b5132 117 fix = seg_info (now_seg)->frchainP->fix_root;
252b5132
RH
118
119 /* Look through the frags of the section to find the code alignment. */
120
6d2cf69f
RH
121 /* First make sure that the CIE Identifier Tag is 0/-1. */
122
123 if (strcmp (segment_name (now_seg), ".debug_frame") == 0)
124 CIE_id = (char)0xff;
125 else
126 CIE_id = 0;
252b5132
RH
127
128 offset = 4;
129 while (f != NULL && offset >= f->fr_fix)
130 {
131 offset -= f->fr_fix;
132 f = f->fr_next;
133 }
134 if (f == NULL
135 || f->fr_fix - offset < 4
6d2cf69f
RH
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)
67a659f6 140 return 0;
252b5132
RH
141
142 /* Next make sure the CIE version number is 1. */
143
144 offset += 4;
145 while (f != NULL && offset >= f->fr_fix)
146 {
147 offset -= f->fr_fix;
148 f = f->fr_next;
149 }
150 if (f == NULL
151 || f->fr_fix - offset < 1
152 || f->fr_literal[offset] != 1)
67a659f6 153 return 0;
252b5132
RH
154
155 /* Skip the augmentation (a null terminated string). */
156
157 iaug = 0;
158 ++offset;
159 while (1)
160 {
161 while (f != NULL && offset >= f->fr_fix)
162 {
163 offset -= f->fr_fix;
164 f = f->fr_next;
165 }
166 if (f == NULL)
67a659f6
RH
167 return 0;
168
252b5132
RH
169 while (offset < f->fr_fix && f->fr_literal[offset] != '\0')
170 {
171 if ((size_t) iaug < (sizeof augmentation) - 1)
172 {
173 augmentation[iaug] = f->fr_literal[offset];
174 ++iaug;
175 }
176 ++offset;
177 }
178 if (offset < f->fr_fix)
179 break;
180 }
181 ++offset;
182 while (f != NULL && offset >= f->fr_fix)
183 {
184 offset -= f->fr_fix;
185 f = f->fr_next;
186 }
187 if (f == NULL)
67a659f6 188 return 0;
252b5132
RH
189
190 augmentation[iaug] = '\0';
191 if (augmentation[0] == '\0')
192 {
193 /* No augmentation. */
194 }
195 else if (strcmp (augmentation, "eh") == 0)
196 {
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. */
199 while (fix != NULL
200 && (fix->fx_frag != f || fix->fx_where != offset))
201 fix = fix->fx_next;
202 if (fix == NULL)
203 offset += 4;
204 else
205 offset += fix->fx_size;
206 while (f != NULL && offset >= f->fr_fix)
207 {
208 offset -= f->fr_fix;
209 f = f->fr_next;
210 }
211 if (f == NULL)
67a659f6 212 return 0;
252b5132 213 }
67a659f6
RH
214 else if (augmentation[0] != 'z')
215 return 0;
252b5132
RH
216
217 /* We're now at the code alignment factor, which is a ULEB128. If
218 it isn't a single byte, forget it. */
219
220 code_alignment = f->fr_literal[offset] & 0xff;
67a659f6
RH
221 if ((code_alignment & 0x80) != 0)
222 code_alignment = 0;
252b5132 223
67a659f6
RH
224 info->code_alignment = code_alignment;
225 info->z_augmentation = (augmentation[0] == 'z');
226
227 return 1;
252b5132
RH
228}
229
230/* This function is called from emit_expr. It looks for cases which
231 we can optimize.
232
233 Rather than try to parse all this information as we read it, we
234 look for a single byte DW_CFA_advance_loc4 followed by a 4 byte
235 difference. We turn that into a rs_cfa_advance frag, and handle
236 those frags at the end of the assembly. If the gcc output changes
237 somewhat, this optimization may stop working.
238
239 This function returns non-zero if it handled the expression and
240 emit_expr should not do anything, or zero otherwise. It can also
241 change *EXP and *PNBYTES. */
242
243int
dd625418 244check_eh_frame (expressionS *exp, unsigned int *pnbytes)
252b5132 245{
6d2cf69f
RH
246 struct frame_data
247 {
67a659f6
RH
248 enum frame_state
249 {
250 state_idle,
251 state_saw_size,
252 state_saw_cie_offset,
253 state_saw_pc_begin,
254 state_seeing_aug_size,
255 state_skipping_aug,
256 state_wait_loc4,
257 state_saw_loc4,
258 state_error,
259 } state;
260
261 int cie_info_ok;
262 struct cie_info cie_info;
263
6d2cf69f
RH
264 symbolS *size_end_sym;
265 fragS *loc4_frag;
6d2cf69f 266 int loc4_fix;
67a659f6
RH
267
268 int aug_size;
269 int aug_shift;
6d2cf69f 270 };
43ad3147 271
6d2cf69f
RH
272 static struct frame_data eh_frame_data;
273 static struct frame_data debug_frame_data;
274 struct frame_data *d;
275
276 /* Don't optimize. */
277 if (flag_traditional_format)
278 return 0;
279
8c750480
NC
280#ifdef md_allow_eh_opt
281 if (! md_allow_eh_opt)
282 return 0;
283#endif
284
6d2cf69f
RH
285 /* Select the proper section data. */
286 if (strcmp (segment_name (now_seg), ".eh_frame") == 0)
287 d = &eh_frame_data;
288 else if (strcmp (segment_name (now_seg), ".debug_frame") == 0)
289 d = &debug_frame_data;
290 else
291 return 0;
292
67a659f6 293 if (d->state >= state_saw_size && S_IS_DEFINED (d->size_end_sym))
252b5132
RH
294 {
295 /* We have come to the end of the CIE or FDE. See below where
296 we set saw_size. We must check this first because we may now
297 be looking at the next size. */
67a659f6 298 d->state = state_idle;
252b5132
RH
299 }
300
67a659f6 301 switch (d->state)
252b5132 302 {
67a659f6
RH
303 case state_idle:
304 if (*pnbytes == 4)
252b5132 305 {
67a659f6
RH
306 /* This might be the size of the CIE or FDE. We want to know
307 the size so that we don't accidentally optimize across an FDE
308 boundary. We recognize the size in one of two forms: a
309 symbol which will later be defined as a difference, or a
310 subtraction of two symbols. Either way, we can tell when we
311 are at the end of the FDE because the symbol becomes defined
312 (in the case of a subtraction, the end symbol, from which the
313 start symbol is being subtracted). Other ways of describing
314 the size will not be optimized. */
315 if ((exp->X_op == O_symbol || exp->X_op == O_subtract)
316 && ! S_IS_DEFINED (exp->X_add_symbol))
317 {
318 d->state = state_saw_size;
319 d->size_end_sym = exp->X_add_symbol;
320 }
252b5132 321 }
67a659f6
RH
322 break;
323
324 case state_saw_size:
325 case state_saw_cie_offset:
326 /* Assume whatever form it appears in, it appears atomically. */
327 d->state += 1;
328 break;
329
330 case state_saw_pc_begin:
331 /* Decide whether we should see an augmentation. */
332 if (! d->cie_info_ok
333 && ! (d->cie_info_ok = get_cie_info (&d->cie_info)))
334 d->state = state_error;
335 else if (d->cie_info.z_augmentation)
252b5132 336 {
67a659f6
RH
337 d->state = state_seeing_aug_size;
338 d->aug_size = 0;
339 d->aug_shift = 0;
252b5132 340 }
67a659f6
RH
341 else
342 d->state = state_wait_loc4;
343 break;
344
345 case state_seeing_aug_size:
346 /* Bytes == -1 means this comes from an leb128 directive. */
347 if ((int)*pnbytes == -1 && exp->X_op == O_constant)
252b5132 348 {
67a659f6
RH
349 d->aug_size = exp->X_add_number;
350 d->state = state_skipping_aug;
252b5132 351 }
67a659f6 352 else if (*pnbytes == 1 && exp->X_op == O_constant)
252b5132 353 {
67a659f6
RH
354 unsigned char byte = exp->X_add_number;
355 d->aug_size |= (byte & 0x7f) << d->aug_shift;
356 d->aug_shift += 7;
357 if ((byte & 0x80) == 0)
358 d->state = state_skipping_aug;
252b5132 359 }
67a659f6
RH
360 else
361 d->state = state_error;
e5f08f7e
JJ
362 if (d->state == state_skipping_aug && d->aug_size == 0)
363 d->state = state_wait_loc4;
67a659f6
RH
364 break;
365
366 case state_skipping_aug:
367 if ((int)*pnbytes < 0)
368 d->state = state_error;
369 else
252b5132 370 {
411863a4 371 int left = (d->aug_size -= *pnbytes);
67a659f6
RH
372 if (left == 0)
373 d->state = state_wait_loc4;
374 else if (left < 0)
375 d->state = state_error;
252b5132 376 }
67a659f6 377 break;
252b5132 378
67a659f6
RH
379 case state_wait_loc4:
380 if (*pnbytes == 1
381 && exp->X_op == O_constant
382 && exp->X_add_number == DW_CFA_advance_loc4)
383 {
384 /* This might be a DW_CFA_advance_loc4. Record the frag and the
385 position within the frag, so that we can change it later. */
386 frag_grow (1);
387 d->state = state_saw_loc4;
388 d->loc4_frag = frag_now;
389 d->loc4_fix = frag_now_fix ();
390 }
391 break;
252b5132 392
67a659f6
RH
393 case state_saw_loc4:
394 d->state = state_wait_loc4;
395 if (*pnbytes != 4)
396 break;
397 if (exp->X_op == O_constant)
398 {
399 /* This is a case which we can optimize. The two symbols being
400 subtracted were in the same frag and the expression was
401 reduced to a constant. We can do the optimization entirely
402 in this function. */
395e8345 403 if (exp->X_add_number < 0x40)
67a659f6
RH
404 {
405 d->loc4_frag->fr_literal[d->loc4_fix]
395e8345 406 = DW_CFA_advance_loc | exp->X_add_number;
67a659f6
RH
407 /* No more bytes needed. */
408 return 1;
409 }
410 else if (exp->X_add_number < 0x100)
411 {
412 d->loc4_frag->fr_literal[d->loc4_fix] = DW_CFA_advance_loc1;
413 *pnbytes = 1;
414 }
415 else if (exp->X_add_number < 0x10000)
416 {
417 d->loc4_frag->fr_literal[d->loc4_fix] = DW_CFA_advance_loc2;
418 *pnbytes = 2;
419 }
420 }
395e8345 421 else if (exp->X_op == O_subtract && d->cie_info.code_alignment == 1)
67a659f6
RH
422 {
423 /* This is a case we can optimize. The expression was not
424 reduced, so we can not finish the optimization until the end
425 of the assembly. We set up a variant frag which we handle
426 later. */
395e8345 427 frag_var (rs_cfa, 4, 0, 1 << 3, make_expr_symbol (exp),
67a659f6
RH
428 d->loc4_fix, (char *) d->loc4_frag);
429 return 1;
430 }
395e8345
JJ
431 else if ((exp->X_op == O_divide
432 || exp->X_op == O_right_shift)
433 && d->cie_info.code_alignment > 1)
434 {
435 if (exp->X_add_symbol->bsym
436 && exp->X_op_symbol->bsym
437 && exp->X_add_symbol->sy_value.X_op == O_subtract
438 && exp->X_op_symbol->sy_value.X_op == O_constant
439 && ((exp->X_op == O_divide
440 ? exp->X_op_symbol->sy_value.X_add_number
441 : (offsetT) 1 << exp->X_op_symbol->sy_value.X_add_number)
442 == (offsetT) d->cie_info.code_alignment))
443 {
444 /* This is a case we can optimize as well. The expression was
445 not reduced, so we can not finish the optimization until the
446 end of the assembly. We set up a variant frag which we
447 handle later. */
448 frag_var (rs_cfa, 4, 0, d->cie_info.code_alignment << 3,
449 make_expr_symbol (&exp->X_add_symbol->sy_value),
450 d->loc4_fix, (char *) d->loc4_frag);
451 return 1;
452 }
453 }
67a659f6 454 break;
252b5132 455
67a659f6
RH
456 case state_error:
457 /* Just skipping everything. */
458 break;
252b5132 459 }
252b5132
RH
460
461 return 0;
462}
463
464/* The function estimates the size of a rs_cfa variant frag based on
465 the current values of the symbols. It is called before the
67a659f6 466 relaxation loop. We set fr_subtype{0:2} to the expected length. */
252b5132
RH
467
468int
dd625418 469eh_frame_estimate_size_before_relax (fragS *frag)
252b5132 470{
252b5132 471 offsetT diff;
67a659f6 472 int ca = frag->fr_subtype >> 3;
252b5132
RH
473 int ret;
474
6386f3a7 475 diff = resolve_symbol_value (frag->fr_symbol);
252b5132 476
395e8345
JJ
477 assert (ca > 0);
478 diff /= ca;
479 if (diff < 0x40)
252b5132
RH
480 ret = 0;
481 else if (diff < 0x100)
482 ret = 1;
483 else if (diff < 0x10000)
484 ret = 2;
485 else
486 ret = 4;
487
67a659f6 488 frag->fr_subtype = (frag->fr_subtype & ~7) | ret;
252b5132
RH
489
490 return ret;
491}
492
493/* This function relaxes a rs_cfa variant frag based on the current
67a659f6
RH
494 values of the symbols. fr_subtype{0:2} is the current length of
495 the frag. This returns the change in frag length. */
252b5132
RH
496
497int
dd625418 498eh_frame_relax_frag (fragS *frag)
252b5132
RH
499{
500 int oldsize, newsize;
501
67a659f6 502 oldsize = frag->fr_subtype & 7;
252b5132
RH
503 newsize = eh_frame_estimate_size_before_relax (frag);
504 return newsize - oldsize;
505}
506
507/* This function converts a rs_cfa variant frag into a normal fill
508 frag. This is called after all relaxation has been done.
67a659f6 509 fr_subtype{0:2} will be the desired length of the frag. */
252b5132
RH
510
511void
dd625418 512eh_frame_convert_frag (fragS *frag)
252b5132
RH
513{
514 offsetT diff;
515 fragS *loc4_frag;
395e8345 516 int loc4_fix, ca;
252b5132
RH
517
518 loc4_frag = (fragS *) frag->fr_opcode;
519 loc4_fix = (int) frag->fr_offset;
520
6386f3a7 521 diff = resolve_symbol_value (frag->fr_symbol);
252b5132 522
395e8345
JJ
523 ca = frag->fr_subtype >> 3;
524 assert (ca > 0);
525 diff /= ca;
67a659f6 526 switch (frag->fr_subtype & 7)
252b5132 527 {
67a659f6 528 case 0:
395e8345
JJ
529 assert (diff < 0x40);
530 loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc | diff;
67a659f6
RH
531 break;
532
533 case 1:
252b5132
RH
534 assert (diff < 0x100);
535 loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc1;
536 frag->fr_literal[frag->fr_fix] = diff;
67a659f6
RH
537 break;
538
539 case 2:
252b5132
RH
540 assert (diff < 0x10000);
541 loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc2;
542 md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 2);
67a659f6
RH
543 break;
544
545 default:
546 md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 4);
547 break;
252b5132 548 }
252b5132 549
de1cb007 550 frag->fr_fix += frag->fr_subtype & 7;
252b5132 551 frag->fr_type = rs_fill;
de1cb007 552 frag->fr_subtype = 0;
252b5132
RH
553 frag->fr_offset = 0;
554}
This page took 0.402081 seconds and 4 git commands to generate.