]>
Commit | Line | Data |
---|---|---|
ee890fe2 | 1 | /* Functions for generic Darwin as target machine for GNU C compiler. |
5b86a469 | 2 | Copyright (C) 1989, 1990, 1991, 1992, 1993, 2000, 2001, 2002, 2003, 2004, |
29d08eba | 3 | 2005, 2006, 2007, 2008, 2009 |
ee890fe2 SS |
4 | Free Software Foundation, Inc. |
5 | Contributed by Apple Computer Inc. | |
6 | ||
7ec022b2 | 7 | This file is part of GCC. |
ee890fe2 | 8 | |
7ec022b2 | 9 | GCC is free software; you can redistribute it and/or modify |
ee890fe2 | 10 | it under the terms of the GNU General Public License as published by |
2f83c7d6 | 11 | the Free Software Foundation; either version 3, or (at your option) |
ee890fe2 SS |
12 | any later version. |
13 | ||
7ec022b2 | 14 | GCC is distributed in the hope that it will be useful, |
ee890fe2 SS |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
2f83c7d6 NC |
20 | along with GCC; see the file COPYING3. If not see |
21 | <http://www.gnu.org/licenses/>. */ | |
ee890fe2 SS |
22 | |
23 | #include "config.h" | |
24 | #include "system.h" | |
4977bab6 ZW |
25 | #include "coretypes.h" |
26 | #include "tm.h" | |
ee890fe2 SS |
27 | #include "rtl.h" |
28 | #include "regs.h" | |
29 | #include "hard-reg-set.h" | |
30 | #include "real.h" | |
31 | #include "insn-config.h" | |
32 | #include "conditions.h" | |
33 | #include "insn-flags.h" | |
34 | #include "output.h" | |
35 | #include "insn-attr.h" | |
36 | #include "flags.h" | |
37 | #include "tree.h" | |
38 | #include "expr.h" | |
39 | #include "reload.h" | |
ee890fe2 SS |
40 | #include "function.h" |
41 | #include "ggc.h" | |
3ac88239 | 42 | #include "langhooks.h" |
1adaa117 | 43 | #include "target.h" |
245ff137 | 44 | #include "tm_p.h" |
4c714dd4 | 45 | #include "toplev.h" |
11abc112 | 46 | #include "hashtab.h" |
6fb5fa3c | 47 | #include "df.h" |
9faca04e | 48 | #include "debug.h" |
ee890fe2 | 49 | |
699c914a MS |
50 | /* Darwin supports a feature called fix-and-continue, which is used |
51 | for rapid turn around debugging. When code is compiled with the | |
52 | -mfix-and-continue flag, two changes are made to the generated code | |
53 | that allow the system to do things that it would normally not be | |
54 | able to do easily. These changes allow gdb to load in | |
55 | recompilation of a translation unit that has been changed into a | |
56 | running program and replace existing functions and methods of that | |
56f42830 | 57 | translation unit with versions of those functions and methods |
699c914a | 58 | from the newly compiled translation unit. The new functions access |
d6ff8575 MS |
59 | the existing static symbols from the old translation unit, if the |
60 | symbol existed in the unit to be replaced, and from the new | |
61 | translation unit, otherwise. | |
699c914a | 62 | |
de2ab0ca | 63 | The changes are to insert 5 nops at the beginning of all functions |
d6ff8575 | 64 | and to use indirection to get at static symbols. The 5 nops |
699c914a MS |
65 | are required by consumers of the generated code. Currently, gdb |
66 | uses this to patch in a jump to the overriding function, this | |
67 | allows all uses of the old name to forward to the replacement, | |
c112cf2b | 68 | including existing function pointers and virtual methods. See |
699c914a | 69 | rs6000_emit_prologue for the code that handles the nop insertions. |
083cad55 | 70 | |
699c914a | 71 | The added indirection allows gdb to redirect accesses to static |
d6ff8575 MS |
72 | symbols from the newly loaded translation unit to the existing |
73 | symbol, if any. @code{static} symbols are special and are handled by | |
74 | setting the second word in the .non_lazy_symbol_pointer data | |
75 | structure to symbol. See indirect_data for the code that handles | |
76 | the extra indirection, and machopic_output_indirection and its use | |
77 | of MACHO_SYMBOL_STATIC for the code that handles @code{static} | |
78 | symbol indirection. */ | |
699c914a | 79 | |
56c779bc GK |
80 | /* Section names. */ |
81 | section * darwin_sections[NUM_DARWIN_SECTIONS]; | |
d6b5193b | 82 | |
4f8bdef9 EC |
83 | /* True if we're setting __attribute__ ((ms_struct)). */ |
84 | int darwin_ms_struct = false; | |
85 | ||
d6b5193b RS |
86 | /* A get_unnamed_section callback used to switch to an ObjC section. |
87 | DIRECTIVE is as for output_section_asm_op. */ | |
88 | ||
89 | static void | |
90 | output_objc_section_asm_op (const void *directive) | |
91 | { | |
56c779bc | 92 | static bool been_here = false; |
d6b5193b | 93 | |
56c779bc | 94 | if (! been_here) |
d6b5193b | 95 | { |
083cad55 | 96 | static const enum darwin_section_enum tomark[] = |
56c779bc GK |
97 | { |
98 | /* written, cold -> hot */ | |
99 | objc_cat_cls_meth_section, | |
100 | objc_cat_inst_meth_section, | |
101 | objc_string_object_section, | |
102 | objc_constant_string_object_section, | |
103 | objc_selector_refs_section, | |
104 | objc_selector_fixup_section, | |
105 | objc_cls_refs_section, | |
106 | objc_class_section, | |
107 | objc_meta_class_section, | |
108 | /* shared, hot -> cold */ | |
109 | objc_cls_meth_section, | |
110 | objc_inst_meth_section, | |
111 | objc_protocol_section, | |
112 | objc_class_names_section, | |
113 | objc_meth_var_types_section, | |
114 | objc_meth_var_names_section, | |
115 | objc_category_section, | |
116 | objc_class_vars_section, | |
117 | objc_instance_vars_section, | |
118 | objc_module_info_section, | |
119 | objc_symbols_section | |
120 | }; | |
121 | size_t i; | |
083cad55 | 122 | |
56c779bc GK |
123 | been_here = true; |
124 | for (i = 0; i < ARRAY_SIZE (tomark); i++) | |
125 | switch_to_section (darwin_sections[tomark[i]]); | |
d6b5193b RS |
126 | } |
127 | output_section_asm_op (directive); | |
128 | } | |
129 | ||
130 | /* Implement TARGET_ASM_INIT_SECTIONS. */ | |
131 | ||
132 | void | |
133 | darwin_init_sections (void) | |
134 | { | |
56c779bc GK |
135 | #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \ |
136 | darwin_sections[NAME] = \ | |
137 | get_unnamed_section (FLAGS, (OBJC \ | |
138 | ? output_objc_section_asm_op \ | |
139 | : output_section_asm_op), \ | |
140 | "\t" DIRECTIVE); | |
141 | #include "config/darwin-sections.def" | |
d6b5193b RS |
142 | #undef DEF_SECTION |
143 | ||
56c779bc GK |
144 | readonly_data_section = darwin_sections[const_section]; |
145 | exception_section = darwin_sections[darwin_exception_section]; | |
146 | eh_frame_section = darwin_sections[darwin_eh_frame_section]; | |
d6b5193b | 147 | } |
699c914a | 148 | |
ee890fe2 | 149 | int |
9c808aad | 150 | name_needs_quotes (const char *name) |
ee890fe2 SS |
151 | { |
152 | int c; | |
153 | while ((c = *name++) != '\0') | |
6f94a68e | 154 | if (! ISIDNUM (c) && c != '.' && c != '$') |
ee890fe2 SS |
155 | return 1; |
156 | return 0; | |
157 | } | |
158 | ||
16515e5c | 159 | /* Return true if SYM_REF can be used without an indirection. */ |
11abc112 MM |
160 | static int |
161 | machopic_symbol_defined_p (rtx sym_ref) | |
ee890fe2 | 162 | { |
16515e5c AP |
163 | if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED) |
164 | return true; | |
165 | ||
166 | /* If a symbol references local and is not an extern to this | |
167 | file, then the symbol might be able to declared as defined. */ | |
168 | if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref)) | |
169 | { | |
170 | /* If the symbol references a variable and the variable is a | |
171 | common symbol, then this symbol is not defined. */ | |
172 | if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE) | |
173 | { | |
174 | tree decl = SYMBOL_REF_DECL (sym_ref); | |
175 | if (!decl) | |
176 | return true; | |
177 | if (DECL_COMMON (decl)) | |
178 | return false; | |
179 | } | |
180 | return true; | |
181 | } | |
182 | return false; | |
ee890fe2 SS |
183 | } |
184 | ||
11abc112 MM |
185 | /* This module assumes that (const (symbol_ref "foo")) is a legal pic |
186 | reference, which will not be changed. */ | |
9c808aad | 187 | |
ee890fe2 | 188 | enum machopic_addr_class |
11abc112 | 189 | machopic_classify_symbol (rtx sym_ref) |
ee890fe2 | 190 | { |
11abc112 MM |
191 | int flags; |
192 | bool function_p; | |
193 | ||
194 | flags = SYMBOL_REF_FLAGS (sym_ref); | |
195 | function_p = SYMBOL_REF_FUNCTION_P (sym_ref); | |
196 | if (machopic_symbol_defined_p (sym_ref)) | |
083cad55 | 197 | return (function_p |
11abc112 MM |
198 | ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA); |
199 | else | |
083cad55 | 200 | return (function_p |
11abc112 | 201 | ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA); |
ee890fe2 SS |
202 | } |
203 | ||
699c914a MS |
204 | #ifndef TARGET_FIX_AND_CONTINUE |
205 | #define TARGET_FIX_AND_CONTINUE 0 | |
206 | #endif | |
207 | ||
208 | /* Indicate when fix-and-continue style code generation is being used | |
209 | and when a reference to data should be indirected so that it can be | |
35fd3193 | 210 | rebound in a new translation unit to reference the original instance |
699c914a MS |
211 | of that data. Symbol names that are for code generation local to |
212 | the translation unit are bound to the new translation unit; | |
213 | currently this means symbols that begin with L or _OBJC_; | |
214 | otherwise, we indicate that an indirect reference should be made to | |
215 | permit the runtime to rebind new instances of the translation unit | |
216 | to the original instance of the data. */ | |
217 | ||
218 | static int | |
219 | indirect_data (rtx sym_ref) | |
220 | { | |
221 | int lprefix; | |
222 | const char *name; | |
223 | ||
62baeb4f EC |
224 | /* If we aren't generating fix-and-continue code, don't do anything |
225 | special. */ | |
699c914a MS |
226 | if (TARGET_FIX_AND_CONTINUE == 0) |
227 | return 0; | |
228 | ||
229 | /* Otherwise, all symbol except symbols that begin with L or _OBJC_ | |
230 | are indirected. Symbols that begin with L and _OBJC_ are always | |
231 | bound to the current translation unit as they are used for | |
232 | generated local data of the translation unit. */ | |
233 | ||
234 | name = XSTR (sym_ref, 0); | |
235 | ||
236 | lprefix = (((name[0] == '*' || name[0] == '&') | |
237 | && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L'))) | |
ad6aaeb6 | 238 | || (strncmp (name, "_OBJC_", 6) == 0)); |
699c914a MS |
239 | |
240 | return ! lprefix; | |
241 | } | |
242 | ||
243 | ||
ee890fe2 | 244 | static int |
11abc112 | 245 | machopic_data_defined_p (rtx sym_ref) |
ee890fe2 | 246 | { |
699c914a MS |
247 | if (indirect_data (sym_ref)) |
248 | return 0; | |
249 | ||
11abc112 | 250 | switch (machopic_classify_symbol (sym_ref)) |
ee890fe2 SS |
251 | { |
252 | case MACHOPIC_DEFINED_DATA: | |
0e1ad529 | 253 | case MACHOPIC_DEFINED_FUNCTION: |
ee890fe2 SS |
254 | return 1; |
255 | default: | |
256 | return 0; | |
257 | } | |
258 | } | |
259 | ||
ee890fe2 | 260 | void |
11abc112 | 261 | machopic_define_symbol (rtx mem) |
ee890fe2 | 262 | { |
11abc112 | 263 | rtx sym_ref; |
083cad55 | 264 | |
992d08b1 | 265 | gcc_assert (GET_CODE (mem) == MEM); |
11abc112 MM |
266 | sym_ref = XEXP (mem, 0); |
267 | SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED; | |
ee890fe2 SS |
268 | } |
269 | ||
08a6a74b | 270 | /* Return either ORIG or: |
ee890fe2 | 271 | |
08a6a74b | 272 | (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET)) |
11abc112 | 273 | |
08a6a74b | 274 | depending on MACHO_DYNAMIC_NO_PIC_P. */ |
11abc112 | 275 | rtx |
08a6a74b | 276 | machopic_gen_offset (rtx orig) |
11abc112 | 277 | { |
08a6a74b | 278 | if (MACHO_DYNAMIC_NO_PIC_P) |
14a07c92 PB |
279 | return orig; |
280 | else | |
08a6a74b RS |
281 | { |
282 | /* Play games to avoid marking the function as needing pic if we | |
283 | are being called as part of the cost-estimation process. */ | |
04ef80ce | 284 | if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl) |
08a6a74b RS |
285 | crtl->uses_pic_offset_table = 1; |
286 | orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig), | |
287 | UNSPEC_MACHOPIC_OFFSET); | |
288 | return gen_rtx_CONST (Pmode, orig); | |
289 | } | |
14a07c92 PB |
290 | } |
291 | ||
1622229c AP |
292 | static GTY(()) const char * function_base_func_name; |
293 | static GTY(()) int current_pic_label_num; | |
294 | ||
295 | void | |
296 | machopic_output_function_base_name (FILE *file) | |
297 | { | |
298 | const char *current_name; | |
299 | ||
ff482c8d | 300 | /* If dynamic-no-pic is on, we should not get here. */ |
992d08b1 | 301 | gcc_assert (!MACHO_DYNAMIC_NO_PIC_P); |
9c808aad | 302 | current_name = |
1622229c | 303 | IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)); |
6788f5ca | 304 | if (function_base_func_name != current_name) |
ee890fe2 | 305 | { |
ee890fe2 | 306 | ++current_pic_label_num; |
6788f5ca | 307 | function_base_func_name = current_name; |
ee890fe2 | 308 | } |
1622229c | 309 | fprintf (file, "\"L%011d$pb\"", current_pic_label_num); |
ee890fe2 SS |
310 | } |
311 | ||
11abc112 MM |
312 | /* The suffix attached to non-lazy pointer symbols. */ |
313 | #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr" | |
314 | /* The suffix attached to stub symbols. */ | |
315 | #define STUB_SUFFIX "$stub" | |
ee890fe2 | 316 | |
d1b38208 | 317 | typedef struct GTY (()) machopic_indirection |
ee890fe2 | 318 | { |
11abc112 MM |
319 | /* The SYMBOL_REF for the entity referenced. */ |
320 | rtx symbol; | |
1adaa117 GK |
321 | /* The name of the stub or non-lazy pointer. */ |
322 | const char * ptr_name; | |
11abc112 MM |
323 | /* True iff this entry is for a stub (as opposed to a non-lazy |
324 | pointer). */ | |
325 | bool stub_p; | |
326 | /* True iff this stub or pointer pointer has been referenced. */ | |
327 | bool used; | |
328 | } machopic_indirection; | |
329 | ||
330 | /* A table mapping stub names and non-lazy pointer names to | |
331 | SYMBOL_REFs for the stubbed-to and pointed-to entities. */ | |
332 | ||
083cad55 | 333 | static GTY ((param_is (struct machopic_indirection))) htab_t |
11abc112 MM |
334 | machopic_indirections; |
335 | ||
336 | /* Return a hash value for a SLOT in the indirections hash table. */ | |
337 | ||
338 | static hashval_t | |
339 | machopic_indirection_hash (const void *slot) | |
340 | { | |
341 | const machopic_indirection *p = (const machopic_indirection *) slot; | |
1adaa117 | 342 | return htab_hash_string (p->ptr_name); |
11abc112 | 343 | } |
9c808aad | 344 | |
11abc112 MM |
345 | /* Returns true if the KEY is the same as that associated with |
346 | SLOT. */ | |
347 | ||
348 | static int | |
349 | machopic_indirection_eq (const void *slot, const void *key) | |
350 | { | |
5ead67f6 KG |
351 | return strcmp (((const machopic_indirection *) slot)->ptr_name, |
352 | (const char *) key) == 0; | |
11abc112 | 353 | } |
ee890fe2 | 354 | |
11abc112 MM |
355 | /* Return the name of the non-lazy pointer (if STUB_P is false) or |
356 | stub (if STUB_B is true) corresponding to the given name. */ | |
df56a27f | 357 | |
11abc112 MM |
358 | const char * |
359 | machopic_indirection_name (rtx sym_ref, bool stub_p) | |
360 | { | |
361 | char *buffer; | |
362 | const char *name = XSTR (sym_ref, 0); | |
1adaa117 | 363 | size_t namelen = strlen (name); |
11abc112 | 364 | machopic_indirection *p; |
1adaa117 | 365 | void ** slot; |
3d20d4d8 MS |
366 | bool saw_star = false; |
367 | bool needs_quotes; | |
368 | const char *suffix; | |
369 | const char *prefix = user_label_prefix; | |
370 | const char *quote = ""; | |
0a6a4494 AO |
371 | tree id; |
372 | ||
373 | id = maybe_get_identifier (name); | |
374 | if (id) | |
375 | { | |
376 | tree id_orig = id; | |
377 | ||
378 | while (IDENTIFIER_TRANSPARENT_ALIAS (id)) | |
379 | id = TREE_CHAIN (id); | |
380 | if (id != id_orig) | |
381 | { | |
382 | name = IDENTIFIER_POINTER (id); | |
383 | namelen = strlen (name); | |
384 | } | |
385 | } | |
083cad55 | 386 | |
3d20d4d8 | 387 | if (name[0] == '*') |
df56a27f | 388 | { |
3d20d4d8 MS |
389 | saw_star = true; |
390 | prefix = ""; | |
391 | ++name; | |
392 | --namelen; | |
11abc112 | 393 | } |
3d20d4d8 MS |
394 | |
395 | needs_quotes = name_needs_quotes (name); | |
396 | if (needs_quotes) | |
11abc112 | 397 | { |
3d20d4d8 | 398 | quote = "\""; |
df56a27f SS |
399 | } |
400 | ||
3d20d4d8 MS |
401 | if (stub_p) |
402 | suffix = STUB_SUFFIX; | |
403 | else | |
404 | suffix = NON_LAZY_POINTER_SUFFIX; | |
405 | ||
5ead67f6 | 406 | buffer = XALLOCAVEC (char, strlen ("&L") |
3d20d4d8 MS |
407 | + strlen (prefix) |
408 | + namelen | |
409 | + strlen (suffix) | |
410 | + 2 * strlen (quote) | |
411 | + 1 /* '\0' */); | |
412 | ||
413 | /* Construct the name of the non-lazy pointer or stub. */ | |
414 | sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote); | |
415 | ||
1adaa117 | 416 | if (!machopic_indirections) |
083cad55 | 417 | machopic_indirections = htab_create_ggc (37, |
1adaa117 GK |
418 | machopic_indirection_hash, |
419 | machopic_indirection_eq, | |
420 | /*htab_del=*/NULL); | |
083cad55 | 421 | |
1adaa117 GK |
422 | slot = htab_find_slot_with_hash (machopic_indirections, buffer, |
423 | htab_hash_string (buffer), INSERT); | |
424 | if (*slot) | |
425 | { | |
426 | p = (machopic_indirection *) *slot; | |
427 | } | |
428 | else | |
11abc112 | 429 | { |
11abc112 MM |
430 | p = (machopic_indirection *) ggc_alloc (sizeof (machopic_indirection)); |
431 | p->symbol = sym_ref; | |
1adaa117 | 432 | p->ptr_name = xstrdup (buffer); |
11abc112 | 433 | p->stub_p = stub_p; |
1adaa117 GK |
434 | p->used = false; |
435 | *slot = p; | |
11abc112 | 436 | } |
083cad55 | 437 | |
1adaa117 | 438 | return p->ptr_name; |
ee890fe2 SS |
439 | } |
440 | ||
11abc112 | 441 | /* Return the name of the stub for the mcount function. */ |
ee890fe2 | 442 | |
11abc112 MM |
443 | const char* |
444 | machopic_mcount_stub_name (void) | |
ee890fe2 | 445 | { |
76f60aa5 AP |
446 | rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount"); |
447 | return machopic_indirection_name (symbol, /*stub_p=*/true); | |
ee890fe2 SS |
448 | } |
449 | ||
11abc112 MM |
450 | /* If NAME is the name of a stub or a non-lazy pointer , mark the stub |
451 | or non-lazy pointer as used -- and mark the object to which the | |
452 | pointer/stub refers as used as well, since the pointer/stub will | |
453 | emit a reference to it. */ | |
454 | ||
ee890fe2 | 455 | void |
11abc112 | 456 | machopic_validate_stub_or_non_lazy_ptr (const char *name) |
ee890fe2 | 457 | { |
11abc112 | 458 | machopic_indirection *p; |
083cad55 EC |
459 | |
460 | p = ((machopic_indirection *) | |
1adaa117 GK |
461 | (htab_find_with_hash (machopic_indirections, name, |
462 | htab_hash_string (name)))); | |
463 | if (p && ! p->used) | |
11abc112 | 464 | { |
1adaa117 GK |
465 | const char *real_name; |
466 | tree id; | |
083cad55 | 467 | |
1adaa117 GK |
468 | p->used = true; |
469 | ||
ca472546 GK |
470 | /* Do what output_addr_const will do when we actually call it. */ |
471 | if (SYMBOL_REF_DECL (p->symbol)) | |
472 | mark_decl_referenced (SYMBOL_REF_DECL (p->symbol)); | |
473 | ||
474 | real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0)); | |
083cad55 | 475 | |
1adaa117 GK |
476 | id = maybe_get_identifier (real_name); |
477 | if (id) | |
478 | mark_referenced (id); | |
11abc112 | 479 | } |
ee890fe2 SS |
480 | } |
481 | ||
482 | /* Transform ORIG, which may be any data source, to the corresponding | |
483 | source using indirections. */ | |
484 | ||
485 | rtx | |
9c808aad | 486 | machopic_indirect_data_reference (rtx orig, rtx reg) |
ee890fe2 SS |
487 | { |
488 | rtx ptr_ref = orig; | |
9c808aad | 489 | |
ee890fe2 SS |
490 | if (! MACHOPIC_INDIRECT) |
491 | return orig; | |
492 | ||
493 | if (GET_CODE (orig) == SYMBOL_REF) | |
494 | { | |
11abc112 | 495 | int defined = machopic_data_defined_p (orig); |
ab82a49f AP |
496 | |
497 | if (defined && MACHO_DYNAMIC_NO_PIC_P) | |
498 | { | |
499 | #if defined (TARGET_TOC) | |
4f8dbd34 | 500 | /* Create a new register for CSE opportunities. */ |
b3a13419 | 501 | rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode)); |
4f8dbd34 AP |
502 | emit_insn (gen_macho_high (hi_reg, orig)); |
503 | emit_insn (gen_macho_low (reg, hi_reg, orig)); | |
ab82a49f AP |
504 | #else |
505 | /* some other cpu -- writeme! */ | |
992d08b1 | 506 | gcc_unreachable (); |
ab82a49f AP |
507 | #endif |
508 | return reg; | |
509 | } | |
510 | else if (defined) | |
ee890fe2 | 511 | { |
7ae8cf75 | 512 | #if defined (TARGET_TOC) || defined (HAVE_lo_sum) |
08a6a74b | 513 | rtx offset = machopic_gen_offset (orig); |
7ae8cf75 | 514 | #endif |
ee890fe2 SS |
515 | |
516 | #if defined (TARGET_TOC) /* i.e., PowerPC */ | |
b3a13419 ILT |
517 | rtx hi_sum_reg = (!can_create_pseudo_p () |
518 | ? reg | |
519 | : gen_reg_rtx (Pmode)); | |
ee890fe2 | 520 | |
992d08b1 | 521 | gcc_assert (reg); |
ee890fe2 | 522 | |
6f94a68e GK |
523 | emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, |
524 | gen_rtx_PLUS (Pmode, pic_offset_table_rtx, | |
525 | gen_rtx_HIGH (Pmode, offset)))); | |
526 | emit_insn (gen_rtx_SET (Pmode, reg, | |
bc211e05 JH |
527 | gen_rtx_LO_SUM (Pmode, hi_sum_reg, |
528 | copy_rtx (offset)))); | |
ee890fe2 SS |
529 | |
530 | orig = reg; | |
531 | #else | |
532 | #if defined (HAVE_lo_sum) | |
992d08b1 | 533 | gcc_assert (reg); |
ee890fe2 | 534 | |
6f94a68e GK |
535 | emit_insn (gen_rtx_SET (VOIDmode, reg, |
536 | gen_rtx_HIGH (Pmode, offset))); | |
537 | emit_insn (gen_rtx_SET (VOIDmode, reg, | |
bc211e05 JH |
538 | gen_rtx_LO_SUM (Pmode, reg, |
539 | copy_rtx (offset)))); | |
c41c1387 | 540 | emit_use (pic_offset_table_rtx); |
ee890fe2 | 541 | |
6f94a68e | 542 | orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg); |
ee890fe2 SS |
543 | #endif |
544 | #endif | |
545 | return orig; | |
546 | } | |
547 | ||
828a4fe4 | 548 | ptr_ref = (gen_rtx_SYMBOL_REF |
083cad55 | 549 | (Pmode, |
11abc112 | 550 | machopic_indirection_name (orig, /*stub_p=*/false))); |
ee890fe2 | 551 | |
c185c797 | 552 | SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig); |
c0d594f1 | 553 | |
542a8afa | 554 | ptr_ref = gen_const_mem (Pmode, ptr_ref); |
828a4fe4 | 555 | machopic_define_symbol (ptr_ref); |
ee890fe2 SS |
556 | |
557 | return ptr_ref; | |
558 | } | |
559 | else if (GET_CODE (orig) == CONST) | |
560 | { | |
561 | rtx base, result; | |
562 | ||
563 | /* legitimize both operands of the PLUS */ | |
564 | if (GET_CODE (XEXP (orig, 0)) == PLUS) | |
565 | { | |
566 | base = machopic_indirect_data_reference (XEXP (XEXP (orig, 0), 0), | |
567 | reg); | |
568 | orig = machopic_indirect_data_reference (XEXP (XEXP (orig, 0), 1), | |
569 | (base == reg ? 0 : reg)); | |
570 | } | |
9c808aad | 571 | else |
ee890fe2 SS |
572 | return orig; |
573 | ||
574 | if (MACHOPIC_PURE && GET_CODE (orig) == CONST_INT) | |
ed8908e7 | 575 | result = plus_constant (base, INTVAL (orig)); |
ee890fe2 | 576 | else |
6f94a68e | 577 | result = gen_rtx_PLUS (Pmode, base, orig); |
ee890fe2 | 578 | |
ee890fe2 SS |
579 | if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM) |
580 | { | |
581 | if (reg) | |
582 | { | |
583 | emit_move_insn (reg, result); | |
584 | result = reg; | |
585 | } | |
586 | else | |
587 | { | |
588 | result = force_reg (GET_MODE (result), result); | |
589 | } | |
590 | } | |
591 | ||
592 | return result; | |
593 | ||
594 | } | |
595 | else if (GET_CODE (orig) == MEM) | |
596 | XEXP (ptr_ref, 0) = machopic_indirect_data_reference (XEXP (orig, 0), reg); | |
597 | /* When the target is i386, this code prevents crashes due to the | |
598 | compiler's ignorance on how to move the PIC base register to | |
599 | other registers. (The reload phase sometimes introduces such | |
600 | insns.) */ | |
601 | else if (GET_CODE (orig) == PLUS | |
602 | && GET_CODE (XEXP (orig, 0)) == REG | |
603 | && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM | |
604 | #ifdef I386 | |
605 | /* Prevent the same register from being erroneously used | |
606 | as both the base and index registers. */ | |
607 | && GET_CODE (XEXP (orig, 1)) == CONST | |
608 | #endif | |
609 | && reg) | |
610 | { | |
611 | emit_move_insn (reg, XEXP (orig, 0)); | |
612 | XEXP (ptr_ref, 0) = reg; | |
613 | } | |
614 | return ptr_ref; | |
615 | } | |
616 | ||
ee890fe2 SS |
617 | /* Transform TARGET (a MEM), which is a function call target, to the |
618 | corresponding symbol_stub if necessary. Return a new MEM. */ | |
619 | ||
620 | rtx | |
9c808aad | 621 | machopic_indirect_call_target (rtx target) |
ee890fe2 SS |
622 | { |
623 | if (GET_CODE (target) != MEM) | |
624 | return target; | |
625 | ||
083cad55 | 626 | if (MACHOPIC_INDIRECT |
11abc112 MM |
627 | && GET_CODE (XEXP (target, 0)) == SYMBOL_REF |
628 | && !(SYMBOL_REF_FLAGS (XEXP (target, 0)) | |
629 | & MACHO_SYMBOL_FLAG_DEFINED)) | |
9c808aad | 630 | { |
11abc112 | 631 | rtx sym_ref = XEXP (target, 0); |
083cad55 | 632 | const char *stub_name = machopic_indirection_name (sym_ref, |
11abc112 MM |
633 | /*stub_p=*/true); |
634 | enum machine_mode mode = GET_MODE (sym_ref); | |
083cad55 | 635 | |
11abc112 | 636 | XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name); |
c185c797 | 637 | SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref); |
389fdba0 | 638 | MEM_READONLY_P (target) = 1; |
542a8afa | 639 | MEM_NOTRAP_P (target) = 1; |
ee890fe2 SS |
640 | } |
641 | ||
642 | return target; | |
643 | } | |
644 | ||
645 | rtx | |
9c808aad | 646 | machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg) |
ee890fe2 SS |
647 | { |
648 | rtx pic_ref = orig; | |
649 | ||
ab82a49f | 650 | if (! MACHOPIC_INDIRECT) |
ee890fe2 SS |
651 | return orig; |
652 | ||
653 | /* First handle a simple SYMBOL_REF or LABEL_REF */ | |
654 | if (GET_CODE (orig) == LABEL_REF | |
655 | || (GET_CODE (orig) == SYMBOL_REF | |
656 | )) | |
657 | { | |
658 | /* addr(foo) = &func+(foo-func) */ | |
ee890fe2 SS |
659 | orig = machopic_indirect_data_reference (orig, reg); |
660 | ||
9c808aad | 661 | if (GET_CODE (orig) == PLUS |
ee890fe2 SS |
662 | && GET_CODE (XEXP (orig, 0)) == REG) |
663 | { | |
664 | if (reg == 0) | |
665 | return force_reg (mode, orig); | |
666 | ||
667 | emit_move_insn (reg, orig); | |
668 | return reg; | |
9c808aad | 669 | } |
ee890fe2 | 670 | |
ee890fe2 SS |
671 | if (GET_CODE (orig) == MEM) |
672 | { | |
673 | if (reg == 0) | |
674 | { | |
992d08b1 NS |
675 | gcc_assert (!reload_in_progress); |
676 | reg = gen_reg_rtx (Pmode); | |
ee890fe2 | 677 | } |
9c808aad | 678 | |
ee890fe2 | 679 | #ifdef HAVE_lo_sum |
ab82a49f AP |
680 | if (MACHO_DYNAMIC_NO_PIC_P |
681 | && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF | |
682 | || GET_CODE (XEXP (orig, 0)) == LABEL_REF)) | |
683 | { | |
684 | #if defined (TARGET_TOC) /* ppc */ | |
b3a13419 ILT |
685 | rtx temp_reg = (!can_create_pseudo_p () |
686 | ? reg : | |
687 | gen_reg_rtx (Pmode)); | |
ab82a49f AP |
688 | rtx asym = XEXP (orig, 0); |
689 | rtx mem; | |
690 | ||
b8a55285 | 691 | emit_insn (gen_macho_high (temp_reg, asym)); |
542a8afa | 692 | mem = gen_const_mem (GET_MODE (orig), |
bc211e05 JH |
693 | gen_rtx_LO_SUM (Pmode, temp_reg, |
694 | copy_rtx (asym))); | |
6f94a68e | 695 | emit_insn (gen_rtx_SET (VOIDmode, reg, mem)); |
ab82a49f | 696 | #else |
62baeb4f EC |
697 | /* Some other CPU -- WriteMe! but right now there are no other |
698 | platforms that can use dynamic-no-pic */ | |
992d08b1 | 699 | gcc_unreachable (); |
ab82a49f AP |
700 | #endif |
701 | pic_ref = reg; | |
702 | } | |
703 | else | |
9c808aad | 704 | if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF |
ee890fe2 SS |
705 | || GET_CODE (XEXP (orig, 0)) == LABEL_REF) |
706 | { | |
08a6a74b | 707 | rtx offset = machopic_gen_offset (XEXP (orig, 0)); |
ee890fe2 SS |
708 | #if defined (TARGET_TOC) /* i.e., PowerPC */ |
709 | /* Generating a new reg may expose opportunities for | |
710 | common subexpression elimination. */ | |
b3a13419 ILT |
711 | rtx hi_sum_reg = (!can_create_pseudo_p () |
712 | ? reg | |
713 | : gen_reg_rtx (Pmode)); | |
6f94a68e GK |
714 | rtx mem; |
715 | rtx insn; | |
716 | rtx sum; | |
083cad55 | 717 | |
6f94a68e GK |
718 | sum = gen_rtx_HIGH (Pmode, offset); |
719 | if (! MACHO_DYNAMIC_NO_PIC_P) | |
720 | sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum); | |
ee890fe2 | 721 | |
6f94a68e GK |
722 | emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum)); |
723 | ||
542a8afa | 724 | mem = gen_const_mem (GET_MODE (orig), |
083cad55 | 725 | gen_rtx_LO_SUM (Pmode, |
bc211e05 JH |
726 | hi_sum_reg, |
727 | copy_rtx (offset))); | |
6f94a68e | 728 | insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem)); |
bd94cb6e | 729 | set_unique_reg_note (insn, REG_EQUAL, pic_ref); |
6f94a68e GK |
730 | |
731 | pic_ref = reg; | |
ee890fe2 | 732 | #else |
c41c1387 | 733 | emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM)); |
6f94a68e GK |
734 | |
735 | emit_insn (gen_rtx_SET (VOIDmode, reg, | |
736 | gen_rtx_HIGH (Pmode, | |
083cad55 | 737 | gen_rtx_CONST (Pmode, |
6f94a68e GK |
738 | offset)))); |
739 | emit_insn (gen_rtx_SET (VOIDmode, reg, | |
740 | gen_rtx_LO_SUM (Pmode, reg, | |
bc211e05 JH |
741 | gen_rtx_CONST (Pmode, |
742 | copy_rtx (offset))))); | |
6f94a68e GK |
743 | pic_ref = gen_rtx_PLUS (Pmode, |
744 | pic_offset_table_rtx, reg); | |
ee890fe2 SS |
745 | #endif |
746 | } | |
747 | else | |
748 | #endif /* HAVE_lo_sum */ | |
749 | { | |
750 | rtx pic = pic_offset_table_rtx; | |
751 | if (GET_CODE (pic) != REG) | |
752 | { | |
753 | emit_move_insn (reg, pic); | |
754 | pic = reg; | |
755 | } | |
756 | #if 0 | |
c41c1387 | 757 | emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM)); |
ee890fe2 SS |
758 | #endif |
759 | ||
7d072037 | 760 | if (reload_in_progress) |
6fb5fa3c | 761 | df_set_regs_ever_live (REGNO (pic), true); |
14a07c92 | 762 | pic_ref = gen_rtx_PLUS (Pmode, pic, |
08a6a74b | 763 | machopic_gen_offset (XEXP (orig, 0))); |
ee890fe2 | 764 | } |
9c808aad | 765 | |
ee890fe2 | 766 | #if !defined (TARGET_TOC) |
ee890fe2 | 767 | emit_move_insn (reg, pic_ref); |
542a8afa | 768 | pic_ref = gen_const_mem (GET_MODE (orig), reg); |
f9b0ac3b | 769 | #endif |
ee890fe2 SS |
770 | } |
771 | else | |
772 | { | |
773 | ||
774 | #ifdef HAVE_lo_sum | |
9c808aad | 775 | if (GET_CODE (orig) == SYMBOL_REF |
ee890fe2 SS |
776 | || GET_CODE (orig) == LABEL_REF) |
777 | { | |
08a6a74b | 778 | rtx offset = machopic_gen_offset (orig); |
ee890fe2 SS |
779 | #if defined (TARGET_TOC) /* i.e., PowerPC */ |
780 | rtx hi_sum_reg; | |
781 | ||
782 | if (reg == 0) | |
783 | { | |
992d08b1 NS |
784 | gcc_assert (!reload_in_progress); |
785 | reg = gen_reg_rtx (Pmode); | |
ee890fe2 | 786 | } |
9c808aad | 787 | |
ee890fe2 SS |
788 | hi_sum_reg = reg; |
789 | ||
6f94a68e GK |
790 | emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, |
791 | (MACHO_DYNAMIC_NO_PIC_P) | |
792 | ? gen_rtx_HIGH (Pmode, offset) | |
793 | : gen_rtx_PLUS (Pmode, | |
794 | pic_offset_table_rtx, | |
083cad55 | 795 | gen_rtx_HIGH (Pmode, |
6f94a68e GK |
796 | offset)))); |
797 | emit_insn (gen_rtx_SET (VOIDmode, reg, | |
798 | gen_rtx_LO_SUM (Pmode, | |
bc211e05 JH |
799 | hi_sum_reg, |
800 | copy_rtx (offset)))); | |
ee890fe2 SS |
801 | pic_ref = reg; |
802 | #else | |
6f94a68e GK |
803 | emit_insn (gen_rtx_SET (VOIDmode, reg, |
804 | gen_rtx_HIGH (Pmode, offset))); | |
805 | emit_insn (gen_rtx_SET (VOIDmode, reg, | |
bc211e05 JH |
806 | gen_rtx_LO_SUM (Pmode, reg, |
807 | copy_rtx (offset)))); | |
6f94a68e GK |
808 | pic_ref = gen_rtx_PLUS (Pmode, |
809 | pic_offset_table_rtx, reg); | |
ee890fe2 SS |
810 | #endif |
811 | } | |
812 | else | |
813 | #endif /* HAVE_lo_sum */ | |
814 | { | |
83cf88cb AP |
815 | if (REG_P (orig) |
816 | || GET_CODE (orig) == SUBREG) | |
ee890fe2 SS |
817 | { |
818 | return orig; | |
819 | } | |
820 | else | |
821 | { | |
822 | rtx pic = pic_offset_table_rtx; | |
823 | if (GET_CODE (pic) != REG) | |
824 | { | |
825 | emit_move_insn (reg, pic); | |
826 | pic = reg; | |
827 | } | |
828 | #if 0 | |
c41c1387 | 829 | emit_use (pic_offset_table_rtx); |
ee890fe2 | 830 | #endif |
7d072037 | 831 | if (reload_in_progress) |
6fb5fa3c | 832 | df_set_regs_ever_live (REGNO (pic), true); |
6f94a68e GK |
833 | pic_ref = gen_rtx_PLUS (Pmode, |
834 | pic, | |
08a6a74b | 835 | machopic_gen_offset (orig)); |
ee890fe2 SS |
836 | } |
837 | } | |
838 | } | |
839 | ||
ee890fe2 SS |
840 | if (GET_CODE (pic_ref) != REG) |
841 | { | |
842 | if (reg != 0) | |
843 | { | |
844 | emit_move_insn (reg, pic_ref); | |
845 | return reg; | |
846 | } | |
847 | else | |
848 | { | |
849 | return force_reg (mode, pic_ref); | |
850 | } | |
851 | } | |
852 | else | |
853 | { | |
854 | return pic_ref; | |
855 | } | |
856 | } | |
857 | ||
858 | else if (GET_CODE (orig) == SYMBOL_REF) | |
859 | return orig; | |
860 | ||
861 | else if (GET_CODE (orig) == PLUS | |
862 | && (GET_CODE (XEXP (orig, 0)) == MEM | |
863 | || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF | |
864 | || GET_CODE (XEXP (orig, 0)) == LABEL_REF) | |
865 | && XEXP (orig, 0) != pic_offset_table_rtx | |
866 | && GET_CODE (XEXP (orig, 1)) != REG) | |
9c808aad | 867 | |
ee890fe2 SS |
868 | { |
869 | rtx base; | |
870 | int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM); | |
871 | ||
872 | base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg); | |
873 | orig = machopic_legitimize_pic_address (XEXP (orig, 1), | |
874 | Pmode, (base == reg ? 0 : reg)); | |
875 | if (GET_CODE (orig) == CONST_INT) | |
876 | { | |
ed8908e7 | 877 | pic_ref = plus_constant (base, INTVAL (orig)); |
ee890fe2 SS |
878 | is_complex = 1; |
879 | } | |
880 | else | |
6f94a68e | 881 | pic_ref = gen_rtx_PLUS (Pmode, base, orig); |
ee890fe2 | 882 | |
ee890fe2 SS |
883 | if (reg && is_complex) |
884 | { | |
885 | emit_move_insn (reg, pic_ref); | |
886 | pic_ref = reg; | |
887 | } | |
888 | /* Likewise, should we set special REG_NOTEs here? */ | |
889 | } | |
890 | ||
891 | else if (GET_CODE (orig) == CONST) | |
892 | { | |
893 | return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg); | |
894 | } | |
895 | ||
896 | else if (GET_CODE (orig) == MEM | |
897 | && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF) | |
898 | { | |
899 | rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg); | |
389fdba0 | 900 | addr = replace_equiv_address (orig, addr); |
ee890fe2 SS |
901 | emit_move_insn (reg, addr); |
902 | pic_ref = reg; | |
903 | } | |
904 | ||
905 | return pic_ref; | |
906 | } | |
907 | ||
11abc112 MM |
908 | /* Output the stub or non-lazy pointer in *SLOT, if it has been used. |
909 | DATA is the FILE* for assembly output. Called from | |
910 | htab_traverse. */ | |
ee890fe2 | 911 | |
11abc112 MM |
912 | static int |
913 | machopic_output_indirection (void **slot, void *data) | |
ee890fe2 | 914 | { |
11abc112 MM |
915 | machopic_indirection *p = *((machopic_indirection **) slot); |
916 | FILE *asm_out_file = (FILE *) data; | |
917 | rtx symbol; | |
918 | const char *sym_name; | |
919 | const char *ptr_name; | |
083cad55 | 920 | |
11abc112 MM |
921 | if (!p->used) |
922 | return 1; | |
ee890fe2 | 923 | |
11abc112 MM |
924 | symbol = p->symbol; |
925 | sym_name = XSTR (symbol, 0); | |
1adaa117 | 926 | ptr_name = p->ptr_name; |
083cad55 | 927 | |
11abc112 | 928 | if (p->stub_p) |
ee890fe2 | 929 | { |
ee890fe2 SS |
930 | char *sym; |
931 | char *stub; | |
0a6a4494 AO |
932 | tree id; |
933 | ||
934 | id = maybe_get_identifier (sym_name); | |
935 | if (id) | |
936 | { | |
937 | tree id_orig = id; | |
938 | ||
939 | while (IDENTIFIER_TRANSPARENT_ALIAS (id)) | |
940 | id = TREE_CHAIN (id); | |
941 | if (id != id_orig) | |
942 | sym_name = IDENTIFIER_POINTER (id); | |
943 | } | |
ee890fe2 | 944 | |
5ead67f6 | 945 | sym = XALLOCAVEC (char, strlen (sym_name) + 2); |
ee890fe2 SS |
946 | if (sym_name[0] == '*' || sym_name[0] == '&') |
947 | strcpy (sym, sym_name + 1); | |
948 | else if (sym_name[0] == '-' || sym_name[0] == '+') | |
9c808aad | 949 | strcpy (sym, sym_name); |
ee890fe2 | 950 | else |
789a4ea3 | 951 | sprintf (sym, "%s%s", user_label_prefix, sym_name); |
ee890fe2 | 952 | |
5ead67f6 | 953 | stub = XALLOCAVEC (char, strlen (ptr_name) + 2); |
11abc112 MM |
954 | if (ptr_name[0] == '*' || ptr_name[0] == '&') |
955 | strcpy (stub, ptr_name + 1); | |
ee890fe2 | 956 | else |
11abc112 | 957 | sprintf (stub, "%s%s", user_label_prefix, ptr_name); |
ee890fe2 | 958 | |
ca472546 | 959 | machopic_output_stub (asm_out_file, sym, stub); |
ee890fe2 | 960 | } |
699c914a | 961 | else if (! indirect_data (symbol) |
156a126c MS |
962 | && (machopic_symbol_defined_p (symbol) |
963 | || SYMBOL_REF_LOCAL_P (symbol))) | |
ee890fe2 | 964 | { |
d6b5193b | 965 | switch_to_section (data_section); |
11abc112 MM |
966 | assemble_align (GET_MODE_ALIGNMENT (Pmode)); |
967 | assemble_label (ptr_name); | |
968 | assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name), | |
969 | GET_MODE_SIZE (Pmode), | |
970 | GET_MODE_ALIGNMENT (Pmode), 1); | |
ee890fe2 | 971 | } |
11abc112 MM |
972 | else |
973 | { | |
699c914a MS |
974 | rtx init = const0_rtx; |
975 | ||
56c779bc | 976 | switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]); |
c70d0414 HPN |
977 | |
978 | /* Mach-O symbols are passed around in code through indirect | |
979 | references and the original symbol_ref hasn't passed through | |
980 | the generic handling and reference-catching in | |
981 | output_operand, so we need to manually mark weak references | |
982 | as such. */ | |
983 | if (SYMBOL_REF_WEAK (symbol)) | |
984 | { | |
985 | tree decl = SYMBOL_REF_DECL (symbol); | |
986 | gcc_assert (DECL_P (decl)); | |
987 | ||
988 | if (decl != NULL_TREE | |
989 | && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl) | |
990 | /* Handle only actual external-only definitions, not | |
991 | e.g. extern inline code or variables for which | |
992 | storage has been allocated. */ | |
993 | && !TREE_STATIC (decl)) | |
994 | { | |
995 | fputs ("\t.weak_reference ", asm_out_file); | |
996 | assemble_name (asm_out_file, sym_name); | |
997 | fputc ('\n', asm_out_file); | |
998 | } | |
999 | } | |
1000 | ||
11abc112 MM |
1001 | assemble_name (asm_out_file, ptr_name); |
1002 | fprintf (asm_out_file, ":\n"); | |
083cad55 | 1003 | |
11abc112 MM |
1004 | fprintf (asm_out_file, "\t.indirect_symbol "); |
1005 | assemble_name (asm_out_file, sym_name); | |
1006 | fprintf (asm_out_file, "\n"); | |
083cad55 | 1007 | |
699c914a MS |
1008 | /* Variables that are marked with MACHO_SYMBOL_STATIC need to |
1009 | have their symbol name instead of 0 in the second entry of | |
1010 | the non-lazy symbol pointer data structure when they are | |
1011 | defined. This allows the runtime to rebind newer instances | |
1012 | of the translation unit with the original instance of the | |
d6ff8575 | 1013 | symbol. */ |
699c914a MS |
1014 | |
1015 | if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC) | |
1016 | && machopic_symbol_defined_p (symbol)) | |
1017 | init = gen_rtx_SYMBOL_REF (Pmode, sym_name); | |
1018 | ||
1019 | assemble_integer (init, GET_MODE_SIZE (Pmode), | |
11abc112 MM |
1020 | GET_MODE_ALIGNMENT (Pmode), 1); |
1021 | } | |
083cad55 | 1022 | |
11abc112 MM |
1023 | return 1; |
1024 | } | |
1025 | ||
1026 | void | |
1027 | machopic_finish (FILE *asm_out_file) | |
1028 | { | |
1029 | if (machopic_indirections) | |
699c914a | 1030 | htab_traverse_noresize (machopic_indirections, |
11abc112 MM |
1031 | machopic_output_indirection, |
1032 | asm_out_file); | |
ee890fe2 SS |
1033 | } |
1034 | ||
9c808aad AJ |
1035 | int |
1036 | machopic_operand_p (rtx op) | |
ee890fe2 SS |
1037 | { |
1038 | if (MACHOPIC_JUST_INDIRECT) | |
08a6a74b RS |
1039 | return (GET_CODE (op) == SYMBOL_REF |
1040 | && machopic_symbol_defined_p (op)); | |
1041 | else | |
1042 | return (GET_CODE (op) == CONST | |
1043 | && GET_CODE (XEXP (op, 0)) == UNSPEC | |
1044 | && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET); | |
ee890fe2 | 1045 | } |
df56a27f SS |
1046 | |
1047 | /* This function records whether a given name corresponds to a defined | |
1048 | or undefined function or variable, for machopic_classify_ident to | |
1049 | use later. */ | |
1050 | ||
1051 | void | |
9c808aad | 1052 | darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED) |
df56a27f | 1053 | { |
353e51f8 | 1054 | rtx sym_ref; |
df56a27f | 1055 | |
e1a4211d SS |
1056 | /* Do the standard encoding things first. */ |
1057 | default_encode_section_info (decl, rtl, first); | |
1058 | ||
11abc112 MM |
1059 | if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL) |
1060 | return; | |
e1a4211d | 1061 | |
11abc112 MM |
1062 | sym_ref = XEXP (rtl, 0); |
1063 | if (TREE_CODE (decl) == VAR_DECL) | |
1064 | SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE; | |
1065 | ||
1066 | if (!DECL_EXTERNAL (decl) | |
f1a66265 | 1067 | && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl)) |
a9b0b825 | 1068 | && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)) |
df56a27f SS |
1069 | && ((TREE_STATIC (decl) |
1070 | && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl))) | |
bfbdca0b | 1071 | || (!DECL_COMMON (decl) && DECL_INITIAL (decl) |
abe72dd8 | 1072 | && DECL_INITIAL (decl) != error_mark_node))) |
11abc112 | 1073 | SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED; |
699c914a | 1074 | |
d6ff8575 | 1075 | if (! TREE_PUBLIC (decl)) |
699c914a | 1076 | SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC; |
4e08ba6c | 1077 | } |
2cc07db4 | 1078 | |
8e3e233b DP |
1079 | void |
1080 | darwin_mark_decl_preserved (const char *name) | |
1081 | { | |
1082 | fprintf (asm_out_file, ".no_dead_strip "); | |
1083 | assemble_name (asm_out_file, name); | |
1084 | fputc ('\n', asm_out_file); | |
1085 | } | |
1086 | ||
59ff4a1c EC |
1087 | static section * |
1088 | darwin_text_section (int reloc, int weak) | |
ae46c4e0 | 1089 | { |
59ff4a1c EC |
1090 | if (reloc) |
1091 | return (weak | |
1092 | ? darwin_sections[text_unlikely_coal_section] | |
1093 | : unlikely_text_section ()); | |
3d7964d5 | 1094 | else |
59ff4a1c EC |
1095 | return (weak |
1096 | ? darwin_sections[text_coal_section] | |
1097 | : text_section); | |
1098 | } | |
1099 | ||
1100 | static section * | |
1101 | darwin_rodata_section (int weak) | |
1102 | { | |
1103 | return (weak | |
1104 | ? darwin_sections[const_coal_section] | |
1105 | : darwin_sections[const_section]); | |
1106 | } | |
9c808aad | 1107 | |
59ff4a1c EC |
1108 | static section * |
1109 | darwin_mergeable_string_section (tree exp, | |
1110 | unsigned HOST_WIDE_INT align) | |
1111 | { | |
1112 | if (flag_merge_constants | |
1113 | && TREE_CODE (exp) == STRING_CST | |
1114 | && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE | |
1115 | && align <= 256 | |
b69d564b JJ |
1116 | && (int_size_in_bytes (TREE_TYPE (exp)) |
1117 | == TREE_STRING_LENGTH (exp)) | |
9c808aad | 1118 | && ((size_t) TREE_STRING_LENGTH (exp) |
3521b33c | 1119 | == strlen (TREE_STRING_POINTER (exp)) + 1)) |
56c779bc | 1120 | return darwin_sections[cstring_section]; |
59ff4a1c EC |
1121 | |
1122 | return readonly_data_section; | |
1123 | } | |
1124 | ||
5708d18d MS |
1125 | #ifndef HAVE_GAS_LITERAL16 |
1126 | #define HAVE_GAS_LITERAL16 0 | |
1127 | #endif | |
1128 | ||
59ff4a1c EC |
1129 | static section * |
1130 | darwin_mergeable_constant_section (tree exp, | |
1131 | unsigned HOST_WIDE_INT align) | |
1132 | { | |
1133 | enum machine_mode mode = DECL_MODE (exp); | |
1134 | unsigned int modesize = GET_MODE_BITSIZE (mode); | |
1135 | ||
1136 | if (flag_merge_constants | |
1137 | && mode != VOIDmode | |
1138 | && mode != BLKmode | |
1139 | && modesize <= align | |
1140 | && align >= 8 | |
1141 | && align <= 256 | |
1142 | && (align & (align -1)) == 0) | |
ae46c4e0 | 1143 | { |
e1e04267 | 1144 | tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp)); |
ae46c4e0 | 1145 | |
59ff4a1c EC |
1146 | if (TREE_CODE (size) == INTEGER_CST |
1147 | && TREE_INT_CST_LOW (size) == 4 | |
1148 | && TREE_INT_CST_HIGH (size) == 0) | |
1149 | return darwin_sections[literal4_section]; | |
1150 | else if (TREE_CODE (size) == INTEGER_CST | |
1151 | && TREE_INT_CST_LOW (size) == 8 | |
f7288899 | 1152 | && TREE_INT_CST_HIGH (size) == 0) |
59ff4a1c | 1153 | return darwin_sections[literal8_section]; |
5708d18d MS |
1154 | else if (HAVE_GAS_LITERAL16 |
1155 | && TARGET_64BIT | |
59ff4a1c EC |
1156 | && TREE_CODE (size) == INTEGER_CST |
1157 | && TREE_INT_CST_LOW (size) == 16 | |
1158 | && TREE_INT_CST_HIGH (size) == 0) | |
1159 | return darwin_sections[literal16_section]; | |
1160 | else | |
1161 | return readonly_data_section; | |
1162 | } | |
1163 | ||
1164 | return readonly_data_section; | |
1165 | } | |
1166 | ||
24c78b3a RH |
1167 | int |
1168 | machopic_reloc_rw_mask (void) | |
1169 | { | |
1170 | return MACHOPIC_INDIRECT ? 3 : 0; | |
1171 | } | |
1172 | ||
59ff4a1c EC |
1173 | section * |
1174 | machopic_select_section (tree decl, | |
1175 | int reloc, | |
1176 | unsigned HOST_WIDE_INT align) | |
1177 | { | |
1178 | bool weak = (DECL_P (decl) | |
1179 | && DECL_WEAK (decl) | |
1180 | && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl)) | |
1181 | || ! lookup_attribute ("weak_import", | |
1182 | DECL_ATTRIBUTES (decl)))); | |
59ff4a1c EC |
1183 | section *base_section; |
1184 | ||
4f648002 | 1185 | switch (categorize_decl_for_section (decl, reloc)) |
59ff4a1c EC |
1186 | { |
1187 | case SECCAT_TEXT: | |
1188 | base_section = darwin_text_section (reloc, weak); | |
1189 | break; | |
1190 | ||
1191 | case SECCAT_RODATA: | |
1192 | case SECCAT_SRODATA: | |
1193 | base_section = darwin_rodata_section (weak); | |
1194 | break; | |
1195 | ||
1196 | case SECCAT_RODATA_MERGE_STR: | |
1197 | base_section = darwin_mergeable_string_section (decl, align); | |
1198 | break; | |
1199 | ||
1200 | case SECCAT_RODATA_MERGE_STR_INIT: | |
1201 | base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align); | |
1202 | break; | |
1203 | ||
1204 | case SECCAT_RODATA_MERGE_CONST: | |
1205 | base_section = darwin_mergeable_constant_section (decl, align); | |
1206 | break; | |
1207 | ||
1208 | case SECCAT_DATA: | |
1209 | case SECCAT_DATA_REL: | |
1210 | case SECCAT_DATA_REL_LOCAL: | |
1211 | case SECCAT_DATA_REL_RO: | |
1212 | case SECCAT_DATA_REL_RO_LOCAL: | |
1213 | case SECCAT_SDATA: | |
1214 | case SECCAT_TDATA: | |
1215 | case SECCAT_BSS: | |
1216 | case SECCAT_SBSS: | |
1217 | case SECCAT_TBSS: | |
1218 | if (TREE_READONLY (decl) || TREE_CONSTANT (decl)) | |
1219 | base_section = weak ? darwin_sections[const_data_coal_section] | |
1220 | : darwin_sections[const_data_section]; | |
ae46c4e0 | 1221 | else |
59ff4a1c EC |
1222 | base_section = weak ? darwin_sections[data_coal_section] : data_section; |
1223 | break; | |
1224 | ||
1225 | default: | |
1226 | gcc_unreachable (); | |
ae46c4e0 | 1227 | } |
59ff4a1c EC |
1228 | |
1229 | /* Darwin weird special cases. */ | |
1230 | if (TREE_CODE (decl) == CONSTRUCTOR | |
1231 | && TREE_TYPE (decl) | |
1232 | && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE | |
1233 | && TYPE_NAME (TREE_TYPE (decl))) | |
ae46c4e0 | 1234 | { |
59ff4a1c | 1235 | tree name = TYPE_NAME (TREE_TYPE (decl)); |
ae46c4e0 | 1236 | if (TREE_CODE (name) == TYPE_DECL) |
59ff4a1c | 1237 | name = DECL_NAME (name); |
c64de75f ZL |
1238 | |
1239 | if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString")) | |
59ff4a1c EC |
1240 | { |
1241 | if (flag_next_runtime) | |
1242 | return darwin_sections[objc_constant_string_object_section]; | |
1243 | else | |
1244 | return darwin_sections[objc_string_object_section]; | |
1245 | } | |
ae46c4e0 | 1246 | else |
59ff4a1c | 1247 | return base_section; |
ae46c4e0 | 1248 | } |
59ff4a1c EC |
1249 | else if (TREE_CODE (decl) == VAR_DECL |
1250 | && DECL_NAME (decl) | |
1251 | && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE | |
1252 | && IDENTIFIER_POINTER (DECL_NAME (decl)) | |
1253 | && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6)) | |
ae46c4e0 | 1254 | { |
59ff4a1c | 1255 | const char *name = IDENTIFIER_POINTER (DECL_NAME (decl)); |
ae46c4e0 RH |
1256 | |
1257 | if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20)) | |
59ff4a1c | 1258 | return darwin_sections[objc_cls_meth_section]; |
ae46c4e0 | 1259 | else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23)) |
59ff4a1c | 1260 | return darwin_sections[objc_inst_meth_section]; |
ae46c4e0 | 1261 | else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 20)) |
59ff4a1c | 1262 | return darwin_sections[objc_cat_cls_meth_section]; |
ae46c4e0 | 1263 | else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 23)) |
59ff4a1c | 1264 | return darwin_sections[objc_cat_inst_meth_section]; |
ae46c4e0 | 1265 | else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22)) |
59ff4a1c | 1266 | return darwin_sections[objc_class_vars_section]; |
ae46c4e0 | 1267 | else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25)) |
59ff4a1c | 1268 | return darwin_sections[objc_instance_vars_section]; |
ae46c4e0 | 1269 | else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22)) |
59ff4a1c | 1270 | return darwin_sections[objc_cat_cls_meth_section]; |
ae46c4e0 | 1271 | else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17)) |
59ff4a1c | 1272 | return darwin_sections[objc_class_names_section]; |
ae46c4e0 | 1273 | else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20)) |
59ff4a1c | 1274 | return darwin_sections[objc_meth_var_names_section]; |
ae46c4e0 | 1275 | else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20)) |
59ff4a1c | 1276 | return darwin_sections[objc_meth_var_types_section]; |
ae46c4e0 | 1277 | else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22)) |
59ff4a1c | 1278 | return darwin_sections[objc_cls_refs_section]; |
ae46c4e0 | 1279 | else if (!strncmp (name, "_OBJC_CLASS_", 12)) |
59ff4a1c | 1280 | return darwin_sections[objc_class_section]; |
ae46c4e0 | 1281 | else if (!strncmp (name, "_OBJC_METACLASS_", 16)) |
59ff4a1c | 1282 | return darwin_sections[objc_meta_class_section]; |
ae46c4e0 | 1283 | else if (!strncmp (name, "_OBJC_CATEGORY_", 15)) |
59ff4a1c | 1284 | return darwin_sections[objc_category_section]; |
ae46c4e0 | 1285 | else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25)) |
59ff4a1c | 1286 | return darwin_sections[objc_selector_refs_section]; |
ae46c4e0 | 1287 | else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20)) |
59ff4a1c | 1288 | return darwin_sections[objc_selector_fixup_section]; |
ae46c4e0 | 1289 | else if (!strncmp (name, "_OBJC_SYMBOLS", 13)) |
59ff4a1c | 1290 | return darwin_sections[objc_symbols_section]; |
ae46c4e0 | 1291 | else if (!strncmp (name, "_OBJC_MODULES", 13)) |
59ff4a1c | 1292 | return darwin_sections[objc_module_info_section]; |
264fa2db | 1293 | else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16)) |
59ff4a1c | 1294 | return darwin_sections[objc_image_info_section]; |
ae46c4e0 | 1295 | else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32)) |
59ff4a1c | 1296 | return darwin_sections[objc_cat_inst_meth_section]; |
ae46c4e0 | 1297 | else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29)) |
59ff4a1c | 1298 | return darwin_sections[objc_cat_cls_meth_section]; |
ae46c4e0 | 1299 | else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20)) |
59ff4a1c | 1300 | return darwin_sections[objc_cat_cls_meth_section]; |
ae46c4e0 | 1301 | else if (!strncmp (name, "_OBJC_PROTOCOL_", 15)) |
59ff4a1c | 1302 | return darwin_sections[objc_protocol_section]; |
ae46c4e0 | 1303 | else |
59ff4a1c | 1304 | return base_section; |
ae46c4e0 | 1305 | } |
59ff4a1c EC |
1306 | |
1307 | return base_section; | |
ae46c4e0 RH |
1308 | } |
1309 | ||
b64a1b53 | 1310 | /* This can be called with address expressions as "rtx". |
991b6592 | 1311 | They must go in "const". */ |
b64a1b53 | 1312 | |
d6b5193b | 1313 | section * |
9c808aad AJ |
1314 | machopic_select_rtx_section (enum machine_mode mode, rtx x, |
1315 | unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED) | |
b64a1b53 | 1316 | { |
b79d4265 SS |
1317 | if (GET_MODE_SIZE (mode) == 8 |
1318 | && (GET_CODE (x) == CONST_INT | |
1319 | || GET_CODE (x) == CONST_DOUBLE)) | |
56c779bc | 1320 | return darwin_sections[literal8_section]; |
b64a1b53 RH |
1321 | else if (GET_MODE_SIZE (mode) == 4 |
1322 | && (GET_CODE (x) == CONST_INT | |
1323 | || GET_CODE (x) == CONST_DOUBLE)) | |
56c779bc | 1324 | return darwin_sections[literal4_section]; |
5708d18d MS |
1325 | else if (HAVE_GAS_LITERAL16 |
1326 | && TARGET_64BIT | |
1be1e2dd | 1327 | && GET_MODE_SIZE (mode) == 16 |
f7288899 | 1328 | && (GET_CODE (x) == CONST_INT |
ca6f2eef EC |
1329 | || GET_CODE (x) == CONST_DOUBLE |
1330 | || GET_CODE (x) == CONST_VECTOR)) | |
f7288899 | 1331 | return darwin_sections[literal16_section]; |
86b0a4f3 | 1332 | else if (MACHOPIC_INDIRECT |
2e53734e GK |
1333 | && (GET_CODE (x) == SYMBOL_REF |
1334 | || GET_CODE (x) == CONST | |
1335 | || GET_CODE (x) == LABEL_REF)) | |
56c779bc | 1336 | return darwin_sections[const_data_section]; |
b64a1b53 | 1337 | else |
56c779bc | 1338 | return darwin_sections[const_section]; |
b64a1b53 RH |
1339 | } |
1340 | ||
2cc07db4 | 1341 | void |
9c808aad | 1342 | machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED) |
2cc07db4 | 1343 | { |
ab82a49f | 1344 | if (MACHOPIC_INDIRECT) |
56c779bc | 1345 | switch_to_section (darwin_sections[mod_init_section]); |
2cc07db4 | 1346 | else |
56c779bc | 1347 | switch_to_section (darwin_sections[constructor_section]); |
c8af3574 RH |
1348 | assemble_align (POINTER_SIZE); |
1349 | assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1); | |
2cc07db4 | 1350 | |
ab82a49f | 1351 | if (! MACHOPIC_INDIRECT) |
2cc07db4 RH |
1352 | fprintf (asm_out_file, ".reference .constructors_used\n"); |
1353 | } | |
1354 | ||
1355 | void | |
9c808aad | 1356 | machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED) |
2cc07db4 | 1357 | { |
ab82a49f | 1358 | if (MACHOPIC_INDIRECT) |
56c779bc | 1359 | switch_to_section (darwin_sections[mod_term_section]); |
2cc07db4 | 1360 | else |
56c779bc | 1361 | switch_to_section (darwin_sections[destructor_section]); |
c8af3574 RH |
1362 | assemble_align (POINTER_SIZE); |
1363 | assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1); | |
2cc07db4 | 1364 | |
ab82a49f | 1365 | if (! MACHOPIC_INDIRECT) |
2cc07db4 RH |
1366 | fprintf (asm_out_file, ".reference .destructors_used\n"); |
1367 | } | |
e2500fed | 1368 | |
5eb99654 | 1369 | void |
9c808aad | 1370 | darwin_globalize_label (FILE *stream, const char *name) |
5eb99654 KG |
1371 | { |
1372 | if (!!strncmp (name, "_OBJC_", 6)) | |
1373 | default_globalize_label (stream, name); | |
1374 | } | |
1375 | ||
d8299715 DK |
1376 | /* This routine returns non-zero if 'name' starts with the special objective-c |
1377 | anonymous file-scope static name. It accommodates c++'s mangling of such | |
1378 | symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */ | |
1379 | ||
1380 | int | |
1381 | darwin_label_is_anonymous_local_objc_name (const char *name) | |
1382 | { | |
1383 | const unsigned char *p = (const unsigned char *) name; | |
1384 | if (*p != '_') | |
1385 | return 0; | |
1386 | if (p[1] == 'Z' && p[2] == 'L') | |
1387 | { | |
1388 | p += 3; | |
1389 | while (*p >= '0' && *p <= '9') | |
1390 | p++; | |
1391 | } | |
1392 | return (!strncmp ((const char *)p, "_OBJC_", 6)); | |
1393 | } | |
1394 | ||
4746cf84 | 1395 | void |
083cad55 | 1396 | darwin_asm_named_section (const char *name, |
c18a5b6c MM |
1397 | unsigned int flags ATTRIBUTE_UNUSED, |
1398 | tree decl ATTRIBUTE_UNUSED) | |
4746cf84 | 1399 | { |
f1a66265 | 1400 | fprintf (asm_out_file, "\t.section %s\n", name); |
4746cf84 MA |
1401 | } |
1402 | ||
083cad55 | 1403 | void |
f1a66265 | 1404 | darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED) |
4746cf84 | 1405 | { |
f1a66265 | 1406 | /* Darwin does not use unique sections. */ |
4746cf84 MA |
1407 | } |
1408 | ||
f475fd3c MS |
1409 | /* Handle __attribute__ ((apple_kext_compatibility)). |
1410 | This only applies to darwin kexts for 2.95 compatibility -- it shrinks the | |
1411 | vtable for classes with this attribute (and their descendants) by not | |
1412 | outputting the new 3.0 nondeleting destructor. This means that such | |
1413 | objects CANNOT be allocated on the stack or as globals UNLESS they have | |
1414 | a completely empty `operator delete'. | |
1415 | Luckily, this fits in with the Darwin kext model. | |
1416 | ||
1417 | This attribute also disables gcc3's potential overlaying of derived | |
1418 | class data members on the padding at the end of the base class. */ | |
1419 | ||
1420 | tree | |
1421 | darwin_handle_kext_attribute (tree *node, tree name, | |
1422 | tree args ATTRIBUTE_UNUSED, | |
1423 | int flags ATTRIBUTE_UNUSED, | |
1424 | bool *no_add_attrs) | |
1425 | { | |
1426 | /* APPLE KEXT stuff -- only applies with pure static C++ code. */ | |
1427 | if (! TARGET_KEXTABI) | |
1428 | { | |
29d08eba JM |
1429 | warning (0, "%qE 2.95 vtable-compatibility attribute applies " |
1430 | "only when compiling a kext", name); | |
f475fd3c MS |
1431 | |
1432 | *no_add_attrs = true; | |
1433 | } | |
1434 | else if (TREE_CODE (*node) != RECORD_TYPE) | |
1435 | { | |
29d08eba JM |
1436 | warning (0, "%qE 2.95 vtable-compatibility attribute applies " |
1437 | "only to C++ classes", name); | |
f475fd3c MS |
1438 | |
1439 | *no_add_attrs = true; | |
1440 | } | |
1441 | ||
1442 | return NULL_TREE; | |
1443 | } | |
1444 | ||
005c1a13 GK |
1445 | /* Handle a "weak_import" attribute; arguments as in |
1446 | struct attribute_spec.handler. */ | |
1447 | ||
1448 | tree | |
1449 | darwin_handle_weak_import_attribute (tree *node, tree name, | |
1450 | tree ARG_UNUSED (args), | |
1451 | int ARG_UNUSED (flags), | |
1452 | bool * no_add_attrs) | |
1453 | { | |
d7001ae5 | 1454 | if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL) |
005c1a13 | 1455 | { |
29d08eba JM |
1456 | warning (OPT_Wattributes, "%qE attribute ignored", |
1457 | name); | |
005c1a13 GK |
1458 | *no_add_attrs = true; |
1459 | } | |
1460 | else | |
1461 | declare_weak (*node); | |
1462 | ||
1463 | return NULL_TREE; | |
1464 | } | |
45cc4783 | 1465 | |
083cad55 | 1466 | /* Emit a label for an FDE, making it global and/or weak if appropriate. |
eeab4d81 MS |
1467 | The third parameter is nonzero if this is for exception handling. |
1468 | The fourth parameter is nonzero if this is just a placeholder for an | |
4746cf84 | 1469 | FDE that we are omitting. */ |
eeab4d81 | 1470 | |
083cad55 | 1471 | void |
eeab4d81 | 1472 | darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty) |
4746cf84 | 1473 | { |
44ef77c1 | 1474 | char *lab; |
eeab4d81 MS |
1475 | |
1476 | if (! for_eh) | |
192d0f89 | 1477 | return; |
4746cf84 | 1478 | |
c3b07ec2 | 1479 | lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), ".eh", NULL); |
4746cf84 MA |
1480 | |
1481 | if (TREE_PUBLIC (decl)) | |
c3b07ec2 GK |
1482 | { |
1483 | targetm.asm_out.globalize_label (file, lab); | |
1484 | if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN) | |
1485 | { | |
1486 | fputs ("\t.private_extern ", file); | |
1487 | assemble_name (file, lab); | |
1488 | fputc ('\n', file); | |
1489 | } | |
1490 | } | |
4746cf84 | 1491 | |
f1a66265 | 1492 | if (DECL_WEAK (decl)) |
c3b07ec2 GK |
1493 | { |
1494 | fputs ("\t.weak_definition ", file); | |
1495 | assemble_name (file, lab); | |
1496 | fputc ('\n', file); | |
1497 | } | |
4746cf84 | 1498 | |
c3b07ec2 | 1499 | assemble_name (file, lab); |
4746cf84 | 1500 | if (empty) |
45cc4783 | 1501 | { |
c3b07ec2 | 1502 | fputs (" = 0\n", file); |
45cc4783 MS |
1503 | |
1504 | /* Mark the absolute .eh and .eh1 style labels as needed to | |
1505 | ensure that we don't dead code strip them and keep such | |
1506 | labels from another instantiation point until we can fix this | |
1507 | properly with group comdat support. */ | |
c3b07ec2 | 1508 | darwin_mark_decl_preserved (lab); |
45cc4783 | 1509 | } |
4746cf84 | 1510 | else |
c3b07ec2 | 1511 | fputs (":\n", file); |
4746cf84 MA |
1512 | |
1513 | free (lab); | |
1514 | } | |
1515 | ||
083cad55 EC |
1516 | static GTY(()) unsigned long except_table_label_num; |
1517 | ||
1518 | void | |
1519 | darwin_emit_except_table_label (FILE *file) | |
1520 | { | |
1521 | char section_start_label[30]; | |
1522 | ||
1523 | ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table", | |
1524 | except_table_label_num++); | |
1525 | ASM_OUTPUT_LABEL (file, section_start_label); | |
1526 | } | |
1527 | /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */ | |
eeab4d81 | 1528 | |
4746cf84 MA |
1529 | void |
1530 | darwin_non_lazy_pcrel (FILE *file, rtx addr) | |
1531 | { | |
4746cf84 MA |
1532 | const char *nlp_name; |
1533 | ||
992d08b1 | 1534 | gcc_assert (GET_CODE (addr) == SYMBOL_REF); |
4746cf84 | 1535 | |
11abc112 | 1536 | nlp_name = machopic_indirection_name (addr, /*stub_p=*/false); |
4746cf84 MA |
1537 | fputs ("\t.long\t", file); |
1538 | ASM_OUTPUT_LABELREF (file, nlp_name); | |
1539 | fputs ("-.", file); | |
1540 | } | |
1541 | ||
6ce4806b MA |
1542 | /* Emit an assembler directive to set visibility for a symbol. The |
1543 | only supported visibilities are VISIBILITY_DEFAULT and | |
1544 | VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private | |
1545 | extern". There is no MACH-O equivalent of ELF's | |
1546 | VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */ | |
1547 | ||
083cad55 | 1548 | void |
6ce4806b MA |
1549 | darwin_assemble_visibility (tree decl, int vis) |
1550 | { | |
1551 | if (vis == VISIBILITY_DEFAULT) | |
1552 | ; | |
1553 | else if (vis == VISIBILITY_HIDDEN) | |
1554 | { | |
1555 | fputs ("\t.private_extern ", asm_out_file); | |
1556 | assemble_name (asm_out_file, | |
1557 | (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)))); | |
1558 | fputs ("\n", asm_out_file); | |
1559 | } | |
1560 | else | |
5c498b10 DD |
1561 | warning (OPT_Wattributes, "internal and protected visibility attributes " |
1562 | "not supported in this configuration; ignored"); | |
6ce4806b MA |
1563 | } |
1564 | ||
7606e68f SS |
1565 | /* Output a difference of two labels that will be an assembly time |
1566 | constant if the two labels are local. (.long lab1-lab2 will be | |
1567 | very different if lab1 is at the boundary between two sections; it | |
1568 | will be relocated according to the second section, not the first, | |
1569 | so one ends up with a difference between labels in different | |
1570 | sections, which is bad in the dwarf2 eh context for instance.) */ | |
1571 | ||
1572 | static int darwin_dwarf_label_counter; | |
1573 | ||
1574 | void | |
59d8fe27 | 1575 | darwin_asm_output_dwarf_delta (FILE *file, int size, |
9c808aad | 1576 | const char *lab1, const char *lab2) |
7606e68f | 1577 | { |
4746cf84 MA |
1578 | int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L' |
1579 | && lab2[0] == '*' && lab2[1] == 'L'); | |
d4b56511 | 1580 | const char *directive = (size == 8 ? ".quad" : ".long"); |
7606e68f SS |
1581 | |
1582 | if (islocaldiff) | |
1583 | fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter); | |
1584 | else | |
59d8fe27 | 1585 | fprintf (file, "\t%s\t", directive); |
57829bc4 | 1586 | assemble_name_raw (file, lab1); |
7606e68f | 1587 | fprintf (file, "-"); |
57829bc4 | 1588 | assemble_name_raw (file, lab2); |
7606e68f | 1589 | if (islocaldiff) |
59d8fe27 | 1590 | fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++); |
7606e68f | 1591 | } |
192d0f89 GK |
1592 | |
1593 | /* Output labels for the start of the DWARF sections if necessary. */ | |
1594 | void | |
1595 | darwin_file_start (void) | |
1596 | { | |
1597 | if (write_symbols == DWARF2_DEBUG) | |
1598 | { | |
4f8bdef9 | 1599 | static const char * const debugnames[] = |
192d0f89 GK |
1600 | { |
1601 | DEBUG_FRAME_SECTION, | |
1602 | DEBUG_INFO_SECTION, | |
1603 | DEBUG_ABBREV_SECTION, | |
1604 | DEBUG_ARANGES_SECTION, | |
1605 | DEBUG_MACINFO_SECTION, | |
1606 | DEBUG_LINE_SECTION, | |
1607 | DEBUG_LOC_SECTION, | |
1608 | DEBUG_PUBNAMES_SECTION, | |
89708594 | 1609 | DEBUG_PUBTYPES_SECTION, |
192d0f89 GK |
1610 | DEBUG_STR_SECTION, |
1611 | DEBUG_RANGES_SECTION | |
1612 | }; | |
1613 | size_t i; | |
1614 | ||
1615 | for (i = 0; i < ARRAY_SIZE (debugnames); i++) | |
1616 | { | |
1617 | int namelen; | |
1618 | ||
1619 | switch_to_section (get_section (debugnames[i], SECTION_DEBUG, NULL)); | |
4f8bdef9 | 1620 | |
192d0f89 GK |
1621 | gcc_assert (strncmp (debugnames[i], "__DWARF,", 8) == 0); |
1622 | gcc_assert (strchr (debugnames[i] + 8, ',')); | |
4f8bdef9 | 1623 | |
192d0f89 GK |
1624 | namelen = strchr (debugnames[i] + 8, ',') - (debugnames[i] + 8); |
1625 | fprintf (asm_out_file, "Lsection%.*s:\n", namelen, debugnames[i] + 8); | |
1626 | } | |
1627 | } | |
1628 | } | |
1629 | ||
1630 | /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section | |
1631 | offsets are not represented using relocs in .o files; either the | |
1632 | section never leaves the .o file, or the linker or other tool is | |
1633 | responsible for parsing the DWARF and updating the offsets. */ | |
1634 | ||
1635 | void | |
1636 | darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab, | |
1637 | section *base) | |
1638 | { | |
1639 | char sname[64]; | |
1640 | int namelen; | |
4f8bdef9 | 1641 | |
192d0f89 GK |
1642 | gcc_assert (base->common.flags & SECTION_NAMED); |
1643 | gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0); | |
1644 | gcc_assert (strchr (base->named.name + 8, ',')); | |
1645 | ||
1646 | namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8); | |
1647 | sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8); | |
1648 | darwin_asm_output_dwarf_delta (file, size, lab, sname); | |
1649 | } | |
7606e68f | 1650 | |
a5fe455b | 1651 | void |
9c808aad | 1652 | darwin_file_end (void) |
a5fe455b ZW |
1653 | { |
1654 | machopic_finish (asm_out_file); | |
1655 | if (strcmp (lang_hooks.name, "GNU C++") == 0) | |
1656 | { | |
56c779bc GK |
1657 | switch_to_section (darwin_sections[constructor_section]); |
1658 | switch_to_section (darwin_sections[destructor_section]); | |
a5fe455b ZW |
1659 | ASM_OUTPUT_ALIGN (asm_out_file, 1); |
1660 | } | |
92b9a671 | 1661 | fprintf (asm_out_file, "\t.subsections_via_symbols\n"); |
a5fe455b ZW |
1662 | } |
1663 | ||
f475fd3c MS |
1664 | /* TODO: Add a language hook for identifying if a decl is a vtable. */ |
1665 | #define DARWIN_VTABLE_P(DECL) 0 | |
1666 | ||
31920d83 | 1667 | /* Cross-module name binding. Darwin does not support overriding |
f475fd3c | 1668 | functions at dynamic-link time, except for vtables in kexts. */ |
31920d83 DJ |
1669 | |
1670 | bool | |
3101faab | 1671 | darwin_binds_local_p (const_tree decl) |
31920d83 | 1672 | { |
f475fd3c MS |
1673 | return default_binds_local_p_1 (decl, |
1674 | TARGET_KEXTABI && DARWIN_VTABLE_P (decl)); | |
31920d83 DJ |
1675 | } |
1676 | ||
23f99493 MS |
1677 | #if 0 |
1678 | /* See TARGET_ASM_OUTPUT_ANCHOR for why we can't do this yet. */ | |
ab0ff804 AP |
1679 | /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the |
1680 | anchor relative to ".", the current section position. We cannot use | |
1681 | the default one because ASM_OUTPUT_DEF is wrong for Darwin. */ | |
1682 | ||
1683 | void | |
1684 | darwin_asm_output_anchor (rtx symbol) | |
1685 | { | |
1686 | fprintf (asm_out_file, "\t.set\t"); | |
1687 | assemble_name (asm_out_file, XSTR (symbol, 0)); | |
1688 | fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n", | |
1689 | SYMBOL_REF_BLOCK_OFFSET (symbol)); | |
1690 | } | |
23f99493 | 1691 | #endif |
ab0ff804 | 1692 | |
4f8bdef9 EC |
1693 | /* Set the darwin specific attributes on TYPE. */ |
1694 | void | |
1695 | darwin_set_default_type_attributes (tree type) | |
1696 | { | |
1697 | if (darwin_ms_struct | |
1698 | && TREE_CODE (type) == RECORD_TYPE) | |
1699 | TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"), | |
1700 | NULL_TREE, | |
1701 | TYPE_ATTRIBUTES (type)); | |
1702 | } | |
1703 | ||
3a057207 | 1704 | /* True, iff we're generating code for loadable kernel extensions. */ |
f475fd3c MS |
1705 | |
1706 | bool | |
1707 | darwin_kextabi_p (void) { | |
1708 | return flag_apple_kext; | |
1709 | } | |
1710 | ||
1711 | void | |
1712 | darwin_override_options (void) | |
1713 | { | |
047a3193 IS |
1714 | /* Don't emit DWARF3/4 unless specifically selected. This is a |
1715 | workaround for tool bugs. */ | |
1716 | if (dwarf_strict < 0) | |
1717 | dwarf_strict = 1; | |
1718 | ||
47f447b1 JH |
1719 | /* Disable -freorder-blocks-and-partition for darwin_emit_unwind_label. */ |
1720 | if (flag_reorder_blocks_and_partition | |
1721 | && (targetm.asm_out.unwind_label == darwin_emit_unwind_label)) | |
1722 | { | |
1723 | inform (input_location, | |
1724 | "-freorder-blocks-and-partition does not work with exceptions " | |
1725 | "on this architecture"); | |
1726 | flag_reorder_blocks_and_partition = 0; | |
1727 | flag_reorder_blocks = 1; | |
1728 | } | |
1729 | ||
f475fd3c MS |
1730 | if (flag_mkernel || flag_apple_kext) |
1731 | { | |
1732 | /* -mkernel implies -fapple-kext for C++ */ | |
1733 | if (strcmp (lang_hooks.name, "GNU C++") == 0) | |
1734 | flag_apple_kext = 1; | |
1735 | ||
1736 | flag_no_common = 1; | |
1737 | ||
1738 | /* No EH in kexts. */ | |
1739 | flag_exceptions = 0; | |
1740 | /* No -fnon-call-exceptions data in kexts. */ | |
1741 | flag_non_call_exceptions = 0; | |
1742 | } | |
62760ffd | 1743 | if (flag_var_tracking |
a857cbfb | 1744 | && strverscmp (darwin_macosx_version_min, "10.5") >= 0 |
4352ed4d DB |
1745 | && debug_info_level >= DINFO_LEVEL_NORMAL |
1746 | && debug_hooks->var_location != do_nothing_debug_hooks.var_location) | |
62760ffd | 1747 | flag_var_tracking_uninit = 1; |
f475fd3c MS |
1748 | } |
1749 | ||
fb220235 FXC |
1750 | /* Add $LDBL128 suffix to long double builtins. */ |
1751 | ||
1752 | static void | |
1753 | darwin_patch_builtin (int fncode) | |
1754 | { | |
1755 | tree fn = built_in_decls[fncode]; | |
1756 | tree sym; | |
1757 | char *newname; | |
1758 | ||
1759 | if (!fn) | |
1760 | return; | |
1761 | ||
1762 | sym = DECL_ASSEMBLER_NAME (fn); | |
c92712d1 UB |
1763 | newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL)); |
1764 | ||
fb220235 | 1765 | set_user_assembler_name (fn, newname); |
fb220235 FXC |
1766 | |
1767 | fn = implicit_built_in_decls[fncode]; | |
1768 | if (fn) | |
1769 | set_user_assembler_name (fn, newname); | |
fb220235 FXC |
1770 | } |
1771 | ||
1772 | void | |
1773 | darwin_patch_builtins (void) | |
1774 | { | |
1775 | if (LONG_DOUBLE_TYPE_SIZE != 128) | |
1776 | return; | |
1777 | ||
1778 | #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode); | |
c92712d1 UB |
1779 | #define PATCH_BUILTIN_NO64(fncode) \ |
1780 | if (!TARGET_64BIT) \ | |
fb220235 | 1781 | darwin_patch_builtin (fncode); |
c92712d1 UB |
1782 | #define PATCH_BUILTIN_VARIADIC(fncode) \ |
1783 | if (!TARGET_64BIT \ | |
fb220235 FXC |
1784 | && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \ |
1785 | darwin_patch_builtin (fncode); | |
1786 | #include "darwin-ppc-ldouble-patch.def" | |
1787 | #undef PATCH_BUILTIN | |
1788 | #undef PATCH_BUILTIN_NO64 | |
1789 | #undef PATCH_BUILTIN_VARIADIC | |
1790 | } | |
1791 | ||
1792 | ||
e2500fed | 1793 | #include "gt-darwin.h" |