ehopt.c revision 89857
1151937Sjkim/* ehopt.c--optimize gcc exception frame information. 2151937Sjkim Copyright 1998, 2000, 2001 Free Software Foundation, Inc. 3151937Sjkim Written by Ian Lance Taylor <ian@cygnus.com>. 4151937Sjkim 5151937SjkimThis file is part of GAS, the GNU Assembler. 6151937Sjkim 7151937SjkimGAS is free software; you can redistribute it and/or modify 8151937Sjkimit under the terms of the GNU General Public License as published by 9151937Sjkimthe Free Software Foundation; either version 2, or (at your option) 10151937Sjkimany later version. 11151937Sjkim 12151937SjkimGAS is distributed in the hope that it will be useful, 13151937Sjkimbut WITHOUT ANY WARRANTY; without even the implied warranty of 14151937SjkimMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15151937SjkimGNU General Public License for more details. 16151937Sjkim 17151937SjkimYou should have received a copy of the GNU General Public License 18151937Sjkimalong with GAS; see the file COPYING. If not, write to the Free 19151937SjkimSoftware Foundation, 59 Temple Place - Suite 330, Boston, MA 20151937Sjkim02111-1307, USA. */ 21151937Sjkim 22151937Sjkim#include "as.h" 23151937Sjkim#include "subsegs.h" 24151937Sjkim 25151937Sjkim/* We include this ELF file, even though we may not be assembling for 26151937Sjkim ELF, since the exception frame information is always in a format 27151937Sjkim derived from DWARF. */ 28151937Sjkim 29151937Sjkim#include "elf/dwarf2.h" 30151937Sjkim 31151937Sjkim/* Try to optimize gcc 2.8 exception frame information. 32151937Sjkim 33151937Sjkim Exception frame information is emitted for every function in the 34151937Sjkim .eh_frame or .debug_frame sections. Simple information for a function 35151937Sjkim with no exceptions looks like this: 36151937Sjkim 37151937Sjkim__FRAME_BEGIN__: 38151937Sjkim .4byte .LLCIE1 / Length of Common Information Entry 39151937Sjkim.LSCIE1: 40151937Sjkim#if .eh_frame 41151937Sjkim .4byte 0x0 / CIE Identifier Tag 42151937Sjkim#elif .debug_frame 43151937Sjkim .4byte 0xffffffff / CIE Identifier Tag 44151937Sjkim#endif 45151937Sjkim .byte 0x1 / CIE Version 46151937Sjkim .byte 0x0 / CIE Augmentation (none) 47151937Sjkim .byte 0x1 / ULEB128 0x1 (CIE Code Alignment Factor) 48151937Sjkim .byte 0x7c / SLEB128 -4 (CIE Data Alignment Factor) 49151937Sjkim .byte 0x8 / CIE RA Column 50151937Sjkim .byte 0xc / DW_CFA_def_cfa 51151937Sjkim .byte 0x4 / ULEB128 0x4 52151937Sjkim .byte 0x4 / ULEB128 0x4 53151937Sjkim .byte 0x88 / DW_CFA_offset, column 0x8 54151937Sjkim .byte 0x1 / ULEB128 0x1 55151937Sjkim .align 4 56151937Sjkim.LECIE1: 57151937Sjkim .set .LLCIE1,.LECIE1-.LSCIE1 / CIE Length Symbol 58151937Sjkim .4byte .LLFDE1 / FDE Length 59151937Sjkim.LSFDE1: 60151937Sjkim .4byte .LSFDE1-__FRAME_BEGIN__ / FDE CIE offset 61151937Sjkim .4byte .LFB1 / FDE initial location 62151937Sjkim .4byte .LFE1-.LFB1 / FDE address range 63151937Sjkim .byte 0x4 / DW_CFA_advance_loc4 64151937Sjkim .4byte .LCFI0-.LFB1 65151937Sjkim .byte 0xe / DW_CFA_def_cfa_offset 66151937Sjkim .byte 0x8 / ULEB128 0x8 67151937Sjkim .byte 0x85 / DW_CFA_offset, column 0x5 68151937Sjkim .byte 0x2 / ULEB128 0x2 69151937Sjkim .byte 0x4 / DW_CFA_advance_loc4 70151937Sjkim .4byte .LCFI1-.LCFI0 71151937Sjkim .byte 0xd / DW_CFA_def_cfa_register 72151937Sjkim .byte 0x5 / ULEB128 0x5 73151937Sjkim .byte 0x4 / DW_CFA_advance_loc4 74151937Sjkim .4byte .LCFI2-.LCFI1 75151937Sjkim .byte 0x2e / DW_CFA_GNU_args_size 76151937Sjkim .byte 0x4 / ULEB128 0x4 77151937Sjkim .byte 0x4 / DW_CFA_advance_loc4 78151937Sjkim .4byte .LCFI3-.LCFI2 79151937Sjkim .byte 0x2e / DW_CFA_GNU_args_size 80151937Sjkim .byte 0x0 / ULEB128 0x0 81151937Sjkim .align 4 82151937Sjkim.LEFDE1: 83151937Sjkim .set .LLFDE1,.LEFDE1-.LSFDE1 / FDE Length Symbol 84151937Sjkim 85151937Sjkim The immediate issue we can address in the assembler is the 86151937Sjkim DW_CFA_advance_loc4 followed by a four byte value. The value is 87151937Sjkim the difference of two addresses in the function. Since gcc does 88151937Sjkim not know this value, it always uses four bytes. We will know the 89151937Sjkim value at the end of assembly, so we can do better. */ 90151937Sjkim 91151937Sjkimstruct cie_info 92151937Sjkim{ 93151937Sjkim unsigned code_alignment; 94151937Sjkim int z_augmentation; 95151937Sjkim}; 96151937Sjkim 97151937Sjkimstatic int get_cie_info PARAMS ((struct cie_info *)); 98151937Sjkim 99151937Sjkim/* Extract information from the CIE. */ 100151937Sjkim 101151937Sjkimstatic int 102151937Sjkimget_cie_info (info) 103151937Sjkim struct cie_info *info; 104151937Sjkim{ 105151937Sjkim fragS *f; 106151937Sjkim fixS *fix; 107151937Sjkim int offset; 108151937Sjkim char CIE_id; 109151937Sjkim char augmentation[10]; 110151937Sjkim int iaug; 111151937Sjkim int code_alignment = 0; 112151937Sjkim 113151937Sjkim /* We should find the CIE at the start of the section. */ 114151937Sjkim 115151937Sjkim#if defined (BFD_ASSEMBLER) || defined (MANY_SEGMENTS) 116151937Sjkim f = seg_info (now_seg)->frchainP->frch_root; 117151937Sjkim#else 118151937Sjkim f = frchain_now->frch_root; 119151937Sjkim#endif 120151937Sjkim#ifdef BFD_ASSEMBLER 121151937Sjkim fix = seg_info (now_seg)->frchainP->fix_root; 122151937Sjkim#else 123151937Sjkim fix = *seg_fix_rootP; 124151937Sjkim#endif 125151937Sjkim 126151937Sjkim /* Look through the frags of the section to find the code alignment. */ 127151937Sjkim 128151937Sjkim /* First make sure that the CIE Identifier Tag is 0/-1. */ 129151937Sjkim 130151937Sjkim if (strcmp (segment_name (now_seg), ".debug_frame") == 0) 131151937Sjkim CIE_id = (char)0xff; 132151937Sjkim else 133151937Sjkim CIE_id = 0; 134151937Sjkim 135151937Sjkim offset = 4; 136151937Sjkim while (f != NULL && offset >= f->fr_fix) 137151937Sjkim { 138151937Sjkim offset -= f->fr_fix; 139151937Sjkim f = f->fr_next; 140151937Sjkim } 141151937Sjkim if (f == NULL 142151937Sjkim || f->fr_fix - offset < 4 143151937Sjkim || f->fr_literal[offset] != CIE_id 144151937Sjkim || f->fr_literal[offset + 1] != CIE_id 145151937Sjkim || f->fr_literal[offset + 2] != CIE_id 146151937Sjkim || f->fr_literal[offset + 3] != CIE_id) 147151937Sjkim return 0; 148151937Sjkim 149151937Sjkim /* Next make sure the CIE version number is 1. */ 150151937Sjkim 151151937Sjkim offset += 4; 152151937Sjkim while (f != NULL && offset >= f->fr_fix) 153151937Sjkim { 154151937Sjkim offset -= f->fr_fix; 155151937Sjkim f = f->fr_next; 156151937Sjkim } 157151937Sjkim if (f == NULL 158151937Sjkim || f->fr_fix - offset < 1 159151937Sjkim || f->fr_literal[offset] != 1) 160151937Sjkim return 0; 161151937Sjkim 162151937Sjkim /* Skip the augmentation (a null terminated string). */ 163151937Sjkim 164151937Sjkim iaug = 0; 165151937Sjkim ++offset; 166151937Sjkim while (1) 167151937Sjkim { 168151937Sjkim while (f != NULL && offset >= f->fr_fix) 169151937Sjkim { 170151937Sjkim offset -= f->fr_fix; 171151937Sjkim f = f->fr_next; 172151937Sjkim } 173151937Sjkim if (f == NULL) 174151937Sjkim return 0; 175151937Sjkim 176151937Sjkim while (offset < f->fr_fix && f->fr_literal[offset] != '\0') 177151937Sjkim { 178151937Sjkim if ((size_t) iaug < (sizeof augmentation) - 1) 179151937Sjkim { 180151937Sjkim augmentation[iaug] = f->fr_literal[offset]; 181151937Sjkim ++iaug; 182151937Sjkim } 183151937Sjkim ++offset; 184151937Sjkim } 185151937Sjkim if (offset < f->fr_fix) 186151937Sjkim break; 187151937Sjkim } 188151937Sjkim ++offset; 189151937Sjkim while (f != NULL && offset >= f->fr_fix) 190151937Sjkim { 191151937Sjkim offset -= f->fr_fix; 192151937Sjkim f = f->fr_next; 193151937Sjkim } 194151937Sjkim if (f == NULL) 195151937Sjkim return 0; 196151937Sjkim 197151937Sjkim augmentation[iaug] = '\0'; 198151937Sjkim if (augmentation[0] == '\0') 199151937Sjkim { 200151937Sjkim /* No augmentation. */ 201151937Sjkim } 202151937Sjkim else if (strcmp (augmentation, "eh") == 0) 203151937Sjkim { 204151937Sjkim /* We have to skip a pointer. Unfortunately, we don't know how 205151937Sjkim large it is. We find out by looking for a matching fixup. */ 206151937Sjkim while (fix != NULL 207151937Sjkim && (fix->fx_frag != f || fix->fx_where != offset)) 208151937Sjkim fix = fix->fx_next; 209151937Sjkim if (fix == NULL) 210151937Sjkim offset += 4; 211151937Sjkim else 212151937Sjkim offset += fix->fx_size; 213151937Sjkim while (f != NULL && offset >= f->fr_fix) 214151937Sjkim { 215151937Sjkim offset -= f->fr_fix; 216151937Sjkim f = f->fr_next; 217151937Sjkim } 218151937Sjkim if (f == NULL) 219151937Sjkim return 0; 220151937Sjkim } 221151937Sjkim else if (augmentation[0] != 'z') 222151937Sjkim return 0; 223151937Sjkim 224151937Sjkim /* We're now at the code alignment factor, which is a ULEB128. If 225151937Sjkim it isn't a single byte, forget it. */ 226151937Sjkim 227151937Sjkim code_alignment = f->fr_literal[offset] & 0xff; 228151937Sjkim if ((code_alignment & 0x80) != 0) 229151937Sjkim code_alignment = 0; 230151937Sjkim 231151937Sjkim info->code_alignment = code_alignment; 232151937Sjkim info->z_augmentation = (augmentation[0] == 'z'); 233151937Sjkim 234151937Sjkim return 1; 235151937Sjkim} 236151937Sjkim 237151937Sjkim/* This function is called from emit_expr. It looks for cases which 238151937Sjkim we can optimize. 239151937Sjkim 240151937Sjkim Rather than try to parse all this information as we read it, we 241151937Sjkim look for a single byte DW_CFA_advance_loc4 followed by a 4 byte 242151937Sjkim difference. We turn that into a rs_cfa_advance frag, and handle 243151937Sjkim those frags at the end of the assembly. If the gcc output changes 244151937Sjkim somewhat, this optimization may stop working. 245151937Sjkim 246151937Sjkim This function returns non-zero if it handled the expression and 247151937Sjkim emit_expr should not do anything, or zero otherwise. It can also 248151937Sjkim change *EXP and *PNBYTES. */ 249151937Sjkim 250151937Sjkimint 251151937Sjkimcheck_eh_frame (exp, pnbytes) 252151937Sjkim expressionS *exp; 253151937Sjkim unsigned int *pnbytes; 254151937Sjkim{ 255151937Sjkim struct frame_data 256151937Sjkim { 257151937Sjkim enum frame_state 258151937Sjkim { 259151937Sjkim state_idle, 260151937Sjkim state_saw_size, 261151937Sjkim state_saw_cie_offset, 262151937Sjkim state_saw_pc_begin, 263151937Sjkim state_seeing_aug_size, 264151937Sjkim state_skipping_aug, 265151937Sjkim state_wait_loc4, 266151937Sjkim state_saw_loc4, 267151937Sjkim state_error, 268151937Sjkim } state; 269151937Sjkim 270151937Sjkim int cie_info_ok; 271151937Sjkim struct cie_info cie_info; 272151937Sjkim 273151937Sjkim symbolS *size_end_sym; 274151937Sjkim fragS *loc4_frag; 275151937Sjkim int loc4_fix; 276151937Sjkim 277151937Sjkim int aug_size; 278151937Sjkim int aug_shift; 279151937Sjkim }; 280151937Sjkim 281151937Sjkim static struct frame_data eh_frame_data; 282151937Sjkim static struct frame_data debug_frame_data; 283151937Sjkim struct frame_data *d; 284151937Sjkim 285151937Sjkim /* Don't optimize. */ 286151937Sjkim if (flag_traditional_format) 287151937Sjkim return 0; 288151937Sjkim 289151937Sjkim /* Select the proper section data. */ 290151937Sjkim if (strcmp (segment_name (now_seg), ".eh_frame") == 0) 291151937Sjkim d = &eh_frame_data; 292151937Sjkim else if (strcmp (segment_name (now_seg), ".debug_frame") == 0) 293151937Sjkim d = &debug_frame_data; 294151937Sjkim else 295151937Sjkim return 0; 296151937Sjkim 297151937Sjkim if (d->state >= state_saw_size && S_IS_DEFINED (d->size_end_sym)) 298151937Sjkim { 299151937Sjkim /* We have come to the end of the CIE or FDE. See below where 300151937Sjkim we set saw_size. We must check this first because we may now 301151937Sjkim be looking at the next size. */ 302151937Sjkim d->state = state_idle; 303151937Sjkim } 304151937Sjkim 305151937Sjkim switch (d->state) 306151937Sjkim { 307151937Sjkim case state_idle: 308151937Sjkim if (*pnbytes == 4) 309151937Sjkim { 310151937Sjkim /* This might be the size of the CIE or FDE. We want to know 311151937Sjkim the size so that we don't accidentally optimize across an FDE 312151937Sjkim boundary. We recognize the size in one of two forms: a 313151937Sjkim symbol which will later be defined as a difference, or a 314151937Sjkim subtraction of two symbols. Either way, we can tell when we 315151937Sjkim are at the end of the FDE because the symbol becomes defined 316151937Sjkim (in the case of a subtraction, the end symbol, from which the 317151937Sjkim start symbol is being subtracted). Other ways of describing 318151937Sjkim the size will not be optimized. */ 319151937Sjkim if ((exp->X_op == O_symbol || exp->X_op == O_subtract) 320151937Sjkim && ! S_IS_DEFINED (exp->X_add_symbol)) 321151937Sjkim { 322151937Sjkim d->state = state_saw_size; 323151937Sjkim d->size_end_sym = exp->X_add_symbol; 324151937Sjkim } 325151937Sjkim } 326151937Sjkim break; 327151937Sjkim 328151937Sjkim case state_saw_size: 329151937Sjkim case state_saw_cie_offset: 330151937Sjkim /* Assume whatever form it appears in, it appears atomically. */ 331151937Sjkim d->state += 1; 332151937Sjkim break; 333151937Sjkim 334151937Sjkim case state_saw_pc_begin: 335151937Sjkim /* Decide whether we should see an augmentation. */ 336151937Sjkim if (! d->cie_info_ok 337151937Sjkim && ! (d->cie_info_ok = get_cie_info (&d->cie_info))) 338151937Sjkim d->state = state_error; 339151937Sjkim else if (d->cie_info.z_augmentation) 340151937Sjkim { 341151937Sjkim d->state = state_seeing_aug_size; 342151937Sjkim d->aug_size = 0; 343151937Sjkim d->aug_shift = 0; 344151937Sjkim } 345151937Sjkim else 346151937Sjkim d->state = state_wait_loc4; 347151937Sjkim break; 348151937Sjkim 349151937Sjkim case state_seeing_aug_size: 350151937Sjkim /* Bytes == -1 means this comes from an leb128 directive. */ 351151937Sjkim if ((int)*pnbytes == -1 && exp->X_op == O_constant) 352151937Sjkim { 353151937Sjkim d->aug_size = exp->X_add_number; 354151937Sjkim d->state = state_skipping_aug; 355151937Sjkim } 356151937Sjkim else if (*pnbytes == 1 && exp->X_op == O_constant) 357151937Sjkim { 358151937Sjkim unsigned char byte = exp->X_add_number; 359151937Sjkim d->aug_size |= (byte & 0x7f) << d->aug_shift; 360151937Sjkim d->aug_shift += 7; 361151937Sjkim if ((byte & 0x80) == 0) 362151937Sjkim d->state = state_skipping_aug; 363151937Sjkim } 364151937Sjkim else 365151937Sjkim d->state = state_error; 366151937Sjkim break; 367151937Sjkim 368151937Sjkim case state_skipping_aug: 369151937Sjkim if ((int)*pnbytes < 0) 370151937Sjkim d->state = state_error; 371151937Sjkim else 372151937Sjkim { 373151937Sjkim int left = (d->aug_size -= *pnbytes); 374151937Sjkim if (left == 0) 375151937Sjkim d->state = state_wait_loc4; 376151937Sjkim else if (left < 0) 377151937Sjkim d->state = state_error; 378151937Sjkim } 379151937Sjkim break; 380151937Sjkim 381151937Sjkim case state_wait_loc4: 382151937Sjkim if (*pnbytes == 1 383151937Sjkim && exp->X_op == O_constant 384151937Sjkim && exp->X_add_number == DW_CFA_advance_loc4) 385151937Sjkim { 386151937Sjkim /* This might be a DW_CFA_advance_loc4. Record the frag and the 387151937Sjkim position within the frag, so that we can change it later. */ 388151937Sjkim frag_grow (1); 389151937Sjkim d->state = state_saw_loc4; 390151937Sjkim d->loc4_frag = frag_now; 391151937Sjkim d->loc4_fix = frag_now_fix (); 392151937Sjkim } 393151937Sjkim break; 394151937Sjkim 395151937Sjkim case state_saw_loc4: 396151937Sjkim d->state = state_wait_loc4; 397151937Sjkim if (*pnbytes != 4) 398151937Sjkim break; 399 if (exp->X_op == O_constant) 400 { 401 /* This is a case which we can optimize. The two symbols being 402 subtracted were in the same frag and the expression was 403 reduced to a constant. We can do the optimization entirely 404 in this function. */ 405 if (d->cie_info.code_alignment > 0 406 && exp->X_add_number % d->cie_info.code_alignment == 0 407 && exp->X_add_number / d->cie_info.code_alignment < 0x40) 408 { 409 d->loc4_frag->fr_literal[d->loc4_fix] 410 = DW_CFA_advance_loc 411 | (exp->X_add_number / d->cie_info.code_alignment); 412 /* No more bytes needed. */ 413 return 1; 414 } 415 else if (exp->X_add_number < 0x100) 416 { 417 d->loc4_frag->fr_literal[d->loc4_fix] = DW_CFA_advance_loc1; 418 *pnbytes = 1; 419 } 420 else if (exp->X_add_number < 0x10000) 421 { 422 d->loc4_frag->fr_literal[d->loc4_fix] = DW_CFA_advance_loc2; 423 *pnbytes = 2; 424 } 425 } 426 else if (exp->X_op == O_subtract) 427 { 428 /* This is a case we can optimize. The expression was not 429 reduced, so we can not finish the optimization until the end 430 of the assembly. We set up a variant frag which we handle 431 later. */ 432 int fr_subtype; 433 434 if (d->cie_info.code_alignment > 0) 435 fr_subtype = d->cie_info.code_alignment << 3; 436 else 437 fr_subtype = 0; 438 439 frag_var (rs_cfa, 4, 0, fr_subtype, make_expr_symbol (exp), 440 d->loc4_fix, (char *) d->loc4_frag); 441 return 1; 442 } 443 break; 444 445 case state_error: 446 /* Just skipping everything. */ 447 break; 448 } 449 450 return 0; 451} 452 453/* The function estimates the size of a rs_cfa variant frag based on 454 the current values of the symbols. It is called before the 455 relaxation loop. We set fr_subtype{0:2} to the expected length. */ 456 457int 458eh_frame_estimate_size_before_relax (frag) 459 fragS *frag; 460{ 461 offsetT diff; 462 int ca = frag->fr_subtype >> 3; 463 int ret; 464 465 diff = resolve_symbol_value (frag->fr_symbol); 466 467 if (ca > 0 && diff % ca == 0 && diff / ca < 0x40) 468 ret = 0; 469 else if (diff < 0x100) 470 ret = 1; 471 else if (diff < 0x10000) 472 ret = 2; 473 else 474 ret = 4; 475 476 frag->fr_subtype = (frag->fr_subtype & ~7) | ret; 477 478 return ret; 479} 480 481/* This function relaxes a rs_cfa variant frag based on the current 482 values of the symbols. fr_subtype{0:2} is the current length of 483 the frag. This returns the change in frag length. */ 484 485int 486eh_frame_relax_frag (frag) 487 fragS *frag; 488{ 489 int oldsize, newsize; 490 491 oldsize = frag->fr_subtype & 7; 492 newsize = eh_frame_estimate_size_before_relax (frag); 493 return newsize - oldsize; 494} 495 496/* This function converts a rs_cfa variant frag into a normal fill 497 frag. This is called after all relaxation has been done. 498 fr_subtype{0:2} will be the desired length of the frag. */ 499 500void 501eh_frame_convert_frag (frag) 502 fragS *frag; 503{ 504 offsetT diff; 505 fragS *loc4_frag; 506 int loc4_fix; 507 508 loc4_frag = (fragS *) frag->fr_opcode; 509 loc4_fix = (int) frag->fr_offset; 510 511 diff = resolve_symbol_value (frag->fr_symbol); 512 513 switch (frag->fr_subtype & 7) 514 { 515 case 0: 516 { 517 int ca = frag->fr_subtype >> 3; 518 assert (ca > 0 && diff % ca == 0 && diff / ca < 0x40); 519 loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc | (diff / ca); 520 } 521 break; 522 523 case 1: 524 assert (diff < 0x100); 525 loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc1; 526 frag->fr_literal[frag->fr_fix] = diff; 527 break; 528 529 case 2: 530 assert (diff < 0x10000); 531 loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc2; 532 md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 2); 533 break; 534 535 default: 536 md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 4); 537 break; 538 } 539 540 frag->fr_fix += frag->fr_subtype & 7; 541 frag->fr_type = rs_fill; 542 frag->fr_subtype = 0; 543 frag->fr_offset = 0; 544} 545