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