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"
25 /* Private segment collection list. */
33 static seh_context
*seh_ctx_cur
= NULL
;
35 static struct hash_control
*seh_hash
;
37 static struct seh_seg_list
*x_segcur
= NULL
;
38 static struct seh_seg_list
*p_segcur
= NULL
;
40 static void write_function_xdata (seh_context
*);
41 static void write_function_pdata (seh_context
*);
44 /* Build based on segment the derived .pdata/.xdata
45 segment name containing origin segment's postfix name part. */
47 get_pxdata_name (segT seg
, const char *base_name
)
49 const char *name
,*dollar
, *dot
;
52 name
= bfd_get_section_name (stdoutput
, seg
);
54 dollar
= strchr (name
, '$');
55 dot
= strchr (name
+ 1, '.');
63 else if (dot
< dollar
)
68 sname
= concat (base_name
, name
, NULL
);
73 /* Allocate a seh_seg_list structure. */
74 static struct seh_seg_list
*
75 alloc_pxdata_item (segT seg
, int subseg
, char *name
)
77 struct seh_seg_list
*r
;
79 r
= (struct seh_seg_list
*)
80 xmalloc (sizeof (struct seh_seg_list
) + strlen (name
));
87 /* Generate pdata/xdata segment with same linkonce properties
90 make_pxdata_seg (segT cseg
, char *name
)
92 segT save_seg
= now_seg
;
93 int save_subseg
= now_subseg
;
97 r
= subseg_new (name
, 0);
98 /* Check if code segment is marked as linked once. */
99 flags
= bfd_get_section_flags (stdoutput
, cseg
)
100 & (SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
101 | SEC_LINK_DUPLICATES_ONE_ONLY
| SEC_LINK_DUPLICATES_SAME_SIZE
102 | SEC_LINK_DUPLICATES_SAME_CONTENTS
);
104 /* Add standard section flags. */
105 flags
|= SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_DATA
;
107 /* Apply possibly linked once flags to new generated segment, too. */
108 if (!bfd_set_section_flags (stdoutput
, r
, flags
))
109 as_bad (_("bfd_set_section_flags: %s"),
110 bfd_errmsg (bfd_get_error ()));
112 /* Restore to previous segment. */
113 subseg_set (save_seg
, save_subseg
);
118 seh_hash_insert (const char *name
, struct seh_seg_list
*item
)
120 const char *error_string
;
122 if ((error_string
= hash_jam (seh_hash
, name
, (char *) item
)))
123 as_fatal (_("Inserting \"%s\" into structure table failed: %s"),
127 static struct seh_seg_list
*
128 seh_hash_find (char *name
)
130 return (struct seh_seg_list
*) hash_find (seh_hash
, name
);
133 static struct seh_seg_list
*
134 seh_hash_find_or_make (segT cseg
, const char *base_name
)
136 struct seh_seg_list
*item
;
139 /* Initialize seh_hash once. */
141 seh_hash
= hash_new ();
143 name
= get_pxdata_name (cseg
, base_name
);
145 item
= seh_hash_find (name
);
148 item
= alloc_pxdata_item (make_pxdata_seg (cseg
, name
), 0, name
);
150 seh_hash_insert (item
->seg_name
, item
);
159 switch_xdata (int subseg
, segT code_seg
)
161 x_segcur
= seh_hash_find_or_make (code_seg
, ".xdata");
163 subseg_set (x_segcur
->seg
, subseg
);
167 switch_pdata (segT code_seg
)
169 p_segcur
= seh_hash_find_or_make (code_seg
, ".pdata");
171 subseg_set (p_segcur
->seg
, p_segcur
->subseg
);
174 /* Parsing routines. */
176 /* Return the style of SEH unwind info to generate. */
179 seh_get_target_kind (void)
182 return seh_kind_unknown
;
183 switch (bfd_get_arch (stdoutput
))
186 case bfd_arch_powerpc
:
190 switch (bfd_get_mach (stdoutput
))
192 case bfd_mach_x86_64
:
193 case bfd_mach_x86_64_intel_syntax
:
200 return seh_kind_mips
;
202 /* Should return seh_kind_x64. But not implemented yet. */
203 return seh_kind_unknown
;
207 return seh_kind_unknown
;
210 /* Verify that we're in the context of a seh_proc. */
213 verify_context (const char *directive
)
215 if (seh_ctx_cur
== NULL
)
217 as_bad (_("%s used outside of .seh_proc block"), directive
);
218 ignore_rest_of_line ();
224 /* Similar, except we also verify the appropriate target. */
227 verify_context_and_target (const char *directive
, seh_kind target
)
229 if (seh_get_target_kind () != target
)
231 as_warn (_("%s ignored for this target"), directive
);
232 ignore_rest_of_line ();
235 return verify_context (directive
);
238 /* Skip whitespace and a comma. Error if the comma is not seen. */
241 skip_whitespace_and_comma (int required
)
244 if (*input_line_pointer
== ',')
246 input_line_pointer
++;
252 as_bad (_("missing separator"));
253 ignore_rest_of_line ();
256 demand_empty_rest_of_line ();
260 /* Mark current context to use 32-bit instruction (arm). */
263 obj_coff_seh_32 (int what
)
265 if (!verify_context_and_target ((what
? ".seh_32" : ".seh_no32"),
269 seh_ctx_cur
->use_instruction_32
= (what
? 1 : 0);
270 demand_empty_rest_of_line ();
273 /* Set for current context the handler and optional data (arm). */
276 obj_coff_seh_eh (int what ATTRIBUTE_UNUSED
)
278 if (!verify_context_and_target (".seh_eh", seh_kind_arm
))
281 /* Write block to .text if exception handler is set. */
282 seh_ctx_cur
->handler_written
= 1;
283 emit_expr (&seh_ctx_cur
->handler
, 4);
284 emit_expr (&seh_ctx_cur
->handler_data
, 4);
286 demand_empty_rest_of_line ();
289 /* Set for current context the default handler (x64). */
292 obj_coff_seh_handler (int what ATTRIBUTE_UNUSED
)
297 if (!verify_context (".seh_handler"))
300 if (*input_line_pointer
== 0 || *input_line_pointer
== '\n')
302 as_bad (_(".seh_handler requires a handler"));
303 demand_empty_rest_of_line ();
309 if (*input_line_pointer
== '@')
311 symbol_name
= input_line_pointer
;
312 name_end
= get_symbol_end ();
314 seh_ctx_cur
->handler
.X_op
= O_constant
;
315 seh_ctx_cur
->handler
.X_add_number
= 0;
317 if (strcasecmp (symbol_name
, "@0") == 0
318 || strcasecmp (symbol_name
, "@null") == 0)
320 else if (strcasecmp (symbol_name
, "@1") == 0)
321 seh_ctx_cur
->handler
.X_add_number
= 1;
323 as_bad (_("unknown constant value '%s' for handler"), symbol_name
);
325 *input_line_pointer
= name_end
;
328 expression (&seh_ctx_cur
->handler
);
330 seh_ctx_cur
->handler_data
.X_op
= O_constant
;
331 seh_ctx_cur
->handler_data
.X_add_number
= 0;
332 seh_ctx_cur
->handler_flags
= 0;
334 if (!skip_whitespace_and_comma (0))
337 if (seh_get_target_kind () == seh_kind_x64
)
341 symbol_name
= input_line_pointer
;
342 name_end
= get_symbol_end ();
344 if (strcasecmp (symbol_name
, "@unwind") == 0)
345 seh_ctx_cur
->handler_flags
|= UNW_FLAG_UHANDLER
;
346 else if (strcasecmp (symbol_name
, "@except") == 0)
347 seh_ctx_cur
->handler_flags
|= UNW_FLAG_EHANDLER
;
349 as_bad (_(".seh_handler constant '%s' unknown"), symbol_name
);
351 *input_line_pointer
= name_end
;
353 while (skip_whitespace_and_comma (0));
357 expression (&seh_ctx_cur
->handler_data
);
358 demand_empty_rest_of_line ();
360 if (seh_ctx_cur
->handler_written
)
361 as_warn (_(".seh_handler after .seh_eh is ignored"));
365 /* Switch to subsection for handler data for exception region (x64). */
368 obj_coff_seh_handlerdata (int what ATTRIBUTE_UNUSED
)
370 if (!verify_context_and_target (".seh_handlerdata", seh_kind_x64
))
372 demand_empty_rest_of_line ();
374 switch_xdata (seh_ctx_cur
->subsection
+ 1, seh_ctx_cur
->code_seg
);
377 /* Mark end of current context. */
380 do_seh_endproc (void)
382 seh_ctx_cur
->end_addr
= symbol_temp_new_now ();
384 write_function_xdata (seh_ctx_cur
);
385 write_function_pdata (seh_ctx_cur
);
390 obj_coff_seh_endproc (int what ATTRIBUTE_UNUSED
)
392 demand_empty_rest_of_line ();
393 if (seh_ctx_cur
== NULL
)
395 as_bad (_(".seh_endproc used without .seh_proc"));
402 /* Mark begin of new context. */
405 obj_coff_seh_proc (int what ATTRIBUTE_UNUSED
)
410 if (seh_ctx_cur
!= NULL
)
412 as_bad (_("previous SEH entry not closed (missing .seh_endproc)"));
416 if (*input_line_pointer
== 0 || *input_line_pointer
== '\n')
418 as_bad (_(".seh_proc requires function label name"));
419 demand_empty_rest_of_line ();
423 seh_ctx_cur
= XCNEW (seh_context
);
425 seh_ctx_cur
->code_seg
= now_seg
;
427 if (seh_get_target_kind () == seh_kind_x64
)
429 x_segcur
= seh_hash_find_or_make (seh_ctx_cur
->code_seg
, ".xdata");
430 seh_ctx_cur
->subsection
= x_segcur
->subseg
;
431 x_segcur
->subseg
+= 2;
436 symbol_name
= input_line_pointer
;
437 name_end
= get_symbol_end ();
438 seh_ctx_cur
->func_name
= xstrdup (symbol_name
);
439 *input_line_pointer
= name_end
;
441 demand_empty_rest_of_line ();
443 seh_ctx_cur
->start_addr
= symbol_temp_new_now ();
446 /* Mark end of prologue for current context. */
449 obj_coff_seh_endprologue (int what ATTRIBUTE_UNUSED
)
451 if (!verify_context (".seh_endprologue"))
453 demand_empty_rest_of_line ();
455 if (seh_ctx_cur
->endprologue_addr
!= NULL
)
456 as_warn (_("duplicate .seh_endprologue in .seh_proc block"));
458 seh_ctx_cur
->endprologue_addr
= symbol_temp_new_now ();
461 /* End-of-file hook. */
464 obj_coff_seh_do_final (void)
466 if (seh_ctx_cur
!= NULL
)
468 as_bad (_("open SEH entry at end of file (missing .cfi_endproc)"));
473 /* Enter a prologue element into current context (x64). */
476 seh_x64_make_prologue_element (int code
, int info
, offsetT off
)
478 seh_prologue_element
*n
;
480 if (seh_ctx_cur
== NULL
)
482 if (seh_ctx_cur
->elems_count
== seh_ctx_cur
->elems_max
)
484 seh_ctx_cur
->elems_max
+= 8;
485 seh_ctx_cur
->elems
= XRESIZEVEC (seh_prologue_element
,
487 seh_ctx_cur
->elems_max
);
490 n
= &seh_ctx_cur
->elems
[seh_ctx_cur
->elems_count
++];
494 n
->pc_addr
= symbol_temp_new_now ();
497 /* Helper to read a register name from input stream (x64). */
500 seh_x64_read_reg (const char *directive
, int kind
)
502 static const char * const int_regs
[16] =
503 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp","rsi","rdi",
504 "r8","r9","r10","r11","r12","r13","r14","r15" };
505 static const char * const xmm_regs
[16] =
506 { "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
507 "xmm8", "xmm9", "xmm10","xmm11","xmm12","xmm13","xmm14","xmm15" };
509 const char * const *regs
= NULL
;
511 char *symbol_name
= NULL
;
528 if (*input_line_pointer
== '%')
529 ++input_line_pointer
;
530 symbol_name
= input_line_pointer
;
531 name_end
= get_symbol_end ();
533 for (i
= 0; i
< 16; i
++)
534 if (! strcasecmp (regs
[i
], symbol_name
))
537 *input_line_pointer
= name_end
;
539 /* Error if register not found, or EAX used as a frame pointer. */
540 if (i
== 16 || (kind
== 0 && i
== 0))
542 as_bad (_("invalid register for %s"), directive
);
549 /* Add a register push-unwind token to the current context. */
552 obj_coff_seh_pushreg (int what ATTRIBUTE_UNUSED
)
556 if (!verify_context_and_target (".seh_pushreg", seh_kind_x64
))
559 reg
= seh_x64_read_reg (".seh_pushreg", 1);
560 demand_empty_rest_of_line ();
565 seh_x64_make_prologue_element (UWOP_PUSH_NONVOL
, reg
, 0);
568 /* Add a register frame-unwind token to the current context. */
571 obj_coff_seh_pushframe (int what ATTRIBUTE_UNUSED
)
573 if (!verify_context_and_target (".seh_pushframe", seh_kind_x64
))
575 demand_empty_rest_of_line ();
577 seh_x64_make_prologue_element (UWOP_PUSH_MACHFRAME
, 0, 0);
580 /* Add a register save-unwind token to current context. */
583 obj_coff_seh_save (int what
)
585 const char *directive
= (what
== 1 ? ".seh_savereg" : ".seh_savexmm");
586 int code
, reg
, scale
;
589 if (!verify_context_and_target (directive
, seh_kind_x64
))
592 reg
= seh_x64_read_reg (directive
, what
);
594 if (!skip_whitespace_and_comma (1))
597 off
= get_absolute_expression ();
598 demand_empty_rest_of_line ();
604 as_bad (_("%s offset is negative"), directive
);
608 scale
= (what
== 1 ? 8 : 16);
610 if ((off
& (scale
- 1)) == 0 && off
<= (offsetT
) (0xffff * scale
))
612 code
= (what
== 1 ? UWOP_SAVE_NONVOL
: UWOP_SAVE_XMM128
);
615 else if (off
< (offsetT
) 0xffffffff)
616 code
= (what
== 1 ? UWOP_SAVE_NONVOL_FAR
: UWOP_SAVE_XMM128_FAR
);
619 as_bad (_("%s offset out of range"), directive
);
623 seh_x64_make_prologue_element (code
, reg
, off
);
626 /* Add a stack-allocation token to current context. */
629 obj_coff_seh_stackalloc (int what ATTRIBUTE_UNUSED
)
634 if (!verify_context_and_target (".seh_stackalloc", seh_kind_x64
))
637 off
= get_absolute_expression ();
638 demand_empty_rest_of_line ();
644 as_bad (_(".seh_stackalloc offset is negative"));
648 if ((off
& 7) == 0 && off
<= 128)
649 code
= UWOP_ALLOC_SMALL
, info
= (off
- 8) >> 3, off
= 0;
650 else if ((off
& 7) == 0 && off
<= (offsetT
) (0xffff * 8))
651 code
= UWOP_ALLOC_LARGE
, info
= 0, off
>>= 3;
652 else if (off
<= (offsetT
) 0xffffffff)
653 code
= UWOP_ALLOC_LARGE
, info
= 1;
656 as_bad (_(".seh_stackalloc offset out of range"));
660 seh_x64_make_prologue_element (code
, info
, off
);
663 /* Add a frame-pointer token to current context. */
666 obj_coff_seh_setframe (int what ATTRIBUTE_UNUSED
)
671 if (!verify_context_and_target (".seh_setframe", seh_kind_x64
))
674 reg
= seh_x64_read_reg (".seh_setframe", 0);
676 if (!skip_whitespace_and_comma (1))
679 off
= get_absolute_expression ();
680 demand_empty_rest_of_line ();
685 as_bad (_(".seh_setframe offset is negative"));
687 as_bad (_(".seh_setframe offset out of range"));
689 as_bad (_(".seh_setframe offset not a multiple of 16"));
690 else if (seh_ctx_cur
->framereg
!= 0)
691 as_bad (_("duplicate .seh_setframe in current .seh_proc"));
694 seh_ctx_cur
->framereg
= reg
;
695 seh_ctx_cur
->frameoff
= off
;
696 seh_x64_make_prologue_element (UWOP_SET_FPREG
, 0, 0);
700 /* Data writing routines. */
702 /* Output raw integers in 1, 2, or 4 bytes. */
707 FRAG_APPEND_1_CHAR (byte
);
713 md_number_to_chars (frag_more (2), data
, 2);
719 md_number_to_chars (frag_more (4), data
, 4);
722 /* Write out prologue data for x64. */
725 seh_x64_write_prologue_data (const seh_context
*c
)
729 /* We have to store in reverse order. */
730 for (i
= c
->elems_count
- 1; i
>= 0; --i
)
732 const seh_prologue_element
*e
= c
->elems
+ i
;
735 /* First comes byte offset in code. */
736 exp
.X_op
= O_subtract
;
737 exp
.X_add_symbol
= e
->pc_addr
;
738 exp
.X_op_symbol
= c
->start_addr
;
739 exp
.X_add_number
= 0;
742 /* Second comes code+info packed into a byte. */
743 out_one ((e
->info
<< 4) | e
->code
);
747 case UWOP_PUSH_NONVOL
:
748 case UWOP_ALLOC_SMALL
:
750 case UWOP_PUSH_MACHFRAME
:
751 /* These have no extra data. */
754 case UWOP_ALLOC_LARGE
:
757 case UWOP_SAVE_NONVOL_FAR
:
758 case UWOP_SAVE_XMM128_FAR
:
759 /* An unscaled 4 byte offset. */
765 case UWOP_SAVE_NONVOL
:
766 case UWOP_SAVE_XMM128
:
767 /* A scaled 2 byte offset. */
778 seh_x64_size_prologue_data (const seh_context
*c
)
782 for (i
= c
->elems_count
- 1; i
>= 0; --i
)
783 switch (c
->elems
[i
].code
)
785 case UWOP_PUSH_NONVOL
:
786 case UWOP_ALLOC_SMALL
:
788 case UWOP_PUSH_MACHFRAME
:
792 case UWOP_SAVE_NONVOL
:
793 case UWOP_SAVE_XMM128
:
797 case UWOP_SAVE_NONVOL_FAR
:
798 case UWOP_SAVE_XMM128_FAR
:
802 case UWOP_ALLOC_LARGE
:
803 ret
+= (c
->elems
[i
].info
? 3 : 2);
813 /* Write out the xdata information for one function (x64). */
816 seh_x64_write_function_xdata (seh_context
*c
)
818 int flags
, count_unwind_codes
;
821 /* Set 4-byte alignment. */
822 frag_align (2, 0, 0);
824 c
->xdata_addr
= symbol_temp_new_now ();
825 flags
= c
->handler_flags
;
826 count_unwind_codes
= seh_x64_size_prologue_data (c
);
828 /* ubyte:3 version, ubyte:5 flags. */
829 out_one ((flags
<< 3) | 1);
831 /* Size of prologue. */
832 if (c
->endprologue_addr
)
834 exp
.X_op
= O_subtract
;
835 exp
.X_add_symbol
= c
->endprologue_addr
;
836 exp
.X_op_symbol
= c
->start_addr
;
837 exp
.X_add_number
= 0;
843 /* Number of slots (i.e. shorts) in the unwind codes array. */
844 if (count_unwind_codes
> 255)
845 as_fatal (_("too much unwind data in this .seh_proc"));
846 out_one (count_unwind_codes
);
848 /* ubyte:4 frame-reg, ubyte:4 frame-reg-offset. */
849 /* Note that frameoff is already a multiple of 16, and therefore
850 the offset is already both scaled and shifted into place. */
851 out_one (c
->frameoff
| c
->framereg
);
853 seh_x64_write_prologue_data (c
);
855 /* We need to align prologue data. */
856 if (count_unwind_codes
& 1)
859 if (flags
& (UNW_FLAG_EHANDLER
| UNW_FLAG_UHANDLER
))
861 /* Force the use of segment-relative relocations instead of absolute
862 valued expressions. Don't adjust for constants (e.g. NULL). */
863 if (c
->handler
.X_op
== O_symbol
)
864 c
->handler
.X_op
= O_symbol_rva
;
865 emit_expr (&c
->handler
, 4);
868 /* Handler data will be tacked in here by subsections. */
871 /* Write out xdata for one function. */
874 write_function_xdata (seh_context
*c
)
876 segT save_seg
= now_seg
;
877 int save_subseg
= now_subseg
;
879 /* MIPS, SH, ARM don't have xdata. */
880 if (seh_get_target_kind () != seh_kind_x64
)
883 switch_xdata (c
->subsection
, c
->code_seg
);
885 seh_x64_write_function_xdata (c
);
887 subseg_set (save_seg
, save_subseg
);
890 /* Write pdata section data for one function (arm). */
893 seh_arm_write_function_pdata (seh_context
*c
)
896 unsigned int prol_len
= 0, func_len
= 0;
899 /* Start address of the function. */
901 exp
.X_add_symbol
= c
->start_addr
;
902 exp
.X_add_number
= 0;
905 exp
.X_op
= O_subtract
;
906 exp
.X_add_symbol
= c
->end_addr
;
907 exp
.X_op_symbol
= c
->start_addr
;
908 exp
.X_add_number
= 0;
909 if (resolve_expression (&exp
) && exp
.X_op
== O_constant
)
910 func_len
= exp
.X_add_number
;
912 as_bad (_(".seh_endproc in a different section from .seh_proc"));
914 if (c
->endprologue_addr
)
916 exp
.X_op
= O_subtract
;
917 exp
.X_add_symbol
= c
->endprologue_addr
;
918 exp
.X_op_symbol
= c
->start_addr
;
919 exp
.X_add_number
= 0;
921 if (resolve_expression (&exp
) && exp
.X_op
== O_constant
)
922 prol_len
= exp
.X_add_number
;
924 as_bad (_(".seh_endprologue in a different section from .seh_proc"));
927 /* Both function and prologue are in units of instructions. */
928 func_len
>>= (c
->use_instruction_32
? 2 : 1);
929 prol_len
>>= (c
->use_instruction_32
? 2 : 1);
931 /* Assemble the second word of the pdata. */
932 val
= prol_len
& 0xff;
933 val
|= (func_len
& 0x3fffff) << 8;
934 if (c
->use_instruction_32
)
936 if (c
->handler_written
)
941 /* Write out pdata for one function. */
944 write_function_pdata (seh_context
*c
)
947 segT save_seg
= now_seg
;
948 int save_subseg
= now_subseg
;
949 memset (&exp
, 0, sizeof (expressionS
));
950 switch_pdata (c
->code_seg
);
952 switch (seh_get_target_kind ())
955 exp
.X_op
= O_symbol_rva
;
956 exp
.X_add_number
= 0;
958 exp
.X_add_symbol
= c
->start_addr
;
960 exp
.X_op
= O_symbol_rva
;
961 exp
.X_add_number
= 0;
962 exp
.X_add_symbol
= c
->end_addr
;
964 exp
.X_op
= O_symbol_rva
;
965 exp
.X_add_number
= 0;
966 exp
.X_add_symbol
= c
->xdata_addr
;
972 exp
.X_add_number
= 0;
974 exp
.X_add_symbol
= c
->start_addr
;
976 exp
.X_add_symbol
= c
->end_addr
;
979 emit_expr (&c
->handler
, 4);
980 emit_expr (&c
->handler_data
, 4);
982 exp
.X_add_symbol
= (c
->endprologue_addr
983 ? c
->endprologue_addr
989 seh_arm_write_function_pdata (c
);
996 subseg_set (save_seg
, save_subseg
);