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