View | Details | Raw Unified | Return to bug 48108 | Differences between
and this patch

Collapse All | Expand All | Context: (Patch / File /
)

(-)libiberty/simple-object-mach-o.c (-45 / +160 lines)
 Lines 170-178   struct mach_o_section_64 Link Here 
170
170
171
#define MACH_O_NAME_LEN (16)
171
#define MACH_O_NAME_LEN (16)
172
172
173
/* A GNU specific extension for long section names.  */
173
/* A GNU-specific extension to wrap multiple subsections using three 
174
   mach-o sections within a given segment.  The section '__subsect_sects'
175
   is subdivided according to the index '__subsect_index' and each sub
176
   sect is named according to the names supplied in '__subsect_names'.  */
174
177
175
#define GNU_SECTION_NAMES "__section_names"
178
#define GNU_SECTION_SECTS "__subsect_sects"
179
#define GNU_SECTION_INDEX "__subsect_index"
180
#define GNU_SECTION_NAMES "__subsect_names"
176
181
177
/* Private data for an simple_object_read.  */
182
/* Private data for an simple_object_read.  */
178
183
 Lines 214-221   struct simple_object_mach_o_attributes Link Here 
214
  unsigned int reserved;
219
  unsigned int reserved;
215
};
220
};
216
221
217
/* See if we have a Mach-O file.  */
222
/* See if we have a Mach-O MH_OBJECT file:
218
223
224
   A standard MH_OBJECT (from as) will have three load commands:
225
   0 - LC_SEGMENT/LC_SEGMENT64
226
   1 - LC_SYMTAB
227
   2 - LC_DYSYMTAB
228
   
229
   The LC_SEGMENT/LC_SEGMENT64 will introduce a single anonymous segment
230
   containing all the sections.
231
   
232
   Files written by simple-object will have only the segment command
233
   (no symbol tables).  */
