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.
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.
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)
161
/* True iff NAME designated a BSD 4.4 extended name. */
163
#define is_bsd44_extended_name(NAME) \
164
(NAME[0] == '#' && NAME[1] == '1' && NAME[2] == '/' && ISDIGIT (NAME[3]))
164
167
_bfd_ar_spacepad (char *p, size_t n, const char *fmt, long val)
355
358
get_extended_arelt_filename (bfd *arch, const char *name, file_ptr *originp)
357
unsigned long index = 0;
360
unsigned long table_index = 0;
358
361
const char *endp;
360
363
/* Should extract string so that I can guarantee not to overflow into
361
364
the next region, but I'm too lazy. */
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)
367
370
bfd_set_error (bfd_error_malformed_archive);
450
454
if (filename == NULL)
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))
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;
465
466
allocptr = (char *) bfd_zalloc (abfd, allocsize);
466
467
if (allocptr == NULL)
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. */
662
_bfd_generic_get_elt_at_index (bfd *abfd, symindex index)
664
_bfd_generic_get_elt_at_index (bfd *abfd, symindex sym_index)
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);
1059
else if (CONST_STRNEQ (nextname, "#1/20 "))
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
1067
if (bfd_bread (&hdr, sizeof (hdr), abfd) != sizeof (hdr))
1069
/* Read the extended name. We know its length. */
1070
if (bfd_bread (extname, 20, abfd) != 20)
1072
if (bfd_seek (abfd, (file_ptr) -(sizeof (hdr) + 20), SEEK_CUR) != 0)
1074
if (CONST_STRNEQ (extname, "__.SYMDEF SORTED")
1075
|| CONST_STRNEQ (extname, "__.SYMDEF"))
1076
return do_slurp_bsd_armap (abfd);
1058
1079
bfd_has_map (abfd) = FALSE;
1294
1315
static const char *
1295
1316
normalize (bfd *abfd ATTRIBUTE_UNUSED, const char *file)
1297
const char *filename = strrchr (file, '/');
1299
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
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))
1305
if (filename == NULL && file[0] != '\0' && file[1] == ':')
1306
filename = file + 1;
1309
if (filename != NULL)
1318
return lbasename (file);
1606
/* Do not construct an extended name table but transforms name field into
1607
its extended form. */
1610
_bfd_archive_bsd44_construct_extended_name_table (bfd *abfd,
1612
bfd_size_type *tablen,
1615
unsigned int maxname = abfd->xvec->ar_max_namelen;
1622
for (current = abfd->archive_head;
1624
current = current->archive_next)
1626
const char *normal = normalize (current, current->filename);
1633
for (len = 0; normal[len]; len++)
1634
if (normal[len] == ' ')
1637
if (len > maxname || has_space)
1639
struct ar_hdr *hdr = arch_hdr (current);
1641
len = (len + 3) & ~3;
1642
arch_eltdata (current)->extra_size = len;
1643
_bfd_ar_spacepad (hdr->ar_name, maxname, "#1/%u", len);
1650
/* Write an archive header. */
1653
_bfd_generic_write_ar_hdr (bfd *archive, bfd *abfd)
1655
struct ar_hdr *hdr = arch_hdr (abfd);
1657
if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1662
/* Write an archive header using BSD4.4 convention. */
1665
_bfd_bsd44_write_ar_hdr (bfd *archive, bfd *abfd)
1667
struct ar_hdr *hdr = arch_hdr (abfd);
1669
if (is_bsd44_extended_name (hdr->ar_name))
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;
1676
BFD_ASSERT (padded_len == arch_eltdata (abfd)->extra_size);
1678
_bfd_ar_spacepad (hdr->ar_size, sizeof (hdr->ar_size), "%-10ld",
1679
arch_eltdata (abfd)->parsed_size + padded_len);
1681
if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1684
if (bfd_bwrite (fullname, len, archive) != len)
1688
static const char pad[3] = { 0, 0, 0 };
1690
len = 4 - (len & 3);
1691
if (bfd_bwrite (pad, len, archive) != len)
1697
if (bfd_bwrite (hdr, sizeof (*hdr), archive) != sizeof (*hdr))
1601
1703
/* A couple of functions for creating ar_hdrs. */
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! */
1711
struct ar_hdr *bfd_special_undocumented_glue (bfd *, const char *);
1714
bfd_special_undocumented_glue (bfd *abfd, const char *filename)
1716
struct areltdata *ar_elt = bfd_ar_hdr_from_filesystem (abfd, filename, 0);
1719
return (struct ar_hdr *) ar_elt->arch_header;
1722
1808
/* Analogous to stat call. */
1819
1905
struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1821
const char *filename = strrchr (pathname, '/');
1907
const char *filename = lbasename (pathname);
1822
1908
size_t maxlen = ar_maxnamelen (abfd);
1824
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
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))
1830
if (filename == NULL && pathname[0] != '\0' && pathname[1] == ':')
1831
filename = pathname + 1;
1835
if (filename == NULL)
1836
filename = pathname;
1840
1910
length = strlen (filename);
1842
1912
if (length <= maxlen)
1867
1937
struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1869
const char *filename = strrchr (pathname, '/');
1939
const char *filename = lbasename (pathname);
1870
1940
size_t maxlen = ar_maxnamelen (abfd);
1872
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1874
/* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1875
char *bslash = strrchr (pathname, '\\');
1877
if (filename == NULL || (bslash != NULL && bslash > filename))
1879
if (filename == NULL && pathname[0] != '\0' && pathname[1] == ':')
1880
filename = pathname + 1;
1884
if (filename == NULL)
1885
filename = pathname;
1889
1942
length = strlen (filename);
1891
1944
if (length <= maxlen)
2005
2058
char buffer[DEFAULT_BUFFERSIZE];
2006
2059
unsigned int remaining = arelt_size (current);
2007
struct ar_hdr *hdr = arch_hdr (current);
2009
2061
/* Write ar header. */
2010
if (bfd_bwrite (hdr, sizeof (*hdr), arch)
2062
if (!_bfd_write_ar_hdr (arch, current))
2013
2064
if (bfd_is_thin_archive (arch))
2015
2066
if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0)
2285
firstreal += arelt_size (current) + sizeof (struct ar_hdr);
2336
struct areltdata *ared = arch_eltdata (current);
2338
firstreal += (ared->parsed_size + ared->extra_size
2339
+ sizeof (struct ar_hdr));
2286
2340
firstreal += firstreal % 2;
2287
2341
current = current->archive_next;