1 /* seh pdata/xdata coff object file format
3 Free Software Foundation, Inc.
5 This file is part of GAS.
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)
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.
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
22 #include "obj-coff-seh.h"
26 static seh_context
*seh_ctx_cur
= NULL
;
28 static segT xdata_seg
;
29 static segT pdata_seg
;
30 static int xdata_subseg
;
32 static void write_function_xdata (seh_context
*);
33 static void write_function_pdata (seh_context
*);
37 switch_xdata (int subseg
)
39 if (xdata_seg
== NULL
)
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
)));
46 subseg_set (xdata_seg
, subseg
);
52 if (pdata_seg
== NULL
)
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
)));
60 subseg_set (pdata_seg
, 0);
63 /* Parsing routines. */
65 /* Return the style of SEH unwind info to generate. */
68 seh_get_target_kind (void)
71 return seh_kind_unknown
;
72 switch (bfd_get_arch (stdoutput
))
75 case bfd_arch_powerpc
:
79 switch (bfd_get_mach (stdoutput
))
82 case bfd_mach_x86_64_intel_syntax
:
91 /* Should return seh_kind_x64. But not implemented yet. */
92 return seh_kind_unknown
;
96 return seh_kind_unknown
;
99 /* Verify that we're in the context of a seh_proc. */
102 verify_context (const char *directive
)
104 if (seh_ctx_cur
== NULL
)
106 as_bad (_("%s used outside of .seh_proc block"), directive
);
107 ignore_rest_of_line ();
113 /* Similar, except we also verify the appropriate target. */
116 verify_context_and_target (const char *directive
, seh_kind target
)
118 if (seh_get_target_kind () != target
)
120 as_warn (_("%s ignored for this target"), directive
);
121 ignore_rest_of_line ();
124 return verify_context (directive
);
127 /* Skip whitespace and a comma. Error if the comma is not seen. */
130 skip_whitespace_and_comma (int required
)
133 if (*input_line_pointer
== ',')
135 input_line_pointer
++;
141 as_bad (_("missing separator"));
142 ignore_rest_of_line ();
145 demand_empty_rest_of_line ();
149 /* Mark current context to use 32-bit instruction (arm). */
152 obj_coff_seh_32 (int what
)
154 if (!verify_context_and_target ((what
? ".seh_32" : ".seh_no32"),
158 seh_ctx_cur
->use_instruction_32
= (what
? 1 : 0);
159 demand_empty_rest_of_line ();
162 /* Set for current context the handler and optional data (arm). */
165 obj_coff_seh_eh (int what ATTRIBUTE_UNUSED
)
167 if (!verify_context_and_target (".seh_eh", seh_kind_arm
))
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);
175 demand_empty_rest_of_line ();
178 /* Set for current context the default handler (x64). */
181 obj_coff_seh_handler (int what ATTRIBUTE_UNUSED
)
186 if (!verify_context (".seh_handler"))
189 if (*input_line_pointer
== 0 || *input_line_pointer
== '\n')
191 as_bad (_(".seh_handler requires a handler"));
192 demand_empty_rest_of_line ();
198 if (*input_line_pointer
== '@')
200 symbol_name
= input_line_pointer
;
201 name_end
= get_symbol_end ();
203 seh_ctx_cur
->handler
.X_op
= O_constant
;
204 seh_ctx_cur
->handler
.X_add_number
= 0;
206 if (strcasecmp (symbol_name
, "@0") == 0
207 || strcasecmp (symbol_name
, "@null") == 0)
209 else if (strcasecmp (symbol_name
, "@1") == 0)
210 seh_ctx_cur
->handler
.X_add_number
= 1;
212 as_bad (_("unknown constant value '%s' for handler"), symbol_name
);
214 *input_line_pointer
= name_end
;
217 expression (&seh_ctx_cur
->handler
);
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;
223 if (!skip_whitespace_and_comma (0))
226 if (seh_get_target_kind () == seh_kind_x64
)
230 symbol_name
= input_line_pointer
;
231 name_end
= get_symbol_end ();
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
;
238 as_bad (_(".seh_handler constant '%s' unknown"), symbol_name
);
240 *input_line_pointer
= name_end
;
242 while (skip_whitespace_and_comma (0));
246 expression (&seh_ctx_cur
->handler_data
);
247 demand_empty_rest_of_line ();
249 if (seh_ctx_cur
->handler_written
)
250 as_warn (_(".seh_handler after .seh_eh is ignored"));
254 /* Switch to subsection for handler data for exception region (x64). */
257 obj_coff_seh_handlerdata (int what ATTRIBUTE_UNUSED
)
259 if (!verify_context_and_target (".seh_handlerdata", seh_kind_x64
))
261 demand_empty_rest_of_line ();
263 switch_xdata (seh_ctx_cur
->subsection
+ 1);
266 /* Mark end of current context. */
269 do_seh_endproc (void)
271 seh_ctx_cur
->end_addr
= symbol_temp_new_now ();
273 write_function_xdata (seh_ctx_cur
);
274 write_function_pdata (seh_ctx_cur
);
279 obj_coff_seh_endproc (int what ATTRIBUTE_UNUSED
)
281 demand_empty_rest_of_line ();
282 if (seh_ctx_cur
== NULL
)
284 as_bad (_(".seh_endproc used without .seh_proc"));
291 /* Mark begin of new context. */
294 obj_coff_seh_proc (int what ATTRIBUTE_UNUSED
)
299 if (seh_ctx_cur
!= NULL
)
301 as_bad (_("previous SEH entry not closed (missing .seh_endproc)"));
305 if (*input_line_pointer
== 0 || *input_line_pointer
== '\n')
307 as_bad (_(".seh_proc requires function label name"));
308 demand_empty_rest_of_line ();
312 seh_ctx_cur
= XCNEW (seh_context
);
314 if (seh_get_target_kind () == seh_kind_x64
)
316 seh_ctx_cur
->subsection
= xdata_subseg
;
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
;
327 demand_empty_rest_of_line ();
329 seh_ctx_cur
->start_addr
= symbol_temp_new_now ();
332 /* Mark end of prologue for current context. */
335 obj_coff_seh_endprologue (int what ATTRIBUTE_UNUSED
)
337 if (!verify_context (".seh_endprologue"))
339 demand_empty_rest_of_line ();
341 if (seh_ctx_cur
->endprologue_addr
!= NULL
)
342 as_warn (_("duplicate .seh_endprologue in .seh_proc block"));
344 seh_ctx_cur
->endprologue_addr
= symbol_temp_new_now ();
347 /* End-of-file hook. */
350 obj_coff_seh_do_final (void)
352 if (seh_ctx_cur
!= NULL
)
354 as_bad (_("open SEH entry at end of file (missing .cfi_endproc)"));
359 /* Enter a prologue element into current context (x64). */
362 seh_x64_make_prologue_element (int code
, int info
, offsetT off
)
364 seh_prologue_element
*n
;
366 if (seh_ctx_cur
== NULL
)
368 if (seh_ctx_cur
->elems_count
== seh_ctx_cur
->elems_max
)
370 seh_ctx_cur
->elems_max
+= 8;
371 seh_ctx_cur
->elems
= XRESIZEVEC (seh_prologue_element
,
373 seh_ctx_cur
->elems_max
);
376 n
= &seh_ctx_cur
->elems
[seh_ctx_cur
->elems_count
++];
380 n
->pc_addr
= symbol_temp_new_now ();
383 /* Helper to read a register name from input stream (x64). */
386 seh_x64_read_reg (const char *directive
, int kind
)
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" };
395 const char * const *regs
= NULL
;
397 char *symbol_name
= NULL
;
414 if (*input_line_pointer
== '%')
415 ++input_line_pointer
;
416 symbol_name
= input_line_pointer
;
417 name_end
= get_symbol_end ();
419 for (i
= 0; i
< 16; i
++)
420 if (! strcasecmp (regs
[i
], symbol_name
))
423 *input_line_pointer
= name_end
;
425 /* Error if register not found, or EAX used as a frame pointer. */
426 if (i
== 16 || (kind
== 0 && i
== 0))
428 as_bad (_("invalid register for %s"), directive
);
435 /* Add a register push-unwind token to the current context. */
438 obj_coff_seh_pushreg (int what ATTRIBUTE_UNUSED
)
442 if (!verify_context_and_target (".seh_pushreg", seh_kind_x64
))
445 reg
= seh_x64_read_reg (".seh_pushreg", 1);
446 demand_empty_rest_of_line ();
451 seh_x64_make_prologue_element (UWOP_PUSH_NONVOL
, reg
, 0);
454 /* Add a register frame-unwind token to the current context. */
457 obj_coff_seh_pushframe (int what ATTRIBUTE_UNUSED
)
459 if (!verify_context_and_target (".seh_pushframe", seh_kind_x64
))
461 demand_empty_rest_of_line ();
463 seh_x64_make_prologue_element (UWOP_PUSH_MACHFRAME
, 0, 0);
466 /* Add a register save-unwind token to current context. */
469 obj_coff_seh_save (int what
)
471 const char *directive
= (what
== 1 ? ".seh_savereg" : ".seh_savexmm");
472 int code
, reg
, scale
;
475 if (!verify_context_and_target (directive
, seh_kind_x64
))
478 reg
= seh_x64_read_reg (directive
, what
);
480 if (!skip_whitespace_and_comma (1))
483 off
= get_absolute_expression ();
484 demand_empty_rest_of_line ();
490 as_bad (_("%s offset is negative"), directive
);
494 scale
= (what
== 1 ? 8 : 16);
496 if ((off
& (scale
- 1)) == 0 && off
<= 0xffff * scale
)
498 code
= (what
== 1 ? UWOP_SAVE_NONVOL
: UWOP_SAVE_XMM128
);
501 else if (off
< 0xffffffff)
502 code
= (what
== 1 ? UWOP_SAVE_NONVOL_FAR
: UWOP_SAVE_XMM128_FAR
);
505 as_bad (_("%s offset out of range"), directive
);
509 seh_x64_make_prologue_element (code
, reg
, off
);
512 /* Add a stack-allocation token to current context. */
515 obj_coff_seh_stackalloc (int what ATTRIBUTE_UNUSED
)
520 if (!verify_context_and_target (".seh_stackalloc", seh_kind_x64
))
523 off
= get_absolute_expression ();
524 demand_empty_rest_of_line ();
530 as_bad (_(".seh_stackalloc offset is negative"));
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;
542 as_bad (_(".seh_stackalloc offset out of range"));
546 seh_x64_make_prologue_element (code
, info
, off
);
549 /* Add a frame-pointer token to current context. */
552 obj_coff_seh_setframe (int what ATTRIBUTE_UNUSED
)
557 if (!verify_context_and_target (".seh_setframe", seh_kind_x64
))
560 reg
= seh_x64_read_reg (".seh_setframe", 0);
562 if (!skip_whitespace_and_comma (1))
565 off
= get_absolute_expression ();
566 demand_empty_rest_of_line ();
571 as_bad (_(".seh_setframe offset is negative"));
573 as_bad (_(".seh_setframe offset out of range"));
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"));
580 seh_ctx_cur
->framereg
= reg
;
581 seh_ctx_cur
->frameoff
= off
;
582 seh_x64_make_prologue_element (UWOP_SET_FPREG
, 0, 0);
586 /* Data writing routines. */
588 /* Output raw integers in 1, 2, or 4 bytes. */
593 FRAG_APPEND_1_CHAR (byte
);
599 md_number_to_chars (frag_more (2), data
, 2);
605 md_number_to_chars (frag_more (4), data
, 4);
608 /* Write out prologue data for x64. */
611 seh_x64_write_prologue_data (const seh_context
*c
)
615 /* We have to store in reverse order. */
616 for (i
= c
->elems_count
- 1; i
>= 0; --i
)
618 const seh_prologue_element
*e
= c
->elems
+ i
;
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;
628 /* Second comes code+info packed into a byte. */
629 out_one ((e
->info
<< 4) | e
->code
);
633 case UWOP_PUSH_NONVOL
:
634 case UWOP_ALLOC_SMALL
:
636 case UWOP_PUSH_MACHFRAME
:
637 /* These have no extra data. */
640 case UWOP_ALLOC_LARGE
:
643 case UWOP_SAVE_NONVOL_FAR
:
644 case UWOP_SAVE_XMM128_FAR
:
645 /* An unscaled 4 byte offset. */
651 case UWOP_SAVE_NONVOL
:
652 case UWOP_SAVE_XMM128
:
653 /* A scaled 2 byte offset. */
664 seh_x64_size_prologue_data (const seh_context
*c
)
668 for (i
= c
->elems_count
- 1; i
>= 0; --i
)
669 switch (c
->elems
[i
].code
)
671 case UWOP_PUSH_NONVOL
:
672 case UWOP_ALLOC_SMALL
:
674 case UWOP_PUSH_MACHFRAME
:
678 case UWOP_SAVE_NONVOL
:
679 case UWOP_SAVE_XMM128
:
683 case UWOP_SAVE_NONVOL_FAR
:
684 case UWOP_SAVE_XMM128_FAR
:
688 case UWOP_ALLOC_LARGE
:
689 ret
+= (c
->elems
[i
].info
? 3 : 2);
699 /* Write out the xdata information for one function (x64). */
702 seh_x64_write_function_xdata (seh_context
*c
)
704 int flags
, count_unwind_codes
;
707 /* Set 4-byte alignment. */
708 frag_align (2, 0, 0);
710 c
->xdata_addr
= symbol_temp_new_now ();
711 flags
= c
->handler_flags
;
712 count_unwind_codes
= seh_x64_size_prologue_data (c
);
714 /* ubyte:3 version, ubyte:5 flags. */
715 out_one ((flags
<< 3) | 1);
717 /* Size of prologue. */
718 if (c
->endprologue_addr
)
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;
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
);
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
);
739 seh_x64_write_prologue_data (c
);
741 /* We need to align prologue data. */
742 if (count_unwind_codes
& 1)
745 if (flags
& (UNW_FLAG_EHANDLER
| UNW_FLAG_UHANDLER
))
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);
754 /* Handler data will be tacked in here by subsections. */
757 /* Write out xdata for one function. */
760 write_function_xdata (seh_context
*c
)
762 segT save_seg
= now_seg
;
763 int save_subseg
= now_subseg
;
765 /* MIPS, SH, ARM don't have xdata. */
766 if (seh_get_target_kind () != seh_kind_x64
)
769 switch_xdata (c
->subsection
);
771 seh_x64_write_function_xdata (c
);
773 subseg_set (save_seg
, save_subseg
);
776 /* Write pdata section data for one function (arm). */
779 seh_arm_write_function_pdata (seh_context
*c
)
782 unsigned int prol_len
= 0, func_len
= 0;
785 /* Start address of the function. */
787 exp
.X_add_symbol
= c
->start_addr
;
788 exp
.X_add_number
= 0;
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
;
798 as_bad (_(".seh_endproc in a different section from .seh_proc"));
800 if (c
->endprologue_addr
)
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;
807 if (resolve_expression (&exp
) && exp
.X_op
== O_constant
)
808 prol_len
= exp
.X_add_number
;
810 as_bad (_(".seh_endprologue in a different section from .seh_proc"));
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);
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
)
822 if (c
->handler_written
)
827 /* Write out pdata for one function. */
830 write_function_pdata (seh_context
*c
)
833 segT save_seg
= now_seg
;
834 int save_subseg
= now_subseg
;
838 switch (seh_get_target_kind ())
841 exp
.X_op
= O_symbol_rva
;
842 exp
.X_add_number
= 0;
844 exp
.X_add_symbol
= c
->start_addr
;
846 exp
.X_add_symbol
= c
->end_addr
;
848 exp
.X_add_symbol
= c
->xdata_addr
;
854 exp
.X_add_number
= 0;
856 exp
.X_add_symbol
= c
->start_addr
;
858 exp
.X_add_symbol
= c
->end_addr
;
861 emit_expr (&c
->handler
, 4);
862 emit_expr (&c
->handler_data
, 4);
864 exp
.X_add_symbol
= (c
->endprologue_addr
865 ? c
->endprologue_addr
871 seh_arm_write_function_pdata (c
);
878 subseg_set (save_seg
, save_subseg
);