]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/obj-coff-seh.c
gdb: remove unused includes in symfile.c
[thirdparty/binutils-gdb.git] / gas / config / obj-coff-seh.c
CommitLineData
284e0531 1/* seh pdata/xdata coff object file format
fd67aa11 2 Copyright (C) 2009-2024 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
7bfc4db2 67 sname = notes_concat (base_name, name, NULL);
2d7f4929
KT
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
7bfc4db2 78 r = notes_alloc (sizeof (struct seh_seg_list) + strlen (name));
2d7f4929
KT
79 r->seg = seg;
80 r->subseg = subseg;
81 r->seg_name = name;
82 return r;
83}
84
85/* Generate pdata/xdata segment with same linkonce properties
86 of based segment. */
87static segT
88make_pxdata_seg (segT cseg, char *name)
89{
90 segT save_seg = now_seg;
91 int save_subseg = now_subseg;
92 segT r;
93 flagword flags;
94
95 r = subseg_new (name, 0);
96 /* Check if code segment is marked as linked once. */
fd361982
AM
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));
2d7f4929
KT
101
102 /* Add standard section flags. */
103 flags |= SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA;
104
105 /* Apply possibly linked once flags to new generated segment, too. */
fd361982 106 if (!bfd_set_section_flags (r, flags))
2d7f4929
KT
107 as_bad (_("bfd_set_section_flags: %s"),
108 bfd_errmsg (bfd_get_error ()));
109
110 /* Restore to previous segment. */
111 subseg_set (save_seg, save_subseg);
112 return r;
987499b2
KT
113}
114
987499b2 115static void
2d7f4929
KT
116seh_hash_insert (const char *name, struct seh_seg_list *item)
117{
fe0e921f 118 str_hash_insert (seh_hash, name, item, 1);
2d7f4929
KT
119}
120
121static struct seh_seg_list *
122seh_hash_find (char *name)
987499b2 123{
629310ab 124 return (struct seh_seg_list *) str_hash_find (seh_hash, name);
2d7f4929
KT
125}
126
127static struct seh_seg_list *
128seh_hash_find_or_make (segT cseg, const char *base_name)
129{
130 struct seh_seg_list *item;
131 char *name;
132
133 /* Initialize seh_hash once. */
134 if (!seh_hash)
629310ab 135 seh_hash = str_htab_create ();
2d7f4929
KT
136
137 name = get_pxdata_name (cseg, base_name);
138
139 item = seh_hash_find (name);
140 if (!item)
681418c2 141 {
2d7f4929
KT
142 item = alloc_pxdata_item (make_pxdata_seg (cseg, name), 0, name);
143
144 seh_hash_insert (item->seg_name, item);
681418c2
RH
145 }
146 else
7bfc4db2 147 notes_free (name);
2d7f4929
KT
148
149 return item;
150}
151
bea2c1d7
KT
152/* Check if current segment has same name. */
153static int
154seh_validate_seg (const char *directive)
155{
156 const char *cseg_name, *nseg_name;
157 if (seh_ctx_cur->code_seg == now_seg)
158 return 1;
fd361982
AM
159 cseg_name = bfd_section_name (seh_ctx_cur->code_seg);
160 nseg_name = bfd_section_name (now_seg);
bea2c1d7
KT
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;
165}
166
3c6256d2
NC
167/* Switch back to the code section, whatever that may be. */
168static void
169obj_coff_seh_code (int ignored ATTRIBUTE_UNUSED)
170{
171 subseg_set (seh_ctx_cur->code_seg, 0);
172}
173
2d7f4929
KT
174static void
175switch_xdata (int subseg, segT code_seg)
176{
177 x_segcur = seh_hash_find_or_make (code_seg, ".xdata");
178
179 subseg_set (x_segcur->seg, subseg);
180}
181
182static void
183switch_pdata (segT code_seg)
184{
185 p_segcur = seh_hash_find_or_make (code_seg, ".pdata");
186
187 subseg_set (p_segcur->seg, p_segcur->subseg);
681418c2
RH
188}
189\f
190/* Parsing routines. */
987499b2 191
681418c2
RH
192/* Return the style of SEH unwind info to generate. */
193
194static seh_kind
195seh_get_target_kind (void)
196{
197 if (!stdoutput)
198 return seh_kind_unknown;
73c8603c 199
681418c2 200 switch (bfd_get_arch (stdoutput))
987499b2 201 {
73c8603c 202 case bfd_arch_aarch64:
681418c2
RH
203 case bfd_arch_arm:
204 case bfd_arch_powerpc:
205 case bfd_arch_sh:
206 return seh_kind_arm;
73c8603c 207
681418c2
RH
208 case bfd_arch_i386:
209 switch (bfd_get_mach (stdoutput))
987499b2 210 {
681418c2
RH
211 case bfd_mach_x86_64:
212 case bfd_mach_x86_64_intel_syntax:
213 return seh_kind_x64;
214 default:
215 break;
987499b2 216 }
681418c2
RH
217 /* FALL THROUGH. */
218 case bfd_arch_mips:
219 return seh_kind_mips;
73c8603c 220
681418c2
RH
221 case bfd_arch_ia64:
222 /* Should return seh_kind_x64. But not implemented yet. */
223 return seh_kind_unknown;
73c8603c 224
987499b2
KT
225 default:
226 break;
227 }
681418c2 228 return seh_kind_unknown;
987499b2
KT
229}
230
7574c0c2
AM
231/* Verify that seh directives are supported. */
232
233static bool
234verify_target (const char *directive)
235{
236 if (seh_get_target_kind () == seh_kind_unknown)
237 {
238 as_warn (_("%s ignored for this target"), directive);
239 ignore_rest_of_line ();
240 return false;
241 }
242 return true;
243}
244
681418c2
RH
245/* Verify that we're in the context of a seh_proc. */
246
247static int
248verify_context (const char *directive)
987499b2 249{
681418c2 250 if (seh_ctx_cur == NULL)
987499b2 251 {
681418c2
RH
252 as_bad (_("%s used outside of .seh_proc block"), directive);
253 ignore_rest_of_line ();
254 return 0;
987499b2 255 }
681418c2
RH
256 return 1;
257}
987499b2 258
681418c2 259/* Similar, except we also verify the appropriate target. */
987499b2 260
681418c2
RH
261static int
262verify_context_and_target (const char *directive, seh_kind target)
263{
264 if (seh_get_target_kind () != target)
987499b2 265 {
681418c2
RH
266 as_warn (_("%s ignored for this target"), directive);
267 ignore_rest_of_line ();
268 return 0;
269 }
270 return verify_context (directive);
271}
272
273/* Skip whitespace and a comma. Error if the comma is not seen. */
274
275static int
276skip_whitespace_and_comma (int required)
277{
278 SKIP_WHITESPACE ();
279 if (*input_line_pointer == ',')
280 {
281 input_line_pointer++;
282 SKIP_WHITESPACE ();
283 return 1;
987499b2 284 }
681418c2
RH
285 else if (required)
286 {
287 as_bad (_("missing separator"));
288 ignore_rest_of_line ();
289 }
290 else
291 demand_empty_rest_of_line ();
292 return 0;
987499b2
KT
293}
294
681418c2 295/* Mark current context to use 32-bit instruction (arm). */
1e17085d 296
987499b2 297static void
681418c2 298obj_coff_seh_32 (int what)
987499b2 299{
681418c2
RH
300 if (!verify_context_and_target ((what ? ".seh_32" : ".seh_no32"),
301 seh_kind_arm))
302 return;
604ab327 303
681418c2
RH
304 seh_ctx_cur->use_instruction_32 = (what ? 1 : 0);
305 demand_empty_rest_of_line ();
306}
307
308/* Set for current context the handler and optional data (arm). */
309
310static void
311obj_coff_seh_eh (int what ATTRIBUTE_UNUSED)
312{
313 if (!verify_context_and_target (".seh_eh", seh_kind_arm))
987499b2 314 return;
681418c2
RH
315
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);
320
321 demand_empty_rest_of_line ();
987499b2
KT
322}
323
681418c2
RH
324/* Set for current context the default handler (x64). */
325
987499b2 326static void
681418c2 327obj_coff_seh_handler (int what ATTRIBUTE_UNUSED)
987499b2 328{
681418c2
RH
329 char *symbol_name;
330 char name_end;
331
7574c0c2
AM
332 if (!verify_target (".seh_handler")
333 || !verify_context (".seh_handler"))
987499b2 334 return;
681418c2
RH
335
336 if (*input_line_pointer == 0 || *input_line_pointer == '\n')
337 {
338 as_bad (_(".seh_handler requires a handler"));
339 demand_empty_rest_of_line ();
340 return;
341 }
342
343 SKIP_WHITESPACE ();
344
345 if (*input_line_pointer == '@')
987499b2 346 {
d02603dc 347 name_end = get_symbol_name (&symbol_name);
681418c2
RH
348
349 seh_ctx_cur->handler.X_op = O_constant;
350 seh_ctx_cur->handler.X_add_number = 0;
351
352 if (strcasecmp (symbol_name, "@0") == 0
353 || strcasecmp (symbol_name, "@null") == 0)
354 ;
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);
359
d02603dc 360 (void) restore_line_pointer (name_end);
987499b2 361 }
681418c2
RH
362 else
363 expression (&seh_ctx_cur->handler);
987499b2 364
681418c2
RH
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;
368
369 if (!skip_whitespace_and_comma (0))
987499b2
KT
370 return;
371
681418c2 372 if (seh_get_target_kind () == seh_kind_x64)
987499b2 373 {
681418c2 374 do
987499b2 375 {
d02603dc 376 name_end = get_symbol_name (&symbol_name);
681418c2
RH
377
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);
384
d02603dc 385 (void) restore_line_pointer (name_end);
987499b2 386 }
681418c2
RH
387 while (skip_whitespace_and_comma (0));
388 }
389 else
390 {
391 expression (&seh_ctx_cur->handler_data);
392 demand_empty_rest_of_line ();
393
394 if (seh_ctx_cur->handler_written)
395 as_warn (_(".seh_handler after .seh_eh is ignored"));
396 }
397}
398
399/* Switch to subsection for handler data for exception region (x64). */
400
401static void
402obj_coff_seh_handlerdata (int what ATTRIBUTE_UNUSED)
403{
404 if (!verify_context_and_target (".seh_handlerdata", seh_kind_x64))
405 return;
406 demand_empty_rest_of_line ();
407
2d7f4929 408 switch_xdata (seh_ctx_cur->subsection + 1, seh_ctx_cur->code_seg);
681418c2
RH
409}
410
411/* Mark end of current context. */
412
413static void
414do_seh_endproc (void)
415{
416 seh_ctx_cur->end_addr = symbol_temp_new_now ();
417
418 write_function_xdata (seh_ctx_cur);
419 write_function_pdata (seh_ctx_cur);
420 seh_ctx_cur = NULL;
421}
422
423static void
424obj_coff_seh_endproc (int what ATTRIBUTE_UNUSED)
425{
7574c0c2
AM
426 if (!verify_target (".seh_endproc"))
427 return;
681418c2
RH
428 demand_empty_rest_of_line ();
429 if (seh_ctx_cur == NULL)
430 {
431 as_bad (_(".seh_endproc used without .seh_proc"));
432 return;
433 }
bea2c1d7 434 seh_validate_seg (".seh_endproc");
681418c2
RH
435 do_seh_endproc ();
436}
437
438/* Mark begin of new context. */
439
440static void
441obj_coff_seh_proc (int what ATTRIBUTE_UNUSED)
442{
443 char *symbol_name;
444 char name_end;
445
7574c0c2
AM
446 if (!verify_target (".seh_proc"))
447 return;
681418c2
RH
448 if (seh_ctx_cur != NULL)
449 {
450 as_bad (_("previous SEH entry not closed (missing .seh_endproc)"));
451 do_seh_endproc ();
452 }
453
454 if (*input_line_pointer == 0 || *input_line_pointer == '\n')
455 {
456 as_bad (_(".seh_proc requires function label name"));
457 demand_empty_rest_of_line ();
458 return;
459 }
460
461 seh_ctx_cur = XCNEW (seh_context);
462
2d7f4929
KT
463 seh_ctx_cur->code_seg = now_seg;
464
681418c2
RH
465 if (seh_get_target_kind () == seh_kind_x64)
466 {
2d7f4929
KT
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;
987499b2 470 }
681418c2
RH
471
472 SKIP_WHITESPACE ();
473
d02603dc 474 name_end = get_symbol_name (&symbol_name);
681418c2 475 seh_ctx_cur->func_name = xstrdup (symbol_name);
d02603dc 476 (void) restore_line_pointer (name_end);
681418c2
RH
477
478 demand_empty_rest_of_line ();
479
480 seh_ctx_cur->start_addr = symbol_temp_new_now ();
987499b2
KT
481}
482
681418c2
RH
483/* Mark end of prologue for current context. */
484
485static void
486obj_coff_seh_endprologue (int what ATTRIBUTE_UNUSED)
487{
7574c0c2
AM
488 if (!verify_target (".seh_endprologue")
489 || !verify_context (".seh_endprologue")
bea2c1d7 490 || !seh_validate_seg (".seh_endprologue"))
681418c2
RH
491 return;
492 demand_empty_rest_of_line ();
493
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 ();
498}
499
500/* End-of-file hook. */
501
987499b2
KT
502void
503obj_coff_seh_do_final (void)
504{
681418c2 505 if (seh_ctx_cur != NULL)
804a4093 506 as_bad (_("open SEH entry at end of file (missing .seh_endproc)"));
987499b2
KT
507}
508
681418c2
RH
509/* Enter a prologue element into current context (x64). */
510
987499b2 511static void
681418c2 512seh_x64_make_prologue_element (int code, int info, offsetT off)
987499b2
KT
513{
514 seh_prologue_element *n;
604ab327 515
987499b2
KT
516 if (seh_ctx_cur == NULL)
517 return;
518 if (seh_ctx_cur->elems_count == seh_ctx_cur->elems_max)
519 {
987499b2 520 seh_ctx_cur->elems_max += 8;
681418c2
RH
521 seh_ctx_cur->elems = XRESIZEVEC (seh_prologue_element,
522 seh_ctx_cur->elems,
523 seh_ctx_cur->elems_max);
987499b2 524 }
681418c2
RH
525
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 ();
987499b2
KT
531}
532
681418c2
RH
533/* Helper to read a register name from input stream (x64). */
534
987499b2 535static int
681418c2 536seh_x64_read_reg (const char *directive, int kind)
987499b2 537{
681418c2 538 static const char * const int_regs[16] =
604ab327
NC
539 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp","rsi","rdi",
540 "r8","r9","r10","r11","r12","r13","r14","r15" };
681418c2 541 static const char * const xmm_regs[16] =
604ab327
NC
542 { "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
543 "xmm8", "xmm9", "xmm10","xmm11","xmm12","xmm13","xmm14","xmm15" };
681418c2
RH
544
545 const char * const *regs = NULL;
987499b2
KT
546 char name_end;
547 char *symbol_name = NULL;
548 int i;
549
987499b2 550 switch (kind)
604ab327
NC
551 {
552 case 0:
604ab327 553 case 1:
681418c2 554 regs = int_regs;
604ab327
NC
555 break;
556 case 2:
681418c2 557 regs = xmm_regs;
604ab327
NC
558 break;
559 default:
560 abort ();
561 }
562
681418c2 563 SKIP_WHITESPACE ();
987499b2
KT
564 if (*input_line_pointer == '%')
565 ++input_line_pointer;
d02603dc 566 name_end = get_symbol_name (& symbol_name);
604ab327 567
987499b2 568 for (i = 0; i < 16; i++)
681418c2 569 if (! strcasecmp (regs[i], symbol_name))
987499b2 570 break;
604ab327 571
d02603dc 572 (void) restore_line_pointer (name_end);
987499b2 573
681418c2
RH
574 /* Error if register not found, or EAX used as a frame pointer. */
575 if (i == 16 || (kind == 0 && i == 0))
604ab327 576 {
681418c2
RH
577 as_bad (_("invalid register for %s"), directive);
578 return -1;
604ab327 579 }
1e17085d 580
681418c2 581 return i;
987499b2
KT
582}
583
681418c2
RH
584/* Add a register push-unwind token to the current context. */
585
987499b2 586static void
681418c2 587obj_coff_seh_pushreg (int what ATTRIBUTE_UNUSED)
987499b2 588{
681418c2
RH
589 int reg;
590
bea2c1d7
KT
591 if (!verify_context_and_target (".seh_pushreg", seh_kind_x64)
592 || !seh_validate_seg (".seh_pushreg"))
681418c2
RH
593 return;
594
595 reg = seh_x64_read_reg (".seh_pushreg", 1);
987499b2 596 demand_empty_rest_of_line ();
681418c2
RH
597
598 if (reg < 0)
599 return;
600
601 seh_x64_make_prologue_element (UWOP_PUSH_NONVOL, reg, 0);
987499b2
KT
602}
603
681418c2
RH
604/* Add a register frame-unwind token to the current context. */
605
987499b2 606static void
681418c2 607obj_coff_seh_pushframe (int what ATTRIBUTE_UNUSED)
987499b2 608{
d039200a
G
609 int code = 0;
610
bea2c1d7
KT
611 if (!verify_context_and_target (".seh_pushframe", seh_kind_x64)
612 || !seh_validate_seg (".seh_pushframe"))
681418c2 613 return;
d039200a
G
614
615 SKIP_WHITESPACE();
616
617 if (is_name_beginner (*input_line_pointer))
618 {
619 char* identifier;
620
621 get_symbol_name (&identifier);
622 if (strcmp (identifier, "code") != 0)
623 {
624 as_bad(_("invalid argument \"%s\" for .seh_pushframe. Expected \"code\" or nothing"),
625 identifier);
626 return;
627 }
628 code = 1;
629 }
630
987499b2 631 demand_empty_rest_of_line ();
987499b2 632
d039200a 633 seh_x64_make_prologue_element (UWOP_PUSH_MACHFRAME, code, 0);
987499b2
KT
634}
635
681418c2
RH
636/* Add a register save-unwind token to current context. */
637
987499b2 638static void
681418c2 639obj_coff_seh_save (int what)
987499b2 640{
681418c2
RH
641 const char *directive = (what == 1 ? ".seh_savereg" : ".seh_savexmm");
642 int code, reg, scale;
643 offsetT off;
987499b2 644
bea2c1d7
KT
645 if (!verify_context_and_target (directive, seh_kind_x64)
646 || !seh_validate_seg (directive))
681418c2 647 return;
987499b2 648
681418c2 649 reg = seh_x64_read_reg (directive, what);
987499b2 650
681418c2
RH
651 if (!skip_whitespace_and_comma (1))
652 return;
987499b2 653
681418c2 654 off = get_absolute_expression ();
987499b2 655 demand_empty_rest_of_line ();
987499b2 656
681418c2
RH
657 if (reg < 0)
658 return;
659 if (off < 0)
987499b2 660 {
681418c2 661 as_bad (_("%s offset is negative"), directive);
987499b2
KT
662 return;
663 }
664
681418c2 665 scale = (what == 1 ? 8 : 16);
987499b2 666
6e0973c0 667 if ((off & (scale - 1)) == 0 && off <= (offsetT) (0xffff * scale))
987499b2 668 {
681418c2
RH
669 code = (what == 1 ? UWOP_SAVE_NONVOL : UWOP_SAVE_XMM128);
670 off /= scale;
987499b2 671 }
6e0973c0 672 else if (off < (offsetT) 0xffffffff)
681418c2
RH
673 code = (what == 1 ? UWOP_SAVE_NONVOL_FAR : UWOP_SAVE_XMM128_FAR);
674 else
604ab327 675 {
681418c2 676 as_bad (_("%s offset out of range"), directive);
604ab327
NC
677 return;
678 }
681418c2
RH
679
680 seh_x64_make_prologue_element (code, reg, off);
987499b2
KT
681}
682
681418c2
RH
683/* Add a stack-allocation token to current context. */
684
987499b2 685static void
681418c2 686obj_coff_seh_stackalloc (int what ATTRIBUTE_UNUSED)
987499b2 687{
681418c2
RH
688 offsetT off;
689 int code, info;
604ab327 690
bea2c1d7
KT
691 if (!verify_context_and_target (".seh_stackalloc", seh_kind_x64)
692 || !seh_validate_seg (".seh_stackalloc"))
681418c2 693 return;
1e17085d 694
681418c2 695 off = get_absolute_expression ();
987499b2 696 demand_empty_rest_of_line ();
987499b2 697
681418c2
RH
698 if (off == 0)
699 return;
700 if (off < 0)
604ab327 701 {
681418c2 702 as_bad (_(".seh_stackalloc offset is negative"));
604ab327
NC
703 return;
704 }
987499b2 705
681418c2
RH
706 if ((off & 7) == 0 && off <= 128)
707 code = UWOP_ALLOC_SMALL, info = (off - 8) >> 3, off = 0;
6e0973c0 708 else if ((off & 7) == 0 && off <= (offsetT) (0xffff * 8))
681418c2 709 code = UWOP_ALLOC_LARGE, info = 0, off >>= 3;
6e0973c0 710 else if (off <= (offsetT) 0xffffffff)
681418c2 711 code = UWOP_ALLOC_LARGE, info = 1;
987499b2 712 else
604ab327 713 {
681418c2 714 as_bad (_(".seh_stackalloc offset out of range"));
604ab327
NC
715 return;
716 }
681418c2
RH
717
718 seh_x64_make_prologue_element (code, info, off);
987499b2
KT
719}
720
681418c2
RH
721/* Add a frame-pointer token to current context. */
722
987499b2
KT
723static void
724obj_coff_seh_setframe (int what ATTRIBUTE_UNUSED)
725{
681418c2 726 offsetT off;
987499b2 727 int reg;
987499b2 728
bea2c1d7
KT
729 if (!verify_context_and_target (".seh_setframe", seh_kind_x64)
730 || !seh_validate_seg (".seh_setframe"))
681418c2
RH
731 return;
732
733 reg = seh_x64_read_reg (".seh_setframe", 0);
734
735 if (!skip_whitespace_and_comma (1))
736 return;
737
738 off = get_absolute_expression ();
739 demand_empty_rest_of_line ();
740
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
604ab327
NC
752 {
753 seh_ctx_cur->framereg = reg;
754 seh_ctx_cur->frameoff = off;
681418c2 755 seh_x64_make_prologue_element (UWOP_SET_FPREG, 0, 0);
604ab327 756 }
987499b2 757}
681418c2
RH
758\f
759/* Data writing routines. */
987499b2 760
681418c2 761/* Output raw integers in 1, 2, or 4 bytes. */
987499b2 762
681418c2
RH
763static inline void
764out_one (int byte)
987499b2 765{
681418c2 766 FRAG_APPEND_1_CHAR (byte);
987499b2
KT
767}
768
681418c2
RH
769static inline void
770out_two (int data)
987499b2 771{
681418c2 772 md_number_to_chars (frag_more (2), data, 2);
987499b2
KT
773}
774
681418c2
RH
775static inline void
776out_four (int data)
987499b2 777{
681418c2 778 md_number_to_chars (frag_more (4), data, 4);
987499b2
KT
779}
780
681418c2
RH
781/* Write out prologue data for x64. */
782
783static void
784seh_x64_write_prologue_data (const seh_context *c)
987499b2 785{
681418c2 786 int i;
604ab327 787
681418c2
RH
788 /* We have to store in reverse order. */
789 for (i = c->elems_count - 1; i >= 0; --i)
790 {
791 const seh_prologue_element *e = c->elems + i;
792 expressionS exp;
987499b2 793
681418c2
RH
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);
987499b2 800
681418c2
RH
801 /* Second comes code+info packed into a byte. */
802 out_one ((e->info << 4) | e->code);
987499b2 803
681418c2 804 switch (e->code)
987499b2 805 {
681418c2
RH
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. */
987499b2 811 break;
681418c2
RH
812
813 case UWOP_ALLOC_LARGE:
814 if (e->info)
815 {
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;
821 }
822 /* FALLTHRU */
823
824 case UWOP_SAVE_NONVOL:
825 case UWOP_SAVE_XMM128:
826 /* A scaled 2 byte offset. */
827 out_two (e->off);
828 break;
829
830 default:
831 abort ();
987499b2 832 }
987499b2 833 }
987499b2
KT
834}
835
681418c2
RH
836static int
837seh_x64_size_prologue_data (const seh_context *c)
838{
839 int i, ret = 0;
840
841 for (i = c->elems_count - 1; i >= 0; --i)
842 switch (c->elems[i].code)
843 {
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;
987499b2 850
681418c2
RH
851 case UWOP_SAVE_NONVOL:
852 case UWOP_SAVE_XMM128:
853 ret += 2;
854 break;
987499b2 855
681418c2
RH
856 case UWOP_SAVE_NONVOL_FAR:
857 case UWOP_SAVE_XMM128_FAR:
858 ret += 3;
859 break;
987499b2 860
681418c2
RH
861 case UWOP_ALLOC_LARGE:
862 ret += (c->elems[i].info ? 3 : 2);
863 break;
987499b2 864
681418c2
RH
865 default:
866 abort ();
867 }
987499b2 868
681418c2 869 return ret;
987499b2
KT
870}
871
681418c2
RH
872/* Write out the xdata information for one function (x64). */
873
874static void
875seh_x64_write_function_xdata (seh_context *c)
987499b2 876{
681418c2
RH
877 int flags, count_unwind_codes;
878 expressionS exp;
987499b2 879
681418c2
RH
880 /* Set 4-byte alignment. */
881 frag_align (2, 0, 0);
987499b2 882
681418c2
RH
883 c->xdata_addr = symbol_temp_new_now ();
884 flags = c->handler_flags;
885 count_unwind_codes = seh_x64_size_prologue_data (c);
987499b2 886
681418c2
RH
887 /* ubyte:3 version, ubyte:5 flags. */
888 out_one ((flags << 3) | 1);
987499b2 889
681418c2
RH
890 /* Size of prologue. */
891 if (c->endprologue_addr)
892 {
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);
898 }
899 else
900 out_one (0);
987499b2 901
681418c2
RH
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);
987499b2 906
681418c2
RH
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);
604ab327 911
681418c2 912 seh_x64_write_prologue_data (c);
987499b2 913
681418c2
RH
914 /* We need to align prologue data. */
915 if (count_unwind_codes & 1)
916 out_two (0);
917
918 if (flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
604ab327 919 {
681418c2
RH
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);
604ab327 925 }
681418c2
RH
926
927 /* Handler data will be tacked in here by subsections. */
987499b2
KT
928}
929
681418c2 930/* Write out xdata for one function. */
987499b2
KT
931
932static void
681418c2 933write_function_xdata (seh_context *c)
987499b2 934{
681418c2
RH
935 segT save_seg = now_seg;
936 int save_subseg = now_subseg;
937
938 /* MIPS, SH, ARM don't have xdata. */
939 if (seh_get_target_kind () != seh_kind_x64)
987499b2 940 return;
987499b2 941
2d7f4929 942 switch_xdata (c->subsection, c->code_seg);
987499b2 943
681418c2 944 seh_x64_write_function_xdata (c);
1e17085d 945
681418c2 946 subseg_set (save_seg, save_subseg);
1e17085d
KT
947}
948
681418c2 949/* Write pdata section data for one function (arm). */
987499b2 950
681418c2
RH
951static void
952seh_arm_write_function_pdata (seh_context *c)
987499b2 953{
681418c2
RH
954 expressionS exp;
955 unsigned int prol_len = 0, func_len = 0;
956 unsigned int val;
604ab327 957
681418c2
RH
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);
963
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;
987499b2 970 else
681418c2
RH
971 as_bad (_(".seh_endproc in a different section from .seh_proc"));
972
973 if (c->endprologue_addr)
987499b2 974 {
681418c2
RH
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;
979
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"));
987499b2 984 }
681418c2
RH
985
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);
989
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);
987499b2
KT
998}
999
681418c2
RH
1000/* Write out pdata for one function. */
1001
987499b2 1002static void
681418c2 1003write_function_pdata (seh_context *c)
987499b2 1004{
681418c2
RH
1005 expressionS exp;
1006 segT save_seg = now_seg;
1007 int save_subseg = now_subseg;
2d7f4929
KT
1008 memset (&exp, 0, sizeof (expressionS));
1009 switch_pdata (c->code_seg);
681418c2
RH
1010
1011 switch (seh_get_target_kind ())
987499b2 1012 {
681418c2
RH
1013 case seh_kind_x64:
1014 exp.X_op = O_symbol_rva;
1015 exp.X_add_number = 0;
1016
1017 exp.X_add_symbol = c->start_addr;
1018 emit_expr (&exp, 4);
2d7f4929
KT
1019 exp.X_op = O_symbol_rva;
1020 exp.X_add_number = 0;
681418c2
RH
1021 exp.X_add_symbol = c->end_addr;
1022 emit_expr (&exp, 4);
2d7f4929
KT
1023 exp.X_op = O_symbol_rva;
1024 exp.X_add_number = 0;
681418c2
RH
1025 exp.X_add_symbol = c->xdata_addr;
1026 emit_expr (&exp, 4);
1027 break;
987499b2 1028
681418c2
RH
1029 case seh_kind_mips:
1030 exp.X_op = O_symbol;
1031 exp.X_add_number = 0;
987499b2 1032
681418c2
RH
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);
987499b2 1037
681418c2
RH
1038 emit_expr (&c->handler, 4);
1039 emit_expr (&c->handler_data, 4);
604ab327 1040
681418c2
RH
1041 exp.X_add_symbol = (c->endprologue_addr
1042 ? c->endprologue_addr
1043 : c->start_addr);
1044 emit_expr (&exp, 4);
1045 break;
987499b2 1046
681418c2
RH
1047 case seh_kind_arm:
1048 seh_arm_write_function_pdata (c);
1049 break;
604ab327 1050
681418c2
RH
1051 default:
1052 abort ();
987499b2 1053 }
681418c2
RH
1054
1055 subseg_set (save_seg, save_subseg);
987499b2 1056}