| 1 | /* seh pdata/xdata coff object file format |
| 2 | Copyright 2009, 2010 |
| 3 | Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GAS. |
| 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 3, 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, 51 Franklin Street - Fifth Floor, Boston, MA |
| 20 | 02110-1301, USA. */ |
| 21 | |
| 22 | #include "obj-coff-seh.h" |
| 23 | |
| 24 | |
| 25 | /* Local data. */ |
| 26 | static seh_context *seh_ctx_cur = NULL; |
| 27 | |
| 28 | static segT xdata_seg; |
| 29 | static segT pdata_seg; |
| 30 | static int xdata_subseg; |
| 31 | |
| 32 | static void write_function_xdata (seh_context *); |
| 33 | static void write_function_pdata (seh_context *); |
| 34 | |
| 35 | \f |
| 36 | static void |
| 37 | switch_xdata (int subseg) |
| 38 | { |
| 39 | if (xdata_seg == NULL) |
| 40 | { |
| 41 | xdata_seg = subseg_new (".xdata", 0); |
| 42 | bfd_set_section_flags (stdoutput, xdata_seg, |
| 43 | ((SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA) |
| 44 | & bfd_applicable_section_flags (stdoutput))); |
| 45 | } |
| 46 | subseg_set (xdata_seg, subseg); |
| 47 | } |
| 48 | |
| 49 | static void |
| 50 | switch_pdata (void) |
| 51 | { |
| 52 | if (pdata_seg == NULL) |
| 53 | { |
| 54 | pdata_seg = subseg_new (".pdata", 0); |
| 55 | bfd_set_section_flags (stdoutput, pdata_seg, |
| 56 | ((SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA) |
| 57 | & bfd_applicable_section_flags (stdoutput))); |
| 58 | } |
| 59 | else |
| 60 | subseg_set (pdata_seg, 0); |
| 61 | } |
| 62 | \f |
| 63 | /* Parsing routines. */ |
| 64 | |
| 65 | /* Return the style of SEH unwind info to generate. */ |
| 66 | |
| 67 | static seh_kind |
| 68 | seh_get_target_kind (void) |
| 69 | { |
| 70 | if (!stdoutput) |
| 71 | return seh_kind_unknown; |
| 72 | switch (bfd_get_arch (stdoutput)) |
| 73 | { |
| 74 | case bfd_arch_arm: |
| 75 | case bfd_arch_powerpc: |
| 76 | case bfd_arch_sh: |
| 77 | return seh_kind_arm; |
| 78 | case bfd_arch_i386: |
| 79 | switch (bfd_get_mach (stdoutput)) |
| 80 | { |
| 81 | case bfd_mach_x86_64: |
| 82 | case bfd_mach_x86_64_intel_syntax: |
| 83 | return seh_kind_x64; |
| 84 | default: |
| 85 | break; |
| 86 | } |
| 87 | /* FALL THROUGH. */ |
| 88 | case bfd_arch_mips: |
| 89 | return seh_kind_mips; |
| 90 | case bfd_arch_ia64: |
| 91 | /* Should return seh_kind_x64. But not implemented yet. */ |
| 92 | return seh_kind_unknown; |
| 93 | default: |
| 94 | break; |
| 95 | } |
| 96 | return seh_kind_unknown; |
| 97 | } |
| 98 | |
| 99 | /* Verify that we're in the context of a seh_proc. */ |
| 100 | |
| 101 | static int |
| 102 | verify_context (const char *directive) |
| 103 | { |
| 104 | if (seh_ctx_cur == NULL) |
| 105 | { |
| 106 | as_bad (_("%s used outside of .seh_proc block"), directive); |
| 107 | ignore_rest_of_line (); |
| 108 | return 0; |
| 109 | } |
| 110 | return 1; |
| 111 | } |
| 112 | |
| 113 | /* Similar, except we also verify the appropriate target. */ |
| 114 | |
| 115 | static int |
| 116 | verify_context_and_target (const char *directive, seh_kind target) |
| 117 | { |
| 118 | if (seh_get_target_kind () != target) |
| 119 | { |
| 120 | as_warn (_("%s ignored for this target"), directive); |
| 121 | ignore_rest_of_line (); |
| 122 | return 0; |
| 123 | } |
| 124 | return verify_context (directive); |
| 125 | } |
| 126 | |
| 127 | /* Skip whitespace and a comma. Error if the comma is not seen. */ |
| 128 | |
| 129 | static int |
| 130 | skip_whitespace_and_comma (int required) |
| 131 | { |
| 132 | SKIP_WHITESPACE (); |
| 133 | if (*input_line_pointer == ',') |
| 134 | { |
| 135 | input_line_pointer++; |
| 136 | SKIP_WHITESPACE (); |
| 137 | return 1; |
| 138 | } |
| 139 | else if (required) |
| 140 | { |
| 141 | as_bad (_("missing separator")); |
| 142 | ignore_rest_of_line (); |
| 143 | } |
| 144 | else |
| 145 | demand_empty_rest_of_line (); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | /* Mark current context to use 32-bit instruction (arm). */ |
| 150 | |
| 151 | static void |
| 152 | obj_coff_seh_32 (int what) |
| 153 | { |
| 154 | if (!verify_context_and_target ((what ? ".seh_32" : ".seh_no32"), |
| 155 | seh_kind_arm)) |
| 156 | return; |
| 157 | |
| 158 | seh_ctx_cur->use_instruction_32 = (what ? 1 : 0); |
| 159 | demand_empty_rest_of_line (); |
| 160 | } |
| 161 | |
| 162 | /* Set for current context the handler and optional data (arm). */ |
| 163 | |
| 164 | static void |
| 165 | obj_coff_seh_eh (int what ATTRIBUTE_UNUSED) |
| 166 | { |
| 167 | if (!verify_context_and_target (".seh_eh", seh_kind_arm)) |
| 168 | return; |
| 169 | |
| 170 | /* Write block to .text if exception handler is set. */ |
| 171 | seh_ctx_cur->handler_written = 1; |
| 172 | emit_expr (&seh_ctx_cur->handler, 4); |
| 173 | emit_expr (&seh_ctx_cur->handler_data, 4); |
| 174 | |
| 175 | demand_empty_rest_of_line (); |
| 176 | } |
| 177 | |
| 178 | /* Set for current context the default handler (x64). */ |
| 179 | |
| 180 | static void |
| 181 | obj_coff_seh_handler (int what ATTRIBUTE_UNUSED) |
| 182 | { |
| 183 | char *symbol_name; |
| 184 | char name_end; |
| 185 | |
| 186 | if (!verify_context (".seh_handler")) |
| 187 | return; |
| 188 | |
| 189 | if (*input_line_pointer == 0 || *input_line_pointer == '\n') |
| 190 | { |
| 191 | as_bad (_(".seh_handler requires a handler")); |
| 192 | demand_empty_rest_of_line (); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | SKIP_WHITESPACE (); |
| 197 | |
| 198 | if (*input_line_pointer == '@') |
| 199 | { |
| 200 | symbol_name = input_line_pointer; |
| 201 | name_end = get_symbol_end (); |
| 202 | |
| 203 | seh_ctx_cur->handler.X_op = O_constant; |
| 204 | seh_ctx_cur->handler.X_add_number = 0; |
| 205 | |
| 206 | if (strcasecmp (symbol_name, "@0") == 0 |
| 207 | || strcasecmp (symbol_name, "@null") == 0) |
| 208 | ; |
| 209 | else if (strcasecmp (symbol_name, "@1") == 0) |
| 210 | seh_ctx_cur->handler.X_add_number = 1; |
| 211 | else |
| 212 | as_bad (_("unknown constant value '%s' for handler"), symbol_name); |
| 213 | |
| 214 | *input_line_pointer = name_end; |
| 215 | } |
| 216 | else |
| 217 | expression (&seh_ctx_cur->handler); |
| 218 | |
| 219 | seh_ctx_cur->handler_data.X_op = O_constant; |
| 220 | seh_ctx_cur->handler_data.X_add_number = 0; |
| 221 | seh_ctx_cur->handler_flags = 0; |
| 222 | |
| 223 | if (!skip_whitespace_and_comma (0)) |
| 224 | return; |
| 225 | |
| 226 | if (seh_get_target_kind () == seh_kind_x64) |
| 227 | { |
| 228 | do |
| 229 | { |
| 230 | symbol_name = input_line_pointer; |
| 231 | name_end = get_symbol_end (); |
| 232 | |
| 233 | if (strcasecmp (symbol_name, "@unwind") == 0) |
| 234 | seh_ctx_cur->handler_flags |= UNW_FLAG_UHANDLER; |
| 235 | else if (strcasecmp (symbol_name, "@except") == 0) |
| 236 | seh_ctx_cur->handler_flags |= UNW_FLAG_EHANDLER; |
| 237 | else |
| 238 | as_bad (_(".seh_handler constant '%s' unknown"), symbol_name); |
| 239 | |
| 240 | *input_line_pointer = name_end; |
| 241 | } |
| 242 | while (skip_whitespace_and_comma (0)); |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | expression (&seh_ctx_cur->handler_data); |
| 247 | demand_empty_rest_of_line (); |
| 248 | |
| 249 | if (seh_ctx_cur->handler_written) |
| 250 | as_warn (_(".seh_handler after .seh_eh is ignored")); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | /* Switch to subsection for handler data for exception region (x64). */ |
| 255 | |
| 256 | static void |
| 257 | obj_coff_seh_handlerdata (int what ATTRIBUTE_UNUSED) |
| 258 | { |
| 259 | if (!verify_context_and_target (".seh_handlerdata", seh_kind_x64)) |
| 260 | return; |
| 261 | demand_empty_rest_of_line (); |
| 262 | |
| 263 | switch_xdata (seh_ctx_cur->subsection + 1); |
| 264 | } |
| 265 | |
| 266 | /* Mark end of current context. */ |
| 267 | |
| 268 | static void |
| 269 | do_seh_endproc (void) |
| 270 | { |
| 271 | seh_ctx_cur->end_addr = symbol_temp_new_now (); |
| 272 | |
| 273 | write_function_xdata (seh_ctx_cur); |
| 274 | write_function_pdata (seh_ctx_cur); |
| 275 | seh_ctx_cur = NULL; |
| 276 | } |
| 277 | |
| 278 | static void |
| 279 | obj_coff_seh_endproc (int what ATTRIBUTE_UNUSED) |
| 280 | { |
| 281 | demand_empty_rest_of_line (); |
| 282 | if (seh_ctx_cur == NULL) |
| 283 | { |
| 284 | as_bad (_(".seh_endproc used without .seh_proc")); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | do_seh_endproc (); |
| 289 | } |
| 290 | |
| 291 | /* Mark begin of new context. */ |
| 292 | |
| 293 | static void |
| 294 | obj_coff_seh_proc (int what ATTRIBUTE_UNUSED) |
| 295 | { |
| 296 | char *symbol_name; |
| 297 | char name_end; |
| 298 | |
| 299 | if (seh_ctx_cur != NULL) |
| 300 | { |
| 301 | as_bad (_("previous SEH entry not closed (missing .seh_endproc)")); |
| 302 | do_seh_endproc (); |
| 303 | } |
| 304 | |
| 305 | if (*input_line_pointer == 0 || *input_line_pointer == '\n') |
| 306 | { |
| 307 | as_bad (_(".seh_proc requires function label name")); |
| 308 | demand_empty_rest_of_line (); |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | seh_ctx_cur = XCNEW (seh_context); |
| 313 | |
| 314 | if (seh_get_target_kind () == seh_kind_x64) |
| 315 | { |
| 316 | seh_ctx_cur->subsection = xdata_subseg; |
| 317 | xdata_subseg += 2; |
| 318 | } |
| 319 | |
| 320 | SKIP_WHITESPACE (); |
| 321 | |
| 322 | symbol_name = input_line_pointer; |
| 323 | name_end = get_symbol_end (); |
| 324 | seh_ctx_cur->func_name = xstrdup (symbol_name); |
| 325 | *input_line_pointer = name_end; |
| 326 | |
| 327 | demand_empty_rest_of_line (); |
| 328 | |
| 329 | seh_ctx_cur->start_addr = symbol_temp_new_now (); |
| 330 | } |
| 331 | |
| 332 | /* Mark end of prologue for current context. */ |
| 333 | |
| 334 | static void |
| 335 | obj_coff_seh_endprologue (int what ATTRIBUTE_UNUSED) |
| 336 | { |
| 337 | if (!verify_context (".seh_endprologue")) |
| 338 | return; |
| 339 | demand_empty_rest_of_line (); |
| 340 | |
| 341 | if (seh_ctx_cur->endprologue_addr != NULL) |
| 342 | as_warn (_("duplicate .seh_endprologue in .seh_proc block")); |
| 343 | else |
| 344 | seh_ctx_cur->endprologue_addr = symbol_temp_new_now (); |
| 345 | } |
| 346 | |
| 347 | /* End-of-file hook. */ |
| 348 | |
| 349 | void |
| 350 | obj_coff_seh_do_final (void) |
| 351 | { |
| 352 | if (seh_ctx_cur != NULL) |
| 353 | { |
| 354 | as_bad (_("open SEH entry at end of file (missing .cfi_endproc)")); |
| 355 | do_seh_endproc (); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /* Enter a prologue element into current context (x64). */ |
| 360 | |
| 361 | static void |
| 362 | seh_x64_make_prologue_element (int code, int info, offsetT off) |
| 363 | { |
| 364 | seh_prologue_element *n; |
| 365 | |
| 366 | if (seh_ctx_cur == NULL) |
| 367 | return; |
| 368 | if (seh_ctx_cur->elems_count == seh_ctx_cur->elems_max) |
| 369 | { |
| 370 | seh_ctx_cur->elems_max += 8; |
| 371 | seh_ctx_cur->elems = XRESIZEVEC (seh_prologue_element, |
| 372 | seh_ctx_cur->elems, |
| 373 | seh_ctx_cur->elems_max); |
| 374 | } |
| 375 | |
| 376 | n = &seh_ctx_cur->elems[seh_ctx_cur->elems_count++]; |
| 377 | n->code = code; |
| 378 | n->info = info; |
| 379 | n->off = off; |
| 380 | n->pc_addr = symbol_temp_new_now (); |
| 381 | } |
| 382 | |
| 383 | /* Helper to read a register name from input stream (x64). */ |
| 384 | |
| 385 | static int |
| 386 | seh_x64_read_reg (const char *directive, int kind) |
| 387 | { |
| 388 | static const char * const int_regs[16] = |
| 389 | { "rax", "rcx", "rdx", "rbx", "rsp", "rbp","rsi","rdi", |
| 390 | "r8","r9","r10","r11","r12","r13","r14","r15" }; |
| 391 | static const char * const xmm_regs[16] = |
| 392 | { "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7", |
| 393 | "xmm8", "xmm9", "xmm10","xmm11","xmm12","xmm13","xmm14","xmm15" }; |
| 394 | |
| 395 | const char * const *regs = NULL; |
| 396 | char name_end; |
| 397 | char *symbol_name = NULL; |
| 398 | int i; |
| 399 | |
| 400 | switch (kind) |
| 401 | { |
| 402 | case 0: |
| 403 | case 1: |
| 404 | regs = int_regs; |
| 405 | break; |
| 406 | case 2: |
| 407 | regs = xmm_regs; |
| 408 | break; |
| 409 | default: |
| 410 | abort (); |
| 411 | } |
| 412 | |
| 413 | SKIP_WHITESPACE (); |
| 414 | if (*input_line_pointer == '%') |
| 415 | ++input_line_pointer; |
| 416 | symbol_name = input_line_pointer; |
| 417 | name_end = get_symbol_end (); |
| 418 | |
| 419 | for (i = 0; i < 16; i++) |
| 420 | if (! strcasecmp (regs[i], symbol_name)) |
| 421 | break; |
| 422 | |
| 423 | *input_line_pointer = name_end; |
| 424 | |
| 425 | /* Error if register not found, or EAX used as a frame pointer. */ |
| 426 | if (i == 16 || (kind == 0 && i == 0)) |
| 427 | { |
| 428 | as_bad (_("invalid register for %s"), directive); |
| 429 | return -1; |
| 430 | } |
| 431 | |
| 432 | return i; |
| 433 | } |
| 434 | |
| 435 | /* Add a register push-unwind token to the current context. */ |
| 436 | |
| 437 | static void |
| 438 | obj_coff_seh_pushreg (int what ATTRIBUTE_UNUSED) |
| 439 | { |
| 440 | int reg; |
| 441 | |
| 442 | if (!verify_context_and_target (".seh_pushreg", seh_kind_x64)) |
| 443 | return; |
| 444 | |
| 445 | reg = seh_x64_read_reg (".seh_pushreg", 1); |
| 446 | demand_empty_rest_of_line (); |
| 447 | |
| 448 | if (reg < 0) |
| 449 | return; |
| 450 | |
| 451 | seh_x64_make_prologue_element (UWOP_PUSH_NONVOL, reg, 0); |
| 452 | } |
| 453 | |
| 454 | /* Add a register frame-unwind token to the current context. */ |
| 455 | |
| 456 | static void |
| 457 | obj_coff_seh_pushframe (int what ATTRIBUTE_UNUSED) |
| 458 | { |
| 459 | if (!verify_context_and_target (".seh_pushframe", seh_kind_x64)) |
| 460 | return; |
| 461 | demand_empty_rest_of_line (); |
| 462 | |
| 463 | seh_x64_make_prologue_element (UWOP_PUSH_MACHFRAME, 0, 0); |
| 464 | } |
| 465 | |
| 466 | /* Add a register save-unwind token to current context. */ |
| 467 | |
| 468 | static void |
| 469 | obj_coff_seh_save (int what) |
| 470 | { |
| 471 | const char *directive = (what == 1 ? ".seh_savereg" : ".seh_savexmm"); |
| 472 | int code, reg, scale; |
| 473 | offsetT off; |
| 474 | |
| 475 | if (!verify_context_and_target (directive, seh_kind_x64)) |
| 476 | return; |
| 477 | |
| 478 | reg = seh_x64_read_reg (directive, what); |
| 479 | |
| 480 | if (!skip_whitespace_and_comma (1)) |
| 481 | return; |
| 482 | |
| 483 | off = get_absolute_expression (); |
| 484 | demand_empty_rest_of_line (); |
| 485 | |
| 486 | if (reg < 0) |
| 487 | return; |
| 488 | if (off < 0) |
| 489 | { |
| 490 | as_bad (_("%s offset is negative"), directive); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | scale = (what == 1 ? 8 : 16); |
| 495 | |
| 496 | if ((off & (scale - 1)) == 0 && off <= 0xffff * scale) |
| 497 | { |
| 498 | code = (what == 1 ? UWOP_SAVE_NONVOL : UWOP_SAVE_XMM128); |
| 499 | off /= scale; |
| 500 | } |
| 501 | else if (off < 0xffffffff) |
| 502 | code = (what == 1 ? UWOP_SAVE_NONVOL_FAR : UWOP_SAVE_XMM128_FAR); |
| 503 | else |
| 504 | { |
| 505 | as_bad (_("%s offset out of range"), directive); |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | seh_x64_make_prologue_element (code, reg, off); |
| 510 | } |
| 511 | |
| 512 | /* Add a stack-allocation token to current context. */ |
| 513 | |
| 514 | static void |
| 515 | obj_coff_seh_stackalloc (int what ATTRIBUTE_UNUSED) |
| 516 | { |
| 517 | offsetT off; |
| 518 | int code, info; |
| 519 | |
| 520 | if (!verify_context_and_target (".seh_stackalloc", seh_kind_x64)) |
| 521 | return; |
| 522 | |
| 523 | off = get_absolute_expression (); |
| 524 | demand_empty_rest_of_line (); |
| 525 | |
| 526 | if (off == 0) |
| 527 | return; |
| 528 | if (off < 0) |
| 529 | { |
| 530 | as_bad (_(".seh_stackalloc offset is negative")); |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | if ((off & 7) == 0 && off <= 128) |
| 535 | code = UWOP_ALLOC_SMALL, info = (off - 8) >> 3, off = 0; |
| 536 | else if ((off & 7) == 0 && off <= 0xffff * 8) |
| 537 | code = UWOP_ALLOC_LARGE, info = 0, off >>= 3; |
| 538 | else if (off <= 0xffffffff) |
| 539 | code = UWOP_ALLOC_LARGE, info = 1; |
| 540 | else |
| 541 | { |
| 542 | as_bad (_(".seh_stackalloc offset out of range")); |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | seh_x64_make_prologue_element (code, info, off); |
| 547 | } |
| 548 | |
| 549 | /* Add a frame-pointer token to current context. */ |
| 550 | |
| 551 | static void |
| 552 | obj_coff_seh_setframe (int what ATTRIBUTE_UNUSED) |
| 553 | { |
| 554 | offsetT off; |
| 555 | int reg; |
| 556 | |
| 557 | if (!verify_context_and_target (".seh_setframe", seh_kind_x64)) |
| 558 | return; |
| 559 | |
| 560 | reg = seh_x64_read_reg (".seh_setframe", 0); |
| 561 | |
| 562 | if (!skip_whitespace_and_comma (1)) |
| 563 | return; |
| 564 | |
| 565 | off = get_absolute_expression (); |
| 566 | demand_empty_rest_of_line (); |
| 567 | |
| 568 | if (reg < 0) |
| 569 | return; |
| 570 | if (off < 0) |
| 571 | as_bad (_(".seh_setframe offset is negative")); |
| 572 | else if (off > 240) |
| 573 | as_bad (_(".seh_setframe offset out of range")); |
| 574 | else if (off & 15) |
| 575 | as_bad (_(".seh_setframe offset not a multiple of 16")); |
| 576 | else if (seh_ctx_cur->framereg != 0) |
| 577 | as_bad (_("duplicate .seh_setframe in current .seh_proc")); |
| 578 | else |
| 579 | { |
| 580 | seh_ctx_cur->framereg = reg; |
| 581 | seh_ctx_cur->frameoff = off; |
| 582 | seh_x64_make_prologue_element (UWOP_SET_FPREG, 0, 0); |
| 583 | } |
| 584 | } |
| 585 | \f |
| 586 | /* Data writing routines. */ |
| 587 | |
| 588 | /* Output raw integers in 1, 2, or 4 bytes. */ |
| 589 | |
| 590 | static inline void |
| 591 | out_one (int byte) |
| 592 | { |
| 593 | FRAG_APPEND_1_CHAR (byte); |
| 594 | } |
| 595 | |
| 596 | static inline void |
| 597 | out_two (int data) |
| 598 | { |
| 599 | md_number_to_chars (frag_more (2), data, 2); |
| 600 | } |
| 601 | |
| 602 | static inline void |
| 603 | out_four (int data) |
| 604 | { |
| 605 | md_number_to_chars (frag_more (4), data, 4); |
| 606 | } |
| 607 | |
| 608 | /* Write out prologue data for x64. */ |
| 609 | |
| 610 | static void |
| 611 | seh_x64_write_prologue_data (const seh_context *c) |
| 612 | { |
| 613 | int i; |
| 614 | |
| 615 | /* We have to store in reverse order. */ |
| 616 | for (i = c->elems_count - 1; i >= 0; --i) |
| 617 | { |
| 618 | const seh_prologue_element *e = c->elems + i; |
| 619 | expressionS exp; |
| 620 | |
| 621 | /* First comes byte offset in code. */ |
| 622 | exp.X_op = O_subtract; |
| 623 | exp.X_add_symbol = e->pc_addr; |
| 624 | exp.X_op_symbol = c->start_addr; |
| 625 | exp.X_add_number = 0; |
| 626 | emit_expr (&exp, 1); |
| 627 | |
| 628 | /* Second comes code+info packed into a byte. */ |
| 629 | out_one ((e->info << 4) | e->code); |
| 630 | |
| 631 | switch (e->code) |
| 632 | { |
| 633 | case UWOP_PUSH_NONVOL: |
| 634 | case UWOP_ALLOC_SMALL: |
| 635 | case UWOP_SET_FPREG: |
| 636 | case UWOP_PUSH_MACHFRAME: |
| 637 | /* These have no extra data. */ |
| 638 | break; |
| 639 | |
| 640 | case UWOP_ALLOC_LARGE: |
| 641 | if (e->info) |
| 642 | { |
| 643 | case UWOP_SAVE_NONVOL_FAR: |
| 644 | case UWOP_SAVE_XMM128_FAR: |
| 645 | /* An unscaled 4 byte offset. */ |
| 646 | out_four (e->off); |
| 647 | break; |
| 648 | } |
| 649 | /* FALLTHRU */ |
| 650 | |
| 651 | case UWOP_SAVE_NONVOL: |
| 652 | case UWOP_SAVE_XMM128: |
| 653 | /* A scaled 2 byte offset. */ |
| 654 | out_two (e->off); |
| 655 | break; |
| 656 | |
| 657 | default: |
| 658 | abort (); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | static int |
| 664 | seh_x64_size_prologue_data (const seh_context *c) |
| 665 | { |
| 666 | int i, ret = 0; |
| 667 | |
| 668 | for (i = c->elems_count - 1; i >= 0; --i) |
| 669 | switch (c->elems[i].code) |
| 670 | { |
| 671 | case UWOP_PUSH_NONVOL: |
| 672 | case UWOP_ALLOC_SMALL: |
| 673 | case UWOP_SET_FPREG: |
| 674 | case UWOP_PUSH_MACHFRAME: |
| 675 | ret += 1; |
| 676 | break; |
| 677 | |
| 678 | case UWOP_SAVE_NONVOL: |
| 679 | case UWOP_SAVE_XMM128: |
| 680 | ret += 2; |
| 681 | break; |
| 682 | |
| 683 | case UWOP_SAVE_NONVOL_FAR: |
| 684 | case UWOP_SAVE_XMM128_FAR: |
| 685 | ret += 3; |
| 686 | break; |
| 687 | |
| 688 | case UWOP_ALLOC_LARGE: |
| 689 | ret += (c->elems[i].info ? 3 : 2); |
| 690 | break; |
| 691 | |
| 692 | default: |
| 693 | abort (); |
| 694 | } |
| 695 | |
| 696 | return ret; |
| 697 | } |
| 698 | |
| 699 | /* Write out the xdata information for one function (x64). */ |
| 700 | |
| 701 | static void |
| 702 | seh_x64_write_function_xdata (seh_context *c) |
| 703 | { |
| 704 | int flags, count_unwind_codes; |
| 705 | expressionS exp; |
| 706 | |
| 707 | /* Set 4-byte alignment. */ |
| 708 | frag_align (2, 0, 0); |
| 709 | |
| 710 | c->xdata_addr = symbol_temp_new_now (); |
| 711 | flags = c->handler_flags; |
| 712 | count_unwind_codes = seh_x64_size_prologue_data (c); |
| 713 | |
| 714 | /* ubyte:3 version, ubyte:5 flags. */ |
| 715 | out_one ((flags << 3) | 1); |
| 716 | |
| 717 | /* Size of prologue. */ |
| 718 | if (c->endprologue_addr) |
| 719 | { |
| 720 | exp.X_op = O_subtract; |
| 721 | exp.X_add_symbol = c->endprologue_addr; |
| 722 | exp.X_op_symbol = c->start_addr; |
| 723 | exp.X_add_number = 0; |
| 724 | emit_expr (&exp, 1); |
| 725 | } |
| 726 | else |
| 727 | out_one (0); |
| 728 | |
| 729 | /* Number of slots (i.e. shorts) in the unwind codes array. */ |
| 730 | if (count_unwind_codes > 255) |
| 731 | as_fatal (_("too much unwind data in this .seh_proc")); |
| 732 | out_one (count_unwind_codes); |
| 733 | |
| 734 | /* ubyte:4 frame-reg, ubyte:4 frame-reg-offset. */ |
| 735 | /* Note that frameoff is already a multiple of 16, and therefore |
| 736 | the offset is already both scaled and shifted into place. */ |
| 737 | out_one (c->frameoff | c->framereg); |
| 738 | |
| 739 | seh_x64_write_prologue_data (c); |
| 740 | |
| 741 | /* We need to align prologue data. */ |
| 742 | if (count_unwind_codes & 1) |
| 743 | out_two (0); |
| 744 | |
| 745 | if (flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER)) |
| 746 | { |
| 747 | /* Force the use of segment-relative relocations instead of absolute |
| 748 | valued expressions. Don't adjust for constants (e.g. NULL). */ |
| 749 | if (c->handler.X_op == O_symbol) |
| 750 | c->handler.X_op = O_symbol_rva; |
| 751 | emit_expr (&c->handler, 4); |
| 752 | } |
| 753 | |
| 754 | /* Handler data will be tacked in here by subsections. */ |
| 755 | } |
| 756 | |
| 757 | /* Write out xdata for one function. */ |
| 758 | |
| 759 | static void |
| 760 | write_function_xdata (seh_context *c) |
| 761 | { |
| 762 | segT save_seg = now_seg; |
| 763 | int save_subseg = now_subseg; |
| 764 | |
| 765 | /* MIPS, SH, ARM don't have xdata. */ |
| 766 | if (seh_get_target_kind () != seh_kind_x64) |
| 767 | return; |
| 768 | |
| 769 | switch_xdata (c->subsection); |
| 770 | |
| 771 | seh_x64_write_function_xdata (c); |
| 772 | |
| 773 | subseg_set (save_seg, save_subseg); |
| 774 | } |
| 775 | |
| 776 | /* Write pdata section data for one function (arm). */ |
| 777 | |
| 778 | static void |
| 779 | seh_arm_write_function_pdata (seh_context *c) |
| 780 | { |
| 781 | expressionS exp; |
| 782 | unsigned int prol_len = 0, func_len = 0; |
| 783 | unsigned int val; |
| 784 | |
| 785 | /* Start address of the function. */ |
| 786 | exp.X_op = O_symbol; |
| 787 | exp.X_add_symbol = c->start_addr; |
| 788 | exp.X_add_number = 0; |
| 789 | emit_expr (&exp, 4); |
| 790 | |
| 791 | exp.X_op = O_subtract; |
| 792 | exp.X_add_symbol = c->end_addr; |
| 793 | exp.X_op_symbol = c->start_addr; |
| 794 | exp.X_add_number = 0; |
| 795 | if (resolve_expression (&exp) && exp.X_op == O_constant) |
| 796 | func_len = exp.X_add_number; |
| 797 | else |
| 798 | as_bad (_(".seh_endproc in a different section from .seh_proc")); |
| 799 | |
| 800 | if (c->endprologue_addr) |
| 801 | { |
| 802 | exp.X_op = O_subtract; |
| 803 | exp.X_add_symbol = c->endprologue_addr; |
| 804 | exp.X_op_symbol = c->start_addr; |
| 805 | exp.X_add_number = 0; |
| 806 | |
| 807 | if (resolve_expression (&exp) && exp.X_op == O_constant) |
| 808 | prol_len = exp.X_add_number; |
| 809 | else |
| 810 | as_bad (_(".seh_endprologue in a different section from .seh_proc")); |
| 811 | } |
| 812 | |
| 813 | /* Both function and prologue are in units of instructions. */ |
| 814 | func_len >>= (c->use_instruction_32 ? 2 : 1); |
| 815 | prol_len >>= (c->use_instruction_32 ? 2 : 1); |
| 816 | |
| 817 | /* Assemble the second word of the pdata. */ |
| 818 | val = prol_len & 0xff; |
| 819 | val |= (func_len & 0x3fffff) << 8; |
| 820 | if (c->use_instruction_32) |
| 821 | val |= 0x40000000U; |
| 822 | if (c->handler_written) |
| 823 | val |= 0x80000000U; |
| 824 | out_four (val); |
| 825 | } |
| 826 | |
| 827 | /* Write out pdata for one function. */ |
| 828 | |
| 829 | static void |
| 830 | write_function_pdata (seh_context *c) |
| 831 | { |
| 832 | expressionS exp; |
| 833 | segT save_seg = now_seg; |
| 834 | int save_subseg = now_subseg; |
| 835 | |
| 836 | switch_pdata (); |
| 837 | |
| 838 | switch (seh_get_target_kind ()) |
| 839 | { |
| 840 | case seh_kind_x64: |
| 841 | exp.X_op = O_symbol_rva; |
| 842 | exp.X_add_number = 0; |
| 843 | |
| 844 | exp.X_add_symbol = c->start_addr; |
| 845 | emit_expr (&exp, 4); |
| 846 | exp.X_add_symbol = c->end_addr; |
| 847 | emit_expr (&exp, 4); |
| 848 | exp.X_add_symbol = c->xdata_addr; |
| 849 | emit_expr (&exp, 4); |
| 850 | break; |
| 851 | |
| 852 | case seh_kind_mips: |
| 853 | exp.X_op = O_symbol; |
| 854 | exp.X_add_number = 0; |
| 855 | |
| 856 | exp.X_add_symbol = c->start_addr; |
| 857 | emit_expr (&exp, 4); |
| 858 | exp.X_add_symbol = c->end_addr; |
| 859 | emit_expr (&exp, 4); |
| 860 | |
| 861 | emit_expr (&c->handler, 4); |
| 862 | emit_expr (&c->handler_data, 4); |
| 863 | |
| 864 | exp.X_add_symbol = (c->endprologue_addr |
| 865 | ? c->endprologue_addr |
| 866 | : c->start_addr); |
| 867 | emit_expr (&exp, 4); |
| 868 | break; |
| 869 | |
| 870 | case seh_kind_arm: |
| 871 | seh_arm_write_function_pdata (c); |
| 872 | break; |
| 873 | |
| 874 | default: |
| 875 | abort (); |
| 876 | } |
| 877 | |
| 878 | subseg_set (save_seg, save_subseg); |
| 879 | } |