1 /* BFD back-end for archive files (libraries).
2 Copyright (C) 1990-1991 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 Gumby, you promised to write this bit...
29 Archives are supported in BFD in <<archive.c>>.
31 An archive is represented internally just like another BFD,
32 with a pointer to a chain of contained BFDs. Archives can be
33 created by opening BFDs, linking them together and attaching
34 them as children to another BFD and then closing the parent
40 o - all archive elements start on an even boundary, newline padded;
41 o - all arch headers are char *;
42 o - all arch headers are the same size (across architectures).
51 #include "aout/ranlib.h"
54 #define BFD_GNU960_ARMAG(abfd) (BFD_COFF_FILE_P((abfd)) ? ARMAG : ARMAGB)
57 /* We keep a cache of archive filepointers to archive elements to
58 speed up searching the archive by filepos. We only add an entry to
59 the cache when we actually read one. We also don't sort the cache;
60 it's short enough to search linearly.
61 Note that the pointers here point to the front of the ar_hdr, not
62 to the front of the contents!
67 struct ar_cache
*next
;
70 #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
71 #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
73 #define arch_hdr(bfd) ((struct ar_hdr *) \
74 (((struct areltdata *)((bfd)->arelt_data))->arch_header))
77 _bfd_generic_mkarchive (abfd
)
80 set_tdata (abfd
, bfd_zalloc(abfd
, sizeof (struct artdata
)));
82 if (bfd_ardata (abfd
) == NULL
) {
83 bfd_error
= no_memory
;
86 bfd_ardata(abfd
)->cache
= 0;
98 symindex bfd_get_next_mapent(bfd *, symindex, carsym **);
101 bfd_get_next_mapent (abfd
, prev
, entry
)
106 if (!bfd_has_map (abfd
)) {
107 bfd_error
= invalid_operation
;
108 return BFD_NO_MORE_SYMBOLS
;
111 if (prev
== BFD_NO_MORE_SYMBOLS
) prev
= 0;
112 else if (++prev
>= bfd_ardata (abfd
)->symdef_count
)
113 return BFD_NO_MORE_SYMBOLS
;
115 *entry
= (bfd_ardata (abfd
)->symdefs
+ prev
);
120 /* To be called by backends only */
122 _bfd_create_empty_archive_element_shell (obfd
)
127 nbfd
= new_bfd_contained_in(obfd
);
129 bfd_error
= no_memory
;
140 Used whilst processing archives. Sets the head of the chain of
141 BFDs contained in an archive to @var{new_head}. (see chapter
145 boolean bfd_set_archive_head(bfd *output, bfd *new_head);
150 DEFUN(bfd_set_archive_head
,(output_archive
, new_head
),
151 bfd
*output_archive AND
155 output_archive
->archive_head
= new_head
;
160 look_for_bfd_in_cache (arch_bfd
, filepos
)
164 struct ar_cache
*current
;
166 for (current
= bfd_ardata (arch_bfd
)->cache
; current
!= NULL
;
167 current
= current
->next
)
168 if (current
->ptr
== filepos
) return current
->arelt
;
173 /* Kind of stupid to call cons for each one, but we don't do too many */
175 add_bfd_to_cache (arch_bfd
, filepos
, new_elt
)
176 bfd
*arch_bfd
, *new_elt
;
179 struct ar_cache
*new_cache
= (struct ar_cache
*)
180 bfd_zalloc(arch_bfd
, sizeof (struct ar_cache
));
182 if (new_cache
== NULL
) {
183 bfd_error
= no_memory
;
187 new_cache
->ptr
= filepos
;
188 new_cache
->arelt
= new_elt
;
189 new_cache
->next
= (struct ar_cache
*)NULL
;
190 if (bfd_ardata (arch_bfd
)->cache
== NULL
)
191 bfd_ardata (arch_bfd
)->cache
= new_cache
;
193 struct ar_cache
*current
= bfd_ardata (arch_bfd
)->cache
;
195 for (; current
->next
!= NULL
; current
= current
->next
);
196 current
->next
= new_cache
;
204 /* The name begins with space. Hence the rest of the name is an index into
208 get_extended_arelt_filename (arch
, name
)
215 unsigned long index
= 0;
217 /* Should extract string so that I can guarantee not to overflow into
218 the next region, but I"m too lazy. */
220 index
= strtol (name
, NULL
, 10);
222 bfd_error
= malformed_archive
;
226 return bfd_ardata (arch
)->extended_names
+ index
;
229 /* This functions reads an arch header and returns an areltdata pointer, or
232 Presumes the file pointer is already in the right place (ie pointing
233 to the ar_hdr in the file). Moves the file pointer; on success it
234 should be pointing to the front of the file contents; on failure it
235 could have been moved arbitrarily.
247 char *hdrp
= (char *) &hdr
;
248 unsigned int parsed_size
;
249 struct areltdata
*ared
;
250 char *filename
= NULL
;
251 unsigned int namelen
= 0;
252 unsigned int allocsize
= sizeof (struct areltdata
) + sizeof (struct ar_hdr
);
255 if (bfd_read ((PTR
)hdrp
, 1, sizeof (struct ar_hdr
), abfd
)
256 != sizeof (struct ar_hdr
)) {
257 bfd_error
= no_more_archived_files
;
260 if (strncmp ((hdr
.ar_fmag
), ARFMAG
, 2)) {
261 bfd_error
= malformed_archive
;
266 parsed_size
= strtol (hdr
.ar_size
, NULL
, 10);
268 bfd_error
= malformed_archive
;
272 /* extract the filename from the archive - there are two ways to
273 specify an extendend name table, either the first char of the
274 name is a space, or it's a slash */
275 if ((hdr
.ar_name
[0] == '/' || hdr
.ar_name
[0] == ' ') && bfd_ardata (abfd
)->extended_names
!= NULL
) {
276 filename
= get_extended_arelt_filename (abfd
, hdr
.ar_name
);
277 if (filename
== NULL
) {
278 bfd_error
= malformed_archive
;
284 /* We judge the end of the name by looking for a space or a
289 while (namelen
< (unsigned)ar_maxnamelen(abfd
) &&
290 ( hdr
.ar_name
[namelen
] != 0 &&
291 hdr
.ar_name
[namelen
] != ' ' &&
292 hdr
.ar_name
[namelen
] != ar_padchar(abfd
))) {
296 allocsize
+= namelen
+ 1;
299 allocptr
= bfd_zalloc(abfd
, allocsize
);
300 if (allocptr
== NULL
) {
301 bfd_error
= no_memory
;
305 ared
= (struct areltdata
*) allocptr
;
307 ared
->arch_header
= allocptr
+ sizeof (struct areltdata
);
308 memcpy ((char *) ared
->arch_header
, (char *) &hdr
, sizeof (struct ar_hdr
));
309 ared
->parsed_size
= parsed_size
;
311 if (filename
!= NULL
) ared
->filename
= filename
;
313 ared
->filename
= allocptr
+ (sizeof (struct areltdata
) +
314 sizeof (struct ar_hdr
));
316 memcpy (ared
->filename
, hdr
.ar_name
, namelen
);
317 ared
->filename
[namelen
] = '\0';
324 get_elt_at_filepos (archive
, filepos
)
328 struct areltdata
*new_areldata
;
331 n_nfd
= look_for_bfd_in_cache (archive
, filepos
);
332 if (n_nfd
) return n_nfd
;
334 if (0 > bfd_seek (archive
, filepos
, SEEK_SET
)) {
335 bfd_error
= system_call_error
;
339 if ((new_areldata
= snarf_ar_hdr (archive
)) == NULL
) return NULL
;
341 n_nfd
= _bfd_create_empty_archive_element_shell (archive
);
343 bfd_release (archive
, (PTR
)new_areldata
);
346 n_nfd
->origin
= bfd_tell (archive
);
347 n_nfd
->arelt_data
= (PTR
) new_areldata
;
348 n_nfd
->filename
= new_areldata
->filename
;
350 if (add_bfd_to_cache (archive
, filepos
, n_nfd
))
354 bfd_release (archive
, (PTR
)n_nfd
);
355 bfd_release (archive
, (PTR
)new_areldata
);
364 Return the sub bfd contained within the archive at archive index n.
367 bfd *bfd_get_elt_at_index(bfd *, int);
371 bfd_get_elt_at_index (abfd
, index
)
377 (abfd
, (bfd_ardata (abfd
)->symdefs
+ index
)->file_offset
);
383 bfd_openr_next_archived_file
386 Initially provided a BFD containing an archive and NULL, opens
387 a BFD on the first contained element and returns that.
388 Subsequent calls to bfd_openr_next_archived_file should pass
389 the archive and the previous return value to return a created
390 BFD to the next contained element. NULL is returned when there
394 bfd* bfd_openr_next_archived_file(bfd *archive, bfd *previous);
399 DEFUN(bfd_openr_next_archived_file
,(archive
, last_file
),
404 if ((bfd_get_format (archive
) != bfd_archive
) ||
405 (archive
->direction
== write_direction
)) {
406 bfd_error
= invalid_operation
;
411 return BFD_SEND (archive
,
412 openr_next_archived_file
,
418 bfd
*bfd_generic_openr_next_archived_file(archive
, last_file
)
425 filestart
= bfd_ardata (archive
)->first_file_filepos
;
427 unsigned int size
= arelt_size(last_file
);
428 /* Pad to an even boundary... */
429 filestart
= last_file
->origin
+ size
+ size
%2;
432 return get_elt_at_filepos (archive
, filestart
);
437 bfd_generic_archive_p (abfd
)
440 char armag
[SARMAG
+1];
442 if (bfd_read ((PTR
)armag
, 1, SARMAG
, abfd
) != SARMAG
) {
443 bfd_error
= wrong_format
;
448 if (strncmp (armag
, BFD_GNU960_ARMAG(abfd
), SARMAG
)) return 0;
450 if (strncmp (armag
, ARMAG
, SARMAG
)) return 0;
453 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
454 involves a cast, we can't do it as the left operand of assignment. */
455 set_tdata (abfd
, bfd_zalloc(abfd
,sizeof (struct artdata
)));
457 if (bfd_ardata (abfd
) == NULL
) {
458 bfd_error
= no_memory
;
462 bfd_ardata (abfd
)->first_file_filepos
= SARMAG
;
464 if (!BFD_SEND (abfd
, _bfd_slurp_armap
, (abfd
))) {
465 bfd_release(abfd
, bfd_ardata (abfd
));
470 if (!BFD_SEND (abfd
, _bfd_slurp_extended_name_table
, (abfd
))) {
471 bfd_release(abfd
, bfd_ardata (abfd
));
479 /* Returns false on error, true otherwise */
481 bfd_slurp_bsd_armap (abfd
)
485 struct areltdata
*mapdata
;
487 unsigned int counter
= 0;
488 int *raw_armap
, *rbase
;
489 struct artdata
*ardata
= bfd_ardata (abfd
);
492 /* FIXME, if the read fails, this routine quietly returns "true"!!
493 It should probably do that if the read gives 0 bytes (empty archive),
494 but fail for any other size... */
495 if (bfd_read ((PTR
)nextname
, 1, 16, abfd
) == 16) {
496 /* The archive has at least 16 bytes in it */
497 bfd_seek (abfd
, -16L, SEEK_CUR
);
499 /* This should be using RANLIBMAG, but at least it can be grepped for
501 if (strncmp (nextname
, "__.SYMDEF ", 16)) {
502 bfd_has_map (abfd
) = false;
506 mapdata
= snarf_ar_hdr (abfd
);
507 if (mapdata
== NULL
) return false;
509 raw_armap
= (int *) bfd_zalloc(abfd
,mapdata
->parsed_size
);
510 if (raw_armap
== NULL
) {
511 bfd_error
= no_memory
;
513 bfd_release (abfd
, (PTR
)mapdata
);
517 if (bfd_read ((PTR
)raw_armap
, 1, mapdata
->parsed_size
, abfd
) !=
518 mapdata
->parsed_size
) {
519 bfd_error
= malformed_archive
;
520 bfd_release (abfd
, (PTR
)raw_armap
);
524 ardata
->symdef_count
= bfd_h_get_32(abfd
, (PTR
)raw_armap
) / sizeof (struct symdef
);
527 ardata
->symdefs
= (carsym
*) rbase
;
528 stringbase
= ((char *) (ardata
->symdefs
+ ardata
->symdef_count
)) + 4;
530 for (;counter
< ardata
->symdef_count
; counter
++) {
531 struct symdef
*sym
= ((struct symdef
*) rbase
) + counter
;
532 sym
->s
.name
= bfd_h_get_32(abfd
, (PTR
)(&(sym
->s
.string_offset
))) + stringbase
;
533 sym
->file_offset
= bfd_h_get_32(abfd
, (PTR
)( &(sym
->file_offset
)));
536 ardata
->first_file_filepos
= bfd_tell (abfd
);
537 /* Pad to an even boundary if you have to */
538 ardata
->first_file_filepos
+= (ardata
-> first_file_filepos
) %2;
539 /* FIXME, we should provide some way to free raw_ardata when
540 we are done using the strings from it. For now, it seems
541 to be allocated on an obstack anyway... */
542 bfd_has_map (abfd
) = true;
547 /* Returns false on error, true otherwise */
549 bfd_slurp_coff_armap (abfd
)
552 struct areltdata
*mapdata
;
554 int *raw_armap
, *rawptr
;
555 struct artdata
*ardata
= bfd_ardata (abfd
);
557 unsigned int stringsize
;
561 result
= bfd_read ((PTR
)&nextname
, 1, 1, abfd
);
562 bfd_seek (abfd
, -1L, SEEK_CUR
);
564 if (result
!= 1 || nextname
!= '/') {
565 /* Actually I think this is an error for a COFF archive */
566 bfd_has_map (abfd
) = false;
570 mapdata
= snarf_ar_hdr (abfd
);
571 if (mapdata
== NULL
) return false;
573 raw_armap
= (int *) bfd_alloc(abfd
,mapdata
->parsed_size
);
575 if (raw_armap
== NULL
)
577 bfd_error
= no_memory
;
579 bfd_release (abfd
, (PTR
)mapdata
);
583 /* read in the raw map */
584 if (bfd_read ((PTR
)raw_armap
, 1, mapdata
->parsed_size
, abfd
) !=
585 mapdata
->parsed_size
) {
586 bfd_error
= malformed_archive
;
588 bfd_release (abfd
, (PTR
)raw_armap
);
592 /* The coff armap must be read sequentially. So we construct a bsd-style
593 one in core all at once, for simplicity.
595 It seems that all numeric information in a coff archive is always
596 in big endian format, nomatter the host or target. */
598 stringsize
= mapdata
->parsed_size
- (4 * (_do_getb32((PTR
)raw_armap
))) - 4;
601 unsigned int nsymz
= _do_getb32( (PTR
)raw_armap
);
602 unsigned int carsym_size
= (nsymz
* sizeof (carsym
));
603 unsigned int ptrsize
= (4 * nsymz
);
605 ardata
->symdefs
= (carsym
*) bfd_zalloc(abfd
,carsym_size
+ stringsize
+ 1);
606 if (ardata
->symdefs
== NULL
) {
607 bfd_error
= no_memory
;
610 carsyms
= ardata
->symdefs
;
612 stringbase
= ((char *) ardata
->symdefs
) + carsym_size
;
613 memcpy (stringbase
, (char*)raw_armap
+ ptrsize
+ 4, stringsize
);
616 /* OK, build the carsyms */
617 for (i
= 0; i
< nsymz
; i
++)
619 rawptr
= raw_armap
+ i
+ 1;
620 carsyms
->file_offset
= _do_getb32((PTR
)rawptr
);
621 carsyms
->name
= stringbase
;
622 for (; *(stringbase
++););
627 ardata
->symdef_count
= _do_getb32((PTR
)raw_armap
);
628 ardata
->first_file_filepos
= bfd_tell (abfd
);
629 /* Pad to an even boundary if you have to */
630 ardata
->first_file_filepos
+= (ardata
->first_file_filepos
) %2;
632 /* We'd like to release these allocations, but we have allocated stuff
633 since then (using the same obstack, if bfd_release is obstack based).
634 So they will stick around until the BFD is closed. */
635 /* bfd_release (abfd, (PTR)raw_armap);
636 bfd_release (abfd, (PTR)mapdata); */
637 bfd_has_map (abfd
) = true;
641 /** Extended name table.
643 Normally archives support only 14-character filenames.
645 Intel has extended the format: longer names are stored in a special
646 element (the first in the archive, or second if there is an armap);
647 the name in the ar_hdr is replaced by <space><index into filename
648 element>. Index is the P.R. of an int (radix: 8). Data General have
649 extended the format by using the prefix // for the special element */
651 /* Returns false on error, true otherwise */
653 _bfd_slurp_extended_name_table (abfd
)
657 struct areltdata
*namedata
;
659 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
660 we probably don't want to return true. */
661 if (bfd_read ((PTR
)nextname
, 1, 16, abfd
) == 16) {
663 bfd_seek (abfd
, -16L, SEEK_CUR
);
665 if (strncmp (nextname
, "ARFILENAMES/ ", 16) != 0 &&
666 strncmp (nextname
, "// ", 16) != 0)
668 bfd_ardata (abfd
)->extended_names
= NULL
;
672 namedata
= snarf_ar_hdr (abfd
);
673 if (namedata
== NULL
) return false;
675 bfd_ardata (abfd
)->extended_names
= bfd_zalloc(abfd
,namedata
->parsed_size
);
676 if (bfd_ardata (abfd
)->extended_names
== NULL
) {
677 bfd_error
= no_memory
;
679 bfd_release (abfd
, (PTR
)namedata
);
683 if (bfd_read ((PTR
)bfd_ardata (abfd
)->extended_names
, 1,
684 namedata
->parsed_size
, abfd
) != namedata
->parsed_size
) {
685 bfd_error
= malformed_archive
;
686 bfd_release (abfd
, (PTR
)(bfd_ardata (abfd
)->extended_names
));
687 bfd_ardata (abfd
)->extended_names
= NULL
;
691 /* Since the archive is supposed to be printable if it contains
692 text, the entries in the list are newline-padded, not null
693 padded. We'll fix that there.. */
695 char *temp
= bfd_ardata (abfd
)->extended_names
;
696 for (; *temp
!= '\0'; ++temp
)
697 if (*temp
== '\n') *temp
= '\0';
700 /* Pad to an even boundary if you have to */
701 bfd_ardata (abfd
)->first_file_filepos
= bfd_tell (abfd
);
702 bfd_ardata (abfd
)->first_file_filepos
+=
703 (bfd_ardata (abfd
)->first_file_filepos
) %2;
705 /* FIXME, we can't release namedata here because it was allocated
706 below extended_names on the obstack... */
707 /* bfd_release (abfd, namedata); */
713 char *normalize(file
)
716 char * filename
= strrchr(file
, '/');
717 if (filename
!= (char *)NULL
) {
726 /* Follows archive_head and produces an extended name table if necessary.
727 Returns (in tabloc) a pointer to an extended name table, and in tablen
728 the length of the table. If it makes an entry it clobbers the filename
729 so that the element may be written without further massage.
730 Returns true if it ran successfully, false if something went wrong.
731 A successful return may still involve a zero-length tablen!
734 bfd_construct_extended_name_table (abfd
, tabloc
, tablen
)
737 unsigned int *tablen
;
739 unsigned int maxname
= abfd
->xvec
->ar_max_namelen
;
740 unsigned int total_namelen
= 0;
746 /* Figure out how long the table should be */
747 for (current
= abfd
->archive_head
; current
!= NULL
; current
= current
->next
){
748 unsigned int thislen
= strlen (normalize(current
->filename
));
749 if (thislen
> maxname
) total_namelen
+= thislen
+ 1; /* leave room for \n */
752 if (total_namelen
== 0) return true;
754 *tabloc
= bfd_zalloc (abfd
,total_namelen
);
755 if (*tabloc
== NULL
) {
756 bfd_error
= no_memory
;
760 *tablen
= total_namelen
;
763 for (current
= abfd
->archive_head
; current
!= NULL
; current
=
765 char *normal
=normalize( current
->filename
);
766 unsigned int thislen
= strlen (normal
);
767 if (thislen
> maxname
) {
768 /* Works for now; may need to be re-engineered if we encounter an oddball
769 archive format and want to generalise this hack. */
770 struct ar_hdr
*hdr
= arch_hdr(current
);
771 strcpy (strptr
, normal
);
772 strptr
[thislen
] = '\n';
773 hdr
->ar_name
[0] = ' ';
774 /* We know there will always be enough room (one of the few cases
775 where you may safely use sprintf). */
776 sprintf ((hdr
->ar_name
) + 1, "%-o", (unsigned) (strptr
- *tabloc
));
777 /* Kinda Kludgy. We should just use the returned value of sprintf
778 but not all implementations get this right */
780 char *temp
= hdr
->ar_name
+2;
781 for (; temp
< hdr
->ar_name
+ maxname
; temp
++)
782 if (*temp
== '\0') *temp
= ' ';
784 strptr
+= thislen
+ 1;
791 /** A couple of functions for creating ar_hdrs */
793 /* Takes a filename, returns an arelt_data for it, or NULL if it can't make one.
794 The filename must refer to a filename in the filesystem.
795 The filename field of the ar_hdr will NOT be initialized
799 DEFUN(bfd_ar_hdr_from_filesystem
, (abfd
,filename
),
801 CONST
char *filename
)
804 struct areltdata
*ared
;
809 if (stat (filename
, &status
) != 0) {
810 bfd_error
= system_call_error
;
814 ared
= (struct areltdata
*) bfd_zalloc(abfd
, sizeof (struct ar_hdr
) +
815 sizeof (struct areltdata
));
817 bfd_error
= no_memory
;
820 hdr
= (struct ar_hdr
*) (((char *) ared
) + sizeof (struct areltdata
));
822 /* ar headers are space padded, not null padded! */
824 temp1
= temp
+ sizeof (struct ar_hdr
) - 2;
825 for (; temp
< temp1
; *(temp
++) = ' ');
826 strncpy (hdr
->ar_fmag
, ARFMAG
, 2);
828 /* Goddamned sprintf doesn't permit MAXIMUM field lengths */
829 sprintf ((hdr
->ar_date
), "%-12ld", status
.st_mtime
);
830 sprintf ((hdr
->ar_uid
), "%d", status
.st_uid
);
831 sprintf ((hdr
->ar_gid
), "%d", status
.st_gid
);
832 sprintf ((hdr
->ar_mode
), "%-8o", (unsigned) status
.st_mode
);
833 sprintf ((hdr
->ar_size
), "%-10ld", status
.st_size
);
834 /* Correct for a lossage in sprintf whereby it null-terminates. I cannot
835 understand how these C losers could design such a ramshackle bunch of
838 temp1
= temp
+ sizeof (struct ar_hdr
) - 2;
839 for (; temp
< temp1
; temp
++) {
840 if (*temp
== '\0') *temp
= ' ';
842 strncpy (hdr
->ar_fmag
, ARFMAG
, 2);
843 ared
->parsed_size
= status
.st_size
;
844 ared
->arch_header
= (char *) hdr
;
850 DEFUN(bfd_special_undocumented_glue
, (abfd
, filename
),
854 struct areltdata
*ar_elt
= bfd_ar_hdr_from_filesystem (abfd
, filename
);
857 return (struct ar_hdr
*) ar_elt
->arch_header
;
861 /* Analogous to stat call */
863 bfd_generic_stat_arch_elt (abfd
, buf
)
870 if (abfd
->arelt_data
== NULL
) {
871 bfd_error
= invalid_operation
;
875 hdr
= arch_hdr (abfd
);
877 #define foo(arelt, stelt, size) \
878 buf->stelt = strtol (hdr->arelt, &aloser, size); \
879 if (aloser == hdr->arelt) return -1;
881 foo (ar_date
, st_mtime
, 10);
882 foo (ar_uid
, st_uid
, 10);
883 foo (ar_gid
, st_gid
, 10);
884 foo (ar_mode
, st_mode
, 8);
885 foo (ar_size
, st_size
, 10);
891 bfd_dont_truncate_arname (abfd
, pathname
, arhdr
)
893 CONST
char *pathname
;
896 /* FIXME: This interacts unpleasantly with ar's quick-append option.
897 Fortunately ic960 users will never use that option. Fixing this
898 is very hard; fortunately I know how to do it and will do so once
899 intel's release is out the door. */
901 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
903 CONST
char *filename
= strrchr (pathname
, '/');
904 int maxlen
= ar_maxnamelen (abfd
);
906 if (filename
== NULL
)
911 length
= strlen (filename
);
913 if (length
<= maxlen
)
914 memcpy (hdr
->ar_name
, filename
, length
);
916 if (length
< maxlen
) (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
922 bfd_bsd_truncate_arname (abfd
, pathname
, arhdr
)
924 CONST
char *pathname
;
927 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
929 CONST
char *filename
= strrchr (pathname
, '/');
930 int maxlen
= ar_maxnamelen (abfd
);
933 if (filename
== NULL
)
938 length
= strlen (filename
);
940 if (length
<= maxlen
)
941 memcpy (hdr
->ar_name
, filename
, length
);
943 /* pathname: meet procrustes */
944 memcpy (hdr
->ar_name
, filename
, maxlen
);
948 if (length
< maxlen
) (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
951 /* Store name into ar header. Truncates the name to fit.
952 1> strip pathname to be just the basename.
953 2> if it's short enuf to fit, stuff it in.
954 3> If it doesn't end with .o, truncate it to fit
955 4> truncate it before the .o, append .o, stuff THAT in.
958 /* This is what gnu ar does. It's better but incompatible with the bsd ar. */
960 bfd_gnu_truncate_arname (abfd
, pathname
, arhdr
)
962 CONST
char *pathname
;
965 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
967 CONST
char *filename
= strrchr (pathname
, '/');
968 int maxlen
= ar_maxnamelen (abfd
);
970 if (filename
== NULL
)
975 length
= strlen (filename
);
977 if (length
<= maxlen
)
978 memcpy (hdr
->ar_name
, filename
, length
);
979 else { /* pathname: meet procrustes */
980 memcpy (hdr
->ar_name
, filename
, maxlen
);
981 if ((filename
[length
- 2] == '.') && (filename
[length
- 1] == 'o')) {
982 hdr
->ar_name
[maxlen
- 2] = '.';
983 hdr
->ar_name
[maxlen
- 1] = 'o';
988 if (length
< 16) (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
992 PROTO (boolean
, compute_and_write_armap
, (bfd
*arch
, unsigned int elength
));
994 /* The BFD is open for write and has its format set to bfd_archive */
996 _bfd_write_archive_contents (arch
)
1000 char *etable
= NULL
;
1001 unsigned int elength
= 0;
1002 boolean makemap
= bfd_has_map (arch
);
1003 boolean hasobjects
= false; /* if no .o's, don't bother to make a map */
1006 /* Verify the viability of all entries; if any of them live in the
1007 filesystem (as opposed to living in an archive open for input)
1008 then construct a fresh ar_hdr for them.
1010 for (current
= arch
->archive_head
; current
; current
= current
->next
) {
1011 if (bfd_write_p (current
)) {
1012 bfd_error
= invalid_operation
;
1015 if (!current
->arelt_data
) {
1016 current
->arelt_data
=
1017 (PTR
) bfd_ar_hdr_from_filesystem (arch
, current
->filename
);
1018 if (!current
->arelt_data
) return false;
1020 /* Put in the file name */
1022 BFD_SEND (arch
, _bfd_truncate_arname
,(arch
,
1024 (char *) arch_hdr(current
)));
1029 if (makemap
) { /* don't bother if we won't make a map! */
1030 if ((bfd_check_format (current
, bfd_object
))
1031 #if 0 /* FIXME -- these are not set correctly */
1032 && ((bfd_get_file_flags (current
) & HAS_SYMS
))
1039 if (!bfd_construct_extended_name_table (arch
, &etable
, &elength
))
1042 bfd_seek (arch
, 0, SEEK_SET
);
1044 bfd_write (BFD_GNU960_ARMAG(arch
), 1, SARMAG
, arch
);
1046 bfd_write (ARMAG
, 1, SARMAG
, arch
);
1049 if (makemap
&& hasobjects
) {
1051 if (compute_and_write_armap (arch
, elength
) != true) {
1059 memset ((char *)(&hdr
), 0, sizeof (struct ar_hdr
));
1060 sprintf (&(hdr
.ar_name
[0]), "ARFILENAMES/");
1061 sprintf (&(hdr
.ar_size
[0]), "%-10d", (int) elength
);
1062 hdr
.ar_fmag
[0] = '`'; hdr
.ar_fmag
[1] = '\n';
1063 for (i
= 0; i
< sizeof (struct ar_hdr
); i
++)
1064 if (((char *)(&hdr
))[i
] == '\0') (((char *)(&hdr
))[i
]) = ' ';
1065 bfd_write ((char *)&hdr
, 1, sizeof (struct ar_hdr
), arch
);
1066 bfd_write (etable
, 1, elength
, arch
);
1067 if ((elength
% 2) == 1) bfd_write ("\n", 1, 1, arch
);
1071 for (current
= arch
->archive_head
; current
; current
= current
->next
) {
1072 char buffer
[DEFAULT_BUFFERSIZE
];
1073 unsigned int remaining
= arelt_size (current
);
1074 struct ar_hdr
*hdr
= arch_hdr(current
);
1075 /* write ar header */
1077 if (bfd_write ((char *)hdr
, 1, sizeof(*hdr
), arch
) != sizeof(*hdr
)) {
1079 bfd_error
= system_call_error
;
1082 if (bfd_seek (current
, 0L, SEEK_SET
) != 0L) goto syserr
;
1085 unsigned int amt
= DEFAULT_BUFFERSIZE
;
1086 if (amt
> remaining
) {
1089 if (bfd_read (buffer
, amt
, 1, current
) != amt
) goto syserr
;
1090 if (bfd_write (buffer
, amt
, 1, arch
) != amt
) goto syserr
;
1093 if ((arelt_size (current
) % 2) == 1) bfd_write ("\n", 1, 1, arch
);
1098 /* Note that the namidx for the first symbol is 0 */
1101 compute_and_write_armap (arch
, elength
)
1103 unsigned int elength
;
1106 file_ptr elt_no
= 0;
1108 int orl_max
= 15000; /* fine initial default */
1110 int stridx
= 0; /* string index */
1112 /* Dunno if this is the best place for this info... */
1113 if (elength
!= 0) elength
+= sizeof (struct ar_hdr
);
1114 elength
+= elength
%2 ;
1116 map
= (struct orl
*) bfd_zalloc (arch
,orl_max
* sizeof (struct orl
));
1118 bfd_error
= no_memory
;
1122 /* Drop all the files called __.SYMDEF, we're going to make our
1124 while (arch
->archive_head
&&
1125 strcmp(arch
->archive_head
->filename
,"__.SYMDEF") == 0)
1127 arch
->archive_head
= arch
->archive_head
->next
;
1129 /* Map over each element */
1130 for (current
= arch
->archive_head
;
1131 current
!= (bfd
*)NULL
;
1132 current
= current
->next
, elt_no
++)
1134 if ((bfd_check_format (current
, bfd_object
) == true)
1135 && ((bfd_get_file_flags (current
) & HAS_SYMS
))) {
1137 unsigned int storage
;
1138 unsigned int symcount
;
1139 unsigned int src_count
;
1141 storage
= get_symtab_upper_bound (current
);
1144 syms
= (asymbol
**) bfd_zalloc (arch
,storage
);
1146 bfd_error
= no_memory
; /* FIXME -- memory leak */
1149 symcount
= bfd_canonicalize_symtab (current
, syms
);
1152 /* Now map over all the symbols, picking out the ones we want */
1153 for (src_count
= 0; src_count
<symcount
; src_count
++) {
1154 flagword flags
= (syms
[src_count
])->flags
;
1155 if ((flags
& BSF_GLOBAL
) ||
1156 (flags
& BSF_FORT_COMM
)) {
1158 /* This symbol will go into the archive header */
1159 if (orl_count
== orl_max
)
1162 map
= (struct orl
*) bfd_realloc (arch
, (char *) map
,
1163 orl_max
* sizeof (struct orl
));
1166 (map
[orl_count
]).name
= (char **) &((syms
[src_count
])->name
);
1167 (map
[orl_count
]).pos
= (file_ptr
) current
;
1168 (map
[orl_count
]).namidx
= stridx
;
1170 stridx
+= strlen ((syms
[src_count
])->name
) + 1;
1177 /* OK, now we have collected all the data, let's write them out */
1178 if (!BFD_SEND (arch
, write_armap
,
1179 (arch
, elength
, map
, orl_count
, stridx
))) {
1189 bsd_write_armap (arch
, elength
, map
, orl_count
, stridx
)
1191 unsigned int elength
;
1193 unsigned int orl_count
;
1196 unsigned int ranlibsize
= orl_count
* sizeof (struct ranlib
);
1197 unsigned int stringsize
= stridx
+ 4;
1198 unsigned int mapsize
= stringsize
+ ranlibsize
+ 4;
1200 bfd
*current
= arch
->archive_head
;
1201 bfd
*last_elt
= current
; /* last element arch seen */
1205 struct stat statbuf
;
1207 int padit
= mapsize
& 1;
1209 if (padit
) mapsize
++;
1211 firstreal
= mapsize
+ elength
+ sizeof (struct ar_hdr
) + SARMAG
;
1213 stat (arch
->filename
, &statbuf
);
1214 memset ((char *)(&hdr
), 0, sizeof (struct ar_hdr
));
1215 sprintf (hdr
.ar_name
, RANLIBMAG
);
1216 sprintf (hdr
.ar_date
, "%ld", statbuf
.st_mtime
);
1217 sprintf (hdr
.ar_uid
, "%d", getuid());
1218 sprintf (hdr
.ar_gid
, "%d", getgid());
1219 sprintf (hdr
.ar_size
, "%-10d", (int) mapsize
);
1220 hdr
.ar_fmag
[0] = '`'; hdr
.ar_fmag
[1] = '\n';
1221 for (i
= 0; i
< sizeof (struct ar_hdr
); i
++)
1222 if (((char *)(&hdr
))[i
] == '\0') (((char *)(&hdr
))[i
]) = ' ';
1223 bfd_write ((char *)&hdr
, 1, sizeof (struct ar_hdr
), arch
);
1224 bfd_h_put_32(arch
, ranlibsize
, (PTR
)&temp
);
1225 bfd_write (&temp
, 1, sizeof (temp
), arch
);
1227 for (count
= 0; count
< orl_count
; count
++) {
1229 struct symdef
*outp
= &outs
;
1231 if (((bfd
*)(map
[count
]).pos
) != last_elt
) {
1233 firstreal
+= arelt_size (current
) + sizeof (struct ar_hdr
);
1234 firstreal
+= firstreal
% 2;
1235 current
= current
->next
;
1236 } while (current
!= (bfd
*)(map
[count
]).pos
);
1237 } /* if new archive element */
1240 bfd_h_put_32(arch
, ((map
[count
]).namidx
),(PTR
) &outs
.s
.string_offset
);
1241 bfd_h_put_32(arch
, firstreal
,(PTR
) &outs
.file_offset
);
1242 bfd_write ((char *)outp
, 1, sizeof (outs
), arch
);
1245 /* now write the strings themselves */
1246 bfd_h_put_32(arch
, stridx
, (PTR
)&temp
);
1247 bfd_write ((PTR
)&temp
, 1, sizeof (temp
), arch
);
1248 for (count
= 0; count
< orl_count
; count
++)
1249 bfd_write (*((map
[count
]).name
), 1, strlen (*((map
[count
]).name
))+1, arch
);
1251 /* The spec sez this should be a newline. But in order to be
1252 bug-compatible for sun's ar we use a null. */
1254 bfd_write("\0",1,1,arch
);
1260 /* A coff armap looks like :
1262 struct ar_hdr with name = '/'
1264 offset of file for symbol 0
1265 offset of file for symbol 1
1267 offset of file for symbol n-1
1276 coff_write_armap (arch
, elength
, map
, symbol_count
, stridx
)
1278 unsigned int elength
;
1280 unsigned int symbol_count
;
1283 /* The size of the ranlib is the number of exported symbols in the
1284 archive * the number of bytes in a int, + an int for the count */
1286 unsigned int ranlibsize
= (symbol_count
* 4) + 4;
1287 unsigned int stringsize
= stridx
;
1288 unsigned int mapsize
= stringsize
+ ranlibsize
;
1289 file_ptr archive_member_file_ptr
;
1290 bfd
*current
= arch
->archive_head
;
1291 bfd
*last_elt
= current
; /* last element arch seen */
1295 int padit
= mapsize
& 1;
1297 if (padit
) mapsize
++;
1299 /* work out where the first object file will go in the archive */
1300 archive_member_file_ptr
= mapsize
+ elength
+ sizeof (struct ar_hdr
) + SARMAG
;
1302 memset ((char *)(&hdr
), 0, sizeof (struct ar_hdr
));
1303 hdr
.ar_name
[0] = '/';
1304 sprintf (hdr
.ar_size
, "%-10d", (int) mapsize
);
1305 sprintf (hdr
.ar_date
, "%ld", (long)time (NULL
));
1306 /* This, at least, is what Intel coff sets the values to.: */
1307 sprintf ((hdr
.ar_uid
), "%d", 0);
1308 sprintf ((hdr
.ar_gid
), "%d", 0);
1309 sprintf ((hdr
.ar_mode
), "%-7o",(unsigned ) 0);
1310 hdr
.ar_fmag
[0] = '`'; hdr
.ar_fmag
[1] = '\n';
1312 for (i
= 0; i
< sizeof (struct ar_hdr
); i
++)
1313 if (((char *)(&hdr
))[i
] == '\0') (((char *)(&hdr
))[i
]) = ' ';
1315 /* Write the ar header for this item and the number of symbols */
1318 bfd_write ((PTR
)&hdr
, 1, sizeof (struct ar_hdr
), arch
);
1320 bfd_write_bigendian_4byte_int(arch
, symbol_count
);
1322 /* Two passes, first write the file offsets for each symbol -
1323 remembering that each offset is on a two byte boundary
1326 /* Write out the file offset for the file associated with each
1327 symbol, and remember to keep the offsets padded out */
1329 current
= arch
->archive_head
;
1331 while (current
!= (bfd
*)NULL
&& count
< symbol_count
) {
1332 /* For each symbol which is used defined in this object, write out
1333 the object file's address in the archive */
1335 while (((bfd
*)(map
[count
]).pos
) == current
) {
1336 bfd_write_bigendian_4byte_int(arch
, archive_member_file_ptr
);
1339 /* Add size of this archive entry */
1340 archive_member_file_ptr
+= arelt_size (current
) + sizeof (struct
1342 /* remember aboout the even alignment */
1343 archive_member_file_ptr
+= archive_member_file_ptr
% 2;
1344 current
= current
->next
;
1349 /* now write the strings themselves */
1350 for (count
= 0; count
< symbol_count
; count
++) {
1351 bfd_write ((PTR
)*((map
[count
]).name
),
1353 strlen (*((map
[count
]).name
))+1, arch
);
1356 /* The spec sez this should be a newline. But in order to be
1357 bug-compatible for arc960 we use a null. */
1359 bfd_write("\0",1,1,arch
);