3 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Diddler.
7 BFD 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 1, or (at your option)
12 BFD 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 BFD; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 @setfilename archive-info
26 Gumby, you promised to write this bit...
28 Archives are supported in bfd in @code{archive.c}.
30 An archive is represented internally just like another bfd, with a
31 pointer to a chain of contained bfds. Archives can be created by
32 opening bfds, linking them together and attatching them as children to
33 another bfd and then closing the parent bfd.
38 o - all archive elements start on an even boundary, newline padded;
39 o - all arch headers are char *;
40 o - all arch headers are the same size (across architectures).
52 #define BFD_GNU960_ARMAG(abfd) (BFD_COFF_FILE_P((abfd)) ? ARMAG : ARMAGB)
55 /* We keep a cache of archive filepointers to archive elements to
56 speed up searching the archive by filepos. We only add an entry to
57 the cache when we actually read one. We also don't sort the cache;
58 it's short enough to search linearly.
59 Note that the pointers here point to the front of the ar_hdr, not
60 to the front of the contents!
65 struct ar_cache *next;
68 #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
69 #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
71 #define arch_hdr(bfd) ((struct ar_hdr *) \
72 (((struct areltdata *)((bfd)->arelt_data))->arch_header))
75 _bfd_generic_mkarchive (abfd)
78 set_tdata (abfd, bfd_zalloc(abfd, sizeof (struct artdata)));
80 if (bfd_ardata (abfd) == NULL) {
81 bfd_error = no_memory;
84 bfd_ardata(abfd)->cache = 0;
88 /*proto* bfd_get_next_mapent
90 *; PROTO(symindex, bfd_get_next_mapent, (bfd *, symindex, carsym **));
93 bfd_get_next_mapent (abfd, prev, entry)
98 if (!bfd_has_map (abfd)) {
99 bfd_error = invalid_operation;
100 return BFD_NO_MORE_SYMBOLS;
103 if (prev == BFD_NO_MORE_SYMBOLS) prev = 0;
104 else if (++prev >= bfd_ardata (abfd)->symdef_count)
105 return BFD_NO_MORE_SYMBOLS;
107 *entry = (bfd_ardata (abfd)->symdefs + prev);
112 /* To be called by backends only */
114 _bfd_create_empty_archive_element_shell (obfd)
119 nbfd = new_bfd_contained_in(obfd);
121 bfd_error = no_memory;
127 /*proto* bfd_set_archive_head
128 Used whilst processing archives. Sets the head of the chain of bfds
129 contained in an archive to @var{new_head}. (see chapter on archives)
130 *; PROTO(boolean, bfd_set_archive_head, (bfd *output, bfd *new_head));
134 DEFUN(bfd_set_archive_head,(output_archive, new_head),
135 bfd *output_archive AND
139 output_archive->archive_head = new_head;
144 look_for_bfd_in_cache (arch_bfd, filepos)
148 struct ar_cache *current;
150 for (current = bfd_ardata (arch_bfd)->cache; current != NULL;
151 current = current->next)
152 if (current->ptr == filepos) return current->arelt;
157 /* Kind of stupid to call cons for each one, but we don't do too many */
159 add_bfd_to_cache (arch_bfd, filepos, new_elt)
160 bfd *arch_bfd, *new_elt;
163 struct ar_cache *new_cache = (struct ar_cache *)
164 bfd_zalloc(arch_bfd, sizeof (struct ar_cache));
166 if (new_cache == NULL) {
167 bfd_error = no_memory;
171 new_cache->ptr = filepos;
172 new_cache->arelt = new_elt;
173 new_cache->next = (struct ar_cache *)NULL;
174 if (bfd_ardata (arch_bfd)->cache == NULL)
175 bfd_ardata (arch_bfd)->cache = new_cache;
177 struct ar_cache *current = bfd_ardata (arch_bfd)->cache;
179 for (; current->next != NULL; current = current->next);
180 current->next = new_cache;
188 /* The name begins with space. Hence the rest of the name is an index into
192 get_extended_arelt_filename (arch, name)
199 unsigned long index = 0;
201 /* Should extract string so that I can guarantee not to overflow into
202 the next region, but I"m too lazy. */
204 index = strtol (name, NULL, 10);
206 bfd_error = malformed_archive;
210 return bfd_ardata (arch)->extended_names + index;
213 /* This functions reads an arch header and returns an areltdata pointer, or
216 Presumes the file pointer is already in the right place (ie pointing
217 to the ar_hdr in the file). Moves the file pointer; on success it
218 should be pointing to the front of the file contents; on failure it
219 could have been moved arbitrarily.
231 char *hdrp = (char *) &hdr;
232 unsigned int parsed_size;
233 struct areltdata *ared;
234 char *filename = NULL;
235 unsigned int namelen = 0;
236 unsigned int allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
239 if (bfd_read ((PTR)hdrp, 1, sizeof (struct ar_hdr), abfd)
240 != sizeof (struct ar_hdr)) {
241 bfd_error = no_more_archived_files;
244 if (strncmp ((hdr.ar_fmag), ARFMAG, 2)) {
245 bfd_error = malformed_archive;
250 parsed_size = strtol (hdr.ar_size, NULL, 10);
252 bfd_error = malformed_archive;
256 /* extract the filename from the archive - there are two ways to
257 specify an extendend name table, either the first char of the
258 name is a space, or it's a slash */
259 if ((hdr.ar_name[0] == '/' || hdr.ar_name[0] == ' ') && bfd_ardata (abfd)->extended_names != NULL) {
260 filename = get_extended_arelt_filename (abfd, hdr.ar_name);
261 if (filename == NULL) {
262 bfd_error = malformed_archive;
268 /* We judge the end of the name by looking for a space or a
273 while (namelen < (unsigned)ar_maxnamelen(abfd) &&
274 ( hdr.ar_name[namelen] != 0 &&
275 hdr.ar_name[namelen] != ' ' &&
276 hdr.ar_name[namelen] != ar_padchar(abfd))) {
280 allocsize += namelen + 1;
283 allocptr = bfd_zalloc(abfd, allocsize);
284 if (allocptr == NULL) {
285 bfd_error = no_memory;
289 ared = (struct areltdata *) allocptr;
291 ared->arch_header = allocptr + sizeof (struct areltdata);
292 memcpy ((char *) ared->arch_header, &hdr, sizeof (struct ar_hdr));
293 ared->parsed_size = parsed_size;
295 if (filename != NULL) ared->filename = filename;
297 ared->filename = allocptr + (sizeof (struct areltdata) +
298 sizeof (struct ar_hdr));
300 memcpy (ared->filename, hdr.ar_name, namelen);
301 ared->filename[namelen] = '\0';
308 get_elt_at_filepos (archive, filepos)
312 struct areltdata *new_areldata;
315 n_nfd = look_for_bfd_in_cache (archive, filepos);
316 if (n_nfd) return n_nfd;
318 if (0 > bfd_seek (archive, filepos, SEEK_SET)) {
319 bfd_error = system_call_error;
323 if ((new_areldata = snarf_ar_hdr (archive)) == NULL) return NULL;
325 n_nfd = _bfd_create_empty_archive_element_shell (archive);
327 bfd_release (archive, (PTR)new_areldata);
330 n_nfd->origin = bfd_tell (archive);
331 n_nfd->arelt_data = (PTR) new_areldata;
332 n_nfd->filename = new_areldata->filename;
334 if (add_bfd_to_cache (archive, filepos, n_nfd))
338 bfd_release (archive, (PTR)n_nfd);
339 bfd_release (archive, (PTR)new_areldata);
344 bfd_get_elt_at_index (abfd, index)
350 (abfd, (bfd_ardata (abfd)->symdefs + index)->file_offset);
354 /*proto* bfd_openr_next_archived_file
355 Initially provided a bfd containing an archive and NULL, opens a bfd
356 on the first contained element and returns that. Subsequent calls to
357 bfd_openr_next_archived_file should pass the archive and the previous
358 return value to return a created bfd to the next contained element.
359 NULL is returned when there are no more.
361 *; PROTO(bfd*, bfd_openr_next_archived_file,
362 (bfd *archive, bfd *previous));
367 DEFUN(bfd_openr_next_archived_file,(archive, last_file),
372 if ((bfd_get_format (archive) != bfd_archive) ||
373 (archive->direction == write_direction)) {
374 bfd_error = invalid_operation;
379 return BFD_SEND (archive,
380 openr_next_archived_file,
386 bfd *bfd_generic_openr_next_archived_file(archive, last_file)
393 filestart = bfd_ardata (archive)->first_file_filepos;
395 unsigned int size = arelt_size(last_file);
396 /* Pad to an even boundary... */
397 filestart = last_file->origin + size + size%2;
400 return get_elt_at_filepos (archive, filestart);
405 bfd_generic_archive_p (abfd)
408 char armag[SARMAG+1];
410 if (bfd_read ((PTR)armag, 1, SARMAG, abfd) != SARMAG) {
411 bfd_error = wrong_format;
416 if (strncmp (armag, BFD_GNU960_ARMAG(abfd), SARMAG)) return 0;
418 if (strncmp (armag, ARMAG, SARMAG)) return 0;
421 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
422 involves a cast, we can't do it as the left operand of assignment. */
423 set_tdata (abfd, bfd_zalloc(abfd,sizeof (struct artdata)));
425 if (bfd_ardata (abfd) == NULL) {
426 bfd_error = no_memory;
430 bfd_ardata (abfd)->first_file_filepos = SARMAG;
432 if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd))) {
433 bfd_release(abfd, bfd_ardata (abfd));
438 if (!BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd))) {
439 bfd_release(abfd, bfd_ardata (abfd));
447 /* Returns false on error, true otherwise */
449 bfd_slurp_bsd_armap (abfd)
453 struct areltdata *mapdata;
455 unsigned int counter = 0;
456 int *raw_armap, *rbase;
457 struct artdata *ardata = bfd_ardata (abfd);
460 /* FIXME, if the read fails, this routine quietly returns "true"!!
461 It should probably do that if the read gives 0 bytes (empty archive),
462 but fail for any other size... */
463 if (bfd_read ((PTR)nextname, 1, 16, abfd) == 16) {
464 /* The archive has at least 16 bytes in it */
465 bfd_seek (abfd, -16L, SEEK_CUR);
467 /* This should be using RANLIBMAG, but at least it can be grepped for
469 if (strncmp (nextname, "__.SYMDEF ", 16)) {
470 bfd_has_map (abfd) = false;
474 mapdata = snarf_ar_hdr (abfd);
475 if (mapdata == NULL) return false;
477 raw_armap = (int *) bfd_zalloc(abfd,mapdata->parsed_size);
478 if (raw_armap == NULL) {
479 bfd_error = no_memory;
481 bfd_release (abfd, (PTR)mapdata);
485 if (bfd_read ((PTR)raw_armap, 1, mapdata->parsed_size, abfd) !=
486 mapdata->parsed_size) {
487 bfd_error = malformed_archive;
488 bfd_release (abfd, (PTR)raw_armap);
492 ardata->symdef_count = bfd_h_get_32(abfd, (PTR)raw_armap) / sizeof (struct symdef);
495 ardata->symdefs = (carsym *) rbase;
496 stringbase = ((char *) (ardata->symdefs + ardata->symdef_count)) + 4;
498 for (;counter < ardata->symdef_count; counter++) {
499 struct symdef *sym = ((struct symdef *) rbase) + counter;
500 sym->s.name = bfd_h_get_32(abfd, (PTR)(&(sym->s.string_offset))) + stringbase;
501 sym->file_offset = bfd_h_get_32(abfd, (PTR)( &(sym->file_offset)));
504 ardata->first_file_filepos = bfd_tell (abfd);
505 /* Pad to an even boundary if you have to */
506 ardata->first_file_filepos += (ardata-> first_file_filepos) %2;
507 /* FIXME, we should provide some way to free raw_ardata when
508 we are done using the strings from it. For now, it seems
509 to be allocated on an obstack anyway... */
510 bfd_has_map (abfd) = true;
515 /* Returns false on error, true otherwise */
517 bfd_slurp_coff_armap (abfd)
520 struct areltdata *mapdata;
522 int *raw_armap, *rawptr;
523 struct artdata *ardata = bfd_ardata (abfd);
525 unsigned int stringsize;
529 result = bfd_read ((PTR)&nextname, 1, 1, abfd);
530 bfd_seek (abfd, -1L, SEEK_CUR);
532 if (result != 1 || nextname != '/') {
533 /* Actually I think this is an error for a COFF archive */
534 bfd_has_map (abfd) = false;
538 mapdata = snarf_ar_hdr (abfd);
539 if (mapdata == NULL) return false;
541 raw_armap = (int *) bfd_alloc(abfd,mapdata->parsed_size);
543 if (raw_armap == NULL)
545 bfd_error = no_memory;
547 bfd_release (abfd, (PTR)mapdata);
551 /* read in the raw map */
552 if (bfd_read ((PTR)raw_armap, 1, mapdata->parsed_size, abfd) !=
553 mapdata->parsed_size) {
554 bfd_error = malformed_archive;
556 bfd_release (abfd, (PTR)raw_armap);
560 /* The coff armap must be read sequentially. So we construct a bsd-style
561 one in core all at once, for simplicity. */
563 stringsize = mapdata->parsed_size - (4 * (*raw_armap)) - 4;
566 unsigned int nsymz = *raw_armap;
567 unsigned int carsym_size = (nsymz * sizeof (carsym));
568 unsigned int ptrsize = (4 * nsymz);
570 ardata->symdefs = (carsym *) bfd_zalloc(abfd,carsym_size + stringsize + 1);
571 if (ardata->symdefs == NULL) {
572 bfd_error = no_memory;
575 carsyms = ardata->symdefs;
577 stringbase = ((char *) ardata->symdefs) + carsym_size;
578 memcpy (stringbase, (char*)raw_armap + ptrsize + 4, stringsize);
581 /* OK, build the carsyms */
582 for (i = 0; i < nsymz; i++)
584 rawptr = raw_armap + i + 1;
585 carsyms->file_offset = *rawptr;
586 carsyms->name = stringbase;
587 for (; *(stringbase++););
592 ardata->symdef_count = *raw_armap;
593 ardata->first_file_filepos = bfd_tell (abfd);
594 /* Pad to an even boundary if you have to */
595 ardata->first_file_filepos += (ardata->first_file_filepos) %2;
597 /* bfd_release (abfd, (PTR)raw_armap);
598 bfd_release (abfd, (PTR)mapdata);*/
599 bfd_has_map (abfd) = true;
603 /** Extended name table.
605 Normally archives support only 14-character filenames.
607 Intel has extended the format: longer names are stored in a special
608 element (the first in the archive, or second if there is an armap);
609 the name in the ar_hdr is replaced by <space><index into filename
610 element>. Index is the P.R. of an int (radix: 8). Data General have
611 extended the format by using the prefix // for the special element */
613 /* Returns false on error, true otherwise */
615 _bfd_slurp_extended_name_table (abfd)
619 struct areltdata *namedata;
621 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
622 we probably don't want to return true. */
623 if (bfd_read ((PTR)nextname, 1, 16, abfd) == 16) {
625 bfd_seek (abfd, -16L, SEEK_CUR);
627 if (strncmp (nextname, "ARFILENAMES/ ", 16) != 0 &&
628 strncmp (nextname, "// ", 16) != 0)
630 bfd_ardata (abfd)->extended_names = NULL;
634 namedata = snarf_ar_hdr (abfd);
635 if (namedata == NULL) return false;
637 bfd_ardata (abfd)->extended_names = bfd_zalloc(abfd,namedata->parsed_size);
638 if (bfd_ardata (abfd)->extended_names == NULL) {
639 bfd_error = no_memory;
641 bfd_release (abfd, (PTR)namedata);
645 if (bfd_read ((PTR)bfd_ardata (abfd)->extended_names, 1,
646 namedata->parsed_size, abfd) != namedata->parsed_size) {
647 bfd_error = malformed_archive;
648 bfd_release (abfd, (PTR)(bfd_ardata (abfd)->extended_names));
649 bfd_ardata (abfd)->extended_names = NULL;
653 /* Since the archive is supposed to be printable if it contains
654 text, the entries in the list are newline-padded, not null
655 padded. We'll fix that there.. */
657 char *temp = bfd_ardata (abfd)->extended_names;
658 for (; *temp != '\0'; ++temp)
659 if (*temp == '\n') *temp = '\0';
662 /* Pad to an even boundary if you have to */
663 bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd);
664 bfd_ardata (abfd)->first_file_filepos +=
665 (bfd_ardata (abfd)->first_file_filepos) %2;
667 /* FIXME, we can't release namedata here because it was allocated
668 below extended_names on the obstack... */
669 /* bfd_release (abfd, namedata); */
675 char *normalize(file)
678 char * filename = strrchr(file, '/');
679 if (filename != (char *)NULL) {
688 /* Follows archive_head and produces an extended name table if necessary.
689 Returns (in tabloc) a pointer to an extended name table, and in tablen
690 the length of the table. If it makes an entry it clobbers the filename
691 so that the element may be written without further massage.
692 Returns true if it ran successfully, false if something went wrong.
693 A successful return may still involve a zero-length tablen!
696 bfd_construct_extended_name_table (abfd, tabloc, tablen)
699 unsigned int *tablen;
701 unsigned int maxname = abfd->xvec->ar_max_namelen;
702 unsigned int total_namelen = 0;
708 /* Figure out how long the table should be */
709 for (current = abfd->archive_head; current != NULL; current = current->next){
710 unsigned int thislen = strlen (normalize(current->filename));
711 if (thislen > maxname) total_namelen += thislen + 1; /* leave room for \n */
714 if (total_namelen == 0) return true;
716 *tabloc = bfd_zalloc (abfd,total_namelen);
717 if (*tabloc == NULL) {
718 bfd_error = no_memory;
722 *tablen = total_namelen;
725 for (current = abfd->archive_head; current != NULL; current =
727 char *normal =normalize( current->filename);
728 unsigned int thislen = strlen (normal);
729 if (thislen > maxname) {
730 /* Works for now; may need to be re-engineered if we encounter an oddball
731 archive format and want to generalise this hack. */
732 struct ar_hdr *hdr = arch_hdr(current);
733 strcpy (strptr, normal);
734 strptr[thislen] = '\n';
735 hdr->ar_name[0] = ' ';
736 /* We know there will always be enough room (one of the few cases
737 where you may safely use sprintf). */
738 sprintf ((hdr->ar_name) + 1, "%-o", (unsigned) (strptr - *tabloc));
739 /* Kinda Kludgy. We should just use the returned value of sprintf
740 but not all implementations get this right */
742 char *temp = hdr->ar_name +2;
743 for (; temp < hdr->ar_name + maxname; temp++)
744 if (*temp == '\0') *temp = ' ';
746 strptr += thislen + 1;
753 /** A couple of functions for creating ar_hdrs */
755 /* Takes a filename, returns an arelt_data for it, or NULL if it can't make one.
756 The filename must refer to a filename in the filesystem.
757 The filename field of the ar_hdr will NOT be initialized
761 DEFUN(bfd_ar_hdr_from_filesystem, (abfd,filename),
763 CONST char *filename)
766 struct areltdata *ared;
771 if (stat (filename, &status) != 0) {
772 bfd_error = system_call_error;
776 ared = (struct areltdata *) bfd_zalloc(abfd, sizeof (struct ar_hdr) +
777 sizeof (struct areltdata));
779 bfd_error = no_memory;
782 hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
784 /* ar headers are space padded, not null padded! */
786 temp1 = temp + sizeof (struct ar_hdr) - 2;
787 for (; temp < temp1; *(temp++) = ' ');
788 strncpy (hdr->ar_fmag, ARFMAG, 2);
790 /* Goddamned sprintf doesn't permit MAXIMUM field lengths */
791 sprintf ((hdr->ar_date), "%-12ld", status.st_mtime);
792 sprintf ((hdr->ar_uid), "%d", status.st_uid);
793 sprintf ((hdr->ar_gid), "%d", status.st_gid);
794 sprintf ((hdr->ar_mode), "%-8o", (unsigned) status.st_mode);
795 sprintf ((hdr->ar_size), "%-10ld", status.st_size);
796 /* Correct for a lossage in sprintf whereby it null-terminates. I cannot
797 understand how these C losers could design such a ramshackle bunch of
800 temp1 = temp + sizeof (struct ar_hdr) - 2;
801 for (; temp < temp1; temp++) {
802 if (*temp == '\0') *temp = ' ';
804 strncpy (hdr->ar_fmag, ARFMAG, 2);
805 ared->parsed_size = status.st_size;
806 ared->arch_header = (char *) hdr;
812 DEFUN(bfd_special_undocumented_glue, (abfd, filename),
817 return (struct ar_hdr *) bfd_ar_hdr_from_filesystem (abfd, filename) -> arch_header;
821 /* Analogous to stat call */
823 bfd_generic_stat_arch_elt (abfd, buf)
830 if (abfd->arelt_data == NULL) {
831 bfd_error = invalid_operation;
835 hdr = arch_hdr (abfd);
837 #define foo(arelt, stelt, size) \
838 buf->stelt = strtol (hdr->arelt, &aloser, size); \
839 if (aloser == hdr->arelt) return -1;
841 foo (ar_date, st_mtime, 10);
842 foo (ar_uid, st_uid, 10);
843 foo (ar_gid, st_gid, 10);
844 foo (ar_mode, st_mode, 8);
845 foo (ar_size, st_size, 10);
851 bfd_dont_truncate_arname (abfd, pathname, arhdr)
853 CONST char *pathname;
856 /* FIXME: This interacts unpleasantly with ar's quick-append option.
857 Fortunately ic960 users will never use that option. Fixing this
858 is very hard; fortunately I know how to do it and will do so once
859 intel's release is out the door. */
861 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
863 CONST char *filename = strrchr (pathname, '/');
864 int maxlen = ar_maxnamelen (abfd);
866 if (filename == NULL)
871 length = strlen (filename);
873 if (length <= maxlen)
874 memcpy (hdr->ar_name, filename, length);
876 if (length < maxlen) (hdr->ar_name)[length] = ar_padchar (abfd);
882 bfd_bsd_truncate_arname (abfd, pathname, arhdr)
884 CONST char *pathname;
887 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
889 CONST char *filename = strrchr (pathname, '/');
890 int maxlen = ar_maxnamelen (abfd);
893 if (filename == NULL)
898 length = strlen (filename);
900 if (length <= maxlen)
901 memcpy (hdr->ar_name, filename, length);
903 /* pathname: meet procrustes */
904 memcpy (hdr->ar_name, filename, maxlen);
908 if (length < maxlen) (hdr->ar_name)[length] = ar_padchar (abfd);
911 /* Store name into ar header. Truncates the name to fit.
912 1> strip pathname to be just the basename.
913 2> if it's short enuf to fit, stuff it in.
914 3> If it doesn't end with .o, truncate it to fit
915 4> truncate it before the .o, append .o, stuff THAT in.
918 /* This is what gnu ar does. It's better but incompatible with the bsd ar. */
920 bfd_gnu_truncate_arname (abfd, pathname, arhdr)
922 CONST char *pathname;
925 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
927 CONST char *filename = strrchr (pathname, '/');
928 int maxlen = ar_maxnamelen (abfd);
930 if (filename == NULL)
935 length = strlen (filename);
937 if (length <= maxlen)
938 memcpy (hdr->ar_name, filename, length);
939 else { /* pathname: meet procrustes */
940 memcpy (hdr->ar_name, filename, maxlen);
941 if ((filename[length - 2] == '.') && (filename[length - 1] == 'o')) {
942 hdr->ar_name[maxlen - 2] = '.';
943 hdr->ar_name[maxlen - 1] = 'o';
948 if (length < 16) (hdr->ar_name)[length] = ar_padchar (abfd);
952 PROTO (boolean, compute_and_write_armap, (bfd *arch, unsigned int elength));
954 /* The bfd is open for write and has its format set to bfd_archive */
956 _bfd_write_archive_contents (arch)
961 unsigned int elength = 0;
962 boolean makemap = bfd_has_map (arch);
963 boolean hasobjects = false; /* if no .o's, don't bother to make a map */
966 /* Verify the viability of all entries; if any of them live in the
967 filesystem (as opposed to living in an archive open for input)
968 then construct a fresh ar_hdr for them.
970 for (current = arch->archive_head; current; current = current->next) {
971 if (bfd_write_p (current)) {
972 bfd_error = invalid_operation;
975 if (!current->arelt_data) {
976 current->arelt_data =
977 (PTR) bfd_ar_hdr_from_filesystem (arch, current->filename);
978 if (!current->arelt_data) return false;
980 /* Put in the file name */
982 BFD_SEND (arch, _bfd_truncate_arname,(arch,
984 (char *) arch_hdr(current)));
989 if (makemap) { /* don't bother if we won't make a map! */
990 if ((bfd_check_format (current, bfd_object))
991 #if 0 /* FIXME -- these are not set correctly */
992 && ((bfd_get_file_flags (current) & HAS_SYMS))
999 if (!bfd_construct_extended_name_table (arch, &etable, &elength))
1002 bfd_seek (arch, 0, SEEK_SET);
1004 bfd_write (BFD_GNU960_ARMAG(arch), 1, SARMAG, arch);
1006 bfd_write (ARMAG, 1, SARMAG, arch);
1009 if (makemap && hasobjects) {
1011 if (compute_and_write_armap (arch, elength) != true) {
1019 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1020 sprintf (&(hdr.ar_name[0]), "ARFILENAMES/");
1021 sprintf (&(hdr.ar_size[0]), "%-10d", (int) elength);
1022 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1023 for (i = 0; i < sizeof (struct ar_hdr); i++)
1024 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
1025 bfd_write ((char *)&hdr, 1, sizeof (struct ar_hdr), arch);
1026 bfd_write (etable, 1, elength, arch);
1027 if ((elength % 2) == 1) bfd_write ("\n", 1, 1, arch);
1031 for (current = arch->archive_head; current; current = current->next) {
1032 char buffer[DEFAULT_BUFFERSIZE];
1033 unsigned int remaining = arelt_size (current);
1034 struct ar_hdr *hdr = arch_hdr(current);
1035 /* write ar header */
1037 if (bfd_write ((char *)hdr, 1, sizeof(*hdr), arch) != sizeof(*hdr)) {
1039 bfd_error = system_call_error;
1042 if (bfd_seek (current, 0L, SEEK_SET) != 0L) goto syserr;
1045 unsigned int amt = DEFAULT_BUFFERSIZE;
1046 if (amt > remaining) {
1049 if (bfd_read (buffer, amt, 1, current) != amt) goto syserr;
1050 if (bfd_write (buffer, amt, 1, arch) != amt) goto syserr;
1053 if ((arelt_size (current) % 2) == 1) bfd_write ("\n", 1, 1, arch);
1058 /* Note that the namidx for the first symbol is 0 */
1061 compute_and_write_armap (arch, elength)
1063 unsigned int elength;
1066 file_ptr elt_no = 0;
1068 int orl_max = 15000; /* fine initial default */
1070 int stridx = 0; /* string index */
1072 /* Dunno if this is the best place for this info... */
1073 if (elength != 0) elength += sizeof (struct ar_hdr);
1074 elength += elength %2 ;
1076 map = (struct orl *) bfd_zalloc (arch,orl_max * sizeof (struct orl));
1078 bfd_error = no_memory;
1082 /* Map over each element */
1083 for (current = arch->archive_head;
1084 current != (bfd *)NULL;
1085 current = current->next, elt_no++)
1087 if ((bfd_check_format (current, bfd_object) == true)
1088 && ((bfd_get_file_flags (current) & HAS_SYMS))) {
1090 unsigned int storage;
1091 unsigned int symcount;
1092 unsigned int src_count;
1094 storage = get_symtab_upper_bound (current);
1097 syms = (asymbol **) bfd_zalloc (arch,storage);
1099 bfd_error = no_memory; /* FIXME -- memory leak */
1102 symcount = bfd_canonicalize_symtab (current, syms);
1105 /* Now map over all the symbols, picking out the ones we want */
1106 for (src_count = 0; src_count <symcount; src_count++) {
1107 flagword flags = (syms[src_count])->flags;
1108 if ((flags & BSF_GLOBAL) ||
1109 (flags & BSF_FORT_COMM)) {
1111 /* This symbol will go into the archive header */
1112 if (orl_count == orl_max)
1115 map = (struct orl *) bfd_realloc (arch, (char *) map,
1116 orl_max * sizeof (struct orl));
1119 (map[orl_count]).name = (char **) &((syms[src_count])->name);
1120 (map[orl_count]).pos = (file_ptr) current;
1121 (map[orl_count]).namidx = stridx;
1123 stridx += strlen ((syms[src_count])->name) + 1;
1130 /* OK, now we have collected all the data, let's write them out */
1131 if (!BFD_SEND (arch, write_armap,
1132 (arch, elength, map, orl_count, stridx))) {
1142 bsd_write_armap (arch, elength, map, orl_count, stridx)
1144 unsigned int elength;
1149 unsigned int ranlibsize = (orl_count * sizeof (struct ranlib)) + 4;
1150 unsigned int stringsize = stridx + 4;
1151 unsigned int mapsize = stringsize + ranlibsize;
1153 bfd *current = arch->archive_head;
1154 bfd *last_elt = current; /* last element arch seen */
1158 struct stat statbuf;
1160 int padit = mapsize & 1;
1162 if (padit) mapsize ++;
1164 firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
1166 stat (arch->filename, &statbuf);
1167 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1168 sprintf (hdr.ar_name, RANLIBMAG);
1169 sprintf (hdr.ar_date, "%ld", statbuf.st_mtime);
1170 sprintf (hdr.ar_uid, "%d", getuid());
1171 sprintf (hdr.ar_gid, "%d", getgid());
1172 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
1173 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1174 for (i = 0; i < sizeof (struct ar_hdr); i++)
1175 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
1176 bfd_write ((char *)&hdr, 1, sizeof (struct ar_hdr), arch);
1177 bfd_h_put_32(arch, ranlibsize, (PTR)&temp);
1178 bfd_write (&temp, 1, sizeof (temp), arch);
1180 for (count = 0; count < orl_count; count++) {
1182 struct symdef *outp = &outs;
1184 if (((bfd *)(map[count]).pos) != last_elt) {
1186 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
1187 firstreal += firstreal % 2;
1188 current = current->next;
1189 } while (current != (bfd *)(map[count]).pos);
1190 } /* if new archive element */
1193 bfd_h_put_32(arch, ((map[count]).namidx),(PTR) &outs.s.string_offset);
1194 bfd_h_put_32(arch, firstreal,(PTR) &outs.file_offset);
1195 bfd_write ((char *)outp, 1, sizeof (outs), arch);
1198 /* now write the strings themselves */
1199 bfd_h_put_32(arch, stridx, (PTR)&temp);
1200 bfd_write ((PTR)&temp, 1, sizeof (temp), arch);
1201 for (count = 0; count < orl_count; count++)
1202 bfd_write (*((map[count]).name), 1, strlen (*((map[count]).name))+1, arch);
1204 /* The spec sez this should be a newline. But in order to be
1205 bug-compatible for sun's ar we use a null. */
1207 bfd_write("\0",1,1,arch);
1213 /* A coff armap looks like :
1215 struct ar_hdr with name = '/'
1217 offset of file for symbol 0
1218 offset of file for symbol 1
1220 offset of file for symbol n-1
1229 coff_write_armap (arch, elength, map, orl_count, stridx)
1231 unsigned int elength;
1236 unsigned int ranlibsize = (orl_count * 4) + 4;
1237 unsigned int stringsize = stridx;
1238 unsigned int mapsize = stringsize + ranlibsize;
1239 file_ptr archive_member_file_ptr;
1240 bfd *current = arch->archive_head;
1241 int last_eltno = 0; /* last element arch seen */
1245 int padit = mapsize & 1;
1247 if (padit) mapsize ++;
1249 archive_member_file_ptr =
1250 mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
1252 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1253 hdr.ar_name[0] = '/';
1254 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
1255 sprintf (hdr.ar_date, "%ld", (long)time (NULL));
1256 /* This, at least, is what Intel coff sets the values to.: */
1257 sprintf ((hdr.ar_uid), "%d", 0);
1258 sprintf ((hdr.ar_gid), "%d", 0);
1259 sprintf ((hdr.ar_mode), "%-7o",(unsigned ) 0);
1260 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1262 for (i = 0; i < sizeof (struct ar_hdr); i++)
1263 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
1265 /* Write the ar header for this item and the number of symbols */
1267 bfd_write ((PTR)&hdr, 1, sizeof (struct ar_hdr), arch);
1268 /* FIXME, this needs to be byte-swapped */
1269 bfd_write ((PTR)&orl_count, 1, sizeof (orl_count), arch);
1271 /* Two passes, first write the file offsets for each symbol -
1272 remembering that each offset is on a two byte boundary
1275 for (count = 0; count < orl_count; count++) {
1276 while ((map[count]).pos != last_eltno) {
1277 /* If this is the first time we've seen a ref to this archive
1278 then remember it's size */
1279 archive_member_file_ptr +=
1280 arelt_size (current) + sizeof (struct ar_hdr);
1281 archive_member_file_ptr += archive_member_file_ptr % 2;
1282 current = current->next;
1285 /* FIXME, this needs to be byte-swapped */
1286 bfd_write ((PTR)&archive_member_file_ptr,
1288 sizeof (archive_member_file_ptr),
1292 /* now write the strings themselves */
1293 for (count = 0; count < orl_count; count++) {
1294 bfd_write ((PTR)*((map[count]).name),
1296 strlen (*((map[count]).name))+1, arch);
1299 /* The spec sez this should be a newline. But in order to be
1300 bug-compatible for arc960 we use a null. */
1302 bfd_write("\0",1,1,arch);