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