| 1 |
/* Functions for generic Darwin as target machine for GNU C compiler. |
| 2 |
Copyright (C) 1989-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Apple Computer Inc. |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3, or (at your option) |
| 10 |
any later version. |
| 11 |
|
| 12 |
GCC is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with GCC; see the file COPYING3. If not see |
| 19 |
<http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
#include "system.h" |
| 23 |
#include "coretypes.h" |
| 24 |
#include "backend.h" |
| 25 |
#include "target.h" |
| 26 |
#include "rtl.h" |
| 27 |
#include "tree.h" |
| 28 |
#include "gimple.h" |
| 29 |
#include "cfghooks.h" |
| 30 |
#include "df.h" |
| 31 |
#include "memmodel.h" |
| 32 |
#include "tm_p.h" |
| 33 |
#include "stringpool.h" |
| 34 |
#include "attribs.h" |
| 35 |
#include "insn-config.h" |
| 36 |
#include "emit-rtl.h" |
| 37 |
#include "cgraph.h" |
| 38 |
#include "lto-streamer.h" |
| 39 |
#include "output.h" |
| 40 |
#include "varasm.h" |
| 41 |
#include "stor-layout.h" |
| 42 |
#include "explow.h" |
| 43 |
#include "expr.h" |
| 44 |
#include "langhooks.h" |
| 45 |
#include "targhooks.h" |
| 46 |
#include "toplev.h" |
| 47 |
#include "lto-section-names.h" |
| 48 |
#include "intl.h" |
| 49 |
#include "optabs.h" |
| 50 |
#include "flags.h" |
| 51 |
#include "opts.h" |
| 52 |
|
| 53 |
/* Fix and Continue. |
| 54 |
|
| 55 |
NOTES: |
| 56 |
1) this facility requires suitable support from a modified version |
| 57 |
of GDB, which is not provided on any system after MacOS 10.7/Darwin11. |
| 58 |
2) There is no support for this in any X86 version of the FSF compiler. |
| 59 |
|
| 60 |
Fix and continue was used on some earlier MacOS systems for rapid turn |
| 61 |
around debugging. When code is compiled with the -mfix-and-continue |
| 62 |
flag, two changes are made to the generated code that allow the system |
| 63 |
to do things that it would normally not be able to do easily. These |
| 64 |
changes allow gdb to load in recompilation of a translation unit that |
| 65 |
has been changed into a running program and replace existing functions |
| 66 |
and methods of that translation unit with versions of those functions |
| 67 |
and methods from the newly compiled translation unit. The new functions |
| 68 |
access the existing static symbols from the old translation unit, if the |
| 69 |
symbol existed in the unit to be replaced, and from the new translation |
| 70 |
unit, otherwise. |
| 71 |
|
| 72 |
The changes are to insert 5 nops at the beginning of all functions |
| 73 |
and to use indirection to get at static symbols. The 5 nops |
| 74 |
are required by consumers of the generated code. Currently, gdb |
| 75 |
uses this to patch in a jump to the overriding function, this |
| 76 |
allows all uses of the old name to forward to the replacement, |
| 77 |
including existing function pointers and virtual methods. See |
| 78 |
rs6000_emit_prologue for the code that handles the nop insertions. |
| 79 |
|
| 80 |
The added indirection allows gdb to redirect accesses to static |
| 81 |
symbols from the newly loaded translation unit to the existing |
| 82 |
symbol, if any. @code{static} symbols are special and are handled by |
| 83 |
setting the second word in the .non_lazy_symbol_pointer data |
| 84 |
structure to symbol. See indirect_data for the code that handles |
| 85 |
the extra indirection, and machopic_output_indirection and its use |
| 86 |
of MACHO_SYMBOL_FLAG_STATIC for the code that handles @code{static} |
| 87 |
symbol indirection. */ |
| 88 |
|
| 89 |
typedef struct GTY(()) cdtor_record { |
| 90 |
rtx symbol; |
| 91 |
int priority; /* [con/de]structor priority */ |
| 92 |
unsigned position; /* original position */ |
| 93 |
} cdtor_record; |
| 94 |
|
| 95 |
static GTY(()) vec<cdtor_record, va_gc> *ctors = NULL; |
| 96 |
static GTY(()) vec<cdtor_record, va_gc> *dtors = NULL; |
| 97 |
|
| 98 |
/* A flag to determine whether we are running c++ or obj-c++. This has to be |
| 99 |
settable from non-c-family contexts too (i.e. we can't use the c_dialect_ |
| 100 |
functions). */ |
| 101 |
int darwin_running_cxx; |
| 102 |
|
| 103 |
/* Some code-gen now depends on OS major version numbers (at least). */ |
| 104 |
int generating_for_darwin_version ; |
| 105 |
|
| 106 |
/* For older linkers we need to emit special sections (marked 'coalesced') for |
| 107 |
for weak or single-definition items. */ |
| 108 |
static bool ld_uses_coal_sects = false; |
| 109 |
|
| 110 |
/* Very old (ld_classic) linkers need a symbol to mark the start of |
| 111 |
each FDE. */ |
| 112 |
static bool ld_needs_eh_markers = false; |
| 113 |
|
| 114 |
/* Emit a section-start symbol for mod init and term sections. */ |
| 115 |
static bool ld_init_term_start_labels = false; |
| 116 |
|
| 117 |
/* The source and version of dsymutil in use. */ |
| 118 |
#ifndef DSYMUTIL_VERSION |
| 119 |
# warning Darwin toolchain without a defined dsymutil. |
| 120 |
# define DSYMUTIL_VERSION DET_UNKNOWN,0,0,0 |
| 121 |
#endif |
| 122 |
|
| 123 |
struct { |
| 124 |
darwin_external_toolchain kind; /* cctools, llvm, clang etc. */ |
| 125 |
int major; /* version number. */ |
| 126 |
int minor; |
| 127 |
int tiny; |
| 128 |
} dsymutil_version = {DSYMUTIL_VERSION}; |
| 129 |
|
| 130 |
/* Section names. */ |
| 131 |
section * darwin_sections[NUM_DARWIN_SECTIONS]; |
| 132 |
|
| 133 |
/* While we transition to using in-tests instead of ifdef'd code. */ |
| 134 |
#if !HAVE_lo_sum |
| 135 |
#define gen_macho_high(m,a,b) (a) |
| 136 |
#define gen_macho_low(m,a,b,c) (a) |
| 137 |
#endif |
| 138 |
|
| 139 |
/* True if we're setting __attribute__ ((ms_struct)). */ |
| 140 |
int darwin_ms_struct = false; |
| 141 |
|
| 142 |
/* Earlier versions of Darwin as do not recognize an alignment field in |
| 143 |
.comm directives, this should be set for versions that allow it. */ |
| 144 |
int emit_aligned_common = false; |
| 145 |
|
| 146 |
/* A get_unnamed_section callback used to switch to an ObjC section. |
| 147 |
DIRECTIVE is as for output_section_asm_op. */ |
| 148 |
|
| 149 |
static void |
| 150 |
output_objc_section_asm_op (const char *directive) |
| 151 |
{ |
| 152 |
static bool been_here = false; |
| 153 |
|
| 154 |
/* The NeXT ObjC Runtime requires these sections to be present and in |
| 155 |
order in the object. The code below implements this by emitting |
| 156 |
a section header for each ObjC section the first time that an ObjC |
| 157 |
section is requested. */ |
| 158 |
if (darwin_symbol_stubs && ! been_here) |
| 159 |
{ |
| 160 |
section *saved_in_section = in_section; |
| 161 |
static const enum darwin_section_enum tomark[] = |
| 162 |
{ |
| 163 |
/* written, cold -> hot */ |
| 164 |
objc_cat_cls_meth_section, |
| 165 |
objc_cat_inst_meth_section, |
| 166 |
objc_string_object_section, |
| 167 |
objc_constant_string_object_section, |
| 168 |
objc_selector_refs_section, |
| 169 |
objc_selector_fixup_section, |
| 170 |
objc_cls_refs_section, |
| 171 |
objc_class_section, |
| 172 |
objc_meta_class_section, |
| 173 |
/* shared, hot -> cold */ |
| 174 |
objc_cls_meth_section, |
| 175 |
objc_inst_meth_section, |
| 176 |
objc_protocol_section, |
| 177 |
objc_class_names_section, |
| 178 |
objc_meth_var_types_section, |
| 179 |
objc_meth_var_names_section, |
| 180 |
objc_category_section, |
| 181 |
objc_class_vars_section, |
| 182 |
objc_instance_vars_section, |
| 183 |
objc_module_info_section, |
| 184 |
objc_symbols_section, |
| 185 |
}; |
| 186 |
/* ABI=1 */ |
| 187 |
static const enum darwin_section_enum tomarkv1[] = |
| 188 |
{ |
| 189 |
objc1_protocol_ext_section, |
| 190 |
objc1_class_ext_section, |
| 191 |
objc1_prop_list_section |
| 192 |
} ; |
| 193 |
/* ABI=2 */ |
| 194 |
static const enum darwin_section_enum tomarkv2[] = |
| 195 |
{ |
| 196 |
objc2_method_names_section, |
| 197 |
objc2_message_refs_section, |
| 198 |
objc2_selector_refs_section, |
| 199 |
objc2_ivar_section, |
| 200 |
objc2_classdefs_section, |
| 201 |
objc2_metadata_section, |
| 202 |
objc2_classrefs_section, |
| 203 |
objc2_class_names_section, |
| 204 |
objc2_classlist_section, |
| 205 |
objc2_categorylist_section, |
| 206 |
objc2_nonlazy_class_section, |
| 207 |
objc2_nonlazy_category_section, |
| 208 |
objc2_protocollist_section, |
| 209 |
objc2_protocolrefs_section, |
| 210 |
objc2_super_classrefs_section, |
| 211 |
objc2_constant_string_object_section, |
| 212 |
objc2_image_info_section, |
| 213 |
} ; |
| 214 |
size_t i; |
| 215 |
|
| 216 |
been_here = true; |
| 217 |
if (flag_objc_abi < 2) |
| 218 |
{ |
| 219 |
for (i = 0; i < ARRAY_SIZE (tomark); i++) |
| 220 |
switch_to_section (darwin_sections[tomark[i]]); |
| 221 |
if (flag_objc_abi == 1) |
| 222 |
for (i = 0; i < ARRAY_SIZE (tomarkv1); i++) |
| 223 |
switch_to_section (darwin_sections[tomarkv1[i]]); |
| 224 |
} |
| 225 |
else |
| 226 |
for (i = 0; i < ARRAY_SIZE (tomarkv2); i++) |
| 227 |
switch_to_section (darwin_sections[tomarkv2[i]]); |
| 228 |
/* Make sure we don't get varasm.cc out of sync with us. */ |
| 229 |
switch_to_section (saved_in_section); |
| 230 |
} |
| 231 |
output_section_asm_op (directive); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
/* Private flag applied to disable section-anchors in a particular section. */ |
| 236 |
#define SECTION_NO_ANCHOR SECTION_MACH_DEP |
| 237 |
|
| 238 |
|
| 239 |
/* Implement TARGET_ASM_INIT_SECTIONS. */ |
| 240 |
|
| 241 |
void |
| 242 |
darwin_init_sections (void) |
| 243 |
{ |
| 244 |
#define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \ |
| 245 |
darwin_sections[NAME] = \ |
| 246 |
get_unnamed_section (FLAGS, (OBJC \ |
| 247 |
? output_objc_section_asm_op \ |
| 248 |
: output_section_asm_op), \ |
| 249 |
"\t" DIRECTIVE); |
| 250 |
#include "config/darwin-sections.def" |
| 251 |
#undef DEF_SECTION |
| 252 |
|
| 253 |
readonly_data_section = darwin_sections[const_section]; |
| 254 |
exception_section = darwin_sections[darwin_exception_section]; |
| 255 |
eh_frame_section = darwin_sections[darwin_eh_frame_section]; |
| 256 |
|
| 257 |
/* If our linker is new enough to coalesce weak symbols, then we |
| 258 |
can just put picbase_thunks into the text section. */ |
| 259 |
if (! ld_uses_coal_sects ) |
| 260 |
darwin_sections[picbase_thunk_section] = text_section; |
| 261 |
} |
| 262 |
|
| 263 |
int |
| 264 |
name_needs_quotes (const char *name) |
| 265 |
{ |
| 266 |
int c; |
| 267 |
while ((c = *name++) != '\0') |
| 268 |
if (! ISIDNUM (c) |
| 269 |
&& c != '.' && c != '$' && c != '_' ) |
| 270 |
return 1; |
| 271 |
return 0; |
| 272 |
} |
| 273 |
|
| 274 |
DEBUG_FUNCTION void |
| 275 |
dump_machopic_symref_flags (FILE *dump, rtx sym_ref) |
| 276 |
{ |
| 277 |
unsigned long flags = SYMBOL_REF_FLAGS (sym_ref); |
| 278 |
|
| 279 |
fprintf (dump, "flags: %08lx %c%c%c%c%c%c%c", |
| 280 |
flags, |
| 281 |
(MACHO_SYMBOL_STATIC_P (sym_ref) ? 's' : '-'), |
| 282 |
(MACHO_SYMBOL_INDIRECTION_P (sym_ref) ? 'I' : '-'), |
| 283 |
(MACHO_SYMBOL_LINKER_VIS_P (sym_ref) ? 'l' : '-'), |
| 284 |
(MACHO_SYMBOL_HIDDEN_VIS_P (sym_ref) ? 'h' : '-'), |
| 285 |
(MACHO_SYMBOL_DEFINED_P (sym_ref) ? 'd' : '-'), |
| 286 |
(MACHO_SYMBOL_MUST_INDIRECT_P (sym_ref) ? 'i' : '-'), |
| 287 |
(MACHO_SYMBOL_VARIABLE_P (sym_ref) ? 'v' : '-')); |
| 288 |
|
| 289 |
#if (DARWIN_X86) |
| 290 |
fprintf (dump, "%c%c%c%c", |
| 291 |
(SYMBOL_REF_STUBVAR_P (sym_ref) ? 'S' : '-'), |
| 292 |
(SYMBOL_REF_DLLEXPORT_P (sym_ref) ? 'X' : '-'), |
| 293 |
(SYMBOL_REF_DLLIMPORT_P (sym_ref) ? 'I' : '-'), |
| 294 |
(SYMBOL_REF_FAR_ADDR_P (sym_ref) ? 'F' : '-')); |
| 295 |
#endif |
| 296 |
|
| 297 |
fprintf (dump, "%c%c%c%03u%c%c%c\n", |
| 298 |
(SYMBOL_REF_ANCHOR_P (sym_ref) ? 'a' : '-'), |
| 299 |
(SYMBOL_REF_HAS_BLOCK_INFO_P (sym_ref) ? 'b' : '-'), |
| 300 |
(SYMBOL_REF_EXTERNAL_P (sym_ref) ? 'e' : '-'), |
| 301 |
(unsigned)SYMBOL_REF_TLS_MODEL (sym_ref), |
| 302 |
(SYMBOL_REF_SMALL_P (sym_ref) ? 'm' : '-'), |
| 303 |
(SYMBOL_REF_LOCAL_P (sym_ref) ? 'l' : '-'), |
| 304 |
(SYMBOL_REF_FUNCTION_P (sym_ref) ? 'f' : '-')); |
| 305 |
} |
| 306 |
|
| 307 |
DEBUG_FUNCTION void |
| 308 |
debug_machopic_symref_flags (rtx sym_ref) |
| 309 |
{ |
| 310 |
dump_machopic_symref_flags (stderr, sym_ref); |
| 311 |
} |
| 312 |
|
| 313 |
/* Return true if SYM_REF can be used without an indirection. */ |
| 314 |
int |
| 315 |
machopic_symbol_defined_p (rtx sym_ref) |
| 316 |
{ |
| 317 |
if (MACHO_SYMBOL_DEFINED_P (sym_ref)) |
| 318 |
return true; |
| 319 |
|
| 320 |
/* If a symbol references local and is not an extern to this |
| 321 |
file, then the symbol might be able to declared as defined. */ |
| 322 |
if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref)) |
| 323 |
{ |
| 324 |
/* If the symbol references a variable and the variable is a |
| 325 |
common symbol, then this symbol is not defined. */ |
| 326 |
if (MACHO_SYMBOL_VARIABLE_P (sym_ref)) |
| 327 |
{ |
| 328 |
tree decl = SYMBOL_REF_DECL (sym_ref); |
| 329 |
if (!decl) |
| 330 |
return true; |
| 331 |
if (DECL_COMMON (decl)) |
| 332 |
return false; |
| 333 |
} |
| 334 |
return true; |
| 335 |
} |
| 336 |
return false; |
| 337 |
} |
| 338 |
|
| 339 |
/* This module assumes that (const (symbol_ref "foo")) is a legal pic |
| 340 |
reference, which will not be changed. */ |
| 341 |
|
| 342 |
enum machopic_addr_class |
| 343 |
machopic_classify_symbol (rtx sym_ref) |
| 344 |
{ |
| 345 |
bool function_p; |
| 346 |
|
| 347 |
function_p = SYMBOL_REF_FUNCTION_P (sym_ref); |
| 348 |
if (machopic_symbol_defined_p (sym_ref)) |
| 349 |
return (function_p |
| 350 |
? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA); |
| 351 |
else |
| 352 |
return (function_p |
| 353 |
? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA); |
| 354 |
} |
| 355 |
|
| 356 |
#ifndef TARGET_FIX_AND_CONTINUE |
| 357 |
#define TARGET_FIX_AND_CONTINUE 0 |
| 358 |
#endif |
| 359 |
|
| 360 |
/* Indicate when fix-and-continue style code generation is being used |
| 361 |
and when a reference to data should be indirected so that it can be |
| 362 |
rebound in a new translation unit to reference the original instance |
| 363 |
of that data. Symbol names that are for code generation local to |
| 364 |
the translation unit are bound to the new translation unit; |
| 365 |
currently this means symbols that begin with L or _OBJC_; |
| 366 |
otherwise, we indicate that an indirect reference should be made to |
| 367 |
permit the runtime to rebind new instances of the translation unit |
| 368 |
to the original instance of the data. */ |
| 369 |
|
| 370 |
static int |
| 371 |
indirect_data (rtx sym_ref) |
| 372 |
{ |
| 373 |
int lprefix; |
| 374 |
const char *name; |
| 375 |
|
| 376 |
/* If we aren't generating fix-and-continue code, don't do anything |
| 377 |
special. */ |
| 378 |
if (TARGET_FIX_AND_CONTINUE == 0) |
| 379 |
return 0; |
| 380 |
|
| 381 |
/* Otherwise, all symbol except symbols that begin with L or _OBJC_ |
| 382 |
are indirected. Symbols that begin with L and _OBJC_ are always |
| 383 |
bound to the current translation unit as they are used for |
| 384 |
generated local data of the translation unit. */ |
| 385 |
|
| 386 |
name = XSTR (sym_ref, 0); |
| 387 |
|
| 388 |
lprefix = (((name[0] == '*' || name[0] == '&') |
| 389 |
&& (name[1] == 'L' || (name[1] == '"' && name[2] == 'L'))) |
| 390 |
|| (startswith (name, "_OBJC_"))); |
| 391 |
|
| 392 |
return ! lprefix; |
| 393 |
} |
| 394 |
|
| 395 |
static int |
| 396 |
machopic_data_defined_p (rtx sym_ref) |
| 397 |
{ |
| 398 |
if (indirect_data (sym_ref)) |
| 399 |
return 0; |
| 400 |
|
| 401 |
switch (machopic_classify_symbol (sym_ref)) |
| 402 |
{ |
| 403 |
case MACHOPIC_DEFINED_DATA: |
| 404 |
case MACHOPIC_DEFINED_FUNCTION: |
| 405 |
return 1; |
| 406 |
default: |
| 407 |
return 0; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
void |
| 412 |
machopic_define_symbol (rtx mem) |
| 413 |
{ |
| 414 |
rtx sym_ref; |
| 415 |
|
| 416 |
gcc_assert (GET_CODE (mem) == MEM); |
| 417 |
sym_ref = XEXP (mem, 0); |
| 418 |
SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED; |
| 419 |
} |
| 420 |
|
| 421 |
/* Return either ORIG or: |
| 422 |
|
| 423 |
(const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET)) |
| 424 |
|
| 425 |
depending on MACHO_DYNAMIC_NO_PIC_P. */ |
| 426 |
rtx |
| 427 |
machopic_gen_offset (rtx orig) |
| 428 |
{ |
| 429 |
if (MACHO_DYNAMIC_NO_PIC_P) |
| 430 |
return orig; |
| 431 |
else |
| 432 |
{ |
| 433 |
/* Play games to avoid marking the function as needing pic if we |
| 434 |
are being called as part of the cost-estimation process. */ |
| 435 |
if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl) |
| 436 |
crtl->uses_pic_offset_table = 1; |
| 437 |
orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig), |
| 438 |
UNSPEC_MACHOPIC_OFFSET); |
| 439 |
return gen_rtx_CONST (Pmode, orig); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
static GTY(()) const char * function_base_func_name = NULL; |
| 444 |
static GTY(()) unsigned current_pic_label_num = 0; |
| 445 |
static GTY(()) unsigned emitted_pic_label_num = 0; |
| 446 |
|
| 447 |
/* We need to keep one picbase label per function, but (when we emit code |
| 448 |
to reload the picbase for setjump receiver) we might need to check for |
| 449 |
a second use. So, only update the picbase label counter when we see a |
| 450 |
new function. When there's no function decl, we assume that the call is |
| 451 |
from the x86 stub generation code. */ |
| 452 |
static void |
| 453 |
update_pic_label_number_if_needed (void) |
| 454 |
{ |
| 455 |
if (current_function_decl) |
| 456 |
{ |
| 457 |
|
| 458 |
const char *current_name = |
| 459 |
IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl)); |
| 460 |
if (function_base_func_name != current_name) |
| 461 |
{ |
| 462 |
++current_pic_label_num; |
| 463 |
function_base_func_name = current_name; |
| 464 |
} |
| 465 |
} |
| 466 |
else |
| 467 |
{ |
| 468 |
++current_pic_label_num; |
| 469 |
function_base_func_name = "L_machopic_stub_dummy"; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
void |
| 474 |
machopic_output_function_base_name (FILE *file) |
| 475 |
{ |
| 476 |
/* We should only get here for -fPIC. */ |
| 477 |
gcc_checking_assert (MACHOPIC_PURE); |
| 478 |
|
| 479 |
update_pic_label_number_if_needed (); |
| 480 |
fprintf (file, "L%u$pb", current_pic_label_num); |
| 481 |
} |
| 482 |
|
| 483 |
char curr_picbasename[32]; |
| 484 |
|
| 485 |
const char * |
| 486 |
machopic_get_function_picbase (void) |
| 487 |
{ |
| 488 |
/* We should only get here for -fPIC. */ |
| 489 |
gcc_checking_assert (MACHOPIC_PURE); |
| 490 |
|
| 491 |
update_pic_label_number_if_needed (); |
| 492 |
snprintf (curr_picbasename, 32, "L%u$pb", current_pic_label_num); |
| 493 |
return (const char *) curr_picbasename; |
| 494 |
} |
| 495 |
|
| 496 |
bool |
| 497 |
machopic_should_output_picbase_label (void) |
| 498 |
{ |
| 499 |
update_pic_label_number_if_needed (); |
| 500 |
|
| 501 |
if (current_pic_label_num == emitted_pic_label_num) |
| 502 |
return false; |
| 503 |
|
| 504 |
emitted_pic_label_num = current_pic_label_num; |
| 505 |
return true; |
| 506 |
} |
| 507 |
|
| 508 |
/* The suffix attached to non-lazy pointer symbols. */ |
| 509 |
#define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr" |
| 510 |
/* The suffix attached to stub symbols. */ |
| 511 |
#define STUB_SUFFIX "$stub" |
| 512 |
|
| 513 |
typedef struct GTY ((for_user)) machopic_indirection |
| 514 |
{ |
| 515 |
/* The SYMBOL_REF for the entity referenced. */ |
| 516 |
rtx symbol; |
| 517 |
/* The name of the stub or non-lazy pointer. */ |
| 518 |
const char * ptr_name; |
| 519 |
/* True iff this entry is for a stub (as opposed to a non-lazy |
| 520 |
pointer). */ |
| 521 |
bool stub_p; |
| 522 |
/* True iff this stub or pointer has been referenced. */ |
| 523 |
bool used; |
| 524 |
/* True iff a non-lazy symbol pointer should be emitted into the .data |
| 525 |
section, rather than the non-lazy symbol pointers section. The cases |
| 526 |
for which this occurred seem to have been unintentional, and later |
| 527 |
toolchains emit all of the indirections to the 'usual' section. We |
| 528 |
are keeping this in case it is necessary to preserve compatibility with |
| 529 |
older toolchains. */ |
| 530 |
bool nlsp_in_data_section; |
| 531 |
} machopic_indirection; |
| 532 |
|
| 533 |
struct indirection_hasher : ggc_ptr_hash<machopic_indirection> |
| 534 |
{ |
| 535 |
typedef const char *compare_type; |
| 536 |
static hashval_t hash (machopic_indirection *); |
| 537 |
static bool equal (machopic_indirection *, const char *); |
| 538 |
}; |
| 539 |
|
| 540 |
/* A table mapping stub names and non-lazy pointer names to |
| 541 |
SYMBOL_REFs for the stubbed-to and pointed-to entities. */ |
| 542 |
|
| 543 |
static GTY (()) hash_table<indirection_hasher> *machopic_indirections; |
| 544 |
|
| 545 |
/* Return a hash value for a SLOT in the indirections hash table. */ |
| 546 |
|
| 547 |
hashval_t |
| 548 |
indirection_hasher::hash (machopic_indirection *p) |
| 549 |
{ |
| 550 |
return htab_hash_string (p->ptr_name); |
| 551 |
} |
| 552 |
|
| 553 |
/* Returns true if the KEY is the same as that associated with |
| 554 |
SLOT. */ |
| 555 |
|
| 556 |
bool |
| 557 |
indirection_hasher::equal (machopic_indirection *s, const char *k) |
| 558 |
{ |
| 559 |
return strcmp (s->ptr_name, k) == 0; |
| 560 |
} |
| 561 |
|
| 562 |
/* Return the name of the non-lazy pointer (if STUB_P is false) or |
| 563 |
stub (if STUB_B is true) corresponding to the given name. |
| 564 |
|
| 565 |
PR71767 - If we have a situation like: |
| 566 |
|
| 567 |
global_weak_symbol: |
| 568 |
.... |
| 569 |
Lnon_weak_local: |
| 570 |
.... |
| 571 |
|
| 572 |
ld64 will be unable to split this into two atoms (because the "L" makes |
| 573 |
the second symbol 'invisible'). This means that legitimate direct accesses |
| 574 |
to the second symbol will appear to be direct accesses to an atom of type |
| 575 |
weak, global which are not allowed. |
| 576 |
|
| 577 |
To avoid this, we make any data-section indirections have a leading 'l' |
| 578 |
(lower-case L) which has a special meaning: linker can see this and use |
| 579 |
it to determine atoms, but it is not placed into the final symbol table. |
| 580 |
|
| 581 |
Symbols in the non-lazy symbol pointers section (or stubs) do not have this |
| 582 |
problem because ld64 already knows the size of each entry. |
| 583 |
*/ |
| 584 |
|
| 585 |
const char * |
| 586 |
machopic_indirection_name (rtx sym_ref, bool stub_p) |
| 587 |
{ |
| 588 |
const char *name = XSTR (sym_ref, 0); |
| 589 |
tree id = maybe_get_identifier (name); |
| 590 |
if (id) |
| 591 |
{ |
| 592 |
tree id_orig = id; |
| 593 |
|
| 594 |
while (IDENTIFIER_TRANSPARENT_ALIAS (id)) |
| 595 |
id = TREE_CHAIN (id); |
| 596 |
if (id != id_orig) |
| 597 |
name = IDENTIFIER_POINTER (id); |
| 598 |
} |
| 599 |
|
| 600 |
const char *prefix = user_label_prefix; |
| 601 |
/* If we are emitting the label 'verbatim' then omit the U_L_P and count |
| 602 |
the name without the leading '*'. */ |
| 603 |
if (name[0] == '*') |
| 604 |
{ |
| 605 |
prefix = ""; |
| 606 |
++name; |
| 607 |
} |
| 608 |
|
| 609 |
/* Here we are undoing a number of causes that placed some indirections |
| 610 |
(apparently erroneously) into the .data section. Specifically, some |
| 611 |
symbols that are ABI mandated indirections and some hidden symbols |
| 612 |
were being placed there - which cause difficulties with later |
| 613 |
versions of ld64. Iff (after these checks) some symbol still gets an |
| 614 |
indirection in the data section, we want to adjust the indirection |
| 615 |
name to be linker visible to deal with PR71767 (notes above). */ |
| 616 |
bool nlsp_in_data_section = |
| 617 |
! MACHO_SYMBOL_MUST_INDIRECT_P (sym_ref) |
| 618 |
&& ! MACHO_SYMBOL_HIDDEN_VIS_P (sym_ref) |
| 619 |
&& (machopic_symbol_defined_p (sym_ref) || SYMBOL_REF_LOCAL_P (sym_ref)) |
| 620 |
&& ! indirect_data (sym_ref); |
| 621 |
|
| 622 |
const char *suffix = stub_p ? STUB_SUFFIX : NON_LAZY_POINTER_SUFFIX; |
| 623 |
/* If the indirection is in the data section, let the linker see it. */ |
| 624 |
char L_or_l = (!stub_p && nlsp_in_data_section) ? 'l' : 'L'; |
| 625 |
/* We have mangled symbols with spaces and punctuation which typically |
| 626 |
need surrounding in quotes for the assembler to consume them. */ |
| 627 |
const char *quote = name_needs_quotes (name) ? "\"" : ""; |
| 628 |
char *buffer = XALLOCAVEC (char, 2 /* strlen ("&L") or ("&l") */ |
| 629 |
+ strlen (prefix) |
| 630 |
+ strlen (name) |
| 631 |
+ strlen (suffix) |
| 632 |
+ 2 * strlen (quote) |
| 633 |
+ 1 /* '\0' */); |
| 634 |
|
| 635 |
/* Construct the name of the non-lazy pointer or stub. */ |
| 636 |
sprintf (buffer, "&%s%c%s%s%s%s", quote, L_or_l, prefix, name, |
| 637 |
suffix, quote); |
| 638 |
|
| 639 |
if (!machopic_indirections) |
| 640 |
machopic_indirections = hash_table<indirection_hasher>::create_ggc (37); |
| 641 |
|
| 642 |
machopic_indirection **slot |
| 643 |
= machopic_indirections->find_slot_with_hash (buffer, |
| 644 |
htab_hash_string (buffer), |
| 645 |
INSERT); |
| 646 |
machopic_indirection *p; |
| 647 |
if (*slot) |
| 648 |
p = *slot; |
| 649 |
else |
| 650 |
{ |
| 651 |
p = ggc_alloc<machopic_indirection> (); |
| 652 |
p->symbol = sym_ref; |
| 653 |
p->ptr_name = xstrdup (buffer); |
| 654 |
p->stub_p = stub_p; |
| 655 |
p->used = false; |
| 656 |
p->nlsp_in_data_section = nlsp_in_data_section; |
| 657 |
*slot = p; |
| 658 |
} |
| 659 |
|
| 660 |
return p->ptr_name; |
| 661 |
} |
| 662 |
|
| 663 |
/* If NAME is the name of a stub or a non-lazy pointer , mark the stub |
| 664 |
or non-lazy pointer as used -- and mark the object to which the |
| 665 |
pointer/stub refers as used as well, since the pointer/stub will |
| 666 |
emit a reference to it. */ |
| 667 |
|
| 668 |
void |
| 669 |
machopic_validate_stub_or_non_lazy_ptr (const char *name) |
| 670 |
{ |
| 671 |
machopic_indirection *p |
| 672 |
= machopic_indirections->find_with_hash (name, htab_hash_string (name)); |
| 673 |
if (p && ! p->used) |
| 674 |
{ |
| 675 |
const char *real_name; |
| 676 |
tree id; |
| 677 |
|
| 678 |
p->used = true; |
| 679 |
|
| 680 |
/* Do what output_addr_const will do when we actually call it. */ |
| 681 |
if (SYMBOL_REF_DECL (p->symbol)) |
| 682 |
mark_decl_referenced (SYMBOL_REF_DECL (p->symbol)); |
| 683 |
|
| 684 |
real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0)); |
| 685 |
|
| 686 |
id = maybe_get_identifier (real_name); |
| 687 |
if (id) |
| 688 |
mark_referenced (id); |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
/* Transform ORIG, which may be any data source, to the corresponding |
| 693 |
source using indirections. */ |
| 694 |
|
| 695 |
rtx |
| 696 |
machopic_indirect_data_reference (rtx orig, rtx reg) |
| 697 |
{ |
| 698 |
rtx ptr_ref = orig; |
| 699 |
|
| 700 |
if (! MACHOPIC_INDIRECT) |
| 701 |
return orig; |
| 702 |
|
| 703 |
if (GET_CODE (orig) == SYMBOL_REF) |
| 704 |
{ |
| 705 |
int defined = machopic_data_defined_p (orig); |
| 706 |
|
| 707 |
if (defined && MACHO_DYNAMIC_NO_PIC_P) |
| 708 |
{ |
| 709 |
if (DARWIN_PPC) |
| 710 |
{ |
| 711 |
/* Create a new register for CSE opportunities. */ |
| 712 |
rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode)); |
| 713 |
emit_insn (gen_macho_high (Pmode, hi_reg, orig)); |
| 714 |
emit_insn (gen_macho_low (Pmode, reg, hi_reg, orig)); |
| 715 |
return reg; |
| 716 |
} |
| 717 |
else if (DARWIN_X86) |
| 718 |
return orig; |
| 719 |
else |
| 720 |
/* some other cpu -- writeme! */ |
| 721 |
gcc_unreachable (); |
| 722 |
} |
| 723 |
else if (defined && ! MACHO_SYMBOL_MUST_INDIRECT_P (orig)) |
| 724 |
{ |
| 725 |
rtx offset = NULL; |
| 726 |
if (DARWIN_PPC || HAVE_lo_sum) |
| 727 |
offset = machopic_gen_offset (orig); |
| 728 |
|
| 729 |
if (DARWIN_PPC) |
| 730 |
{ |
| 731 |
rtx hi_sum_reg = (!can_create_pseudo_p () |
| 732 |
? reg |
| 733 |
: gen_reg_rtx (Pmode)); |
| 734 |
|
| 735 |
gcc_assert (reg); |
| 736 |
|
| 737 |
emit_insn (gen_rtx_SET (hi_sum_reg, |
| 738 |
gen_rtx_PLUS (Pmode, pic_offset_table_rtx, |
| 739 |
gen_rtx_HIGH (Pmode, offset)))); |
| 740 |
emit_insn (gen_rtx_SET (reg, |
| 741 |
gen_rtx_LO_SUM (Pmode, hi_sum_reg, |
| 742 |
copy_rtx (offset)))); |
| 743 |
|
| 744 |
orig = reg; |
| 745 |
} |
| 746 |
else if (HAVE_lo_sum) |
| 747 |
{ |
| 748 |
gcc_assert (reg); |
| 749 |
|
| 750 |
emit_insn (gen_rtx_SET (reg, gen_rtx_HIGH (Pmode, offset))); |
| 751 |
emit_insn (gen_rtx_SET (reg, gen_rtx_LO_SUM (Pmode, reg, |
| 752 |
copy_rtx (offset)))); |
| 753 |
emit_use (pic_offset_table_rtx); |
| 754 |
|
| 755 |
orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg); |
| 756 |
} |
| 757 |
return orig; |
| 758 |
} |
| 759 |
|
| 760 |
ptr_ref = (gen_rtx_SYMBOL_REF |
| 761 |
(Pmode, |
| 762 |
machopic_indirection_name (orig, /*stub_p=*/false))); |
| 763 |
|
| 764 |
SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig); |
| 765 |
SYMBOL_REF_FLAGS (ptr_ref) |= MACHO_SYMBOL_FLAG_INDIRECTION; |
| 766 |
|
| 767 |
ptr_ref = gen_const_mem (Pmode, ptr_ref); |
| 768 |
machopic_define_symbol (ptr_ref); |
| 769 |
|
| 770 |
if (DARWIN_X86 |
| 771 |
&& reg |
| 772 |
&& MACHO_DYNAMIC_NO_PIC_P) |
| 773 |
{ |
| 774 |
emit_insn (gen_rtx_SET (reg, ptr_ref)); |
| 775 |
ptr_ref = reg; |
| 776 |
} |
| 777 |
|
| 778 |
return ptr_ref; |
| 779 |
} |
| 780 |
else if (GET_CODE (orig) == CONST) |
| 781 |
{ |
| 782 |
/* If "(const (plus ...", walk the PLUS and return that result. |
| 783 |
PLUS processing (below) will restore the "(const ..." if |
| 784 |
appropriate. */ |
| 785 |
if (GET_CODE (XEXP (orig, 0)) == PLUS) |
| 786 |
return machopic_indirect_data_reference (XEXP (orig, 0), reg); |
| 787 |
else |
| 788 |
return orig; |
| 789 |
} |
| 790 |
else if (GET_CODE (orig) == MEM) |
| 791 |
{ |
| 792 |
XEXP (ptr_ref, 0) = |
| 793 |
machopic_indirect_data_reference (XEXP (orig, 0), reg); |
| 794 |
return ptr_ref; |
| 795 |
} |
| 796 |
else if (GET_CODE (orig) == PLUS) |
| 797 |
{ |
| 798 |
rtx base, result; |
| 799 |
|
| 800 |
/* Legitimize both operands of the PLUS. */ |
| 801 |
base = machopic_indirect_data_reference (XEXP (orig, 0), reg); |
| 802 |
orig = machopic_indirect_data_reference (XEXP (orig, 1), |
| 803 |
(base == reg ? 0 : reg)); |
| 804 |
if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT)) |
| 805 |
result = plus_constant (Pmode, base, INTVAL (orig)); |
| 806 |
else |
| 807 |
result = gen_rtx_PLUS (Pmode, base, orig); |
| 808 |
|
| 809 |
if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM) |
| 810 |
{ |
| 811 |
if (reg) |
| 812 |
{ |
| 813 |
emit_move_insn (reg, result); |
| 814 |
result = reg; |
| 815 |
} |
| 816 |
else |
| 817 |
{ |
| 818 |
result = force_reg (GET_MODE (result), result); |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
return result; |
| 823 |
} |
| 824 |
return ptr_ref; |
| 825 |
} |
| 826 |
|
| 827 |
/* Transform TARGET (a MEM), which is a function call target, to the |
| 828 |
corresponding symbol_stub if necessary. Return a new MEM. */ |
| 829 |
|
| 830 |
rtx |
| 831 |
machopic_indirect_call_target (rtx target) |
| 832 |
{ |
| 833 |
if (! darwin_symbol_stubs) |
| 834 |
return target; |
| 835 |
|
| 836 |
if (GET_CODE (target) != MEM) |
| 837 |
return target; |
| 838 |
|
| 839 |
if (MACHOPIC_INDIRECT |
| 840 |
&& GET_CODE (XEXP (target, 0)) == SYMBOL_REF |
| 841 |
&& ! MACHO_SYMBOL_DEFINED_P (XEXP (target, 0))) |
| 842 |
{ |
| 843 |
rtx sym_ref = XEXP (target, 0); |
| 844 |
const char *stub_name = machopic_indirection_name (sym_ref, |
| 845 |
/*stub_p=*/true); |
| 846 |
machine_mode mode = GET_MODE (sym_ref); |
| 847 |
|
| 848 |
XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name); |
| 849 |
SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref); |
| 850 |
SYMBOL_REF_FLAGS (XEXP (target, 0)) |= MACHO_SYMBOL_FLAG_INDIRECTION; |
| 851 |
MEM_READONLY_P (target) = 1; |
| 852 |
MEM_NOTRAP_P (target) = 1; |
| 853 |
} |
| 854 |
|
| 855 |
return target; |
| 856 |
} |
| 857 |
|
| 858 |
rtx |
| 859 |
machopic_legitimize_pic_address (rtx orig, machine_mode mode, rtx reg) |
| 860 |
{ |
| 861 |
rtx pic_ref = orig; |
| 862 |
|
| 863 |
if (! MACHOPIC_INDIRECT) |
| 864 |
return orig; |
| 865 |
|
| 866 |
/* First handle a simple SYMBOL_REF or LABEL_REF */ |
| 867 |
if (GET_CODE (orig) == LABEL_REF |
| 868 |
|| GET_CODE (orig) == SYMBOL_REF) |
| 869 |
{ |
| 870 |
/* addr(foo) = &func+(foo-func) */ |
| 871 |
orig = machopic_indirect_data_reference (orig, reg); |
| 872 |
|
| 873 |
if (GET_CODE (orig) == PLUS |
| 874 |
&& GET_CODE (XEXP (orig, 0)) == REG) |
| 875 |
{ |
| 876 |
if (reg == 0) |
| 877 |
return force_reg (mode, orig); |
| 878 |
|
| 879 |
emit_move_insn (reg, orig); |
| 880 |
return reg; |
| 881 |
} |
| 882 |
|
| 883 |
if (GET_CODE (orig) == MEM) |
| 884 |
{ |
| 885 |
if (reg == 0) |
| 886 |
{ |
| 887 |
gcc_assert (!lra_in_progress); |
| 888 |
reg = gen_reg_rtx (Pmode); |
| 889 |
} |
| 890 |
|
| 891 |
#if HAVE_lo_sum |
| 892 |
if (MACHO_DYNAMIC_NO_PIC_P |
| 893 |
&& (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF |
| 894 |
|| GET_CODE (XEXP (orig, 0)) == LABEL_REF)) |
| 895 |
{ |
| 896 |
#if defined (TARGET_TOC) /* ppc */ |
| 897 |
rtx temp_reg = (!can_create_pseudo_p () |
| 898 |
? reg : |
| 899 |
gen_reg_rtx (Pmode)); |
| 900 |
rtx asym = XEXP (orig, 0); |
| 901 |
rtx mem; |
| 902 |
|
| 903 |
emit_insn (gen_macho_high (Pmode, temp_reg, asym)); |
| 904 |
mem = gen_const_mem (GET_MODE (orig), |
| 905 |
gen_rtx_LO_SUM (Pmode, temp_reg, |
| 906 |
copy_rtx (asym))); |
| 907 |
emit_insn (gen_rtx_SET (reg, mem)); |
| 908 |
#else |
| 909 |
/* Some other CPU -- WriteMe! but right now there are no other |
| 910 |
platforms that can use dynamic-no-pic */ |
| 911 |
gcc_unreachable (); |
| 912 |
#endif |
| 913 |
pic_ref = reg; |
| 914 |
} |
| 915 |
else |
| 916 |
if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF |
| 917 |
|| GET_CODE (XEXP (orig, 0)) == LABEL_REF) |
| 918 |
{ |
| 919 |
rtx offset = machopic_gen_offset (XEXP (orig, 0)); |
| 920 |
#if defined (TARGET_TOC) /* i.e., PowerPC */ |
| 921 |
/* Generating a new reg may expose opportunities for |
| 922 |
common subexpression elimination. */ |
| 923 |
rtx hi_sum_reg = (!can_create_pseudo_p () |
| 924 |
? reg |
| 925 |
: gen_reg_rtx (Pmode)); |
| 926 |
rtx mem; |
| 927 |
rtx sum; |
| 928 |
|
| 929 |
sum = gen_rtx_HIGH (Pmode, offset); |
| 930 |
if (! MACHO_DYNAMIC_NO_PIC_P) |
| 931 |
sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum); |
| 932 |
|
| 933 |
emit_insn (gen_rtx_SET (hi_sum_reg, sum)); |
| 934 |
|
| 935 |
mem = gen_const_mem (GET_MODE (orig), |
| 936 |
gen_rtx_LO_SUM (Pmode, |
| 937 |
hi_sum_reg, |
| 938 |
copy_rtx (offset))); |
| 939 |
rtx_insn *insn = emit_insn (gen_rtx_SET (reg, mem)); |
| 940 |
set_unique_reg_note (insn, REG_EQUAL, pic_ref); |
| 941 |
|
| 942 |
pic_ref = reg; |
| 943 |
#else |
| 944 |
emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM)); |
| 945 |
|
| 946 |
emit_insn (gen_rtx_SET (reg, |
| 947 |
gen_rtx_HIGH (Pmode, |
| 948 |
gen_rtx_CONST (Pmode, |
| 949 |
offset)))); |
| 950 |
emit_insn (gen_rtx_SET (reg, |
| 951 |
gen_rtx_LO_SUM (Pmode, reg, |
| 952 |
gen_rtx_CONST (Pmode, |
| 953 |
copy_rtx (offset))))); |
| 954 |
pic_ref = gen_rtx_PLUS (Pmode, |
| 955 |
pic_offset_table_rtx, reg); |
| 956 |
#endif |
| 957 |
} |
| 958 |
else |
| 959 |
#endif /* HAVE_lo_sum */ |
| 960 |
{ |
| 961 |
rtx pic = pic_offset_table_rtx; |
| 962 |
if (GET_CODE (pic) != REG) |
| 963 |
{ |
| 964 |
emit_move_insn (reg, pic); |
| 965 |
pic = reg; |
| 966 |
} |
| 967 |
|
| 968 |
if (lra_in_progress && HARD_REGISTER_P (pic)) |
| 969 |
df_set_regs_ever_live (REGNO (pic), true); |
| 970 |
pic_ref = gen_rtx_PLUS (Pmode, pic, |
| 971 |
machopic_gen_offset (XEXP (orig, 0))); |
| 972 |
} |
| 973 |
|
| 974 |
#if !defined (TARGET_TOC) |
| 975 |
emit_move_insn (reg, pic_ref); |
| 976 |
pic_ref = gen_const_mem (GET_MODE (orig), reg); |
| 977 |
#endif |
| 978 |
} |
| 979 |
else |
| 980 |
{ |
| 981 |
|
| 982 |
#if HAVE_lo_sum |
| 983 |
if (GET_CODE (orig) == SYMBOL_REF |
| 984 |
|| GET_CODE (orig) == LABEL_REF) |
| 985 |
{ |
| 986 |
rtx offset = machopic_gen_offset (orig); |
| 987 |
#if defined (TARGET_TOC) /* i.e., PowerPC */ |
| 988 |
rtx hi_sum_reg; |
| 989 |
|
| 990 |
if (reg == 0) |
| 991 |
{ |
| 992 |
gcc_assert (!lra_in_progress); |
| 993 |
reg = gen_reg_rtx (Pmode); |
| 994 |
} |
| 995 |
|
| 996 |
hi_sum_reg = reg; |
| 997 |
|
| 998 |
emit_insn (gen_rtx_SET (hi_sum_reg, |
| 999 |
(MACHO_DYNAMIC_NO_PIC_P) |
| 1000 |
? gen_rtx_HIGH (Pmode, offset) |
| 1001 |
: gen_rtx_PLUS (Pmode, |
| 1002 |
pic_offset_table_rtx, |
| 1003 |
gen_rtx_HIGH (Pmode, |
| 1004 |
offset)))); |
| 1005 |
emit_insn (gen_rtx_SET (reg, |
| 1006 |
gen_rtx_LO_SUM (Pmode, |
| 1007 |
hi_sum_reg, |
| 1008 |
copy_rtx (offset)))); |
| 1009 |
pic_ref = reg; |
| 1010 |
#else |
| 1011 |
emit_insn (gen_rtx_SET (reg, gen_rtx_HIGH (Pmode, offset))); |
| 1012 |
emit_insn (gen_rtx_SET (reg, |
| 1013 |
gen_rtx_LO_SUM (Pmode, reg, |
| 1014 |
copy_rtx (offset)))); |
| 1015 |
pic_ref = gen_rtx_PLUS (Pmode, |
| 1016 |
pic_offset_table_rtx, reg); |
| 1017 |
#endif |
| 1018 |
} |
| 1019 |
else |
| 1020 |
#endif /* HAVE_lo_sum */ |
| 1021 |
{ |
| 1022 |
if (REG_P (orig) |
| 1023 |
|| GET_CODE (orig) == SUBREG) |
| 1024 |
{ |
| 1025 |
return orig; |
| 1026 |
} |
| 1027 |
else |
| 1028 |
{ |
| 1029 |
rtx pic = pic_offset_table_rtx; |
| 1030 |
if (GET_CODE (pic) != REG) |
| 1031 |
{ |
| 1032 |
emit_move_insn (reg, pic); |
| 1033 |
pic = reg; |
| 1034 |
} |
| 1035 |
|
| 1036 |
if (lra_in_progress && HARD_REGISTER_P (pic)) |
| 1037 |
df_set_regs_ever_live (REGNO (pic), true); |
| 1038 |
pic_ref = gen_rtx_PLUS (Pmode, |
| 1039 |
pic, |
| 1040 |
machopic_gen_offset (orig)); |
| 1041 |
} |
| 1042 |
} |
| 1043 |
} |
| 1044 |
|
| 1045 |
if (GET_CODE (pic_ref) != REG) |
| 1046 |
{ |
| 1047 |
if (reg != 0) |
| 1048 |
{ |
| 1049 |
emit_move_insn (reg, pic_ref); |
| 1050 |
return reg; |
| 1051 |
} |
| 1052 |
else |
| 1053 |
{ |
| 1054 |
return force_reg (mode, pic_ref); |
| 1055 |
} |
| 1056 |
} |
| 1057 |
else |
| 1058 |
{ |
| 1059 |
return pic_ref; |
| 1060 |
} |
| 1061 |
} |
| 1062 |
else if (GET_CODE (orig) == PLUS |
| 1063 |
&& (GET_CODE (XEXP (orig, 0)) == MEM |
| 1064 |
|| GET_CODE (XEXP (orig, 0)) == SYMBOL_REF |
| 1065 |
|| GET_CODE (XEXP (orig, 0)) == LABEL_REF) |
| 1066 |
&& XEXP (orig, 0) != pic_offset_table_rtx |
| 1067 |
&& GET_CODE (XEXP (orig, 1)) != REG) |
| 1068 |
|
| 1069 |
{ |
| 1070 |
rtx base; |
| 1071 |
int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM); |
| 1072 |
|
| 1073 |
base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg); |
| 1074 |
orig = machopic_legitimize_pic_address (XEXP (orig, 1), |
| 1075 |
Pmode, (base == reg ? 0 : reg)); |
| 1076 |
if (GET_CODE (orig) == CONST_INT) |
| 1077 |
{ |
| 1078 |
pic_ref = plus_constant (Pmode, base, INTVAL (orig)); |
| 1079 |
is_complex = 1; |
| 1080 |
} |
| 1081 |
else |
| 1082 |
pic_ref = gen_rtx_PLUS (Pmode, base, orig); |
| 1083 |
|
| 1084 |
if (reg && is_complex) |
| 1085 |
{ |
| 1086 |
emit_move_insn (reg, pic_ref); |
| 1087 |
pic_ref = reg; |
| 1088 |
} |
| 1089 |
/* Likewise, should we set special REG_NOTEs here? */ |
| 1090 |
} |
| 1091 |
else if (GET_CODE (orig) == CONST) |
| 1092 |
{ |
| 1093 |
return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg); |
| 1094 |
} |
| 1095 |
else if (GET_CODE (orig) == MEM |
| 1096 |
&& GET_CODE (XEXP (orig, 0)) == SYMBOL_REF) |
| 1097 |
{ |
| 1098 |
rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg); |
| 1099 |
addr = replace_equiv_address (orig, addr); |
| 1100 |
emit_move_insn (reg, addr); |
| 1101 |
pic_ref = reg; |
| 1102 |
} |
| 1103 |
|
| 1104 |
return pic_ref; |
| 1105 |
} |
| 1106 |
|
| 1107 |
/* Callbacks to output the stub or non-lazy pointers. |
| 1108 |
Each works on the item in *SLOT,if it has been used. |
| 1109 |
DATA is the FILE* for assembly output. |
| 1110 |
Called from htab_traverses, invoked from machopic_finish(). */ |
| 1111 |
|
| 1112 |
int |
| 1113 |
machopic_output_data_section_indirection (machopic_indirection **slot, |
| 1114 |
FILE *out_file) |
| 1115 |
{ |
| 1116 |
machopic_indirection *p = *slot; |
| 1117 |
|
| 1118 |
if (!p->used || !p->nlsp_in_data_section) |
| 1119 |
return 1; |
| 1120 |
|
| 1121 |
rtx symbol = p->symbol; |
| 1122 |
/* The original symbol name. */ |
| 1123 |
const char *sym_name = XSTR (symbol, 0); |
| 1124 |
/* The name of the indirection symbol. */ |
| 1125 |
const char *ptr_name = p->ptr_name; |
| 1126 |
|
| 1127 |
switch_to_section (data_section); |
| 1128 |
assemble_align (GET_MODE_ALIGNMENT (Pmode)); |
| 1129 |
assemble_label (out_file, ptr_name); |
| 1130 |
assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name), |
| 1131 |
GET_MODE_SIZE (Pmode), |
| 1132 |
GET_MODE_ALIGNMENT (Pmode), 1); |
| 1133 |
|
| 1134 |
return 1; |
| 1135 |
} |
| 1136 |
|
| 1137 |
int |
| 1138 |
machopic_output_stub_indirection (machopic_indirection **slot, |
| 1139 |
FILE *out_file) |
| 1140 |
{ |
| 1141 |
machopic_indirection *p = *slot; |
| 1142 |
|
| 1143 |
if (!p->used || !p->stub_p) |
| 1144 |
return 1; |
| 1145 |
|
| 1146 |
rtx symbol = p->symbol; |
| 1147 |
/* The original symbol name. */ |
| 1148 |
const char *sym_name = XSTR (symbol, 0); |
| 1149 |
/* The name of the stub symbol. */ |
| 1150 |
const char *ptr_name = p->ptr_name; |
| 1151 |
|
| 1152 |
tree id = maybe_get_identifier (sym_name); |
| 1153 |
if (id) |
| 1154 |
{ |
| 1155 |
tree id_orig = id; |
| 1156 |
|
| 1157 |
while (IDENTIFIER_TRANSPARENT_ALIAS (id)) |
| 1158 |
id = TREE_CHAIN (id); |
| 1159 |
if (id != id_orig) |
| 1160 |
sym_name = IDENTIFIER_POINTER (id); |
| 1161 |
} |
| 1162 |
|
| 1163 |
char *sym = XALLOCAVEC (char, strlen (sym_name) + 2); |
| 1164 |
if (sym_name[0] == '*' || sym_name[0] == '&') |
| 1165 |
strcpy (sym, sym_name + 1); |
| 1166 |
else if (sym_name[0] == '-' || sym_name[0] == '+') |
| 1167 |
strcpy (sym, sym_name); |
| 1168 |
else |
| 1169 |
sprintf (sym, "%s%s", user_label_prefix, sym_name); |
| 1170 |
|
| 1171 |
char *stub = XALLOCAVEC (char, strlen (ptr_name) + 2); |
| 1172 |
if (ptr_name[0] == '*' || ptr_name[0] == '&') |
| 1173 |
strcpy (stub, ptr_name + 1); |
| 1174 |
else |
| 1175 |
sprintf (stub, "%s%s", user_label_prefix, ptr_name); |
| 1176 |
|
| 1177 |
machopic_output_stub (out_file, sym, stub); |
| 1178 |
|
| 1179 |
return 1; |
| 1180 |
} |
| 1181 |
|
| 1182 |
int |
| 1183 |
machopic_output_indirection (machopic_indirection **slot, FILE *out_file) |
| 1184 |
{ |
| 1185 |
machopic_indirection *p = *slot; |
| 1186 |
|
| 1187 |
if (!p->used || p->stub_p || p->nlsp_in_data_section) |
| 1188 |
return 1; |
| 1189 |
|
| 1190 |
rtx symbol = p->symbol; |
| 1191 |
/* The original symbol name. */ |
| 1192 |
const char *sym_name = XSTR (symbol, 0); |
| 1193 |
/* The nonlazy-stub symbol name. */ |
| 1194 |
const char *ptr_name = p->ptr_name; |
| 1195 |
|
| 1196 |
switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]); |
| 1197 |
|
| 1198 |
/* Mach-O symbols are passed around in code through indirect references and |
| 1199 |
the original symbol_ref hasn't passed through the generic handling and |
| 1200 |
reference-catching in output_operand, so we need to manually mark weak |
| 1201 |
references as such. */ |
| 1202 |
|
| 1203 |
if (SYMBOL_REF_WEAK (symbol)) |
| 1204 |
{ |
| 1205 |
tree decl = SYMBOL_REF_DECL (symbol); |
| 1206 |
gcc_checking_assert (DECL_P (decl)); |
| 1207 |
|
| 1208 |
if (decl != NULL_TREE |
| 1209 |
&& DECL_EXTERNAL (decl) && TREE_PUBLIC (decl) |
| 1210 |
/* Handle only actual external-only definitions, not |
| 1211 |
e.g. extern inline code or variables for which |
| 1212 |
storage has been allocated. */ |
| 1213 |
&& !TREE_STATIC (decl)) |
| 1214 |
{ |
| 1215 |
fputs ("\t.weak_reference ", out_file); |
| 1216 |
assemble_name (out_file, sym_name); |
| 1217 |
fputc ('\n', out_file); |
| 1218 |
} |
| 1219 |
} |
| 1220 |
|
| 1221 |
assemble_name (out_file, ptr_name); |
| 1222 |
fprintf (out_file, ":\n"); |
| 1223 |
|
| 1224 |
fprintf (out_file, "\t.indirect_symbol "); |
| 1225 |
assemble_name (out_file, sym_name); |
| 1226 |
fprintf (out_file, "\n"); |
| 1227 |
|
| 1228 |
/* Variables that are marked with MACHO_SYMBOL_FLAG_STATIC need to |
| 1229 |
have their symbol name instead of 0 in the second entry of |
| 1230 |
the non-lazy symbol pointer data structure when they are |
| 1231 |
defined. This allows the runtime to rebind newer instances |
| 1232 |
of the translation unit with the original instance of the |
| 1233 |
symbol. */ |
| 1234 |
|
| 1235 |
rtx init = const0_rtx; |
| 1236 |
if (MACHO_SYMBOL_STATIC_P (symbol) && machopic_symbol_defined_p (symbol)) |
| 1237 |
init = gen_rtx_SYMBOL_REF (Pmode, sym_name); |
| 1238 |
|
| 1239 |
assemble_integer (init, GET_MODE_SIZE (Pmode), |
| 1240 |
GET_MODE_ALIGNMENT (Pmode), 1); |
| 1241 |
|
| 1242 |
return 1; |
| 1243 |
} |
| 1244 |
|
| 1245 |
static void |
| 1246 |
machopic_finish (FILE *out_file) |
| 1247 |
{ |
| 1248 |
if (!machopic_indirections) |
| 1249 |
return; |
| 1250 |
|
| 1251 |
/* First output an symbol indirections that have been placed into .data |
| 1252 |
(we don't expect these now). */ |
| 1253 |
machopic_indirections->traverse_noresize |
| 1254 |
<FILE *, machopic_output_data_section_indirection> (out_file); |
| 1255 |
|
| 1256 |
machopic_indirections->traverse_noresize |
| 1257 |
<FILE *, machopic_output_stub_indirection> (out_file); |
| 1258 |
|
| 1259 |
machopic_indirections->traverse_noresize |
| 1260 |
<FILE *, machopic_output_indirection> (out_file); |
| 1261 |
} |
| 1262 |
|
| 1263 |
int |
| 1264 |
machopic_operand_p (rtx op) |
| 1265 |
{ |
| 1266 |
if (MACHOPIC_JUST_INDIRECT) |
| 1267 |
return (GET_CODE (op) == SYMBOL_REF |
| 1268 |
&& machopic_symbol_defined_p (op)); |
| 1269 |
else |
| 1270 |
return (GET_CODE (op) == CONST |
| 1271 |
&& GET_CODE (XEXP (op, 0)) == UNSPEC |
| 1272 |
&& XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET); |
| 1273 |
} |
| 1274 |
|
| 1275 |
/* This function: |
| 1276 |
computes and caches a series of flags that characterise the symbol's |
| 1277 |
properties that affect Mach-O code gen (including accidental cases |
| 1278 |
from older toolchains). |
| 1279 |
|
| 1280 |
TODO: |
| 1281 |
Here we also need to do enough analysis to determine if a symbol's |
| 1282 |
name needs to be made linker-visible. This is more tricky - since |
| 1283 |
it depends on whether we've previously seen a global weak definition |
| 1284 |
in the same section. |
| 1285 |
*/ |
| 1286 |
|
| 1287 |
void |
| 1288 |
darwin_encode_section_info (tree decl, rtx rtl, int first) |
| 1289 |
{ |
| 1290 |
/* Careful not to prod global register variables. */ |
| 1291 |
if (!MEM_P (rtl)) |
| 1292 |
return; |
| 1293 |
|
| 1294 |
/* Do the standard encoding things first; this sets: |
| 1295 |
SYMBOL_FLAG_FUNCTION, |
| 1296 |
SYMBOL_FLAG_LOCAL, (binds_local_p) |
| 1297 |
TLS_MODEL, SYMBOL_FLAG_SMALL |
| 1298 |
SYMBOL_FLAG_EXTERNAL. */ |
| 1299 |
default_encode_section_info (decl, rtl, first); |
| 1300 |
|
| 1301 |
if (! VAR_OR_FUNCTION_DECL_P (decl)) |
| 1302 |
return; |
| 1303 |
|
| 1304 |
rtx sym_ref = XEXP (rtl, 0); |
| 1305 |
if (VAR_P (decl)) |
| 1306 |
SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE; |
| 1307 |
|
| 1308 |
/* Only really common if there's no initialiser. */ |
| 1309 |
bool really_common_p = (DECL_COMMON (decl) |
| 1310 |
&& (DECL_INITIAL (decl) == NULL |
| 1311 |
|| (!in_lto_p |
| 1312 |
&& DECL_INITIAL (decl) == error_mark_node))); |
| 1313 |
|
| 1314 |
/* For Darwin, if we have specified visibility and it's not the default |
| 1315 |
that's counted 'hidden'. */ |
| 1316 |
if (DECL_VISIBILITY_SPECIFIED (decl) |
| 1317 |
&& DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT) |
| 1318 |
SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_HIDDEN_VIS; |
| 1319 |
|
| 1320 |
if (!DECL_EXTERNAL (decl) |
| 1321 |
&& (!TREE_PUBLIC (decl) || !DECL_WEAK (decl)) |
| 1322 |
&& ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)) |
| 1323 |
&& ((TREE_STATIC (decl) |
| 1324 |
&& (!DECL_COMMON (decl) || !TREE_PUBLIC (decl))) |
| 1325 |
|| (!DECL_COMMON (decl) && DECL_INITIAL (decl) |
| 1326 |
&& DECL_INITIAL (decl) != error_mark_node))) |
| 1327 |
SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED; |
| 1328 |
|
| 1329 |
if (! TREE_PUBLIC (decl)) |
| 1330 |
SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_STATIC; |
| 1331 |
|
| 1332 |
/* Short cut check for Darwin 'must indirect' rules. */ |
| 1333 |
if (really_common_p |
| 1334 |
|| (DECL_WEAK (decl) && ! MACHO_SYMBOL_HIDDEN_VIS_P (sym_ref)) |
| 1335 |
|| lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))) |
| 1336 |
SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_MUST_INDIRECT; |
| 1337 |
|
| 1338 |
#if DARWIN_PPC |
| 1339 |
/* Objective C V2 (m64) IVAR offset refs from Apple GCC-4.x have an |
| 1340 |
indirection for m64 code on PPC. Historically, these indirections |
| 1341 |
also appear in the .data section. */ |
| 1342 |
tree o2meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl)); |
| 1343 |
o2meta = o2meta ? TREE_VALUE (o2meta) : NULL_TREE; |
| 1344 |
|
| 1345 |
if (o2meta && startswith (IDENTIFIER_POINTER (o2meta), "V2_IVRF")) |
| 1346 |
SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_MUST_INDIRECT; |
| 1347 |
#endif |
| 1348 |
} |
| 1349 |
|
| 1350 |
void |
| 1351 |
darwin_mark_decl_preserved (const char *name) |
| 1352 |
{ |
| 1353 |
/* Actually we shouldn't mark any local symbol this way, but for now |
| 1354 |
this only happens with ObjC meta-data. */ |
| 1355 |
if (darwin_label_is_anonymous_local_objc_name (name)) |
| 1356 |
return; |
| 1357 |
|
| 1358 |
fprintf (asm_out_file, "\t.no_dead_strip "); |
| 1359 |
assemble_name (asm_out_file, name); |
| 1360 |
fputc ('\n', asm_out_file); |
| 1361 |
} |
| 1362 |
|
| 1363 |
static section * |
| 1364 |
darwin_rodata_section (int use_coal, bool zsize, int reloc) |
| 1365 |
{ |
| 1366 |
return (use_coal |
| 1367 |
? darwin_sections[const_coal_section] |
| 1368 |
: (zsize ? darwin_sections[zobj_const_section] |
| 1369 |
: reloc ? darwin_sections[const_data_section] |
| 1370 |
: darwin_sections[const_section])); |
| 1371 |
} |
| 1372 |
|
| 1373 |
static section * |
| 1374 |
darwin_mergeable_string_section (tree exp, |
| 1375 |
unsigned HOST_WIDE_INT align) |
| 1376 |
{ |
| 1377 |
/* Darwin's ld expects to see non-writable string literals in the .cstring |
| 1378 |
section. Later versions of ld check and complain when CFStrings are |
| 1379 |
enabled. Therefore we shall force the strings into .cstring since we |
| 1380 |
don't support writable ones anyway. */ |
| 1381 |
if ((darwin_constant_cfstrings || flag_merge_constants) |
| 1382 |
&& TREE_CODE (exp) == STRING_CST |
| 1383 |
&& TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE |
| 1384 |
&& align <= 256 |
| 1385 |
&& (int_size_in_bytes (TREE_TYPE (exp)) |
| 1386 |
== TREE_STRING_LENGTH (exp)) |
| 1387 |
&& ((size_t) TREE_STRING_LENGTH (exp) |
| 1388 |
== strlen (TREE_STRING_POINTER (exp)) + 1)) |
| 1389 |
return darwin_sections[cstring_section]; |
| 1390 |
|
| 1391 |
if (DARWIN_SECTION_ANCHORS && flag_section_anchors |
| 1392 |
&& TREE_CODE (exp) == STRING_CST |
| 1393 |
&& TREE_STRING_LENGTH (exp) == 0) |
| 1394 |
return darwin_sections[zobj_const_section]; |
| 1395 |
|
| 1396 |
return readonly_data_section; |
| 1397 |
} |
| 1398 |
|
| 1399 |
#ifndef HAVE_GAS_LITERAL16 |
| 1400 |
#define HAVE_GAS_LITERAL16 0 |
| 1401 |
#endif |
| 1402 |
|
| 1403 |
static section * |
| 1404 |
darwin_mergeable_constant_section (tree exp, |
| 1405 |
unsigned HOST_WIDE_INT align, |
| 1406 |
bool zsize) |
| 1407 |
{ |
| 1408 |
if (zsize) |
| 1409 |
return darwin_sections[zobj_const_section]; |
| 1410 |
|
| 1411 |
machine_mode mode = DECL_MODE (exp); |
| 1412 |
if (!flag_merge_constants |
| 1413 |
|| mode == VOIDmode |
| 1414 |
|| mode == BLKmode |
| 1415 |
|| align < 8 |
| 1416 |
|| align > 256 |
| 1417 |
|| (align & (align -1)) != 0) |
| 1418 |
return readonly_data_section; |
| 1419 |
|
| 1420 |
/* This will ICE if the mode is not a constant size, but that is reasonable, |
| 1421 |
since one cannot put a variable-sized thing into a constant section, we |
| 1422 |
shouldn't be trying. */ |
| 1423 |
const unsigned int modesize = GET_MODE_BITSIZE (mode).to_constant (); |
| 1424 |
|
| 1425 |
if (modesize > align) |
| 1426 |
return readonly_data_section; |
| 1427 |
|
| 1428 |
tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp)); |
| 1429 |
|
| 1430 |
if (TREE_CODE (size) != INTEGER_CST) |
| 1431 |
return readonly_data_section; |
| 1432 |
|
| 1433 |
unsigned isize = TREE_INT_CST_LOW (size); |
| 1434 |
if (isize == 4) |
| 1435 |
return darwin_sections[literal4_section]; |
| 1436 |
else if (isize == 8) |
| 1437 |
return darwin_sections[literal8_section]; |
| 1438 |
else if (HAVE_GAS_LITERAL16 |
| 1439 |
&& TARGET_64BIT |
| 1440 |
&& isize == 16) |
| 1441 |
return darwin_sections[literal16_section]; |
| 1442 |
|
| 1443 |
return readonly_data_section; |
| 1444 |
} |
| 1445 |
|
| 1446 |
section * |
| 1447 |
darwin_tm_clone_table_section (void) |
| 1448 |
{ |
| 1449 |
return get_named_section (NULL, |
| 1450 |
"__DATA,__tm_clone_table,regular,no_dead_strip", |
| 1451 |
3); |
| 1452 |
} |
| 1453 |
|
| 1454 |
int |
| 1455 |
machopic_reloc_rw_mask (void) |
| 1456 |
{ |
| 1457 |
return MACHOPIC_INDIRECT ? 3 : 0; |
| 1458 |
} |
| 1459 |
|
| 1460 |
/* We have to deal with ObjC/C++ metadata section placement in the common |
| 1461 |
code, since it will also be called from LTO. |
| 1462 |
|
| 1463 |
Return metadata attributes, if present (searching for ABI=2 first) |
| 1464 |
Return NULL_TREE if no such attributes are found. */ |
| 1465 |
|
| 1466 |
static tree |
| 1467 |
is_objc_metadata (tree decl) |
| 1468 |
{ |
| 1469 |
if (DECL_P (decl) |
| 1470 |
&& (VAR_P (decl) || TREE_CODE (decl) == CONST_DECL) |
| 1471 |
&& DECL_ATTRIBUTES (decl)) |
| 1472 |
{ |
| 1473 |
tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl)); |
| 1474 |
if (meta) |
| 1475 |
return meta; |
| 1476 |
meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl)); |
| 1477 |
if (meta) |
| 1478 |
return meta; |
| 1479 |
} |
| 1480 |
return NULL_TREE; |
| 1481 |
} |
| 1482 |
|
| 1483 |
static int classes_seen; |
| 1484 |
static int objc_metadata_seen; |
| 1485 |
|
| 1486 |
/* Return the section required for Objective C ABI 2 metadata. */ |
| 1487 |
static section * |
| 1488 |
darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base) |
| 1489 |
{ |
| 1490 |
const char *p; |
| 1491 |
tree ident = TREE_VALUE (meta); |
| 1492 |
gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE); |
| 1493 |
p = IDENTIFIER_POINTER (ident); |
| 1494 |
|
| 1495 |
gcc_checking_assert (flag_next_runtime >= 1 && flag_objc_abi == 2); |
| 1496 |
|
| 1497 |
objc_metadata_seen = 1; |
| 1498 |
|
| 1499 |
if (base == data_section) |
| 1500 |
base = darwin_sections[objc2_metadata_section]; |
| 1501 |
|
| 1502 |
/* Most of the OBJC2 META-data end up in the base section, so check it |
| 1503 |
first. */ |
| 1504 |
if (startswith (p, "V2_BASE")) |
| 1505 |
return base; |
| 1506 |
else if (startswith (p, "V2_CNAM")) |
| 1507 |
return darwin_sections[objc2_class_names_section]; |
| 1508 |
else if (startswith (p, "V2_MNAM")) |
| 1509 |
return darwin_sections[objc2_method_names_section]; |
| 1510 |
else if (startswith (p, "V2_MTYP")) |
| 1511 |
return darwin_sections[objc2_method_types_section]; |
| 1512 |
else if (startswith (p, "V2_STRG")) |
| 1513 |
return darwin_sections[cstring_section]; |
| 1514 |
|
| 1515 |
else if (startswith (p, "G2_META") || startswith (p, "G2_CLAS")) |
| 1516 |
return darwin_sections[objc2_classdefs_section]; |
| 1517 |
else if (startswith (p, "V2_PCOL")) |
| 1518 |
return ld_uses_coal_sects ? darwin_sections[data_coal_section] |
| 1519 |
: darwin_sections[objc2_data_section]; |
| 1520 |
else if (startswith (p, "V2_MREF")) |
| 1521 |
return darwin_sections[objc2_message_refs_section]; |
| 1522 |
else if (startswith (p, "V2_CLRF")) |
| 1523 |
return darwin_sections[objc2_classrefs_section]; |
| 1524 |
else if (startswith (p, "V2_SURF")) |
| 1525 |
return darwin_sections[objc2_super_classrefs_section]; |
| 1526 |
else if (startswith (p, "V2_NLCL")) |
| 1527 |
return darwin_sections[objc2_nonlazy_class_section]; |
| 1528 |
else if (startswith (p, "V2_CLAB")) |
| 1529 |
{ |
| 1530 |
classes_seen = 1; |
| 1531 |
return darwin_sections[objc2_classlist_section]; |
| 1532 |
} |
| 1533 |
else if (startswith (p, "V2_SRFS")) |
| 1534 |
return darwin_sections[objc2_selector_refs_section]; |
| 1535 |
else if (startswith (p, "V2_NLCA")) |
| 1536 |
return darwin_sections[objc2_nonlazy_category_section]; |
| 1537 |
else if (startswith (p, "V2_CALA")) |
| 1538 |
return darwin_sections[objc2_categorylist_section]; |
| 1539 |
|
| 1540 |
else if (startswith (p, "V2_PLST")) |
| 1541 |
return darwin_sections[objc2_protocollist_section]; |
| 1542 |
else if (startswith (p, "V2_PRFS")) |
| 1543 |
return darwin_sections[objc2_protocolrefs_section]; |
| 1544 |
|
| 1545 |
else if (startswith (p, "V2_INFO")) |
| 1546 |
return darwin_sections[objc2_image_info_section]; |
| 1547 |
|
| 1548 |
else if (startswith (p, "V2_EHTY")) |
| 1549 |
return ld_uses_coal_sects ? darwin_sections[data_coal_section] |
| 1550 |
: data_section; |
| 1551 |
|
| 1552 |
else if (startswith (p, "V2_CSTR")) |
| 1553 |
return darwin_sections[objc2_constant_string_object_section]; |
| 1554 |
|
| 1555 |
else if (startswith (p, "V2_IVRF")) |
| 1556 |
return darwin_sections[objc2_ivar_section]; |
| 1557 |
|
| 1558 |
/* Not recognized, default. */ |
| 1559 |
return base; |
| 1560 |
} |
| 1561 |
|
| 1562 |
/* Return the section required for Objective C ABI 0/1 metadata. */ |
| 1563 |
static section * |
| 1564 |
darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base) |
| 1565 |
{ |
| 1566 |
const char *p; |
| 1567 |
tree ident = TREE_VALUE (meta); |
| 1568 |
gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE); |
| 1569 |
p = IDENTIFIER_POINTER (ident); |
| 1570 |
|
| 1571 |
gcc_checking_assert (flag_next_runtime >= 1 && flag_objc_abi < 2); |
| 1572 |
|
| 1573 |
objc_metadata_seen = 1; |
| 1574 |
|
| 1575 |
/* String sections first, cos there are lots of strings. */ |
| 1576 |
if (startswith (p, "V1_STRG")) |
| 1577 |
return darwin_sections[cstring_section]; |
| 1578 |
else if (startswith (p, "V1_CLSN")) |
| 1579 |
return darwin_sections[objc_class_names_section]; |
| 1580 |
else if (startswith (p, "V1_METN")) |
| 1581 |
return darwin_sections[objc_meth_var_names_section]; |
| 1582 |
else if (startswith (p, "V1_METT")) |
| 1583 |
return darwin_sections[objc_meth_var_types_section]; |
| 1584 |
|
| 1585 |
else if (startswith (p, "V1_CLAS")) |
| 1586 |
{ |
| 1587 |
classes_seen = 1; |
| 1588 |
return darwin_sections[objc_class_section]; |
| 1589 |
} |
| 1590 |
else if (startswith (p, "V1_META")) |
| 1591 |
return darwin_sections[objc_meta_class_section]; |
| 1592 |
else if (startswith (p, "V1_CATG")) |
| 1593 |
return darwin_sections[objc_category_section]; |
| 1594 |
else if (startswith (p, "V1_PROT")) |
| 1595 |
return darwin_sections[objc_protocol_section]; |
| 1596 |
|
| 1597 |
else if (startswith (p, "V1_CLCV")) |
| 1598 |
return darwin_sections[objc_class_vars_section]; |
| 1599 |
else if (startswith (p, "V1_CLIV")) |
| 1600 |
return darwin_sections[objc_instance_vars_section]; |
| 1601 |
|
| 1602 |
else if (startswith (p, "V1_CLCM")) |
| 1603 |
return darwin_sections[objc_cls_meth_section]; |
| 1604 |
else if (startswith (p, "V1_CLIM")) |
| 1605 |
return darwin_sections[objc_inst_meth_section]; |
| 1606 |
else if (startswith (p, "V1_CACM")) |
| 1607 |
return darwin_sections[objc_cat_cls_meth_section]; |
| 1608 |
else if (startswith (p, "V1_CAIM")) |
| 1609 |
return darwin_sections[objc_cat_inst_meth_section]; |
| 1610 |
else if (startswith (p, "V1_PNSM")) |
| 1611 |
return darwin_sections[objc_cat_inst_meth_section]; |
| 1612 |
else if (startswith (p, "V1_PCLM")) |
| 1613 |
return darwin_sections[objc_cat_cls_meth_section]; |
| 1614 |
|
| 1615 |
else if (startswith (p, "V1_CLPR")) |
| 1616 |
return darwin_sections[objc_cat_cls_meth_section]; |
| 1617 |
else if (startswith (p, "V1_CAPR")) |
| 1618 |
return darwin_sections[objc_category_section]; /* ??? CHECK me. */ |
| 1619 |
|
| 1620 |
else if (startswith (p, "V1_PRFS")) |
| 1621 |
return darwin_sections[objc_cat_cls_meth_section]; |
| 1622 |
else if (startswith (p, "V1_CLRF")) |
| 1623 |
return darwin_sections[objc_cls_refs_section]; |
| 1624 |
else if (startswith (p, "V1_SRFS")) |
| 1625 |
return darwin_sections[objc_selector_refs_section]; |
| 1626 |
|
| 1627 |
else if (startswith (p, "V1_MODU")) |
| 1628 |
return darwin_sections[objc_module_info_section]; |
| 1629 |
else if (startswith (p, "V1_SYMT")) |
| 1630 |
return darwin_sections[objc_symbols_section]; |
| 1631 |
else if (startswith (p, "V1_INFO")) |
| 1632 |
return darwin_sections[objc_image_info_section]; |
| 1633 |
|
| 1634 |
else if (startswith (p, "V1_PLST")) |
| 1635 |
return darwin_sections[objc1_prop_list_section]; |
| 1636 |
else if (startswith (p, "V1_PEXT")) |
| 1637 |
return darwin_sections[objc1_protocol_ext_section]; |
| 1638 |
else if (startswith (p, "V1_CEXT")) |
| 1639 |
return darwin_sections[objc1_class_ext_section]; |
| 1640 |
|
| 1641 |
else if (startswith (p, "V1_CSTR")) |
| 1642 |
return darwin_sections[objc_constant_string_object_section]; |
| 1643 |
|
| 1644 |
return base; |
| 1645 |
} |
| 1646 |
|
| 1647 |
section * |
| 1648 |
machopic_select_section (tree decl, |
| 1649 |
int reloc, |
| 1650 |
unsigned HOST_WIDE_INT align) |
| 1651 |
{ |
| 1652 |
bool zsize, one, weak, use_coal, ro; |
| 1653 |
section *base_section = NULL; |
| 1654 |
|
| 1655 |
weak = (DECL_P (decl) |
| 1656 |
&& DECL_WEAK (decl) |
| 1657 |
&& !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl))); |
| 1658 |
|
| 1659 |
/* Darwin pads zero-sized objects with at least one byte, so that the ld64 |
| 1660 |
atom model is preserved (objects must have distinct regions starting with |
| 1661 |
a unique linker-visible symbol). |
| 1662 |
In order to support section anchors, we need to move objects with zero |
| 1663 |
size into sections which are marked as "no section anchors"; the padded |
| 1664 |
objects, obviously, have real sizes that differ from their DECL sizes. */ |
| 1665 |
zsize = DARWIN_SECTION_ANCHORS && flag_section_anchors; |
| 1666 |
|
| 1667 |
/* In the streaming of LTO symbol data, we might have a situation where the |
| 1668 |
var is incomplete or layout not finished (DECL_SIZE_UNIT is NULL_TREE). |
| 1669 |
We cannot tell if it is zero-sized then, but we can get the section |
| 1670 |
category correct so that nm reports the right kind of section |
| 1671 |
(e.g. BSS c.f. data). */ |
| 1672 |
zsize = (zsize |
| 1673 |
&& DECL_P (decl) |
| 1674 |
&& (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL) |
| 1675 |
&& DECL_SIZE_UNIT (decl) |
| 1676 |
&& tree_to_uhwi (DECL_SIZE_UNIT (decl)) == 0); |
| 1677 |
|
| 1678 |
one = DECL_P (decl) |
| 1679 |
&& TREE_CODE (decl) == VAR_DECL |
| 1680 |
&& DECL_COMDAT_GROUP (decl); |
| 1681 |
|
| 1682 |
use_coal = (weak || one) && ld_uses_coal_sects; |
| 1683 |
|
| 1684 |
ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ; |
| 1685 |
|
| 1686 |
switch (categorize_decl_for_section (decl, reloc)) |
| 1687 |
{ |
| 1688 |
case SECCAT_TEXT: |
| 1689 |
gcc_unreachable (); |
| 1690 |
break; |
| 1691 |
|
| 1692 |
case SECCAT_RODATA: |
| 1693 |
case SECCAT_SRODATA: |
| 1694 |
base_section = darwin_rodata_section (use_coal, zsize, reloc); |
| 1695 |
break; |
| 1696 |
|
| 1697 |
case SECCAT_RODATA_MERGE_STR: |
| 1698 |
base_section = darwin_mergeable_string_section (decl, align); |
| 1699 |
break; |
| 1700 |
|
| 1701 |
case SECCAT_RODATA_MERGE_STR_INIT: |
| 1702 |
base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align); |
| 1703 |
break; |
| 1704 |
|
| 1705 |
case SECCAT_RODATA_MERGE_CONST: |
| 1706 |
base_section = darwin_mergeable_constant_section (decl, align, zsize); |
| 1707 |
break; |
| 1708 |
|
| 1709 |
case SECCAT_DATA: |
| 1710 |
case SECCAT_DATA_REL: |
| 1711 |
case SECCAT_DATA_REL_LOCAL: |
| 1712 |
case SECCAT_DATA_REL_RO: |
| 1713 |
case SECCAT_DATA_REL_RO_LOCAL: |
| 1714 |
case SECCAT_SDATA: |
| 1715 |
case SECCAT_TDATA: |
| 1716 |
if (use_coal) |
| 1717 |
{ |
| 1718 |
if (ro) |
| 1719 |
base_section = darwin_sections[const_data_coal_section]; |
| 1720 |
else |
| 1721 |
base_section = darwin_sections[data_coal_section]; |
| 1722 |
} |
| 1723 |
else if (zsize) |
| 1724 |
{ |
| 1725 |
/* If we're doing section anchors, then punt zero-sized objects into |
| 1726 |
their own sections so that they don't interfere with offset |
| 1727 |
computation for the remaining vars. */ |
| 1728 |
if (ro) |
| 1729 |
base_section = darwin_sections[zobj_const_data_section]; |
| 1730 |
else |
| 1731 |
base_section = darwin_sections[zobj_data_section]; |
| 1732 |
} |
| 1733 |
else if (ro) |
| 1734 |
{ |
| 1735 |
if (VAR_P (decl) && TREE_TYPE (decl) |
| 1736 |
&& TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE |
| 1737 |
&& DECL_NAME (decl) |
| 1738 |
&& strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), |
| 1739 |
"__anon_cfstring", 15) == 0) |
| 1740 |
base_section = darwin_sections[cfstring_constant_object_section]; |
| 1741 |
else |
| 1742 |
base_section = darwin_sections[const_data_section]; |
| 1743 |
} |
| 1744 |
else |
| 1745 |
base_section = data_section; |
| 1746 |
break; |
| 1747 |
case SECCAT_BSS: |
| 1748 |
case SECCAT_SBSS: |
| 1749 |
case SECCAT_TBSS: |
| 1750 |
if (use_coal) |
| 1751 |
base_section = darwin_sections[data_coal_section]; |
| 1752 |
else |
| 1753 |
{ |
| 1754 |
if (!TREE_PUBLIC (decl)) |
| 1755 |
base_section = lcomm_section; |
| 1756 |
else if (bss_noswitch_section) |
| 1757 |
base_section = bss_noswitch_section; |
| 1758 |
else |
| 1759 |
base_section = data_section; |
| 1760 |
} |
| 1761 |
break; |
| 1762 |
|
| 1763 |
default: |
| 1764 |
gcc_unreachable (); |
| 1765 |
} |
| 1766 |
|
| 1767 |
/* Darwin weird special cases. |
| 1768 |
a) OBJC Meta-data. */ |
| 1769 |
if (DECL_P (decl) |
| 1770 |
&& (TREE_CODE (decl) == VAR_DECL |
| 1771 |
|| TREE_CODE (decl) == CONST_DECL) |
| 1772 |
&& DECL_ATTRIBUTES (decl)) |
| 1773 |
{ |
| 1774 |
tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl)); |
| 1775 |
if (meta) |
| 1776 |
return darwin_objc2_section (decl, meta, base_section); |
| 1777 |
meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl)); |
| 1778 |
if (meta) |
| 1779 |
return darwin_objc1_section (decl, meta, base_section); |
| 1780 |
meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl)); |
| 1781 |
if (meta) |
| 1782 |
return base_section; /* GNU runtime is happy with it all in one pot. */ |
| 1783 |
} |
| 1784 |
|
| 1785 |
/* b) Constructors for constant NSstring [but not CFString] objects. */ |
| 1786 |
if (TREE_CODE (decl) == CONSTRUCTOR |
| 1787 |
&& TREE_TYPE (decl) |
| 1788 |
&& TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE |
| 1789 |
&& TYPE_NAME (TREE_TYPE (decl))) |
| 1790 |
{ |
| 1791 |
tree name = TYPE_NAME (TREE_TYPE (decl)); |
| 1792 |
if (TREE_CODE (name) == TYPE_DECL) |
| 1793 |
name = DECL_NAME (name); |
| 1794 |
|
| 1795 |
if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString")) |
| 1796 |
{ |
| 1797 |
if (flag_next_runtime) |
| 1798 |
{ |
| 1799 |
if (flag_objc_abi == 2) |
| 1800 |
return darwin_sections[objc2_constant_string_object_section]; |
| 1801 |
else |
| 1802 |
return darwin_sections[objc_constant_string_object_section]; |
| 1803 |
} |
| 1804 |
else |
| 1805 |
return darwin_sections[objc_string_object_section]; |
| 1806 |
} |
| 1807 |
else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString")) |
| 1808 |
{ |
| 1809 |
/* We should have handled __anon_cfstrings above. */ |
| 1810 |
gcc_checking_assert (0); |
| 1811 |
return darwin_sections[cfstring_constant_object_section]; |
| 1812 |
} |
| 1813 |
else |
| 1814 |
return base_section; |
| 1815 |
} |
| 1816 |
else if (flag_next_runtime |
| 1817 |
&& VAR_P (decl) |
| 1818 |
&& DECL_NAME (decl) |
| 1819 |
&& TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE |
| 1820 |
&& IDENTIFIER_POINTER (DECL_NAME (decl)) |
| 1821 |
&& startswith (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_")) |
| 1822 |
/* c) legacy meta-data selection was deprecated at 4.6, removed now. */ |
| 1823 |
gcc_unreachable (); |
| 1824 |
|
| 1825 |
return base_section; |
| 1826 |
} |
| 1827 |
|
| 1828 |
/* This can be called with address expressions as "rtx". |
| 1829 |
They must go in "const". */ |
| 1830 |
|
| 1831 |
section * |
| 1832 |
machopic_select_rtx_section (machine_mode mode, rtx x, |
| 1833 |
unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED) |
| 1834 |
{ |
| 1835 |
if (known_eq (GET_MODE_SIZE (mode), 8) |
| 1836 |
&& (GET_CODE (x) == CONST_INT |
| 1837 |
|| GET_CODE (x) == CONST_WIDE_INT |
| 1838 |
|| GET_CODE (x) == CONST_DOUBLE)) |
| 1839 |
return darwin_sections[literal8_section]; |
| 1840 |
else if (known_eq (GET_MODE_SIZE (mode), 4) |
| 1841 |
&& (GET_CODE (x) == CONST_INT |
| 1842 |
|| GET_CODE (x) == CONST_WIDE_INT |
| 1843 |
|| GET_CODE (x) == CONST_DOUBLE)) |
| 1844 |
return darwin_sections[literal4_section]; |
| 1845 |
else if (HAVE_GAS_LITERAL16 |
| 1846 |
&& TARGET_64BIT |
| 1847 |
&& known_eq (GET_MODE_SIZE (mode), 16) |
| 1848 |
&& (GET_CODE (x) == CONST_INT |
| 1849 |
|| GET_CODE (x) == CONST_WIDE_INT |
| 1850 |
|| GET_CODE (x) == CONST_DOUBLE |
| 1851 |
|| GET_CODE (x) == CONST_VECTOR)) |
| 1852 |
return darwin_sections[literal16_section]; |
| 1853 |
else if (MACHOPIC_INDIRECT |
| 1854 |
&& (GET_CODE (x) == SYMBOL_REF |
| 1855 |
|| GET_CODE (x) == CONST |
| 1856 |
|| GET_CODE (x) == LABEL_REF)) |
| 1857 |
return darwin_sections[const_data_section]; |
| 1858 |
else |
| 1859 |
return darwin_sections[const_section]; |
| 1860 |
} |
| 1861 |
|
| 1862 |
void |
| 1863 |
machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED) |
| 1864 |
{ |
| 1865 |
cdtor_record new_elt = {symbol, priority, vec_safe_length (ctors)}; |
| 1866 |
|
| 1867 |
vec_safe_push (ctors, new_elt); |
| 1868 |
|
| 1869 |
if (! MACHOPIC_INDIRECT) |
| 1870 |
fprintf (asm_out_file, ".reference .constructors_used\n"); |
| 1871 |
} |
| 1872 |
|
| 1873 |
void |
| 1874 |
machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED) |
| 1875 |
{ |
| 1876 |
cdtor_record new_elt = {symbol, priority, vec_safe_length (dtors)}; |
| 1877 |
|
| 1878 |
vec_safe_push (dtors, new_elt); |
| 1879 |
|
| 1880 |
if (! MACHOPIC_INDIRECT) |
| 1881 |
fprintf (asm_out_file, ".reference .destructors_used\n"); |
| 1882 |
} |
| 1883 |
|
| 1884 |
static int |
| 1885 |
sort_cdtor_records (const void * a, const void * b) |
| 1886 |
{ |
| 1887 |
const cdtor_record *cda = (const cdtor_record *)a; |
| 1888 |
const cdtor_record *cdb = (const cdtor_record *)b; |
| 1889 |
if (cda->priority > cdb->priority) |
| 1890 |
return 1; |
| 1891 |
if (cda->priority < cdb->priority) |
| 1892 |
return -1; |
| 1893 |
if (cda->position > cdb->position) |
| 1894 |
return 1; |
| 1895 |
if (cda->position < cdb->position) |
| 1896 |
return -1; |
| 1897 |
return 0; |
| 1898 |
} |
| 1899 |
|
| 1900 |
static void |
| 1901 |
finalize_ctors () |
| 1902 |
{ |
| 1903 |
unsigned int i; |
| 1904 |
cdtor_record *elt; |
| 1905 |
|
| 1906 |
if (MACHOPIC_INDIRECT) |
| 1907 |
switch_to_section (darwin_sections[mod_init_section]); |
| 1908 |
else |
| 1909 |
switch_to_section (darwin_sections[constructor_section]); |
| 1910 |
|
| 1911 |
/* Where needed, provide a linker-visible section-start symbol so that we |
| 1912 |
have stable output between debug and non-debug. */ |
| 1913 |
if (ld_init_term_start_labels) |
| 1914 |
fputs (MACHOPIC_INDIRECT ? "_Mod.init:\n" : "_CTOR.sect:\n", asm_out_file); |
| 1915 |
|
| 1916 |
if (vec_safe_length (ctors) > 1) |
| 1917 |
ctors->qsort (sort_cdtor_records); |
| 1918 |
FOR_EACH_VEC_SAFE_ELT (ctors, i, elt) |
| 1919 |
{ |
| 1920 |
assemble_align (POINTER_SIZE); |
| 1921 |
assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1); |
| 1922 |
} |
| 1923 |
} |
| 1924 |
|
| 1925 |
static void |
| 1926 |
finalize_dtors () |
| 1927 |
{ |
| 1928 |
unsigned int i; |
| 1929 |
cdtor_record *elt; |
| 1930 |
|
| 1931 |
if (MACHOPIC_INDIRECT) |
| 1932 |
switch_to_section (darwin_sections[mod_term_section]); |
| 1933 |
else |
| 1934 |
switch_to_section (darwin_sections[destructor_section]); |
| 1935 |
|
| 1936 |
/* Where needed, provide a linker-visible section-start symbol so that we |
| 1937 |
have stable output between debug and non-debug. */ |
| 1938 |
if (ld_init_term_start_labels) |
| 1939 |
fputs (MACHOPIC_INDIRECT ? "_Mod.term:\n" : "_DTOR.sect:\n", asm_out_file); |
| 1940 |
|
| 1941 |
if (vec_safe_length (dtors) > 1) |
| 1942 |
dtors->qsort (sort_cdtor_records); |
| 1943 |
FOR_EACH_VEC_SAFE_ELT (dtors, i, elt) |
| 1944 |
{ |
| 1945 |
assemble_align (POINTER_SIZE); |
| 1946 |
assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1); |
| 1947 |
} |
| 1948 |
} |
| 1949 |
|
| 1950 |
void |
| 1951 |
darwin_globalize_label (FILE *stream, const char *name) |
| 1952 |
{ |
| 1953 |
if (!startswith (name, "_OBJC_")) |
| 1954 |
default_globalize_label (stream, name); |
| 1955 |
/* We have some Objective C cases that need to be global, but only on newer |
| 1956 |
OS versions. */ |
| 1957 |
if (flag_objc_abi < 2 || flag_next_runtime < 100700) |
| 1958 |
return; |
| 1959 |
if (startswith (name+6, "LabelPro")) |
| 1960 |
default_globalize_label (stream, name); |
| 1961 |
if (startswith (name+6, "Protocol_")) |
| 1962 |
default_globalize_label (stream, name); |
| 1963 |
} |
| 1964 |
|
| 1965 |
/* This routine returns non-zero if 'name' starts with the special objective-c |
| 1966 |
anonymous file-scope static name. It accommodates c++'s mangling of such |
| 1967 |
symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */ |
| 1968 |
|
| 1969 |
int |
| 1970 |
darwin_label_is_anonymous_local_objc_name (const char *name) |
| 1971 |
{ |
| 1972 |
const unsigned char *p = (const unsigned char *) name; |
| 1973 |
if (*p != '_') |
| 1974 |
return 0; |
| 1975 |
if (p[1] == 'Z' && p[2] == 'L') |
| 1976 |
{ |
| 1977 |
p += 3; |
| 1978 |
while (*p >= '0' && *p <= '9') |
| 1979 |
p++; |
| 1980 |
} |
| 1981 |
if (!startswith ((const char *)p, "_OBJC_")) |
| 1982 |
return false; |
| 1983 |
|
| 1984 |
/* We need some of the objective c meta-data symbols to be visible to the |
| 1985 |
linker (when the target OS version is newer). FIXME: this is horrible, |
| 1986 |
we need a better mechanism. */ |
| 1987 |
|
| 1988 |
if (flag_objc_abi < 2 || flag_next_runtime < 100700) |
| 1989 |
return true; |
| 1990 |
|
| 1991 |
p += 6; |
| 1992 |
if (startswith ((const char *)p, "ClassRef")) |
| 1993 |
return false; |
| 1994 |
else if (startswith ((const char *)p, "SelRef")) |
| 1995 |
return false; |
| 1996 |
else if (startswith ((const char *)p, "Category")) |
| 1997 |
{ |
| 1998 |
if (p[8] == '_' || p[8] == 'I' || p[8] == 'P' || p[8] == 'C' ) |
| 1999 |
return false; |
| 2000 |
return true; |
| 2001 |
} |
| 2002 |
else if (startswith ((const char *)p, "ClassMethods")) |
| 2003 |
return false; |
| 2004 |
else if (startswith ((const char *)p, "ClassProtocols")) |
| 2005 |
return false; |
| 2006 |
else if (startswith ((const char *)p, "Instance")) |
| 2007 |
{ |
| 2008 |
if (p[8] == 'I' || p[8] == 'M') |
| 2009 |
return false; |
| 2010 |
return true; |
| 2011 |
} |
| 2012 |
else if (startswith ((const char *)p, "CLASS_RO")) |
| 2013 |
return false; |
| 2014 |
else if (startswith ((const char *)p, "METACLASS_RO")) |
| 2015 |
return false; |
| 2016 |
else if (startswith ((const char *)p, "Protocol")) |
| 2017 |
{ |
| 2018 |
if (p[8] == '_' || p[8] == 'I' || p[8] == 'P' |
| 2019 |
|| p[8] == 'M' || p[8] == 'C' || p[8] == 'O') |
| 2020 |
return false; |
| 2021 |
return true; |
| 2022 |
} |
| 2023 |
else if (startswith ((const char *)p, "LabelPro")) |
| 2024 |
return false; |
| 2025 |
return true; |
| 2026 |
} |
| 2027 |
|
| 2028 |
/* LTO support for Mach-O. |
| 2029 |
|
| 2030 |
This version uses three mach-o sections to encapsulate the (unlimited |
| 2031 |
number of) lto sections. |
| 2032 |
|
| 2033 |
__GNU_LTO, __lto_sections contains the concatented GNU LTO section data. |
| 2034 |
__GNU_LTO, __section_names contains the GNU LTO section names. |
| 2035 |
__GNU_LTO, __section_index contains an array of values that index these. |
| 2036 |
|
| 2037 |
Indexed thus: |
| 2038 |
<section offset from the start of __GNU_LTO, __lto_sections>, |
| 2039 |
<section length> |
| 2040 |
<name offset from the start of __GNU_LTO, __section_names, |
| 2041 |
<name length>. |
| 2042 |
|
| 2043 |
At present, for both m32 and m64 mach-o files each of these fields is |
| 2044 |
represented by a uint32_t. This is because, AFAICT, a mach-o object |
| 2045 |
cannot exceed 4Gb because the section_64 offset field (see below) is 32bits. |
| 2046 |
|
| 2047 |
uint32_t offset; |
| 2048 |
"offset An integer specifying the offset to this section in the file." */ |
| 2049 |
|
| 2050 |
/* Count lto section numbers. */ |
| 2051 |
static unsigned int lto_section_num = 0; |
| 2052 |
|
| 2053 |
/* A vector of information about LTO sections, at present, we only have |
| 2054 |
the name. TODO: see if we can get the data length somehow. */ |
| 2055 |
typedef struct GTY (()) darwin_lto_section_e { |
| 2056 |
const char *sectname; |
| 2057 |
} darwin_lto_section_e ; |
| 2058 |
|
| 2059 |
static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names; |
| 2060 |
|
| 2061 |
/* Section wrapper scheme (used here to wrap the unlimited number of LTO |
| 2062 |
sections into three Mach-O ones). |
| 2063 |
NOTE: These names MUST be kept in sync with those in |
| 2064 |
libiberty/simple-object-mach-o. */ |
| 2065 |
#define LTO_SECTS_SECTION "__wrapper_sects" |
| 2066 |
#define LTO_NAMES_SECTION "__wrapper_names" |
| 2067 |
#define LTO_INDEX_SECTION "__wrapper_index" |
| 2068 |
|
| 2069 |
/* File to temporarily store LTO data. This is appended to asm_out_file |
| 2070 |
in darwin_end_file. */ |
| 2071 |
static FILE *lto_asm_out_file, *saved_asm_out_file; |
| 2072 |
static char *lto_asm_out_name; |
| 2073 |
static enum debug_info_levels saved_debug_info_level; |
| 2074 |
|
| 2075 |
/* Prepare asm_out_file for LTO output. For darwin, this means hiding |
| 2076 |
asm_out_file and switching to an alternative output file. */ |
| 2077 |
void |
| 2078 |
darwin_asm_lto_start (void) |
| 2079 |
{ |
| 2080 |
gcc_assert (! saved_asm_out_file); |
| 2081 |
saved_asm_out_file = asm_out_file; |
| 2082 |
saved_debug_info_level = debug_info_level; |
| 2083 |
debug_info_level = DINFO_LEVEL_NONE; |
| 2084 |
if (! lto_asm_out_name) |
| 2085 |
lto_asm_out_name = make_temp_file (".lto.s"); |
| 2086 |
lto_asm_out_file = fopen (lto_asm_out_name, "a"); |
| 2087 |
if (lto_asm_out_file == NULL) |
| 2088 |
fatal_error (input_location, |
| 2089 |
"failed to open temporary file %s for LTO output", |
| 2090 |
lto_asm_out_name); |
| 2091 |
asm_out_file = lto_asm_out_file; |
| 2092 |
} |
| 2093 |
|
| 2094 |
/* Restore asm_out_file. */ |
| 2095 |
void |
| 2096 |
darwin_asm_lto_end (void) |
| 2097 |
{ |
| 2098 |
gcc_assert (saved_asm_out_file); |
| 2099 |
fclose (lto_asm_out_file); |
| 2100 |
asm_out_file = saved_asm_out_file; |
| 2101 |
saved_asm_out_file = NULL; |
| 2102 |
debug_info_level = saved_debug_info_level; |
| 2103 |
} |
| 2104 |
|
| 2105 |
static void |
| 2106 |
darwin_asm_dwarf_section (const char *name, unsigned int flags, |
| 2107 |
tree decl, bool is_for_lto); |
| 2108 |
|
| 2109 |
/* Called for the TARGET_ASM_NAMED_SECTION hook. */ |
| 2110 |
|
| 2111 |
void |
| 2112 |
darwin_asm_named_section (const char *name, |
| 2113 |
unsigned int flags, |
| 2114 |
tree decl ATTRIBUTE_UNUSED) |
| 2115 |
{ |
| 2116 |
/* LTO sections go in a special section that encapsulates the (unlimited) |
| 2117 |
number of GNU LTO sections within a single mach-o one. */ |
| 2118 |
if (startswith (name, LTO_SECTION_NAME_PREFIX)) |
| 2119 |
{ |
| 2120 |
darwin_lto_section_e e; |
| 2121 |
/* We expect certain flags to be set... */ |
| 2122 |
gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED)) |
| 2123 |
== (SECTION_DEBUG | SECTION_NAMED)); |
| 2124 |
|
| 2125 |
/* Switch to our combined section. */ |
| 2126 |
fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n", |
| 2127 |
LTO_SEGMENT_NAME, LTO_SECTS_SECTION); |
| 2128 |
/* Output a label for the start of this sub-section. */ |
| 2129 |
fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n", |
| 2130 |
lto_section_num, name); |
| 2131 |
/* We have to jump through hoops to get the values of the intra-section |
| 2132 |
offsets... */ |
| 2133 |
fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n", |
| 2134 |
lto_section_num, lto_section_num); |
| 2135 |
fprintf (asm_out_file, |
| 2136 |
"\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n", |
| 2137 |
lto_section_num, lto_section_num+1, lto_section_num); |
| 2138 |
lto_section_num++; |
| 2139 |
e.sectname = xstrdup (name); |
| 2140 |
/* Keep the names, we'll need to make a table later. |
| 2141 |
TODO: check that we do not revisit sections, that would break |
| 2142 |
the assumption of how this is done. */ |
| 2143 |
if (lto_section_names == NULL) |
| 2144 |
vec_alloc (lto_section_names, 16); |
| 2145 |
vec_safe_push (lto_section_names, e); |
| 2146 |
} |
| 2147 |
else if (startswith (name, "__DWARF,")) |
| 2148 |
darwin_asm_dwarf_section (name, flags, decl, false); |
| 2149 |
else if (startswith (name, "__GNU_DWARF_LTO,")) |
| 2150 |
darwin_asm_dwarf_section (name, flags, decl, true); |
| 2151 |
else |
| 2152 |
fprintf (asm_out_file, "\t.section %s\n", name); |
| 2153 |
} |
| 2154 |
|
| 2155 |
void |
| 2156 |
darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED) |
| 2157 |
{ |
| 2158 |
/* Darwin does not use unique sections. */ |
| 2159 |
} |
| 2160 |
|
| 2161 |
/* Handle __attribute__ ((apple_kext_compatibility)). |
| 2162 |
This only applies to darwin kexts for 2.95 compatibility -- it shrinks the |
| 2163 |
vtable for classes with this attribute (and their descendants) by not |
| 2164 |
outputting the new 3.0 nondeleting destructor. This means that such |
| 2165 |
objects CANNOT be allocated on the stack or as globals UNLESS they have |
| 2166 |
a completely empty `operator delete'. |
| 2167 |
Luckily, this fits in with the Darwin kext model. |
| 2168 |
|
| 2169 |
This attribute also disables gcc3's potential overlaying of derived |
| 2170 |
class data members on the padding at the end of the base class. */ |
| 2171 |
|
| 2172 |
tree |
| 2173 |
darwin_handle_kext_attribute (tree *node, tree name, |
| 2174 |
tree args ATTRIBUTE_UNUSED, |
| 2175 |
int flags ATTRIBUTE_UNUSED, |
| 2176 |
bool *no_add_attrs) |
| 2177 |
{ |
| 2178 |
/* APPLE KEXT stuff -- only applies with pure static C++ code. */ |
| 2179 |
if (! TARGET_KEXTABI) |
| 2180 |
{ |
| 2181 |
warning (0, "%qE 2.95 vtable-compatibility attribute applies " |
| 2182 |
"only when compiling a kext", name); |
| 2183 |
|
| 2184 |
*no_add_attrs = true; |
| 2185 |
} |
| 2186 |
else if (TREE_CODE (*node) != RECORD_TYPE) |
| 2187 |
{ |
| 2188 |
warning (0, "%qE 2.95 vtable-compatibility attribute applies " |
| 2189 |
"only to C++ classes", name); |
| 2190 |
|
| 2191 |
*no_add_attrs = true; |
| 2192 |
} |
| 2193 |
|
| 2194 |
return NULL_TREE; |
| 2195 |
} |
| 2196 |
|
| 2197 |
/* Handle a "weak_import" attribute; arguments as in |
| 2198 |
struct attribute_spec.handler. */ |
| 2199 |
|
| 2200 |
tree |
| 2201 |
darwin_handle_weak_import_attribute (tree *node, tree name, |
| 2202 |
tree ARG_UNUSED (args), |
| 2203 |
int ARG_UNUSED (flags), |
| 2204 |
bool * no_add_attrs) |
| 2205 |
{ |
| 2206 |
if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL) |
| 2207 |
{ |
| 2208 |
warning (OPT_Wattributes, "%qE attribute ignored", |
| 2209 |
name); |
| 2210 |
*no_add_attrs = true; |
| 2211 |
} |
| 2212 |
else |
| 2213 |
declare_weak (*node); |
| 2214 |
|
| 2215 |
return NULL_TREE; |
| 2216 |
} |
| 2217 |
|
| 2218 |
/* Emit a label for an FDE, making it global and/or weak if appropriate. |
| 2219 |
The third parameter is nonzero if this is for exception handling. |
| 2220 |
The fourth parameter is nonzero if this is just a placeholder for an |
| 2221 |
FDE that we are omitting. */ |
| 2222 |
|
| 2223 |
void |
| 2224 |
darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty) |
| 2225 |
{ |
| 2226 |
char *lab ; |
| 2227 |
char buf[32]; |
| 2228 |
static int invok_count = 0; |
| 2229 |
static tree last_fun_decl = NULL_TREE; |
| 2230 |
|
| 2231 |
/* Modern linkers can produce distinct FDEs without compiler support. */ |
| 2232 |
if (! for_eh || ! ld_needs_eh_markers) |
| 2233 |
return; |
| 2234 |
|
| 2235 |
/* FIXME: This only works when the eh for all sections of a function are |
| 2236 |
emitted at the same time. If that changes, we would need to use a lookup |
| 2237 |
table of some form to determine what to do. Also, we should emit the |
| 2238 |
unadorned label for the partition containing the public label for a |
| 2239 |
function. This is of limited use, probably, since we do not currently |
| 2240 |
enable partitioning. */ |
| 2241 |
strcpy (buf, ".eh"); |
| 2242 |
if (decl && TREE_CODE (decl) == FUNCTION_DECL) |
| 2243 |
{ |
| 2244 |
if (decl == last_fun_decl) |
| 2245 |
{ |
| 2246 |
invok_count++; |
| 2247 |
snprintf (buf, 31, "$$part$$%d.eh", invok_count); |
| 2248 |
} |
| 2249 |
else |
| 2250 |
{ |
| 2251 |
last_fun_decl = decl; |
| 2252 |
invok_count = 0; |
| 2253 |
} |
| 2254 |
} |
| 2255 |
|
| 2256 |
lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL); |
| 2257 |
|
| 2258 |
if (TREE_PUBLIC (decl)) |
| 2259 |
{ |
| 2260 |
targetm.asm_out.globalize_label (file, lab); |
| 2261 |
if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN) |
| 2262 |
{ |
| 2263 |
fputs ("\t.private_extern ", file); |
| 2264 |
assemble_name (file, lab); |
| 2265 |
fputc ('\n', file); |
| 2266 |
} |
| 2267 |
} |
| 2268 |
|
| 2269 |
if (DECL_WEAK (decl)) |
| 2270 |
{ |
| 2271 |
fputs ("\t.weak_definition ", file); |
| 2272 |
assemble_name (file, lab); |
| 2273 |
fputc ('\n', file); |
| 2274 |
} |
| 2275 |
|
| 2276 |
assemble_name (file, lab); |
| 2277 |
if (empty) |
| 2278 |
{ |
| 2279 |
fputs (" = 0\n", file); |
| 2280 |
|
| 2281 |
/* Mark the absolute .eh and .eh1 style labels as needed to |
| 2282 |
ensure that we don't dead code strip them and keep such |
| 2283 |
labels from another instantiation point until we can fix this |
| 2284 |
properly with group comdat support. */ |
| 2285 |
darwin_mark_decl_preserved (lab); |
| 2286 |
} |
| 2287 |
else |
| 2288 |
fputs (":\n", file); |
| 2289 |
|
| 2290 |
free (lab); |
| 2291 |
} |
| 2292 |
|
| 2293 |
static GTY(()) unsigned long except_table_label_num; |
| 2294 |
|
| 2295 |
void |
| 2296 |
darwin_emit_except_table_label (FILE *file) |
| 2297 |
{ |
| 2298 |
char section_start_label[30]; |
| 2299 |
|
| 2300 |
fputs ("\t.p2align\t2\n", file); |
| 2301 |
ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table", |
| 2302 |
except_table_label_num++); |
| 2303 |
ASM_OUTPUT_LABEL (file, section_start_label); |
| 2304 |
} |
| 2305 |
|
| 2306 |
rtx |
| 2307 |
darwin_make_eh_symbol_indirect (rtx orig, bool ARG_UNUSED (pubvis)) |
| 2308 |
{ |
| 2309 |
if (DARWIN_PPC == 0 && TARGET_64BIT) |
| 2310 |
return orig; |
| 2311 |
|
| 2312 |
return gen_rtx_SYMBOL_REF (Pmode, |
| 2313 |
machopic_indirection_name (orig, |
| 2314 |
/*stub_p=*/false)); |
| 2315 |
} |
| 2316 |
|
| 2317 |
/* The unwinders in earlier Darwin versions are based on an old version |
| 2318 |
of libgcc_s and need current frame address stateto be reset after a |
| 2319 |
DW_CFA_restore_state recovers the register values. */ |
| 2320 |
|
| 2321 |
bool |
| 2322 |
darwin_should_restore_cfa_state (void) |
| 2323 |
{ |
| 2324 |
return generating_for_darwin_version <= 10; |
| 2325 |
} |
| 2326 |
|
| 2327 |
/* Return, and mark as used, the name of the stub for the mcount function. |
| 2328 |
Currently, this is only called by X86 code in the expansion of the |
| 2329 |
FUNCTION_PROFILER macro, when stubs are enabled. */ |
| 2330 |
|
| 2331 |
const char* |
| 2332 |
machopic_mcount_stub_name (void) |
| 2333 |
{ |
| 2334 |
rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount"); |
| 2335 |
const char *name = machopic_indirection_name (symbol, /*stub_p=*/true); |
| 2336 |
machopic_validate_stub_or_non_lazy_ptr (name); |
| 2337 |
return name; |
| 2338 |
} |
| 2339 |
|
| 2340 |
/* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */ |
| 2341 |
|
| 2342 |
void |
| 2343 |
darwin_non_lazy_pcrel (FILE *file, rtx addr) |
| 2344 |
{ |
| 2345 |
const char *nlp_name; |
| 2346 |
|
| 2347 |
gcc_assert (GET_CODE (addr) == SYMBOL_REF); |
| 2348 |
|
| 2349 |
nlp_name = machopic_indirection_name (addr, /*stub_p=*/false); |
| 2350 |
fputs ("\t.long\t", file); |
| 2351 |
ASM_OUTPUT_LABELREF (file, nlp_name); |
| 2352 |
fputs ("-.", file); |
| 2353 |
} |
| 2354 |
|
| 2355 |
/* If this is uncommented, details of each allocation will be printed |
| 2356 |
in the asm right before the actual code. WARNING - this will cause some |
| 2357 |
test-suite fails (since the printout will contain items that some tests |
| 2358 |
are not expecting) -- so don't leave it on by default (it bloats the |
| 2359 |
asm too). */ |
| 2360 |
/*#define DEBUG_DARWIN_MEM_ALLOCATORS*/ |
| 2361 |
|
| 2362 |
/* The first two of these routines are ostensibly just intended to put |
| 2363 |
names into the asm. However, they are both hijacked in order to ensure |
| 2364 |
that zero-sized items do not make their way into the output. Consequently, |
| 2365 |
we also need to make these participate in provisions for dealing with |
| 2366 |
such items in section anchors. */ |
| 2367 |
|
| 2368 |
/* The implementation of ASM_DECLARE_OBJECT_NAME. */ |
| 2369 |
/* The RTTI data (e.g., __ti4name) is common and public (and static), |
| 2370 |
but it does need to be referenced via indirect PIC data pointers. |
| 2371 |
The machopic_define_symbol calls are telling the machopic subsystem |
| 2372 |
that the name *is* defined in this module, so it doesn't need to |
| 2373 |
make them indirect. */ |
| 2374 |
void |
| 2375 |
darwin_asm_declare_object_name (FILE *file, |
| 2376 |
const char *nam, tree decl) |
| 2377 |
{ |
| 2378 |
const char *xname = nam; |
| 2379 |
unsigned HOST_WIDE_INT size; |
| 2380 |
bool local_def, weak; |
| 2381 |
|
| 2382 |
weak = (DECL_P (decl) |
| 2383 |
&& DECL_WEAK (decl) |
| 2384 |
&& !lookup_attribute ("weak_import", |
| 2385 |
DECL_ATTRIBUTES (decl))); |
| 2386 |
|
| 2387 |
local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl) |
| 2388 |
&& (!DECL_COMMON (decl) |
| 2389 |
|| !TREE_PUBLIC (decl))); |
| 2390 |
|
| 2391 |
if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF) |
| 2392 |
xname = IDENTIFIER_POINTER (DECL_NAME (decl)); |
| 2393 |
|
| 2394 |
if (local_def) |
| 2395 |
{ |
| 2396 |
(* targetm.encode_section_info) (decl, DECL_RTL (decl), false); |
| 2397 |
if (!weak) |
| 2398 |
machopic_define_symbol (DECL_RTL (decl)); |
| 2399 |
} |
| 2400 |
|
| 2401 |
size = tree_to_uhwi (DECL_SIZE_UNIT (decl)); |
| 2402 |
|
| 2403 |
#ifdef DEBUG_DARWIN_MEM_ALLOCATORS |
| 2404 |
fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d" |
| 2405 |
" stat %d com %d pub %d t-const %d t-ro %d init %lx\n", |
| 2406 |
xname, TREE_CODE (decl) == VAR_DECL ? "var" : "const", |
| 2407 |
(unsigned long long)size, DECL_ALIGN (decl), local_def, |
| 2408 |
DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl), |
| 2409 |
TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl), |
| 2410 |
(unsigned long)DECL_INITIAL (decl)); |
| 2411 |
#endif |
| 2412 |
|
| 2413 |
/* Darwin needs help to support local zero-sized objects. |
| 2414 |
They must be made at least one byte, and the section containing must be |
| 2415 |
marked as unsuitable for section-anchors (see storage allocators below). |
| 2416 |
|
| 2417 |
For non-zero objects this output is handled by varasm.cc. |
| 2418 |
*/ |
| 2419 |
if (!size) |
| 2420 |
{ |
| 2421 |
unsigned int l2align = 0; |
| 2422 |
|
| 2423 |
/* The align must be honored, even for zero-sized. */ |
| 2424 |
if (DECL_ALIGN (decl)) |
| 2425 |
{ |
| 2426 |
l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT); |
| 2427 |
fprintf (file, "\t.align\t%u\n", l2align); |
| 2428 |
} |
| 2429 |
|
| 2430 |
ASM_OUTPUT_LABEL (file, xname); |
| 2431 |
size = 1; |
| 2432 |
fprintf (file, "\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size); |
| 2433 |
|
| 2434 |
/* Check that we've correctly picked up the zero-sized item and placed it |
| 2435 |
properly. */ |
| 2436 |
gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors) |
| 2437 |
|| (in_section |
| 2438 |
&& (in_section->common.flags & SECTION_NO_ANCHOR))); |
| 2439 |
} |
| 2440 |
else |
| 2441 |
ASM_OUTPUT_LABEL (file, xname); |
| 2442 |
} |
| 2443 |
|
| 2444 |
/* The implementation of ASM_DECLARE_CONSTANT_NAME. */ |
| 2445 |
void |
| 2446 |
darwin_asm_declare_constant_name (FILE *file, const char *name, |
| 2447 |
const_tree exp ATTRIBUTE_UNUSED, |
| 2448 |
HOST_WIDE_INT size) |
| 2449 |
{ |
| 2450 |
assemble_label (file, name); |
| 2451 |
/* As for other items, we need at least one byte. */ |
| 2452 |
if (!size) |
| 2453 |
{ |
| 2454 |
fputs ("\t.space\t1\n", file); |
| 2455 |
/* Check that we've correctly picked up the zero-sized item and placed it |
| 2456 |
properly. */ |
| 2457 |
gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors) |
| 2458 |
|| (in_section |
| 2459 |
&& (in_section->common.flags & SECTION_NO_ANCHOR))); |
| 2460 |
} |
| 2461 |
} |
| 2462 |
|
| 2463 |
/* Darwin storage allocators. |
| 2464 |
|
| 2465 |
Zerofill sections are desirable for large blank data since, otherwise, these |
| 2466 |
data bloat objects (PR33210). |
| 2467 |
|
| 2468 |
However, section anchors don't work in .zerofill sections (one cannot switch |
| 2469 |
to a zerofill section). Ergo, for Darwin targets using section anchors we need |
| 2470 |
to put (at least some) data into 'normal' switchable sections. |
| 2471 |
|
| 2472 |
Here we set a relatively arbitrary value for the size of an object to trigger |
| 2473 |
zerofill when section anchors are enabled (anything bigger than a page for |
| 2474 |
current Darwin implementations). FIXME: there ought to be some objective way |
| 2475 |
to make this choice. |
| 2476 |
|
| 2477 |
When section anchor are off this is ignored anyway. */ |
| 2478 |
|
| 2479 |
#define BYTES_ZFILL 4096 |
| 2480 |
|
| 2481 |
/* Emit a chunk of data for items coalesced by the linker. */ |
| 2482 |
static void |
| 2483 |
darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name, |
| 2484 |
unsigned HOST_WIDE_INT size, |
| 2485 |
bool use_coal, |
| 2486 |
unsigned int align) |
| 2487 |
{ |
| 2488 |
/* Since the sections used here are coalesced, they will not be eligible |
| 2489 |
for section anchors, and therefore we don't need to break that out. |
| 2490 |
CHECKME: for modern linker on PowerPC. */ |
| 2491 |
if (TREE_READONLY (decl) || TREE_CONSTANT (decl)) |
| 2492 |
switch_to_section (use_coal ? darwin_sections[const_data_coal_section] |
| 2493 |
: darwin_sections[const_data_section]); |
| 2494 |
else |
| 2495 |
switch_to_section (use_coal ? darwin_sections[data_coal_section] |
| 2496 |
: data_section); |
| 2497 |
|
| 2498 |
/* To be consistent, we'll allow darwin_asm_declare_object_name to assemble |
| 2499 |
the align info for zero-sized items... but do it here otherwise. */ |
| 2500 |
if (size && align) |
| 2501 |
fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT)); |
| 2502 |
|
| 2503 |
if (TREE_PUBLIC (decl)) |
| 2504 |
darwin_globalize_label (fp, name); |
| 2505 |
|
| 2506 |
/* ... and we let it deal with outputting one byte of zero for them too. */ |
| 2507 |
darwin_asm_declare_object_name (fp, name, decl); |
| 2508 |
if (size) |
| 2509 |
assemble_zeros (size); |
| 2510 |
} |
| 2511 |
|
| 2512 |
/* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */ |
| 2513 |
static void |
| 2514 |
darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name, |
| 2515 |
unsigned HOST_WIDE_INT size, |
| 2516 |
unsigned int align, tree meta) |
| 2517 |
{ |
| 2518 |
section *ocs = data_section; |
| 2519 |
|
| 2520 |
if (TREE_PURPOSE (meta) == get_identifier("OBJC2META")) |
| 2521 |
ocs = darwin_objc2_section (decl, meta, ocs); |
| 2522 |
else |
| 2523 |
ocs = darwin_objc1_section (decl, meta, ocs); |
| 2524 |
|
| 2525 |
switch_to_section (ocs); |
| 2526 |
|
| 2527 |
/* We shall declare that zero-sized meta-data are not valid (yet). */ |
| 2528 |
gcc_assert (size); |
| 2529 |
fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT)); |
| 2530 |
|
| 2531 |
/* ... and we let it deal with outputting one byte of zero for them too. */ |
| 2532 |
darwin_asm_declare_object_name (fp, name, decl); |
| 2533 |
assemble_zeros (size); |
| 2534 |
} |
| 2535 |
|
| 2536 |
/* This routine emits 'local' storage: |
| 2537 |
|
| 2538 |
When Section Anchors are off this routine emits .zerofill commands in |
| 2539 |
sections named for their alignment. |
| 2540 |
|
| 2541 |
When Section Anchors are on, smaller (non-zero-sized) items are placed in |
| 2542 |
the .static_data section so that the section anchoring system can see them. |
| 2543 |
Larger items are still placed in .zerofill sections, addressing PR33210. |
| 2544 |
The routine has no checking - it is all assumed to be done by the caller. |
| 2545 |
*/ |
| 2546 |
static void |
| 2547 |
darwin_emit_local_bss (FILE *fp, tree decl, const char *name, |
| 2548 |
unsigned HOST_WIDE_INT size, |
| 2549 |
unsigned int l2align) |
| 2550 |
{ |
| 2551 |
if (DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL) |
| 2552 |
{ |
| 2553 |
/* Put smaller objects in _static_data, where the section anchors system |
| 2554 |
can get them. |
| 2555 |
However, if they are zero-sized punt them to yet a different section |
| 2556 |
(that is not allowed to participate in anchoring). */ |
| 2557 |
if (!size) |
| 2558 |
{ |
| 2559 |
fputs ("\t.section\t__DATA,__zobj_bss\n", fp); |
| 2560 |
in_section = darwin_sections[zobj_bss_section]; |
| 2561 |
size = 1; |
| 2562 |
} |
| 2563 |
else |
| 2564 |
{ |
| 2565 |
fputs ("\t.static_data\n", fp); |
| 2566 |
in_section = darwin_sections[static_data_section]; |
| 2567 |
} |
| 2568 |
|
| 2569 |
if (l2align) |
| 2570 |
fprintf (fp, "\t.align\t%u\n", l2align); |
| 2571 |
|
| 2572 |
assemble_name (fp, name); |
| 2573 |
fprintf (fp, ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size); |
| 2574 |
} |
| 2575 |
else |
| 2576 |
{ |
| 2577 |
/* When we are on a non-section anchor target (or not using section |
| 2578 |
anchors, we can get zero-sized items here. However, all we need to |
| 2579 |
do is to bump them to one byte and the section alignment will take |
| 2580 |
care of the rest. */ |
| 2581 |
char secnam[64]; |
| 2582 |
snprintf (secnam, 64, "__DATA,__bss"); |
| 2583 |
unsigned int flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR; |
| 2584 |
in_section = get_section (secnam, flags, NULL); |
| 2585 |
fprintf (fp, "\t.zerofill %s,", secnam); |
| 2586 |
assemble_name (fp, name); |
| 2587 |
if (!size) |
| 2588 |
size = 1; |
| 2589 |
|
| 2590 |
if (l2align) |
| 2591 |
fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", |
| 2592 |
size, (unsigned) l2align); |
| 2593 |
else |
| 2594 |
fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED",0\n", size); |
| 2595 |
} |
| 2596 |
|
| 2597 |
(*targetm.encode_section_info) (decl, DECL_RTL (decl), false); |
| 2598 |
/* This is defined as a file-scope var, so we know to notify machopic. */ |
| 2599 |
machopic_define_symbol (DECL_RTL (decl)); |
| 2600 |
} |
| 2601 |
|
| 2602 |
/* Emit a chunk of common. */ |
| 2603 |
static void |
| 2604 |
darwin_emit_common (FILE *fp, const char *name, |
| 2605 |
unsigned HOST_WIDE_INT size, unsigned int align) |
| 2606 |
{ |
| 2607 |
unsigned HOST_WIDE_INT rounded; |
| 2608 |
unsigned int l2align; |
| 2609 |
|
| 2610 |
/* Earlier systems complain if the alignment exceeds the page size. |
| 2611 |
The magic number is 4096 * 8 - hard-coded for legacy systems. */ |
| 2612 |
if (!emit_aligned_common && (align > 32768UL)) |
| 2613 |
align = 4096UL; /* In units. */ |
| 2614 |
else |
| 2615 |
align /= BITS_PER_UNIT; |
| 2616 |
|
| 2617 |
/* Make sure we have a meaningful align. */ |
| 2618 |
if (!align) |
| 2619 |
align = 1; |
| 2620 |
|
| 2621 |
/* For earlier toolchains, we need to emit the var as a rounded size to |
| 2622 |
tell ld the alignment. */ |
| 2623 |
if (size < align) |
| 2624 |
rounded = align; |
| 2625 |
else |
| 2626 |
rounded = (size + (align-1)) & ~(align-1); |
| 2627 |
|
| 2628 |
l2align = floor_log2 (align); |
| 2629 |
|
| 2630 |
in_section = comm_section; |
| 2631 |
/* We mustn't allow multiple public symbols to share an address when using |
| 2632 |
the normal OSX toolchain. */ |
| 2633 |
if (!size) |
| 2634 |
{ |
| 2635 |
/* Put at least one byte. */ |
| 2636 |
size = 1; |
| 2637 |
/* This section can no longer participate in section anchoring. */ |
| 2638 |
comm_section->common.flags |= SECTION_NO_ANCHOR; |
| 2639 |
} |
| 2640 |
|
| 2641 |
fputs ("\t.comm\t", fp); |
| 2642 |
assemble_name (fp, name); |
| 2643 |
fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED, |
| 2644 |
emit_aligned_common ? size : rounded); |
| 2645 |
if (l2align && emit_aligned_common) |
| 2646 |
fprintf (fp, ",%u", l2align); |
| 2647 |
fputs ("\n", fp); |
| 2648 |
} |
| 2649 |
|
| 2650 |
/* Output a var which is all zero - into aligned BSS sections, common, lcomm |
| 2651 |
or coalescable data sections (for weak or comdat) as appropriate. */ |
| 2652 |
|
| 2653 |
void |
| 2654 |
darwin_output_aligned_bss (FILE *fp, tree decl, const char *name, |
| 2655 |
unsigned HOST_WIDE_INT size, unsigned int align) |
| 2656 |
{ |
| 2657 |
unsigned int l2align; |
| 2658 |
bool one, pub, weak; |
| 2659 |
tree meta; |
| 2660 |
|
| 2661 |
pub = TREE_PUBLIC (decl); |
| 2662 |
one = DECL_ONE_ONLY (decl); |
| 2663 |
weak = (DECL_P (decl) |
| 2664 |
&& DECL_WEAK (decl) |
| 2665 |
&& !lookup_attribute ("weak_import", |
| 2666 |
DECL_ATTRIBUTES (decl))); |
| 2667 |
|
| 2668 |
#ifdef DEBUG_DARWIN_MEM_ALLOCATORS |
| 2669 |
fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d" |
| 2670 |
" pub %d weak %d one %d init %lx\n", |
| 2671 |
name, (long long)size, (int)align, TREE_READONLY (decl), |
| 2672 |
TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl), |
| 2673 |
pub, weak, one, (unsigned long)DECL_INITIAL (decl)); |
| 2674 |
#endif |
| 2675 |
|
| 2676 |
/* ObjC metadata can get put in BSS because varasm.cc decides it's BSS |
| 2677 |
before the target has a chance to comment. */ |
| 2678 |
if ((meta = is_objc_metadata (decl))) |
| 2679 |
{ |
| 2680 |
darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta); |
| 2681 |
return; |
| 2682 |
} |
| 2683 |
|
| 2684 |
/* Check that any initializer is valid. */ |
| 2685 |
gcc_assert ((DECL_INITIAL (decl) == NULL) |
| 2686 |
|| (DECL_INITIAL (decl) == error_mark_node) |
| 2687 |
|| initializer_zerop (DECL_INITIAL (decl))); |
| 2688 |
|
| 2689 |
gcc_assert (DECL_SECTION_NAME (decl) == NULL); |
| 2690 |
gcc_assert (!DECL_COMMON (decl)); |
| 2691 |
|
| 2692 |
/* Pick up the correct alignment. */ |
| 2693 |
if (!size || !align) |
| 2694 |
align = DECL_ALIGN (decl); |
| 2695 |
|
| 2696 |
l2align = floor_log2 (align / BITS_PER_UNIT); |
| 2697 |
gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT); |
| 2698 |
|
| 2699 |
last_assemble_variable_decl = decl; |
| 2700 |
|
| 2701 |
/* We would rather not have to check this here - but it seems that we might |
| 2702 |
be passed a decl that should be in coalesced space. */ |
| 2703 |
if (one || weak) |
| 2704 |
{ |
| 2705 |
/* Weak or COMDAT objects are put in mergeable sections. */ |
| 2706 |
darwin_emit_weak_or_comdat (fp, decl, name, size, |
| 2707 |
ld_uses_coal_sects, DECL_ALIGN (decl)); |
| 2708 |
return; |
| 2709 |
} |
| 2710 |
|
| 2711 |
/* If this is not public, then emit according to local rules. */ |
| 2712 |
if (!pub) |
| 2713 |
{ |
| 2714 |
darwin_emit_local_bss (fp, decl, name, size, l2align); |
| 2715 |
return; |
| 2716 |
} |
| 2717 |
|
| 2718 |
/* So we have a public symbol. */ |
| 2719 |
if (DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL) |
| 2720 |
{ |
| 2721 |
/* Put smaller objects in data, where the section anchors system can get |
| 2722 |
them. However, if they are zero-sized punt them to yet a different |
| 2723 |
section (that is not allowed to participate in anchoring). */ |
| 2724 |
if (!size) |
| 2725 |
{ |
| 2726 |
fputs ("\t.section\t__DATA,__zobj_data\n", fp); |
| 2727 |
in_section = darwin_sections[zobj_data_section]; |
| 2728 |
size = 1; |
| 2729 |
} |
| 2730 |
else |
| 2731 |
{ |
| 2732 |
fputs ("\t.data\n", fp); |
| 2733 |
in_section = data_section; |
| 2734 |
} |
| 2735 |
|
| 2736 |
if (l2align) |
| 2737 |
fprintf (fp, "\t.align\t%u\n", l2align); |
| 2738 |
|
| 2739 |
assemble_name (fp, name); |
| 2740 |
fprintf (fp, ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size); |
| 2741 |
} |
| 2742 |
else |
| 2743 |
{ |
| 2744 |
/* Section anchors not in use. */ |
| 2745 |
unsigned int flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR; |
| 2746 |
char secnam[64]; |
| 2747 |
snprintf (secnam, 64, "__DATA,__common"); |
| 2748 |
in_section = get_section (secnam, flags, NULL); |
| 2749 |
fprintf (fp, "\t.zerofill %s,", secnam); |
| 2750 |
assemble_name (fp, name); |
| 2751 |
if (!size) |
| 2752 |
size = 1; |
| 2753 |
|
| 2754 |
if (l2align) |
| 2755 |
fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align); |
| 2756 |
else |
| 2757 |
fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED",0\n", size); |
| 2758 |
} |
| 2759 |
(* targetm.encode_section_info) (decl, DECL_RTL (decl), false); |
| 2760 |
} |
| 2761 |
|
| 2762 |
/* Output a chunk of common, with alignment specified (where the target |
| 2763 |
supports this). */ |
| 2764 |
void |
| 2765 |
darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name, |
| 2766 |
unsigned HOST_WIDE_INT size, |
| 2767 |
unsigned int align) |
| 2768 |
{ |
| 2769 |
unsigned int l2align; |
| 2770 |
bool one, weak; |
| 2771 |
tree meta; |
| 2772 |
|
| 2773 |
/* No corresponding var. */ |
| 2774 |
if (decl==NULL) |
| 2775 |
{ |
| 2776 |
#ifdef DEBUG_DARWIN_MEM_ALLOCATORS |
| 2777 |
fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align); |
| 2778 |
#endif |
| 2779 |
/* Common variables are limited to a maximum alignment of 2^15. */ |
| 2780 |
if (align > 32768) |
| 2781 |
error_at (UNKNOWN_LOCATION, "common variables must have an alignment" |
| 2782 |
" of 32678 or less"); |
| 2783 |
darwin_emit_common (fp, name, size, align); |
| 2784 |
return; |
| 2785 |
} |
| 2786 |
|
| 2787 |
one = DECL_ONE_ONLY (decl); |
| 2788 |
weak = (DECL_P (decl) |
| 2789 |
&& DECL_WEAK (decl) |
| 2790 |
&& !lookup_attribute ("weak_import", |
| 2791 |
DECL_ATTRIBUTES (decl))); |
| 2792 |
|
| 2793 |
#ifdef DEBUG_DARWIN_MEM_ALLOCATORS |
| 2794 |
fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d" |
| 2795 |
" weak %d one %d init %lx\n", |
| 2796 |
name, (long long)size, (int)align, TREE_READONLY (decl), |
| 2797 |
TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl), |
| 2798 |
TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl)); |
| 2799 |
#endif |
| 2800 |
|
| 2801 |
/* ObjC metadata can get put in BSS because varasm.cc decides it's BSS |
| 2802 |
before the target has a chance to comment. */ |
| 2803 |
if ((meta = is_objc_metadata (decl))) |
| 2804 |
{ |
| 2805 |
darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta); |
| 2806 |
return; |
| 2807 |
} |
| 2808 |
|
| 2809 |
/* We shouldn't be messing with this if the decl has a section name. */ |
| 2810 |
gcc_checking_assert (DECL_SECTION_NAME (decl) == NULL); |
| 2811 |
|
| 2812 |
/* We would rather not have to check this here - but it seems that we might |
| 2813 |
be passed a decl that should be in coalesced space. */ |
| 2814 |
if (one || weak) |
| 2815 |
{ |
| 2816 |
/* Weak or COMDAT objects are put in mergable sections. */ |
| 2817 |
darwin_emit_weak_or_comdat (fp, decl, name, size, |
| 2818 |
ld_uses_coal_sects, DECL_ALIGN (decl)); |
| 2819 |
return; |
| 2820 |
} |
| 2821 |
|
| 2822 |
/* We should only get here for DECL_COMMON, with a zero init (and, in |
| 2823 |
principle, only for public symbols too - although we deal with local |
| 2824 |
ones below). */ |
| 2825 |
|
| 2826 |
/* Check the initializer is OK. */ |
| 2827 |
gcc_assert (DECL_COMMON (decl) |
| 2828 |
&& ((DECL_INITIAL (decl) == NULL) |
| 2829 |
|| (DECL_INITIAL (decl) == error_mark_node) |
| 2830 |
|| initializer_zerop (DECL_INITIAL (decl)))); |
| 2831 |
|
| 2832 |
last_assemble_variable_decl = decl; |
| 2833 |
|
| 2834 |
if (!size || !align) |
| 2835 |
align = DECL_ALIGN (decl); |
| 2836 |
|
| 2837 |
l2align = floor_log2 (align / BITS_PER_UNIT); |
| 2838 |
/* Check we aren't asking for more aligment than the platform allows. */ |
| 2839 |
gcc_checking_assert (l2align <= L2_MAX_OFILE_ALIGNMENT); |
| 2840 |
|
| 2841 |
if (TREE_PUBLIC (decl) != 0) |
| 2842 |
{ |
| 2843 |
/* Common variables are limited to a maximum alignment of 2^15. */ |
| 2844 |
if (l2align > 15) |
| 2845 |
error_at (DECL_SOURCE_LOCATION (decl), "common variables must have" |
| 2846 |
" an alignment of 32678 or less"); |
| 2847 |
darwin_emit_common (fp, name, size, align); |
| 2848 |
} |
| 2849 |
else |
| 2850 |
darwin_emit_local_bss (fp, decl, name, size, l2align); |
| 2851 |
} |
| 2852 |
|
| 2853 |
/* Output a chunk of BSS with alignment specfied. */ |
| 2854 |
void |
| 2855 |
darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name, |
| 2856 |
unsigned HOST_WIDE_INT size, |
| 2857 |
unsigned int align) |
| 2858 |
{ |
| 2859 |
unsigned long l2align; |
| 2860 |
bool one, weak; |
| 2861 |
tree meta; |
| 2862 |
|
| 2863 |
one = DECL_ONE_ONLY (decl); |
| 2864 |
weak = (DECL_P (decl) |
| 2865 |
&& DECL_WEAK (decl) |
| 2866 |
&& !lookup_attribute ("weak_import", |
| 2867 |
DECL_ATTRIBUTES (decl))); |
| 2868 |
|
| 2869 |
#ifdef DEBUG_DARWIN_MEM_ALLOCATORS |
| 2870 |
fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d" |
| 2871 |
" weak %d init %lx\n", |
| 2872 |
name, (long long)size, (int)align, TREE_READONLY (decl), |
| 2873 |
TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl), |
| 2874 |
weak , (unsigned long)DECL_INITIAL (decl)); |
| 2875 |
#endif |
| 2876 |
|
| 2877 |
/* ObjC metadata can get put in BSS because varasm.cc decides it's BSS |
| 2878 |
before the target has a chance to comment. */ |
| 2879 |
if ((meta = is_objc_metadata (decl))) |
| 2880 |
{ |
| 2881 |
darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta); |
| 2882 |
return; |
| 2883 |
} |
| 2884 |
|
| 2885 |
/* We shouldn't be messing with this if the decl has a section name. */ |
| 2886 |
gcc_assert (DECL_SECTION_NAME (decl) == NULL); |
| 2887 |
|
| 2888 |
/* We would rather not have to check this here - but it seems that we might |
| 2889 |
be passed a decl that should be in coalesced space. */ |
| 2890 |
if (one || weak) |
| 2891 |
{ |
| 2892 |
/* Weak or COMDAT objects are put in mergable sections. */ |
| 2893 |
darwin_emit_weak_or_comdat (fp, decl, name, size, |
| 2894 |
ld_uses_coal_sects, DECL_ALIGN (decl)); |
| 2895 |
return; |
| 2896 |
} |
| 2897 |
|
| 2898 |
/* .. and it should be suitable for placement in local mem. */ |
| 2899 |
gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl)); |
| 2900 |
/* .. and any initializer must be all-zero. */ |
| 2901 |
gcc_assert ((DECL_INITIAL (decl) == NULL) |
| 2902 |
|| (DECL_INITIAL (decl) == error_mark_node) |
| 2903 |
|| initializer_zerop (DECL_INITIAL (decl))); |
| 2904 |
|
| 2905 |
last_assemble_variable_decl = decl; |
| 2906 |
|
| 2907 |
if (!size || !align) |
| 2908 |
align = DECL_ALIGN (decl); |
| 2909 |
|
| 2910 |
l2align = floor_log2 (align / BITS_PER_UNIT); |
| 2911 |
gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT); |
| 2912 |
|
| 2913 |
darwin_emit_local_bss (fp, decl, name, size, l2align); |
| 2914 |
} |
| 2915 |
|
| 2916 |
/* Emit an assembler directive to set visibility for a symbol. The |
| 2917 |
only supported visibilities are VISIBILITY_DEFAULT and |
| 2918 |
VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private |
| 2919 |
extern". There is no MACH-O equivalent of ELF's |
| 2920 |
VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */ |
| 2921 |
|
| 2922 |
void |
| 2923 |
darwin_assemble_visibility (tree decl, int vis) |
| 2924 |
{ |
| 2925 |
if (vis == VISIBILITY_DEFAULT) |
| 2926 |
; |
| 2927 |
else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL) |
| 2928 |
{ |
| 2929 |
fputs ("\t.private_extern ", asm_out_file); |
| 2930 |
assemble_name (asm_out_file, |
| 2931 |
(IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)))); |
| 2932 |
fputs ("\n", asm_out_file); |
| 2933 |
} |
| 2934 |
else |
| 2935 |
warning (OPT_Wattributes, "protected visibility attribute " |
| 2936 |
"not supported in this configuration; ignored"); |
| 2937 |
} |
| 2938 |
|
| 2939 |
/* vec used by darwin_asm_dwarf_section. |
| 2940 |
Maybe a hash tab would be better here - but the intention is that this is |
| 2941 |
a very short list (fewer than 16 items) and each entry should (ideally, |
| 2942 |
eventually) only be presented once. |
| 2943 |
|
| 2944 |
A structure to hold a dwarf debug section used entry. */ |
| 2945 |
|
| 2946 |
typedef struct GTY(()) dwarf_sect_used_entry { |
| 2947 |
const char *name; |
| 2948 |
unsigned count; |
| 2949 |
} |
| 2950 |
dwarf_sect_used_entry; |
| 2951 |
|
| 2952 |
|
| 2953 |
/* A list of used __DWARF sections. */ |
| 2954 |
static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table; |
| 2955 |
|
| 2956 |
/* This is called when we are asked to assemble a named section and the |
| 2957 |
name begins with __DWARF,. We keep a list of the section names (without |
| 2958 |
the __DWARF, prefix) and use this to emit our required start label on the |
| 2959 |
first switch to each section. */ |
| 2960 |
|
| 2961 |
static void |
| 2962 |
darwin_asm_dwarf_section (const char *name, unsigned int flags, |
| 2963 |
tree ARG_UNUSED (decl), bool is_for_lto) |
| 2964 |
{ |
| 2965 |
unsigned i; |
| 2966 |
int namelen, extra = 0; |
| 2967 |
const char *sect, *lto_add = ""; |
| 2968 |
char sname[64]; |
| 2969 |
dwarf_sect_used_entry *ref; |
| 2970 |
bool found = false; |
| 2971 |
|
| 2972 |
gcc_checking_assert ((flags & (SECTION_DEBUG | SECTION_NAMED)) |
| 2973 |
== (SECTION_DEBUG | SECTION_NAMED)); |
| 2974 |
|
| 2975 |
/* We know that the name starts with __DWARF, or __GNU_DAWRF_LTO */ |
| 2976 |
sect = strchr (name, ',') + 1; |
| 2977 |
namelen = strchr (sect, ',') - sect; |
| 2978 |
gcc_checking_assert (namelen); |
| 2979 |
|
| 2980 |
/* The section switch is output as written... */ |
| 2981 |
fprintf (asm_out_file, "\t.section %s\n", name); |
| 2982 |
|
| 2983 |
/* ... but the string we keep to make section start labels needs |
| 2984 |
adjustment for lto cases. */ |
| 2985 |
if (is_for_lto) |
| 2986 |
{ |
| 2987 |
lto_add = "_lto"; |
| 2988 |
extra = 4; |
| 2989 |
} |
| 2990 |
|
| 2991 |
snprintf (sname, 64, "%.*s%.*s", namelen, sect, extra, lto_add); |
| 2992 |
namelen += extra; |
| 2993 |
|
| 2994 |
if (dwarf_sect_names_table == NULL) |
| 2995 |
vec_alloc (dwarf_sect_names_table, 16); |
| 2996 |
else |
| 2997 |
for (i = 0; |
| 2998 |
dwarf_sect_names_table->iterate (i, &ref); |
| 2999 |
i++) |
| 3000 |
{ |
| 3001 |
if (!ref) |
| 3002 |
break; |
| 3003 |
if (!strcmp (ref->name, sname)) |
| 3004 |
{ |
| 3005 |
found = true; |
| 3006 |
ref->count++; |
| 3007 |
break; |
| 3008 |
} |
| 3009 |
} |
| 3010 |
|
| 3011 |
if (!found) |
| 3012 |
{ |
| 3013 |
dwarf_sect_used_entry e; |
| 3014 |
fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname); |
| 3015 |
e.count = 1; |
| 3016 |
e.name = xstrdup (sname); |
| 3017 |
vec_safe_push (dwarf_sect_names_table, e); |
| 3018 |
} |
| 3019 |
} |
| 3020 |
|
| 3021 |
/* Output a difference of two labels that will be an assembly time |
| 3022 |
constant if the two labels are local. (.long lab1-lab2 will be |
| 3023 |
very different if lab1 is at the boundary between two sections; it |
| 3024 |
will be relocated according to the second section, not the first, |
| 3025 |
so one ends up with a difference between labels in different |
| 3026 |
sections, which is bad in the dwarf2 eh context for instance.) */ |
| 3027 |
|
| 3028 |
static int darwin_dwarf_label_counter; |
| 3029 |
|
| 3030 |
void |
| 3031 |
darwin_asm_output_dwarf_delta (FILE *file, int size, |
| 3032 |
const char *lab1, const char *lab2, |
| 3033 |
HOST_WIDE_INT offset) |
| 3034 |
{ |
| 3035 |
int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L' |
| 3036 |
&& lab2[0] == '*' && lab2[1] == 'L'); |
| 3037 |
const char *directive = (size == 8 ? ".quad" : ".long"); |
| 3038 |
|
| 3039 |
if (islocaldiff) |
| 3040 |
fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter); |
| 3041 |
else |
| 3042 |
fprintf (file, "\t%s\t", directive); |
| 3043 |
|
| 3044 |
assemble_name_raw (file, lab1); |
| 3045 |
fprintf (file, "-"); |
| 3046 |
assemble_name_raw (file, lab2); |
| 3047 |
if (offset != 0) |
| 3048 |
fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset); |
| 3049 |
if (islocaldiff) |
| 3050 |
fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++); |
| 3051 |
} |
| 3052 |
|
| 3053 |
/* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section |
| 3054 |
offsets are not represented using relocs in .o files; either the |
| 3055 |
section never leaves the .o file, or the linker or other tool is |
| 3056 |
responsible for parsing the DWARF and updating the offsets. */ |
| 3057 |
|
| 3058 |
void |
| 3059 |
darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab, |
| 3060 |
HOST_WIDE_INT offset, section *base) |
| 3061 |
{ |
| 3062 |
char sname[64]; |
| 3063 |
int namelen, extra = 0; |
| 3064 |
bool is_for_lto; |
| 3065 |
const char *lto_add = ""; |
| 3066 |
|
| 3067 |
gcc_checking_assert (base->common.flags & SECTION_NAMED); |
| 3068 |
is_for_lto = startswith (base->named.name, "__GNU_DWARF_LTO,"); |
| 3069 |
gcc_checking_assert (is_for_lto |
| 3070 |
|| startswith (base->named.name, "__DWARF,")); |
| 3071 |
const char *name = strchr (base->named.name, ',') + 1; |
| 3072 |
gcc_checking_assert (name); |
| 3073 |
|
| 3074 |
namelen = strchr (name, ',') - (name); |
| 3075 |
if (is_for_lto) |
| 3076 |
{ |
| 3077 |
lto_add = "_lto"; |
| 3078 |
extra = 4; |
| 3079 |
} |
| 3080 |
snprintf (sname, 64, "*Lsection%.*s%.*s", namelen, name, extra, lto_add); |
| 3081 |
darwin_asm_output_dwarf_delta (file, size, lab, sname, offset); |
| 3082 |
} |
| 3083 |
|
| 3084 |
/* Called from the within the TARGET_ASM_FILE_START for each target. */ |
| 3085 |
|
| 3086 |
void |
| 3087 |
darwin_file_start (void) |
| 3088 |
{ |
| 3089 |
#ifdef HAVE_AS_MMACOSX_VERSION_MIN_OPTION |
| 3090 |
/* This should not happen with a well-formed command line, but the user could |
| 3091 |
invoke cc1* directly without it. */ |
| 3092 |
if (!darwin_macosx_version_min) |
| 3093 |
return; |
| 3094 |
/* This assumes that the version passed has been validated in the driver. */ |
| 3095 |
unsigned maj, min, tiny; |
| 3096 |
int count = sscanf (darwin_macosx_version_min, "%u.%u.%u", &maj, &min, &tiny); |
| 3097 |
if (count < 0) |
| 3098 |
return; |
| 3099 |
if (count < 3) |
| 3100 |
tiny = 0; |
| 3101 |
if (count < 2) |
| 3102 |
min = 0; |
| 3103 |
const char *directive; |
| 3104 |
#ifdef HAVE_AS_MACOS_BUILD_VERSION |
| 3105 |
/* We only handle macos, so far. */ |
| 3106 |
if (generating_for_darwin_version >= 18) |
| 3107 |
directive = "build_version macos, "; |
| 3108 |
else |
| 3109 |
#endif |
| 3110 |
directive = "macosx_version_min "; |
| 3111 |
if (count > 2 && tiny != 0) |
| 3112 |
fprintf (asm_out_file, "\t.%s %u, %u, %u\n", directive, maj, min, tiny); |
| 3113 |
else if (count > 1) |
| 3114 |
fprintf (asm_out_file, "\t.%s %u, %u\n", directive, maj, min); |
| 3115 |
else |
| 3116 |
fprintf (asm_out_file, "\t.%s %u, 0\n", directive, maj); |
| 3117 |
#endif |
| 3118 |
} |
| 3119 |
|
| 3120 |
/* Called for the TARGET_ASM_FILE_END hook. |
| 3121 |
Emit the mach-o pic indirection data, the lto data and, finally a flag |
| 3122 |
to tell the linker that it can break the file object into sections and |
| 3123 |
move those around for efficiency. */ |
| 3124 |
|
| 3125 |
void |
| 3126 |
darwin_file_end (void) |
| 3127 |
{ |
| 3128 |
if (!vec_safe_is_empty (ctors)) |
| 3129 |
finalize_ctors (); |
| 3130 |
if (!vec_safe_is_empty (dtors)) |
| 3131 |
finalize_dtors (); |
| 3132 |
|
| 3133 |
/* If we are expecting to output NeXT ObjC meta-data, (and we actually see |
| 3134 |
some) then we output the fix-and-continue marker (Image Info). |
| 3135 |
This applies to Objective C, Objective C++ and LTO with either language |
| 3136 |
as part of the input. */ |
| 3137 |
if (flag_next_runtime && objc_metadata_seen) |
| 3138 |
{ |
| 3139 |
unsigned int flags = 0; |
| 3140 |
if (flag_objc_abi >= 2) |
| 3141 |
{ |
| 3142 |
flags = 16; |
| 3143 |
switch_to_section (darwin_sections[objc2_image_info_section]); |
| 3144 |
} |
| 3145 |
else |
| 3146 |
switch_to_section (darwin_sections[objc_image_info_section]); |
| 3147 |
|
| 3148 |
ASM_OUTPUT_ALIGN (asm_out_file, 2); |
| 3149 |
fputs ("L_OBJC_ImageInfo:\n", asm_out_file); |
| 3150 |
|
| 3151 |
flags |= (flag_replace_objc_classes && classes_seen) ? 1 : 0; |
| 3152 |
flags |= flag_objc_gc ? 2 : 0; |
| 3153 |
|
| 3154 |
fprintf (asm_out_file, "\t.long\t0\n\t.long\t%u\n", flags); |
| 3155 |
} |
| 3156 |
|
| 3157 |
machopic_finish (asm_out_file); |
| 3158 |
if (flag_apple_kext) |
| 3159 |
{ |
| 3160 |
/* These sections are only used for kernel code. */ |
| 3161 |
switch_to_section (darwin_sections[constructor_section]); |
| 3162 |
switch_to_section (darwin_sections[destructor_section]); |
| 3163 |
ASM_OUTPUT_ALIGN (asm_out_file, 1); |
| 3164 |
} |
| 3165 |
|
| 3166 |
/* If there was LTO assembler output, append it to asm_out_file. */ |
| 3167 |
if (lto_asm_out_name) |
| 3168 |
{ |
| 3169 |
int n; |
| 3170 |
char *buf, *lto_asm_txt; |
| 3171 |
|
| 3172 |
/* Shouldn't be here if we failed to switch back. */ |
| 3173 |
gcc_assert (! saved_asm_out_file); |
| 3174 |
|
| 3175 |
lto_asm_out_file = fopen (lto_asm_out_name, "r"); |
| 3176 |
if (lto_asm_out_file == NULL) |
| 3177 |
fatal_error (input_location, |
| 3178 |
"failed to open temporary file %s with LTO output", |
| 3179 |
lto_asm_out_name); |
| 3180 |
fseek (lto_asm_out_file, 0, SEEK_END); |
| 3181 |
n = ftell (lto_asm_out_file); |
| 3182 |
if (n > 0) |
| 3183 |
{ |
| 3184 |
fseek (lto_asm_out_file, 0, SEEK_SET); |
| 3185 |
lto_asm_txt = buf = (char *) xmalloc (n + 1); |
| 3186 |
while (fgets (lto_asm_txt, n, lto_asm_out_file)) |
| 3187 |
fputs (lto_asm_txt, asm_out_file); |
| 3188 |
/* Put a termination label. */ |
| 3189 |
fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n", |
| 3190 |
LTO_SEGMENT_NAME, LTO_SECTS_SECTION); |
| 3191 |
fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n", |
| 3192 |
lto_section_num); |
| 3193 |
/* Make sure our termination label stays in this section. */ |
| 3194 |
fputs ("\t.space\t1\n", asm_out_file); |
| 3195 |
} |
| 3196 |
|
| 3197 |
/* Remove the temporary file. */ |
| 3198 |
fclose (lto_asm_out_file); |
| 3199 |
unlink_if_ordinary (lto_asm_out_name); |
| 3200 |
free (lto_asm_out_name); |
| 3201 |
} |
| 3202 |
|
| 3203 |
/* Output the names and indices. */ |
| 3204 |
if (lto_section_names && lto_section_names->length ()) |
| 3205 |
{ |
| 3206 |
int count; |
| 3207 |
darwin_lto_section_e *ref; |
| 3208 |
/* For now, we'll make the offsets 4 bytes and unaligned - we'll fix |
| 3209 |
the latter up ourselves. */ |
| 3210 |
const char *op = integer_asm_op (4,0); |
| 3211 |
|
| 3212 |
/* Emit the names. */ |
| 3213 |
fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n", |
| 3214 |
LTO_SEGMENT_NAME, LTO_NAMES_SECTION); |
| 3215 |
FOR_EACH_VEC_ELT (*lto_section_names, count, ref) |
| 3216 |
{ |
| 3217 |
fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count); |
| 3218 |
/* We have to jump through hoops to get the values of the intra-section |
| 3219 |
offsets... */ |
| 3220 |
fprintf (asm_out_file, |
| 3221 |
"\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n", |
| 3222 |
count, count); |
| 3223 |
fprintf (asm_out_file, |
| 3224 |
"\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n", |
| 3225 |
count, count+1, count); |
| 3226 |
fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname); |
| 3227 |
} |
| 3228 |
fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num); |
| 3229 |
/* make sure our termination label stays in this section. */ |
| 3230 |
fputs ("\t.space\t1\n", asm_out_file); |
| 3231 |
|
| 3232 |
/* Emit the Index. */ |
| 3233 |
fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n", |
| 3234 |
LTO_SEGMENT_NAME, LTO_INDEX_SECTION); |
| 3235 |
fputs ("\t.align\t2\n", asm_out_file); |
| 3236 |
fputs ("# Section offset, Section length, Name offset, Name length\n", |
| 3237 |
asm_out_file); |
| 3238 |
FOR_EACH_VEC_ELT (*lto_section_names, count, ref) |
| 3239 |
{ |
| 3240 |
fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n", |
| 3241 |
op, count, ref->sectname); |
| 3242 |
fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count); |
| 3243 |
fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count); |
| 3244 |
fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count); |
| 3245 |
} |
| 3246 |
} |
| 3247 |
|
| 3248 |
/* If we have section anchors, then we must prevent the linker from |
| 3249 |
re-arranging data. */ |
| 3250 |
if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors) |
| 3251 |
fprintf (asm_out_file, "\t.subsections_via_symbols\n"); |
| 3252 |
|
| 3253 |
/* We rely on this being NULL at the start of compilation; reset it here |
| 3254 |
so that JIT can reuse a context. */ |
| 3255 |
if (dwarf_sect_names_table != NULL) |
| 3256 |
{ |
| 3257 |
dwarf_sect_names_table->truncate (0); |
| 3258 |
dwarf_sect_names_table = NULL; |
| 3259 |
} |
| 3260 |
} |
| 3261 |
|
| 3262 |
/* TODO: Add a language hook for identifying if a decl is a vtable. */ |
| 3263 |
#define DARWIN_VTABLE_P(DECL) 0 |
| 3264 |
|
| 3265 |
/* Cross-module name binding. Darwin does not support overriding |
| 3266 |
functions at dynamic-link time, except for vtables in kexts. */ |
| 3267 |
|
| 3268 |
bool |
| 3269 |
darwin_binds_local_p (const_tree decl) |
| 3270 |
{ |
| 3271 |
/* We use the "shlib" input to indicate that a symbol should be |
| 3272 |
considered overridable; only relevant for vtables in kernel modules |
| 3273 |
on earlier system versions, and with a TODO to complete. */ |
| 3274 |
bool force_overridable = TARGET_KEXTABI && DARWIN_VTABLE_P (decl); |
| 3275 |
return default_binds_local_p_3 (decl, force_overridable /* shlib */, |
| 3276 |
false /* weak dominate */, |
| 3277 |
false /* extern_protected_data */, |
| 3278 |
false /* common_local_p */); |
| 3279 |
} |
| 3280 |
|
| 3281 |
/* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the |
| 3282 |
anchor relative to ".", the current section position. We cannot use |
| 3283 |
the default one because ASM_OUTPUT_DEF is wrong for Darwin. */ |
| 3284 |
void |
| 3285 |
darwin_asm_output_anchor (rtx symbol) |
| 3286 |
{ |
| 3287 |
fprintf (asm_out_file, "\t.set\t"); |
| 3288 |
assemble_name (asm_out_file, XSTR (symbol, 0)); |
| 3289 |
fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n", |
| 3290 |
SYMBOL_REF_BLOCK_OFFSET (symbol)); |
| 3291 |
} |
| 3292 |
|
| 3293 |
/* Disable section anchoring on any section containing a zero-sized |
| 3294 |
object. */ |
| 3295 |
bool |
| 3296 |
darwin_use_anchors_for_symbol_p (const_rtx symbol) |
| 3297 |
{ |
| 3298 |
if (DARWIN_SECTION_ANCHORS && flag_section_anchors) |
| 3299 |
{ |
| 3300 |
section *sect; |
| 3301 |
/* If the section contains a zero-sized object it's ineligible. */ |
| 3302 |
sect = SYMBOL_REF_BLOCK (symbol)->sect; |
| 3303 |
/* This should have the effect of disabling anchors for vars that follow |
| 3304 |
any zero-sized one, in a given section. */ |
| 3305 |
if (sect->common.flags & SECTION_NO_ANCHOR) |
| 3306 |
return false; |
| 3307 |
|
| 3308 |
/* Also check the normal reasons for suppressing. */ |
| 3309 |
return default_use_anchors_for_symbol_p (symbol); |
| 3310 |
} |
| 3311 |
else |
| 3312 |
return false; |
| 3313 |
} |
| 3314 |
|
| 3315 |
/* Set the darwin specific attributes on TYPE. */ |
| 3316 |
void |
| 3317 |
darwin_set_default_type_attributes (tree type) |
| 3318 |
{ |
| 3319 |
if (darwin_ms_struct |
| 3320 |
&& TREE_CODE (type) == RECORD_TYPE) |
| 3321 |
TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"), |
| 3322 |
NULL_TREE, |
| 3323 |
TYPE_ATTRIBUTES (type)); |
| 3324 |
} |
| 3325 |
|
| 3326 |
/* True, iff we're generating code for loadable kernel extensions. */ |
| 3327 |
|
| 3328 |
bool |
| 3329 |
darwin_kextabi_p (void) { |
| 3330 |
return flag_apple_kext; |
| 3331 |
} |
| 3332 |
|
| 3333 |
void |
| 3334 |
darwin_override_options (void) |
| 3335 |
{ |
| 3336 |
/* Keep track of which (major) version we're generating code for. */ |
| 3337 |
if (darwin_macosx_version_min) |
| 3338 |
{ |
| 3339 |
if (strverscmp (darwin_macosx_version_min, "10.14") >= 0) |
| 3340 |
generating_for_darwin_version = 18; |
| 3341 |
else if (strverscmp (darwin_macosx_version_min, "10.8") >= 0) |
| 3342 |
generating_for_darwin_version = 12; |
| 3343 |
else if (strverscmp (darwin_macosx_version_min, "10.7") >= 0) |
| 3344 |
generating_for_darwin_version = 11; |
| 3345 |
else if (strverscmp (darwin_macosx_version_min, "10.6") >= 0) |
| 3346 |
generating_for_darwin_version = 10; |
| 3347 |
else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0) |
| 3348 |
generating_for_darwin_version = 9; |
| 3349 |
else if (strverscmp (darwin_macosx_version_min, "10.4") >= 0) |
| 3350 |
generating_for_darwin_version = 8; |
| 3351 |
|
| 3352 |
/* Earlier versions are not specifically accounted, until required. */ |
| 3353 |
} |
| 3354 |
|
| 3355 |
/* Some codegen needs to account for the capabilities of the target |
| 3356 |
linker. */ |
| 3357 |
if (darwin_target_linker) |
| 3358 |
{ |
| 3359 |
/* Older Darwin ld could not coalesce weak entities without them being |
| 3360 |
placed in special sections. */ |
| 3361 |
if (strverscmp (darwin_target_linker, MIN_LD64_NO_COAL_SECTS) < 0) |
| 3362 |
ld_uses_coal_sects = true; |
| 3363 |
|
| 3364 |
/* Some newer assemblers emit section start temp symbols for mod init |
| 3365 |
and term sections if there is no suitable symbol present already. |
| 3366 |
The temp symbols are linker visible and therefore appear in the |
| 3367 |
symbol tables. Since the temp symbol number can vary when debug is |
| 3368 |
enabled, that causes compare-debug fails. The solution is to provide |
| 3369 |
a stable linker-visible symbol. */ |
| 3370 |
if (strverscmp (darwin_target_linker, |
| 3371 |
MIN_LD64_INIT_TERM_START_LABELS) >= 0) |
| 3372 |
ld_init_term_start_labels = true; |
| 3373 |
} |
| 3374 |
|
| 3375 |
/* In principle, this should be c-family only. However, we really need to |
| 3376 |
set sensible defaults for LTO as well, since the section selection stuff |
| 3377 |
should check for correctness re. the ABI. TODO: check and provide the |
| 3378 |
flags (runtime & ABI) from the lto wrapper). */ |
| 3379 |
|
| 3380 |
/* At present, make a hard update to the runtime version based on the target |
| 3381 |
OS version. */ |
| 3382 |
if (flag_next_runtime) |
| 3383 |
{ |
| 3384 |
if (generating_for_darwin_version > 10) |
| 3385 |
flag_next_runtime = 100705; |
| 3386 |
else if (generating_for_darwin_version > 9) |
| 3387 |
flag_next_runtime = 100608; |
| 3388 |
else if (generating_for_darwin_version > 8) |
| 3389 |
flag_next_runtime = 100508; |
| 3390 |
else |
| 3391 |
flag_next_runtime = 100000; |
| 3392 |
} |
| 3393 |
|
| 3394 |
/* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */ |
| 3395 |
if (!OPTION_SET_P (flag_objc_abi)) |
| 3396 |
global_options.x_flag_objc_abi |
| 3397 |
= (!flag_next_runtime) |
| 3398 |
? 0 |
| 3399 |
: (TARGET_64BIT ? 2 |
| 3400 |
: (generating_for_darwin_version >= 9) ? 1 |
| 3401 |
: 0); |
| 3402 |
|
| 3403 |
if (OPTION_SET_P (flag_objc_abi) && flag_next_runtime) |
| 3404 |
{ |
| 3405 |
if (TARGET_64BIT && global_options.x_flag_objc_abi != 2) |
| 3406 |
/* The Objective-C family ABI 2 is the only valid version NeXT/m64. */ |
| 3407 |
error_at (UNKNOWN_LOCATION, |
| 3408 |
"%<-fobjc-abi-version%> 2 must be used for 64 bit targets" |
| 3409 |
" with %<-fnext-runtime%>"); |
| 3410 |
else if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2) |
| 3411 |
/* ABI versions 0 and 1 are the only valid versions NeXT/m32. */ |
| 3412 |
error_at (UNKNOWN_LOCATION, |
| 3413 |
"%<-fobjc-abi-version%> %d is not supported for 32 bit" |
| 3414 |
" targets with %<-fnext-runtime%>", |
| 3415 |
global_options.x_flag_objc_abi); |
| 3416 |
} |
| 3417 |
|
| 3418 |
if (!OPTION_SET_P (dwarf_version)) |
| 3419 |
{ |
| 3420 |
/* External toolchains based on LLVM or clang 7+ have support for |
| 3421 |
dwarf-4. */ |
| 3422 |
if ((dsymutil_version.kind == LLVM && dsymutil_version.major >= 7) |
| 3423 |
|| (dsymutil_version.kind == CLANG && dsymutil_version.major >= 700)) |
| 3424 |
dwarf_version = 4; |
| 3425 |
else if (dsymutil_version.kind == DWARFUTILS |
| 3426 |
&& dsymutil_version.major >= 121) |
| 3427 |
dwarf_version = 3; /* From XC 6.4. */ |
| 3428 |
else |
| 3429 |
dwarf_version = 2; /* Older cannot safely exceed dwarf-2. */ |
| 3430 |
} |
| 3431 |
|
| 3432 |
if (OPTION_SET_P (dwarf_split_debug_info) && dwarf_split_debug_info) |
| 3433 |
{ |
| 3434 |
inform (input_location, |
| 3435 |
"%<-gsplit-dwarf%> is not supported on this platform, ignored"); |
| 3436 |
dwarf_split_debug_info = 0; |
| 3437 |
OPTION_SET_P (dwarf_split_debug_info) = 0; |
| 3438 |
} |
| 3439 |
|
| 3440 |
/* Cases where dsymutil will exclude files with .macinfo sections; we are |
| 3441 |
better off forcing the debug level to 2 than completely excluding the |
| 3442 |
files. If strict dwarf is set, then emit the macinfo anyway. */ |
| 3443 |
if (debug_info_level == DINFO_LEVEL_VERBOSE |
| 3444 |
&& (!OPTION_SET_P (dwarf_strict) || dwarf_strict == 0) |
| 3445 |
&& ((dsymutil_version.kind == CLANG && dsymutil_version.major >= 1500) |
| 3446 |
|| (dsymutil_version.kind == LLVM && dsymutil_version.major >= 15))) |
| 3447 |
{ |
| 3448 |
inform (input_location, |
| 3449 |
"%<-g3%> is not supported by the debug linker in use (set to 2)"); |
| 3450 |
debug_info_level = DINFO_LEVEL_NORMAL; |
| 3451 |
} |
| 3452 |
|
| 3453 |
/* Limit DWARF to the chosen version, the linker and debug linker might not |
| 3454 |
be able to consume newer structures. */ |
| 3455 |
if (!OPTION_SET_P (dwarf_strict)) |
| 3456 |
dwarf_strict = 1; |
| 3457 |
|
| 3458 |
/* Do not allow unwind tables to be generated by default for m32. |
| 3459 |
fnon-call-exceptions will override this, regardless of what we do. */ |
| 3460 |
if (generating_for_darwin_version < 10 |
| 3461 |
&& !OPTION_SET_P (flag_asynchronous_unwind_tables) |
| 3462 |
&& !TARGET_64BIT) |
| 3463 |
global_options.x_flag_asynchronous_unwind_tables = 0; |
| 3464 |
|
| 3465 |
/* Disable -freorder-blocks-and-partition when unwind tables are being |
| 3466 |
emitted for Darwin < 9 (OSX 10.5). |
| 3467 |
The strategy is, "Unless the user has specifically set/unset an unwind |
| 3468 |
flag we will switch off -freorder-blocks-and-partition when unwind tables |
| 3469 |
will be generated". If the user specifically sets flags, we have to |
| 3470 |
assume they know why. */ |
| 3471 |
if (generating_for_darwin_version < 9 |
| 3472 |
&& OPTION_SET_P (flag_reorder_blocks_and_partition) |
| 3473 |
&& flag_reorder_blocks_and_partition |
| 3474 |
&& ((global_options.x_flag_exceptions /* User, c++, java */ |
| 3475 |
&& !OPTION_SET_P (flag_exceptions)) /* User specified... */ |
| 3476 |
|| (global_options.x_flag_unwind_tables |
| 3477 |
&& !OPTION_SET_P (flag_unwind_tables)) |
| 3478 |
|| (global_options.x_flag_non_call_exceptions |
| 3479 |
&& !OPTION_SET_P (flag_non_call_exceptions)) |
| 3480 |
|| (global_options.x_flag_asynchronous_unwind_tables |
| 3481 |
&& !OPTION_SET_P (flag_asynchronous_unwind_tables)))) |
| 3482 |
{ |
| 3483 |
inform (input_location, |
| 3484 |
"%<-freorder-blocks-and-partition%> does not work with " |
| 3485 |
"exceptions on this architecture"); |
| 3486 |
flag_reorder_blocks_and_partition = 0; |
| 3487 |
flag_reorder_blocks = 1; |
| 3488 |
} |
| 3489 |
|
| 3490 |
/* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only |
| 3491 |
one valid choice of exception scheme for each runtime. */ |
| 3492 |
if (!OPTION_SET_P (flag_objc_sjlj_exceptions)) |
| 3493 |
global_options.x_flag_objc_sjlj_exceptions = |
| 3494 |
flag_next_runtime && !TARGET_64BIT; |
| 3495 |
|
| 3496 |
/* FIXME: and this could be eliminated then too. */ |
| 3497 |
if (!OPTION_SET_P (flag_exceptions) |
| 3498 |
&& flag_objc_exceptions |
| 3499 |
&& TARGET_64BIT) |
| 3500 |
flag_exceptions = 1; |
| 3501 |
|
| 3502 |
if (flag_mkernel || flag_apple_kext) |
| 3503 |
{ |
| 3504 |
/* -mkernel implies -fapple-kext for C++ */ |
| 3505 |
if (lang_GNU_CXX ()) |
| 3506 |
flag_apple_kext = 1; |
| 3507 |
|
| 3508 |
flag_no_common = 1; |
| 3509 |
|
| 3510 |
/* No EH in kexts. */ |
| 3511 |
flag_exceptions = 0; |
| 3512 |
/* No -fnon-call-exceptions data in kexts. */ |
| 3513 |
flag_non_call_exceptions = 0; |
| 3514 |
/* so no tables either.. */ |
| 3515 |
flag_unwind_tables = 0; |
| 3516 |
flag_asynchronous_unwind_tables = 0; |
| 3517 |
} |
| 3518 |
|
| 3519 |
if (flag_var_tracking_uninit == 0 |
| 3520 |
&& generating_for_darwin_version >= 9 |
| 3521 |
&& (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE) |
| 3522 |
: (debug_info_level >= DINFO_LEVEL_NORMAL)) |
| 3523 |
&& dwarf_debuginfo_p ()) |
| 3524 |
flag_var_tracking_uninit = flag_var_tracking; |
| 3525 |
|
| 3526 |
if (OPTION_SET_P (flag_pie) && flag_pie) |
| 3527 |
{ |
| 3528 |
/* This is a little complicated, to match Xcode tools. |
| 3529 |
For Darwin, PIE requires PIC codegen, but otherwise is only a link- |
| 3530 |
time change. For almost all Darwin, we do not report __PIE__; the |
| 3531 |
exception is Darwin12-17 and for 32b only. */ |
| 3532 |
flag_pie = generating_for_darwin_version >= 12 && !TARGET_64BIT ? 2 : 0; |
| 3533 |
flag_pic = 2; /* We always set this. */ |
| 3534 |
} |
| 3535 |
|
| 3536 |
/* Final check on PIC options. */ |
| 3537 |
if (MACHO_DYNAMIC_NO_PIC_P) |
| 3538 |
{ |
| 3539 |
if (flag_pic) |
| 3540 |
warning_at (UNKNOWN_LOCATION, 0, |
| 3541 |
"%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>," |
| 3542 |
" %<-fpie%> or %<-fPIE%>"); |
| 3543 |
flag_pic = 0; |
| 3544 |
} |
| 3545 |
else if (flag_pic == 1 |
| 3546 |
|| (flag_pic == 0 && !(flag_mkernel || flag_apple_kext))) |
| 3547 |
{ |
| 3548 |
/* Darwin's -fpic is -fPIC. |
| 3549 |
We only support "static" code in the kernel and kernel exts. */ |
| 3550 |
flag_pic = 2; |
| 3551 |
} |
| 3552 |
|
| 3553 |
/* Linkers >= ld64-62.1 (at least) are capable of making the necessary PIC |
| 3554 |
indirections and we no longer need to emit pic symbol stubs. |
| 3555 |
However, if we are generating code for earlier ones (or for use in the |
| 3556 |
kernel) the stubs might still be required, and this will be set true. |
| 3557 |
If the user sets it on or off - then that takes precedence. |
| 3558 |
|
| 3559 |
Linkers that don't need stubs, don't need the EH symbol markers either. |
| 3560 |
*/ |
| 3561 |
|
| 3562 |
if (!OPTION_SET_P (darwin_symbol_stubs)) |
| 3563 |
{ |
| 3564 |
if (darwin_target_linker) |
| 3565 |
{ |
| 3566 |
if (strverscmp (darwin_target_linker, MIN_LD64_OMIT_STUBS) < 0) |
| 3567 |
{ |
| 3568 |
darwin_symbol_stubs = true; |
| 3569 |
ld_needs_eh_markers = true; |
| 3570 |
} |
| 3571 |
} |
| 3572 |
else if (generating_for_darwin_version < 9) |
| 3573 |
{ |
| 3574 |
/* If we don't know the linker version and we're targeting an old |
| 3575 |
system, we know no better than to assume the use of an earlier |
| 3576 |
linker. */ |
| 3577 |
darwin_symbol_stubs = true; |
| 3578 |
ld_needs_eh_markers = true; |
| 3579 |
} |
| 3580 |
} |
| 3581 |
else if (DARWIN_X86 && darwin_symbol_stubs && TARGET_64BIT) |
| 3582 |
{ |
| 3583 |
inform (input_location, |
| 3584 |
"%<-mpic-symbol-stubs%> is not required for 64-bit code " |
| 3585 |
"(ignored)"); |
| 3586 |
darwin_symbol_stubs = false; |
| 3587 |
} |
| 3588 |
|
| 3589 |
if (generating_for_darwin_version >= 9) |
| 3590 |
/* Later systems can support aligned common. */ |
| 3591 |
emit_aligned_common = true; |
| 3592 |
|
| 3593 |
/* The c_dialect...() macros are not available to us here. */ |
| 3594 |
darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0); |
| 3595 |
} |
| 3596 |
|
| 3597 |
#if DARWIN_PPC |
| 3598 |
/* Add $LDBL128 suffix to long double builtins for ppc darwin. */ |
| 3599 |
|
| 3600 |
static void |
| 3601 |
darwin_patch_builtin (enum built_in_function fncode) |
| 3602 |
{ |
| 3603 |
tree fn = builtin_decl_explicit (fncode); |
| 3604 |
tree sym; |
| 3605 |
char *newname; |
| 3606 |
|
| 3607 |
if (!fn) |
| 3608 |
return; |
| 3609 |
|
| 3610 |
sym = DECL_ASSEMBLER_NAME (fn); |
| 3611 |
newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL)); |
| 3612 |
|
| 3613 |
set_user_assembler_name (fn, newname); |
| 3614 |
|
| 3615 |
fn = builtin_decl_implicit (fncode); |
| 3616 |
if (fn) |
| 3617 |
set_user_assembler_name (fn, newname); |
| 3618 |
} |
| 3619 |
|
| 3620 |
void |
| 3621 |
darwin_patch_builtins (void) |
| 3622 |
{ |
| 3623 |
if (TYPE_PRECISION (long_double_type_node) != 128) |
| 3624 |
return; |
| 3625 |
|
| 3626 |
#define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode); |
| 3627 |
#define PATCH_BUILTIN_NO64(fncode) \ |
| 3628 |
if (!TARGET_64BIT) \ |
| 3629 |
darwin_patch_builtin (fncode); |
| 3630 |
#define PATCH_BUILTIN_VARIADIC(fncode) \ |
| 3631 |
if (!TARGET_64BIT \ |
| 3632 |
&& (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \ |
| 3633 |
darwin_patch_builtin (fncode); |
| 3634 |
#include "darwin-ppc-ldouble-patch.def" |
| 3635 |
#undef PATCH_BUILTIN |
| 3636 |
#undef PATCH_BUILTIN_NO64 |
| 3637 |
#undef PATCH_BUILTIN_VARIADIC |
| 3638 |
} |
| 3639 |
#endif |
| 3640 |
|
| 3641 |
void |
| 3642 |
darwin_rename_builtins (void) |
| 3643 |
{ |
| 3644 |
} |
| 3645 |
|
| 3646 |
/* Implementation for the TARGET_LIBC_HAS_FUNCTION hook. */ |
| 3647 |
|
| 3648 |
bool |
| 3649 |
darwin_libc_has_function (enum function_class fn_class, |
| 3650 |
tree type ATTRIBUTE_UNUSED) |
| 3651 |
{ |
| 3652 |
if (fn_class == function_sincos && darwin_macosx_version_min) |
| 3653 |
return (strverscmp (darwin_macosx_version_min, "10.9") >= 0); |
| 3654 |
#if DARWIN_PPC && SUPPORT_DARWIN_LEGACY |
| 3655 |
if (fn_class == function_c99_math_complex |
| 3656 |
|| fn_class == function_c99_misc) |
| 3657 |
return (TARGET_64BIT |
| 3658 |
|| (darwin_macosx_version_min && |
| 3659 |
strverscmp (darwin_macosx_version_min, "10.3") >= 0)); |
| 3660 |
#endif |
| 3661 |
return default_libc_has_function (fn_class, type); |
| 3662 |
} |
| 3663 |
|
| 3664 |
/* CFStrings implementation. */ |
| 3665 |
static GTY(()) tree cfstring_class_reference = NULL_TREE; |
| 3666 |
static GTY(()) tree cfstring_type_node = NULL_TREE; |
| 3667 |
static GTY(()) tree ccfstring_type_node = NULL_TREE; |
| 3668 |
static GTY(()) tree pccfstring_type_node = NULL_TREE; |
| 3669 |
static GTY(()) tree pcint_type_node = NULL_TREE; |
| 3670 |
static GTY(()) tree pcchar_type_node = NULL_TREE; |
| 3671 |
|
| 3672 |
static enum built_in_function darwin_builtin_cfstring; |
| 3673 |
|
| 3674 |
/* Store all constructed constant CFStrings in a hash table so that |
| 3675 |
they get uniqued properly. */ |
| 3676 |
|
| 3677 |
typedef struct GTY ((for_user)) cfstring_descriptor { |
| 3678 |
/* The string literal. */ |
| 3679 |
tree literal; |
| 3680 |
/* The resulting constant CFString. */ |
| 3681 |
tree ccf_str; |
| 3682 |
} cfstring_descriptor; |
| 3683 |
|
| 3684 |
struct cfstring_hasher : ggc_ptr_hash<cfstring_descriptor> |
| 3685 |
{ |
| 3686 |
static hashval_t hash (cfstring_descriptor *); |
| 3687 |
static bool equal (cfstring_descriptor *, cfstring_descriptor *); |
| 3688 |
}; |
| 3689 |
|
| 3690 |
static GTY (()) hash_table<cfstring_hasher> *cfstring_htab; |
| 3691 |
|
| 3692 |
static tree |
| 3693 |
add_builtin_field_decl (tree type, const char *name, tree **chain) |
| 3694 |
{ |
| 3695 |
tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL, |
| 3696 |
get_identifier (name), type); |
| 3697 |
|
| 3698 |
if (*chain != NULL) |
| 3699 |
**chain = field; |
| 3700 |
*chain = &DECL_CHAIN (field); |
| 3701 |
|
| 3702 |
return field; |
| 3703 |
} |
| 3704 |
|
| 3705 |
tree |
| 3706 |
darwin_init_cfstring_builtins (unsigned builtin_cfstring) |
| 3707 |
{ |
| 3708 |
tree cfsfun, fields, pccfstring_ftype_pcchar; |
| 3709 |
tree *chain = NULL; |
| 3710 |
|
| 3711 |
darwin_builtin_cfstring = |
| 3712 |
(enum built_in_function) builtin_cfstring; |
| 3713 |
|
| 3714 |
/* struct __builtin_CFString { |
| 3715 |
const int *isa; (will point at |
| 3716 |
int flags; __CFConstantStringClassReference) |
| 3717 |
const char *str; |
| 3718 |
long length; |
| 3719 |
}; */ |
| 3720 |
|
| 3721 |
pcint_type_node = build_pointer_type |
| 3722 |
(build_qualified_type (integer_type_node, TYPE_QUAL_CONST)); |
| 3723 |
|
| 3724 |
pcchar_type_node = build_pointer_type |
| 3725 |
(build_qualified_type (char_type_node, TYPE_QUAL_CONST)); |
| 3726 |
|
| 3727 |
cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE); |
| 3728 |
|
| 3729 |
/* Have to build backwards for finish struct. */ |
| 3730 |
fields = add_builtin_field_decl (long_integer_type_node, "length", &chain); |
| 3731 |
add_builtin_field_decl (pcchar_type_node, "str", &chain); |
| 3732 |
add_builtin_field_decl (integer_type_node, "flags", &chain); |
| 3733 |
add_builtin_field_decl (pcint_type_node, "isa", &chain); |
| 3734 |
finish_builtin_struct (cfstring_type_node, "__builtin_CFString", |
| 3735 |
fields, NULL_TREE); |
| 3736 |
|
| 3737 |
/* const struct __builtin_CFstring * |
| 3738 |
__builtin___CFStringMakeConstantString (const char *); */ |
| 3739 |
|
| 3740 |
ccfstring_type_node = build_qualified_type |
| 3741 |
(cfstring_type_node, TYPE_QUAL_CONST); |
| 3742 |
pccfstring_type_node = build_pointer_type (ccfstring_type_node); |
| 3743 |
pccfstring_ftype_pcchar = build_function_type_list |
| 3744 |
(pccfstring_type_node, pcchar_type_node, NULL_TREE); |
| 3745 |
|
| 3746 |
cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL, |
| 3747 |
get_identifier ("__builtin___CFStringMakeConstantString"), |
| 3748 |
pccfstring_ftype_pcchar); |
| 3749 |
|
| 3750 |
TREE_PUBLIC (cfsfun) = 1; |
| 3751 |
DECL_EXTERNAL (cfsfun) = 1; |
| 3752 |
DECL_ARTIFICIAL (cfsfun) = 1; |
| 3753 |
/* Make a lang-specific section - dup_lang_specific_decl makes a new node |
| 3754 |
in place of the existing, which may be NULL. */ |
| 3755 |
DECL_LANG_SPECIFIC (cfsfun) = NULL; |
| 3756 |
lang_hooks.dup_lang_specific_decl (cfsfun); |
| 3757 |
set_decl_built_in_function (cfsfun, BUILT_IN_MD, darwin_builtin_cfstring); |
| 3758 |
lang_hooks.builtin_function (cfsfun); |
| 3759 |
|
| 3760 |
/* extern int __CFConstantStringClassReference[]; */ |
| 3761 |
cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL, |
| 3762 |
get_identifier ("__CFConstantStringClassReference"), |
| 3763 |
build_array_type (integer_type_node, NULL_TREE)); |
| 3764 |
|
| 3765 |
TREE_PUBLIC (cfstring_class_reference) = 1; |
| 3766 |
DECL_ARTIFICIAL (cfstring_class_reference) = 1; |
| 3767 |
lang_hooks.decls.pushdecl (cfstring_class_reference); |
| 3768 |
DECL_EXTERNAL (cfstring_class_reference) = 1; |
| 3769 |
rest_of_decl_compilation (cfstring_class_reference, 0, 0); |
| 3770 |
|
| 3771 |
/* Initialize the hash table used to hold the constant CFString objects. */ |
| 3772 |
cfstring_htab = hash_table<cfstring_hasher>::create_ggc (31); |
| 3773 |
|
| 3774 |
return cfstring_type_node; |
| 3775 |
} |
| 3776 |
|
| 3777 |
tree |
| 3778 |
darwin_fold_builtin (tree fndecl, int n_args, tree *argp, |
| 3779 |
bool ARG_UNUSED (ignore)) |
| 3780 |
{ |
| 3781 |
int fcode = DECL_MD_FUNCTION_CODE (fndecl); |
| 3782 |
|
| 3783 |
if (fcode == darwin_builtin_cfstring) |
| 3784 |
{ |
| 3785 |
if (!darwin_constant_cfstrings) |
| 3786 |
{ |
| 3787 |
error ("built-in function %qD requires the" |
| 3788 |
" %<-mconstant-cfstrings%> flag", fndecl); |
| 3789 |
return error_mark_node; |
| 3790 |
} |
| 3791 |
|
| 3792 |
if (n_args != 1) |
| 3793 |
{ |
| 3794 |
error ("built-in function %qD takes one argument only", fndecl); |
| 3795 |
return error_mark_node; |
| 3796 |
} |
| 3797 |
|
| 3798 |
return darwin_build_constant_cfstring (*argp); |
| 3799 |
} |
| 3800 |
|
| 3801 |
return NULL_TREE; |
| 3802 |
} |
| 3803 |
|
| 3804 |
hashval_t |
| 3805 |
cfstring_hasher::hash (cfstring_descriptor *ptr) |
| 3806 |
{ |
| 3807 |
tree str = ptr->literal; |
| 3808 |
const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str); |
| 3809 |
int i, len = TREE_STRING_LENGTH (str); |
| 3810 |
hashval_t h = len; |
| 3811 |
|
| 3812 |
for (i = 0; i < len; i++) |
| 3813 |
h = ((h * 613) + p[i]); |
| 3814 |
|
| 3815 |
return h; |
| 3816 |
} |
| 3817 |
|
| 3818 |
bool |
| 3819 |
cfstring_hasher::equal (cfstring_descriptor *ptr1, cfstring_descriptor *ptr2) |
| 3820 |
{ |
| 3821 |
tree str1 = ptr1->literal; |
| 3822 |
tree str2 = ptr2->literal; |
| 3823 |
int len1 = TREE_STRING_LENGTH (str1); |
| 3824 |
|
| 3825 |
return (len1 == TREE_STRING_LENGTH (str2) |
| 3826 |
&& !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2), |
| 3827 |
len1)); |
| 3828 |
} |
| 3829 |
|
| 3830 |
tree |
| 3831 |
darwin_build_constant_cfstring (tree str) |
| 3832 |
{ |
| 3833 |
struct cfstring_descriptor *desc, key; |
| 3834 |
tree addr; |
| 3835 |
|
| 3836 |
if (!str) |
| 3837 |
{ |
| 3838 |
error ("CFString literal is missing"); |
| 3839 |
return error_mark_node; |
| 3840 |
} |
| 3841 |
|
| 3842 |
STRIP_NOPS (str); |
| 3843 |
|
| 3844 |
if (TREE_CODE (str) == ADDR_EXPR) |
| 3845 |
str = TREE_OPERAND (str, 0); |
| 3846 |
|
| 3847 |
if (TREE_CODE (str) != STRING_CST) |
| 3848 |
{ |
| 3849 |
error ("CFString literal expression is not a string constant"); |
| 3850 |
return error_mark_node; |
| 3851 |
} |
| 3852 |
|
| 3853 |
/* Perhaps we already constructed a constant CFString just like this one? */ |
| 3854 |
key.literal = str; |
| 3855 |
cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT); |
| 3856 |
desc = *loc; |
| 3857 |
|
| 3858 |
if (!desc) |
| 3859 |
{ |
| 3860 |
tree var, constructor, field; |
| 3861 |
vec<constructor_elt, va_gc> *v = NULL; |
| 3862 |
int length = TREE_STRING_LENGTH (str) - 1; |
| 3863 |
|
| 3864 |
if (darwin_warn_nonportable_cfstrings) |
| 3865 |
{ |
| 3866 |
const char *s = TREE_STRING_POINTER (str); |
| 3867 |
int l = 0; |
| 3868 |
|
| 3869 |
for (l = 0; l < length; l++) |
| 3870 |
if (!s[l] || !isascii (s[l])) |
| 3871 |
{ |
| 3872 |
warning (darwin_warn_nonportable_cfstrings, |
| 3873 |
s[l] ? G_("non-ASCII character in CFString literal") |
| 3874 |
: G_("embedded NUL in CFString literal")); |
| 3875 |
break; |
| 3876 |
} |
| 3877 |
} |
| 3878 |
|
| 3879 |
*loc = desc = ggc_cleared_alloc<cfstring_descriptor> (); |
| 3880 |
desc->literal = str; |
| 3881 |
|
| 3882 |
/* isa *. */ |
| 3883 |
field = TYPE_FIELDS (ccfstring_type_node); |
| 3884 |
CONSTRUCTOR_APPEND_ELT(v, NULL_TREE, |
| 3885 |
build1 (ADDR_EXPR, TREE_TYPE (field), |
| 3886 |
cfstring_class_reference)); |
| 3887 |
/* flags */ |
| 3888 |
field = DECL_CHAIN (field); |
| 3889 |
CONSTRUCTOR_APPEND_ELT(v, NULL_TREE, |
| 3890 |
build_int_cst (TREE_TYPE (field), 0x000007c8)); |
| 3891 |
/* string *. */ |
| 3892 |
field = DECL_CHAIN (field); |
| 3893 |
CONSTRUCTOR_APPEND_ELT(v, NULL_TREE, |
| 3894 |
build1 (ADDR_EXPR, TREE_TYPE (field), str)); |
| 3895 |
/* length */ |
| 3896 |
field = DECL_CHAIN (field); |
| 3897 |
CONSTRUCTOR_APPEND_ELT(v, NULL_TREE, |
| 3898 |
build_int_cst (TREE_TYPE (field), length)); |
| 3899 |
|
| 3900 |
constructor = build_constructor (ccfstring_type_node, v); |
| 3901 |
TREE_READONLY (constructor) = true; |
| 3902 |
TREE_CONSTANT (constructor) = true; |
| 3903 |
TREE_STATIC (constructor) = true; |
| 3904 |
|
| 3905 |
/* This file is being built without any knowledge of C++ tree accessors; |
| 3906 |
hence, we shall use the generic accessor to set TREE_HAS_CONSTRUCTOR. |
| 3907 |
??? Is this actually used any more? */ |
| 3908 |
if (darwin_running_cxx) |
| 3909 |
TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */ |
| 3910 |
|
| 3911 |
/* Create an anonymous global variable for this CFString. */ |
| 3912 |
var = build_decl (input_location, VAR_DECL, |
| 3913 |
NULL, TREE_TYPE (constructor)); |
| 3914 |
char *name = xasprintf ("__anon_cfstring.%u", DECL_UID (var)); |
| 3915 |
DECL_NAME (var) = get_identifier (name); |
| 3916 |
free (name); |
| 3917 |
DECL_ARTIFICIAL (var) = true; |
| 3918 |
TREE_STATIC (var) = true; |
| 3919 |
TREE_READONLY (var) = true; |
| 3920 |
TREE_CONSTANT (var) = true; |
| 3921 |
DECL_INITIAL (var) = constructor; |
| 3922 |
/* global namespace. */ |
| 3923 |
DECL_CONTEXT (var) = NULL_TREE; |
| 3924 |
DECL_INITIAL (var) = constructor; |
| 3925 |
DECL_USER_ALIGN (var) = 1; |
| 3926 |
lang_hooks.decls.pushdecl (var); |
| 3927 |
rest_of_decl_compilation (var, 1, 0); |
| 3928 |
desc->ccf_str = var; |
| 3929 |
} |
| 3930 |
|
| 3931 |
addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->ccf_str); |
| 3932 |
TREE_CONSTANT (addr) = 1; |
| 3933 |
|
| 3934 |
return addr; |
| 3935 |
} |
| 3936 |
|
| 3937 |
bool |
| 3938 |
darwin_cfstring_p (tree str) |
| 3939 |
{ |
| 3940 |
struct cfstring_descriptor key; |
| 3941 |
|
| 3942 |
if (!str) |
| 3943 |
return false; |
| 3944 |
|
| 3945 |
STRIP_NOPS (str); |
| 3946 |
|
| 3947 |
if (TREE_CODE (str) == ADDR_EXPR) |
| 3948 |
str = TREE_OPERAND (str, 0); |
| 3949 |
|
| 3950 |
if (TREE_CODE (str) != STRING_CST) |
| 3951 |
return false; |
| 3952 |
|
| 3953 |
key.literal = str; |
| 3954 |
cfstring_descriptor **loc = cfstring_htab->find_slot (&key, NO_INSERT); |
| 3955 |
|
| 3956 |
if (loc) |
| 3957 |
return true; |
| 3958 |
|
| 3959 |
return false; |
| 3960 |
} |
| 3961 |
|
| 3962 |
void |
| 3963 |
darwin_enter_string_into_cfstring_table (tree str) |
| 3964 |
{ |
| 3965 |
struct cfstring_descriptor key; |
| 3966 |
|
| 3967 |
key.literal = str; |
| 3968 |
cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT); |
| 3969 |
|
| 3970 |
if (!*loc) |
| 3971 |
{ |
| 3972 |
*loc = ggc_cleared_alloc<cfstring_descriptor> (); |
| 3973 |
((struct cfstring_descriptor *)*loc)->literal = str; |
| 3974 |
} |
| 3975 |
} |
| 3976 |
|
| 3977 |
/* Choose named function section based on its frequency. */ |
| 3978 |
|
| 3979 |
section * |
| 3980 |
darwin_function_section (tree decl, enum node_frequency freq, |
| 3981 |
bool startup, bool exit) |
| 3982 |
{ |
| 3983 |
/* Decide if we need to put this in a coalescable section. */ |
| 3984 |
bool weak = (decl |
| 3985 |
&& DECL_WEAK (decl) |
| 3986 |
&& (!DECL_ATTRIBUTES (decl) |
| 3987 |
|| !lookup_attribute ("weak_import", |
| 3988 |
DECL_ATTRIBUTES (decl)))); |
| 3989 |
|
| 3990 |
bool use_coal = weak && ld_uses_coal_sects; |
| 3991 |
/* If there is a specified section name, we should not be trying to |
| 3992 |
override. */ |
| 3993 |
if (decl && DECL_SECTION_NAME (decl) != NULL) |
| 3994 |
return get_named_section (decl, NULL, 0); |
| 3995 |
|
| 3996 |
/* We always put unlikely executed stuff in the cold section; we have to put |
| 3997 |
this ahead of the global init section, since partitioning within a section |
| 3998 |
breaks some assumptions made in the DWARF handling. */ |
| 3999 |
if (freq == NODE_FREQUENCY_UNLIKELY_EXECUTED) |
| 4000 |
return (use_coal) ? darwin_sections[text_cold_coal_section] |
| 4001 |
: darwin_sections[text_cold_section]; |
| 4002 |
|
| 4003 |
/* Intercept functions in global init; these are placed in separate sections. |
| 4004 |
FIXME: there should be some neater way to do this, FIXME we should be able |
| 4005 |
to partition within a section. */ |
| 4006 |
if (DECL_NAME (decl) |
| 4007 |
&& (startswith (IDENTIFIER_POINTER (DECL_NAME (decl)), "_GLOBAL__sub_I") |
| 4008 |
|| startswith (IDENTIFIER_POINTER (DECL_NAME (decl)), |
| 4009 |
"__static_initialization_and_destruction"))) |
| 4010 |
return darwin_sections[static_init_section]; |
| 4011 |
|
| 4012 |
/* If we have LTO *and* feedback information, then let LTO handle |
| 4013 |
the function ordering, it makes a better job (for normal, hot, |
| 4014 |
startup and exit - hence the bailout for cold above). */ |
| 4015 |
if (in_lto_p && flag_profile_values) |
| 4016 |
goto default_function_sections; |
| 4017 |
|
| 4018 |
/* Non-cold startup code should go to startup subsection. */ |
| 4019 |
if (startup) |
| 4020 |
return (use_coal) ? darwin_sections[text_startup_coal_section] |
| 4021 |
: darwin_sections[text_startup_section]; |
| 4022 |
|
| 4023 |
/* Similarly for exit. */ |
| 4024 |
if (exit) |
| 4025 |
return (use_coal) ? darwin_sections[text_exit_coal_section] |
| 4026 |
: darwin_sections[text_exit_section]; |
| 4027 |
|
| 4028 |
/* Place hot code. */ |
| 4029 |
if (freq == NODE_FREQUENCY_HOT) |
| 4030 |
return (use_coal) ? darwin_sections[text_hot_coal_section] |
| 4031 |
: darwin_sections[text_hot_section]; |
| 4032 |
|
| 4033 |
/* Otherwise, default to the 'normal' non-reordered sections. */ |
| 4034 |
default_function_sections: |
| 4035 |
return (use_coal) ? darwin_sections[text_coal_section] |
| 4036 |
: text_section; |
| 4037 |
} |
| 4038 |
|
| 4039 |
#include "gt-darwin.h" |