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 Archives are supported in BFD in <<archive.c>>.
29 An archive (or library) is just another BFD. It has a symbol
30 table, although there's not much a user program will do with it.
32 The big difference between an archive BFD and an ordinary BFD
33 is that the archive doesn't have sections. Instead it has a
34 chain of BFDs considered its contents. These BFDs can be
35 manipulated just like any other. The BFDs contained in an
36 archive opened for reading will all be opened for reading; you
37 may put either input or output BFDs into an archive opened for
38 output; it will be handled correctly when the archive is closed.
40 Use <<bfd_openr_next_archived_file>> to step through all
41 the contents of an archive opened for input. It's not
42 required that you read the entire archive if you don't want
43 to! Read it until you find what you want.
45 Archive contents of output BFDs are chained through the
46 <<next>> pointer in a BFD. The first one is findable through
47 the <<archive_head>> slot of the archive. Set it with
48 <<set_archive_head>> (q.v.). A given BFD may be in only one
49 open output archive at a time.
51 As expected, the BFD archive code is more general than the
52 archive code of any given environment. BFD archives may
53 contain files of different formats (eg a.out and coff) and
54 even different architectures. You may even place archives
55 recursively into archives!
57 This can cause unexpected confusion, since some archive
58 formats are more expressive than others. For instance intel
59 COFF archives can preserve long filenames; Sun a.out archives
60 cannot. If you move a file from the first to the second
61 format and back again, the filename may be truncated.
62 Likewise, different a.out environments have different
63 conventions as to how they truncate filenames, whether they
64 preserve directory names in filenames, etc. When
65 interoperating with native tools, be sure your files are
68 Beware: most of these formats do not react well to the
69 presence of spaces in filenames. We do the best we can, but
70 can't always handle this due to restrctions in the format of
71 archives. Many unix utilities are braindead in regards to
72 spaces and such in filenames anyway, so this shouldn't be much
77 o - all archive elements start on an even boundary, newline padded;
78 o - all arch headers are char *;
79 o - all arch headers are the same size (across architectures).
82 /* Some formats provide a way to cram a long filename into the short
83 (16 chars) space provided by a bsd archive. The trick is: make a
84 special "file" in the front of the archive, sort of like the SYMDEF
85 entry. If the filename is too long to fit, put it in the extended
86 name table, and use its index as the filename. To prevent
87 confusion prepend the index with a space. This means you can't
88 have filenames that start with a space, but then again, many unix
89 utilities can't handle that anyway.
91 This scheme unfortunately requires that you stand on your head in
92 order to write an archive since you need to put a magic file at the
93 front, and need to touch every entry to do so. C'est la vie.
100 #include "aout/ranlib.h"
108 #define BFD_GNU960_ARMAG(abfd) (BFD_COFF_FILE_P((abfd)) ? ARMAG : ARMAGB)
111 /* We keep a cache of archive filepointers to archive elements to
112 speed up searching the archive by filepos. We only add an entry to
113 the cache when we actually read one. We also don't sort the cache;
114 it's generally short enough to search linearly.
115 Note that the pointers here point to the front of the ar_hdr, not
116 to the front of the contents!
121 struct ar_cache
*next
;
124 #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
125 #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
127 #define arch_hdr(bfd) ((struct ar_hdr *) \
128 (((struct areltdata *)((bfd)->arelt_data))->arch_header))
131 _bfd_generic_mkarchive (abfd
)
134 abfd
->tdata
.aout_ar_data
= (struct artdata
*)bfd_zalloc(abfd
, sizeof (struct artdata
));
136 if (bfd_ardata (abfd
) == NULL
) {
137 bfd_error
= no_memory
;
140 bfd_ardata(abfd
)->cache
= 0;
149 symindex bfd_get_next_mapent(bfd *, symindex previous, carsym ** sym);
152 This function steps through an archive's symbol table (if it
153 has one). Successively updates <<sym>> with the next symbol's
154 information, returning that symbol's (internal) index into the
157 Supply BFD_NO_MORE_SYMBOLS as the <<previous>> entry to get
158 the first one; returns BFD_NO_MORE_SYMBOLS when you're already
161 A <<carsym>> is a canonical archive symbol. The only
162 user-visible element is its name, a null-terminated string.
166 DEFUN(bfd_get_next_mapent
,(abfd
, prev
, entry
),
171 if (!bfd_has_map (abfd
)) {
172 bfd_error
= invalid_operation
;
173 return BFD_NO_MORE_SYMBOLS
;
176 if (prev
== BFD_NO_MORE_SYMBOLS
) prev
= 0;
177 else if (++prev
>= bfd_ardata (abfd
)->symdef_count
)
178 return BFD_NO_MORE_SYMBOLS
;
180 *entry
= (bfd_ardata (abfd
)->symdefs
+ prev
);
184 /* To be called by backends only */
186 _bfd_create_empty_archive_element_shell (obfd
)
191 nbfd
= new_bfd_contained_in(obfd
);
193 bfd_error
= no_memory
;
204 boolean bfd_set_archive_head(bfd *output, bfd *new_head);
207 Used whilst processing archives. Sets the head of the chain of
208 BFDs contained in an archive to @var{new_head}.
212 DEFUN(bfd_set_archive_head
,(output_archive
, new_head
),
213 bfd
*output_archive AND
217 output_archive
->archive_head
= new_head
;
222 look_for_bfd_in_cache (arch_bfd
, filepos
)
226 struct ar_cache
*current
;
228 for (current
= bfd_ardata (arch_bfd
)->cache
; current
!= NULL
;
229 current
= current
->next
)
230 if (current
->ptr
== filepos
) return current
->arelt
;
235 /* Kind of stupid to call cons for each one, but we don't do too many */
237 add_bfd_to_cache (arch_bfd
, filepos
, new_elt
)
238 bfd
*arch_bfd
, *new_elt
;
241 struct ar_cache
*new_cache
= (struct ar_cache
*)
242 bfd_zalloc(arch_bfd
, sizeof (struct ar_cache
));
244 if (new_cache
== NULL
) {
245 bfd_error
= no_memory
;
249 new_cache
->ptr
= filepos
;
250 new_cache
->arelt
= new_elt
;
251 new_cache
->next
= (struct ar_cache
*)NULL
;
252 if (bfd_ardata (arch_bfd
)->cache
== NULL
)
253 bfd_ardata (arch_bfd
)->cache
= new_cache
;
255 struct ar_cache
*current
= bfd_ardata (arch_bfd
)->cache
;
257 for (; current
->next
!= NULL
; current
= current
->next
);
258 current
->next
= new_cache
;
266 /* The name begins with space. Hence the rest of the name is an index into
269 get_extended_arelt_filename (arch
, name
)
273 unsigned long index
= 0;
275 /* Should extract string so that I can guarantee not to overflow into
276 the next region, but I"m too lazy. */
278 index
= strtol (name
, NULL
, 10);
280 bfd_error
= malformed_archive
;
284 return bfd_ardata (arch
)->extended_names
+ index
;
287 /* This functions reads an arch header and returns an areltdata pointer, or
290 Presumes the file pointer is already in the right place (ie pointing
291 to the ar_hdr in the file). Moves the file pointer; on success it
292 should be pointing to the front of the file contents; on failure it
293 could have been moved arbitrarily.
305 char *hdrp
= (char *) &hdr
;
306 unsigned int parsed_size
;
307 struct areltdata
*ared
;
308 char *filename
= NULL
;
309 unsigned int namelen
= 0;
310 unsigned int allocsize
= sizeof (struct areltdata
) + sizeof (struct ar_hdr
);
313 if (bfd_read ((PTR
)hdrp
, 1, sizeof (struct ar_hdr
), abfd
)
314 != sizeof (struct ar_hdr
)) {
315 bfd_error
= no_more_archived_files
;
318 if (strncmp ((hdr
.ar_fmag
), ARFMAG
, 2)) {
319 bfd_error
= malformed_archive
;
324 parsed_size
= strtol (hdr
.ar_size
, NULL
, 10);
326 bfd_error
= malformed_archive
;
330 /* extract the filename from the archive - there are two ways to
331 specify an extendend name table, either the first char of the
332 name is a space, or it's a slash */
333 if ((hdr
.ar_name
[0] == '/' || hdr
.ar_name
[0] == ' ')
334 && bfd_ardata (abfd
)->extended_names
!= NULL
) {
335 filename
= get_extended_arelt_filename (abfd
, hdr
.ar_name
);
336 if (filename
== NULL
) {
337 bfd_error
= malformed_archive
;
343 /* We judge the end of the name by looking for a space or a
348 while (namelen
< (unsigned)ar_maxnamelen(abfd
) &&
349 ( hdr
.ar_name
[namelen
] != 0 &&
350 hdr
.ar_name
[namelen
] != ' ' &&
351 hdr
.ar_name
[namelen
] != ar_padchar(abfd
))) {
355 allocsize
+= namelen
+ 1;
358 allocptr
= bfd_zalloc(abfd
, allocsize
);
359 if (allocptr
== NULL
) {
360 bfd_error
= no_memory
;
364 ared
= (struct areltdata
*) allocptr
;
366 ared
->arch_header
= allocptr
+ sizeof (struct areltdata
);
367 memcpy ((char *) ared
->arch_header
, (char *) &hdr
, sizeof (struct ar_hdr
));
368 ared
->parsed_size
= parsed_size
;
370 if (filename
!= NULL
) ared
->filename
= filename
;
372 ared
->filename
= allocptr
+ (sizeof (struct areltdata
) +
373 sizeof (struct ar_hdr
));
375 memcpy (ared
->filename
, hdr
.ar_name
, namelen
);
376 ared
->filename
[namelen
] = '\0';
382 /* This is an internal function; it's mainly used when indexing
383 through the archive symbol table, but also used to get the next
384 element, since it handles the bookkeeping so nicely for us.
388 get_elt_at_filepos (archive
, filepos
)
392 struct areltdata
*new_areldata
;
395 n_nfd
= look_for_bfd_in_cache (archive
, filepos
);
396 if (n_nfd
) return n_nfd
;
398 if (0 > bfd_seek (archive
, filepos
, SEEK_SET
)) {
399 bfd_error
= system_call_error
;
403 if ((new_areldata
= snarf_ar_hdr (archive
)) == NULL
) return NULL
;
405 n_nfd
= _bfd_create_empty_archive_element_shell (archive
);
407 bfd_release (archive
, (PTR
)new_areldata
);
410 n_nfd
->origin
= bfd_tell (archive
);
411 n_nfd
->arelt_data
= (PTR
) new_areldata
;
412 n_nfd
->filename
= new_areldata
->filename
;
414 if (add_bfd_to_cache (archive
, filepos
, n_nfd
))
418 bfd_release (archive
, (PTR
)n_nfd
);
419 bfd_release (archive
, (PTR
)new_areldata
);
428 bfd *bfd_get_elt_at_index(bfd * archive, int index);
431 Return the bfd which is referenced by the symbol indexed by <<index>>.
432 <<index>> should have been returned by <<bfd_get_next_mapent>> (q.v.).
436 DEFUN(bfd_get_elt_at_index
,(abfd
, index
),
442 (abfd
, (bfd_ardata (abfd
)->symdefs
+ index
)->file_offset
);
448 bfd_openr_next_archived_file
451 bfd* bfd_openr_next_archived_file(bfd *archive, bfd *previous);
454 Initially provided a BFD containing an archive and NULL, opens
455 an inpout BFD on the first contained element and returns that.
456 Subsequent calls to bfd_openr_next_archived_file should pass
457 the archive and the previous return value to return a created
458 BFD to the next contained element. NULL is returned when there
464 DEFUN(bfd_openr_next_archived_file
,(archive
, last_file
),
469 if ((bfd_get_format (archive
) != bfd_archive
) ||
470 (archive
->direction
== write_direction
)) {
471 bfd_error
= invalid_operation
;
476 return BFD_SEND (archive
,
477 openr_next_archived_file
,
483 bfd
*bfd_generic_openr_next_archived_file(archive
, last_file
)
490 filestart
= bfd_ardata (archive
)->first_file_filepos
;
492 unsigned int size
= arelt_size(last_file
);
493 /* Pad to an even boundary... */
494 filestart
= last_file
->origin
+ size
+ size
%2;
497 return get_elt_at_filepos (archive
, filestart
);
502 bfd_generic_archive_p (abfd
)
505 char armag
[SARMAG
+1];
507 if (bfd_read ((PTR
)armag
, 1, SARMAG
, abfd
) != SARMAG
) {
508 bfd_error
= wrong_format
;
513 if (strncmp (armag
, BFD_GNU960_ARMAG(abfd
), SARMAG
)) return 0;
515 if (strncmp (armag
, ARMAG
, SARMAG
) &&
516 strncmp (armag
, ARMAGB
, SARMAG
)) return 0;
521 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
522 involves a cast, we can't do it as the left operand of assignment. */
523 abfd
->tdata
.aout_ar_data
= (struct artdata
*) bfd_zalloc(abfd
,sizeof (struct artdata
));
525 if (bfd_ardata (abfd
) == NULL
) {
526 bfd_error
= no_memory
;
530 bfd_ardata (abfd
)->first_file_filepos
= SARMAG
;
532 if (!BFD_SEND (abfd
, _bfd_slurp_armap
, (abfd
))) {
533 bfd_release(abfd
, bfd_ardata (abfd
));
534 abfd
->tdata
.aout_ar_data
= NULL
;
538 if (!BFD_SEND (abfd
, _bfd_slurp_extended_name_table
, (abfd
))) {
539 bfd_release(abfd
, bfd_ardata (abfd
));
540 abfd
->tdata
.aout_ar_data
= NULL
;
547 /* Returns false on error, true otherwise */
549 bfd_slurp_bsd_armap (abfd
)
553 struct areltdata
*mapdata
;
555 unsigned int counter
= 0;
556 int *raw_armap
, *rbase
;
557 struct artdata
*ardata
= bfd_ardata (abfd
);
560 /* FIXME, if the read fails, this routine quietly returns "true"!!
561 It should probably do that if the read gives 0 bytes (empty archive),
562 but fail for any other size... */
563 if (bfd_read ((PTR
)nextname
, 1, 16, abfd
) == 16) {
564 /* The archive has at least 16 bytes in it */
565 bfd_seek (abfd
, -16L, SEEK_CUR
);
567 /* This should be using RANLIBMAG, but at least it can be grepped for
569 if (strncmp (nextname
, "__.SYMDEF ", 16)) {
570 bfd_has_map (abfd
) = false;
574 mapdata
= snarf_ar_hdr (abfd
);
575 if (mapdata
== NULL
) return false;
577 raw_armap
= (int *) bfd_zalloc(abfd
,mapdata
->parsed_size
);
578 if (raw_armap
== NULL
) {
579 bfd_error
= no_memory
;
581 bfd_release (abfd
, (PTR
)mapdata
);
585 if (bfd_read ((PTR
)raw_armap
, 1, mapdata
->parsed_size
, abfd
) !=
586 mapdata
->parsed_size
) {
587 bfd_error
= malformed_archive
;
589 bfd_release (abfd
, (PTR
)raw_armap
);
593 ardata
->symdef_count
= bfd_h_get_32(abfd
, (PTR
)raw_armap
) / sizeof (struct symdef
);
595 if (ardata
->symdef_count
* sizeof (struct symdef
)
596 > mapdata
->parsed_size
- sizeof (*raw_armap
)) {
597 /* Probably we're using the wrong byte ordering. */
598 bfd_error
= wrong_format
;
604 ardata
->symdefs
= (carsym
*) rbase
;
605 stringbase
= ((char *) (ardata
->symdefs
+ ardata
->symdef_count
)) + 4;
607 for (;counter
< ardata
->symdef_count
; counter
++) {
608 struct symdef
*sym
= ((struct symdef
*) rbase
) + counter
;
609 sym
->s
.name
= bfd_h_get_32(abfd
, (PTR
)(&(sym
->s
.string_offset
))) + stringbase
;
610 sym
->file_offset
= bfd_h_get_32(abfd
, (PTR
)( &(sym
->file_offset
)));
613 ardata
->first_file_filepos
= bfd_tell (abfd
);
614 /* Pad to an even boundary if you have to */
615 ardata
->first_file_filepos
+= (ardata
-> first_file_filepos
) %2;
616 /* FIXME, we should provide some way to free raw_ardata when
617 we are done using the strings from it. For now, it seems
618 to be allocated on an obstack anyway... */
619 bfd_has_map (abfd
) = true;
624 /* Returns false on error, true otherwise */
626 bfd_slurp_coff_armap (abfd
)
629 struct areltdata
*mapdata
;
631 int *raw_armap
, *rawptr
;
632 struct artdata
*ardata
= bfd_ardata (abfd
);
634 unsigned int stringsize
;
639 result
= bfd_read ((PTR
)&nextname
, 1, 1, abfd
);
640 bfd_seek (abfd
, -1L, SEEK_CUR
);
642 if (result
!= 1 || nextname
!= '/') {
643 /* Actually I think this is an error for a COFF archive */
644 bfd_has_map (abfd
) = false;
648 mapdata
= snarf_ar_hdr (abfd
);
649 if (mapdata
== NULL
) return false;
651 raw_armap
= (int *) bfd_alloc(abfd
,mapdata
->parsed_size
);
653 if (raw_armap
== NULL
)
655 bfd_error
= no_memory
;
657 bfd_release (abfd
, (PTR
)mapdata
);
661 /* read in the raw map */
662 if (bfd_read ((PTR
)raw_armap
, 1, mapdata
->parsed_size
, abfd
) !=
663 mapdata
->parsed_size
) {
664 bfd_error
= malformed_archive
;
666 bfd_release (abfd
, (PTR
)raw_armap
);
670 /* The coff armap must be read sequentially. So we construct a bsd-style
671 one in core all at once, for simplicity.
673 It seems that all numeric information in a coff archive is always
674 in big endian format, nomatter the host or target. */
677 = mapdata
->parsed_size
- (4 * (_do_getb32((PTR
)raw_armap
))) - 4;
678 /* Except that some archive formats are broken, and it may be our
679 fault - the i960 little endian coff sometimes has big and sometimes
680 little, because our tools changed. Here's a horrible hack to clean
685 if (stringsize
> 0xfffff)
687 /* This looks dangerous, let's do it the other way around */
688 stringsize
= mapdata
->parsed_size
- (4 *
689 (_do_getl32((PTR
)raw_armap
))) - 4;
696 unsigned int nsymz
= swap( (PTR
)raw_armap
);
697 unsigned int carsym_size
= (nsymz
* sizeof (carsym
));
698 unsigned int ptrsize
= (4 * nsymz
);
700 ardata
->symdefs
= (carsym
*) bfd_zalloc(abfd
,carsym_size
+ stringsize
+ 1);
701 if (ardata
->symdefs
== NULL
) {
702 bfd_error
= no_memory
;
705 carsyms
= ardata
->symdefs
;
707 stringbase
= ((char *) ardata
->symdefs
) + carsym_size
;
708 memcpy (stringbase
, (char*)raw_armap
+ ptrsize
+ 4, stringsize
);
711 /* OK, build the carsyms */
712 for (i
= 0; i
< nsymz
; i
++)
714 rawptr
= raw_armap
+ i
+ 1;
715 carsyms
->file_offset
= swap((PTR
)rawptr
);
716 carsyms
->name
= stringbase
;
717 for (; *(stringbase
++););
722 ardata
->symdef_count
= swap((PTR
)raw_armap
);
723 ardata
->first_file_filepos
= bfd_tell (abfd
);
724 /* Pad to an even boundary if you have to */
725 ardata
->first_file_filepos
+= (ardata
->first_file_filepos
) %2;
727 /* We'd like to release these allocations, but we have allocated stuff
728 since then (using the same obstack, if bfd_release is obstack based).
729 So they will stick around until the BFD is closed. */
730 /* bfd_release (abfd, (PTR)raw_armap);
731 bfd_release (abfd, (PTR)mapdata); */
732 bfd_has_map (abfd
) = true;
736 /** Extended name table.
738 Normally archives support only 14-character filenames.
740 Intel has extended the format: longer names are stored in a special
741 element (the first in the archive, or second if there is an armap);
742 the name in the ar_hdr is replaced by <space><index into filename
743 element>. Index is the P.R. of an int (decimal). Data General have
744 extended the format by using the prefix // for the special element */
746 /* Returns false on error, true otherwise */
748 _bfd_slurp_extended_name_table (abfd
)
752 struct areltdata
*namedata
;
754 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
755 we probably don't want to return true. */
756 if (bfd_read ((PTR
)nextname
, 1, 16, abfd
) == 16) {
758 bfd_seek (abfd
, -16L, SEEK_CUR
);
760 if (strncmp (nextname
, "ARFILENAMES/ ", 16) != 0 &&
761 strncmp (nextname
, "// ", 16) != 0)
763 bfd_ardata (abfd
)->extended_names
= NULL
;
767 namedata
= snarf_ar_hdr (abfd
);
768 if (namedata
== NULL
) return false;
770 bfd_ardata (abfd
)->extended_names
= bfd_zalloc(abfd
,namedata
->parsed_size
);
771 if (bfd_ardata (abfd
)->extended_names
== NULL
) {
772 bfd_error
= no_memory
;
774 bfd_release (abfd
, (PTR
)namedata
);
778 if (bfd_read ((PTR
)bfd_ardata (abfd
)->extended_names
, 1,
779 namedata
->parsed_size
, abfd
) != namedata
->parsed_size
) {
780 bfd_error
= malformed_archive
;
781 bfd_release (abfd
, (PTR
)(bfd_ardata (abfd
)->extended_names
));
782 bfd_ardata (abfd
)->extended_names
= NULL
;
786 /* Since the archive is supposed to be printable if it contains
787 text, the entries in the list are newline-padded, not null
788 padded. We'll fix that there.. */
790 char *temp
= bfd_ardata (abfd
)->extended_names
;
791 for (; *temp
!= '\0'; ++temp
)
792 if (*temp
== '\n') *temp
= '\0';
795 /* Pad to an even boundary if you have to */
796 bfd_ardata (abfd
)->first_file_filepos
= bfd_tell (abfd
);
797 bfd_ardata (abfd
)->first_file_filepos
+=
798 (bfd_ardata (abfd
)->first_file_filepos
) %2;
800 /* FIXME, we can't release namedata here because it was allocated
801 below extended_names on the obstack... */
802 /* bfd_release (abfd, namedata); */
809 /* Return a copy of the stuff in the filename between any :]> and a
812 DEFUN(normalize
,(file
),
819 first
= file
+ strlen(file
)-1;
822 while (first
!= file
)
826 if (*first
== ':' || *first
== ']' ||*first
== '>')
835 copy
= malloc(last
- first
+ 1);
836 memcpy(copy
, first
, last
-first
);
837 copy
[last
-first
] = 0;
844 CONST
char *normalize(file
)
847 CONST
char * filename
= strrchr(file
, '/');
849 if (filename
!= (char *)NULL
) {
858 /* Follows archive_head and produces an extended name table if necessary.
859 Returns (in tabloc) a pointer to an extended name table, and in tablen
860 the length of the table. If it makes an entry it clobbers the filename
861 so that the element may be written without further massage.
862 Returns true if it ran successfully, false if something went wrong.
863 A successful return may still involve a zero-length tablen!
866 bfd_construct_extended_name_table (abfd
, tabloc
, tablen
)
869 unsigned int *tablen
;
871 unsigned int maxname
= abfd
->xvec
->ar_max_namelen
;
872 unsigned int total_namelen
= 0;
878 /* Figure out how long the table should be */
879 for (current
= abfd
->archive_head
; current
!= NULL
; current
= current
->next
){
880 unsigned int thislen
= strlen (normalize(current
->filename
));
881 if (thislen
> maxname
) total_namelen
+= thislen
+ 1; /* leave room for \n */
884 if (total_namelen
== 0) return true;
886 *tabloc
= bfd_zalloc (abfd
,total_namelen
);
887 if (*tabloc
== NULL
) {
888 bfd_error
= no_memory
;
892 *tablen
= total_namelen
;
895 for (current
= abfd
->archive_head
; current
!= NULL
; current
=
897 CONST
char *normal
=normalize( current
->filename
);
898 unsigned int thislen
= strlen (normal
);
899 if (thislen
> maxname
) {
900 /* Works for now; may need to be re-engineered if we encounter an oddball
901 archive format and want to generalise this hack. */
902 struct ar_hdr
*hdr
= arch_hdr(current
);
903 strcpy (strptr
, normal
);
904 strptr
[thislen
] = '\n';
905 hdr
->ar_name
[0] = ' ';
906 /* We know there will always be enough room (one of the few cases
907 where you may safely use sprintf). */
908 sprintf ((hdr
->ar_name
) + 1, "%-d", (unsigned) (strptr
- *tabloc
));
909 /* Kinda Kludgy. We should just use the returned value of sprintf
910 but not all implementations get this right */
912 char *temp
= hdr
->ar_name
+2;
913 for (; temp
< hdr
->ar_name
+ maxname
; temp
++)
914 if (*temp
== '\0') *temp
= ' ';
916 strptr
+= thislen
+ 1;
923 /** A couple of functions for creating ar_hdrs */
925 /* Takes a filename, returns an arelt_data for it, or NULL if it can't make one.
926 The filename must refer to a filename in the filesystem.
927 The filename field of the ar_hdr will NOT be initialized
931 DEFUN(bfd_ar_hdr_from_filesystem
, (abfd
,filename
),
933 CONST
char *filename
)
936 struct areltdata
*ared
;
941 if (stat (filename
, &status
) != 0) {
942 bfd_error
= system_call_error
;
946 ared
= (struct areltdata
*) bfd_zalloc(abfd
, sizeof (struct ar_hdr
) +
947 sizeof (struct areltdata
));
949 bfd_error
= no_memory
;
952 hdr
= (struct ar_hdr
*) (((char *) ared
) + sizeof (struct areltdata
));
954 /* ar headers are space padded, not null padded! */
956 temp1
= temp
+ sizeof (struct ar_hdr
) - 2;
957 for (; temp
< temp1
; *(temp
++) = ' ');
958 strncpy (hdr
->ar_fmag
, ARFMAG
, 2);
960 /* Goddamned sprintf doesn't permit MAXIMUM field lengths */
961 sprintf ((hdr
->ar_date
), "%-12ld", status
.st_mtime
);
962 sprintf ((hdr
->ar_uid
), "%d", status
.st_uid
);
963 sprintf ((hdr
->ar_gid
), "%d", status
.st_gid
);
964 sprintf ((hdr
->ar_mode
), "%-8o", (unsigned) status
.st_mode
);
965 sprintf ((hdr
->ar_size
), "%-10ld", status
.st_size
);
966 /* Correct for a lossage in sprintf whereby it null-terminates. I cannot
967 understand how these C losers could design such a ramshackle bunch of
970 temp1
= temp
+ sizeof (struct ar_hdr
) - 2;
971 for (; temp
< temp1
; temp
++) {
972 if (*temp
== '\0') *temp
= ' ';
974 strncpy (hdr
->ar_fmag
, ARFMAG
, 2);
975 ared
->parsed_size
= status
.st_size
;
976 ared
->arch_header
= (char *) hdr
;
981 /* This is magic required by the "ar" program. Since it's
982 undocumented, it's undocumented. You may think that it would
983 take a strong stomach to write this, and it does, but it takes
984 even a stronger stomach to try to code around such a thing!
988 DEFUN(bfd_special_undocumented_glue
, (abfd
, filename
),
992 struct areltdata
*ar_elt
= bfd_ar_hdr_from_filesystem (abfd
, filename
);
995 return (struct ar_hdr
*) ar_elt
->arch_header
;
999 /* Analogous to stat call */
1001 bfd_generic_stat_arch_elt (abfd
, buf
)
1008 if (abfd
->arelt_data
== NULL
) {
1009 bfd_error
= invalid_operation
;
1013 hdr
= arch_hdr (abfd
);
1015 #define foo(arelt, stelt, size) \
1016 buf->stelt = strtol (hdr->arelt, &aloser, size); \
1017 if (aloser == hdr->arelt) return -1;
1019 foo (ar_date
, st_mtime
, 10);
1020 foo (ar_uid
, st_uid
, 10);
1021 foo (ar_gid
, st_gid
, 10);
1022 foo (ar_mode
, st_mode
, 8);
1023 foo (ar_size
, st_size
, 10);
1029 bfd_dont_truncate_arname (abfd
, pathname
, arhdr
)
1031 CONST
char *pathname
;
1034 /* FIXME: This interacts unpleasantly with ar's quick-append option.
1035 Fortunately ic960 users will never use that option. Fixing this
1036 is very hard; fortunately I know how to do it and will do so once
1037 intel's release is out the door. */
1039 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
1041 CONST
char *filename
= normalize(pathname
);
1042 int maxlen
= ar_maxnamelen (abfd
);
1044 length
= strlen (filename
);
1046 if (length
<= maxlen
)
1047 memcpy (hdr
->ar_name
, filename
, length
);
1049 if (length
< maxlen
) (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
1055 bfd_bsd_truncate_arname (abfd
, pathname
, arhdr
)
1057 CONST
char *pathname
;
1060 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
1062 CONST
char *filename
= strrchr (pathname
, '/');
1063 int maxlen
= ar_maxnamelen (abfd
);
1066 if (filename
== NULL
)
1067 filename
= pathname
;
1071 length
= strlen (filename
);
1073 if (length
<= maxlen
)
1074 memcpy (hdr
->ar_name
, filename
, length
);
1076 /* pathname: meet procrustes */
1077 memcpy (hdr
->ar_name
, filename
, maxlen
);
1081 if (length
< maxlen
) (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
1084 /* Store name into ar header. Truncates the name to fit.
1085 1> strip pathname to be just the basename.
1086 2> if it's short enuf to fit, stuff it in.
1087 3> If it doesn't end with .o, truncate it to fit
1088 4> truncate it before the .o, append .o, stuff THAT in.
1091 /* This is what gnu ar does. It's better but incompatible with the bsd ar. */
1093 bfd_gnu_truncate_arname (abfd
, pathname
, arhdr
)
1095 CONST
char *pathname
;
1098 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
1100 CONST
char *filename
= strrchr (pathname
, '/');
1101 int maxlen
= ar_maxnamelen (abfd
);
1103 if (filename
== NULL
)
1104 filename
= pathname
;
1108 length
= strlen (filename
);
1110 if (length
<= maxlen
)
1111 memcpy (hdr
->ar_name
, filename
, length
);
1112 else { /* pathname: meet procrustes */
1113 memcpy (hdr
->ar_name
, filename
, maxlen
);
1114 if ((filename
[length
- 2] == '.') && (filename
[length
- 1] == 'o')) {
1115 hdr
->ar_name
[maxlen
- 2] = '.';
1116 hdr
->ar_name
[maxlen
- 1] = 'o';
1121 if (length
< 16) (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
1125 PROTO (boolean
, compute_and_write_armap
, (bfd
*arch
, unsigned int elength
));
1127 /* The BFD is open for write and has its format set to bfd_archive */
1129 _bfd_write_archive_contents (arch
)
1133 char *etable
= NULL
;
1134 unsigned int elength
= 0;
1135 boolean makemap
= bfd_has_map (arch
);
1136 boolean hasobjects
= false; /* if no .o's, don't bother to make a map */
1139 /* Verify the viability of all entries; if any of them live in the
1140 filesystem (as opposed to living in an archive open for input)
1141 then construct a fresh ar_hdr for them.
1143 for (current
= arch
->archive_head
; current
; current
= current
->next
) {
1144 if (bfd_write_p (current
)) {
1145 bfd_error
= invalid_operation
;
1148 if (!current
->arelt_data
) {
1149 current
->arelt_data
=
1150 (PTR
) bfd_ar_hdr_from_filesystem (arch
, current
->filename
);
1151 if (!current
->arelt_data
) return false;
1153 /* Put in the file name */
1155 BFD_SEND (arch
, _bfd_truncate_arname
,(arch
,
1157 (char *) arch_hdr(current
)));
1162 if (makemap
) { /* don't bother if we won't make a map! */
1163 if ((bfd_check_format (current
, bfd_object
))
1164 #if 0 /* FIXME -- these are not set correctly */
1165 && ((bfd_get_file_flags (current
) & HAS_SYMS
))
1172 if (!bfd_construct_extended_name_table (arch
, &etable
, &elength
))
1175 bfd_seek (arch
, 0, SEEK_SET
);
1177 bfd_write (BFD_GNU960_ARMAG(arch
), 1, SARMAG
, arch
);
1179 bfd_write (ARMAG
, 1, SARMAG
, arch
);
1182 if (makemap
&& hasobjects
) {
1184 if (compute_and_write_armap (arch
, elength
) != true) {
1192 memset ((char *)(&hdr
), 0, sizeof (struct ar_hdr
));
1193 sprintf (&(hdr
.ar_name
[0]), "ARFILENAMES/");
1194 sprintf (&(hdr
.ar_size
[0]), "%-10d", (int) elength
);
1195 hdr
.ar_fmag
[0] = '`'; hdr
.ar_fmag
[1] = '\n';
1196 for (i
= 0; i
< sizeof (struct ar_hdr
); i
++)
1197 if (((char *)(&hdr
))[i
] == '\0') (((char *)(&hdr
))[i
]) = ' ';
1198 bfd_write ((char *)&hdr
, 1, sizeof (struct ar_hdr
), arch
);
1199 bfd_write (etable
, 1, elength
, arch
);
1200 if ((elength
% 2) == 1) bfd_write ("\n", 1, 1, arch
);
1204 for (current
= arch
->archive_head
; current
; current
= current
->next
) {
1205 char buffer
[DEFAULT_BUFFERSIZE
];
1206 unsigned int remaining
= arelt_size (current
);
1207 struct ar_hdr
*hdr
= arch_hdr(current
);
1208 /* write ar header */
1210 if (bfd_write ((char *)hdr
, 1, sizeof(*hdr
), arch
) != sizeof(*hdr
)) {
1212 bfd_error
= system_call_error
;
1215 if (bfd_seek (current
, 0L, SEEK_SET
) != 0L) goto syserr
;
1218 unsigned int amt
= DEFAULT_BUFFERSIZE
;
1219 if (amt
> remaining
) {
1223 if (bfd_read (buffer
, amt
, 1, current
) != amt
) {
1224 if (errno
) goto syserr
;
1225 /* Looks like a truncated archive. */
1226 bfd_error
= malformed_archive
;
1229 if (bfd_write (buffer
, amt
, 1, arch
) != amt
) goto syserr
;
1232 if ((arelt_size (current
) % 2) == 1) bfd_write ("\n", 1, 1, arch
);
1237 /* Note that the namidx for the first symbol is 0 */
1240 compute_and_write_armap (arch
, elength
)
1242 unsigned int elength
;
1245 file_ptr elt_no
= 0;
1247 int orl_max
= 15000; /* fine initial default */
1249 int stridx
= 0; /* string index */
1251 /* Dunno if this is the best place for this info... */
1252 if (elength
!= 0) elength
+= sizeof (struct ar_hdr
);
1253 elength
+= elength
%2 ;
1255 map
= (struct orl
*) bfd_zalloc (arch
,orl_max
* sizeof (struct orl
));
1257 bfd_error
= no_memory
;
1261 /* Drop all the files called __.SYMDEF, we're going to make our
1263 while (arch
->archive_head
&&
1264 strcmp(arch
->archive_head
->filename
,"__.SYMDEF") == 0)
1266 arch
->archive_head
= arch
->archive_head
->next
;
1268 /* Map over each element */
1269 for (current
= arch
->archive_head
;
1270 current
!= (bfd
*)NULL
;
1271 current
= current
->next
, elt_no
++)
1273 if ((bfd_check_format (current
, bfd_object
) == true)
1274 && ((bfd_get_file_flags (current
) & HAS_SYMS
))) {
1276 unsigned int storage
;
1277 unsigned int symcount
;
1278 unsigned int src_count
;
1280 storage
= get_symtab_upper_bound (current
);
1283 syms
= (asymbol
**) bfd_zalloc (arch
,storage
);
1285 bfd_error
= no_memory
; /* FIXME -- memory leak */
1288 symcount
= bfd_canonicalize_symtab (current
, syms
);
1291 /* Now map over all the symbols, picking out the ones we want */
1292 for (src_count
= 0; src_count
<symcount
; src_count
++) {
1294 (syms
[src_count
])->flags
;
1296 syms
[src_count
]->section
;
1298 if ((flags
& BSF_GLOBAL
) ||
1299 (flags
& BSF_INDIRECT
) ||
1300 (sec
== &bfd_com_section
)) {
1302 /* This symbol will go into the archive header */
1303 if (orl_count
== orl_max
)
1306 map
= (struct orl
*) bfd_realloc (arch
, (char *) map
,
1307 orl_max
* sizeof (struct orl
));
1310 (map
[orl_count
]).name
= (char **) &((syms
[src_count
])->name
);
1311 (map
[orl_count
]).pos
= (file_ptr
) current
;
1312 (map
[orl_count
]).namidx
= stridx
;
1314 stridx
+= strlen ((syms
[src_count
])->name
) + 1;
1321 /* OK, now we have collected all the data, let's write them out */
1322 if (!BFD_SEND (arch
, write_armap
,
1323 (arch
, elength
, map
, orl_count
, stridx
))) {
1333 bsd_write_armap (arch
, elength
, map
, orl_count
, stridx
)
1335 unsigned int elength
;
1337 unsigned int orl_count
;
1340 int padit
= stridx
& 1;
1341 unsigned int ranlibsize
= orl_count
* sizeof (struct ranlib
);
1342 unsigned int stringsize
= stridx
+ padit
;
1343 /* Include 8 bytes to store ranlibsize and stringsize in output. */
1344 unsigned int mapsize
= ranlibsize
+ stringsize
+ 8;
1346 bfd
*current
= arch
->archive_head
;
1347 bfd
*last_elt
= current
; /* last element arch seen */
1351 struct stat statbuf
;
1354 firstreal
= mapsize
+ elength
+ sizeof (struct ar_hdr
) + SARMAG
;
1356 stat (arch
->filename
, &statbuf
);
1357 memset ((char *)(&hdr
), 0, sizeof (struct ar_hdr
));
1358 sprintf (hdr
.ar_name
, RANLIBMAG
);
1360 /* write the timestamp of the archive header to be just a little bit
1361 later than the timestamp of the file, otherwise the linker will
1362 complain that the index is out of date.
1365 sprintf (hdr
.ar_date
, "%ld", statbuf
.st_mtime
+ 60);
1366 sprintf (hdr
.ar_uid
, "%d", getuid());
1367 sprintf (hdr
.ar_gid
, "%d", getgid());
1368 sprintf (hdr
.ar_size
, "%-10d", (int) mapsize
);
1369 hdr
.ar_fmag
[0] = '`'; hdr
.ar_fmag
[1] = '\n';
1370 for (i
= 0; i
< sizeof (struct ar_hdr
); i
++)
1371 if (((char *)(&hdr
))[i
] == '\0') (((char *)(&hdr
))[i
]) = ' ';
1372 bfd_write ((char *)&hdr
, 1, sizeof (struct ar_hdr
), arch
);
1373 bfd_h_put_32(arch
, ranlibsize
, (PTR
)&temp
);
1374 bfd_write (&temp
, 1, sizeof (temp
), arch
);
1376 for (count
= 0; count
< orl_count
; count
++) {
1378 struct symdef
*outp
= &outs
;
1380 if (((bfd
*)(map
[count
]).pos
) != last_elt
) {
1382 firstreal
+= arelt_size (current
) + sizeof (struct ar_hdr
);
1383 firstreal
+= firstreal
% 2;
1384 current
= current
->next
;
1385 } while (current
!= (bfd
*)(map
[count
]).pos
);
1386 } /* if new archive element */
1389 bfd_h_put_32(arch
, ((map
[count
]).namidx
),(PTR
) &outs
.s
.string_offset
);
1390 bfd_h_put_32(arch
, firstreal
,(PTR
) &outs
.file_offset
);
1391 bfd_write ((char *)outp
, 1, sizeof (outs
), arch
);
1394 /* now write the strings themselves */
1395 bfd_h_put_32(arch
, stringsize
, (PTR
)&temp
);
1396 bfd_write ((PTR
)&temp
, 1, sizeof (temp
), arch
);
1397 for (count
= 0; count
< orl_count
; count
++)
1398 bfd_write (*((map
[count
]).name
), 1, strlen (*((map
[count
]).name
))+1, arch
);
1400 /* The spec sez this should be a newline. But in order to be
1401 bug-compatible for sun's ar we use a null. */
1403 bfd_write("\0",1,1,arch
);
1409 /* A coff armap looks like :
1411 struct ar_hdr with name = '/'
1413 offset of file for symbol 0
1414 offset of file for symbol 1
1416 offset of file for symbol n-1
1425 coff_write_armap (arch
, elength
, map
, symbol_count
, stridx
)
1427 unsigned int elength
;
1429 unsigned int symbol_count
;
1432 /* The size of the ranlib is the number of exported symbols in the
1433 archive * the number of bytes in a int, + an int for the count */
1435 unsigned int ranlibsize
= (symbol_count
* 4) + 4;
1436 unsigned int stringsize
= stridx
;
1437 unsigned int mapsize
= stringsize
+ ranlibsize
;
1438 file_ptr archive_member_file_ptr
;
1439 bfd
*current
= arch
->archive_head
;
1443 int padit
= mapsize
& 1;
1445 if (padit
) mapsize
++;
1447 /* work out where the first object file will go in the archive */
1448 archive_member_file_ptr
= mapsize
+ elength
+ sizeof (struct ar_hdr
) + SARMAG
;
1450 memset ((char *)(&hdr
), 0, sizeof (struct ar_hdr
));
1451 hdr
.ar_name
[0] = '/';
1452 sprintf (hdr
.ar_size
, "%-10d", (int) mapsize
);
1453 sprintf (hdr
.ar_date
, "%ld", (long)time (NULL
));
1454 /* This, at least, is what Intel coff sets the values to.: */
1455 sprintf ((hdr
.ar_uid
), "%d", 0);
1456 sprintf ((hdr
.ar_gid
), "%d", 0);
1457 sprintf ((hdr
.ar_mode
), "%-7o",(unsigned ) 0);
1458 hdr
.ar_fmag
[0] = '`'; hdr
.ar_fmag
[1] = '\n';
1460 for (i
= 0; i
< sizeof (struct ar_hdr
); i
++)
1461 if (((char *)(&hdr
))[i
] == '\0') (((char *)(&hdr
))[i
]) = ' ';
1463 /* Write the ar header for this item and the number of symbols */
1466 bfd_write ((PTR
)&hdr
, 1, sizeof (struct ar_hdr
), arch
);
1468 bfd_write_bigendian_4byte_int(arch
, symbol_count
);
1470 /* Two passes, first write the file offsets for each symbol -
1471 remembering that each offset is on a two byte boundary. */
1473 /* Write out the file offset for the file associated with each
1474 symbol, and remember to keep the offsets padded out. */
1476 current
= arch
->archive_head
;
1478 while (current
!= (bfd
*)NULL
&& count
< symbol_count
) {
1479 /* For each symbol which is used defined in this object, write out
1480 the object file's address in the archive */
1482 while (((bfd
*)(map
[count
]).pos
) == current
) {
1483 bfd_write_bigendian_4byte_int(arch
, archive_member_file_ptr
);
1486 /* Add size of this archive entry */
1487 archive_member_file_ptr
+= arelt_size (current
) + sizeof (struct
1489 /* remember aboout the even alignment */
1490 archive_member_file_ptr
+= archive_member_file_ptr
% 2;
1491 current
= current
->next
;
1496 /* now write the strings themselves */
1497 for (count
= 0; count
< symbol_count
; count
++) {
1498 bfd_write ((PTR
)*((map
[count
]).name
),
1500 strlen (*((map
[count
]).name
))+1, arch
);
1503 /* The spec sez this should be a newline. But in order to be
1504 bug-compatible for arc960 we use a null. */
1506 bfd_write("\0",1,1,arch
);