Debian Codegraph @

gcc/config/mingw/winnt.cc

raw
1 /* Subroutines for insn-output.cc for Windows NT.
2 Contributed by Douglas Rupp ([email protected])
3 Copyright (C) 1995-2025 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #define IN_TARGET_CODE 1
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "target.h"
27 #include "function.h"
28 #include "basic-block.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "gimple.h"
32 #include "memmodel.h"
33 #include "tm_p.h"
34 #include "stringpool.h"
35 #include "attribs.h"
36 #include "emit-rtl.h"
37 #include "cgraph.h"
38 #include "lto-streamer.h"
39 #include "except.h"
40 #include "output.h"
41 #include "varasm.h"
42 #include "lto-section-names.h"
43
44 /* i386/PE specific attribute support.
45
46 i386/PE has two new attributes:
47 dllexport - for exporting a function/variable that will live in a dll
48 dllimport - for importing a function/variable from a dll
49
50 Microsoft allows multiple declspecs in one __declspec, separating
51 them with spaces. We do NOT support this. Instead, use __declspec
52 multiple times.
53 */
54
55 /* Handle a "shared" attribute;
56 arguments as in struct attribute_spec.handler. */
57 tree
58 ix86_handle_shared_attribute (tree *node, tree name, tree, int,
59 bool *no_add_attrs)
60 {
61 if (TREE_CODE (*node) != VAR_DECL)
62 {
63 warning (OPT_Wattributes, "%qE attribute only applies to variables",
64 name);
65 *no_add_attrs = true;
66 }
67
68 return NULL_TREE;
69 }
70
71 /* Handle a "selectany" attribute;
72 arguments as in struct attribute_spec.handler. */
73 tree
74 mingw_handle_selectany_attribute (tree *node, tree name, tree, int,
75 bool *no_add_attrs)
76 {
77 tree decl = *node;
78 /* The attribute applies only to objects that are initialized and have
79 external linkage. However, we may not know about initialization
80 until the language frontend has processed the decl. Therefore
81 we make sure that variable isn't initialized as common. */
82 if (TREE_CODE (decl) != VAR_DECL || !TREE_PUBLIC (decl))
83 error ("%qE attribute applies only to initialized variables"
84 " with external linkage", name);
85 else
86 {
87 make_decl_one_only (decl, DECL_ASSEMBLER_NAME (decl));
88 /* A variable with attribute selectany never can be common. */
89 DECL_COMMON (decl) = 0;
90 }
91
92 /* We don't need to keep attribute itself. */
93 *no_add_attrs = true;
94 return NULL_TREE;
95 }
96
97
98 /* Return the type that we should use to determine if DECL is
99 imported or exported. */
100
101 static tree
102 associated_type (tree decl)
103 {
104 return (DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
105 ? DECL_CONTEXT (decl) : NULL_TREE);
106 }
107
108 /* Return true if DECL should be a dllexport'd object. */
109
110 static bool
111 i386_pe_determine_dllexport_p (tree decl)
112 {
113 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
114 return false;
115
116 /* Don't export local clones of dllexports. */
117 if (!TREE_PUBLIC (decl))
118 return false;
119
120 if (TREE_CODE (decl) == FUNCTION_DECL
121 && DECL_DECLARED_INLINE_P (decl)
122 && !flag_keep_inline_dllexport)
123 return false;
124
125 if (lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
126 return true;
127
128 return false;
129 }
130
131 /* Return true if DECL should be a dllimport'd object. */
132
133 static bool
134 i386_pe_determine_dllimport_p (tree decl)
135 {
136 tree assoc;
137
138 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
139 return false;
140
141 if (DECL_DLLIMPORT_P (decl))
142 return true;
143
144 /* The DECL_DLLIMPORT_P flag was set for decls in the class definition
145 by targetm.cxx.adjust_class_at_definition. Check again to emit
146 error message if the class attribute has been overridden by an
147 out-of-class definition of static data. */
148 assoc = associated_type (decl);
149 if (assoc && lookup_attribute ("dllimport", TYPE_ATTRIBUTES (assoc))
150 && VAR_P (decl)
151 && TREE_STATIC (decl) && TREE_PUBLIC (decl)
152 && !DECL_EXTERNAL (decl)
153 /* vtable's are linkonce constants, so defining a vtable is not
154 an error as long as we don't try to import it too. */
155 && !DECL_VIRTUAL_P (decl))
156 error ("definition of static data member %q+D of "
157 "dllimport%'d class", decl);
158
159 return false;
160 }
161
162 /* Handle the -mno-fun-dllimport target switch. */
163
164 bool
165 mingw_pe_valid_dllimport_attribute_p (const_tree decl)
166 {
167 if (TARGET_NOP_FUN_DLLIMPORT && TREE_CODE (decl) == FUNCTION_DECL)
168 return false;
169 return true;
170 }
171
172 /* Return string which is the function name, identified by ID, modified
173 with a suffix consisting of an atsign (@) followed by the number of
174 bytes of arguments. If ID is NULL use the DECL_NAME as base. If
175 FASTCALL is true, also add the FASTCALL_PREFIX.
176 Return NULL if no change required. */
177
178 static tree
179 gen_stdcall_or_fastcall_suffix (tree decl, tree id, bool fastcall)
180 {
181 HOST_WIDE_INT total = 0;
182 const char *old_str = IDENTIFIER_POINTER (id != NULL_TREE ? id : DECL_NAME (decl));
183 char *new_str, *p;
184 tree type = TREE_TYPE (DECL_ORIGIN (decl));
185 tree arg;
186 function_args_iterator args_iter;
187
188 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
189
190 if (prototype_p (type))
191 {
192 /* This attribute is ignored for variadic functions. */
193 if (stdarg_p (type))
194 return NULL_TREE;
195
196 /* Quit if we hit an incomplete type. Error is reported
197 by convert_arguments in c-typeck.cc or cp/typeck.cc. */
198 FOREACH_FUNCTION_ARGS(type, arg, args_iter)
199 {
200 HOST_WIDE_INT parm_size;
201 HOST_WIDE_INT parm_boundary_bytes = PARM_BOUNDARY / BITS_PER_UNIT;
202
203 if (! COMPLETE_TYPE_P (arg))
204 break;
205
206 parm_size = int_size_in_bytes (arg);
207 if (parm_size < 0)
208 break;
209
210 /* Must round up to include padding. This is done the same
211 way as in store_one_arg. */
212 parm_size = ((parm_size + parm_boundary_bytes - 1)
213 / parm_boundary_bytes * parm_boundary_bytes);
214 total += parm_size;
215 }
216 }
217
218 /* Assume max of 8 base 10 digits in the suffix. */
219 p = new_str = XALLOCAVEC (char, 1 + strlen (old_str) + 1 + 8 + 1);
220 if (fastcall)
221 *p++ = FASTCALL_PREFIX;
222 sprintf (p, "%s@" HOST_WIDE_INT_PRINT_DEC, old_str, total);
223
224 return get_identifier (new_str);
225 }
226
227 #if !defined (TARGET_AARCH64_MS_ABI)
228
229 /* Maybe decorate and get a new identifier for the DECL of a stdcall or
230 fastcall function. The original identifier is supplied in ID. */
231
232 static tree
233 i386_pe_maybe_mangle_decl_assembler_name (tree decl, tree id)
234 {
235 tree new_id = NULL_TREE;
236
237 if (TREE_CODE (decl) == FUNCTION_DECL)
238 {
239 unsigned int ccvt = ix86_get_callcvt (TREE_TYPE (decl));
240 if ((ccvt & IX86_CALLCVT_STDCALL) != 0)
241 {
242 if (TARGET_RTD)
243 /* If we are using -mrtd emit undecorated symbol and let linker
244 do the proper resolving. */
245 return NULL_TREE;
246 new_id = gen_stdcall_or_fastcall_suffix (decl, id, false);
247 }
248 else if ((ccvt & IX86_CALLCVT_FASTCALL) != 0)
249 new_id = gen_stdcall_or_fastcall_suffix (decl, id, true);
250 }
251
252 return new_id;
253 }
254
255 #endif
256
257 /* Emit an assembler directive to set symbol for DECL visibility to
258 the visibility type VIS, which must not be VISIBILITY_DEFAULT.
259 As for PE there is no hidden support in gas, we just warn for
260 user-specified visibility attributes. */
261
262 void
263 i386_pe_assemble_visibility (tree decl, int)
264 {
265 if (!decl
266 || !lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)))
267 return;
268 if (!DECL_ARTIFICIAL (decl))
269 warning (OPT_Wattributes, "visibility attribute not supported "
270 "in this configuration; ignored");
271 }
272
273 #if !defined (TARGET_AARCH64_MS_ABI)
274
275 /* This is used as a target hook to modify the DECL_ASSEMBLER_NAME
276 in the language-independent default hook
277 langhooks,c:lhd_set_decl_assembler_name ()
278 and in cp/mangle,c:mangle_decl (). */
279 tree
280 i386_pe_mangle_decl_assembler_name (tree decl, tree id)
281 {
282 tree new_id = i386_pe_maybe_mangle_decl_assembler_name (decl, id);
283
284 return (new_id ? new_id : id);
285 }
286
287 #endif
288
289 /* This hook behaves the same as varasm.cc/assemble_name(), but
290 generates the name into memory rather than outputting it to
291 a file stream. */
292
293 tree
294 i386_pe_mangle_assembler_name (const char *name)
295 {
296 const char *skipped = name + (*name == '*' ? 1 : 0);
297 const char *stripped = targetm.strip_name_encoding (skipped);
298 if (*name != '*' && *user_label_prefix && *stripped != FASTCALL_PREFIX)
299 stripped = ACONCAT ((user_label_prefix, stripped, NULL));
300 return get_identifier (stripped);
301 }
302
303 void
304 mingw_pe_encode_section_info (tree decl, rtx rtl, int first)
305 {
306 rtx symbol;
307 int flags;
308
309 /* Do this last, due to our frobbing of DECL_DLLIMPORT_P above. */
310 default_encode_section_info (decl, rtl, first);
311
312 /* Careful not to prod global register variables. */
313 if (!MEM_P (rtl))
314 return;
315
316 symbol = XEXP (rtl, 0);
317 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
318
319 switch (TREE_CODE (decl))
320 {
321 case FUNCTION_DECL:
322 case VAR_DECL:
323 break;
324
325 default:
326 return;
327 }
328
329 /* Mark the decl so we can tell from the rtl whether the object is
330 dllexport'd or dllimport'd. tree.cc: merge_dllimport_decl_attributes
331 handles dllexport/dllimport override semantics. */
332 flags = (SYMBOL_REF_FLAGS (symbol) &
333 ~(SYMBOL_FLAG_DLLIMPORT | SYMBOL_FLAG_DLLEXPORT));
334 if (i386_pe_determine_dllexport_p (decl))
335 flags |= SYMBOL_FLAG_DLLEXPORT;
336 else if (i386_pe_determine_dllimport_p (decl))
337 flags |= SYMBOL_FLAG_DLLIMPORT;
338
339 SYMBOL_REF_FLAGS (symbol) = flags;
340 }
341
342
343 bool
344 i386_pe_binds_local_p (const_tree exp)
345 {
346 if ((VAR_P (exp) || TREE_CODE (exp) == FUNCTION_DECL)
347 && DECL_DLLIMPORT_P (exp))
348 return false;
349
350 /* External public symbols, which aren't weakref-s,
351 have local-binding for PE targets. */
352 if (DECL_P (exp)
353 && !lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
354 && TREE_PUBLIC (exp)
355 && DECL_EXTERNAL (exp))
356 return true;
357
358 #ifndef MAKE_DECL_ONE_ONLY
359 /* PR target/66655: If a function has been marked as DECL_ONE_ONLY
360 but we do not the means to make it so, then do not allow it to
361 bind locally. */
362 if (DECL_P (exp)
363 && TREE_CODE (exp) == FUNCTION_DECL
364 && TREE_PUBLIC (exp)
365 && DECL_ONE_ONLY (exp)
366 && ! DECL_EXTERNAL (exp)
367 && DECL_DECLARED_INLINE_P (exp))
368 return false;
369 #endif
370
371 return default_binds_local_p_1 (exp, 0);
372 }
373
374 /* Also strip the fastcall prefix and stdcall suffix. */
375
376 const char *
377 i386_pe_strip_name_encoding_full (const char *str)
378 {
379 const char *p;
380 const char *name = default_strip_name_encoding (str);
381
382 /* Strip leading '@' on fastcall symbols. */
383 if (*name == '@')
384 name++;
385
386 /* Strip trailing "@n". */
387 p = strchr (name, '@');
388 if (p)
389 return ggc_alloc_string (name, p - name);
390
391 return name;
392 }
393
394 void
395 mingw_pe_unique_section (tree decl, int reloc)
396 {
397 int len;
398 const char *name, *prefix;
399 char *string;
400
401 /* Ignore RELOC, if we are allowed to put relocated
402 const data into read-only section. */
403 if (!flag_writable_rel_rdata)
404 reloc = 0;
405 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
406 name = i386_pe_strip_name_encoding_full (name);
407
408 /* The object is put in, for example, section .text$foo.
409 The linker will then ultimately place them in .text
410 (everything from the $ on is stripped). Don't put
411 read-only data in .rdata section to avoid a PE linker
412 bug when .rdata$* grouped sections are used in code
413 without a .rdata section. */
414 if (TREE_CODE (decl) == FUNCTION_DECL)
415 prefix = ".text$";
416 else if (decl_readonly_section (decl, reloc))
417 prefix = ".rdata$";
418 else
419 prefix = ".data$";
420 len = strlen (name) + strlen (prefix);
421 string = XALLOCAVEC (char, len + 1);
422 sprintf (string, "%s%s", prefix, name);
423
424 set_decl_section_name (decl, string);
425 }
426
427 /* Local and global relocs can be placed always into readonly memory for
428 memory for PE-COFF targets. */
429 int
430 i386_pe_reloc_rw_mask (void)
431 {
432 return 0;
433 }
434
435 /* Select a set of attributes for section NAME based on the properties
436 of DECL and whether or not RELOC indicates that DECL's initializer
437 might contain runtime relocations.
438
439 We make the section read-only and executable for a function decl,
440 read-only for a const data decl, and writable for a non-const data decl.
441
442 If the section has already been defined, to not allow it to have
443 different attributes, as (1) this is ambiguous since we're not seeing
444 all the declarations up front and (2) some assemblers (e.g. SVR4)
445 do not recognize section redefinitions. */
446 /* ??? This differs from the "standard" PE implementation in that we
447 handle the SHARED variable attribute. Should this be done for all
448 PE targets? */
449
450 #define SECTION_PE_SHARED SECTION_MACH_DEP
451
452 unsigned int
453 mingw_pe_section_type_flags (tree decl, const char *, int reloc)
454 {
455 unsigned int flags;
456
457 /* Ignore RELOC, if we are allowed to put relocated
458 const data into read-only section. */
459 if (!flag_writable_rel_rdata)
460 reloc = 0;
461
462 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
463 flags = SECTION_CODE;
464 else if (decl && decl_readonly_section (decl, reloc))
465 flags = 0;
466 else
467 {
468 flags = SECTION_WRITE;
469
470 if (decl && VAR_P (decl)
471 && lookup_attribute ("shared", DECL_ATTRIBUTES (decl)))
472 flags |= SECTION_PE_SHARED;
473 }
474
475 if (decl && DECL_P (decl) && DECL_ONE_ONLY (decl))
476 flags |= SECTION_LINKONCE;
477
478 return flags;
479 }
480
481 void
482 mingw_pe_asm_named_section (const char *name, unsigned int flags,
483 tree decl)
484 {
485 char flagchars[8], *f = flagchars;
486
487 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
488 if ((flags & SECTION_EXCLUDE) != 0)
489 *f++ = 'e';
490 #endif
491
492 if ((flags & (SECTION_CODE | SECTION_WRITE)) == 0)
493 /* readonly data */
494 {
495 *f++ ='d'; /* This is necessary for older versions of gas. */
496 *f++ ='r';
497 }
498 else
499 {
500 if (flags & SECTION_CODE)
501 *f++ = 'x';
502 if (flags & SECTION_WRITE)
503 *f++ = 'w';
504 if (flags & SECTION_PE_SHARED)
505 *f++ = 's';
506 #if !defined (HAVE_GAS_SECTION_EXCLUDE) || HAVE_GAS_SECTION_EXCLUDE == 0
507 /* If attribute "e" isn't supported we mark this section as
508 never-load. */
509 if ((flags & SECTION_EXCLUDE) != 0)
510 *f++ = 'n';
511 #endif
512 }
513
514 /* LTO sections need 1-byte alignment to avoid confusing the
515 zlib decompression algorithm with trailing zero pad bytes. */
516 if (startswith (name, LTO_SECTION_NAME_PREFIX))
517 *f++ = '0';
518
519 *f = '\0';
520
521 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
522
523 if (flags & SECTION_LINKONCE)
524 {
525 /* Functions may have been compiled at various levels of
526 optimization so we can't use `same_size' here.
527 Instead, have the linker pick one, without warning.
528 If 'selectany' attribute has been specified, MS compiler
529 sets 'discard' characteristic, rather than telling linker
530 to warn of size or content mismatch, so do the same. */
531 bool discard = (flags & SECTION_CODE)
532 || (TREE_CODE (decl) != IDENTIFIER_NODE
533 && lookup_attribute ("selectany",
534 DECL_ATTRIBUTES (decl)));
535 fprintf (asm_out_file, "\t.linkonce %s\n",
536 (discard ? "discard" : "same_size"));
537 }
538 }
539
540 /* Beware, DECL may be NULL if compile_file() is emitting the LTO marker. */
541
542 void
543 i386_pe_asm_output_aligned_decl_common (FILE *stream, tree decl,
544 const char *name, HOST_WIDE_INT size,
545 HOST_WIDE_INT align)
546 {
547 HOST_WIDE_INT rounded;
548
549 /* Compute as in assemble_noswitch_variable, since we don't have
550 support for aligned common on older binutils. We must also
551 avoid emitting a common symbol of size zero, as this is the
552 overloaded representation that indicates an undefined external
553 symbol in the PE object file format. */
554 rounded = size ? size : 1;
555 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
556 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
557 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
558
559 mingw_pe_maybe_record_exported_symbol (decl, name, 1);
560
561 fprintf (stream, "\t.comm\t");
562 assemble_name (stream, name);
563 if (use_pe_aligned_common)
564 fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC ", %d\n",
565 size ? size : HOST_WIDE_INT_1,
566 exact_log2 (align) - exact_log2 (CHAR_BIT));
567 else
568 fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC "\t" ASM_COMMENT_START
569 " " HOST_WIDE_INT_PRINT_DEC "\n", rounded, size);
570 }
571
572 /* The Microsoft linker requires that every function be marked as
573 DT_FCN. When using gas on cygwin, we must emit appropriate .type
574 directives. */
575
576 #include "gsyms.h"
577
578 /* Mark a function or an object appropriately. This should only be called for
579 functions for which we are not emitting COFF debugging information.
580 FILE is the assembler output file, NAME is the name of the
581 function, and PUB is nonzero if the function is globally
582 visible. */
583
584 void
585 mingw_pe_declare_type (FILE *file, const char *name, bool pub, bool func)
586 {
587 fprintf (file, "\t.def\t");
588 assemble_name (file, name);
589 fprintf (file, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
590 pub ? (int) C_EXT : (int) C_STAT,
591 (int) (func ? DT_FCN : DT_NON) << N_BTSHFT);
592 }
593
594 /* Keep a list of external functions. */
595
596 struct GTY(()) extern_list
597 {
598 struct extern_list *next;
599 tree decl;
600 const char *name;
601 };
602
603 static GTY(()) struct extern_list *extern_head;
604
605 /* Assemble an external function reference. We need to keep a list of
606 these, so that we can output the function types at the end of the
607 assembly. We can't output the types now, because we might see a
608 definition of the function later on and emit debugging information
609 for it then. */
610
611 void
612 i386_pe_record_external_function (tree decl, const char *name)
613 {
614 struct extern_list *p;
615
616 p = ggc_alloc<extern_list> ();
617 p->next = extern_head;
618 p->decl = decl;
619 p->name = name;
620 extern_head = p;
621 }
622
623 /* Keep a list of exported symbols. */
624
625 struct GTY(()) export_list
626 {
627 struct export_list *next;
628 const char *name;
629 int is_data; /* used to type tag exported symbols. */
630 };
631
632 /* Keep a list of stub symbols. */
633
634 struct GTY(()) stub_list
635 {
636 struct stub_list *next;
637 const char *name;
638 bool is_weak_decl_needed;
639 };
640
641 static GTY(()) struct export_list *export_head;
642
643 static GTY(()) struct stub_list *stub_head;
644
645 /* Assemble an export symbol entry. We need to keep a list of
646 these, so that we can output the export list at the end of the
647 assembly. We used to output these export symbols in each function,
648 but that causes problems with GNU ld when the sections are
649 linkonce. Beware, DECL may be NULL if compile_file() is emitting
650 the LTO marker. */
651
652 void
653 mingw_pe_maybe_record_exported_symbol (tree decl, const char *name, int is_data)
654 {
655 rtx symbol;
656 struct export_list *p;
657
658 if (!decl)
659 return;
660
661 symbol = XEXP (DECL_RTL (decl), 0);
662 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
663 if (!SYMBOL_REF_DLLEXPORT_P (symbol))
664 return;
665
666 gcc_assert (TREE_PUBLIC (decl));
667
668 p = ggc_alloc<export_list> ();
669 p->next = export_head;
670 p->name = name;
671 p->is_data = is_data;
672 export_head = p;
673 }
674
675 void
676 mingw_pe_record_stub (const char *name, bool is_weak_decl_needed)
677 {
678 struct stub_list *p;
679
680 if (!name || *name == 0)
681 return;
682
683 p = stub_head;
684 while (p != NULL)
685 {
686 if (p->name[0] == *name
687 && !strcmp (p->name, name))
688 return;
689 p = p->next;
690 }
691
692 p = ggc_alloc<stub_list> ();
693 p->next = stub_head;
694 p->name = name;
695 p->is_weak_decl_needed = is_weak_decl_needed;
696 stub_head = p;
697 }
698
699
700 #ifdef CXX_WRAP_SPEC_LIST
701
702 /* Search for a function named TARGET in the list of library wrappers
703 we are using, returning a pointer to it if found or NULL if not.
704 This function might be called on quite a few symbols, and we only
705 have the list of names of wrapped functions available to us as a
706 spec string, so first time round we lazily initialise a hash table
707 to make things quicker. */
708
709 static const char *
710 i386_find_on_wrapper_list (const char *target)
711 {
712 static char first_time = 1;
713 static hash_table<nofree_string_hash> *wrappers;
714
715 if (first_time)
716 {
717 /* Beware that this is not a complicated parser, it assumes
718 that any sequence of non-whitespace beginning with an
719 underscore is one of the wrapped symbols. For now that's
720 adequate to distinguish symbols from spec substitutions
721 and command-line options. */
722 static char wrapper_list_buffer[] = CXX_WRAP_SPEC_LIST;
723 char *bufptr;
724 /* Breaks up the char array into separated strings
725 strings and enter them into the hash table. */
726 wrappers = new hash_table<nofree_string_hash> (8);
727 for (bufptr = wrapper_list_buffer; *bufptr; ++bufptr)
728 {
729 char *found = NULL;
730 if (ISSPACE (*bufptr))
731 continue;
732 if (*bufptr == '_')
733 found = bufptr;
734 while (*bufptr && !ISSPACE (*bufptr))
735 ++bufptr;
736 if (*bufptr)
737 *bufptr = 0;
738 if (found)
739 *wrappers->find_slot (found, INSERT) = found;
740 }
741 first_time = 0;
742 }
743
744 return wrappers->find (target);
745 }
746
747 #endif /* CXX_WRAP_SPEC_LIST */
748
749 /* This is called at the end of assembly. For each external function
750 which has not been defined, we output a declaration now. We also
751 output the .drectve section. */
752
753 void
754 mingw_pe_file_end (void)
755 {
756 struct extern_list *p;
757
758 for (p = extern_head; p != NULL; p = p->next)
759 {
760 tree decl;
761
762 decl = p->decl;
763
764 /* Positively ensure only one declaration for any given symbol. */
765 if (! TREE_ASM_WRITTEN (decl)
766 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
767 {
768 #ifdef CXX_WRAP_SPEC_LIST
769 /* To ensure the DLL that provides the corresponding real
770 functions is still loaded at runtime, we must reference
771 the real function so that an (unused) import is created. */
772 const char *realsym = i386_find_on_wrapper_list (p->name);
773 if (realsym)
774 mingw_pe_declare_type (asm_out_file,
775 concat ("__real_", realsym, NULL), TREE_PUBLIC (decl), 1);
776 #endif /* CXX_WRAP_SPEC_LIST */
777 TREE_ASM_WRITTEN (decl) = 1;
778 mingw_pe_declare_type (asm_out_file, p->name,
779 TREE_PUBLIC (decl), 1);
780 }
781 }
782
783 if (export_head)
784 {
785 struct export_list *q;
786 drectve_section ();
787 for (q = export_head; q != NULL; q = q->next)
788 {
789 fprintf (asm_out_file, "\t.ascii \" -export:\\\"%s\\\"%s\"\n",
790 default_strip_name_encoding (q->name),
791 (q->is_data ? ",data" : ""));
792 }
793 }
794
795 if (stub_head)
796 {
797 struct stub_list *q;
798
799 for (q = stub_head; q != NULL; q = q->next)
800 {
801 const char *name = q->name;
802 const char *oname;
803
804 if (name[0] == '*')
805 ++name;
806 oname = name;
807 if (name[0] == '.')
808 ++name;
809 if (!startswith (name, "refptr."))
810 continue;
811 name += 7;
812
813 if (q->is_weak_decl_needed)
814 {
815 #ifdef ASM_WEAKEN_LABEL
816 ASM_WEAKEN_LABEL (asm_out_file, name);
817 #endif
818 mingw_pe_declare_type (asm_out_file, name, 1, 1);
819 }
820
821 fprintf (asm_out_file, "\t.section\t.rdata$%s, \"dr\"\n"
822 "\t.p2align\t3, 0\n"
823 "\t.globl\t%s\n"
824 "\t.linkonce\tdiscard\n", oname, oname);
825 fprintf (asm_out_file, "%s:\n\t.quad\t%s\n", oname, name);
826 }
827 }
828 }
829
830 /* Kludge because of missing PE-COFF support for early LTO debug. */
831
832 static enum debug_info_levels saved_debug_info_level;
833
834 void
835 mingw_pe_asm_lto_start (void)
836 {
837 saved_debug_info_level = debug_info_level;
838 debug_info_level = DINFO_LEVEL_NONE;
839 }
840
841 void
842 mingw_pe_asm_lto_end (void)
843 {
844 debug_info_level = saved_debug_info_level;
845 }
846
847
848 /* x64 Structured Exception Handling unwind info. */
849
850 struct seh_frame_state
851 {
852 /* SEH records offsets relative to the lowest address of the fixed stack
853 allocation. If there is no frame pointer, these offsets are from the
854 stack pointer; if there is a frame pointer, these offsets are from the
855 value of the stack pointer when the frame pointer was established, i.e.
856 the frame pointer minus the offset in the .seh_setframe directive.
857
858 We do not distinguish these two cases, i.e. we consider that the offsets
859 are always relative to the "current" stack pointer. This means that we
860 need to perform the fixed stack allocation before establishing the frame
861 pointer whenever there are registers to be saved, and this is guaranteed
862 by the prologue provided that we force the frame pointer to point at or
863 below the lowest used register save area, see ix86_compute_frame_layout.
864
865 This tracks the current stack pointer offset from the CFA. */
866 HOST_WIDE_INT sp_offset;
867
868 /* The CFA is located at CFA_REG + CFA_OFFSET. */
869 HOST_WIDE_INT cfa_offset;
870 rtx cfa_reg;
871
872 /* The offset wrt the CFA where register N has been saved. */
873 HOST_WIDE_INT reg_offset[FIRST_PSEUDO_REGISTER];
874
875 /* True if we are past the end of the epilogue. */
876 bool after_prologue;
877
878 /* True if we are in the cold section. */
879 bool in_cold_section;
880 };
881
882 /* Set up data structures beginning output for SEH. */
883
884 void
885 i386_pe_seh_init (FILE *f)
886 {
887 struct seh_frame_state *seh;
888
889 if (!TARGET_SEH)
890 return;
891 if (cfun->is_thunk)
892 return;
893
894 /* We cannot support DRAP with SEH. We turned off support for it by
895 re-defining MAX_STACK_ALIGNMENT when SEH is enabled. */
896 gcc_assert (!stack_realign_drap);
897
898 seh = XCNEW (struct seh_frame_state);
899 cfun->machine->seh = seh;
900
901 seh->sp_offset = INCOMING_FRAME_SP_OFFSET;
902 seh->cfa_offset = INCOMING_FRAME_SP_OFFSET;
903 seh->cfa_reg = stack_pointer_rtx;
904
905 fputs ("\t.seh_proc\t", f);
906 assemble_name (f, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (cfun->decl)));
907 fputc ('\n', f);
908 }
909
910 /* Emit an assembler directive for the end of the prologue. */
911
912 void
913 i386_pe_seh_end_prologue (FILE *f)
914 {
915 if (!TARGET_SEH)
916 return;
917 if (cfun->is_thunk)
918 return;
919 cfun->machine->seh->after_prologue = true;
920 fputs ("\t.seh_endprologue\n", f);
921 }
922
923 /* Emit assembler directives to reconstruct the SEH state. */
924
925 void
926 i386_pe_seh_cold_init (FILE *f, const char *name)
927 {
928 struct seh_frame_state *seh;
929 HOST_WIDE_INT alloc_offset, offset;
930
931 if (!TARGET_SEH)
932 return;
933 if (cfun->is_thunk)
934 return;
935 seh = cfun->machine->seh;
936
937 fputs ("\t.seh_proc\t", f);
938 assemble_name (f, name);
939 fputc ('\n', f);
940
941 /* In the normal case, the frame pointer is near the bottom of the frame
942 so we can do the full stack allocation and set it afterwards. There
943 is an exception if the function overflows the SEH maximum frame size
944 or accesses prior frames so, in this case, we need to pre-allocate a
945 small chunk of stack before setting it. */
946 offset = seh->sp_offset - INCOMING_FRAME_SP_OFFSET;
947 if (offset < SEH_MAX_FRAME_SIZE && !crtl->accesses_prior_frames)
948 alloc_offset = seh->sp_offset;
949 else
950 alloc_offset = MIN (seh->cfa_offset + 240, seh->sp_offset);
951
952 offset = alloc_offset - INCOMING_FRAME_SP_OFFSET;
953 if (offset > 0)
954 fprintf (f, "\t.seh_stackalloc\t" HOST_WIDE_INT_PRINT_DEC "\n", offset);
955
956 for (int regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
957 if (seh->reg_offset[regno] > 0 && seh->reg_offset[regno] <= alloc_offset)
958 {
959 if (SSE_REGNO_P (regno))
960 fputs ("\t.seh_savexmm\t", f);
961 else if (GENERAL_REGNO_P (regno))
962 fputs ("\t.seh_savereg\t", f);
963 else
964 gcc_unreachable ();
965 print_reg (gen_rtx_REG (DImode, regno), 0, f);
966 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n",
967 alloc_offset - seh->reg_offset[regno]);
968 }
969
970 if (seh->cfa_reg != stack_pointer_rtx)
971 {
972 offset = alloc_offset - seh->cfa_offset;
973
974 gcc_assert ((offset & 15) == 0);
975 gcc_assert (IN_RANGE (offset, 0, 240));
976
977 fputs ("\t.seh_setframe\t", f);
978 print_reg (seh->cfa_reg, 0, f);
979 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n", offset);
980 }
981
982 if (alloc_offset != seh->sp_offset)
983 {
984 offset = seh->sp_offset - alloc_offset;
985 if (offset > 0 && offset < SEH_MAX_FRAME_SIZE)
986 fprintf (f, "\t.seh_stackalloc\t" HOST_WIDE_INT_PRINT_DEC "\n", offset);
987
988 for (int regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
989 if (seh->reg_offset[regno] > alloc_offset)
990 {
991 if (SSE_REGNO_P (regno))
992 fputs ("\t.seh_savexmm\t", f);
993 else if (GENERAL_REGNO_P (regno))
994 fputs ("\t.seh_savereg\t", f);
995 else
996 gcc_unreachable ();
997 print_reg (gen_rtx_REG (DImode, regno), 0, f);
998 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n",
999 seh->sp_offset - seh->reg_offset[regno]);
1000 }
1001 }
1002
1003 fputs ("\t.seh_endprologue\n", f);
1004 }
1005
1006 /* Emit an assembler directive for the end of the function. */
1007
1008 static void
1009 i386_pe_seh_fini (FILE *f, bool cold)
1010 {
1011 struct seh_frame_state *seh;
1012
1013 if (!TARGET_SEH)
1014 return;
1015 if (cfun->is_thunk)
1016 return;
1017 seh = cfun->machine->seh;
1018 if (cold != seh->in_cold_section)
1019 return;
1020 XDELETE (seh);
1021 cfun->machine->seh = NULL;
1022 fputs ("\t.seh_endproc\n", f);
1023 }
1024
1025 /* Emit an assembler directive to save REG via a PUSH. */
1026
1027 static void
1028 seh_emit_push (FILE *f, struct seh_frame_state *seh, rtx reg)
1029 {
1030 const unsigned int regno = REGNO (reg);
1031
1032 gcc_checking_assert (GENERAL_REGNO_P (regno));
1033
1034 seh->sp_offset += UNITS_PER_WORD;
1035 seh->reg_offset[regno] = seh->sp_offset;
1036 if (seh->cfa_reg == stack_pointer_rtx)
1037 seh->cfa_offset += UNITS_PER_WORD;
1038
1039 fputs ("\t.seh_pushreg\t", f);
1040 print_reg (reg, 0, f);
1041 fputc ('\n', f);
1042 }
1043
1044 /* Emit an assembler directive to save REG at CFA - CFA_OFFSET. */
1045
1046 static void
1047 seh_emit_save (FILE *f, struct seh_frame_state *seh,
1048 rtx reg, HOST_WIDE_INT cfa_offset)
1049 {
1050 const unsigned int regno = REGNO (reg);
1051 HOST_WIDE_INT offset;
1052
1053 seh->reg_offset[regno] = cfa_offset;
1054
1055 /* Negative save offsets are of course not supported, since that
1056 would be a store below the stack pointer and thus clobberable. */
1057 gcc_assert (seh->sp_offset >= cfa_offset);
1058 offset = seh->sp_offset - cfa_offset;
1059
1060 fputs ((SSE_REGNO_P (regno) ? "\t.seh_savexmm\t"
1061 : GENERAL_REGNO_P (regno) ? "\t.seh_savereg\t"
1062 : (gcc_unreachable (), "")), f);
1063 print_reg (reg, 0, f);
1064 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n", offset);
1065 }
1066
1067 /* Emit an assembler directive to adjust RSP by OFFSET. */
1068
1069 static void
1070 seh_emit_stackalloc (FILE *f, struct seh_frame_state *seh,
1071 HOST_WIDE_INT offset)
1072 {
1073 /* We're only concerned with prologue stack allocations, which all
1074 are subtractions from the stack pointer. */
1075 gcc_assert (offset < 0);
1076 offset = -offset;
1077
1078 if (seh->cfa_reg == stack_pointer_rtx)
1079 seh->cfa_offset += offset;
1080 seh->sp_offset += offset;
1081
1082 /* Do not output the stackalloc in that case (it won't work as there is no
1083 encoding for very large frame size). */
1084 if (offset < SEH_MAX_FRAME_SIZE)
1085 fprintf (f, "\t.seh_stackalloc\t" HOST_WIDE_INT_PRINT_DEC "\n", offset);
1086 }
1087
1088 /* Process REG_CFA_ADJUST_CFA for SEH. */
1089
1090 static void
1091 seh_cfa_adjust_cfa (FILE *f, struct seh_frame_state *seh, rtx pat)
1092 {
1093 rtx dest, src;
1094 HOST_WIDE_INT reg_offset = 0;
1095 unsigned int dest_regno;
1096
1097 dest = SET_DEST (pat);
1098 src = SET_SRC (pat);
1099
1100 if (GET_CODE (src) == PLUS)
1101 {
1102 reg_offset = INTVAL (XEXP (src, 1));
1103 src = XEXP (src, 0);
1104 }
1105 else if (GET_CODE (src) == MINUS)
1106 {
1107 reg_offset = -INTVAL (XEXP (src, 1));
1108 src = XEXP (src, 0);
1109 }
1110 gcc_assert (src == stack_pointer_rtx);
1111 gcc_assert (seh->cfa_reg == stack_pointer_rtx);
1112 dest_regno = REGNO (dest);
1113
1114 if (dest_regno == STACK_POINTER_REGNUM)
1115 seh_emit_stackalloc (f, seh, reg_offset);
1116 else if (dest_regno == HARD_FRAME_POINTER_REGNUM)
1117 {
1118 HOST_WIDE_INT offset;
1119
1120 seh->cfa_reg = dest;
1121 seh->cfa_offset -= reg_offset;
1122
1123 offset = seh->sp_offset - seh->cfa_offset;
1124
1125 gcc_assert ((offset & 15) == 0);
1126 gcc_assert (IN_RANGE (offset, 0, 240));
1127
1128 fputs ("\t.seh_setframe\t", f);
1129 print_reg (seh->cfa_reg, 0, f);
1130 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n", offset);
1131 }
1132 else
1133 gcc_unreachable ();
1134 }
1135
1136 /* Process REG_CFA_OFFSET for SEH. */
1137
1138 static void
1139 seh_cfa_offset (FILE *f, struct seh_frame_state *seh, rtx pat)
1140 {
1141 rtx dest, src;
1142 HOST_WIDE_INT reg_offset;
1143
1144 dest = SET_DEST (pat);
1145 src = SET_SRC (pat);
1146
1147 gcc_assert (MEM_P (dest));
1148 dest = XEXP (dest, 0);
1149 if (REG_P (dest))
1150 reg_offset = 0;
1151 else
1152 {
1153 gcc_assert (GET_CODE (dest) == PLUS);
1154 reg_offset = INTVAL (XEXP (dest, 1));
1155 dest = XEXP (dest, 0);
1156 }
1157 gcc_assert (dest == seh->cfa_reg);
1158
1159 seh_emit_save (f, seh, src, seh->cfa_offset - reg_offset);
1160 }
1161
1162 /* Process a FRAME_RELATED_EXPR for SEH. */
1163
1164 static void
1165 seh_frame_related_expr (FILE *f, struct seh_frame_state *seh, rtx pat)
1166 {
1167 rtx dest, src;
1168 HOST_WIDE_INT addend;
1169
1170 /* See the full loop in dwarf2out_frame_debug_expr. */
1171 if (GET_CODE (pat) == PARALLEL || GET_CODE (pat) == SEQUENCE)
1172 {
1173 int i, n = XVECLEN (pat, 0), pass, npass;
1174
1175 npass = (GET_CODE (pat) == PARALLEL ? 2 : 1);
1176 for (pass = 0; pass < npass; ++pass)
1177 for (i = 0; i < n; ++i)
1178 {
1179 rtx ele = XVECEXP (pat, 0, i);
1180
1181 if (GET_CODE (ele) != SET)
1182 continue;
1183 dest = SET_DEST (ele);
1184
1185 /* Process each member of the PARALLEL independently. The first
1186 member is always processed; others only if they are marked. */
1187 if (i == 0 || RTX_FRAME_RELATED_P (ele))
1188 {
1189 /* Evaluate all register saves in the first pass and all
1190 register updates in the second pass. */
1191 if ((MEM_P (dest) ^ pass) || npass == 1)
1192 seh_frame_related_expr (f, seh, ele);
1193 }
1194 }
1195 return;
1196 }
1197
1198 dest = SET_DEST (pat);
1199 src = SET_SRC (pat);
1200
1201 switch (GET_CODE (dest))
1202 {
1203 case REG:
1204 switch (GET_CODE (src))
1205 {
1206 case REG:
1207 /* REG = REG: This should be establishing a frame pointer. */
1208 gcc_assert (src == stack_pointer_rtx);
1209 gcc_assert (dest == hard_frame_pointer_rtx);
1210 seh_cfa_adjust_cfa (f, seh, pat);
1211 break;
1212
1213 case PLUS:
1214 addend = INTVAL (XEXP (src, 1));
1215 src = XEXP (src, 0);
1216 if (dest == hard_frame_pointer_rtx)
1217 seh_cfa_adjust_cfa (f, seh, pat);
1218 else if (dest == stack_pointer_rtx)
1219 {
1220 gcc_assert (src == stack_pointer_rtx);
1221 seh_emit_stackalloc (f, seh, addend);
1222 }
1223 else
1224 gcc_unreachable ();
1225 break;
1226
1227 default:
1228 gcc_unreachable ();
1229 }
1230 break;
1231
1232 case MEM:
1233 /* A save of some kind. */
1234 dest = XEXP (dest, 0);
1235 if (GET_CODE (dest) == PRE_DEC)
1236 {
1237 gcc_checking_assert (GET_MODE (src) == Pmode);
1238 gcc_checking_assert (REG_P (src));
1239 seh_emit_push (f, seh, src);
1240 }
1241 else
1242 seh_cfa_offset (f, seh, pat);
1243 break;
1244
1245 default:
1246 gcc_unreachable ();
1247 }
1248 }
1249
1250 /* This function looks at a single insn and emits any SEH directives
1251 required for unwind of this insn. */
1252
1253 void
1254 i386_pe_seh_unwind_emit (FILE *out_file, rtx_insn *insn)
1255 {
1256 rtx note, pat;
1257 bool handled_one = false;
1258 struct seh_frame_state *seh;
1259
1260 if (!TARGET_SEH)
1261 return;
1262
1263 seh = cfun->machine->seh;
1264 if (NOTE_P (insn) && NOTE_KIND (insn) == NOTE_INSN_SWITCH_TEXT_SECTIONS)
1265 {
1266 /* See ix86_output_call_insn/seh_fixup_eh_fallthru for the rationale. */
1267 rtx_insn *prev = prev_active_insn (insn);
1268 if (prev && (CALL_P (prev) || !insn_nothrow_p (prev)))
1269 fputs ("\tnop\n", out_file);
1270 fputs ("\t.seh_endproc\n", out_file);
1271 seh->in_cold_section = true;
1272 return;
1273 }
1274
1275 if (NOTE_P (insn) || !RTX_FRAME_RELATED_P (insn))
1276 return;
1277
1278 /* Skip RTX_FRAME_RELATED_P insns that are associated with the epilogue. */
1279 if (seh->after_prologue)
1280 return;
1281
1282 for (note = REG_NOTES (insn); note ; note = XEXP (note, 1))
1283 {
1284 switch (REG_NOTE_KIND (note))
1285 {
1286 case REG_FRAME_RELATED_EXPR:
1287 pat = XEXP (note, 0);
1288 goto found;
1289
1290 case REG_CFA_DEF_CFA:
1291 case REG_CFA_EXPRESSION:
1292 /* Only emitted with DRAP and aligned memory access using a
1293 realigned SP, both of which we disable. */
1294 gcc_unreachable ();
1295 break;
1296
1297 case REG_CFA_REGISTER:
1298 /* Only emitted in epilogues, which we skip. */
1299 gcc_unreachable ();
1300
1301 case REG_CFA_ADJUST_CFA:
1302 pat = XEXP (note, 0);
1303 if (pat == NULL)
1304 {
1305 pat = PATTERN (insn);
1306 if (GET_CODE (pat) == PARALLEL)
1307 pat = XVECEXP (pat, 0, 0);
1308 }
1309 seh_cfa_adjust_cfa (out_file, seh, pat);
1310 handled_one = true;
1311 break;
1312
1313 case REG_CFA_OFFSET:
1314 pat = XEXP (note, 0);
1315 if (pat == NULL)
1316 pat = single_set (insn);
1317 seh_cfa_offset (out_file, seh, pat);
1318 handled_one = true;
1319 break;
1320
1321 default:
1322 break;
1323 }
1324 }
1325 if (handled_one)
1326 return;
1327 pat = PATTERN (insn);
1328 found:
1329 seh_frame_related_expr (out_file, seh, pat);
1330 }
1331
1332 void
1333 i386_pe_seh_emit_except_personality (rtx personality)
1334 {
1335 int flags = 0;
1336
1337 if (!TARGET_SEH)
1338 return;
1339
1340 fputs ("\t.seh_handler\t", asm_out_file);
1341 output_addr_const (asm_out_file, personality);
1342
1343 #if 0
1344 /* ??? The current implementation of _GCC_specific_handler requires
1345 both except and unwind handling, regardless of which sorts the
1346 user-level function requires. */
1347 eh_region r;
1348 FOR_ALL_EH_REGION(r)
1349 {
1350 if (r->type == ERT_CLEANUP)
1351 flags |= 1;
1352 else
1353 flags |= 2;
1354 }
1355 #else
1356 flags = 3;
1357 #endif
1358
1359 if (flags & 1)
1360 fputs (", @unwind", asm_out_file);
1361 if (flags & 2)
1362 fputs (", @except", asm_out_file);
1363 fputc ('\n', asm_out_file);
1364 }
1365
1366 void
1367 i386_pe_seh_init_sections (void)
1368 {
1369 if (TARGET_SEH)
1370 exception_section = get_unnamed_section (0, output_section_asm_op,
1371 "\t.seh_handlerdata");
1372 }
1373
1374 void
1375 i386_pe_start_function (FILE *f, const char *name, tree decl)
1376 {
1377 mingw_pe_maybe_record_exported_symbol (decl, name, 0);
1378 mingw_pe_declare_type (f, name, TREE_PUBLIC (decl), 1);
1379 /* In case section was altered by debugging output. */
1380 if (decl != NULL_TREE)
1381 switch_to_section (function_section (decl));
1382 ASM_OUTPUT_FUNCTION_LABEL (f, name, decl);
1383 }
1384
1385 void
1386 i386_pe_end_function (FILE *f, const char *, tree)
1387 {
1388 i386_pe_seh_fini (f, false);
1389 }
1390
1391 void
1392 i386_pe_end_cold_function (FILE *f, const char *, tree)
1393 {
1394 i386_pe_seh_fini (f, true);
1395 }
1396
1397 #include "gt-winnt.h"