OSDN Git Service

* config/i386/winnt.c (i386_pe_output_labelref): Correct
[pf3gnuchains/gcc-fork.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    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
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 2, or (at your option) any later
11 version.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA.  */
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 "toplev.h"
35 #include "hashtab.h"
36 #include "ggc.h"
37
38 /* i386/PE specific attribute support.
39
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
43
44    Microsoft allows multiple declspecs in one __declspec, separating
45    them with spaces.  We do NOT support this.  Instead, use __declspec
46    multiple times.
47 */
48
49 static tree associated_type (tree);
50 static const char * gen_stdcall_suffix (tree);
51 static const char * gen_fastcall_suffix (tree);
52 static int i386_pe_dllexport_p (tree);
53 static int i386_pe_dllimport_p (tree);
54 static void i386_pe_mark_dllexport (tree);
55 static void i386_pe_mark_dllimport (tree);
56
57 /* This is we how mark internal identifiers with dllimport or dllexport
58    attributes.  */
59 #ifndef DLL_IMPORT_PREFIX
60 #define DLL_IMPORT_PREFIX "#i."
61 #endif
62 #ifndef DLL_EXPORT_PREFIX
63 #define DLL_EXPORT_PREFIX "#e."
64 #endif
65
66 /* Handle a "dllimport" or "dllexport" attribute;
67    arguments as in struct attribute_spec.handler.  */
68 tree
69 ix86_handle_dll_attribute (tree * pnode, tree name, tree args, int flags,
70                            bool *no_add_attrs)
71 {
72   tree node = *pnode;
73
74   /* These attributes may apply to structure and union types being created,
75      but otherwise should pass to the declaration involved.  */
76   if (!DECL_P (node))
77     {
78       if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
79                    | (int) ATTR_FLAG_ARRAY_NEXT))
80         {
81           *no_add_attrs = true;
82           return tree_cons (name, args, NULL_TREE);
83         }
84       if (TREE_CODE (node) != RECORD_TYPE && TREE_CODE (node) != UNION_TYPE)
85         {
86           warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
87           *no_add_attrs = true;
88         }
89
90       return NULL_TREE;
91     }
92
93   /* Report error on dllimport ambiguities seen now before they cause
94      any damage.  */
95   else if (is_attribute_p ("dllimport", name))
96     {
97       /* Like MS, treat definition of dllimported variables and
98          non-inlined functions on declaration as syntax errors.
99          We allow the attribute for function definitions if declared
100          inline, but just ignore it in i386_pe_dllimport_p.  */
101       if (TREE_CODE (node) == FUNCTION_DECL  && DECL_INITIAL (node)
102           && !DECL_INLINE (node))
103         {
104           error ("%Jfunction `%D' definition is marked dllimport.", node, node);
105           *no_add_attrs = true;
106         }
107
108       else if (TREE_CODE (node) == VAR_DECL)
109         {
110           if (DECL_INITIAL (node))
111             {
112               error ("%Jvariable `%D' definition is marked dllimport.",
113                      node, node);
114               *no_add_attrs = true;
115             }
116
117           /* `extern' needn't be specified with dllimport.
118              Specify `extern' now and hope for the best.  Sigh.  */
119           DECL_EXTERNAL (node) = 1;
120           /* Also, implicitly give dllimport'd variables declared within
121              a function global scope, unless declared static.  */
122           if (current_function_decl != NULL_TREE && !TREE_STATIC (node))
123             TREE_PUBLIC (node) = 1;
124         }
125     }
126
127   /*  Report error if symbol is not accessible at global scope.  */
128   if (!TREE_PUBLIC (node)
129       && (TREE_CODE (node) == VAR_DECL
130           || TREE_CODE (node) == FUNCTION_DECL))
131     {
132       error ("%Jexternal linkage required for symbol '%D' because of "
133              "'%s' attribute.", node, node, IDENTIFIER_POINTER (name));
134       *no_add_attrs = true;
135     }
136
137   return NULL_TREE;
138 }
139
140 /* Handle a "shared" attribute;
141    arguments as in struct attribute_spec.handler.  */
142 tree
143 ix86_handle_shared_attribute (tree *node, tree name,
144                               tree args ATTRIBUTE_UNUSED,
145                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
146 {
147   if (TREE_CODE (*node) != VAR_DECL)
148     {
149       warning ("`%s' attribute only applies to variables",
150                IDENTIFIER_POINTER (name));
151       *no_add_attrs = true;
152     }
153
154   return NULL_TREE;
155 }
156 \f
157 /* Return the type that we should use to determine if DECL is
158    imported or exported.  */
159
160 static tree
161 associated_type (tree decl)
162 {
163   tree t = NULL_TREE;
164
165   /* In the C++ frontend, DECL_CONTEXT for a method doesn't actually refer
166      to the containing class.  So we look at the 'this' arg.  */
167   if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
168     {
169       /* Artificial methods are not affected by the import/export status
170          of their class unless they are COMDAT.  Implicit copy ctor's and
171          dtor's are not affected by class status but virtual and
172          non-virtual thunks are.  */
173       if (!DECL_ARTIFICIAL (decl) || DECL_COMDAT (decl))
174         t = TYPE_MAIN_VARIANT
175           (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
176     }
177   else if (DECL_CONTEXT (decl)
178            && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (decl))) == 't')
179     t = DECL_CONTEXT (decl);
180
181   return t;
182 }
183
184 /* Return nonzero if DECL is a dllexport'd object.  */
185
186 static int
187 i386_pe_dllexport_p (tree decl)
188 {
189   tree exp;
190
191   if (TREE_CODE (decl) != VAR_DECL
192       && TREE_CODE (decl) != FUNCTION_DECL)
193     return 0;
194   exp = lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl));
195   if (exp)
196     return 1;
197
198   /* Class members get the dllexport status of their class.  */
199   if (associated_type (decl))
200     {
201       exp = lookup_attribute ("dllexport",
202                               TYPE_ATTRIBUTES (associated_type (decl)));
203       if (exp)
204         return 1;
205     }
206
207   return 0;
208 }
209
210 /* Return nonzero if DECL is a dllimport'd object.  */
211
212 static int
213 i386_pe_dllimport_p (tree decl)
214 {
215   tree imp;
216   int context_imp = 0;
217
218   if (TREE_CODE (decl) == FUNCTION_DECL
219       && TARGET_NOP_FUN_DLLIMPORT)
220     return 0;
221
222   if (TREE_CODE (decl) != VAR_DECL
223       && TREE_CODE (decl) != FUNCTION_DECL)
224     return 0;
225
226   imp = lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl));
227
228   /* Class members get the dllimport status of their class.  */
229   if (!imp && associated_type (decl))
230     {
231       imp = lookup_attribute ("dllimport",
232                               TYPE_ATTRIBUTES (associated_type (decl)));
233       if (imp)
234         context_imp = 1;
235     }
236
237   if (imp)
238     {
239       /* Don't mark defined functions as dllimport.  If the definition
240          itself was marked with dllimport, than ix86_handle_dll_attribute
241          reports an error. This handles the case when the definition
242          overrides an earlier declaration.  */
243       if (TREE_CODE (decl) ==  FUNCTION_DECL && DECL_INITIAL (decl)
244           && !DECL_INLINE (decl))
245         {
246            /* Don't warn about artificial methods.  */
247           if (!DECL_ARTIFICIAL (decl))
248             warning ("%Jfunction '%D' is defined after prior declaration "
249                      "as dllimport: attribute ignored", decl, decl);
250           return 0;
251         }
252
253       /* We ignore the dllimport attribute for inline member functions.
254          This differs from MSVC behavior which treats it like GNUC
255          'extern inline' extension.  */
256       else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_INLINE (decl))
257         {
258           if (extra_warnings)
259             warning ("%Jinline function '%D' is declared as dllimport: "
260                      "attribute ignored.", decl, decl);
261           return 0;
262         }
263
264       /*  Don't allow definitions of static data members in dllimport class,
265           Just ignore attribute for vtable data.  */
266       else if (TREE_CODE (decl) == VAR_DECL
267                && TREE_STATIC (decl) && TREE_PUBLIC (decl)
268                && !DECL_EXTERNAL (decl) && context_imp)
269         {
270           if (!DECL_VIRTUAL_P (decl))
271             error ("%Jdefinition of static data member '%D' of "
272                    "dllimport'd class.", decl, decl);
273           return 0;
274         }
275
276       /* Since we can't treat a pointer to a dllimport'd symbol as a
277          constant address, we turn off the attribute on C++ virtual
278          methods to allow creation of vtables using thunks.  Don't mark
279          artificial methods either (in associated_type, only COMDAT
280          artificial method get import status from class context).  */
281       else if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
282                && (DECL_VIRTUAL_P (decl) || DECL_ARTIFICIAL (decl)))
283         return 0;
284
285       return 1;
286     }
287
288   return 0;
289 }
290
291 /* Return nonzero if SYMBOL is marked as being dllexport'd.  */
292
293 int
294 i386_pe_dllexport_name_p (const char *symbol)
295 {
296   return (strncmp (DLL_EXPORT_PREFIX, symbol,
297                    strlen (DLL_EXPORT_PREFIX)) == 0);
298 }
299
300 /* Return nonzero if SYMBOL is marked as being dllimport'd.  */
301
302 int
303 i386_pe_dllimport_name_p (const char *symbol)
304 {
305   return (strncmp (DLL_IMPORT_PREFIX, symbol,
306                    strlen (DLL_IMPORT_PREFIX)) == 0);
307 }
308
309 /* Mark a DECL as being dllexport'd.
310    Note that we override the previous setting (eg: dllimport).  */
311
312 static void
313 i386_pe_mark_dllexport (tree decl)
314 {
315   const char *oldname;
316   char  *newname;
317   rtx rtlname;
318   tree idp;
319
320   rtlname = XEXP (DECL_RTL (decl), 0);
321   if (GET_CODE (rtlname) == SYMBOL_REF)
322     oldname = XSTR (rtlname, 0);
323   else if (GET_CODE (rtlname) == MEM
324            && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
325     oldname = XSTR (XEXP (rtlname, 0), 0);
326   else
327     abort ();
328   if (i386_pe_dllimport_name_p (oldname))
329     {
330       warning ("%Jinconsistent dll linkage for '%D', dllexport assumed.",
331                decl, decl);
332      /* Remove DLL_IMPORT_PREFIX.  */
333       oldname += strlen (DLL_IMPORT_PREFIX);
334       DECL_NON_ADDR_CONST_P (decl) = 0;
335     }
336   else if (i386_pe_dllexport_name_p (oldname))
337     return;  /*  already done  */
338
339   newname = alloca (strlen (DLL_EXPORT_PREFIX) + strlen (oldname) + 1);
340   sprintf (newname, "%s%s", DLL_EXPORT_PREFIX, oldname);
341
342   /* We pass newname through get_identifier to ensure it has a unique
343      address.  RTL processing can sometimes peek inside the symbol ref
344      and compare the string's addresses to see if two symbols are
345      identical.  */
346   idp = get_identifier (newname);
347
348   XEXP (DECL_RTL (decl), 0) =
349     gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (idp));
350 }
351
352 /* Mark a DECL as being dllimport'd.  */
353
354 static void
355 i386_pe_mark_dllimport (tree decl)
356 {
357   const char *oldname;
358   char  *newname;
359   tree idp;
360   rtx rtlname, newrtl;
361
362   rtlname = XEXP (DECL_RTL (decl), 0);
363   if (GET_CODE (rtlname) == SYMBOL_REF)
364     oldname = XSTR (rtlname, 0);
365   else if (GET_CODE (rtlname) == MEM
366            && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
367     oldname = XSTR (XEXP (rtlname, 0), 0);
368   else
369     abort ();
370   if (i386_pe_dllexport_name_p (oldname))
371     {
372       error ("`%s' declared as both exported to and imported from a DLL",
373              IDENTIFIER_POINTER (DECL_NAME (decl)));
374       return;
375     }
376   else if (i386_pe_dllimport_name_p (oldname))
377     {
378       /* Already done, but do a sanity check to prevent assembler errors.  */
379       if (!DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
380         {
381           error ("%Jfailure in redeclaration of '%D': dllimport'd "
382                  "symbol lacks external linkage.", decl, decl);
383           abort();
384         }
385       return;
386     }
387
388   newname = alloca (strlen (DLL_IMPORT_PREFIX) + strlen (oldname) + 1);
389   sprintf (newname, "%s%s", DLL_IMPORT_PREFIX, oldname);
390
391   /* We pass newname through get_identifier to ensure it has a unique
392      address.  RTL processing can sometimes peek inside the symbol ref
393      and compare the string's addresses to see if two symbols are
394      identical.  */
395   idp = get_identifier (newname);
396
397   newrtl = gen_rtx_MEM (Pmode,
398                         gen_rtx_SYMBOL_REF (Pmode,
399                                             IDENTIFIER_POINTER (idp)));
400   XEXP (DECL_RTL (decl), 0) = newrtl;
401
402   /* Can't treat a pointer to this as a constant address */
403   DECL_NON_ADDR_CONST_P (decl) = 1;
404 }
405
406 /* Return string which is the former assembler name modified with a
407    prefix consisting of FASTCALL_PREFIX and a suffix consisting of an
408    atsign (@) followed by the number of bytes of arguments.  */
409
410 static const char *
411 gen_fastcall_suffix (tree decl)
412 {
413   int total = 0;
414   const char *asmname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
415   char *newsym;
416
417   if (TYPE_ARG_TYPES (TREE_TYPE (decl)))
418     if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (decl))))
419         == void_type_node)
420       {
421         tree formal_type = TYPE_ARG_TYPES (TREE_TYPE (decl));
422
423         /* Quit if we hit an incomplete type.  Error is reported
424            by convert_arguments in c-typeck.c or cp/typeck.c.  */
425         while (TREE_VALUE (formal_type) != void_type_node
426                && COMPLETE_TYPE_P (TREE_VALUE (formal_type)))   
427           {
428             int parm_size
429               = TREE_INT_CST_LOW (TYPE_SIZE (TREE_VALUE (formal_type)));
430             /* Must round up to include padding.  This is done the same
431                way as in store_one_arg.  */
432             parm_size = ((parm_size + PARM_BOUNDARY - 1)
433                          / PARM_BOUNDARY * PARM_BOUNDARY);
434             total += parm_size;
435             formal_type = TREE_CHAIN (formal_type);
436           }
437       }
438
439   /* Assume max of 8 base 10 digits in the suffix.  */
440   newsym = xmalloc (1 + strlen (asmname) + 1 + 8 + 1);
441   sprintf (newsym, "%c%s@%d", FASTCALL_PREFIX, asmname, total/BITS_PER_UNIT);
442   return IDENTIFIER_POINTER (get_identifier (newsym));
443 }
444
445 /* Return string which is the former assembler name modified with a
446    suffix consisting of an atsign (@) followed by the number of bytes of
447    arguments */
448
449 static const char *
450 gen_stdcall_suffix (tree decl)
451 {
452   int total = 0;
453   /* ??? This probably should use XSTR (XEXP (DECL_RTL (decl), 0), 0) instead
454      of DECL_ASSEMBLER_NAME.  */
455   const char *asmname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
456   char *newsym;
457
458   if (TYPE_ARG_TYPES (TREE_TYPE (decl)))
459     if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (decl))))
460         == void_type_node)
461       {
462         tree formal_type = TYPE_ARG_TYPES (TREE_TYPE (decl));
463
464         /* Quit if we hit an incomplete type.  Error is reported
465            by convert_arguments in c-typeck.c or cp/typeck.c.  */
466         while (TREE_VALUE (formal_type) != void_type_node
467                && COMPLETE_TYPE_P (TREE_VALUE (formal_type)))   
468           {
469             int parm_size
470               = TREE_INT_CST_LOW (TYPE_SIZE (TREE_VALUE (formal_type)));
471             /* Must round up to include padding.  This is done the same
472                way as in store_one_arg.  */
473             parm_size = ((parm_size + PARM_BOUNDARY - 1)
474                          / PARM_BOUNDARY * PARM_BOUNDARY);
475             total += parm_size;
476             formal_type = TREE_CHAIN (formal_type);
477           }
478       }
479
480   /* Assume max of 8 base 10 digits in the suffix.  */
481   newsym = xmalloc (strlen (asmname) + 1 + 8 + 1);
482   sprintf (newsym, "%s@%d", asmname, total/BITS_PER_UNIT);
483   return IDENTIFIER_POINTER (get_identifier (newsym));
484 }
485
486 void
487 i386_pe_encode_section_info (tree decl, rtx rtl, int first)
488 {
489   default_encode_section_info (decl, rtl, first);
490
491   if (TREE_CODE (decl) == FUNCTION_DECL)
492     {
493       if (lookup_attribute ("stdcall",
494                             TYPE_ATTRIBUTES (TREE_TYPE (decl))))
495         XEXP (DECL_RTL (decl), 0) =
496           gen_rtx_SYMBOL_REF (Pmode, gen_stdcall_suffix (decl));
497       else if (lookup_attribute ("fastcall",
498                                  TYPE_ATTRIBUTES (TREE_TYPE (decl))))
499         XEXP (DECL_RTL (decl), 0) =
500           gen_rtx_SYMBOL_REF (Pmode, gen_fastcall_suffix (decl));
501     }
502
503   /* Mark the decl so we can tell from the rtl whether the object is
504      dllexport'd or dllimport'd.  This also handles dllexport/dllimport
505      override semantics.  */
506
507   if (i386_pe_dllexport_p (decl))
508     i386_pe_mark_dllexport (decl);
509   else if (i386_pe_dllimport_p (decl))
510     i386_pe_mark_dllimport (decl);
511   /* It might be that DECL has already been marked as dllimport, but a
512      subsequent definition nullified that.  The attribute is gone but
513      DECL_RTL still has (DLL_IMPORT_PREFIX) prefixed. We need to remove
514      that. Ditto for the DECL_NON_ADDR_CONST_P flag.  */
515   else if ((TREE_CODE (decl) == FUNCTION_DECL
516             || TREE_CODE (decl) == VAR_DECL)
517            && DECL_RTL (decl) != NULL_RTX
518            && GET_CODE (DECL_RTL (decl)) == MEM
519            && GET_CODE (XEXP (DECL_RTL (decl), 0)) == MEM
520            && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == SYMBOL_REF
521            && i386_pe_dllimport_name_p (XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0)))
522     {
523       const char *oldname = XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0);
524
525       /* Remove DLL_IMPORT_PREFIX.  */
526       tree idp = get_identifier (oldname + strlen (DLL_IMPORT_PREFIX));
527       rtx newrtl = gen_rtx_SYMBOL_REF (Pmode, IDENTIFIER_POINTER (idp));
528
529       if (DECL_INITIAL (decl) || !DECL_EXTERNAL (decl))
530         warning ("%J'%D' defined locally after being "
531                  "referenced with dllimport linkage", decl, decl);
532       else
533         warning ("%J'%D' redeclared without dllimport attribute "
534                  "after being referenced with dllimport linkage", decl, decl);
535
536       XEXP (DECL_RTL (decl), 0) = newrtl;
537
538       DECL_NON_ADDR_CONST_P (decl) = 0;
539
540       /* We previously set TREE_PUBLIC and DECL_EXTERNAL.
541          We leave these alone for now.  */
542     }
543 }
544
545 /* Strip only the leading encoding, leaving the stdcall suffix and fastcall
546    prefix if it exists.  */
547
548 const char *
549 i386_pe_strip_name_encoding (const char *str)
550 {
551   if (strncmp (str, DLL_IMPORT_PREFIX, strlen (DLL_IMPORT_PREFIX))
552       == 0)
553     str += strlen (DLL_IMPORT_PREFIX);
554   else if (strncmp (str, DLL_EXPORT_PREFIX, strlen (DLL_EXPORT_PREFIX))
555            == 0)
556     str += strlen (DLL_EXPORT_PREFIX);
557   if (*str == '*')
558     str += 1;
559   return str;
560 }
561
562 /* Also strip the fastcall prefix and stdcall suffix.  */
563
564 const char *
565 i386_pe_strip_name_encoding_full (const char *str)
566 {
567   const char *p;
568   const char *name = i386_pe_strip_name_encoding (str);
569
570   /* Strip leading '@' on fastcall symbols.  */
571   if (*name == '@')
572     name++;
573
574   /* Strip trailing "@n".  */
575   p = strchr (name, '@');
576   if (p)
577     return ggc_alloc_string (name, p - name);
578
579   return name;
580 }
581
582 /* Output a reference to a label. Fastcall symbols are prefixed with @,
583    whereas symbols for functions using other calling conventions don't
584    have a prefix (unless they are marked dllimport or dllexport).  */
585
586 void i386_pe_output_labelref (FILE *stream, const char *name)
587 {
588   if (strncmp (name, DLL_IMPORT_PREFIX, strlen (DLL_IMPORT_PREFIX))
589       == 0)
590     /* A dll import */
591     {
592       if (name[strlen (DLL_IMPORT_PREFIX)] == FASTCALL_PREFIX)
593       /* A dllimport fastcall symbol.  */
594         {
595           fprintf (stream, "__imp_%s",
596                    i386_pe_strip_name_encoding (name));
597         }
598       else
599       /* A dllimport non-fastcall symbol.  */
600         {
601           fprintf (stream, "__imp__%s",
602                    i386_pe_strip_name_encoding (name));
603         }
604     }
605   else if ((name[0] == FASTCALL_PREFIX)
606            || (strncmp (name, DLL_EXPORT_PREFIX, strlen (DLL_EXPORT_PREFIX))
607                == 0
608                && name[strlen (DLL_EXPORT_PREFIX)] == FASTCALL_PREFIX))
609     /* A fastcall symbol.  */
610     {
611       fprintf (stream, "%s",
612                i386_pe_strip_name_encoding (name));
613     }
614   else
615     /* Everything else.  */
616     {
617       fprintf (stream, "%s%s", USER_LABEL_PREFIX,
618                i386_pe_strip_name_encoding (name));
619     }
620 }
621
622 void
623 i386_pe_unique_section (tree decl, int reloc)
624 {
625   int len;
626   const char *name, *prefix;
627   char *string;
628
629   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
630   name = i386_pe_strip_name_encoding_full (name);
631
632   /* The object is put in, for example, section .text$foo.
633      The linker will then ultimately place them in .text
634      (everything from the $ on is stripped). Don't put
635      read-only data in .rdata section to avoid a PE linker
636      bug when .rdata$* grouped sections are used in code
637      without a .rdata section.  */
638   if (TREE_CODE (decl) == FUNCTION_DECL)
639     prefix = ".text$";
640   else if (decl_readonly_section (decl, reloc))
641     prefix = ".rdata$";
642   else
643     prefix = ".data$";
644   len = strlen (name) + strlen (prefix);
645   string = alloca (len + 1);
646   sprintf (string, "%s%s", prefix, name);
647
648   DECL_SECTION_NAME (decl) = build_string (len, string);
649 }
650
651 /* Select a set of attributes for section NAME based on the properties
652    of DECL and whether or not RELOC indicates that DECL's initializer
653    might contain runtime relocations.
654
655    We make the section read-only and executable for a function decl,
656    read-only for a const data decl, and writable for a non-const data decl.
657
658    If the section has already been defined, to not allow it to have
659    different attributes, as (1) this is ambiguous since we're not seeing
660    all the declarations up front and (2) some assemblers (e.g. SVR4)
661    do not recognize section redefinitions.  */
662 /* ??? This differs from the "standard" PE implementation in that we
663    handle the SHARED variable attribute.  Should this be done for all
664    PE targets?  */
665
666 #define SECTION_PE_SHARED       SECTION_MACH_DEP
667
668 unsigned int
669 i386_pe_section_type_flags (tree decl, const char *name, int reloc)
670 {
671   static htab_t htab;
672   unsigned int flags;
673   unsigned int **slot;
674
675   /* The names we put in the hashtable will always be the unique
676      versions given to us by the stringtable, so we can just use
677      their addresses as the keys.  */
678   if (!htab)
679     htab = htab_create (31, htab_hash_pointer, htab_eq_pointer, NULL);
680
681   if (decl && TREE_CODE (decl) == FUNCTION_DECL)
682     flags = SECTION_CODE;
683   else if (decl && decl_readonly_section (decl, reloc))
684     flags = 0;
685   else
686     {
687       flags = SECTION_WRITE;
688
689       if (decl && TREE_CODE (decl) == VAR_DECL
690           && lookup_attribute ("shared", DECL_ATTRIBUTES (decl)))
691         flags |= SECTION_PE_SHARED;
692     }
693
694   if (decl && DECL_ONE_ONLY (decl))
695     flags |= SECTION_LINKONCE;
696
697   /* See if we already have an entry for this section.  */
698   slot = (unsigned int **) htab_find_slot (htab, name, INSERT);
699   if (!*slot)
700     {
701       *slot = (unsigned int *) xmalloc (sizeof (unsigned int));
702       **slot = flags;
703     }
704   else
705     {
706       if (decl && **slot != flags)
707         error ("%J'%D' causes a section type conflict", decl, decl);
708     }
709
710   return flags;
711 }
712
713 void
714 i386_pe_asm_named_section (const char *name, unsigned int flags)
715 {
716   char flagchars[8], *f = flagchars;
717
718   if ((flags & (SECTION_CODE | SECTION_WRITE)) == 0)
719     /* readonly data */
720     {
721       *f++ ='d';  /* This is necessary for older versions of gas.  */
722       *f++ ='r';
723     }
724   else  
725     {
726       if (flags & SECTION_CODE)
727         *f++ = 'x';
728       if (flags & SECTION_WRITE)
729         *f++ = 'w';
730       if (flags & SECTION_PE_SHARED)
731         *f++ = 's';
732     }
733
734   *f = '\0';
735
736   fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
737
738   if (flags & SECTION_LINKONCE)
739     {
740       /* Functions may have been compiled at various levels of
741          optimization so we can't use `same_size' here.
742          Instead, have the linker pick one.  */
743       fprintf (asm_out_file, "\t.linkonce %s\n",
744                (flags & SECTION_CODE ? "discard" : "same_size"));
745     }
746 }
747 \f
748 /* The Microsoft linker requires that every function be marked as
749    DT_FCN.  When using gas on cygwin, we must emit appropriate .type
750    directives.  */
751
752 #include "gsyms.h"
753
754 /* Mark a function appropriately.  This should only be called for
755    functions for which we are not emitting COFF debugging information.
756    FILE is the assembler output file, NAME is the name of the
757    function, and PUBLIC is nonzero if the function is globally
758    visible.  */
759
760 void
761 i386_pe_declare_function_type (FILE *file, const char *name, int public)
762 {
763   fprintf (file, "\t.def\t");
764   assemble_name (file, name);
765   fprintf (file, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
766            public ? (int) C_EXT : (int) C_STAT,
767            (int) DT_FCN << N_BTSHFT);
768 }
769
770 /* Keep a list of external functions.  */
771
772 struct extern_list GTY(())
773 {
774   struct extern_list *next;
775   const char *name;
776 };
777
778 static GTY(()) struct extern_list *extern_head;
779
780 /* Assemble an external function reference.  We need to keep a list of
781    these, so that we can output the function types at the end of the
782    assembly.  We can't output the types now, because we might see a
783    definition of the function later on and emit debugging information
784    for it then.  */
785
786 void
787 i386_pe_record_external_function (const char *name)
788 {
789   struct extern_list *p;
790
791   p = (struct extern_list *) ggc_alloc (sizeof *p);
792   p->next = extern_head;
793   p->name = name;
794   extern_head = p;
795 }
796
797 /* Keep a list of exported symbols.  */
798
799 struct export_list GTY(())
800 {
801   struct export_list *next;
802   const char *name;
803   int is_data;          /* used to type tag exported symbols.  */
804 };
805
806 static GTY(()) struct export_list *export_head;
807
808 /* Assemble an export symbol entry.  We need to keep a list of
809    these, so that we can output the export list at the end of the
810    assembly.  We used to output these export symbols in each function,
811    but that causes problems with GNU ld when the sections are
812    linkonce.  */
813
814 void
815 i386_pe_record_exported_symbol (const char *name, int is_data)
816 {
817   struct export_list *p;
818
819   p = (struct export_list *) ggc_alloc (sizeof *p);
820   p->next = export_head;
821   p->name = name;
822   p->is_data = is_data;
823   export_head = p;
824 }
825
826 /* This is called at the end of assembly.  For each external function
827    which has not been defined, we output a declaration now.  We also
828    output the .drectve section.  */
829
830 void
831 i386_pe_file_end (void)
832 {
833   struct extern_list *p;
834
835   ix86_file_end ();
836
837   for (p = extern_head; p != NULL; p = p->next)
838     {
839       tree decl;
840
841       decl = get_identifier (p->name);
842
843       /* Positively ensure only one declaration for any given symbol.  */
844       if (! TREE_ASM_WRITTEN (decl) && TREE_SYMBOL_REFERENCED (decl))
845         {
846           TREE_ASM_WRITTEN (decl) = 1;
847           i386_pe_declare_function_type (asm_out_file, p->name,
848                                          TREE_PUBLIC (decl));
849         }
850     }
851
852   if (export_head)
853     {
854       struct export_list *q;
855       drectve_section ();
856       for (q = export_head; q != NULL; q = q->next)
857         {
858           fprintf (asm_out_file, "\t.ascii \" -export:%s%s\"\n",
859                    i386_pe_strip_name_encoding (q->name),
860                    (q->is_data) ? ",data" : "");
861         }
862     }
863 }
864
865 #include "gt-winnt.h"