1 /* Routines for GCC for ARM/pe.
2 Copyright (C) 1995, 1996, 2000, 2001, 2002, 2004, 2005, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Doug Evans (dje@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published
10 by the Free Software Foundation; either version 3, or (at your
11 option) any later version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License 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/>. */
24 #include "coretypes.h"
31 #include "diagnostic-core.h"
35 extern int current_function_anonymous_args
;
38 /* Return nonzero if DECL is a dllexport'd object. */
40 tree current_class_type
; /* FIXME */
43 arm_dllexport_p (tree decl
)
47 if (TREE_CODE (decl
) != VAR_DECL
48 && TREE_CODE (decl
) != FUNCTION_DECL
)
50 exp
= lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl
));
57 /* Return nonzero if DECL is a dllimport'd object. */
60 arm_dllimport_p (tree decl
)
64 if (TREE_CODE (decl
) == FUNCTION_DECL
65 && TARGET_NOP_FUN_DLLIMPORT
)
68 if (TREE_CODE (decl
) != VAR_DECL
69 && TREE_CODE (decl
) != FUNCTION_DECL
)
71 imp
= lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl
));
78 /* Return nonzero if SYMBOL is marked as being dllexport'd. */
81 arm_dllexport_name_p (const char *symbol
)
83 return symbol
[0] == ARM_PE_FLAG_CHAR
&& symbol
[1] == 'e' && symbol
[2] == '.';
86 /* Return nonzero if SYMBOL is marked as being dllimport'd. */
89 arm_dllimport_name_p (const char *symbol
)
91 return symbol
[0] == ARM_PE_FLAG_CHAR
&& symbol
[1] == 'i' && symbol
[2] == '.';
94 /* Mark a DECL as being dllexport'd.
95 Note that we override the previous setting (e.g.: dllimport). */
98 arm_mark_dllexport (tree decl
)
100 const char * oldname
;
105 rtlname
= XEXP (DECL_RTL (decl
), 0);
106 if (GET_CODE (rtlname
) == MEM
)
107 rtlname
= XEXP (rtlname
, 0);
108 gcc_assert (GET_CODE (rtlname
) == SYMBOL_REF
);
109 oldname
= XSTR (rtlname
, 0);
111 if (arm_dllimport_name_p (oldname
))
113 else if (arm_dllexport_name_p (oldname
))
114 return; /* already done */
116 newname
= XALLOCAVEC (char, strlen (oldname
) + 4);
117 sprintf (newname
, "%ce.%s", ARM_PE_FLAG_CHAR
, oldname
);
119 /* We pass newname through get_identifier to ensure it has a unique
120 address. RTL processing can sometimes peek inside the symbol ref
121 and compare the string's addresses to see if two symbols are
123 /* ??? At least I think that's why we do this. */
124 idp
= get_identifier (newname
);
126 XEXP (DECL_RTL (decl
), 0) =
127 gen_rtx_SYMBOL_REF (Pmode
, IDENTIFIER_POINTER (idp
));
130 /* Mark a DECL as being dllimport'd. */
133 arm_mark_dllimport (tree decl
)
135 const char * oldname
;
140 rtlname
= XEXP (DECL_RTL (decl
), 0);
142 if (GET_CODE (rtlname
) == MEM
)
143 rtlname
= XEXP (rtlname
, 0);
144 gcc_assert (GET_CODE (rtlname
) == SYMBOL_REF
);
145 oldname
= XSTR (rtlname
, 0);
147 gcc_assert (!arm_dllexport_name_p (oldname
));
148 if (arm_dllimport_name_p (oldname
))
149 return; /* already done */
151 /* ??? One can well ask why we're making these checks here,
152 and that would be a good question. */
154 /* Imported variables can't be initialized. */
155 if (TREE_CODE (decl
) == VAR_DECL
156 && !DECL_VIRTUAL_P (decl
)
157 && DECL_INITIAL (decl
))
159 error ("initialized variable %q+D is marked dllimport", decl
);
162 /* Nor can they be static. */
163 if (TREE_CODE (decl
) == VAR_DECL
164 /* ??? Is this test for vtables needed? */
165 && !DECL_VIRTUAL_P (decl
)
168 error ("static variable %q+D is marked dllimport", decl
);
172 /* `extern' needn't be specified with dllimport.
173 Specify `extern' now and hope for the best. Sigh. */
174 if (TREE_CODE (decl
) == VAR_DECL
175 /* ??? Is this test for vtables needed? */
176 && !DECL_VIRTUAL_P (decl
))
178 DECL_EXTERNAL (decl
) = 1;
179 TREE_PUBLIC (decl
) = 1;
182 newname
= XALLOCAVEC (char, strlen (oldname
) + 11);
183 sprintf (newname
, "%ci.__imp_%s", ARM_PE_FLAG_CHAR
, oldname
);
185 /* We pass newname through get_identifier to ensure it has a unique
186 address. RTL processing can sometimes peek inside the symbol ref
187 and compare the string's addresses to see if two symbols are
189 /* ??? At least I think that's why we do this. */
190 idp
= get_identifier (newname
);
192 newrtl
= gen_rtx_MEM (Pmode
,
193 gen_rtx_SYMBOL_REF (Pmode
,
194 IDENTIFIER_POINTER (idp
)));
195 XEXP (DECL_RTL (decl
), 0) = newrtl
;
199 arm_pe_encode_section_info (tree decl
, rtx rtl
, int first ATTRIBUTE_UNUSED
)
201 /* This bit is copied from arm_encode_section_info. */
202 if (optimize
> 0 && TREE_CONSTANT (decl
))
203 SYMBOL_REF_FLAG (XEXP (rtl
, 0)) = 1;
205 /* Mark the decl so we can tell from the rtl whether the object is
206 dllexport'd or dllimport'd. */
207 if (arm_dllexport_p (decl
))
208 arm_mark_dllexport (decl
);
209 else if (arm_dllimport_p (decl
))
210 arm_mark_dllimport (decl
);
211 /* It might be that DECL has already been marked as dllimport, but a
212 subsequent definition nullified that. The attribute is gone but
213 DECL_RTL still has @i.__imp_foo. We need to remove that. */
214 else if ((TREE_CODE (decl
) == FUNCTION_DECL
215 || TREE_CODE (decl
) == VAR_DECL
)
216 && DECL_RTL (decl
) != NULL_RTX
217 && GET_CODE (DECL_RTL (decl
)) == MEM
218 && GET_CODE (XEXP (DECL_RTL (decl
), 0)) == MEM
219 && GET_CODE (XEXP (XEXP (DECL_RTL (decl
), 0), 0)) == SYMBOL_REF
220 && arm_dllimport_name_p (XSTR (XEXP (XEXP (DECL_RTL (decl
), 0), 0), 0)))
222 const char *oldname
= XSTR (XEXP (XEXP (DECL_RTL (decl
), 0), 0), 0);
223 tree idp
= get_identifier (oldname
+ 9);
224 rtx newrtl
= gen_rtx_SYMBOL_REF (Pmode
, IDENTIFIER_POINTER (idp
));
226 XEXP (DECL_RTL (decl
), 0) = newrtl
;
228 /* We previously set TREE_PUBLIC and DECL_EXTERNAL.
229 ??? We leave these alone for now. */
234 arm_pe_unique_section (tree decl
, int reloc
)
241 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
242 name
= arm_strip_name_encoding (name
);
244 /* The object is put in, for example, section .text$foo.
245 The linker will then ultimately place them in .text
246 (everything from the $ on is stripped). */
247 if (TREE_CODE (decl
) == FUNCTION_DECL
)
249 else if (decl_readonly_section (decl
, reloc
))
253 len
= strlen (name
) + strlen (prefix
);
254 string
= XALLOCAVEC (char, len
+ 1);
255 sprintf (string
, "%s%s", prefix
, name
);
257 DECL_SECTION_NAME (decl
) = build_string (len
, string
);