OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [config/] [darwin.c] - Blame information for rev 867

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.