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