1 /* BFD back-end for archive files (libraries).
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 Free Software Foundation, Inc.
5 Written by Cygnus Support. Mostly Gumby Henkel-Wallace's fault.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 @setfilename archive-info
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 that are considered its contents. These BFDs can
35 be manipulated 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; they will be handled correctly when the archive is closed.
40 Use <<bfd_openr_next_archived_file>> to step through
41 the contents of an archive opened for input. You don't
42 have to 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 <<bfd_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; SunOS 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 case due to restrictions 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
75 Archives are supported in BFD in <<archive.c>>.
80 o - all archive elements start on an even boundary, newline padded;
81 o - all arch headers are char *;
82 o - all arch headers are the same size (across architectures).
85 /* Some formats provide a way to cram a long filename into the short
86 (16 chars) space provided by a BSD archive. The trick is: make a
87 special "file" in the front of the archive, sort of like the SYMDEF
88 entry. If the filename is too long to fit, put it in the extended
89 name table, and use its index as the filename. To prevent
90 confusion prepend the index with a space. This means you can't
91 have filenames that start with a space, but then again, many Unix
92 utilities can't handle that anyway.
94 This scheme unfortunately requires that you stand on your head in
95 order to write an archive since you need to put a magic file at the
96 front, and need to touch every entry to do so. C'est la vie.
98 We support two variants of this idea:
99 The SVR4 format (extended name table is named "//"),
100 and an extended pseudo-BSD variant (extended name table is named
101 "ARFILENAMES/"). The origin of the latter format is uncertain.
103 BSD 4.4 uses a third scheme: It writes a long filename
104 directly after the header. This allows 'ar q' to work.
105 We currently can read BSD 4.4 archives, but not write them.
108 /* Summary of archive member names:
110 Symbol table (must be first):
111 "__.SYMDEF " - Symbol table, Berkeley style, produced by ranlib.
112 "/ " - Symbol table, system 5 style.
114 Long name table (must be before regular file members):
115 "// " - Long name table, System 5 R4 style.
116 "ARFILENAMES/ " - Long name table, non-standard extended BSD (not BSD 4.4).
118 Regular file members with short names:
119 "filename.o/ " - Regular file, System 5 style (embedded spaces ok).
120 "filename.o " - Regular file, Berkeley style (no embedded spaces).
122 Regular files with long names (or embedded spaces, for BSD variants):
123 "/18 " - SVR4 style, name at offset 18 in name table.
124 "#1/23 " - Long name (or embedded paces) 23 characters long,
125 BSD 4.4 style, full name follows header.
126 Implemented for reading, not writing.
127 " 18 " - Long name 18 characters long, extended pseudo-BSD.
134 #include "aout/ranlib.h"
135 #include "safe-ctype.h"
142 #define BFD_GNU960_ARMAG(abfd) (BFD_COFF_FILE_P((abfd)) ? ARMAG : ARMAGB)
145 /* We keep a cache of archive filepointers to archive elements to
146 speed up searching the archive by filepos. We only add an entry to
147 the cache when we actually read one. We also don't sort the cache;
148 it's generally short enough to search linearly.
149 Note that the pointers here point to the front of the ar_hdr, not
150 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 static char *get_extended_arelt_filename
PARAMS ((bfd
*arch
,
165 static boolean do_slurp_bsd_armap
PARAMS ((bfd
*abfd
));
166 static boolean do_slurp_coff_armap
PARAMS ((bfd
*abfd
));
167 boolean bfd_elf64_archive_slurp_armap
PARAMS ((bfd
*abfd
));
168 static const char *normalize
PARAMS ((bfd
*, const char *file
));
169 static struct areltdata
*bfd_ar_hdr_from_filesystem
PARAMS ((bfd
*abfd
,
174 _bfd_generic_mkarchive (abfd
)
177 bfd_size_type amt
= sizeof (struct artdata
);
179 abfd
->tdata
.aout_ar_data
= (struct artdata
*) bfd_zalloc (abfd
, amt
);
180 if (bfd_ardata (abfd
) == NULL
)
183 bfd_ardata (abfd
)->cache
= NULL
;
184 bfd_ardata (abfd
)->archive_head
= NULL
;
185 bfd_ardata (abfd
)->symdefs
= NULL
;
186 bfd_ardata (abfd
)->extended_names
= NULL
;
187 bfd_ardata (abfd
)->tdata
= NULL
;
197 symindex bfd_get_next_mapent(bfd *abfd, symindex previous, carsym **sym);
200 Step through archive @var{abfd}'s symbol table (if it
201 has one). Successively update @var{sym} with the next symbol's
202 information, returning that symbol's (internal) index into the
205 Supply <<BFD_NO_MORE_SYMBOLS>> as the @var{previous} entry to get
206 the first one; returns <<BFD_NO_MORE_SYMBOLS>> when you've already
209 A <<carsym>> is a canonical archive symbol. The only
210 user-visible element is its name, a null-terminated string.
214 bfd_get_next_mapent (abfd
, prev
, entry
)
219 if (!bfd_has_map (abfd
))
221 bfd_set_error (bfd_error_invalid_operation
);
222 return BFD_NO_MORE_SYMBOLS
;
225 if (prev
== BFD_NO_MORE_SYMBOLS
)
229 if (prev
>= bfd_ardata (abfd
)->symdef_count
)
230 return BFD_NO_MORE_SYMBOLS
;
232 *entry
= (bfd_ardata (abfd
)->symdefs
+ prev
);
236 /* To be called by backends only */
239 _bfd_create_empty_archive_element_shell (obfd
)
242 return _bfd_new_bfd_contained_in (obfd
);
250 boolean bfd_set_archive_head(bfd *output, bfd *new_head);
253 Set the head of the chain of
254 BFDs contained in the archive @var{output} to @var{new_head}.
258 bfd_set_archive_head (output_archive
, new_head
)
263 output_archive
->archive_head
= new_head
;
268 _bfd_look_for_bfd_in_cache (arch_bfd
, filepos
)
272 struct ar_cache
*current
;
274 for (current
= bfd_ardata (arch_bfd
)->cache
; current
!= NULL
;
275 current
= current
->next
)
276 if (current
->ptr
== filepos
)
277 return current
->arelt
;
282 /* Kind of stupid to call cons for each one, but we don't do too many */
284 _bfd_add_bfd_to_archive_cache (arch_bfd
, filepos
, new_elt
)
285 bfd
*arch_bfd
, *new_elt
;
288 bfd_size_type amt
= sizeof (struct ar_cache
);
290 struct ar_cache
*new_cache
= (struct ar_cache
*) bfd_zalloc (arch_bfd
, amt
);
291 if (new_cache
== NULL
)
294 new_cache
->ptr
= filepos
;
295 new_cache
->arelt
= new_elt
;
296 new_cache
->next
= (struct ar_cache
*) NULL
;
297 if (bfd_ardata (arch_bfd
)->cache
== NULL
)
298 bfd_ardata (arch_bfd
)->cache
= new_cache
;
301 struct ar_cache
*current
= bfd_ardata (arch_bfd
)->cache
;
303 while (current
->next
!= NULL
)
304 current
= current
->next
;
305 current
->next
= new_cache
;
311 /* The name begins with space. Hence the rest of the name is an index into
315 get_extended_arelt_filename (arch
, name
)
319 unsigned long index
= 0;
321 /* Should extract string so that I can guarantee not to overflow into
322 the next region, but I'm too lazy. */
324 /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */
325 index
= strtol (name
+ 1, NULL
, 10);
328 bfd_set_error (bfd_error_malformed_archive
);
332 return bfd_ardata (arch
)->extended_names
+ index
;
335 /* This functions reads an arch header and returns an areltdata pointer, or
338 Presumes the file pointer is already in the right place (ie pointing
339 to the ar_hdr in the file). Moves the file pointer; on success it
340 should be pointing to the front of the file contents; on failure it
341 could have been moved arbitrarily.
345 _bfd_generic_read_ar_hdr (abfd
)
348 return _bfd_generic_read_ar_hdr_mag (abfd
, (const char *) NULL
);
351 /* Alpha ECOFF uses an optional different ARFMAG value, so we have a
352 variant of _bfd_generic_read_ar_hdr which accepts a magic string. */
355 _bfd_generic_read_ar_hdr_mag (abfd
, mag
)
360 char *hdrp
= (char *) &hdr
;
362 struct areltdata
*ared
;
363 char *filename
= NULL
;
364 bfd_size_type namelen
= 0;
365 bfd_size_type allocsize
= sizeof (struct areltdata
) + sizeof (struct ar_hdr
);
368 if (bfd_bread ((PTR
) hdrp
, (bfd_size_type
) sizeof (struct ar_hdr
), abfd
)
369 != sizeof (struct ar_hdr
))
371 if (bfd_get_error () != bfd_error_system_call
)
372 bfd_set_error (bfd_error_no_more_archived_files
);
375 if (strncmp (hdr
.ar_fmag
, ARFMAG
, 2) != 0
377 || strncmp (hdr
.ar_fmag
, mag
, 2) != 0))
379 bfd_set_error (bfd_error_malformed_archive
);
384 parsed_size
= strtol (hdr
.ar_size
, NULL
, 10);
387 bfd_set_error (bfd_error_malformed_archive
);
391 /* Extract the filename from the archive - there are two ways to
392 specify an extended name table, either the first char of the
393 name is a space, or it's a slash. */
394 if ((hdr
.ar_name
[0] == '/'
395 || (hdr
.ar_name
[0] == ' '
396 && memchr (hdr
.ar_name
, '/', ar_maxnamelen (abfd
)) == NULL
))
397 && bfd_ardata (abfd
)->extended_names
!= NULL
)
399 filename
= get_extended_arelt_filename (abfd
, hdr
.ar_name
);
400 if (filename
== NULL
)
402 bfd_set_error (bfd_error_malformed_archive
);
406 /* BSD4.4-style long filename.
407 Only implemented for reading, so far! */
408 else if (hdr
.ar_name
[0] == '#'
409 && hdr
.ar_name
[1] == '1'
410 && hdr
.ar_name
[2] == '/'
411 && ISDIGIT (hdr
.ar_name
[3]))
413 /* BSD-4.4 extended name */
414 namelen
= atoi (&hdr
.ar_name
[3]);
415 allocsize
+= namelen
+ 1;
416 parsed_size
-= namelen
;
418 allocptr
= bfd_zalloc (abfd
, allocsize
);
419 if (allocptr
== NULL
)
422 + sizeof (struct areltdata
)
423 + sizeof (struct ar_hdr
));
424 if (bfd_bread (filename
, namelen
, abfd
) != namelen
)
426 if (bfd_get_error () != bfd_error_system_call
)
427 bfd_set_error (bfd_error_no_more_archived_files
);
430 filename
[namelen
] = '\0';
434 /* We judge the end of the name by looking for '/' or ' '.
435 Note: The SYSV format (terminated by '/') allows embedded
436 spaces, so only look for ' ' if we don't find '/'. */
439 e
= memchr (hdr
.ar_name
, '\0', ar_maxnamelen (abfd
));
442 e
= memchr (hdr
.ar_name
, '/', ar_maxnamelen (abfd
));
444 e
= memchr (hdr
.ar_name
, ' ', ar_maxnamelen (abfd
));
448 namelen
= e
- hdr
.ar_name
;
451 /* If we didn't find a termination character, then the name
452 must be the entire field. */
453 namelen
= ar_maxnamelen (abfd
);
456 allocsize
+= namelen
+ 1;
461 allocptr
= bfd_zalloc (abfd
, allocsize
);
462 if (allocptr
== NULL
)
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
)
473 ared
->filename
= filename
;
476 ared
->filename
= allocptr
+ (sizeof (struct areltdata
) +
477 sizeof (struct ar_hdr
));
479 memcpy (ared
->filename
, hdr
.ar_name
, (size_t) namelen
);
480 ared
->filename
[namelen
] = '\0';
486 /* This is an internal function; it's mainly used when indexing
487 through the archive symbol table, but also used to get the next
488 element, since it handles the bookkeeping so nicely for us. */
491 _bfd_get_elt_at_filepos (archive
, filepos
)
495 struct areltdata
*new_areldata
;
498 n_nfd
= _bfd_look_for_bfd_in_cache (archive
, filepos
);
502 if (0 > bfd_seek (archive
, filepos
, SEEK_SET
))
505 if ((new_areldata
= (struct areltdata
*) _bfd_read_ar_hdr (archive
)) == NULL
)
508 n_nfd
= _bfd_create_empty_archive_element_shell (archive
);
511 bfd_release (archive
, (PTR
) new_areldata
);
515 n_nfd
->origin
= bfd_tell (archive
);
516 n_nfd
->arelt_data
= (PTR
) new_areldata
;
517 n_nfd
->filename
= new_areldata
->filename
;
519 if (_bfd_add_bfd_to_archive_cache (archive
, filepos
, n_nfd
))
523 bfd_release (archive
, (PTR
) n_nfd
);
524 bfd_release (archive
, (PTR
) new_areldata
);
528 /* Return the BFD which is referenced by the symbol in ABFD indexed by
529 INDEX. INDEX should have been returned by bfd_get_next_mapent. */
532 _bfd_generic_get_elt_at_index (abfd
, index
)
538 entry
= bfd_ardata (abfd
)->symdefs
+ index
;
539 return _bfd_get_elt_at_filepos (abfd
, entry
->file_offset
);
544 bfd_openr_next_archived_file
547 bfd *bfd_openr_next_archived_file(bfd *archive, bfd *previous);
550 Provided a BFD, @var{archive}, containing an archive and NULL, open
551 an input BFD on the first contained element and returns that.
552 Subsequent calls should pass
553 the archive and the previous return value to return a created
554 BFD to the next contained element. NULL is returned when there
559 bfd_openr_next_archived_file (archive
, last_file
)
563 if ((bfd_get_format (archive
) != bfd_archive
) ||
564 (archive
->direction
== write_direction
))
566 bfd_set_error (bfd_error_invalid_operation
);
570 return BFD_SEND (archive
,
571 openr_next_archived_file
,
577 bfd_generic_openr_next_archived_file (archive
, last_file
)
584 filestart
= bfd_ardata (archive
)->first_file_filepos
;
587 unsigned int size
= arelt_size (last_file
);
588 /* Pad to an even boundary...
589 Note that last_file->origin can be odd in the case of
590 BSD-4.4-style element with a long odd size. */
591 filestart
= last_file
->origin
+ size
;
592 filestart
+= filestart
% 2;
595 return _bfd_get_elt_at_filepos (archive
, filestart
);
599 bfd_generic_archive_p (abfd
)
602 struct artdata
*tdata_hold
;
603 char armag
[SARMAG
+ 1];
606 tdata_hold
= abfd
->tdata
.aout_ar_data
;
608 if (bfd_bread ((PTR
) armag
, (bfd_size_type
) SARMAG
, abfd
) != SARMAG
)
610 if (bfd_get_error () != bfd_error_system_call
)
611 bfd_set_error (bfd_error_wrong_format
);
616 if (strncmp (armag
, BFD_GNU960_ARMAG (abfd
), SARMAG
) != 0)
619 if (strncmp (armag
, ARMAG
, SARMAG
) != 0 &&
620 strncmp (armag
, ARMAGB
, SARMAG
) != 0)
624 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
625 involves a cast, we can't do it as the left operand of assignment. */
626 amt
= sizeof (struct artdata
);
627 abfd
->tdata
.aout_ar_data
= (struct artdata
*) bfd_zalloc (abfd
, amt
);
629 if (bfd_ardata (abfd
) == NULL
)
632 bfd_ardata (abfd
)->first_file_filepos
= SARMAG
;
633 bfd_ardata (abfd
)->cache
= NULL
;
634 bfd_ardata (abfd
)->archive_head
= NULL
;
635 bfd_ardata (abfd
)->symdefs
= NULL
;
636 bfd_ardata (abfd
)->extended_names
= NULL
;
637 bfd_ardata (abfd
)->tdata
= NULL
;
639 if (!BFD_SEND (abfd
, _bfd_slurp_armap
, (abfd
)))
641 bfd_release (abfd
, bfd_ardata (abfd
));
642 abfd
->tdata
.aout_ar_data
= tdata_hold
;
643 if (bfd_get_error () != bfd_error_system_call
)
644 bfd_set_error (bfd_error_wrong_format
);
648 if (!BFD_SEND (abfd
, _bfd_slurp_extended_name_table
, (abfd
)))
650 bfd_release (abfd
, bfd_ardata (abfd
));
651 abfd
->tdata
.aout_ar_data
= tdata_hold
;
652 if (bfd_get_error () != bfd_error_system_call
)
653 bfd_set_error (bfd_error_wrong_format
);
657 if (bfd_has_map (abfd
))
661 /* This archive has a map, so we may presume that the contents
662 are object files. Make sure that if the first file in the
663 archive can be recognized as an object file, it is for this
664 target. If not, assume that this is the wrong format. If
665 the first file is not an object file, somebody is doing
666 something weird, and we permit it so that ar -t will work.
668 This is done because any normal format will recognize any
669 normal archive, regardless of the format of the object files.
670 We do accept an empty archive. */
672 first
= bfd_openr_next_archived_file (abfd
, (bfd
*) NULL
);
677 first
->target_defaulted
= false;
679 if (bfd_check_format (first
, bfd_object
)
680 && first
->xvec
!= abfd
->xvec
)
683 /* We ought to close `first' here, but we can't, because
684 we have no way to remove it from the archive cache.
685 It's close to impossible to figure out when we can
686 release bfd_ardata. FIXME. */
687 (void) bfd_close (first
);
688 bfd_release (abfd
, bfd_ardata (abfd
));
689 abfd
->tdata
.aout_ar_data
= tdata_hold
;
691 bfd_set_error (bfd_error_wrong_object_format
);
694 /* And we ought to close `first' here too. */
701 /* Some constants for a 32 bit BSD archive structure. We do not
702 support 64 bit archives presently; so far as I know, none actually
703 exist. Supporting them would require changing these constants, and
704 changing some H_GET_32 to H_GET_64. */
706 /* The size of an external symdef structure. */
707 #define BSD_SYMDEF_SIZE 8
709 /* The offset from the start of a symdef structure to the file offset. */
710 #define BSD_SYMDEF_OFFSET_SIZE 4
712 /* The size of the symdef count. */
713 #define BSD_SYMDEF_COUNT_SIZE 4
715 /* The size of the string count. */
716 #define BSD_STRING_COUNT_SIZE 4
718 /* Returns false on error, true otherwise */
721 do_slurp_bsd_armap (abfd
)
724 struct areltdata
*mapdata
;
725 unsigned int counter
;
726 bfd_byte
*raw_armap
, *rbase
;
727 struct artdata
*ardata
= bfd_ardata (abfd
);
729 bfd_size_type parsed_size
, amt
;
732 mapdata
= (struct areltdata
*) _bfd_read_ar_hdr (abfd
);
735 parsed_size
= mapdata
->parsed_size
;
736 bfd_release (abfd
, (PTR
) mapdata
); /* Don't need it any more. */
738 raw_armap
= (bfd_byte
*) bfd_zalloc (abfd
, parsed_size
);
739 if (raw_armap
== (bfd_byte
*) NULL
)
742 if (bfd_bread ((PTR
) raw_armap
, parsed_size
, abfd
) != parsed_size
)
744 if (bfd_get_error () != bfd_error_system_call
)
745 bfd_set_error (bfd_error_malformed_archive
);
747 bfd_release (abfd
, (PTR
) raw_armap
);
751 ardata
->symdef_count
= H_GET_32 (abfd
, raw_armap
) / BSD_SYMDEF_SIZE
;
753 if (ardata
->symdef_count
* BSD_SYMDEF_SIZE
>
754 parsed_size
- BSD_SYMDEF_COUNT_SIZE
)
756 /* Probably we're using the wrong byte ordering. */
757 bfd_set_error (bfd_error_wrong_format
);
762 rbase
= raw_armap
+ BSD_SYMDEF_COUNT_SIZE
;
763 stringbase
= ((char *) rbase
764 + ardata
->symdef_count
* BSD_SYMDEF_SIZE
765 + BSD_STRING_COUNT_SIZE
);
766 amt
= (bfd_size_type
) ardata
->symdef_count
* sizeof (carsym
);
767 ardata
->symdefs
= (carsym
*) bfd_alloc (abfd
, amt
);
768 if (!ardata
->symdefs
)
771 for (counter
= 0, set
= ardata
->symdefs
;
772 counter
< ardata
->symdef_count
;
773 counter
++, set
++, rbase
+= BSD_SYMDEF_SIZE
)
775 set
->name
= H_GET_32 (abfd
, rbase
) + stringbase
;
776 set
->file_offset
= H_GET_32 (abfd
, rbase
+ BSD_SYMDEF_OFFSET_SIZE
);
779 ardata
->first_file_filepos
= bfd_tell (abfd
);
780 /* Pad to an even boundary if you have to. */
781 ardata
->first_file_filepos
+= (ardata
->first_file_filepos
) % 2;
782 /* FIXME, we should provide some way to free raw_ardata when
783 we are done using the strings from it. For now, it seems
784 to be allocated on an objalloc anyway... */
785 bfd_has_map (abfd
) = true;
789 /* Returns false on error, true otherwise. */
792 do_slurp_coff_armap (abfd
)
795 struct areltdata
*mapdata
;
796 int *raw_armap
, *rawptr
;
797 struct artdata
*ardata
= bfd_ardata (abfd
);
799 bfd_size_type stringsize
;
800 unsigned int parsed_size
;
802 bfd_size_type nsymz
; /* Number of symbols in armap. */
803 bfd_vma (*swap
) PARAMS ((const bfd_byte
*));
804 char int_buf
[sizeof (long)];
805 bfd_size_type carsym_size
, ptrsize
;
808 mapdata
= (struct areltdata
*) _bfd_read_ar_hdr (abfd
);
811 parsed_size
= mapdata
->parsed_size
;
812 bfd_release (abfd
, (PTR
) mapdata
); /* Don't need it any more. */
814 if (bfd_bread ((PTR
) int_buf
, (bfd_size_type
) 4, abfd
) != 4)
816 if (bfd_get_error () != bfd_error_system_call
)
817 bfd_set_error (bfd_error_malformed_archive
);
820 /* It seems that all numeric information in a coff archive is always
821 in big endian format, nomatter the host or target. */
823 nsymz
= bfd_getb32 ((PTR
) int_buf
);
824 stringsize
= parsed_size
- (4 * nsymz
) - 4;
827 /* ... except that some archive formats are broken, and it may be our
828 fault - the i960 little endian coff sometimes has big and sometimes
829 little, because our tools changed. Here's a horrible hack to clean
832 if (stringsize
> 0xfffff
833 && bfd_get_arch (abfd
) == bfd_arch_i960
834 && bfd_get_flavour (abfd
) == bfd_target_coff_flavour
)
836 /* This looks dangerous, let's do it the other way around. */
837 nsymz
= bfd_getl32 ((PTR
) int_buf
);
838 stringsize
= parsed_size
- (4 * nsymz
) - 4;
843 /* The coff armap must be read sequentially. So we construct a
844 bsd-style one in core all at once, for simplicity. */
846 carsym_size
= (nsymz
* sizeof (carsym
));
847 ptrsize
= (4 * nsymz
);
849 ardata
->symdefs
= (carsym
*) bfd_zalloc (abfd
, carsym_size
+ stringsize
+ 1);
850 if (ardata
->symdefs
== NULL
)
852 carsyms
= ardata
->symdefs
;
853 stringbase
= ((char *) ardata
->symdefs
) + carsym_size
;
855 /* Allocate and read in the raw offsets. */
856 raw_armap
= (int *) bfd_alloc (abfd
, ptrsize
);
857 if (raw_armap
== NULL
)
858 goto release_symdefs
;
859 if (bfd_bread ((PTR
) raw_armap
, ptrsize
, abfd
) != ptrsize
860 || (bfd_bread ((PTR
) stringbase
, stringsize
, abfd
) != stringsize
))
862 if (bfd_get_error () != bfd_error_system_call
)
863 bfd_set_error (bfd_error_malformed_archive
);
864 goto release_raw_armap
;
867 /* OK, build the carsyms. */
868 for (i
= 0; i
< nsymz
; i
++)
870 rawptr
= raw_armap
+ i
;
871 carsyms
->file_offset
= swap ((PTR
) rawptr
);
872 carsyms
->name
= stringbase
;
873 stringbase
+= strlen (stringbase
) + 1;
878 ardata
->symdef_count
= nsymz
;
879 ardata
->first_file_filepos
= bfd_tell (abfd
);
880 /* Pad to an even boundary if you have to. */
881 ardata
->first_file_filepos
+= (ardata
->first_file_filepos
) % 2;
883 bfd_has_map (abfd
) = true;
884 bfd_release (abfd
, (PTR
) raw_armap
);
886 /* Check for a second archive header (as used by PE). */
888 struct areltdata
*tmp
;
890 bfd_seek (abfd
, ardata
->first_file_filepos
, SEEK_SET
);
891 tmp
= (struct areltdata
*) _bfd_read_ar_hdr (abfd
);
894 if (tmp
->arch_header
[0] == '/'
895 && tmp
->arch_header
[1] == ' ')
897 ardata
->first_file_filepos
+=
898 (tmp
->parsed_size
+ sizeof (struct ar_hdr
) + 1) & ~(unsigned) 1;
900 bfd_release (abfd
, tmp
);
907 bfd_release (abfd
, (PTR
) raw_armap
);
909 bfd_release (abfd
, (PTR
) (ardata
)->symdefs
);
913 /* This routine can handle either coff-style or bsd-style armaps.
914 Returns false on error, true otherwise */
917 bfd_slurp_armap (abfd
)
921 int i
= bfd_bread ((PTR
) nextname
, (bfd_size_type
) 16, abfd
);
928 if (bfd_seek (abfd
, (file_ptr
) -16, SEEK_CUR
) != 0)
931 if (!strncmp (nextname
, "__.SYMDEF ", 16)
932 || !strncmp (nextname
, "__.SYMDEF/ ", 16)) /* old Linux archives */
933 return do_slurp_bsd_armap (abfd
);
934 else if (!strncmp (nextname
, "/ ", 16))
935 return do_slurp_coff_armap (abfd
);
936 else if (!strncmp (nextname
, "/SYM64/ ", 16))
938 /* 64bit ELF (Irix 6) archive. */
940 return bfd_elf64_archive_slurp_armap (abfd
);
942 bfd_set_error (bfd_error_wrong_format
);
947 bfd_has_map (abfd
) = false;
951 /* Returns false on error, true otherwise */
952 /* flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the
953 header is in a slightly different order and the map name is '/'.
954 This flavour is used by hp300hpux. */
956 #define HPUX_SYMDEF_COUNT_SIZE 2
959 bfd_slurp_bsd_armap_f2 (abfd
)
962 struct areltdata
*mapdata
;
964 unsigned int counter
;
965 bfd_byte
*raw_armap
, *rbase
;
966 struct artdata
*ardata
= bfd_ardata (abfd
);
968 unsigned int stringsize
;
971 int i
= bfd_bread ((PTR
) nextname
, (bfd_size_type
) 16, abfd
);
978 /* The archive has at least 16 bytes in it. */
979 if (bfd_seek (abfd
, (file_ptr
) -16, SEEK_CUR
) != 0)
982 if (!strncmp (nextname
, "__.SYMDEF ", 16)
983 || !strncmp (nextname
, "__.SYMDEF/ ", 16)) /* old Linux archives */
984 return do_slurp_bsd_armap (abfd
);
986 if (strncmp (nextname
, "/ ", 16))
988 bfd_has_map (abfd
) = false;
992 mapdata
= (struct areltdata
*) _bfd_read_ar_hdr (abfd
);
996 amt
= mapdata
->parsed_size
;
997 raw_armap
= (bfd_byte
*) bfd_zalloc (abfd
, amt
);
998 if (raw_armap
== NULL
)
1001 bfd_release (abfd
, (PTR
) mapdata
);
1005 if (bfd_bread ((PTR
) raw_armap
, amt
, abfd
) != amt
)
1007 if (bfd_get_error () != bfd_error_system_call
)
1008 bfd_set_error (bfd_error_malformed_archive
);
1010 bfd_release (abfd
, (PTR
) raw_armap
);
1014 ardata
->symdef_count
= H_GET_16 (abfd
, (PTR
) raw_armap
);
1016 if (ardata
->symdef_count
* BSD_SYMDEF_SIZE
1017 > mapdata
->parsed_size
- HPUX_SYMDEF_COUNT_SIZE
)
1019 /* Probably we're using the wrong byte ordering. */
1020 bfd_set_error (bfd_error_wrong_format
);
1026 stringsize
= H_GET_32 (abfd
, raw_armap
+ HPUX_SYMDEF_COUNT_SIZE
);
1027 /* Skip sym count and string sz. */
1028 stringbase
= ((char *) raw_armap
1029 + HPUX_SYMDEF_COUNT_SIZE
1030 + BSD_STRING_COUNT_SIZE
);
1031 rbase
= (bfd_byte
*) stringbase
+ stringsize
;
1032 amt
= (bfd_size_type
) ardata
->symdef_count
* BSD_SYMDEF_SIZE
;
1033 ardata
->symdefs
= (carsym
*) bfd_alloc (abfd
, amt
);
1034 if (!ardata
->symdefs
)
1037 for (counter
= 0, set
= ardata
->symdefs
;
1038 counter
< ardata
->symdef_count
;
1039 counter
++, set
++, rbase
+= BSD_SYMDEF_SIZE
)
1041 set
->name
= H_GET_32 (abfd
, rbase
) + stringbase
;
1042 set
->file_offset
= H_GET_32 (abfd
, rbase
+ BSD_SYMDEF_OFFSET_SIZE
);
1045 ardata
->first_file_filepos
= bfd_tell (abfd
);
1046 /* Pad to an even boundary if you have to. */
1047 ardata
->first_file_filepos
+= (ardata
->first_file_filepos
) % 2;
1048 /* FIXME, we should provide some way to free raw_ardata when
1049 we are done using the strings from it. For now, it seems
1050 to be allocated on an objalloc anyway... */
1051 bfd_has_map (abfd
) = true;
1055 /** Extended name table.
1057 Normally archives support only 14-character filenames.
1059 Intel has extended the format: longer names are stored in a special
1060 element (the first in the archive, or second if there is an armap);
1061 the name in the ar_hdr is replaced by <space><index into filename
1062 element>. Index is the P.R. of an int (decimal). Data General have
1063 extended the format by using the prefix // for the special element. */
1065 /* Returns false on error, true otherwise. */
1068 _bfd_slurp_extended_name_table (abfd
)
1072 struct areltdata
*namedata
;
1075 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
1076 we probably don't want to return true. */
1077 bfd_seek (abfd
, bfd_ardata (abfd
)->first_file_filepos
, SEEK_SET
);
1078 if (bfd_bread ((PTR
) nextname
, (bfd_size_type
) 16, abfd
) == 16)
1080 if (bfd_seek (abfd
, (file_ptr
) -16, SEEK_CUR
) != 0)
1083 if (strncmp (nextname
, "ARFILENAMES/ ", 16) != 0 &&
1084 strncmp (nextname
, "// ", 16) != 0)
1086 bfd_ardata (abfd
)->extended_names
= NULL
;
1090 namedata
= (struct areltdata
*) _bfd_read_ar_hdr (abfd
);
1091 if (namedata
== NULL
)
1094 amt
= namedata
->parsed_size
;
1095 bfd_ardata (abfd
)->extended_names
= bfd_zalloc (abfd
, amt
);
1096 if (bfd_ardata (abfd
)->extended_names
== NULL
)
1099 bfd_release (abfd
, (PTR
) namedata
);
1103 if (bfd_bread ((PTR
) bfd_ardata (abfd
)->extended_names
, amt
, abfd
) != amt
)
1105 if (bfd_get_error () != bfd_error_system_call
)
1106 bfd_set_error (bfd_error_malformed_archive
);
1107 bfd_release (abfd
, (PTR
) (bfd_ardata (abfd
)->extended_names
));
1108 bfd_ardata (abfd
)->extended_names
= NULL
;
1112 /* Since the archive is supposed to be printable if it contains
1113 text, the entries in the list are newline-padded, not null
1114 padded. In SVR4-style archives, the names also have a
1115 trailing '/'. DOS/NT created archive often have \ in them
1116 We'll fix all problems here.. */
1118 char *temp
= bfd_ardata (abfd
)->extended_names
;
1119 char *limit
= temp
+ namedata
->parsed_size
;
1120 for (; temp
< limit
; ++temp
)
1122 if (*temp
== '\012')
1123 temp
[temp
[-1] == '/' ? -1 : 0] = '\0';
1129 /* Pad to an even boundary if you have to. */
1130 bfd_ardata (abfd
)->first_file_filepos
= bfd_tell (abfd
);
1131 bfd_ardata (abfd
)->first_file_filepos
+=
1132 (bfd_ardata (abfd
)->first_file_filepos
) % 2;
1134 /* FIXME, we can't release namedata here because it was allocated
1135 below extended_names on the objalloc... */
1137 bfd_release (abfd
, namedata
);
1145 /* Return a copy of the stuff in the filename between any :]> and a
1149 normalize (abfd
, file
)
1157 first
= file
+ strlen (file
) - 1;
1160 while (first
!= file
)
1164 if (*first
== ':' || *first
== ']' || *first
== '>')
1172 copy
= (char *) bfd_alloc (abfd
, (bfd_size_type
) (last
- first
+ 1));
1176 memcpy (copy
, first
, last
- first
);
1177 copy
[last
- first
] = 0;
1184 normalize (abfd
, file
)
1185 bfd
*abfd ATTRIBUTE_UNUSED
;
1188 const char *filename
= strrchr (file
, '/');
1190 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
1192 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1193 char *bslash
= strrchr (file
, '\\');
1194 if (filename
== NULL
|| (bslash
!= NULL
&& bslash
> filename
))
1196 if (filename
== NULL
&& file
[0] != '\0' && file
[1] == ':')
1197 filename
= file
+ 1;
1200 if (filename
!= (char *) NULL
)
1208 /* Build a BFD style extended name table. */
1211 _bfd_archive_bsd_construct_extended_name_table (abfd
, tabloc
, tablen
, name
)
1214 bfd_size_type
*tablen
;
1217 *name
= "ARFILENAMES/";
1218 return _bfd_construct_extended_name_table (abfd
, false, tabloc
, tablen
);
1221 /* Build an SVR4 style extended name table. */
1224 _bfd_archive_coff_construct_extended_name_table (abfd
, tabloc
, tablen
, name
)
1227 bfd_size_type
*tablen
;
1231 return _bfd_construct_extended_name_table (abfd
, true, tabloc
, tablen
);
1234 /* Follows archive_head and produces an extended name table if
1235 necessary. Returns (in tabloc) a pointer to an extended name
1236 table, and in tablen the length of the table. If it makes an entry
1237 it clobbers the filename so that the element may be written without
1238 further massage. Returns true if it ran successfully, false if
1239 something went wrong. A successful return may still involve a
1240 zero-length tablen! */
1243 _bfd_construct_extended_name_table (abfd
, trailing_slash
, tabloc
, tablen
)
1245 boolean trailing_slash
;
1247 bfd_size_type
*tablen
;
1249 unsigned int maxname
= abfd
->xvec
->ar_max_namelen
;
1250 bfd_size_type total_namelen
= 0;
1256 /* Figure out how long the table should be. */
1257 for (current
= abfd
->archive_head
; current
!= NULL
; current
= current
->next
)
1260 unsigned int thislen
;
1262 normal
= normalize (current
, current
->filename
);
1266 thislen
= strlen (normal
);
1268 if (thislen
> maxname
1269 && (bfd_get_file_flags (abfd
) & BFD_TRADITIONAL_FORMAT
) != 0)
1272 if (thislen
> maxname
)
1274 /* Add one to leave room for \n. */
1275 total_namelen
+= thislen
+ 1;
1278 /* Leave room for trailing slash. */
1284 struct ar_hdr
*hdr
= arch_hdr (current
);
1285 if (strncmp (normal
, hdr
->ar_name
, thislen
) != 0
1286 || (thislen
< sizeof hdr
->ar_name
1287 && hdr
->ar_name
[thislen
] != ar_padchar (current
)))
1289 /* Must have been using extended format even though it
1290 didn't need to. Fix it to use normal format. */
1291 memcpy (hdr
->ar_name
, normal
, thislen
);
1292 if (thislen
< maxname
1293 || (thislen
== maxname
&& thislen
< sizeof hdr
->ar_name
))
1294 hdr
->ar_name
[thislen
] = ar_padchar (current
);
1299 if (total_namelen
== 0)
1302 *tabloc
= bfd_zalloc (abfd
, total_namelen
);
1303 if (*tabloc
== NULL
)
1306 *tablen
= total_namelen
;
1309 for (current
= abfd
->archive_head
; current
!= NULL
; current
=
1313 unsigned int thislen
;
1315 normal
= normalize (current
, current
->filename
);
1319 thislen
= strlen (normal
);
1320 if (thislen
> maxname
)
1322 /* Works for now; may need to be re-engineered if we
1323 encounter an oddball archive format and want to
1324 generalise this hack. */
1325 struct ar_hdr
*hdr
= arch_hdr (current
);
1326 strcpy (strptr
, normal
);
1327 if (! trailing_slash
)
1328 strptr
[thislen
] = '\012';
1331 strptr
[thislen
] = '/';
1332 strptr
[thislen
+ 1] = '\012';
1334 hdr
->ar_name
[0] = ar_padchar (current
);
1335 /* We know there will always be enough room (one of the few
1336 cases where you may safely use sprintf). */
1337 sprintf ((hdr
->ar_name
) + 1, "%-d", (unsigned) (strptr
- *tabloc
));
1338 /* Kinda Kludgy. We should just use the returned value of
1339 sprintf but not all implementations get this right. */
1341 char *temp
= hdr
->ar_name
+ 2;
1342 for (; temp
< hdr
->ar_name
+ maxname
; temp
++)
1346 strptr
+= thislen
+ 1;
1355 /** A couple of functions for creating ar_hdrs */
1357 #ifdef HPUX_LARGE_AR_IDS
1358 /* Function to encode large UID/GID values according to HP. */
1361 hpux_uid_gid_encode (str
, id
)
1367 str
[5] = '@' + (id
& 3);
1370 for (cnt
= 4; cnt
>= 0; ++cnt
, id
>>= 6)
1371 str
[cnt
] = ' ' + (id
& 0x3f);
1373 #endif /* HPUX_LARGE_AR_IDS */
1383 /* Takes a filename, returns an arelt_data for it, or NULL if it can't
1384 make one. The filename must refer to a filename in the filesystem.
1385 The filename field of the ar_hdr will NOT be initialized. If member
1386 is set, and it's an in-memory bfd, we fake it. */
1388 static struct areltdata
*
1389 bfd_ar_hdr_from_filesystem (abfd
, filename
, member
)
1391 const char *filename
;
1395 struct areltdata
*ared
;
1400 if (member
&& (member
->flags
& BFD_IN_MEMORY
) != 0)
1402 /* Assume we just "made" the member, and fake it. */
1403 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) member
->iostream
;
1404 time (&status
.st_mtime
);
1405 status
.st_uid
= getuid ();
1406 status
.st_gid
= getgid ();
1407 status
.st_mode
= 0644;
1408 status
.st_size
= bim
->size
;
1410 else if (stat (filename
, &status
) != 0)
1412 bfd_set_error (bfd_error_system_call
);
1416 amt
= sizeof (struct ar_hdr
) + sizeof (struct areltdata
);
1417 ared
= (struct areltdata
*) bfd_zalloc (abfd
, amt
);
1420 hdr
= (struct ar_hdr
*) (((char *) ared
) + sizeof (struct areltdata
));
1422 /* ar headers are space padded, not null padded! */
1423 memset ((PTR
) hdr
, ' ', sizeof (struct ar_hdr
));
1425 strncpy (hdr
->ar_fmag
, ARFMAG
, 2);
1427 /* Goddamned sprintf doesn't permit MAXIMUM field lengths. */
1428 sprintf ((hdr
->ar_date
), "%-12ld", (long) status
.st_mtime
);
1429 #ifdef HPUX_LARGE_AR_IDS
1430 /* HP has a very "special" way to handle UID/GID's with numeric values
1432 if (status
.st_uid
> 99999)
1433 hpux_uid_gid_encode (hdr
->ar_gid
, (long) status
.st_uid
);
1436 sprintf ((hdr
->ar_uid
), "%ld", (long) status
.st_uid
);
1437 #ifdef HPUX_LARGE_AR_IDS
1438 /* HP has a very "special" way to handle UID/GID's with numeric values
1440 if (status
.st_gid
> 99999)
1441 hpux_uid_gid_encode (hdr
->ar_uid
, (long) status
.st_gid
);
1444 sprintf ((hdr
->ar_gid
), "%ld", (long) status
.st_gid
);
1445 sprintf ((hdr
->ar_mode
), "%-8o", (unsigned int) status
.st_mode
);
1446 sprintf ((hdr
->ar_size
), "%-10ld", (long) status
.st_size
);
1447 /* Correct for a lossage in sprintf whereby it null-terminates. I cannot
1448 understand how these C losers could design such a ramshackle bunch of
1450 temp
= (char *) hdr
;
1451 temp1
= temp
+ sizeof (struct ar_hdr
) - 2;
1452 for (; temp
< temp1
; temp
++)
1457 strncpy (hdr
->ar_fmag
, ARFMAG
, 2);
1458 ared
->parsed_size
= status
.st_size
;
1459 ared
->arch_header
= (char *) hdr
;
1464 /* This is magic required by the "ar" program. Since it's
1465 undocumented, it's undocumented. You may think that it would take
1466 a strong stomach to write this, and it does, but it takes even a
1467 stronger stomach to try to code around such a thing! */
1469 struct ar_hdr
*bfd_special_undocumented_glue
PARAMS ((bfd
*, const char *));
1472 bfd_special_undocumented_glue (abfd
, filename
)
1474 const char *filename
;
1476 struct areltdata
*ar_elt
= bfd_ar_hdr_from_filesystem (abfd
, filename
, 0);
1479 return (struct ar_hdr
*) ar_elt
->arch_header
;
1482 /* Analogous to stat call. */
1485 bfd_generic_stat_arch_elt (abfd
, buf
)
1492 if (abfd
->arelt_data
== NULL
)
1494 bfd_set_error (bfd_error_invalid_operation
);
1498 hdr
= arch_hdr (abfd
);
1500 #define foo(arelt, stelt, size) \
1501 buf->stelt = strtol (hdr->arelt, &aloser, size); \
1502 if (aloser == hdr->arelt) \
1505 /* Some platforms support special notations for large IDs. */
1506 #ifdef HPUX_LARGE_AR_IDS
1507 # define foo2(arelt, stelt, size) \
1508 if (hdr->arelt[5] == ' ') \
1510 foo (arelt, stelt, size); \
1515 for (buf->stelt = cnt = 0; cnt < 5; ++cnt) \
1517 if (hdr->arelt[cnt] < ' ' || hdr->arelt[cnt] > ' ' + 0x3f) \
1520 buf->stelt += hdr->arelt[cnt] - ' '; \
1522 if (hdr->arelt[5] < '@' || hdr->arelt[5] > '@' + 3) \
1525 buf->stelt += hdr->arelt[5] - '@'; \
1528 # define foo2(arelt, stelt, size) foo (arelt, stelt, size)
1531 foo (ar_date
, st_mtime
, 10);
1532 foo2 (ar_uid
, st_uid
, 10);
1533 foo2 (ar_gid
, st_gid
, 10);
1534 foo (ar_mode
, st_mode
, 8);
1536 buf
->st_size
= arch_eltdata (abfd
)->parsed_size
;
1542 bfd_dont_truncate_arname (abfd
, pathname
, arhdr
)
1544 const char *pathname
;
1547 /* FIXME: This interacts unpleasantly with ar's quick-append option.
1548 Fortunately ic960 users will never use that option. Fixing this
1549 is very hard; fortunately I know how to do it and will do so once
1550 intel's release is out the door. */
1552 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
1554 const char *filename
;
1555 size_t maxlen
= ar_maxnamelen (abfd
);
1557 if ((bfd_get_file_flags (abfd
) & BFD_TRADITIONAL_FORMAT
) != 0)
1559 bfd_bsd_truncate_arname (abfd
, pathname
, arhdr
);
1563 filename
= normalize (abfd
, pathname
);
1564 if (filename
== NULL
)
1570 length
= strlen (filename
);
1572 if (length
<= maxlen
)
1573 memcpy (hdr
->ar_name
, filename
, length
);
1575 /* Add the padding character if there is room for it. */
1577 || (length
== maxlen
&& length
< sizeof hdr
->ar_name
))
1578 (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
1582 bfd_bsd_truncate_arname (abfd
, pathname
, arhdr
)
1584 const char *pathname
;
1587 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
1589 const char *filename
= strrchr (pathname
, '/');
1590 size_t maxlen
= ar_maxnamelen (abfd
);
1592 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
1594 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1595 char *bslash
= strrchr (pathname
, '\\');
1596 if (filename
== NULL
|| (bslash
!= NULL
&& bslash
> filename
))
1598 if (filename
== NULL
&& pathname
[0] != '\0' && pathname
[1] == ':')
1599 filename
= pathname
+ 1;
1603 if (filename
== NULL
)
1604 filename
= pathname
;
1608 length
= strlen (filename
);
1610 if (length
<= maxlen
)
1611 memcpy (hdr
->ar_name
, filename
, length
);
1614 /* pathname: meet procrustes */
1615 memcpy (hdr
->ar_name
, filename
, maxlen
);
1619 if (length
< maxlen
)
1620 (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
1623 /* Store name into ar header. Truncates the name to fit.
1624 1> strip pathname to be just the basename.
1625 2> if it's short enuf to fit, stuff it in.
1626 3> If it doesn't end with .o, truncate it to fit
1627 4> truncate it before the .o, append .o, stuff THAT in. */
1629 /* This is what gnu ar does. It's better but incompatible with the
1633 bfd_gnu_truncate_arname (abfd
, pathname
, arhdr
)
1635 const char *pathname
;
1638 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
1640 const char *filename
= strrchr (pathname
, '/');
1641 size_t maxlen
= ar_maxnamelen (abfd
);
1643 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
1645 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1646 char *bslash
= strrchr (pathname
, '\\');
1647 if (filename
== NULL
|| (bslash
!= NULL
&& bslash
> filename
))
1649 if (filename
== NULL
&& pathname
[0] != '\0' && pathname
[1] == ':')
1650 filename
= pathname
+ 1;
1654 if (filename
== NULL
)
1655 filename
= pathname
;
1659 length
= strlen (filename
);
1661 if (length
<= maxlen
)
1662 memcpy (hdr
->ar_name
, filename
, length
);
1664 { /* pathname: meet procrustes */
1665 memcpy (hdr
->ar_name
, filename
, maxlen
);
1666 if ((filename
[length
- 2] == '.') && (filename
[length
- 1] == 'o'))
1668 hdr
->ar_name
[maxlen
- 2] = '.';
1669 hdr
->ar_name
[maxlen
- 1] = 'o';
1675 (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
1678 /* The BFD is open for write and has its format set to bfd_archive. */
1681 _bfd_write_archive_contents (arch
)
1685 char *etable
= NULL
;
1686 bfd_size_type elength
= 0;
1687 const char *ename
= NULL
;
1688 boolean makemap
= bfd_has_map (arch
);
1689 boolean hasobjects
= false; /* If no .o's, don't bother to make a map. */
1690 bfd_size_type wrote
;
1694 /* Verify the viability of all entries; if any of them live in the
1695 filesystem (as opposed to living in an archive open for input)
1696 then construct a fresh ar_hdr for them. */
1697 for (current
= arch
->archive_head
; current
; current
= current
->next
)
1699 /* This check is checking the bfds for the objects we're reading
1700 from (which are usually either an object file or archive on
1701 disk), not the archive entries we're writing to. We don't
1702 actually create bfds for the archive members, we just copy
1703 them byte-wise when we write out the archive. */
1704 if (bfd_write_p (current
))
1706 bfd_set_error (bfd_error_invalid_operation
);
1709 if (!current
->arelt_data
)
1711 current
->arelt_data
=
1712 (PTR
) bfd_ar_hdr_from_filesystem (arch
, current
->filename
, current
);
1713 if (!current
->arelt_data
)
1716 /* Put in the file name. */
1717 BFD_SEND (arch
, _bfd_truncate_arname
, (arch
,
1719 (char *) arch_hdr (current
)));
1722 if (makemap
&& ! hasobjects
)
1723 { /* Don't bother if we won't make a map! */
1724 if ((bfd_check_format (current
, bfd_object
))
1725 #if 0 /* FIXME -- these are not set correctly */
1726 && ((bfd_get_file_flags (current
) & HAS_SYMS
))
1733 if (!BFD_SEND (arch
, _bfd_construct_extended_name_table
,
1734 (arch
, &etable
, &elength
, &ename
)))
1737 if (bfd_seek (arch
, (file_ptr
) 0, SEEK_SET
) != 0)
1740 wrote
= bfd_bwrite (BFD_GNU960_ARMAG (arch
), (bfd_size_type
) SARMAG
, arch
);
1742 wrote
= bfd_bwrite (ARMAG
, (bfd_size_type
) SARMAG
, arch
);
1744 if (wrote
!= SARMAG
)
1747 if (makemap
&& hasobjects
)
1749 if (! _bfd_compute_and_write_armap (arch
, (unsigned int) elength
))
1757 memset ((char *) (&hdr
), 0, sizeof (struct ar_hdr
));
1758 strcpy (hdr
.ar_name
, ename
);
1759 /* Round size up to even number in archive header. */
1760 sprintf (&(hdr
.ar_size
[0]), "%-10d",
1761 (int) ((elength
+ 1) & ~(bfd_size_type
) 1));
1762 strncpy (hdr
.ar_fmag
, ARFMAG
, 2);
1763 for (i
= 0; i
< sizeof (struct ar_hdr
); i
++)
1764 if (((char *) (&hdr
))[i
] == '\0')
1765 (((char *) (&hdr
))[i
]) = ' ';
1766 if ((bfd_bwrite ((PTR
) &hdr
, (bfd_size_type
) sizeof (struct ar_hdr
), arch
)
1767 != sizeof (struct ar_hdr
))
1768 || bfd_bwrite (etable
, elength
, arch
) != elength
)
1770 if ((elength
% 2) == 1)
1772 if (bfd_bwrite ("\012", (bfd_size_type
) 1, arch
) != 1)
1777 for (current
= arch
->archive_head
; current
; current
= current
->next
)
1779 char buffer
[DEFAULT_BUFFERSIZE
];
1780 unsigned int remaining
= arelt_size (current
);
1781 struct ar_hdr
*hdr
= arch_hdr (current
);
1783 /* Write ar header. */
1784 if (bfd_bwrite ((PTR
) hdr
, (bfd_size_type
) sizeof (*hdr
), arch
)
1787 if (bfd_seek (current
, (file_ptr
) 0, SEEK_SET
) != 0)
1791 unsigned int amt
= DEFAULT_BUFFERSIZE
;
1792 if (amt
> remaining
)
1795 if (bfd_bread (buffer
, (bfd_size_type
) amt
, current
) != amt
)
1797 if (bfd_get_error () != bfd_error_system_call
)
1798 bfd_set_error (bfd_error_malformed_archive
);
1801 if (bfd_bwrite (buffer
, (bfd_size_type
) amt
, arch
) != amt
)
1805 if ((arelt_size (current
) % 2) == 1)
1807 if (bfd_bwrite ("\012", (bfd_size_type
) 1, arch
) != 1)
1812 if (makemap
&& hasobjects
)
1814 /* Verify the timestamp in the archive file. If it would not be
1815 accepted by the linker, rewrite it until it would be. If
1816 anything odd happens, break out and just return. (The
1817 Berkeley linker checks the timestamp and refuses to read the
1818 table-of-contents if it is >60 seconds less than the file's
1819 modified-time. That painful hack requires this painful hack. */
1823 if (bfd_update_armap_timestamp (arch
))
1825 (*_bfd_error_handler
)
1826 (_("Warning: writing archive was slow: rewriting timestamp\n"));
1828 while (++tries
< 6);
1834 /* Note that the namidx for the first symbol is 0. */
1837 _bfd_compute_and_write_armap (arch
, elength
)
1839 unsigned int elength
;
1841 char *first_name
= NULL
;
1843 file_ptr elt_no
= 0;
1844 struct orl
*map
= NULL
;
1845 unsigned int orl_max
= 1024; /* fine initial default */
1846 unsigned int orl_count
= 0;
1847 int stridx
= 0; /* string index */
1848 asymbol
**syms
= NULL
;
1853 /* Dunno if this is the best place for this info... */
1855 elength
+= sizeof (struct ar_hdr
);
1856 elength
+= elength
% 2;
1858 amt
= (bfd_size_type
) orl_max
* sizeof (struct orl
);
1859 map
= (struct orl
*) bfd_malloc (amt
);
1863 /* We put the symbol names on the arch objalloc, and then discard
1865 first_name
= bfd_alloc (arch
, (bfd_size_type
) 1);
1866 if (first_name
== NULL
)
1869 /* Drop all the files called __.SYMDEF, we're going to make our own. */
1870 while (arch
->archive_head
&&
1871 strcmp (arch
->archive_head
->filename
, "__.SYMDEF") == 0)
1872 arch
->archive_head
= arch
->archive_head
->next
;
1874 /* Map over each element. */
1875 for (current
= arch
->archive_head
;
1876 current
!= (bfd
*) NULL
;
1877 current
= current
->next
, elt_no
++)
1879 if (bfd_check_format (current
, bfd_object
)
1880 && (bfd_get_file_flags (current
) & HAS_SYMS
) != 0)
1886 storage
= bfd_get_symtab_upper_bound (current
);
1892 if (storage
> syms_max
)
1897 syms
= (asymbol
**) bfd_malloc ((bfd_size_type
) syms_max
);
1901 symcount
= bfd_canonicalize_symtab (current
, syms
);
1905 /* Now map over all the symbols, picking out the ones we
1907 for (src_count
= 0; src_count
< symcount
; src_count
++)
1909 flagword flags
= (syms
[src_count
])->flags
;
1910 asection
*sec
= syms
[src_count
]->section
;
1912 if ((flags
& BSF_GLOBAL
||
1914 flags
& BSF_INDIRECT
||
1915 bfd_is_com_section (sec
))
1916 && ! bfd_is_und_section (sec
))
1918 bfd_size_type namelen
;
1919 struct orl
*new_map
;
1921 /* This symbol will go into the archive header. */
1922 if (orl_count
== orl_max
)
1925 amt
= (bfd_size_type
) orl_max
* sizeof (struct orl
);
1926 new_map
= (struct orl
*) bfd_realloc (map
, amt
);
1927 if (new_map
== (struct orl
*) NULL
)
1933 namelen
= strlen (syms
[src_count
]->name
);
1934 amt
= sizeof (char *);
1935 map
[orl_count
].name
= (char **) bfd_alloc (arch
, amt
);
1936 if (map
[orl_count
].name
== NULL
)
1938 *(map
[orl_count
].name
) = bfd_alloc (arch
, namelen
+ 1);
1939 if (*(map
[orl_count
].name
) == NULL
)
1941 strcpy (*(map
[orl_count
].name
), syms
[src_count
]->name
);
1942 map
[orl_count
].u
.abfd
= current
;
1943 map
[orl_count
].namidx
= stridx
;
1945 stridx
+= namelen
+ 1;
1951 /* Now ask the BFD to free up any cached information, so we
1952 don't fill all of memory with symbol tables. */
1953 if (! bfd_free_cached_info (current
))
1958 /* OK, now we have collected all the data, let's write them out. */
1959 ret
= BFD_SEND (arch
, write_armap
,
1960 (arch
, elength
, map
, orl_count
, stridx
));
1966 if (first_name
!= NULL
)
1967 bfd_release (arch
, first_name
);
1976 if (first_name
!= NULL
)
1977 bfd_release (arch
, first_name
);
1983 bsd_write_armap (arch
, elength
, map
, orl_count
, stridx
)
1985 unsigned int elength
;
1987 unsigned int orl_count
;
1990 int padit
= stridx
& 1;
1991 unsigned int ranlibsize
= orl_count
* BSD_SYMDEF_SIZE
;
1992 unsigned int stringsize
= stridx
+ padit
;
1993 /* Include 8 bytes to store ranlibsize and stringsize in output. */
1994 unsigned int mapsize
= ranlibsize
+ stringsize
+ 8;
1996 bfd
*current
= arch
->archive_head
;
1997 bfd
*last_elt
= current
; /* last element arch seen */
2001 struct stat statbuf
;
2004 firstreal
= mapsize
+ elength
+ sizeof (struct ar_hdr
) + SARMAG
;
2006 stat (arch
->filename
, &statbuf
);
2007 memset ((char *) (&hdr
), 0, sizeof (struct ar_hdr
));
2008 sprintf (hdr
.ar_name
, RANLIBMAG
);
2009 /* Remember the timestamp, to keep it holy. But fudge it a little. */
2010 bfd_ardata (arch
)->armap_timestamp
= statbuf
.st_mtime
+ ARMAP_TIME_OFFSET
;
2011 bfd_ardata (arch
)->armap_datepos
= (SARMAG
2012 + offsetof (struct ar_hdr
, ar_date
[0]));
2013 sprintf (hdr
.ar_date
, "%ld", bfd_ardata (arch
)->armap_timestamp
);
2014 sprintf (hdr
.ar_uid
, "%ld", (long) getuid ());
2015 sprintf (hdr
.ar_gid
, "%ld", (long) getgid ());
2016 sprintf (hdr
.ar_size
, "%-10d", (int) mapsize
);
2017 strncpy (hdr
.ar_fmag
, ARFMAG
, 2);
2018 for (i
= 0; i
< sizeof (struct ar_hdr
); i
++)
2019 if (((char *) (&hdr
))[i
] == '\0')
2020 (((char *) (&hdr
))[i
]) = ' ';
2021 if (bfd_bwrite ((PTR
) &hdr
, (bfd_size_type
) sizeof (struct ar_hdr
), arch
)
2022 != sizeof (struct ar_hdr
))
2024 H_PUT_32 (arch
, ranlibsize
, temp
);
2025 if (bfd_bwrite (temp
, (bfd_size_type
) sizeof (temp
), arch
) != sizeof (temp
))
2028 for (count
= 0; count
< orl_count
; count
++)
2030 bfd_byte buf
[BSD_SYMDEF_SIZE
];
2032 if (map
[count
].u
.abfd
!= last_elt
)
2036 firstreal
+= arelt_size (current
) + sizeof (struct ar_hdr
);
2037 firstreal
+= firstreal
% 2;
2038 current
= current
->next
;
2040 while (current
!= map
[count
].u
.abfd
);
2041 } /* if new archive element */
2044 H_PUT_32 (arch
, map
[count
].namidx
, buf
);
2045 H_PUT_32 (arch
, firstreal
, buf
+ BSD_SYMDEF_OFFSET_SIZE
);
2046 if (bfd_bwrite (buf
, (bfd_size_type
) BSD_SYMDEF_SIZE
, arch
)
2051 /* Now write the strings themselves. */
2052 H_PUT_32 (arch
, stringsize
, temp
);
2053 if (bfd_bwrite (temp
, (bfd_size_type
) sizeof (temp
), arch
) != sizeof (temp
))
2055 for (count
= 0; count
< orl_count
; count
++)
2057 size_t len
= strlen (*map
[count
].name
) + 1;
2059 if (bfd_bwrite (*map
[count
].name
, (bfd_size_type
) len
, arch
) != len
)
2063 /* The spec sez this should be a newline. But in order to be
2064 bug-compatible for sun's ar we use a null. */
2067 if (bfd_bwrite ("", (bfd_size_type
) 1, arch
) != 1)
2074 /* At the end of archive file handling, update the timestamp in the
2075 file, so the linker will accept it.
2077 Return true if the timestamp was OK, or an unusual problem happened.
2078 Return false if we updated the timestamp. */
2081 _bfd_archive_bsd_update_armap_timestamp (arch
)
2084 struct stat archstat
;
2088 /* Flush writes, get last-write timestamp from file, and compare it
2089 to the timestamp IN the file. */
2091 if (bfd_stat (arch
, &archstat
) == -1)
2093 perror (_("Reading archive file mod timestamp"));
2095 /* Can't read mod time for some reason. */
2098 if (archstat
.st_mtime
<= bfd_ardata (arch
)->armap_timestamp
)
2099 /* OK by the linker's rules. */
2102 /* Update the timestamp. */
2103 bfd_ardata (arch
)->armap_timestamp
= archstat
.st_mtime
+ ARMAP_TIME_OFFSET
;
2105 /* Prepare an ASCII version suitable for writing. */
2106 memset (hdr
.ar_date
, 0, sizeof (hdr
.ar_date
));
2107 sprintf (hdr
.ar_date
, "%ld", bfd_ardata (arch
)->armap_timestamp
);
2108 for (i
= 0; i
< sizeof (hdr
.ar_date
); i
++)
2109 if (hdr
.ar_date
[i
] == '\0')
2110 (hdr
.ar_date
)[i
] = ' ';
2112 /* Write it into the file. */
2113 bfd_ardata (arch
)->armap_datepos
= (SARMAG
2114 + offsetof (struct ar_hdr
, ar_date
[0]));
2115 if (bfd_seek (arch
, bfd_ardata (arch
)->armap_datepos
, SEEK_SET
) != 0
2116 || (bfd_bwrite (hdr
.ar_date
, (bfd_size_type
) sizeof (hdr
.ar_date
), arch
)
2117 != sizeof (hdr
.ar_date
)))
2119 /* FIXME: bfd can't call perror. */
2120 perror (_("Writing updated armap timestamp"));
2122 /* Some error while writing. */
2126 /* We updated the timestamp successfully. */
2130 /* A coff armap looks like :
2132 struct ar_hdr with name = '/'
2134 offset of file for symbol 0
2135 offset of file for symbol 1
2137 offset of file for symbol n-1
2145 coff_write_armap (arch
, elength
, map
, symbol_count
, stridx
)
2147 unsigned int elength
;
2149 unsigned int symbol_count
;
2152 /* The size of the ranlib is the number of exported symbols in the
2153 archive * the number of bytes in an int, + an int for the count. */
2154 unsigned int ranlibsize
= (symbol_count
* 4) + 4;
2155 unsigned int stringsize
= stridx
;
2156 unsigned int mapsize
= stringsize
+ ranlibsize
;
2157 unsigned int archive_member_file_ptr
;
2158 bfd
*current
= arch
->archive_head
;
2162 int padit
= mapsize
& 1;
2167 /* Work out where the first object file will go in the archive. */
2168 archive_member_file_ptr
= (mapsize
2170 + sizeof (struct ar_hdr
)
2173 memset ((char *) (&hdr
), 0, sizeof (struct ar_hdr
));
2174 hdr
.ar_name
[0] = '/';
2175 sprintf (hdr
.ar_size
, "%-10d", (int) mapsize
);
2176 sprintf (hdr
.ar_date
, "%ld", (long) time (NULL
));
2177 /* This, at least, is what Intel coff sets the values to. */
2178 sprintf ((hdr
.ar_uid
), "%d", 0);
2179 sprintf ((hdr
.ar_gid
), "%d", 0);
2180 sprintf ((hdr
.ar_mode
), "%-7o", (unsigned) 0);
2181 strncpy (hdr
.ar_fmag
, ARFMAG
, 2);
2183 for (i
= 0; i
< sizeof (struct ar_hdr
); i
++)
2184 if (((char *) (&hdr
))[i
] == '\0')
2185 (((char *) (&hdr
))[i
]) = ' ';
2187 /* Write the ar header for this item and the number of symbols. */
2189 if (bfd_bwrite ((PTR
) &hdr
, (bfd_size_type
) sizeof (struct ar_hdr
), arch
)
2190 != sizeof (struct ar_hdr
))
2193 if (!bfd_write_bigendian_4byte_int (arch
, symbol_count
))
2196 /* Two passes, first write the file offsets for each symbol -
2197 remembering that each offset is on a two byte boundary. */
2199 /* Write out the file offset for the file associated with each
2200 symbol, and remember to keep the offsets padded out. */
2202 current
= arch
->archive_head
;
2204 while (current
!= (bfd
*) NULL
&& count
< symbol_count
)
2206 /* For each symbol which is used defined in this object, write
2207 out the object file's address in the archive. */
2209 while (count
< symbol_count
&& map
[count
].u
.abfd
== current
)
2211 if (!bfd_write_bigendian_4byte_int (arch
, archive_member_file_ptr
))
2215 /* Add size of this archive entry. */
2216 archive_member_file_ptr
+= (arelt_size (current
)
2217 + sizeof (struct ar_hdr
));
2218 /* Remember aboout the even alignment. */
2219 archive_member_file_ptr
+= archive_member_file_ptr
% 2;
2220 current
= current
->next
;
2223 /* Now write the strings themselves. */
2224 for (count
= 0; count
< symbol_count
; count
++)
2226 size_t len
= strlen (*map
[count
].name
) + 1;
2228 if (bfd_bwrite (*map
[count
].name
, (bfd_size_type
) len
, arch
) != len
)
2232 /* The spec sez this should be a newline. But in order to be
2233 bug-compatible for arc960 we use a null. */
2236 if (bfd_bwrite ("", (bfd_size_type
) 1, arch
) != 1)