| 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 | |
| 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 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 | |
| 87 | static int eh_frame_code_alignment PARAMS ((void)); |
| 88 | |
| 89 | /* Get the code alignment factor from the CIE. */ |
| 90 | |
| 91 | static int |
| 92 | eh_frame_code_alignment () |
| 93 | { |
| 94 | static int code_alignment; |
| 95 | segT current_seg; |
| 96 | subsegT current_subseg; |
| 97 | fragS *f; |
| 98 | fixS *fix; |
| 99 | int offset; |
| 100 | char augmentation[10]; |
| 101 | int iaug; |
| 102 | |
| 103 | if (code_alignment != 0) |
| 104 | return code_alignment; |
| 105 | |
| 106 | /* We should find the CIE at the start of the .eh_frame section. */ |
| 107 | |
| 108 | current_seg = now_seg; |
| 109 | current_subseg = now_subseg; |
| 110 | subseg_new (".eh_frame", 0); |
| 111 | f = seg_info (now_seg)->frchainP->frch_root; |
| 112 | fix = seg_info (now_seg)->frchainP->fix_root; |
| 113 | subseg_set (current_seg, current_subseg); |
| 114 | |
| 115 | /* Look through the frags of the section to find the code alignment. */ |
| 116 | |
| 117 | /* First make sure that the CIE Identifier Tag is 0. */ |
| 118 | |
| 119 | offset = 4; |
| 120 | while (f != NULL && offset >= f->fr_fix) |
| 121 | { |
| 122 | offset -= f->fr_fix; |
| 123 | f = f->fr_next; |
| 124 | } |
| 125 | if (f == NULL |
| 126 | || f->fr_fix - offset < 4 |
| 127 | || f->fr_literal[offset] != 0 |
| 128 | || f->fr_literal[offset + 1] != 0 |
| 129 | || f->fr_literal[offset + 2] != 0 |
| 130 | || f->fr_literal[offset + 3] != 0) |
| 131 | { |
| 132 | code_alignment = -1; |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | /* Next make sure the CIE version number is 1. */ |
| 137 | |
| 138 | offset += 4; |
| 139 | while (f != NULL && offset >= f->fr_fix) |
| 140 | { |
| 141 | offset -= f->fr_fix; |
| 142 | f = f->fr_next; |
| 143 | } |
| 144 | if (f == NULL |
| 145 | || f->fr_fix - offset < 1 |
| 146 | || f->fr_literal[offset] != 1) |
| 147 | { |
| 148 | code_alignment = -1; |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | /* Skip the augmentation (a null terminated string). */ |
| 153 | |
| 154 | iaug = 0; |
| 155 | ++offset; |
| 156 | while (1) |
| 157 | { |
| 158 | while (f != NULL && offset >= f->fr_fix) |
| 159 | { |
| 160 | offset -= f->fr_fix; |
| 161 | f = f->fr_next; |
| 162 | } |
| 163 | if (f == NULL) |
| 164 | { |
| 165 | code_alignment = -1; |
| 166 | return -1; |
| 167 | } |
| 168 | while (offset < f->fr_fix && f->fr_literal[offset] != '\0') |
| 169 | { |
| 170 | if (iaug < (sizeof augmentation) - 1) |
| 171 | { |
| 172 | augmentation[iaug] = f->fr_literal[offset]; |
| 173 | ++iaug; |
| 174 | } |
| 175 | ++offset; |
| 176 | } |
| 177 | if (offset < f->fr_fix) |
| 178 | break; |
| 179 | } |
| 180 | ++offset; |
| 181 | while (f != NULL && offset >= f->fr_fix) |
| 182 | { |
| 183 | offset -= f->fr_fix; |
| 184 | f = f->fr_next; |
| 185 | } |
| 186 | if (f == NULL) |
| 187 | { |
| 188 | code_alignment = -1; |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | augmentation[iaug] = '\0'; |
| 193 | if (augmentation[0] == '\0') |
| 194 | { |
| 195 | /* No augmentation. */ |
| 196 | } |
| 197 | else if (strcmp (augmentation, "eh") == 0) |
| 198 | { |
| 199 | /* We have to skip a pointer. Unfortunately, we don't know how |
| 200 | large it is. We find out by looking for a matching fixup. */ |
| 201 | while (fix != NULL |
| 202 | && (fix->fx_frag != f || fix->fx_where != offset)) |
| 203 | fix = fix->fx_next; |
| 204 | if (fix == NULL) |
| 205 | offset += 4; |
| 206 | else |
| 207 | offset += fix->fx_size; |
| 208 | while (f != NULL && offset >= f->fr_fix) |
| 209 | { |
| 210 | offset -= f->fr_fix; |
| 211 | f = f->fr_next; |
| 212 | } |
| 213 | if (f == NULL) |
| 214 | { |
| 215 | code_alignment = -1; |
| 216 | return -1; |
| 217 | } |
| 218 | } |
| 219 | else |
| 220 | { |
| 221 | code_alignment = -1; |
| 222 | return -1; |
| 223 | } |
| 224 | |
| 225 | /* We're now at the code alignment factor, which is a ULEB128. If |
| 226 | it isn't a single byte, forget it. */ |
| 227 | |
| 228 | code_alignment = f->fr_literal[offset] & 0xff; |
| 229 | if ((code_alignment & 0x80) != 0 || code_alignment == 0) |
| 230 | { |
| 231 | code_alignment = -1; |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | return code_alignment; |
| 236 | } |
| 237 | |
| 238 | /* This function is called from emit_expr. It looks for cases which |
| 239 | we can optimize. |
| 240 | |
| 241 | Rather than try to parse all this information as we read it, we |
| 242 | look for a single byte DW_CFA_advance_loc4 followed by a 4 byte |
| 243 | difference. We turn that into a rs_cfa_advance frag, and handle |
| 244 | those frags at the end of the assembly. If the gcc output changes |
| 245 | somewhat, this optimization may stop working. |
| 246 | |
| 247 | This function returns non-zero if it handled the expression and |
| 248 | emit_expr should not do anything, or zero otherwise. It can also |
| 249 | change *EXP and *PNBYTES. */ |
| 250 | |
| 251 | int |
| 252 | check_eh_frame (exp, pnbytes) |
| 253 | expressionS *exp; |
| 254 | unsigned int *pnbytes; |
| 255 | { |
| 256 | static int saw_advance_loc4; |
| 257 | static fragS *loc4_frag; |
| 258 | static int loc4_fix; |
| 259 | |
| 260 | if (flag_traditional_format) |
| 261 | { |
| 262 | /* Don't optimize. */ |
| 263 | } |
| 264 | else if (strcmp (segment_name (now_seg), ".eh_frame") != 0) |
| 265 | saw_advance_loc4 = 0; |
| 266 | else if (*pnbytes == 1 |
| 267 | && exp->X_op == O_constant |
| 268 | && exp->X_add_number == DW_CFA_advance_loc4) |
| 269 | { |
| 270 | /* This might be a DW_CFA_advance_loc4. Record the frag and the |
| 271 | position within the frag, so that we can change it later. */ |
| 272 | saw_advance_loc4 = 1; |
| 273 | frag_grow (1); |
| 274 | loc4_frag = frag_now; |
| 275 | loc4_fix = frag_now_fix (); |
| 276 | } |
| 277 | else if (saw_advance_loc4 |
| 278 | && *pnbytes == 4 |
| 279 | && exp->X_op == O_constant) |
| 280 | { |
| 281 | int ca; |
| 282 | |
| 283 | /* This is a case which we can optimize. The two symbols being |
| 284 | subtracted were in the same frag and the expression was |
| 285 | reduced to a constant. We can do the optimization entirely |
| 286 | in this function. */ |
| 287 | |
| 288 | saw_advance_loc4 = 0; |
| 289 | |
| 290 | ca = eh_frame_code_alignment (); |
| 291 | if (ca < 0) |
| 292 | { |
| 293 | /* Don't optimize. */ |
| 294 | } |
| 295 | else if (exp->X_add_number % ca == 0 |
| 296 | && exp->X_add_number / ca < 0x40) |
| 297 | { |
| 298 | loc4_frag->fr_literal[loc4_fix] |
| 299 | = DW_CFA_advance_loc | (exp->X_add_number / ca); |
| 300 | /* No more bytes needed. */ |
| 301 | return 1; |
| 302 | } |
| 303 | else if (exp->X_add_number < 0x100) |
| 304 | { |
| 305 | loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc1; |
| 306 | *pnbytes = 1; |
| 307 | } |
| 308 | else if (exp->X_add_number < 0x10000) |
| 309 | { |
| 310 | loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc2; |
| 311 | *pnbytes = 2; |
| 312 | } |
| 313 | } |
| 314 | else if (saw_advance_loc4 |
| 315 | && *pnbytes == 4 |
| 316 | && exp->X_op == O_subtract) |
| 317 | { |
| 318 | |
| 319 | /* This is a case we can optimize. The expression was not |
| 320 | reduced, so we can not finish the optimization until the end |
| 321 | of the assembly. We set up a variant frag which we handle |
| 322 | later. */ |
| 323 | |
| 324 | saw_advance_loc4 = 0; |
| 325 | |
| 326 | frag_var (rs_cfa, 4, 0, 0, make_expr_symbol (exp), |
| 327 | loc4_fix, (char *) loc4_frag); |
| 328 | |
| 329 | return 1; |
| 330 | } |
| 331 | else |
| 332 | saw_advance_loc4 = 0; |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | /* The function estimates the size of a rs_cfa variant frag based on |
| 338 | the current values of the symbols. It is called before the |
| 339 | relaxation loop. We set fr_subtype to the expected length. */ |
| 340 | |
| 341 | int |
| 342 | eh_frame_estimate_size_before_relax (frag) |
| 343 | fragS *frag; |
| 344 | { |
| 345 | int ca; |
| 346 | offsetT diff; |
| 347 | int ret; |
| 348 | |
| 349 | ca = eh_frame_code_alignment (); |
| 350 | diff = resolve_symbol_value (frag->fr_symbol, 0); |
| 351 | |
| 352 | if (ca < 0) |
| 353 | ret = 4; |
| 354 | else if (diff % ca == 0 && diff / ca < 0x40) |
| 355 | ret = 0; |
| 356 | else if (diff < 0x100) |
| 357 | ret = 1; |
| 358 | else if (diff < 0x10000) |
| 359 | ret = 2; |
| 360 | else |
| 361 | ret = 4; |
| 362 | |
| 363 | frag->fr_subtype = ret; |
| 364 | |
| 365 | return ret; |
| 366 | } |
| 367 | |
| 368 | /* This function relaxes a rs_cfa variant frag based on the current |
| 369 | values of the symbols. fr_subtype is the current length of the |
| 370 | frag. This returns the change in frag length. */ |
| 371 | |
| 372 | int |
| 373 | eh_frame_relax_frag (frag) |
| 374 | fragS *frag; |
| 375 | { |
| 376 | int oldsize, newsize; |
| 377 | |
| 378 | oldsize = frag->fr_subtype; |
| 379 | newsize = eh_frame_estimate_size_before_relax (frag); |
| 380 | return newsize - oldsize; |
| 381 | } |
| 382 | |
| 383 | /* This function converts a rs_cfa variant frag into a normal fill |
| 384 | frag. This is called after all relaxation has been done. |
| 385 | fr_subtype will be the desired length of the frag. */ |
| 386 | |
| 387 | void |
| 388 | eh_frame_convert_frag (frag) |
| 389 | fragS *frag; |
| 390 | { |
| 391 | offsetT diff; |
| 392 | fragS *loc4_frag; |
| 393 | int loc4_fix; |
| 394 | |
| 395 | loc4_frag = (fragS *) frag->fr_opcode; |
| 396 | loc4_fix = (int) frag->fr_offset; |
| 397 | |
| 398 | diff = resolve_symbol_value (frag->fr_symbol, 1); |
| 399 | |
| 400 | if (frag->fr_subtype == 0) |
| 401 | { |
| 402 | int ca; |
| 403 | |
| 404 | ca = eh_frame_code_alignment (); |
| 405 | assert (ca > 0 && diff % ca == 0 && diff / ca < 0x40); |
| 406 | loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc | (diff / ca); |
| 407 | } |
| 408 | else if (frag->fr_subtype == 1) |
| 409 | { |
| 410 | assert (diff < 0x100); |
| 411 | loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc1; |
| 412 | frag->fr_literal[frag->fr_fix] = diff; |
| 413 | } |
| 414 | else if (frag->fr_subtype == 2) |
| 415 | { |
| 416 | assert (diff < 0x10000); |
| 417 | loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc2; |
| 418 | md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 2); |
| 419 | } |
| 420 | else |
| 421 | md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 4); |
| 422 | |
| 423 | frag->fr_fix += frag->fr_subtype; |
| 424 | frag->fr_type = rs_fill; |
| 425 | frag->fr_offset = 0; |
| 426 | } |