1 /* BFD back-end for archive files (libraries).
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 Written by Cygnus Support. Mostly Gumby Henkel-Wallace's fault.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 @setfilename archive-info
27 An archive (or library) is just another BFD. It has a symbol
28 table, although there's not much a user program will do with it.
30 The big difference between an archive BFD and an ordinary BFD
31 is that the archive doesn't have sections. Instead it has a
32 chain of BFDs that are considered its contents. These BFDs can
33 be manipulated like any other. The BFDs contained in an
34 archive opened for reading will all be opened for reading. You
35 may put either input or output BFDs into an archive opened for
36 output; they will be handled correctly when the archive is closed.
38 Use <<bfd_openr_next_archived_file>> to step through
39 the contents of an archive opened for input. You don't
40 have to read the entire archive if you don't want
41 to! Read it until you find what you want.
43 Archive contents of output BFDs are chained through the
44 <<next>> pointer in a BFD. The first one is findable through
45 the <<archive_head>> slot of the archive. Set it with
46 <<bfd_set_archive_head>> (q.v.). A given BFD may be in only one
47 open output archive at a time.
49 As expected, the BFD archive code is more general than the
50 archive code of any given environment. BFD archives may
51 contain files of different formats (e.g., a.out and coff) and
52 even different architectures. You may even place archives
53 recursively into archives!
55 This can cause unexpected confusion, since some archive
56 formats are more expressive than others. For instance, Intel
57 COFF archives can preserve long filenames; SunOS a.out archives
58 cannot. If you move a file from the first to the second
59 format and back again, the filename may be truncated.
60 Likewise, different a.out environments have different
61 conventions as to how they truncate filenames, whether they
62 preserve directory names in filenames, etc. When
63 interoperating with native tools, be sure your files are
66 Beware: most of these formats do not react well to the
67 presence of spaces in filenames. We do the best we can, but
68 can't always handle this case due to restrictions in the format of
69 archives. Many Unix utilities are braindead in regards to
70 spaces and such in filenames anyway, so this shouldn't be much
73 Archives are supported in BFD in <<archive.c>>.
78 o - all archive elements start on an even boundary, newline padded;
79 o - all arch headers are char *;
80 o - all arch headers are the same size (across architectures).
83 /* Some formats provide a way to cram a long filename into the short
84 (16 chars) space provided by a BSD archive. The trick is: make a
85 special "file" in the front of the archive, sort of like the SYMDEF
86 entry. If the filename is too long to fit, put it in the extended
87 name table, and use its index as the filename. To prevent
88 confusion prepend the index with a space. This means you can't
89 have filenames that start with a space, but then again, many Unix
90 utilities can't handle that anyway.
92 This scheme unfortunately requires that you stand on your head in
93 order to write an archive since you need to put a magic file at the
94 front, and need to touch every entry to do so. C'est la vie.
96 We support two variants of this idea:
97 The SVR4 format (extended name table is named "//"),
98 and an extended pseudo-BSD variant (extended name table is named
99 "ARFILENAMES/"). The origin of the latter format is uncertain.
101 BSD 4.4 uses a third scheme: It writes a long filename
102 directly after the header. This allows 'ar q' to work.
103 We currently can read BSD 4.4 archives, but not write them.
106 /* Summary of archive member names:
108 Symbol table (must be first):
109 "__.SYMDEF " - Symbol table, Berkeley style, produced by ranlib.
110 "/ " - Symbol table, system 5 style.
112 Long name table (must be before regular file members):
113 "// " - Long name table, System 5 R4 style.
114 "ARFILENAMES/ " - Long name table, non-standard extended BSD (not BSD 4.4).
116 Regular file members with short names:
117 "filename.o/ " - Regular file, System 5 style (embedded spaces ok).
118 "filename.o " - Regular file, Berkeley style (no embedded spaces).
120 Regular files with long names (or embedded spaces, for BSD variants):
121 "/18 " - SVR4 style, name at offset 18 in name table.
122 "#1/23 " - Long name (or embedded paces) 23 characters long,
123 BSD 4.4 style, full name follows header.
124 Implemented for reading, not writing.
125 " 18 " - Long name 18 characters long, extended pseudo-BSD.
132 #include "aout/ranlib.h"
134 #include <string.h> /* For memchr, strrchr and friends */
142 #define BFD_GNU960_ARMAG(abfd) (BFD_COFF_FILE_P((abfd)) ? ARMAG : ARMAGB)
145 /* Can't define this in hosts/foo.h, because (e.g. in gprof) the hosts file
146 is included, then obstack.h, which thinks if offsetof is defined, it
147 doesn't need to include stddef.h. */
148 /* Define offsetof for those systems which lack it */
150 #if !defined (offsetof)
151 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
154 /* We keep a cache of archive filepointers to archive elements to
155 speed up searching the archive by filepos. We only add an entry to
156 the cache when we actually read one. We also don't sort the cache;
157 it's generally short enough to search linearly.
158 Note that the pointers here point to the front of the ar_hdr, not
159 to the front of the contents!
164 struct ar_cache *next;
167 #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
168 #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
170 #define arch_eltdata(bfd) ((struct areltdata *)((bfd)->arelt_data))
171 #define arch_hdr(bfd) ((struct ar_hdr *)arch_eltdata(bfd)->arch_header)
173 static char *get_extended_arelt_filename PARAMS ((bfd *arch,
175 static boolean do_slurp_bsd_armap PARAMS ((bfd *abfd));
176 static boolean do_slurp_coff_armap PARAMS ((bfd *abfd));
177 static const char *normalize PARAMS ((const char *file));
178 static boolean bfd_construct_extended_name_table PARAMS ((bfd *abfd,
181 static struct areltdata *bfd_ar_hdr_from_filesystem PARAMS ((bfd *abfd,
183 static boolean compute_and_write_armap PARAMS ((bfd *arch,
184 unsigned int elength));
185 static boolean bsd_update_armap_timestamp PARAMS ((bfd *arch));
188 _bfd_generic_mkarchive (abfd)
191 abfd->tdata.aout_ar_data = ((struct artdata *)
192 bfd_zalloc (abfd, sizeof (struct artdata)));
194 if (bfd_ardata (abfd) == NULL)
196 bfd_error = no_memory;
200 bfd_ardata (abfd)->cache = NULL;
201 bfd_ardata (abfd)->archive_head = NULL;
202 bfd_ardata (abfd)->symdefs = NULL;
203 bfd_ardata (abfd)->extended_names = NULL;
204 bfd_ardata (abfd)->tdata = NULL;
214 symindex bfd_get_next_mapent(bfd *abfd, symindex previous, carsym **sym);
217 Step through archive @var{abfd}'s symbol table (if it
218 has one). Successively update @var{sym} with the next symbol's
219 information, returning that symbol's (internal) index into the
222 Supply <<BFD_NO_MORE_SYMBOLS>> as the @var{previous} entry to get
223 the first one; returns <<BFD_NO_MORE_SYMBOLS>> when you've already
226 A <<carsym>> is a canonical archive symbol. The only
227 user-visible element is its name, a null-terminated string.
231 bfd_get_next_mapent (abfd, prev, entry)
236 if (!bfd_has_map (abfd)) {
237 bfd_error = invalid_operation;
238 return BFD_NO_MORE_SYMBOLS;
241 if (prev == BFD_NO_MORE_SYMBOLS) prev = 0;
242 else if (++prev >= bfd_ardata (abfd)->symdef_count)
243 return BFD_NO_MORE_SYMBOLS;
245 *entry = (bfd_ardata (abfd)->symdefs + prev);
249 /* To be called by backends only */
252 _bfd_create_empty_archive_element_shell (obfd)
257 nbfd = new_bfd_contained_in(obfd);
260 bfd_error = no_memory;
271 boolean bfd_set_archive_head(bfd *output, bfd *new_head);
274 Set the head of the chain of
275 BFDs contained in the archive @var{output} to @var{new_head}.
279 bfd_set_archive_head (output_archive, new_head)
284 output_archive->archive_head = new_head;
289 look_for_bfd_in_cache (arch_bfd, filepos)
293 struct ar_cache *current;
295 for (current = bfd_ardata (arch_bfd)->cache; current != NULL;
296 current = current->next)
297 if (current->ptr == filepos) return current->arelt;
302 /* Kind of stupid to call cons for each one, but we don't do too many */
304 _bfd_add_bfd_to_archive_cache (arch_bfd, filepos, new_elt)
305 bfd *arch_bfd, *new_elt;
308 struct ar_cache *new_cache = (struct ar_cache *)
309 bfd_zalloc(arch_bfd, sizeof (struct ar_cache));
311 if (new_cache == NULL) {
312 bfd_error = no_memory;
316 new_cache->ptr = filepos;
317 new_cache->arelt = new_elt;
318 new_cache->next = (struct ar_cache *)NULL;
319 if (bfd_ardata (arch_bfd)->cache == NULL)
320 bfd_ardata (arch_bfd)->cache = new_cache;
322 struct ar_cache *current = bfd_ardata (arch_bfd)->cache;
324 while (current->next != NULL)
325 current = current->next;
326 current->next = new_cache;
332 /* The name begins with space. Hence the rest of the name is an index into
336 get_extended_arelt_filename (arch, name)
340 unsigned long index = 0;
342 /* Should extract string so that I can guarantee not to overflow into
343 the next region, but I'm too lazy. */
345 /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */
346 index = strtol (name+1, NULL, 10);
348 bfd_error = malformed_archive;
352 return bfd_ardata (arch)->extended_names + index;
355 /* This functions reads an arch header and returns an areltdata pointer, or
358 Presumes the file pointer is already in the right place (ie pointing
359 to the ar_hdr in the file). Moves the file pointer; on success it
360 should be pointing to the front of the file contents; on failure it
361 could have been moved arbitrarily.
365 _bfd_snarf_ar_hdr (abfd)
373 char *hdrp = (char *) &hdr;
374 unsigned int parsed_size;
375 struct areltdata *ared;
376 char *filename = NULL;
377 unsigned int namelen = 0;
378 unsigned int allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
381 if (bfd_read ((PTR)hdrp, 1, sizeof (struct ar_hdr), abfd)
382 != sizeof (struct ar_hdr)) {
383 bfd_error = no_more_archived_files;
386 if (strncmp ((hdr.ar_fmag), ARFMAG, 2)) {
387 bfd_error = malformed_archive;
392 parsed_size = strtol (hdr.ar_size, NULL, 10);
394 bfd_error = malformed_archive;
398 /* extract the filename from the archive - there are two ways to
399 specify an extendend name table, either the first char of the
400 name is a space, or it's a slash. */
401 if ((hdr.ar_name[0] == '/'
402 || (hdr.ar_name[0] == ' '
403 && memchr (hdr.ar_name, '/', ar_maxnamelen(abfd)) == NULL))
404 && bfd_ardata (abfd)->extended_names != NULL) {
405 filename = get_extended_arelt_filename (abfd, hdr.ar_name);
406 if (filename == NULL) {
407 bfd_error = malformed_archive;
411 /* BSD4.4-style long filename.
412 Only implemented for reading, so far! */
413 else if (hdr.ar_name[0] == '#' && hdr.ar_name[1] == '1'
414 && hdr.ar_name[2] == '/' && isdigit(hdr.ar_name[3]))
416 /* BSD-4.4 extended name */
417 namelen = atoi (&hdr.ar_name[3]);
418 allocsize += namelen + 1;
419 parsed_size -= namelen;
421 allocptr = bfd_zalloc(abfd, allocsize);
422 if (allocptr == NULL) {
423 bfd_error = no_memory;
427 + (sizeof (struct areltdata) + sizeof (struct ar_hdr));
428 if (bfd_read (filename, 1, namelen, abfd) != namelen) {
429 bfd_error = no_more_archived_files;
432 filename[namelen] = '\0';
436 /* We judge the end of the name by looking for '/' or ' '.
437 Note: The SYSV format (terminated by '/') allows embedded
438 spaces, so only look for ' ' if we don't find '/'. */
441 while (hdr.ar_name[namelen] != '\0' &&
442 hdr.ar_name[namelen] != '/') {
444 if (namelen == (unsigned)ar_maxnamelen(abfd)) {
446 while (hdr.ar_name[namelen] != ' '
447 && namelen < (unsigned)ar_maxnamelen(abfd)) {
454 allocsize += namelen + 1;
458 allocptr = bfd_zalloc(abfd, allocsize);
459 if (allocptr == NULL) {
460 bfd_error = no_memory;
465 ared = (struct areltdata *) allocptr;
467 ared->arch_header = allocptr + sizeof (struct areltdata);
468 memcpy ((char *) ared->arch_header, (char *) &hdr, sizeof (struct ar_hdr));
469 ared->parsed_size = parsed_size;
471 if (filename != NULL) ared->filename = filename;
473 ared->filename = allocptr + (sizeof (struct areltdata) +
474 sizeof (struct ar_hdr));
476 memcpy (ared->filename, hdr.ar_name, namelen);
477 ared->filename[namelen] = '\0';
483 /* This is an internal function; it's mainly used when indexing
484 through the archive symbol table, but also used to get the next
485 element, since it handles the bookkeeping so nicely for us.
489 _bfd_get_elt_at_filepos (archive, filepos)
493 struct areltdata *new_areldata;
496 n_nfd = look_for_bfd_in_cache (archive, filepos);
500 if (0 > bfd_seek (archive, filepos, SEEK_SET))
502 bfd_error = system_call_error;
506 if ((new_areldata = _bfd_snarf_ar_hdr (archive)) == NULL)
509 n_nfd = _bfd_create_empty_archive_element_shell (archive);
512 bfd_release (archive, (PTR)new_areldata);
516 n_nfd->origin = bfd_tell (archive);
517 n_nfd->arelt_data = (PTR) new_areldata;
518 n_nfd->filename = new_areldata->filename;
520 if (_bfd_add_bfd_to_archive_cache (archive, filepos, n_nfd))
524 bfd_release (archive, (PTR)n_nfd);
525 bfd_release (archive, (PTR)new_areldata);
534 bfd *bfd_get_elt_at_index(bfd *archive, int index);
537 Return the BFD which is referenced by the symbol in @var{archive}
538 indexed by @var{index}. @var{index} should have been returned by
539 <<bfd_get_next_mapent>> (q.v.).
543 bfd_get_elt_at_index (abfd, index)
548 _bfd_get_elt_at_filepos
549 (abfd, (bfd_ardata (abfd)->symdefs + index)->file_offset);
555 bfd_openr_next_archived_file
558 bfd *bfd_openr_next_archived_file(bfd *archive, bfd *previous);
561 Provided a BFD, @var{archive}, containing an archive and NULL, open
562 an input BFD on the first contained element and returns that.
563 Subsequent calls should pass
564 the archive and the previous return value to return a created
565 BFD to the next contained element. NULL is returned when there
571 bfd_openr_next_archived_file (archive, last_file)
575 if ((bfd_get_format (archive) != bfd_archive) ||
576 (archive->direction == write_direction))
578 bfd_error = invalid_operation;
582 return BFD_SEND (archive,
583 openr_next_archived_file,
589 bfd_generic_openr_next_archived_file (archive, last_file)
596 filestart = bfd_ardata (archive)->first_file_filepos;
598 unsigned int size = arelt_size(last_file);
599 /* Pad to an even boundary...
600 Note that last_file->origin can be odd in the case of
601 BSD-4.4-style element with a long odd size. */
602 filestart = last_file->origin + size;
603 filestart += filestart % 2;
606 return _bfd_get_elt_at_filepos (archive, filestart);
611 bfd_generic_archive_p (abfd)
614 char armag[SARMAG+1];
616 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG)
618 bfd_error = wrong_format;
623 if (strncmp (armag, BFD_GNU960_ARMAG(abfd), SARMAG) != 0)
626 if (strncmp (armag, ARMAG, SARMAG) != 0 &&
627 strncmp (armag, ARMAGB, SARMAG) != 0)
631 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
632 involves a cast, we can't do it as the left operand of assignment. */
633 abfd->tdata.aout_ar_data = ((struct artdata *)
634 bfd_zalloc (abfd, sizeof (struct artdata)));
636 if (bfd_ardata (abfd) == NULL)
638 bfd_error = no_memory;
642 bfd_ardata (abfd)->first_file_filepos = SARMAG;
643 bfd_ardata (abfd)->cache = NULL;
644 bfd_ardata (abfd)->archive_head = NULL;
645 bfd_ardata (abfd)->symdefs = NULL;
646 bfd_ardata (abfd)->extended_names = NULL;
647 bfd_ardata (abfd)->tdata = NULL;
649 if (! BFD_SEND (abfd, _bfd_slurp_armap, (abfd)))
651 bfd_release(abfd, bfd_ardata (abfd));
652 abfd->tdata.aout_ar_data = NULL;
656 if (! BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd)))
658 bfd_release(abfd, bfd_ardata (abfd));
659 abfd->tdata.aout_ar_data = NULL;
666 /* Returns false on error, true otherwise */
668 do_slurp_bsd_armap (abfd)
671 struct areltdata *mapdata;
672 unsigned int counter = 0;
673 int *raw_armap, *rbase;
674 struct artdata *ardata = bfd_ardata (abfd);
676 unsigned int parsed_size;
678 mapdata = _bfd_snarf_ar_hdr (abfd);
681 parsed_size = mapdata->parsed_size;
682 bfd_release (abfd, (PTR)mapdata); /* Don't need it any more. */
684 raw_armap = (int *) bfd_zalloc(abfd, parsed_size);
685 if (raw_armap == NULL) {
686 bfd_error = no_memory;
690 if (bfd_read ((PTR)raw_armap, 1, parsed_size, abfd) != parsed_size) {
691 bfd_error = malformed_archive;
693 bfd_release (abfd, (PTR)raw_armap);
697 ardata->symdef_count = (bfd_h_get_32 (abfd, (bfd_byte *) raw_armap)
698 / sizeof (struct symdef));
700 if (ardata->symdef_count * sizeof (struct symdef)
701 > parsed_size - sizeof (*raw_armap)) {
702 /* Probably we're using the wrong byte ordering. */
703 bfd_error = wrong_format;
709 ardata->symdefs = (carsym *) rbase;
710 stringbase = ((char *) (ardata->symdefs + ardata->symdef_count)) + 4;
712 for (;counter < ardata->symdef_count; counter++) {
713 struct symdef *sym = ((struct symdef *) rbase) + counter;
714 sym->s.name = (bfd_h_get_32 (abfd, (bfd_byte *) (&sym->s.string_offset))
716 sym->file_offset = bfd_h_get_32 (abfd,
717 (bfd_byte *) (&(sym->file_offset)));
720 ardata->first_file_filepos = bfd_tell (abfd);
721 /* Pad to an even boundary if you have to */
722 ardata->first_file_filepos += (ardata-> first_file_filepos) %2;
723 /* FIXME, we should provide some way to free raw_ardata when
724 we are done using the strings from it. For now, it seems
725 to be allocated on an obstack anyway... */
726 bfd_has_map (abfd) = true;
730 /* Returns false on error, true otherwise */
732 do_slurp_coff_armap (abfd)
735 struct areltdata *mapdata;
736 int *raw_armap, *rawptr;
737 struct artdata *ardata = bfd_ardata (abfd);
739 unsigned int stringsize;
740 unsigned int parsed_size;
742 unsigned int nsymz; /* Number of symbols in armap. */
744 bfd_vma (*swap) PARAMS ((bfd_byte*));
745 char int_buf[sizeof(long)];
746 unsigned int carsym_size, ptrsize, i;
748 mapdata = _bfd_snarf_ar_hdr (abfd);
751 parsed_size = mapdata->parsed_size;
752 bfd_release (abfd, (PTR)mapdata); /* Don't need it any more. */
754 if (bfd_read ((PTR)int_buf, 1, 4, abfd) != 4) {
755 bfd_error = malformed_archive;
758 /* It seems that all numeric information in a coff archive is always
759 in big endian format, nomatter the host or target. */
761 nsymz = bfd_getb32((PTR)int_buf);
762 stringsize = parsed_size - (4 * nsymz) - 4;
765 /* ... except that some archive formats are broken, and it may be our
766 fault - the i960 little endian coff sometimes has big and sometimes
767 little, because our tools changed. Here's a horrible hack to clean
770 if (stringsize > 0xfffff) {
771 /* This looks dangerous, let's do it the other way around */
772 nsymz = bfd_getl32((PTR)int_buf);
773 stringsize = parsed_size - (4 * nsymz) - 4;
778 /* The coff armap must be read sequentially. So we construct a bsd-style
779 one in core all at once, for simplicity. */
781 carsym_size = (nsymz * sizeof (carsym));
782 ptrsize = (4 * nsymz);
784 ardata->symdefs = (carsym *) bfd_zalloc(abfd, carsym_size + stringsize + 1);
785 if (ardata->symdefs == NULL) {
786 bfd_error = no_memory;
789 carsyms = ardata->symdefs;
790 stringbase = ((char *) ardata->symdefs) + carsym_size;
792 /* Allocate and read in the raw offsets. */
793 raw_armap = (int *) bfd_alloc(abfd, ptrsize);
794 if (raw_armap == NULL) {
795 bfd_error = no_memory;
796 goto release_symdefs;
798 if (bfd_read ((PTR)raw_armap, 1, ptrsize, abfd) != ptrsize
799 || bfd_read ((PTR)stringbase, 1, stringsize, abfd) != stringsize) {
800 bfd_error = malformed_archive;
801 goto release_raw_armap;
804 /* OK, build the carsyms */
805 for (i = 0; i < nsymz; i++) {
806 rawptr = raw_armap + i;
807 carsyms->file_offset = swap((PTR)rawptr);
808 carsyms->name = stringbase;
809 stringbase += strlen (stringbase) + 1;
814 ardata->symdef_count = nsymz;
815 ardata->first_file_filepos = bfd_tell (abfd);
816 /* Pad to an even boundary if you have to */
817 ardata->first_file_filepos += (ardata->first_file_filepos) %2;
819 bfd_has_map (abfd) = true;
820 bfd_release (abfd, (PTR)raw_armap);
824 bfd_release (abfd, (PTR)raw_armap);
826 bfd_release (abfd, (PTR)(ardata)->symdefs);
830 /* This routine can handle either coff-style or bsd-style armaps.
831 Returns false on error, true otherwise */
834 bfd_slurp_armap (abfd)
838 int i = bfd_read ((PTR)nextname, 1, 16, abfd);
845 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
847 if (!strncmp (nextname, "__.SYMDEF ", 16))
848 return do_slurp_bsd_armap (abfd);
849 else if (!strncmp (nextname, "/ ", 16))
850 return do_slurp_coff_armap (abfd);
852 bfd_has_map (abfd) = false;
856 /* Returns false on error, true otherwise */
857 /* flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the
858 header is in a slightly different order and the map name is '/'.
859 This flavour is used by hp300hpux. */
861 bfd_slurp_bsd_armap_f2 (abfd)
864 struct areltdata *mapdata;
866 unsigned int counter = 0;
867 int *raw_armap, *rbase;
868 struct artdata *ardata = bfd_ardata (abfd);
870 unsigned int stringsize;
871 int i = bfd_read ((PTR)nextname, 1, 16, abfd);
878 /* The archive has at least 16 bytes in it */
879 bfd_seek (abfd, -16L, SEEK_CUR);
881 if (!strncmp (nextname, "__.SYMDEF ", 16))
882 return do_slurp_bsd_armap (abfd);
884 if (strncmp (nextname, "/ ", 16))
886 bfd_has_map (abfd) = false;
890 mapdata = _bfd_snarf_ar_hdr (abfd);
894 raw_armap = (int *) bfd_zalloc(abfd,mapdata->parsed_size);
895 if (raw_armap == NULL)
897 bfd_error = no_memory;
899 bfd_release (abfd, (PTR)mapdata);
903 if (bfd_read ((PTR)raw_armap, 1, mapdata->parsed_size, abfd) !=
904 mapdata->parsed_size)
906 bfd_error = malformed_archive;
908 bfd_release (abfd, (PTR)raw_armap);
912 ardata->symdef_count = bfd_h_get_16(abfd, (PTR)raw_armap);
914 if (ardata->symdef_count * sizeof (struct symdef)
915 > mapdata->parsed_size - sizeof (*raw_armap))
917 /* Probably we're using the wrong byte ordering. */
918 bfd_error = wrong_format;
924 stringsize = bfd_h_get_32(abfd, (PTR)(((char*)raw_armap)+2));
925 /* skip sym count and string sz */
926 rbase = (int*)(((char*)raw_armap) + 6);
927 stringbase = (char *) rbase;
928 ardata->symdefs = (carsym *)(((char*) rbase) + stringsize);
930 for (;counter < ardata->symdef_count; counter++)
932 struct symdef *sym = ((struct symdef *) ardata->symdefs) + counter;
933 sym->s.name = bfd_h_get_32(abfd, (PTR)(&(sym->s.string_offset))) + stringbase;
934 sym->file_offset = bfd_h_get_32(abfd, (PTR)( &(sym->file_offset)));
937 ardata->first_file_filepos = bfd_tell (abfd);
938 /* Pad to an even boundary if you have to */
939 ardata->first_file_filepos += (ardata-> first_file_filepos) %2;
940 /* FIXME, we should provide some way to free raw_ardata when
941 we are done using the strings from it. For now, it seems
942 to be allocated on an obstack anyway... */
943 bfd_has_map (abfd) = true;
947 /** Extended name table.
949 Normally archives support only 14-character filenames.
951 Intel has extended the format: longer names are stored in a special
952 element (the first in the archive, or second if there is an armap);
953 the name in the ar_hdr is replaced by <space><index into filename
954 element>. Index is the P.R. of an int (decimal). Data General have
955 extended the format by using the prefix // for the special element */
957 /* Returns false on error, true otherwise */
959 _bfd_slurp_extended_name_table (abfd)
963 struct areltdata *namedata;
965 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
966 we probably don't want to return true. */
967 if (bfd_read ((PTR)nextname, 1, 16, abfd) == 16) {
969 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
971 if (strncmp (nextname, "ARFILENAMES/ ", 16) != 0 &&
972 strncmp (nextname, "// ", 16) != 0)
974 bfd_ardata (abfd)->extended_names = NULL;
978 namedata = _bfd_snarf_ar_hdr (abfd);
979 if (namedata == NULL)
982 bfd_ardata (abfd)->extended_names = bfd_zalloc(abfd,namedata->parsed_size);
983 if (bfd_ardata (abfd)->extended_names == NULL) {
984 bfd_error = no_memory;
986 bfd_release (abfd, (PTR)namedata);
990 if (bfd_read ((PTR)bfd_ardata (abfd)->extended_names, 1,
991 namedata->parsed_size, abfd) != namedata->parsed_size) {
992 bfd_error = malformed_archive;
993 bfd_release (abfd, (PTR)(bfd_ardata (abfd)->extended_names));
994 bfd_ardata (abfd)->extended_names = NULL;
998 /* Since the archive is supposed to be printable if it contains
999 text, the entries in the list are newline-padded, not null
1000 padded. In SVR4-style archives, the names also have a
1001 trailing '/'. We'll fix both problems here.. */
1003 char *temp = bfd_ardata (abfd)->extended_names;
1004 char *limit = temp + namedata->parsed_size;
1005 for (; temp < limit; ++temp)
1007 temp[temp[-1] == '/' ? -1 : 0] = '\0';
1010 /* Pad to an even boundary if you have to */
1011 bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd);
1012 bfd_ardata (abfd)->first_file_filepos +=
1013 (bfd_ardata (abfd)->first_file_filepos) %2;
1015 /* FIXME, we can't release namedata here because it was allocated
1016 below extended_names on the obstack... */
1017 /* bfd_release (abfd, namedata); */
1024 /* Return a copy of the stuff in the filename between any :]> and a
1034 first = file + strlen(file)-1;
1037 while (first != file)
1041 if (*first == ':' || *first == ']' ||*first == '>')
1050 copy = bfd_xmalloc(last - first + 1);
1051 memcpy(copy, first, last-first);
1052 copy[last-first] = 0;
1062 CONST char * filename = strrchr(file, '/');
1064 if (filename != (char *)NULL) {
1073 /* Follows archive_head and produces an extended name table if necessary.
1074 Returns (in tabloc) a pointer to an extended name table, and in tablen
1075 the length of the table. If it makes an entry it clobbers the filename
1076 so that the element may be written without further massage.
1077 Returns true if it ran successfully, false if something went wrong.
1078 A successful return may still involve a zero-length tablen!
1081 bfd_construct_extended_name_table (abfd, tabloc, tablen)
1084 unsigned int *tablen;
1086 unsigned int maxname = abfd->xvec->ar_max_namelen;
1087 unsigned int total_namelen = 0;
1093 /* Figure out how long the table should be */
1094 for (current = abfd->archive_head; current != NULL; current = current->next){
1095 unsigned int thislen = strlen (normalize(current->filename));
1096 if (thislen > maxname) total_namelen += thislen + 1; /* leave room for \n */
1099 if (total_namelen == 0) return true;
1101 *tabloc = bfd_zalloc (abfd,total_namelen);
1102 if (*tabloc == NULL) {
1103 bfd_error = no_memory;
1107 *tablen = total_namelen;
1110 for (current = abfd->archive_head; current != NULL; current =
1112 CONST char *normal =normalize( current->filename);
1113 unsigned int thislen = strlen (normal);
1114 if (thislen > maxname) {
1115 /* Works for now; may need to be re-engineered if we encounter an oddball
1116 archive format and want to generalise this hack. */
1117 struct ar_hdr *hdr = arch_hdr(current);
1118 strcpy (strptr, normal);
1119 strptr[thislen] = '\n';
1120 hdr->ar_name[0] = ' ';
1121 /* We know there will always be enough room (one of the few cases
1122 where you may safely use sprintf). */
1123 sprintf ((hdr->ar_name) + 1, "%-d", (unsigned) (strptr - *tabloc));
1124 /* Kinda Kludgy. We should just use the returned value of sprintf
1125 but not all implementations get this right */
1127 char *temp = hdr->ar_name +2;
1128 for (; temp < hdr->ar_name + maxname; temp++)
1129 if (*temp == '\0') *temp = ' ';
1131 strptr += thislen + 1;
1138 /** A couple of functions for creating ar_hdrs */
1140 /* Takes a filename, returns an arelt_data for it, or NULL if it can't make one.
1141 The filename must refer to a filename in the filesystem.
1142 The filename field of the ar_hdr will NOT be initialized
1145 static struct areltdata *
1146 bfd_ar_hdr_from_filesystem (abfd,filename)
1148 const char *filename;
1151 struct areltdata *ared;
1155 if (stat (filename, &status) != 0) {
1156 bfd_error = system_call_error;
1160 ared = (struct areltdata *) bfd_zalloc(abfd, sizeof (struct ar_hdr) +
1161 sizeof (struct areltdata));
1163 bfd_error = no_memory;
1166 hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
1168 /* ar headers are space padded, not null padded! */
1169 memset (hdr, ' ', sizeof (struct ar_hdr));
1171 strncpy (hdr->ar_fmag, ARFMAG, 2);
1173 /* Goddamned sprintf doesn't permit MAXIMUM field lengths */
1174 sprintf ((hdr->ar_date), "%-12ld", status.st_mtime);
1175 sprintf ((hdr->ar_uid), "%d", status.st_uid);
1176 sprintf ((hdr->ar_gid), "%d", status.st_gid);
1177 sprintf ((hdr->ar_mode), "%-8o", (unsigned) status.st_mode);
1178 sprintf ((hdr->ar_size), "%-10ld", status.st_size);
1179 /* Correct for a lossage in sprintf whereby it null-terminates. I cannot
1180 understand how these C losers could design such a ramshackle bunch of
1182 temp = (char *) hdr;
1183 temp1 = temp + sizeof (struct ar_hdr) - 2;
1184 for (; temp < temp1; temp++) {
1185 if (*temp == '\0') *temp = ' ';
1187 strncpy (hdr->ar_fmag, ARFMAG, 2);
1188 ared->parsed_size = status.st_size;
1189 ared->arch_header = (char *) hdr;
1194 /* This is magic required by the "ar" program. Since it's
1195 undocumented, it's undocumented. You may think that it would
1196 take a strong stomach to write this, and it does, but it takes
1197 even a stronger stomach to try to code around such a thing!
1201 bfd_special_undocumented_glue (abfd, filename)
1205 struct areltdata *ar_elt = bfd_ar_hdr_from_filesystem (abfd, filename);
1208 return (struct ar_hdr *) ar_elt->arch_header;
1212 /* Analogous to stat call */
1214 bfd_generic_stat_arch_elt (abfd, buf)
1221 if (abfd->arelt_data == NULL) {
1222 bfd_error = invalid_operation;
1226 hdr = arch_hdr (abfd);
1228 #define foo(arelt, stelt, size) \
1229 buf->stelt = strtol (hdr->arelt, &aloser, size); \
1230 if (aloser == hdr->arelt) return -1;
1232 foo (ar_date, st_mtime, 10);
1233 foo (ar_uid, st_uid, 10);
1234 foo (ar_gid, st_gid, 10);
1235 foo (ar_mode, st_mode, 8);
1237 buf->st_size = arch_eltdata (abfd)->parsed_size;
1243 bfd_dont_truncate_arname (abfd, pathname, arhdr)
1245 CONST char *pathname;
1248 /* FIXME: This interacts unpleasantly with ar's quick-append option.
1249 Fortunately ic960 users will never use that option. Fixing this
1250 is very hard; fortunately I know how to do it and will do so once
1251 intel's release is out the door. */
1253 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1255 CONST char *filename = normalize(pathname);
1256 int maxlen = ar_maxnamelen (abfd);
1258 length = strlen (filename);
1260 if (length <= maxlen)
1261 memcpy (hdr->ar_name, filename, length);
1263 if (length < maxlen) (hdr->ar_name)[length] = ar_padchar (abfd);
1269 bfd_bsd_truncate_arname (abfd, pathname, arhdr)
1271 CONST char *pathname;
1274 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1276 CONST char *filename = strrchr (pathname, '/');
1277 int maxlen = ar_maxnamelen (abfd);
1280 if (filename == NULL)
1281 filename = pathname;
1285 length = strlen (filename);
1287 if (length <= maxlen)
1288 memcpy (hdr->ar_name, filename, length);
1290 /* pathname: meet procrustes */
1291 memcpy (hdr->ar_name, filename, maxlen);
1295 if (length < maxlen)
1296 (hdr->ar_name)[length] = ar_padchar (abfd);
1299 /* Store name into ar header. Truncates the name to fit.
1300 1> strip pathname to be just the basename.
1301 2> if it's short enuf to fit, stuff it in.
1302 3> If it doesn't end with .o, truncate it to fit
1303 4> truncate it before the .o, append .o, stuff THAT in.
1306 /* This is what gnu ar does. It's better but incompatible with the bsd ar. */
1308 bfd_gnu_truncate_arname (abfd, pathname, arhdr)
1310 CONST char *pathname;
1313 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1315 CONST char *filename = strrchr (pathname, '/');
1316 int maxlen = ar_maxnamelen (abfd);
1318 if (filename == NULL)
1319 filename = pathname;
1323 length = strlen (filename);
1325 if (length <= maxlen)
1326 memcpy (hdr->ar_name, filename, length);
1327 else { /* pathname: meet procrustes */
1328 memcpy (hdr->ar_name, filename, maxlen);
1329 if ((filename[length - 2] == '.') && (filename[length - 1] == 'o')) {
1330 hdr->ar_name[maxlen - 2] = '.';
1331 hdr->ar_name[maxlen - 1] = 'o';
1337 (hdr->ar_name)[length] = ar_padchar (abfd);
1341 /* The BFD is open for write and has its format set to bfd_archive */
1343 _bfd_write_archive_contents (arch)
1347 char *etable = NULL;
1348 unsigned int elength = 0;
1349 boolean makemap = bfd_has_map (arch);
1350 boolean hasobjects = false; /* if no .o's, don't bother to make a map */
1354 /* Verify the viability of all entries; if any of them live in the
1355 filesystem (as opposed to living in an archive open for input)
1356 then construct a fresh ar_hdr for them.
1358 for (current = arch->archive_head; current; current = current->next) {
1359 if (bfd_write_p (current)) {
1360 bfd_error = invalid_operation;
1363 if (!current->arelt_data) {
1364 current->arelt_data =
1365 (PTR) bfd_ar_hdr_from_filesystem (arch, current->filename);
1366 if (!current->arelt_data) return false;
1368 /* Put in the file name */
1370 BFD_SEND (arch, _bfd_truncate_arname,(arch,
1372 (char *) arch_hdr(current)));
1377 if (makemap) { /* don't bother if we won't make a map! */
1378 if ((bfd_check_format (current, bfd_object))
1379 #if 0 /* FIXME -- these are not set correctly */
1380 && ((bfd_get_file_flags (current) & HAS_SYMS))
1387 if (!bfd_construct_extended_name_table (arch, &etable, &elength))
1390 bfd_seek (arch, (file_ptr) 0, SEEK_SET);
1392 bfd_write (BFD_GNU960_ARMAG(arch), 1, SARMAG, arch);
1394 bfd_write (ARMAG, 1, SARMAG, arch);
1397 if (makemap && hasobjects) {
1399 if (compute_and_write_armap (arch, elength) != true) {
1407 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1408 sprintf (&(hdr.ar_name[0]), "ARFILENAMES/");
1409 sprintf (&(hdr.ar_size[0]), "%-10d", (int) elength);
1410 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1411 for (i = 0; i < sizeof (struct ar_hdr); i++)
1412 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
1413 bfd_write ((char *)&hdr, 1, sizeof (struct ar_hdr), arch);
1414 bfd_write (etable, 1, elength, arch);
1415 if ((elength % 2) == 1) bfd_write ("\n", 1, 1, arch);
1419 for (current = arch->archive_head; current; current = current->next) {
1420 char buffer[DEFAULT_BUFFERSIZE];
1421 unsigned int remaining = arelt_size (current);
1422 struct ar_hdr *hdr = arch_hdr(current);
1423 /* write ar header */
1425 if (bfd_write ((char *)hdr, 1, sizeof(*hdr), arch) != sizeof(*hdr)) {
1427 bfd_error = system_call_error;
1430 if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0) goto syserr;
1433 unsigned int amt = DEFAULT_BUFFERSIZE;
1434 if (amt > remaining) {
1438 if (bfd_read (buffer, amt, 1, current) != amt) {
1439 if (errno) goto syserr;
1440 /* Looks like a truncated archive. */
1441 bfd_error = malformed_archive;
1444 if (bfd_write (buffer, amt, 1, arch) != amt) goto syserr;
1447 if ((arelt_size (current) % 2) == 1) bfd_write ("\n", 1, 1, arch);
1450 /* Verify the timestamp in the archive file. If it would
1451 not be accepted by the linker, rewrite it until it would be.
1452 If anything odd happens, break out and just return.
1453 (The Berkeley linker checks the timestamp and refuses to read the
1454 table-of-contents if it is >60 seconds less than the file's
1455 modified-time. That painful hack requires this painful hack. */
1459 /* FIXME! This kludge is to avoid adding a member to the xvec,
1460 while generating a small patch for Adobe. FIXME! The
1461 update_armap_timestamp function call should be in the xvec,
1464 if (bfd_update_armap_timestamp (arch) == true) break;
1467 Instead, we check whether in a BSD archive, and call directly. */
1469 if (arch->xvec->write_armap != bsd_write_armap)
1471 if (bsd_update_armap_timestamp(arch) == true) /* FIXME!!! Vector it */
1475 "Warning: writing archive was slow: rewriting timestamp\n");
1476 } while (++tries < 6 );
1481 /* Note that the namidx for the first symbol is 0 */
1484 compute_and_write_armap (arch, elength)
1486 unsigned int elength;
1489 file_ptr elt_no = 0;
1491 int orl_max = 15000; /* fine initial default */
1493 int stridx = 0; /* string index */
1495 /* Dunno if this is the best place for this info... */
1496 if (elength != 0) elength += sizeof (struct ar_hdr);
1497 elength += elength %2 ;
1499 map = (struct orl *) bfd_zalloc (arch,orl_max * sizeof (struct orl));
1501 bfd_error = no_memory;
1505 /* Drop all the files called __.SYMDEF, we're going to make our
1507 while (arch->archive_head &&
1508 strcmp(arch->archive_head->filename,"__.SYMDEF") == 0)
1510 arch->archive_head = arch->archive_head->next;
1512 /* Map over each element */
1513 for (current = arch->archive_head;
1514 current != (bfd *)NULL;
1515 current = current->next, elt_no++)
1517 if ((bfd_check_format (current, bfd_object) == true)
1518 && ((bfd_get_file_flags (current) & HAS_SYMS))) {
1520 unsigned int storage;
1521 unsigned int symcount;
1522 unsigned int src_count;
1524 storage = get_symtab_upper_bound (current);
1527 syms = (asymbol **) bfd_zalloc (arch,storage);
1529 bfd_error = no_memory; /* FIXME -- memory leak */
1532 symcount = bfd_canonicalize_symtab (current, syms);
1535 /* Now map over all the symbols, picking out the ones we want */
1536 for (src_count = 0; src_count <symcount; src_count++) {
1538 (syms[src_count])->flags;
1540 syms[src_count]->section;
1542 if ((flags & BSF_GLOBAL ||
1544 flags & BSF_INDIRECT ||
1545 bfd_is_com_section (sec))
1546 && (sec != &bfd_und_section)) {
1548 /* This symbol will go into the archive header */
1549 if (orl_count == orl_max)
1552 map = (struct orl *) bfd_realloc (arch, (char *) map,
1553 orl_max * sizeof (struct orl));
1556 (map[orl_count]).name = (char **) &((syms[src_count])->name);
1557 (map[orl_count]).pos = (file_ptr) current;
1558 (map[orl_count]).namidx = stridx;
1560 stridx += strlen ((syms[src_count])->name) + 1;
1567 /* OK, now we have collected all the data, let's write them out */
1568 if (!BFD_SEND (arch, write_armap,
1569 (arch, elength, map, orl_count, stridx))) {
1579 bsd_write_armap (arch, elength, map, orl_count, stridx)
1581 unsigned int elength;
1583 unsigned int orl_count;
1586 int padit = stridx & 1;
1587 unsigned int ranlibsize = orl_count * sizeof (struct ranlib);
1588 unsigned int stringsize = stridx + padit;
1589 /* Include 8 bytes to store ranlibsize and stringsize in output. */
1590 unsigned int mapsize = ranlibsize + stringsize + 8;
1592 bfd *current = arch->archive_head;
1593 bfd *last_elt = current; /* last element arch seen */
1597 struct stat statbuf;
1600 firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
1602 stat (arch->filename, &statbuf);
1603 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1604 sprintf (hdr.ar_name, RANLIBMAG);
1605 /* Remember the timestamp, to keep it holy. But fudge it a little. */
1606 bfd_ardata(arch)->armap_timestamp = statbuf.st_mtime + ARMAP_TIME_OFFSET;
1607 bfd_ardata(arch)->armap_datepos = SARMAG +
1608 offsetof(struct ar_hdr, ar_date[0]);
1609 sprintf (hdr.ar_date, "%ld", bfd_ardata(arch)->armap_timestamp);
1610 sprintf (hdr.ar_uid, "%d", getuid());
1611 sprintf (hdr.ar_gid, "%d", getgid());
1612 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
1613 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1614 for (i = 0; i < sizeof (struct ar_hdr); i++)
1615 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
1616 bfd_write ((char *)&hdr, 1, sizeof (struct ar_hdr), arch);
1617 bfd_h_put_32(arch, (bfd_vma) ranlibsize, (PTR)&temp);
1618 bfd_write (&temp, 1, sizeof (temp), arch);
1620 for (count = 0; count < orl_count; count++) {
1622 struct symdef *outp = &outs;
1624 if (((bfd *)(map[count]).pos) != last_elt) {
1626 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
1627 firstreal += firstreal % 2;
1628 current = current->next;
1629 } while (current != (bfd *)(map[count]).pos);
1630 } /* if new archive element */
1633 bfd_h_put_32(arch, ((map[count]).namidx),(PTR) &outs.s.string_offset);
1634 bfd_h_put_32(arch, firstreal,(PTR) &outs.file_offset);
1635 bfd_write ((char *)outp, 1, sizeof (outs), arch);
1638 /* now write the strings themselves */
1639 bfd_h_put_32(arch, stringsize, (PTR)&temp);
1640 bfd_write ((PTR)&temp, 1, sizeof (temp), arch);
1641 for (count = 0; count < orl_count; count++)
1642 bfd_write (*((map[count]).name), 1, strlen (*((map[count]).name))+1, arch);
1644 /* The spec sez this should be a newline. But in order to be
1645 bug-compatible for sun's ar we use a null. */
1647 bfd_write("",1,1,arch);
1653 /* At the end of archive file handling, update the timestamp in the
1654 file, so the linker will accept it.
1656 Return true if the timestamp was OK, or an unusual problem happened.
1657 Return false if we updated the timestamp. */
1660 bsd_update_armap_timestamp (arch)
1663 struct stat archstat;
1667 /* Flush writes, get last-write timestamp from file, and compare it
1668 to the timestamp IN the file. */
1670 if (bfd_stat (arch, &archstat) == -1) {
1671 perror ("Reading archive file mod timestamp");
1672 return true; /* Can't read mod time for some reason */
1674 if (archstat.st_mtime <= bfd_ardata(arch)->armap_timestamp)
1675 return true; /* OK by the linker's rules */
1677 /* Update the timestamp. */
1678 bfd_ardata(arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET;
1680 /* Prepare an ASCII version suitable for writing. */
1681 memset (hdr.ar_date, 0, sizeof (hdr.ar_date));
1682 sprintf (hdr.ar_date, "%ld", bfd_ardata(arch)->armap_timestamp);
1683 for (i = 0; i < sizeof (hdr.ar_date); i++)
1684 if (hdr.ar_date[i] == '\0')
1685 (hdr.ar_date)[i] = ' ';
1687 /* Write it into the file. */
1688 bfd_seek (arch, bfd_ardata(arch)->armap_datepos, SEEK_SET);
1689 if (bfd_write (hdr.ar_date, sizeof(hdr.ar_date), 1, arch)
1690 != sizeof(hdr.ar_date)) {
1691 perror ("Writing updated armap timestamp");
1692 return true; /* Some error while writing */
1695 return false; /* We updated the timestamp successfully. */
1699 /* A coff armap looks like :
1701 struct ar_hdr with name = '/'
1703 offset of file for symbol 0
1704 offset of file for symbol 1
1706 offset of file for symbol n-1
1715 coff_write_armap (arch, elength, map, symbol_count, stridx)
1717 unsigned int elength;
1719 unsigned int symbol_count;
1722 /* The size of the ranlib is the number of exported symbols in the
1723 archive * the number of bytes in a int, + an int for the count */
1725 unsigned int ranlibsize = (symbol_count * 4) + 4;
1726 unsigned int stringsize = stridx;
1727 unsigned int mapsize = stringsize + ranlibsize;
1728 file_ptr archive_member_file_ptr;
1729 bfd *current = arch->archive_head;
1733 int padit = mapsize & 1;
1735 if (padit) mapsize ++;
1737 /* work out where the first object file will go in the archive */
1738 archive_member_file_ptr = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
1740 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1741 hdr.ar_name[0] = '/';
1742 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
1743 sprintf (hdr.ar_date, "%ld", (long)time (NULL));
1744 /* This, at least, is what Intel coff sets the values to.: */
1745 sprintf ((hdr.ar_uid), "%d", 0);
1746 sprintf ((hdr.ar_gid), "%d", 0);
1747 sprintf ((hdr.ar_mode), "%-7o",(unsigned ) 0);
1748 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1750 for (i = 0; i < sizeof (struct ar_hdr); i++)
1751 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
1753 /* Write the ar header for this item and the number of symbols */
1756 bfd_write ((PTR)&hdr, 1, sizeof (struct ar_hdr), arch);
1758 bfd_write_bigendian_4byte_int(arch, symbol_count);
1760 /* Two passes, first write the file offsets for each symbol -
1761 remembering that each offset is on a two byte boundary. */
1763 /* Write out the file offset for the file associated with each
1764 symbol, and remember to keep the offsets padded out. */
1766 current = arch->archive_head;
1768 while (current != (bfd *)NULL && count < symbol_count) {
1769 /* For each symbol which is used defined in this object, write out
1770 the object file's address in the archive */
1772 while (((bfd *)(map[count]).pos) == current) {
1773 bfd_write_bigendian_4byte_int(arch, archive_member_file_ptr);
1776 /* Add size of this archive entry */
1777 archive_member_file_ptr += arelt_size (current) + sizeof (struct
1779 /* remember aboout the even alignment */
1780 archive_member_file_ptr += archive_member_file_ptr % 2;
1781 current = current->next;
1786 /* now write the strings themselves */
1787 for (count = 0; count < symbol_count; count++) {
1788 bfd_write ((PTR)*((map[count]).name),
1790 strlen (*((map[count]).name))+1, arch);
1793 /* The spec sez this should be a newline. But in order to be
1794 bug-compatible for arc960 we use a null. */
1796 bfd_write("",1,1,arch);