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 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 (e.g., 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.
95 We support two variants of this idea:
96 The SVR4 format (extended name table is named "//"),
97 and an extended pseudo-BSD variant (extended name table is named
98 "ARFILENAMES/"). The origin of the latter format is uncertain.
100 BSD 4.4 uses a third scheme: It writes a long filename
101 directly after the header. This allows 'ar q' to work.
102 We current can read BSD 4.4 archives, but not write them.
105 /* Summary of archive member names:
107 Symbol table (must be first):
108 "__.SYMDEF " - Symbol table, Berkeley style, produced by ranlib.
109 "/ " - Symbol table, system 5 style.
111 Long name table (must be before regular file members):
112 "// " - Long name table, System 5 R4 style.
113 "ARFILENAMES/ " - Long name table, non-standard extended BSD (not BSD 4.4).
115 Regular file members with short names:
116 "filename.o/ " - Regular file, System 5 style (embedded spaces ok).
117 "filename.o " - Regular file, Berkeley style (no embedded spaces).
119 Regular files with long names (or embedded spaces, for BSD variants):
120 "/18 " - SVR4 style, name at offset 18 in name table.
121 "#1/23 " - Long name (or embedded paces) 23 characters long,
122 BSD 4.4 style, full name follows header.
123 Implemented for reading, not writing.
124 " 18 " - Long name 18 characters long, extended pseudo-BSD.
131 #include "aout/ranlib.h"
133 #include <string.h> /* For memchr, strrchr and friends */
141 #define BFD_GNU960_ARMAG(abfd) (BFD_COFF_FILE_P((abfd)) ? ARMAG : ARMAGB)
144 /* We keep a cache of archive filepointers to archive elements to
145 speed up searching the archive by filepos. We only add an entry to
146 the cache when we actually read one. We also don't sort the cache;
147 it's generally short enough to search linearly.
148 Note that the pointers here point to the front of the ar_hdr, not
149 to the front of the contents!
154 struct ar_cache
*next
;
157 #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
158 #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
160 #define arch_eltdata(bfd) ((struct areltdata *)((bfd)->arelt_data))
161 #define arch_hdr(bfd) ((struct ar_hdr *)arch_eltdata(bfd)->arch_header)
163 /* Forward declarations of functions */
166 compute_and_write_armap
PARAMS ((bfd
*arch
, unsigned int elength
));
169 bsd_update_armap_timestamp
PARAMS ((bfd
*arch
));
174 _bfd_generic_mkarchive (abfd
)
177 abfd
->tdata
.aout_ar_data
= (struct artdata
*)bfd_zalloc(abfd
,
178 sizeof (struct artdata
));
180 if (bfd_ardata (abfd
) == NULL
) {
181 bfd_error
= no_memory
;
184 bfd_ardata(abfd
)->cache
= 0;
193 symindex bfd_get_next_mapent(bfd *, symindex previous, carsym ** sym);
196 This function steps through an archive's symbol table (if it
197 has one). Successively updates <<sym>> with the next symbol's
198 information, returning that symbol's (internal) index into the
201 Supply BFD_NO_MORE_SYMBOLS as the <<previous>> entry to get
202 the first one; returns BFD_NO_MORE_SYMBOLS when you're already
205 A <<carsym>> is a canonical archive symbol. The only
206 user-visible element is its name, a null-terminated string.
210 DEFUN(bfd_get_next_mapent
,(abfd
, prev
, entry
),
215 if (!bfd_has_map (abfd
)) {
216 bfd_error
= invalid_operation
;
217 return BFD_NO_MORE_SYMBOLS
;
220 if (prev
== BFD_NO_MORE_SYMBOLS
) prev
= 0;
221 else if (++prev
>= bfd_ardata (abfd
)->symdef_count
)
222 return BFD_NO_MORE_SYMBOLS
;
224 *entry
= (bfd_ardata (abfd
)->symdefs
+ prev
);
228 /* To be called by backends only */
230 _bfd_create_empty_archive_element_shell (obfd
)
235 nbfd
= new_bfd_contained_in(obfd
);
237 bfd_error
= no_memory
;
248 boolean bfd_set_archive_head(bfd *output, bfd *new_head);
251 Used whilst processing archives. Sets the head of the chain of
252 BFDs contained in an archive to @var{new_head}.
256 DEFUN(bfd_set_archive_head
,(output_archive
, new_head
),
257 bfd
*output_archive AND
261 output_archive
->archive_head
= new_head
;
266 look_for_bfd_in_cache (arch_bfd
, filepos
)
270 struct ar_cache
*current
;
272 for (current
= bfd_ardata (arch_bfd
)->cache
; current
!= NULL
;
273 current
= current
->next
)
274 if (current
->ptr
== filepos
) return current
->arelt
;
279 /* Kind of stupid to call cons for each one, but we don't do too many */
281 add_bfd_to_cache (arch_bfd
, filepos
, new_elt
)
282 bfd
*arch_bfd
, *new_elt
;
285 struct ar_cache
*new_cache
= (struct ar_cache
*)
286 bfd_zalloc(arch_bfd
, sizeof (struct ar_cache
));
288 if (new_cache
== NULL
) {
289 bfd_error
= no_memory
;
293 new_cache
->ptr
= filepos
;
294 new_cache
->arelt
= new_elt
;
295 new_cache
->next
= (struct ar_cache
*)NULL
;
296 if (bfd_ardata (arch_bfd
)->cache
== NULL
)
297 bfd_ardata (arch_bfd
)->cache
= new_cache
;
299 struct ar_cache
*current
= bfd_ardata (arch_bfd
)->cache
;
301 for (; current
->next
!= NULL
; current
= current
->next
);
302 current
->next
= new_cache
;
310 /* The name begins with space. Hence the rest of the name is an index into
313 get_extended_arelt_filename (arch
, name
)
317 unsigned long index
= 0;
319 /* Should extract string so that I can guarantee not to overflow into
320 the next region, but I'm too lazy. */
322 /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */
323 index
= strtol (name
+1, NULL
, 10);
325 bfd_error
= malformed_archive
;
329 return bfd_ardata (arch
)->extended_names
+ index
;
332 /* This functions reads an arch header and returns an areltdata pointer, or
335 Presumes the file pointer is already in the right place (ie pointing
336 to the ar_hdr in the file). Moves the file pointer; on success it
337 should be pointing to the front of the file contents; on failure it
338 could have been moved arbitrarily.
350 char *hdrp
= (char *) &hdr
;
351 unsigned int parsed_size
;
352 struct areltdata
*ared
;
353 char *filename
= NULL
;
354 unsigned int namelen
= 0;
355 unsigned int allocsize
= sizeof (struct areltdata
) + sizeof (struct ar_hdr
);
358 if (bfd_read ((PTR
)hdrp
, 1, sizeof (struct ar_hdr
), abfd
)
359 != sizeof (struct ar_hdr
)) {
360 bfd_error
= no_more_archived_files
;
363 if (strncmp ((hdr
.ar_fmag
), ARFMAG
, 2)) {
364 bfd_error
= malformed_archive
;
369 parsed_size
= strtol (hdr
.ar_size
, NULL
, 10);
371 bfd_error
= malformed_archive
;
375 /* extract the filename from the archive - there are two ways to
376 specify an extendend name table, either the first char of the
377 name is a space, or it's a slash. */
378 if ((hdr
.ar_name
[0] == '/'
379 || (hdr
.ar_name
[0] == ' '
380 && memchr (hdr
.ar_name
, '/', ar_maxnamelen(abfd
)) == NULL
))
381 && bfd_ardata (abfd
)->extended_names
!= NULL
) {
382 filename
= get_extended_arelt_filename (abfd
, hdr
.ar_name
);
383 if (filename
== NULL
) {
384 bfd_error
= malformed_archive
;
388 /* BSD4.4-style long filename.
389 Only implemented for reading, so far! */
390 else if (hdr
.ar_name
[0] == '#' && hdr
.ar_name
[1] == '1'
391 && hdr
.ar_name
[2] == '/' && isdigit(hdr
.ar_name
[3]))
393 /* BSD-4.4 extended name */
394 namelen
= atoi (&hdr
.ar_name
[3]);
395 allocsize
+= namelen
+ 1;
396 parsed_size
-= namelen
;
398 allocptr
= bfd_zalloc(abfd
, allocsize
);
399 if (allocptr
== NULL
) {
400 bfd_error
= no_memory
;
404 + (sizeof (struct areltdata
) + sizeof (struct ar_hdr
));
405 if (bfd_read (filename
, 1, namelen
, abfd
) != namelen
) {
406 bfd_error
= no_more_archived_files
;
409 filename
[namelen
] = '\0';
413 /* We judge the end of the name by looking for '/' or ' '.
414 Note: The SYSV format (terminated by '/') allows embedded
415 spaces, so only look for ' ' if we don't find '/'. */
418 while (hdr
.ar_name
[namelen
] != '\0' &&
419 hdr
.ar_name
[namelen
] != '/') {
421 if (namelen
== (unsigned)ar_maxnamelen(abfd
)) {
423 while (hdr
.ar_name
[namelen
] != ' '
424 && namelen
< (unsigned)ar_maxnamelen(abfd
)) {
431 allocsize
+= namelen
+ 1;
435 allocptr
= bfd_zalloc(abfd
, allocsize
);
436 if (allocptr
== NULL
) {
437 bfd_error
= no_memory
;
442 ared
= (struct areltdata
*) allocptr
;
444 ared
->arch_header
= allocptr
+ sizeof (struct areltdata
);
445 memcpy ((char *) ared
->arch_header
, (char *) &hdr
, sizeof (struct ar_hdr
));
446 ared
->parsed_size
= parsed_size
;
448 if (filename
!= NULL
) ared
->filename
= filename
;
450 ared
->filename
= allocptr
+ (sizeof (struct areltdata
) +
451 sizeof (struct ar_hdr
));
453 memcpy (ared
->filename
, hdr
.ar_name
, namelen
);
454 ared
->filename
[namelen
] = '\0';
460 /* This is an internal function; it's mainly used when indexing
461 through the archive symbol table, but also used to get the next
462 element, since it handles the bookkeeping so nicely for us.
466 DEFUN (get_elt_at_filepos
, (archive
, filepos
),
470 struct areltdata
*new_areldata
;
473 n_nfd
= look_for_bfd_in_cache (archive
, filepos
);
474 if (n_nfd
) return n_nfd
;
476 if (0 > bfd_seek (archive
, filepos
, SEEK_SET
)) {
477 bfd_error
= system_call_error
;
481 if ((new_areldata
= snarf_ar_hdr (archive
)) == NULL
) return NULL
;
483 n_nfd
= _bfd_create_empty_archive_element_shell (archive
);
485 bfd_release (archive
, (PTR
)new_areldata
);
488 n_nfd
->origin
= bfd_tell (archive
);
489 n_nfd
->arelt_data
= (PTR
) new_areldata
;
490 n_nfd
->filename
= new_areldata
->filename
;
492 if (add_bfd_to_cache (archive
, filepos
, n_nfd
))
496 bfd_release (archive
, (PTR
)n_nfd
);
497 bfd_release (archive
, (PTR
)new_areldata
);
506 bfd *bfd_get_elt_at_index(bfd * archive, int index);
509 Return the bfd which is referenced by the symbol indexed by <<index>>.
510 <<index>> should have been returned by <<bfd_get_next_mapent>> (q.v.).
514 DEFUN(bfd_get_elt_at_index
,(abfd
, index
),
520 (abfd
, (bfd_ardata (abfd
)->symdefs
+ index
)->file_offset
);
526 bfd_openr_next_archived_file
529 bfd* bfd_openr_next_archived_file(bfd *archive, bfd *previous);
532 Initially provided a BFD containing an archive and NULL, opens
533 an inpout BFD on the first contained element and returns that.
534 Subsequent calls to bfd_openr_next_archived_file should pass
535 the archive and the previous return value to return a created
536 BFD to the next contained element. NULL is returned when there
542 DEFUN(bfd_openr_next_archived_file
,(archive
, last_file
),
547 if ((bfd_get_format (archive
) != bfd_archive
) ||
548 (archive
->direction
== write_direction
)) {
549 bfd_error
= invalid_operation
;
554 return BFD_SEND (archive
,
555 openr_next_archived_file
,
561 bfd
*bfd_generic_openr_next_archived_file(archive
, last_file
)
568 filestart
= bfd_ardata (archive
)->first_file_filepos
;
570 unsigned int size
= arelt_size(last_file
);
571 /* Pad to an even boundary...
572 Note that last_file->origin can be odd in the case of
573 BSD-4.4-style element with a long odd size. */
574 filestart
= last_file
->origin
+ size
;
575 filestart
+= filestart
% 2;
578 return get_elt_at_filepos (archive
, filestart
);
583 bfd_generic_archive_p (abfd
)
586 char armag
[SARMAG
+1];
588 if (bfd_read ((PTR
)armag
, 1, SARMAG
, abfd
) != SARMAG
) {
589 bfd_error
= wrong_format
;
594 if (strncmp (armag
, BFD_GNU960_ARMAG(abfd
), SARMAG
)) return 0;
596 if (strncmp (armag
, ARMAG
, SARMAG
) &&
597 strncmp (armag
, ARMAGB
, SARMAG
)) return 0;
602 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
603 involves a cast, we can't do it as the left operand of assignment. */
604 abfd
->tdata
.aout_ar_data
= (struct artdata
*) bfd_zalloc(abfd
,sizeof (struct artdata
));
606 if (bfd_ardata (abfd
) == NULL
) {
607 bfd_error
= no_memory
;
611 bfd_ardata (abfd
)->first_file_filepos
= SARMAG
;
613 if (!BFD_SEND (abfd
, _bfd_slurp_armap
, (abfd
))) {
614 bfd_release(abfd
, bfd_ardata (abfd
));
615 abfd
->tdata
.aout_ar_data
= NULL
;
619 if (!BFD_SEND (abfd
, _bfd_slurp_extended_name_table
, (abfd
))) {
620 bfd_release(abfd
, bfd_ardata (abfd
));
621 abfd
->tdata
.aout_ar_data
= NULL
;
628 /* Returns false on error, true otherwise */
630 DEFUN (do_slurp_bsd_armap
, (abfd
),
633 struct areltdata
*mapdata
;
634 unsigned int counter
= 0;
635 int *raw_armap
, *rbase
;
636 struct artdata
*ardata
= bfd_ardata (abfd
);
638 unsigned int parsed_size
;
640 mapdata
= snarf_ar_hdr (abfd
);
641 if (mapdata
== NULL
) return false;
642 parsed_size
= mapdata
->parsed_size
;
643 bfd_release (abfd
, (PTR
)mapdata
); /* Don't need it any more. */
645 raw_armap
= (int *) bfd_zalloc(abfd
, parsed_size
);
646 if (raw_armap
== NULL
) {
647 bfd_error
= no_memory
;
651 if (bfd_read ((PTR
)raw_armap
, 1, parsed_size
, abfd
) != parsed_size
) {
652 bfd_error
= malformed_archive
;
654 bfd_release (abfd
, (PTR
)raw_armap
);
658 ardata
->symdef_count
=
659 bfd_h_get_32(abfd
, (PTR
)raw_armap
) / sizeof (struct symdef
);
661 if (ardata
->symdef_count
* sizeof (struct symdef
)
662 > parsed_size
- sizeof (*raw_armap
)) {
663 /* Probably we're using the wrong byte ordering. */
664 bfd_error
= wrong_format
;
670 ardata
->symdefs
= (carsym
*) rbase
;
671 stringbase
= ((char *) (ardata
->symdefs
+ ardata
->symdef_count
)) + 4;
673 for (;counter
< ardata
->symdef_count
; counter
++) {
674 struct symdef
*sym
= ((struct symdef
*) rbase
) + counter
;
675 sym
->s
.name
= bfd_h_get_32(abfd
, (PTR
)(&(sym
->s
.string_offset
))) + stringbase
;
676 sym
->file_offset
= bfd_h_get_32(abfd
, (PTR
)( &(sym
->file_offset
)));
679 ardata
->first_file_filepos
= bfd_tell (abfd
);
680 /* Pad to an even boundary if you have to */
681 ardata
->first_file_filepos
+= (ardata
-> first_file_filepos
) %2;
682 /* FIXME, we should provide some way to free raw_ardata when
683 we are done using the strings from it. For now, it seems
684 to be allocated on an obstack anyway... */
685 bfd_has_map (abfd
) = true;
689 /* Returns false on error, true otherwise */
691 DEFUN (do_slurp_coff_armap
, (abfd
),
694 struct areltdata
*mapdata
;
695 int *raw_armap
, *rawptr
;
696 struct artdata
*ardata
= bfd_ardata (abfd
);
698 unsigned int stringsize
;
699 unsigned int parsed_size
;
701 unsigned int nsymz
; /* Number of symbols in armap. */
703 bfd_vma (*swap
) PARAMS ((bfd_byte
*));
704 char int_buf
[sizeof(long)];
705 unsigned int carsym_size
, ptrsize
, i
;
707 mapdata
= snarf_ar_hdr (abfd
);
708 if (mapdata
== NULL
) return false;
709 parsed_size
= mapdata
->parsed_size
;
710 bfd_release (abfd
, (PTR
)mapdata
); /* Don't need it any more. */
712 if (bfd_read ((PTR
)int_buf
, 1, 4, abfd
) != 4) {
713 bfd_error
= malformed_archive
;
716 /* It seems that all numeric information in a coff archive is always
717 in big endian format, nomatter the host or target. */
719 nsymz
= bfd_getb32((PTR
)int_buf
);
720 stringsize
= parsed_size
- (4 * nsymz
) - 4;
723 /* ... except that some archive formats are broken, and it may be our
724 fault - the i960 little endian coff sometimes has big and sometimes
725 little, because our tools changed. Here's a horrible hack to clean
728 if (stringsize
> 0xfffff) {
729 /* This looks dangerous, let's do it the other way around */
730 nsymz
= bfd_getl32((PTR
)int_buf
);
731 stringsize
= parsed_size
- (4 * nsymz
) - 4;
736 /* The coff armap must be read sequentially. So we construct a bsd-style
737 one in core all at once, for simplicity. */
739 carsym_size
= (nsymz
* sizeof (carsym
));
740 ptrsize
= (4 * nsymz
);
742 ardata
->symdefs
= (carsym
*) bfd_zalloc(abfd
, carsym_size
+ stringsize
+ 1);
743 if (ardata
->symdefs
== NULL
) {
744 bfd_error
= no_memory
;
747 carsyms
= ardata
->symdefs
;
748 stringbase
= ((char *) ardata
->symdefs
) + carsym_size
;
750 /* Allocate and read in the raw offsets. */
751 raw_armap
= (int *) bfd_alloc(abfd
, ptrsize
);
752 if (raw_armap
== NULL
) {
753 bfd_error
= no_memory
;
754 goto release_symdefs
;
756 if (bfd_read ((PTR
)raw_armap
, 1, ptrsize
, abfd
) != ptrsize
757 || bfd_read ((PTR
)stringbase
, 1, stringsize
, abfd
) != stringsize
) {
758 bfd_error
= malformed_archive
;
759 goto release_raw_armap
;
762 /* OK, build the carsyms */
763 for (i
= 0; i
< nsymz
; i
++) {
764 rawptr
= raw_armap
+ i
;
765 carsyms
->file_offset
= swap((PTR
)rawptr
);
766 carsyms
->name
= stringbase
;
767 while (*stringbase
++) ;
772 ardata
->symdef_count
= nsymz
;
773 ardata
->first_file_filepos
= bfd_tell (abfd
);
774 /* Pad to an even boundary if you have to */
775 ardata
->first_file_filepos
+= (ardata
->first_file_filepos
) %2;
777 bfd_has_map (abfd
) = true;
778 bfd_release (abfd
, (PTR
)raw_armap
);
782 bfd_release (abfd
, (PTR
)raw_armap
);
784 bfd_release (abfd
, (PTR
)(ardata
)->symdefs
);
788 /* This routine can handle either coff-style or bsd-style armaps.
789 Returns false on error, true otherwise */
792 bfd_slurp_armap (abfd
)
796 int i
= bfd_read ((PTR
)nextname
, 1, 16, abfd
);
803 bfd_seek (abfd
, (file_ptr
) -16, SEEK_CUR
);
805 if (!strncmp (nextname
, "__.SYMDEF ", 16))
806 return do_slurp_bsd_armap (abfd
);
807 else if (!strncmp (nextname
, "/ ", 16))
808 return do_slurp_coff_armap (abfd
);
810 bfd_has_map (abfd
) = false;
814 /* Returns false on error, true otherwise */
815 /* flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the
816 header is in a slightly different order and the map name is '/'.
817 This flavour is used by hp300hpux. */
819 bfd_slurp_bsd_armap_f2 (abfd
)
822 struct areltdata
*mapdata
;
824 unsigned int counter
= 0;
825 int *raw_armap
, *rbase
;
826 struct artdata
*ardata
= bfd_ardata (abfd
);
828 unsigned int stringsize
;
829 int i
= bfd_read ((PTR
)nextname
, 1, 16, abfd
);
836 /* The archive has at least 16 bytes in it */
837 bfd_seek (abfd
, -16L, SEEK_CUR
);
839 if (!strncmp (nextname
, "__.SYMDEF ", 16))
840 return do_slurp_bsd_armap (abfd
);
842 if (strncmp (nextname
, "/ ", 16))
844 bfd_has_map (abfd
) = false;
848 mapdata
= snarf_ar_hdr (abfd
);
849 if (mapdata
== NULL
) return false;
851 raw_armap
= (int *) bfd_zalloc(abfd
,mapdata
->parsed_size
);
852 if (raw_armap
== NULL
)
854 bfd_error
= no_memory
;
856 bfd_release (abfd
, (PTR
)mapdata
);
860 if (bfd_read ((PTR
)raw_armap
, 1, mapdata
->parsed_size
, abfd
) !=
861 mapdata
->parsed_size
)
863 bfd_error
= malformed_archive
;
865 bfd_release (abfd
, (PTR
)raw_armap
);
869 ardata
->symdef_count
= bfd_h_get_16(abfd
, (PTR
)raw_armap
);
871 if (ardata
->symdef_count
* sizeof (struct symdef
)
872 > mapdata
->parsed_size
- sizeof (*raw_armap
))
874 /* Probably we're using the wrong byte ordering. */
875 bfd_error
= wrong_format
;
881 stringsize
= bfd_h_get_32(abfd
, (PTR
)(((char*)raw_armap
)+2));
882 /* skip sym count and string sz */
883 rbase
= (int*)(((char*)raw_armap
) + 6);
884 stringbase
= (char *) rbase
;
885 ardata
->symdefs
= (carsym
*)(((char*) rbase
) + stringsize
);
887 for (;counter
< ardata
->symdef_count
; counter
++)
889 struct symdef
*sym
= ((struct symdef
*) ardata
->symdefs
) + counter
;
890 sym
->s
.name
= bfd_h_get_32(abfd
, (PTR
)(&(sym
->s
.string_offset
))) + stringbase
;
891 sym
->file_offset
= bfd_h_get_32(abfd
, (PTR
)( &(sym
->file_offset
)));
894 ardata
->first_file_filepos
= bfd_tell (abfd
);
895 /* Pad to an even boundary if you have to */
896 ardata
->first_file_filepos
+= (ardata
-> first_file_filepos
) %2;
897 /* FIXME, we should provide some way to free raw_ardata when
898 we are done using the strings from it. For now, it seems
899 to be allocated on an obstack anyway... */
900 bfd_has_map (abfd
) = true;
904 /** Extended name table.
906 Normally archives support only 14-character filenames.
908 Intel has extended the format: longer names are stored in a special
909 element (the first in the archive, or second if there is an armap);
910 the name in the ar_hdr is replaced by <space><index into filename
911 element>. Index is the P.R. of an int (decimal). Data General have
912 extended the format by using the prefix // for the special element */
914 /* Returns false on error, true otherwise */
916 _bfd_slurp_extended_name_table (abfd
)
920 struct areltdata
*namedata
;
922 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
923 we probably don't want to return true. */
924 if (bfd_read ((PTR
)nextname
, 1, 16, abfd
) == 16) {
926 bfd_seek (abfd
, (file_ptr
) -16, SEEK_CUR
);
928 if (strncmp (nextname
, "ARFILENAMES/ ", 16) != 0 &&
929 strncmp (nextname
, "// ", 16) != 0)
931 bfd_ardata (abfd
)->extended_names
= NULL
;
935 namedata
= snarf_ar_hdr (abfd
);
936 if (namedata
== NULL
) return false;
938 bfd_ardata (abfd
)->extended_names
= bfd_zalloc(abfd
,namedata
->parsed_size
);
939 if (bfd_ardata (abfd
)->extended_names
== NULL
) {
940 bfd_error
= no_memory
;
942 bfd_release (abfd
, (PTR
)namedata
);
946 if (bfd_read ((PTR
)bfd_ardata (abfd
)->extended_names
, 1,
947 namedata
->parsed_size
, abfd
) != namedata
->parsed_size
) {
948 bfd_error
= malformed_archive
;
949 bfd_release (abfd
, (PTR
)(bfd_ardata (abfd
)->extended_names
));
950 bfd_ardata (abfd
)->extended_names
= NULL
;
954 /* Since the archive is supposed to be printable if it contains
955 text, the entries in the list are newline-padded, not null
956 padded. In SVR4-style archives, the names also have a
957 trailing '/'. We'll fix both problems here.. */
959 char *temp
= bfd_ardata (abfd
)->extended_names
;
960 char *limit
= temp
+ namedata
->parsed_size
;
961 for (; temp
< limit
; ++temp
)
963 temp
[temp
[-1] == '/' ? -1 : 0] = '\0';
966 /* Pad to an even boundary if you have to */
967 bfd_ardata (abfd
)->first_file_filepos
= bfd_tell (abfd
);
968 bfd_ardata (abfd
)->first_file_filepos
+=
969 (bfd_ardata (abfd
)->first_file_filepos
) %2;
971 /* FIXME, we can't release namedata here because it was allocated
972 below extended_names on the obstack... */
973 /* bfd_release (abfd, namedata); */
980 /* Return a copy of the stuff in the filename between any :]> and a
983 DEFUN(normalize
,(file
),
990 first
= file
+ strlen(file
)-1;
993 while (first
!= file
)
997 if (*first
== ':' || *first
== ']' ||*first
== '>')
1006 copy
= bfd_xmalloc(last
- first
+ 1);
1007 memcpy(copy
, first
, last
-first
);
1008 copy
[last
-first
] = 0;
1015 DEFUN (normalize
, (file
),
1018 CONST
char * filename
= strrchr(file
, '/');
1020 if (filename
!= (char *)NULL
) {
1029 /* Follows archive_head and produces an extended name table if necessary.
1030 Returns (in tabloc) a pointer to an extended name table, and in tablen
1031 the length of the table. If it makes an entry it clobbers the filename
1032 so that the element may be written without further massage.
1033 Returns true if it ran successfully, false if something went wrong.
1034 A successful return may still involve a zero-length tablen!
1037 DEFUN (bfd_construct_extended_name_table
, (abfd
, tabloc
, tablen
),
1040 unsigned int *tablen
)
1042 unsigned int maxname
= abfd
->xvec
->ar_max_namelen
;
1043 unsigned int total_namelen
= 0;
1049 /* Figure out how long the table should be */
1050 for (current
= abfd
->archive_head
; current
!= NULL
; current
= current
->next
){
1051 unsigned int thislen
= strlen (normalize(current
->filename
));
1052 if (thislen
> maxname
) total_namelen
+= thislen
+ 1; /* leave room for \n */
1055 if (total_namelen
== 0) return true;
1057 *tabloc
= bfd_zalloc (abfd
,total_namelen
);
1058 if (*tabloc
== NULL
) {
1059 bfd_error
= no_memory
;
1063 *tablen
= total_namelen
;
1066 for (current
= abfd
->archive_head
; current
!= NULL
; current
=
1068 CONST
char *normal
=normalize( current
->filename
);
1069 unsigned int thislen
= strlen (normal
);
1070 if (thislen
> maxname
) {
1071 /* Works for now; may need to be re-engineered if we encounter an oddball
1072 archive format and want to generalise this hack. */
1073 struct ar_hdr
*hdr
= arch_hdr(current
);
1074 strcpy (strptr
, normal
);
1075 strptr
[thislen
] = '\n';
1076 hdr
->ar_name
[0] = ' ';
1077 /* We know there will always be enough room (one of the few cases
1078 where you may safely use sprintf). */
1079 sprintf ((hdr
->ar_name
) + 1, "%-d", (unsigned) (strptr
- *tabloc
));
1080 /* Kinda Kludgy. We should just use the returned value of sprintf
1081 but not all implementations get this right */
1083 char *temp
= hdr
->ar_name
+2;
1084 for (; temp
< hdr
->ar_name
+ maxname
; temp
++)
1085 if (*temp
== '\0') *temp
= ' ';
1087 strptr
+= thislen
+ 1;
1094 /** A couple of functions for creating ar_hdrs */
1096 /* Takes a filename, returns an arelt_data for it, or NULL if it can't make one.
1097 The filename must refer to a filename in the filesystem.
1098 The filename field of the ar_hdr will NOT be initialized
1102 DEFUN(bfd_ar_hdr_from_filesystem
, (abfd
,filename
),
1104 CONST
char *filename
)
1107 struct areltdata
*ared
;
1112 if (stat (filename
, &status
) != 0) {
1113 bfd_error
= system_call_error
;
1117 ared
= (struct areltdata
*) bfd_zalloc(abfd
, sizeof (struct ar_hdr
) +
1118 sizeof (struct areltdata
));
1120 bfd_error
= no_memory
;
1123 hdr
= (struct ar_hdr
*) (((char *) ared
) + sizeof (struct areltdata
));
1125 /* ar headers are space padded, not null padded! */
1126 temp
= (char *) hdr
;
1127 temp1
= temp
+ sizeof (struct ar_hdr
) - 2;
1128 for (; temp
< temp1
; *(temp
++) = ' ');
1129 strncpy (hdr
->ar_fmag
, ARFMAG
, 2);
1131 /* Goddamned sprintf doesn't permit MAXIMUM field lengths */
1132 sprintf ((hdr
->ar_date
), "%-12ld", status
.st_mtime
);
1133 sprintf ((hdr
->ar_uid
), "%d", status
.st_uid
);
1134 sprintf ((hdr
->ar_gid
), "%d", status
.st_gid
);
1135 sprintf ((hdr
->ar_mode
), "%-8o", (unsigned) status
.st_mode
);
1136 sprintf ((hdr
->ar_size
), "%-10ld", status
.st_size
);
1137 /* Correct for a lossage in sprintf whereby it null-terminates. I cannot
1138 understand how these C losers could design such a ramshackle bunch of
1140 temp
= (char *) hdr
;
1141 temp1
= temp
+ sizeof (struct ar_hdr
) - 2;
1142 for (; temp
< temp1
; temp
++) {
1143 if (*temp
== '\0') *temp
= ' ';
1145 strncpy (hdr
->ar_fmag
, ARFMAG
, 2);
1146 ared
->parsed_size
= status
.st_size
;
1147 ared
->arch_header
= (char *) hdr
;
1152 /* This is magic required by the "ar" program. Since it's
1153 undocumented, it's undocumented. You may think that it would
1154 take a strong stomach to write this, and it does, but it takes
1155 even a stronger stomach to try to code around such a thing!
1159 DEFUN(bfd_special_undocumented_glue
, (abfd
, filename
),
1163 struct areltdata
*ar_elt
= bfd_ar_hdr_from_filesystem (abfd
, filename
);
1166 return (struct ar_hdr
*) ar_elt
->arch_header
;
1170 /* Analogous to stat call */
1172 bfd_generic_stat_arch_elt (abfd
, buf
)
1179 if (abfd
->arelt_data
== NULL
) {
1180 bfd_error
= invalid_operation
;
1184 hdr
= arch_hdr (abfd
);
1186 #define foo(arelt, stelt, size) \
1187 buf->stelt = strtol (hdr->arelt, &aloser, size); \
1188 if (aloser == hdr->arelt) return -1;
1190 foo (ar_date
, st_mtime
, 10);
1191 foo (ar_uid
, st_uid
, 10);
1192 foo (ar_gid
, st_gid
, 10);
1193 foo (ar_mode
, st_mode
, 8);
1195 buf
->st_size
= arch_eltdata (abfd
)->parsed_size
;
1201 bfd_dont_truncate_arname (abfd
, pathname
, arhdr
)
1203 CONST
char *pathname
;
1206 /* FIXME: This interacts unpleasantly with ar's quick-append option.
1207 Fortunately ic960 users will never use that option. Fixing this
1208 is very hard; fortunately I know how to do it and will do so once
1209 intel's release is out the door. */
1211 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
1213 CONST
char *filename
= normalize(pathname
);
1214 int maxlen
= ar_maxnamelen (abfd
);
1216 length
= strlen (filename
);
1218 if (length
<= maxlen
)
1219 memcpy (hdr
->ar_name
, filename
, length
);
1221 if (length
< maxlen
) (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
1227 bfd_bsd_truncate_arname (abfd
, pathname
, arhdr
)
1229 CONST
char *pathname
;
1232 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
1234 CONST
char *filename
= strrchr (pathname
, '/');
1235 int maxlen
= ar_maxnamelen (abfd
);
1238 if (filename
== NULL
)
1239 filename
= pathname
;
1243 length
= strlen (filename
);
1245 if (length
<= maxlen
)
1246 memcpy (hdr
->ar_name
, filename
, length
);
1248 /* pathname: meet procrustes */
1249 memcpy (hdr
->ar_name
, filename
, maxlen
);
1253 if (length
< maxlen
) (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
1256 /* Store name into ar header. Truncates the name to fit.
1257 1> strip pathname to be just the basename.
1258 2> if it's short enuf to fit, stuff it in.
1259 3> If it doesn't end with .o, truncate it to fit
1260 4> truncate it before the .o, append .o, stuff THAT in.
1263 /* This is what gnu ar does. It's better but incompatible with the bsd ar. */
1265 bfd_gnu_truncate_arname (abfd
, pathname
, arhdr
)
1267 CONST
char *pathname
;
1270 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
1272 CONST
char *filename
= strrchr (pathname
, '/');
1273 int maxlen
= ar_maxnamelen (abfd
);
1275 if (filename
== NULL
)
1276 filename
= pathname
;
1280 length
= strlen (filename
);
1282 if (length
<= maxlen
)
1283 memcpy (hdr
->ar_name
, filename
, length
);
1284 else { /* pathname: meet procrustes */
1285 memcpy (hdr
->ar_name
, filename
, maxlen
);
1286 if ((filename
[length
- 2] == '.') && (filename
[length
- 1] == 'o')) {
1287 hdr
->ar_name
[maxlen
- 2] = '.';
1288 hdr
->ar_name
[maxlen
- 1] = 'o';
1293 if (length
< 16) (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
1297 /* The BFD is open for write and has its format set to bfd_archive */
1299 _bfd_write_archive_contents (arch
)
1303 char *etable
= NULL
;
1304 unsigned int elength
= 0;
1305 boolean makemap
= bfd_has_map (arch
);
1306 boolean hasobjects
= false; /* if no .o's, don't bother to make a map */
1310 /* Verify the viability of all entries; if any of them live in the
1311 filesystem (as opposed to living in an archive open for input)
1312 then construct a fresh ar_hdr for them.
1314 for (current
= arch
->archive_head
; current
; current
= current
->next
) {
1315 if (bfd_write_p (current
)) {
1316 bfd_error
= invalid_operation
;
1319 if (!current
->arelt_data
) {
1320 current
->arelt_data
=
1321 (PTR
) bfd_ar_hdr_from_filesystem (arch
, current
->filename
);
1322 if (!current
->arelt_data
) return false;
1324 /* Put in the file name */
1326 BFD_SEND (arch
, _bfd_truncate_arname
,(arch
,
1328 (char *) arch_hdr(current
)));
1333 if (makemap
) { /* don't bother if we won't make a map! */
1334 if ((bfd_check_format (current
, bfd_object
))
1335 #if 0 /* FIXME -- these are not set correctly */
1336 && ((bfd_get_file_flags (current
) & HAS_SYMS
))
1343 if (!bfd_construct_extended_name_table (arch
, &etable
, &elength
))
1346 bfd_seek (arch
, (file_ptr
) 0, SEEK_SET
);
1348 bfd_write (BFD_GNU960_ARMAG(arch
), 1, SARMAG
, arch
);
1350 bfd_write (ARMAG
, 1, SARMAG
, arch
);
1353 if (makemap
&& hasobjects
) {
1355 if (compute_and_write_armap (arch
, elength
) != true) {
1363 memset ((char *)(&hdr
), 0, sizeof (struct ar_hdr
));
1364 sprintf (&(hdr
.ar_name
[0]), "ARFILENAMES/");
1365 sprintf (&(hdr
.ar_size
[0]), "%-10d", (int) elength
);
1366 hdr
.ar_fmag
[0] = '`'; hdr
.ar_fmag
[1] = '\n';
1367 for (i
= 0; i
< sizeof (struct ar_hdr
); i
++)
1368 if (((char *)(&hdr
))[i
] == '\0') (((char *)(&hdr
))[i
]) = ' ';
1369 bfd_write ((char *)&hdr
, 1, sizeof (struct ar_hdr
), arch
);
1370 bfd_write (etable
, 1, elength
, arch
);
1371 if ((elength
% 2) == 1) bfd_write ("\n", 1, 1, arch
);
1375 for (current
= arch
->archive_head
; current
; current
= current
->next
) {
1376 char buffer
[DEFAULT_BUFFERSIZE
];
1377 unsigned int remaining
= arelt_size (current
);
1378 struct ar_hdr
*hdr
= arch_hdr(current
);
1379 /* write ar header */
1381 if (bfd_write ((char *)hdr
, 1, sizeof(*hdr
), arch
) != sizeof(*hdr
)) {
1383 bfd_error
= system_call_error
;
1386 if (bfd_seek (current
, (file_ptr
) 0, SEEK_SET
) != 0) goto syserr
;
1389 unsigned int amt
= DEFAULT_BUFFERSIZE
;
1390 if (amt
> remaining
) {
1394 if (bfd_read (buffer
, amt
, 1, current
) != amt
) {
1395 if (errno
) goto syserr
;
1396 /* Looks like a truncated archive. */
1397 bfd_error
= malformed_archive
;
1400 if (bfd_write (buffer
, amt
, 1, arch
) != amt
) goto syserr
;
1403 if ((arelt_size (current
) % 2) == 1) bfd_write ("\n", 1, 1, arch
);
1406 /* Verify the timestamp in the archive file. If it would
1407 not be accepted by the linker, rewrite it until it would be.
1408 If anything odd happens, break out and just return.
1409 (The Berkeley linker checks the timestamp and refuses to read the
1410 table-of-contents if it is >60 seconds less than the file's
1411 modified-time. That painful hack requires this painful hack. */
1415 /* FIXME! This kludge is to avoid adding a member to the xvec,
1416 while generating a small patch for Adobe. FIXME! The
1417 update_armap_timestamp function call should be in the xvec,
1420 if (bfd_update_armap_timestamp (arch) == true) break;
1423 Instead, we check whether in a BSD archive, and call directly. */
1425 if (arch
->xvec
->write_armap
!= bsd_write_armap
)
1427 if (bsd_update_armap_timestamp(arch
) == true) /* FIXME!!! Vector it */
1431 "Warning: writing archive was slow: rewriting timestamp\n");
1432 } while (++tries
< 6 );
1437 /* Note that the namidx for the first symbol is 0 */
1440 compute_and_write_armap (arch
, elength
)
1442 unsigned int elength
;
1445 file_ptr elt_no
= 0;
1447 int orl_max
= 15000; /* fine initial default */
1449 int stridx
= 0; /* string index */
1451 /* Dunno if this is the best place for this info... */
1452 if (elength
!= 0) elength
+= sizeof (struct ar_hdr
);
1453 elength
+= elength
%2 ;
1455 map
= (struct orl
*) bfd_zalloc (arch
,orl_max
* sizeof (struct orl
));
1457 bfd_error
= no_memory
;
1461 /* Drop all the files called __.SYMDEF, we're going to make our
1463 while (arch
->archive_head
&&
1464 strcmp(arch
->archive_head
->filename
,"__.SYMDEF") == 0)
1466 arch
->archive_head
= arch
->archive_head
->next
;
1468 /* Map over each element */
1469 for (current
= arch
->archive_head
;
1470 current
!= (bfd
*)NULL
;
1471 current
= current
->next
, elt_no
++)
1473 if ((bfd_check_format (current
, bfd_object
) == true)
1474 && ((bfd_get_file_flags (current
) & HAS_SYMS
))) {
1476 unsigned int storage
;
1477 unsigned int symcount
;
1478 unsigned int src_count
;
1480 storage
= get_symtab_upper_bound (current
);
1483 syms
= (asymbol
**) bfd_zalloc (arch
,storage
);
1485 bfd_error
= no_memory
; /* FIXME -- memory leak */
1488 symcount
= bfd_canonicalize_symtab (current
, syms
);
1491 /* Now map over all the symbols, picking out the ones we want */
1492 for (src_count
= 0; src_count
<symcount
; src_count
++) {
1494 (syms
[src_count
])->flags
;
1496 syms
[src_count
]->section
;
1498 if ((flags
& BSF_GLOBAL
||
1499 flags
& BSF_INDIRECT
||
1500 bfd_is_com_section (sec
))
1501 && (sec
!= &bfd_und_section
)) {
1503 /* This symbol will go into the archive header */
1504 if (orl_count
== orl_max
)
1507 map
= (struct orl
*) bfd_realloc (arch
, (char *) map
,
1508 orl_max
* sizeof (struct orl
));
1511 (map
[orl_count
]).name
= (char **) &((syms
[src_count
])->name
);
1512 (map
[orl_count
]).pos
= (file_ptr
) current
;
1513 (map
[orl_count
]).namidx
= stridx
;
1515 stridx
+= strlen ((syms
[src_count
])->name
) + 1;
1522 /* OK, now we have collected all the data, let's write them out */
1523 if (!BFD_SEND (arch
, write_armap
,
1524 (arch
, elength
, map
, orl_count
, stridx
))) {
1534 bsd_write_armap (arch
, elength
, map
, orl_count
, stridx
)
1536 unsigned int elength
;
1538 unsigned int orl_count
;
1541 int padit
= stridx
& 1;
1542 unsigned int ranlibsize
= orl_count
* sizeof (struct ranlib
);
1543 unsigned int stringsize
= stridx
+ padit
;
1544 /* Include 8 bytes to store ranlibsize and stringsize in output. */
1545 unsigned int mapsize
= ranlibsize
+ stringsize
+ 8;
1547 bfd
*current
= arch
->archive_head
;
1548 bfd
*last_elt
= current
; /* last element arch seen */
1552 struct stat statbuf
;
1555 firstreal
= mapsize
+ elength
+ sizeof (struct ar_hdr
) + SARMAG
;
1557 stat (arch
->filename
, &statbuf
);
1558 memset ((char *)(&hdr
), 0, sizeof (struct ar_hdr
));
1559 sprintf (hdr
.ar_name
, RANLIBMAG
);
1560 /* Remember the timestamp, to keep it holy. But fudge it a little. */
1561 bfd_ardata(arch
)->armap_timestamp
= statbuf
.st_mtime
+ ARMAP_TIME_OFFSET
;
1562 bfd_ardata(arch
)->armap_datepos
= SARMAG
+ offsetof(struct ar_hdr
, ar_date
);
1563 sprintf (hdr
.ar_date
, "%ld", bfd_ardata(arch
)->armap_timestamp
);
1564 sprintf (hdr
.ar_uid
, "%d", getuid());
1565 sprintf (hdr
.ar_gid
, "%d", getgid());
1566 sprintf (hdr
.ar_size
, "%-10d", (int) mapsize
);
1567 hdr
.ar_fmag
[0] = '`'; hdr
.ar_fmag
[1] = '\n';
1568 for (i
= 0; i
< sizeof (struct ar_hdr
); i
++)
1569 if (((char *)(&hdr
))[i
] == '\0') (((char *)(&hdr
))[i
]) = ' ';
1570 bfd_write ((char *)&hdr
, 1, sizeof (struct ar_hdr
), arch
);
1571 bfd_h_put_32(arch
, (bfd_vma
) ranlibsize
, (PTR
)&temp
);
1572 bfd_write (&temp
, 1, sizeof (temp
), arch
);
1574 for (count
= 0; count
< orl_count
; count
++) {
1576 struct symdef
*outp
= &outs
;
1578 if (((bfd
*)(map
[count
]).pos
) != last_elt
) {
1580 firstreal
+= arelt_size (current
) + sizeof (struct ar_hdr
);
1581 firstreal
+= firstreal
% 2;
1582 current
= current
->next
;
1583 } while (current
!= (bfd
*)(map
[count
]).pos
);
1584 } /* if new archive element */
1587 bfd_h_put_32(arch
, ((map
[count
]).namidx
),(PTR
) &outs
.s
.string_offset
);
1588 bfd_h_put_32(arch
, firstreal
,(PTR
) &outs
.file_offset
);
1589 bfd_write ((char *)outp
, 1, sizeof (outs
), arch
);
1592 /* now write the strings themselves */
1593 bfd_h_put_32(arch
, stringsize
, (PTR
)&temp
);
1594 bfd_write ((PTR
)&temp
, 1, sizeof (temp
), arch
);
1595 for (count
= 0; count
< orl_count
; count
++)
1596 bfd_write (*((map
[count
]).name
), 1, strlen (*((map
[count
]).name
))+1, arch
);
1598 /* The spec sez this should be a newline. But in order to be
1599 bug-compatible for sun's ar we use a null. */
1601 bfd_write("\0",1,1,arch
);
1607 /* At the end of archive file handling, update the timestamp in the
1608 file, so the linker will accept it.
1610 Return true if the timestamp was OK, or an unusual problem happened.
1611 Return false if we updated the timestamp. */
1614 bsd_update_armap_timestamp (arch
)
1617 struct stat archstat
;
1621 /* Flush writes, get last-write timestamp from file, and compare it
1622 to the timestamp IN the file. */
1624 if (bfd_stat (arch
, &archstat
) == -1) {
1625 perror ("Reading archive file mod timestamp");
1626 return true; /* Can't read mod time for some reason */
1628 if (archstat
.st_mtime
<= bfd_ardata(arch
)->armap_timestamp
)
1629 return true; /* OK by the linker's rules */
1631 /* Update the timestamp. */
1632 bfd_ardata(arch
)->armap_timestamp
= archstat
.st_mtime
+ ARMAP_TIME_OFFSET
;
1634 /* Prepare an ASCII version suitable for writing. */
1635 memset (hdr
.ar_date
, 0, sizeof (hdr
.ar_date
));
1636 sprintf (hdr
.ar_date
, "%ld", bfd_ardata(arch
)->armap_timestamp
);
1637 for (i
= 0; i
< sizeof (hdr
.ar_date
); i
++)
1638 if (hdr
.ar_date
[i
] == '\0')
1639 (hdr
.ar_date
)[i
] = ' ';
1641 /* Write it into the file. */
1642 bfd_seek (arch
, bfd_ardata(arch
)->armap_datepos
, SEEK_SET
);
1643 if (bfd_write (hdr
.ar_date
, sizeof(hdr
.ar_date
), 1, arch
)
1644 != sizeof(hdr
.ar_date
)) {
1645 perror ("Writing updated armap timestamp");
1646 return true; /* Some error while writing */
1649 return false; /* We updated the timestamp successfully. */
1653 /* A coff armap looks like :
1655 struct ar_hdr with name = '/'
1657 offset of file for symbol 0
1658 offset of file for symbol 1
1660 offset of file for symbol n-1
1669 coff_write_armap (arch
, elength
, map
, symbol_count
, stridx
)
1671 unsigned int elength
;
1673 unsigned int symbol_count
;
1676 /* The size of the ranlib is the number of exported symbols in the
1677 archive * the number of bytes in a int, + an int for the count */
1679 unsigned int ranlibsize
= (symbol_count
* 4) + 4;
1680 unsigned int stringsize
= stridx
;
1681 unsigned int mapsize
= stringsize
+ ranlibsize
;
1682 file_ptr archive_member_file_ptr
;
1683 bfd
*current
= arch
->archive_head
;
1687 int padit
= mapsize
& 1;
1689 if (padit
) mapsize
++;
1691 /* work out where the first object file will go in the archive */
1692 archive_member_file_ptr
= mapsize
+ elength
+ sizeof (struct ar_hdr
) + SARMAG
;
1694 memset ((char *)(&hdr
), 0, sizeof (struct ar_hdr
));
1695 hdr
.ar_name
[0] = '/';
1696 sprintf (hdr
.ar_size
, "%-10d", (int) mapsize
);
1697 sprintf (hdr
.ar_date
, "%ld", (long)time (NULL
));
1698 /* This, at least, is what Intel coff sets the values to.: */
1699 sprintf ((hdr
.ar_uid
), "%d", 0);
1700 sprintf ((hdr
.ar_gid
), "%d", 0);
1701 sprintf ((hdr
.ar_mode
), "%-7o",(unsigned ) 0);
1702 hdr
.ar_fmag
[0] = '`'; hdr
.ar_fmag
[1] = '\n';
1704 for (i
= 0; i
< sizeof (struct ar_hdr
); i
++)
1705 if (((char *)(&hdr
))[i
] == '\0') (((char *)(&hdr
))[i
]) = ' ';
1707 /* Write the ar header for this item and the number of symbols */
1710 bfd_write ((PTR
)&hdr
, 1, sizeof (struct ar_hdr
), arch
);
1712 bfd_write_bigendian_4byte_int(arch
, symbol_count
);
1714 /* Two passes, first write the file offsets for each symbol -
1715 remembering that each offset is on a two byte boundary. */
1717 /* Write out the file offset for the file associated with each
1718 symbol, and remember to keep the offsets padded out. */
1720 current
= arch
->archive_head
;
1722 while (current
!= (bfd
*)NULL
&& count
< symbol_count
) {
1723 /* For each symbol which is used defined in this object, write out
1724 the object file's address in the archive */
1726 while (((bfd
*)(map
[count
]).pos
) == current
) {
1727 bfd_write_bigendian_4byte_int(arch
, archive_member_file_ptr
);
1730 /* Add size of this archive entry */
1731 archive_member_file_ptr
+= arelt_size (current
) + sizeof (struct
1733 /* remember aboout the even alignment */
1734 archive_member_file_ptr
+= archive_member_file_ptr
% 2;
1735 current
= current
->next
;
1740 /* now write the strings themselves */
1741 for (count
= 0; count
< symbol_count
; count
++) {
1742 bfd_write ((PTR
)*((map
[count
]).name
),
1744 strlen (*((map
[count
]).name
))+1, arch
);
1747 /* The spec sez this should be a newline. But in order to be
1748 bug-compatible for arc960 we use a null. */
1750 bfd_write("\0",1,1,arch
);