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