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
4 Free Software Foundation, Inc.
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
25 #include "coretypes.h"
29 #include "hard-reg-set.h"
38 /* i386/PE specific attribute support.
40 i386/PE has two new attributes:
41 dllexport - for exporting a function/variable that will live in a dll
42 dllimport - for importing a function/variable from a dll
44 Microsoft allows multiple declspecs in one __declspec, separating
45 them with spaces. We do NOT support this. Instead, use __declspec
49 static tree associated_type
PARAMS ((tree
));
50 const char * gen_stdcall_suffix
PARAMS ((tree
));
51 const char * gen_fastcall_suffix
PARAMS ((tree
));
52 int i386_pe_dllexport_p
PARAMS ((tree
));
53 int i386_pe_dllimport_p
PARAMS ((tree
));
54 void i386_pe_mark_dllexport
PARAMS ((tree
));
55 void i386_pe_mark_dllimport
PARAMS ((tree
));
57 /* This is we how mark internal identifiers with dllimport or dllexport
59 #ifndef DLL_IMPORT_PREFIX
60 #define DLL_IMPORT_PREFIX "#i."
62 #ifndef DLL_EXPORT_PREFIX
63 #define DLL_EXPORT_PREFIX "#e."
66 /* Handle a "dllimport" or "dllexport" attribute;
67 arguments as in struct attribute_spec.handler. */
69 ix86_handle_dll_attribute (node
, name
, args
, flags
, no_add_attrs
)
76 /* These attributes may apply to structure and union types being created,
77 but otherwise should pass to the declaration involved. */
80 if (flags
& ((int) ATTR_FLAG_DECL_NEXT
| (int) ATTR_FLAG_FUNCTION_NEXT
81 | (int) ATTR_FLAG_ARRAY_NEXT
))
84 return tree_cons (name
, args
, NULL_TREE
);
86 if (TREE_CODE (*node
) != RECORD_TYPE
&& TREE_CODE (*node
) != UNION_TYPE
)
88 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
93 /* `extern' needn't be specified with dllimport.
94 Specify `extern' now and hope for the best. Sigh. */
95 else if (TREE_CODE (*node
) == VAR_DECL
96 && is_attribute_p ("dllimport", name
))
98 DECL_EXTERNAL (*node
) = 1;
99 TREE_PUBLIC (*node
) = 1;
105 /* Handle a "shared" attribute;
106 arguments as in struct attribute_spec.handler. */
108 ix86_handle_shared_attribute (node
, name
, args
, flags
, no_add_attrs
)
111 tree args ATTRIBUTE_UNUSED
;
112 int flags ATTRIBUTE_UNUSED
;
115 if (TREE_CODE (*node
) != VAR_DECL
)
117 warning ("`%s' attribute only applies to variables",
118 IDENTIFIER_POINTER (name
));
119 *no_add_attrs
= true;
125 /* Return the type that we should use to determine if DECL is
126 imported or exported. */
129 associated_type (decl
)
134 /* In the C++ frontend, DECL_CONTEXT for a method doesn't actually refer
135 to the containing class. So we look at the 'this' arg. */
136 if (TREE_CODE (TREE_TYPE (decl
)) == METHOD_TYPE
)
138 /* Artificial methods are not affected by the import/export status of
139 their class unless they are virtual. */
140 if (! DECL_ARTIFICIAL (decl
) || DECL_VINDEX (decl
))
141 t
= TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl
))));
143 else if (DECL_CONTEXT (decl
)
144 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (decl
))) == 't')
145 t
= DECL_CONTEXT (decl
);
150 /* Return nonzero if DECL is a dllexport'd object. */
153 i386_pe_dllexport_p (decl
)
158 if (TREE_CODE (decl
) != VAR_DECL
159 && TREE_CODE (decl
) != FUNCTION_DECL
)
161 exp
= lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl
));
165 /* Class members get the dllexport status of their class. */
166 if (associated_type (decl
))
168 exp
= lookup_attribute ("dllexport",
169 TYPE_ATTRIBUTES (associated_type (decl
)));
177 /* Return nonzero if DECL is a dllimport'd object. */
180 i386_pe_dllimport_p (decl
)
185 if (TREE_CODE (decl
) == FUNCTION_DECL
186 && TARGET_NOP_FUN_DLLIMPORT
)
189 if (TREE_CODE (decl
) != VAR_DECL
190 && TREE_CODE (decl
) != FUNCTION_DECL
)
192 imp
= lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl
));
196 /* Class members get the dllimport status of their class. */
197 if (associated_type (decl
))
199 imp
= lookup_attribute ("dllimport",
200 TYPE_ATTRIBUTES (associated_type (decl
)));
208 /* Return nonzero if SYMBOL is marked as being dllexport'd. */
211 i386_pe_dllexport_name_p (symbol
)
214 return (strncmp (DLL_EXPORT_PREFIX
, symbol
,
215 strlen (DLL_EXPORT_PREFIX
)) == 0);
218 /* Return nonzero if SYMBOL is marked as being dllimport'd. */
221 i386_pe_dllimport_name_p (symbol
)
224 return (strncmp (DLL_IMPORT_PREFIX
, symbol
,
225 strlen (DLL_IMPORT_PREFIX
)) == 0);
228 /* Mark a DECL as being dllexport'd.
229 Note that we override the previous setting (eg: dllimport). */
232 i386_pe_mark_dllexport (decl
)
240 rtlname
= XEXP (DECL_RTL (decl
), 0);
241 if (GET_CODE (rtlname
) == SYMBOL_REF
)
242 oldname
= XSTR (rtlname
, 0);
243 else if (GET_CODE (rtlname
) == MEM
244 && GET_CODE (XEXP (rtlname
, 0)) == SYMBOL_REF
)
245 oldname
= XSTR (XEXP (rtlname
, 0), 0);
248 if (i386_pe_dllimport_name_p (oldname
))
249 /* Remove DLL_IMPORT_PREFIX. */
250 oldname
+= strlen (DLL_IMPORT_PREFIX
);
251 else if (i386_pe_dllexport_name_p (oldname
))
252 return; /* already done */
254 newname
= alloca (strlen (DLL_EXPORT_PREFIX
) + strlen (oldname
) + 1);
255 sprintf (newname
, "%s%s", DLL_EXPORT_PREFIX
, oldname
);
257 /* We pass newname through get_identifier to ensure it has a unique
258 address. RTL processing can sometimes peek inside the symbol ref
259 and compare the string's addresses to see if two symbols are
261 idp
= get_identifier (newname
);
263 XEXP (DECL_RTL (decl
), 0) =
264 gen_rtx (SYMBOL_REF
, Pmode
, IDENTIFIER_POINTER (idp
));
267 /* Mark a DECL as being dllimport'd. */
270 i386_pe_mark_dllimport (decl
)
278 rtlname
= XEXP (DECL_RTL (decl
), 0);
279 if (GET_CODE (rtlname
) == SYMBOL_REF
)
280 oldname
= XSTR (rtlname
, 0);
281 else if (GET_CODE (rtlname
) == MEM
282 && GET_CODE (XEXP (rtlname
, 0)) == SYMBOL_REF
)
283 oldname
= XSTR (XEXP (rtlname
, 0), 0);
286 if (i386_pe_dllexport_name_p (oldname
))
288 error ("`%s' declared as both exported to and imported from a DLL",
289 IDENTIFIER_POINTER (DECL_NAME (decl
)));
292 else if (i386_pe_dllimport_name_p (oldname
))
294 /* Already done, but force correct linkage since the redeclaration
295 might have omitted explicit extern. Sigh. */
296 if (TREE_CODE (decl
) == VAR_DECL
297 /* ??? Is this test for vtables needed? */
298 && !DECL_VIRTUAL_P (decl
))
300 DECL_EXTERNAL (decl
) = 1;
301 TREE_PUBLIC (decl
) = 1;
306 /* ??? One can well ask why we're making these checks here,
307 and that would be a good question. */
309 /* Imported variables can't be initialized. Note that C++ classes
310 are marked initial, so we need to check. */
311 if (TREE_CODE (decl
) == VAR_DECL
312 && !DECL_VIRTUAL_P (decl
)
313 && (DECL_INITIAL (decl
)
314 && ! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl
))))
316 error_with_decl (decl
, "initialized variable `%s' is marked dllimport");
319 /* Nor can they be static. */
320 if (TREE_CODE (decl
) == VAR_DECL
321 /* ??? Is this test for vtables needed? */
322 && !DECL_VIRTUAL_P (decl
)
325 error_with_decl (decl
, "static variable `%s' is marked dllimport");
329 newname
= alloca (strlen (DLL_IMPORT_PREFIX
) + strlen (oldname
) + 1);
330 sprintf (newname
, "%s%s", DLL_IMPORT_PREFIX
, oldname
);
332 /* We pass newname through get_identifier to ensure it has a unique
333 address. RTL processing can sometimes peek inside the symbol ref
334 and compare the string's addresses to see if two symbols are
336 idp
= get_identifier (newname
);
338 XEXP (DECL_RTL (decl
), 0)
339 = gen_rtx_SYMBOL_REF (Pmode
, IDENTIFIER_POINTER (idp
));
341 /* Can't treat a pointer to this as a constant address */
342 DECL_NON_ADDR_CONST_P (decl
) = 1;
345 /* Return string which is the former assembler name modified with a
346 prefix consisting of FASTCALL_PREFIX and a suffix consisting of an
347 atsign (@) followed by the number of bytes of arguments. */
350 gen_fastcall_suffix (decl
)
355 const char *asmname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
358 if (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
359 if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (decl
))))
362 tree formal_type
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
364 while (TREE_VALUE (formal_type
) != void_type_node
)
367 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_VALUE (formal_type
)));
368 /* Must round up to include padding. This is done the same
369 way as in store_one_arg. */
370 parm_size
= ((parm_size
+ PARM_BOUNDARY
- 1)
371 / PARM_BOUNDARY
* PARM_BOUNDARY
);
373 formal_type
= TREE_CHAIN (formal_type
);
377 /* Assume max of 8 base 10 digits in the suffix. */
378 newsym
= xmalloc (1 + strlen (asmname
) + 1 + 8 + 1);
379 sprintf (newsym
, "%c%s@%d", FASTCALL_PREFIX
, asmname
, total
/BITS_PER_UNIT
);
380 return IDENTIFIER_POINTER (get_identifier (newsym
));
383 /* Return string which is the former assembler name modified with a
384 suffix consisting of an atsign (@) followed by the number of bytes of
388 gen_stdcall_suffix (decl
)
392 /* ??? This probably should use XSTR (XEXP (DECL_RTL (decl), 0), 0) instead
393 of DECL_ASSEMBLER_NAME. */
394 const char *asmname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
397 if (TYPE_ARG_TYPES (TREE_TYPE (decl
)))
398 if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (decl
))))
401 tree formal_type
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
403 while (TREE_VALUE (formal_type
) != void_type_node
)
406 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_VALUE (formal_type
)));
407 /* Must round up to include padding. This is done the same
408 way as in store_one_arg. */
409 parm_size
= ((parm_size
+ PARM_BOUNDARY
- 1)
410 / PARM_BOUNDARY
* PARM_BOUNDARY
);
412 formal_type
= TREE_CHAIN (formal_type
);
416 /* Assume max of 8 base 10 digits in the suffix. */
417 newsym
= xmalloc (strlen (asmname
) + 1 + 8 + 1);
418 sprintf (newsym
, "%s@%d", asmname
, total
/BITS_PER_UNIT
);
419 return IDENTIFIER_POINTER (get_identifier (newsym
));
423 i386_pe_encode_section_info (decl
, rtl
, first
)
431 default_encode_section_info (decl
, rtl
, first
);
433 if (TREE_CODE (decl
) == FUNCTION_DECL
)
435 if (lookup_attribute ("stdcall",
436 TYPE_ATTRIBUTES (TREE_TYPE (decl
))))
437 XEXP (DECL_RTL (decl
), 0) =
438 gen_rtx (SYMBOL_REF
, Pmode
, gen_stdcall_suffix (decl
));
439 else if (lookup_attribute ("fastcall",
440 TYPE_ATTRIBUTES (TREE_TYPE (decl
))))
441 XEXP (DECL_RTL (decl
), 0) =
442 gen_rtx (SYMBOL_REF
, Pmode
, gen_fastcall_suffix (decl
));
445 /* Mark the decl so we can tell from the rtl whether the object is
446 dllexport'd or dllimport'd. */
448 if (i386_pe_dllexport_p (decl
))
449 i386_pe_mark_dllexport (decl
);
450 else if (i386_pe_dllimport_p (decl
))
451 i386_pe_mark_dllimport (decl
);
452 /* It might be that DECL has already been marked as dllimport, but a
453 subsequent definition nullified that. The attribute is gone but
454 DECL_RTL still has (DLL_IMPORT_PREFIX) prefixed. We need to remove
455 that. Ditto for the DECL_NON_ADDR_CONST_P flag. */
456 else if ((TREE_CODE (decl
) == FUNCTION_DECL
457 || TREE_CODE (decl
) == VAR_DECL
)
458 && DECL_RTL (decl
) != NULL_RTX
459 && GET_CODE (DECL_RTL (decl
)) == MEM
460 && GET_CODE (XEXP (DECL_RTL (decl
), 0)) == MEM
461 && GET_CODE (XEXP (XEXP (DECL_RTL (decl
), 0), 0)) == SYMBOL_REF
462 && i386_pe_dllimport_name_p (XSTR (XEXP (XEXP (DECL_RTL (decl
), 0), 0), 0)))
464 const char *oldname
= XSTR (XEXP (XEXP (DECL_RTL (decl
), 0), 0), 0);
465 /* Remove DLL_IMPORT_PREFIX. */
466 tree idp
= get_identifier (oldname
+ strlen (DLL_IMPORT_PREFIX
));
467 rtx newrtl
= gen_rtx (SYMBOL_REF
, Pmode
, IDENTIFIER_POINTER (idp
));
469 XEXP (DECL_RTL (decl
), 0) = newrtl
;
471 DECL_NON_ADDR_CONST_P (decl
) = 0;
473 /* We previously set TREE_PUBLIC and DECL_EXTERNAL.
474 We leave these alone for now. */
478 /* Strip only the leading encoding, leaving the stdcall suffix and fastcall
479 prefix if it exists. */
482 i386_pe_strip_name_encoding (str
)
485 if (strncmp (str
, DLL_IMPORT_PREFIX
, strlen (DLL_IMPORT_PREFIX
))
487 str
+= strlen (DLL_IMPORT_PREFIX
);
488 else if (strncmp (str
, DLL_EXPORT_PREFIX
, strlen (DLL_EXPORT_PREFIX
))
490 str
+= strlen (DLL_EXPORT_PREFIX
);
496 /* Also strip the stdcall suffix. */
499 i386_pe_strip_name_encoding_full (str
)
503 const char *name
= i386_pe_strip_name_encoding (str
);
505 p
= strchr (name
, '@');
507 return ggc_alloc_string (name
, p
- name
);
512 /* Output a reference to a label. Fastcall symbols are prefixed with @,
513 whereas symbols for functions using other calling conventions don't
514 have a prefix (unless they are marked dllimport or dllexport). */
516 void i386_pe_output_labelref (stream
, name
)
520 if (strncmp (name
, DLL_IMPORT_PREFIX
, strlen (DLL_IMPORT_PREFIX
))
524 if (name
[strlen (DLL_IMPORT_PREFIX
)] == FASTCALL_PREFIX
)
525 /* A dllimport fastcall symbol. */
527 fprintf (stream
, "__imp_%s",
528 i386_pe_strip_name_encoding (name
));
531 /* A dllimport non-fastcall symbol. */
533 fprintf (stream
, "__imp__%s",
534 i386_pe_strip_name_encoding (name
));
537 else if ((name
[0] == FASTCALL_PREFIX
)
538 || (strncmp (name
, DLL_EXPORT_PREFIX
, strlen (DLL_EXPORT_PREFIX
)
540 && name
[strlen (DLL_EXPORT_PREFIX
)] == FASTCALL_PREFIX
)))
541 /* A fastcall symbol. */
543 fprintf (stream
, "%s",
544 i386_pe_strip_name_encoding (name
));
547 /* Everything else. */
549 fprintf (stream
, "%s%s", USER_LABEL_PREFIX
,
550 i386_pe_strip_name_encoding (name
));
555 i386_pe_unique_section (decl
, reloc
)
560 const char *name
, *prefix
;
563 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
564 name
= i386_pe_strip_name_encoding_full (name
);
566 /* The object is put in, for example, section .text$foo.
567 The linker will then ultimately place them in .text
568 (everything from the $ on is stripped). Don't put
569 read-only data in .rdata section to avoid a PE linker
570 bug when .rdata$* grouped sections are used in code
571 without a .rdata section. */
572 if (TREE_CODE (decl
) == FUNCTION_DECL
)
574 else if (decl_readonly_section (decl
, reloc
))
578 len
= strlen (name
) + strlen (prefix
);
579 string
= alloca (len
+ 1);
580 sprintf (string
, "%s%s", prefix
, name
);
582 DECL_SECTION_NAME (decl
) = build_string (len
, string
);
585 /* Select a set of attributes for section NAME based on the properties
586 of DECL and whether or not RELOC indicates that DECL's initializer
587 might contain runtime relocations.
589 We make the section read-only and executable for a function decl,
590 read-only for a const data decl, and writable for a non-const data decl.
592 If the section has already been defined, to not allow it to have
593 different attributes, as (1) this is ambiguous since we're not seeing
594 all the declarations up front and (2) some assemblers (e.g. SVR4)
595 do not recognize section redefinitions. */
596 /* ??? This differs from the "standard" PE implementation in that we
597 handle the SHARED variable attribute. Should this be done for all
600 #define SECTION_PE_SHARED SECTION_MACH_DEP
603 i386_pe_section_type_flags (decl
, name
, reloc
)
612 /* The names we put in the hashtable will always be the unique
613 versions gived to us by the stringtable, so we can just use
614 their addresses as the keys. */
616 htab
= htab_create (31, htab_hash_pointer
, htab_eq_pointer
, NULL
);
618 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
619 flags
= SECTION_CODE
;
620 else if (decl
&& decl_readonly_section (decl
, reloc
))
624 flags
= SECTION_WRITE
;
626 if (decl
&& TREE_CODE (decl
) == VAR_DECL
627 && lookup_attribute ("shared", DECL_ATTRIBUTES (decl
)))
628 flags
|= SECTION_PE_SHARED
;
631 if (decl
&& DECL_ONE_ONLY (decl
))
632 flags
|= SECTION_LINKONCE
;
634 /* See if we already have an entry for this section. */
635 slot
= (unsigned int **) htab_find_slot (htab
, name
, INSERT
);
638 *slot
= (unsigned int *) xmalloc (sizeof (unsigned int));
643 if (decl
&& **slot
!= flags
)
644 error_with_decl (decl
, "%s causes a section type conflict");
651 i386_pe_asm_named_section (name
, flags
)
655 char flagchars
[8], *f
= flagchars
;
657 if (flags
& SECTION_CODE
)
659 if (flags
& SECTION_WRITE
)
661 if (flags
& SECTION_PE_SHARED
)
665 fprintf (asm_out_file
, "\t.section\t%s,\"%s\"\n", name
, flagchars
);
667 if (flags
& SECTION_LINKONCE
)
669 /* Functions may have been compiled at various levels of
670 optimization so we can't use `same_size' here.
671 Instead, have the linker pick one. */
672 fprintf (asm_out_file
, "\t.linkonce %s\n",
673 (flags
& SECTION_CODE
? "discard" : "same_size"));
677 /* The Microsoft linker requires that every function be marked as
678 DT_FCN. When using gas on cygwin, we must emit appropriate .type
683 /* Mark a function appropriately. This should only be called for
684 functions for which we are not emitting COFF debugging information.
685 FILE is the assembler output file, NAME is the name of the
686 function, and PUBLIC is nonzero if the function is globally
690 i386_pe_declare_function_type (file
, name
, public)
695 fprintf (file
, "\t.def\t");
696 assemble_name (file
, name
);
697 fprintf (file
, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
698 public ? (int) C_EXT
: (int) C_STAT
,
699 (int) DT_FCN
<< N_BTSHFT
);
702 /* Keep a list of external functions. */
706 struct extern_list
*next
;
710 static struct extern_list
*extern_head
;
712 /* Assemble an external function reference. We need to keep a list of
713 these, so that we can output the function types at the end of the
714 assembly. We can't output the types now, because we might see a
715 definition of the function later on and emit debugging information
719 i386_pe_record_external_function (name
)
722 struct extern_list
*p
;
724 p
= (struct extern_list
*) xmalloc (sizeof *p
);
725 p
->next
= extern_head
;
730 /* Keep a list of exported symbols. */
734 struct export_list
*next
;
736 int is_data
; /* used to type tag exported symbols. */
739 static struct export_list
*export_head
;
741 /* Assemble an export symbol entry. We need to keep a list of
742 these, so that we can output the export list at the end of the
743 assembly. We used to output these export symbols in each function,
744 but that causes problems with GNU ld when the sections are
748 i386_pe_record_exported_symbol (name
, is_data
)
752 struct export_list
*p
;
754 p
= (struct export_list
*) xmalloc (sizeof *p
);
755 p
->next
= export_head
;
757 p
->is_data
= is_data
;
761 /* This is called at the end of assembly. For each external function
762 which has not been defined, we output a declaration now. We also
763 output the .drectve section. */
766 i386_pe_asm_file_end (file
)
769 struct extern_list
*p
;
771 ix86_asm_file_end (file
);
773 for (p
= extern_head
; p
!= NULL
; p
= p
->next
)
777 decl
= get_identifier (p
->name
);
779 /* Positively ensure only one declaration for any given symbol. */
780 if (! TREE_ASM_WRITTEN (decl
) && TREE_SYMBOL_REFERENCED (decl
))
782 TREE_ASM_WRITTEN (decl
) = 1;
783 i386_pe_declare_function_type (file
, p
->name
, TREE_PUBLIC (decl
));
789 struct export_list
*q
;
791 for (q
= export_head
; q
!= NULL
; q
= q
->next
)
793 fprintf (file
, "\t.ascii \" -export:%s%s\"\n",
794 i386_pe_strip_name_encoding (q
->name
),
795 (q
->is_data
) ? ",data" : "");