gdb/dap: allow some requests when the process is running
[binutils-gdb.git] / gas / config / obj-coff-seh.c
blob13c4439db930f86af8e5c175f3beca1f95dbed41
1 /* seh pdata/xdata coff object file format
2 Copyright (C) 2009-2024 Free Software Foundation, Inc.
4 This file is part of GAS.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
21 #include "obj-coff-seh.h"
24 /* Private segment collection list. */
25 struct seh_seg_list {
26 segT seg;
27 int subseg;
28 char *seg_name;
31 /* Local data. */
32 static seh_context *seh_ctx_cur = NULL;
34 static htab_t seh_hash;
36 static struct seh_seg_list *x_segcur = NULL;
37 static struct seh_seg_list *p_segcur = NULL;
39 static void write_function_xdata (seh_context *);
40 static void write_function_pdata (seh_context *);
43 /* Build based on segment the derived .pdata/.xdata
44 segment name containing origin segment's postfix name part. */
45 static char *
46 get_pxdata_name (segT seg, const char *base_name)
48 const char *name,*dollar, *dot;
49 char *sname;
51 name = bfd_section_name (seg);
53 dollar = strchr (name, '$');
54 dot = strchr (name + 1, '.');
56 if (!dollar && !dot)
57 name = "";
58 else if (!dollar)
59 name = dot;
60 else if (!dot)
61 name = dollar;
62 else if (dot < dollar)
63 name = dot;
64 else
65 name = dollar;
67 sname = notes_concat (base_name, name, NULL);
69 return sname;
72 /* Allocate a seh_seg_list structure. */
73 static struct seh_seg_list *
74 alloc_pxdata_item (segT seg, int subseg, char *name)
76 struct seh_seg_list *r;
78 r = notes_alloc (sizeof (struct seh_seg_list) + strlen (name));
79 r->seg = seg;
80 r->subseg = subseg;
81 r->seg_name = name;
82 return r;
85 /* Generate pdata/xdata segment with same linkonce properties
86 of based segment. */
87 static segT
88 make_pxdata_seg (segT cseg, char *name)
90 segT save_seg = now_seg;
91 int save_subseg = now_subseg;
92 segT r;
93 flagword flags;
95 r = subseg_new (name, 0);
96 /* Check if code segment is marked as linked once. */
97 flags = (bfd_section_flags (cseg)
98 & (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
99 | SEC_LINK_DUPLICATES_ONE_ONLY | SEC_LINK_DUPLICATES_SAME_SIZE
100 | SEC_LINK_DUPLICATES_SAME_CONTENTS));
102 /* Add standard section flags. */
103 flags |= SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA;
105 /* Apply possibly linked once flags to new generated segment, too. */
106 if (!bfd_set_section_flags (r, flags))
107 as_bad (_("bfd_set_section_flags: %s"),
108 bfd_errmsg (bfd_get_error ()));
110 /* Restore to previous segment. */
111 subseg_set (save_seg, save_subseg);
112 return r;
115 static void
116 seh_hash_insert (const char *name, struct seh_seg_list *item)
118 str_hash_insert (seh_hash, name, item, 1);
121 static struct seh_seg_list *
122 seh_hash_find (char *name)
124 return (struct seh_seg_list *) str_hash_find (seh_hash, name);
127 static struct seh_seg_list *
128 seh_hash_find_or_make (segT cseg, const char *base_name)
130 struct seh_seg_list *item;
131 char *name;
133 /* Initialize seh_hash once. */
134 if (!seh_hash)
135 seh_hash = str_htab_create ();
137 name = get_pxdata_name (cseg, base_name);
139 item = seh_hash_find (name);
140 if (!item)
142 item = alloc_pxdata_item (make_pxdata_seg (cseg, name), 0, name);
144 seh_hash_insert (item->seg_name, item);
146 else
147 notes_free (name);
149 return item;
152 /* Check if current segment has same name. */
153 static int
154 seh_validate_seg (const char *directive)
156 const char *cseg_name, *nseg_name;
157 if (seh_ctx_cur->code_seg == now_seg)
158 return 1;
159 cseg_name = bfd_section_name (seh_ctx_cur->code_seg);
160 nseg_name = bfd_section_name (now_seg);
161 as_bad (_("%s used in segment '%s' instead of expected '%s'"),
162 directive, nseg_name, cseg_name);
163 ignore_rest_of_line ();
164 return 0;
167 /* Switch back to the code section, whatever that may be. */
168 static void
169 obj_coff_seh_code (int ignored ATTRIBUTE_UNUSED)
171 subseg_set (seh_ctx_cur->code_seg, 0);
174 static void
175 switch_xdata (int subseg, segT code_seg)
177 x_segcur = seh_hash_find_or_make (code_seg, ".xdata");
179 subseg_set (x_segcur->seg, subseg);
182 static void
183 switch_pdata (segT code_seg)
185 p_segcur = seh_hash_find_or_make (code_seg, ".pdata");
187 subseg_set (p_segcur->seg, p_segcur->subseg);
190 /* Parsing routines. */
192 /* Return the style of SEH unwind info to generate. */
194 static seh_kind
195 seh_get_target_kind (void)
197 if (!stdoutput)
198 return seh_kind_unknown;
200 switch (bfd_get_arch (stdoutput))
202 case bfd_arch_aarch64:
203 case bfd_arch_arm:
204 case bfd_arch_powerpc:
205 case bfd_arch_sh:
206 return seh_kind_arm;
208 case bfd_arch_i386:
209 switch (bfd_get_mach (stdoutput))
211 case bfd_mach_x86_64:
212 case bfd_mach_x86_64_intel_syntax:
213 return seh_kind_x64;
214 default:
215 break;
217 /* FALL THROUGH. */
218 case bfd_arch_mips:
219 return seh_kind_mips;
221 case bfd_arch_ia64:
222 /* Should return seh_kind_x64. But not implemented yet. */
223 return seh_kind_unknown;
225 default:
226 break;
228 return seh_kind_unknown;
231 /* Verify that seh directives are supported. */
233 static bool
234 verify_target (const char *directive)
236 if (seh_get_target_kind () == seh_kind_unknown)
238 as_warn (_("%s ignored for this target"), directive);
239 ignore_rest_of_line ();
240 return false;
242 return true;
245 /* Verify that we're in the context of a seh_proc. */
247 static int
248 verify_context (const char *directive)
250 if (seh_ctx_cur == NULL)
252 as_bad (_("%s used outside of .seh_proc block"), directive);
253 ignore_rest_of_line ();
254 return 0;
256 return 1;
259 /* Similar, except we also verify the appropriate target. */
261 static int
262 verify_context_and_target (const char *directive, seh_kind target)
264 if (seh_get_target_kind () != target)
266 as_warn (_("%s ignored for this target"), directive);
267 ignore_rest_of_line ();
268 return 0;
270 return verify_context (directive);
273 /* Skip whitespace and a comma. Error if the comma is not seen. */
275 static int
276 skip_whitespace_and_comma (int required)
278 SKIP_WHITESPACE ();
279 if (*input_line_pointer == ',')
281 input_line_pointer++;
282 SKIP_WHITESPACE ();
283 return 1;
285 else if (required)
287 as_bad (_("missing separator"));
288 ignore_rest_of_line ();
290 else
291 demand_empty_rest_of_line ();
292 return 0;
295 /* Mark current context to use 32-bit instruction (arm). */
297 static void
298 obj_coff_seh_32 (int what)
300 if (!verify_context_and_target ((what ? ".seh_32" : ".seh_no32"),
301 seh_kind_arm))
302 return;
304 seh_ctx_cur->use_instruction_32 = (what ? 1 : 0);
305 demand_empty_rest_of_line ();
308 /* Set for current context the handler and optional data (arm). */
310 static void
311 obj_coff_seh_eh (int what ATTRIBUTE_UNUSED)
313 if (!verify_context_and_target (".seh_eh", seh_kind_arm))
314 return;
316 /* Write block to .text if exception handler is set. */
317 seh_ctx_cur->handler_written = 1;
318 emit_expr (&seh_ctx_cur->handler, 4);
319 emit_expr (&seh_ctx_cur->handler_data, 4);
321 demand_empty_rest_of_line ();
324 /* Set for current context the default handler (x64). */
326 static void
327 obj_coff_seh_handler (int what ATTRIBUTE_UNUSED)
329 char *symbol_name;
330 char name_end;
332 if (!verify_target (".seh_handler")
333 || !verify_context (".seh_handler"))
334 return;
336 if (*input_line_pointer == 0 || *input_line_pointer == '\n')
338 as_bad (_(".seh_handler requires a handler"));
339 demand_empty_rest_of_line ();
340 return;
343 SKIP_WHITESPACE ();
345 if (*input_line_pointer == '@')
347 name_end = get_symbol_name (&symbol_name);
349 seh_ctx_cur->handler.X_op = O_constant;
350 seh_ctx_cur->handler.X_add_number = 0;
352 if (strcasecmp (symbol_name, "@0") == 0
353 || strcasecmp (symbol_name, "@null") == 0)
355 else if (strcasecmp (symbol_name, "@1") == 0)
356 seh_ctx_cur->handler.X_add_number = 1;
357 else
358 as_bad (_("unknown constant value '%s' for handler"), symbol_name);
360 (void) restore_line_pointer (name_end);
362 else
363 expression (&seh_ctx_cur->handler);
365 seh_ctx_cur->handler_data.X_op = O_constant;
366 seh_ctx_cur->handler_data.X_add_number = 0;
367 seh_ctx_cur->handler_flags = 0;
369 if (!skip_whitespace_and_comma (0))
370 return;
372 if (seh_get_target_kind () == seh_kind_x64)
376 name_end = get_symbol_name (&symbol_name);
378 if (strcasecmp (symbol_name, "@unwind") == 0)
379 seh_ctx_cur->handler_flags |= UNW_FLAG_UHANDLER;
380 else if (strcasecmp (symbol_name, "@except") == 0)
381 seh_ctx_cur->handler_flags |= UNW_FLAG_EHANDLER;
382 else
383 as_bad (_(".seh_handler constant '%s' unknown"), symbol_name);
385 (void) restore_line_pointer (name_end);
387 while (skip_whitespace_and_comma (0));
389 else
391 expression (&seh_ctx_cur->handler_data);
392 demand_empty_rest_of_line ();
394 if (seh_ctx_cur->handler_written)
395 as_warn (_(".seh_handler after .seh_eh is ignored"));
399 /* Switch to subsection for handler data for exception region (x64). */
401 static void
402 obj_coff_seh_handlerdata (int what ATTRIBUTE_UNUSED)
404 if (!verify_context_and_target (".seh_handlerdata", seh_kind_x64))
405 return;
406 demand_empty_rest_of_line ();
408 switch_xdata (seh_ctx_cur->subsection + 1, seh_ctx_cur->code_seg);
411 /* Mark end of current context. */
413 static void
414 do_seh_endproc (void)
416 seh_ctx_cur->end_addr = symbol_temp_new_now ();
418 write_function_xdata (seh_ctx_cur);
419 write_function_pdata (seh_ctx_cur);
420 seh_ctx_cur = NULL;
423 static void
424 obj_coff_seh_endproc (int what ATTRIBUTE_UNUSED)
426 if (!verify_target (".seh_endproc"))
427 return;
428 demand_empty_rest_of_line ();
429 if (seh_ctx_cur == NULL)
431 as_bad (_(".seh_endproc used without .seh_proc"));
432 return;
434 seh_validate_seg (".seh_endproc");
435 do_seh_endproc ();
438 /* Mark begin of new context. */
440 static void
441 obj_coff_seh_proc (int what ATTRIBUTE_UNUSED)
443 char *symbol_name;
444 char name_end;
446 if (!verify_target (".seh_proc"))
447 return;
448 if (seh_ctx_cur != NULL)
450 as_bad (_("previous SEH entry not closed (missing .seh_endproc)"));
451 do_seh_endproc ();
454 if (*input_line_pointer == 0 || *input_line_pointer == '\n')
456 as_bad (_(".seh_proc requires function label name"));
457 demand_empty_rest_of_line ();
458 return;
461 seh_ctx_cur = XCNEW (seh_context);
463 seh_ctx_cur->code_seg = now_seg;
465 if (seh_get_target_kind () == seh_kind_x64)
467 x_segcur = seh_hash_find_or_make (seh_ctx_cur->code_seg, ".xdata");
468 seh_ctx_cur->subsection = x_segcur->subseg;
469 x_segcur->subseg += 2;
472 SKIP_WHITESPACE ();
474 name_end = get_symbol_name (&symbol_name);
475 seh_ctx_cur->func_name = xstrdup (symbol_name);
476 (void) restore_line_pointer (name_end);
478 demand_empty_rest_of_line ();
480 seh_ctx_cur->start_addr = symbol_temp_new_now ();
483 /* Mark end of prologue for current context. */
485 static void
486 obj_coff_seh_endprologue (int what ATTRIBUTE_UNUSED)
488 if (!verify_target (".seh_endprologue")
489 || !verify_context (".seh_endprologue")
490 || !seh_validate_seg (".seh_endprologue"))
491 return;
492 demand_empty_rest_of_line ();
494 if (seh_ctx_cur->endprologue_addr != NULL)
495 as_warn (_("duplicate .seh_endprologue in .seh_proc block"));
496 else
497 seh_ctx_cur->endprologue_addr = symbol_temp_new_now ();
500 /* End-of-file hook. */
502 void
503 obj_coff_seh_do_final (void)
505 if (seh_ctx_cur != NULL)
506 as_bad (_("open SEH entry at end of file (missing .seh_endproc)"));
509 /* Enter a prologue element into current context (x64). */
511 static void
512 seh_x64_make_prologue_element (int code, int info, offsetT off)
514 seh_prologue_element *n;
516 if (seh_ctx_cur == NULL)
517 return;
518 if (seh_ctx_cur->elems_count == seh_ctx_cur->elems_max)
520 seh_ctx_cur->elems_max += 8;
521 seh_ctx_cur->elems = XRESIZEVEC (seh_prologue_element,
522 seh_ctx_cur->elems,
523 seh_ctx_cur->elems_max);
526 n = &seh_ctx_cur->elems[seh_ctx_cur->elems_count++];
527 n->code = code;
528 n->info = info;
529 n->off = off;
530 n->pc_addr = symbol_temp_new_now ();
533 /* Helper to read a register name from input stream (x64). */
535 static int
536 seh_x64_read_reg (const char *directive, int kind)
538 static const char * const int_regs[16] =
539 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp","rsi","rdi",
540 "r8","r9","r10","r11","r12","r13","r14","r15" };
541 static const char * const xmm_regs[16] =
542 { "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
543 "xmm8", "xmm9", "xmm10","xmm11","xmm12","xmm13","xmm14","xmm15" };
545 const char * const *regs = NULL;
546 char name_end;
547 char *symbol_name = NULL;
548 int i;
550 switch (kind)
552 case 0:
553 case 1:
554 regs = int_regs;
555 break;
556 case 2:
557 regs = xmm_regs;
558 break;
559 default:
560 abort ();
563 SKIP_WHITESPACE ();
564 if (*input_line_pointer == '%')
565 ++input_line_pointer;
566 name_end = get_symbol_name (& symbol_name);
568 for (i = 0; i < 16; i++)
569 if (! strcasecmp (regs[i], symbol_name))
570 break;
572 (void) restore_line_pointer (name_end);
574 /* Error if register not found, or EAX used as a frame pointer. */
575 if (i == 16 || (kind == 0 && i == 0))
577 as_bad (_("invalid register for %s"), directive);
578 return -1;
581 return i;
584 /* Add a register push-unwind token to the current context. */
586 static void
587 obj_coff_seh_pushreg (int what ATTRIBUTE_UNUSED)
589 int reg;
591 if (!verify_context_and_target (".seh_pushreg", seh_kind_x64)
592 || !seh_validate_seg (".seh_pushreg"))
593 return;
595 reg = seh_x64_read_reg (".seh_pushreg", 1);
596 demand_empty_rest_of_line ();
598 if (reg < 0)
599 return;
601 seh_x64_make_prologue_element (UWOP_PUSH_NONVOL, reg, 0);
604 /* Add a register frame-unwind token to the current context. */
606 static void
607 obj_coff_seh_pushframe (int what ATTRIBUTE_UNUSED)
609 int code = 0;
611 if (!verify_context_and_target (".seh_pushframe", seh_kind_x64)
612 || !seh_validate_seg (".seh_pushframe"))
613 return;
615 SKIP_WHITESPACE();
617 if (is_name_beginner (*input_line_pointer))
619 char* identifier;
621 get_symbol_name (&identifier);
622 if (strcmp (identifier, "code") != 0)
624 as_bad(_("invalid argument \"%s\" for .seh_pushframe. Expected \"code\" or nothing"),
625 identifier);
626 return;
628 code = 1;
631 demand_empty_rest_of_line ();
633 seh_x64_make_prologue_element (UWOP_PUSH_MACHFRAME, code, 0);
636 /* Add a register save-unwind token to current context. */
638 static void
639 obj_coff_seh_save (int what)
641 const char *directive = (what == 1 ? ".seh_savereg" : ".seh_savexmm");
642 int code, reg, scale;
643 offsetT off;
645 if (!verify_context_and_target (directive, seh_kind_x64)
646 || !seh_validate_seg (directive))
647 return;
649 reg = seh_x64_read_reg (directive, what);
651 if (!skip_whitespace_and_comma (1))
652 return;
654 off = get_absolute_expression ();
655 demand_empty_rest_of_line ();
657 if (reg < 0)
658 return;
659 if (off < 0)
661 as_bad (_("%s offset is negative"), directive);
662 return;
665 scale = (what == 1 ? 8 : 16);
667 if ((off & (scale - 1)) == 0 && off <= (offsetT) (0xffff * scale))
669 code = (what == 1 ? UWOP_SAVE_NONVOL : UWOP_SAVE_XMM128);
670 off /= scale;
672 else if (off < (offsetT) 0xffffffff)
673 code = (what == 1 ? UWOP_SAVE_NONVOL_FAR : UWOP_SAVE_XMM128_FAR);
674 else
676 as_bad (_("%s offset out of range"), directive);
677 return;
680 seh_x64_make_prologue_element (code, reg, off);
683 /* Add a stack-allocation token to current context. */
685 static void
686 obj_coff_seh_stackalloc (int what ATTRIBUTE_UNUSED)
688 offsetT off;
689 int code, info;
691 if (!verify_context_and_target (".seh_stackalloc", seh_kind_x64)
692 || !seh_validate_seg (".seh_stackalloc"))
693 return;
695 off = get_absolute_expression ();
696 demand_empty_rest_of_line ();
698 if (off == 0)
699 return;
700 if (off < 0)
702 as_bad (_(".seh_stackalloc offset is negative"));
703 return;
706 if ((off & 7) == 0 && off <= 128)
707 code = UWOP_ALLOC_SMALL, info = (off - 8) >> 3, off = 0;
708 else if ((off & 7) == 0 && off <= (offsetT) (0xffff * 8))
709 code = UWOP_ALLOC_LARGE, info = 0, off >>= 3;
710 else if (off <= (offsetT) 0xffffffff)
711 code = UWOP_ALLOC_LARGE, info = 1;
712 else
714 as_bad (_(".seh_stackalloc offset out of range"));
715 return;
718 seh_x64_make_prologue_element (code, info, off);
721 /* Add a frame-pointer token to current context. */
723 static void
724 obj_coff_seh_setframe (int what ATTRIBUTE_UNUSED)
726 offsetT off;
727 int reg;
729 if (!verify_context_and_target (".seh_setframe", seh_kind_x64)
730 || !seh_validate_seg (".seh_setframe"))
731 return;
733 reg = seh_x64_read_reg (".seh_setframe", 0);
735 if (!skip_whitespace_and_comma (1))
736 return;
738 off = get_absolute_expression ();
739 demand_empty_rest_of_line ();
741 if (reg < 0)
742 return;
743 if (off < 0)
744 as_bad (_(".seh_setframe offset is negative"));
745 else if (off > 240)
746 as_bad (_(".seh_setframe offset out of range"));
747 else if (off & 15)
748 as_bad (_(".seh_setframe offset not a multiple of 16"));
749 else if (seh_ctx_cur->framereg != 0)
750 as_bad (_("duplicate .seh_setframe in current .seh_proc"));
751 else
753 seh_ctx_cur->framereg = reg;
754 seh_ctx_cur->frameoff = off;
755 seh_x64_make_prologue_element (UWOP_SET_FPREG, 0, 0);
759 /* Data writing routines. */
761 /* Output raw integers in 1, 2, or 4 bytes. */
763 static inline void
764 out_one (int byte)
766 FRAG_APPEND_1_CHAR (byte);
769 static inline void
770 out_two (int data)
772 md_number_to_chars (frag_more (2), data, 2);
775 static inline void
776 out_four (int data)
778 md_number_to_chars (frag_more (4), data, 4);
781 /* Write out prologue data for x64. */
783 static void
784 seh_x64_write_prologue_data (const seh_context *c)
786 int i;
788 /* We have to store in reverse order. */
789 for (i = c->elems_count - 1; i >= 0; --i)
791 const seh_prologue_element *e = c->elems + i;
792 expressionS exp;
794 /* First comes byte offset in code. */
795 exp.X_op = O_subtract;
796 exp.X_add_symbol = e->pc_addr;
797 exp.X_op_symbol = c->start_addr;
798 exp.X_add_number = 0;
799 emit_expr (&exp, 1);
801 /* Second comes code+info packed into a byte. */
802 out_one ((e->info << 4) | e->code);
804 switch (e->code)
806 case UWOP_PUSH_NONVOL:
807 case UWOP_ALLOC_SMALL:
808 case UWOP_SET_FPREG:
809 case UWOP_PUSH_MACHFRAME:
810 /* These have no extra data. */
811 break;
813 case UWOP_ALLOC_LARGE:
814 if (e->info)
816 case UWOP_SAVE_NONVOL_FAR:
817 case UWOP_SAVE_XMM128_FAR:
818 /* An unscaled 4 byte offset. */
819 out_four (e->off);
820 break;
822 /* FALLTHRU */
824 case UWOP_SAVE_NONVOL:
825 case UWOP_SAVE_XMM128:
826 /* A scaled 2 byte offset. */
827 out_two (e->off);
828 break;
830 default:
831 abort ();
836 static int
837 seh_x64_size_prologue_data (const seh_context *c)
839 int i, ret = 0;
841 for (i = c->elems_count - 1; i >= 0; --i)
842 switch (c->elems[i].code)
844 case UWOP_PUSH_NONVOL:
845 case UWOP_ALLOC_SMALL:
846 case UWOP_SET_FPREG:
847 case UWOP_PUSH_MACHFRAME:
848 ret += 1;
849 break;
851 case UWOP_SAVE_NONVOL:
852 case UWOP_SAVE_XMM128:
853 ret += 2;
854 break;
856 case UWOP_SAVE_NONVOL_FAR:
857 case UWOP_SAVE_XMM128_FAR:
858 ret += 3;
859 break;
861 case UWOP_ALLOC_LARGE:
862 ret += (c->elems[i].info ? 3 : 2);
863 break;
865 default:
866 abort ();
869 return ret;
872 /* Write out the xdata information for one function (x64). */
874 static void
875 seh_x64_write_function_xdata (seh_context *c)
877 int flags, count_unwind_codes;
878 expressionS exp;
880 /* Set 4-byte alignment. */
881 frag_align (2, 0, 0);
883 c->xdata_addr = symbol_temp_new_now ();
884 flags = c->handler_flags;
885 count_unwind_codes = seh_x64_size_prologue_data (c);
887 /* ubyte:3 version, ubyte:5 flags. */
888 out_one ((flags << 3) | 1);
890 /* Size of prologue. */
891 if (c->endprologue_addr)
893 exp.X_op = O_subtract;
894 exp.X_add_symbol = c->endprologue_addr;
895 exp.X_op_symbol = c->start_addr;
896 exp.X_add_number = 0;
897 emit_expr (&exp, 1);
899 else
900 out_one (0);
902 /* Number of slots (i.e. shorts) in the unwind codes array. */
903 if (count_unwind_codes > 255)
904 as_fatal (_("too much unwind data in this .seh_proc"));
905 out_one (count_unwind_codes);
907 /* ubyte:4 frame-reg, ubyte:4 frame-reg-offset. */
908 /* Note that frameoff is already a multiple of 16, and therefore
909 the offset is already both scaled and shifted into place. */
910 out_one (c->frameoff | c->framereg);
912 seh_x64_write_prologue_data (c);
914 /* We need to align prologue data. */
915 if (count_unwind_codes & 1)
916 out_two (0);
918 if (flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
920 /* Force the use of segment-relative relocations instead of absolute
921 valued expressions. Don't adjust for constants (e.g. NULL). */
922 if (c->handler.X_op == O_symbol)
923 c->handler.X_op = O_symbol_rva;
924 emit_expr (&c->handler, 4);
927 /* Handler data will be tacked in here by subsections. */
930 /* Write out xdata for one function. */
932 static void
933 write_function_xdata (seh_context *c)
935 segT save_seg = now_seg;
936 int save_subseg = now_subseg;
938 /* MIPS, SH, ARM don't have xdata. */
939 if (seh_get_target_kind () != seh_kind_x64)
940 return;
942 switch_xdata (c->subsection, c->code_seg);
944 seh_x64_write_function_xdata (c);
946 subseg_set (save_seg, save_subseg);
949 /* Write pdata section data for one function (arm). */
951 static void
952 seh_arm_write_function_pdata (seh_context *c)
954 expressionS exp;
955 unsigned int prol_len = 0, func_len = 0;
956 unsigned int val;
958 /* Start address of the function. */
959 exp.X_op = O_symbol;
960 exp.X_add_symbol = c->start_addr;
961 exp.X_add_number = 0;
962 emit_expr (&exp, 4);
964 exp.X_op = O_subtract;
965 exp.X_add_symbol = c->end_addr;
966 exp.X_op_symbol = c->start_addr;
967 exp.X_add_number = 0;
968 if (resolve_expression (&exp) && exp.X_op == O_constant)
969 func_len = exp.X_add_number;
970 else
971 as_bad (_(".seh_endproc in a different section from .seh_proc"));
973 if (c->endprologue_addr)
975 exp.X_op = O_subtract;
976 exp.X_add_symbol = c->endprologue_addr;
977 exp.X_op_symbol = c->start_addr;
978 exp.X_add_number = 0;
980 if (resolve_expression (&exp) && exp.X_op == O_constant)
981 prol_len = exp.X_add_number;
982 else
983 as_bad (_(".seh_endprologue in a different section from .seh_proc"));
986 /* Both function and prologue are in units of instructions. */
987 func_len >>= (c->use_instruction_32 ? 2 : 1);
988 prol_len >>= (c->use_instruction_32 ? 2 : 1);
990 /* Assemble the second word of the pdata. */
991 val = prol_len & 0xff;
992 val |= (func_len & 0x3fffff) << 8;
993 if (c->use_instruction_32)
994 val |= 0x40000000U;
995 if (c->handler_written)
996 val |= 0x80000000U;
997 out_four (val);
1000 /* Write out pdata for one function. */
1002 static void
1003 write_function_pdata (seh_context *c)
1005 expressionS exp;
1006 segT save_seg = now_seg;
1007 int save_subseg = now_subseg;
1008 memset (&exp, 0, sizeof (expressionS));
1009 switch_pdata (c->code_seg);
1011 switch (seh_get_target_kind ())
1013 case seh_kind_x64:
1014 exp.X_op = O_symbol_rva;
1015 exp.X_add_number = 0;
1017 exp.X_add_symbol = c->start_addr;
1018 emit_expr (&exp, 4);
1019 exp.X_op = O_symbol_rva;
1020 exp.X_add_number = 0;
1021 exp.X_add_symbol = c->end_addr;
1022 emit_expr (&exp, 4);
1023 exp.X_op = O_symbol_rva;
1024 exp.X_add_number = 0;
1025 exp.X_add_symbol = c->xdata_addr;
1026 emit_expr (&exp, 4);
1027 break;
1029 case seh_kind_mips:
1030 exp.X_op = O_symbol;
1031 exp.X_add_number = 0;
1033 exp.X_add_symbol = c->start_addr;
1034 emit_expr (&exp, 4);
1035 exp.X_add_symbol = c->end_addr;
1036 emit_expr (&exp, 4);
1038 emit_expr (&c->handler, 4);
1039 emit_expr (&c->handler_data, 4);
1041 exp.X_add_symbol = (c->endprologue_addr
1042 ? c->endprologue_addr
1043 : c->start_addr);
1044 emit_expr (&exp, 4);
1045 break;
1047 case seh_kind_arm:
1048 seh_arm_write_function_pdata (c);
1049 break;
1051 default:
1052 abort ();
1055 subseg_set (save_seg, save_subseg);