234
219
static void *
235
static void *
220
simple_object_mach_o_match (
236
simple_object_mach_o_match (
221
    unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
237
    unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
 Lines 258-275   simple_object_mach_o_match ( Link Here 
258
    }
274
    }
259
#endif
275
#endif
260
276
261
  /* We require the user to provide a segment name.  This is
277
  /* Although a standard MH_OBJECT has a single anonymous segment, we allow
262
     unfortunate but I don't see any good choices here.  */
278
     a segment name to be specified - but don't act on it at this stage.  */
263
279
  if (segment_name && strlen (segment_name) > MACH_O_NAME_LEN)
264
  if (segment_name == NULL)
265
    {
280
    {
266
      *errmsg = "Mach-O file found but no segment name specified";
267
      *err = 0;
268
      return NULL;
269
    }
270
271
  if (strlen (segment_name) > MACH_O_NAME_LEN)
272
    {
273
      *errmsg = "Mach-O segment name too long";
281
      *errmsg = "Mach-O segment name too long";
274
      *err = 0;
282
      *err = 0;
275
      return NULL;
283
      return NULL;
 Lines 294-306   simple_object_mach_o_match ( Link Here 
294
  filetype = (*fetch_32) (b + offsetof (struct mach_o_header_32, filetype));
302
  filetype = (*fetch_32) (b + offsetof (struct mach_o_header_32, filetype));
295
  if (filetype != MACH_O_MH_OBJECT)
303
  if (filetype != MACH_O_MH_OBJECT)
296
    {
304
    {
297
      *errmsg = "Mach-O file is not object file";
305
      *errmsg = "Mach-O file is not an MH_OBJECT file";
298
      *err = 0;
306
      *err = 0;
299
      return NULL;
307
      return NULL;
300
    }
308
    }
301
309
302
  omr = XNEW (struct simple_object_mach_o_read);
310
  omr = XNEW (struct simple_object_mach_o_read);
303
  omr->segment_name = xstrdup (segment_name);
311
  if (segment_name)
312
    omr->segment_name = xstrdup (segment_name);
304
  omr->magic = magic;
313
  omr->magic = magic;
305
  omr->is_big_endian = is_big_endian;
314
  omr->is_big_endian = is_big_endian;
306
  omr->cputype = (*fetch_32) (b + offsetof (struct mach_o_header_32, cputype));
315
  omr->cputype = (*fetch_32) (b + offsetof (struct mach_o_header_32, cputype));
 Lines 356-364   simple_object_mach_o_section_info (int is_big_endi Link Here 
356
    }
365
    }
357
}
366
}
358
367
359
/* Handle a segment in a Mach-O file.  Return 1 if we should continue,
368
/* Handle a segment in a Mach-O file.
360
   0 if the caller should return.  */
361
369
370
   This will callback to the function pfn for each section found in the
371
   segment named in 
372
   
373
   We implement a sub-section scheme (which will, in principle, work
374
   with any segment).  At present, likely to be meaningless except for
375
   __GNU_LTO.  
376
   
377
   Return 1 if we should continue, 0 if the caller should return.  */
378
362
static int
379
static int
363
simple_object_mach_o_segment (simple_object_read *sobj, off_t offset,
380
simple_object_mach_o_segment (simple_object_read *sobj, off_t offset,
364
			      const unsigned char *segbuf,
381
			      const unsigned char *segbuf,
 Lines 376-386   simple_object_mach_o_segment (simple_object_read * Link Here 
376
  size_t segname_offset;
393
  size_t segname_offset;
377
  size_t sectname_offset;
394
  size_t sectname_offset;
378
  unsigned int nsects;
395
  unsigned int nsects;
379
  unsigned char *secdata;
396
  unsigned char *secdata = NULL;
380
  unsigned int i;
397
  unsigned int i;
381
  unsigned int strtab_index;
398
  int strtab_index, index_index, sections_index;
382
  char *strtab;
399
  char *strtab = NULL;
383
  size_t strtab_size;
400
  size_t strtab_size = 0;
401
  unsigned char *index = NULL;
402
  size_t index_size;
403
  unsigned int n_lto_sects = 0;
404
  size_t lto_sect_size = 0;
405
  off_t lto_sect_offset = (off_t)0;
384
406
385
  fetch_32 = (omr->is_big_endian
407
  fetch_32 = (omr->is_big_endian
386
	      ? simple_object_fetch_big_32
408
	      ? simple_object_fetch_big_32
 Lines 417-445   simple_object_mach_o_segment (simple_object_read * Link Here 
417
      return 0;
439
      return 0;
418
    }
440
    }
419
441
420
  /* Scan for a __section_names section.  This is in effect a GNU
442
  /* If the user has set a segment name to be processed for GNU sub-sections
421
     extension that permits section names longer than 16 chars.  */
443
     then scan for the three relevant sections.  */
444
  strtab_index = index_index = sections_index = -1;
445
  if (omr->segment_name && strlen (omr->segment_name) > 0)
446
    for (i = 0; i < nsects; ++i)
447
      {
448
	size_t nameoff;
422
449
423
  for (i = 0; i < nsects; ++i)
450
	nameoff = i * sechdrsize + segname_offset;
424
    {
451
	if (strcmp ((char *) secdata + nameoff, omr->segment_name) != 0)
425
      size_t nameoff;
452
	  continue;
426
453
427
      nameoff = i * sechdrsize + segname_offset;
454
	nameoff = i * sechdrsize + sectname_offset;
428
      if (strcmp ((char *) secdata + nameoff, omr->segment_name) != 0)
455
	if (strcmp ((char *) secdata + nameoff, GNU_SECTION_NAMES) == 0)
429
	continue;
456
	  strtab_index = i;
430
      nameoff = i * sechdrsize + sectname_offset;
457
	else if (strcmp ((char *) secdata + nameoff, GNU_SECTION_INDEX) == 0)
431
      if (strcmp ((char *) secdata + nameoff, GNU_SECTION_NAMES) == 0)
458
	  index_index = i;
432
	break;
459
	else if (strcmp ((char *) secdata + nameoff, GNU_SECTION_SECTS) == 0)
433
    }
460
	  sections_index = i;
461
      }
434
462
435
  strtab_index = i;
463
  /* Support code compiled with the orginal LTO implementation of one mach-o 
436
  if (strtab_index >= nsects)
464
     section for each LTO section.  */
465
  if (strtab_index >= 0)
437
    {
466
    {
438
      strtab = NULL;
439
      strtab_size = 0;
440
    }
441
  else
442
    {
443
      off_t strtab_offset;
467
      off_t strtab_offset;
444
468
445
      simple_object_mach_o_section_info (omr->is_big_endian, is_32,
469
      simple_object_mach_o_section_info (omr->is_big_endian, is_32,
 Lines 457-484   simple_object_mach_o_segment (simple_object_read * Link Here 
457
	}
481
	}
458
    }
482
    }
459
483
460
  /* Process the sections.  */
484
  /* The new version puts all the LTO data into a single mach-o section and
485
     provides a separate index from which we implement sub-sectioning.
486
     For now, it is not an error for this to be missing, we could have an
487
     old-style file.  */
488
  if (index_index >= 0)
489
    {
490
      off_t index_offset;
461
491
492
      simple_object_mach_o_section_info (omr->is_big_endian, is_32,
493
					 secdata + index_index * sechdrsize,
494
					 &index_offset, &index_size);
495
      index = XNEWVEC (unsigned char, index_size);
496
      if (!simple_object_internal_read (sobj->descriptor,
497
					sobj->offset + index_offset,
498
					index, index_size,
499
					errmsg, err))
500
	{
501
	  XDELETEVEC (index);
502
	  XDELETEVEC (strtab);
503
	  XDELETEVEC (secdata);
504
	  return 0;
505
	}
506
    }
507
508
  if (sections_index >= 0)
509
    simple_object_mach_o_section_info (omr->is_big_endian, is_32,
510
				       secdata + sections_index * sechdrsize,
511
				       &lto_sect_offset, &lto_sect_size);
512
  /* The index contains 4 uint32_t per sub-section:
513
     sub-section offset/length, sub-section name/length.  
514
     We fix this for both 32 and 64 bit mach-o for now, since other 
515
     fields limit the maximum size of an object to 4G.  */
516
  n_lto_sects = index_size / 16;
517
518
  /* Process the sections:
519
520
     If we have a SEGMENT_NAME and it is finite sized, then fish out the
521
     sub-sections when we encounter the sub-sections entry.  Otherwise 
522
     proceed as below.
523
524
     If we have a strtab and the name matches the form of this, then
525
     substitute the name as per the original scheme.
526
     
527
     Otherwise, the section name is 'segment_name,section_name' in line
528
     with assembler usage.  */
529
462
  for (i = 0; i < nsects; ++i)
530
  for (i = 0; i < nsects; ++i)
463
    {
531
    {
464
      const unsigned char *sechdr;
532
      const unsigned char *sechdr;
465
      char namebuf[MACH_O_NAME_LEN + 1];
533
      char namebuf[MACH_O_NAME_LEN*2 + 2]; /* space for segment,section\0.  */
466
      char *name;
534
      char *name;
467
      off_t secoffset;
535
      off_t secoffset;
468
      size_t secsize;
536
      size_t secsize;
469
537
470
      if (i == strtab_index)
538
      if (i == strtab_index || i == index_index)
471
	continue;
539
	continue;
472
540
473
      sechdr = secdata + i * sechdrsize;
541
      sechdr = secdata + i * sechdrsize;
474
542
475
      if (strcmp ((char *) sechdr + segname_offset, omr->segment_name) != 0)
543
      if (omr->segment_name 
544
	  && strlen (omr->segment_name) > 0
545
	  && strcmp ((char *) sechdr + segname_offset, omr->segment_name) != 0)
476
	continue;
546
	continue;
477
547
548
      if (i == sections_index)
549
	{
550
	/* If we have a new-style file - handle that here.  */
551
	if (n_lto_sects && index)
552
	  {
553
	    int j;
554
	    for (j = 0; j < n_lto_sects; ++j)
555
	      {
556
		unsigned int subsect_offset, subsect_length, name_offset;
557
		subsect_offset = (*fetch_32) (index + 16 * j);
558
		subsect_length = (*fetch_32) (index + 16 * j + 4);
559
		name_offset = (*fetch_32) (index + 16 * j + 8);
560
		/* We don't use the name_length yet.  */
561
	  
562
		secoffset = lto_sect_offset + subsect_offset;
563
		secsize = subsect_length;
564
		name = strtab + name_offset;
565
566
		if (!(*pfn) (data, name, secoffset, secsize))
567
		  {
568
		    *errmsg = NULL;
569
		    *err = 0;
570
		    XDELETEVEC (index);
571
		    XDELETEVEC (strtab);
572
		    XDELETEVEC (secdata);
573
		    return 0;
574
		  }
575
	      }
576
	  }
577
	  continue;
578
	}
579
478
      memcpy (namebuf, sechdr + sectname_offset, MACH_O_NAME_LEN);
580
      memcpy (namebuf, sechdr + sectname_offset, MACH_O_NAME_LEN);
479
      namebuf[MACH_O_NAME_LEN] = '\0';
581
      namebuf[MACH_O_NAME_LEN] = '\0';
480
582
481
      name = &namebuf[0];
583
      name = &namebuf[0];
584
482
      if (strtab != NULL && name[0] == '_' && name[1] == '_')
585
      if (strtab != NULL && name[0] == '_' && name[1] == '_')
483
	{
586
	{
484
	  unsigned long stringoffset;
587
	  unsigned long stringoffset;
 Lines 497-502   simple_object_mach_o_segment (simple_object_read * Link Here 
497
	      name = strtab + stringoffset;
600
	      name = strtab + stringoffset;
498
	    }
601
	    }
499
	}
602
	}
603
      else
604
	{
605
	  /* Compose a section name like __SEGMENT,__section.  */
606
	  size_t l;
607
	  memset (namebuf, 0, MACH_O_NAME_LEN*2+2);
608
	  memcpy (namebuf, (char *) sechdr + segname_offset, MACH_O_NAME_LEN);
609
	  l = strlen (namebuf);
610
	  namebuf[l] = ',';
611
	  memcpy (namebuf, (char *) sechdr + sectname_offset, MACH_O_NAME_LEN);
612
	}
500
613
501
      simple_object_mach_o_section_info (omr->is_big_endian, is_32, sechdr,
614
      simple_object_mach_o_section_info (omr->is_big_endian, is_32, sechdr,
502
					 &secoffset, &secsize);
615
					 &secoffset, &secsize);
 Lines 505-516   simple_object_mach_o_segment (simple_object_read * Link Here 
505
	{
618
	{
506
	  *errmsg = NULL;
619
	  *errmsg = NULL;
507
	  *err = 0;
620
	  *err = 0;
621
	  XDELETEVEC (index);
508
	  XDELETEVEC (strtab);
622
	  XDELETEVEC (strtab);
509
	  XDELETEVEC (secdata);
623
	  XDELETEVEC (secdata);
510
	  return 0;
624
	  return 0;
511
	}
625
	}
512
    }
626
    }
513
627
628
  XDELETEVEC (index);
514
  XDELETEVEC (strtab);
629
  XDELETEVEC (strtab);
515
  XDELETEVEC (secdata);
630
  XDELETEVEC (secdata);
516
631
(-)include/simple-object.h (-12 / +13 lines)
 Lines 45-56   typedef struct simple_object_read_struct simple_ob Link Here 
45
   descriptor, and OFFSET, an offset within the file.  The offset is
45
   descriptor, and OFFSET, an offset within the file.  The offset is
46
   for use with archives, and should be 0 for an ordinary object file.
46
   for use with archives, and should be 0 for an ordinary object file.
47
   The descriptor must remain open until done with the returned
47
   The descriptor must remain open until done with the returned
48
   simple_object_read.  SEGMENT_NAME is used on Mach-O and is required
48
   simple_object_read.  SEGMENT_NAME is used to provide a GNU extension
49
   on that platform: it means to only look at sections within the
49
   to Mach-O.  When provided, only sections within that segment will be
50
   segment with that name.  It is ignored for other object file
50
   considered and, additionally, they will be processed according to a GNU
51
   formats.  On error, this function returns NULL, and sets *ERRMSG to
51
   sub-sectioning extension to Mach-O.  SEGMENT_NAME is ignored for other
52
   an error string and sets *ERR to an errno value or 0 if there is no
52
   object file formats.  On error, this function returns NULL, and sets
53
   relevant errno.  */
53
   *ERRMSG to an error string and sets *ERR to an errno value or 0 if there
54
   is no relevant errno.  */
54
55
55
extern simple_object_read *
56
extern simple_object_read *
56
simple_object_start_read (int descriptor, off_t offset,
57
simple_object_start_read (int descriptor, off_t offset,
 Lines 139-150   typedef struct simple_object_write_struct simple_o Link Here 
139
/* Start creating a new object file which is like ATTRS.  You must
140
/* Start creating a new object file which is like ATTRS.  You must
140
   fetch attribute information from an existing object file before you
141
   fetch attribute information from an existing object file before you
141
   can create a new one.  There is currently no support for creating
142
   can create a new one.  There is currently no support for creating
142
   an object file de novo.  The segment name is only used on Mach-O,
143
   an object file de novo.  SEGMENT_NAME is only used on Mach-O and, if
143
   where it is required.  It means that all sections are created
144
   set, it will cause all sections supplied to be stored according to a
144
   within that segment.  It is ignored for other object file formats.
145
   GNU sub-sectioning extension to the format.  It is ignored for other
145
   On error this function returns NULL, sets *ERRMSG to an error
146
   object file formats.  On error this function returns NULL, sets *ERRMSG
146
   message, and sets *ERR to an errno value or 0 if there isn't
147
   to an error message, and sets *ERR to an errno value or 0 if there
147
   one.  */
148
   isn't one.  */
148
149
149
extern simple_object_write *
150
extern simple_object_write *
150
simple_object_start_write (simple_object_attributes *attrs,
151
simple_object_start_write (simple_object_attributes *attrs,
(-)gcc/config/darwin.c (-52 / +167 lines)
 Lines 1439-1444   darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, Link Here 
1439
  else if (!strncmp (p, "V2_CSTR", 7))
1439
  else if (!strncmp (p, "V2_CSTR", 7))
1440
    return darwin_sections[objc_constant_string_object_section];
1440
    return darwin_sections[objc_constant_string_object_section];
1441
1441
1442
  else if (!strncmp (p, "V1_CREF", 7))
1443
    return darwin_sections[objc_cls_refs_section];
1444
  else if (!strncmp (p, "V1_CDEF", 7))
1445
    return data_section;
1446
1442
  return base;
1447
  return base;
1443
}
1448
}
1444
1449
 Lines 1753-1772   darwin_label_is_anonymous_local_objc_name (const c Link Here 
1753
  return (!strncmp ((const char *)p, "_OBJC_", 6));
1758
  return (!strncmp ((const char *)p, "_OBJC_", 6));
1754
}
1759
}
1755
1760
1756
/* LTO support for Mach-O.  */
1761
/* LTO support for Mach-O.
1757
1762
1758
/* Section names for LTO sections.  */
1763
   This version uses three mach-o sections to encapsulate the (unlimited
1759
static unsigned int lto_section_names_offset = 0;
1764
   number of) lto sections.
1765
   
1766
   __GNU_LTO, __lto_sections  contains the concatented GNU LTO section data.
1767
   __GNU_LTO, __section_names contains the GNU LTO section names.
1768
   __GNU_LTO, __section_index contains an array of values that index these.
1760
1769
1761
/* This is the obstack which we use to allocate the many strings.  */
1770
   Indexed thus:
1762
static struct obstack lto_section_names_obstack;
1771
     <section offset from the start of __GNU_LTO, __lto_sections>,
1772
     <section length>
1773
     <name offset from the start of __GNU_LTO, __section_names,
1774
     <name length>.
1763
1775
1764
/* Segment name for LTO sections.  */
1776
   At present, for both m32 and m64 mach-o files each of these fields is
1777
   represented  by a uint32_t.  This is because, AFAICT, a mach-o object
1778
   cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
1779
   
1780
    uint32_t offset;
1781
   "offset  An integer specifying the offset to this section in the file."  */
1782
1783
/* Count lto section numbers. */
1784
static unsigned int lto_section_num = 0;
1785
1786
/* A vector of information about LTO sections, at present, we only have
1787
   the name.  TODO: see if we can get the data length somehow.  */
1788
typedef struct GTY(()) darwin_lto_section_e {
1789
  const char *sectname;
1790
} darwin_lto_section_e ;
1791
DEF_VEC_O(darwin_lto_section_e);
1792
DEF_VEC_ALLOC_O(darwin_lto_section_e, gc);
1793
1794
static GTY (()) VEC (darwin_lto_section_e, gc) * lto_section_names;
1795
1796
/* Segment for LTO (sub-)sections.  */
1765
#define LTO_SEGMENT_NAME "__GNU_LTO"
1797
#define LTO_SEGMENT_NAME "__GNU_LTO"
1766
1798
1767
/* Section name for LTO section names section.  */
1799
/* Section for LTO sub-sections names.  */
1768
#define LTO_NAMES_SECTION "__section_names"
1800
#define LTO_NAMES_SECTION "__subsect_names"
1769
1801
1802
/* Section for LTO sub-sections index.  */
1803
#define LTO_INDEX_SECTION "__subsect_index"
1804
1805
/* Section for LTO sub-sections.  */
1806
#define LTO_SECTS_SECTION "__subsect_sects"
1807
1770
/* File to temporarily store LTO data.  This is appended to asm_out_file
1808
/* File to temporarily store LTO data.  This is appended to asm_out_file
1771
   in darwin_end_file.  */
1809
   in darwin_end_file.  */
1772
static FILE *lto_asm_out_file, *saved_asm_out_file;
1810
static FILE *lto_asm_out_file, *saved_asm_out_file;
 Lines 1808-1844   darwin_asm_named_section (const char *name, Link Here 
1808
			  unsigned int flags,
1846
			  unsigned int flags,
1809
			  tree decl ATTRIBUTE_UNUSED)
1847
			  tree decl ATTRIBUTE_UNUSED)
1810
{
1848
{
1811
  /* LTO sections go in a special segment __GNU_LTO.  We want to replace the
1849
  /* LTO sections go in a special section that encapsulates the (unlimited)
1812
     section name with something we can use to represent arbitrary-length
1850
     number of GNU LTO sections within a single mach-o one.  */
1813
     names (section names in Mach-O are at most 16 characters long).  */
1814
  if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1851
  if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1815
	       strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1852
	       strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1816
    {
1853
    {
1854
      darwin_lto_section_e e;
1817
      /* We expect certain flags to be set...  */
1855
      /* We expect certain flags to be set...  */
1818
      gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1856
      gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1819
		  == (SECTION_DEBUG | SECTION_NAMED));
1857
		  == (SECTION_DEBUG | SECTION_NAMED));
1820
1858
1821
      /* Add the section name to the things to output when we end the
1859
      /* Switch to our combined section.  */
1822
	 current assembler output file.
1860
      fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
1823
	 This is all not very efficient, but that doesn't matter -- this
1861
	       LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
1824
	 shouldn't be a hot path in the compiler...  */
1862
      /* Output a label for the start of this sub-section.  */
1825
      obstack_1grow (&lto_section_names_obstack, '\t');
1863
      fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
1826
      obstack_grow (&lto_section_names_obstack, ".ascii ", 7);
1864
	       lto_section_num, name);
1827
      obstack_1grow (&lto_section_names_obstack, '"');
1865
      /* We have to jump through hoops to get the values of the intra-section
1828
      obstack_grow (&lto_section_names_obstack, name, strlen (name));
1866
         offsets... */
1829
      obstack_grow (&lto_section_names_obstack, "\\0\"\n", 4);
1867
      fprintf (asm_out_file, "\t.set $gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
1830
1868
	       lto_section_num, lto_section_num);
1831
      /* Output the dummy section name.  */
1869
      fprintf (asm_out_file, "\t.set $gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
1832
      fprintf (asm_out_file, "\t# %s\n", name);
1870
	       lto_section_num, lto_section_num+1, lto_section_num);
1833
      fprintf (asm_out_file, "\t.section %s,__%08X,regular,debug\n",
1871
      lto_section_num++;
1834
	       LTO_SEGMENT_NAME, lto_section_names_offset);
1872
      e.sectname = xstrdup (name);
1835
1873
      /* Keep the names, we'll need to make a table later.
1836
      /* Update the offset for the next section name.  Make sure we stay
1874
         TODO: check that we do not revisit sections, that would break 
1837
	 within reasonable length.  */  
1875
         the assumption of how this is done. */
1838
      lto_section_names_offset += strlen (name) + 1;
1876
      if (lto_section_names == NULL)
1839
      gcc_assert (lto_section_names_offset > 0
1877
        lto_section_names = VEC_alloc (darwin_lto_section_e, gc, 16);
1840
		  && lto_section_names_offset < ((unsigned) 1 << 31));
1878
      VEC_safe_push (darwin_lto_section_e, gc, lto_section_names, &e);
1841
    }
1879
   }
1842
  else if (strncmp (name, "__DWARF,", 8) == 0)
1880
  else if (strncmp (name, "__DWARF,", 8) == 0)
1843
    darwin_asm_dwarf_section (name, flags, decl);
1881
    darwin_asm_dwarf_section (name, flags, decl);
1844
  else
1882
  else
 Lines 2594-2599   darwin_assemble_visibility (tree decl, int vis) Link Here 
2594
	     "not supported in this configuration; ignored");
2632
	     "not supported in this configuration; ignored");
2595
}
2633
}
2596
2634
2635
/* For certain Objective-C metadata we handle the assembly of the variables
2636
   here (it must be here, rather than in darwin-c.c, to cater for LTO).  When
2637
   we reference a class we emit a lazy ref, when we have class metadata
2638
   (local to a specific object), we emit a tag so that linkage will be
2639
   satisfied for the class.  */
2640
2641
bool
2642
darwin_handled_assemble_variable_p (tree decl)
2643
{
2644
  tree meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
2645
  if (meta)
2646
    {
2647
      const char *name;
2648
      rtx decl_rtl, symbol;
2649
2650
      if (TREE_VALUE (meta) == get_identifier ("V1_CREF"))
2651
	{
2652
	  tree exp = DECL_INITIAL (decl);
2653
	  gcc_checking_assert (exp 
2654
			       && exp != error_mark_node 
2655
			       && TREE_CODE (exp) == ADDR_EXPR);
2656
	  exp = TREE_OPERAND (exp, 0);
2657
	  decl_rtl = DECL_RTL (exp);
2658
	  symbol = XEXP (decl_rtl, 0);
2659
	  name = XSTR (symbol, 0);
2660
	  targetm.asm_out.globalize_decl_name (asm_out_file, exp);
2661
	  fputs ("\t.lazy_reference\t",asm_out_file);
2662
	  assemble_name (asm_out_file, name);
2663
	  fputs ("\n",asm_out_file);
2664
	  TREE_ASM_WRITTEN (decl) = 1;
2665
	  return true;
2666
	}
2667
      else if (TREE_VALUE (meta) == get_identifier ("V1_CDEF"))
2668
	{
2669
	  decl_rtl = DECL_RTL (decl);
2670
	  symbol = XEXP (decl_rtl, 0);
2671
	  name = XSTR (symbol, 0);
2672
	  targetm.asm_out.globalize_decl_name (asm_out_file, decl);
2673
	  fputs ("\t",asm_out_file);
2674
	  assemble_name (asm_out_file, name);
2675
	  fputs (" = 0\n",asm_out_file);
2676
	  TREE_ASM_WRITTEN (decl) = 1;
2677
	  return true;
2678
	}
2679
    }
2680
  return false;
2681
}
2682
2597
/* VEC Used by darwin_asm_dwarf_section.
2683
/* VEC Used by darwin_asm_dwarf_section.
2598
   Maybe a hash tab would be better here - but the intention is that this is
2684
   Maybe a hash tab would be better here - but the intention is that this is
2599
   a very short list (fewer than 16 items) and each entry should (ideally, 
2685
   a very short list (fewer than 16 items) and each entry should (ideally, 
 Lines 2711-2726   darwin_asm_output_dwarf_offset (FILE *file, int si Link Here 
2711
  darwin_asm_output_dwarf_delta (file, size, lab, sname);
2797
  darwin_asm_output_dwarf_delta (file, size, lab, sname);
2712
}
2798
}
2713
2799
2714
/* Called from the within the TARGET_ASM_FILE_START for each target. 
2800
/* Called from the within the TARGET_ASM_FILE_START for each target.  */
2715
  Initialize the stuff we need for LTO long section names support.  */
2716
2801
2717
void
2802
void
2718
darwin_file_start (void)
2803
darwin_file_start (void)
2719
{
2804
{
2720
  /* We fill this obstack with the complete section text for the lto section
2805
  /* Nothing to do.  */
2721
     names to write in darwin_file_end.  */
2722
  obstack_init (&lto_section_names_obstack);
2723
  lto_section_names_offset = 0;
2724
}
2806
}
2725
2807
2726
/* Called for the TARGET_ASM_FILE_END hook.
2808
/* Called for the TARGET_ASM_FILE_END hook.
 Lines 2731-2738   darwin_file_start (void) Link Here 
2731
void
2813
void
2732
darwin_file_end (void)
2814
darwin_file_end (void)
2733
{
2815
{
2734
  const char *lto_section_names;
2735
2736
  machopic_finish (asm_out_file);
2816
  machopic_finish (asm_out_file);
2737
  if (strcmp (lang_hooks.name, "GNU C++") == 0)
2817
  if (strcmp (lang_hooks.name, "GNU C++") == 0)
2738
    {
2818
    {
 Lines 2762-2767   darwin_file_end (void) Link Here 
2762
	  lto_asm_txt = buf = (char *) xmalloc (n + 1);
2842
	  lto_asm_txt = buf = (char *) xmalloc (n + 1);
2763
	  while (fgets (lto_asm_txt, n, lto_asm_out_file))
2843
	  while (fgets (lto_asm_txt, n, lto_asm_out_file))
2764
	    fputs (lto_asm_txt, asm_out_file);
2844
	    fputs (lto_asm_txt, asm_out_file);
2845
	  /* Put a termination label.  */
2846
	  fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2847
		   LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
2848
	  fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
2849
		   lto_section_num);
