diff options
author | Federico G. Schwindt <fgsch@cvs.openbsd.org> | 2002-05-13 13:39:43 +0000 |
---|---|---|
committer | Federico G. Schwindt <fgsch@cvs.openbsd.org> | 2002-05-13 13:39:43 +0000 |
commit | f411c5546fc13b1e37e1ed1faa2d7c48d50e9b84 (patch) | |
tree | 9ec7ef5c8ce926b036fda2c83b51a3f6b6d2539c /gnu/usr.bin/binutils/gas/ehopt.c | |
parent | 56b2bd657b0d4248b516800fef776d9e2c772d7d (diff) |
Import binutils-2.11.2
- only the binutils package (no gdb here)
- don't import libiberty and texinfo, they are elsewhere
- remove all .info* generated files
Diffstat (limited to 'gnu/usr.bin/binutils/gas/ehopt.c')
-rw-r--r-- | gnu/usr.bin/binutils/gas/ehopt.c | 417 |
1 files changed, 246 insertions, 171 deletions
diff --git a/gnu/usr.bin/binutils/gas/ehopt.c b/gnu/usr.bin/binutils/gas/ehopt.c index a0a5f62f4d7..64ea7e0b247 100644 --- a/gnu/usr.bin/binutils/gas/ehopt.c +++ b/gnu/usr.bin/binutils/gas/ehopt.c @@ -1,5 +1,5 @@ /* ehopt.c--optimize gcc exception frame information. - Copyright (C) 1998 Free Software Foundation, Inc. + Copyright 1998, 2000, 2001 Free Software Foundation, Inc. Written by Ian Lance Taylor <ian@cygnus.com>. This file is part of GAS, the GNU Assembler. @@ -17,7 +17,7 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GAS; see the file COPYING. If not, write to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA -02111-1307, USA. */ +02111-1307, USA. */ #include "as.h" #include "subsegs.h" @@ -31,13 +31,17 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA /* Try to optimize gcc 2.8 exception frame information. Exception frame information is emitted for every function in the - .eh_frame section. Simple information for a function with no - exceptions looks like this: + .eh_frame or .debug_frame sections. Simple information for a function + with no exceptions looks like this: __FRAME_BEGIN__: .4byte .LLCIE1 / Length of Common Information Entry .LSCIE1: +#if .eh_frame .4byte 0x0 / CIE Identifier Tag +#elif .debug_frame + .4byte 0xffffffff / CIE Identifier Tag +#endif .byte 0x1 / CIE Version .byte 0x0 / CIE Augmentation (none) .byte 0x1 / ULEB128 0x1 (CIE Code Alignment Factor) @@ -84,30 +88,30 @@ __FRAME_BEGIN__: not know this value, it always uses four bytes. We will know the value at the end of assembly, so we can do better. */ -static int eh_frame_code_alignment PARAMS ((void)); +struct cie_info +{ + unsigned code_alignment; + int z_augmentation; +}; + +static int get_cie_info PARAMS ((struct cie_info *)); -/* Get the code alignment factor from the CIE. */ +/* Extract information from the CIE. */ static int -eh_frame_code_alignment () +get_cie_info (info) + struct cie_info *info; { - static int code_alignment; - segT current_seg; - subsegT current_subseg; fragS *f; fixS *fix; int offset; + char CIE_id; char augmentation[10]; int iaug; + int code_alignment = 0; - if (code_alignment != 0) - return code_alignment; + /* We should find the CIE at the start of the section. */ - /* We should find the CIE at the start of the .eh_frame section. */ - - current_seg = now_seg; - current_subseg = now_subseg; - subseg_new (".eh_frame", 0); #if defined (BFD_ASSEMBLER) || defined (MANY_SEGMENTS) f = seg_info (now_seg)->frchainP->frch_root; #else @@ -118,11 +122,15 @@ eh_frame_code_alignment () #else fix = *seg_fix_rootP; #endif - subseg_set (current_seg, current_subseg); /* Look through the frags of the section to find the code alignment. */ - /* First make sure that the CIE Identifier Tag is 0. */ + /* First make sure that the CIE Identifier Tag is 0/-1. */ + + if (strcmp (segment_name (now_seg), ".debug_frame") == 0) + CIE_id = (char)0xff; + else + CIE_id = 0; offset = 4; while (f != NULL && offset >= f->fr_fix) @@ -132,14 +140,11 @@ eh_frame_code_alignment () } if (f == NULL || f->fr_fix - offset < 4 - || f->fr_literal[offset] != 0 - || f->fr_literal[offset + 1] != 0 - || f->fr_literal[offset + 2] != 0 - || f->fr_literal[offset + 3] != 0) - { - code_alignment = -1; - return -1; - } + || f->fr_literal[offset] != CIE_id + || f->fr_literal[offset + 1] != CIE_id + || f->fr_literal[offset + 2] != CIE_id + || f->fr_literal[offset + 3] != CIE_id) + return 0; /* Next make sure the CIE version number is 1. */ @@ -152,10 +157,7 @@ eh_frame_code_alignment () if (f == NULL || f->fr_fix - offset < 1 || f->fr_literal[offset] != 1) - { - code_alignment = -1; - return -1; - } + return 0; /* Skip the augmentation (a null terminated string). */ @@ -169,10 +171,8 @@ eh_frame_code_alignment () f = f->fr_next; } if (f == NULL) - { - code_alignment = -1; - return -1; - } + return 0; + while (offset < f->fr_fix && f->fr_literal[offset] != '\0') { if ((size_t) iaug < (sizeof augmentation) - 1) @@ -192,10 +192,7 @@ eh_frame_code_alignment () f = f->fr_next; } if (f == NULL) - { - code_alignment = -1; - return -1; - } + return 0; augmentation[iaug] = '\0'; if (augmentation[0] == '\0') @@ -219,28 +216,22 @@ eh_frame_code_alignment () f = f->fr_next; } if (f == NULL) - { - code_alignment = -1; - return -1; - } - } - else - { - code_alignment = -1; - return -1; + return 0; } + else if (augmentation[0] != 'z') + return 0; /* We're now at the code alignment factor, which is a ULEB128. If it isn't a single byte, forget it. */ code_alignment = f->fr_literal[offset] & 0xff; - if ((code_alignment & 0x80) != 0 || code_alignment == 0) - { - code_alignment = -1; - return -1; - } + if ((code_alignment & 0x80) != 0) + code_alignment = 0; - return code_alignment; + info->code_alignment = code_alignment; + info->z_augmentation = (augmentation[0] == 'z'); + + return 1; } /* This function is called from emit_expr. It looks for cases which @@ -261,140 +252,219 @@ check_eh_frame (exp, pnbytes) expressionS *exp; unsigned int *pnbytes; { - static int saw_size; - static symbolS *size_end_sym; - static int saw_advance_loc4; - static fragS *loc4_frag; - static int loc4_fix; - - if (saw_size - && S_IS_DEFINED (size_end_sym)) + struct frame_data + { + enum frame_state + { + state_idle, + state_saw_size, + state_saw_cie_offset, + state_saw_pc_begin, + state_seeing_aug_size, + state_skipping_aug, + state_wait_loc4, + state_saw_loc4, + state_error, + } state; + + int cie_info_ok; + struct cie_info cie_info; + + symbolS *size_end_sym; + fragS *loc4_frag; + int loc4_fix; + + int aug_size; + int aug_shift; + }; + + static struct frame_data eh_frame_data; + static struct frame_data debug_frame_data; + struct frame_data *d; + + /* Don't optimize. */ + if (flag_traditional_format) + return 0; + + /* Select the proper section data. */ + if (strcmp (segment_name (now_seg), ".eh_frame") == 0) + d = &eh_frame_data; + else if (strcmp (segment_name (now_seg), ".debug_frame") == 0) + d = &debug_frame_data; + else + return 0; + + if (d->state >= state_saw_size && S_IS_DEFINED (d->size_end_sym)) { /* We have come to the end of the CIE or FDE. See below where we set saw_size. We must check this first because we may now be looking at the next size. */ - saw_size = 0; - saw_advance_loc4 = 0; + d->state = state_idle; } - if (flag_traditional_format) + switch (d->state) { - /* Don't optimize. */ - } - else if (strcmp (segment_name (now_seg), ".eh_frame") != 0) - { - saw_size = 0; - saw_advance_loc4 = 0; - } - else if (! saw_size - && *pnbytes == 4) - { - /* This might be the size of the CIE or FDE. We want to know - the size so that we don't accidentally optimize across an FDE - boundary. We recognize the size in one of two forms: a - symbol which will later be defined as a difference, or a - subtraction of two symbols. Either way, we can tell when we - are at the end of the FDE because the symbol becomes defined - (in the case of a subtraction, the end symbol, from which the - start symbol is being subtracted). Other ways of describing - the size will not be optimized. */ - if ((exp->X_op == O_symbol || exp->X_op == O_subtract) - && ! S_IS_DEFINED (exp->X_add_symbol)) + case state_idle: + if (*pnbytes == 4) { - saw_size = 1; - size_end_sym = exp->X_add_symbol; + /* This might be the size of the CIE or FDE. We want to know + the size so that we don't accidentally optimize across an FDE + boundary. We recognize the size in one of two forms: a + symbol which will later be defined as a difference, or a + subtraction of two symbols. Either way, we can tell when we + are at the end of the FDE because the symbol becomes defined + (in the case of a subtraction, the end symbol, from which the + start symbol is being subtracted). Other ways of describing + the size will not be optimized. */ + if ((exp->X_op == O_symbol || exp->X_op == O_subtract) + && ! S_IS_DEFINED (exp->X_add_symbol)) + { + d->state = state_saw_size; + d->size_end_sym = exp->X_add_symbol; + } } - } - else if (saw_size - && *pnbytes == 1 - && exp->X_op == O_constant - && exp->X_add_number == DW_CFA_advance_loc4) - { - /* This might be a DW_CFA_advance_loc4. Record the frag and the - position within the frag, so that we can change it later. */ - saw_advance_loc4 = 1; - frag_grow (1); - loc4_frag = frag_now; - loc4_fix = frag_now_fix (); - } - else if (saw_advance_loc4 - && *pnbytes == 4 - && exp->X_op == O_constant) - { - int ca; - - /* This is a case which we can optimize. The two symbols being - subtracted were in the same frag and the expression was - reduced to a constant. We can do the optimization entirely - in this function. */ - - saw_advance_loc4 = 0; - - ca = eh_frame_code_alignment (); - if (ca < 0) + break; + + case state_saw_size: + case state_saw_cie_offset: + /* Assume whatever form it appears in, it appears atomically. */ + d->state += 1; + break; + + case state_saw_pc_begin: + /* Decide whether we should see an augmentation. */ + if (! d->cie_info_ok + && ! (d->cie_info_ok = get_cie_info (&d->cie_info))) + d->state = state_error; + else if (d->cie_info.z_augmentation) { - /* Don't optimize. */ + d->state = state_seeing_aug_size; + d->aug_size = 0; + d->aug_shift = 0; } - else if (exp->X_add_number % ca == 0 - && exp->X_add_number / ca < 0x40) + else + d->state = state_wait_loc4; + break; + + case state_seeing_aug_size: + /* Bytes == -1 means this comes from an leb128 directive. */ + if ((int)*pnbytes == -1 && exp->X_op == O_constant) { - loc4_frag->fr_literal[loc4_fix] - = DW_CFA_advance_loc | (exp->X_add_number / ca); - /* No more bytes needed. */ - return 1; + d->aug_size = exp->X_add_number; + d->state = state_skipping_aug; } - else if (exp->X_add_number < 0x100) + else if (*pnbytes == 1 && exp->X_op == O_constant) { - loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc1; - *pnbytes = 1; + unsigned char byte = exp->X_add_number; + d->aug_size |= (byte & 0x7f) << d->aug_shift; + d->aug_shift += 7; + if ((byte & 0x80) == 0) + d->state = state_skipping_aug; } - else if (exp->X_add_number < 0x10000) + else + d->state = state_error; + break; + + case state_skipping_aug: + if ((int)*pnbytes < 0) + d->state = state_error; + else { - loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc2; - *pnbytes = 2; + int left = (d->aug_size -= *pnbytes); + if (left == 0) + d->state = state_wait_loc4; + else if (left < 0) + d->state = state_error; } - } - else if (saw_advance_loc4 - && *pnbytes == 4 - && exp->X_op == O_subtract) - { - - /* This is a case we can optimize. The expression was not - reduced, so we can not finish the optimization until the end - of the assembly. We set up a variant frag which we handle - later. */ + break; - saw_advance_loc4 = 0; + case state_wait_loc4: + if (*pnbytes == 1 + && exp->X_op == O_constant + && exp->X_add_number == DW_CFA_advance_loc4) + { + /* This might be a DW_CFA_advance_loc4. Record the frag and the + position within the frag, so that we can change it later. */ + frag_grow (1); + d->state = state_saw_loc4; + d->loc4_frag = frag_now; + d->loc4_fix = frag_now_fix (); + } + break; - frag_var (rs_cfa, 4, 0, 0, make_expr_symbol (exp), - loc4_fix, (char *) loc4_frag); + case state_saw_loc4: + d->state = state_wait_loc4; + if (*pnbytes != 4) + break; + if (exp->X_op == O_constant) + { + /* This is a case which we can optimize. The two symbols being + subtracted were in the same frag and the expression was + reduced to a constant. We can do the optimization entirely + in this function. */ + if (d->cie_info.code_alignment > 0 + && exp->X_add_number % d->cie_info.code_alignment == 0 + && exp->X_add_number / d->cie_info.code_alignment < 0x40) + { + d->loc4_frag->fr_literal[d->loc4_fix] + = DW_CFA_advance_loc + | (exp->X_add_number / d->cie_info.code_alignment); + /* No more bytes needed. */ + return 1; + } + else if (exp->X_add_number < 0x100) + { + d->loc4_frag->fr_literal[d->loc4_fix] = DW_CFA_advance_loc1; + *pnbytes = 1; + } + else if (exp->X_add_number < 0x10000) + { + d->loc4_frag->fr_literal[d->loc4_fix] = DW_CFA_advance_loc2; + *pnbytes = 2; + } + } + else if (exp->X_op == O_subtract) + { + /* This is a case we can optimize. The expression was not + reduced, so we can not finish the optimization until the end + of the assembly. We set up a variant frag which we handle + later. */ + int fr_subtype; + + if (d->cie_info.code_alignment > 0) + fr_subtype = d->cie_info.code_alignment << 3; + else + fr_subtype = 0; + + frag_var (rs_cfa, 4, 0, fr_subtype, make_expr_symbol (exp), + d->loc4_fix, (char *) d->loc4_frag); + return 1; + } + break; - return 1; + case state_error: + /* Just skipping everything. */ + break; } - else - saw_advance_loc4 = 0; return 0; } /* The function estimates the size of a rs_cfa variant frag based on the current values of the symbols. It is called before the - relaxation loop. We set fr_subtype to the expected length. */ + relaxation loop. We set fr_subtype{0:2} to the expected length. */ int eh_frame_estimate_size_before_relax (frag) fragS *frag; { - int ca; offsetT diff; + int ca = frag->fr_subtype >> 3; int ret; - ca = eh_frame_code_alignment (); diff = resolve_symbol_value (frag->fr_symbol, 0); - if (ca < 0) - ret = 4; - else if (diff % ca == 0 && diff / ca < 0x40) + if (ca > 0 && diff % ca == 0 && diff / ca < 0x40) ret = 0; else if (diff < 0x100) ret = 1; @@ -403,14 +473,14 @@ eh_frame_estimate_size_before_relax (frag) else ret = 4; - frag->fr_subtype = ret; + frag->fr_subtype = (frag->fr_subtype & ~7) | ret; return ret; } /* This function relaxes a rs_cfa variant frag based on the current - values of the symbols. fr_subtype is the current length of the - frag. This returns the change in frag length. */ + values of the symbols. fr_subtype{0:2} is the current length of + the frag. This returns the change in frag length. */ int eh_frame_relax_frag (frag) @@ -418,14 +488,14 @@ eh_frame_relax_frag (frag) { int oldsize, newsize; - oldsize = frag->fr_subtype; + oldsize = frag->fr_subtype & 7; newsize = eh_frame_estimate_size_before_relax (frag); return newsize - oldsize; } /* This function converts a rs_cfa variant frag into a normal fill frag. This is called after all relaxation has been done. - fr_subtype will be the desired length of the frag. */ + fr_subtype{0:2} will be the desired length of the frag. */ void eh_frame_convert_frag (frag) @@ -440,30 +510,35 @@ eh_frame_convert_frag (frag) diff = resolve_symbol_value (frag->fr_symbol, 1); - if (frag->fr_subtype == 0) - { - int ca; - - ca = eh_frame_code_alignment (); - assert (ca > 0 && diff % ca == 0 && diff / ca < 0x40); - loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc | (diff / ca); - } - else if (frag->fr_subtype == 1) + switch (frag->fr_subtype & 7) { + case 0: + { + int ca = frag->fr_subtype >> 3; + assert (ca > 0 && diff % ca == 0 && diff / ca < 0x40); + loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc | (diff / ca); + } + break; + + case 1: assert (diff < 0x100); loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc1; frag->fr_literal[frag->fr_fix] = diff; - } - else if (frag->fr_subtype == 2) - { + break; + + case 2: assert (diff < 0x10000); loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc2; md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 2); + break; + + default: + md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 4); + break; } - else - md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 4); - frag->fr_fix += frag->fr_subtype; + frag->fr_fix += frag->fr_subtype & 7; frag->fr_type = rs_fill; + frag->fr_subtype = 0; frag->fr_offset = 0; } |