winnt.c (i386_pe_encode_section_info): Update call to make_decl_one_only.
[official-gcc.git] / gcc / config / i386 / winnt.c
blobfe5081d37a5df3f2a41cbfd432b4a37fe73ec747
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 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "regs.h"
28 #include "hard-reg-set.h"
29 #include "output.h"
30 #include "tree.h"
31 #include "flags.h"
32 #include "tm_p.h"
33 #include "toplev.h"
34 #include "hashtab.h"
35 #include "langhooks.h"
36 #include "ggc.h"
37 #include "target.h"
39 /* i386/PE specific attribute support.
41 i386/PE has two new attributes:
42 dllexport - for exporting a function/variable that will live in a dll
43 dllimport - for importing a function/variable from a dll
45 Microsoft allows multiple declspecs in one __declspec, separating
46 them with spaces. We do NOT support this. Instead, use __declspec
47 multiple times.
50 /* Handle a "shared" attribute;
51 arguments as in struct attribute_spec.handler. */
52 tree
53 ix86_handle_shared_attribute (tree *node, tree name,
54 tree args ATTRIBUTE_UNUSED,
55 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
57 if (TREE_CODE (*node) != VAR_DECL)
59 warning (OPT_Wattributes, "%qE attribute only applies to variables",
60 name);
61 *no_add_attrs = true;
64 return NULL_TREE;
67 /* Handle a "selectany" attribute;
68 arguments as in struct attribute_spec.handler. */
69 tree
70 ix86_handle_selectany_attribute (tree *node, tree name,
71 tree args ATTRIBUTE_UNUSED,
72 int flags ATTRIBUTE_UNUSED,
73 bool *no_add_attrs)
75 /* The attribute applies only to objects that are initialized and have
76 external linkage. However, we may not know about initialization
77 until the language frontend has processed the decl. We'll check for
78 initialization later in encode_section_info. */
79 if (TREE_CODE (*node) != VAR_DECL || !TREE_PUBLIC (*node))
81 error ("%qE attribute applies only to initialized variables"
82 " with external linkage", name);
83 *no_add_attrs = true;
86 return NULL_TREE;
90 /* Return the type that we should use to determine if DECL is
91 imported or exported. */
93 static tree
94 associated_type (tree decl)
96 return (DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
97 ? DECL_CONTEXT (decl) : NULL_TREE);
100 /* Return true if DECL should be a dllexport'd object. */
102 static bool
103 i386_pe_determine_dllexport_p (tree decl)
105 tree assoc;
107 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
108 return false;
110 if (lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))
111 return true;
113 /* Also mark class members of exported classes with dllexport. */
114 assoc = associated_type (decl);
115 if (assoc && lookup_attribute ("dllexport", TYPE_ATTRIBUTES (assoc)))
116 return i386_pe_type_dllexport_p (decl);
118 return false;
121 /* Return true if DECL should be a dllimport'd object. */
123 static bool
124 i386_pe_determine_dllimport_p (tree decl)
126 tree assoc;
128 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != FUNCTION_DECL)
129 return false;
131 /* Lookup the attribute in addition to checking the DECL_DLLIMPORT_P flag.
132 We may need to override an earlier decision. */
133 if (DECL_DLLIMPORT_P (decl))
134 return true;
136 /* The DECL_DLLIMPORT_P flag was set for decls in the class definition
137 by targetm.cxx.adjust_class_at_definition. Check again to emit
138 warnings if the class attribute has been overridden by an
139 out-of-class definition. */
140 assoc = associated_type (decl);
141 if (assoc && lookup_attribute ("dllimport", TYPE_ATTRIBUTES (assoc)))
142 return i386_pe_type_dllimport_p (decl);
144 return false;
147 /* Handle the -mno-fun-dllimport target switch. */
149 bool
150 i386_pe_valid_dllimport_attribute_p (const_tree decl)
152 if (TARGET_NOP_FUN_DLLIMPORT && TREE_CODE (decl) == FUNCTION_DECL)
153 return false;
154 return true;
157 /* Return string which is the function name, identified by ID, modified
158 with a suffix consisting of an atsign (@) followed by the number of
159 bytes of arguments. If ID is NULL use the DECL_NAME as base. If
160 FASTCALL is true, also add the FASTCALL_PREFIX.
161 Return NULL if no change required. */
163 static tree
164 gen_stdcall_or_fastcall_suffix (tree decl, tree id, bool fastcall)
166 HOST_WIDE_INT total = 0;
167 const char *old_str = IDENTIFIER_POINTER (id != NULL_TREE ? id : DECL_NAME (decl));
168 char *new_str, *p;
169 tree type = TREE_TYPE (decl);
170 tree arg;
171 function_args_iterator args_iter;
173 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
175 if (prototype_p (type))
177 /* This attribute is ignored for variadic functions. */
178 if (stdarg_p (type))
179 return NULL_TREE;
181 /* Quit if we hit an incomplete type. Error is reported
182 by convert_arguments in c-typeck.c or cp/typeck.c. */
183 FOREACH_FUNCTION_ARGS(type, arg, args_iter)
185 HOST_WIDE_INT parm_size;
186 HOST_WIDE_INT parm_boundary_bytes = PARM_BOUNDARY / BITS_PER_UNIT;
188 if (! COMPLETE_TYPE_P (arg))
189 break;
191 parm_size = int_size_in_bytes (arg);
192 if (parm_size < 0)
193 break;
195 /* Must round up to include padding. This is done the same
196 way as in store_one_arg. */
197 parm_size = ((parm_size + parm_boundary_bytes - 1)
198 / parm_boundary_bytes * parm_boundary_bytes);
199 total += parm_size;
202 /* Assume max of 8 base 10 digits in the suffix. */
203 p = new_str = XALLOCAVEC (char, 1 + strlen (old_str) + 1 + 8 + 1);
204 if (fastcall)
205 *p++ = FASTCALL_PREFIX;
206 sprintf (p, "%s@" HOST_WIDE_INT_PRINT_DEC, old_str, total);
208 return get_identifier (new_str);
211 /* Maybe decorate and get a new identifier for the DECL of a stdcall or
212 fastcall function. The original identifier is supplied in ID. */
214 static tree
215 i386_pe_maybe_mangle_decl_assembler_name (tree decl, tree id)
217 tree new_id = NULL_TREE;
219 if (TREE_CODE (decl) == FUNCTION_DECL)
221 tree type_attributes = TYPE_ATTRIBUTES (TREE_TYPE (decl));
222 if (lookup_attribute ("stdcall", type_attributes))
223 new_id = gen_stdcall_or_fastcall_suffix (decl, id, false);
224 else if (lookup_attribute ("fastcall", type_attributes))
225 new_id = gen_stdcall_or_fastcall_suffix (decl, id, true);
228 return new_id;
231 /* This is used as a target hook to modify the DECL_ASSEMBLER_NAME
232 in the language-independent default hook
233 langhooks,c:lhd_set_decl_assembler_name ()
234 and in cp/mangle,c:mangle_decl (). */
235 tree
236 i386_pe_mangle_decl_assembler_name (tree decl, tree id)
238 tree new_id = i386_pe_maybe_mangle_decl_assembler_name (decl, id);
240 return (new_id ? new_id : id);
243 void
244 i386_pe_encode_section_info (tree decl, rtx rtl, int first)
246 rtx symbol;
247 int flags;
249 /* Do this last, due to our frobbing of DECL_DLLIMPORT_P above. */
250 default_encode_section_info (decl, rtl, first);
252 /* Careful not to prod global register variables. */
253 if (!MEM_P (rtl))
254 return;
256 symbol = XEXP (rtl, 0);
257 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
259 switch (TREE_CODE (decl))
261 case FUNCTION_DECL:
262 /* FIXME: Imported stdcall names are not modified by the Ada frontend.
263 Check and decorate the RTL name now. */
264 if (strcmp (lang_hooks.name, "GNU Ada") == 0)
266 tree new_id;
267 tree old_id = DECL_ASSEMBLER_NAME (decl);
268 const char* asm_str = IDENTIFIER_POINTER (old_id);
269 /* Do not change the identifier if a verbatim asmspec
270 or if stdcall suffix already added. */
271 if (!(*asm_str == '*' || strchr (asm_str, '@'))
272 && (new_id = i386_pe_maybe_mangle_decl_assembler_name (decl,
273 old_id)))
274 XSTR (symbol, 0) = IDENTIFIER_POINTER (new_id);
276 break;
278 case VAR_DECL:
279 if (lookup_attribute ("selectany", DECL_ATTRIBUTES (decl)))
281 if (DECL_INITIAL (decl)
282 /* If an object is initialized with a ctor, the static
283 initialization and destruction code for it is present in
284 each unit defining the object. The code that calls the
285 ctor is protected by a link-once guard variable, so that
286 the object still has link-once semantics, */
287 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
288 make_decl_one_only (decl, DECL_ASSEMBLER_NAME (decl));
289 else
290 error ("%q+D:'selectany' attribute applies only to "
291 "initialized objects", decl);
293 break;
295 default:
296 return;
299 /* Mark the decl so we can tell from the rtl whether the object is
300 dllexport'd or dllimport'd. tree.c: merge_dllimport_decl_attributes
301 handles dllexport/dllimport override semantics. */
302 flags = (SYMBOL_REF_FLAGS (symbol) &
303 ~(SYMBOL_FLAG_DLLIMPORT | SYMBOL_FLAG_DLLEXPORT));
304 if (i386_pe_determine_dllexport_p (decl))
305 flags |= SYMBOL_FLAG_DLLEXPORT;
306 else if (i386_pe_determine_dllimport_p (decl))
308 flags |= SYMBOL_FLAG_DLLIMPORT;
309 /* If we went through the associated_type path, this won't already
310 be set. Though, frankly, this seems wrong, and should be fixed
311 elsewhere. */
312 if (!DECL_DLLIMPORT_P (decl))
314 DECL_DLLIMPORT_P (decl) = 1;
315 flags &= ~SYMBOL_FLAG_LOCAL;
318 SYMBOL_REF_FLAGS (symbol) = flags;
321 bool
322 i386_pe_binds_local_p (const_tree exp)
324 /* PE does not do dynamic binding. Indeed, the only kind of
325 non-local reference comes from a dllimport'd symbol. */
326 if ((TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == FUNCTION_DECL)
327 && DECL_DLLIMPORT_P (exp))
328 return false;
330 return true;
333 /* Also strip the fastcall prefix and stdcall suffix. */
335 const char *
336 i386_pe_strip_name_encoding_full (const char *str)
338 const char *p;
339 const char *name = default_strip_name_encoding (str);
341 /* Strip leading '@' on fastcall symbols. */
342 if (*name == '@')
343 name++;
345 /* Strip trailing "@n". */
346 p = strchr (name, '@');
347 if (p)
348 return ggc_alloc_string (name, p - name);
350 return name;
353 void
354 i386_pe_unique_section (tree decl, int reloc)
356 int len;
357 const char *name, *prefix;
358 char *string;
360 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
361 name = i386_pe_strip_name_encoding_full (name);
363 /* The object is put in, for example, section .text$foo.
364 The linker will then ultimately place them in .text
365 (everything from the $ on is stripped). Don't put
366 read-only data in .rdata section to avoid a PE linker
367 bug when .rdata$* grouped sections are used in code
368 without a .rdata section. */
369 if (TREE_CODE (decl) == FUNCTION_DECL)
370 prefix = ".text$";
371 else if (decl_readonly_section (decl, reloc))
372 prefix = ".rdata$";
373 else
374 prefix = ".data$";
375 len = strlen (name) + strlen (prefix);
376 string = XALLOCAVEC (char, len + 1);
377 sprintf (string, "%s%s", prefix, name);
379 DECL_SECTION_NAME (decl) = build_string (len, string);
382 /* Select a set of attributes for section NAME based on the properties
383 of DECL and whether or not RELOC indicates that DECL's initializer
384 might contain runtime relocations.
386 We make the section read-only and executable for a function decl,
387 read-only for a const data decl, and writable for a non-const data decl.
389 If the section has already been defined, to not allow it to have
390 different attributes, as (1) this is ambiguous since we're not seeing
391 all the declarations up front and (2) some assemblers (e.g. SVR4)
392 do not recognize section redefinitions. */
393 /* ??? This differs from the "standard" PE implementation in that we
394 handle the SHARED variable attribute. Should this be done for all
395 PE targets? */
397 #define SECTION_PE_SHARED SECTION_MACH_DEP
399 unsigned int
400 i386_pe_section_type_flags (tree decl, const char *name, int reloc)
402 static htab_t htab;
403 unsigned int flags;
404 unsigned int **slot;
406 /* The names we put in the hashtable will always be the unique
407 versions given to us by the stringtable, so we can just use
408 their addresses as the keys. */
409 if (!htab)
410 htab = htab_create (31, htab_hash_pointer, htab_eq_pointer, NULL);
412 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
413 flags = SECTION_CODE;
414 else if (decl && decl_readonly_section (decl, reloc))
415 flags = 0;
416 else if (current_function_decl
417 && cfun
418 && crtl->subsections.unlikely_text_section_name
419 && strcmp (name, crtl->subsections.unlikely_text_section_name) == 0)
420 flags = SECTION_CODE;
421 else if (!decl
422 && (!current_function_decl || !cfun)
423 && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
424 flags = SECTION_CODE;
425 else
427 flags = SECTION_WRITE;
429 if (decl && TREE_CODE (decl) == VAR_DECL
430 && lookup_attribute ("shared", DECL_ATTRIBUTES (decl)))
431 flags |= SECTION_PE_SHARED;
434 if (decl && DECL_ONE_ONLY (decl))
435 flags |= SECTION_LINKONCE;
437 /* See if we already have an entry for this section. */
438 slot = (unsigned int **) htab_find_slot (htab, name, INSERT);
439 if (!*slot)
441 *slot = (unsigned int *) xmalloc (sizeof (unsigned int));
442 **slot = flags;
444 else
446 if (decl && **slot != flags)
447 error ("%q+D causes a section type conflict", decl);
450 return flags;
453 void
454 i386_pe_asm_named_section (const char *name, unsigned int flags,
455 tree decl)
457 char flagchars[8], *f = flagchars;
459 if ((flags & (SECTION_CODE | SECTION_WRITE)) == 0)
460 /* readonly data */
462 *f++ ='d'; /* This is necessary for older versions of gas. */
463 *f++ ='r';
465 else
467 if (flags & SECTION_CODE)
468 *f++ = 'x';
469 if (flags & SECTION_WRITE)
470 *f++ = 'w';
471 if (flags & SECTION_PE_SHARED)
472 *f++ = 's';
475 *f = '\0';
477 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
479 if (flags & SECTION_LINKONCE)
481 /* Functions may have been compiled at various levels of
482 optimization so we can't use `same_size' here.
483 Instead, have the linker pick one, without warning.
484 If 'selectany' attribute has been specified, MS compiler
485 sets 'discard' characteristic, rather than telling linker
486 to warn of size or content mismatch, so do the same. */
487 bool discard = (flags & SECTION_CODE)
488 || lookup_attribute ("selectany",
489 DECL_ATTRIBUTES (decl));
490 fprintf (asm_out_file, "\t.linkonce %s\n",
491 (discard ? "discard" : "same_size"));
495 void
496 i386_pe_asm_output_aligned_decl_common (FILE *stream, tree decl,
497 const char *name, HOST_WIDE_INT size,
498 HOST_WIDE_INT align ATTRIBUTE_UNUSED)
500 HOST_WIDE_INT rounded;
502 /* Compute as in assemble_noswitch_variable, since we don't have
503 support for aligned common on older binutils. We must also
504 avoid emitting a common symbol of size zero, as this is the
505 overloaded representation that indicates an undefined external
506 symbol in the PE object file format. */
507 rounded = size ? size : 1;
508 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
509 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
510 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
512 i386_pe_maybe_record_exported_symbol (decl, name, 1);
514 fprintf (stream, "\t.comm\t");
515 assemble_name (stream, name);
516 if (use_pe_aligned_common)
517 fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC ", %d\n",
518 size ? size : (HOST_WIDE_INT) 1,
519 exact_log2 (align) - exact_log2 (CHAR_BIT));
520 else
521 fprintf (stream, ", " HOST_WIDE_INT_PRINT_DEC "\t" ASM_COMMENT_START
522 " " HOST_WIDE_INT_PRINT_DEC "\n", rounded, size);
525 /* The Microsoft linker requires that every function be marked as
526 DT_FCN. When using gas on cygwin, we must emit appropriate .type
527 directives. */
529 #include "gsyms.h"
531 /* Mark a function appropriately. This should only be called for
532 functions for which we are not emitting COFF debugging information.
533 FILE is the assembler output file, NAME is the name of the
534 function, and PUB is nonzero if the function is globally
535 visible. */
537 void
538 i386_pe_declare_function_type (FILE *file, const char *name, int pub)
540 fprintf (file, "\t.def\t");
541 assemble_name (file, name);
542 fprintf (file, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
543 pub ? (int) C_EXT : (int) C_STAT,
544 (int) DT_FCN << N_BTSHFT);
547 /* Keep a list of external functions. */
549 struct GTY(()) extern_list
551 struct extern_list *next;
552 tree decl;
553 const char *name;
556 static GTY(()) struct extern_list *extern_head;
558 /* Assemble an external function reference. We need to keep a list of
559 these, so that we can output the function types at the end of the
560 assembly. We can't output the types now, because we might see a
561 definition of the function later on and emit debugging information
562 for it then. */
564 void
565 i386_pe_record_external_function (tree decl, const char *name)
567 struct extern_list *p;
569 p = (struct extern_list *) ggc_alloc (sizeof *p);
570 p->next = extern_head;
571 p->decl = decl;
572 p->name = name;
573 extern_head = p;
576 /* Keep a list of exported symbols. */
578 struct GTY(()) export_list
580 struct export_list *next;
581 const char *name;
582 int is_data; /* used to type tag exported symbols. */
585 static GTY(()) struct export_list *export_head;
587 /* Assemble an export symbol entry. We need to keep a list of
588 these, so that we can output the export list at the end of the
589 assembly. We used to output these export symbols in each function,
590 but that causes problems with GNU ld when the sections are
591 linkonce. */
593 void
594 i386_pe_maybe_record_exported_symbol (tree decl, const char *name, int is_data)
596 rtx symbol;
597 struct export_list *p;
599 symbol = XEXP (DECL_RTL (decl), 0);
600 gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
601 if (!SYMBOL_REF_DLLEXPORT_P (symbol))
602 return;
604 p = (struct export_list *) ggc_alloc (sizeof *p);
605 p->next = export_head;
606 p->name = name;
607 p->is_data = is_data;
608 export_head = p;
611 /* This is called at the end of assembly. For each external function
612 which has not been defined, we output a declaration now. We also
613 output the .drectve section. */
615 void
616 i386_pe_file_end (void)
618 struct extern_list *p;
620 ix86_file_end ();
622 for (p = extern_head; p != NULL; p = p->next)
624 tree decl;
626 decl = p->decl;
628 /* Positively ensure only one declaration for any given symbol. */
629 if (! TREE_ASM_WRITTEN (decl)
630 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
632 TREE_ASM_WRITTEN (decl) = 1;
633 i386_pe_declare_function_type (asm_out_file, p->name,
634 TREE_PUBLIC (decl));
638 if (export_head)
640 struct export_list *q;
641 drectve_section ();
642 for (q = export_head; q != NULL; q = q->next)
644 fprintf (asm_out_file, "\t.ascii \" -export:%s%s\"\n",
645 default_strip_name_encoding (q->name),
646 (q->is_data ? ",data" : ""));
651 #include "gt-winnt.h"