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