1 /* BFD back-end for archive files (libraries).
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 3 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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>>.
82 o - all archive elements start on an even boundary, newline padded;
83 o - all arch headers are char *;
84 o - all arch headers are the same size (across architectures).
87 /* Some formats provide a way to cram a long filename into the short
88 (16 chars) space provided by a BSD archive. The trick is: make a
89 special "file" in the front of the archive, sort of like the SYMDEF
90 entry. If the filename is too long to fit, put it in the extended
91 name table, and use its index as the filename. To prevent
92 confusion prepend the index with a space. This means you can't
93 have filenames that start with a space, but then again, many Unix
94 utilities can't handle that anyway.
96 This scheme unfortunately requires that you stand on your head in
97 order to write an archive since you need to put a magic file at the
98 front, and need to touch every entry to do so. C'est la vie.
100 We support two variants of this idea:
101 The SVR4 format (extended name table is named "//"),
102 and an extended pseudo-BSD variant (extended name table is named
103 "ARFILENAMES/"). The origin of the latter format is uncertain.
105 BSD 4.4 uses a third scheme: It writes a long filename
106 directly after the header. This allows 'ar q' to work.
109 /* Summary of archive member names:
111 Symbol table (must be first):
112 "__.SYMDEF " - Symbol table, Berkeley style, produced by ranlib.
113 "/ " - Symbol table, system 5 style.
115 Long name table (must be before regular file members):
116 "// " - Long name table, System 5 R4 style.
117 "ARFILENAMES/ " - Long name table, non-standard extended BSD (not BSD 4.4).
119 Regular file members with short names:
120 "filename.o/ " - Regular file, System 5 style (embedded spaces ok).
121 "filename.o " - Regular file, Berkeley style (no embedded spaces).
123 Regular files with long names (or embedded spaces, for BSD variants):
124 "/18 " - SVR4 style, name at offset 18 in name table.
125 "#1/23 " - Long name (or embedded spaces) 23 characters long,
126 BSD 4.4 style, full name follows header.
127 " 18 " - Long name 18 characters long, extended pseudo-BSD.
132 #include "libiberty.h"
135 #include "aout/ranlib.h"
136 #include "safe-ctype.h"
138 #include "filenames.h"
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! */
155 #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
156 #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
158 #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
159 #define arch_hdr(bfd) ((struct ar_hdr *) arch_eltdata (bfd)->arch_header)
161 /* True iff NAME designated a BSD 4.4 extended name. */
163 #define is_bsd44_extended_name(NAME) \
164 (NAME[0] == '#' && NAME[1] == '1' && NAME[2] == '/' && ISDIGIT (NAME[3]))
167 _bfd_ar_spacepad (char *p
, size_t n
, const char *fmt
, long val
)
171 snprintf (buf
, sizeof (buf
), fmt
, val
);
175 memcpy (p
, buf
, len
);
176 memset (p
+ len
, ' ', n
- len
);
183 _bfd_generic_mkarchive (bfd
*abfd
)
185 bfd_size_type amt
= sizeof (struct artdata
);
187 abfd
->tdata
.aout_ar_data
= (struct artdata
*) bfd_zalloc (abfd
, amt
);
188 if (bfd_ardata (abfd
) == NULL
)
191 /* Already cleared by bfd_zalloc above.
192 bfd_ardata (abfd)->cache = NULL;
193 bfd_ardata (abfd)->archive_head = NULL;
194 bfd_ardata (abfd)->symdefs = NULL;
195 bfd_ardata (abfd)->extended_names = NULL;
196 bfd_ardata (abfd)->extended_names_size = 0;
197 bfd_ardata (abfd)->tdata = NULL; */
207 symindex bfd_get_next_mapent
208 (bfd *abfd, symindex previous, carsym **sym);
211 Step through archive @var{abfd}'s symbol table (if it
212 has one). Successively update @var{sym} with the next symbol's
213 information, returning that symbol's (internal) index into the
216 Supply <<BFD_NO_MORE_SYMBOLS>> as the @var{previous} entry to get
217 the first one; returns <<BFD_NO_MORE_SYMBOLS>> when you've already
220 A <<carsym>> is a canonical archive symbol. The only
221 user-visible element is its name, a null-terminated string.
225 bfd_get_next_mapent (bfd
*abfd
, symindex prev
, carsym
**entry
)
227 if (!bfd_has_map (abfd
))
229 bfd_set_error (bfd_error_invalid_operation
);
230 return BFD_NO_MORE_SYMBOLS
;
233 if (prev
== BFD_NO_MORE_SYMBOLS
)
237 if (prev
>= bfd_ardata (abfd
)->symdef_count
)
238 return BFD_NO_MORE_SYMBOLS
;
240 *entry
= (bfd_ardata (abfd
)->symdefs
+ prev
);
244 /* To be called by backends only. */
247 _bfd_create_empty_archive_element_shell (bfd
*obfd
)
249 return _bfd_new_bfd_contained_in (obfd
);
257 bfd_boolean bfd_set_archive_head (bfd *output, bfd *new_head);
260 Set the head of the chain of
261 BFDs contained in the archive @var{output} to @var{new_head}.
265 bfd_set_archive_head (bfd
*output_archive
, bfd
*new_head
)
267 output_archive
->archive_head
= new_head
;
272 _bfd_look_for_bfd_in_cache (bfd
*arch_bfd
, file_ptr filepos
)
274 htab_t hash_table
= bfd_ardata (arch_bfd
)->cache
;
280 struct ar_cache
*entry
= (struct ar_cache
*) htab_find (hash_table
, &m
);
291 hash_file_ptr (const PTR p
)
293 return (hashval_t
) (((struct ar_cache
*) p
)->ptr
);
296 /* Returns non-zero if P1 and P2 are equal. */
299 eq_file_ptr (const PTR p1
, const PTR p2
)
301 struct ar_cache
*arc1
= (struct ar_cache
*) p1
;
302 struct ar_cache
*arc2
= (struct ar_cache
*) p2
;
303 return arc1
->ptr
== arc2
->ptr
;
306 /* Kind of stupid to call cons for each one, but we don't do too many. */
309 _bfd_add_bfd_to_archive_cache (bfd
*arch_bfd
, file_ptr filepos
, bfd
*new_elt
)
311 struct ar_cache
*cache
;
312 htab_t hash_table
= bfd_ardata (arch_bfd
)->cache
;
314 /* If the hash table hasn't been created, create it. */
315 if (hash_table
== NULL
)
317 hash_table
= htab_create_alloc (16, hash_file_ptr
, eq_file_ptr
,
319 if (hash_table
== NULL
)
321 bfd_ardata (arch_bfd
)->cache
= hash_table
;
324 /* Insert new_elt into the hash table by filepos. */
325 cache
= (struct ar_cache
*) bfd_zalloc (arch_bfd
, sizeof (struct ar_cache
));
326 cache
->ptr
= filepos
;
327 cache
->arbfd
= new_elt
;
328 *htab_find_slot (hash_table
, (const void *) cache
, INSERT
) = cache
;
334 _bfd_find_nested_archive (bfd
*arch_bfd
, const char *filename
)
338 for (abfd
= arch_bfd
->nested_archives
;
340 abfd
= abfd
->archive_next
)
342 if (strcmp (filename
, abfd
->filename
) == 0)
345 abfd
= bfd_openr (filename
, NULL
);
348 abfd
->archive_next
= arch_bfd
->nested_archives
;
349 arch_bfd
->nested_archives
= abfd
;
354 /* The name begins with space. Hence the rest of the name is an index into
358 get_extended_arelt_filename (bfd
*arch
, const char *name
, file_ptr
*originp
)
360 unsigned long table_index
= 0;
363 /* Should extract string so that I can guarantee not to overflow into
364 the next region, but I'm too lazy. */
366 /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */
367 table_index
= strtol (name
+ 1, (char **) &endp
, 10);
368 if (errno
!= 0 || table_index
>= bfd_ardata (arch
)->extended_names_size
)
370 bfd_set_error (bfd_error_malformed_archive
);
373 /* In a thin archive, a member of an archive-within-an-archive
374 will have the offset in the inner archive encoded here. */
375 if (bfd_is_thin_archive (arch
) && endp
!= NULL
&& *endp
== ':')
377 file_ptr origin
= strtol (endp
+ 1, NULL
, 10);
381 bfd_set_error (bfd_error_malformed_archive
);
389 return bfd_ardata (arch
)->extended_names
+ table_index
;
392 /* This functions reads an arch header and returns an areltdata pointer, or
395 Presumes the file pointer is already in the right place (ie pointing
396 to the ar_hdr in the file). Moves the file pointer; on success it
397 should be pointing to the front of the file contents; on failure it
398 could have been moved arbitrarily. */
401 _bfd_generic_read_ar_hdr (bfd
*abfd
)
403 return _bfd_generic_read_ar_hdr_mag (abfd
, NULL
);
406 /* Alpha ECOFF uses an optional different ARFMAG value, so we have a
407 variant of _bfd_generic_read_ar_hdr which accepts a magic string. */
410 _bfd_generic_read_ar_hdr_mag (bfd
*abfd
, const char *mag
)
413 char *hdrp
= (char *) &hdr
;
415 struct areltdata
*ared
;
416 char *filename
= NULL
;
417 bfd_size_type namelen
= 0;
418 bfd_size_type allocsize
= sizeof (struct areltdata
) + sizeof (struct ar_hdr
);
421 unsigned int extra_size
= 0;
423 if (bfd_bread (hdrp
, sizeof (struct ar_hdr
), abfd
) != sizeof (struct ar_hdr
))
425 if (bfd_get_error () != bfd_error_system_call
)
426 bfd_set_error (bfd_error_no_more_archived_files
);
429 if (strncmp (hdr
.ar_fmag
, ARFMAG
, 2) != 0
431 || strncmp (hdr
.ar_fmag
, mag
, 2) != 0))
433 bfd_set_error (bfd_error_malformed_archive
);
438 parsed_size
= strtol (hdr
.ar_size
, NULL
, 10);
441 bfd_set_error (bfd_error_malformed_archive
);
445 /* Extract the filename from the archive - there are two ways to
446 specify an extended name table, either the first char of the
447 name is a space, or it's a slash. */
448 if ((hdr
.ar_name
[0] == '/'
449 || (hdr
.ar_name
[0] == ' '
450 && memchr (hdr
.ar_name
, '/', ar_maxnamelen (abfd
)) == NULL
))
451 && bfd_ardata (abfd
)->extended_names
!= NULL
)
453 filename
= get_extended_arelt_filename (abfd
, hdr
.ar_name
, &origin
);
454 if (filename
== NULL
)
457 /* BSD4.4-style long filename. */
458 else if (is_bsd44_extended_name (hdr
.ar_name
))
460 /* BSD-4.4 extended name */
461 namelen
= atoi (&hdr
.ar_name
[3]);
462 allocsize
+= namelen
+ 1;
463 parsed_size
-= namelen
;
464 extra_size
= namelen
;
466 allocptr
= (char *) bfd_zalloc (abfd
, allocsize
);
467 if (allocptr
== NULL
)
470 + sizeof (struct areltdata
)
471 + sizeof (struct ar_hdr
));
472 if (bfd_bread (filename
, namelen
, abfd
) != namelen
)
474 if (bfd_get_error () != bfd_error_system_call
)
475 bfd_set_error (bfd_error_no_more_archived_files
);
478 filename
[namelen
] = '\0';
482 /* We judge the end of the name by looking for '/' or ' '.
483 Note: The SYSV format (terminated by '/') allows embedded
484 spaces, so only look for ' ' if we don't find '/'. */
487 e
= (char *) memchr (hdr
.ar_name
, '\0', ar_maxnamelen (abfd
));
490 e
= (char *) memchr (hdr
.ar_name
, '/', ar_maxnamelen (abfd
));
492 e
= (char *) memchr (hdr
.ar_name
, ' ', ar_maxnamelen (abfd
));
496 namelen
= e
- hdr
.ar_name
;
499 /* If we didn't find a termination character, then the name
500 must be the entire field. */
501 namelen
= ar_maxnamelen (abfd
);
504 allocsize
+= namelen
+ 1;
509 allocptr
= (char *) bfd_zalloc (abfd
, allocsize
);
510 if (allocptr
== NULL
)
514 ared
= (struct areltdata
*) allocptr
;
516 ared
->arch_header
= allocptr
+ sizeof (struct areltdata
);
517 memcpy (ared
->arch_header
, &hdr
, sizeof (struct ar_hdr
));
518 ared
->parsed_size
= parsed_size
;
519 ared
->extra_size
= extra_size
;
520 ared
->origin
= origin
;
522 if (filename
!= NULL
)
523 ared
->filename
= filename
;
526 ared
->filename
= allocptr
+ (sizeof (struct areltdata
) +
527 sizeof (struct ar_hdr
));
529 memcpy (ared
->filename
, hdr
.ar_name
, namelen
);
530 ared
->filename
[namelen
] = '\0';
536 /* Append the relative pathname for a member of the thin archive
537 to the pathname of the directory containing the archive. */
540 append_relative_path (bfd
*arch
, char *elt_name
)
542 const char *arch_name
= arch
->filename
;
543 const char *base_name
= lbasename (arch_name
);
547 if (base_name
== arch_name
)
550 prefix_len
= base_name
- arch_name
;
551 filename
= (char *) bfd_alloc (arch
, prefix_len
+ strlen (elt_name
) + 1);
552 if (filename
== NULL
)
555 strncpy (filename
, arch_name
, prefix_len
);
556 strcpy (filename
+ prefix_len
, elt_name
);
560 /* This is an internal function; it's mainly used when indexing
561 through the archive symbol table, but also used to get the next
562 element, since it handles the bookkeeping so nicely for us. */
565 _bfd_get_elt_at_filepos (bfd
*archive
, file_ptr filepos
)
567 struct areltdata
*new_areldata
;
571 if (archive
->my_archive
)
573 filepos
+= archive
->origin
;
574 archive
= archive
->my_archive
;
577 n_nfd
= _bfd_look_for_bfd_in_cache (archive
, filepos
);
581 if (0 > bfd_seek (archive
, filepos
, SEEK_SET
))
584 if ((new_areldata
= (struct areltdata
*) _bfd_read_ar_hdr (archive
)) == NULL
)
587 filename
= new_areldata
->filename
;
589 if (bfd_is_thin_archive (archive
))
591 /* This is a proxy entry for an external file. */
592 if (! IS_ABSOLUTE_PATH (filename
))
594 filename
= append_relative_path (archive
, filename
);
595 if (filename
== NULL
)
599 if (new_areldata
->origin
> 0)
601 /* This proxy entry refers to an element of a nested archive.
602 Locate the member of that archive and return a bfd for it. */
603 bfd
*ext_arch
= _bfd_find_nested_archive (archive
, filename
);
606 || ! bfd_check_format (ext_arch
, bfd_archive
))
608 bfd_release (archive
, new_areldata
);
611 n_nfd
= _bfd_get_elt_at_filepos (ext_arch
, new_areldata
->origin
);
614 bfd_release (archive
, new_areldata
);
617 n_nfd
->proxy_origin
= bfd_tell (archive
);
620 /* It's not an element of a nested archive;
621 open the external file as a bfd. */
622 n_nfd
= bfd_openr (filename
, NULL
);
626 n_nfd
= _bfd_create_empty_archive_element_shell (archive
);
631 bfd_release (archive
, new_areldata
);
635 n_nfd
->proxy_origin
= bfd_tell (archive
);
637 if (bfd_is_thin_archive (archive
))
643 n_nfd
->origin
= n_nfd
->proxy_origin
;
644 n_nfd
->filename
= filename
;
647 n_nfd
->arelt_data
= new_areldata
;
649 if (_bfd_add_bfd_to_archive_cache (archive
, filepos
, n_nfd
))
653 /* FIXME: n_nfd isn't allocated in the archive's memory pool.
654 If we reach this point, I think bfd_release will abort. */
655 bfd_release (archive
, n_nfd
);
656 bfd_release (archive
, new_areldata
);
660 /* Return the BFD which is referenced by the symbol in ABFD indexed by
661 SYM_INDEX. SYM_INDEX should have been returned by bfd_get_next_mapent. */
664 _bfd_generic_get_elt_at_index (bfd
*abfd
, symindex sym_index
)
668 entry
= bfd_ardata (abfd
)->symdefs
+ sym_index
;
669 return _bfd_get_elt_at_filepos (abfd
, entry
->file_offset
);
674 bfd_openr_next_archived_file
677 bfd *bfd_openr_next_archived_file (bfd *archive, bfd *previous);
680 Provided a BFD, @var{archive}, containing an archive and NULL, open
681 an input BFD on the first contained element and returns that.
682 Subsequent calls should pass
683 the archive and the previous return value to return a created
684 BFD to the next contained element. NULL is returned when there
689 bfd_openr_next_archived_file (bfd
*archive
, bfd
*last_file
)
691 if ((bfd_get_format (archive
) != bfd_archive
)
692 || (archive
->direction
== write_direction
))
694 bfd_set_error (bfd_error_invalid_operation
);
698 return BFD_SEND (archive
,
699 openr_next_archived_file
, (archive
, last_file
));
703 bfd_generic_openr_next_archived_file (bfd
*archive
, bfd
*last_file
)
708 filestart
= bfd_ardata (archive
)->first_file_filepos
;
711 unsigned int size
= arelt_size (last_file
);
712 filestart
= last_file
->proxy_origin
;
713 if (! bfd_is_thin_archive (archive
))
715 if (archive
->my_archive
)
716 filestart
-= archive
->origin
;
717 /* Pad to an even boundary...
718 Note that last_file->origin can be odd in the case of
719 BSD-4.4-style element with a long odd size. */
720 filestart
+= filestart
% 2;
723 return _bfd_get_elt_at_filepos (archive
, filestart
);
727 bfd_generic_archive_p (bfd
*abfd
)
729 struct artdata
*tdata_hold
;
730 char armag
[SARMAG
+ 1];
733 if (bfd_bread (armag
, SARMAG
, abfd
) != SARMAG
)
735 if (bfd_get_error () != bfd_error_system_call
)
736 bfd_set_error (bfd_error_wrong_format
);
740 bfd_is_thin_archive (abfd
) = (strncmp (armag
, ARMAGT
, SARMAG
) == 0);
742 if (strncmp (armag
, ARMAG
, SARMAG
) != 0
743 && strncmp (armag
, ARMAGB
, SARMAG
) != 0
744 && ! bfd_is_thin_archive (abfd
))
747 tdata_hold
= bfd_ardata (abfd
);
749 amt
= sizeof (struct artdata
);
750 bfd_ardata (abfd
) = (struct artdata
*) bfd_zalloc (abfd
, amt
);
751 if (bfd_ardata (abfd
) == NULL
)
753 bfd_ardata (abfd
) = tdata_hold
;
757 bfd_ardata (abfd
)->first_file_filepos
= SARMAG
;
758 /* Cleared by bfd_zalloc above.
759 bfd_ardata (abfd)->cache = NULL;
760 bfd_ardata (abfd)->archive_head = NULL;
761 bfd_ardata (abfd)->symdefs = NULL;
762 bfd_ardata (abfd)->extended_names = NULL;
763 bfd_ardata (abfd)->extended_names_size = 0;
764 bfd_ardata (abfd)->tdata = NULL; */
766 if (!BFD_SEND (abfd
, _bfd_slurp_armap
, (abfd
))
767 || !BFD_SEND (abfd
, _bfd_slurp_extended_name_table
, (abfd
)))
769 if (bfd_get_error () != bfd_error_system_call
)
770 bfd_set_error (bfd_error_wrong_format
);
771 bfd_release (abfd
, bfd_ardata (abfd
));
772 bfd_ardata (abfd
) = tdata_hold
;
776 if (bfd_has_map (abfd
))
780 /* This archive has a map, so we may presume that the contents
781 are object files. Make sure that if the first file in the
782 archive can be recognized as an object file, it is for this
783 target. If not, assume that this is the wrong format. If
784 the first file is not an object file, somebody is doing
785 something weird, and we permit it so that ar -t will work.
787 This is done because any normal format will recognize any
788 normal archive, regardless of the format of the object files.
789 We do accept an empty archive. */
791 first
= bfd_openr_next_archived_file (abfd
, NULL
);
794 first
->target_defaulted
= FALSE
;
795 if (bfd_check_format (first
, bfd_object
)
796 && first
->xvec
!= abfd
->xvec
)
798 bfd_set_error (bfd_error_wrong_object_format
);
799 bfd_ardata (abfd
) = tdata_hold
;
802 /* And we ought to close `first' here too. */
809 /* Some constants for a 32 bit BSD archive structure. We do not
810 support 64 bit archives presently; so far as I know, none actually
811 exist. Supporting them would require changing these constants, and
812 changing some H_GET_32 to H_GET_64. */
814 /* The size of an external symdef structure. */
815 #define BSD_SYMDEF_SIZE 8
817 /* The offset from the start of a symdef structure to the file offset. */
818 #define BSD_SYMDEF_OFFSET_SIZE 4
820 /* The size of the symdef count. */
821 #define BSD_SYMDEF_COUNT_SIZE 4
823 /* The size of the string count. */
824 #define BSD_STRING_COUNT_SIZE 4
826 /* Read a BSD-style archive symbol table. Returns FALSE on error,
830 do_slurp_bsd_armap (bfd
*abfd
)
832 struct areltdata
*mapdata
;
833 unsigned int counter
;
834 bfd_byte
*raw_armap
, *rbase
;
835 struct artdata
*ardata
= bfd_ardata (abfd
);
837 bfd_size_type parsed_size
, amt
;
840 mapdata
= (struct areltdata
*) _bfd_read_ar_hdr (abfd
);
843 parsed_size
= mapdata
->parsed_size
;
844 bfd_release (abfd
, mapdata
); /* Don't need it any more. */
846 raw_armap
= (bfd_byte
*) bfd_zalloc (abfd
, parsed_size
);
847 if (raw_armap
== NULL
)
850 if (bfd_bread (raw_armap
, parsed_size
, abfd
) != parsed_size
)
852 if (bfd_get_error () != bfd_error_system_call
)
853 bfd_set_error (bfd_error_malformed_archive
);
855 bfd_release (abfd
, raw_armap
);
859 ardata
->symdef_count
= H_GET_32 (abfd
, raw_armap
) / BSD_SYMDEF_SIZE
;
861 if (ardata
->symdef_count
* BSD_SYMDEF_SIZE
>
862 parsed_size
- BSD_SYMDEF_COUNT_SIZE
)
864 /* Probably we're using the wrong byte ordering. */
865 bfd_set_error (bfd_error_wrong_format
);
870 rbase
= raw_armap
+ BSD_SYMDEF_COUNT_SIZE
;
871 stringbase
= ((char *) rbase
872 + ardata
->symdef_count
* BSD_SYMDEF_SIZE
873 + BSD_STRING_COUNT_SIZE
);
874 amt
= ardata
->symdef_count
* sizeof (carsym
);
875 ardata
->symdefs
= (struct carsym
*) bfd_alloc (abfd
, amt
);
876 if (!ardata
->symdefs
)
879 for (counter
= 0, set
= ardata
->symdefs
;
880 counter
< ardata
->symdef_count
;
881 counter
++, set
++, rbase
+= BSD_SYMDEF_SIZE
)
883 set
->name
= H_GET_32 (abfd
, rbase
) + stringbase
;
884 set
->file_offset
= H_GET_32 (abfd
, rbase
+ BSD_SYMDEF_OFFSET_SIZE
);
887 ardata
->first_file_filepos
= bfd_tell (abfd
);
888 /* Pad to an even boundary if you have to. */
889 ardata
->first_file_filepos
+= (ardata
->first_file_filepos
) % 2;
890 /* FIXME, we should provide some way to free raw_ardata when
891 we are done using the strings from it. For now, it seems
892 to be allocated on an objalloc anyway... */
893 bfd_has_map (abfd
) = TRUE
;
897 /* Read a COFF archive symbol table. Returns FALSE on error, TRUE
901 do_slurp_coff_armap (bfd
*abfd
)
903 struct areltdata
*mapdata
;
904 int *raw_armap
, *rawptr
;
905 struct artdata
*ardata
= bfd_ardata (abfd
);
907 bfd_size_type stringsize
;
908 unsigned int parsed_size
;
910 bfd_size_type nsymz
; /* Number of symbols in armap. */
911 bfd_vma (*swap
) (const void *);
912 char int_buf
[sizeof (long)];
913 bfd_size_type carsym_size
, ptrsize
;
916 mapdata
= (struct areltdata
*) _bfd_read_ar_hdr (abfd
);
919 parsed_size
= mapdata
->parsed_size
;
920 bfd_release (abfd
, mapdata
); /* Don't need it any more. */
922 if (bfd_bread (int_buf
, 4, abfd
) != 4)
924 if (bfd_get_error () != bfd_error_system_call
)
925 bfd_set_error (bfd_error_malformed_archive
);
928 /* It seems that all numeric information in a coff archive is always
929 in big endian format, nomatter the host or target. */
931 nsymz
= bfd_getb32 (int_buf
);
932 stringsize
= parsed_size
- (4 * nsymz
) - 4;
934 /* ... except that some archive formats are broken, and it may be our
935 fault - the i960 little endian coff sometimes has big and sometimes
936 little, because our tools changed. Here's a horrible hack to clean
939 if (stringsize
> 0xfffff
940 && bfd_get_arch (abfd
) == bfd_arch_i960
941 && bfd_get_flavour (abfd
) == bfd_target_coff_flavour
)
943 /* This looks dangerous, let's do it the other way around. */
944 nsymz
= bfd_getl32 (int_buf
);
945 stringsize
= parsed_size
- (4 * nsymz
) - 4;
949 /* The coff armap must be read sequentially. So we construct a
950 bsd-style one in core all at once, for simplicity. */
952 if (nsymz
> ~ (bfd_size_type
) 0 / sizeof (carsym
))
955 carsym_size
= (nsymz
* sizeof (carsym
));
956 ptrsize
= (4 * nsymz
);
958 if (carsym_size
+ stringsize
+ 1 <= carsym_size
)
961 ardata
->symdefs
= (struct carsym
*) bfd_zalloc (abfd
,
962 carsym_size
+ stringsize
+ 1);
963 if (ardata
->symdefs
== NULL
)
965 carsyms
= ardata
->symdefs
;
966 stringbase
= ((char *) ardata
->symdefs
) + carsym_size
;
968 /* Allocate and read in the raw offsets. */
969 raw_armap
= (int *) bfd_alloc (abfd
, ptrsize
);
970 if (raw_armap
== NULL
)
971 goto release_symdefs
;
972 if (bfd_bread (raw_armap
, ptrsize
, abfd
) != ptrsize
973 || (bfd_bread (stringbase
, stringsize
, abfd
) != stringsize
))
975 if (bfd_get_error () != bfd_error_system_call
)
976 bfd_set_error (bfd_error_malformed_archive
);
977 goto release_raw_armap
;
980 /* OK, build the carsyms. */
981 for (i
= 0; i
< nsymz
; i
++)
983 rawptr
= raw_armap
+ i
;
984 carsyms
->file_offset
= swap ((bfd_byte
*) rawptr
);
985 carsyms
->name
= stringbase
;
986 stringbase
+= strlen (stringbase
) + 1;
991 ardata
->symdef_count
= nsymz
;
992 ardata
->first_file_filepos
= bfd_tell (abfd
);
993 /* Pad to an even boundary if you have to. */
994 ardata
->first_file_filepos
+= (ardata
->first_file_filepos
) % 2;
996 bfd_has_map (abfd
) = TRUE
;
997 bfd_release (abfd
, raw_armap
);
999 /* Check for a second archive header (as used by PE). */
1001 struct areltdata
*tmp
;
1003 bfd_seek (abfd
, ardata
->first_file_filepos
, SEEK_SET
);
1004 tmp
= (struct areltdata
*) _bfd_read_ar_hdr (abfd
);
1007 if (tmp
->arch_header
[0] == '/'
1008 && tmp
->arch_header
[1] == ' ')
1010 ardata
->first_file_filepos
+=
1011 (tmp
->parsed_size
+ sizeof (struct ar_hdr
) + 1) & ~(unsigned) 1;
1013 bfd_release (abfd
, tmp
);
1020 bfd_release (abfd
, raw_armap
);
1022 bfd_release (abfd
, (ardata
)->symdefs
);
1026 /* This routine can handle either coff-style or bsd-style armaps
1027 (archive symbol table). Returns FALSE on error, TRUE otherwise */
1030 bfd_slurp_armap (bfd
*abfd
)
1033 int i
= bfd_bread (nextname
, 16, abfd
);
1040 if (bfd_seek (abfd
, (file_ptr
) -16, SEEK_CUR
) != 0)
1043 if (CONST_STRNEQ (nextname
, "__.SYMDEF ")
1044 || CONST_STRNEQ (nextname
, "__.SYMDEF/ ")) /* Old Linux archives. */
1045 return do_slurp_bsd_armap (abfd
);
1046 else if (CONST_STRNEQ (nextname
, "/ "))
1047 return do_slurp_coff_armap (abfd
);
1048 else if (CONST_STRNEQ (nextname
, "/SYM64/ "))
1050 /* 64bit ELF (Irix 6) archive. */
1052 extern bfd_boolean
bfd_elf64_archive_slurp_armap (bfd
*);
1053 return bfd_elf64_archive_slurp_armap (abfd
);
1055 bfd_set_error (bfd_error_wrong_format
);
1059 else if (CONST_STRNEQ (nextname
, "#1/20 "))
1061 /* Mach-O has a special name for armap when the map is sorted by name.
1062 However because this name has a space it is slightly more difficult
1067 if (bfd_bread (&hdr
, sizeof (hdr
), abfd
) != sizeof (hdr
))
1069 /* Read the extended name. We know its length. */
1070 if (bfd_bread (extname
, 20, abfd
) != 20)
1072 if (bfd_seek (abfd
, (file_ptr
) -(sizeof (hdr
) + 20), SEEK_CUR
) != 0)
1074 if (CONST_STRNEQ (extname
, "__.SYMDEF SORTED")
1075 || CONST_STRNEQ (extname
, "__.SYMDEF"))
1076 return do_slurp_bsd_armap (abfd
);
1079 bfd_has_map (abfd
) = FALSE
;
1083 /* Returns FALSE on error, TRUE otherwise. */
1084 /* Flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the
1085 header is in a slightly different order and the map name is '/'.
1086 This flavour is used by hp300hpux. */
1088 #define HPUX_SYMDEF_COUNT_SIZE 2
1091 bfd_slurp_bsd_armap_f2 (bfd
*abfd
)
1093 struct areltdata
*mapdata
;
1095 unsigned int counter
;
1096 bfd_byte
*raw_armap
, *rbase
;
1097 struct artdata
*ardata
= bfd_ardata (abfd
);
1099 unsigned int stringsize
;
1102 int i
= bfd_bread (nextname
, 16, abfd
);
1109 /* The archive has at least 16 bytes in it. */
1110 if (bfd_seek (abfd
, (file_ptr
) -16, SEEK_CUR
) != 0)
1113 if (CONST_STRNEQ (nextname
, "__.SYMDEF ")
1114 || CONST_STRNEQ (nextname
, "__.SYMDEF/ ")) /* Old Linux archives. */
1115 return do_slurp_bsd_armap (abfd
);
1117 if (! CONST_STRNEQ (nextname
, "/ "))
1119 bfd_has_map (abfd
) = FALSE
;
1123 mapdata
= (struct areltdata
*) _bfd_read_ar_hdr (abfd
);
1124 if (mapdata
== NULL
)
1127 amt
= mapdata
->parsed_size
;
1128 raw_armap
= (bfd_byte
*) bfd_zalloc (abfd
, amt
);
1129 if (raw_armap
== NULL
)
1132 bfd_release (abfd
, mapdata
);
1136 if (bfd_bread (raw_armap
, amt
, abfd
) != amt
)
1138 if (bfd_get_error () != bfd_error_system_call
)
1139 bfd_set_error (bfd_error_malformed_archive
);
1141 bfd_release (abfd
, raw_armap
);
1145 ardata
->symdef_count
= H_GET_16 (abfd
, raw_armap
);
1147 if (ardata
->symdef_count
* BSD_SYMDEF_SIZE
1148 > mapdata
->parsed_size
- HPUX_SYMDEF_COUNT_SIZE
)
1150 /* Probably we're using the wrong byte ordering. */
1151 bfd_set_error (bfd_error_wrong_format
);
1157 stringsize
= H_GET_32 (abfd
, raw_armap
+ HPUX_SYMDEF_COUNT_SIZE
);
1158 /* Skip sym count and string sz. */
1159 stringbase
= ((char *) raw_armap
1160 + HPUX_SYMDEF_COUNT_SIZE
1161 + BSD_STRING_COUNT_SIZE
);
1162 rbase
= (bfd_byte
*) stringbase
+ stringsize
;
1163 amt
= ardata
->symdef_count
* BSD_SYMDEF_SIZE
;
1164 ardata
->symdefs
= (struct carsym
*) bfd_alloc (abfd
, amt
);
1165 if (!ardata
->symdefs
)
1168 for (counter
= 0, set
= ardata
->symdefs
;
1169 counter
< ardata
->symdef_count
;
1170 counter
++, set
++, rbase
+= BSD_SYMDEF_SIZE
)
1172 set
->name
= H_GET_32 (abfd
, rbase
) + stringbase
;
1173 set
->file_offset
= H_GET_32 (abfd
, rbase
+ BSD_SYMDEF_OFFSET_SIZE
);
1176 ardata
->first_file_filepos
= bfd_tell (abfd
);
1177 /* Pad to an even boundary if you have to. */
1178 ardata
->first_file_filepos
+= (ardata
->first_file_filepos
) % 2;
1179 /* FIXME, we should provide some way to free raw_ardata when
1180 we are done using the strings from it. For now, it seems
1181 to be allocated on an objalloc anyway... */
1182 bfd_has_map (abfd
) = TRUE
;
1186 /** Extended name table.
1188 Normally archives support only 14-character filenames.
1190 Intel has extended the format: longer names are stored in a special
1191 element (the first in the archive, or second if there is an armap);
1192 the name in the ar_hdr is replaced by <space><index into filename
1193 element>. Index is the P.R. of an int (decimal). Data General have
1194 extended the format by using the prefix // for the special element. */
1196 /* Returns FALSE on error, TRUE otherwise. */
1199 _bfd_slurp_extended_name_table (bfd
*abfd
)
1202 struct areltdata
*namedata
;
1205 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
1206 we probably don't want to return TRUE. */
1207 bfd_seek (abfd
, bfd_ardata (abfd
)->first_file_filepos
, SEEK_SET
);
1208 if (bfd_bread (nextname
, 16, abfd
) == 16)
1210 if (bfd_seek (abfd
, (file_ptr
) -16, SEEK_CUR
) != 0)
1213 if (! CONST_STRNEQ (nextname
, "ARFILENAMES/ ")
1214 && ! CONST_STRNEQ (nextname
, "// "))
1216 bfd_ardata (abfd
)->extended_names
= NULL
;
1217 bfd_ardata (abfd
)->extended_names_size
= 0;
1221 namedata
= (struct areltdata
*) _bfd_read_ar_hdr (abfd
);
1222 if (namedata
== NULL
)
1225 amt
= namedata
->parsed_size
;
1229 bfd_ardata (abfd
)->extended_names_size
= amt
;
1230 bfd_ardata (abfd
)->extended_names
= (char *) bfd_zalloc (abfd
, amt
+ 1);
1231 if (bfd_ardata (abfd
)->extended_names
== NULL
)
1234 bfd_release (abfd
, namedata
);
1238 if (bfd_bread (bfd_ardata (abfd
)->extended_names
, amt
, abfd
) != amt
)
1240 if (bfd_get_error () != bfd_error_system_call
)
1241 bfd_set_error (bfd_error_malformed_archive
);
1242 bfd_release (abfd
, (bfd_ardata (abfd
)->extended_names
));
1243 bfd_ardata (abfd
)->extended_names
= NULL
;
1247 /* Since the archive is supposed to be printable if it contains
1248 text, the entries in the list are newline-padded, not null
1249 padded. In SVR4-style archives, the names also have a
1250 trailing '/'. DOS/NT created archive often have \ in them
1251 We'll fix all problems here.. */
1253 char *ext_names
= bfd_ardata (abfd
)->extended_names
;
1254 char *temp
= ext_names
;
1255 char *limit
= temp
+ namedata
->parsed_size
;
1256 for (; temp
< limit
; ++temp
)
1258 if (*temp
== ARFMAG
[1])
1259 temp
[temp
> ext_names
&& temp
[-1] == '/' ? -1 : 0] = '\0';
1266 /* Pad to an even boundary if you have to. */
1267 bfd_ardata (abfd
)->first_file_filepos
= bfd_tell (abfd
);
1268 bfd_ardata (abfd
)->first_file_filepos
+=
1269 (bfd_ardata (abfd
)->first_file_filepos
) % 2;
1271 /* FIXME, we can't release namedata here because it was allocated
1272 below extended_names on the objalloc... */
1279 /* Return a copy of the stuff in the filename between any :]> and a
1283 normalize (bfd
*abfd
, const char *file
)
1289 first
= file
+ strlen (file
) - 1;
1292 while (first
!= file
)
1296 if (*first
== ':' || *first
== ']' || *first
== '>')
1304 copy
= bfd_alloc (abfd
, last
- first
+ 1);
1308 memcpy (copy
, first
, last
- first
);
1309 copy
[last
- first
] = 0;
1316 normalize (bfd
*abfd ATTRIBUTE_UNUSED
, const char *file
)
1318 return lbasename (file
);
1322 /* Adjust a relative path name based on the reference path. */
1325 adjust_relative_path (const char * path
, const char * ref_path
)
1327 static char *pathbuf
= NULL
;
1328 static int pathbuf_len
= 0;
1329 const char *pathp
= path
;
1330 const char *refp
= ref_path
;
1331 int element_count
= 0;
1335 /* Remove common leading path elements. */
1338 const char *e1
= pathp
;
1339 const char *e2
= refp
;
1341 while (*e1
&& ! IS_DIR_SEPARATOR (*e1
))
1343 while (*e2
&& ! IS_DIR_SEPARATOR (*e2
))
1345 if (*e1
== '\0' || *e2
== '\0' || e1
- pathp
!= e2
- refp
1346 || strncmp (pathp
, refp
, e1
- pathp
) != 0)
1352 /* For each leading path element in the reference path,
1353 insert "../" into the path. */
1354 for (; *refp
; ++refp
)
1355 if (IS_DIR_SEPARATOR (*refp
))
1357 len
= 3 * element_count
+ strlen (path
) + 1;
1359 if (len
> pathbuf_len
)
1361 if (pathbuf
!= NULL
)
1364 pathbuf
= (char *) bfd_malloc (len
);
1365 if (pathbuf
== NULL
)
1371 while (element_count
-- > 0)
1373 /* FIXME: Support Windows style path separators as well. */
1374 strcpy (newp
, "../");
1377 strcpy (newp
, pathp
);
1382 /* Build a BFD style extended name table. */
1385 _bfd_archive_bsd_construct_extended_name_table (bfd
*abfd
,
1387 bfd_size_type
*tablen
,
1390 *name
= "ARFILENAMES/";
1391 return _bfd_construct_extended_name_table (abfd
, FALSE
, tabloc
, tablen
);
1394 /* Build an SVR4 style extended name table. */
1397 _bfd_archive_coff_construct_extended_name_table (bfd
*abfd
,
1399 bfd_size_type
*tablen
,
1403 return _bfd_construct_extended_name_table (abfd
, TRUE
, tabloc
, tablen
);
1406 /* Follows archive_head and produces an extended name table if
1407 necessary. Returns (in tabloc) a pointer to an extended name
1408 table, and in tablen the length of the table. If it makes an entry
1409 it clobbers the filename so that the element may be written without
1410 further massage. Returns TRUE if it ran successfully, FALSE if
1411 something went wrong. A successful return may still involve a
1412 zero-length tablen! */
1415 _bfd_construct_extended_name_table (bfd
*abfd
,
1416 bfd_boolean trailing_slash
,
1418 bfd_size_type
*tablen
)
1420 unsigned int maxname
= abfd
->xvec
->ar_max_namelen
;
1421 bfd_size_type total_namelen
= 0;
1424 const char *last_filename
;
1428 last_filename
= NULL
;
1430 /* Figure out how long the table should be. */
1431 for (current
= abfd
->archive_head
;
1433 current
= current
->archive_next
)
1436 unsigned int thislen
;
1438 if (bfd_is_thin_archive (abfd
))
1440 const char *filename
= current
->filename
;
1442 /* If the element being added is a member of another archive
1443 (i.e., we are flattening), use the containing archive's name. */
1444 if (current
->my_archive
1445 && ! bfd_is_thin_archive (current
->my_archive
))
1446 filename
= current
->my_archive
->filename
;
1448 /* If the path is the same as the previous path seen,
1449 reuse it. This can happen when flattening a thin
1450 archive that contains other archives. */
1451 if (last_filename
&& strcmp (last_filename
, filename
) == 0)
1454 last_filename
= filename
;
1456 /* If the path is relative, adjust it relative to
1457 the containing archive. */
1458 if (! IS_ABSOLUTE_PATH (filename
)
1459 && ! IS_ABSOLUTE_PATH (abfd
->filename
))
1460 normal
= adjust_relative_path (filename
, abfd
->filename
);
1464 /* In a thin archive, always store the full pathname
1465 in the extended name table. */
1466 total_namelen
+= strlen (normal
) + 1;
1468 /* Leave room for trailing slash. */
1474 normal
= normalize (current
, current
->filename
);
1478 thislen
= strlen (normal
);
1480 if (thislen
> maxname
1481 && (bfd_get_file_flags (abfd
) & BFD_TRADITIONAL_FORMAT
) != 0)
1484 if (thislen
> maxname
)
1486 /* Add one to leave room for \n. */
1487 total_namelen
+= thislen
+ 1;
1490 /* Leave room for trailing slash. */
1496 struct ar_hdr
*hdr
= arch_hdr (current
);
1497 if (strncmp (normal
, hdr
->ar_name
, thislen
) != 0
1498 || (thislen
< sizeof hdr
->ar_name
1499 && hdr
->ar_name
[thislen
] != ar_padchar (current
)))
1501 /* Must have been using extended format even though it
1502 didn't need to. Fix it to use normal format. */
1503 memcpy (hdr
->ar_name
, normal
, thislen
);
1504 if (thislen
< maxname
1505 || (thislen
== maxname
&& thislen
< sizeof hdr
->ar_name
))
1506 hdr
->ar_name
[thislen
] = ar_padchar (current
);
1511 if (total_namelen
== 0)
1514 *tabloc
= (char *) bfd_zalloc (abfd
, total_namelen
);
1515 if (*tabloc
== NULL
)
1518 *tablen
= total_namelen
;
1521 last_filename
= NULL
;
1524 for (current
= abfd
->archive_head
;
1526 current
= current
->archive_next
)
1529 unsigned int thislen
;
1531 const char *filename
= current
->filename
;
1533 if (bfd_is_thin_archive (abfd
))
1535 /* If the element being added is a member of another archive
1536 (i.e., we are flattening), use the containing archive's name. */
1537 if (current
->my_archive
1538 && ! bfd_is_thin_archive (current
->my_archive
))
1539 filename
= current
->my_archive
->filename
;
1540 /* If the path is the same as the previous path seen,
1541 reuse it. This can happen when flattening a thin
1542 archive that contains other archives.
1543 If the path is relative, adjust it relative to
1544 the containing archive. */
1545 if (last_filename
&& strcmp (last_filename
, filename
) == 0)
1546 normal
= last_filename
;
1547 else if (! IS_ABSOLUTE_PATH (filename
)
1548 && ! IS_ABSOLUTE_PATH (abfd
->filename
))
1549 normal
= adjust_relative_path (filename
, abfd
->filename
);
1555 normal
= normalize (current
, filename
);
1560 thislen
= strlen (normal
);
1561 if (thislen
> maxname
|| bfd_is_thin_archive (abfd
))
1563 /* Works for now; may need to be re-engineered if we
1564 encounter an oddball archive format and want to
1565 generalise this hack. */
1566 struct ar_hdr
*hdr
= arch_hdr (current
);
1567 if (normal
== last_filename
)
1568 stroff
= last_stroff
;
1571 strcpy (strptr
, normal
);
1572 if (! trailing_slash
)
1573 strptr
[thislen
] = ARFMAG
[1];
1576 strptr
[thislen
] = '/';
1577 strptr
[thislen
+ 1] = ARFMAG
[1];
1579 stroff
= strptr
- *tabloc
;
1580 last_stroff
= stroff
;
1582 hdr
->ar_name
[0] = ar_padchar (current
);
1583 if (bfd_is_thin_archive (abfd
) && current
->origin
> 0)
1585 int len
= snprintf (hdr
->ar_name
+ 1, maxname
- 1, "%-ld:",
1587 _bfd_ar_spacepad (hdr
->ar_name
+ 1 + len
, maxname
- 1 - len
,
1589 current
->origin
- sizeof (struct ar_hdr
));
1592 _bfd_ar_spacepad (hdr
->ar_name
+ 1, maxname
- 1, "%-ld", stroff
);
1593 if (normal
!= last_filename
)
1595 strptr
+= thislen
+ 1;
1598 last_filename
= filename
;
1606 /* Do not construct an extended name table but transforms name field into
1607 its extended form. */
1610 _bfd_archive_bsd44_construct_extended_name_table (bfd
*abfd
,
1612 bfd_size_type
*tablen
,
1615 unsigned int maxname
= abfd
->xvec
->ar_max_namelen
;
1622 for (current
= abfd
->archive_head
;
1624 current
= current
->archive_next
)
1626 const char *normal
= normalize (current
, current
->filename
);
1633 for (len
= 0; normal
[len
]; len
++)
1634 if (normal
[len
] == ' ')
1637 if (len
> maxname
|| has_space
)
1639 struct ar_hdr
*hdr
= arch_hdr (current
);
1641 len
= (len
+ 3) & ~3;
1642 arch_eltdata (current
)->extra_size
= len
;
1643 _bfd_ar_spacepad (hdr
->ar_name
, maxname
, "#1/%u", len
);
1650 /* Write an archive header. */
1653 _bfd_generic_write_ar_hdr (bfd
*archive
, bfd
*abfd
)
1655 struct ar_hdr
*hdr
= arch_hdr (abfd
);
1657 if (bfd_bwrite (hdr
, sizeof (*hdr
), archive
) != sizeof (*hdr
))
1662 /* Write an archive header using BSD4.4 convention. */
1665 _bfd_bsd44_write_ar_hdr (bfd
*archive
, bfd
*abfd
)
1667 struct ar_hdr
*hdr
= arch_hdr (abfd
);
1669 if (is_bsd44_extended_name (hdr
->ar_name
))
1671 /* This is a BSD 4.4 extended name. */
1672 const char *fullname
= normalize (abfd
, abfd
->filename
);
1673 unsigned int len
= strlen (fullname
);
1674 unsigned int padded_len
= (len
+ 3) & ~3;
1676 BFD_ASSERT (padded_len
== arch_eltdata (abfd
)->extra_size
);
1678 _bfd_ar_spacepad (hdr
->ar_size
, sizeof (hdr
->ar_size
), "%-10ld",
1679 arch_eltdata (abfd
)->parsed_size
+ padded_len
);
1681 if (bfd_bwrite (hdr
, sizeof (*hdr
), archive
) != sizeof (*hdr
))
1684 if (bfd_bwrite (fullname
, len
, archive
) != len
)
1688 static const char pad
[3] = { 0, 0, 0 };
1690 len
= 4 - (len
& 3);
1691 if (bfd_bwrite (pad
, len
, archive
) != len
)
1697 if (bfd_bwrite (hdr
, sizeof (*hdr
), archive
) != sizeof (*hdr
))
1703 /* A couple of functions for creating ar_hdrs. */
1705 #ifdef HPUX_LARGE_AR_IDS
1706 /* Function to encode large UID/GID values according to HP. */
1709 hpux_uid_gid_encode (char str
[6], long int id
)
1713 str
[5] = '@' + (id
& 3);
1716 for (cnt
= 4; cnt
>= 0; --cnt
, id
>>= 6)
1717 str
[cnt
] = ' ' + (id
& 0x3f);
1719 #endif /* HPUX_LARGE_AR_IDS */
1729 /* Takes a filename, returns an arelt_data for it, or NULL if it can't
1730 make one. The filename must refer to a filename in the filesystem.
1731 The filename field of the ar_hdr will NOT be initialized. If member
1732 is set, and it's an in-memory bfd, we fake it. */
1734 static struct areltdata
*
1735 bfd_ar_hdr_from_filesystem (bfd
*abfd
, const char *filename
, bfd
*member
)
1738 struct areltdata
*ared
;
1742 if (member
&& (member
->flags
& BFD_IN_MEMORY
) != 0)
1744 /* Assume we just "made" the member, and fake it. */
1745 struct bfd_in_memory
*bim
= (struct bfd_in_memory
*) member
->iostream
;
1746 time (&status
.st_mtime
);
1747 status
.st_uid
= getuid ();
1748 status
.st_gid
= getgid ();
1749 status
.st_mode
= 0644;
1750 status
.st_size
= bim
->size
;
1752 else if (stat (filename
, &status
) != 0)
1754 bfd_set_error (bfd_error_system_call
);
1758 /* If the caller requested that the BFD generate deterministic output,
1759 fake values for modification time, UID, GID, and file mode. */
1760 if ((abfd
->flags
& BFD_DETERMINISTIC_OUTPUT
) != 0)
1762 status
.st_mtime
= 0;
1765 status
.st_mode
= 0644;
1768 amt
= sizeof (struct ar_hdr
) + sizeof (struct areltdata
);
1769 ared
= (struct areltdata
*) bfd_zalloc (abfd
, amt
);
1772 hdr
= (struct ar_hdr
*) (((char *) ared
) + sizeof (struct areltdata
));
1774 /* ar headers are space padded, not null padded! */
1775 memset (hdr
, ' ', sizeof (struct ar_hdr
));
1777 _bfd_ar_spacepad (hdr
->ar_date
, sizeof (hdr
->ar_date
), "%-12ld",
1779 #ifdef HPUX_LARGE_AR_IDS
1780 /* HP has a very "special" way to handle UID/GID's with numeric values
1782 if (status
.st_uid
> 99999)
1783 hpux_uid_gid_encode (hdr
->ar_uid
, (long) status
.st_uid
);
1786 _bfd_ar_spacepad (hdr
->ar_uid
, sizeof (hdr
->ar_uid
), "%ld",
1788 #ifdef HPUX_LARGE_AR_IDS
1789 /* HP has a very "special" way to handle UID/GID's with numeric values
1791 if (status
.st_gid
> 99999)
1792 hpux_uid_gid_encode (hdr
->ar_gid
, (long) status
.st_gid
);
1795 _bfd_ar_spacepad (hdr
->ar_gid
, sizeof (hdr
->ar_gid
), "%ld",
1797 _bfd_ar_spacepad (hdr
->ar_mode
, sizeof (hdr
->ar_mode
), "%-8lo",
1799 _bfd_ar_spacepad (hdr
->ar_size
, sizeof (hdr
->ar_size
), "%-10ld",
1801 memcpy (hdr
->ar_fmag
, ARFMAG
, 2);
1802 ared
->parsed_size
= status
.st_size
;
1803 ared
->arch_header
= (char *) hdr
;
1808 /* Analogous to stat call. */
1811 bfd_generic_stat_arch_elt (bfd
*abfd
, struct stat
*buf
)
1816 if (abfd
->arelt_data
== NULL
)
1818 bfd_set_error (bfd_error_invalid_operation
);
1822 hdr
= arch_hdr (abfd
);
1824 #define foo(arelt, stelt, size) \
1825 buf->stelt = strtol (hdr->arelt, &aloser, size); \
1826 if (aloser == hdr->arelt) \
1829 /* Some platforms support special notations for large IDs. */
1830 #ifdef HPUX_LARGE_AR_IDS
1831 # define foo2(arelt, stelt, size) \
1832 if (hdr->arelt[5] == ' ') \
1834 foo (arelt, stelt, size); \
1839 for (buf->stelt = cnt = 0; cnt < 5; ++cnt) \
1841 if (hdr->arelt[cnt] < ' ' || hdr->arelt[cnt] > ' ' + 0x3f) \
1844 buf->stelt += hdr->arelt[cnt] - ' '; \
1846 if (hdr->arelt[5] < '@' || hdr->arelt[5] > '@' + 3) \
1849 buf->stelt += hdr->arelt[5] - '@'; \
1852 # define foo2(arelt, stelt, size) foo (arelt, stelt, size)
1855 foo (ar_date
, st_mtime
, 10);
1856 foo2 (ar_uid
, st_uid
, 10);
1857 foo2 (ar_gid
, st_gid
, 10);
1858 foo (ar_mode
, st_mode
, 8);
1860 buf
->st_size
= arch_eltdata (abfd
)->parsed_size
;
1866 bfd_dont_truncate_arname (bfd
*abfd
, const char *pathname
, char *arhdr
)
1868 /* FIXME: This interacts unpleasantly with ar's quick-append option.
1869 Fortunately ic960 users will never use that option. Fixing this
1870 is very hard; fortunately I know how to do it and will do so once
1871 intel's release is out the door. */
1873 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
1875 const char *filename
;
1876 size_t maxlen
= ar_maxnamelen (abfd
);
1878 if ((bfd_get_file_flags (abfd
) & BFD_TRADITIONAL_FORMAT
) != 0)
1880 bfd_bsd_truncate_arname (abfd
, pathname
, arhdr
);
1884 filename
= normalize (abfd
, pathname
);
1885 if (filename
== NULL
)
1891 length
= strlen (filename
);
1893 if (length
<= maxlen
)
1894 memcpy (hdr
->ar_name
, filename
, length
);
1896 /* Add the padding character if there is room for it. */
1898 || (length
== maxlen
&& length
< sizeof hdr
->ar_name
))
1899 (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
1903 bfd_bsd_truncate_arname (bfd
*abfd
, const char *pathname
, char *arhdr
)
1905 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
1907 const char *filename
= lbasename (pathname
);
1908 size_t maxlen
= ar_maxnamelen (abfd
);
1910 length
= strlen (filename
);
1912 if (length
<= maxlen
)
1913 memcpy (hdr
->ar_name
, filename
, length
);
1916 /* pathname: meet procrustes */
1917 memcpy (hdr
->ar_name
, filename
, maxlen
);
1921 if (length
< maxlen
)
1922 (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
1925 /* Store name into ar header. Truncates the name to fit.
1926 1> strip pathname to be just the basename.
1927 2> if it's short enuf to fit, stuff it in.
1928 3> If it doesn't end with .o, truncate it to fit
1929 4> truncate it before the .o, append .o, stuff THAT in. */
1931 /* This is what gnu ar does. It's better but incompatible with the
1935 bfd_gnu_truncate_arname (bfd
*abfd
, const char *pathname
, char *arhdr
)
1937 struct ar_hdr
*hdr
= (struct ar_hdr
*) arhdr
;
1939 const char *filename
= lbasename (pathname
);
1940 size_t maxlen
= ar_maxnamelen (abfd
);
1942 length
= strlen (filename
);
1944 if (length
<= maxlen
)
1945 memcpy (hdr
->ar_name
, filename
, length
);
1948 /* pathname: meet procrustes. */
1949 memcpy (hdr
->ar_name
, filename
, maxlen
);
1950 if ((filename
[length
- 2] == '.') && (filename
[length
- 1] == 'o'))
1952 hdr
->ar_name
[maxlen
- 2] = '.';
1953 hdr
->ar_name
[maxlen
- 1] = 'o';
1959 (hdr
->ar_name
)[length
] = ar_padchar (abfd
);
1962 /* The BFD is open for write and has its format set to bfd_archive. */
1965 _bfd_write_archive_contents (bfd
*arch
)
1968 char *etable
= NULL
;
1969 bfd_size_type elength
= 0;
1970 const char *ename
= NULL
;
1971 bfd_boolean makemap
= bfd_has_map (arch
);
1972 /* If no .o's, don't bother to make a map. */
1973 bfd_boolean hasobjects
= FALSE
;
1974 bfd_size_type wrote
;
1978 /* Verify the viability of all entries; if any of them live in the
1979 filesystem (as opposed to living in an archive open for input)
1980 then construct a fresh ar_hdr for them. */
1981 for (current
= arch
->archive_head
;
1983 current
= current
->archive_next
)
1985 /* This check is checking the bfds for the objects we're reading
1986 from (which are usually either an object file or archive on
1987 disk), not the archive entries we're writing to. We don't
1988 actually create bfds for the archive members, we just copy
1989 them byte-wise when we write out the archive. */
1990 if (bfd_write_p (current
))
1992 bfd_set_error (bfd_error_invalid_operation
);
1995 if (!current
->arelt_data
)
1997 current
->arelt_data
=
1998 bfd_ar_hdr_from_filesystem (arch
, current
->filename
, current
);
1999 if (!current
->arelt_data
)
2002 /* Put in the file name. */
2003 BFD_SEND (arch
, _bfd_truncate_arname
,
2004 (arch
, current
->filename
, (char *) arch_hdr (current
)));
2007 if (makemap
&& ! hasobjects
)
2008 { /* Don't bother if we won't make a map! */
2009 if ((bfd_check_format (current
, bfd_object
)))
2014 if (!BFD_SEND (arch
, _bfd_construct_extended_name_table
,
2015 (arch
, &etable
, &elength
, &ename
)))
2018 if (bfd_seek (arch
, (file_ptr
) 0, SEEK_SET
) != 0)
2021 if (bfd_is_thin_archive (arch
))
2023 wrote
= bfd_bwrite (armag
, SARMAG
, arch
);
2024 if (wrote
!= SARMAG
)
2027 if (makemap
&& hasobjects
)
2029 if (! _bfd_compute_and_write_armap (arch
, (unsigned int) elength
))
2037 memset (&hdr
, ' ', sizeof (struct ar_hdr
));
2038 memcpy (hdr
.ar_name
, ename
, strlen (ename
));
2039 /* Round size up to even number in archive header. */
2040 _bfd_ar_spacepad (hdr
.ar_size
, sizeof (hdr
.ar_size
), "%-10ld",
2041 (elength
+ 1) & ~(bfd_size_type
) 1);
2042 memcpy (hdr
.ar_fmag
, ARFMAG
, 2);
2043 if ((bfd_bwrite (&hdr
, sizeof (struct ar_hdr
), arch
)
2044 != sizeof (struct ar_hdr
))
2045 || bfd_bwrite (etable
, elength
, arch
) != elength
)
2047 if ((elength
% 2) == 1)
2049 if (bfd_bwrite (&ARFMAG
[1], 1, arch
) != 1)
2054 for (current
= arch
->archive_head
;
2056 current
= current
->archive_next
)
2058 char buffer
[DEFAULT_BUFFERSIZE
];
2059 unsigned int remaining
= arelt_size (current
);
2061 /* Write ar header. */
2062 if (!_bfd_write_ar_hdr (arch
, current
))
2064 if (bfd_is_thin_archive (arch
))
2066 if (bfd_seek (current
, (file_ptr
) 0, SEEK_SET
) != 0)
2071 unsigned int amt
= DEFAULT_BUFFERSIZE
;
2073 if (amt
> remaining
)
2076 if (bfd_bread (buffer
, amt
, current
) != amt
)
2078 if (bfd_get_error () != bfd_error_system_call
)
2079 bfd_set_error (bfd_error_file_truncated
);
2082 if (bfd_bwrite (buffer
, amt
, arch
) != amt
)
2087 if ((arelt_size (current
) % 2) == 1)
2089 if (bfd_bwrite (&ARFMAG
[1], 1, arch
) != 1)
2094 if (makemap
&& hasobjects
)
2096 /* Verify the timestamp in the archive file. If it would not be
2097 accepted by the linker, rewrite it until it would be. If
2098 anything odd happens, break out and just return. (The
2099 Berkeley linker checks the timestamp and refuses to read the
2100 table-of-contents if it is >60 seconds less than the file's
2101 modified-time. That painful hack requires this painful hack. */
2105 if (bfd_update_armap_timestamp (arch
))
2107 (*_bfd_error_handler
)
2108 (_("Warning: writing archive was slow: rewriting timestamp\n"));
2110 while (++tries
< 6);
2116 bfd_set_error (bfd_error_on_input
, current
, bfd_get_error ());
2120 /* Note that the namidx for the first symbol is 0. */
2123 _bfd_compute_and_write_armap (bfd
*arch
, unsigned int elength
)
2125 char *first_name
= NULL
;
2127 file_ptr elt_no
= 0;
2128 struct orl
*map
= NULL
;
2129 unsigned int orl_max
= 1024; /* Fine initial default. */
2130 unsigned int orl_count
= 0;
2132 asymbol
**syms
= NULL
;
2137 /* Dunno if this is the best place for this info... */
2139 elength
+= sizeof (struct ar_hdr
);
2140 elength
+= elength
% 2;
2142 amt
= orl_max
* sizeof (struct orl
);
2143 map
= (struct orl
*) bfd_malloc (amt
);
2147 /* We put the symbol names on the arch objalloc, and then discard
2149 first_name
= (char *) bfd_alloc (arch
, 1);
2150 if (first_name
== NULL
)
2153 /* Drop all the files called __.SYMDEF, we're going to make our own. */
2154 while (arch
->archive_head
2155 && strcmp (arch
->archive_head
->filename
, "__.SYMDEF") == 0)
2156 arch
->archive_head
= arch
->archive_head
->archive_next
;
2158 /* Map over each element. */
2159 for (current
= arch
->archive_head
;
2161 current
= current
->archive_next
, elt_no
++)
2163 if (bfd_check_format (current
, bfd_object
)
2164 && (bfd_get_file_flags (current
) & HAS_SYMS
) != 0)
2170 storage
= bfd_get_symtab_upper_bound (current
);
2176 if (storage
> syms_max
)
2181 syms
= (asymbol
**) bfd_malloc (syms_max
);
2185 symcount
= bfd_canonicalize_symtab (current
, syms
);
2189 /* Now map over all the symbols, picking out the ones we
2191 for (src_count
= 0; src_count
< symcount
; src_count
++)
2193 flagword flags
= (syms
[src_count
])->flags
;
2194 asection
*sec
= syms
[src_count
]->section
;
2196 if ((flags
& BSF_GLOBAL
2198 || flags
& BSF_INDIRECT
2199 || bfd_is_com_section (sec
))
2200 && ! bfd_is_und_section (sec
))
2202 bfd_size_type namelen
;
2203 struct orl
*new_map
;
2205 /* This symbol will go into the archive header. */
2206 if (orl_count
== orl_max
)
2209 amt
= orl_max
* sizeof (struct orl
);
2210 new_map
= (struct orl
*) bfd_realloc (map
, amt
);
2211 if (new_map
== NULL
)
2217 namelen
= strlen (syms
[src_count
]->name
);
2218 amt
= sizeof (char *);
2219 map
[orl_count
].name
= (char **) bfd_alloc (arch
, amt
);
2220 if (map
[orl_count
].name
== NULL
)
2222 *(map
[orl_count
].name
) = (char *) bfd_alloc (arch
,
2224 if (*(map
[orl_count
].name
) == NULL
)
2226 strcpy (*(map
[orl_count
].name
), syms
[src_count
]->name
);
2227 map
[orl_count
].u
.abfd
= current
;
2228 map
[orl_count
].namidx
= stridx
;
2230 stridx
+= namelen
+ 1;
2236 /* Now ask the BFD to free up any cached information, so we
2237 don't fill all of memory with symbol tables. */
2238 if (! bfd_free_cached_info (current
))
2243 /* OK, now we have collected all the data, let's write them out. */
2244 ret
= BFD_SEND (arch
, write_armap
,
2245 (arch
, elength
, map
, orl_count
, stridx
));
2251 if (first_name
!= NULL
)
2252 bfd_release (arch
, first_name
);
2261 if (first_name
!= NULL
)
2262 bfd_release (arch
, first_name
);
2268 bsd_write_armap (bfd
*arch
,
2269 unsigned int elength
,
2271 unsigned int orl_count
,
2274 int padit
= stridx
& 1;
2275 unsigned int ranlibsize
= orl_count
* BSD_SYMDEF_SIZE
;
2276 unsigned int stringsize
= stridx
+ padit
;
2277 /* Include 8 bytes to store ranlibsize and stringsize in output. */
2278 unsigned int mapsize
= ranlibsize
+ stringsize
+ 8;
2280 bfd
*current
= arch
->archive_head
;
2281 bfd
*last_elt
= current
; /* Last element arch seen. */
2285 struct stat statbuf
;
2288 firstreal
= mapsize
+ elength
+ sizeof (struct ar_hdr
) + SARMAG
;
2290 stat (arch
->filename
, &statbuf
);
2291 if ((arch
->flags
& BFD_DETERMINISTIC_OUTPUT
) == 0)
2293 /* Remember the timestamp, to keep it holy. But fudge it a little. */
2294 bfd_ardata (arch
)->armap_timestamp
= (statbuf
.st_mtime
2295 + ARMAP_TIME_OFFSET
);
2301 /* If deterministic, we use 0 as the timestamp in the map.
2302 Some linkers may require that the archive filesystem modification
2303 time is less than (or near to) the archive map timestamp. Those
2304 linkers should not be used with deterministic mode. (GNU ld and
2305 Gold do not have this restriction.) */
2306 bfd_ardata (arch
)->armap_timestamp
= 0;
2311 memset (&hdr
, ' ', sizeof (struct ar_hdr
));
2312 memcpy (hdr
.ar_name
, RANLIBMAG
, strlen (RANLIBMAG
));
2313 bfd_ardata (arch
)->armap_datepos
= (SARMAG
2314 + offsetof (struct ar_hdr
, ar_date
[0]));
2315 _bfd_ar_spacepad (hdr
.ar_date
, sizeof (hdr
.ar_date
), "%ld",
2316 bfd_ardata (arch
)->armap_timestamp
);
2317 _bfd_ar_spacepad (hdr
.ar_uid
, sizeof (hdr
.ar_uid
), "%ld", uid
);
2318 _bfd_ar_spacepad (hdr
.ar_gid
, sizeof (hdr
.ar_gid
), "%ld", gid
);
2319 _bfd_ar_spacepad (hdr
.ar_size
, sizeof (hdr
.ar_size
), "%-10ld", mapsize
);
2320 memcpy (hdr
.ar_fmag
, ARFMAG
, 2);
2321 if (bfd_bwrite (&hdr
, sizeof (struct ar_hdr
), arch
)
2322 != sizeof (struct ar_hdr
))
2324 H_PUT_32 (arch
, ranlibsize
, temp
);
2325 if (bfd_bwrite (temp
, sizeof (temp
), arch
) != sizeof (temp
))
2328 for (count
= 0; count
< orl_count
; count
++)
2330 bfd_byte buf
[BSD_SYMDEF_SIZE
];
2332 if (map
[count
].u
.abfd
!= last_elt
)
2336 struct areltdata
*ared
= arch_eltdata (current
);
2338 firstreal
+= (ared
->parsed_size
+ ared
->extra_size
2339 + sizeof (struct ar_hdr
));
2340 firstreal
+= firstreal
% 2;
2341 current
= current
->archive_next
;
2343 while (current
!= map
[count
].u
.abfd
);
2347 H_PUT_32 (arch
, map
[count
].namidx
, buf
);
2348 H_PUT_32 (arch
, firstreal
, buf
+ BSD_SYMDEF_OFFSET_SIZE
);
2349 if (bfd_bwrite (buf
, BSD_SYMDEF_SIZE
, arch
)
2354 /* Now write the strings themselves. */
2355 H_PUT_32 (arch
, stringsize
, temp
);
2356 if (bfd_bwrite (temp
, sizeof (temp
), arch
) != sizeof (temp
))
2358 for (count
= 0; count
< orl_count
; count
++)
2360 size_t len
= strlen (*map
[count
].name
) + 1;
2362 if (bfd_bwrite (*map
[count
].name
, len
, arch
) != len
)
2366 /* The spec sez this should be a newline. But in order to be
2367 bug-compatible for sun's ar we use a null. */
2370 if (bfd_bwrite ("", 1, arch
) != 1)
2377 /* At the end of archive file handling, update the timestamp in the
2378 file, so the linker will accept it.
2380 Return TRUE if the timestamp was OK, or an unusual problem happened.
2381 Return FALSE if we updated the timestamp. */
2384 _bfd_archive_bsd_update_armap_timestamp (bfd
*arch
)
2386 struct stat archstat
;
2389 /* If creating deterministic archives, just leave the timestamp as-is. */
2390 if ((arch
->flags
& BFD_DETERMINISTIC_OUTPUT
) != 0)
2393 /* Flush writes, get last-write timestamp from file, and compare it
2394 to the timestamp IN the file. */
2396 if (bfd_stat (arch
, &archstat
) == -1)
2398 bfd_perror (_("Reading archive file mod timestamp"));
2400 /* Can't read mod time for some reason. */
2403 if (((long) archstat
.st_mtime
) <= bfd_ardata (arch
)->armap_timestamp
)
2404 /* OK by the linker's rules. */
2407 /* Update the timestamp. */
2408 bfd_ardata (arch
)->armap_timestamp
= archstat
.st_mtime
+ ARMAP_TIME_OFFSET
;
2410 /* Prepare an ASCII version suitable for writing. */
2411 memset (hdr
.ar_date
, ' ', sizeof (hdr
.ar_date
));
2412 _bfd_ar_spacepad (hdr
.ar_date
, sizeof (hdr
.ar_date
), "%ld",
2413 bfd_ardata (arch
)->armap_timestamp
);
2415 /* Write it into the file. */
2416 bfd_ardata (arch
)->armap_datepos
= (SARMAG
2417 + offsetof (struct ar_hdr
, ar_date
[0]));
2418 if (bfd_seek (arch
, bfd_ardata (arch
)->armap_datepos
, SEEK_SET
) != 0
2419 || (bfd_bwrite (hdr
.ar_date
, sizeof (hdr
.ar_date
), arch
)
2420 != sizeof (hdr
.ar_date
)))
2422 bfd_perror (_("Writing updated armap timestamp"));
2424 /* Some error while writing. */
2428 /* We updated the timestamp successfully. */
2432 /* A coff armap looks like :
2434 struct ar_hdr with name = '/'
2436 offset of file for symbol 0
2437 offset of file for symbol 1
2439 offset of file for symbol n-1
2446 coff_write_armap (bfd
*arch
,
2447 unsigned int elength
,
2449 unsigned int symbol_count
,
2452 /* The size of the ranlib is the number of exported symbols in the
2453 archive * the number of bytes in an int, + an int for the count. */
2454 unsigned int ranlibsize
= (symbol_count
* 4) + 4;
2455 unsigned int stringsize
= stridx
;
2456 unsigned int mapsize
= stringsize
+ ranlibsize
;
2457 unsigned int archive_member_file_ptr
;
2458 bfd
*current
= arch
->archive_head
;
2461 int padit
= mapsize
& 1;
2466 /* Work out where the first object file will go in the archive. */
2467 archive_member_file_ptr
= (mapsize
2469 + sizeof (struct ar_hdr
)
2472 memset (&hdr
, ' ', sizeof (struct ar_hdr
));
2473 hdr
.ar_name
[0] = '/';
2474 _bfd_ar_spacepad (hdr
.ar_size
, sizeof (hdr
.ar_size
), "%-10ld",
2476 _bfd_ar_spacepad (hdr
.ar_date
, sizeof (hdr
.ar_date
), "%ld",
2477 ((arch
->flags
& BFD_DETERMINISTIC_OUTPUT
) == 0
2478 ? time (NULL
) : 0));
2479 /* This, at least, is what Intel coff sets the values to. */
2480 _bfd_ar_spacepad (hdr
.ar_uid
, sizeof (hdr
.ar_uid
), "%ld", 0);
2481 _bfd_ar_spacepad (hdr
.ar_gid
, sizeof (hdr
.ar_gid
), "%ld", 0);
2482 _bfd_ar_spacepad (hdr
.ar_mode
, sizeof (hdr
.ar_mode
), "%-7lo", 0);
2483 memcpy (hdr
.ar_fmag
, ARFMAG
, 2);
2485 /* Write the ar header for this item and the number of symbols. */
2486 if (bfd_bwrite (&hdr
, sizeof (struct ar_hdr
), arch
)
2487 != sizeof (struct ar_hdr
))
2490 if (!bfd_write_bigendian_4byte_int (arch
, symbol_count
))
2493 /* Two passes, first write the file offsets for each symbol -
2494 remembering that each offset is on a two byte boundary. */
2496 /* Write out the file offset for the file associated with each
2497 symbol, and remember to keep the offsets padded out. */
2499 current
= arch
->archive_head
;
2501 while (current
!= NULL
&& count
< symbol_count
)
2503 /* For each symbol which is used defined in this object, write
2504 out the object file's address in the archive. */
2506 while (count
< symbol_count
&& map
[count
].u
.abfd
== current
)
2508 if (!bfd_write_bigendian_4byte_int (arch
, archive_member_file_ptr
))
2512 archive_member_file_ptr
+= sizeof (struct ar_hdr
);
2513 if (! bfd_is_thin_archive (arch
))
2515 /* Add size of this archive entry. */
2516 archive_member_file_ptr
+= arelt_size (current
);
2517 /* Remember about the even alignment. */
2518 archive_member_file_ptr
+= archive_member_file_ptr
% 2;
2520 current
= current
->archive_next
;
2523 /* Now write the strings themselves. */
2524 for (count
= 0; count
< symbol_count
; count
++)
2526 size_t len
= strlen (*map
[count
].name
) + 1;
2528 if (bfd_bwrite (*map
[count
].name
, len
, arch
) != len
)
2532 /* The spec sez this should be a newline. But in order to be
2533 bug-compatible for arc960 we use a null. */
2536 if (bfd_bwrite ("", 1, arch
) != 1)