| 1 | /* dw2gencfi.c - Support for generating Dwarf2 CFI information. |
| 2 | Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
| 3 | Free Software Foundation, Inc. |
| 4 | Contributed by Michal Ludvig <mludvig@suse.cz> |
| 5 | |
| 6 | This file is part of GAS, the GNU Assembler. |
| 7 | |
| 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. |
| 12 | |
| 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. |
| 17 | |
| 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. */ |
| 22 | |
| 23 | #include "as.h" |
| 24 | #include "dw2gencfi.h" |
| 25 | #include "subsegs.h" |
| 26 | |
| 27 | #ifdef TARGET_USE_CFIPOP |
| 28 | |
| 29 | /* By default, use difference expressions if DIFF_EXPR_OK is defined. */ |
| 30 | #ifndef CFI_DIFF_EXPR_OK |
| 31 | # ifdef DIFF_EXPR_OK |
| 32 | # define CFI_DIFF_EXPR_OK 1 |
| 33 | # else |
| 34 | # define CFI_DIFF_EXPR_OK 0 |
| 35 | # endif |
| 36 | #endif |
| 37 | |
| 38 | /* We re-use DWARF2_LINE_MIN_INSN_LENGTH for the code alignment field |
| 39 | of the CIE. Default to 1 if not otherwise specified. */ |
| 40 | #ifndef DWARF2_LINE_MIN_INSN_LENGTH |
| 41 | # define DWARF2_LINE_MIN_INSN_LENGTH 1 |
| 42 | #endif |
| 43 | |
| 44 | /* By default, use 32-bit relocations from .eh_frame into .text. */ |
| 45 | #ifndef DWARF2_FDE_RELOC_SIZE |
| 46 | # define DWARF2_FDE_RELOC_SIZE 4 |
| 47 | #endif |
| 48 | |
| 49 | /* By default, use a read-only .eh_frame section. */ |
| 50 | #ifndef DWARF2_EH_FRAME_READ_ONLY |
| 51 | # define DWARF2_EH_FRAME_READ_ONLY SEC_READONLY |
| 52 | #endif |
| 53 | |
| 54 | #ifndef EH_FRAME_ALIGNMENT |
| 55 | # define EH_FRAME_ALIGNMENT (bfd_get_arch_size (stdoutput) == 64 ? 3 : 2) |
| 56 | #endif |
| 57 | |
| 58 | #ifndef tc_cfi_frame_initial_instructions |
| 59 | # define tc_cfi_frame_initial_instructions() ((void)0) |
| 60 | #endif |
| 61 | |
| 62 | #ifndef DWARF2_ADDR_SIZE |
| 63 | # define DWARF2_ADDR_SIZE(bfd) (bfd_arch_bits_per_address (bfd) / 8) |
| 64 | #endif |
| 65 | |
| 66 | |
| 67 | struct cfi_insn_data |
| 68 | { |
| 69 | struct cfi_insn_data *next; |
| 70 | int insn; |
| 71 | union { |
| 72 | struct { |
| 73 | unsigned reg; |
| 74 | offsetT offset; |
| 75 | } ri; |
| 76 | |
| 77 | struct { |
| 78 | unsigned reg1; |
| 79 | unsigned reg2; |
| 80 | } rr; |
| 81 | |
| 82 | unsigned r; |
| 83 | offsetT i; |
| 84 | |
| 85 | struct { |
| 86 | symbolS *lab1; |
| 87 | symbolS *lab2; |
| 88 | } ll; |
| 89 | |
| 90 | struct cfi_escape_data { |
| 91 | struct cfi_escape_data *next; |
| 92 | expressionS exp; |
| 93 | } *esc; |
| 94 | |
| 95 | struct { |
| 96 | unsigned reg, encoding; |
| 97 | expressionS exp; |
| 98 | } ea; |
| 99 | } u; |
| 100 | }; |
| 101 | |
| 102 | struct fde_entry |
| 103 | { |
| 104 | struct fde_entry *next; |
| 105 | symbolS *start_address; |
| 106 | symbolS *end_address; |
| 107 | struct cfi_insn_data *data; |
| 108 | struct cfi_insn_data **last; |
| 109 | unsigned char per_encoding; |
| 110 | unsigned char lsda_encoding; |
| 111 | expressionS personality; |
| 112 | expressionS lsda; |
| 113 | unsigned int return_column; |
| 114 | unsigned int signal_frame; |
| 115 | }; |
| 116 | |
| 117 | struct cie_entry |
| 118 | { |
| 119 | struct cie_entry *next; |
| 120 | symbolS *start_address; |
| 121 | unsigned int return_column; |
| 122 | unsigned int signal_frame; |
| 123 | unsigned char per_encoding; |
| 124 | unsigned char lsda_encoding; |
| 125 | expressionS personality; |
| 126 | struct cfi_insn_data *first, *last; |
| 127 | }; |
| 128 | |
| 129 | |
| 130 | /* List of FDE entries. */ |
| 131 | static struct fde_entry *all_fde_data; |
| 132 | static struct fde_entry **last_fde_data = &all_fde_data; |
| 133 | |
| 134 | /* List of CIEs so that they could be reused. */ |
| 135 | static struct cie_entry *cie_root; |
| 136 | |
| 137 | /* Stack of old CFI data, for save/restore. */ |
| 138 | struct cfa_save_data |
| 139 | { |
| 140 | struct cfa_save_data *next; |
| 141 | offsetT cfa_offset; |
| 142 | }; |
| 143 | |
| 144 | /* Current open FDE entry. */ |
| 145 | struct frch_cfi_data |
| 146 | { |
| 147 | struct fde_entry *cur_fde_data; |
| 148 | symbolS *last_address; |
| 149 | offsetT cur_cfa_offset; |
| 150 | struct cfa_save_data *cfa_save_stack; |
| 151 | }; |
| 152 | \f |
| 153 | /* Construct a new FDE structure and add it to the end of the fde list. */ |
| 154 | |
| 155 | static struct fde_entry * |
| 156 | alloc_fde_entry (void) |
| 157 | { |
| 158 | struct fde_entry *fde = xcalloc (1, sizeof (struct fde_entry)); |
| 159 | |
| 160 | frchain_now->frch_cfi_data = xcalloc (1, sizeof (struct frch_cfi_data)); |
| 161 | frchain_now->frch_cfi_data->cur_fde_data = fde; |
| 162 | *last_fde_data = fde; |
| 163 | last_fde_data = &fde->next; |
| 164 | |
| 165 | fde->last = &fde->data; |
| 166 | fde->return_column = DWARF2_DEFAULT_RETURN_COLUMN; |
| 167 | fde->per_encoding = DW_EH_PE_omit; |
| 168 | fde->lsda_encoding = DW_EH_PE_omit; |
| 169 | |
| 170 | return fde; |
| 171 | } |
| 172 | |
| 173 | /* The following functions are available for a backend to construct its |
| 174 | own unwind information, usually from legacy unwind directives. */ |
| 175 | |
| 176 | /* Construct a new INSN structure and add it to the end of the insn list |
| 177 | for the currently active FDE. */ |
| 178 | |
| 179 | static struct cfi_insn_data * |
| 180 | alloc_cfi_insn_data (void) |
| 181 | { |
| 182 | struct cfi_insn_data *insn = xcalloc (1, sizeof (struct cfi_insn_data)); |
| 183 | struct fde_entry *cur_fde_data = frchain_now->frch_cfi_data->cur_fde_data; |
| 184 | |
| 185 | *cur_fde_data->last = insn; |
| 186 | cur_fde_data->last = &insn->next; |
| 187 | |
| 188 | return insn; |
| 189 | } |
| 190 | |
| 191 | /* Construct a new FDE structure that begins at LABEL. */ |
| 192 | |
| 193 | void |
| 194 | cfi_new_fde (symbolS *label) |
| 195 | { |
| 196 | struct fde_entry *fde = alloc_fde_entry (); |
| 197 | fde->start_address = label; |
| 198 | frchain_now->frch_cfi_data->last_address = label; |
| 199 | } |
| 200 | |
| 201 | /* End the currently open FDE. */ |
| 202 | |
| 203 | void |
| 204 | cfi_end_fde (symbolS *label) |
| 205 | { |
| 206 | frchain_now->frch_cfi_data->cur_fde_data->end_address = label; |
| 207 | free (frchain_now->frch_cfi_data); |
| 208 | frchain_now->frch_cfi_data = NULL; |
| 209 | } |
| 210 | |
| 211 | /* Set the return column for the current FDE. */ |
| 212 | |
| 213 | void |
| 214 | cfi_set_return_column (unsigned regno) |
| 215 | { |
| 216 | frchain_now->frch_cfi_data->cur_fde_data->return_column = regno; |
| 217 | } |
| 218 | |
| 219 | /* Universal functions to store new instructions. */ |
| 220 | |
| 221 | static void |
| 222 | cfi_add_CFA_insn(int insn) |
| 223 | { |
| 224 | struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data (); |
| 225 | |
| 226 | insn_ptr->insn = insn; |
| 227 | } |
| 228 | |
| 229 | static void |
| 230 | cfi_add_CFA_insn_reg (int insn, unsigned regno) |
| 231 | { |
| 232 | struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data (); |
| 233 | |
| 234 | insn_ptr->insn = insn; |
| 235 | insn_ptr->u.r = regno; |
| 236 | } |
| 237 | |
| 238 | static void |
| 239 | cfi_add_CFA_insn_offset (int insn, offsetT offset) |
| 240 | { |
| 241 | struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data (); |
| 242 | |
| 243 | insn_ptr->insn = insn; |
| 244 | insn_ptr->u.i = offset; |
| 245 | } |
| 246 | |
| 247 | static void |
| 248 | cfi_add_CFA_insn_reg_reg (int insn, unsigned reg1, unsigned reg2) |
| 249 | { |
| 250 | struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data (); |
| 251 | |
| 252 | insn_ptr->insn = insn; |
| 253 | insn_ptr->u.rr.reg1 = reg1; |
| 254 | insn_ptr->u.rr.reg2 = reg2; |
| 255 | } |
| 256 | |
| 257 | static void |
| 258 | cfi_add_CFA_insn_reg_offset (int insn, unsigned regno, offsetT offset) |
| 259 | { |
| 260 | struct cfi_insn_data *insn_ptr = alloc_cfi_insn_data (); |
| 261 | |
| 262 | insn_ptr->insn = insn; |
| 263 | insn_ptr->u.ri.reg = regno; |
| 264 | insn_ptr->u.ri.offset = offset; |
| 265 | } |
| 266 | |
| 267 | /* Add a CFI insn to advance the PC from the last address to LABEL. */ |
| 268 | |
| 269 | void |
| 270 | cfi_add_advance_loc (symbolS *label) |
| 271 | { |
| 272 | struct cfi_insn_data *insn = alloc_cfi_insn_data (); |
| 273 | |
| 274 | insn->insn = DW_CFA_advance_loc; |
| 275 | insn->u.ll.lab1 = frchain_now->frch_cfi_data->last_address; |
| 276 | insn->u.ll.lab2 = label; |
| 277 | |
| 278 | frchain_now->frch_cfi_data->last_address = label; |
| 279 | } |
| 280 | |
| 281 | /* Add a DW_CFA_offset record to the CFI data. */ |
| 282 | |
| 283 | void |
| 284 | cfi_add_CFA_offset (unsigned regno, offsetT offset) |
| 285 | { |
| 286 | unsigned int abs_data_align; |
| 287 | |
| 288 | assert (DWARF2_CIE_DATA_ALIGNMENT != 0); |
| 289 | cfi_add_CFA_insn_reg_offset (DW_CFA_offset, regno, offset); |
| 290 | |
| 291 | abs_data_align = (DWARF2_CIE_DATA_ALIGNMENT < 0 |
| 292 | ? -DWARF2_CIE_DATA_ALIGNMENT : DWARF2_CIE_DATA_ALIGNMENT); |
| 293 | if (offset % abs_data_align) |
| 294 | as_bad (_("register save offset not a multiple of %u"), abs_data_align); |
| 295 | } |
| 296 | |
| 297 | /* Add a DW_CFA_def_cfa record to the CFI data. */ |
| 298 | |
| 299 | void |
| 300 | cfi_add_CFA_def_cfa (unsigned regno, offsetT offset) |
| 301 | { |
| 302 | cfi_add_CFA_insn_reg_offset (DW_CFA_def_cfa, regno, offset); |
| 303 | frchain_now->frch_cfi_data->cur_cfa_offset = offset; |
| 304 | } |
| 305 | |
| 306 | /* Add a DW_CFA_register record to the CFI data. */ |
| 307 | |
| 308 | void |
| 309 | cfi_add_CFA_register (unsigned reg1, unsigned reg2) |
| 310 | { |
| 311 | cfi_add_CFA_insn_reg_reg (DW_CFA_register, reg1, reg2); |
| 312 | } |
| 313 | |
| 314 | /* Add a DW_CFA_def_cfa_register record to the CFI data. */ |
| 315 | |
| 316 | void |
| 317 | cfi_add_CFA_def_cfa_register (unsigned regno) |
| 318 | { |
| 319 | cfi_add_CFA_insn_reg (DW_CFA_def_cfa_register, regno); |
| 320 | } |
| 321 | |
| 322 | /* Add a DW_CFA_def_cfa_offset record to the CFI data. */ |
| 323 | |
| 324 | void |
| 325 | cfi_add_CFA_def_cfa_offset (offsetT offset) |
| 326 | { |
| 327 | cfi_add_CFA_insn_offset (DW_CFA_def_cfa_offset, offset); |
| 328 | frchain_now->frch_cfi_data->cur_cfa_offset = offset; |
| 329 | } |
| 330 | |
| 331 | void |
| 332 | cfi_add_CFA_restore (unsigned regno) |
| 333 | { |
| 334 | cfi_add_CFA_insn_reg (DW_CFA_restore, regno); |
| 335 | } |
| 336 | |
| 337 | void |
| 338 | cfi_add_CFA_undefined (unsigned regno) |
| 339 | { |
| 340 | cfi_add_CFA_insn_reg (DW_CFA_undefined, regno); |
| 341 | } |
| 342 | |
| 343 | void |
| 344 | cfi_add_CFA_same_value (unsigned regno) |
| 345 | { |
| 346 | cfi_add_CFA_insn_reg (DW_CFA_same_value, regno); |
| 347 | } |
| 348 | |
| 349 | void |
| 350 | cfi_add_CFA_remember_state (void) |
| 351 | { |
| 352 | struct cfa_save_data *p; |
| 353 | |
| 354 | cfi_add_CFA_insn (DW_CFA_remember_state); |
| 355 | |
| 356 | p = xmalloc (sizeof (*p)); |
| 357 | p->cfa_offset = frchain_now->frch_cfi_data->cur_cfa_offset; |
| 358 | p->next = frchain_now->frch_cfi_data->cfa_save_stack; |
| 359 | frchain_now->frch_cfi_data->cfa_save_stack = p; |
| 360 | } |
| 361 | |
| 362 | void |
| 363 | cfi_add_CFA_restore_state (void) |
| 364 | { |
| 365 | struct cfa_save_data *p; |
| 366 | |
| 367 | cfi_add_CFA_insn (DW_CFA_restore_state); |
| 368 | |
| 369 | p = frchain_now->frch_cfi_data->cfa_save_stack; |
| 370 | if (p) |
| 371 | { |
| 372 | frchain_now->frch_cfi_data->cur_cfa_offset = p->cfa_offset; |
| 373 | frchain_now->frch_cfi_data->cfa_save_stack = p->next; |
| 374 | free (p); |
| 375 | } |
| 376 | else |
| 377 | as_bad (_("CFI state restore without previous remember")); |
| 378 | } |
| 379 | |
| 380 | \f |
| 381 | /* Parse CFI assembler directives. */ |
| 382 | |
| 383 | static void dot_cfi (int); |
| 384 | static void dot_cfi_escape (int); |
| 385 | static void dot_cfi_startproc (int); |
| 386 | static void dot_cfi_endproc (int); |
| 387 | static void dot_cfi_personality (int); |
| 388 | static void dot_cfi_lsda (int); |
| 389 | static void dot_cfi_val_encoded_addr (int); |
| 390 | |
| 391 | /* Fake CFI type; outside the byte range of any real CFI insn. */ |
| 392 | #define CFI_adjust_cfa_offset 0x100 |
| 393 | #define CFI_return_column 0x101 |
| 394 | #define CFI_rel_offset 0x102 |
| 395 | #define CFI_escape 0x103 |
| 396 | #define CFI_signal_frame 0x104 |
| 397 | #define CFI_val_encoded_addr 0x105 |
| 398 | |
| 399 | const pseudo_typeS cfi_pseudo_table[] = |
| 400 | { |
| 401 | { "cfi_startproc", dot_cfi_startproc, 0 }, |
| 402 | { "cfi_endproc", dot_cfi_endproc, 0 }, |
| 403 | { "cfi_def_cfa", dot_cfi, DW_CFA_def_cfa }, |
| 404 | { "cfi_def_cfa_register", dot_cfi, DW_CFA_def_cfa_register }, |
| 405 | { "cfi_def_cfa_offset", dot_cfi, DW_CFA_def_cfa_offset }, |
| 406 | { "cfi_adjust_cfa_offset", dot_cfi, CFI_adjust_cfa_offset }, |
| 407 | { "cfi_offset", dot_cfi, DW_CFA_offset }, |
| 408 | { "cfi_rel_offset", dot_cfi, CFI_rel_offset }, |
| 409 | { "cfi_register", dot_cfi, DW_CFA_register }, |
| 410 | { "cfi_return_column", dot_cfi, CFI_return_column }, |
| 411 | { "cfi_restore", dot_cfi, DW_CFA_restore }, |
| 412 | { "cfi_undefined", dot_cfi, DW_CFA_undefined }, |
| 413 | { "cfi_same_value", dot_cfi, DW_CFA_same_value }, |
| 414 | { "cfi_remember_state", dot_cfi, DW_CFA_remember_state }, |
| 415 | { "cfi_restore_state", dot_cfi, DW_CFA_restore_state }, |
| 416 | { "cfi_window_save", dot_cfi, DW_CFA_GNU_window_save }, |
| 417 | { "cfi_escape", dot_cfi_escape, 0 }, |
| 418 | { "cfi_signal_frame", dot_cfi, CFI_signal_frame }, |
| 419 | { "cfi_personality", dot_cfi_personality, 0 }, |
| 420 | { "cfi_lsda", dot_cfi_lsda, 0 }, |
| 421 | { "cfi_val_encoded_addr", dot_cfi_val_encoded_addr, 0 }, |
| 422 | { NULL, NULL, 0 } |
| 423 | }; |
| 424 | |
| 425 | static void |
| 426 | cfi_parse_separator (void) |
| 427 | { |
| 428 | SKIP_WHITESPACE (); |
| 429 | if (*input_line_pointer == ',') |
| 430 | input_line_pointer++; |
| 431 | else |
| 432 | as_bad (_("missing separator")); |
| 433 | } |
| 434 | |
| 435 | #ifndef tc_parse_to_dw2regnum |
| 436 | static void |
| 437 | tc_parse_to_dw2regnum(expressionS *exp) |
| 438 | { |
| 439 | # ifdef tc_regname_to_dw2regnum |
| 440 | SKIP_WHITESPACE (); |
| 441 | if (is_name_beginner (*input_line_pointer) |
| 442 | || (*input_line_pointer == '%' |
| 443 | && is_name_beginner (*++input_line_pointer))) |
| 444 | { |
| 445 | char *name, c; |
| 446 | |
| 447 | name = input_line_pointer; |
| 448 | c = get_symbol_end (); |
| 449 | |
| 450 | exp->X_op = O_constant; |
| 451 | exp->X_add_number = tc_regname_to_dw2regnum (name); |
| 452 | |
| 453 | *input_line_pointer = c; |
| 454 | } |
| 455 | else |
| 456 | # endif |
| 457 | expression_and_evaluate (exp); |
| 458 | } |
| 459 | #endif |
| 460 | |
| 461 | static unsigned |
| 462 | cfi_parse_reg (void) |
| 463 | { |
| 464 | int regno; |
| 465 | expressionS exp; |
| 466 | |
| 467 | tc_parse_to_dw2regnum (&exp); |
| 468 | switch (exp.X_op) |
| 469 | { |
| 470 | case O_register: |
| 471 | case O_constant: |
| 472 | regno = exp.X_add_number; |
| 473 | break; |
| 474 | |
| 475 | default: |
| 476 | regno = -1; |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | if (regno < 0) |
| 481 | { |
| 482 | as_bad (_("bad register expression")); |
| 483 | regno = 0; |
| 484 | } |
| 485 | |
| 486 | return regno; |
| 487 | } |
| 488 | |
| 489 | static offsetT |
| 490 | cfi_parse_const (void) |
| 491 | { |
| 492 | return get_absolute_expression (); |
| 493 | } |
| 494 | |
| 495 | static void |
| 496 | dot_cfi (int arg) |
| 497 | { |
| 498 | offsetT offset; |
| 499 | unsigned reg1, reg2; |
| 500 | |
| 501 | if (frchain_now->frch_cfi_data == NULL) |
| 502 | { |
| 503 | as_bad (_("CFI instruction used without previous .cfi_startproc")); |
| 504 | ignore_rest_of_line (); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | /* If the last address was not at the current PC, advance to current. */ |
| 509 | if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now |
| 510 | || S_GET_VALUE (frchain_now->frch_cfi_data->last_address) |
| 511 | != frag_now_fix ()) |
| 512 | cfi_add_advance_loc (symbol_temp_new_now ()); |
| 513 | |
| 514 | switch (arg) |
| 515 | { |
| 516 | case DW_CFA_offset: |
| 517 | reg1 = cfi_parse_reg (); |
| 518 | cfi_parse_separator (); |
| 519 | offset = cfi_parse_const (); |
| 520 | cfi_add_CFA_offset (reg1, offset); |
| 521 | break; |
| 522 | |
| 523 | case CFI_rel_offset: |
| 524 | reg1 = cfi_parse_reg (); |
| 525 | cfi_parse_separator (); |
| 526 | offset = cfi_parse_const (); |
| 527 | cfi_add_CFA_offset (reg1, |
| 528 | offset - frchain_now->frch_cfi_data->cur_cfa_offset); |
| 529 | break; |
| 530 | |
| 531 | case DW_CFA_def_cfa: |
| 532 | reg1 = cfi_parse_reg (); |
| 533 | cfi_parse_separator (); |
| 534 | offset = cfi_parse_const (); |
| 535 | cfi_add_CFA_def_cfa (reg1, offset); |
| 536 | break; |
| 537 | |
| 538 | case DW_CFA_register: |
| 539 | reg1 = cfi_parse_reg (); |
| 540 | cfi_parse_separator (); |
| 541 | reg2 = cfi_parse_reg (); |
| 542 | cfi_add_CFA_register (reg1, reg2); |
| 543 | break; |
| 544 | |
| 545 | case DW_CFA_def_cfa_register: |
| 546 | reg1 = cfi_parse_reg (); |
| 547 | cfi_add_CFA_def_cfa_register (reg1); |
| 548 | break; |
| 549 | |
| 550 | case DW_CFA_def_cfa_offset: |
| 551 | offset = cfi_parse_const (); |
| 552 | cfi_add_CFA_def_cfa_offset (offset); |
| 553 | break; |
| 554 | |
| 555 | case CFI_adjust_cfa_offset: |
| 556 | offset = cfi_parse_const (); |
| 557 | cfi_add_CFA_def_cfa_offset (frchain_now->frch_cfi_data->cur_cfa_offset |
| 558 | + offset); |
| 559 | break; |
| 560 | |
| 561 | case DW_CFA_restore: |
| 562 | for (;;) |
| 563 | { |
| 564 | reg1 = cfi_parse_reg (); |
| 565 | cfi_add_CFA_restore (reg1); |
| 566 | SKIP_WHITESPACE (); |
| 567 | if (*input_line_pointer != ',') |
| 568 | break; |
| 569 | ++input_line_pointer; |
| 570 | } |
| 571 | break; |
| 572 | |
| 573 | case DW_CFA_undefined: |
| 574 | for (;;) |
| 575 | { |
| 576 | reg1 = cfi_parse_reg (); |
| 577 | cfi_add_CFA_undefined (reg1); |
| 578 | SKIP_WHITESPACE (); |
| 579 | if (*input_line_pointer != ',') |
| 580 | break; |
| 581 | ++input_line_pointer; |
| 582 | } |
| 583 | break; |
| 584 | |
| 585 | case DW_CFA_same_value: |
| 586 | reg1 = cfi_parse_reg (); |
| 587 | cfi_add_CFA_same_value (reg1); |
| 588 | break; |
| 589 | |
| 590 | case CFI_return_column: |
| 591 | reg1 = cfi_parse_reg (); |
| 592 | cfi_set_return_column (reg1); |
| 593 | break; |
| 594 | |
| 595 | case DW_CFA_remember_state: |
| 596 | cfi_add_CFA_remember_state (); |
| 597 | break; |
| 598 | |
| 599 | case DW_CFA_restore_state: |
| 600 | cfi_add_CFA_restore_state (); |
| 601 | break; |
| 602 | |
| 603 | case DW_CFA_GNU_window_save: |
| 604 | cfi_add_CFA_insn (DW_CFA_GNU_window_save); |
| 605 | break; |
| 606 | |
| 607 | case CFI_signal_frame: |
| 608 | frchain_now->frch_cfi_data->cur_fde_data->signal_frame = 1; |
| 609 | break; |
| 610 | |
| 611 | default: |
| 612 | abort (); |
| 613 | } |
| 614 | |
| 615 | demand_empty_rest_of_line (); |
| 616 | } |
| 617 | |
| 618 | static void |
| 619 | dot_cfi_escape (int ignored ATTRIBUTE_UNUSED) |
| 620 | { |
| 621 | struct cfi_escape_data *head, **tail, *e; |
| 622 | struct cfi_insn_data *insn; |
| 623 | |
| 624 | if (frchain_now->frch_cfi_data == NULL) |
| 625 | { |
| 626 | as_bad (_("CFI instruction used without previous .cfi_startproc")); |
| 627 | ignore_rest_of_line (); |
| 628 | return; |
| 629 | } |
| 630 | |
| 631 | /* If the last address was not at the current PC, advance to current. */ |
| 632 | if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now |
| 633 | || S_GET_VALUE (frchain_now->frch_cfi_data->last_address) |
| 634 | != frag_now_fix ()) |
| 635 | cfi_add_advance_loc (symbol_temp_new_now ()); |
| 636 | |
| 637 | tail = &head; |
| 638 | do |
| 639 | { |
| 640 | e = xmalloc (sizeof (*e)); |
| 641 | do_parse_cons_expression (&e->exp, 1); |
| 642 | *tail = e; |
| 643 | tail = &e->next; |
| 644 | } |
| 645 | while (*input_line_pointer++ == ','); |
| 646 | *tail = NULL; |
| 647 | |
| 648 | insn = alloc_cfi_insn_data (); |
| 649 | insn->insn = CFI_escape; |
| 650 | insn->u.esc = head; |
| 651 | |
| 652 | --input_line_pointer; |
| 653 | demand_empty_rest_of_line (); |
| 654 | } |
| 655 | |
| 656 | static void |
| 657 | dot_cfi_personality (int ignored ATTRIBUTE_UNUSED) |
| 658 | { |
| 659 | struct fde_entry *fde; |
| 660 | offsetT encoding; |
| 661 | |
| 662 | if (frchain_now->frch_cfi_data == NULL) |
| 663 | { |
| 664 | as_bad (_("CFI instruction used without previous .cfi_startproc")); |
| 665 | ignore_rest_of_line (); |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | fde = frchain_now->frch_cfi_data->cur_fde_data; |
| 670 | encoding = cfi_parse_const (); |
| 671 | if (encoding == DW_EH_PE_omit) |
| 672 | { |
| 673 | demand_empty_rest_of_line (); |
| 674 | fde->per_encoding = encoding; |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | if ((encoding & 0xff) != encoding |
| 679 | || ((encoding & 0x70) != 0 |
| 680 | #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr |
| 681 | && (encoding & 0x70) != DW_EH_PE_pcrel |
| 682 | #endif |
| 683 | ) |
| 684 | /* leb128 can be handled, but does something actually need it? */ |
| 685 | || (encoding & 7) == DW_EH_PE_uleb128 |
| 686 | || (encoding & 7) > DW_EH_PE_udata8) |
| 687 | { |
| 688 | as_bad (_("invalid or unsupported encoding in .cfi_personality")); |
| 689 | ignore_rest_of_line (); |
| 690 | return; |
| 691 | } |
| 692 | |
| 693 | if (*input_line_pointer++ != ',') |
| 694 | { |
| 695 | as_bad (_(".cfi_personality requires encoding and symbol arguments")); |
| 696 | ignore_rest_of_line (); |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | expression_and_evaluate (&fde->personality); |
| 701 | switch (fde->personality.X_op) |
| 702 | { |
| 703 | case O_symbol: |
| 704 | break; |
| 705 | case O_constant: |
| 706 | if ((encoding & 0x70) == DW_EH_PE_pcrel) |
| 707 | encoding = DW_EH_PE_omit; |
| 708 | break; |
| 709 | default: |
| 710 | encoding = DW_EH_PE_omit; |
| 711 | break; |
| 712 | } |
| 713 | |
| 714 | fde->per_encoding = encoding; |
| 715 | |
| 716 | if (encoding == DW_EH_PE_omit) |
| 717 | { |
| 718 | as_bad (_("wrong second argument to .cfi_personality")); |
| 719 | ignore_rest_of_line (); |
| 720 | return; |
| 721 | } |
| 722 | |
| 723 | demand_empty_rest_of_line (); |
| 724 | } |
| 725 | |
| 726 | static void |
| 727 | dot_cfi_lsda (int ignored ATTRIBUTE_UNUSED) |
| 728 | { |
| 729 | struct fde_entry *fde; |
| 730 | offsetT encoding; |
| 731 | |
| 732 | if (frchain_now->frch_cfi_data == NULL) |
| 733 | { |
| 734 | as_bad (_("CFI instruction used without previous .cfi_startproc")); |
| 735 | ignore_rest_of_line (); |
| 736 | return; |
| 737 | } |
| 738 | |
| 739 | fde = frchain_now->frch_cfi_data->cur_fde_data; |
| 740 | encoding = cfi_parse_const (); |
| 741 | if (encoding == DW_EH_PE_omit) |
| 742 | { |
| 743 | demand_empty_rest_of_line (); |
| 744 | fde->lsda_encoding = encoding; |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | if ((encoding & 0xff) != encoding |
| 749 | || ((encoding & 0x70) != 0 |
| 750 | #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr |
| 751 | && (encoding & 0x70) != DW_EH_PE_pcrel |
| 752 | #endif |
| 753 | ) |
| 754 | /* leb128 can be handled, but does something actually need it? */ |
| 755 | || (encoding & 7) == DW_EH_PE_uleb128 |
| 756 | || (encoding & 7) > DW_EH_PE_udata8) |
| 757 | { |
| 758 | as_bad (_("invalid or unsupported encoding in .cfi_lsda")); |
| 759 | ignore_rest_of_line (); |
| 760 | return; |
| 761 | } |
| 762 | |
| 763 | if (*input_line_pointer++ != ',') |
| 764 | { |
| 765 | as_bad (_(".cfi_lsda requires encoding and symbol arguments")); |
| 766 | ignore_rest_of_line (); |
| 767 | return; |
| 768 | } |
| 769 | |
| 770 | fde->lsda_encoding = encoding; |
| 771 | |
| 772 | expression_and_evaluate (&fde->lsda); |
| 773 | switch (fde->lsda.X_op) |
| 774 | { |
| 775 | case O_symbol: |
| 776 | break; |
| 777 | case O_constant: |
| 778 | if ((encoding & 0x70) == DW_EH_PE_pcrel) |
| 779 | encoding = DW_EH_PE_omit; |
| 780 | break; |
| 781 | default: |
| 782 | encoding = DW_EH_PE_omit; |
| 783 | break; |
| 784 | } |
| 785 | |
| 786 | fde->lsda_encoding = encoding; |
| 787 | |
| 788 | if (encoding == DW_EH_PE_omit) |
| 789 | { |
| 790 | as_bad (_("wrong second argument to .cfi_lsda")); |
| 791 | ignore_rest_of_line (); |
| 792 | return; |
| 793 | } |
| 794 | |
| 795 | demand_empty_rest_of_line (); |
| 796 | } |
| 797 | |
| 798 | static void |
| 799 | dot_cfi_val_encoded_addr (int ignored ATTRIBUTE_UNUSED) |
| 800 | { |
| 801 | struct cfi_insn_data *insn_ptr; |
| 802 | offsetT encoding; |
| 803 | |
| 804 | if (frchain_now->frch_cfi_data == NULL) |
| 805 | { |
| 806 | as_bad (_("CFI instruction used without previous .cfi_startproc")); |
| 807 | ignore_rest_of_line (); |
| 808 | return; |
| 809 | } |
| 810 | |
| 811 | /* If the last address was not at the current PC, advance to current. */ |
| 812 | if (symbol_get_frag (frchain_now->frch_cfi_data->last_address) != frag_now |
| 813 | || S_GET_VALUE (frchain_now->frch_cfi_data->last_address) |
| 814 | != frag_now_fix ()) |
| 815 | cfi_add_advance_loc (symbol_temp_new_now ()); |
| 816 | |
| 817 | insn_ptr = alloc_cfi_insn_data (); |
| 818 | insn_ptr->insn = CFI_val_encoded_addr; |
| 819 | |
| 820 | insn_ptr->u.ea.reg = cfi_parse_reg (); |
| 821 | |
| 822 | cfi_parse_separator (); |
| 823 | encoding = cfi_parse_const (); |
| 824 | if ((encoding & 0xff) != encoding |
| 825 | || ((encoding & 0x70) != 0 |
| 826 | #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr |
| 827 | && (encoding & 0x70) != DW_EH_PE_pcrel |
| 828 | #endif |
| 829 | ) |
| 830 | /* leb128 can be handled, but does something actually need it? */ |
| 831 | || (encoding & 7) == DW_EH_PE_uleb128 |
| 832 | || (encoding & 7) > DW_EH_PE_udata8) |
| 833 | { |
| 834 | as_bad (_("invalid or unsupported encoding in .cfi_lsda")); |
| 835 | encoding = DW_EH_PE_omit; |
| 836 | } |
| 837 | |
| 838 | cfi_parse_separator (); |
| 839 | expression_and_evaluate (&insn_ptr->u.ea.exp); |
| 840 | switch (insn_ptr->u.ea.exp.X_op) |
| 841 | { |
| 842 | case O_symbol: |
| 843 | break; |
| 844 | case O_constant: |
| 845 | if ((encoding & 0x70) != DW_EH_PE_pcrel) |
| 846 | break; |
| 847 | default: |
| 848 | encoding = DW_EH_PE_omit; |
| 849 | break; |
| 850 | } |
| 851 | |
| 852 | insn_ptr->u.ea.encoding = encoding; |
| 853 | if (encoding == DW_EH_PE_omit) |
| 854 | { |
| 855 | as_bad (_("wrong third argument to .cfi_val_encoded_addr")); |
| 856 | ignore_rest_of_line (); |
| 857 | return; |
| 858 | } |
| 859 | |
| 860 | demand_empty_rest_of_line (); |
| 861 | } |
| 862 | |
| 863 | static void |
| 864 | dot_cfi_startproc (int ignored ATTRIBUTE_UNUSED) |
| 865 | { |
| 866 | int simple = 0; |
| 867 | |
| 868 | if (frchain_now->frch_cfi_data != NULL) |
| 869 | { |
| 870 | as_bad (_("previous CFI entry not closed (missing .cfi_endproc)")); |
| 871 | ignore_rest_of_line (); |
| 872 | return; |
| 873 | } |
| 874 | |
| 875 | cfi_new_fde (symbol_temp_new_now ()); |
| 876 | |
| 877 | SKIP_WHITESPACE (); |
| 878 | if (is_name_beginner (*input_line_pointer)) |
| 879 | { |
| 880 | char *name, c; |
| 881 | |
| 882 | name = input_line_pointer; |
| 883 | c = get_symbol_end (); |
| 884 | |
| 885 | if (strcmp (name, "simple") == 0) |
| 886 | { |
| 887 | simple = 1; |
| 888 | *input_line_pointer = c; |
| 889 | } |
| 890 | else |
| 891 | input_line_pointer = name; |
| 892 | } |
| 893 | demand_empty_rest_of_line (); |
| 894 | |
| 895 | frchain_now->frch_cfi_data->cur_cfa_offset = 0; |
| 896 | if (!simple) |
| 897 | tc_cfi_frame_initial_instructions (); |
| 898 | } |
| 899 | |
| 900 | static void |
| 901 | dot_cfi_endproc (int ignored ATTRIBUTE_UNUSED) |
| 902 | { |
| 903 | if (frchain_now->frch_cfi_data == NULL) |
| 904 | { |
| 905 | as_bad (_(".cfi_endproc without corresponding .cfi_startproc")); |
| 906 | ignore_rest_of_line (); |
| 907 | return; |
| 908 | } |
| 909 | |
| 910 | cfi_end_fde (symbol_temp_new_now ()); |
| 911 | |
| 912 | demand_empty_rest_of_line (); |
| 913 | } |
| 914 | |
| 915 | \f |
| 916 | /* Emit a single byte into the current segment. */ |
| 917 | |
| 918 | static inline void |
| 919 | out_one (int byte) |
| 920 | { |
| 921 | FRAG_APPEND_1_CHAR (byte); |
| 922 | } |
| 923 | |
| 924 | /* Emit a two-byte word into the current segment. */ |
| 925 | |
| 926 | static inline void |
| 927 | out_two (int data) |
| 928 | { |
| 929 | md_number_to_chars (frag_more (2), data, 2); |
| 930 | } |
| 931 | |
| 932 | /* Emit a four byte word into the current segment. */ |
| 933 | |
| 934 | static inline void |
| 935 | out_four (int data) |
| 936 | { |
| 937 | md_number_to_chars (frag_more (4), data, 4); |
| 938 | } |
| 939 | |
| 940 | /* Emit an unsigned "little-endian base 128" number. */ |
| 941 | |
| 942 | static void |
| 943 | out_uleb128 (addressT value) |
| 944 | { |
| 945 | output_leb128 (frag_more (sizeof_leb128 (value, 0)), value, 0); |
| 946 | } |
| 947 | |
| 948 | /* Emit an unsigned "little-endian base 128" number. */ |
| 949 | |
| 950 | static void |
| 951 | out_sleb128 (offsetT value) |
| 952 | { |
| 953 | output_leb128 (frag_more (sizeof_leb128 (value, 1)), value, 1); |
| 954 | } |
| 955 | |
| 956 | static void |
| 957 | output_cfi_insn (struct cfi_insn_data *insn) |
| 958 | { |
| 959 | offsetT offset; |
| 960 | unsigned int regno; |
| 961 | |
| 962 | switch (insn->insn) |
| 963 | { |
| 964 | case DW_CFA_advance_loc: |
| 965 | { |
| 966 | symbolS *from = insn->u.ll.lab1; |
| 967 | symbolS *to = insn->u.ll.lab2; |
| 968 | |
| 969 | if (symbol_get_frag (to) == symbol_get_frag (from)) |
| 970 | { |
| 971 | addressT delta = S_GET_VALUE (to) - S_GET_VALUE (from); |
| 972 | addressT scaled = delta / DWARF2_LINE_MIN_INSN_LENGTH; |
| 973 | |
| 974 | if (scaled <= 0x3F) |
| 975 | out_one (DW_CFA_advance_loc + scaled); |
| 976 | else if (scaled <= 0xFF) |
| 977 | { |
| 978 | out_one (DW_CFA_advance_loc1); |
| 979 | out_one (scaled); |
| 980 | } |
| 981 | else if (scaled <= 0xFFFF) |
| 982 | { |
| 983 | out_one (DW_CFA_advance_loc2); |
| 984 | out_two (scaled); |
| 985 | } |
| 986 | else |
| 987 | { |
| 988 | out_one (DW_CFA_advance_loc4); |
| 989 | out_four (scaled); |
| 990 | } |
| 991 | } |
| 992 | else |
| 993 | { |
| 994 | expressionS exp; |
| 995 | |
| 996 | exp.X_op = O_subtract; |
| 997 | exp.X_add_symbol = to; |
| 998 | exp.X_op_symbol = from; |
| 999 | exp.X_add_number = 0; |
| 1000 | |
| 1001 | /* The code in ehopt.c expects that one byte of the encoding |
| 1002 | is already allocated to the frag. This comes from the way |
| 1003 | that it scans the .eh_frame section looking first for the |
| 1004 | .byte DW_CFA_advance_loc4. */ |
| 1005 | *frag_more (1) = DW_CFA_advance_loc4; |
| 1006 | |
| 1007 | frag_var (rs_cfa, 4, 0, DWARF2_LINE_MIN_INSN_LENGTH << 3, |
| 1008 | make_expr_symbol (&exp), frag_now_fix () - 1, |
| 1009 | (char *) frag_now); |
| 1010 | } |
| 1011 | } |
| 1012 | break; |
| 1013 | |
| 1014 | case DW_CFA_def_cfa: |
| 1015 | offset = insn->u.ri.offset; |
| 1016 | if (offset < 0) |
| 1017 | { |
| 1018 | out_one (DW_CFA_def_cfa_sf); |
| 1019 | out_uleb128 (insn->u.ri.reg); |
| 1020 | out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT); |
| 1021 | } |
| 1022 | else |
| 1023 | { |
| 1024 | out_one (DW_CFA_def_cfa); |
| 1025 | out_uleb128 (insn->u.ri.reg); |
| 1026 | out_uleb128 (offset); |
| 1027 | } |
| 1028 | break; |
| 1029 | |
| 1030 | case DW_CFA_def_cfa_register: |
| 1031 | case DW_CFA_undefined: |
| 1032 | case DW_CFA_same_value: |
| 1033 | out_one (insn->insn); |
| 1034 | out_uleb128 (insn->u.r); |
| 1035 | break; |
| 1036 | |
| 1037 | case DW_CFA_def_cfa_offset: |
| 1038 | offset = insn->u.i; |
| 1039 | if (offset < 0) |
| 1040 | { |
| 1041 | out_one (DW_CFA_def_cfa_offset_sf); |
| 1042 | out_sleb128 (offset / DWARF2_CIE_DATA_ALIGNMENT); |
| 1043 | } |
| 1044 | else |
| 1045 | { |
| 1046 | out_one (DW_CFA_def_cfa_offset); |
| 1047 | out_uleb128 (offset); |
| 1048 | } |
| 1049 | break; |
| 1050 | |
| 1051 | case DW_CFA_restore: |
| 1052 | regno = insn->u.r; |
| 1053 | if (regno <= 0x3F) |
| 1054 | { |
| 1055 | out_one (DW_CFA_restore + regno); |
| 1056 | } |
| 1057 | else |
| 1058 | { |
| 1059 | out_one (DW_CFA_restore_extended); |
| 1060 | out_uleb128 (regno); |
| 1061 | } |
| 1062 | break; |
| 1063 | |
| 1064 | case DW_CFA_offset: |
| 1065 | regno = insn->u.ri.reg; |
| 1066 | offset = insn->u.ri.offset / DWARF2_CIE_DATA_ALIGNMENT; |
| 1067 | if (offset < 0) |
| 1068 | { |
| 1069 | out_one (DW_CFA_offset_extended_sf); |
| 1070 | out_uleb128 (regno); |
| 1071 | out_sleb128 (offset); |
| 1072 | } |
| 1073 | else if (regno <= 0x3F) |
| 1074 | { |
| 1075 | out_one (DW_CFA_offset + regno); |
| 1076 | out_uleb128 (offset); |
| 1077 | } |
| 1078 | else |
| 1079 | { |
| 1080 | out_one (DW_CFA_offset_extended); |
| 1081 | out_uleb128 (regno); |
| 1082 | out_uleb128 (offset); |
| 1083 | } |
| 1084 | break; |
| 1085 | |
| 1086 | case DW_CFA_register: |
| 1087 | out_one (DW_CFA_register); |
| 1088 | out_uleb128 (insn->u.rr.reg1); |
| 1089 | out_uleb128 (insn->u.rr.reg2); |
| 1090 | break; |
| 1091 | |
| 1092 | case DW_CFA_remember_state: |
| 1093 | case DW_CFA_restore_state: |
| 1094 | out_one (insn->insn); |
| 1095 | break; |
| 1096 | |
| 1097 | case DW_CFA_GNU_window_save: |
| 1098 | out_one (DW_CFA_GNU_window_save); |
| 1099 | break; |
| 1100 | |
| 1101 | case CFI_escape: |
| 1102 | { |
| 1103 | struct cfi_escape_data *e; |
| 1104 | for (e = insn->u.esc; e ; e = e->next) |
| 1105 | emit_expr (&e->exp, 1); |
| 1106 | break; |
| 1107 | } |
| 1108 | |
| 1109 | case CFI_val_encoded_addr: |
| 1110 | { |
| 1111 | unsigned encoding = insn->u.ea.encoding; |
| 1112 | offsetT encoding_size; |
| 1113 | |
| 1114 | if (encoding == DW_EH_PE_omit) |
| 1115 | break; |
| 1116 | out_one (DW_CFA_val_expression); |
| 1117 | out_uleb128 (insn->u.ea.reg); |
| 1118 | |
| 1119 | switch (encoding & 0x7) |
| 1120 | { |
| 1121 | case DW_EH_PE_absptr: |
| 1122 | encoding_size = DWARF2_ADDR_SIZE (stdoutput); |
| 1123 | break; |
| 1124 | case DW_EH_PE_udata2: |
| 1125 | encoding_size = 2; |
| 1126 | break; |
| 1127 | case DW_EH_PE_udata4: |
| 1128 | encoding_size = 4; |
| 1129 | break; |
| 1130 | case DW_EH_PE_udata8: |
| 1131 | encoding_size = 8; |
| 1132 | break; |
| 1133 | default: |
| 1134 | abort (); |
| 1135 | } |
| 1136 | |
| 1137 | /* If the user has requested absolute encoding, |
| 1138 | then use the smaller DW_OP_addr encoding. */ |
| 1139 | if (insn->u.ea.encoding == DW_EH_PE_absptr) |
| 1140 | { |
| 1141 | out_uleb128 (1 + encoding_size); |
| 1142 | out_one (DW_OP_addr); |
| 1143 | } |
| 1144 | else |
| 1145 | { |
| 1146 | out_uleb128 (1 + 1 + encoding_size); |
| 1147 | out_one (DW_OP_GNU_encoded_addr); |
| 1148 | out_one (encoding); |
| 1149 | |
| 1150 | if ((encoding & 0x70) == DW_EH_PE_pcrel) |
| 1151 | { |
| 1152 | #if CFI_DIFF_EXPR_OK |
| 1153 | insn->u.ea.exp.X_op = O_subtract; |
| 1154 | insn->u.ea.exp.X_op_symbol = symbol_temp_new_now (); |
| 1155 | #elif defined (tc_cfi_emit_pcrel_expr) |
| 1156 | tc_cfi_emit_pcrel_expr (&insn->u.ea.exp, encoding_size); |
| 1157 | break; |
| 1158 | #else |
| 1159 | abort (); |
| 1160 | #endif |
| 1161 | } |
| 1162 | } |
| 1163 | emit_expr (&insn->u.ea.exp, encoding_size); |
| 1164 | } |
| 1165 | break; |
| 1166 | |
| 1167 | default: |
| 1168 | abort (); |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | static offsetT |
| 1173 | encoding_size (unsigned char encoding) |
| 1174 | { |
| 1175 | if (encoding == DW_EH_PE_omit) |
| 1176 | return 0; |
| 1177 | switch (encoding & 0x7) |
| 1178 | { |
| 1179 | case 0: |
| 1180 | return bfd_get_arch_size (stdoutput) == 64 ? 8 : 4; |
| 1181 | case DW_EH_PE_udata2: |
| 1182 | return 2; |
| 1183 | case DW_EH_PE_udata4: |
| 1184 | return 4; |
| 1185 | case DW_EH_PE_udata8: |
| 1186 | return 8; |
| 1187 | default: |
| 1188 | abort (); |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | static void |
| 1193 | output_cie (struct cie_entry *cie) |
| 1194 | { |
| 1195 | symbolS *after_size_address, *end_address; |
| 1196 | expressionS exp; |
| 1197 | struct cfi_insn_data *i; |
| 1198 | offsetT augmentation_size; |
| 1199 | int enc; |
| 1200 | |
| 1201 | cie->start_address = symbol_temp_new_now (); |
| 1202 | after_size_address = symbol_temp_make (); |
| 1203 | end_address = symbol_temp_make (); |
| 1204 | |
| 1205 | exp.X_op = O_subtract; |
| 1206 | exp.X_add_symbol = end_address; |
| 1207 | exp.X_op_symbol = after_size_address; |
| 1208 | exp.X_add_number = 0; |
| 1209 | |
| 1210 | emit_expr (&exp, 4); /* Length. */ |
| 1211 | symbol_set_value_now (after_size_address); |
| 1212 | out_four (0); /* CIE id. */ |
| 1213 | out_one (DW_CIE_VERSION); /* Version. */ |
| 1214 | out_one ('z'); /* Augmentation. */ |
| 1215 | if (cie->per_encoding != DW_EH_PE_omit) |
| 1216 | out_one ('P'); |
| 1217 | if (cie->lsda_encoding != DW_EH_PE_omit) |
| 1218 | out_one ('L'); |
| 1219 | out_one ('R'); |
| 1220 | if (cie->signal_frame) |
| 1221 | out_one ('S'); |
| 1222 | out_one (0); |
| 1223 | out_uleb128 (DWARF2_LINE_MIN_INSN_LENGTH); /* Code alignment. */ |
| 1224 | out_sleb128 (DWARF2_CIE_DATA_ALIGNMENT); /* Data alignment. */ |
| 1225 | if (DW_CIE_VERSION == 1) /* Return column. */ |
| 1226 | out_one (cie->return_column); |
| 1227 | else |
| 1228 | out_uleb128 (cie->return_column); |
| 1229 | augmentation_size = 1 + (cie->lsda_encoding != DW_EH_PE_omit); |
| 1230 | if (cie->per_encoding != DW_EH_PE_omit) |
| 1231 | augmentation_size += 1 + encoding_size (cie->per_encoding); |
| 1232 | out_uleb128 (augmentation_size); /* Augmentation size. */ |
| 1233 | if (cie->per_encoding != DW_EH_PE_omit) |
| 1234 | { |
| 1235 | offsetT size = encoding_size (cie->per_encoding); |
| 1236 | out_one (cie->per_encoding); |
| 1237 | exp = cie->personality; |
| 1238 | if ((cie->per_encoding & 0x70) == DW_EH_PE_pcrel) |
| 1239 | { |
| 1240 | #if CFI_DIFF_EXPR_OK |
| 1241 | exp.X_op = O_subtract; |
| 1242 | exp.X_op_symbol = symbol_temp_new_now (); |
| 1243 | emit_expr (&exp, size); |
| 1244 | #elif defined (tc_cfi_emit_pcrel_expr) |
| 1245 | tc_cfi_emit_pcrel_expr (&exp, size); |
| 1246 | #else |
| 1247 | abort (); |
| 1248 | #endif |
| 1249 | } |
| 1250 | else |
| 1251 | emit_expr (&exp, size); |
| 1252 | } |
| 1253 | if (cie->lsda_encoding != DW_EH_PE_omit) |
| 1254 | out_one (cie->lsda_encoding); |
| 1255 | |
| 1256 | switch (DWARF2_FDE_RELOC_SIZE) |
| 1257 | { |
| 1258 | case 2: |
| 1259 | enc = DW_EH_PE_sdata2; |
| 1260 | break; |
| 1261 | case 4: |
| 1262 | enc = DW_EH_PE_sdata4; |
| 1263 | break; |
| 1264 | case 8: |
| 1265 | enc = DW_EH_PE_sdata8; |
| 1266 | break; |
| 1267 | default: |
| 1268 | abort (); |
| 1269 | } |
| 1270 | #if CFI_DIFF_EXPR_OK || defined tc_cfi_emit_pcrel_expr |
| 1271 | enc |= DW_EH_PE_pcrel; |
| 1272 | #endif |
| 1273 | out_one (enc); |
| 1274 | |
| 1275 | if (cie->first) |
| 1276 | for (i = cie->first; i != cie->last; i = i->next) |
| 1277 | output_cfi_insn (i); |
| 1278 | |
| 1279 | frag_align (2, DW_CFA_nop, 0); |
| 1280 | symbol_set_value_now (end_address); |
| 1281 | } |
| 1282 | |
| 1283 | static void |
| 1284 | output_fde (struct fde_entry *fde, struct cie_entry *cie, |
| 1285 | struct cfi_insn_data *first, int align) |
| 1286 | { |
| 1287 | symbolS *after_size_address, *end_address; |
| 1288 | expressionS exp; |
| 1289 | offsetT augmentation_size; |
| 1290 | |
| 1291 | after_size_address = symbol_temp_make (); |
| 1292 | end_address = symbol_temp_make (); |
| 1293 | |
| 1294 | exp.X_op = O_subtract; |
| 1295 | exp.X_add_symbol = end_address; |
| 1296 | exp.X_op_symbol = after_size_address; |
| 1297 | exp.X_add_number = 0; |
| 1298 | emit_expr (&exp, 4); /* Length. */ |
| 1299 | symbol_set_value_now (after_size_address); |
| 1300 | |
| 1301 | exp.X_add_symbol = after_size_address; |
| 1302 | exp.X_op_symbol = cie->start_address; |
| 1303 | emit_expr (&exp, 4); /* CIE offset. */ |
| 1304 | |
| 1305 | #if CFI_DIFF_EXPR_OK |
| 1306 | exp.X_add_symbol = fde->start_address; |
| 1307 | exp.X_op_symbol = symbol_temp_new_now (); |
| 1308 | emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */ |
| 1309 | #else |
| 1310 | exp.X_op = O_symbol; |
| 1311 | exp.X_add_symbol = fde->start_address; |
| 1312 | exp.X_op_symbol = NULL; |
| 1313 | #ifdef tc_cfi_emit_pcrel_expr |
| 1314 | tc_cfi_emit_pcrel_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */ |
| 1315 | #else |
| 1316 | emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); /* Code offset. */ |
| 1317 | #endif |
| 1318 | exp.X_op = O_subtract; |
| 1319 | #endif |
| 1320 | |
| 1321 | exp.X_add_symbol = fde->end_address; |
| 1322 | exp.X_op_symbol = fde->start_address; /* Code length. */ |
| 1323 | emit_expr (&exp, DWARF2_FDE_RELOC_SIZE); |
| 1324 | |
| 1325 | augmentation_size = encoding_size (fde->lsda_encoding); |
| 1326 | out_uleb128 (augmentation_size); /* Augmentation size. */ |
| 1327 | |
| 1328 | if (fde->lsda_encoding != DW_EH_PE_omit) |
| 1329 | { |
| 1330 | exp = fde->lsda; |
| 1331 | if ((fde->lsda_encoding & 0x70) == DW_EH_PE_pcrel) |
| 1332 | { |
| 1333 | #if CFI_DIFF_EXPR_OK |
| 1334 | exp.X_op = O_subtract; |
| 1335 | exp.X_op_symbol = symbol_temp_new_now (); |
| 1336 | emit_expr (&exp, augmentation_size); |
| 1337 | #elif defined (tc_cfi_emit_pcrel_expr) |
| 1338 | tc_cfi_emit_pcrel_expr (&exp, augmentation_size); |
| 1339 | #else |
| 1340 | abort (); |
| 1341 | #endif |
| 1342 | } |
| 1343 | else |
| 1344 | emit_expr (&exp, augmentation_size); |
| 1345 | } |
| 1346 | |
| 1347 | for (; first; first = first->next) |
| 1348 | output_cfi_insn (first); |
| 1349 | |
| 1350 | frag_align (align, DW_CFA_nop, 0); |
| 1351 | symbol_set_value_now (end_address); |
| 1352 | } |
| 1353 | |
| 1354 | static struct cie_entry * |
| 1355 | select_cie_for_fde (struct fde_entry *fde, struct cfi_insn_data **pfirst) |
| 1356 | { |
| 1357 | struct cfi_insn_data *i, *j; |
| 1358 | struct cie_entry *cie; |
| 1359 | |
| 1360 | for (cie = cie_root; cie; cie = cie->next) |
| 1361 | { |
| 1362 | if (cie->return_column != fde->return_column |
| 1363 | || cie->signal_frame != fde->signal_frame |
| 1364 | || cie->per_encoding != fde->per_encoding |
| 1365 | || cie->lsda_encoding != fde->lsda_encoding) |
| 1366 | continue; |
| 1367 | if (cie->per_encoding != DW_EH_PE_omit) |
| 1368 | { |
| 1369 | if (cie->personality.X_op != fde->personality.X_op |
| 1370 | || cie->personality.X_add_number |
| 1371 | != fde->personality.X_add_number) |
| 1372 | continue; |
| 1373 | switch (cie->personality.X_op) |
| 1374 | { |
| 1375 | case O_constant: |
| 1376 | if (cie->personality.X_unsigned != fde->personality.X_unsigned) |
| 1377 | continue; |
| 1378 | break; |
| 1379 | case O_symbol: |
| 1380 | if (cie->personality.X_add_symbol |
| 1381 | != fde->personality.X_add_symbol) |
| 1382 | continue; |
| 1383 | break; |
| 1384 | default: |
| 1385 | abort (); |
| 1386 | } |
| 1387 | } |
| 1388 | for (i = cie->first, j = fde->data; |
| 1389 | i != cie->last && j != NULL; |
| 1390 | i = i->next, j = j->next) |
| 1391 | { |
| 1392 | if (i->insn != j->insn) |
| 1393 | goto fail; |
| 1394 | switch (i->insn) |
| 1395 | { |
| 1396 | case DW_CFA_advance_loc: |
| 1397 | case DW_CFA_remember_state: |
| 1398 | /* We reached the first advance/remember in the FDE, |
| 1399 | but did not reach the end of the CIE list. */ |
| 1400 | goto fail; |
| 1401 | |
| 1402 | case DW_CFA_offset: |
| 1403 | case DW_CFA_def_cfa: |
| 1404 | if (i->u.ri.reg != j->u.ri.reg) |
| 1405 | goto fail; |
| 1406 | if (i->u.ri.offset != j->u.ri.offset) |
| 1407 | goto fail; |
| 1408 | break; |
| 1409 | |
| 1410 | case DW_CFA_register: |
| 1411 | if (i->u.rr.reg1 != j->u.rr.reg1) |
| 1412 | goto fail; |
| 1413 | if (i->u.rr.reg2 != j->u.rr.reg2) |
| 1414 | goto fail; |
| 1415 | break; |
| 1416 | |
| 1417 | case DW_CFA_def_cfa_register: |
| 1418 | case DW_CFA_restore: |
| 1419 | case DW_CFA_undefined: |
| 1420 | case DW_CFA_same_value: |
| 1421 | if (i->u.r != j->u.r) |
| 1422 | goto fail; |
| 1423 | break; |
| 1424 | |
| 1425 | case DW_CFA_def_cfa_offset: |
| 1426 | if (i->u.i != j->u.i) |
| 1427 | goto fail; |
| 1428 | break; |
| 1429 | |
| 1430 | case CFI_escape: |
| 1431 | case CFI_val_encoded_addr: |
| 1432 | /* Don't bother matching these for now. */ |
| 1433 | goto fail; |
| 1434 | |
| 1435 | default: |
| 1436 | abort (); |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | /* Success if we reached the end of the CIE list, and we've either |
| 1441 | run out of FDE entries or we've encountered an advance, |
| 1442 | remember, or escape. */ |
| 1443 | if (i == cie->last |
| 1444 | && (!j |
| 1445 | || j->insn == DW_CFA_advance_loc |
| 1446 | || j->insn == DW_CFA_remember_state |
| 1447 | || j->insn == CFI_escape |
| 1448 | || j->insn == CFI_val_encoded_addr)) |
| 1449 | { |
| 1450 | *pfirst = j; |
| 1451 | return cie; |
| 1452 | } |
| 1453 | |
| 1454 | fail:; |
| 1455 | } |
| 1456 | |
| 1457 | cie = xmalloc (sizeof (struct cie_entry)); |
| 1458 | cie->next = cie_root; |
| 1459 | cie_root = cie; |
| 1460 | cie->return_column = fde->return_column; |
| 1461 | cie->signal_frame = fde->signal_frame; |
| 1462 | cie->per_encoding = fde->per_encoding; |
| 1463 | cie->lsda_encoding = fde->lsda_encoding; |
| 1464 | cie->personality = fde->personality; |
| 1465 | cie->first = fde->data; |
| 1466 | |
| 1467 | for (i = cie->first; i ; i = i->next) |
| 1468 | if (i->insn == DW_CFA_advance_loc |
| 1469 | || i->insn == DW_CFA_remember_state |
| 1470 | || i->insn == CFI_escape |
| 1471 | || i->insn == CFI_val_encoded_addr) |
| 1472 | break; |
| 1473 | |
| 1474 | cie->last = i; |
| 1475 | *pfirst = i; |
| 1476 | |
| 1477 | output_cie (cie); |
| 1478 | |
| 1479 | return cie; |
| 1480 | } |
| 1481 | |
| 1482 | void |
| 1483 | cfi_finish (void) |
| 1484 | { |
| 1485 | segT cfi_seg; |
| 1486 | struct fde_entry *fde; |
| 1487 | int save_flag_traditional_format; |
| 1488 | |
| 1489 | if (all_fde_data == 0) |
| 1490 | return; |
| 1491 | |
| 1492 | /* Open .eh_frame section. */ |
| 1493 | cfi_seg = subseg_new (".eh_frame", 0); |
| 1494 | bfd_set_section_flags (stdoutput, cfi_seg, |
| 1495 | SEC_ALLOC | SEC_LOAD | SEC_DATA |
| 1496 | | DWARF2_EH_FRAME_READ_ONLY); |
| 1497 | subseg_set (cfi_seg, 0); |
| 1498 | record_alignment (cfi_seg, EH_FRAME_ALIGNMENT); |
| 1499 | |
| 1500 | #ifdef md_fix_up_eh_frame |
| 1501 | md_fix_up_eh_frame (cfi_seg); |
| 1502 | #endif |
| 1503 | |
| 1504 | /* Make sure check_eh_frame doesn't do anything with our output. */ |
| 1505 | save_flag_traditional_format = flag_traditional_format; |
| 1506 | flag_traditional_format = 1; |
| 1507 | |
| 1508 | for (fde = all_fde_data; fde ; fde = fde->next) |
| 1509 | { |
| 1510 | struct cfi_insn_data *first; |
| 1511 | struct cie_entry *cie; |
| 1512 | |
| 1513 | if (fde->end_address == NULL) |
| 1514 | { |
| 1515 | as_bad (_("open CFI at the end of file; missing .cfi_endproc directive")); |
| 1516 | fde->end_address = fde->start_address; |
| 1517 | } |
| 1518 | |
| 1519 | cie = select_cie_for_fde (fde, &first); |
| 1520 | output_fde (fde, cie, first, fde->next == NULL ? EH_FRAME_ALIGNMENT : 2); |
| 1521 | } |
| 1522 | |
| 1523 | flag_traditional_format = save_flag_traditional_format; |
| 1524 | } |
| 1525 | |
| 1526 | #else /* TARGET_USE_CFIPOP */ |
| 1527 | void |
| 1528 | cfi_finish (void) |
| 1529 | { |
| 1530 | } |
| 1531 | #endif /* TARGET_USE_CFIPOP */ |