2850
	  /* Make sure our termination label stays in this section.  */
2851
	  fputs ("\t.space\t1\n", asm_out_file);
2765
	}
2852
	}
2766
2853
2767
      /* Remove the temporary file.  */
2854
      /* Remove the temporary file.  */
 Lines 2770-2790   darwin_file_end (void) Link Here 
2770
      free (lto_asm_out_name);
2857
      free (lto_asm_out_name);
2771
    }
2858
    }
2772
2859
2773
  /* Finish the LTO section names obstack.  Don't output anything if
2860
  /* Output the names and indices.  */
2774
     there are no recorded section names.  */
2861
  if (lto_section_names && VEC_length (darwin_lto_section_e, lto_section_names))
2775
  obstack_1grow (&lto_section_names_obstack, '\0');
2776
  lto_section_names = XOBFINISH (&lto_section_names_obstack, const char *);
2777
  if (strlen (lto_section_names) > 0)
2778
    {
2862
    {
2779
      fprintf (asm_out_file,
2863
      int count;
2780
	       "\t.section %s,%s,regular,debug\n",
2864
      darwin_lto_section_e *ref;
2865
      /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
2866
         the latter up ourselves. */
2867
      const char *op = integer_asm_op (4,0); 
2868
2869
      /* Emit the names. */
2870
      fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2781
	       LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
2871
	       LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
2782
      fprintf (asm_out_file,
2872
      FOR_EACH_VEC_ELT (darwin_lto_section_e, lto_section_names, count, ref)
2783
	       "\t# Section names in %s are offsets into this table\n",
2873
	{
2784
	       LTO_SEGMENT_NAME);
2874
	  fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
2785
      fprintf (asm_out_file, "%s\n", lto_section_names);
2875
         /* We have to jump through hoops to get the values of the intra-section
2876
            offsets... */
2877
	  fprintf (asm_out_file, 
2878
		   "\t.set $gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
2879
		   count, count);
2880
	  fprintf (asm_out_file,
2881
		   "\t.set $gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
2882
		   count, count+1, count);
2883
	  fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
2884
	}
2885
      fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
2886
      /* make sure our termination label stays in this section.  */
2887
      fputs ("\t.space\t1\n", asm_out_file);
2888
2889
      /* Emit the Index. */
2890
      fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2891
	       LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
2892
      fputs ("\t.align\t2\n", asm_out_file);
2893
      fputs ("# Section offset, Section length, Name offset, Name length\n", asm_out_file);
2894
      FOR_EACH_VEC_ELT (darwin_lto_section_e, lto_section_names, count, ref)
2895
	{
2896
	  fprintf (asm_out_file, "%s $gnu$lto$offs%d\t;# %s\n",
2897
		   op, count, ref->sectname);
2898
	  fprintf (asm_out_file, "%s $gnu$lto$size%d\n", op, count);
2899
	  fprintf (asm_out_file, "%s $gnu$lto$noff%d\n", op, count);
2900
	  fprintf (asm_out_file, "%s $gnu$lto$nsiz%d\n", op, count);
2901
	}
2786
    }
2902
    }
2787
  obstack_free (&lto_section_names_obstack, NULL);
2788
2903
2789
  /* If we have section anchors, then we must prevent the linker from
2904
  /* If we have section anchors, then we must prevent the linker from
2790
     re-arranging data.  */
2905
     re-arranging data.  */

Return to bug 48108