OSDN Git Service

* final.c (HAVE_READONLY_DATA_SECTION): New.
[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
4    Free Software Foundation, Inc.
5
6 This file is part of GNU CC.
7
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)
11 any later version.
12
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.
17
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.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "output.h"
29 #include "tree.h"
30 #include "flags.h"
31 #include "tm_p.h"
32 #include "toplev.h"
33 #include "hashtab.h"
34
35 /* i386/PE specific attribute support.
36
37    i386/PE has two new attributes:
38    dllexport - for exporting a function/variable that will live in a dll
39    dllimport - for importing a function/variable from a dll
40
41    Microsoft allows multiple declspecs in one __declspec, separating
42    them with spaces.  We do NOT support this.  Instead, use __declspec
43    multiple times.
44 */
45
46 static tree associated_type PARAMS ((tree));
47 const char * gen_stdcall_suffix PARAMS ((tree));
48 int i386_pe_dllexport_p PARAMS ((tree));
49 int i386_pe_dllimport_p PARAMS ((tree));
50 void i386_pe_mark_dllexport PARAMS ((tree));
51 void i386_pe_mark_dllimport PARAMS ((tree));
52
53 /* Handle a "dllimport" or "dllexport" attribute;
54    arguments as in struct attribute_spec.handler.  */
55 tree
56 ix86_handle_dll_attribute (node, name, args, flags, no_add_attrs)
57      tree *node;
58      tree name;
59      tree args;
60      int flags;
61      bool *no_add_attrs;
62 {
63   /* These attributes may apply to structure and union types being created,
64      but otherwise should pass to the declaration involved.  */
65   if (!DECL_P (*node))
66     {
67       if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT
68                    | (int) ATTR_FLAG_ARRAY_NEXT))
69         {
70           *no_add_attrs = true;
71           return tree_cons (name, args, NULL_TREE);
72         }
73       if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
74         {
75           warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
76           *no_add_attrs = true;
77         }
78     }
79
80   return NULL_TREE;
81 }
82
83 /* Handle a "shared" attribute;
84    arguments as in struct attribute_spec.handler.  */
85 tree
86 ix86_handle_shared_attribute (node, name, args, flags, no_add_attrs)
87      tree *node;
88      tree name;
89      tree args ATTRIBUTE_UNUSED;
90      int flags ATTRIBUTE_UNUSED;
91      bool *no_add_attrs;
92 {
93   if (TREE_CODE (*node) != VAR_DECL)
94     {
95       warning ("`%s' attribute only applies to variables",
96                IDENTIFIER_POINTER (name));
97       *no_add_attrs = true;
98     }
99
100   return NULL_TREE;
101 }
102 \f
103 /* Return the type that we should use to determine if DECL is
104    imported or exported.  */
105
106 static tree
107 associated_type (decl)
108      tree decl;
109 {
110   tree t = NULL_TREE;
111
112   /* In the C++ frontend, DECL_CONTEXT for a method doesn't actually refer
113      to the containing class.  So we look at the 'this' arg.  */
114   if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
115     {
116       /* Artificial methods are not affected by the import/export status of
117          their class unless they are virtual.  */
118       if (! DECL_ARTIFICIAL (decl) || DECL_VINDEX (decl))
119         t = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl))));
120     }
121   else if (DECL_CONTEXT (decl)
122            && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (decl))) == 't')
123     t = DECL_CONTEXT (decl);
124
125   return t;
126 }
127
128 /* Return non-zero if DECL is a dllexport'd object.  */
129
130 int
131 i386_pe_dllexport_p (decl)
132      tree decl;
133 {
134   tree exp;
135
136   if (TREE_CODE (decl) != VAR_DECL
137       && TREE_CODE (decl) != FUNCTION_DECL)
138     return 0;
139   exp = lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl));
140   if (exp)
141     return 1;
142
143   /* Class members get the dllexport status of their class.  */
144   if (associated_type (decl))
145     {
146       exp = lookup_attribute ("dllexport",
147                               TYPE_ATTRIBUTES (associated_type (decl)));
148       if (exp)
149         return 1;
150     }
151
152   return 0;
153 }
154
155 /* Return non-zero if DECL is a dllimport'd object.  */
156
157 int
158 i386_pe_dllimport_p (decl)
159      tree decl;
160 {
161   tree imp;
162
163   if (TREE_CODE (decl) == FUNCTION_DECL
164       && TARGET_NOP_FUN_DLLIMPORT)
165     return 0;
166
167   if (TREE_CODE (decl) != VAR_DECL
168       && TREE_CODE (decl) != FUNCTION_DECL)
169     return 0;
170   imp = lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl));
171   if (imp)
172     return 1;
173
174   /* Class members get the dllimport status of their class.  */
175   if (associated_type (decl))
176     {
177       imp = lookup_attribute ("dllimport",
178                               TYPE_ATTRIBUTES (associated_type (decl)));
179       if (imp)
180         return 1;
181     }
182
183   return 0;
184 }
185
186 /* Return non-zero if SYMBOL is marked as being dllexport'd.  */
187
188 int
189 i386_pe_dllexport_name_p (symbol)
190      const char *symbol;
191 {
192   return symbol[0] == '@' && symbol[1] == 'e' && symbol[2] == '.';
193 }
194
195 /* Return non-zero if SYMBOL is marked as being dllimport'd.  */
196
197 int
198 i386_pe_dllimport_name_p (symbol)
199      const char *symbol;
200 {
201   return symbol[0] == '@' && symbol[1] == 'i' && symbol[2] == '.';
202 }
203
204 /* Mark a DECL as being dllexport'd.
205    Note that we override the previous setting (eg: dllimport).  */
206
207 void
208 i386_pe_mark_dllexport (decl)
209      tree decl;
210 {
211   const char *oldname;
212   char  *newname;
213   rtx rtlname;
214   tree idp;
215
216   rtlname = XEXP (DECL_RTL (decl), 0);
217   if (GET_CODE (rtlname) == SYMBOL_REF)
218     oldname = XSTR (rtlname, 0);
219   else if (GET_CODE (rtlname) == MEM
220            && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
221     oldname = XSTR (XEXP (rtlname, 0), 0);
222   else
223     abort ();
224   if (i386_pe_dllimport_name_p (oldname))
225     oldname += 9;
226   else if (i386_pe_dllexport_name_p (oldname))
227     return; /* already done */
228
229   newname = alloca (strlen (oldname) + 4);
230   sprintf (newname, "@e.%s", oldname);
231
232   /* We pass newname through get_identifier to ensure it has a unique
233      address.  RTL processing can sometimes peek inside the symbol ref
234      and compare the string's addresses to see if two symbols are
235      identical.  */
236   idp = get_identifier (newname);
237
238   XEXP (DECL_RTL (decl), 0) =
239     gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp));
240 }
241
242 /* Mark a DECL as being dllimport'd.  */
243
244 void
245 i386_pe_mark_dllimport (decl)
246      tree decl;
247 {
248   const char *oldname;
249   char  *newname;
250   tree idp;
251   rtx rtlname, newrtl;
252
253   rtlname = XEXP (DECL_RTL (decl), 0);
254   if (GET_CODE (rtlname) == SYMBOL_REF)
255     oldname = XSTR (rtlname, 0);
256   else if (GET_CODE (rtlname) == MEM
257            && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
258     oldname = XSTR (XEXP (rtlname, 0), 0);
259   else
260     abort ();
261   if (i386_pe_dllexport_name_p (oldname))
262     {
263       error ("`%s' declared as both exported to and imported from a DLL",
264              IDENTIFIER_POINTER (DECL_NAME (decl)));
265       return;
266     }
267   else if (i386_pe_dllimport_name_p (oldname))
268     {
269       /* Already done, but force correct linkage since the redeclaration 
270          might have omitted explicit extern.  Sigh.  */
271       if (TREE_CODE (decl) == VAR_DECL
272           /* ??? Is this test for vtables needed?  */
273           && !DECL_VIRTUAL_P (decl))
274         {
275           DECL_EXTERNAL (decl) = 1;
276           TREE_PUBLIC (decl) = 1;
277         }
278       return;
279     }
280
281   /* ??? One can well ask why we're making these checks here,
282      and that would be a good question.  */
283
284   /* Imported variables can't be initialized. Note that C++ classes
285      are marked initial, so we need to check.  */
286   if (TREE_CODE (decl) == VAR_DECL
287       && !DECL_VIRTUAL_P (decl)
288       && (DECL_INITIAL (decl)
289           && ! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
290     {
291       error_with_decl (decl, "initialized variable `%s' is marked dllimport");
292       return;
293     }
294   /* Nor can they be static.  */
295   if (TREE_CODE (decl) == VAR_DECL
296       /* ??? Is this test for vtables needed?  */
297       && !DECL_VIRTUAL_P (decl)
298       && 0 /*???*/)
299     {
300       error_with_decl (decl, "static variable `%s' is marked dllimport");
301       return;
302     }
303
304   /* `extern' needn't be specified with dllimport.
305      Specify `extern' now and hope for the best.  Sigh.  */
306   if (TREE_CODE (decl) == VAR_DECL
307       /* ??? Is this test for vtables needed?  */
308       && !DECL_VIRTUAL_P (decl))
309     {
310       DECL_EXTERNAL (decl) = 1;
311       TREE_PUBLIC (decl) = 1;
312     }
313
314   newname = alloca (strlen (oldname) + 11);
315   sprintf (newname, "@i._imp__%s", oldname);
316
317   /* We pass newname through get_identifier to ensure it has a unique
318      address.  RTL processing can sometimes peek inside the symbol ref
319      and compare the string's addresses to see if two symbols are
320      identical.  */
321   idp = get_identifier (newname);
322
323   newrtl = gen_rtx (MEM, Pmode,
324                     gen_rtx (SYMBOL_REF, Pmode,
325                              IDENTIFIER_POINTER (idp)));
326   XEXP (DECL_RTL (decl), 0) = newrtl;
327
328   /* Can't treat a pointer to this as a constant address */
329   DECL_NON_ADDR_CONST_P (decl) = 1;
330 }
331
332 /* Return string which is the former assembler name modified with a 
333    suffix consisting of an atsign (@) followed by the number of bytes of 
334    arguments */
335
336 const char *
337 gen_stdcall_suffix (decl)
338   tree decl;
339 {
340   int total = 0;
341   /* ??? This probably should use XSTR (XEXP (DECL_RTL (decl), 0), 0) instead
342      of DECL_ASSEMBLER_NAME.  */
343   const char *asmname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
344   char *newsym;
345
346   if (TYPE_ARG_TYPES (TREE_TYPE (decl)))
347     if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (decl)))) 
348         == void_type_node)
349       {
350         tree formal_type = TYPE_ARG_TYPES (TREE_TYPE (decl));
351
352         while (TREE_VALUE (formal_type) != void_type_node)
353           {
354             int parm_size
355               = TREE_INT_CST_LOW (TYPE_SIZE (TREE_VALUE (formal_type)));
356             /* Must round up to include padding.  This is done the same
357                way as in store_one_arg.  */
358             parm_size = ((parm_size + PARM_BOUNDARY - 1)
359                          / PARM_BOUNDARY * PARM_BOUNDARY);
360             total += parm_size;
361             formal_type = TREE_CHAIN (formal_type);
362           }
363       }
364
365   newsym = xmalloc (strlen (asmname) + 10);
366   sprintf (newsym, "%s@%d", asmname, total/BITS_PER_UNIT);
367   return IDENTIFIER_POINTER (get_identifier (newsym));
368 }
369
370 /* Cover function to implement ENCODE_SECTION_INFO.  */
371
372 void
373 i386_pe_encode_section_info (decl, first)
374      tree decl;
375      int first;
376 {
377   if (!first)
378     return;
379
380   /* This bit is copied from i386.h.  */
381   if (optimize > 0 && TREE_CONSTANT (decl)
382       && (!flag_writable_strings || TREE_CODE (decl) != STRING_CST))
383     {
384       rtx rtl = (TREE_CODE_CLASS (TREE_CODE (decl)) != 'd'
385                  ? TREE_CST_RTL (decl) : DECL_RTL (decl));
386       SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
387     }
388
389   if (TREE_CODE (decl) == FUNCTION_DECL)
390     if (lookup_attribute ("stdcall",
391                           TYPE_ATTRIBUTES (TREE_TYPE (decl))))
392       XEXP (DECL_RTL (decl), 0) = 
393         gen_rtx (SYMBOL_REF, Pmode, gen_stdcall_suffix (decl));
394
395   /* Mark the decl so we can tell from the rtl whether the object is
396      dllexport'd or dllimport'd.  */
397
398   if (i386_pe_dllexport_p (decl))
399     i386_pe_mark_dllexport (decl);
400   else if (i386_pe_dllimport_p (decl))
401     i386_pe_mark_dllimport (decl);
402   /* It might be that DECL has already been marked as dllimport, but a
403      subsequent definition nullified that.  The attribute is gone but
404      DECL_RTL still has @i._imp__foo.  We need to remove that. Ditto
405      for the DECL_NON_ADDR_CONST_P flag.  */
406   else if ((TREE_CODE (decl) == FUNCTION_DECL
407             || TREE_CODE (decl) == VAR_DECL)
408            && DECL_RTL (decl) != NULL_RTX
409            && GET_CODE (DECL_RTL (decl)) == MEM
410            && GET_CODE (XEXP (DECL_RTL (decl), 0)) == MEM
411            && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == SYMBOL_REF
412            && i386_pe_dllimport_name_p (XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0)))
413     {
414       const char *oldname = XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0);
415       tree idp = get_identifier (oldname + 9);
416       rtx newrtl = gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp));
417
418       XEXP (DECL_RTL (decl), 0) = newrtl;
419
420       DECL_NON_ADDR_CONST_P (decl) = 0;
421
422       /* We previously set TREE_PUBLIC and DECL_EXTERNAL.
423          We leave these alone for now.  */
424     }
425 }
426
427 void
428 i386_pe_unique_section (decl, reloc)
429      tree decl;
430      int reloc;
431 {
432   int len;
433   const char *name, *prefix;
434   char *string;
435
436   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
437   /* Strip off any encoding in fnname.  */
438   STRIP_NAME_ENCODING (name, name);
439
440   /* The object is put in, for example, section .text$foo.
441      The linker will then ultimately place them in .text
442      (everything from the $ on is stripped). Don't put
443      read-only data in .rdata section to avoid a PE linker 
444      bug when .rdata$* grouped sections are used in code
445      without a .rdata section.  */
446   if (TREE_CODE (decl) == FUNCTION_DECL)
447     prefix = ".text$";
448   else if (DECL_READONLY_SECTION (decl, reloc))
449     prefix = ".rdata$";
450   else
451     prefix = ".data$";
452   len = strlen (name) + strlen (prefix);
453   string = alloca (len + 1);
454   sprintf (string, "%s%s", prefix, name);
455
456   DECL_SECTION_NAME (decl) = build_string (len, string);
457 }
458
459 /* Select a set of attributes for section NAME based on the properties
460    of DECL and whether or not RELOC indicates that DECL's initializer
461    might contain runtime relocations.
462
463    We make the section read-only and executable for a function decl,
464    read-only for a const data decl, and writable for a non-const data decl.
465
466    If the section has already been defined, to not allow it to have
467    different attributes, as (1) this is ambiguous since we're not seeing
468    all the declarations up front and (2) some assemblers (e.g. SVR4)
469    do not recoginize section redefinitions.  */
470 /* ??? This differs from the "standard" PE implementation in that we
471    handle the SHARED variable attribute.  Should this be done for all
472    PE targets?  */
473
474 #define SECTION_PE_SHARED       SECTION_MACH_DEP
475
476 unsigned int
477 i386_pe_section_type_flags (decl, name, reloc)
478      tree decl;
479      const char *name;
480      int reloc;
481 {
482   static htab_t htab;
483   unsigned int flags;
484   unsigned int **slot;
485
486   /* The names we put in the hashtable will always be the unique
487      versions gived to us by the stringtable, so we can just use
488      their addresses as the keys.  */
489   if (!htab)
490     htab = htab_create (31, htab_hash_pointer, htab_eq_pointer, NULL);
491
492   if (decl && TREE_CODE (decl) == FUNCTION_DECL)
493     flags = SECTION_CODE;
494   else if (decl && DECL_READONLY_SECTION (decl, reloc))
495     flags = 0;
496   else
497     {
498       flags = SECTION_WRITE;
499
500       if (decl && TREE_CODE (decl) == VAR_DECL
501           && lookup_attribute ("shared", DECL_ATTRIBUTES (decl)))
502         flags |= SECTION_PE_SHARED;
503     }
504
505   if (decl && DECL_ONE_ONLY (decl))
506     flags |= SECTION_LINKONCE;
507
508   /* See if we already have an entry for this section.  */
509   slot = (unsigned int **) htab_find_slot (htab, name, INSERT);
510   if (!*slot)
511     {
512       *slot = (unsigned int *) xmalloc (sizeof (unsigned int));
513       **slot = flags;
514     }
515   else
516     {
517       if (decl && **slot != flags)
518         error_with_decl (decl, "%s causes a section type conflict");
519     }
520
521   return flags;
522 }
523
524 void
525 i386_pe_asm_named_section (name, flags)
526      const char *name;
527      unsigned int flags;
528 {
529   char flagchars[8], *f = flagchars;
530
531   if (flags & SECTION_CODE)
532     *f++ = 'x';
533   if (flags & SECTION_WRITE)
534     *f++ = 'w';
535   if (flags & SECTION_PE_SHARED)
536     *f++ = 's';
537   *f = '\0';
538
539   fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
540
541   if (flags & SECTION_LINKONCE)
542     {
543       /* Functions may have been compiled at various levels of
544          optimization so we can't use `same_size' here.
545          Instead, have the linker pick one.  */
546       fprintf (asm_out_file, "\t.linkonce %s\n",
547                (flags & SECTION_CODE ? "discard" : "same_size"));
548     }
549 }
550 \f
551 /* The Microsoft linker requires that every function be marked as
552    DT_FCN.  When using gas on cygwin, we must emit appropriate .type
553    directives.  */
554
555 #include "gsyms.h"
556
557 /* Mark a function appropriately.  This should only be called for
558    functions for which we are not emitting COFF debugging information.
559    FILE is the assembler output file, NAME is the name of the
560    function, and PUBLIC is non-zero if the function is globally
561    visible.  */
562
563 void
564 i386_pe_declare_function_type (file, name, public)
565      FILE *file;
566      const char *name;
567      int public;
568 {
569   fprintf (file, "\t.def\t");
570   assemble_name (file, name);
571   fprintf (file, ";\t.scl\t%d;\t.type\t%d;\t.endef\n",
572            public ? (int) C_EXT : (int) C_STAT,
573            (int) DT_FCN << N_BTSHFT);
574 }
575
576 /* Keep a list of external functions.  */
577
578 struct extern_list
579 {
580   struct extern_list *next;
581   const char *name;
582 };
583
584 static struct extern_list *extern_head;
585
586 /* Assemble an external function reference.  We need to keep a list of
587    these, so that we can output the function types at the end of the
588    assembly.  We can't output the types now, because we might see a
589    definition of the function later on and emit debugging information
590    for it then.  */
591
592 void
593 i386_pe_record_external_function (name)
594      const char *name;
595 {
596   struct extern_list *p;
597
598   p = (struct extern_list *) permalloc (sizeof *p);
599   p->next = extern_head;
600   p->name = name;
601   extern_head = p;
602 }
603
604 /* Keep a list of exported symbols.  */
605
606 struct export_list
607 {
608   struct export_list *next;
609   const char *name;
610   int is_data;          /* used to type tag exported symbols.  */
611 };
612
613 static struct export_list *export_head;
614
615 /* Assemble an export symbol entry.  We need to keep a list of
616    these, so that we can output the export list at the end of the
617    assembly.  We used to output these export symbols in each function,
618    but that causes problems with GNU ld when the sections are 
619    linkonce.  */
620
621 void
622 i386_pe_record_exported_symbol (name, is_data)
623      const char *name;
624      int is_data;
625 {
626   struct export_list *p;
627
628   p = (struct export_list *) permalloc (sizeof *p);
629   p->next = export_head;
630   p->name = name;
631   p->is_data = is_data;
632   export_head = p;
633 }
634
635 /* This is called at the end of assembly.  For each external function
636    which has not been defined, we output a declaration now.  We also
637    output the .drectve section.  */
638
639 void
640 i386_pe_asm_file_end (file)
641      FILE *file;
642 {
643   struct extern_list *p;
644
645   ix86_asm_file_end (file);
646
647   for (p = extern_head; p != NULL; p = p->next)
648     {
649       tree decl;
650
651       decl = get_identifier (p->name);
652
653       /* Positively ensure only one declaration for any given symbol.  */
654       if (! TREE_ASM_WRITTEN (decl) && TREE_SYMBOL_REFERENCED (decl))
655         {
656           TREE_ASM_WRITTEN (decl) = 1;
657           i386_pe_declare_function_type (file, p->name, TREE_PUBLIC (decl));
658         }
659     }
660
661   if (export_head)
662     {
663       struct export_list *q;
664       drectve_section ();
665       for (q = export_head; q != NULL; q = q->next)
666         {
667           fprintf (file, "\t.ascii \" -export:%s%s\"\n",
668                    I386_PE_STRIP_ENCODING (q->name),
669                    (q->is_data) ? ",data" : "");
670         }
671     }
672 }
673