pe.c revision 117395
196263Sobrien/* Routines for GCC for ARM/pe.
296263Sobrien   Copyright (C) 1995, 1996, 2000, 2001, 2002 Free Software Foundation, Inc.
396263Sobrien   Contributed by Doug Evans (dje@cygnus.com).
496263Sobrien
596263SobrienThis file is part of GNU CC.
696263Sobrien
796263SobrienGNU CC is free software; you can redistribute it and/or modify
896263Sobrienit under the terms of the GNU General Public License as published by
996263Sobrienthe Free Software Foundation; either version 2, or (at your option)
1096263Sobrienany later version.
1196263Sobrien
1296263SobrienGNU CC is distributed in the hope that it will be useful,
1396263Sobrienbut WITHOUT ANY WARRANTY; without even the implied warranty of
1496263SobrienMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1596263SobrienGNU General Public License for more details.
1696263Sobrien
1796263SobrienYou should have received a copy of the GNU General Public License
1896263Sobrienalong with GNU CC; see the file COPYING.  If not, write to
1996263Sobrienthe Free Software Foundation, 59 Temple Place - Suite 330,
2096263SobrienBoston, MA 02111-1307, USA.  */
2196263Sobrien
2296263Sobrien#include "config.h"
2396263Sobrien#include "system.h"
2496263Sobrien#include "rtl.h"
2596263Sobrien#include "output.h"
2696263Sobrien#include "flags.h"
2796263Sobrien#include "tree.h"
2896263Sobrien#include "expr.h"
2996263Sobrien#include "toplev.h"
3096263Sobrien#include "tm_p.h"
3196263Sobrien
3296263Sobrienextern int current_function_anonymous_args;
3396263Sobrien
3496263Sobrien
35/* Return nonzero if DECL is a dllexport'd object.  */
36
37tree current_class_type; /* FIXME */
38
39int
40arm_dllexport_p (decl)
41     tree decl;
42{
43  tree exp;
44
45  if (TREE_CODE (decl) != VAR_DECL
46      && TREE_CODE (decl) != FUNCTION_DECL)
47    return 0;
48  exp = lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl));
49  if (exp)
50    return 1;
51
52  return 0;
53}
54
55/* Return nonzero if DECL is a dllimport'd object.  */
56
57int
58arm_dllimport_p (decl)
59     tree decl;
60{
61  tree imp;
62
63  if (TREE_CODE (decl) == FUNCTION_DECL
64      && TARGET_NOP_FUN_DLLIMPORT)
65    return 0;
66
67  if (TREE_CODE (decl) != VAR_DECL
68      && TREE_CODE (decl) != FUNCTION_DECL)
69    return 0;
70  imp = lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl));
71  if (imp)
72    return 1;
73
74  return 0;
75}
76
77/* Return nonzero if SYMBOL is marked as being dllexport'd.  */
78
79int
80arm_dllexport_name_p (symbol)
81     const char * symbol;
82{
83  return symbol[0] == ARM_PE_FLAG_CHAR && symbol[1] == 'e' && symbol[2] == '.';
84}
85
86/* Return nonzero if SYMBOL is marked as being dllimport'd.  */
87
88int
89arm_dllimport_name_p (symbol)
90     const char * symbol;
91{
92  return symbol[0] == ARM_PE_FLAG_CHAR && symbol[1] == 'i' && symbol[2] == '.';
93}
94
95/* Mark a DECL as being dllexport'd.
96   Note that we override the previous setting (eg: dllimport).  */
97
98void
99arm_mark_dllexport (decl)
100     tree decl;
101{
102  const char * oldname;
103  char * newname;
104  rtx rtlname;
105  tree idp;
106
107  rtlname = XEXP (DECL_RTL (decl), 0);
108  if (GET_CODE (rtlname) == SYMBOL_REF)
109    oldname = XSTR (rtlname, 0);
110  else if (GET_CODE (rtlname) == MEM
111	   && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
112    oldname = XSTR (XEXP (rtlname, 0), 0);
113  else
114    abort ();
115  if (arm_dllimport_name_p (oldname))
116    oldname += 9;
117  else if (arm_dllexport_name_p (oldname))
118    return; /* already done */
119
120  newname = alloca (strlen (oldname) + 4);
121  sprintf (newname, "%ce.%s", ARM_PE_FLAG_CHAR, oldname);
122
123  /* We pass newname through get_identifier to ensure it has a unique
124     address.  RTL processing can sometimes peek inside the symbol ref
125     and compare the string's addresses to see if two symbols are
126     identical.  */
127  /* ??? At least I think that's why we do this.  */
128  idp = get_identifier (newname);
129
130  XEXP (DECL_RTL (decl), 0) =
131    gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp));
132}
133
134/* Mark a DECL as being dllimport'd.  */
135
136void
137arm_mark_dllimport (decl)
138     tree decl;
139{
140  const char * oldname;
141  char * newname;
142  tree idp;
143  rtx rtlname, newrtl;
144
145  rtlname = XEXP (DECL_RTL (decl), 0);
146
147  if (GET_CODE (rtlname) == SYMBOL_REF)
148    oldname = XSTR (rtlname, 0);
149  else if (GET_CODE (rtlname) == MEM
150	   && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF)
151    oldname = XSTR (XEXP (rtlname, 0), 0);
152  else
153    abort ();
154
155  if (arm_dllexport_name_p (oldname))
156    abort (); /* this shouldn't happen */
157  else if (arm_dllimport_name_p (oldname))
158    return; /* already done */
159
160  /* ??? One can well ask why we're making these checks here,
161     and that would be a good question.  */
162
163  /* Imported variables can't be initialized.  */
164  if (TREE_CODE (decl) == VAR_DECL
165      && !DECL_VIRTUAL_P (decl)
166      && DECL_INITIAL (decl))
167    {
168      error_with_decl (decl, "initialized variable `%s' is marked dllimport");
169      return;
170    }
171  /* Nor can they be static.  */
172  if (TREE_CODE (decl) == VAR_DECL
173      /* ??? Is this test for vtables needed?  */
174      && !DECL_VIRTUAL_P (decl)
175      && 0 /*???*/)
176    {
177      error_with_decl (decl, "static variable `%s' is marked dllimport");
178      return;
179    }
180
181  /* `extern' needn't be specified with dllimport.
182     Specify `extern' now and hope for the best.  Sigh.  */
183  if (TREE_CODE (decl) == VAR_DECL
184      /* ??? Is this test for vtables needed?  */
185      && !DECL_VIRTUAL_P (decl))
186    {
187      DECL_EXTERNAL (decl) = 1;
188      TREE_PUBLIC (decl) = 1;
189    }
190
191  newname = alloca (strlen (oldname) + 11);
192  sprintf (newname, "%ci.__imp_%s", ARM_PE_FLAG_CHAR, oldname);
193
194  /* We pass newname through get_identifier to ensure it has a unique
195     address.  RTL processing can sometimes peek inside the symbol ref
196     and compare the string's addresses to see if two symbols are
197     identical.  */
198  /* ??? At least I think that's why we do this.  */
199  idp = get_identifier (newname);
200
201  newrtl = gen_rtx (MEM, Pmode,
202		    gen_rtx (SYMBOL_REF, Pmode,
203			     IDENTIFIER_POINTER (idp)));
204  XEXP (DECL_RTL (decl), 0) = newrtl;
205}
206
207void
208arm_pe_encode_section_info (decl, first)
209     tree decl;
210     int first ATTRIBUTE_UNUSED;
211{
212  /* This bit is copied from arm_encode_section_info.  */
213  if (optimize > 0 && TREE_CONSTANT (decl)
214      && (!flag_writable_strings || TREE_CODE (decl) != STRING_CST))
215    {
216      rtx rtl = (TREE_CODE_CLASS (TREE_CODE (decl)) != 'd'
217                 ? TREE_CST_RTL (decl) : DECL_RTL (decl));
218      SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1;
219    }
220
221  /* Mark the decl so we can tell from the rtl whether the object is
222     dllexport'd or dllimport'd.  */
223  if (arm_dllexport_p (decl))
224    arm_mark_dllexport (decl);
225  else if (arm_dllimport_p (decl))
226    arm_mark_dllimport (decl);
227  /* It might be that DECL has already been marked as dllimport, but a
228     subsequent definition nullified that.  The attribute is gone but
229     DECL_RTL still has @i.__imp_foo.  We need to remove that.  */
230  else if ((TREE_CODE (decl) == FUNCTION_DECL
231	    || TREE_CODE (decl) == VAR_DECL)
232	   && DECL_RTL (decl) != NULL_RTX
233	   && GET_CODE (DECL_RTL (decl)) == MEM
234	   && GET_CODE (XEXP (DECL_RTL (decl), 0)) == MEM
235	   && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == SYMBOL_REF
236	   && arm_dllimport_name_p (XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0)))
237    {
238      const char *oldname = XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0);
239      tree idp = get_identifier (oldname + 9);
240      rtx newrtl = gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp));
241
242      XEXP (DECL_RTL (decl), 0) = newrtl;
243
244      /* We previously set TREE_PUBLIC and DECL_EXTERNAL.
245	 ??? We leave these alone for now.  */
246    }
247}
248
249void
250arm_pe_unique_section (decl, reloc)
251     tree decl;
252     int reloc;
253{
254  int len;
255  const char * name;
256  char * string;
257  const char * prefix;
258
259  name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
260  name = arm_strip_name_encoding (name);
261
262  /* The object is put in, for example, section .text$foo.
263     The linker will then ultimately place them in .text
264     (everything from the $ on is stripped).  */
265  if (TREE_CODE (decl) == FUNCTION_DECL)
266    prefix = ".text$";
267  else if (decl_readonly_section (decl, reloc))
268    prefix = ".rdata$";
269  else
270    prefix = ".data$";
271  len = strlen (name) + strlen (prefix);
272  string = alloca (len + 1);
273  sprintf (string, "%s%s", prefix, name);
274
275  DECL_SECTION_NAME (decl) = build_string (len, string);
276}
277