]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/archive.c
* aout-adobe.c: Don't compare against "true" or "false.
[thirdparty/binutils-gdb.git] / bfd / archive.c
CommitLineData
252b5132 1/* BFD back-end for archive files (libraries).
7898deda 2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
54327882 3 2000, 2001, 2002
252b5132
RH
4 Free Software Foundation, Inc.
5 Written by Cygnus Support. Mostly Gumby Henkel-Wallace's fault.
6
7This file is part of BFD, the Binary File Descriptor library.
8
9This program is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22
23/*
24@setfilename archive-info
25SECTION
26 Archives
27
28DESCRIPTION
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.
31
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.
39
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.
44
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.
50
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!
56
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
66 homogeneous.
67
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
73 of a restriction.
74
75 Archives are supported in BFD in <<archive.c>>.
76
77*/
78
79/* Assumes:
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).
83*/
84
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.
93
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.
97
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.
102
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.
106*/
107
108/* Summary of archive member names:
109
110 Symbol table (must be first):
111 "__.SYMDEF " - Symbol table, Berkeley style, produced by ranlib.
112 "/ " - Symbol table, system 5 style.
113
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).
117
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).
121
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.
128 */
129
130#include "bfd.h"
131#include "sysdep.h"
132#include "libbfd.h"
133#include "aout/ar.h"
134#include "aout/ranlib.h"
3882b010 135#include "safe-ctype.h"
252b5132
RH
136
137#ifndef errno
138extern int errno;
139#endif
140
141#ifdef GNU960
142#define BFD_GNU960_ARMAG(abfd) (BFD_COFF_FILE_P((abfd)) ? ARMAG : ARMAGB)
143#endif
144
252b5132
RH
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
047066e1
KH
150 to the front of the contents! */
151struct ar_cache {
252b5132
RH
152 file_ptr ptr;
153 bfd *arelt;
154 struct ar_cache *next;
155};
156
157#define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
158#define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
159
beb0d161 160#define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
252b5132
RH
161#define arch_hdr(bfd) ((struct ar_hdr *)arch_eltdata(bfd)->arch_header)
162
163static char *get_extended_arelt_filename PARAMS ((bfd *arch,
164 const char *name));
165static boolean do_slurp_bsd_armap PARAMS ((bfd *abfd));
166static boolean do_slurp_coff_armap PARAMS ((bfd *abfd));
36b45482 167boolean bfd_elf64_archive_slurp_armap PARAMS ((bfd *abfd));
252b5132
RH
168static const char *normalize PARAMS ((bfd *, const char *file));
169static struct areltdata *bfd_ar_hdr_from_filesystem PARAMS ((bfd *abfd,
170 const char *,
171 bfd *member));
172\f
173boolean
174_bfd_generic_mkarchive (abfd)
175 bfd *abfd;
176{
dc810e39 177 bfd_size_type amt = sizeof (struct artdata);
252b5132 178
dc810e39 179 abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc (abfd, amt);
252b5132
RH
180 if (bfd_ardata (abfd) == NULL)
181 return false;
182
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;
188
189 return true;
190}
191
192/*
193FUNCTION
194 bfd_get_next_mapent
195
196SYNOPSIS
197 symindex bfd_get_next_mapent(bfd *abfd, symindex previous, carsym **sym);
198
199DESCRIPTION
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
203 symbol table.
204
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
207 got the last one.
208
209 A <<carsym>> is a canonical archive symbol. The only
210 user-visible element is its name, a null-terminated string.
211*/
212
213symindex
214bfd_get_next_mapent (abfd, prev, entry)
215 bfd *abfd;
216 symindex prev;
217 carsym **entry;
218{
219 if (!bfd_has_map (abfd))
220 {
221 bfd_set_error (bfd_error_invalid_operation);
222 return BFD_NO_MORE_SYMBOLS;
223 }
224
225 if (prev == BFD_NO_MORE_SYMBOLS)
226 prev = 0;
227 else
228 ++prev;
229 if (prev >= bfd_ardata (abfd)->symdef_count)
230 return BFD_NO_MORE_SYMBOLS;
231
232 *entry = (bfd_ardata (abfd)->symdefs + prev);
233 return prev;
234}
235
236/* To be called by backends only */
237
238bfd *
239_bfd_create_empty_archive_element_shell (obfd)
240 bfd *obfd;
241{
242 return _bfd_new_bfd_contained_in (obfd);
243}
244
245/*
246FUNCTION
247 bfd_set_archive_head
248
249SYNOPSIS
250 boolean bfd_set_archive_head(bfd *output, bfd *new_head);
251
252DESCRIPTION
253 Set the head of the chain of
254 BFDs contained in the archive @var{output} to @var{new_head}.
255*/
256
257boolean
258bfd_set_archive_head (output_archive, new_head)
259 bfd *output_archive;
260 bfd *new_head;
261{
262
263 output_archive->archive_head = new_head;
264 return true;
265}
266
267bfd *
268_bfd_look_for_bfd_in_cache (arch_bfd, filepos)
269 bfd *arch_bfd;
270 file_ptr filepos;
271{
272 struct ar_cache *current;
273
274 for (current = bfd_ardata (arch_bfd)->cache; current != NULL;
275 current = current->next)
276 if (current->ptr == filepos)
277 return current->arelt;
278
279 return NULL;
280}
281
282/* Kind of stupid to call cons for each one, but we don't do too many */
283boolean
284_bfd_add_bfd_to_archive_cache (arch_bfd, filepos, new_elt)
285 bfd *arch_bfd, *new_elt;
286 file_ptr filepos;
287{
dc810e39 288 bfd_size_type amt = sizeof (struct ar_cache);
252b5132 289
dc810e39 290 struct ar_cache *new_cache = (struct ar_cache *) bfd_zalloc (arch_bfd, amt);
252b5132
RH
291 if (new_cache == NULL)
292 return false;
293
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;
299 else
300 {
301 struct ar_cache *current = bfd_ardata (arch_bfd)->cache;
302
303 while (current->next != NULL)
304 current = current->next;
305 current->next = new_cache;
306 }
307
308 return true;
309}
310\f
311/* The name begins with space. Hence the rest of the name is an index into
d70910e8 312 the string table. */
252b5132
RH
313
314static char *
315get_extended_arelt_filename (arch, name)
316 bfd *arch;
317 const char *name;
318{
319 unsigned long index = 0;
320
321 /* Should extract string so that I can guarantee not to overflow into
d70910e8 322 the next region, but I'm too lazy. */
252b5132 323 errno = 0;
d70910e8 324 /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */
252b5132
RH
325 index = strtol (name + 1, NULL, 10);
326 if (errno != 0)
327 {
328 bfd_set_error (bfd_error_malformed_archive);
329 return NULL;
330 }
331
332 return bfd_ardata (arch)->extended_names + index;
333}
334
335/* This functions reads an arch header and returns an areltdata pointer, or
336 NULL on error.
337
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.
342*/
343
344PTR
345_bfd_generic_read_ar_hdr (abfd)
346 bfd *abfd;
347{
348 return _bfd_generic_read_ar_hdr_mag (abfd, (const char *) NULL);
349}
350
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. */
353
354PTR
355_bfd_generic_read_ar_hdr_mag (abfd, mag)
356 bfd *abfd;
357 const char *mag;
358{
359 struct ar_hdr hdr;
360 char *hdrp = (char *) &hdr;
dc810e39 361 size_t parsed_size;
252b5132
RH
362 struct areltdata *ared;
363 char *filename = NULL;
dc810e39
AM
364 bfd_size_type namelen = 0;
365 bfd_size_type allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
252b5132
RH
366 char *allocptr = 0;
367
dc810e39 368 if (bfd_bread ((PTR) hdrp, (bfd_size_type) sizeof (struct ar_hdr), abfd)
252b5132
RH
369 != sizeof (struct ar_hdr))
370 {
371 if (bfd_get_error () != bfd_error_system_call)
372 bfd_set_error (bfd_error_no_more_archived_files);
373 return NULL;
374 }
375 if (strncmp (hdr.ar_fmag, ARFMAG, 2) != 0
376 && (mag == NULL
377 || strncmp (hdr.ar_fmag, mag, 2) != 0))
378 {
379 bfd_set_error (bfd_error_malformed_archive);
380 return NULL;
381 }
382
383 errno = 0;
384 parsed_size = strtol (hdr.ar_size, NULL, 10);
385 if (errno != 0)
386 {
387 bfd_set_error (bfd_error_malformed_archive);
388 return NULL;
389 }
390
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)
398 {
399 filename = get_extended_arelt_filename (abfd, hdr.ar_name);
400 if (filename == NULL)
401 {
402 bfd_set_error (bfd_error_malformed_archive);
403 return NULL;
404 }
405 }
406 /* BSD4.4-style long filename.
047066e1 407 Only implemented for reading, so far! */
252b5132
RH
408 else if (hdr.ar_name[0] == '#'
409 && hdr.ar_name[1] == '1'
410 && hdr.ar_name[2] == '/'
3882b010 411 && ISDIGIT (hdr.ar_name[3]))
252b5132
RH
412 {
413 /* BSD-4.4 extended name */
414 namelen = atoi (&hdr.ar_name[3]);
415 allocsize += namelen + 1;
416 parsed_size -= namelen;
417
418 allocptr = bfd_zalloc (abfd, allocsize);
419 if (allocptr == NULL)
420 return NULL;
421 filename = (allocptr
422 + sizeof (struct areltdata)
423 + sizeof (struct ar_hdr));
dc810e39 424 if (bfd_bread (filename, namelen, abfd) != namelen)
252b5132
RH
425 {
426 if (bfd_get_error () != bfd_error_system_call)
427 bfd_set_error (bfd_error_no_more_archived_files);
428 return NULL;
429 }
430 filename[namelen] = '\0';
431 }
432 else
433 {
434 /* We judge the end of the name by looking for '/' or ' '.
435 Note: The SYSV format (terminated by '/') allows embedded
d70910e8 436 spaces, so only look for ' ' if we don't find '/'. */
252b5132
RH
437
438 char *e;
439 e = memchr (hdr.ar_name, '\0', ar_maxnamelen (abfd));
440 if (e == NULL)
441 {
047066e1 442 e = memchr (hdr.ar_name, '/', ar_maxnamelen (abfd));
252b5132 443 if (e == NULL)
047066e1 444 e = memchr (hdr.ar_name, ' ', ar_maxnamelen (abfd));
252b5132
RH
445 }
446
447 if (e != NULL)
448 namelen = e - hdr.ar_name;
449 else
450 {
451 /* If we didn't find a termination character, then the name
452 must be the entire field. */
453 namelen = ar_maxnamelen (abfd);
454 }
455
456 allocsize += namelen + 1;
457 }
458
459 if (!allocptr)
460 {
461 allocptr = bfd_zalloc (abfd, allocsize);
462 if (allocptr == NULL)
463 return NULL;
464 }
465
466 ared = (struct areltdata *) allocptr;
467
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;
471
472 if (filename != NULL)
473 ared->filename = filename;
474 else
475 {
476 ared->filename = allocptr + (sizeof (struct areltdata) +
477 sizeof (struct ar_hdr));
478 if (namelen)
dc810e39 479 memcpy (ared->filename, hdr.ar_name, (size_t) namelen);
252b5132
RH
480 ared->filename[namelen] = '\0';
481 }
482
483 return (PTR) ared;
484}
485\f
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. */
489
490bfd *
491_bfd_get_elt_at_filepos (archive, filepos)
492 bfd *archive;
493 file_ptr filepos;
494{
495 struct areltdata *new_areldata;
496 bfd *n_nfd;
497
498 n_nfd = _bfd_look_for_bfd_in_cache (archive, filepos);
499 if (n_nfd)
500 return n_nfd;
501
502 if (0 > bfd_seek (archive, filepos, SEEK_SET))
503 return NULL;
504
505 if ((new_areldata = (struct areltdata *) _bfd_read_ar_hdr (archive)) == NULL)
506 return NULL;
507
508 n_nfd = _bfd_create_empty_archive_element_shell (archive);
509 if (n_nfd == NULL)
510 {
511 bfd_release (archive, (PTR) new_areldata);
512 return NULL;
513 }
514
515 n_nfd->origin = bfd_tell (archive);
516 n_nfd->arelt_data = (PTR) new_areldata;
517 n_nfd->filename = new_areldata->filename;
518
519 if (_bfd_add_bfd_to_archive_cache (archive, filepos, n_nfd))
520 return n_nfd;
521
047066e1 522 /* Huh? */
252b5132
RH
523 bfd_release (archive, (PTR) n_nfd);
524 bfd_release (archive, (PTR) new_areldata);
525 return NULL;
526}
527
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. */
530
531bfd *
532_bfd_generic_get_elt_at_index (abfd, index)
533 bfd *abfd;
534 symindex index;
535{
536 carsym *entry;
537
538 entry = bfd_ardata (abfd)->symdefs + index;
539 return _bfd_get_elt_at_filepos (abfd, entry->file_offset);
540}
541
542/*
543FUNCTION
544 bfd_openr_next_archived_file
545
546SYNOPSIS
547 bfd *bfd_openr_next_archived_file(bfd *archive, bfd *previous);
548
549DESCRIPTION
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
555 are no more.
252b5132
RH
556*/
557
558bfd *
559bfd_openr_next_archived_file (archive, last_file)
560 bfd *archive;
561 bfd *last_file;
562{
563 if ((bfd_get_format (archive) != bfd_archive) ||
564 (archive->direction == write_direction))
565 {
566 bfd_set_error (bfd_error_invalid_operation);
567 return NULL;
568 }
569
570 return BFD_SEND (archive,
571 openr_next_archived_file,
572 (archive,
573 last_file));
574}
575
576bfd *
577bfd_generic_openr_next_archived_file (archive, last_file)
578 bfd *archive;
579 bfd *last_file;
580{
581 file_ptr filestart;
582
583 if (!last_file)
584 filestart = bfd_ardata (archive)->first_file_filepos;
585 else
586 {
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
d70910e8 590 BSD-4.4-style element with a long odd size. */
252b5132
RH
591 filestart = last_file->origin + size;
592 filestart += filestart % 2;
593 }
594
595 return _bfd_get_elt_at_filepos (archive, filestart);
596}
597
252b5132
RH
598const bfd_target *
599bfd_generic_archive_p (abfd)
600 bfd *abfd;
601{
602 struct artdata *tdata_hold;
603 char armag[SARMAG + 1];
dc810e39 604 bfd_size_type amt;
252b5132
RH
605
606 tdata_hold = abfd->tdata.aout_ar_data;
607
dc810e39 608 if (bfd_bread ((PTR) armag, (bfd_size_type) SARMAG, abfd) != SARMAG)
252b5132
RH
609 {
610 if (bfd_get_error () != bfd_error_system_call)
611 bfd_set_error (bfd_error_wrong_format);
612 return NULL;
613 }
614
615#ifdef GNU960
616 if (strncmp (armag, BFD_GNU960_ARMAG (abfd), SARMAG) != 0)
617 return 0;
618#else
619 if (strncmp (armag, ARMAG, SARMAG) != 0 &&
620 strncmp (armag, ARMAGB, SARMAG) != 0)
621 return 0;
622#endif
623
624 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
d70910e8 625 involves a cast, we can't do it as the left operand of assignment. */
dc810e39
AM
626 amt = sizeof (struct artdata);
627 abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc (abfd, amt);
252b5132
RH
628
629 if (bfd_ardata (abfd) == NULL)
630 return NULL;
631
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;
638
639 if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd)))
640 {
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);
645 return NULL;
646 }
647
648 if (!BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd)))
649 {
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);
654 return NULL;
655 }
656
657 if (bfd_has_map (abfd))
658 {
659 bfd *first;
660
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.
667
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. */
671
672 first = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
673 if (first != NULL)
674 {
675 boolean fail;
676
677 first->target_defaulted = false;
678 fail = false;
679 if (bfd_check_format (first, bfd_object)
680 && first->xvec != abfd->xvec)
681 {
3619ad04
AM
682#if 0
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. */
252b5132
RH
687 (void) bfd_close (first);
688 bfd_release (abfd, bfd_ardata (abfd));
689 abfd->tdata.aout_ar_data = tdata_hold;
3619ad04
AM
690#endif
691 bfd_set_error (bfd_error_wrong_object_format);
252b5132
RH
692 return NULL;
693 }
3619ad04 694 /* And we ought to close `first' here too. */
252b5132
RH
695 }
696 }
697
698 return abfd->xvec;
699}
700
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
dc810e39 704 changing some H_GET_32 to H_GET_64. */
252b5132
RH
705
706/* The size of an external symdef structure. */
707#define BSD_SYMDEF_SIZE 8
708
709/* The offset from the start of a symdef structure to the file offset. */
710#define BSD_SYMDEF_OFFSET_SIZE 4
711
712/* The size of the symdef count. */
713#define BSD_SYMDEF_COUNT_SIZE 4
714
715/* The size of the string count. */
716#define BSD_STRING_COUNT_SIZE 4
717
718/* Returns false on error, true otherwise */
719
720static boolean
721do_slurp_bsd_armap (abfd)
722 bfd *abfd;
723{
724 struct areltdata *mapdata;
725 unsigned int counter;
726 bfd_byte *raw_armap, *rbase;
727 struct artdata *ardata = bfd_ardata (abfd);
728 char *stringbase;
dc810e39 729 bfd_size_type parsed_size, amt;
252b5132
RH
730 carsym *set;
731
732 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
733 if (mapdata == NULL)
734 return false;
735 parsed_size = mapdata->parsed_size;
d70910e8 736 bfd_release (abfd, (PTR) mapdata); /* Don't need it any more. */
252b5132
RH
737
738 raw_armap = (bfd_byte *) bfd_zalloc (abfd, parsed_size);
739 if (raw_armap == (bfd_byte *) NULL)
740 return false;
741
dc810e39 742 if (bfd_bread ((PTR) raw_armap, parsed_size, abfd) != parsed_size)
252b5132
RH
743 {
744 if (bfd_get_error () != bfd_error_system_call)
745 bfd_set_error (bfd_error_malformed_archive);
746 byebye:
747 bfd_release (abfd, (PTR) raw_armap);
748 return false;
749 }
750
dc810e39 751 ardata->symdef_count = H_GET_32 (abfd, raw_armap) / BSD_SYMDEF_SIZE;
252b5132
RH
752
753 if (ardata->symdef_count * BSD_SYMDEF_SIZE >
754 parsed_size - BSD_SYMDEF_COUNT_SIZE)
755 {
756 /* Probably we're using the wrong byte ordering. */
757 bfd_set_error (bfd_error_wrong_format);
758 goto byebye;
759 }
760
761 ardata->cache = 0;
762 rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE;
763 stringbase = ((char *) rbase
764 + ardata->symdef_count * BSD_SYMDEF_SIZE
765 + BSD_STRING_COUNT_SIZE);
dc810e39
AM
766 amt = (bfd_size_type) ardata->symdef_count * sizeof (carsym);
767 ardata->symdefs = (carsym *) bfd_alloc (abfd, amt);
252b5132
RH
768 if (!ardata->symdefs)
769 return false;
770
771 for (counter = 0, set = ardata->symdefs;
772 counter < ardata->symdef_count;
773 counter++, set++, rbase += BSD_SYMDEF_SIZE)
774 {
dc810e39
AM
775 set->name = H_GET_32 (abfd, rbase) + stringbase;
776 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
252b5132
RH
777 }
778
779 ardata->first_file_filepos = bfd_tell (abfd);
047066e1 780 /* Pad to an even boundary if you have to. */
252b5132
RH
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
d70910e8 784 to be allocated on an objalloc anyway... */
252b5132
RH
785 bfd_has_map (abfd) = true;
786 return true;
787}
788
047066e1
KH
789/* Returns false on error, true otherwise. */
790
252b5132
RH
791static boolean
792do_slurp_coff_armap (abfd)
793 bfd *abfd;
794{
795 struct areltdata *mapdata;
796 int *raw_armap, *rawptr;
797 struct artdata *ardata = bfd_ardata (abfd);
798 char *stringbase;
dc810e39 799 bfd_size_type stringsize;
252b5132
RH
800 unsigned int parsed_size;
801 carsym *carsyms;
dc810e39 802 bfd_size_type nsymz; /* Number of symbols in armap. */
252b5132
RH
803 bfd_vma (*swap) PARAMS ((const bfd_byte *));
804 char int_buf[sizeof (long)];
dc810e39
AM
805 bfd_size_type carsym_size, ptrsize;
806 unsigned int i;
252b5132
RH
807
808 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
809 if (mapdata == NULL)
810 return false;
811 parsed_size = mapdata->parsed_size;
d70910e8 812 bfd_release (abfd, (PTR) mapdata); /* Don't need it any more. */
252b5132 813
dc810e39 814 if (bfd_bread ((PTR) int_buf, (bfd_size_type) 4, abfd) != 4)
252b5132
RH
815 {
816 if (bfd_get_error () != bfd_error_system_call)
817 bfd_set_error (bfd_error_malformed_archive);
818 return false;
819 }
820 /* It seems that all numeric information in a coff archive is always
d70910e8 821 in big endian format, nomatter the host or target. */
252b5132
RH
822 swap = bfd_getb32;
823 nsymz = bfd_getb32 ((PTR) int_buf);
824 stringsize = parsed_size - (4 * nsymz) - 4;
825
826#if 1
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
830 up the crap. */
831
832 if (stringsize > 0xfffff
833 && bfd_get_arch (abfd) == bfd_arch_i960
834 && bfd_get_flavour (abfd) == bfd_target_coff_flavour)
835 {
047066e1 836 /* This looks dangerous, let's do it the other way around. */
252b5132
RH
837 nsymz = bfd_getl32 ((PTR) int_buf);
838 stringsize = parsed_size - (4 * nsymz) - 4;
839 swap = bfd_getl32;
840 }
841#endif
842
843 /* The coff armap must be read sequentially. So we construct a
d70910e8 844 bsd-style one in core all at once, for simplicity. */
252b5132
RH
845
846 carsym_size = (nsymz * sizeof (carsym));
847 ptrsize = (4 * nsymz);
848
849 ardata->symdefs = (carsym *) bfd_zalloc (abfd, carsym_size + stringsize + 1);
850 if (ardata->symdefs == NULL)
851 return false;
852 carsyms = ardata->symdefs;
853 stringbase = ((char *) ardata->symdefs) + carsym_size;
854
d70910e8 855 /* Allocate and read in the raw offsets. */
252b5132
RH
856 raw_armap = (int *) bfd_alloc (abfd, ptrsize);
857 if (raw_armap == NULL)
858 goto release_symdefs;
dc810e39
AM
859 if (bfd_bread ((PTR) raw_armap, ptrsize, abfd) != ptrsize
860 || (bfd_bread ((PTR) stringbase, stringsize, abfd) != stringsize))
252b5132
RH
861 {
862 if (bfd_get_error () != bfd_error_system_call)
863 bfd_set_error (bfd_error_malformed_archive);
864 goto release_raw_armap;
865 }
866
047066e1 867 /* OK, build the carsyms. */
252b5132
RH
868 for (i = 0; i < nsymz; i++)
869 {
870 rawptr = raw_armap + i;
871 carsyms->file_offset = swap ((PTR) rawptr);
872 carsyms->name = stringbase;
873 stringbase += strlen (stringbase) + 1;
874 carsyms++;
875 }
876 *stringbase = 0;
877
878 ardata->symdef_count = nsymz;
879 ardata->first_file_filepos = bfd_tell (abfd);
047066e1 880 /* Pad to an even boundary if you have to. */
252b5132
RH
881 ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
882
252b5132
RH
883 bfd_has_map (abfd) = true;
884 bfd_release (abfd, (PTR) raw_armap);
885
047066e1 886 /* Check for a second archive header (as used by PE). */
252b5132
RH
887 {
888 struct areltdata *tmp;
889
dc810e39 890 bfd_seek (abfd, ardata->first_file_filepos, SEEK_SET);
252b5132 891 tmp = (struct areltdata *) _bfd_read_ar_hdr (abfd);
23afc6f6 892 if (tmp != NULL)
252b5132
RH
893 {
894 if (tmp->arch_header[0] == '/'
23afc6f6 895 && tmp->arch_header[1] == ' ')
252b5132
RH
896 {
897 ardata->first_file_filepos +=
dc810e39 898 (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1;
252b5132
RH
899 }
900 bfd_release (abfd, tmp);
901 }
902 }
903
904 return true;
905
906release_raw_armap:
907 bfd_release (abfd, (PTR) raw_armap);
908release_symdefs:
909 bfd_release (abfd, (PTR) (ardata)->symdefs);
910 return false;
911}
912
913/* This routine can handle either coff-style or bsd-style armaps.
914 Returns false on error, true otherwise */
915
916boolean
917bfd_slurp_armap (abfd)
918 bfd *abfd;
919{
920 char nextname[17];
dc810e39 921 int i = bfd_bread ((PTR) nextname, (bfd_size_type) 16, abfd);
252b5132
RH
922
923 if (i == 0)
924 return true;
925 if (i != 16)
926 return false;
927
dc810e39 928 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
252b5132
RH
929 return false;
930
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))
937 {
36b45482
TS
938 /* 64bit ELF (Irix 6) archive. */
939#ifdef BFD64
940 return bfd_elf64_archive_slurp_armap (abfd);
941#else
252b5132
RH
942 bfd_set_error (bfd_error_wrong_format);
943 return false;
36b45482 944#endif
252b5132
RH
945 }
946
947 bfd_has_map (abfd) = false;
948 return true;
949}
950\f
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 '/'.
d70910e8 954 This flavour is used by hp300hpux. */
252b5132
RH
955
956#define HPUX_SYMDEF_COUNT_SIZE 2
957
958boolean
959bfd_slurp_bsd_armap_f2 (abfd)
960 bfd *abfd;
961{
962 struct areltdata *mapdata;
963 char nextname[17];
964 unsigned int counter;
965 bfd_byte *raw_armap, *rbase;
966 struct artdata *ardata = bfd_ardata (abfd);
967 char *stringbase;
968 unsigned int stringsize;
dc810e39 969 bfd_size_type amt;
252b5132 970 carsym *set;
dc810e39 971 int i = bfd_bread ((PTR) nextname, (bfd_size_type) 16, abfd);
252b5132
RH
972
973 if (i == 0)
974 return true;
975 if (i != 16)
976 return false;
977
047066e1 978 /* The archive has at least 16 bytes in it. */
dc810e39 979 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
252b5132
RH
980 return false;
981
982 if (!strncmp (nextname, "__.SYMDEF ", 16)
983 || !strncmp (nextname, "__.SYMDEF/ ", 16)) /* old Linux archives */
984 return do_slurp_bsd_armap (abfd);
985
986 if (strncmp (nextname, "/ ", 16))
987 {
988 bfd_has_map (abfd) = false;
989 return true;
990 }
991
992 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
993 if (mapdata == NULL)
994 return false;
995
dc810e39
AM
996 amt = mapdata->parsed_size;
997 raw_armap = (bfd_byte *) bfd_zalloc (abfd, amt);
252b5132
RH
998 if (raw_armap == NULL)
999 {
1000 byebye:
1001 bfd_release (abfd, (PTR) mapdata);
1002 return false;
1003 }
1004
dc810e39 1005 if (bfd_bread ((PTR) raw_armap, amt, abfd) != amt)
252b5132
RH
1006 {
1007 if (bfd_get_error () != bfd_error_system_call)
1008 bfd_set_error (bfd_error_malformed_archive);
1009 byebyebye:
1010 bfd_release (abfd, (PTR) raw_armap);
1011 goto byebye;
1012 }
1013
dc810e39 1014 ardata->symdef_count = H_GET_16 (abfd, (PTR) raw_armap);
252b5132
RH
1015
1016 if (ardata->symdef_count * BSD_SYMDEF_SIZE
1017 > mapdata->parsed_size - HPUX_SYMDEF_COUNT_SIZE)
1018 {
1019 /* Probably we're using the wrong byte ordering. */
1020 bfd_set_error (bfd_error_wrong_format);
1021 goto byebyebye;
1022 }
1023
1024 ardata->cache = 0;
1025
dc810e39 1026 stringsize = H_GET_32 (abfd, raw_armap + HPUX_SYMDEF_COUNT_SIZE);
047066e1 1027 /* Skip sym count and string sz. */
252b5132
RH
1028 stringbase = ((char *) raw_armap
1029 + HPUX_SYMDEF_COUNT_SIZE
1030 + BSD_STRING_COUNT_SIZE);
1031 rbase = (bfd_byte *) stringbase + stringsize;
dc810e39
AM
1032 amt = (bfd_size_type) ardata->symdef_count * BSD_SYMDEF_SIZE;
1033 ardata->symdefs = (carsym *) bfd_alloc (abfd, amt);
252b5132
RH
1034 if (!ardata->symdefs)
1035 return false;
1036
1037 for (counter = 0, set = ardata->symdefs;
1038 counter < ardata->symdef_count;
1039 counter++, set++, rbase += BSD_SYMDEF_SIZE)
1040 {
dc810e39
AM
1041 set->name = H_GET_32 (abfd, rbase) + stringbase;
1042 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
252b5132
RH
1043 }
1044
1045 ardata->first_file_filepos = bfd_tell (abfd);
047066e1 1046 /* Pad to an even boundary if you have to. */
252b5132
RH
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
d70910e8 1050 to be allocated on an objalloc anyway... */
252b5132
RH
1051 bfd_has_map (abfd) = true;
1052 return true;
1053}
1054\f
1055/** Extended name table.
1056
1057 Normally archives support only 14-character filenames.
1058
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
047066e1
KH
1063 extended the format by using the prefix // for the special element. */
1064
1065/* Returns false on error, true otherwise. */
252b5132 1066
252b5132
RH
1067boolean
1068_bfd_slurp_extended_name_table (abfd)
1069 bfd *abfd;
1070{
1071 char nextname[17];
1072 struct areltdata *namedata;
dc810e39 1073 bfd_size_type amt;
252b5132
RH
1074
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);
dc810e39 1078 if (bfd_bread ((PTR) nextname, (bfd_size_type) 16, abfd) == 16)
252b5132 1079 {
dc810e39 1080 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
252b5132
RH
1081 return false;
1082
1083 if (strncmp (nextname, "ARFILENAMES/ ", 16) != 0 &&
1084 strncmp (nextname, "// ", 16) != 0)
1085 {
1086 bfd_ardata (abfd)->extended_names = NULL;
1087 return true;
1088 }
1089
1090 namedata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
1091 if (namedata == NULL)
1092 return false;
1093
dc810e39
AM
1094 amt = namedata->parsed_size;
1095 bfd_ardata (abfd)->extended_names = bfd_zalloc (abfd, amt);
252b5132
RH
1096 if (bfd_ardata (abfd)->extended_names == NULL)
1097 {
1098 byebye:
1099 bfd_release (abfd, (PTR) namedata);
1100 return false;
1101 }
1102
dc810e39 1103 if (bfd_bread ((PTR) bfd_ardata (abfd)->extended_names, amt, abfd) != amt)
252b5132
RH
1104 {
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;
1109 goto byebye;
1110 }
1111
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.. */
1117 {
1118 char *temp = bfd_ardata (abfd)->extended_names;
1119 char *limit = temp + namedata->parsed_size;
047066e1
KH
1120 for (; temp < limit; ++temp)
1121 {
1122 if (*temp == '\012')
1123 temp[temp[-1] == '/' ? -1 : 0] = '\0';
1124 if (*temp == '\\')
1125 *temp = '/';
1126 }
252b5132
RH
1127 }
1128
047066e1 1129 /* Pad to an even boundary if you have to. */
252b5132
RH
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;
1133
1134 /* FIXME, we can't release namedata here because it was allocated
d70910e8 1135 below extended_names on the objalloc... */
047066e1
KH
1136#if 0
1137 bfd_release (abfd, namedata);
1138#endif
252b5132
RH
1139 }
1140 return true;
1141}
1142
1143#ifdef VMS
1144
1145/* Return a copy of the stuff in the filename between any :]> and a
047066e1
KH
1146 semicolon. */
1147
252b5132
RH
1148static const char *
1149normalize (abfd, file)
1150 bfd *abfd;
1151 const char *file;
1152{
dc810e39
AM
1153 const char *first;
1154 const char *last;
252b5132
RH
1155 char *copy;
1156
1157 first = file + strlen (file) - 1;
1158 last = first + 1;
1159
1160 while (first != file)
1161 {
1162 if (*first == ';')
1163 last = first;
1164 if (*first == ':' || *first == ']' || *first == '>')
1165 {
1166 first++;
1167 break;
1168 }
1169 first--;
1170 }
1171
dc810e39 1172 copy = (char *) bfd_alloc (abfd, (bfd_size_type) (last - first + 1));
252b5132
RH
1173 if (copy == NULL)
1174 return NULL;
1175
1176 memcpy (copy, first, last - first);
1177 copy[last - first] = 0;
1178
1179 return copy;
1180}
1181
1182#else
1183static const char *
1184normalize (abfd, file)
7442e600 1185 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
1186 const char *file;
1187{
1188 const char *filename = strrchr (file, '/');
1189
5af11cab
AM
1190#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1191 {
1192 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1193 char *bslash = strrchr (file, '\\');
2ab47eed 1194 if (filename == NULL || (bslash != NULL && bslash > filename))
5af11cab
AM
1195 filename = bslash;
1196 if (filename == NULL && file[0] != '\0' && file[1] == ':')
1197 filename = file + 1;
1198 }
1199#endif
252b5132
RH
1200 if (filename != (char *) NULL)
1201 filename++;
1202 else
1203 filename = file;
1204 return filename;
1205}
1206#endif
1207
1208/* Build a BFD style extended name table. */
1209
1210boolean
1211_bfd_archive_bsd_construct_extended_name_table (abfd, tabloc, tablen, name)
1212 bfd *abfd;
1213 char **tabloc;
1214 bfd_size_type *tablen;
1215 const char **name;
1216{
1217 *name = "ARFILENAMES/";
1218 return _bfd_construct_extended_name_table (abfd, false, tabloc, tablen);
1219}
1220
1221/* Build an SVR4 style extended name table. */
1222
1223boolean
1224_bfd_archive_coff_construct_extended_name_table (abfd, tabloc, tablen, name)
1225 bfd *abfd;
1226 char **tabloc;
1227 bfd_size_type *tablen;
1228 const char **name;
1229{
1230 *name = "//";
1231 return _bfd_construct_extended_name_table (abfd, true, tabloc, tablen);
1232}
1233
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! */
1241
1242boolean
1243_bfd_construct_extended_name_table (abfd, trailing_slash, tabloc, tablen)
1244 bfd *abfd;
1245 boolean trailing_slash;
1246 char **tabloc;
1247 bfd_size_type *tablen;
1248{
1249 unsigned int maxname = abfd->xvec->ar_max_namelen;
dc810e39 1250 bfd_size_type total_namelen = 0;
252b5132
RH
1251 bfd *current;
1252 char *strptr;
1253
1254 *tablen = 0;
1255
047066e1 1256 /* Figure out how long the table should be. */
252b5132
RH
1257 for (current = abfd->archive_head; current != NULL; current = current->next)
1258 {
1259 const char *normal;
1260 unsigned int thislen;
1261
1262 normal = normalize (current, current->filename);
1263 if (normal == NULL)
1264 return false;
1265
1266 thislen = strlen (normal);
1267
1268 if (thislen > maxname
1269 && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
1270 thislen = maxname;
1271
1272 if (thislen > maxname)
1273 {
1274 /* Add one to leave room for \n. */
1275 total_namelen += thislen + 1;
1276 if (trailing_slash)
1277 {
1278 /* Leave room for trailing slash. */
1279 ++total_namelen;
1280 }
1281 }
1282 else
1283 {
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)))
1288 {
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);
1295 }
1296 }
1297 }
1298
1299 if (total_namelen == 0)
1300 return true;
1301
1302 *tabloc = bfd_zalloc (abfd, total_namelen);
1303 if (*tabloc == NULL)
1304 return false;
1305
1306 *tablen = total_namelen;
1307 strptr = *tabloc;
1308
1309 for (current = abfd->archive_head; current != NULL; current =
1310 current->next)
1311 {
1312 const char *normal;
1313 unsigned int thislen;
1314
1315 normal = normalize (current, current->filename);
1316 if (normal == NULL)
1317 return false;
1318
1319 thislen = strlen (normal);
1320 if (thislen > maxname)
1321 {
1322 /* Works for now; may need to be re-engineered if we
1323 encounter an oddball archive format and want to
d70910e8 1324 generalise this hack. */
252b5132
RH
1325 struct ar_hdr *hdr = arch_hdr (current);
1326 strcpy (strptr, normal);
1327 if (! trailing_slash)
1328 strptr[thislen] = '\012';
1329 else
1330 {
1331 strptr[thislen] = '/';
1332 strptr[thislen + 1] = '\012';
1333 }
1334 hdr->ar_name[0] = ar_padchar (current);
1335 /* We know there will always be enough room (one of the few
d70910e8 1336 cases where you may safely use sprintf). */
252b5132
RH
1337 sprintf ((hdr->ar_name) + 1, "%-d", (unsigned) (strptr - *tabloc));
1338 /* Kinda Kludgy. We should just use the returned value of
047066e1 1339 sprintf but not all implementations get this right. */
252b5132
RH
1340 {
1341 char *temp = hdr->ar_name + 2;
1342 for (; temp < hdr->ar_name + maxname; temp++)
1343 if (*temp == '\0')
1344 *temp = ' ';
1345 }
1346 strptr += thislen + 1;
1347 if (trailing_slash)
1348 ++strptr;
1349 }
1350 }
1351
1352 return true;
1353}
1354\f
1355/** A couple of functions for creating ar_hdrs */
1356
23afc6f6
JL
1357#ifdef HPUX_LARGE_AR_IDS
1358/* Function to encode large UID/GID values according to HP. */
047066e1 1359
23afc6f6
JL
1360static void
1361hpux_uid_gid_encode (str, id)
1362 char str[6];
1363 long int id;
1364{
1365 int cnt;
1366
1367 str[5] = '@' + (id & 3);
1368 id >>= 2;
1369
1370 for (cnt = 4; cnt >= 0; ++cnt, id >>= 6)
1371 str[cnt] = ' ' + (id & 0x3f);
1372}
1373#endif /* HPUX_LARGE_AR_IDS */
1374
633fd09f
ILT
1375#ifndef HAVE_GETUID
1376#define getuid() 0
1377#endif
1378
1379#ifndef HAVE_GETGID
1380#define getgid() 0
1381#endif
1382
252b5132
RH
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
d70910e8 1386 is set, and it's an in-memory bfd, we fake it. */
252b5132
RH
1387
1388static struct areltdata *
1389bfd_ar_hdr_from_filesystem (abfd, filename, member)
1390 bfd *abfd;
1391 const char *filename;
1392 bfd *member;
1393{
1394 struct stat status;
1395 struct areltdata *ared;
1396 struct ar_hdr *hdr;
1397 char *temp, *temp1;
dc810e39 1398 bfd_size_type amt;
252b5132
RH
1399
1400 if (member && (member->flags & BFD_IN_MEMORY) != 0)
1401 {
047066e1 1402 /* Assume we just "made" the member, and fake it. */
252b5132 1403 struct bfd_in_memory *bim = (struct bfd_in_memory *) member->iostream;
047066e1
KH
1404 time (&status.st_mtime);
1405 status.st_uid = getuid ();
1406 status.st_gid = getgid ();
252b5132
RH
1407 status.st_mode = 0644;
1408 status.st_size = bim->size;
1409 }
1410 else if (stat (filename, &status) != 0)
1411 {
1412 bfd_set_error (bfd_error_system_call);
1413 return NULL;
1414 }
1415
dc810e39
AM
1416 amt = sizeof (struct ar_hdr) + sizeof (struct areltdata);
1417 ared = (struct areltdata *) bfd_zalloc (abfd, amt);
252b5132
RH
1418 if (ared == NULL)
1419 return NULL;
1420 hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
1421
047066e1 1422 /* ar headers are space padded, not null padded! */
252b5132
RH
1423 memset ((PTR) hdr, ' ', sizeof (struct ar_hdr));
1424
1425 strncpy (hdr->ar_fmag, ARFMAG, 2);
1426
047066e1 1427 /* Goddamned sprintf doesn't permit MAXIMUM field lengths. */
252b5132 1428 sprintf ((hdr->ar_date), "%-12ld", (long) status.st_mtime);
23afc6f6
JL
1429#ifdef HPUX_LARGE_AR_IDS
1430 /* HP has a very "special" way to handle UID/GID's with numeric values
1431 > 99999. */
1432 if (status.st_uid > 99999)
1433 hpux_uid_gid_encode (hdr->ar_gid, (long) status.st_uid);
1434 else
1435#endif
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
1439 > 99999. */
1440 if (status.st_gid > 99999)
1441 hpux_uid_gid_encode (hdr->ar_uid, (long) status.st_gid);
1442 else
1443#endif
252b5132
RH
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
047066e1 1449 IO operations. */
252b5132
RH
1450 temp = (char *) hdr;
1451 temp1 = temp + sizeof (struct ar_hdr) - 2;
1452 for (; temp < temp1; temp++)
1453 {
1454 if (*temp == '\0')
1455 *temp = ' ';
1456 }
1457 strncpy (hdr->ar_fmag, ARFMAG, 2);
1458 ared->parsed_size = status.st_size;
1459 ared->arch_header = (char *) hdr;
1460
1461 return ared;
1462}
1463
1464/* This is magic required by the "ar" program. Since it's
047066e1
KH
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! */
252b5132
RH
1468
1469struct ar_hdr *bfd_special_undocumented_glue PARAMS ((bfd *, const char *));
1470
1471struct ar_hdr *
1472bfd_special_undocumented_glue (abfd, filename)
1473 bfd *abfd;
1474 const char *filename;
1475{
1476 struct areltdata *ar_elt = bfd_ar_hdr_from_filesystem (abfd, filename, 0);
1477 if (ar_elt == NULL)
1478 return NULL;
1479 return (struct ar_hdr *) ar_elt->arch_header;
1480}
1481
047066e1
KH
1482/* Analogous to stat call. */
1483
252b5132
RH
1484int
1485bfd_generic_stat_arch_elt (abfd, buf)
1486 bfd *abfd;
1487 struct stat *buf;
1488{
1489 struct ar_hdr *hdr;
1490 char *aloser;
1491
1492 if (abfd->arelt_data == NULL)
1493 {
1494 bfd_set_error (bfd_error_invalid_operation);
1495 return -1;
1496 }
1497
1498 hdr = arch_hdr (abfd);
1499
047066e1
KH
1500#define foo(arelt, stelt, size) \
1501 buf->stelt = strtol (hdr->arelt, &aloser, size); \
1502 if (aloser == hdr->arelt) \
1503 return -1;
1504
23afc6f6
JL
1505 /* Some platforms support special notations for large IDs. */
1506#ifdef HPUX_LARGE_AR_IDS
047066e1
KH
1507# define foo2(arelt, stelt, size) \
1508 if (hdr->arelt[5] == ' ') \
1509 { \
1510 foo (arelt, stelt, size); \
1511 } \
1512 else \
1513 { \
1514 int cnt; \
1515 for (buf->stelt = cnt = 0; cnt < 5; ++cnt) \
1516 { \
1517 if (hdr->arelt[cnt] < ' ' || hdr->arelt[cnt] > ' ' + 0x3f) \
1518 return -1; \
1519 buf->stelt <<= 6; \
1520 buf->stelt += hdr->arelt[cnt] - ' '; \
1521 } \
1522 if (hdr->arelt[5] < '@' || hdr->arelt[5] > '@' + 3) \
1523 return -1; \
1524 buf->stelt <<= 2; \
1525 buf->stelt += hdr->arelt[5] - '@'; \
1526 }
23afc6f6
JL
1527#else
1528# define foo2(arelt, stelt, size) foo (arelt, stelt, size)
1529#endif
252b5132
RH
1530
1531 foo (ar_date, st_mtime, 10);
23afc6f6
JL
1532 foo2 (ar_uid, st_uid, 10);
1533 foo2 (ar_gid, st_gid, 10);
252b5132
RH
1534 foo (ar_mode, st_mode, 8);
1535
1536 buf->st_size = arch_eltdata (abfd)->parsed_size;
1537
1538 return 0;
1539}
1540
1541void
1542bfd_dont_truncate_arname (abfd, pathname, arhdr)
1543 bfd *abfd;
dc810e39 1544 const char *pathname;
252b5132
RH
1545 char *arhdr;
1546{
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
d70910e8 1550 intel's release is out the door. */
252b5132
RH
1551
1552 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1553 size_t length;
1554 const char *filename;
1555 size_t maxlen = ar_maxnamelen (abfd);
1556
1557 if ((bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
1558 {
1559 bfd_bsd_truncate_arname (abfd, pathname, arhdr);
1560 return;
1561 }
1562
1563 filename = normalize (abfd, pathname);
1564 if (filename == NULL)
1565 {
1566 /* FIXME */
1567 abort ();
1568 }
1569
1570 length = strlen (filename);
1571
1572 if (length <= maxlen)
1573 memcpy (hdr->ar_name, filename, length);
1574
1575 /* Add the padding character if there is room for it. */
1576 if (length < maxlen
1577 || (length == maxlen && length < sizeof hdr->ar_name))
1578 (hdr->ar_name)[length] = ar_padchar (abfd);
1579}
1580
1581void
1582bfd_bsd_truncate_arname (abfd, pathname, arhdr)
1583 bfd *abfd;
dc810e39 1584 const char *pathname;
252b5132
RH
1585 char *arhdr;
1586{
1587 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
dc810e39
AM
1588 size_t length;
1589 const char *filename = strrchr (pathname, '/');
1590 size_t maxlen = ar_maxnamelen (abfd);
252b5132 1591
5af11cab
AM
1592#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1593 {
1594 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1595 char *bslash = strrchr (pathname, '\\');
2ab47eed 1596 if (filename == NULL || (bslash != NULL && bslash > filename))
5af11cab
AM
1597 filename = bslash;
1598 if (filename == NULL && pathname[0] != '\0' && pathname[1] == ':')
1599 filename = pathname + 1;
1600 }
1601#endif
1602
252b5132
RH
1603 if (filename == NULL)
1604 filename = pathname;
1605 else
1606 ++filename;
1607
1608 length = strlen (filename);
1609
1610 if (length <= maxlen)
1611 memcpy (hdr->ar_name, filename, length);
1612 else
1613 {
1614 /* pathname: meet procrustes */
1615 memcpy (hdr->ar_name, filename, maxlen);
1616 length = maxlen;
1617 }
1618
1619 if (length < maxlen)
1620 (hdr->ar_name)[length] = ar_padchar (abfd);
1621}
1622
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. */
1628
1629/* This is what gnu ar does. It's better but incompatible with the
d70910e8 1630 bsd ar. */
252b5132
RH
1631
1632void
1633bfd_gnu_truncate_arname (abfd, pathname, arhdr)
1634 bfd *abfd;
dc810e39 1635 const char *pathname;
252b5132
RH
1636 char *arhdr;
1637{
1638 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
dc810e39
AM
1639 size_t length;
1640 const char *filename = strrchr (pathname, '/');
1641 size_t maxlen = ar_maxnamelen (abfd);
252b5132 1642
5af11cab
AM
1643#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1644 {
1645 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1646 char *bslash = strrchr (pathname, '\\');
2ab47eed 1647 if (filename == NULL || (bslash != NULL && bslash > filename))
5af11cab
AM
1648 filename = bslash;
1649 if (filename == NULL && pathname[0] != '\0' && pathname[1] == ':')
1650 filename = pathname + 1;
1651 }
1652#endif
1653
252b5132
RH
1654 if (filename == NULL)
1655 filename = pathname;
1656 else
1657 ++filename;
1658
1659 length = strlen (filename);
1660
1661 if (length <= maxlen)
1662 memcpy (hdr->ar_name, filename, length);
1663 else
1664 { /* pathname: meet procrustes */
1665 memcpy (hdr->ar_name, filename, maxlen);
1666 if ((filename[length - 2] == '.') && (filename[length - 1] == 'o'))
1667 {
1668 hdr->ar_name[maxlen - 2] = '.';
1669 hdr->ar_name[maxlen - 1] = 'o';
1670 }
1671 length = maxlen;
1672 }
1673
1674 if (length < 16)
1675 (hdr->ar_name)[length] = ar_padchar (abfd);
1676}
1677\f
047066e1 1678/* The BFD is open for write and has its format set to bfd_archive. */
252b5132
RH
1679
1680boolean
1681_bfd_write_archive_contents (arch)
1682 bfd *arch;
1683{
1684 bfd *current;
1685 char *etable = NULL;
1686 bfd_size_type elength = 0;
1687 const char *ename = NULL;
1688 boolean makemap = bfd_has_map (arch);
047066e1 1689 boolean hasobjects = false; /* If no .o's, don't bother to make a map. */
252b5132
RH
1690 bfd_size_type wrote;
1691 unsigned int i;
1692 int tries;
1693
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)
1698 {
8000a618
DD
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. */
252b5132
RH
1704 if (bfd_write_p (current))
1705 {
1706 bfd_set_error (bfd_error_invalid_operation);
1707 return false;
1708 }
1709 if (!current->arelt_data)
1710 {
1711 current->arelt_data =
1712 (PTR) bfd_ar_hdr_from_filesystem (arch, current->filename, current);
1713 if (!current->arelt_data)
1714 return false;
1715
047066e1 1716 /* Put in the file name. */
252b5132
RH
1717 BFD_SEND (arch, _bfd_truncate_arname, (arch,
1718 current->filename,
1719 (char *) arch_hdr (current)));
1720 }
1721
1722 if (makemap && ! hasobjects)
047066e1 1723 { /* Don't bother if we won't make a map! */
252b5132
RH
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))
1727#endif
1728 )
1729 hasobjects = true;
1730 }
1731 }
1732
1733 if (!BFD_SEND (arch, _bfd_construct_extended_name_table,
1734 (arch, &etable, &elength, &ename)))
1735 return false;
1736
1737 if (bfd_seek (arch, (file_ptr) 0, SEEK_SET) != 0)
1738 return false;
1739#ifdef GNU960
dc810e39 1740 wrote = bfd_bwrite (BFD_GNU960_ARMAG (arch), (bfd_size_type) SARMAG, arch);
252b5132 1741#else
dc810e39 1742 wrote = bfd_bwrite (ARMAG, (bfd_size_type) SARMAG, arch);
252b5132
RH
1743#endif
1744 if (wrote != SARMAG)
1745 return false;
1746
1747 if (makemap && hasobjects)
1748 {
82e51918 1749 if (! _bfd_compute_and_write_armap (arch, (unsigned int) elength))
252b5132
RH
1750 return false;
1751 }
1752
1753 if (elength != 0)
1754 {
1755 struct ar_hdr hdr;
1756
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",
dc810e39 1761 (int) ((elength + 1) & ~(bfd_size_type) 1));
252b5132
RH
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]) = ' ';
dc810e39 1766 if ((bfd_bwrite ((PTR) &hdr, (bfd_size_type) sizeof (struct ar_hdr), arch)
252b5132 1767 != sizeof (struct ar_hdr))
dc810e39 1768 || bfd_bwrite (etable, elength, arch) != elength)
252b5132
RH
1769 return false;
1770 if ((elength % 2) == 1)
1771 {
dc810e39 1772 if (bfd_bwrite ("\012", (bfd_size_type) 1, arch) != 1)
252b5132
RH
1773 return false;
1774 }
1775 }
1776
1777 for (current = arch->archive_head; current; current = current->next)
1778 {
1779 char buffer[DEFAULT_BUFFERSIZE];
1780 unsigned int remaining = arelt_size (current);
1781 struct ar_hdr *hdr = arch_hdr (current);
1782
047066e1 1783 /* Write ar header. */
dc810e39
AM
1784 if (bfd_bwrite ((PTR) hdr, (bfd_size_type) sizeof (*hdr), arch)
1785 != sizeof (*hdr))
252b5132
RH
1786 return false;
1787 if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0)
1788 return false;
1789 while (remaining)
1790 {
1791 unsigned int amt = DEFAULT_BUFFERSIZE;
1792 if (amt > remaining)
1793 amt = remaining;
1794 errno = 0;
dc810e39 1795 if (bfd_bread (buffer, (bfd_size_type) amt, current) != amt)
252b5132
RH
1796 {
1797 if (bfd_get_error () != bfd_error_system_call)
1798 bfd_set_error (bfd_error_malformed_archive);
1799 return false;
1800 }
dc810e39 1801 if (bfd_bwrite (buffer, (bfd_size_type) amt, arch) != amt)
252b5132
RH
1802 return false;
1803 remaining -= amt;
1804 }
1805 if ((arelt_size (current) % 2) == 1)
1806 {
dc810e39 1807 if (bfd_bwrite ("\012", (bfd_size_type) 1, arch) != 1)
252b5132
RH
1808 return false;
1809 }
1810 }
1811
1812 if (makemap && hasobjects)
1813 {
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. */
1820 tries = 1;
1821 do
1822 {
1823 if (bfd_update_armap_timestamp (arch))
1824 break;
1825 (*_bfd_error_handler)
1826 (_("Warning: writing archive was slow: rewriting timestamp\n"));
1827 }
1828 while (++tries < 6);
1829 }
1830
1831 return true;
1832}
1833\f
047066e1 1834/* Note that the namidx for the first symbol is 0. */
252b5132
RH
1835
1836boolean
1837_bfd_compute_and_write_armap (arch, elength)
1838 bfd *arch;
1839 unsigned int elength;
1840{
1841 char *first_name = NULL;
1842 bfd *current;
1843 file_ptr elt_no = 0;
1844 struct orl *map = NULL;
dc810e39
AM
1845 unsigned int orl_max = 1024; /* fine initial default */
1846 unsigned int orl_count = 0;
252b5132
RH
1847 int stridx = 0; /* string index */
1848 asymbol **syms = NULL;
1849 long syms_max = 0;
1850 boolean ret;
dc810e39 1851 bfd_size_type amt;
252b5132 1852
d70910e8 1853 /* Dunno if this is the best place for this info... */
252b5132
RH
1854 if (elength != 0)
1855 elength += sizeof (struct ar_hdr);
1856 elength += elength % 2;
1857
dc810e39
AM
1858 amt = (bfd_size_type) orl_max * sizeof (struct orl);
1859 map = (struct orl *) bfd_malloc (amt);
252b5132
RH
1860 if (map == NULL)
1861 goto error_return;
1862
1863 /* We put the symbol names on the arch objalloc, and then discard
1864 them when done. */
dc810e39 1865 first_name = bfd_alloc (arch, (bfd_size_type) 1);
252b5132
RH
1866 if (first_name == NULL)
1867 goto error_return;
1868
047066e1 1869 /* Drop all the files called __.SYMDEF, we're going to make our own. */
252b5132
RH
1870 while (arch->archive_head &&
1871 strcmp (arch->archive_head->filename, "__.SYMDEF") == 0)
1872 arch->archive_head = arch->archive_head->next;
1873
047066e1 1874 /* Map over each element. */
252b5132
RH
1875 for (current = arch->archive_head;
1876 current != (bfd *) NULL;
1877 current = current->next, elt_no++)
1878 {
82e51918
AM
1879 if (bfd_check_format (current, bfd_object)
1880 && (bfd_get_file_flags (current) & HAS_SYMS) != 0)
252b5132
RH
1881 {
1882 long storage;
1883 long symcount;
1884 long src_count;
1885
1886 storage = bfd_get_symtab_upper_bound (current);
1887 if (storage < 0)
1888 goto error_return;
1889
1890 if (storage != 0)
1891 {
1892 if (storage > syms_max)
1893 {
1894 if (syms_max > 0)
1895 free (syms);
1896 syms_max = storage;
dc810e39 1897 syms = (asymbol **) bfd_malloc ((bfd_size_type) syms_max);
252b5132
RH
1898 if (syms == NULL)
1899 goto error_return;
1900 }
1901 symcount = bfd_canonicalize_symtab (current, syms);
1902 if (symcount < 0)
1903 goto error_return;
1904
047066e1
KH
1905 /* Now map over all the symbols, picking out the ones we
1906 want. */
252b5132
RH
1907 for (src_count = 0; src_count < symcount; src_count++)
1908 {
1909 flagword flags = (syms[src_count])->flags;
1910 asection *sec = syms[src_count]->section;
1911
77fb9c28
NC
1912 if ((flags & BSF_GLOBAL ||
1913 flags & BSF_WEAK ||
1914 flags & BSF_INDIRECT ||
1915 bfd_is_com_section (sec))
1916 && ! bfd_is_und_section (sec))
252b5132 1917 {
dc810e39 1918 bfd_size_type namelen;
77fb9c28
NC
1919 struct orl *new_map;
1920
047066e1 1921 /* This symbol will go into the archive header. */
252b5132
RH
1922 if (orl_count == orl_max)
1923 {
1924 orl_max *= 2;
dc810e39
AM
1925 amt = (bfd_size_type) orl_max * sizeof (struct orl);
1926 new_map = (struct orl *) bfd_realloc (map, amt);
252b5132
RH
1927 if (new_map == (struct orl *) NULL)
1928 goto error_return;
1929
1930 map = new_map;
1931 }
1932
1933 namelen = strlen (syms[src_count]->name);
dc810e39
AM
1934 amt = sizeof (char *);
1935 map[orl_count].name = (char **) bfd_alloc (arch, amt);
252b5132
RH
1936 if (map[orl_count].name == NULL)
1937 goto error_return;
1938 *(map[orl_count].name) = bfd_alloc (arch, namelen + 1);
1939 if (*(map[orl_count].name) == NULL)
1940 goto error_return;
1941 strcpy (*(map[orl_count].name), syms[src_count]->name);
dc810e39
AM
1942 map[orl_count].u.abfd = current;
1943 map[orl_count].namidx = stridx;
252b5132
RH
1944
1945 stridx += namelen + 1;
1946 ++orl_count;
77fb9c28 1947 }
252b5132
RH
1948 }
1949 }
1950
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))
1954 goto error_return;
1955 }
1956 }
1957
047066e1 1958 /* OK, now we have collected all the data, let's write them out. */
252b5132
RH
1959 ret = BFD_SEND (arch, write_armap,
1960 (arch, elength, map, orl_count, stridx));
1961
1962 if (syms_max > 0)
1963 free (syms);
1964 if (map != NULL)
1965 free (map);
1966 if (first_name != NULL)
1967 bfd_release (arch, first_name);
1968
1969 return ret;
1970
1971 error_return:
1972 if (syms_max > 0)
1973 free (syms);
1974 if (map != NULL)
1975 free (map);
1976 if (first_name != NULL)
1977 bfd_release (arch, first_name);
1978
1979 return false;
1980}
1981
1982boolean
1983bsd_write_armap (arch, elength, map, orl_count, stridx)
1984 bfd *arch;
1985 unsigned int elength;
1986 struct orl *map;
1987 unsigned int orl_count;
1988 int stridx;
1989{
1990 int padit = stridx & 1;
1991 unsigned int ranlibsize = orl_count * BSD_SYMDEF_SIZE;
1992 unsigned int stringsize = stridx + padit;
d70910e8 1993 /* Include 8 bytes to store ranlibsize and stringsize in output. */
252b5132
RH
1994 unsigned int mapsize = ranlibsize + stringsize + 8;
1995 file_ptr firstreal;
1996 bfd *current = arch->archive_head;
1997 bfd *last_elt = current; /* last element arch seen */
1998 bfd_byte temp[4];
1999 unsigned int count;
2000 struct ar_hdr hdr;
2001 struct stat statbuf;
2002 unsigned int i;
2003
2004 firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
2005
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);
252b5132
RH
2014 sprintf (hdr.ar_uid, "%ld", (long) getuid ());
2015 sprintf (hdr.ar_gid, "%ld", (long) getgid ());
252b5132
RH
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]) = ' ';
dc810e39 2021 if (bfd_bwrite ((PTR) &hdr, (bfd_size_type) sizeof (struct ar_hdr), arch)
252b5132
RH
2022 != sizeof (struct ar_hdr))
2023 return false;
dc810e39
AM
2024 H_PUT_32 (arch, ranlibsize, temp);
2025 if (bfd_bwrite (temp, (bfd_size_type) sizeof (temp), arch) != sizeof (temp))
252b5132
RH
2026 return false;
2027
2028 for (count = 0; count < orl_count; count++)
2029 {
2030 bfd_byte buf[BSD_SYMDEF_SIZE];
2031
dc810e39 2032 if (map[count].u.abfd != last_elt)
252b5132
RH
2033 {
2034 do
2035 {
2036 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
2037 firstreal += firstreal % 2;
2038 current = current->next;
2039 }
dc810e39 2040 while (current != map[count].u.abfd);
252b5132
RH
2041 } /* if new archive element */
2042
2043 last_elt = current;
dc810e39
AM
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)
2047 != BSD_SYMDEF_SIZE)
252b5132
RH
2048 return false;
2049 }
2050
047066e1 2051 /* Now write the strings themselves. */
dc810e39
AM
2052 H_PUT_32 (arch, stringsize, temp);
2053 if (bfd_bwrite (temp, (bfd_size_type) sizeof (temp), arch) != sizeof (temp))
252b5132
RH
2054 return false;
2055 for (count = 0; count < orl_count; count++)
2056 {
2057 size_t len = strlen (*map[count].name) + 1;
2058
dc810e39 2059 if (bfd_bwrite (*map[count].name, (bfd_size_type) len, arch) != len)
252b5132
RH
2060 return false;
2061 }
2062
2063 /* The spec sez this should be a newline. But in order to be
d70910e8 2064 bug-compatible for sun's ar we use a null. */
252b5132
RH
2065 if (padit)
2066 {
dc810e39 2067 if (bfd_bwrite ("", (bfd_size_type) 1, arch) != 1)
252b5132
RH
2068 return false;
2069 }
2070
2071 return true;
2072}
2073
2074/* At the end of archive file handling, update the timestamp in the
2075 file, so the linker will accept it.
2076
2077 Return true if the timestamp was OK, or an unusual problem happened.
2078 Return false if we updated the timestamp. */
2079
2080boolean
2081_bfd_archive_bsd_update_armap_timestamp (arch)
2082 bfd *arch;
2083{
2084 struct stat archstat;
2085 struct ar_hdr hdr;
2086 unsigned int i;
2087
2088 /* Flush writes, get last-write timestamp from file, and compare it
2089 to the timestamp IN the file. */
2090 bfd_flush (arch);
2091 if (bfd_stat (arch, &archstat) == -1)
2092 {
2093 perror (_("Reading archive file mod timestamp"));
047066e1
KH
2094
2095 /* Can't read mod time for some reason. */
2096 return true;
252b5132
RH
2097 }
2098 if (archstat.st_mtime <= bfd_ardata (arch)->armap_timestamp)
047066e1
KH
2099 /* OK by the linker's rules. */
2100 return true;
252b5132
RH
2101
2102 /* Update the timestamp. */
2103 bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET;
2104
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] = ' ';
2111
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
dc810e39 2116 || (bfd_bwrite (hdr.ar_date, (bfd_size_type) sizeof (hdr.ar_date), arch)
252b5132
RH
2117 != sizeof (hdr.ar_date)))
2118 {
2119 /* FIXME: bfd can't call perror. */
2120 perror (_("Writing updated armap timestamp"));
047066e1
KH
2121
2122 /* Some error while writing. */
2123 return true;
252b5132
RH
2124 }
2125
047066e1
KH
2126 /* We updated the timestamp successfully. */
2127 return false;
252b5132
RH
2128}
2129\f
2130/* A coff armap looks like :
2131 lARMAG
2132 struct ar_hdr with name = '/'
2133 number of symbols
2134 offset of file for symbol 0
2135 offset of file for symbol 1
2136
2137 offset of file for symbol n-1
2138 symbol name 0
2139 symbol name 1
2140
2141 symbol name n-1
2142*/
2143
2144boolean
2145coff_write_armap (arch, elength, map, symbol_count, stridx)
2146 bfd *arch;
2147 unsigned int elength;
2148 struct orl *map;
2149 unsigned int symbol_count;
2150 int stridx;
2151{
2152 /* The size of the ranlib is the number of exported symbols in the
08da05b0 2153 archive * the number of bytes in an int, + an int for the count. */
252b5132
RH
2154 unsigned int ranlibsize = (symbol_count * 4) + 4;
2155 unsigned int stringsize = stridx;
2156 unsigned int mapsize = stringsize + ranlibsize;
dc810e39 2157 unsigned int archive_member_file_ptr;
252b5132
RH
2158 bfd *current = arch->archive_head;
2159 unsigned int count;
2160 struct ar_hdr hdr;
2161 unsigned int i;
2162 int padit = mapsize & 1;
2163
2164 if (padit)
2165 mapsize++;
2166
047066e1 2167 /* Work out where the first object file will go in the archive. */
252b5132
RH
2168 archive_member_file_ptr = (mapsize
2169 + elength
2170 + sizeof (struct ar_hdr)
2171 + SARMAG);
2172
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));
047066e1 2177 /* This, at least, is what Intel coff sets the values to. */
252b5132
RH
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);
2182
2183 for (i = 0; i < sizeof (struct ar_hdr); i++)
2184 if (((char *) (&hdr))[i] == '\0')
2185 (((char *) (&hdr))[i]) = ' ';
2186
047066e1 2187 /* Write the ar header for this item and the number of symbols. */
252b5132 2188
dc810e39 2189 if (bfd_bwrite ((PTR) &hdr, (bfd_size_type) sizeof (struct ar_hdr), arch)
252b5132
RH
2190 != sizeof (struct ar_hdr))
2191 return false;
2192
4dae1ae7
AM
2193 if (!bfd_write_bigendian_4byte_int (arch, symbol_count))
2194 return false;
252b5132
RH
2195
2196 /* Two passes, first write the file offsets for each symbol -
2197 remembering that each offset is on a two byte boundary. */
2198
2199 /* Write out the file offset for the file associated with each
2200 symbol, and remember to keep the offsets padded out. */
2201
2202 current = arch->archive_head;
2203 count = 0;
2204 while (current != (bfd *) NULL && count < symbol_count)
2205 {
047066e1
KH
2206 /* For each symbol which is used defined in this object, write
2207 out the object file's address in the archive. */
252b5132 2208
dc810e39 2209 while (count < symbol_count && map[count].u.abfd == current)
252b5132 2210 {
4dae1ae7
AM
2211 if (!bfd_write_bigendian_4byte_int (arch, archive_member_file_ptr))
2212 return false;
252b5132
RH
2213 count++;
2214 }
047066e1 2215 /* Add size of this archive entry. */
252b5132
RH
2216 archive_member_file_ptr += (arelt_size (current)
2217 + sizeof (struct ar_hdr));
047066e1 2218 /* Remember aboout the even alignment. */
252b5132
RH
2219 archive_member_file_ptr += archive_member_file_ptr % 2;
2220 current = current->next;
2221 }
2222
047066e1 2223 /* Now write the strings themselves. */
252b5132
RH
2224 for (count = 0; count < symbol_count; count++)
2225 {
2226 size_t len = strlen (*map[count].name) + 1;
2227
dc810e39 2228 if (bfd_bwrite (*map[count].name, (bfd_size_type) len, arch) != len)
252b5132
RH
2229 return false;
2230 }
2231
2232 /* The spec sez this should be a newline. But in order to be
d70910e8 2233 bug-compatible for arc960 we use a null. */
252b5132
RH
2234 if (padit)
2235 {
dc810e39 2236 if (bfd_bwrite ("", (bfd_size_type) 1, arch) != 1)
252b5132
RH
2237 return false;
2238 }
2239
2240 return true;
2241}