1 /* BFD back-end for archive files (libraries).
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 Written by Cygnus Support. Mostly Gumby Henkel-Wallace's fault.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
22 @setfilename archive-info
27 An archive (or library) is just another BFD. It has a symbol
28 table, although there's not much a user program will do with it.
30 The big difference between an archive BFD and an ordinary BFD
31 is that the archive doesn't have sections. Instead it has a
32 chain of BFDs that are considered its contents. These BFDs can
33 be manipulated like any other. The BFDs contained in an
34 archive opened for reading will all be opened for reading. You
35 may put either input or output BFDs into an archive opened for
36 output; they will be handled correctly when the archive is closed.
38 Use <<bfd_openr_next_archived_file>> to step through
39 the contents of an archive opened for input. You don't
40 have to read the entire archive if you don't want
41 to! Read it until you find what you want.
43 Archive contents of output BFDs are chained through the
44 <<next>> pointer in a BFD. The first one is findable through
45 the <<archive_head>> slot of the archive. Set it with
46 <<bfd_set_archive_head>> (q.v.). A given BFD may be in only one
47 open output archive at a time.
49 As expected, the BFD archive code is more general than the
50 archive code of any given environment. BFD archives may
51 contain files of different formats (e.g., a.out and coff) and
52 even different architectures. You may even place archives
53 recursively into archives!
55 This can cause unexpected confusion, since some archive
56 formats are more expressive than others. For instance, Intel
57 COFF archives can preserve long filenames; SunOS a.out archives
58 cannot. If you move a file from the first to the second
59 format and back again, the filename may be truncated.
60 Likewise, different a.out environments have different
61 conventions as to how they truncate filenames, whether they
62 preserve directory names in filenames, etc. When
63 interoperating with native tools, be sure your files are
66 Beware: most of these formats do not react well to the
67 presence of spaces in filenames. We do the best we can, but
68 can't always handle this case due to restrictions in the format of
69 archives. Many Unix utilities are braindead in regards to
70 spaces and such in filenames anyway, so this shouldn't be much
73 Archives are supported in BFD in <<archive.c>>.
78 o - all archive elements start on an even boundary, newline padded;
79 o - all arch headers are char *;
80 o - all arch headers are the same size (across architectures).
83 /* Some formats provide a way to cram a long filename into the short
84 (16 chars) space provided by a BSD archive. The trick is: make a
85 special "file" in the front of the archive, sort of like the SYMDEF
86 entry. If the filename is too long to fit, put it in the extended
87 name table, and use its index as the filename. To prevent
88 confusion prepend the index with a space. This means you can't
89 have filenames that start with a space, but then again, many Unix
90 utilities can't handle that anyway.
92 This scheme unfortunately requires that you stand on your head in
93 order to write an archive since you need to put a magic file at the
94 front, and need to touch every entry to do so. C'est la vie.
96 We support two variants of this idea:
97 The SVR4 format (extended name table is named "//"),
98 and an extended pseudo-BSD variant (extended name table is named
99 "ARFILENAMES/"). The origin of the latter format is uncertain.
101 BSD 4.4 uses a third scheme: It writes a long filename
102 directly after the header. This allows 'ar q' to work.
103 We currently can read BSD 4.4 archives, but not write them.
106 /* Summary of archive member names:
108 Symbol table (must be first):
109 "__.SYMDEF " - Symbol table, Berkeley style, produced by ranlib.
110 "/ " - Symbol table, system 5 style.
112 Long name table (must be before regular file members):
113 "// " - Long name table, System 5 R4 style.
114 "ARFILENAMES/ " - Long name table, non-standard extended BSD (not BSD 4.4).
116 Regular file members with short names:
117 "filename.o/ " - Regular file, System 5 style (embedded spaces ok).
118 "filename.o " - Regular file, Berkeley style (no embedded spaces).
120 Regular files with long names (or embedded spaces, for BSD variants):
121 "/18 " - SVR4 style, name at offset 18 in name table.
122 "#1/23 " - Long name (or embedded paces) 23 characters long,
123 BSD 4.4 style, full name follows header.
124 Implemented for reading, not writing.
125 " 18 " - Long name 18 characters long, extended pseudo-BSD.
132 #include "aout/ranlib.h"
134 #include <string.h> /* For memchr, strrchr and friends */
142 #define BFD_GNU960_ARMAG(abfd) (BFD_COFF_FILE_P((abfd)) ? ARMAG : ARMAGB)
145 /* Can't define this in hosts/foo.h, because (e.g. in gprof) the hosts file
146 is included, then obstack.h, which thinks if offsetof is defined, it
147 doesn't need to include stddef.h. */
148 /* Define offsetof for those systems which lack it */
150 #if !defined (offsetof)
151 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
154 /* We keep a cache of archive filepointers to archive elements to
155 speed up searching the archive by filepos. We only add an entry to
156 the cache when we actually read one. We also don't sort the cache;
157 it's generally short enough to search linearly.
158 Note that the pointers here point to the front of the ar_hdr, not
159 to the front of the contents!
164 struct ar_cache
*next
;
167 #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
168 #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
170 #define arch_eltdata(bfd) ((struct areltdata *)((bfd)->arelt_data))
171 #define arch_hdr(bfd) ((struct ar_hdr *)arch_eltdata(bfd)->arch_header)
173 static char *get_extended_arelt_filename
PARAMS ((bfd
*arch
,
175 static bfd
*get_elt_at_filepos
PARAMS ((bfd
*archive
, file_ptr filepos
));
176 static boolean do_slurp_bsd_armap
PARAMS ((bfd
*abfd
));
177 static boolean do_slurp_coff_armap
PARAMS ((bfd
*abfd
));
178 static const char *normalize
PARAMS ((const char *file
));
179 static boolean bfd_construct_extended_name_table
PARAMS ((bfd
*abfd
,
182 static struct areltdata
*bfd_ar_hdr_from_filesystem
PARAMS ((bfd
*abfd
,
184 static boolean compute_and_write_armap
PARAMS ((bfd
*arch
,
185 unsigned int elength
));
186 static boolean bsd_update_armap_timestamp
PARAMS ((bfd
*arch
));
189 _bfd_generic_mkarchive (abfd
)
192 abfd
->tdata
.aout_ar_data
= ((struct artdata
*)
193 bfd_zalloc (abfd
, sizeof (struct artdata
)));
195 if (bfd_ardata (abfd
) == NULL
)
197 bfd_error
= no_memory
;
201 bfd_ardata (abfd
)->cache
= NULL
;
202 bfd_ardata (abfd
)->archive_head
= NULL
;
203 bfd_ardata (abfd
)->symdefs
= NULL
;
204 bfd_ardata (abfd
)->extended_names
= NULL
;
205 bfd_ardata (abfd
)->tdata
= NULL
;
215 symindex bfd_get_next_mapent(bfd *abfd, symindex previous, carsym **sym);
218 Step through archive @var{abfd}'s symbol table (if it
219 has one). Successively update @var{sym} with the next symbol's
220 information, returning that symbol's (internal) index into the
223 Supply <<BFD_NO_MORE_SYMBOLS>> as the @var{previous} entry to get
224 the first one; returns <<BFD_NO_MORE_SYMBOLS>> when you've already
227 A <<carsym>> is a canonical archive symbol. The only
228 user-visible element is its name, a null-terminated string.
232 bfd_get_next_mapent (abfd
, prev
, entry
)
237 if (!bfd_has_map (abfd
)) {
238 bfd_error
= invalid_operation
;
239 return BFD_NO_MORE_SYMBOLS
;
242 if (prev
== BFD_NO_MORE_SYMBOLS
) prev
= 0;
243 else if (++prev
>= bfd_ardata (abfd
)->symdef_count
)
244 return BFD_NO_MORE_SYMBOLS
;
246 *entry
= (bfd_ardata (abfd
)->symdefs
+ prev
);
250 /* To be called by backends only */
253 _bfd_create_empty_archive_element_shell (obfd
)
258 nbfd
= new_bfd_contained_in(obfd
);
261 bfd_error
= no_memory
;
272 boolean bfd_set_archive_head(bfd *output, bfd *new_head);
275 Set the head of the chain of
276 BFDs contained in the archive @var{output} to @var{new_head}.
280 bfd_set_archive_head (output_archive
, new_head
)
285 output_archive
->archive_head
= new_head
;
290 look_for_bfd_in_cache (arch_bfd
, filepos
)
294 struct ar_cache
*current
;
296 for (current
= bfd_ardata (arch_bfd
)->cache
; current
!= NULL
;
297 current
= current
->next
)
298 if (current
->ptr
== filepos
) return current
->arelt
;
303 /* Kind of stupid to call cons for each one, but we don't do too many */
305 _bfd_add_bfd_to_archive_cache (arch_bfd
, filepos
, new_elt
)
306 bfd
*arch_bfd
, *new_elt
;
309 struct ar_cache
*new_cache
= (struct ar_cache
*)
310 bfd_zalloc(arch_bfd
, sizeof (struct ar_cache
));
312 if (new_cache
== NULL
) {
313 bfd_error
= no_memory
;
317 new_cache
->ptr
= filepos
;
318 new_cache
->arelt
= new_elt
;
319 new_cache
->next
= (struct ar_cache
*)NULL
;
320 if (bfd_ardata (arch_bfd
)->cache
== NULL
)
321 bfd_ardata (arch_bfd
)->cache
= new_cache
;
323 struct ar_cache
*current
= bfd_ardata (arch_bfd
)->cache
;
325 while (current
->next
!= NULL
)
326 current
= current
->next
;
327 current
->next
= new_cache
;
333 /* The name begins with space. Hence the rest of the name is an index into
337 get_extended_arelt_filename (arch
, name
)
341 unsigned long index
= 0;
343 /* Should extract string so that I can guarantee not to overflow into
344 the next region, but I'm too lazy. */
346 /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */
347 index
= strtol (name
+1, NULL
, 10);
349 bfd_error
= malformed_archive
;
353 return bfd_ardata (arch
)->extended_names
+ index
;
356 /* This functions reads an arch header and returns an areltdata pointer, or
359 Presumes the file pointer is already in the right place (ie pointing
360 to the ar_hdr in the file). Moves the file pointer; on success it
361 should be pointing to the front of the file contents; on failure it
362 could have been moved arbitrarily.
366 _bfd_snarf_ar_hdr (abfd
)
374 char *hdrp
= (char *) &hdr
;
375 unsigned int parsed_size
;
376 struct areltdata
*ared
;
377 char *filename
= NULL
;
378 unsigned int namelen
= 0;
379 unsigned int allocsize
= sizeof (struct areltdata
) + sizeof (struct ar_hdr
);
382 if (bfd_read ((PTR
)hdrp
, 1, sizeof (struct ar_hdr
), abfd
)
383 != sizeof (struct ar_hdr
)) {
384 bfd_error
= no_more_archived_files
;
387 if (strncmp ((hdr
.ar_fmag
), ARFMAG
, 2)) {
388 bfd_error
= malformed_archive
;
393 parsed_size
= strtol (hdr
.ar_size
, NULL
, 10);
395 bfd_error
= malformed_archive
;
399 /* extract the filename from the archive - there are two ways to
400 specify an extendend name table, either the first char of the
401 name is a space, or it's a slash. */
402 if ((hdr
.ar_name
[0] == '/'
403 || (hdr
.ar_name
[0] == ' '
404 && memchr (hdr
.ar_name
, '/', ar_maxnamelen(abfd
)) == NULL
))
405 && bfd_ardata (abfd
)->extended_names
!= NULL
) {
406 filename
= get_extended_arelt_filename (abfd
, hdr
.ar_name
);
407 if (filename
== NULL
) {
408 bfd_error
= malformed_archive
;
412 /* BSD4.4-style long filename.
413 Only implemented for reading, so far! */
414 else if (hdr
.ar_name
[0] == '#' && hdr
.ar_name
[1] == '1'
415 && hdr
.ar_name
[2] == '/' && isdigit(hdr
.ar_name
[3]))
417 /* BSD-4.4 extended name */
418 namelen
= atoi (&hdr
.ar_name
[3]);
419 allocsize
+= namelen
+ 1;
420 parsed_size
-= namelen
;
422 allocptr
= bfd_zalloc(abfd
, allocsize
);
423 if (allocptr
== NULL
) {
424 bfd_error
= no_memory
;
428 + (sizeof (struct areltdata
) + sizeof (struct ar_hdr
));
429 if (bfd_read (filename
, 1, namelen
, abfd
) != namelen
) {
430 bfd_error
= no_more_archived_files
;
433 filename
[namelen
] = '\0';
437 /* We judge the end of the name by looking for '/' or ' '.
438 Note: The SYSV format (terminated by '/') allows embedded
439 spaces, so only look for ' ' if we don't find '/'. */
442 while (hdr
.ar_name
[namelen
] != '\0' &&
443 hdr
.ar_name
[namelen
] != '/') {
445 if (namelen
== (unsigned)ar_maxnamelen(abfd
)) {
447 while (hdr
.ar_name
[namelen
] != ' '
448 && namelen
< (unsigned)ar_maxnamelen(abfd
)) {
455 allocsize
+= namelen
+ 1;
459 allocptr
= bfd_zalloc(abfd
, allocsize
);
460 if (allocptr
== NULL
) {
461 bfd_error
= no_memory
;
466 ared
= (struct areltdata
*) allocptr
;
468 ared
->arch_header
= allocptr
+ sizeof (struct areltdata
);
469 memcpy ((char *) ared
->arch_header
, (char *) &hdr
, sizeof (struct ar_hdr
));
470 ared
->parsed_size
= parsed_size
;
472 if (filename
!= NULL
) ared
->filename
= filename
;
474 ared
->filename
= allocptr
+ (sizeof (struct areltdata
) +
475 sizeof (struct ar_hdr
));
477 memcpy (ared
->filename
, hdr
.ar_name
, namelen
);
478 ared
->filename
[namelen
] = '\0';
484 /* This is an internal function; it's mainly used when indexing
485 through the archive symbol table, but also used to get the next
486 element, since it handles the bookkeeping so nicely for us.
490 get_elt_at_filepos (archive
, filepos
)
494 struct areltdata
*new_areldata
;
497 n_nfd
= look_for_bfd_in_cache (archive
, filepos
);
501 if (0 > bfd_seek (archive
, filepos
, SEEK_SET
))
503 bfd_error
= system_call_error
;
507 if ((new_areldata
= _bfd_snarf_ar_hdr (archive
)) == NULL
)
510 n_nfd
= _bfd_create_empty_archive_element_shell (archive
);
513 bfd_release (archive
, (PTR
)new_areldata
);
517 n_nfd
->origin
= bfd_tell (archive
);
518 n_nfd
->arelt_data
= (PTR
) new_areldata
;
519 n_nfd
->filename
= new_areldata
->filename
;
521 if (_bfd_add_bfd_to_archive_cache (archive
, filepos
, n_nfd
))
525 bfd_release (archive
, (PTR
)n_nfd
);
526 bfd_release (archive
, (PTR
)new_areldata
);
535 bfd *bfd_get_elt_at_index(bfd *archive, int index);
538 Return the BFD which is referenced by the symbol in @var{archive}
539 indexed by @var{index}. @var{index} should have been returned by
540 <<bfd_get_next_mapent>> (q.v.).
544 bfd_get_elt_at_index (abfd
, index
)
550 (abfd
, (bfd_ardata (abfd
)->symdefs
+ index
)->file_offset
);
556 bfd_openr_next_archived_file
559 bfd *bfd_openr_next_archived_file(bfd *archive, bfd *previous);
562 Provided a BFD, @var{archive}, containing an archive and NULL, open
563 an input BFD on the first contained element and returns that.
564 Subsequent calls should pass
565 the archive and the previous return value to return a created
566 BFD to the next contained element. NULL is returned when there
572 bfd_openr_next_archived_file (archive
, last_file
)
576 if ((bfd_get_format (archive
) != bfd_archive
) ||
577 (archive
->direction
== write_direction
))
579 bfd_error
= invalid_operation
;
583 return BFD_SEND (archive
,
584 openr_next_archived_file
,
590 bfd_generic_openr_next_archived_file (archive
, last_file
)
597 filestart
= bfd_ardata (archive
)->first_file_filepos
;
599 unsigned int size
= arelt_size(last_file
);
600 /* Pad to an even boundary...
601 Note that last_file->origin can be odd in the case of
602 BSD-4.4-style element with a long odd size. */
603 filestart
= last_file
->origin
+ size
;
604 filestart
+= filestart
% 2;
607 return get_elt_at_filepos (archive
, filestart
);
612 bfd_generic_archive_p (abfd
)
615 char armag
[SARMAG
+1];
617 if (bfd_read ((PTR
) armag
, 1, SARMAG
, abfd
) != SARMAG
)
619 bfd_error
= wrong_format
;
624 if (strncmp (armag
, BFD_GNU960_ARMAG(abfd
), SARMAG
) != 0)
627 if (strncmp (armag
, ARMAG
, SARMAG
) != 0 &&
628 strncmp (armag
, ARMAGB
, SARMAG
) != 0)
632 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
633 involves a cast, we can't do it as the left operand of assignment. */
634 abfd
->tdata
.aout_ar_data
= ((struct artdata
*)
635 bfd_zalloc (abfd
, sizeof (struct artdata
)));
637 if (bfd_ardata (abfd
) == NULL
)
639 bfd_error
= no_memory
;
643 bfd_ardata (abfd
)->first_file_filepos
= SARMAG
;
644 bfd_ardata (abfd
)->cache
= NULL
;
645 bfd_ardata (abfd
)->archive_head
= NULL
;
646 bfd_ardata (abfd
)->symdefs
= NULL
;
647 bfd_ardata (abfd
)->extended_names
= NULL
;
648 bfd_ardata (abfd
)->tdata
= NULL
;
650 if (! BFD_SEND (abfd
, _bfd_slurp_armap
, (abfd
)))
652 bfd_release(abfd
, bfd_ardata (abfd
));
653 abfd
->tdata
.aout_ar_data
= NULL
;
657 if (! BFD_SEND (abfd
, _bfd_slurp_extended_name_table
, (abfd
)))
659 bfd_release(abfd
, bfd_ardata (abfd
));
660 abfd
->tdata
.aout_ar_data
= NULL
;
667 /* Returns false on error, true otherwise */
669 do_slurp_bsd_armap (abfd
)
672 struct areltdata
*mapdata
;
673 unsigned int counter
= 0;
674 int *raw_armap
, *rbase
;
675 struct artdata
*ardata
= bfd_ardata (abfd
);
677 unsigned int parsed_size
;
679 mapdata
= _bfd_snarf_ar_hdr (abfd
);
682 parsed_size
= mapdata
->parsed_size
;
683 bfd_release (abfd
, (PTR
)mapdata
); /* Don't need it any more. */
685 raw_armap
= (int *) bfd_zalloc(abfd
, parsed_size
);
686 if (raw_armap
== NULL
) {
687 bfd_error
= no_memory
;
691 if (bfd_read ((PTR
)raw_armap
, 1, parsed_size
, abfd
) != parsed_size
) {
692 bfd_error
= malformed_archive
;
694 bfd_release (abfd
, (PTR
)raw_armap
);
698 ardata
->symdef_count
= (bfd_h_get_32 (abfd
, (bfd_byte
*) raw_armap
)
699 / sizeof (struct symdef
));
701 if (ardata
->symdef_count
* sizeof (struct symdef
)
702 > parsed_size
- sizeof (*raw_armap
)) {
703 /* Probably we're using the wrong byte ordering. */
704 bfd_error
= wrong_format
;
710 ardata
->symdefs
= (carsym
*) rbase
;
711 stringbase
= ((char *) (ardata
->symdefs
+ ardata
->symdef_count
)) + 4;
713 for (;counter
< ardata
->symdef_count
; counter
++) {
714 struct symdef
*sym
= ((struct symdef
*) rbase
) + counter
;
715 sym
->s
.name
= (bfd_h_get_32 (abfd
, (bfd_byte
*) (&sym
->s
.string_offset
))
717 sym
->file_offset
= bfd_h_get_32 (abfd
,
718 (bfd_byte
*) (&(sym
->file_offset
)));
721 ardata
->first_file_filepos
= bfd_tell (abfd
);
722 /* Pad to an even boundary if you have to */
723 ardata
->first_file_filepos
+= (ardata
-> first_file_filepos
) %2;
724 /* FIXME, we should provide some way to free raw_ardata when
725 we are done using the strings from it. For now, it seems
726 to be allocated on an obstack anyway... */
727 bfd_has_map (abfd
) = true;
731 /* Returns false on error, true otherwise */
733 do_slurp_coff_armap (abfd
)
736 struct areltdata
*mapdata
;
737 int *raw_armap
, *rawptr
;
738 struct artdata
*ardata
= bfd_ardata (abfd
);
740 unsigned int stringsize
;
741 unsigned int parsed_size
;
743 unsigned int nsymz
; /* Number of symbols in armap. */
745 bfd_vma (*swap
) PARAMS ((bfd_byte
*));
746 char int_buf
[sizeof(long)];
747 unsigned int carsym_size
, ptrsize
, i
;
749 mapdata
= _bfd_snarf_ar_hdr (abfd
);
752 parsed_size
= mapdata
->parsed_size
;
753 bfd_release (abfd
, (PTR
)mapdata
); /* Don't need it any more. */
755 if (bfd_read ((PTR
)int_buf
, 1, 4, abfd
) != 4) {
756 bfd_error
= malformed_archive
;
759 /* It seems that all numeric information in a coff archive is always
760 in big endian format, nomatter the host or target. */
762 nsymz
= bfd_getb32((PTR
)int_buf
);
763 stringsize
= parsed_size
- (4 * nsymz
) - 4;
766 /* ... except that some archive formats are broken, and it may be our
767 fault - the i960 little endian coff sometimes has big and sometimes
768 little, because our tools changed. Here's a horrible hack to clean
771 if (stringsize
> 0xfffff) {
772 /* This looks dangerous, let's do it the other way around */
773 nsymz
= bfd_getl32((PTR
)int_buf
);
774 stringsize
= parsed_size
- (4 * nsymz
) - 4;
779 /* The coff armap must be read sequentially. So we construct a bsd-style
780 one in core all at once, for simplicity. */
782 carsym_size
= (nsymz
* sizeof (carsym
));
783 ptrsize
= (4 * nsymz
);
785 ardata
->symdefs
= (carsym
*) bfd_zalloc(abfd
, carsym_size
+ stringsize
+ 1);
786 if (ardata
->symdefs
== NULL
) {
787 bfd_error
= no_memory
;
790 carsyms
= ardata
->symdefs
;
791 stringbase
= ((char *) ardata
->symdefs
) + carsym_size
;
793 /* Allocate and read in the raw offsets. */
794 raw_armap
= (int *) bfd_alloc(abfd
, ptrsize
);
795 if (raw_armap
== NULL
) {
796 bfd_error
= no_memory
;
797 goto release_symdefs
;
799 if (bfd_read ((PTR
)raw_armap
, 1, ptrsize
, abfd
) != ptrsize
800 || bfd_read ((PTR
)stringbase
, 1, stringsize
, abfd
) != stringsize
) {
801 bfd_error
= malformed_archive
;
802 goto release_raw_armap
;
805 /* OK, build the carsyms */
806 for (i
= 0; i
< nsymz
; i
++) {
807 rawptr
= raw_armap
+ i
;
808 carsyms
->file_offset
= swap((PTR
)rawptr
);
809 carsyms
->name
= stringbase
;
810 stringbase
+= strlen (stringbase
) + 1;
815 ardata
->symdef_count
= nsymz
;
816 ardata
->first_file_filepos
= bfd_tell (abfd
);
817 /* Pad to an even boundary if you have to */
818 ardata
->first_file_filepos
+= (ardata
->first_file_filepos
) %2;
820 bfd_has_map (abfd
) = true;
821 bfd_release (abfd
, (PTR
)raw_armap
);
825 bfd_release (abfd
, (PTR
)raw_armap
);
827 bfd_release (abfd
, (PTR
)(ardata
)->symdefs
);
831 /* This routine can handle either coff-style or bsd-style armaps.
832 Returns false on error, true otherwise */
835 bfd_slurp_armap (abfd
)
839 int i
= bfd_read ((PTR
)nextname
, 1, 16, abfd
);
846 bfd_seek (abfd
, (file_ptr
) -16, SEEK_CUR
);
848 if (!strncmp (nextname
, "__.SYMDEF ", 16))
849 return do_slurp_bsd_armap (abfd
);
850 else if (!strncmp (nextname
, "/ ", 16))
851 return do_slurp_coff_armap (abfd
);
853 bfd_has_map (abfd
) = false;
857 /* Returns false on error, true otherwise */
858 /* flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the
859 header is in a slightly different order and the map name is '/'.
860 This flavour is used by hp300hpux. */
862 bfd_slurp_bsd_armap_f2 (abfd
)
865 struct areltdata
*mapdata
;
867 unsigned int counter
= 0;
868 int *raw_armap
, *rbase
;
869 struct artdata
*ardata
= bfd_ardata (abfd
);
871 unsigned int stringsize
;
872 int i
= bfd_read ((PTR
)nextname
, 1, 16, abfd
);
879 /* The archive has at least 16 bytes in it */
880 bfd_seek (abfd
, -16L, SEEK_CUR
);
882 if (!strncmp (nextname
, "__.SYMDEF ", 16))
883 return do_slurp_bsd_armap (abfd
);
885 if (strncmp (nextname
, "/ ", 16))
887 bfd_has_map (abfd
) = false;
891 mapdata
= _bfd_snarf_ar_hdr (abfd
);
895 raw_armap
= (int *) bfd_zalloc(abfd
,mapdata
->parsed_size
);
896 if (raw_armap
== NULL
)
898 bfd_error
= no_memory
;
900 bfd_release (abfd
, (PTR
)mapdata
);
904 if (bfd_read ((PTR
)raw_armap
, 1, mapdata
->parsed_size
, abfd
) !=
905 mapdata
->parsed_size
)
907 bfd_error
= malformed_archive
;
909 bfd_release (abfd
, (PTR
)raw_armap
);
913 ardata
->symdef_count
= bfd_h_get_16(abfd
, (PTR
)raw_armap
);
915 if (ardata
->symdef_count
* sizeof (struct symdef
)
916 > mapdata
->parsed_size
- sizeof (*raw_armap
))
918 /* Probably we're using the wrong byte ordering. */
919 bfd_error
= wrong_format
;
925 stringsize
= bfd_h_get_32(abfd
, (PTR
)(((char*)raw_armap
)+2));
926 /* skip sym count and string sz */
927 rbase
= (int*)(((char*)raw_armap
) + 6);
928 stringbase
= (char *) rbase
;
929 ardata
->symdefs
= (carsym
*)(((char*) rbase
) + stringsize
);
931 for (;counter
< ardata
->symdef_count
; counter
++)
933 struct symdef
*sym
= ((struct symdef
*) ardata
->symdefs
) + counter
;
934 sym
->s
.name
= bfd_h_get_32(abfd
, (PTR
)(&(sym
->s
.string_offset
))) + stringbase
;
935 sym
->file_offset
= bfd_h_get_32(abfd
, (PTR
)( &(sym
->file_offset
)));
938 ardata
->first_file_filepos
= bfd_tell (abfd
);
939 /* Pad to an even boundary if you have to */
940 ardata
->first_file_filepos
+= (ardata
-> first_file_filepos
) %2;
941 /* FIXME, we should provide some way to free raw_ardata when
942 we are done using the strings from it. For now, it seems
943 to be allocated on an obstack anyway... */
944 bfd_has_map (abfd
) = true;
948 /** Extended name table.
950 Normally archives support only 14-character filenames.
952 Intel has extended the format: longer names are stored in a special
953 element (the first in the archive, or second if there is an armap);
954 the name in the ar_hdr is replaced by <space><index into filename
955 element>. Index is the P.R. of an int (decimal). Data General have
956 extended the format by using the prefix // for the special element */
958 /* Returns false on error, true otherwise */
960 _bfd_slurp_extended_name_table (abfd
)
964 struct areltdata
*namedata
;
966 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
967 we probably don't want to return true. */
968 if (bfd_read ((PTR
)nextname
, 1, 16, abfd
) == 16) {
970 bfd_seek (abfd
, (file_ptr
) -16, SEEK_CUR
);
972 if (strncmp (nextname
, "ARFILENAMES/ ", 16) != 0 &&
973 strncmp (nextname
, "// ", 16) != 0)
975 bfd_ardata (abfd
)->extended_names
= NULL
;
979 namedata
= _bfd_snarf_ar_hdr (abfd
);
980 if (namedata
== NULL
)
983 bfd_ardata (abfd
)->extended_names
= bfd_zalloc(abfd
,namedata
->parsed_size
);
984 if (bfd_ardata (abfd
)->extended_names
== NULL
) {
985 bfd_error
= no_memory
;
987 bfd_release (abfd
, (PTR
)namedata
);
991 if (bfd_read ((PTR
)bfd_ardata (abfd
)->extended_names
, 1,
992 namedata
->parsed_size
, abfd
) != namedata
->parsed_size
) {
993 bfd_error
= malformed_archive
;
994 bfd_release (abfd
, (PTR
)(bfd_ardata (abfd
)->extended_names
));
995 bfd_ardata (abfd
)->extended_names
= NULL
;
999 /* Since the archive is supposed to be printable if it contains
1000 text, the entries in the list are newline-padded, not null
1001 padded. In SVR4-style archives, the names also have a
1002 trailing '/'. We'll fix both problems here.. */
1004 char *temp
= bfd_ardata (abfd
)->extended_names
;
1005 char *limit
= temp
+ namedata
->parsed_size
;
1006 for (; temp
< limit
; ++temp
)
1008 temp
[temp
[-1] == '/' ? -1 : 0] = '\0';
1011 /* Pad to an even boundary if you have to */
1012 bfd_ardata (abfd
)->first_file_filepos
= bfd_tell (abfd
);
1013 bfd_ardata (abfd
)->first_file_filepos
+=
1014 (bfd_ardata (abfd
)->first_file_filepos
) %2;
1016 /* FIXME, we can't release namedata here because it was allocated
1017 below extended_names on the obstack... */
1018 /* bfd_release (abfd, namedata); */
1025 /* Return a copy of the stuff in the filename between any :]> and a
1035 first
= file
+ strlen(file
)-1;
1038 while (first
!= file
)
1042 if (*first
== ':' || *first
== ']' ||*first
== '>')
1051 copy
= bfd_xmalloc(last
- first
+ 1);
1052 memcpy(copy
, first
, last
-first
);
1053 copy
[last
-first
] = 0;
1063 CONST
char * filename
= strrchr(file
, '/');
1065 if (filename
!= (char *)NULL
) {
1074 /* Follows archive_head and produces an extended name table if necessary.
1075 Returns (in tabloc) a pointer to an extended name table, and in tablen
1076 the length of the table. If it makes an entry it clobbers the filename
1077 so that the element may be written without further massage.
1078 Returns true if it ran successfully, false if something went wrong.
1079 A successful return may still involve a zero-length tablen!
1082 bfd_construct_extended_name_table (abfd
, tabloc
, tablen
)
1085 unsigned int *tablen
;
1087 unsigned int maxname
= abfd
->xvec
->ar_max_namelen
;
1088 unsigned int total_namelen
= 0;
1094 /* Figure out how long the table should be */
1095 for (current
= abfd
->archive_head
; current
!= NULL
; current
= current
->next
){
1096 unsigned int thislen
= strlen (normalize(current
->filename
));
1097 if (thislen
> maxname
) total_namelen
+= thislen
+ 1; /* leave room for \n */
1100 if (total_namelen
== 0) return true;
1102 *tabloc
= bfd_zalloc (abfd
,total_namelen
);
1103 if (*tabloc
== NULL
) {
1104 bfd_error
= no_memory
;
1108 *tablen
= total_namelen
;
1111 for (current
= abfd
->archive_head
; current
!= NULL
; current
=
1113 CONST
char *normal
=normalize( current
->filename
);
1114 unsigned int thislen
= strlen (normal
);
1115 if (thislen
> maxname
) {
1116 /* Works for now; may need to be re-engineered if we encounter an oddball
1117 archive format and want to generalise this hack. */
1118 struct ar_hdr
*hdr
= arch_hdr(current
);
1119 strcpy (strptr
, normal
);
1120 strptr
[thislen
] = '\n';
1121 hdr
->ar_name
[0] = ' ';
1122 /* We know there will always be enough room (one of the few cases
1123 where you may safely use sprintf). */
1124 sprintf ((hdr
->ar_name
) + 1, "%-d", (unsigned) (strptr
- *tabloc
));
1125 /* Kinda Kludgy. We should just use the returned value of sprintf
1126 but not all implementations get this right */
1128 char *temp
= hdr
->ar_name
+2;
1129 for (; temp
< hdr
->ar_name
+ maxname
; temp
++)
1130 if (*temp
== '\0') *temp
= ' ';
1132 strptr
+= thislen
+ 1;
1139 /** A couple of functions for creating ar_hdrs */
1141 /* Takes a filename, returns an arelt_data for it, or NULL if it can't make one.
1142 The filename must refer to a filename in the filesystem.
1143 The filename field of the ar_hdr will NOT be initialized
1146 static struct areltdata
*
1147 bfd_ar_hdr_from_filesystem (abfd
,filename
)
1149 const char *filename
;
1152 struct areltdata
*ared
;
1156 if (stat (filename
, &status
) != 0) {
1157 bfd_error
= system_call_error
;
1161 ared
= (struct areltdata
*) bfd_zalloc(abfd
, sizeof (struct ar_hdr
) +
1162 sizeof (struct areltdata
));
1164 bfd_error
= no_memory
;
1167 hdr
= (struct ar_hdr
*) (((char *) ared
) + sizeof (struct areltdata
));
1169 /* ar headers are space padded, not null padded! */
1170 memset (hdr
, ' ', sizeof (struct ar_hdr
));
1172 strncpy (hdr
->ar_fmag
, ARFMAG
, 2);
1174 /* Goddamned sprintf doesn't permit MAXIMUM field lengths */
1175 sprintf ((hdr
->ar_date
), "%-12ld", status
.st_mtime
);
1176 sprintf ((hdr
->ar_uid
), "%d", status
.st_uid
);
1177 sprintf ((hdr
->ar_gid
), "%d", status
.st_gid
);
1178 sprintf ((hdr
->ar_mode
), "%-8o", (unsigned) status
.st_mode
);
1179 sprintf ((hdr
->ar_size
), "%-10ld", status
.st_size
);
1180 /* Correct for a lossage in sprintf whereby it null-terminates. I cannot
1181 understand how these C losers could design such a ramshackle bunch of
1183 temp
= (char *) hdr
;
1184 temp1
= temp
+ sizeof (struct ar_hdr
) - 2;
1185 for (; temp
< temp1
; temp
++) {
1186 if (*temp
== '\0') *temp
= ' ';
1188 strncpy (hdr
->ar_fmag
, ARFMAG
, 2);
1189 ared
->parsed_size
= status
.st_size
;
1190 ared
->arch_header
= (char *) hdr
;
1195 /* This is magic required by the "ar" program. Since it's
1196 undocumented, it's undocumented. You may think that it would
1197 take a strong stomach to write this, and it does, but it takes
1198 even a stronger stomach to try to code around such a thing!
1202 bfd_special_undocumented_glue (abfd
, filename
)
1206 struct areltdata
*ar_elt
= bfd_ar_hdr_from_filesystem (abfd
, filename
);
1209 return (struct ar_hdr
*) ar_elt
->arch_header
;
1213 /* Analogous to stat call */
1215 bfd_generic_stat_arch_elt (abfd
, buf
)
1222 if (abfd
->arelt_data
== NULL
) {
1223 bfd_error
= invalid_operation
;
1227 hdr
= arch_hdr (abfd
);
1229 #define foo(arelt, stelt, size) \
1230 buf->stelt = strtol (hdr->arelt, &aloser, size); \
1231 if (aloser == hdr->arelt) return -1;
1233 foo (ar_date
, st_mtime
, 10);
1234 foo (ar_uid
, st_uid
, 10);
1235 foo (ar_gid
, st_gid
, 10);
1236 foo (ar_mode
, st_mode
, 8);
1238 buf
->st_size
= arch_eltdata (abfd
)->parsed_size
;
1244 bfd_dont_truncate_arname (abfd
, pathname
, arhdr
)
1246 CONST
char *pathname
;
1249 /* FIXME: This interacts unpleasantly with ar's quick-append option.
1250 Fortunately ic960 users will never use that option. Fixing this
1251 is very hard; fortunately I know how to do it and will do so once
1252 intel's release is out the door. */
1254 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
1256 CONST
char *filename
= normalize(pathname
);
1257 int maxlen
= ar_maxnamelen (abfd
);
1259 length
= strlen (filename
);
1261 if (length
<= maxlen
)
1262 memcpy (hdr
->ar_name
, filename
, length
);
1264 if (length
< maxlen
) (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
1270 bfd_bsd_truncate_arname (abfd
, pathname
, arhdr
)
1272 CONST
char *pathname
;
1275 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
1277 CONST
char *filename
= strrchr (pathname
, '/');
1278 int maxlen
= ar_maxnamelen (abfd
);
1281 if (filename
== NULL
)
1282 filename
= pathname
;
1286 length
= strlen (filename
);
1288 if (length
<= maxlen
)
1289 memcpy (hdr
->ar_name
, filename
, length
);
1291 /* pathname: meet procrustes */
1292 memcpy (hdr
->ar_name
, filename
, maxlen
);
1296 if (length
< maxlen
)
1297 (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
1300 /* Store name into ar header. Truncates the name to fit.
1301 1> strip pathname to be just the basename.
1302 2> if it's short enuf to fit, stuff it in.
1303 3> If it doesn't end with .o, truncate it to fit
1304 4> truncate it before the .o, append .o, stuff THAT in.
1307 /* This is what gnu ar does. It's better but incompatible with the bsd ar. */
1309 bfd_gnu_truncate_arname (abfd
, pathname
, arhdr
)
1311 CONST
char *pathname
;
1314 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
1316 CONST
char *filename
= strrchr (pathname
, '/');
1317 int maxlen
= ar_maxnamelen (abfd
);
1319 if (filename
== NULL
)
1320 filename
= pathname
;
1324 length
= strlen (filename
);
1326 if (length
<= maxlen
)
1327 memcpy (hdr
->ar_name
, filename
, length
);
1328 else { /* pathname: meet procrustes */
1329 memcpy (hdr
->ar_name
, filename
, maxlen
);
1330 if ((filename
[length
- 2] == '.') && (filename
[length
- 1] == 'o')) {
1331 hdr
->ar_name
[maxlen
- 2] = '.';
1332 hdr
->ar_name
[maxlen
- 1] = 'o';
1338 (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
1342 /* The BFD is open for write and has its format set to bfd_archive */
1344 _bfd_write_archive_contents (arch
)
1348 char *etable
= NULL
;
1349 unsigned int elength
= 0;
1350 boolean makemap
= bfd_has_map (arch
);
1351 boolean hasobjects
= false; /* if no .o's, don't bother to make a map */
1355 /* Verify the viability of all entries; if any of them live in the
1356 filesystem (as opposed to living in an archive open for input)
1357 then construct a fresh ar_hdr for them.
1359 for (current
= arch
->archive_head
; current
; current
= current
->next
) {
1360 if (bfd_write_p (current
)) {
1361 bfd_error
= invalid_operation
;
1364 if (!current
->arelt_data
) {
1365 current
->arelt_data
=
1366 (PTR
) bfd_ar_hdr_from_filesystem (arch
, current
->filename
);
1367 if (!current
->arelt_data
) return false;
1369 /* Put in the file name */
1371 BFD_SEND (arch
, _bfd_truncate_arname
,(arch
,
1373 (char *) arch_hdr(current
)));
1378 if (makemap
) { /* don't bother if we won't make a map! */
1379 if ((bfd_check_format (current
, bfd_object
))
1380 #if 0 /* FIXME -- these are not set correctly */
1381 && ((bfd_get_file_flags (current
) & HAS_SYMS
))
1388 if (!bfd_construct_extended_name_table (arch
, &etable
, &elength
))
1391 bfd_seek (arch
, (file_ptr
) 0, SEEK_SET
);
1393 bfd_write (BFD_GNU960_ARMAG(arch
), 1, SARMAG
, arch
);
1395 bfd_write (ARMAG
, 1, SARMAG
, arch
);
1398 if (makemap
&& hasobjects
) {
1400 if (compute_and_write_armap (arch
, elength
) != true) {
1408 memset ((char *)(&hdr
), 0, sizeof (struct ar_hdr
));
1409 sprintf (&(hdr
.ar_name
[0]), "ARFILENAMES/");
1410 sprintf (&(hdr
.ar_size
[0]), "%-10d", (int) elength
);
1411 hdr
.ar_fmag
[0] = '`'; hdr
.ar_fmag
[1] = '\n';
1412 for (i
= 0; i
< sizeof (struct ar_hdr
); i
++)
1413 if (((char *)(&hdr
))[i
] == '\0') (((char *)(&hdr
))[i
]) = ' ';
1414 bfd_write ((char *)&hdr
, 1, sizeof (struct ar_hdr
), arch
);
1415 bfd_write (etable
, 1, elength
, arch
);
1416 if ((elength
% 2) == 1) bfd_write ("\n", 1, 1, arch
);
1420 for (current
= arch
->archive_head
; current
; current
= current
->next
) {
1421 char buffer
[DEFAULT_BUFFERSIZE
];
1422 unsigned int remaining
= arelt_size (current
);
1423 struct ar_hdr
*hdr
= arch_hdr(current
);
1424 /* write ar header */
1426 if (bfd_write ((char *)hdr
, 1, sizeof(*hdr
), arch
) != sizeof(*hdr
)) {
1428 bfd_error
= system_call_error
;
1431 if (bfd_seek (current
, (file_ptr
) 0, SEEK_SET
) != 0) goto syserr
;
1434 unsigned int amt
= DEFAULT_BUFFERSIZE
;
1435 if (amt
> remaining
) {
1439 if (bfd_read (buffer
, amt
, 1, current
) != amt
) {
1440 if (errno
) goto syserr
;
1441 /* Looks like a truncated archive. */
1442 bfd_error
= malformed_archive
;
1445 if (bfd_write (buffer
, amt
, 1, arch
) != amt
) goto syserr
;
1448 if ((arelt_size (current
) % 2) == 1) bfd_write ("\n", 1, 1, arch
);
1451 /* Verify the timestamp in the archive file. If it would
1452 not be accepted by the linker, rewrite it until it would be.
1453 If anything odd happens, break out and just return.
1454 (The Berkeley linker checks the timestamp and refuses to read the
1455 table-of-contents if it is >60 seconds less than the file's
1456 modified-time. That painful hack requires this painful hack. */
1460 /* FIXME! This kludge is to avoid adding a member to the xvec,
1461 while generating a small patch for Adobe. FIXME! The
1462 update_armap_timestamp function call should be in the xvec,
1465 if (bfd_update_armap_timestamp (arch) == true) break;
1468 Instead, we check whether in a BSD archive, and call directly. */
1470 if (arch
->xvec
->write_armap
!= bsd_write_armap
)
1472 if (bsd_update_armap_timestamp(arch
) == true) /* FIXME!!! Vector it */
1476 "Warning: writing archive was slow: rewriting timestamp\n");
1477 } while (++tries
< 6 );
1482 /* Note that the namidx for the first symbol is 0 */
1485 compute_and_write_armap (arch
, elength
)
1487 unsigned int elength
;
1490 file_ptr elt_no
= 0;
1492 int orl_max
= 15000; /* fine initial default */
1494 int stridx
= 0; /* string index */
1496 /* Dunno if this is the best place for this info... */
1497 if (elength
!= 0) elength
+= sizeof (struct ar_hdr
);
1498 elength
+= elength
%2 ;
1500 map
= (struct orl
*) bfd_zalloc (arch
,orl_max
* sizeof (struct orl
));
1502 bfd_error
= no_memory
;
1506 /* Drop all the files called __.SYMDEF, we're going to make our
1508 while (arch
->archive_head
&&
1509 strcmp(arch
->archive_head
->filename
,"__.SYMDEF") == 0)
1511 arch
->archive_head
= arch
->archive_head
->next
;
1513 /* Map over each element */
1514 for (current
= arch
->archive_head
;
1515 current
!= (bfd
*)NULL
;
1516 current
= current
->next
, elt_no
++)
1518 if ((bfd_check_format (current
, bfd_object
) == true)
1519 && ((bfd_get_file_flags (current
) & HAS_SYMS
))) {
1521 unsigned int storage
;
1522 unsigned int symcount
;
1523 unsigned int src_count
;
1525 storage
= get_symtab_upper_bound (current
);
1528 syms
= (asymbol
**) bfd_zalloc (arch
,storage
);
1530 bfd_error
= no_memory
; /* FIXME -- memory leak */
1533 symcount
= bfd_canonicalize_symtab (current
, syms
);
1536 /* Now map over all the symbols, picking out the ones we want */
1537 for (src_count
= 0; src_count
<symcount
; src_count
++) {
1539 (syms
[src_count
])->flags
;
1541 syms
[src_count
]->section
;
1543 if ((flags
& BSF_GLOBAL
||
1545 flags
& BSF_INDIRECT
||
1546 bfd_is_com_section (sec
))
1547 && (sec
!= &bfd_und_section
)) {
1549 /* This symbol will go into the archive header */
1550 if (orl_count
== orl_max
)
1553 map
= (struct orl
*) bfd_realloc (arch
, (char *) map
,
1554 orl_max
* sizeof (struct orl
));
1557 (map
[orl_count
]).name
= (char **) &((syms
[src_count
])->name
);
1558 (map
[orl_count
]).pos
= (file_ptr
) current
;
1559 (map
[orl_count
]).namidx
= stridx
;
1561 stridx
+= strlen ((syms
[src_count
])->name
) + 1;
1568 /* OK, now we have collected all the data, let's write them out */
1569 if (!BFD_SEND (arch
, write_armap
,
1570 (arch
, elength
, map
, orl_count
, stridx
))) {
1580 bsd_write_armap (arch
, elength
, map
, orl_count
, stridx
)
1582 unsigned int elength
;
1584 unsigned int orl_count
;
1587 int padit
= stridx
& 1;
1588 unsigned int ranlibsize
= orl_count
* sizeof (struct ranlib
);
1589 unsigned int stringsize
= stridx
+ padit
;
1590 /* Include 8 bytes to store ranlibsize and stringsize in output. */
1591 unsigned int mapsize
= ranlibsize
+ stringsize
+ 8;
1593 bfd
*current
= arch
->archive_head
;
1594 bfd
*last_elt
= current
; /* last element arch seen */
1598 struct stat statbuf
;
1601 firstreal
= mapsize
+ elength
+ sizeof (struct ar_hdr
) + SARMAG
;
1603 stat (arch
->filename
, &statbuf
);
1604 memset ((char *)(&hdr
), 0, sizeof (struct ar_hdr
));
1605 sprintf (hdr
.ar_name
, RANLIBMAG
);
1606 /* Remember the timestamp, to keep it holy. But fudge it a little. */
1607 bfd_ardata(arch
)->armap_timestamp
= statbuf
.st_mtime
+ ARMAP_TIME_OFFSET
;
1608 bfd_ardata(arch
)->armap_datepos
= SARMAG
+
1609 offsetof(struct ar_hdr
, ar_date
[0]);
1610 sprintf (hdr
.ar_date
, "%ld", bfd_ardata(arch
)->armap_timestamp
);
1611 sprintf (hdr
.ar_uid
, "%d", getuid());
1612 sprintf (hdr
.ar_gid
, "%d", getgid());
1613 sprintf (hdr
.ar_size
, "%-10d", (int) mapsize
);
1614 hdr
.ar_fmag
[0] = '`'; hdr
.ar_fmag
[1] = '\n';
1615 for (i
= 0; i
< sizeof (struct ar_hdr
); i
++)
1616 if (((char *)(&hdr
))[i
] == '\0') (((char *)(&hdr
))[i
]) = ' ';
1617 bfd_write ((char *)&hdr
, 1, sizeof (struct ar_hdr
), arch
);
1618 bfd_h_put_32(arch
, (bfd_vma
) ranlibsize
, (PTR
)&temp
);
1619 bfd_write (&temp
, 1, sizeof (temp
), arch
);
1621 for (count
= 0; count
< orl_count
; count
++) {
1623 struct symdef
*outp
= &outs
;
1625 if (((bfd
*)(map
[count
]).pos
) != last_elt
) {
1627 firstreal
+= arelt_size (current
) + sizeof (struct ar_hdr
);
1628 firstreal
+= firstreal
% 2;
1629 current
= current
->next
;
1630 } while (current
!= (bfd
*)(map
[count
]).pos
);
1631 } /* if new archive element */
1634 bfd_h_put_32(arch
, ((map
[count
]).namidx
),(PTR
) &outs
.s
.string_offset
);
1635 bfd_h_put_32(arch
, firstreal
,(PTR
) &outs
.file_offset
);
1636 bfd_write ((char *)outp
, 1, sizeof (outs
), arch
);
1639 /* now write the strings themselves */
1640 bfd_h_put_32(arch
, stringsize
, (PTR
)&temp
);
1641 bfd_write ((PTR
)&temp
, 1, sizeof (temp
), arch
);
1642 for (count
= 0; count
< orl_count
; count
++)
1643 bfd_write (*((map
[count
]).name
), 1, strlen (*((map
[count
]).name
))+1, arch
);
1645 /* The spec sez this should be a newline. But in order to be
1646 bug-compatible for sun's ar we use a null. */
1648 bfd_write("",1,1,arch
);
1654 /* At the end of archive file handling, update the timestamp in the
1655 file, so the linker will accept it.
1657 Return true if the timestamp was OK, or an unusual problem happened.
1658 Return false if we updated the timestamp. */
1661 bsd_update_armap_timestamp (arch
)
1664 struct stat archstat
;
1668 /* Flush writes, get last-write timestamp from file, and compare it
1669 to the timestamp IN the file. */
1671 if (bfd_stat (arch
, &archstat
) == -1) {
1672 perror ("Reading archive file mod timestamp");
1673 return true; /* Can't read mod time for some reason */
1675 if (archstat
.st_mtime
<= bfd_ardata(arch
)->armap_timestamp
)
1676 return true; /* OK by the linker's rules */
1678 /* Update the timestamp. */
1679 bfd_ardata(arch
)->armap_timestamp
= archstat
.st_mtime
+ ARMAP_TIME_OFFSET
;
1681 /* Prepare an ASCII version suitable for writing. */
1682 memset (hdr
.ar_date
, 0, sizeof (hdr
.ar_date
));
1683 sprintf (hdr
.ar_date
, "%ld", bfd_ardata(arch
)->armap_timestamp
);
1684 for (i
= 0; i
< sizeof (hdr
.ar_date
); i
++)
1685 if (hdr
.ar_date
[i
] == '\0')
1686 (hdr
.ar_date
)[i
] = ' ';
1688 /* Write it into the file. */
1689 bfd_seek (arch
, bfd_ardata(arch
)->armap_datepos
, SEEK_SET
);
1690 if (bfd_write (hdr
.ar_date
, sizeof(hdr
.ar_date
), 1, arch
)
1691 != sizeof(hdr
.ar_date
)) {
1692 perror ("Writing updated armap timestamp");
1693 return true; /* Some error while writing */
1696 return false; /* We updated the timestamp successfully. */
1700 /* A coff armap looks like :
1702 struct ar_hdr with name = '/'
1704 offset of file for symbol 0
1705 offset of file for symbol 1
1707 offset of file for symbol n-1
1716 coff_write_armap (arch
, elength
, map
, symbol_count
, stridx
)
1718 unsigned int elength
;
1720 unsigned int symbol_count
;
1723 /* The size of the ranlib is the number of exported symbols in the
1724 archive * the number of bytes in a int, + an int for the count */
1726 unsigned int ranlibsize
= (symbol_count
* 4) + 4;
1727 unsigned int stringsize
= stridx
;
1728 unsigned int mapsize
= stringsize
+ ranlibsize
;
1729 file_ptr archive_member_file_ptr
;
1730 bfd
*current
= arch
->archive_head
;
1734 int padit
= mapsize
& 1;
1736 if (padit
) mapsize
++;
1738 /* work out where the first object file will go in the archive */
1739 archive_member_file_ptr
= mapsize
+ elength
+ sizeof (struct ar_hdr
) + SARMAG
;
1741 memset ((char *)(&hdr
), 0, sizeof (struct ar_hdr
));
1742 hdr
.ar_name
[0] = '/';
1743 sprintf (hdr
.ar_size
, "%-10d", (int) mapsize
);
1744 sprintf (hdr
.ar_date
, "%ld", (long)time (NULL
));
1745 /* This, at least, is what Intel coff sets the values to.: */
1746 sprintf ((hdr
.ar_uid
), "%d", 0);
1747 sprintf ((hdr
.ar_gid
), "%d", 0);
1748 sprintf ((hdr
.ar_mode
), "%-7o",(unsigned ) 0);
1749 hdr
.ar_fmag
[0] = '`'; hdr
.ar_fmag
[1] = '\n';
1751 for (i
= 0; i
< sizeof (struct ar_hdr
); i
++)
1752 if (((char *)(&hdr
))[i
] == '\0') (((char *)(&hdr
))[i
]) = ' ';
1754 /* Write the ar header for this item and the number of symbols */
1757 bfd_write ((PTR
)&hdr
, 1, sizeof (struct ar_hdr
), arch
);
1759 bfd_write_bigendian_4byte_int(arch
, symbol_count
);
1761 /* Two passes, first write the file offsets for each symbol -
1762 remembering that each offset is on a two byte boundary. */
1764 /* Write out the file offset for the file associated with each
1765 symbol, and remember to keep the offsets padded out. */
1767 current
= arch
->archive_head
;
1769 while (current
!= (bfd
*)NULL
&& count
< symbol_count
) {
1770 /* For each symbol which is used defined in this object, write out
1771 the object file's address in the archive */
1773 while (((bfd
*)(map
[count
]).pos
) == current
) {
1774 bfd_write_bigendian_4byte_int(arch
, archive_member_file_ptr
);
1777 /* Add size of this archive entry */
1778 archive_member_file_ptr
+= arelt_size (current
) + sizeof (struct
1780 /* remember aboout the even alignment */
1781 archive_member_file_ptr
+= archive_member_file_ptr
% 2;
1782 current
= current
->next
;
1787 /* now write the strings themselves */
1788 for (count
= 0; count
< symbol_count
; count
++) {
1789 bfd_write ((PTR
)*((map
[count
]).name
),
1791 strlen (*((map
[count
]).name
))+1, arch
);
1794 /* The spec sez this should be a newline. But in order to be
1795 bug-compatible for arc960 we use a null. */
1797 bfd_write("",1,1,arch
);