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
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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>>.
80 o - all archive elements start on an even boundary, newline padded;
81 o - all arch headers are char *;
82 o - all arch headers are the same size (across architectures).
85 /* Some formats provide a way to cram a long filename into the short
86 (16 chars) space provided by a BSD archive. The trick is: make a
87 special "file" in the front of the archive, sort of like the SYMDEF
88 entry. If the filename is too long to fit, put it in the extended
89 name table, and use its index as the filename. To prevent
90 confusion prepend the index with a space. This means you can't
91 have filenames that start with a space, but then again, many Unix
92 utilities can't handle that anyway.
94 This scheme unfortunately requires that you stand on your head in
95 order to write an archive since you need to put a magic file at the
96 front, and need to touch every entry to do so. C'est la vie.
98 We support two variants of this idea:
99 The SVR4 format (extended name table is named "//"),
100 and an extended pseudo-BSD variant (extended name table is named
101 "ARFILENAMES/"). The origin of the latter format is uncertain.
103 BSD 4.4 uses a third scheme: It writes a long filename
104 directly after the header. This allows 'ar q' to work.
105 We currently can read BSD 4.4 archives, but not write them.
108 /* Summary of archive member names:
110 Symbol table (must be first):
111 "__.SYMDEF " - Symbol table, Berkeley style, produced by ranlib.
112 "/ " - Symbol table, system 5 style.
114 Long name table (must be before regular file members):
115 "// " - Long name table, System 5 R4 style.
116 "ARFILENAMES/ " - Long name table, non-standard extended BSD (not BSD 4.4).
118 Regular file members with short names:
119 "filename.o/ " - Regular file, System 5 style (embedded spaces ok).
120 "filename.o " - Regular file, Berkeley style (no embedded spaces).
122 Regular files with long names (or embedded spaces, for BSD variants):
123 "/18 " - SVR4 style, name at offset 18 in name table.
124 "#1/23 " - Long name (or embedded paces) 23 characters long,
125 BSD 4.4 style, full name follows header.
126 Implemented for reading, not writing.
127 " 18 " - Long name 18 characters long, extended pseudo-BSD.
134 #include "aout/ranlib.h"
135 #include "safe-ctype.h"
141 /* We keep a cache of archive filepointers to archive elements to
142 speed up searching the archive by filepos. We only add an entry to
143 the cache when we actually read one. We also don't sort the cache;
144 it's generally short enough to search linearly.
145 Note that the pointers here point to the front of the ar_hdr, not
146 to the front of the contents! */
150 struct ar_cache *next;
153 #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
154 #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
156 #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
157 #define arch_hdr(bfd) ((struct ar_hdr *) arch_eltdata(bfd)->arch_header)
161 _bfd_generic_mkarchive (bfd *abfd)
163 bfd_size_type amt = sizeof (struct artdata);
165 abfd->tdata.aout_ar_data = bfd_zalloc (abfd, amt);
166 if (bfd_ardata (abfd) == NULL)
169 bfd_ardata (abfd)->cache = NULL;
170 bfd_ardata (abfd)->archive_head = NULL;
171 bfd_ardata (abfd)->symdefs = NULL;
172 bfd_ardata (abfd)->extended_names = NULL;
173 bfd_ardata (abfd)->tdata = NULL;
183 symindex bfd_get_next_mapent
184 (bfd *abfd, symindex previous, carsym **sym);
187 Step through archive @var{abfd}'s symbol table (if it
188 has one). Successively update @var{sym} with the next symbol's
189 information, returning that symbol's (internal) index into the
192 Supply <<BFD_NO_MORE_SYMBOLS>> as the @var{previous} entry to get
193 the first one; returns <<BFD_NO_MORE_SYMBOLS>> when you've already
196 A <<carsym>> is a canonical archive symbol. The only
197 user-visible element is its name, a null-terminated string.
201 bfd_get_next_mapent (bfd *abfd, symindex prev, carsym **entry)
203 if (!bfd_has_map (abfd))
205 bfd_set_error (bfd_error_invalid_operation);
206 return BFD_NO_MORE_SYMBOLS;
209 if (prev == BFD_NO_MORE_SYMBOLS)
213 if (prev >= bfd_ardata (abfd)->symdef_count)
214 return BFD_NO_MORE_SYMBOLS;
216 *entry = (bfd_ardata (abfd)->symdefs + prev);
220 /* To be called by backends only. */
223 _bfd_create_empty_archive_element_shell (bfd *obfd)
225 return _bfd_new_bfd_contained_in (obfd);
233 bfd_boolean bfd_set_archive_head (bfd *output, bfd *new_head);
236 Set the head of the chain of
237 BFDs contained in the archive @var{output} to @var{new_head}.
241 bfd_set_archive_head (bfd *output_archive, bfd *new_head)
243 output_archive->archive_head = new_head;
248 _bfd_look_for_bfd_in_cache (bfd *arch_bfd, file_ptr filepos)
250 struct ar_cache *current;
252 for (current = bfd_ardata (arch_bfd)->cache; current != NULL;
253 current = current->next)
254 if (current->ptr == filepos)
255 return current->arelt;
260 /* Kind of stupid to call cons for each one, but we don't do too many. */
263 _bfd_add_bfd_to_archive_cache (bfd *arch_bfd, file_ptr filepos, bfd *new_elt)
265 bfd_size_type amt = sizeof (struct ar_cache);
267 struct ar_cache *new_cache = bfd_zalloc (arch_bfd, amt);
268 if (new_cache == NULL)
271 new_cache->ptr = filepos;
272 new_cache->arelt = new_elt;
273 new_cache->next = NULL;
274 if (bfd_ardata (arch_bfd)->cache == NULL)
275 bfd_ardata (arch_bfd)->cache = new_cache;
278 struct ar_cache *current = bfd_ardata (arch_bfd)->cache;
280 while (current->next != NULL)
281 current = current->next;
282 current->next = new_cache;
288 /* The name begins with space. Hence the rest of the name is an index into
292 get_extended_arelt_filename (bfd *arch, const char *name)
294 unsigned long index = 0;
296 /* Should extract string so that I can guarantee not to overflow into
297 the next region, but I'm too lazy. */
299 /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */
300 index = strtol (name + 1, NULL, 10);
303 bfd_set_error (bfd_error_malformed_archive);
307 return bfd_ardata (arch)->extended_names + index;
310 /* This functions reads an arch header and returns an areltdata pointer, or
313 Presumes the file pointer is already in the right place (ie pointing
314 to the ar_hdr in the file). Moves the file pointer; on success it
315 should be pointing to the front of the file contents; on failure it
316 could have been moved arbitrarily. */
319 _bfd_generic_read_ar_hdr (bfd *abfd)
321 return _bfd_generic_read_ar_hdr_mag (abfd, NULL);
324 /* Alpha ECOFF uses an optional different ARFMAG value, so we have a
325 variant of _bfd_generic_read_ar_hdr which accepts a magic string. */
328 _bfd_generic_read_ar_hdr_mag (bfd *abfd, const char *mag)
331 char *hdrp = (char *) &hdr;
333 struct areltdata *ared;
334 char *filename = NULL;
335 bfd_size_type namelen = 0;
336 bfd_size_type allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
339 if (bfd_bread (hdrp, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr))
341 if (bfd_get_error () != bfd_error_system_call)
342 bfd_set_error (bfd_error_no_more_archived_files);
345 if (strncmp (hdr.ar_fmag, ARFMAG, 2) != 0
347 || strncmp (hdr.ar_fmag, mag, 2) != 0))
349 bfd_set_error (bfd_error_malformed_archive);
354 parsed_size = strtol (hdr.ar_size, NULL, 10);
357 bfd_set_error (bfd_error_malformed_archive);
361 /* Extract the filename from the archive - there are two ways to
362 specify an extended name table, either the first char of the
363 name is a space, or it's a slash. */
364 if ((hdr.ar_name[0] == '/'
365 || (hdr.ar_name[0] == ' '
366 && memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)) == NULL))
367 && bfd_ardata (abfd)->extended_names != NULL)
369 filename = get_extended_arelt_filename (abfd, hdr.ar_name);
370 if (filename == NULL)
372 bfd_set_error (bfd_error_malformed_archive);
376 /* BSD4.4-style long filename.
377 Only implemented for reading, so far! */
378 else if (hdr.ar_name[0] == '#'
379 && hdr.ar_name[1] == '1'
380 && hdr.ar_name[2] == '/'
381 && ISDIGIT (hdr.ar_name[3]))
383 /* BSD-4.4 extended name */
384 namelen = atoi (&hdr.ar_name[3]);
385 allocsize += namelen + 1;
386 parsed_size -= namelen;
388 allocptr = bfd_zalloc (abfd, allocsize);
389 if (allocptr == NULL)
392 + sizeof (struct areltdata)
393 + sizeof (struct ar_hdr));
394 if (bfd_bread (filename, namelen, abfd) != namelen)
396 if (bfd_get_error () != bfd_error_system_call)
397 bfd_set_error (bfd_error_no_more_archived_files);
400 filename[namelen] = '\0';
404 /* We judge the end of the name by looking for '/' or ' '.
405 Note: The SYSV format (terminated by '/') allows embedded
406 spaces, so only look for ' ' if we don't find '/'. */
409 e = memchr (hdr.ar_name, '\0', ar_maxnamelen (abfd));
412 e = memchr (hdr.ar_name, '/', ar_maxnamelen (abfd));
414 e = memchr (hdr.ar_name, ' ', ar_maxnamelen (abfd));
418 namelen = e - hdr.ar_name;
421 /* If we didn't find a termination character, then the name
422 must be the entire field. */
423 namelen = ar_maxnamelen (abfd);
426 allocsize += namelen + 1;
431 allocptr = bfd_zalloc (abfd, allocsize);
432 if (allocptr == NULL)
436 ared = (struct areltdata *) allocptr;
438 ared->arch_header = allocptr + sizeof (struct areltdata);
439 memcpy (ared->arch_header, &hdr, sizeof (struct ar_hdr));
440 ared->parsed_size = parsed_size;
442 if (filename != NULL)
443 ared->filename = filename;
446 ared->filename = allocptr + (sizeof (struct areltdata) +
447 sizeof (struct ar_hdr));
449 memcpy (ared->filename, hdr.ar_name, namelen);
450 ared->filename[namelen] = '\0';
456 /* This is an internal function; it's mainly used when indexing
457 through the archive symbol table, but also used to get the next
458 element, since it handles the bookkeeping so nicely for us. */
461 _bfd_get_elt_at_filepos (bfd *archive, file_ptr filepos)
463 struct areltdata *new_areldata;
466 if (archive->my_archive)
468 filepos += archive->origin;
469 archive = archive->my_archive;
472 n_nfd = _bfd_look_for_bfd_in_cache (archive, filepos);
476 if (0 > bfd_seek (archive, filepos, SEEK_SET))
479 if ((new_areldata = _bfd_read_ar_hdr (archive)) == NULL)
482 n_nfd = _bfd_create_empty_archive_element_shell (archive);
485 bfd_release (archive, new_areldata);
489 n_nfd->origin = bfd_tell (archive);
490 n_nfd->arelt_data = new_areldata;
491 n_nfd->filename = new_areldata->filename;
493 if (_bfd_add_bfd_to_archive_cache (archive, filepos, n_nfd))
497 bfd_release (archive, n_nfd);
498 bfd_release (archive, new_areldata);
502 /* Return the BFD which is referenced by the symbol in ABFD indexed by
503 INDEX. INDEX should have been returned by bfd_get_next_mapent. */
506 _bfd_generic_get_elt_at_index (bfd *abfd, symindex index)
510 entry = bfd_ardata (abfd)->symdefs + index;
511 return _bfd_get_elt_at_filepos (abfd, entry->file_offset);
516 bfd_openr_next_archived_file
519 bfd *bfd_openr_next_archived_file (bfd *archive, bfd *previous);
522 Provided a BFD, @var{archive}, containing an archive and NULL, open
523 an input BFD on the first contained element and returns that.
524 Subsequent calls should pass
525 the archive and the previous return value to return a created
526 BFD to the next contained element. NULL is returned when there
531 bfd_openr_next_archived_file (bfd *archive, bfd *last_file)
533 if ((bfd_get_format (archive) != bfd_archive) ||
534 (archive->direction == write_direction))
536 bfd_set_error (bfd_error_invalid_operation);
540 return BFD_SEND (archive,
541 openr_next_archived_file, (archive, last_file));
545 bfd_generic_openr_next_archived_file (bfd *archive, bfd *last_file)
550 filestart = bfd_ardata (archive)->first_file_filepos;
553 unsigned int size = arelt_size (last_file);
554 filestart = last_file->origin + size;
555 if (archive->my_archive)
556 filestart -= archive->origin;
557 /* Pad to an even boundary...
558 Note that last_file->origin can be odd in the case of
559 BSD-4.4-style element with a long odd size. */
560 filestart += filestart % 2;
563 return _bfd_get_elt_at_filepos (archive, filestart);
567 bfd_generic_archive_p (bfd *abfd)
569 struct artdata *tdata_hold;
570 char armag[SARMAG + 1];
573 if (bfd_bread (armag, SARMAG, abfd) != SARMAG)
575 if (bfd_get_error () != bfd_error_system_call)
576 bfd_set_error (bfd_error_wrong_format);
580 if (strncmp (armag, ARMAG, SARMAG) != 0 &&
581 strncmp (armag, ARMAGB, SARMAG) != 0)
584 tdata_hold = bfd_ardata (abfd);
586 amt = sizeof (struct artdata);
587 bfd_ardata (abfd) = bfd_zalloc (abfd, amt);
588 if (bfd_ardata (abfd) == NULL)
590 bfd_ardata (abfd) = tdata_hold;
594 bfd_ardata (abfd)->first_file_filepos = SARMAG;
595 bfd_ardata (abfd)->cache = NULL;
596 bfd_ardata (abfd)->archive_head = NULL;
597 bfd_ardata (abfd)->symdefs = NULL;
598 bfd_ardata (abfd)->extended_names = NULL;
599 bfd_ardata (abfd)->tdata = NULL;
601 if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd))
602 || !BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd)))
604 if (bfd_get_error () != bfd_error_system_call)
605 bfd_set_error (bfd_error_wrong_format);
606 bfd_release (abfd, bfd_ardata (abfd));
607 bfd_ardata (abfd) = tdata_hold;
611 if (bfd_has_map (abfd))
615 /* This archive has a map, so we may presume that the contents
616 are object files. Make sure that if the first file in the
617 archive can be recognized as an object file, it is for this
618 target. If not, assume that this is the wrong format. If
619 the first file is not an object file, somebody is doing
620 something weird, and we permit it so that ar -t will work.
622 This is done because any normal format will recognize any
623 normal archive, regardless of the format of the object files.
624 We do accept an empty archive. */
626 first = bfd_openr_next_archived_file (abfd, NULL);
631 first->target_defaulted = FALSE;
633 if (bfd_check_format (first, bfd_object)
634 && first->xvec != abfd->xvec)
636 bfd_set_error (bfd_error_wrong_object_format);
637 bfd_ardata (abfd) = tdata_hold;
640 /* And we ought to close `first' here too. */
647 /* Some constants for a 32 bit BSD archive structure. We do not
648 support 64 bit archives presently; so far as I know, none actually
649 exist. Supporting them would require changing these constants, and
650 changing some H_GET_32 to H_GET_64. */
652 /* The size of an external symdef structure. */
653 #define BSD_SYMDEF_SIZE 8
655 /* The offset from the start of a symdef structure to the file offset. */
656 #define BSD_SYMDEF_OFFSET_SIZE 4
658 /* The size of the symdef count. */
659 #define BSD_SYMDEF_COUNT_SIZE 4
661 /* The size of the string count. */
662 #define BSD_STRING_COUNT_SIZE 4
664 /* Returns FALSE on error, TRUE otherwise. */
667 do_slurp_bsd_armap (bfd *abfd)
669 struct areltdata *mapdata;
670 unsigned int counter;
671 bfd_byte *raw_armap, *rbase;
672 struct artdata *ardata = bfd_ardata (abfd);
674 bfd_size_type parsed_size, amt;
677 mapdata = _bfd_read_ar_hdr (abfd);
680 parsed_size = mapdata->parsed_size;
681 bfd_release (abfd, mapdata); /* Don't need it any more. */
683 raw_armap = bfd_zalloc (abfd, parsed_size);
684 if (raw_armap == NULL)
687 if (bfd_bread (raw_armap, parsed_size, abfd) != parsed_size)
689 if (bfd_get_error () != bfd_error_system_call)
690 bfd_set_error (bfd_error_malformed_archive);
692 bfd_release (abfd, raw_armap);
696 ardata->symdef_count = H_GET_32 (abfd, raw_armap) / BSD_SYMDEF_SIZE;
698 if (ardata->symdef_count * BSD_SYMDEF_SIZE >
699 parsed_size - BSD_SYMDEF_COUNT_SIZE)
701 /* Probably we're using the wrong byte ordering. */
702 bfd_set_error (bfd_error_wrong_format);
707 rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE;
708 stringbase = ((char *) rbase
709 + ardata->symdef_count * BSD_SYMDEF_SIZE
710 + BSD_STRING_COUNT_SIZE);
711 amt = ardata->symdef_count * sizeof (carsym);
712 ardata->symdefs = bfd_alloc (abfd, amt);
713 if (!ardata->symdefs)
716 for (counter = 0, set = ardata->symdefs;
717 counter < ardata->symdef_count;
718 counter++, set++, rbase += BSD_SYMDEF_SIZE)
720 set->name = H_GET_32 (abfd, rbase) + stringbase;
721 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
724 ardata->first_file_filepos = bfd_tell (abfd);
725 /* Pad to an even boundary if you have to. */
726 ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
727 /* FIXME, we should provide some way to free raw_ardata when
728 we are done using the strings from it. For now, it seems
729 to be allocated on an objalloc anyway... */
730 bfd_has_map (abfd) = TRUE;
734 /* Returns FALSE on error, TRUE otherwise. */
737 do_slurp_coff_armap (bfd *abfd)
739 struct areltdata *mapdata;
740 int *raw_armap, *rawptr;
741 struct artdata *ardata = bfd_ardata (abfd);
743 bfd_size_type stringsize;
744 unsigned int parsed_size;
746 bfd_size_type nsymz; /* Number of symbols in armap. */
747 bfd_vma (*swap) (const void *);
748 char int_buf[sizeof (long)];
749 bfd_size_type carsym_size, ptrsize;
752 mapdata = _bfd_read_ar_hdr (abfd);
755 parsed_size = mapdata->parsed_size;
756 bfd_release (abfd, mapdata); /* Don't need it any more. */
758 if (bfd_bread (int_buf, 4, abfd) != 4)
760 if (bfd_get_error () != bfd_error_system_call)
761 bfd_set_error (bfd_error_malformed_archive);
764 /* It seems that all numeric information in a coff archive is always
765 in big endian format, nomatter the host or target. */
767 nsymz = bfd_getb32 (int_buf);
768 stringsize = parsed_size - (4 * nsymz) - 4;
770 /* ... except that some archive formats are broken, and it may be our
771 fault - the i960 little endian coff sometimes has big and sometimes
772 little, because our tools changed. Here's a horrible hack to clean
775 if (stringsize > 0xfffff
776 && bfd_get_arch (abfd) == bfd_arch_i960
777 && bfd_get_flavour (abfd) == bfd_target_coff_flavour)
779 /* This looks dangerous, let's do it the other way around. */
780 nsymz = bfd_getl32 (int_buf);
781 stringsize = parsed_size - (4 * nsymz) - 4;
785 /* The coff armap must be read sequentially. So we construct a
786 bsd-style one in core all at once, for simplicity. */
788 carsym_size = (nsymz * sizeof (carsym));
789 ptrsize = (4 * nsymz);
791 ardata->symdefs = bfd_zalloc (abfd, carsym_size + stringsize + 1);
792 if (ardata->symdefs == NULL)
794 carsyms = ardata->symdefs;
795 stringbase = ((char *) ardata->symdefs) + carsym_size;
797 /* Allocate and read in the raw offsets. */
798 raw_armap = bfd_alloc (abfd, ptrsize);
799 if (raw_armap == NULL)
800 goto release_symdefs;
801 if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize
802 || (bfd_bread (stringbase, stringsize, abfd) != stringsize))
804 if (bfd_get_error () != bfd_error_system_call)
805 bfd_set_error (bfd_error_malformed_archive);
806 goto release_raw_armap;
809 /* OK, build the carsyms. */
810 for (i = 0; i < nsymz; i++)
812 rawptr = raw_armap + i;
813 carsyms->file_offset = swap ((bfd_byte *) rawptr);
814 carsyms->name = stringbase;
815 stringbase += strlen (stringbase) + 1;
820 ardata->symdef_count = nsymz;
821 ardata->first_file_filepos = bfd_tell (abfd);
822 /* Pad to an even boundary if you have to. */
823 ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
825 bfd_has_map (abfd) = TRUE;
826 bfd_release (abfd, raw_armap);
828 /* Check for a second archive header (as used by PE). */
830 struct areltdata *tmp;
832 bfd_seek (abfd, ardata->first_file_filepos, SEEK_SET);
833 tmp = _bfd_read_ar_hdr (abfd);
836 if (tmp->arch_header[0] == '/'
837 && tmp->arch_header[1] == ' ')
839 ardata->first_file_filepos +=
840 (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1;
842 bfd_release (abfd, tmp);
849 bfd_release (abfd, raw_armap);
851 bfd_release (abfd, (ardata)->symdefs);
855 /* This routine can handle either coff-style or bsd-style armaps.
856 Returns FALSE on error, TRUE otherwise */
859 bfd_slurp_armap (bfd *abfd)
862 int i = bfd_bread (nextname, 16, abfd);
869 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
872 if (!strncmp (nextname, "__.SYMDEF ", 16)
873 || !strncmp (nextname, "__.SYMDEF/ ", 16)) /* old Linux archives */
874 return do_slurp_bsd_armap (abfd);
875 else if (!strncmp (nextname, "/ ", 16))
876 return do_slurp_coff_armap (abfd);
877 else if (!strncmp (nextname, "/SYM64/ ", 16))
879 /* 64bit ELF (Irix 6) archive. */
881 extern bfd_boolean bfd_elf64_archive_slurp_armap (bfd *);
882 return bfd_elf64_archive_slurp_armap (abfd);
884 bfd_set_error (bfd_error_wrong_format);
889 bfd_has_map (abfd) = FALSE;
893 /* Returns FALSE on error, TRUE otherwise. */
894 /* Flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the
895 header is in a slightly different order and the map name is '/'.
896 This flavour is used by hp300hpux. */
898 #define HPUX_SYMDEF_COUNT_SIZE 2
901 bfd_slurp_bsd_armap_f2 (bfd *abfd)
903 struct areltdata *mapdata;
905 unsigned int counter;
906 bfd_byte *raw_armap, *rbase;
907 struct artdata *ardata = bfd_ardata (abfd);
909 unsigned int stringsize;
912 int i = bfd_bread (nextname, 16, abfd);
919 /* The archive has at least 16 bytes in it. */
920 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
923 if (!strncmp (nextname, "__.SYMDEF ", 16)
924 || !strncmp (nextname, "__.SYMDEF/ ", 16)) /* Old Linux archives. */
925 return do_slurp_bsd_armap (abfd);
927 if (strncmp (nextname, "/ ", 16))
929 bfd_has_map (abfd) = FALSE;
933 mapdata = _bfd_read_ar_hdr (abfd);
937 amt = mapdata->parsed_size;
938 raw_armap = bfd_zalloc (abfd, amt);
939 if (raw_armap == NULL)
942 bfd_release (abfd, mapdata);
946 if (bfd_bread (raw_armap, amt, abfd) != amt)
948 if (bfd_get_error () != bfd_error_system_call)
949 bfd_set_error (bfd_error_malformed_archive);
951 bfd_release (abfd, raw_armap);
955 ardata->symdef_count = H_GET_16 (abfd, raw_armap);
957 if (ardata->symdef_count * BSD_SYMDEF_SIZE
958 > mapdata->parsed_size - HPUX_SYMDEF_COUNT_SIZE)
960 /* Probably we're using the wrong byte ordering. */
961 bfd_set_error (bfd_error_wrong_format);
967 stringsize = H_GET_32 (abfd, raw_armap + HPUX_SYMDEF_COUNT_SIZE);
968 /* Skip sym count and string sz. */
969 stringbase = ((char *) raw_armap
970 + HPUX_SYMDEF_COUNT_SIZE
971 + BSD_STRING_COUNT_SIZE);
972 rbase = (bfd_byte *) stringbase + stringsize;
973 amt = ardata->symdef_count * BSD_SYMDEF_SIZE;
974 ardata->symdefs = bfd_alloc (abfd, amt);
975 if (!ardata->symdefs)
978 for (counter = 0, set = ardata->symdefs;
979 counter < ardata->symdef_count;
980 counter++, set++, rbase += BSD_SYMDEF_SIZE)
982 set->name = H_GET_32 (abfd, rbase) + stringbase;
983 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
986 ardata->first_file_filepos = bfd_tell (abfd);
987 /* Pad to an even boundary if you have to. */
988 ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
989 /* FIXME, we should provide some way to free raw_ardata when
990 we are done using the strings from it. For now, it seems
991 to be allocated on an objalloc anyway... */
992 bfd_has_map (abfd) = TRUE;
996 /** Extended name table.
998 Normally archives support only 14-character filenames.
1000 Intel has extended the format: longer names are stored in a special
1001 element (the first in the archive, or second if there is an armap);
1002 the name in the ar_hdr is replaced by <space><index into filename
1003 element>. Index is the P.R. of an int (decimal). Data General have
1004 extended the format by using the prefix // for the special element. */
1006 /* Returns FALSE on error, TRUE otherwise. */
1009 _bfd_slurp_extended_name_table (bfd *abfd)
1012 struct areltdata *namedata;
1015 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
1016 we probably don't want to return TRUE. */
1017 bfd_seek (abfd, bfd_ardata (abfd)->first_file_filepos, SEEK_SET);
1018 if (bfd_bread (nextname, 16, abfd) == 16)
1020 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
1023 if (strncmp (nextname, "ARFILENAMES/ ", 16) != 0 &&
1024 strncmp (nextname, "// ", 16) != 0)
1026 bfd_ardata (abfd)->extended_names = NULL;
1030 namedata = _bfd_read_ar_hdr (abfd);
1031 if (namedata == NULL)
1034 amt = namedata->parsed_size;
1035 bfd_ardata (abfd)->extended_names = bfd_zalloc (abfd, amt);
1036 if (bfd_ardata (abfd)->extended_names == NULL)
1039 bfd_release (abfd, namedata);
1043 if (bfd_bread (bfd_ardata (abfd)->extended_names, amt, abfd) != amt)
1045 if (bfd_get_error () != bfd_error_system_call)
1046 bfd_set_error (bfd_error_malformed_archive);
1047 bfd_release (abfd, (bfd_ardata (abfd)->extended_names));
1048 bfd_ardata (abfd)->extended_names = NULL;
1052 /* Since the archive is supposed to be printable if it contains
1053 text, the entries in the list are newline-padded, not null
1054 padded. In SVR4-style archives, the names also have a
1055 trailing '/'. DOS/NT created archive often have \ in them
1056 We'll fix all problems here.. */
1058 char *temp = bfd_ardata (abfd)->extended_names;
1059 char *limit = temp + namedata->parsed_size;
1060 for (; temp < limit; ++temp)
1062 if (*temp == '\012')
1063 temp[temp[-1] == '/' ? -1 : 0] = '\0';
1069 /* Pad to an even boundary if you have to. */
1070 bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd);
1071 bfd_ardata (abfd)->first_file_filepos +=
1072 (bfd_ardata (abfd)->first_file_filepos) % 2;
1074 /* FIXME, we can't release namedata here because it was allocated
1075 below extended_names on the objalloc... */
1082 /* Return a copy of the stuff in the filename between any :]> and a
1086 normalize (bfd *abfd, const char *file)
1092 first = file + strlen (file) - 1;
1095 while (first != file)
1099 if (*first == ':' || *first == ']' || *first == '>')
1107 copy = bfd_alloc (abfd, last - first + 1);
1111 memcpy (copy, first, last - first);
1112 copy[last - first] = 0;
1119 normalize (bfd *abfd ATTRIBUTE_UNUSED, const char *file)
1121 const char *filename = strrchr (file, '/');
1123 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
1125 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1126 char *bslash = strrchr (file, '\\');
1127 if (filename == NULL || (bslash != NULL && bslash > filename))
1129 if (filename == NULL && file[0] != '\0' && file[1] == ':')
1130 filename = file + 1;
1133 if (filename != NULL)
1141 /* Build a BFD style extended name table. */
1144 _bfd_archive_bsd_construct_extended_name_table (bfd *abfd,
1146 bfd_size_type *tablen,
1149 *name = "ARFILENAMES/";
1150 return _bfd_construct_extended_name_table (abfd, FALSE, tabloc, tablen);
1153 /* Build an SVR4 style extended name table. */
1156 _bfd_archive_coff_construct_extended_name_table (bfd *abfd,
1158 bfd_size_type *tablen,
1162 return _bfd_construct_extended_name_table (abfd, TRUE, tabloc, tablen);
1165 /* Follows archive_head and produces an extended name table if
1166 necessary. Returns (in tabloc) a pointer to an extended name
1167 table, and in tablen the length of the table. If it makes an entry
1168 it clobbers the filename so that the element may be written without
1169 further massage. Returns TRUE if it ran successfully, FALSE if
1170 something went wrong. A successful return may still involve a
1171 zero-length tablen! */
1174 _bfd_construct_extended_name_table (bfd *abfd,
1175 bfd_boolean trailing_slash,
1177 bfd_size_type *tablen)
1179 unsigned int maxname = abfd->xvec->ar_max_namelen;
1180 bfd_size_type total_namelen = 0;
1186 /* Figure out how long the table should be. */
1187 for (current = abfd->archive_head; current != NULL; current = current->next)
1190 unsigned int thislen;
1192 normal = normalize (current, current->filename);
1196 thislen = strlen (normal);
1198 if (thislen > maxname
1199 && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
1202 if (thislen > maxname)
1204 /* Add one to leave room for \n. */
1205 total_namelen += thislen + 1;
1208 /* Leave room for trailing slash. */
1214 struct ar_hdr *hdr = arch_hdr (current);
1215 if (strncmp (normal, hdr->ar_name, thislen) != 0
1216 || (thislen < sizeof hdr->ar_name
1217 && hdr->ar_name[thislen] != ar_padchar (current)))
1219 /* Must have been using extended format even though it
1220 didn't need to. Fix it to use normal format. */
1221 memcpy (hdr->ar_name, normal, thislen);
1222 if (thislen < maxname
1223 || (thislen == maxname && thislen < sizeof hdr->ar_name))
1224 hdr->ar_name[thislen] = ar_padchar (current);
1229 if (total_namelen == 0)
1232 *tabloc = bfd_zalloc (abfd, total_namelen);
1233 if (*tabloc == NULL)
1236 *tablen = total_namelen;
1239 for (current = abfd->archive_head; current != NULL; current =
1243 unsigned int thislen;
1245 normal = normalize (current, current->filename);
1249 thislen = strlen (normal);
1250 if (thislen > maxname)
1252 /* Works for now; may need to be re-engineered if we
1253 encounter an oddball archive format and want to
1254 generalise this hack. */
1255 struct ar_hdr *hdr = arch_hdr (current);
1256 strcpy (strptr, normal);
1257 if (! trailing_slash)
1258 strptr[thislen] = '\012';
1261 strptr[thislen] = '/';
1262 strptr[thislen + 1] = '\012';
1264 hdr->ar_name[0] = ar_padchar (current);
1265 /* We know there will always be enough room (one of the few
1266 cases where you may safely use sprintf). */
1267 sprintf ((hdr->ar_name) + 1, "%-d", (unsigned) (strptr - *tabloc));
1268 /* Kinda Kludgy. We should just use the returned value of
1269 sprintf but not all implementations get this right. */
1271 char *temp = hdr->ar_name + 2;
1272 for (; temp < hdr->ar_name + maxname; temp++)
1276 strptr += thislen + 1;
1285 /* A couple of functions for creating ar_hdrs. */
1287 #ifdef HPUX_LARGE_AR_IDS
1288 /* Function to encode large UID/GID values according to HP. */
1291 hpux_uid_gid_encode (char str[6], long int id)
1295 str[5] = '@' + (id & 3);
1298 for (cnt = 4; cnt >= 0; ++cnt, id >>= 6)
1299 str[cnt] = ' ' + (id & 0x3f);
1301 #endif /* HPUX_LARGE_AR_IDS */
1311 /* Takes a filename, returns an arelt_data for it, or NULL if it can't
1312 make one. The filename must refer to a filename in the filesystem.
1313 The filename field of the ar_hdr will NOT be initialized. If member
1314 is set, and it's an in-memory bfd, we fake it. */
1316 static struct areltdata *
1317 bfd_ar_hdr_from_filesystem (bfd *abfd, const char *filename, bfd *member)
1320 struct areltdata *ared;
1325 if (member && (member->flags & BFD_IN_MEMORY) != 0)
1327 /* Assume we just "made" the member, and fake it. */
1328 struct bfd_in_memory *bim = member->iostream;
1329 time (&status.st_mtime);
1330 status.st_uid = getuid ();
1331 status.st_gid = getgid ();
1332 status.st_mode = 0644;
1333 status.st_size = bim->size;
1335 else if (stat (filename, &status) != 0)
1337 bfd_set_error (bfd_error_system_call);
1341 amt = sizeof (struct ar_hdr) + sizeof (struct areltdata);
1342 ared = bfd_zalloc (abfd, amt);
1345 hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
1347 /* ar headers are space padded, not null padded! */
1348 memset (hdr, ' ', sizeof (struct ar_hdr));
1350 strncpy (hdr->ar_fmag, ARFMAG, 2);
1352 /* Goddamned sprintf doesn't permit MAXIMUM field lengths. */
1353 sprintf ((hdr->ar_date), "%-12ld", (long) status.st_mtime);
1354 #ifdef HPUX_LARGE_AR_IDS
1355 /* HP has a very "special" way to handle UID/GID's with numeric values
1357 if (status.st_uid > 99999)
1358 hpux_uid_gid_encode (hdr->ar_gid, (long) status.st_uid);
1361 sprintf ((hdr->ar_uid), "%ld", (long) status.st_uid);
1362 #ifdef HPUX_LARGE_AR_IDS
1363 /* HP has a very "special" way to handle UID/GID's with numeric values
1365 if (status.st_gid > 99999)
1366 hpux_uid_gid_encode (hdr->ar_uid, (long) status.st_gid);
1369 sprintf ((hdr->ar_gid), "%ld", (long) status.st_gid);
1370 sprintf ((hdr->ar_mode), "%-8o", (unsigned int) status.st_mode);
1371 sprintf ((hdr->ar_size), "%-10ld", (long) status.st_size);
1372 /* Correct for a lossage in sprintf whereby it null-terminates. I cannot
1373 understand how these C losers could design such a ramshackle bunch of
1375 temp = (char *) hdr;
1376 temp1 = temp + sizeof (struct ar_hdr) - 2;
1377 for (; temp < temp1; temp++)
1382 strncpy (hdr->ar_fmag, ARFMAG, 2);
1383 ared->parsed_size = status.st_size;
1384 ared->arch_header = (char *) hdr;
1389 /* This is magic required by the "ar" program. Since it's
1390 undocumented, it's undocumented. You may think that it would take
1391 a strong stomach to write this, and it does, but it takes even a
1392 stronger stomach to try to code around such a thing! */
1394 struct ar_hdr *bfd_special_undocumented_glue (bfd *, const char *);
1397 bfd_special_undocumented_glue (bfd *abfd, const char *filename)
1399 struct areltdata *ar_elt = bfd_ar_hdr_from_filesystem (abfd, filename, 0);
1402 return (struct ar_hdr *) ar_elt->arch_header;
1405 /* Analogous to stat call. */
1408 bfd_generic_stat_arch_elt (bfd *abfd, struct stat *buf)
1413 if (abfd->arelt_data == NULL)
1415 bfd_set_error (bfd_error_invalid_operation);
1419 hdr = arch_hdr (abfd);
1421 #define foo(arelt, stelt, size) \
1422 buf->stelt = strtol (hdr->arelt, &aloser, size); \
1423 if (aloser == hdr->arelt) \
1426 /* Some platforms support special notations for large IDs. */
1427 #ifdef HPUX_LARGE_AR_IDS
1428 # define foo2(arelt, stelt, size) \
1429 if (hdr->arelt[5] == ' ') \
1431 foo (arelt, stelt, size); \
1436 for (buf->stelt = cnt = 0; cnt < 5; ++cnt) \
1438 if (hdr->arelt[cnt] < ' ' || hdr->arelt[cnt] > ' ' + 0x3f) \
1441 buf->stelt += hdr->arelt[cnt] - ' '; \
1443 if (hdr->arelt[5] < '@' || hdr->arelt[5] > '@' + 3) \
1446 buf->stelt += hdr->arelt[5] - '@'; \
1449 # define foo2(arelt, stelt, size) foo (arelt, stelt, size)
1452 foo (ar_date, st_mtime, 10);
1453 foo2 (ar_uid, st_uid, 10);
1454 foo2 (ar_gid, st_gid, 10);
1455 foo (ar_mode, st_mode, 8);
1457 buf->st_size = arch_eltdata (abfd)->parsed_size;
1463 bfd_dont_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
1465 /* FIXME: This interacts unpleasantly with ar's quick-append option.
1466 Fortunately ic960 users will never use that option. Fixing this
1467 is very hard; fortunately I know how to do it and will do so once
1468 intel's release is out the door. */
1470 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1472 const char *filename;
1473 size_t maxlen = ar_maxnamelen (abfd);
1475 if ((bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
1477 bfd_bsd_truncate_arname (abfd, pathname, arhdr);
1481 filename = normalize (abfd, pathname);
1482 if (filename == NULL)
1488 length = strlen (filename);
1490 if (length <= maxlen)
1491 memcpy (hdr->ar_name, filename, length);
1493 /* Add the padding character if there is room for it. */
1495 || (length == maxlen && length < sizeof hdr->ar_name))
1496 (hdr->ar_name)[length] = ar_padchar (abfd);
1500 bfd_bsd_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
1502 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1504 const char *filename = strrchr (pathname, '/');
1505 size_t maxlen = ar_maxnamelen (abfd);
1507 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
1509 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1510 char *bslash = strrchr (pathname, '\\');
1511 if (filename == NULL || (bslash != NULL && bslash > filename))
1513 if (filename == NULL && pathname[0] != '\0' && pathname[1] == ':')
1514 filename = pathname + 1;
1518 if (filename == NULL)
1519 filename = pathname;
1523 length = strlen (filename);
1525 if (length <= maxlen)
1526 memcpy (hdr->ar_name, filename, length);
1529 /* pathname: meet procrustes */
1530 memcpy (hdr->ar_name, filename, maxlen);
1534 if (length < maxlen)
1535 (hdr->ar_name)[length] = ar_padchar (abfd);
1538 /* Store name into ar header. Truncates the name to fit.
1539 1> strip pathname to be just the basename.
1540 2> if it's short enuf to fit, stuff it in.
1541 3> If it doesn't end with .o, truncate it to fit
1542 4> truncate it before the .o, append .o, stuff THAT in. */
1544 /* This is what gnu ar does. It's better but incompatible with the
1548 bfd_gnu_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
1550 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1552 const char *filename = strrchr (pathname, '/');
1553 size_t maxlen = ar_maxnamelen (abfd);
1555 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
1557 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1558 char *bslash = strrchr (pathname, '\\');
1559 if (filename == NULL || (bslash != NULL && bslash > filename))
1561 if (filename == NULL && pathname[0] != '\0' && pathname[1] == ':')
1562 filename = pathname + 1;
1566 if (filename == NULL)
1567 filename = pathname;
1571 length = strlen (filename);
1573 if (length <= maxlen)
1574 memcpy (hdr->ar_name, filename, length);
1576 { /* pathname: meet procrustes */
1577 memcpy (hdr->ar_name, filename, maxlen);
1578 if ((filename[length - 2] == '.') && (filename[length - 1] == 'o'))
1580 hdr->ar_name[maxlen - 2] = '.';
1581 hdr->ar_name[maxlen - 1] = 'o';
1587 (hdr->ar_name)[length] = ar_padchar (abfd);
1590 /* The BFD is open for write and has its format set to bfd_archive. */
1593 _bfd_write_archive_contents (bfd *arch)
1596 char *etable = NULL;
1597 bfd_size_type elength = 0;
1598 const char *ename = NULL;
1599 bfd_boolean makemap = bfd_has_map (arch);
1600 /* If no .o's, don't bother to make a map. */
1601 bfd_boolean hasobjects = FALSE;
1602 bfd_size_type wrote;
1606 /* Verify the viability of all entries; if any of them live in the
1607 filesystem (as opposed to living in an archive open for input)
1608 then construct a fresh ar_hdr for them. */
1609 for (current = arch->archive_head; current; current = current->next)
1611 /* This check is checking the bfds for the objects we're reading
1612 from (which are usually either an object file or archive on
1613 disk), not the archive entries we're writing to. We don't
1614 actually create bfds for the archive members, we just copy
1615 them byte-wise when we write out the archive. */
1616 if (bfd_write_p (current))
1618 bfd_set_error (bfd_error_invalid_operation);
1621 if (!current->arelt_data)
1623 current->arelt_data =
1624 bfd_ar_hdr_from_filesystem (arch, current->filename, current);
1625 if (!current->arelt_data)
1628 /* Put in the file name. */
1629 BFD_SEND (arch, _bfd_truncate_arname,
1630 (arch, current->filename, (char *) arch_hdr (current)));
1633 if (makemap && ! hasobjects)
1634 { /* Don't bother if we won't make a map! */
1635 if ((bfd_check_format (current, bfd_object)))
1640 if (!BFD_SEND (arch, _bfd_construct_extended_name_table,
1641 (arch, &etable, &elength, &ename)))
1644 if (bfd_seek (arch, (file_ptr) 0, SEEK_SET) != 0)
1646 wrote = bfd_bwrite (ARMAG, SARMAG, arch);
1647 if (wrote != SARMAG)
1650 if (makemap && hasobjects)
1652 if (! _bfd_compute_and_write_armap (arch, (unsigned int) elength))
1660 memset (&hdr, 0, sizeof (struct ar_hdr));
1661 strcpy (hdr.ar_name, ename);
1662 /* Round size up to even number in archive header. */
1663 sprintf (&(hdr.ar_size[0]), "%-10d",
1664 (int) ((elength + 1) & ~(bfd_size_type) 1));
1665 strncpy (hdr.ar_fmag, ARFMAG, 2);
1666 for (i = 0; i < sizeof (struct ar_hdr); i++)
1667 if (((char *) (&hdr))[i] == '\0')
1668 (((char *) (&hdr))[i]) = ' ';
1669 if ((bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
1670 != sizeof (struct ar_hdr))
1671 || bfd_bwrite (etable, elength, arch) != elength)
1673 if ((elength % 2) == 1)
1675 if (bfd_bwrite ("\012", 1, arch) != 1)
1680 for (current = arch->archive_head; current; current = current->next)
1682 char buffer[DEFAULT_BUFFERSIZE];
1683 unsigned int remaining = arelt_size (current);
1684 struct ar_hdr *hdr = arch_hdr (current);
1686 /* Write ar header. */
1687 if (bfd_bwrite (hdr, sizeof (*hdr), arch)
1690 if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0)
1694 unsigned int amt = DEFAULT_BUFFERSIZE;
1695 if (amt > remaining)
1698 if (bfd_bread (buffer, amt, current) != amt)
1700 if (bfd_get_error () != bfd_error_system_call)
1701 bfd_set_error (bfd_error_malformed_archive);
1704 if (bfd_bwrite (buffer, amt, arch) != amt)
1708 if ((arelt_size (current) % 2) == 1)
1710 if (bfd_bwrite ("\012", 1, arch) != 1)
1715 if (makemap && hasobjects)
1717 /* Verify the timestamp in the archive file. If it would not be
1718 accepted by the linker, rewrite it until it would be. If
1719 anything odd happens, break out and just return. (The
1720 Berkeley linker checks the timestamp and refuses to read the
1721 table-of-contents if it is >60 seconds less than the file's
1722 modified-time. That painful hack requires this painful hack. */
1726 if (bfd_update_armap_timestamp (arch))
1728 (*_bfd_error_handler)
1729 (_("Warning: writing archive was slow: rewriting timestamp\n"));
1731 while (++tries < 6);
1737 /* Note that the namidx for the first symbol is 0. */
1740 _bfd_compute_and_write_armap (bfd *arch, unsigned int elength)
1742 char *first_name = NULL;
1744 file_ptr elt_no = 0;
1745 struct orl *map = NULL;
1746 unsigned int orl_max = 1024; /* Fine initial default. */
1747 unsigned int orl_count = 0;
1749 asymbol **syms = NULL;
1754 /* Dunno if this is the best place for this info... */
1756 elength += sizeof (struct ar_hdr);
1757 elength += elength % 2;
1759 amt = orl_max * sizeof (struct orl);
1760 map = bfd_malloc (amt);
1764 /* We put the symbol names on the arch objalloc, and then discard
1766 first_name = bfd_alloc (arch, 1);
1767 if (first_name == NULL)
1770 /* Drop all the files called __.SYMDEF, we're going to make our own. */
1771 while (arch->archive_head &&
1772 strcmp (arch->archive_head->filename, "__.SYMDEF") == 0)
1773 arch->archive_head = arch->archive_head->next;
1775 /* Map over each element. */
1776 for (current = arch->archive_head;
1778 current = current->next, elt_no++)
1780 if (bfd_check_format (current, bfd_object)
1781 && (bfd_get_file_flags (current) & HAS_SYMS) != 0)
1787 storage = bfd_get_symtab_upper_bound (current);
1793 if (storage > syms_max)
1798 syms = bfd_malloc (syms_max);
1802 symcount = bfd_canonicalize_symtab (current, syms);
1806 /* Now map over all the symbols, picking out the ones we
1808 for (src_count = 0; src_count < symcount; src_count++)
1810 flagword flags = (syms[src_count])->flags;
1811 asection *sec = syms[src_count]->section;
1813 if ((flags & BSF_GLOBAL ||
1815 flags & BSF_INDIRECT ||
1816 bfd_is_com_section (sec))
1817 && ! bfd_is_und_section (sec))
1819 bfd_size_type namelen;
1820 struct orl *new_map;
1822 /* This symbol will go into the archive header. */
1823 if (orl_count == orl_max)
1826 amt = orl_max * sizeof (struct orl);
1827 new_map = bfd_realloc (map, amt);
1828 if (new_map == NULL)
1834 namelen = strlen (syms[src_count]->name);
1835 amt = sizeof (char *);
1836 map[orl_count].name = bfd_alloc (arch, amt);
1837 if (map[orl_count].name == NULL)
1839 *(map[orl_count].name) = bfd_alloc (arch, namelen + 1);
1840 if (*(map[orl_count].name) == NULL)
1842 strcpy (*(map[orl_count].name), syms[src_count]->name);
1843 map[orl_count].u.abfd = current;
1844 map[orl_count].namidx = stridx;
1846 stridx += namelen + 1;
1852 /* Now ask the BFD to free up any cached information, so we
1853 don't fill all of memory with symbol tables. */
1854 if (! bfd_free_cached_info (current))
1859 /* OK, now we have collected all the data, let's write them out. */
1860 ret = BFD_SEND (arch, write_armap,
1861 (arch, elength, map, orl_count, stridx));
1867 if (first_name != NULL)
1868 bfd_release (arch, first_name);
1877 if (first_name != NULL)
1878 bfd_release (arch, first_name);
1884 bsd_write_armap (bfd *arch,
1885 unsigned int elength,
1887 unsigned int orl_count,
1890 int padit = stridx & 1;
1891 unsigned int ranlibsize = orl_count * BSD_SYMDEF_SIZE;
1892 unsigned int stringsize = stridx + padit;
1893 /* Include 8 bytes to store ranlibsize and stringsize in output. */
1894 unsigned int mapsize = ranlibsize + stringsize + 8;
1896 bfd *current = arch->archive_head;
1897 bfd *last_elt = current; /* Last element arch seen. */
1901 struct stat statbuf;
1904 firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
1906 stat (arch->filename, &statbuf);
1907 memset (&hdr, 0, sizeof (struct ar_hdr));
1908 sprintf (hdr.ar_name, RANLIBMAG);
1909 /* Remember the timestamp, to keep it holy. But fudge it a little. */
1910 bfd_ardata (arch)->armap_timestamp = statbuf.st_mtime + ARMAP_TIME_OFFSET;
1911 bfd_ardata (arch)->armap_datepos = (SARMAG
1912 + offsetof (struct ar_hdr, ar_date[0]));
1913 sprintf (hdr.ar_date, "%ld", bfd_ardata (arch)->armap_timestamp);
1914 sprintf (hdr.ar_uid, "%ld", (long) getuid ());
1915 sprintf (hdr.ar_gid, "%ld", (long) getgid ());
1916 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
1917 strncpy (hdr.ar_fmag, ARFMAG, 2);
1918 for (i = 0; i < sizeof (struct ar_hdr); i++)
1919 if (((char *) (&hdr))[i] == '\0')
1920 (((char *) (&hdr))[i]) = ' ';
1921 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
1922 != sizeof (struct ar_hdr))
1924 H_PUT_32 (arch, ranlibsize, temp);
1925 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp))
1928 for (count = 0; count < orl_count; count++)
1930 bfd_byte buf[BSD_SYMDEF_SIZE];
1932 if (map[count].u.abfd != last_elt)
1936 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
1937 firstreal += firstreal % 2;
1938 current = current->next;
1940 while (current != map[count].u.abfd);
1944 H_PUT_32 (arch, map[count].namidx, buf);
1945 H_PUT_32 (arch, firstreal, buf + BSD_SYMDEF_OFFSET_SIZE);
1946 if (bfd_bwrite (buf, BSD_SYMDEF_SIZE, arch)
1951 /* Now write the strings themselves. */
1952 H_PUT_32 (arch, stringsize, temp);
1953 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp))
1955 for (count = 0; count < orl_count; count++)
1957 size_t len = strlen (*map[count].name) + 1;
1959 if (bfd_bwrite (*map[count].name, len, arch) != len)
1963 /* The spec sez this should be a newline. But in order to be
1964 bug-compatible for sun's ar we use a null. */
1967 if (bfd_bwrite ("", 1, arch) != 1)
1974 /* At the end of archive file handling, update the timestamp in the
1975 file, so the linker will accept it.
1977 Return TRUE if the timestamp was OK, or an unusual problem happened.
1978 Return FALSE if we updated the timestamp. */
1981 _bfd_archive_bsd_update_armap_timestamp (bfd *arch)
1983 struct stat archstat;
1987 /* Flush writes, get last-write timestamp from file, and compare it
1988 to the timestamp IN the file. */
1990 if (bfd_stat (arch, &archstat) == -1)
1992 bfd_perror (_("Reading archive file mod timestamp"));
1994 /* Can't read mod time for some reason. */
1997 if (archstat.st_mtime <= bfd_ardata (arch)->armap_timestamp)
1998 /* OK by the linker's rules. */
2001 /* Update the timestamp. */
2002 bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET;
2004 /* Prepare an ASCII version suitable for writing. */
2005 memset (hdr.ar_date, 0, sizeof (hdr.ar_date));
2006 sprintf (hdr.ar_date, "%ld", bfd_ardata (arch)->armap_timestamp);
2007 for (i = 0; i < sizeof (hdr.ar_date); i++)
2008 if (hdr.ar_date[i] == '\0')
2009 (hdr.ar_date)[i] = ' ';
2011 /* Write it into the file. */
2012 bfd_ardata (arch)->armap_datepos = (SARMAG
2013 + offsetof (struct ar_hdr, ar_date[0]));
2014 if (bfd_seek (arch, bfd_ardata (arch)->armap_datepos, SEEK_SET) != 0
2015 || (bfd_bwrite (hdr.ar_date, sizeof (hdr.ar_date), arch)
2016 != sizeof (hdr.ar_date)))
2018 bfd_perror (_("Writing updated armap timestamp"));
2020 /* Some error while writing. */
2024 /* We updated the timestamp successfully. */
2028 /* A coff armap looks like :
2030 struct ar_hdr with name = '/'
2032 offset of file for symbol 0
2033 offset of file for symbol 1
2035 offset of file for symbol n-1
2042 coff_write_armap (bfd *arch,
2043 unsigned int elength,
2045 unsigned int symbol_count,
2048 /* The size of the ranlib is the number of exported symbols in the
2049 archive * the number of bytes in an int, + an int for the count. */
2050 unsigned int ranlibsize = (symbol_count * 4) + 4;
2051 unsigned int stringsize = stridx;
2052 unsigned int mapsize = stringsize + ranlibsize;
2053 unsigned int archive_member_file_ptr;
2054 bfd *current = arch->archive_head;
2058 int padit = mapsize & 1;
2063 /* Work out where the first object file will go in the archive. */
2064 archive_member_file_ptr = (mapsize
2066 + sizeof (struct ar_hdr)
2069 memset (&hdr, 0, sizeof (struct ar_hdr));
2070 hdr.ar_name[0] = '/';
2071 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
2072 sprintf (hdr.ar_date, "%ld", (long) time (NULL));
2073 /* This, at least, is what Intel coff sets the values to. */
2074 sprintf ((hdr.ar_uid), "%d", 0);
2075 sprintf ((hdr.ar_gid), "%d", 0);
2076 sprintf ((hdr.ar_mode), "%-7o", (unsigned) 0);
2077 strncpy (hdr.ar_fmag, ARFMAG, 2);
2079 for (i = 0; i < sizeof (struct ar_hdr); i++)
2080 if (((char *) (&hdr))[i] == '\0')
2081 (((char *) (&hdr))[i]) = ' ';
2083 /* Write the ar header for this item and the number of symbols. */
2084 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
2085 != sizeof (struct ar_hdr))
2088 if (!bfd_write_bigendian_4byte_int (arch, symbol_count))
2091 /* Two passes, first write the file offsets for each symbol -
2092 remembering that each offset is on a two byte boundary. */
2094 /* Write out the file offset for the file associated with each
2095 symbol, and remember to keep the offsets padded out. */
2097 current = arch->archive_head;
2099 while (current != NULL && count < symbol_count)
2101 /* For each symbol which is used defined in this object, write
2102 out the object file's address in the archive. */
2104 while (count < symbol_count && map[count].u.abfd == current)
2106 if (!bfd_write_bigendian_4byte_int (arch, archive_member_file_ptr))
2110 /* Add size of this archive entry. */
2111 archive_member_file_ptr += arelt_size (current) + sizeof (struct ar_hdr);
2112 /* Remember aboout the even alignment. */
2113 archive_member_file_ptr += archive_member_file_ptr % 2;
2114 current = current->next;
2117 /* Now write the strings themselves. */
2118 for (count = 0; count < symbol_count; count++)
2120 size_t len = strlen (*map[count].name) + 1;
2122 if (bfd_bwrite (*map[count].name, len, arch) != len)
2126 /* The spec sez this should be a newline. But in order to be
2127 bug-compatible for arc960 we use a null. */
2130 if (bfd_bwrite ("", 1, arch) != 1)