Debian Codegraph @

gcc/config/mingw/winnt-dll.cc

raw
1 /* Expand a SYMBOL into its corresponding dllimport, far-address,
2 or refptr symbol.
3 Copyright (C) 1988-2025 Free Software Foundation, Inc.
4
5 GCC is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 3, or (at your option) any later
8 version.
9
10 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GCC; see the file COPYING3. If not see
17 http://www.gnu.org/licenses/. */
18
19 #include "config.h"
20 #include "system.h"
21 #include "coretypes.h"
22 #include "backend.h"
23 #include "target.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "memmodel.h"
27 #include "stringpool.h"
28 #include "emit-rtl.h"
29 #include "alias.h"
30 #include "varasm.h"
31 #include "output.h"
32 #include "explow.h"
33 #include "winnt.h"
34
35 /* Create or return the unique __imp_DECL dllimport symbol corresponding
36 to symbol DECL if BEIMPORT is true. Otherwise create or return the
37 unique refptr-DECL symbol corresponding to symbol DECL. */
38
39 struct dllimport_hasher : ggc_cache_ptr_hash<tree_map>
40 {
41 static inline hashval_t hash (tree_map *m)
42 {
43 return m->hash;
44 }
45
46 static inline bool
47 equal (tree_map *a, tree_map *b)
48 {
49 return a->base.from == b->base.from;
50 }
51
52 static int
53 keep_cache_entry (tree_map *&m)
54 {
55 return ggc_marked_p (m->base.from);
56 }
57 };
58
59 static GTY ((cache)) hash_table<dllimport_hasher> *dllimport_map;
60
61 /* Nonzero if the symbol is marked as dllimport, or as stub-variable,
62 otherwise zero. */
63
64 bool
65 is_imported_p (rtx x)
66 {
67 if (!TARGET_DLLIMPORT_DECL_ATTRIBUTES
68 || GET_CODE (x) != SYMBOL_REF)
69 return false;
70
71 return SYMBOL_REF_DLLIMPORT_P (x) || SYMBOL_REF_STUBVAR_P (x);
72 }
73
74 /* Return a unique alias set for the GOT. */
75
76 alias_set_type
77 mingw_GOT_alias_set (void)
78 {
79 static alias_set_type set = -1;
80 if (set == -1)
81 set = new_alias_set ();
82 return set;
83 }
84
85 static tree
86 get_dllimport_decl (tree decl, bool beimport)
87 {
88 struct tree_map *h, in;
89 const char *name;
90 const char *prefix;
91 size_t namelen, prefixlen;
92 char *imp_name;
93 tree to;
94 rtx rtl;
95
96 if (!dllimport_map)
97 dllimport_map = hash_table<dllimport_hasher>::create_ggc (512);
98
99 in.hash = htab_hash_pointer (decl);
100 in.base.from = decl;
101 tree_map **loc = dllimport_map->find_slot_with_hash (&in, in.hash, INSERT);
102 h = *loc;
103 if (h)
104 return h->to;
105
106 *loc = h = ggc_alloc<tree_map> ();
107 h->hash = in.hash;
108 h->base.from = decl;
109 h->to = to = build_decl (DECL_SOURCE_LOCATION (decl),
110 VAR_DECL, NULL, ptr_type_node);
111 DECL_ARTIFICIAL (to) = 1;
112 DECL_IGNORED_P (to) = 1;
113 DECL_EXTERNAL (to) = 1;
114 TREE_READONLY (to) = 1;
115
116 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
117 name = targetm.strip_name_encoding (name);
118 if (beimport)
119 prefix = name[0] == FASTCALL_PREFIX || user_label_prefix[0] == 0
120 ? "*__imp_" : "*__imp__";
121 else
122 prefix = user_label_prefix[0] == 0 ? "*.refptr." : "*refptr.";
123 namelen = strlen (name);
124 prefixlen = strlen (prefix);
125 imp_name = (char *) alloca (namelen + prefixlen + 1);
126 memcpy (imp_name, prefix, prefixlen);
127 memcpy (imp_name + prefixlen, name, namelen + 1);
128
129 name = ggc_alloc_string (imp_name, namelen + prefixlen);
130 rtl = gen_rtx_SYMBOL_REF (Pmode, name);
131 SET_SYMBOL_REF_DECL (rtl, to);
132 SYMBOL_REF_FLAGS (rtl) = SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_STUBVAR;
133 if (!beimport)
134 {
135 SYMBOL_REF_FLAGS (rtl) |= SYMBOL_FLAG_EXTERNAL;
136 #ifdef SUB_TARGET_RECORD_STUB
137 SUB_TARGET_RECORD_STUB (name, decl);
138 #endif
139 }
140
141 rtl = gen_const_mem (Pmode, rtl);
142 set_mem_alias_set (rtl, GOT_ALIAS_SET);
143
144 SET_DECL_RTL (to, rtl);
145 SET_DECL_ASSEMBLER_NAME (to, get_identifier (name));
146
147 return to;
148 }
149
150 /* Expand SYMBOL into its corresponding far-address symbol.
151 WANT_REG is true if we require the result be a register. */
152
153 static rtx
154 legitimize_pe_coff_extern_decl (rtx symbol, bool want_reg)
155 {
156 tree imp_decl;
157 rtx x;
158
159 gcc_assert (SYMBOL_REF_DECL (symbol));
160 imp_decl = get_dllimport_decl (SYMBOL_REF_DECL (symbol), false);
161
162 x = DECL_RTL (imp_decl);
163 if (want_reg)
164 x = force_reg (Pmode, x);
165 return x;
166 }
167
168 /* Expand SYMBOL into its corresponding dllimport symbol. WANT_REG is
169 true if we require the result be a register. */
170
171 static rtx
172 legitimize_dllimport_symbol (rtx symbol, bool want_reg)
173 {
174 tree imp_decl;
175 rtx x;
176
177 gcc_assert (SYMBOL_REF_DECL (symbol));
178 imp_decl = get_dllimport_decl (SYMBOL_REF_DECL (symbol), true);
179
180 x = DECL_RTL (imp_decl);
181 if (want_reg)
182 x = force_reg (Pmode, x);
183 return x;
184 }
185
186 /* Expand SYMBOL into its corresponding dllimport or refptr symbol. WANT_REG
187 is true if we require the result be a register. */
188
189 rtx
190 legitimize_pe_coff_symbol (rtx addr, bool inreg)
191 {
192 if (!TARGET_PECOFF)
193 return NULL_RTX;
194
195 if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
196 {
197 if (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (addr))
198 return legitimize_dllimport_symbol (addr, inreg);
199 if (GET_CODE (addr) == CONST
200 && GET_CODE (XEXP (addr, 0)) == PLUS
201 && GET_CODE (XEXP (XEXP (addr, 0), 0)) == SYMBOL_REF
202 && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (addr, 0), 0)))
203 {
204 rtx t = legitimize_dllimport_symbol (XEXP (XEXP (addr, 0), 0), inreg);
205 return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (addr, 0), 1));
206 }
207 }
208
209 if (!PE_COFF_LEGITIMIZE_EXTERN_DECL (addr))
210 return NULL_RTX;
211
212 if (GET_CODE (addr) == SYMBOL_REF
213 && !is_imported_p (addr)
214 && SYMBOL_REF_EXTERNAL_P (addr)
215 && SYMBOL_REF_DECL (addr))
216 return legitimize_pe_coff_extern_decl (addr, inreg);
217
218 if (GET_CODE (addr) == CONST
219 && GET_CODE (XEXP (addr, 0)) == PLUS
220 && GET_CODE (XEXP (XEXP (addr, 0), 0)) == SYMBOL_REF
221 && !is_imported_p (XEXP (XEXP (addr, 0), 0))
222 && SYMBOL_REF_EXTERNAL_P (XEXP (XEXP (addr, 0), 0))
223 && SYMBOL_REF_DECL (XEXP (XEXP (addr, 0), 0)))
224 {
225 rtx t = legitimize_pe_coff_extern_decl (XEXP (XEXP (addr, 0), 0), inreg);
226 return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (addr, 0), 1));
227 }
228 return NULL_RTX;
229 }
230
231 #include "gt-winnt-dll.h"