1 #include "obj-coff-seh.h"
3 /* Forward declarations. */
4 static seh_kind
seh_get_target_kind (void);
5 static int seh_symbol (bfd
*, const char *, const char *, const char *, asection
*, int, int);
6 static void seh_reloc (bfd
*, bfd_size_type
, int, int);
7 static void save_relocs (asection
*sec
);
8 static asection
*quick_section (bfd
*abfd
, const char *name
, int flags
, int align
);
9 static void seh_symbol_init (bfd
*abfd
, unsigned int added
);
10 static void seh_emit_rva (const char *);
11 static void seh_emit_long (const char *);
12 static void seh_make_globl (char *);
13 static segT
seh_make_section (void);
14 static segT
seh_make_section2 (const char *section_name
, unsigned flags
);
15 static char *seh_make_xlbl_name (seh_context
*);
16 static char *make_seh_text_label (seh_context
*c
, symbolS
**addr
);
18 static void seh_write_text_eh_data (const char *hnd
, const char *hnd_data
);
19 static void seh_emit_rva (const char *name
);
20 static int seh_needed_unwind_info (seh_context
*);
21 static void seh_fill_pcsyms (const seh_context
*c
, char **, int *);
22 static size_t seh_getelm_data_size (const seh_context
*, int, int);
23 static size_t seh_getsize_of_unwind_entry (seh_context
*, int, int, int);
24 static void seh_make_unwind_entry (const seh_context
*, char *, int, int, int, unsigned char *, size_t *, int);
25 static size_t seh_getsize_unwind_data (seh_context
*);
26 static void seh_create_unwind_data (seh_context
*, unsigned char *, size_t);
27 static void seh_make_function_entry_xdata (seh_context
*, char *, char *, char *, unsigned char *, size_t *,int);
28 static seh_scope_elem
*seh_x64_makescope_elem (seh_context
*, const char *, const char *, const char *, const char *);
31 static asymbol
**symtab
;
33 static arelent
*reltab
= 0;
34 static int relcount
= 0, relsize
= 0;
36 static seh_context
*seh_ctx_root
= NULL
;
37 static seh_context
*seh_ctx
= NULL
;
38 static seh_context
*seh_ctx_cur
= NULL
;
40 /* Write xdata for arm, sh3, sh4, and ppc. */
42 seh_write_text_eh_data (const char *hnd
, const char *hnd_data
)
50 if (!hnd_data
|| hnd_data
[0] == '@')
53 seh_emit_long (hnd_data
);
56 /* Generate initial pdata for x64 and mips. */
58 make_function_entry_pdata (seh_context
*c
)
61 segT current_seg
= now_seg
;
62 subsegT current_subseg
= now_subseg
;
64 sec
= seh_make_section ();
65 switch (seh_get_target_kind ())
69 seh_emit_rva (c
->func_name
);
70 seh_emit_rva (c
->end_symbol
);
71 seh_emit_rva (c
->xdata_first
);
75 seh_emit_long (c
->func_name
);
76 seh_emit_long (c
->end_symbol
);
77 if (c
->handler_name
== NULL
)
79 else if (c
->handler_name
[0] == '@')
81 if (strcasecmp (c
->handler_name
, "@1") == 0)
87 seh_emit_long (c
->handler_name
);
88 if (c
->handler_data_name
== NULL
|| c
->handler_data_name
[0] == '@')
91 seh_emit_long (c
->handler_data_name
);
92 seh_emit_long (c
->endprologue_symbol
? c
->endprologue_symbol
: c
->func_name
);
97 subseg_set (current_seg
, current_subseg
);
101 seh_x64_write_xdata (void)
104 size_t xdata_size
= 0, count_syms
= 0;
105 size_t xdata_offs
= 0;
108 bfd
*abfd
= stdoutput
;
115 h
->xdata_offset
= xdata_size
;
116 xdata_size
+= seh_getsize_unwind_data (h
);
117 count_syms
+= h
->count_syms
;
124 seh_symbol_init (abfd
, count_syms
);
125 data
= xmalloc (xdata_size
);
126 seg_xdata
= quick_section (abfd
, ".xdata", SEC_HAS_CONTENTS
, 3);
127 seg_xdata
->contents
= data
;
128 memset (data
, 0, xdata_size
);
129 bfd_set_section_size (abfd
, seg_xdata
, xdata_size
);
133 xdata_offs
= h
->xdata_offset
;
134 h
->section
= seg_xdata
;
139 seh_create_unwind_data (h
, data
, xdata_offs
);
144 save_relocs (seg_xdata
);
145 bfd_set_symtab (abfd
, symtab
, symptr
);
146 bfd_set_section_contents (abfd
, seg_xdata
, data
, 0, xdata_size
);
150 seh_arm_create_pdata (seh_context
*c
, unsigned char *data
, size_t pdata_offs
)
155 valueT prolog_len
= 0;
156 valueT start_len
= 0;
157 func_len
= resolve_symbol_value (c
->end_addr
);
158 start_len
= resolve_symbol_value (c
->start_addr
);
159 if (c
->endprologue_addr
)
160 prolog_len
= resolve_symbol_value (c
->endprologue_addr
);
162 prolog_len
= start_len
;
163 func_len
-= start_len
;
164 prolog_len
-= start_len
;
168 idx
= seh_symbol (c
->abfd
, c
->start_symbol
, "", "", UNDSEC
, BSF_GLOBAL
, 0);
169 seh_reloc (c
->abfd
, pdata_offs
, BFD_RELOC_32
, idx
);
170 val
= (unsigned int) func_len
;
172 val
|= ((unsigned int) prolog_len
& 0xffU
);
173 if (c
->use_instruction_32
)
175 if (c
->handler_written
)
177 bfd_put_32 (c
->abfd
, (bfd_vma
) val
, data
+ pdata_offs
+ 4);
181 seh_arm_write_pdata (void)
184 size_t pdata_size
= 0, count_syms
= 0;
185 size_t pdata_offs
= 0;
188 bfd
*abfd
= stdoutput
;
195 h
->xdata_offset
= pdata_size
;
204 seh_symbol_init (abfd
, count_syms
);
205 data
= xmalloc (pdata_size
);
206 seg_pdata
= quick_section (abfd
, ".pdata", SEC_HAS_CONTENTS
, 3);
207 seg_pdata
->contents
= data
;
208 memset (data
, 0, pdata_size
);
209 bfd_set_section_size (abfd
, seg_pdata
, pdata_size
);
213 pdata_offs
= h
->xdata_offset
;
214 h
->section
= seg_pdata
;
218 seh_arm_create_pdata (h
, data
, pdata_offs
);
223 save_relocs (seg_pdata
);
224 bfd_set_symtab (abfd
, symtab
, symptr
);
225 bfd_set_section_contents (abfd
, seg_pdata
, data
, 0, pdata_size
);
229 obj_coff_seh_do_final (void)
231 switch (seh_get_target_kind ())
237 seh_arm_write_pdata ();
240 seh_x64_write_xdata ();
246 seh_x64_make_prologue_element (int kind
, int reg
, bfd_vma off
)
248 seh_prologue_element
*n
;
249 if (seh_ctx_cur
== NULL
)
251 if (seh_ctx_cur
->elems_count
== seh_ctx_cur
->elems_max
)
253 seh_ctx_cur
->elems
= (seh_prologue_element
*)
254 xrealloc (seh_ctx_cur
->elems
,
255 ((seh_ctx_cur
->elems_max
+ 8) * sizeof (seh_prologue_element
)));
256 seh_ctx_cur
->elems_max
+= 8;
258 n
= &seh_ctx_cur
->elems
[seh_ctx_cur
->elems_count
];
259 memset (n
, 0, sizeof (seh_prologue_element
));
263 n
->pc_symbol
= make_seh_text_label (seh_ctx_cur
, &(n
->pc_addr
));
264 seh_ctx_cur
->elems_count
+= 1;
268 seh_x64_read_reg (const char *tok
, int kind
, int *regno
)
270 static const char *frame_regs
[16] =
271 { "cfa", "rcx", "rdx", "rbx", "rsp", "rbp","rsi","rdi",
272 "r8","r9","r10","r11","r12","r13","r14","r15" };
273 static const char *int_regs
[16] =
274 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp","rsi","rdi",
275 "r8","r9","r10","r11","r12","r13","r14","r15" };
276 static const char *xmm_regs
[16] =
277 { "xmm0", "xmm1", "xmm2", "xmm3", "xmm4", "xmm5", "xmm6", "xmm7",
278 "xmm8", "xmm9", "xmm10","xmm11","xmm12","xmm13","xmm14","xmm15" };
279 static const char *mm_regs
[16] =
280 { "xmm0", "mm1", "mm2", "mm3", "mm4", "mm5", "mm6", "mm7",
281 "xmm8", "mm9", "mm10","mm11","mm12","mm13","mm14","mm15" };
282 const char **p
= NULL
;
284 char *symbol_name
= NULL
;
287 while (*input_line_pointer
== ' ' || *input_line_pointer
== '\t')
288 input_line_pointer
++;
289 while (*input_line_pointer
== ' ' || *input_line_pointer
== '\t')
290 input_line_pointer
++;
308 if (*input_line_pointer
== 0 || *input_line_pointer
== '\n')
312 if (*input_line_pointer
== '%')
313 ++input_line_pointer
;
314 symbol_name
= input_line_pointer
;
315 name_end
= get_symbol_end ();
316 for (i
= 0; i
< 16; i
++)
318 if (! strcasecmp (p
[i
], symbol_name
))
323 as_warn (_("In %s we found the invalid register name %s.\n"),
326 *input_line_pointer
= name_end
;
327 while (*input_line_pointer
== ' ' || *input_line_pointer
== '\t')
328 input_line_pointer
++;
329 if (*input_line_pointer
== ',')
330 ++input_line_pointer
;
331 while (*input_line_pointer
== ' ' || *input_line_pointer
== '\t')
332 input_line_pointer
++;
338 seh_read_offset (const char *tok
, bfd_vma
*off
)
340 bfd_vma r
, v
= 0, base
= 10;
343 while (*input_line_pointer
== ' ' || *input_line_pointer
== '\t')
344 input_line_pointer
++;
345 if (*input_line_pointer
== '0')
347 ++input_line_pointer
;
350 switch ((*input_line_pointer
))
355 ++input_line_pointer
;
360 input_line_pointer
++;
365 input_line_pointer
++;
369 while (*input_line_pointer
!= 0)
371 if (input_line_pointer
[0] >= '0' && input_line_pointer
[0] <='9')
372 r
= (bfd_vma
) (input_line_pointer
[0] - '0');
373 else if (base
== 16 && input_line_pointer
[0] >= 'a' && input_line_pointer
[0] <='f')
374 r
= (bfd_vma
) ((input_line_pointer
[0] - 'a') + 10);
375 else if (base
== 16 && input_line_pointer
[0] >= 'A' && input_line_pointer
[0] <='F')
376 r
= (bfd_vma
) ((input_line_pointer
[0] - 'A') + 10);
379 input_line_pointer
++;
387 as_warn (_("In %s we expect a number.\n"),
390 while (*input_line_pointer
== ' ' || *input_line_pointer
== '\t')
391 input_line_pointer
++;
392 if (*input_line_pointer
== ',')
393 ++input_line_pointer
;
394 while (*input_line_pointer
== ' ' || *input_line_pointer
== '\t')
395 input_line_pointer
++;
400 obj_coff_seh_32 (int what
)
402 if (seh_ctx_cur
== NULL
)
404 as_fatal (_(".seh_eh requires to be in .seh_proc/.seh_endproc block.\n"));
405 demand_empty_rest_of_line ();
408 seh_ctx_cur
->use_instruction_32
= (what
? 1 : 0);
409 if (seh_get_target_kind () == seh_kind_arm
)
410 as_warn (_(".seh_%s32 is ignored for this target."), (what
? "" : "no"));
411 demand_empty_rest_of_line ();
415 obj_coff_seh_eh (int what ATTRIBUTE_UNUSED
)
417 if (seh_ctx_cur
== NULL
)
419 as_fatal (_(".seh_eh requires to be in .seh_proc/.seh_endproc block.\n"));
420 demand_empty_rest_of_line ();
423 if (seh_get_target_kind () == seh_kind_arm
)
425 seh_ctx_cur
->handler_written
= 1;
426 /* write block to .text if exception handler is set. */
427 seh_write_text_eh_data (seh_ctx_cur
->handler_name
, seh_ctx_cur
->handler_data_name
);
429 demand_empty_rest_of_line ();
433 obj_coff_seh_handler (int what ATTRIBUTE_UNUSED
)
438 if (seh_ctx_cur
== NULL
)
440 as_fatal (_(".seh_handler requires to be in .seh_proc/.seh_endproc block.\n"));
441 demand_empty_rest_of_line ();
444 if (*input_line_pointer
== 0 || *input_line_pointer
== '\n')
446 as_fatal (_(".seh_handler requires a handler lable name.\n"));
447 demand_empty_rest_of_line ();
451 while (*input_line_pointer
== ' ' || *input_line_pointer
== '\t' || *input_line_pointer
== ',')
452 input_line_pointer
++;
453 symbol_name
= input_line_pointer
;
454 name_end
= get_symbol_end ();
455 seh_ctx
->handler_name
= xstrdup (symbol_name
);
456 if (symbol_name
[0] == '@')
458 if (strcasecmp (symbol_name
, "@0") != 0 && strcasecmp (symbol_name
, "@1") != 0
459 && strcasecmp (symbol_name
, "@null") != 0)
460 as_warn (_("Unknown constant value ,%s' for handler."), symbol_name
);
462 *input_line_pointer
= name_end
;
463 seh_ctx
->handler_data_name
= NULL
;
464 while (*input_line_pointer
== ' ' || *input_line_pointer
== '\t' || *input_line_pointer
== ',')
465 input_line_pointer
++;
466 symbol_name
= input_line_pointer
;
467 if (*input_line_pointer
!= '\n' && *input_line_pointer
!= 0)
469 name_end
= get_symbol_end ();
470 seh_ctx
->handler_data_name
= xstrdup (symbol_name
);
471 if (symbol_name
[0] == '@')
473 if (seh_get_target_kind () != seh_kind_x64
)
474 as_fatal (_("For this target .seh_handler doesn't support constant user-data."));
475 else if (strcasecmp (symbol_name
, "@unwind") != 0 &&
476 strcasecmp (symbol_name
, "@except") != 0)
477 as_warn (_("For .seh_handler the constant ,%s' is ignored."), symbol_name
);
479 *input_line_pointer
= name_end
;
481 if (seh_ctx_cur
->handler_written
)
482 as_warn (_(".seh_handler is ignored as .seh_eh was seen before."));
483 demand_empty_rest_of_line ();
487 obj_coff_seh_scope (int what ATTRIBUTE_UNUSED
)
489 char *symbol_name
,*beg
= NULL
,*end
= NULL
, *handl
= NULL
, *jmp
= NULL
;
492 if (seh_ctx_cur
== NULL
)
494 as_fatal (_(".seh_scope requires to be in .seh_proc/.seh_endproc block.\n"));
495 demand_empty_rest_of_line ();
499 while (*input_line_pointer
== ' ' || *input_line_pointer
== '\t' || *input_line_pointer
== ',')
500 input_line_pointer
++;
501 if (*input_line_pointer
== 0 || *input_line_pointer
== '\n')
503 as_fatal (_(".seh_scope requires four symbol names.\n"));
504 demand_empty_rest_of_line ();
507 symbol_name
= input_line_pointer
;
508 name_end
= get_symbol_end ();
509 beg
= xstrdup (symbol_name
);
510 *input_line_pointer
= name_end
;
511 while (*input_line_pointer
== ' ' || *input_line_pointer
== '\t' || *input_line_pointer
== ',')
512 input_line_pointer
++;
513 if (*input_line_pointer
== 0 || *input_line_pointer
== '\n')
515 as_fatal (_(".seh_scope requires three more symbol names.\n"));
516 demand_empty_rest_of_line ();
519 symbol_name
= input_line_pointer
;
520 name_end
= get_symbol_end ();
521 end
= xstrdup (symbol_name
);
522 *input_line_pointer
= name_end
;
523 while (*input_line_pointer
== ' ' || *input_line_pointer
== '\t' || *input_line_pointer
== ',')
524 input_line_pointer
++;
525 if (*input_line_pointer
== 0 || *input_line_pointer
== '\n')
527 as_fatal (_(".seh_scope requires two more symbol names.\n"));
528 demand_empty_rest_of_line ();
531 symbol_name
= input_line_pointer
;
532 name_end
= get_symbol_end ();
533 handl
= xstrdup (symbol_name
);
534 *input_line_pointer
= name_end
;
537 if (strcasecmp (handl
, "@0") != 0 && strcasecmp (handl
, "@1") != 0
538 && strcasecmp (handl
, "@null") != 0)
539 as_warn (_("Unknown constant for handler ,%s'."), handl
);
542 while (*input_line_pointer
== ' ' || *input_line_pointer
== '\t' || *input_line_pointer
== ',')
543 input_line_pointer
++;
544 if (*input_line_pointer
== 0 || *input_line_pointer
== '\n')
546 as_fatal (_(".seh_scope requires one more symbol names.\n"));
547 demand_empty_rest_of_line ();
550 symbol_name
= input_line_pointer
;
551 name_end
= get_symbol_end ();
552 jmp
= xstrdup (symbol_name
);
553 *input_line_pointer
= name_end
;
556 if (strcasecmp (jmp
, "@0") != 0 && strcasecmp (handl
, "@null") != 0)
557 as_warn (_("Unknown constant for jump ,%s'."), jmp
);
560 if (seh_get_target_kind () != seh_kind_x64
)
561 as_warn (_(".seh_scope is ignored for this target."));
563 seh_x64_makescope_elem (seh_ctx_cur
, beg
, end
, handl
, jmp
);
572 demand_empty_rest_of_line ();
576 obj_coff_seh_proc (int what ATTRIBUTE_UNUSED
)
581 if (seh_ctx_cur
!= NULL
)
583 as_warn (_(".seh_proc has to be closed by .seh_endprog\n"));
584 obj_coff_seh_endproc (0);
587 if (*input_line_pointer
== 0 || *input_line_pointer
== '\n')
589 as_fatal (_(".seh_proc requires function lable name.\n"));
590 demand_empty_rest_of_line ();
594 while (*input_line_pointer
== ' ' || *input_line_pointer
== '\t' || *input_line_pointer
== ',')
595 input_line_pointer
++;
596 symbol_name
= input_line_pointer
;
597 name_end
= get_symbol_end ();
600 seh_ctx_root
= seh_ctx
= (seh_context
*) xmalloc (sizeof (seh_context
));
603 seh_ctx
->next
= (seh_context
*) xmalloc (sizeof (seh_context
));
604 seh_ctx
= seh_ctx
->next
;
606 seh_ctx_cur
= seh_ctx
;
607 memset (seh_ctx
, 0, sizeof (seh_context
));
609 seh_ctx
->func_name
= xstrdup (symbol_name
);
610 *input_line_pointer
= name_end
;
611 while (*input_line_pointer
== ' ' || *input_line_pointer
== '\t' || *input_line_pointer
== ',')
612 input_line_pointer
++;
613 seh_ctx
->start_symbol
= make_seh_text_label (seh_ctx_cur
, &(seh_ctx_cur
->start_addr
));
614 demand_empty_rest_of_line ();
618 obj_coff_seh_endproc (int what ATTRIBUTE_UNUSED
)
620 if (seh_ctx_cur
== NULL
)
622 as_warn (_(".seh_endprog without prior .seh_proc (ignored)\n"));
623 demand_empty_rest_of_line ();
626 seh_ctx
->end_symbol
= make_seh_text_label (seh_ctx
, &(seh_ctx
->end_addr
));
627 seh_ctx
->xdata_first
= seh_make_xlbl_name (seh_ctx
);
628 make_function_entry_pdata (seh_ctx
);
630 demand_empty_rest_of_line ();
634 obj_coff_seh_push (int what
)
638 if (seh_ctx_cur
== NULL
)
640 as_warn (_(".seh_push used outside of .seh_proc block.\n"));
641 demand_empty_rest_of_line ();
644 /* what 0:reg, 1:pushframe. */
648 if (seh_x64_read_reg (".seh_push", 1, ®
))
649 kind
= UWOP_PUSH_NONVOL
;
651 as_warn (_(".seh_pushreg expects register argument."));
654 kind
= UWOP_PUSH_MACHFRAME
;
659 if (seh_get_target_kind () != seh_kind_x64
)
660 as_warn (_(".seh_save... is ignored for this target.\n"));
662 seh_x64_make_prologue_element (kind
, reg
, 0);
663 demand_empty_rest_of_line ();
667 obj_coff_seh_save (int what
)
674 /* what 0:reg, 1:mm, 2:xmm. */
678 ok
&= seh_x64_read_reg (".seh_savereg", 1, ®
);
679 kind
= UWOP_SAVE_NONVOL
;
682 ok
&= seh_x64_read_reg (".seh_savemm", 2, ®
);
683 kind
= UWOP_SAVE_XMM
;
686 ok
&= seh_x64_read_reg (".seh_savexmm", 3, ®
);
687 kind
= UWOP_SAVE_XMM128
;
692 ok
&= seh_read_offset (".seh_save", &off
);
693 if (seh_ctx_cur
== NULL
)
695 as_warn (_(".seh_save used outside of .seh_proc block.\n"));
696 demand_empty_rest_of_line ();
699 if (seh_get_target_kind () != seh_kind_x64
)
700 as_warn (_(".seh_save... is ignored for this target.\n"));
702 seh_x64_make_prologue_element (kind
, reg
, off
);
703 demand_empty_rest_of_line ();
707 obj_coff_seh_endprologue (int what ATTRIBUTE_UNUSED
)
709 if (seh_ctx_cur
== NULL
)
711 as_warn (_(".seh_endprologue used outside of .seh_proc block.\n"));
712 demand_empty_rest_of_line ();
715 if (seh_ctx_cur
->endprologue_symbol
!= NULL
)
716 as_warn (_(".seh_endprologue used more then once in .seh_proc block.\n"));
718 seh_ctx_cur
->endprologue_symbol
= make_seh_text_label (seh_ctx_cur
, &seh_ctx_cur
->endprologue_addr
);
722 obj_coff_seh_stack_alloc (int what ATTRIBUTE_UNUSED
)
725 if (seh_ctx_cur
== NULL
)
727 as_warn (_(".seh_stackalloc used outside of .seh_proc block.\n"));
728 demand_empty_rest_of_line ();
731 if (seh_read_offset (".seh_stackalloc", &size
))
733 if (seh_get_target_kind () != seh_kind_x64
)
734 as_warn (_(".seh_stackalloc is ignored for this target.\n"));
736 seh_x64_make_prologue_element (UWOP_ALLOC_LARGE
, 0, size
);
741 obj_coff_seh_setframe (int what ATTRIBUTE_UNUSED
)
747 ok
&= seh_x64_read_reg (".seh_setframe", 0, ®
);
748 ok
&= seh_read_offset (".seh_setframe", &off
);
749 if (seh_ctx_cur
== NULL
)
751 as_warn (_(".seh_setframe used outside of .seh_proc block.\n"));
752 demand_empty_rest_of_line ();
757 seh_ctx_cur
->framereg
= reg
;
758 seh_ctx_cur
->frameoff
= off
;
760 if (seh_get_target_kind () != seh_kind_x64
)
761 as_warn (_(".seh_setframe is ignored for this target.\n"));
762 demand_empty_rest_of_line ();
765 /* Misc function helpers. */
767 seh_reloc (bfd
*abfd
, bfd_size_type address
, int which_howto
, int symidx
)
769 if (relcount
>= relsize
- 1)
773 reltab
= xrealloc (reltab
, relsize
* sizeof (arelent
));
775 reltab
= xmalloc (relsize
* sizeof (arelent
));
777 reltab
[relcount
].address
= address
;
778 reltab
[relcount
].addend
= 0;
779 reltab
[relcount
].howto
= bfd_reloc_type_lookup (abfd
, which_howto
);
780 reltab
[relcount
].sym_ptr_ptr
= symtab
+ symidx
;
785 save_relocs (asection
*sec
)
789 sec
->relocation
= reltab
;
790 sec
->reloc_count
= relcount
;
791 sec
->orelocation
= xmalloc ((relcount
+ 1) * sizeof (arelent
*));
792 for (i
= 0; i
< relcount
; i
++)
793 sec
->orelocation
[i
] = sec
->relocation
+ i
;
794 sec
->orelocation
[relcount
] = 0;
795 sec
->flags
|= SEC_RELOC
;
797 relcount
= relsize
= 0;
801 seh_symbol_init (bfd
*abfd
, unsigned int added
)
803 unsigned int oldcount
;
804 oldcount
= bfd_get_symcount (abfd
);
806 symtab
= xmalloc ((oldcount
+ added
+ 6) * sizeof (asymbol
*));
808 memcpy (symtab
, bfd_get_outsymbols (abfd
), sizeof (asymbol
*) * oldcount
);
812 seh_symbol (bfd
*abfd
, const char *n1
, const char *n2
, const char *n3
,
813 asection
*sec
, int flags
, int addr
)
816 char *name
= xmalloc (strlen (n1
) + strlen (n2
) + strlen (n3
) + 1);
821 sym
= bfd_make_empty_symbol (abfd
);
826 symtab
[symptr
++] = sym
;
831 quick_section (bfd
*abfd
, const char *name
, int flags
, int align
)
835 sec
= seh_make_section2 (name
, flags
);
836 bfd_set_section_alignment (abfd
, sec
, align
);
837 /* Remember to undo this before trying to link internally! */
839 sym
= bfd_make_empty_symbol (abfd
);
840 symtab
[symptr
++] = sym
;
841 sym
->name
= sec
->name
;
843 sym
->flags
= BSF_LOCAL
;
850 seh_get_target_kind (void)
853 return seh_kind_unknown
;
854 switch (bfd_get_arch (stdoutput
))
857 case bfd_arch_powerpc
:
861 switch (bfd_get_mach (stdoutput
))
863 case bfd_mach_x86_64
:
864 case bfd_mach_x86_64_intel_syntax
:
871 return seh_kind_mips
;
873 /* Should return seh_kind_x64. But not implemented yet. */
874 return seh_kind_unknown
;
878 return seh_kind_unknown
;
882 seh_emit_rva (const char *name
)
884 char *p
= (char *) xmalloc (strlen (name
) + 1);
885 char *s
= input_line_pointer
;
888 input_line_pointer
= p
;
890 input_line_pointer
= s
;
894 seh_emit_long (const char *name
)
896 char *p
= (char *) xmalloc (strlen (name
) + 1);
897 char *s
= input_line_pointer
;
900 input_line_pointer
= p
;
902 input_line_pointer
= s
;
906 seh_make_globl (char *sym_name
)
908 char *s
= input_line_pointer
;
910 input_line_pointer
= sym_name
;
912 input_line_pointer
= s
;
916 seh_make_section2 (const char *section_name
, unsigned flags
)
921 name
= xmalloc (strlen (section_name
) + 1);
922 strcpy (name
, section_name
);
924 sec
= subseg_new (name
, (subsegT
) 0);
925 bfd_set_section_flags (stdoutput
, sec
,
926 ((SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_DATA
| flags
)
927 & bfd_applicable_section_flags (stdoutput
)));
933 seh_make_section (void)
935 static segT seg_pdata
= NULL
;
939 seg_pdata
= seh_make_section2 (".pdata", 0);
945 seh_make_xlbl_name (seh_context
*c
)
947 size_t len
= strlen (".seh_xlbl_") + strlen (c
->func_name
) + 9 + 1;
948 char *ret
= (char*) xmalloc (len
);
950 as_fatal (_("Out of memory for xdata lable for %s"), c
->func_name
);
952 sprintf (ret
, ".seh_xlbl_%s_%x", c
->func_name
, + c
->xlbl_count
);
958 make_seh_text_label (seh_context
*c
, symbolS
**addr
)
961 size_t len
= strlen (".seh_tlbl_") + strlen (c
->func_name
) + 9 + 1;
962 sym_name
= (char *) xmalloc (len
);
964 as_fatal (_("Allocating memory for SEH's text symbol for %s failed"), c
->func_name
);
965 sprintf (sym_name
, ".seh_tlbl_%s_%x", c
->func_name
, c
->tlbl_count
);
969 seh_make_globl (sym_name
);
970 *addr
= colon (sym_name
);
975 /* x64 secific functions. */
978 seh_fill_pcsyms (const seh_context
*c
, char **names
, int *idx
)
982 valueT start_off
= resolve_symbol_value (c
->start_addr
);
984 seh_prologue_element
*e
= c
->elems
;
985 names
[0] = c
->start_symbol
;
987 if (c
->elems_count
== 0)
989 for (i
= 0; i
< c
->elems_count
; i
++)
991 un_off
= resolve_symbol_value (e
[i
].pc_addr
);
992 if ((un_off
- start_off
) > 255)
994 names
[count
] = e
[i
].pc_symbol
;
995 idx
[count
] = (int) i
;
1003 seh_needed_unwind_info (seh_context
*c
)
1007 valueT start_off
= resolve_symbol_value (c
->start_addr
);
1009 seh_prologue_element
*e
= c
->elems
;
1010 if (c
->elems_count
== 0)
1012 for (i
= 0; i
< c
->elems_count
; i
++)
1014 un_off
= resolve_symbol_value (e
[i
].pc_addr
);
1015 if ((un_off
- start_off
) > 255)
1025 seh_getelm_data_size (const seh_context
*c
, int elm_start
, int elm_end
)
1027 size_t ret
= PEX64_UWI_SIZEOF_UWCODE_ARRAY (elm_end
- elm_start
);
1028 while (elm_start
< elm_end
)
1030 switch (c
->elems
[elm_start
].kind
)
1032 case UWOP_PUSH_NONVOL
:
1033 case UWOP_PUSH_MACHFRAME
:
1036 case UWOP_SAVE_NONVOL
:
1038 case UWOP_SAVE_XMM128
:
1039 if ((c
->elems
[elm_start
].offset
& 7) != 0 ||
1040 ((c
->elems
[elm_start
].offset
/ 8) > 0xffff))
1045 case UWOP_ALLOC_LARGE
:
1057 seh_getsize_of_unwind_entry (seh_context
*c
, int elm_start
, int elm_end
, int bechain
)
1059 size_t ret
= seh_getelm_data_size(c
, elm_start
, elm_end
);
1065 c
->count_reloc
+= 1;
1070 if (c
->handler_name
!= NULL
)
1072 if (c
->handler_data_name
!= NULL
1073 && c
->handler_data_name
[0] != '@')
1077 c
->count_reloc
+= 2;
1081 ret
+= 8 + (c
->scope_count
* 4) * 4;
1082 c
->count_syms
+= (c
->scope_count
* 4) + 1;
1083 c
->count_reloc
+= (c
->scope_count
* 4) + 1;
1091 seh_make_unwind_entry (const seh_context
*c
, char *name
, int elm_start
, int elm_end
, int bechain
,
1092 unsigned char *data
, size_t *poffs
, int no
)
1094 size_t off
= *poffs
;
1096 valueT start_off
= resolve_symbol_value (c
->start_addr
);
1097 valueT end_prologue
;
1098 size_t uwcodes
= seh_getelm_data_size(c
, elm_start
, elm_end
);
1099 unsigned int flag
= UNW_FLAG_NHANDLER
;
1102 if (c
->handler_name
!= NULL
)
1104 flag
= UNW_FLAG_EHANDLER
;
1105 if (c
->handler_data_name
!= NULL
&& c
->handler_data_name
[0] != '@')
1106 flag
= UNW_FLAG_FHANDLER
;
1107 else if (c
->handler_data_name
!= NULL
&&
1108 strcasecmp (c
->handler_data_name
, "@unwind") == 0)
1109 flag
= UNW_FLAG_UHANDLER
;
1111 if (!c
->endprologue_addr
)
1112 end_prologue
= start_off
;
1114 end_prologue
= resolve_symbol_value (c
->endprologue_addr
);
1115 seh_symbol (c
->abfd
, name
, "", "", c
->section
, BSF_GLOBAL
, (int) off
);
1116 data
[off
++] = (1 | ((bechain
? UNW_FLAG_CHAININFO
: flag
) << 3));
1118 start_off
= (valueT
) c
->elems
[elm_start
].offset
;
1119 end_prologue
-= start_off
;
1120 if (end_prologue
> 255)
1122 data
[off
++] = (unsigned char) end_prologue
;
1123 data
[off
++] = (unsigned char) (uwcodes
/ 2);
1124 data
[off
] = (unsigned char) c
->framereg
;
1125 data
[off
++] |= (unsigned char) ((c
->frameoff
/ 16) << 4);
1130 sprintf (n
,"%x", no
);
1131 idx
= seh_symbol (c
->abfd
, ".xdata_fct", c
->func_name
, n
, UNDSEC
, BSF_GLOBAL
, (int) off
);
1132 seh_reloc (c
->abfd
, off
, BFD_RELOC_RVA
, idx
);
1135 else if (c
->handler_name
!= NULL
)
1137 if (flag
== UNW_FLAG_FHANDLER
)
1139 if (strcasecmp (c
->handler_name
, "@1") == 0)
1140 bfd_put_32 (c
->abfd
, (bfd_vma
) 1, &data
[off
]);
1141 else if (c
->handler_name
[0] != '@')
1143 idx
= seh_symbol (c
->abfd
, c
->handler_name
, "", "", UNDSEC
, BSF_GLOBAL
, 0);
1144 seh_reloc (c
->abfd
, off
, BFD_RELOC_RVA
, idx
);
1147 idx
= seh_symbol (c
->abfd
, c
->handler_data_name
, "", "", UNDSEC
, BSF_GLOBAL
, 0);
1148 seh_reloc (c
->abfd
, off
, BFD_RELOC_RVA
, idx
);
1151 else if (flag
== UNW_FLAG_UHANDLER
|| flag
== UNW_FLAG_EHANDLER
)
1153 if (strcasecmp (c
->handler_name
, "@1") == 0)
1154 bfd_put_32 (c
->abfd
, (bfd_vma
) 1, &data
[off
]);
1155 else if (c
->handler_name
[0] != '@')
1157 idx
= seh_symbol (c
->abfd
, c
->handler_name
, "", "", UNDSEC
, BSF_GLOBAL
, 0);
1158 seh_reloc (c
->abfd
, off
, BFD_RELOC_RVA
, idx
);
1161 bfd_put_32 (c
->abfd
, (bfd_vma
) c
->scope_count
, &data
[off
]);
1163 for (it
= 0; it
< c
->scope_count
; it
++)
1165 idx
= seh_symbol (c
->abfd
, c
->scopes
[it
].begin_addr
, "", "", UNDSEC
, BSF_GLOBAL
, 0);
1166 seh_reloc (c
->abfd
, off
, BFD_RELOC_RVA
, idx
);
1168 idx
= seh_symbol (c
->abfd
, c
->scopes
[it
].end_addr
, "", "", UNDSEC
, BSF_GLOBAL
, 0);
1169 seh_reloc (c
->abfd
, off
, BFD_RELOC_RVA
, idx
);
1171 if (c
->scopes
[it
].handler_addr
[0] == '@')
1173 if (strcasecmp (c
->scopes
[it
].handler_addr
, "@1") == 0)
1174 bfd_put_32 (c
->abfd
, (bfd_vma
) 1, &data
[off
]);
1178 idx
= seh_symbol (c
->abfd
, c
->scopes
[it
].handler_addr
, "", "", UNDSEC
, BSF_GLOBAL
, 0);
1179 seh_reloc (c
->abfd
, off
, BFD_RELOC_RVA
, idx
);
1182 if (c
->scopes
[it
].jump_addr
[0] == '@')
1184 if (strcasecmp (c
->scopes
[it
].jump_addr
, "@1") == 0)
1185 bfd_put_32 (c
->abfd
, (bfd_vma
) 1, &data
[off
]);
1189 idx
= seh_symbol (c
->abfd
, c
->scopes
[it
].jump_addr
, "", "", UNDSEC
, BSF_GLOBAL
, 0);
1190 seh_reloc (c
->abfd
, off
, BFD_RELOC_RVA
, idx
);
1200 seh_getsize_unwind_data (seh_context
*c
)
1202 int need
= seh_needed_unwind_info (c
);
1204 char **names
= (char **) xmalloc (sizeof (char *) * need
);
1205 char **pc_syms
= (char **) xmalloc (sizeof (char *) * need
);
1206 int *elm_start
= (int *) xmalloc (sizeof (int) * (need
+ 1));
1207 size_t xdata_sz
= 0;
1209 seh_fill_pcsyms (c
, pc_syms
, elm_start
);
1210 elm_start
[need
] = c
->elems_count
;
1212 xdata_sz
+= ((12 * (size_t) need
));
1213 c
->count_syms
+= 5 * need
;
1214 xdata_sz
+= (seh_getsize_of_unwind_entry (c
, elm_start
[0], elm_start
[1], 1 != need
) + 7) & ~7;
1215 for (i
= 1; i
< need
; i
++)
1217 xdata_sz
+= (seh_getsize_of_unwind_entry (c
, elm_start
[i
], elm_start
[i
+ 1], 1 != need
) + 7) & ~7;
1220 /* Create lable names for .xdata unwind info. */
1221 names
[0] = c
->xdata_first
;
1222 for (i
= 1; i
< need
; i
++)
1223 names
[i
] = seh_make_xlbl_name (c
);
1224 c
->xdata_names
= names
;
1225 c
->xdata_pcsyms
= pc_syms
;
1226 c
->xdata_elm_start
= elm_start
;
1227 c
->xdata_sz
= xdata_sz
;
1232 seh_create_unwind_data (seh_context
*c
, unsigned char *data
, size_t offs
)
1234 int need
= seh_needed_unwind_info (c
);
1236 char **names
= c
->xdata_names
;
1237 char **pc_syms
= c
->xdata_pcsyms
;
1238 int *elm_start
= c
->xdata_elm_start
;
1240 for (i
= 1; i
< need
; i
++)
1242 seh_make_function_entry_xdata (c
, pc_syms
[i
], c
->end_symbol
, names
[i
], data
, &offs
, i
);
1244 /* Generate the function entry. Remark, that just
1245 first is in .pdata section and already emitted. */
1246 seh_make_unwind_entry (c
, c
->xdata_first
, elm_start
[0], elm_start
[1], 1 != need
, data
, &offs
, 1);
1247 for (i
= 1; i
< need
; i
++)
1249 seh_make_unwind_entry (c
, names
[i
], elm_start
[i
], elm_start
[i
+ 1], (i
+ 1) != need
, data
, &offs
, i
+ 1);
1251 for (i
= 1; i
< need
; i
++)
1256 c
->xdata_names
= NULL
;
1257 c
->xdata_pcsyms
= NULL
;
1258 c
->xdata_elm_start
= NULL
;
1262 seh_make_function_entry_xdata (seh_context
*c
, char *pc_start
, char *pc_end
, char *pc_xdata
, unsigned char *data
, size_t *poffs
,int no
)
1264 bfd_vma addr
= (bfd_vma
) *poffs
;
1269 sprintf (s
,"%x",no
);
1270 seh_symbol (c
->abfd
, ".xdata_fct",c
->func_name
, s
, c
->section
, BSF_GLOBAL
, (int) poffs
[0]);
1271 idx
= seh_symbol (c
->abfd
, pc_start
,"","", UNDSEC
, BSF_GLOBAL
,0);
1272 seh_reloc (c
->abfd
, addr
, BFD_RELOC_RVA
, idx
);
1273 idx
= seh_symbol (c
->abfd
, pc_end
,"","", UNDSEC
, BSF_GLOBAL
,0);
1274 seh_reloc (c
->abfd
, addr
+ 4, BFD_RELOC_RVA
, idx
);
1275 idx
= seh_symbol (c
->abfd
, pc_xdata
,"","", UNDSEC
, BSF_GLOBAL
,0);
1276 seh_reloc (c
->abfd
, addr
+ 8, BFD_RELOC_RVA
, idx
);
1280 static seh_scope_elem
*
1281 seh_x64_makescope_elem (seh_context
*c
, const char *begin
, const char *end
,
1282 const char *handler
, const char *jmp
)
1287 if (c
->scope_count
>= c
->scope_max
)
1289 seh_scope_elem
*h
= (seh_scope_elem
*) xmalloc (sizeof (seh_scope_elem
) * (c
->scope_max
+ 8));
1290 memset (h
, 0, sizeof (seh_scope_elem
) * (c
->scope_max
+ 8));
1291 if (c
->scopes
!= NULL
)
1292 memcpy (h
, c
->scopes
, sizeof (seh_scope_elem
) * c
->scope_max
);
1293 if (c
->scopes
!= NULL
)
1298 r
= &c
->scopes
[c
->scope_count
++];
1299 r
->begin_addr
= xstrdup (begin
);
1300 r
->end_addr
= xstrdup (end
);
1301 r
->handler_addr
= (!handler
? NULL
: xstrdup (handler
));
1302 r
->jump_addr
= (!jmp
? NULL
: xstrdup (jmp
));