~ubuntu-branches/debian/jessie/gdb/jessie

« back to all changes in this revision

Viewing changes to bfd/archive.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Jacobowitz
  • Date: 2010-03-20 01:21:29 UTC
  • mfrom: (1.3.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20100320012129-t7h25y8zgr8c2369
Tags: 7.1-1
* New upstream release, including:
  - PIE support (Closes: #346409).
  - C++ improvements, including static_cast<> et al, namespace imports,
    and bug fixes in printing virtual base classes.
  - Multi-program debugging.  One GDB can now debug multiple programs
    at the same time.
  - Python scripting improvements, including gdb.parse_and_eval.
  - Updated MIPS Linux signal frame layout (Closes: #570875).
  - No internal error stepping over _dl_debug_state (Closes: #569551).
* Update to Standards-Version: 3.8.4 (no changes required).
* Include more relevant (and smaller) docs in the gdbserver package
  (Closes: #571132).
* Do not duplicate documentation in gdb64, gdb-source, and libgdb-dev.
* Fix crash when switching into TUI mode (Closes: #568489).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* BFD back-end for archive files (libraries).
2
2
   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3
 
   2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
 
3
   2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4
4
   Free Software Foundation, Inc.
5
5
   Written by Cygnus Support.  Mostly Gumby Henkel-Wallace's fault.
6
6
 
104
104
 
105
105
   BSD 4.4 uses a third scheme:  It writes a long filename
106
106
   directly after the header.  This allows 'ar q' to work.
107
 
   We currently can read BSD 4.4 archives, but not write them.
108
107
*/
109
108
 
110
109
/* Summary of archive member names:
125
124
 "/18             " - SVR4 style, name at offset 18 in name table.
126
125
 "#1/23           " - Long name (or embedded spaces) 23 characters long,
127
126
                      BSD 4.4 style, full name follows header.
128
 
                      Implemented for reading, not writing.
129
127
 " 18             " - Long name 18 characters long, extended pseudo-BSD.
130
128
 */
131
129
 
159
157
 
160
158
#define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
161
159
#define arch_hdr(bfd) ((struct ar_hdr *) arch_eltdata (bfd)->arch_header)
 
160
 
 
161
/* True iff NAME designated a BSD 4.4 extended name.  */
 
162
 
 
163
#define is_bsd44_extended_name(NAME) \
 
164
  (NAME[0] == '#'  && NAME[1] == '1' && NAME[2] == '/' && ISDIGIT (NAME[3]))
162
165
 
163
166
void
164
167
_bfd_ar_spacepad (char *p, size_t n, const char *fmt, long val)
354
357
static char *
355
358
get_extended_arelt_filename (bfd *arch, const char *name, file_ptr *originp)
356
359
{
357
 
  unsigned long index = 0;
 
360
  unsigned long table_index = 0;
358
361
  const char *endp;
359
362
 
360
363
  /* Should extract string so that I can guarantee not to overflow into
361
364
     the next region, but I'm too lazy.  */
362
365
  errno = 0;
363
366
  /* Skip first char, which is '/' in SVR4 or ' ' in some other variants.  */
364
 
  index = strtol (name + 1, (char **) &endp, 10);
365
 
  if (errno != 0 || index >= bfd_ardata (arch)->extended_names_size)
 
367
  table_index = strtol (name + 1, (char **) &endp, 10);
 
368
  if (errno != 0 || table_index >= bfd_ardata (arch)->extended_names_size)
366
369
    {
367
370
      bfd_set_error (bfd_error_malformed_archive);
368
371
      return NULL;
383
386
  else
384
387
    *originp = 0;
385
388
 
386
 
  return bfd_ardata (arch)->extended_names + index;
 
389
  return bfd_ardata (arch)->extended_names + table_index;
387
390
}
388
391
 
389
392
/* This functions reads an arch header and returns an areltdata pointer, or
415
418
  bfd_size_type allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
416
419
  char *allocptr = 0;
417
420
  file_ptr origin = 0;
 
421
  unsigned int extra_size = 0;
418
422
 
419
423
  if (bfd_bread (hdrp, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr))
420
424
    {
450
454
      if (filename == NULL)
451
455
        return NULL;
452
456
    }
453
 
  /* BSD4.4-style long filename.
454
 
     Only implemented for reading, so far!  */
455
 
  else if (hdr.ar_name[0] == '#'
456
 
           && hdr.ar_name[1] == '1'
457
 
           && hdr.ar_name[2] == '/'
458
 
           && ISDIGIT (hdr.ar_name[3]))
 
457
  /* BSD4.4-style long filename.  */
 
458
  else if (is_bsd44_extended_name (hdr.ar_name))
459
459
    {
460
460
      /* BSD-4.4 extended name */
461
461
      namelen = atoi (&hdr.ar_name[3]);
462
462
      allocsize += namelen + 1;
463
463
      parsed_size -= namelen;
 
464
      extra_size = namelen;
464
465
 
465
466
      allocptr = (char *) bfd_zalloc (abfd, allocsize);
466
467
      if (allocptr == NULL)
515
516
  ared->arch_header = allocptr + sizeof (struct areltdata);
516
517
  memcpy (ared->arch_header, &hdr, sizeof (struct ar_hdr));
517
518
  ared->parsed_size = parsed_size;
 
519
  ared->extra_size = extra_size;
518
520
  ared->origin = origin;
519
521
 
520
522
  if (filename != NULL)
656
658
}
657
659
 
658
660
/* Return the BFD which is referenced by the symbol in ABFD indexed by
659
 
   INDEX.  INDEX should have been returned by bfd_get_next_mapent.  */
 
661
   SYM_INDEX.  SYM_INDEX should have been returned by bfd_get_next_mapent.  */
660
662
 
661
663
bfd *
662
 
_bfd_generic_get_elt_at_index (bfd *abfd, symindex index)
 
664
_bfd_generic_get_elt_at_index (bfd *abfd, symindex sym_index)
663
665
{
664
666
  carsym *entry;
665
667
 
666
 
  entry = bfd_ardata (abfd)->symdefs + index;
 
668
  entry = bfd_ardata (abfd)->symdefs + sym_index;
667
669
  return _bfd_get_elt_at_filepos (abfd, entry->file_offset);
668
670
}
669
671
 
1054
1056
      return FALSE;
1055
1057
#endif
1056
1058
    }
 
1059
  else if (CONST_STRNEQ (nextname, "#1/20           "))
 
1060
    {
 
1061
      /* Mach-O has a special name for armap when the map is sorted by name.
 
1062
         However because this name has a space it is slightly more difficult
 
1063
         to check it.  */
 
1064
      struct ar_hdr hdr;
 
1065
      char extname[21];
 
1066
 
 
1067
      if (bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
 
1068
        return FALSE;
 
1069
      /* Read the extended name.  We know its length.  */
 
1070
      if (bfd_bread (extname, 20, abfd) != 20)
 
1071
        return FALSE;
 
1072
      if (bfd_seek (abfd, (file_ptr) -(sizeof (hdr) + 20), SEEK_CUR) != 0)
 
1073
        return FALSE;
 
1074
      if (CONST_STRNEQ (extname, "__.SYMDEF SORTED")
 
1075
          || CONST_STRNEQ (extname, "__.SYMDEF"))
 
1076
        return do_slurp_bsd_armap (abfd);
 
1077
    }
1057
1078
 
1058
1079
  bfd_has_map (abfd) = FALSE;
1059
1080
  return TRUE;
1294
1315
static const char *
1295
1316
normalize (bfd *abfd ATTRIBUTE_UNUSED, const char *file)
1296
1317
{
1297
 
  const char *filename = strrchr (file, '/');
1298
 
 
1299
 
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1300
 
  {
1301
 
    /* We could have foo/bar\\baz, or foo\\bar, or d:bar.  */
1302
 
    char *bslash = strrchr (file, '\\');
1303
 
    if (filename == NULL || (bslash != NULL && bslash > filename))
1304
 
      filename = bslash;
1305
 
    if (filename == NULL && file[0] != '\0' && file[1] == ':')
1306
 
      filename = file + 1;
1307
 
  }
1308
 
#endif
1309
 
  if (filename != NULL)
1310
 
    filename++;
1311
 
  else
1312
 
    filename = file;
1313
 
  return filename;
 
1318
  return lbasename (file);
1314
1319
}
1315
1320
#endif
1316
1321
 
1597
1602
 
1598
1603
  return TRUE;
1599
1604
}
 
1605
 
 
1606
/* Do not construct an extended name table but transforms name field into
 
1607
   its extended form.  */
 
1608
 
 
1609
bfd_boolean
 
1610
_bfd_archive_bsd44_construct_extended_name_table (bfd *abfd,
 
1611
                                                  char **tabloc,
 
1612
                                                  bfd_size_type *tablen,
 
1613
                                                  const char **name)
 
1614
{
 
1615
  unsigned int maxname = abfd->xvec->ar_max_namelen;
 
1616
  bfd *current;
 
1617
 
 
1618
  *tablen = 0;
 
1619
  *tabloc = NULL;
 
1620
  *name = NULL;
 
1621
 
 
1622
  for (current = abfd->archive_head;
 
1623
       current != NULL;
 
1624
       current = current->archive_next)
 
1625
    {
 
1626
      const char *normal = normalize (current, current->filename);
 
1627
      int has_space = 0;
 
1628
      unsigned int len;
 
1629
 
 
1630
      if (normal == NULL)
 
1631
        return FALSE;
 
1632
 
 
1633
      for (len = 0; normal[len]; len++)
 
1634
        if (normal[len] == ' ')
 
1635
          has_space = 1;
 
1636
 
 
1637
      if (len > maxname || has_space)
 
1638
        {
 
1639
          struct ar_hdr *hdr = arch_hdr (current);
 
1640
 
 
1641
          len = (len + 3) & ~3;
 
1642
          arch_eltdata (current)->extra_size = len;
 
1643
          _bfd_ar_spacepad (hdr->ar_name, maxname, "#1/%u", len);
 
1644
        }
 
1645
    }
 
1646
 
 
1647
  return TRUE;
 
1648
}
 
1649
 
 
1650
/* Write an archive header.  */
 
1651
 
 
1652
bfd_boolean
 
1653
_bfd_generic_write_ar_hdr (bfd *archive, bfd *abfd)
 
1654
{
 
1655
  struct ar_hdr *hdr = arch_hdr (abfd);
 
1656
 
 
1657
  if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
 
1658
    return FALSE;
 
1659
  return TRUE;
 
1660
}
 
1661
 
 
1662
/* Write an archive header using BSD4.4 convention.  */
 
1663
 
 
1664
bfd_boolean
 
1665
_bfd_bsd44_write_ar_hdr (bfd *archive, bfd *abfd)
 
1666
{
 
1667
  struct ar_hdr *hdr = arch_hdr (abfd);
 
1668
 
 
1669
  if (is_bsd44_extended_name (hdr->ar_name))
 
1670
    {
 
1671
      /* This is a BSD 4.4 extended name.  */
 
1672
      const char *fullname = normalize (abfd, abfd->filename);
 
1673
      unsigned int len = strlen (fullname);
 
1674
      unsigned int padded_len = (len + 3) & ~3;
 
1675
 
 
1676
      BFD_ASSERT (padded_len == arch_eltdata (abfd)->extra_size);
 
1677
 
 
1678
      _bfd_ar_spacepad (hdr->ar_size, sizeof (hdr->ar_size), "%-10ld",
 
1679
                        arch_eltdata (abfd)->parsed_size + padded_len);
 
1680
 
 
1681
      if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
 
1682
        return FALSE;
 
1683
 
 
1684
      if (bfd_bwrite (fullname, len, archive) != len)
 
1685
        return FALSE;
 
1686
      if (len & 3)
 
1687
        {
 
1688
          static const char pad[3] = { 0, 0, 0 };
 
1689
 
 
1690
          len = 4 - (len & 3);
 
1691
          if (bfd_bwrite (pad, len, archive) != len)
 
1692
            return FALSE;
 
1693
        }
 
1694
    }
 
1695
  else
 
1696
    {
 
1697
      if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
 
1698
        return FALSE;
 
1699
    }
 
1700
  return TRUE;
 
1701
}
1600
1702
 
1601
1703
/* A couple of functions for creating ar_hdrs.  */
1602
1704
 
1703
1805
  return ared;
1704
1806
}
1705
1807
 
1706
 
/* This is magic required by the "ar" program.  Since it's
1707
 
   undocumented, it's undocumented.  You may think that it would take
1708
 
   a strong stomach to write this, and it does, but it takes even a
1709
 
   stronger stomach to try to code around such a thing!  */
1710
 
 
1711
 
struct ar_hdr *bfd_special_undocumented_glue (bfd *, const char *);
1712
 
 
1713
 
struct ar_hdr *
1714
 
bfd_special_undocumented_glue (bfd *abfd, const char *filename)
1715
 
{
1716
 
  struct areltdata *ar_elt = bfd_ar_hdr_from_filesystem (abfd, filename, 0);
1717
 
  if (ar_elt == NULL)
1718
 
    return NULL;
1719
 
  return (struct ar_hdr *) ar_elt->arch_header;
1720
 
}
1721
 
 
1722
1808
/* Analogous to stat call.  */
1723
1809
 
1724
1810
int
1818
1904
{
1819
1905
  struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1820
1906
  size_t length;
1821
 
  const char *filename = strrchr (pathname, '/');
 
1907
  const char *filename = lbasename (pathname);
1822
1908
  size_t maxlen = ar_maxnamelen (abfd);
1823
1909
 
1824
 
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1825
 
  {
1826
 
    /* We could have foo/bar\\baz, or foo\\bar, or d:bar.  */
1827
 
    char *bslash = strrchr (pathname, '\\');
1828
 
    if (filename == NULL || (bslash != NULL && bslash > filename))
1829
 
      filename = bslash;
1830
 
    if (filename == NULL && pathname[0] != '\0' && pathname[1] == ':')
1831
 
      filename = pathname + 1;
1832
 
  }
1833
 
#endif
1834
 
 
1835
 
  if (filename == NULL)
1836
 
    filename = pathname;
1837
 
  else
1838
 
    ++filename;
1839
 
 
1840
1910
  length = strlen (filename);
1841
1911
 
1842
1912
  if (length <= maxlen)
1866
1936
{
1867
1937
  struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1868
1938
  size_t length;
1869
 
  const char *filename = strrchr (pathname, '/');
 
1939
  const char *filename = lbasename (pathname);
1870
1940
  size_t maxlen = ar_maxnamelen (abfd);
1871
1941
 
1872
 
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1873
 
  {
1874
 
    /* We could have foo/bar\\baz, or foo\\bar, or d:bar.  */
1875
 
    char *bslash = strrchr (pathname, '\\');
1876
 
 
1877
 
    if (filename == NULL || (bslash != NULL && bslash > filename))
1878
 
      filename = bslash;
1879
 
    if (filename == NULL && pathname[0] != '\0' && pathname[1] == ':')
1880
 
      filename = pathname + 1;
1881
 
  }
1882
 
#endif
1883
 
 
1884
 
  if (filename == NULL)
1885
 
    filename = pathname;
1886
 
  else
1887
 
    ++filename;
1888
 
 
1889
1942
  length = strlen (filename);
1890
1943
 
1891
1944
  if (length <= maxlen)
2004
2057
    {
2005
2058
      char buffer[DEFAULT_BUFFERSIZE];
2006
2059
      unsigned int remaining = arelt_size (current);
2007
 
      struct ar_hdr *hdr = arch_hdr (current);
2008
2060
 
2009
2061
      /* Write ar header.  */
2010
 
      if (bfd_bwrite (hdr, sizeof (*hdr), arch)
2011
 
          != sizeof (*hdr))
2012
 
        return FALSE;
 
2062
      if (!_bfd_write_ar_hdr (arch, current))
 
2063
        return FALSE;
2013
2064
      if (bfd_is_thin_archive (arch))
2014
2065
        continue;
2015
2066
      if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0)
2282
2333
        {
2283
2334
          do
2284
2335
            {
2285
 
              firstreal += arelt_size (current) + sizeof (struct ar_hdr);
 
2336
              struct areltdata *ared = arch_eltdata (current);
 
2337
 
 
2338
              firstreal += (ared->parsed_size + ared->extra_size
 
2339
                            + sizeof (struct ar_hdr));
2286
2340
              firstreal += firstreal % 2;
2287
2341
              current = current->archive_next;
2288
2342
            }