1 /* BFD back-end for archive files (libraries).
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
5 Written by Cygnus Support. Mostly Gumby Henkel-Wallace's fault.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program 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 of the License, or
12 (at your option) any later version.
14 This program 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.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
24 @setfilename archive-info
29 An archive (or library) is just another BFD. It has a symbol
30 table, although there's not much a user program will do with it.
32 The big difference between an archive BFD and an ordinary BFD
33 is that the archive doesn't have sections. Instead it has a
34 chain of BFDs that are considered its contents. These BFDs can
35 be manipulated like any other. The BFDs contained in an
36 archive opened for reading will all be opened for reading. You
37 may put either input or output BFDs into an archive opened for
38 output; they will be handled correctly when the archive is closed.
40 Use <<bfd_openr_next_archived_file>> to step through
41 the contents of an archive opened for input. You don't
42 have to read the entire archive if you don't want
43 to! Read it until you find what you want.
45 Archive contents of output BFDs are chained through the
46 <<next>> pointer in a BFD. The first one is findable through
47 the <<archive_head>> slot of the archive. Set it with
48 <<bfd_set_archive_head>> (q.v.). A given BFD may be in only one
49 open output archive at a time.
51 As expected, the BFD archive code is more general than the
52 archive code of any given environment. BFD archives may
53 contain files of different formats (e.g., a.out and coff) and
54 even different architectures. You may even place archives
55 recursively into archives!
57 This can cause unexpected confusion, since some archive
58 formats are more expressive than others. For instance, Intel
59 COFF archives can preserve long filenames; SunOS a.out archives
60 cannot. If you move a file from the first to the second
61 format and back again, the filename may be truncated.
62 Likewise, different a.out environments have different
63 conventions as to how they truncate filenames, whether they
64 preserve directory names in filenames, etc. When
65 interoperating with native tools, be sure your files are
68 Beware: most of these formats do not react well to the
69 presence of spaces in filenames. We do the best we can, but
70 can't always handle this case due to restrictions in the format of
71 archives. Many Unix utilities are braindead in regards to
72 spaces and such in filenames anyway, so this shouldn't be much
75 Archives are supported in BFD in <<archive.c>>.
82 o - all archive elements start on an even boundary, newline padded;
83 o - all arch headers are char *;
84 o - all arch headers are the same size (across architectures).
87 /* Some formats provide a way to cram a long filename into the short
88 (16 chars) space provided by a BSD archive. The trick is: make a
89 special "file" in the front of the archive, sort of like the SYMDEF
90 entry. If the filename is too long to fit, put it in the extended
91 name table, and use its index as the filename. To prevent
92 confusion prepend the index with a space. This means you can't
93 have filenames that start with a space, but then again, many Unix
94 utilities can't handle that anyway.
96 This scheme unfortunately requires that you stand on your head in
97 order to write an archive since you need to put a magic file at the
98 front, and need to touch every entry to do so. C'est la vie.
100 We support two variants of this idea:
101 The SVR4 format (extended name table is named "//"),
102 and an extended pseudo-BSD variant (extended name table is named
103 "ARFILENAMES/"). The origin of the latter format is uncertain.
105 BSD 4.4 uses a third scheme: It writes a long filename
106 directly after the header. This allows 'ar q' to work.
107 We currently can read BSD 4.4 archives, but not write them.
110 /* Summary of archive member names:
112 Symbol table (must be first):
113 "__.SYMDEF " - Symbol table, Berkeley style, produced by ranlib.
114 "/ " - Symbol table, system 5 style.
116 Long name table (must be before regular file members):
117 "// " - Long name table, System 5 R4 style.
118 "ARFILENAMES/ " - Long name table, non-standard extended BSD (not BSD 4.4).
120 Regular file members with short names:
121 "filename.o/ " - Regular file, System 5 style (embedded spaces ok).
122 "filename.o " - Regular file, Berkeley style (no embedded spaces).
124 Regular files with long names (or embedded spaces, for BSD variants):
125 "/18 " - SVR4 style, name at offset 18 in name table.
126 "#1/23 " - Long name (or embedded spaces) 23 characters long,
127 BSD 4.4 style, full name follows header.
128 Implemented for reading, not writing.
129 " 18 " - Long name 18 characters long, extended pseudo-BSD.
134 #include "libiberty.h"
137 #include "aout/ranlib.h"
138 #include "safe-ctype.h"
145 /* We keep a cache of archive filepointers to archive elements to
146 speed up searching the archive by filepos. We only add an entry to
147 the cache when we actually read one. We also don't sort the cache;
148 it's generally short enough to search linearly.
149 Note that the pointers here point to the front of the ar_hdr, not
150 to the front of the contents! */
156 #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
157 #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
159 #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
160 #define arch_hdr(bfd) ((struct ar_hdr *) arch_eltdata(bfd)->arch_header)
163 _bfd_ar_spacepad (char *p, size_t n, const char *fmt, long val)
167 snprintf (buf, sizeof (buf), fmt, val);
171 memcpy (p, buf, len);
172 memset (p + len, ' ', n - len);
179 _bfd_generic_mkarchive (bfd *abfd)
181 bfd_size_type amt = sizeof (struct artdata);
183 abfd->tdata.aout_ar_data = bfd_zalloc (abfd, amt);
184 if (bfd_ardata (abfd) == NULL)
187 /* Already cleared by bfd_zalloc above.
188 bfd_ardata (abfd)->cache = NULL;
189 bfd_ardata (abfd)->archive_head = NULL;
190 bfd_ardata (abfd)->symdefs = NULL;
191 bfd_ardata (abfd)->extended_names = NULL;
192 bfd_ardata (abfd)->extended_names_size = 0;
193 bfd_ardata (abfd)->tdata = NULL; */
203 symindex bfd_get_next_mapent
204 (bfd *abfd, symindex previous, carsym **sym);
207 Step through archive @var{abfd}'s symbol table (if it
208 has one). Successively update @var{sym} with the next symbol's
209 information, returning that symbol's (internal) index into the
212 Supply <<BFD_NO_MORE_SYMBOLS>> as the @var{previous} entry to get
213 the first one; returns <<BFD_NO_MORE_SYMBOLS>> when you've already
216 A <<carsym>> is a canonical archive symbol. The only
217 user-visible element is its name, a null-terminated string.
221 bfd_get_next_mapent (bfd *abfd, symindex prev, carsym **entry)
223 if (!bfd_has_map (abfd))
225 bfd_set_error (bfd_error_invalid_operation);
226 return BFD_NO_MORE_SYMBOLS;
229 if (prev == BFD_NO_MORE_SYMBOLS)
233 if (prev >= bfd_ardata (abfd)->symdef_count)
234 return BFD_NO_MORE_SYMBOLS;
236 *entry = (bfd_ardata (abfd)->symdefs + prev);
240 /* To be called by backends only. */
243 _bfd_create_empty_archive_element_shell (bfd *obfd)
245 return _bfd_new_bfd_contained_in (obfd);
253 bfd_boolean bfd_set_archive_head (bfd *output, bfd *new_head);
256 Set the head of the chain of
257 BFDs contained in the archive @var{output} to @var{new_head}.
261 bfd_set_archive_head (bfd *output_archive, bfd *new_head)
263 output_archive->archive_head = new_head;
268 _bfd_look_for_bfd_in_cache (bfd *arch_bfd, file_ptr filepos)
270 htab_t hash_table = bfd_ardata (arch_bfd)->cache;
276 struct ar_cache *entry = (struct ar_cache *) htab_find (hash_table, &m);
287 hash_file_ptr (const PTR p)
289 return (hashval_t) (((struct ar_cache *) p)->ptr);
292 /* Returns non-zero if P1 and P2 are equal. */
295 eq_file_ptr (const PTR p1, const PTR p2)
297 struct ar_cache *arc1 = (struct ar_cache *) p1;
298 struct ar_cache *arc2 = (struct ar_cache *) p2;
299 return arc1->ptr == arc2->ptr;
302 /* Kind of stupid to call cons for each one, but we don't do too many. */
305 _bfd_add_bfd_to_archive_cache (bfd *arch_bfd, file_ptr filepos, bfd *new_elt)
307 struct ar_cache *cache;
308 htab_t hash_table = bfd_ardata (arch_bfd)->cache;
310 /* If the hash table hasn't been created, create it. */
311 if (hash_table == NULL)
313 hash_table = htab_create_alloc (16, hash_file_ptr, eq_file_ptr,
315 if (hash_table == NULL)
317 bfd_ardata (arch_bfd)->cache = hash_table;
320 /* Insert new_elt into the hash table by filepos. */
321 cache = bfd_zalloc (arch_bfd, sizeof (struct ar_cache));
322 cache->ptr = filepos;
323 cache->arbfd = new_elt;
324 *htab_find_slot (hash_table, (const void *) cache, INSERT) = cache;
329 /* The name begins with space. Hence the rest of the name is an index into
333 get_extended_arelt_filename (bfd *arch, const char *name)
335 unsigned long index = 0;
337 /* Should extract string so that I can guarantee not to overflow into
338 the next region, but I'm too lazy. */
340 /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */
341 index = strtol (name + 1, NULL, 10);
342 if (errno != 0 || index >= bfd_ardata (arch)->extended_names_size)
344 bfd_set_error (bfd_error_malformed_archive);
348 return bfd_ardata (arch)->extended_names + index;
351 /* This functions reads an arch header and returns an areltdata pointer, or
354 Presumes the file pointer is already in the right place (ie pointing
355 to the ar_hdr in the file). Moves the file pointer; on success it
356 should be pointing to the front of the file contents; on failure it
357 could have been moved arbitrarily. */
360 _bfd_generic_read_ar_hdr (bfd *abfd)
362 return _bfd_generic_read_ar_hdr_mag (abfd, NULL);
365 /* Alpha ECOFF uses an optional different ARFMAG value, so we have a
366 variant of _bfd_generic_read_ar_hdr which accepts a magic string. */
369 _bfd_generic_read_ar_hdr_mag (bfd *abfd, const char *mag)
372 char *hdrp = (char *) &hdr;
374 struct areltdata *ared;
375 char *filename = NULL;
376 bfd_size_type namelen = 0;
377 bfd_size_type allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
380 if (bfd_bread (hdrp, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr))
382 if (bfd_get_error () != bfd_error_system_call)
383 bfd_set_error (bfd_error_no_more_archived_files);
386 if (strncmp (hdr.ar_fmag, ARFMAG, 2) != 0
388 || strncmp (hdr.ar_fmag, mag, 2) != 0))
390 bfd_set_error (bfd_error_malformed_archive);
395 parsed_size = strtol (hdr.ar_size, NULL, 10);
398 bfd_set_error (bfd_error_malformed_archive);
402 /* Extract the filename from the archive - there are two ways to
403 specify an extended name table, either the first char of the
404 name is a space, or it's a slash. */
405 if ((hdr.ar_name[0] == '/'
406 || (hdr.ar_name[0] == ' '
407 && memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)) == NULL))
408 && bfd_ardata (abfd)->extended_names != NULL)
410 filename = get_extended_arelt_filename (abfd, hdr.ar_name);
411 if (filename == NULL)
414 /* BSD4.4-style long filename.
415 Only implemented for reading, so far! */
416 else if (hdr.ar_name[0] == '#'
417 && hdr.ar_name[1] == '1'
418 && hdr.ar_name[2] == '/'
419 && ISDIGIT (hdr.ar_name[3]))
421 /* BSD-4.4 extended name */
422 namelen = atoi (&hdr.ar_name[3]);
423 allocsize += namelen + 1;
424 parsed_size -= namelen;
426 allocptr = bfd_zalloc (abfd, allocsize);
427 if (allocptr == NULL)
430 + sizeof (struct areltdata)
431 + sizeof (struct ar_hdr));
432 if (bfd_bread (filename, namelen, abfd) != namelen)
434 if (bfd_get_error () != bfd_error_system_call)
435 bfd_set_error (bfd_error_no_more_archived_files);
438 filename[namelen] = '\0';
442 /* We judge the end of the name by looking for '/' or ' '.
443 Note: The SYSV format (terminated by '/') allows embedded
444 spaces, so only look for ' ' if we don't find '/'. */
447 e = memchr (hdr.ar_name, '\0', ar_maxnamelen (abfd));
450 e = memchr (hdr.ar_name, '/', ar_maxnamelen (abfd));
452 e = memchr (hdr.ar_name, ' ', ar_maxnamelen (abfd));
456 namelen = e - hdr.ar_name;
459 /* If we didn't find a termination character, then the name
460 must be the entire field. */
461 namelen = ar_maxnamelen (abfd);
464 allocsize += namelen + 1;
469 allocptr = bfd_zalloc (abfd, allocsize);
470 if (allocptr == NULL)
474 ared = (struct areltdata *) allocptr;
476 ared->arch_header = allocptr + sizeof (struct areltdata);
477 memcpy (ared->arch_header, &hdr, sizeof (struct ar_hdr));
478 ared->parsed_size = parsed_size;
480 if (filename != NULL)
481 ared->filename = filename;
484 ared->filename = allocptr + (sizeof (struct areltdata) +
485 sizeof (struct ar_hdr));
487 memcpy (ared->filename, hdr.ar_name, namelen);
488 ared->filename[namelen] = '\0';
494 /* This is an internal function; it's mainly used when indexing
495 through the archive symbol table, but also used to get the next
496 element, since it handles the bookkeeping so nicely for us. */
499 _bfd_get_elt_at_filepos (bfd *archive, file_ptr filepos)
501 struct areltdata *new_areldata;
504 if (archive->my_archive)
506 filepos += archive->origin;
507 archive = archive->my_archive;
510 n_nfd = _bfd_look_for_bfd_in_cache (archive, filepos);
514 if (0 > bfd_seek (archive, filepos, SEEK_SET))
517 if ((new_areldata = _bfd_read_ar_hdr (archive)) == NULL)
520 n_nfd = _bfd_create_empty_archive_element_shell (archive);
523 bfd_release (archive, new_areldata);
527 n_nfd->origin = bfd_tell (archive);
528 n_nfd->arelt_data = new_areldata;
529 n_nfd->filename = new_areldata->filename;
531 if (_bfd_add_bfd_to_archive_cache (archive, filepos, n_nfd))
535 bfd_release (archive, n_nfd);
536 bfd_release (archive, new_areldata);
540 /* Return the BFD which is referenced by the symbol in ABFD indexed by
541 INDEX. INDEX should have been returned by bfd_get_next_mapent. */
544 _bfd_generic_get_elt_at_index (bfd *abfd, symindex index)
548 entry = bfd_ardata (abfd)->symdefs + index;
549 return _bfd_get_elt_at_filepos (abfd, entry->file_offset);
554 bfd_openr_next_archived_file
557 bfd *bfd_openr_next_archived_file (bfd *archive, bfd *previous);
560 Provided a BFD, @var{archive}, containing an archive and NULL, open
561 an input BFD on the first contained element and returns that.
562 Subsequent calls should pass
563 the archive and the previous return value to return a created
564 BFD to the next contained element. NULL is returned when there
569 bfd_openr_next_archived_file (bfd *archive, bfd *last_file)
571 if ((bfd_get_format (archive) != bfd_archive) ||
572 (archive->direction == write_direction))
574 bfd_set_error (bfd_error_invalid_operation);
578 return BFD_SEND (archive,
579 openr_next_archived_file, (archive, last_file));
583 bfd_generic_openr_next_archived_file (bfd *archive, bfd *last_file)
588 filestart = bfd_ardata (archive)->first_file_filepos;
591 unsigned int size = arelt_size (last_file);
592 filestart = last_file->origin + size;
593 if (archive->my_archive)
594 filestart -= archive->origin;
595 /* Pad to an even boundary...
596 Note that last_file->origin can be odd in the case of
597 BSD-4.4-style element with a long odd size. */
598 filestart += filestart % 2;
601 return _bfd_get_elt_at_filepos (archive, filestart);
605 bfd_generic_archive_p (bfd *abfd)
607 struct artdata *tdata_hold;
608 char armag[SARMAG + 1];
611 if (bfd_bread (armag, SARMAG, abfd) != SARMAG)
613 if (bfd_get_error () != bfd_error_system_call)
614 bfd_set_error (bfd_error_wrong_format);
618 if (strncmp (armag, ARMAG, SARMAG) != 0 &&
619 strncmp (armag, ARMAGB, SARMAG) != 0)
622 tdata_hold = bfd_ardata (abfd);
624 amt = sizeof (struct artdata);
625 bfd_ardata (abfd) = bfd_zalloc (abfd, amt);
626 if (bfd_ardata (abfd) == NULL)
628 bfd_ardata (abfd) = tdata_hold;
632 bfd_ardata (abfd)->first_file_filepos = SARMAG;
633 /* Cleared by bfd_zalloc above.
634 bfd_ardata (abfd)->cache = NULL;
635 bfd_ardata (abfd)->archive_head = NULL;
636 bfd_ardata (abfd)->symdefs = NULL;
637 bfd_ardata (abfd)->extended_names = NULL;
638 bfd_ardata (abfd)->extended_names_size = 0;
639 bfd_ardata (abfd)->tdata = NULL; */
641 if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd))
642 || !BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd)))
644 if (bfd_get_error () != bfd_error_system_call)
645 bfd_set_error (bfd_error_wrong_format);
646 bfd_release (abfd, bfd_ardata (abfd));
647 bfd_ardata (abfd) = tdata_hold;
651 if (bfd_has_map (abfd))
655 /* This archive has a map, so we may presume that the contents
656 are object files. Make sure that if the first file in the
657 archive can be recognized as an object file, it is for this
658 target. If not, assume that this is the wrong format. If
659 the first file is not an object file, somebody is doing
660 something weird, and we permit it so that ar -t will work.
662 This is done because any normal format will recognize any
663 normal archive, regardless of the format of the object files.
664 We do accept an empty archive. */
666 first = bfd_openr_next_archived_file (abfd, NULL);
669 first->target_defaulted = FALSE;
670 if (bfd_check_format (first, bfd_object)
671 && first->xvec != abfd->xvec)
673 bfd_set_error (bfd_error_wrong_object_format);
674 bfd_ardata (abfd) = tdata_hold;
677 /* And we ought to close `first' here too. */
684 /* Some constants for a 32 bit BSD archive structure. We do not
685 support 64 bit archives presently; so far as I know, none actually
686 exist. Supporting them would require changing these constants, and
687 changing some H_GET_32 to H_GET_64. */
689 /* The size of an external symdef structure. */
690 #define BSD_SYMDEF_SIZE 8
692 /* The offset from the start of a symdef structure to the file offset. */
693 #define BSD_SYMDEF_OFFSET_SIZE 4
695 /* The size of the symdef count. */
696 #define BSD_SYMDEF_COUNT_SIZE 4
698 /* The size of the string count. */
699 #define BSD_STRING_COUNT_SIZE 4
701 /* Returns FALSE on error, TRUE otherwise. */
704 do_slurp_bsd_armap (bfd *abfd)
706 struct areltdata *mapdata;
707 unsigned int counter;
708 bfd_byte *raw_armap, *rbase;
709 struct artdata *ardata = bfd_ardata (abfd);
711 bfd_size_type parsed_size, amt;
714 mapdata = _bfd_read_ar_hdr (abfd);
717 parsed_size = mapdata->parsed_size;
718 bfd_release (abfd, mapdata); /* Don't need it any more. */
720 raw_armap = bfd_zalloc (abfd, parsed_size);
721 if (raw_armap == NULL)
724 if (bfd_bread (raw_armap, parsed_size, abfd) != parsed_size)
726 if (bfd_get_error () != bfd_error_system_call)
727 bfd_set_error (bfd_error_malformed_archive);
729 bfd_release (abfd, raw_armap);
733 ardata->symdef_count = H_GET_32 (abfd, raw_armap) / BSD_SYMDEF_SIZE;
735 if (ardata->symdef_count * BSD_SYMDEF_SIZE >
736 parsed_size - BSD_SYMDEF_COUNT_SIZE)
738 /* Probably we're using the wrong byte ordering. */
739 bfd_set_error (bfd_error_wrong_format);
744 rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE;
745 stringbase = ((char *) rbase
746 + ardata->symdef_count * BSD_SYMDEF_SIZE
747 + BSD_STRING_COUNT_SIZE);
748 amt = ardata->symdef_count * sizeof (carsym);
749 ardata->symdefs = bfd_alloc (abfd, amt);
750 if (!ardata->symdefs)
753 for (counter = 0, set = ardata->symdefs;
754 counter < ardata->symdef_count;
755 counter++, set++, rbase += BSD_SYMDEF_SIZE)
757 set->name = H_GET_32 (abfd, rbase) + stringbase;
758 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
761 ardata->first_file_filepos = bfd_tell (abfd);
762 /* Pad to an even boundary if you have to. */
763 ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
764 /* FIXME, we should provide some way to free raw_ardata when
765 we are done using the strings from it. For now, it seems
766 to be allocated on an objalloc anyway... */
767 bfd_has_map (abfd) = TRUE;
771 /* Returns FALSE on error, TRUE otherwise. */
774 do_slurp_coff_armap (bfd *abfd)
776 struct areltdata *mapdata;
777 int *raw_armap, *rawptr;
778 struct artdata *ardata = bfd_ardata (abfd);
780 bfd_size_type stringsize;
781 unsigned int parsed_size;
783 bfd_size_type nsymz; /* Number of symbols in armap. */
784 bfd_vma (*swap) (const void *);
785 char int_buf[sizeof (long)];
786 bfd_size_type carsym_size, ptrsize;
789 mapdata = _bfd_read_ar_hdr (abfd);
792 parsed_size = mapdata->parsed_size;
793 bfd_release (abfd, mapdata); /* Don't need it any more. */
795 if (bfd_bread (int_buf, 4, abfd) != 4)
797 if (bfd_get_error () != bfd_error_system_call)
798 bfd_set_error (bfd_error_malformed_archive);
801 /* It seems that all numeric information in a coff archive is always
802 in big endian format, nomatter the host or target. */
804 nsymz = bfd_getb32 (int_buf);
805 stringsize = parsed_size - (4 * nsymz) - 4;
807 /* ... except that some archive formats are broken, and it may be our
808 fault - the i960 little endian coff sometimes has big and sometimes
809 little, because our tools changed. Here's a horrible hack to clean
812 if (stringsize > 0xfffff
813 && bfd_get_arch (abfd) == bfd_arch_i960
814 && bfd_get_flavour (abfd) == bfd_target_coff_flavour)
816 /* This looks dangerous, let's do it the other way around. */
817 nsymz = bfd_getl32 (int_buf);
818 stringsize = parsed_size - (4 * nsymz) - 4;
822 /* The coff armap must be read sequentially. So we construct a
823 bsd-style one in core all at once, for simplicity. */
825 if (nsymz > ~ (bfd_size_type) 0 / sizeof (carsym))
828 carsym_size = (nsymz * sizeof (carsym));
829 ptrsize = (4 * nsymz);
831 if (carsym_size + stringsize + 1 <= carsym_size)
834 ardata->symdefs = bfd_zalloc (abfd, carsym_size + stringsize + 1);
835 if (ardata->symdefs == NULL)
837 carsyms = ardata->symdefs;
838 stringbase = ((char *) ardata->symdefs) + carsym_size;
840 /* Allocate and read in the raw offsets. */
841 raw_armap = bfd_alloc (abfd, ptrsize);
842 if (raw_armap == NULL)
843 goto release_symdefs;
844 if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize
845 || (bfd_bread (stringbase, stringsize, abfd) != stringsize))
847 if (bfd_get_error () != bfd_error_system_call)
848 bfd_set_error (bfd_error_malformed_archive);
849 goto release_raw_armap;
852 /* OK, build the carsyms. */
853 for (i = 0; i < nsymz; i++)
855 rawptr = raw_armap + i;
856 carsyms->file_offset = swap ((bfd_byte *) rawptr);
857 carsyms->name = stringbase;
858 stringbase += strlen (stringbase) + 1;
863 ardata->symdef_count = nsymz;
864 ardata->first_file_filepos = bfd_tell (abfd);
865 /* Pad to an even boundary if you have to. */
866 ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
868 bfd_has_map (abfd) = TRUE;
869 bfd_release (abfd, raw_armap);
871 /* Check for a second archive header (as used by PE). */
873 struct areltdata *tmp;
875 bfd_seek (abfd, ardata->first_file_filepos, SEEK_SET);
876 tmp = _bfd_read_ar_hdr (abfd);
879 if (tmp->arch_header[0] == '/'
880 && tmp->arch_header[1] == ' ')
882 ardata->first_file_filepos +=
883 (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1;
885 bfd_release (abfd, tmp);
892 bfd_release (abfd, raw_armap);
894 bfd_release (abfd, (ardata)->symdefs);
898 /* This routine can handle either coff-style or bsd-style armaps.
899 Returns FALSE on error, TRUE otherwise */
902 bfd_slurp_armap (bfd *abfd)
905 int i = bfd_bread (nextname, 16, abfd);
912 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
915 if (CONST_STRNEQ (nextname, "__.SYMDEF ")
916 || CONST_STRNEQ (nextname, "__.SYMDEF/ ")) /* Old Linux archives. */
917 return do_slurp_bsd_armap (abfd);
918 else if (CONST_STRNEQ (nextname, "/ "))
919 return do_slurp_coff_armap (abfd);
920 else if (CONST_STRNEQ (nextname, "/SYM64/ "))
922 /* 64bit ELF (Irix 6) archive. */
924 extern bfd_boolean bfd_elf64_archive_slurp_armap (bfd *);
925 return bfd_elf64_archive_slurp_armap (abfd);
927 bfd_set_error (bfd_error_wrong_format);
932 bfd_has_map (abfd) = FALSE;
936 /* Returns FALSE on error, TRUE otherwise. */
937 /* Flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the
938 header is in a slightly different order and the map name is '/'.
939 This flavour is used by hp300hpux. */
941 #define HPUX_SYMDEF_COUNT_SIZE 2
944 bfd_slurp_bsd_armap_f2 (bfd *abfd)
946 struct areltdata *mapdata;
948 unsigned int counter;
949 bfd_byte *raw_armap, *rbase;
950 struct artdata *ardata = bfd_ardata (abfd);
952 unsigned int stringsize;
955 int i = bfd_bread (nextname, 16, abfd);
962 /* The archive has at least 16 bytes in it. */
963 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
966 if (CONST_STRNEQ (nextname, "__.SYMDEF ")
967 || CONST_STRNEQ (nextname, "__.SYMDEF/ ")) /* Old Linux archives. */
968 return do_slurp_bsd_armap (abfd);
970 if (! CONST_STRNEQ (nextname, "/ "))
972 bfd_has_map (abfd) = FALSE;
976 mapdata = _bfd_read_ar_hdr (abfd);
980 amt = mapdata->parsed_size;
981 raw_armap = bfd_zalloc (abfd, amt);
982 if (raw_armap == NULL)
985 bfd_release (abfd, mapdata);
989 if (bfd_bread (raw_armap, amt, abfd) != amt)
991 if (bfd_get_error () != bfd_error_system_call)
992 bfd_set_error (bfd_error_malformed_archive);
994 bfd_release (abfd, raw_armap);
998 ardata->symdef_count = H_GET_16 (abfd, raw_armap);
1000 if (ardata->symdef_count * BSD_SYMDEF_SIZE
1001 > mapdata->parsed_size - HPUX_SYMDEF_COUNT_SIZE)
1003 /* Probably we're using the wrong byte ordering. */
1004 bfd_set_error (bfd_error_wrong_format);
1010 stringsize = H_GET_32 (abfd, raw_armap + HPUX_SYMDEF_COUNT_SIZE);
1011 /* Skip sym count and string sz. */
1012 stringbase = ((char *) raw_armap
1013 + HPUX_SYMDEF_COUNT_SIZE
1014 + BSD_STRING_COUNT_SIZE);
1015 rbase = (bfd_byte *) stringbase + stringsize;
1016 amt = ardata->symdef_count * BSD_SYMDEF_SIZE;
1017 ardata->symdefs = bfd_alloc (abfd, amt);
1018 if (!ardata->symdefs)
1021 for (counter = 0, set = ardata->symdefs;
1022 counter < ardata->symdef_count;
1023 counter++, set++, rbase += BSD_SYMDEF_SIZE)
1025 set->name = H_GET_32 (abfd, rbase) + stringbase;
1026 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
1029 ardata->first_file_filepos = bfd_tell (abfd);
1030 /* Pad to an even boundary if you have to. */
1031 ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
1032 /* FIXME, we should provide some way to free raw_ardata when
1033 we are done using the strings from it. For now, it seems
1034 to be allocated on an objalloc anyway... */
1035 bfd_has_map (abfd) = TRUE;
1039 /** Extended name table.
1041 Normally archives support only 14-character filenames.
1043 Intel has extended the format: longer names are stored in a special
1044 element (the first in the archive, or second if there is an armap);
1045 the name in the ar_hdr is replaced by <space><index into filename
1046 element>. Index is the P.R. of an int (decimal). Data General have
1047 extended the format by using the prefix // for the special element. */
1049 /* Returns FALSE on error, TRUE otherwise. */
1052 _bfd_slurp_extended_name_table (bfd *abfd)
1055 struct areltdata *namedata;
1058 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
1059 we probably don't want to return TRUE. */
1060 bfd_seek (abfd, bfd_ardata (abfd)->first_file_filepos, SEEK_SET);
1061 if (bfd_bread (nextname, 16, abfd) == 16)
1063 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
1066 if (! CONST_STRNEQ (nextname, "ARFILENAMES/ ")
1067 && ! CONST_STRNEQ (nextname, "// "))
1069 bfd_ardata (abfd)->extended_names = NULL;
1070 bfd_ardata (abfd)->extended_names_size = 0;
1074 namedata = _bfd_read_ar_hdr (abfd);
1075 if (namedata == NULL)
1078 amt = namedata->parsed_size;
1082 bfd_ardata (abfd)->extended_names_size = amt;
1083 bfd_ardata (abfd)->extended_names = bfd_zalloc (abfd, amt + 1);
1084 if (bfd_ardata (abfd)->extended_names == NULL)
1087 bfd_release (abfd, namedata);
1091 if (bfd_bread (bfd_ardata (abfd)->extended_names, amt, abfd) != amt)
1093 if (bfd_get_error () != bfd_error_system_call)
1094 bfd_set_error (bfd_error_malformed_archive);
1095 bfd_release (abfd, (bfd_ardata (abfd)->extended_names));
1096 bfd_ardata (abfd)->extended_names = NULL;
1100 /* Since the archive is supposed to be printable if it contains
1101 text, the entries in the list are newline-padded, not null
1102 padded. In SVR4-style archives, the names also have a
1103 trailing '/'. DOS/NT created archive often have \ in them
1104 We'll fix all problems here.. */
1106 char *ext_names = bfd_ardata (abfd)->extended_names;
1107 char *temp = ext_names;
1108 char *limit = temp + namedata->parsed_size;
1109 for (; temp < limit; ++temp)
1111 if (*temp == '\012')
1112 temp[temp > ext_names && temp[-1] == '/' ? -1 : 0] = '\0';
1119 /* Pad to an even boundary if you have to. */
1120 bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd);
1121 bfd_ardata (abfd)->first_file_filepos +=
1122 (bfd_ardata (abfd)->first_file_filepos) % 2;
1124 /* FIXME, we can't release namedata here because it was allocated
1125 below extended_names on the objalloc... */
1132 /* Return a copy of the stuff in the filename between any :]> and a
1136 normalize (bfd *abfd, const char *file)
1142 first = file + strlen (file) - 1;
1145 while (first != file)
1149 if (*first == ':' || *first == ']' || *first == '>')
1157 copy = bfd_alloc (abfd, last - first + 1);
1161 memcpy (copy, first, last - first);
1162 copy[last - first] = 0;
1169 normalize (bfd *abfd ATTRIBUTE_UNUSED, const char *file)
1171 const char *filename = strrchr (file, '/');
1173 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
1175 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1176 char *bslash = strrchr (file, '\\');
1177 if (filename == NULL || (bslash != NULL && bslash > filename))
1179 if (filename == NULL && file[0] != '\0' && file[1] == ':')
1180 filename = file + 1;
1183 if (filename != NULL)
1191 /* Build a BFD style extended name table. */
1194 _bfd_archive_bsd_construct_extended_name_table (bfd *abfd,
1196 bfd_size_type *tablen,
1199 *name = "ARFILENAMES/";
1200 return _bfd_construct_extended_name_table (abfd, FALSE, tabloc, tablen);
1203 /* Build an SVR4 style extended name table. */
1206 _bfd_archive_coff_construct_extended_name_table (bfd *abfd,
1208 bfd_size_type *tablen,
1212 return _bfd_construct_extended_name_table (abfd, TRUE, tabloc, tablen);
1215 /* Follows archive_head and produces an extended name table if
1216 necessary. Returns (in tabloc) a pointer to an extended name
1217 table, and in tablen the length of the table. If it makes an entry
1218 it clobbers the filename so that the element may be written without
1219 further massage. Returns TRUE if it ran successfully, FALSE if
1220 something went wrong. A successful return may still involve a
1221 zero-length tablen! */
1224 _bfd_construct_extended_name_table (bfd *abfd,
1225 bfd_boolean trailing_slash,
1227 bfd_size_type *tablen)
1229 unsigned int maxname = abfd->xvec->ar_max_namelen;
1230 bfd_size_type total_namelen = 0;
1236 /* Figure out how long the table should be. */
1237 for (current = abfd->archive_head;
1239 current = current->archive_next)
1242 unsigned int thislen;
1244 normal = normalize (current, current->filename);
1248 thislen = strlen (normal);
1250 if (thislen > maxname
1251 && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
1254 if (thislen > maxname)
1256 /* Add one to leave room for \n. */
1257 total_namelen += thislen + 1;
1260 /* Leave room for trailing slash. */
1266 struct ar_hdr *hdr = arch_hdr (current);
1267 if (strncmp (normal, hdr->ar_name, thislen) != 0
1268 || (thislen < sizeof hdr->ar_name
1269 && hdr->ar_name[thislen] != ar_padchar (current)))
1271 /* Must have been using extended format even though it
1272 didn't need to. Fix it to use normal format. */
1273 memcpy (hdr->ar_name, normal, thislen);
1274 if (thislen < maxname
1275 || (thislen == maxname && thislen < sizeof hdr->ar_name))
1276 hdr->ar_name[thislen] = ar_padchar (current);
1281 if (total_namelen == 0)
1284 *tabloc = bfd_zalloc (abfd, total_namelen);
1285 if (*tabloc == NULL)
1288 *tablen = total_namelen;
1291 for (current = abfd->archive_head;
1293 current = current->archive_next)
1296 unsigned int thislen;
1298 normal = normalize (current, current->filename);
1302 thislen = strlen (normal);
1303 if (thislen > maxname)
1305 /* Works for now; may need to be re-engineered if we
1306 encounter an oddball archive format and want to
1307 generalise this hack. */
1308 struct ar_hdr *hdr = arch_hdr (current);
1309 strcpy (strptr, normal);
1310 if (! trailing_slash)
1311 strptr[thislen] = '\012';
1314 strptr[thislen] = '/';
1315 strptr[thislen + 1] = '\012';
1317 hdr->ar_name[0] = ar_padchar (current);
1318 _bfd_ar_spacepad (hdr->ar_name + 1, maxname - 1, "%-ld",
1320 strptr += thislen + 1;
1329 /* A couple of functions for creating ar_hdrs. */
1331 #ifdef HPUX_LARGE_AR_IDS
1332 /* Function to encode large UID/GID values according to HP. */
1335 hpux_uid_gid_encode (char str[6], long int id)
1339 str[5] = '@' + (id & 3);
1342 for (cnt = 4; cnt >= 0; --cnt, id >>= 6)
1343 str[cnt] = ' ' + (id & 0x3f);
1345 #endif /* HPUX_LARGE_AR_IDS */
1355 /* Takes a filename, returns an arelt_data for it, or NULL if it can't
1356 make one. The filename must refer to a filename in the filesystem.
1357 The filename field of the ar_hdr will NOT be initialized. If member
1358 is set, and it's an in-memory bfd, we fake it. */
1360 static struct areltdata *
1361 bfd_ar_hdr_from_filesystem (bfd *abfd, const char *filename, bfd *member)
1364 struct areltdata *ared;
1368 if (member && (member->flags & BFD_IN_MEMORY) != 0)
1370 /* Assume we just "made" the member, and fake it. */
1371 struct bfd_in_memory *bim = member->iostream;
1372 time (&status.st_mtime);
1373 status.st_uid = getuid ();
1374 status.st_gid = getgid ();
1375 status.st_mode = 0644;
1376 status.st_size = bim->size;
1378 else if (stat (filename, &status) != 0)
1380 bfd_set_error (bfd_error_system_call);
1384 amt = sizeof (struct ar_hdr) + sizeof (struct areltdata);
1385 ared = bfd_zalloc (abfd, amt);
1388 hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
1390 /* ar headers are space padded, not null padded! */
1391 memset (hdr, ' ', sizeof (struct ar_hdr));
1393 _bfd_ar_spacepad (hdr->ar_date, sizeof (hdr->ar_date), "%-12ld",
1395 #ifdef HPUX_LARGE_AR_IDS
1396 /* HP has a very "special" way to handle UID/GID's with numeric values
1398 if (status.st_uid > 99999)
1399 hpux_uid_gid_encode (hdr->ar_uid, (long) status.st_uid);
1402 _bfd_ar_spacepad (hdr->ar_uid, sizeof (hdr->ar_uid), "%ld",
1404 #ifdef HPUX_LARGE_AR_IDS
1405 /* HP has a very "special" way to handle UID/GID's with numeric values
1407 if (status.st_gid > 99999)
1408 hpux_uid_gid_encode (hdr->ar_gid, (long) status.st_gid);
1411 _bfd_ar_spacepad (hdr->ar_gid, sizeof (hdr->ar_gid), "%ld",
1413 _bfd_ar_spacepad (hdr->ar_mode, sizeof (hdr->ar_mode), "%-8lo",
1415 _bfd_ar_spacepad (hdr->ar_size, sizeof (hdr->ar_size), "%-10ld",
1417 memcpy (hdr->ar_fmag, ARFMAG, 2);
1418 ared->parsed_size = status.st_size;
1419 ared->arch_header = (char *) hdr;
1424 /* This is magic required by the "ar" program. Since it's
1425 undocumented, it's undocumented. You may think that it would take
1426 a strong stomach to write this, and it does, but it takes even a
1427 stronger stomach to try to code around such a thing! */
1429 struct ar_hdr *bfd_special_undocumented_glue (bfd *, const char *);
1432 bfd_special_undocumented_glue (bfd *abfd, const char *filename)
1434 struct areltdata *ar_elt = bfd_ar_hdr_from_filesystem (abfd, filename, 0);
1437 return (struct ar_hdr *) ar_elt->arch_header;
1440 /* Analogous to stat call. */
1443 bfd_generic_stat_arch_elt (bfd *abfd, struct stat *buf)
1448 if (abfd->arelt_data == NULL)
1450 bfd_set_error (bfd_error_invalid_operation);
1454 hdr = arch_hdr (abfd);
1456 #define foo(arelt, stelt, size) \
1457 buf->stelt = strtol (hdr->arelt, &aloser, size); \
1458 if (aloser == hdr->arelt) \
1461 /* Some platforms support special notations for large IDs. */
1462 #ifdef HPUX_LARGE_AR_IDS
1463 # define foo2(arelt, stelt, size) \
1464 if (hdr->arelt[5] == ' ') \
1466 foo (arelt, stelt, size); \
1471 for (buf->stelt = cnt = 0; cnt < 5; ++cnt) \
1473 if (hdr->arelt[cnt] < ' ' || hdr->arelt[cnt] > ' ' + 0x3f) \
1476 buf->stelt += hdr->arelt[cnt] - ' '; \
1478 if (hdr->arelt[5] < '@' || hdr->arelt[5] > '@' + 3) \
1481 buf->stelt += hdr->arelt[5] - '@'; \
1484 # define foo2(arelt, stelt, size) foo (arelt, stelt, size)
1487 foo (ar_date, st_mtime, 10);
1488 foo2 (ar_uid, st_uid, 10);
1489 foo2 (ar_gid, st_gid, 10);
1490 foo (ar_mode, st_mode, 8);
1492 buf->st_size = arch_eltdata (abfd)->parsed_size;
1498 bfd_dont_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
1500 /* FIXME: This interacts unpleasantly with ar's quick-append option.
1501 Fortunately ic960 users will never use that option. Fixing this
1502 is very hard; fortunately I know how to do it and will do so once
1503 intel's release is out the door. */
1505 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1507 const char *filename;
1508 size_t maxlen = ar_maxnamelen (abfd);
1510 if ((bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
1512 bfd_bsd_truncate_arname (abfd, pathname, arhdr);
1516 filename = normalize (abfd, pathname);
1517 if (filename == NULL)
1523 length = strlen (filename);
1525 if (length <= maxlen)
1526 memcpy (hdr->ar_name, filename, length);
1528 /* Add the padding character if there is room for it. */
1530 || (length == maxlen && length < sizeof hdr->ar_name))
1531 (hdr->ar_name)[length] = ar_padchar (abfd);
1535 bfd_bsd_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
1537 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1539 const char *filename = strrchr (pathname, '/');
1540 size_t maxlen = ar_maxnamelen (abfd);
1542 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
1544 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1545 char *bslash = strrchr (pathname, '\\');
1546 if (filename == NULL || (bslash != NULL && bslash > filename))
1548 if (filename == NULL && pathname[0] != '\0' && pathname[1] == ':')
1549 filename = pathname + 1;
1553 if (filename == NULL)
1554 filename = pathname;
1558 length = strlen (filename);
1560 if (length <= maxlen)
1561 memcpy (hdr->ar_name, filename, length);
1564 /* pathname: meet procrustes */
1565 memcpy (hdr->ar_name, filename, maxlen);
1569 if (length < maxlen)
1570 (hdr->ar_name)[length] = ar_padchar (abfd);
1573 /* Store name into ar header. Truncates the name to fit.
1574 1> strip pathname to be just the basename.
1575 2> if it's short enuf to fit, stuff it in.
1576 3> If it doesn't end with .o, truncate it to fit
1577 4> truncate it before the .o, append .o, stuff THAT in. */
1579 /* This is what gnu ar does. It's better but incompatible with the
1583 bfd_gnu_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
1585 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1587 const char *filename = strrchr (pathname, '/');
1588 size_t maxlen = ar_maxnamelen (abfd);
1590 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
1592 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1593 char *bslash = strrchr (pathname, '\\');
1594 if (filename == NULL || (bslash != NULL && bslash > filename))
1596 if (filename == NULL && pathname[0] != '\0' && pathname[1] == ':')
1597 filename = pathname + 1;
1601 if (filename == NULL)
1602 filename = pathname;
1606 length = strlen (filename);
1608 if (length <= maxlen)
1609 memcpy (hdr->ar_name, filename, length);
1611 { /* pathname: meet procrustes */
1612 memcpy (hdr->ar_name, filename, maxlen);
1613 if ((filename[length - 2] == '.') && (filename[length - 1] == 'o'))
1615 hdr->ar_name[maxlen - 2] = '.';
1616 hdr->ar_name[maxlen - 1] = 'o';
1622 (hdr->ar_name)[length] = ar_padchar (abfd);
1625 /* The BFD is open for write and has its format set to bfd_archive. */
1628 _bfd_write_archive_contents (bfd *arch)
1631 char *etable = NULL;
1632 bfd_size_type elength = 0;
1633 const char *ename = NULL;
1634 bfd_boolean makemap = bfd_has_map (arch);
1635 /* If no .o's, don't bother to make a map. */
1636 bfd_boolean hasobjects = FALSE;
1637 bfd_size_type wrote;
1640 /* Verify the viability of all entries; if any of them live in the
1641 filesystem (as opposed to living in an archive open for input)
1642 then construct a fresh ar_hdr for them. */
1643 for (current = arch->archive_head;
1645 current = current->archive_next)
1647 /* This check is checking the bfds for the objects we're reading
1648 from (which are usually either an object file or archive on
1649 disk), not the archive entries we're writing to. We don't
1650 actually create bfds for the archive members, we just copy
1651 them byte-wise when we write out the archive. */
1652 if (bfd_write_p (current))
1654 bfd_set_error (bfd_error_invalid_operation);
1657 if (!current->arelt_data)
1659 current->arelt_data =
1660 bfd_ar_hdr_from_filesystem (arch, current->filename, current);
1661 if (!current->arelt_data)
1664 /* Put in the file name. */
1665 BFD_SEND (arch, _bfd_truncate_arname,
1666 (arch, current->filename, (char *) arch_hdr (current)));
1669 if (makemap && ! hasobjects)
1670 { /* Don't bother if we won't make a map! */
1671 if ((bfd_check_format (current, bfd_object)))
1676 if (!BFD_SEND (arch, _bfd_construct_extended_name_table,
1677 (arch, &etable, &elength, &ename)))
1680 if (bfd_seek (arch, (file_ptr) 0, SEEK_SET) != 0)
1682 wrote = bfd_bwrite (ARMAG, SARMAG, arch);
1683 if (wrote != SARMAG)
1686 if (makemap && hasobjects)
1688 if (! _bfd_compute_and_write_armap (arch, (unsigned int) elength))
1696 memset (&hdr, ' ', sizeof (struct ar_hdr));
1697 memcpy (hdr.ar_name, ename, strlen (ename));
1698 /* Round size up to even number in archive header. */
1699 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld",
1700 (elength + 1) & ~(bfd_size_type) 1);
1701 memcpy (hdr.ar_fmag, ARFMAG, 2);
1702 if ((bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
1703 != sizeof (struct ar_hdr))
1704 || bfd_bwrite (etable, elength, arch) != elength)
1706 if ((elength % 2) == 1)
1708 if (bfd_bwrite ("\012", 1, arch) != 1)
1713 for (current = arch->archive_head;
1715 current = current->archive_next)
1717 char buffer[DEFAULT_BUFFERSIZE];
1718 unsigned int remaining = arelt_size (current);
1719 struct ar_hdr *hdr = arch_hdr (current);
1721 /* Write ar header. */
1722 if (bfd_bwrite (hdr, sizeof (*hdr), arch)
1725 if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0)
1729 unsigned int amt = DEFAULT_BUFFERSIZE;
1730 if (amt > remaining)
1733 if (bfd_bread (buffer, amt, current) != amt)
1735 if (bfd_get_error () != bfd_error_system_call)
1736 bfd_set_error (bfd_error_file_truncated);
1739 if (bfd_bwrite (buffer, amt, arch) != amt)
1743 if ((arelt_size (current) % 2) == 1)
1745 if (bfd_bwrite ("\012", 1, arch) != 1)
1750 if (makemap && hasobjects)
1752 /* Verify the timestamp in the archive file. If it would not be
1753 accepted by the linker, rewrite it until it would be. If
1754 anything odd happens, break out and just return. (The
1755 Berkeley linker checks the timestamp and refuses to read the
1756 table-of-contents if it is >60 seconds less than the file's
1757 modified-time. That painful hack requires this painful hack. */
1761 if (bfd_update_armap_timestamp (arch))
1763 (*_bfd_error_handler)
1764 (_("Warning: writing archive was slow: rewriting timestamp\n"));
1766 while (++tries < 6);
1772 bfd_set_error (bfd_error_on_input, current, bfd_get_error ());
1776 /* Note that the namidx for the first symbol is 0. */
1779 _bfd_compute_and_write_armap (bfd *arch, unsigned int elength)
1781 char *first_name = NULL;
1783 file_ptr elt_no = 0;
1784 struct orl *map = NULL;
1785 unsigned int orl_max = 1024; /* Fine initial default. */
1786 unsigned int orl_count = 0;
1788 asymbol **syms = NULL;
1793 /* Dunno if this is the best place for this info... */
1795 elength += sizeof (struct ar_hdr);
1796 elength += elength % 2;
1798 amt = orl_max * sizeof (struct orl);
1799 map = bfd_malloc (amt);
1803 /* We put the symbol names on the arch objalloc, and then discard
1805 first_name = bfd_alloc (arch, 1);
1806 if (first_name == NULL)
1809 /* Drop all the files called __.SYMDEF, we're going to make our own. */
1810 while (arch->archive_head &&
1811 strcmp (arch->archive_head->filename, "__.SYMDEF") == 0)
1812 arch->archive_head = arch->archive_head->archive_next;
1814 /* Map over each element. */
1815 for (current = arch->archive_head;
1817 current = current->archive_next, elt_no++)
1819 if (bfd_check_format (current, bfd_object)
1820 && (bfd_get_file_flags (current) & HAS_SYMS) != 0)
1826 storage = bfd_get_symtab_upper_bound (current);
1832 if (storage > syms_max)
1837 syms = bfd_malloc (syms_max);
1841 symcount = bfd_canonicalize_symtab (current, syms);
1845 /* Now map over all the symbols, picking out the ones we
1847 for (src_count = 0; src_count < symcount; src_count++)
1849 flagword flags = (syms[src_count])->flags;
1850 asection *sec = syms[src_count]->section;
1852 if ((flags & BSF_GLOBAL ||
1854 flags & BSF_INDIRECT ||
1855 bfd_is_com_section (sec))
1856 && ! bfd_is_und_section (sec))
1858 bfd_size_type namelen;
1859 struct orl *new_map;
1861 /* This symbol will go into the archive header. */
1862 if (orl_count == orl_max)
1865 amt = orl_max * sizeof (struct orl);
1866 new_map = bfd_realloc (map, amt);
1867 if (new_map == NULL)
1873 namelen = strlen (syms[src_count]->name);
1874 amt = sizeof (char *);
1875 map[orl_count].name = bfd_alloc (arch, amt);
1876 if (map[orl_count].name == NULL)
1878 *(map[orl_count].name) = bfd_alloc (arch, namelen + 1);
1879 if (*(map[orl_count].name) == NULL)
1881 strcpy (*(map[orl_count].name), syms[src_count]->name);
1882 map[orl_count].u.abfd = current;
1883 map[orl_count].namidx = stridx;
1885 stridx += namelen + 1;
1891 /* Now ask the BFD to free up any cached information, so we
1892 don't fill all of memory with symbol tables. */
1893 if (! bfd_free_cached_info (current))
1898 /* OK, now we have collected all the data, let's write them out. */
1899 ret = BFD_SEND (arch, write_armap,
1900 (arch, elength, map, orl_count, stridx));
1906 if (first_name != NULL)
1907 bfd_release (arch, first_name);
1916 if (first_name != NULL)
1917 bfd_release (arch, first_name);
1923 bsd_write_armap (bfd *arch,
1924 unsigned int elength,
1926 unsigned int orl_count,
1929 int padit = stridx & 1;
1930 unsigned int ranlibsize = orl_count * BSD_SYMDEF_SIZE;
1931 unsigned int stringsize = stridx + padit;
1932 /* Include 8 bytes to store ranlibsize and stringsize in output. */
1933 unsigned int mapsize = ranlibsize + stringsize + 8;
1935 bfd *current = arch->archive_head;
1936 bfd *last_elt = current; /* Last element arch seen. */
1940 struct stat statbuf;
1942 firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
1944 stat (arch->filename, &statbuf);
1945 memset (&hdr, ' ', sizeof (struct ar_hdr));
1946 memcpy (hdr.ar_name, RANLIBMAG, strlen (RANLIBMAG));
1947 /* Remember the timestamp, to keep it holy. But fudge it a little. */
1948 bfd_ardata (arch)->armap_timestamp = statbuf.st_mtime + ARMAP_TIME_OFFSET;
1949 bfd_ardata (arch)->armap_datepos = (SARMAG
1950 + offsetof (struct ar_hdr, ar_date[0]));
1951 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
1952 bfd_ardata (arch)->armap_timestamp);
1953 _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", getuid ());
1954 _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", getgid ());
1955 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld", mapsize);
1956 memcpy (hdr.ar_fmag, ARFMAG, 2);
1957 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
1958 != sizeof (struct ar_hdr))
1960 H_PUT_32 (arch, ranlibsize, temp);
1961 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp))
1964 for (count = 0; count < orl_count; count++)
1966 bfd_byte buf[BSD_SYMDEF_SIZE];
1968 if (map[count].u.abfd != last_elt)
1972 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
1973 firstreal += firstreal % 2;
1974 current = current->archive_next;
1976 while (current != map[count].u.abfd);
1980 H_PUT_32 (arch, map[count].namidx, buf);
1981 H_PUT_32 (arch, firstreal, buf + BSD_SYMDEF_OFFSET_SIZE);
1982 if (bfd_bwrite (buf, BSD_SYMDEF_SIZE, arch)
1987 /* Now write the strings themselves. */
1988 H_PUT_32 (arch, stringsize, temp);
1989 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp))
1991 for (count = 0; count < orl_count; count++)
1993 size_t len = strlen (*map[count].name) + 1;
1995 if (bfd_bwrite (*map[count].name, len, arch) != len)
1999 /* The spec sez this should be a newline. But in order to be
2000 bug-compatible for sun's ar we use a null. */
2003 if (bfd_bwrite ("", 1, arch) != 1)
2010 /* At the end of archive file handling, update the timestamp in the
2011 file, so the linker will accept it.
2013 Return TRUE if the timestamp was OK, or an unusual problem happened.
2014 Return FALSE if we updated the timestamp. */
2017 _bfd_archive_bsd_update_armap_timestamp (bfd *arch)
2019 struct stat archstat;
2022 /* Flush writes, get last-write timestamp from file, and compare it
2023 to the timestamp IN the file. */
2025 if (bfd_stat (arch, &archstat) == -1)
2027 bfd_perror (_("Reading archive file mod timestamp"));
2029 /* Can't read mod time for some reason. */
2032 if (archstat.st_mtime <= bfd_ardata (arch)->armap_timestamp)
2033 /* OK by the linker's rules. */
2036 /* Update the timestamp. */
2037 bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET;
2039 /* Prepare an ASCII version suitable for writing. */
2040 memset (hdr.ar_date, ' ', sizeof (hdr.ar_date));
2041 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2042 bfd_ardata (arch)->armap_timestamp);
2044 /* Write it into the file. */
2045 bfd_ardata (arch)->armap_datepos = (SARMAG
2046 + offsetof (struct ar_hdr, ar_date[0]));
2047 if (bfd_seek (arch, bfd_ardata (arch)->armap_datepos, SEEK_SET) != 0
2048 || (bfd_bwrite (hdr.ar_date, sizeof (hdr.ar_date), arch)
2049 != sizeof (hdr.ar_date)))
2051 bfd_perror (_("Writing updated armap timestamp"));
2053 /* Some error while writing. */
2057 /* We updated the timestamp successfully. */
2061 /* A coff armap looks like :
2063 struct ar_hdr with name = '/'
2065 offset of file for symbol 0
2066 offset of file for symbol 1
2068 offset of file for symbol n-1
2075 coff_write_armap (bfd *arch,
2076 unsigned int elength,
2078 unsigned int symbol_count,
2081 /* The size of the ranlib is the number of exported symbols in the
2082 archive * the number of bytes in an int, + an int for the count. */
2083 unsigned int ranlibsize = (symbol_count * 4) + 4;
2084 unsigned int stringsize = stridx;
2085 unsigned int mapsize = stringsize + ranlibsize;
2086 unsigned int archive_member_file_ptr;
2087 bfd *current = arch->archive_head;
2090 int padit = mapsize & 1;
2095 /* Work out where the first object file will go in the archive. */
2096 archive_member_file_ptr = (mapsize
2098 + sizeof (struct ar_hdr)
2101 memset (&hdr, ' ', sizeof (struct ar_hdr));
2102 hdr.ar_name[0] = '/';
2103 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld",
2105 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2107 /* This, at least, is what Intel coff sets the values to. */
2108 _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", 0);
2109 _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", 0);
2110 _bfd_ar_spacepad (hdr.ar_mode, sizeof (hdr.ar_mode), "%-7lo", 0);
2111 memcpy (hdr.ar_fmag, ARFMAG, 2);
2113 /* Write the ar header for this item and the number of symbols. */
2114 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
2115 != sizeof (struct ar_hdr))
2118 if (!bfd_write_bigendian_4byte_int (arch, symbol_count))
2121 /* Two passes, first write the file offsets for each symbol -
2122 remembering that each offset is on a two byte boundary. */
2124 /* Write out the file offset for the file associated with each
2125 symbol, and remember to keep the offsets padded out. */
2127 current = arch->archive_head;
2129 while (current != NULL && count < symbol_count)
2131 /* For each symbol which is used defined in this object, write
2132 out the object file's address in the archive. */
2134 while (count < symbol_count && map[count].u.abfd == current)
2136 if (!bfd_write_bigendian_4byte_int (arch, archive_member_file_ptr))
2140 /* Add size of this archive entry. */
2141 archive_member_file_ptr += arelt_size (current) + sizeof (struct ar_hdr);
2142 /* Remember aboout the even alignment. */
2143 archive_member_file_ptr += archive_member_file_ptr % 2;
2144 current = current->archive_next;
2147 /* Now write the strings themselves. */
2148 for (count = 0; count < symbol_count; count++)
2150 size_t len = strlen (*map[count].name) + 1;
2152 if (bfd_bwrite (*map[count].name, len, arch) != len)
2156 /* The spec sez this should be a newline. But in order to be
2157 bug-compatible for arc960 we use a null. */
2160 if (bfd_bwrite ("", 1, arch) != 1)