re PR target/55033 (PowerPC section type conflict error)
[official-gcc.git] / gcc / config / i386 / winnt.c
blobf0f972c56d9d07f83be683d9749619d940e2a1eb
1 /* Subroutines for insn-output.c for Windows NT.
2 Contributed by Douglas Rupp (drupp@cs.washington.edu)
3 Copyright (C) 1995-2013 Free Software Foundation, Inc.
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "output.h"
29 #include "tree.h"
30 #include "flags.h"
31 #include "tm_p.h"
32 #include "diagnostic-core.h"
33 #include "hashtab.h"
34 #include "langhooks.h"
35 #include "ggc.h"
36 #include "target.h"
37 #include "except.h"
38 #include "lto-streamer.h"
40 /* i386/PE specific attribute support.
42 i386/PE has two new attributes:
43 dllexport - for exporting a function/variable that will live in a dll
44 dllimport - for importing a function/variable from a dll
46 Microsoft allows multiple declspecs in one __declspec, separating
47 them with spaces. We do NOT support this. Instead, use __declspec
48 multiple times.
51 /* Handle a "shared" attribute;
52 arguments as in struct attribute_spec.handler. */
53 tree
54 ix86_handle_shared_attribute (tree *node, tree name,
55 tree args ATTRIBUTE_UNUSED,
56 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
58 if (TREE_CODE (*node) != VAR_DECL)
60 warning (OPT_Wattributes, "%qE attribute only applies to variables",
61 name);
62 *no_add_attrs = true;
65 return NULL_TREE;
68 /* Handle a "selectany" attribute;
69 arguments as in struct attribute_spec.handler. */
70 tree
71 ix86_handle_selectany_attribute (tree *node, tree name,
72 tree args ATTRIBUTE_UNUSED,
73 int flags ATTRIBUTE_UNUSED,
74 bool *no_add_attrs)
76 /* The attribute applies only to objects that are initialized and have
77 external linkage. However, we may not know about initialization
78 until the language frontend has processed the decl. We'll check for
79 initialization later in encode_section_info. */
80 if (TREE_CODE (*node) != VAR_DECL || !TREE_PUBLIC (*node))
82 error ("%qE attribute applies only to initialized variables"
83 " with external linkage", name);
84 *no_add_attrs = true;
87 return NULL_TREE;
91 /* Return the type that we should use to determine if DECL is
92 imported or exported. */
94 static tree
95 associated_type (tree decl)
97 return (DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
98 ? DECL_CONTEXT (decl) : NULL_TREE);
101 /* Return true if DECL should be a dllexport'd object. */
103 static bool
104 i386_pe_determine_dllexport_p (tree decl)
106 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
107 return false;
109 /* Don't export local clones of dllexports. */
110 if (!TREE_PUBLIC (decl))
111 return false;
113 if (lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
114 return true;
116 return false;
119 /* Return true if DECL should be a dllimport'd object. */
121 static bool
122 i386_pe_determine_dllimport_p (tree decl)
124 tree assoc;
126 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
127 return false;
129 if (DECL_DLLIMPORT_P (decl))
130 return true;
132 /* The DECL_DLLIMPORT_P flag was set for decls in the class definition
133 by targetm.cxx.adjust_class_at_definition. Check again to emit
134 error message if the class attribute has been overridden by an
135 out-of-class definition of static data. */
136 assoc = associated_type (decl);
137 if (assoc && lookup_attribute ("dllimport", TYPE_ATTRIBUTES (assoc))
138 && TREE_CODE (decl) == VAR_DECL
139 && TREE_STATIC (decl) && TREE_PUBLIC (decl)
140 && !DECL_EXTERNAL (decl)
141 /* vtable's are linkonce constants, so defining a vtable is not
142 an error as long as we don't try to import it too. */
143 && !DECL_VIRTUAL_P (decl))
144 error ("definition of static data member %q+D of "
145 "dllimport%'d class", decl);
147 return false;
150 /* Handle the -mno-fun-dllimport target switch. */
152 bool
153 i386_pe_valid_dllimport_attribute_p (const_tree decl)
155 if (TARGET_NOP_FUN_DLLIMPORT && TREE_CODE (decl) == FUNCTION_DECL)
156 return false;
157 return true;
160 /* Return string which is the function name, identified by ID, modified
161 with a suffix consisting of an atsign (@) followed by the number of
162 bytes of arguments. If ID is NULL use the DECL_NAME as base. If
163 FASTCALL is true, also add the FASTCALL_PREFIX.
164 Return NULL if no change required. */
166 static tree
167 gen_stdcall_or_fastcall_suffix (tree decl, tree id, bool fastcall)
169 HOST_WIDE_INT total = 0;
170 const char *old_str = IDENTIFIER_POINTER (id != NULL_TREE ? id : DECL_NAME (decl));
171 char *new_str, *p;
172 tree type = TREE_TYPE (DECL_ORIGIN (decl));
173 tree arg;
174 function_args_iterator args_iter;
176 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
178 if (prototype_p (type))
180 /* This attribute is ignored for variadic functions. */
181 if (stdarg_p (type))
182 return NULL_TREE;
184 /* Quit if we hit an incomplete type. Error is reported
185 by convert_arguments in c-typeck.c or cp/typeck.c. */
186 FOREACH_FUNCTION_ARGS(type, arg, args_iter)
188 HOST_WIDE_INT parm_size;
189 HOST_WIDE_INT parm_boundary_bytes = PARM_BOUNDARY / BITS_PER_UNIT;
191 if (! COMPLETE_TYPE_P (arg))
192 break;
194 parm_size = int_size_in_bytes (arg);
195 if (parm_size < 0)
196 break;
198 /* Must round up to include padding. This is done the same
199 way as in store_one_arg. */
200 parm_size = ((parm_size + parm_boundary_bytes - 1)
201 / parm_boundary_bytes * parm_boundary_bytes);
202 total += parm_size;
206 /* Assume max of 8 base 10 digits in the suffix. */
207 p = new_str = XALLOCAVEC (char, 1 + strlen (old_str) + 1 + 8 + 1);
208 if (fastcall)
209 *p++ = FASTCALL_PREFIX;
210 sprintf (p, "%s@" HOST_WIDE_INT_PRINT_DEC, old_str, total);
212 return get_identifier (new_str);
215 /* Maybe decorate and get a new identifier for the DECL of a stdcall or
216 fastcall function. The original identifier is supplied in ID. */
218 static tree
219 i386_pe_maybe_mangle_decl_assembler_name (tree decl, tree id)
221 tree new_id = NULL_TREE;
223 if (TREE_CODE (decl) == FUNCTION_DECL)
225 unsigned int ccvt = ix86_get_callcvt (TREE_TYPE (decl));
226 if ((ccvt & IX86_CALLCVT_STDCALL) != 0)
228 if (TARGET_RTD)
229 /* If we are using -mrtd emit undecorated symbol and let linker
230 do the proper resolving. */
231 return NULL_TREE;
232 new_id = gen_stdcall_or_fastcall_suffix (decl, id, false);
234 else if ((ccvt & IX86_CALLCVT_FASTCALL) != 0)
235 new_id = gen_stdcall_or_fastcall_suffix (decl, id, true);
238 return new_id;
241 /* Emit an assembler directive to set symbol for DECL visibility to
242 the visibility type VIS, which must not be VISIBILITY_DEFAULT.
243 As for PE there is no hidden support in gas, we just warn for
244 user-specified visibility attributes. */
246 void
247 i386_pe_assemble_visibility (tree decl,
248 int vis ATTRIBUTE_UNUSED)
250 if (!decl
251 || !lookup_attribute ("visibility", DECL_ATTRIBUTES (decl)))
252 return;
253 if (!DECL_ARTIFICIAL (decl))
254 warning (OPT_Wattributes, "visibility attribute not supported "
255 "in this configuration; ignored");
258 /* This is used as a target hook to modify the DECL_ASSEMBLER_NAME
259 in the language-independent default hook
260 langhooks,c:lhd_set_decl_assembler_name ()
261 and in cp/mangle,c:mangle_decl (). */
262 tree
263 i386_pe_mangle_decl_assembler_name (tree decl, tree id)
265 tree new_id = i386_pe_maybe_mangle_decl_assembler_name (decl, id);
267 return (new_id ? new_id : id);
270 /* This hook behaves the same as varasm.c/assemble_name(), but
271 generates the name into memory rather than outputting it to
272 a file stream. */
274 tree
275 i386_pe_mangle_assembler_name (const char *name ATTRIBUTE_UNUSED)
277 const char *skipped = name + (*name == '*' ? 1 : 0);
278 const char *stripped = targetm.strip_name_encoding (skipped);
279 if (*name != '*' && *user_label_prefix && *stripped != FASTCALL_PREFIX)
280 stripped = ACONCAT ((user_label_prefix, stripped, NULL));
281 return get_identifier (stripped);
284 void
285 i386_pe_encode_section_info (tree decl, rtx rtl, int first)
287 rtx symbol;
288 int flags;
290 /* Do this last, due to our frobbing of DECL_DLLIMPORT_P above. */
291 default_encode_section_info (decl, rtl, first);
293 /* Careful not to prod global register variables. */
294 if (!MEM_P (rtl))
295 return;
297 symbol = XEXP (rtl, 0);
298 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
300 switch (TREE_CODE (decl))
302 case FUNCTION_DECL:
303 /* FIXME: Imported stdcall names are not modified by the Ada frontend.
304 Check and decorate the RTL name now. */
305 if (strcmp (lang_hooks.name, "GNU Ada") == 0)
307 tree new_id;
308 tree old_id = DECL_ASSEMBLER_NAME (decl);
309 const char* asm_str = IDENTIFIER_POINTER (old_id);
310 /* Do not change the identifier if a verbatim asmspec
311 or if stdcall suffix already added. */
312 if (!(*asm_str == '*' || strchr (asm_str, '@'))
313 && (new_id = i386_pe_maybe_mangle_decl_assembler_name (decl,
314 old_id)))
315 XSTR (symbol, 0) = IDENTIFIER_POINTER (new_id);
317 break;
319 case VAR_DECL:
320 if (lookup_attribute ("selectany", DECL_ATTRIBUTES (decl)))
322 if (DECL_INITIAL (decl)
323 /* If an object is initialized with a ctor, the static
324 initialization and destruction code for it is present in
325 each unit defining the object. The code that calls the
326 ctor is protected by a link-once guard variable, so that
327 the object still has link-once semantics, */
328 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
329 make_decl_one_only (decl, DECL_ASSEMBLER_NAME (decl));
330 else
331 error ("%q+D:'selectany' attribute applies only to "
332 "initialized objects", decl);
334 break;
336 default:
337 return;
340 /* Mark the decl so we can tell from the rtl whether the object is
341 dllexport'd or dllimport'd. tree.c: merge_dllimport_decl_attributes
342 handles dllexport/dllimport override semantics. */
343 flags = (SYMBOL_REF_FLAGS (symbol) &
344 ~(SYMBOL_FLAG_DLLIMPORT | SYMBOL_FLAG_DLLEXPORT));
345 if (i386_pe_determine_dllexport_p (decl))
346 flags |= SYMBOL_FLAG_DLLEXPORT;
347 else if (i386_pe_determine_dllimport_p (decl))
348 flags |= SYMBOL_FLAG_DLLIMPORT;
350 SYMBOL_REF_FLAGS (symbol) = flags;
354 bool
355 i386_pe_binds_local_p (const_tree exp)
357 if ((TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == FUNCTION_DECL)
358 && DECL_DLLIMPORT_P (exp))
359 return false;
361 /* External public symbols, which aren't weakref-s,
362 have local-binding for PE targets. */
363 if (DECL_P (exp)
364 && !lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
365 && TREE_PUBLIC (exp)
366 && DECL_EXTERNAL (exp))
367 return true;
368 return default_binds_local_p_1 (exp, 0);
371 /* Also strip the fastcall prefix and stdcall suffix. */
373 const char *
374 i386_pe_strip_name_encoding_full (const char *str)
376 const char *p;
377 const char *name = default_strip_name_encoding (str);
379 /* Strip leading '@' on fastcall symbols. */
380 if (*name == '@')
381 name++;
383 /* Strip trailing "@n". */
384 p = strchr (name, '@');
385 if (p)
386 return ggc_alloc_string (name, p - name);
388 return name;
391 void
392 i386_pe_unique_section (tree decl, int reloc)
394 int len;
395 const char *name, *prefix;
396 char *string;
398 /* Ignore RELOC, if we are allowed to put relocated
399 const data into read-only section. */
400 if (!flag_writable_rel_rdata)
401 reloc = 0;
402 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
403 name = i386_pe_strip_name_encoding_full (name);
405 /* The object is put in, for example, section .text$foo.
406 The linker will then ultimately place them in .text
407 (everything from the $ on is stripped). Don't put
408 read-only data in .rdata section to avoid a PE linker
409 bug when .rdata$* grouped sections are used in code
410 without a .rdata section. */
411 if (TREE_CODE (decl) == FUNCTION_DECL)
412 prefix = ".text$";
413 else if (decl_readonly_section (decl, reloc))
414 prefix = ".rdata$";
415 else
416 prefix = ".data$";
417 len = strlen (name) + strlen (prefix);
418 string = XALLOCAVEC (char, len + 1);
419 sprintf (string, "%s%s", prefix, name);
421 DECL_SECTION_NAME (decl) = build_string (len, string);
424 /* Local and global relocs can be placed always into readonly memory for
425 memory for PE-COFF targets. */
427 i386_pe_reloc_rw_mask (void)
429 return 0;
432 /* Select a set of attributes for section NAME based on the properties
433 of DECL and whether or not RELOC indicates that DECL's initializer
434 might contain runtime relocations.
436 We make the section read-only and executable for a function decl,
437 read-only for a const data decl, and writable for a non-const data decl.
439 If the section has already been defined, to not allow it to have
440 different attributes, as (1) this is ambiguous since we're not seeing
441 all the declarations up front and (2) some assemblers (e.g. SVR4)
442 do not recognize section redefinitions. */
443 /* ??? This differs from the "standard" PE implementation in that we
444 handle the SHARED variable attribute. Should this be done for all
445 PE targets? */
447 #define SECTION_PE_SHARED SECTION_MACH_DEP
449 unsigned int
450 i386_pe_section_type_flags (tree decl, const char *name, int reloc)
452 static htab_t htab;
453 unsigned int flags;
454 unsigned int **slot;
456 /* Ignore RELOC, if we are allowed to put relocated
457 const data into read-only section. */
458 if (!flag_writable_rel_rdata)
459 reloc = 0;
460 /* The names we put in the hashtable will always be the unique
461 versions given to us by the stringtable, so we can just use
462 their addresses as the keys. */
463 if (!htab)
464 htab = htab_create (31, htab_hash_pointer, htab_eq_pointer, NULL);
466 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
467 flags = SECTION_CODE;
468 else if (decl && decl_readonly_section (decl, reloc))
469 flags = 0;
470 else
472 flags = SECTION_WRITE;
474 if (decl && TREE_CODE (decl) == VAR_DECL
475 && lookup_attribute ("shared", DECL_ATTRIBUTES (decl)))
476 flags |= SECTION_PE_SHARED;
479 if (decl && DECL_P (decl) && DECL_ONE_ONLY (decl))
480 flags |= SECTION_LINKONCE;
482 /* See if we already have an entry for this section. */
483 slot = (unsigned int **) htab_find_slot (htab, name, INSERT);
484 if (!*slot)
486 *slot = (unsigned int *) xmalloc (sizeof (unsigned int));
487 **slot = flags;
489 else
491 if (decl && **slot != flags)
492 error ("%q+D causes a section type conflict", decl);
495 return flags;
498 void
499 i386_pe_asm_named_section (const char *name, unsigned int flags,
500 tree decl)
502 char flagchars[8], *f = flagchars;
504 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
505 if ((flags & SECTION_EXCLUDE) != 0)
506 *f++ = 'e';
507 #endif
509 if ((flags & (SECTION_CODE | SECTION_WRITE)) == 0)
510 /* readonly data */
512 *f++ ='d'; /* This is necessary for older versions of gas. */
513 *f++ ='r';
515 else
517 if (flags & SECTION_CODE)
518 *f++ = 'x';
519 if (flags & SECTION_WRITE)
520 *f++ = 'w';
521 if (flags & SECTION_PE_SHARED)
522 *f++ = 's';
523 #if !defined (HAVE_GAS_SECTION_EXCLUDE) || HAVE_GAS_SECTION_EXCLUDE == 0
524 /* If attribute "e" isn't supported we mark this section as
525 never-load. */
526 if ((flags & SECTION_EXCLUDE) != 0)
527 *f++ = 'n';
528 #endif
531 /* LTO sections need 1-byte alignment to avoid confusing the
532 zlib decompression algorithm with trailing zero pad bytes. */
533 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
534 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
535 *f++ = '0';
537 *f = '\0';
539 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
541 if (flags & SECTION_LINKONCE)
543 /* Functions may have been compiled at various levels of
544 optimization so we can't use `same_size' here.
545 Instead, have the linker pick one, without warning.
546 If 'selectany' attribute has been specified, MS compiler
547 sets 'discard' characteristic, rather than telling linker
548 to warn of size or content mismatch, so do the same. */
549 bool discard = (flags & SECTION_CODE)
550 || lookup_attribute ("selectany",
551 DECL_ATTRIBUTES (decl));
552 fprintf (asm_out_file, "\t.linkonce %s\n",
553 (discard ? "discard" : "same_size"));
557 /* Beware, DECL may be NULL if compile_file() is emitting the LTO marker. */
559 void
560 i386_pe_asm_output_aligned_decl_common (FILE *stream, tree decl,
561 const char *name, HOST_WIDE_INT size,
562 HOST_WIDE_INT align ATTRIBUTE_UNUSED)
564 HOST_WIDE_INT rounded;
566 /* Compute as in assemble_noswitch_variable, since we don't have
567 support for aligned common on older binutils. We must also
568 avoid emitting a common symbol of size zero, as this is the
569 overloaded representation that indicates an undefined external
570 symbol in the PE object file format. */
571 rounded = size ? size : 1;
572 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
573 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
574 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
576 i386_pe_maybe_record_exported_symbol (decl, name, 1);
578 fprintf (stream, "\t.comm\t");
579 assemble_name (stream, name);
580 if (use_pe_aligned_common)
581 fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC ", %d\n",
582 size ? size : (HOST_WIDE_INT) 1,
583 exact_log2 (align) - exact_log2 (CHAR_BIT));
584 else
585 fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC "\t" ASM_COMMENT_START
586 " " HOST_WIDE_INT_PRINT_DEC "\n", rounded, size);
589 /* The Microsoft linker requires that every function be marked as
590 DT_FCN. When using gas on cygwin, we must emit appropriate .type
591 directives. */
593 #include "gsyms.h"
595 /* Mark a function appropriately. This should only be called for
596 functions for which we are not emitting COFF debugging information.
597 FILE is the assembler output file, NAME is the name of the
598 function, and PUB is nonzero if the function is globally
599 visible. */
601 void
602 i386_pe_declare_function_type (FILE *file, const char *name, int pub)
604 fprintf (file, "\t.def\t");
605 assemble_name (file, name);
606 fprintf (file, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
607 pub ? (int) C_EXT : (int) C_STAT,
608 (int) DT_FCN << N_BTSHFT);
611 /* Keep a list of external functions. */
613 struct GTY(()) extern_list
615 struct extern_list *next;
616 tree decl;
617 const char *name;
620 static GTY(()) struct extern_list *extern_head;
622 /* Assemble an external function reference. We need to keep a list of
623 these, so that we can output the function types at the end of the
624 assembly. We can't output the types now, because we might see a
625 definition of the function later on and emit debugging information
626 for it then. */
628 void
629 i386_pe_record_external_function (tree decl, const char *name)
631 struct extern_list *p;
633 p = ggc_alloc_extern_list ();
634 p->next = extern_head;
635 p->decl = decl;
636 p->name = name;
637 extern_head = p;
640 /* Keep a list of exported symbols. */
642 struct GTY(()) export_list
644 struct export_list *next;
645 const char *name;
646 int is_data; /* used to type tag exported symbols. */
649 /* Keep a list of stub symbols. */
651 struct GTY(()) stub_list
653 struct stub_list *next;
654 const char *name;
657 static GTY(()) struct export_list *export_head;
659 static GTY(()) struct stub_list *stub_head;
661 /* Assemble an export symbol entry. We need to keep a list of
662 these, so that we can output the export list at the end of the
663 assembly. We used to output these export symbols in each function,
664 but that causes problems with GNU ld when the sections are
665 linkonce. Beware, DECL may be NULL if compile_file() is emitting
666 the LTO marker. */
668 void
669 i386_pe_maybe_record_exported_symbol (tree decl, const char *name, int is_data)
671 rtx symbol;
672 struct export_list *p;
674 if (!decl)
675 return;
677 symbol = XEXP (DECL_RTL (decl), 0);
678 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
679 if (!SYMBOL_REF_DLLEXPORT_P (symbol))
680 return;
682 gcc_assert (TREE_PUBLIC (decl));
684 p = ggc_alloc_export_list ();
685 p->next = export_head;
686 p->name = name;
687 p->is_data = is_data;
688 export_head = p;
691 void
692 i386_pe_record_stub (const char *name)
694 struct stub_list *p;
696 if (!name || *name == 0)
697 return;
699 p = stub_head;
700 while (p != NULL)
702 if (p->name[0] == *name
703 && !strcmp (p->name, name))
704 return;
705 p = p->next;
708 p = ggc_alloc_stub_list ();
709 p->next = stub_head;
710 p->name = name;
711 stub_head = p;
715 #ifdef CXX_WRAP_SPEC_LIST
717 /* Hash table equality helper function. */
719 static int
720 wrapper_strcmp (const void *x, const void *y)
722 return !strcmp ((const char *) x, (const char *) y);
725 /* Search for a function named TARGET in the list of library wrappers
726 we are using, returning a pointer to it if found or NULL if not.
727 This function might be called on quite a few symbols, and we only
728 have the list of names of wrapped functions available to us as a
729 spec string, so first time round we lazily initialise a hash table
730 to make things quicker. */
732 static const char *
733 i386_find_on_wrapper_list (const char *target)
735 static char first_time = 1;
736 static htab_t wrappers;
738 if (first_time)
740 /* Beware that this is not a complicated parser, it assumes
741 that any sequence of non-whitespace beginning with an
742 underscore is one of the wrapped symbols. For now that's
743 adequate to distinguish symbols from spec substitutions
744 and command-line options. */
745 static char wrapper_list_buffer[] = CXX_WRAP_SPEC_LIST;
746 char *bufptr;
747 /* Breaks up the char array into separated strings
748 strings and enter them into the hash table. */
749 wrappers = htab_create_alloc (8, htab_hash_string, wrapper_strcmp,
750 0, xcalloc, free);
751 for (bufptr = wrapper_list_buffer; *bufptr; ++bufptr)
753 char *found = NULL;
754 if (ISSPACE (*bufptr))
755 continue;
756 if (*bufptr == '_')
757 found = bufptr;
758 while (*bufptr && !ISSPACE (*bufptr))
759 ++bufptr;
760 if (*bufptr)
761 *bufptr = 0;
762 if (found)
763 *htab_find_slot (wrappers, found, INSERT) = found;
765 first_time = 0;
768 return (const char *) htab_find (wrappers, target);
771 #endif /* CXX_WRAP_SPEC_LIST */
773 /* This is called at the end of assembly. For each external function
774 which has not been defined, we output a declaration now. We also
775 output the .drectve section. */
777 void
778 i386_pe_file_end (void)
780 struct extern_list *p;
782 for (p = extern_head; p != NULL; p = p->next)
784 tree decl;
786 decl = p->decl;
788 /* Positively ensure only one declaration for any given symbol. */
789 if (! TREE_ASM_WRITTEN (decl)
790 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
792 #ifdef CXX_WRAP_SPEC_LIST
793 /* To ensure the DLL that provides the corresponding real
794 functions is still loaded at runtime, we must reference
795 the real function so that an (unused) import is created. */
796 const char *realsym = i386_find_on_wrapper_list (p->name);
797 if (realsym)
798 i386_pe_declare_function_type (asm_out_file,
799 concat ("__real_", realsym, NULL), TREE_PUBLIC (decl));
800 #endif /* CXX_WRAP_SPEC_LIST */
801 TREE_ASM_WRITTEN (decl) = 1;
802 i386_pe_declare_function_type (asm_out_file, p->name,
803 TREE_PUBLIC (decl));
807 if (export_head)
809 struct export_list *q;
810 drectve_section ();
811 for (q = export_head; q != NULL; q = q->next)
813 fprintf (asm_out_file, "\t.ascii \" -export:\\\"%s\\\"%s\"\n",
814 default_strip_name_encoding (q->name),
815 (q->is_data ? ",data" : ""));
819 if (stub_head)
821 struct stub_list *q;
823 for (q = stub_head; q != NULL; q = q->next)
825 const char *name = q->name;
826 const char *oname;
828 if (name[0] == '*')
829 ++name;
830 oname = name;
831 if (name[0] == '.')
832 ++name;
833 if (strncmp (name, "refptr.", 7) != 0)
834 continue;
835 name += 7;
836 fprintf (asm_out_file, "\t.section\t.rdata$%s, \"dr\"\n"
837 "\t.globl\t%s\n"
838 "\t.linkonce\tdiscard\n", oname, oname);
839 fprintf (asm_out_file, "%s:\n\t.quad\t%s\n", oname, name);
845 /* x64 Structured Exception Handling unwind info. */
847 struct seh_frame_state
849 /* SEH records saves relative to the "current" stack pointer, whether
850 or not there's a frame pointer in place. This tracks the current
851 stack pointer offset from the CFA. */
852 HOST_WIDE_INT sp_offset;
854 /* The CFA is located at CFA_REG + CFA_OFFSET. */
855 HOST_WIDE_INT cfa_offset;
856 rtx cfa_reg;
859 /* Set up data structures beginning output for SEH. */
861 void
862 i386_pe_seh_init (FILE *f)
864 struct seh_frame_state *seh;
866 if (!TARGET_SEH)
867 return;
868 if (cfun->is_thunk)
869 return;
871 /* We cannot support DRAP with SEH. We turned off support for it by
872 re-defining MAX_STACK_ALIGNMENT when SEH is enabled. */
873 gcc_assert (!stack_realign_drap);
875 seh = XCNEW (struct seh_frame_state);
876 cfun->machine->seh = seh;
878 seh->sp_offset = INCOMING_FRAME_SP_OFFSET;
879 seh->cfa_offset = INCOMING_FRAME_SP_OFFSET;
880 seh->cfa_reg = stack_pointer_rtx;
882 fputs ("\t.seh_proc\t", f);
883 assemble_name (f, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (cfun->decl)));
884 fputc ('\n', f);
887 void
888 i386_pe_seh_end_prologue (FILE *f)
890 struct seh_frame_state *seh;
892 if (!TARGET_SEH)
893 return;
894 if (cfun->is_thunk)
895 return;
896 seh = cfun->machine->seh;
898 XDELETE (seh);
899 cfun->machine->seh = NULL;
901 fputs ("\t.seh_endprologue\n", f);
904 static void
905 i386_pe_seh_fini (FILE *f)
907 if (!TARGET_SEH)
908 return;
909 if (cfun->is_thunk)
910 return;
911 fputs ("\t.seh_endproc\n", f);
914 /* Emit an assembler directive to save REG via a PUSH. */
916 static void
917 seh_emit_push (FILE *f, struct seh_frame_state *seh, rtx reg)
919 unsigned int regno = REGNO (reg);
921 gcc_checking_assert (GENERAL_REGNO_P (regno));
923 seh->sp_offset += UNITS_PER_WORD;
924 if (seh->cfa_reg == stack_pointer_rtx)
925 seh->cfa_offset += UNITS_PER_WORD;
927 fputs ("\t.seh_pushreg\t", f);
928 print_reg (reg, 0, f);
929 fputc ('\n', f);
932 /* Emit an assembler directive to save REG at CFA - CFA_OFFSET. */
934 static void
935 seh_emit_save (FILE *f, struct seh_frame_state *seh,
936 rtx reg, HOST_WIDE_INT cfa_offset)
938 unsigned int regno = REGNO (reg);
939 HOST_WIDE_INT offset;
941 /* Negative save offsets are of course not supported, since that
942 would be a store below the stack pointer and thus clobberable. */
943 gcc_assert (seh->sp_offset >= cfa_offset);
944 offset = seh->sp_offset - cfa_offset;
946 fputs ((SSE_REGNO_P (regno) ? "\t.seh_savexmm\t"
947 : GENERAL_REGNO_P (regno) ? "\t.seh_savereg\t"
948 : (gcc_unreachable (), "")), f);
949 print_reg (reg, 0, f);
950 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n", offset);
953 /* Emit an assembler directive to adjust RSP by OFFSET. */
955 static void
956 seh_emit_stackalloc (FILE *f, struct seh_frame_state *seh,
957 HOST_WIDE_INT offset)
959 /* We're only concerned with prologue stack allocations, which all
960 are subtractions from the stack pointer. */
961 gcc_assert (offset < 0);
962 offset = -offset;
964 if (seh->cfa_reg == stack_pointer_rtx)
965 seh->cfa_offset += offset;
966 seh->sp_offset += offset;
968 /* Do not output the stackalloc in that case (it won't work as there is no
969 encoding for very large frame size). */
970 if (offset < SEH_MAX_FRAME_SIZE)
971 fprintf (f, "\t.seh_stackalloc\t" HOST_WIDE_INT_PRINT_DEC "\n", offset);
974 /* Process REG_CFA_ADJUST_CFA for SEH. */
976 static void
977 seh_cfa_adjust_cfa (FILE *f, struct seh_frame_state *seh, rtx pat)
979 rtx dest, src;
980 HOST_WIDE_INT reg_offset = 0;
981 unsigned int dest_regno;
983 dest = SET_DEST (pat);
984 src = SET_SRC (pat);
986 if (GET_CODE (src) == PLUS)
988 reg_offset = INTVAL (XEXP (src, 1));
989 src = XEXP (src, 0);
991 else if (GET_CODE (src) == MINUS)
993 reg_offset = -INTVAL (XEXP (src, 1));
994 src = XEXP (src, 0);
996 gcc_assert (src == stack_pointer_rtx);
997 gcc_assert (seh->cfa_reg == stack_pointer_rtx);
998 dest_regno = REGNO (dest);
1000 if (dest_regno == STACK_POINTER_REGNUM)
1001 seh_emit_stackalloc (f, seh, reg_offset);
1002 else if (dest_regno == HARD_FRAME_POINTER_REGNUM)
1004 HOST_WIDE_INT offset;
1006 seh->cfa_reg = dest;
1007 seh->cfa_offset -= reg_offset;
1009 offset = seh->sp_offset - seh->cfa_offset;
1011 gcc_assert ((offset & 15) == 0);
1012 gcc_assert (IN_RANGE (offset, 0, 240));
1014 fputs ("\t.seh_setframe\t", f);
1015 print_reg (seh->cfa_reg, 0, f);
1016 fprintf (f, ", " HOST_WIDE_INT_PRINT_DEC "\n", offset);
1018 else
1019 gcc_unreachable ();
1022 /* Process REG_CFA_OFFSET for SEH. */
1024 static void
1025 seh_cfa_offset (FILE *f, struct seh_frame_state *seh, rtx pat)
1027 rtx dest, src;
1028 HOST_WIDE_INT reg_offset;
1030 dest = SET_DEST (pat);
1031 src = SET_SRC (pat);
1033 gcc_assert (MEM_P (dest));
1034 dest = XEXP (dest, 0);
1035 if (REG_P (dest))
1036 reg_offset = 0;
1037 else
1039 gcc_assert (GET_CODE (dest) == PLUS);
1040 reg_offset = INTVAL (XEXP (dest, 1));
1041 dest = XEXP (dest, 0);
1043 gcc_assert (dest == seh->cfa_reg);
1045 seh_emit_save (f, seh, src, seh->cfa_offset - reg_offset);
1048 /* Process a FRAME_RELATED_EXPR for SEH. */
1050 static void
1051 seh_frame_related_expr (FILE *f, struct seh_frame_state *seh, rtx pat)
1053 rtx dest, src;
1054 HOST_WIDE_INT addend;
1056 /* See the full loop in dwarf2out_frame_debug_expr. */
1057 if (GET_CODE (pat) == PARALLEL || GET_CODE (pat) == SEQUENCE)
1059 int i, n = XVECLEN (pat, 0), pass, npass;
1061 npass = (GET_CODE (pat) == PARALLEL ? 2 : 1);
1062 for (pass = 0; pass < npass; ++pass)
1063 for (i = 0; i < n; ++i)
1065 rtx ele = XVECEXP (pat, 0, i);
1067 if (GET_CODE (ele) != SET)
1068 continue;
1069 dest = SET_DEST (ele);
1071 /* Process each member of the PARALLEL independently. The first
1072 member is always processed; others only if they are marked. */
1073 if (i == 0 || RTX_FRAME_RELATED_P (ele))
1075 /* Evaluate all register saves in the first pass and all
1076 register updates in the second pass. */
1077 if ((MEM_P (dest) ^ pass) || npass == 1)
1078 seh_frame_related_expr (f, seh, ele);
1081 return;
1084 dest = SET_DEST (pat);
1085 src = SET_SRC (pat);
1087 switch (GET_CODE (dest))
1089 case REG:
1090 switch (GET_CODE (src))
1092 case REG:
1093 /* REG = REG: This should be establishing a frame pointer. */
1094 gcc_assert (src == stack_pointer_rtx);
1095 gcc_assert (dest == hard_frame_pointer_rtx);
1096 seh_cfa_adjust_cfa (f, seh, pat);
1097 break;
1099 case PLUS:
1100 addend = INTVAL (XEXP (src, 1));
1101 src = XEXP (src, 0);
1102 if (dest == hard_frame_pointer_rtx)
1103 seh_cfa_adjust_cfa (f, seh, pat);
1104 else if (dest == stack_pointer_rtx)
1106 gcc_assert (src == stack_pointer_rtx);
1107 seh_emit_stackalloc (f, seh, addend);
1109 else
1110 gcc_unreachable ();
1111 break;
1113 default:
1114 gcc_unreachable ();
1116 break;
1118 case MEM:
1119 /* A save of some kind. */
1120 dest = XEXP (dest, 0);
1121 if (GET_CODE (dest) == PRE_DEC)
1123 gcc_checking_assert (GET_MODE (src) == Pmode);
1124 gcc_checking_assert (REG_P (src));
1125 seh_emit_push (f, seh, src);
1127 else
1128 seh_cfa_offset (f, seh, pat);
1129 break;
1131 default:
1132 gcc_unreachable ();
1136 /* This function looks at a single insn and emits any SEH directives
1137 required for unwind of this insn. */
1139 void
1140 i386_pe_seh_unwind_emit (FILE *asm_out_file, rtx insn)
1142 rtx note, pat;
1143 bool handled_one = false;
1144 struct seh_frame_state *seh;
1146 if (!TARGET_SEH)
1147 return;
1149 /* We free the SEH data once done with the prologue. Ignore those
1150 RTX_FRAME_RELATED_P insns that are associated with the epilogue. */
1151 seh = cfun->machine->seh;
1152 if (seh == NULL)
1153 return;
1155 if (NOTE_P (insn) || !RTX_FRAME_RELATED_P (insn))
1156 return;
1158 for (note = REG_NOTES (insn); note ; note = XEXP (note, 1))
1160 pat = XEXP (note, 0);
1161 switch (REG_NOTE_KIND (note))
1163 case REG_FRAME_RELATED_EXPR:
1164 goto found;
1166 case REG_CFA_DEF_CFA:
1167 case REG_CFA_EXPRESSION:
1168 /* Only emitted with DRAP, which we disable. */
1169 gcc_unreachable ();
1170 break;
1172 case REG_CFA_REGISTER:
1173 /* Only emitted in epilogues, which we skip. */
1174 gcc_unreachable ();
1176 case REG_CFA_ADJUST_CFA:
1177 if (pat == NULL)
1179 pat = PATTERN (insn);
1180 if (GET_CODE (pat) == PARALLEL)
1181 pat = XVECEXP (pat, 0, 0);
1183 seh_cfa_adjust_cfa (asm_out_file, seh, pat);
1184 handled_one = true;
1185 break;
1187 case REG_CFA_OFFSET:
1188 if (pat == NULL)
1189 pat = single_set (insn);
1190 seh_cfa_offset (asm_out_file, seh, pat);
1191 handled_one = true;
1192 break;
1194 default:
1195 break;
1198 if (handled_one)
1199 return;
1200 pat = PATTERN (insn);
1201 found:
1202 seh_frame_related_expr (asm_out_file, seh, pat);
1205 void
1206 i386_pe_seh_emit_except_personality (rtx personality)
1208 int flags = 0;
1210 if (!TARGET_SEH)
1211 return;
1213 fputs ("\t.seh_handler\t", asm_out_file);
1214 output_addr_const (asm_out_file, personality);
1216 #if 0
1217 /* ??? The current implementation of _GCC_specific_handler requires
1218 both except and unwind handling, regardless of which sorts the
1219 user-level function requires. */
1220 eh_region r;
1221 FOR_ALL_EH_REGION(r)
1223 if (r->type == ERT_CLEANUP)
1224 flags |= 1;
1225 else
1226 flags |= 2;
1228 #else
1229 flags = 3;
1230 #endif
1232 if (flags & 1)
1233 fputs (", @unwind", asm_out_file);
1234 if (flags & 2)
1235 fputs (", @except", asm_out_file);
1236 fputc ('\n', asm_out_file);
1239 void
1240 i386_pe_seh_init_sections (void)
1242 if (TARGET_SEH)
1243 exception_section = get_unnamed_section (0, output_section_asm_op,
1244 "\t.seh_handlerdata");
1247 void
1248 i386_pe_start_function (FILE *f, const char *name, tree decl)
1250 i386_pe_maybe_record_exported_symbol (decl, name, 0);
1251 if (write_symbols != SDB_DEBUG)
1252 i386_pe_declare_function_type (f, name, TREE_PUBLIC (decl));
1253 /* In case section was altered by debugging output. */
1254 if (decl != NULL_TREE)
1255 switch_to_section (function_section (decl));
1256 ASM_OUTPUT_FUNCTION_LABEL (f, name, decl);
1259 void
1260 i386_pe_end_function (FILE *f, const char *name ATTRIBUTE_UNUSED,
1261 tree decl ATTRIBUTE_UNUSED)
1263 i386_pe_seh_fini (f);
1267 #include "gt-winnt.h"