Fix gas_version.
[binutils-gdb.git] / bfd / archive.c
1 /* BFD back-end for archive files (libraries).
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 Written by Cygnus Support. Mostly Gumby Henkel-Wallace's fault.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 /*
22 @setfilename archive-info
23 SECTION
24 Archives
25
26 DESCRIPTION
27 An archive (or library) is just another BFD. It has a symbol
28 table, although there's not much a user program will do with it.
29
30 The big difference between an archive BFD and an ordinary BFD
31 is that the archive doesn't have sections. Instead it has a
32 chain of BFDs that are considered its contents. These BFDs can
33 be manipulated like any other. The BFDs contained in an
34 archive opened for reading will all be opened for reading. You
35 may put either input or output BFDs into an archive opened for
36 output; they will be handled correctly when the archive is closed.
37
38 Use <<bfd_openr_next_archived_file>> to step through
39 the contents of an archive opened for input. You don't
40 have to read the entire archive if you don't want
41 to! Read it until you find what you want.
42
43 Archive contents of output BFDs are chained through the
44 <<next>> pointer in a BFD. The first one is findable through
45 the <<archive_head>> slot of the archive. Set it with
46 <<bfd_set_archive_head>> (q.v.). A given BFD may be in only one
47 open output archive at a time.
48
49 As expected, the BFD archive code is more general than the
50 archive code of any given environment. BFD archives may
51 contain files of different formats (e.g., a.out and coff) and
52 even different architectures. You may even place archives
53 recursively into archives!
54
55 This can cause unexpected confusion, since some archive
56 formats are more expressive than others. For instance, Intel
57 COFF archives can preserve long filenames; SunOS a.out archives
58 cannot. If you move a file from the first to the second
59 format and back again, the filename may be truncated.
60 Likewise, different a.out environments have different
61 conventions as to how they truncate filenames, whether they
62 preserve directory names in filenames, etc. When
63 interoperating with native tools, be sure your files are
64 homogeneous.
65
66 Beware: most of these formats do not react well to the
67 presence of spaces in filenames. We do the best we can, but
68 can't always handle this case due to restrictions in the format of
69 archives. Many Unix utilities are braindead in regards to
70 spaces and such in filenames anyway, so this shouldn't be much
71 of a restriction.
72
73 Archives are supported in BFD in <<archive.c>>.
74
75 */
76
77 /* Assumes:
78 o - all archive elements start on an even boundary, newline padded;
79 o - all arch headers are char *;
80 o - all arch headers are the same size (across architectures).
81 */
82
83 /* Some formats provide a way to cram a long filename into the short
84 (16 chars) space provided by a BSD archive. The trick is: make a
85 special "file" in the front of the archive, sort of like the SYMDEF
86 entry. If the filename is too long to fit, put it in the extended
87 name table, and use its index as the filename. To prevent
88 confusion prepend the index with a space. This means you can't
89 have filenames that start with a space, but then again, many Unix
90 utilities can't handle that anyway.
91
92 This scheme unfortunately requires that you stand on your head in
93 order to write an archive since you need to put a magic file at the
94 front, and need to touch every entry to do so. C'est la vie.
95
96 We support two variants of this idea:
97 The SVR4 format (extended name table is named "//"),
98 and an extended pseudo-BSD variant (extended name table is named
99 "ARFILENAMES/"). The origin of the latter format is uncertain.
100
101 BSD 4.4 uses a third scheme: It writes a long filename
102 directly after the header. This allows 'ar q' to work.
103 We currently can read BSD 4.4 archives, but not write them.
104 */
105
106 /* Summary of archive member names:
107
108 Symbol table (must be first):
109 "__.SYMDEF " - Symbol table, Berkeley style, produced by ranlib.
110 "/ " - Symbol table, system 5 style.
111
112 Long name table (must be before regular file members):
113 "// " - Long name table, System 5 R4 style.
114 "ARFILENAMES/ " - Long name table, non-standard extended BSD (not BSD 4.4).
115
116 Regular file members with short names:
117 "filename.o/ " - Regular file, System 5 style (embedded spaces ok).
118 "filename.o " - Regular file, Berkeley style (no embedded spaces).
119
120 Regular files with long names (or embedded spaces, for BSD variants):
121 "/18 " - SVR4 style, name at offset 18 in name table.
122 "#1/23 " - Long name (or embedded paces) 23 characters long,
123 BSD 4.4 style, full name follows header.
124 Implemented for reading, not writing.
125 " 18 " - Long name 18 characters long, extended pseudo-BSD.
126 */
127
128 #include "bfd.h"
129 #include "sysdep.h"
130 #include "libbfd.h"
131 #include "aout/ar.h"
132 #include "aout/ranlib.h"
133 #include <errno.h>
134 #include <string.h> /* For memchr, strrchr and friends */
135 #include <ctype.h>
136
137 #ifndef errno
138 extern int errno;
139 #endif
140
141 #ifdef GNU960
142 #define BFD_GNU960_ARMAG(abfd) (BFD_COFF_FILE_P((abfd)) ? ARMAG : ARMAGB)
143 #endif
144
145 /* Can't define this in hosts/foo.h, because (e.g. in gprof) the hosts file
146 is included, then obstack.h, which thinks if offsetof is defined, it
147 doesn't need to include stddef.h. */
148 /* Define offsetof for those systems which lack it */
149
150 #if !defined (offsetof)
151 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
152 #endif
153
154 /* We keep a cache of archive filepointers to archive elements to
155 speed up searching the archive by filepos. We only add an entry to
156 the cache when we actually read one. We also don't sort the cache;
157 it's generally short enough to search linearly.
158 Note that the pointers here point to the front of the ar_hdr, not
159 to the front of the contents!
160 */
161 struct ar_cache {
162 file_ptr ptr;
163 bfd* arelt;
164 struct ar_cache *next;
165 };
166
167 #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
168 #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
169
170 #define arch_eltdata(bfd) ((struct areltdata *)((bfd)->arelt_data))
171 #define arch_hdr(bfd) ((struct ar_hdr *)arch_eltdata(bfd)->arch_header)
172
173 static char *get_extended_arelt_filename PARAMS ((bfd *arch,
174 const char *name));
175 static boolean do_slurp_bsd_armap PARAMS ((bfd *abfd));
176 static boolean do_slurp_coff_armap PARAMS ((bfd *abfd));
177 static const char *normalize PARAMS ((const char *file));
178 static boolean bfd_construct_extended_name_table PARAMS ((bfd *abfd,
179 char **tabloc,
180 unsigned int *));
181 static struct areltdata *bfd_ar_hdr_from_filesystem PARAMS ((bfd *abfd,
182 const char *));
183 static boolean compute_and_write_armap PARAMS ((bfd *arch,
184 unsigned int elength));
185 static boolean bsd_update_armap_timestamp PARAMS ((bfd *arch));
186 \f
187 boolean
188 _bfd_generic_mkarchive (abfd)
189 bfd *abfd;
190 {
191 abfd->tdata.aout_ar_data = ((struct artdata *)
192 bfd_zalloc (abfd, sizeof (struct artdata)));
193
194 if (bfd_ardata (abfd) == NULL)
195 {
196 bfd_error = no_memory;
197 return false;
198 }
199
200 bfd_ardata (abfd)->cache = NULL;
201 bfd_ardata (abfd)->archive_head = NULL;
202 bfd_ardata (abfd)->symdefs = NULL;
203 bfd_ardata (abfd)->extended_names = NULL;
204 bfd_ardata (abfd)->tdata = NULL;
205
206 return true;
207 }
208
209 /*
210 FUNCTION
211 bfd_get_next_mapent
212
213 SYNOPSIS
214 symindex bfd_get_next_mapent(bfd *abfd, symindex previous, carsym **sym);
215
216 DESCRIPTION
217 Step through archive @var{abfd}'s symbol table (if it
218 has one). Successively update @var{sym} with the next symbol's
219 information, returning that symbol's (internal) index into the
220 symbol table.
221
222 Supply <<BFD_NO_MORE_SYMBOLS>> as the @var{previous} entry to get
223 the first one; returns <<BFD_NO_MORE_SYMBOLS>> when you've already
224 got the last one.
225
226 A <<carsym>> is a canonical archive symbol. The only
227 user-visible element is its name, a null-terminated string.
228 */
229
230 symindex
231 bfd_get_next_mapent (abfd, prev, entry)
232 bfd *abfd;
233 symindex prev;
234 carsym **entry;
235 {
236 if (!bfd_has_map (abfd)) {
237 bfd_error = invalid_operation;
238 return BFD_NO_MORE_SYMBOLS;
239 }
240
241 if (prev == BFD_NO_MORE_SYMBOLS) prev = 0;
242 else if (++prev >= bfd_ardata (abfd)->symdef_count)
243 return BFD_NO_MORE_SYMBOLS;
244
245 *entry = (bfd_ardata (abfd)->symdefs + prev);
246 return prev;
247 }
248
249 /* To be called by backends only */
250
251 bfd *
252 _bfd_create_empty_archive_element_shell (obfd)
253 bfd *obfd;
254 {
255 bfd *nbfd;
256
257 nbfd = new_bfd_contained_in(obfd);
258 if (nbfd == NULL)
259 {
260 bfd_error = no_memory;
261 return NULL;
262 }
263 return nbfd;
264 }
265
266 /*
267 FUNCTION
268 bfd_set_archive_head
269
270 SYNOPSIS
271 boolean bfd_set_archive_head(bfd *output, bfd *new_head);
272
273 DESCRIPTION
274 Set the head of the chain of
275 BFDs contained in the archive @var{output} to @var{new_head}.
276 */
277
278 boolean
279 bfd_set_archive_head (output_archive, new_head)
280 bfd *output_archive;
281 bfd *new_head;
282 {
283
284 output_archive->archive_head = new_head;
285 return true;
286 }
287
288 bfd *
289 look_for_bfd_in_cache (arch_bfd, filepos)
290 bfd *arch_bfd;
291 file_ptr filepos;
292 {
293 struct ar_cache *current;
294
295 for (current = bfd_ardata (arch_bfd)->cache; current != NULL;
296 current = current->next)
297 if (current->ptr == filepos) return current->arelt;
298
299 return NULL;
300 }
301
302 /* Kind of stupid to call cons for each one, but we don't do too many */
303 boolean
304 _bfd_add_bfd_to_archive_cache (arch_bfd, filepos, new_elt)
305 bfd *arch_bfd, *new_elt;
306 file_ptr filepos;
307 {
308 struct ar_cache *new_cache = (struct ar_cache *)
309 bfd_zalloc(arch_bfd, sizeof (struct ar_cache));
310
311 if (new_cache == NULL) {
312 bfd_error = no_memory;
313 return false;
314 }
315
316 new_cache->ptr = filepos;
317 new_cache->arelt = new_elt;
318 new_cache->next = (struct ar_cache *)NULL;
319 if (bfd_ardata (arch_bfd)->cache == NULL)
320 bfd_ardata (arch_bfd)->cache = new_cache;
321 else {
322 struct ar_cache *current = bfd_ardata (arch_bfd)->cache;
323
324 while (current->next != NULL)
325 current = current->next;
326 current->next = new_cache;
327 }
328
329 return true;
330 }
331 \f
332 /* The name begins with space. Hence the rest of the name is an index into
333 the string table. */
334
335 static char *
336 get_extended_arelt_filename (arch, name)
337 bfd *arch;
338 const char *name;
339 {
340 unsigned long index = 0;
341
342 /* Should extract string so that I can guarantee not to overflow into
343 the next region, but I'm too lazy. */
344 errno = 0;
345 /* Skip first char, which is '/' in SVR4 or ' ' in some other variants. */
346 index = strtol (name+1, NULL, 10);
347 if (errno != 0) {
348 bfd_error = malformed_archive;
349 return NULL;
350 }
351
352 return bfd_ardata (arch)->extended_names + index;
353 }
354
355 /* This functions reads an arch header and returns an areltdata pointer, or
356 NULL on error.
357
358 Presumes the file pointer is already in the right place (ie pointing
359 to the ar_hdr in the file). Moves the file pointer; on success it
360 should be pointing to the front of the file contents; on failure it
361 could have been moved arbitrarily.
362 */
363
364 struct areltdata *
365 _bfd_snarf_ar_hdr (abfd)
366 bfd *abfd;
367 {
368 #ifndef errno
369 extern int errno;
370 #endif
371
372 struct ar_hdr hdr;
373 char *hdrp = (char *) &hdr;
374 unsigned int parsed_size;
375 struct areltdata *ared;
376 char *filename = NULL;
377 unsigned int namelen = 0;
378 unsigned int allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
379 char *allocptr = 0;
380
381 if (bfd_read ((PTR)hdrp, 1, sizeof (struct ar_hdr), abfd)
382 != sizeof (struct ar_hdr)) {
383 bfd_error = no_more_archived_files;
384 return NULL;
385 }
386 if (strncmp ((hdr.ar_fmag), ARFMAG, 2)) {
387 bfd_error = malformed_archive;
388 return NULL;
389 }
390
391 errno = 0;
392 parsed_size = strtol (hdr.ar_size, NULL, 10);
393 if (errno != 0) {
394 bfd_error = malformed_archive;
395 return NULL;
396 }
397
398 /* extract the filename from the archive - there are two ways to
399 specify an extendend name table, either the first char of the
400 name is a space, or it's a slash. */
401 if ((hdr.ar_name[0] == '/'
402 || (hdr.ar_name[0] == ' '
403 && memchr (hdr.ar_name, '/', ar_maxnamelen(abfd)) == NULL))
404 && bfd_ardata (abfd)->extended_names != NULL) {
405 filename = get_extended_arelt_filename (abfd, hdr.ar_name);
406 if (filename == NULL) {
407 bfd_error = malformed_archive;
408 return NULL;
409 }
410 }
411 /* BSD4.4-style long filename.
412 Only implemented for reading, so far! */
413 else if (hdr.ar_name[0] == '#' && hdr.ar_name[1] == '1'
414 && hdr.ar_name[2] == '/' && isdigit(hdr.ar_name[3]))
415 {
416 /* BSD-4.4 extended name */
417 namelen = atoi (&hdr.ar_name[3]);
418 allocsize += namelen + 1;
419 parsed_size -= namelen;
420
421 allocptr = bfd_zalloc(abfd, allocsize);
422 if (allocptr == NULL) {
423 bfd_error = no_memory;
424 return NULL;
425 }
426 filename = allocptr
427 + (sizeof (struct areltdata) + sizeof (struct ar_hdr));
428 if (bfd_read (filename, 1, namelen, abfd) != namelen) {
429 bfd_error = no_more_archived_files;
430 return NULL;
431 }
432 filename[namelen] = '\0';
433 }
434 else
435 {
436 /* We judge the end of the name by looking for '/' or ' '.
437 Note: The SYSV format (terminated by '/') allows embedded
438 spaces, so only look for ' ' if we don't find '/'. */
439
440 namelen = 0;
441 while (hdr.ar_name[namelen] != '\0' &&
442 hdr.ar_name[namelen] != '/') {
443 namelen++;
444 if (namelen == (unsigned)ar_maxnamelen(abfd)) {
445 namelen = 0;
446 while (hdr.ar_name[namelen] != ' '
447 && namelen < (unsigned)ar_maxnamelen(abfd)) {
448 namelen++;
449 }
450 break;
451 }
452 }
453
454 allocsize += namelen + 1;
455 }
456
457 if (!allocptr) {
458 allocptr = bfd_zalloc(abfd, allocsize);
459 if (allocptr == NULL) {
460 bfd_error = no_memory;
461 return NULL;
462 }
463 }
464
465 ared = (struct areltdata *) allocptr;
466
467 ared->arch_header = allocptr + sizeof (struct areltdata);
468 memcpy ((char *) ared->arch_header, (char *) &hdr, sizeof (struct ar_hdr));
469 ared->parsed_size = parsed_size;
470
471 if (filename != NULL) ared->filename = filename;
472 else {
473 ared->filename = allocptr + (sizeof (struct areltdata) +
474 sizeof (struct ar_hdr));
475 if (namelen)
476 memcpy (ared->filename, hdr.ar_name, namelen);
477 ared->filename[namelen] = '\0';
478 }
479
480 return ared;
481 }
482 \f
483 /* This is an internal function; it's mainly used when indexing
484 through the archive symbol table, but also used to get the next
485 element, since it handles the bookkeeping so nicely for us.
486 */
487
488 bfd *
489 _bfd_get_elt_at_filepos (archive, filepos)
490 bfd *archive;
491 file_ptr filepos;
492 {
493 struct areltdata *new_areldata;
494 bfd *n_nfd;
495
496 n_nfd = look_for_bfd_in_cache (archive, filepos);
497 if (n_nfd)
498 return n_nfd;
499
500 if (0 > bfd_seek (archive, filepos, SEEK_SET))
501 {
502 bfd_error = system_call_error;
503 return NULL;
504 }
505
506 if ((new_areldata = _bfd_snarf_ar_hdr (archive)) == NULL)
507 return NULL;
508
509 n_nfd = _bfd_create_empty_archive_element_shell (archive);
510 if (n_nfd == NULL)
511 {
512 bfd_release (archive, (PTR)new_areldata);
513 return NULL;
514 }
515
516 n_nfd->origin = bfd_tell (archive);
517 n_nfd->arelt_data = (PTR) new_areldata;
518 n_nfd->filename = new_areldata->filename;
519
520 if (_bfd_add_bfd_to_archive_cache (archive, filepos, n_nfd))
521 return n_nfd;
522
523 /* huh? */
524 bfd_release (archive, (PTR)n_nfd);
525 bfd_release (archive, (PTR)new_areldata);
526 return NULL;
527 }
528
529 /*
530 FUNCTION
531 bfd_get_elt_at_index
532
533 SYNOPSIS
534 bfd *bfd_get_elt_at_index(bfd *archive, int index);
535
536 DESCRIPTION
537 Return the BFD which is referenced by the symbol in @var{archive}
538 indexed by @var{index}. @var{index} should have been returned by
539 <<bfd_get_next_mapent>> (q.v.).
540
541 */
542 bfd *
543 bfd_get_elt_at_index (abfd, index)
544 bfd *abfd;
545 int index;
546 {
547 bfd *result =
548 _bfd_get_elt_at_filepos
549 (abfd, (bfd_ardata (abfd)->symdefs + index)->file_offset);
550 return result;
551 }
552
553 /*
554 FUNCTION
555 bfd_openr_next_archived_file
556
557 SYNOPSIS
558 bfd *bfd_openr_next_archived_file(bfd *archive, bfd *previous);
559
560 DESCRIPTION
561 Provided a BFD, @var{archive}, containing an archive and NULL, open
562 an input BFD on the first contained element and returns that.
563 Subsequent calls should pass
564 the archive and the previous return value to return a created
565 BFD to the next contained element. NULL is returned when there
566 are no more.
567
568 */
569
570 bfd *
571 bfd_openr_next_archived_file (archive, last_file)
572 bfd *archive;
573 bfd *last_file;
574 {
575 if ((bfd_get_format (archive) != bfd_archive) ||
576 (archive->direction == write_direction))
577 {
578 bfd_error = invalid_operation;
579 return NULL;
580 }
581
582 return BFD_SEND (archive,
583 openr_next_archived_file,
584 (archive,
585 last_file));
586 }
587
588 bfd *
589 bfd_generic_openr_next_archived_file (archive, last_file)
590 bfd *archive;
591 bfd *last_file;
592 {
593 file_ptr filestart;
594
595 if (!last_file)
596 filestart = bfd_ardata (archive)->first_file_filepos;
597 else {
598 unsigned int size = arelt_size(last_file);
599 /* Pad to an even boundary...
600 Note that last_file->origin can be odd in the case of
601 BSD-4.4-style element with a long odd size. */
602 filestart = last_file->origin + size;
603 filestart += filestart % 2;
604 }
605
606 return _bfd_get_elt_at_filepos (archive, filestart);
607 }
608
609
610 bfd_target *
611 bfd_generic_archive_p (abfd)
612 bfd *abfd;
613 {
614 char armag[SARMAG+1];
615
616 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG)
617 {
618 bfd_error = wrong_format;
619 return NULL;
620 }
621
622 #ifdef GNU960
623 if (strncmp (armag, BFD_GNU960_ARMAG(abfd), SARMAG) != 0)
624 return 0;
625 #else
626 if (strncmp (armag, ARMAG, SARMAG) != 0 &&
627 strncmp (armag, ARMAGB, SARMAG) != 0)
628 return 0;
629 #endif
630
631 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
632 involves a cast, we can't do it as the left operand of assignment. */
633 abfd->tdata.aout_ar_data = ((struct artdata *)
634 bfd_zalloc (abfd, sizeof (struct artdata)));
635
636 if (bfd_ardata (abfd) == NULL)
637 {
638 bfd_error = no_memory;
639 return NULL;
640 }
641
642 bfd_ardata (abfd)->first_file_filepos = SARMAG;
643 bfd_ardata (abfd)->cache = NULL;
644 bfd_ardata (abfd)->archive_head = NULL;
645 bfd_ardata (abfd)->symdefs = NULL;
646 bfd_ardata (abfd)->extended_names = NULL;
647 bfd_ardata (abfd)->tdata = NULL;
648
649 if (! BFD_SEND (abfd, _bfd_slurp_armap, (abfd)))
650 {
651 bfd_release(abfd, bfd_ardata (abfd));
652 abfd->tdata.aout_ar_data = NULL;
653 return NULL;
654 }
655
656 if (! BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd)))
657 {
658 bfd_release(abfd, bfd_ardata (abfd));
659 abfd->tdata.aout_ar_data = NULL;
660 return NULL;
661 }
662
663 return abfd->xvec;
664 }
665
666 /* Returns false on error, true otherwise */
667 static boolean
668 do_slurp_bsd_armap (abfd)
669 bfd *abfd;
670 {
671 struct areltdata *mapdata;
672 unsigned int counter = 0;
673 int *raw_armap, *rbase;
674 struct artdata *ardata = bfd_ardata (abfd);
675 char *stringbase;
676 unsigned int parsed_size;
677
678 mapdata = _bfd_snarf_ar_hdr (abfd);
679 if (mapdata == NULL)
680 return false;
681 parsed_size = mapdata->parsed_size;
682 bfd_release (abfd, (PTR)mapdata); /* Don't need it any more. */
683
684 raw_armap = (int *) bfd_zalloc(abfd, parsed_size);
685 if (raw_armap == NULL) {
686 bfd_error = no_memory;
687 return false;
688 }
689
690 if (bfd_read ((PTR)raw_armap, 1, parsed_size, abfd) != parsed_size) {
691 bfd_error = malformed_archive;
692 byebye:
693 bfd_release (abfd, (PTR)raw_armap);
694 return false;
695 }
696
697 ardata->symdef_count = (bfd_h_get_32 (abfd, (bfd_byte *) raw_armap)
698 / sizeof (struct symdef));
699
700 if (ardata->symdef_count * sizeof (struct symdef)
701 > parsed_size - sizeof (*raw_armap)) {
702 /* Probably we're using the wrong byte ordering. */
703 bfd_error = wrong_format;
704 goto byebye;
705 }
706
707 ardata->cache = 0;
708 rbase = raw_armap+1;
709 ardata->symdefs = (carsym *) rbase;
710 stringbase = ((char *) (ardata->symdefs + ardata->symdef_count)) + 4;
711
712 for (;counter < ardata->symdef_count; counter++) {
713 struct symdef *sym = ((struct symdef *) rbase) + counter;
714 sym->s.name = (bfd_h_get_32 (abfd, (bfd_byte *) (&sym->s.string_offset))
715 + stringbase);
716 sym->file_offset = bfd_h_get_32 (abfd,
717 (bfd_byte *) (&(sym->file_offset)));
718 }
719
720 ardata->first_file_filepos = bfd_tell (abfd);
721 /* Pad to an even boundary if you have to */
722 ardata->first_file_filepos += (ardata-> first_file_filepos) %2;
723 /* FIXME, we should provide some way to free raw_ardata when
724 we are done using the strings from it. For now, it seems
725 to be allocated on an obstack anyway... */
726 bfd_has_map (abfd) = true;
727 return true;
728 }
729
730 /* Returns false on error, true otherwise */
731 static boolean
732 do_slurp_coff_armap (abfd)
733 bfd *abfd;
734 {
735 struct areltdata *mapdata;
736 int *raw_armap, *rawptr;
737 struct artdata *ardata = bfd_ardata (abfd);
738 char *stringbase;
739 unsigned int stringsize;
740 unsigned int parsed_size;
741 carsym *carsyms;
742 unsigned int nsymz; /* Number of symbols in armap. */
743
744 bfd_vma (*swap) PARAMS ((bfd_byte*));
745 char int_buf[sizeof(long)];
746 unsigned int carsym_size, ptrsize, i;
747
748 mapdata = _bfd_snarf_ar_hdr (abfd);
749 if (mapdata == NULL)
750 return false;
751 parsed_size = mapdata->parsed_size;
752 bfd_release (abfd, (PTR)mapdata); /* Don't need it any more. */
753
754 if (bfd_read ((PTR)int_buf, 1, 4, abfd) != 4) {
755 bfd_error = malformed_archive;
756 return false;
757 }
758 /* It seems that all numeric information in a coff archive is always
759 in big endian format, nomatter the host or target. */
760 swap = bfd_getb32;
761 nsymz = bfd_getb32((PTR)int_buf);
762 stringsize = parsed_size - (4 * nsymz) - 4;
763
764 #if 1
765 /* ... except that some archive formats are broken, and it may be our
766 fault - the i960 little endian coff sometimes has big and sometimes
767 little, because our tools changed. Here's a horrible hack to clean
768 up the crap. */
769
770 if (stringsize > 0xfffff) {
771 /* This looks dangerous, let's do it the other way around */
772 nsymz = bfd_getl32((PTR)int_buf);
773 stringsize = parsed_size - (4 * nsymz) - 4;
774 swap = bfd_getl32;
775 }
776 #endif
777
778 /* The coff armap must be read sequentially. So we construct a bsd-style
779 one in core all at once, for simplicity. */
780
781 carsym_size = (nsymz * sizeof (carsym));
782 ptrsize = (4 * nsymz);
783
784 ardata->symdefs = (carsym *) bfd_zalloc(abfd, carsym_size + stringsize + 1);
785 if (ardata->symdefs == NULL) {
786 bfd_error = no_memory;
787 return false;
788 }
789 carsyms = ardata->symdefs;
790 stringbase = ((char *) ardata->symdefs) + carsym_size;
791
792 /* Allocate and read in the raw offsets. */
793 raw_armap = (int *) bfd_alloc(abfd, ptrsize);
794 if (raw_armap == NULL) {
795 bfd_error = no_memory;
796 goto release_symdefs;
797 }
798 if (bfd_read ((PTR)raw_armap, 1, ptrsize, abfd) != ptrsize
799 || bfd_read ((PTR)stringbase, 1, stringsize, abfd) != stringsize) {
800 bfd_error = malformed_archive;
801 goto release_raw_armap;
802 }
803
804 /* OK, build the carsyms */
805 for (i = 0; i < nsymz; i++) {
806 rawptr = raw_armap + i;
807 carsyms->file_offset = swap((PTR)rawptr);
808 carsyms->name = stringbase;
809 stringbase += strlen (stringbase) + 1;
810 carsyms++;
811 }
812 *stringbase = 0;
813
814 ardata->symdef_count = nsymz;
815 ardata->first_file_filepos = bfd_tell (abfd);
816 /* Pad to an even boundary if you have to */
817 ardata->first_file_filepos += (ardata->first_file_filepos) %2;
818
819 bfd_has_map (abfd) = true;
820 bfd_release (abfd, (PTR)raw_armap);
821 return true;
822
823 release_raw_armap:
824 bfd_release (abfd, (PTR)raw_armap);
825 release_symdefs:
826 bfd_release (abfd, (PTR)(ardata)->symdefs);
827 return false;
828 }
829
830 /* This routine can handle either coff-style or bsd-style armaps.
831 Returns false on error, true otherwise */
832
833 boolean
834 bfd_slurp_armap (abfd)
835 bfd *abfd;
836 {
837 char nextname[17];
838 int i = bfd_read ((PTR)nextname, 1, 16, abfd);
839
840 if (i == 0)
841 return true;
842 if (i != 16)
843 return false;
844
845 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
846
847 if (!strncmp (nextname, "__.SYMDEF ", 16))
848 return do_slurp_bsd_armap (abfd);
849 else if (!strncmp (nextname, "/ ", 16))
850 return do_slurp_coff_armap (abfd);
851
852 bfd_has_map (abfd) = false;
853 return true;
854 }
855 \f
856 /* Returns false on error, true otherwise */
857 /* flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the
858 header is in a slightly different order and the map name is '/'.
859 This flavour is used by hp300hpux. */
860 boolean
861 bfd_slurp_bsd_armap_f2 (abfd)
862 bfd *abfd;
863 {
864 struct areltdata *mapdata;
865 char nextname[17];
866 unsigned int counter = 0;
867 int *raw_armap, *rbase;
868 struct artdata *ardata = bfd_ardata (abfd);
869 char *stringbase;
870 unsigned int stringsize;
871 int i = bfd_read ((PTR)nextname, 1, 16, abfd);
872
873 if (i == 0)
874 return true;
875 if (i != 16)
876 return false;
877
878 /* The archive has at least 16 bytes in it */
879 bfd_seek (abfd, -16L, SEEK_CUR);
880
881 if (!strncmp (nextname, "__.SYMDEF ", 16))
882 return do_slurp_bsd_armap (abfd);
883
884 if (strncmp (nextname, "/ ", 16))
885 {
886 bfd_has_map (abfd) = false;
887 return true;
888 }
889
890 mapdata = _bfd_snarf_ar_hdr (abfd);
891 if (mapdata == NULL)
892 return false;
893
894 raw_armap = (int *) bfd_zalloc(abfd,mapdata->parsed_size);
895 if (raw_armap == NULL)
896 {
897 bfd_error = no_memory;
898 byebye:
899 bfd_release (abfd, (PTR)mapdata);
900 return false;
901 }
902
903 if (bfd_read ((PTR)raw_armap, 1, mapdata->parsed_size, abfd) !=
904 mapdata->parsed_size)
905 {
906 bfd_error = malformed_archive;
907 byebyebye:
908 bfd_release (abfd, (PTR)raw_armap);
909 goto byebye;
910 }
911
912 ardata->symdef_count = bfd_h_get_16(abfd, (PTR)raw_armap);
913
914 if (ardata->symdef_count * sizeof (struct symdef)
915 > mapdata->parsed_size - sizeof (*raw_armap))
916 {
917 /* Probably we're using the wrong byte ordering. */
918 bfd_error = wrong_format;
919 goto byebyebye;
920 }
921
922 ardata->cache = 0;
923
924 stringsize = bfd_h_get_32(abfd, (PTR)(((char*)raw_armap)+2));
925 /* skip sym count and string sz */
926 rbase = (int*)(((char*)raw_armap) + 6);
927 stringbase = (char *) rbase;
928 ardata->symdefs = (carsym *)(((char*) rbase) + stringsize);
929
930 for (;counter < ardata->symdef_count; counter++)
931 {
932 struct symdef *sym = ((struct symdef *) ardata->symdefs) + counter;
933 sym->s.name = bfd_h_get_32(abfd, (PTR)(&(sym->s.string_offset))) + stringbase;
934 sym->file_offset = bfd_h_get_32(abfd, (PTR)( &(sym->file_offset)));
935 }
936
937 ardata->first_file_filepos = bfd_tell (abfd);
938 /* Pad to an even boundary if you have to */
939 ardata->first_file_filepos += (ardata-> first_file_filepos) %2;
940 /* FIXME, we should provide some way to free raw_ardata when
941 we are done using the strings from it. For now, it seems
942 to be allocated on an obstack anyway... */
943 bfd_has_map (abfd) = true;
944 return true;
945 }
946 \f
947 /** Extended name table.
948
949 Normally archives support only 14-character filenames.
950
951 Intel has extended the format: longer names are stored in a special
952 element (the first in the archive, or second if there is an armap);
953 the name in the ar_hdr is replaced by <space><index into filename
954 element>. Index is the P.R. of an int (decimal). Data General have
955 extended the format by using the prefix // for the special element */
956
957 /* Returns false on error, true otherwise */
958 boolean
959 _bfd_slurp_extended_name_table (abfd)
960 bfd *abfd;
961 {
962 char nextname[17];
963 struct areltdata *namedata;
964
965 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
966 we probably don't want to return true. */
967 if (bfd_read ((PTR)nextname, 1, 16, abfd) == 16) {
968
969 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
970
971 if (strncmp (nextname, "ARFILENAMES/ ", 16) != 0 &&
972 strncmp (nextname, "// ", 16) != 0)
973 {
974 bfd_ardata (abfd)->extended_names = NULL;
975 return true;
976 }
977
978 namedata = _bfd_snarf_ar_hdr (abfd);
979 if (namedata == NULL)
980 return false;
981
982 bfd_ardata (abfd)->extended_names = bfd_zalloc(abfd,namedata->parsed_size);
983 if (bfd_ardata (abfd)->extended_names == NULL) {
984 bfd_error = no_memory;
985 byebye:
986 bfd_release (abfd, (PTR)namedata);
987 return false;
988 }
989
990 if (bfd_read ((PTR)bfd_ardata (abfd)->extended_names, 1,
991 namedata->parsed_size, abfd) != namedata->parsed_size) {
992 bfd_error = malformed_archive;
993 bfd_release (abfd, (PTR)(bfd_ardata (abfd)->extended_names));
994 bfd_ardata (abfd)->extended_names = NULL;
995 goto byebye;
996 }
997
998 /* Since the archive is supposed to be printable if it contains
999 text, the entries in the list are newline-padded, not null
1000 padded. In SVR4-style archives, the names also have a
1001 trailing '/'. We'll fix both problems here.. */
1002 {
1003 char *temp = bfd_ardata (abfd)->extended_names;
1004 char *limit = temp + namedata->parsed_size;
1005 for (; temp < limit; ++temp)
1006 if (*temp == '\n')
1007 temp[temp[-1] == '/' ? -1 : 0] = '\0';
1008 }
1009
1010 /* Pad to an even boundary if you have to */
1011 bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd);
1012 bfd_ardata (abfd)->first_file_filepos +=
1013 (bfd_ardata (abfd)->first_file_filepos) %2;
1014
1015 /* FIXME, we can't release namedata here because it was allocated
1016 below extended_names on the obstack... */
1017 /* bfd_release (abfd, namedata); */
1018 }
1019 return true;
1020 }
1021
1022 #ifdef VMS
1023
1024 /* Return a copy of the stuff in the filename between any :]> and a
1025 semicolon */
1026 static const char *
1027 normalize (file)
1028 const char *file;
1029 {
1030 CONST char *first;
1031 CONST char *last;
1032 char *copy;
1033
1034 first = file + strlen(file)-1;
1035 last = first+1;
1036
1037 while (first != file)
1038 {
1039 if (*first == ';')
1040 last = first;
1041 if (*first == ':' || *first == ']' ||*first == '>')
1042 {
1043 first++;
1044 break;
1045 }
1046 first --;
1047 }
1048
1049
1050 copy = bfd_xmalloc(last - first + 1);
1051 memcpy(copy, first, last-first);
1052 copy[last-first] = 0;
1053
1054 return copy;
1055 }
1056
1057 #else
1058 static const char *
1059 normalize (file)
1060 const char *file;
1061 {
1062 CONST char * filename = strrchr(file, '/');
1063
1064 if (filename != (char *)NULL) {
1065 filename ++;
1066 }
1067 else {
1068 filename = file;
1069 }
1070 return filename;
1071 }
1072 #endif
1073 /* Follows archive_head and produces an extended name table if necessary.
1074 Returns (in tabloc) a pointer to an extended name table, and in tablen
1075 the length of the table. If it makes an entry it clobbers the filename
1076 so that the element may be written without further massage.
1077 Returns true if it ran successfully, false if something went wrong.
1078 A successful return may still involve a zero-length tablen!
1079 */
1080 static boolean
1081 bfd_construct_extended_name_table (abfd, tabloc, tablen)
1082 bfd *abfd;
1083 char **tabloc;
1084 unsigned int *tablen;
1085 {
1086 unsigned int maxname = abfd->xvec->ar_max_namelen;
1087 unsigned int total_namelen = 0;
1088 bfd *current;
1089 char *strptr;
1090
1091 *tablen = 0;
1092
1093 /* Figure out how long the table should be */
1094 for (current = abfd->archive_head; current != NULL; current = current->next){
1095 unsigned int thislen = strlen (normalize(current->filename));
1096 if (thislen > maxname) total_namelen += thislen + 1; /* leave room for \n */
1097 }
1098
1099 if (total_namelen == 0) return true;
1100
1101 *tabloc = bfd_zalloc (abfd,total_namelen);
1102 if (*tabloc == NULL) {
1103 bfd_error = no_memory;
1104 return false;
1105 }
1106
1107 *tablen = total_namelen;
1108 strptr = *tabloc;
1109
1110 for (current = abfd->archive_head; current != NULL; current =
1111 current->next) {
1112 CONST char *normal =normalize( current->filename);
1113 unsigned int thislen = strlen (normal);
1114 if (thislen > maxname) {
1115 /* Works for now; may need to be re-engineered if we encounter an oddball
1116 archive format and want to generalise this hack. */
1117 struct ar_hdr *hdr = arch_hdr(current);
1118 strcpy (strptr, normal);
1119 strptr[thislen] = '\n';
1120 hdr->ar_name[0] = ' ';
1121 /* We know there will always be enough room (one of the few cases
1122 where you may safely use sprintf). */
1123 sprintf ((hdr->ar_name) + 1, "%-d", (unsigned) (strptr - *tabloc));
1124 /* Kinda Kludgy. We should just use the returned value of sprintf
1125 but not all implementations get this right */
1126 {
1127 char *temp = hdr->ar_name +2;
1128 for (; temp < hdr->ar_name + maxname; temp++)
1129 if (*temp == '\0') *temp = ' ';
1130 }
1131 strptr += thislen + 1;
1132 }
1133 }
1134
1135 return true;
1136 }
1137 \f
1138 /** A couple of functions for creating ar_hdrs */
1139
1140 /* Takes a filename, returns an arelt_data for it, or NULL if it can't make one.
1141 The filename must refer to a filename in the filesystem.
1142 The filename field of the ar_hdr will NOT be initialized
1143 */
1144
1145 static struct areltdata *
1146 bfd_ar_hdr_from_filesystem (abfd,filename)
1147 bfd* abfd;
1148 const char *filename;
1149 {
1150 struct stat status;
1151 struct areltdata *ared;
1152 struct ar_hdr *hdr;
1153 char *temp, *temp1;
1154
1155 if (stat (filename, &status) != 0) {
1156 bfd_error = system_call_error;
1157 return NULL;
1158 }
1159
1160 ared = (struct areltdata *) bfd_zalloc(abfd, sizeof (struct ar_hdr) +
1161 sizeof (struct areltdata));
1162 if (ared == NULL) {
1163 bfd_error = no_memory;
1164 return NULL;
1165 }
1166 hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
1167
1168 /* ar headers are space padded, not null padded! */
1169 memset (hdr, ' ', sizeof (struct ar_hdr));
1170
1171 strncpy (hdr->ar_fmag, ARFMAG, 2);
1172
1173 /* Goddamned sprintf doesn't permit MAXIMUM field lengths */
1174 sprintf ((hdr->ar_date), "%-12ld", status.st_mtime);
1175 sprintf ((hdr->ar_uid), "%d", status.st_uid);
1176 sprintf ((hdr->ar_gid), "%d", status.st_gid);
1177 sprintf ((hdr->ar_mode), "%-8o", (unsigned) status.st_mode);
1178 sprintf ((hdr->ar_size), "%-10ld", status.st_size);
1179 /* Correct for a lossage in sprintf whereby it null-terminates. I cannot
1180 understand how these C losers could design such a ramshackle bunch of
1181 IO operations */
1182 temp = (char *) hdr;
1183 temp1 = temp + sizeof (struct ar_hdr) - 2;
1184 for (; temp < temp1; temp++) {
1185 if (*temp == '\0') *temp = ' ';
1186 }
1187 strncpy (hdr->ar_fmag, ARFMAG, 2);
1188 ared->parsed_size = status.st_size;
1189 ared->arch_header = (char *) hdr;
1190
1191 return ared;
1192 }
1193
1194 /* This is magic required by the "ar" program. Since it's
1195 undocumented, it's undocumented. You may think that it would
1196 take a strong stomach to write this, and it does, but it takes
1197 even a stronger stomach to try to code around such a thing!
1198 */
1199
1200 struct ar_hdr *
1201 bfd_special_undocumented_glue (abfd, filename)
1202 bfd *abfd;
1203 char *filename;
1204 {
1205 struct areltdata *ar_elt = bfd_ar_hdr_from_filesystem (abfd, filename);
1206 if (ar_elt == NULL)
1207 return NULL;
1208 return (struct ar_hdr *) ar_elt->arch_header;
1209 }
1210
1211
1212 /* Analogous to stat call */
1213 int
1214 bfd_generic_stat_arch_elt (abfd, buf)
1215 bfd *abfd;
1216 struct stat *buf;
1217 {
1218 struct ar_hdr *hdr;
1219 char *aloser;
1220
1221 if (abfd->arelt_data == NULL) {
1222 bfd_error = invalid_operation;
1223 return -1;
1224 }
1225
1226 hdr = arch_hdr (abfd);
1227
1228 #define foo(arelt, stelt, size) \
1229 buf->stelt = strtol (hdr->arelt, &aloser, size); \
1230 if (aloser == hdr->arelt) return -1;
1231
1232 foo (ar_date, st_mtime, 10);
1233 foo (ar_uid, st_uid, 10);
1234 foo (ar_gid, st_gid, 10);
1235 foo (ar_mode, st_mode, 8);
1236
1237 buf->st_size = arch_eltdata (abfd)->parsed_size;
1238
1239 return 0;
1240 }
1241
1242 void
1243 bfd_dont_truncate_arname (abfd, pathname, arhdr)
1244 bfd *abfd;
1245 CONST char *pathname;
1246 char *arhdr;
1247 {
1248 /* FIXME: This interacts unpleasantly with ar's quick-append option.
1249 Fortunately ic960 users will never use that option. Fixing this
1250 is very hard; fortunately I know how to do it and will do so once
1251 intel's release is out the door. */
1252
1253 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1254 int length;
1255 CONST char *filename = normalize(pathname);
1256 int maxlen = ar_maxnamelen (abfd);
1257
1258 length = strlen (filename);
1259
1260 if (length <= maxlen)
1261 memcpy (hdr->ar_name, filename, length);
1262
1263 if (length < maxlen) (hdr->ar_name)[length] = ar_padchar (abfd);
1264 return;
1265
1266 }
1267
1268 void
1269 bfd_bsd_truncate_arname (abfd, pathname, arhdr)
1270 bfd *abfd;
1271 CONST char *pathname;
1272 char *arhdr;
1273 {
1274 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1275 int length;
1276 CONST char *filename = strrchr (pathname, '/');
1277 int maxlen = ar_maxnamelen (abfd);
1278
1279
1280 if (filename == NULL)
1281 filename = pathname;
1282 else
1283 ++filename;
1284
1285 length = strlen (filename);
1286
1287 if (length <= maxlen)
1288 memcpy (hdr->ar_name, filename, length);
1289 else {
1290 /* pathname: meet procrustes */
1291 memcpy (hdr->ar_name, filename, maxlen);
1292 length = maxlen;
1293 }
1294
1295 if (length < maxlen)
1296 (hdr->ar_name)[length] = ar_padchar (abfd);
1297 }
1298
1299 /* Store name into ar header. Truncates the name to fit.
1300 1> strip pathname to be just the basename.
1301 2> if it's short enuf to fit, stuff it in.
1302 3> If it doesn't end with .o, truncate it to fit
1303 4> truncate it before the .o, append .o, stuff THAT in.
1304 */
1305
1306 /* This is what gnu ar does. It's better but incompatible with the bsd ar. */
1307 void
1308 bfd_gnu_truncate_arname (abfd, pathname, arhdr)
1309 bfd *abfd;
1310 CONST char *pathname;
1311 char *arhdr;
1312 {
1313 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1314 int length;
1315 CONST char *filename = strrchr (pathname, '/');
1316 int maxlen = ar_maxnamelen (abfd);
1317
1318 if (filename == NULL)
1319 filename = pathname;
1320 else
1321 ++filename;
1322
1323 length = strlen (filename);
1324
1325 if (length <= maxlen)
1326 memcpy (hdr->ar_name, filename, length);
1327 else { /* pathname: meet procrustes */
1328 memcpy (hdr->ar_name, filename, maxlen);
1329 if ((filename[length - 2] == '.') && (filename[length - 1] == 'o')) {
1330 hdr->ar_name[maxlen - 2] = '.';
1331 hdr->ar_name[maxlen - 1] = 'o';
1332 }
1333 length = maxlen;
1334 }
1335
1336 if (length < 16)
1337 (hdr->ar_name)[length] = ar_padchar (abfd);
1338 }
1339 \f
1340
1341 /* The BFD is open for write and has its format set to bfd_archive */
1342 boolean
1343 _bfd_write_archive_contents (arch)
1344 bfd *arch;
1345 {
1346 bfd *current;
1347 char *etable = NULL;
1348 unsigned int elength = 0;
1349 boolean makemap = bfd_has_map (arch);
1350 boolean hasobjects = false; /* if no .o's, don't bother to make a map */
1351 unsigned int i;
1352 int tries;
1353
1354 /* Verify the viability of all entries; if any of them live in the
1355 filesystem (as opposed to living in an archive open for input)
1356 then construct a fresh ar_hdr for them.
1357 */
1358 for (current = arch->archive_head; current; current = current->next) {
1359 if (bfd_write_p (current)) {
1360 bfd_error = invalid_operation;
1361 return false;
1362 }
1363 if (!current->arelt_data) {
1364 current->arelt_data =
1365 (PTR) bfd_ar_hdr_from_filesystem (arch, current->filename);
1366 if (!current->arelt_data) return false;
1367
1368 /* Put in the file name */
1369
1370 BFD_SEND (arch, _bfd_truncate_arname,(arch,
1371 current->filename,
1372 (char *) arch_hdr(current)));
1373
1374
1375 }
1376
1377 if (makemap) { /* don't bother if we won't make a map! */
1378 if ((bfd_check_format (current, bfd_object))
1379 #if 0 /* FIXME -- these are not set correctly */
1380 && ((bfd_get_file_flags (current) & HAS_SYMS))
1381 #endif
1382 )
1383 hasobjects = true;
1384 }
1385 }
1386
1387 if (!bfd_construct_extended_name_table (arch, &etable, &elength))
1388 return false;
1389
1390 bfd_seek (arch, (file_ptr) 0, SEEK_SET);
1391 #ifdef GNU960
1392 bfd_write (BFD_GNU960_ARMAG(arch), 1, SARMAG, arch);
1393 #else
1394 bfd_write (ARMAG, 1, SARMAG, arch);
1395 #endif
1396
1397 if (makemap && hasobjects) {
1398
1399 if (compute_and_write_armap (arch, elength) != true) {
1400 return false;
1401 }
1402 }
1403
1404 if (elength != 0) {
1405 struct ar_hdr hdr;
1406
1407 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1408 sprintf (&(hdr.ar_name[0]), "ARFILENAMES/");
1409 sprintf (&(hdr.ar_size[0]), "%-10d", (int) elength);
1410 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1411 for (i = 0; i < sizeof (struct ar_hdr); i++)
1412 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
1413 bfd_write ((char *)&hdr, 1, sizeof (struct ar_hdr), arch);
1414 bfd_write (etable, 1, elength, arch);
1415 if ((elength % 2) == 1) bfd_write ("\n", 1, 1, arch);
1416
1417 }
1418
1419 for (current = arch->archive_head; current; current = current->next) {
1420 char buffer[DEFAULT_BUFFERSIZE];
1421 unsigned int remaining = arelt_size (current);
1422 struct ar_hdr *hdr = arch_hdr(current);
1423 /* write ar header */
1424
1425 if (bfd_write ((char *)hdr, 1, sizeof(*hdr), arch) != sizeof(*hdr)) {
1426 syserr:
1427 bfd_error = system_call_error;
1428 return false;
1429 }
1430 if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0) goto syserr;
1431 while (remaining)
1432 {
1433 unsigned int amt = DEFAULT_BUFFERSIZE;
1434 if (amt > remaining) {
1435 amt = remaining;
1436 }
1437 errno = 0;
1438 if (bfd_read (buffer, amt, 1, current) != amt) {
1439 if (errno) goto syserr;
1440 /* Looks like a truncated archive. */
1441 bfd_error = malformed_archive;
1442 return false;
1443 }
1444 if (bfd_write (buffer, amt, 1, arch) != amt) goto syserr;
1445 remaining -= amt;
1446 }
1447 if ((arelt_size (current) % 2) == 1) bfd_write ("\n", 1, 1, arch);
1448 }
1449
1450 /* Verify the timestamp in the archive file. If it would
1451 not be accepted by the linker, rewrite it until it would be.
1452 If anything odd happens, break out and just return.
1453 (The Berkeley linker checks the timestamp and refuses to read the
1454 table-of-contents if it is >60 seconds less than the file's
1455 modified-time. That painful hack requires this painful hack. */
1456
1457 tries = 1;
1458 do {
1459 /* FIXME! This kludge is to avoid adding a member to the xvec,
1460 while generating a small patch for Adobe. FIXME! The
1461 update_armap_timestamp function call should be in the xvec,
1462 thus:
1463
1464 if (bfd_update_armap_timestamp (arch) == true) break;
1465 ^
1466
1467 Instead, we check whether in a BSD archive, and call directly. */
1468
1469 if (arch->xvec->write_armap != bsd_write_armap)
1470 break;
1471 if (bsd_update_armap_timestamp(arch) == true) /* FIXME!!! Vector it */
1472 break;
1473 if (tries > 0)
1474 fprintf (stderr,
1475 "Warning: writing archive was slow: rewriting timestamp\n");
1476 } while (++tries < 6 );
1477
1478 return true;
1479 }
1480 \f
1481 /* Note that the namidx for the first symbol is 0 */
1482
1483 static boolean
1484 compute_and_write_armap (arch, elength)
1485 bfd *arch;
1486 unsigned int elength;
1487 {
1488 bfd *current;
1489 file_ptr elt_no = 0;
1490 struct orl *map;
1491 int orl_max = 15000; /* fine initial default */
1492 int orl_count = 0;
1493 int stridx = 0; /* string index */
1494
1495 /* Dunno if this is the best place for this info... */
1496 if (elength != 0) elength += sizeof (struct ar_hdr);
1497 elength += elength %2 ;
1498
1499 map = (struct orl *) bfd_zalloc (arch,orl_max * sizeof (struct orl));
1500 if (map == NULL) {
1501 bfd_error = no_memory;
1502 return false;
1503 }
1504
1505 /* Drop all the files called __.SYMDEF, we're going to make our
1506 own */
1507 while (arch->archive_head &&
1508 strcmp(arch->archive_head->filename,"__.SYMDEF") == 0)
1509 {
1510 arch->archive_head = arch->archive_head->next;
1511 }
1512 /* Map over each element */
1513 for (current = arch->archive_head;
1514 current != (bfd *)NULL;
1515 current = current->next, elt_no++)
1516 {
1517 if ((bfd_check_format (current, bfd_object) == true)
1518 && ((bfd_get_file_flags (current) & HAS_SYMS))) {
1519 asymbol **syms;
1520 unsigned int storage;
1521 unsigned int symcount;
1522 unsigned int src_count;
1523
1524 storage = get_symtab_upper_bound (current);
1525 if (storage != 0) {
1526
1527 syms = (asymbol **) bfd_zalloc (arch,storage);
1528 if (syms == NULL) {
1529 bfd_error = no_memory; /* FIXME -- memory leak */
1530 return false;
1531 }
1532 symcount = bfd_canonicalize_symtab (current, syms);
1533
1534
1535 /* Now map over all the symbols, picking out the ones we want */
1536 for (src_count = 0; src_count <symcount; src_count++) {
1537 flagword flags =
1538 (syms[src_count])->flags;
1539 asection *sec =
1540 syms[src_count]->section;
1541
1542 if ((flags & BSF_GLOBAL ||
1543 flags & BSF_WEAK ||
1544 flags & BSF_INDIRECT ||
1545 bfd_is_com_section (sec))
1546 && (sec != &bfd_und_section)) {
1547
1548 /* This symbol will go into the archive header */
1549 if (orl_count == orl_max)
1550 {
1551 orl_max *= 2;
1552 map = (struct orl *) bfd_realloc (arch, (char *) map,
1553 orl_max * sizeof (struct orl));
1554 }
1555
1556 (map[orl_count]).name = (char **) &((syms[src_count])->name);
1557 (map[orl_count]).pos = (file_ptr) current;
1558 (map[orl_count]).namidx = stridx;
1559
1560 stridx += strlen ((syms[src_count])->name) + 1;
1561 ++orl_count;
1562 }
1563 }
1564 }
1565 }
1566 }
1567 /* OK, now we have collected all the data, let's write them out */
1568 if (!BFD_SEND (arch, write_armap,
1569 (arch, elength, map, orl_count, stridx))) {
1570
1571 return false;
1572 }
1573
1574
1575 return true;
1576 }
1577
1578 boolean
1579 bsd_write_armap (arch, elength, map, orl_count, stridx)
1580 bfd *arch;
1581 unsigned int elength;
1582 struct orl *map;
1583 unsigned int orl_count;
1584 int stridx;
1585 {
1586 int padit = stridx & 1;
1587 unsigned int ranlibsize = orl_count * sizeof (struct ranlib);
1588 unsigned int stringsize = stridx + padit;
1589 /* Include 8 bytes to store ranlibsize and stringsize in output. */
1590 unsigned int mapsize = ranlibsize + stringsize + 8;
1591 file_ptr firstreal;
1592 bfd *current = arch->archive_head;
1593 bfd *last_elt = current; /* last element arch seen */
1594 int temp;
1595 int count;
1596 struct ar_hdr hdr;
1597 struct stat statbuf;
1598 unsigned int i;
1599
1600 firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
1601
1602 stat (arch->filename, &statbuf);
1603 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1604 sprintf (hdr.ar_name, RANLIBMAG);
1605 /* Remember the timestamp, to keep it holy. But fudge it a little. */
1606 bfd_ardata(arch)->armap_timestamp = statbuf.st_mtime + ARMAP_TIME_OFFSET;
1607 bfd_ardata(arch)->armap_datepos = SARMAG +
1608 offsetof(struct ar_hdr, ar_date[0]);
1609 sprintf (hdr.ar_date, "%ld", bfd_ardata(arch)->armap_timestamp);
1610 sprintf (hdr.ar_uid, "%d", getuid());
1611 sprintf (hdr.ar_gid, "%d", getgid());
1612 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
1613 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1614 for (i = 0; i < sizeof (struct ar_hdr); i++)
1615 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
1616 bfd_write ((char *)&hdr, 1, sizeof (struct ar_hdr), arch);
1617 bfd_h_put_32(arch, (bfd_vma) ranlibsize, (PTR)&temp);
1618 bfd_write (&temp, 1, sizeof (temp), arch);
1619
1620 for (count = 0; count < orl_count; count++) {
1621 struct symdef outs;
1622 struct symdef *outp = &outs;
1623
1624 if (((bfd *)(map[count]).pos) != last_elt) {
1625 do {
1626 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
1627 firstreal += firstreal % 2;
1628 current = current->next;
1629 } while (current != (bfd *)(map[count]).pos);
1630 } /* if new archive element */
1631
1632 last_elt = current;
1633 bfd_h_put_32(arch, ((map[count]).namidx),(PTR) &outs.s.string_offset);
1634 bfd_h_put_32(arch, firstreal,(PTR) &outs.file_offset);
1635 bfd_write ((char *)outp, 1, sizeof (outs), arch);
1636 }
1637
1638 /* now write the strings themselves */
1639 bfd_h_put_32(arch, stringsize, (PTR)&temp);
1640 bfd_write ((PTR)&temp, 1, sizeof (temp), arch);
1641 for (count = 0; count < orl_count; count++)
1642 bfd_write (*((map[count]).name), 1, strlen (*((map[count]).name))+1, arch);
1643
1644 /* The spec sez this should be a newline. But in order to be
1645 bug-compatible for sun's ar we use a null. */
1646 if (padit)
1647 bfd_write("",1,1,arch);
1648
1649 return true;
1650 }
1651
1652
1653 /* At the end of archive file handling, update the timestamp in the
1654 file, so the linker will accept it.
1655
1656 Return true if the timestamp was OK, or an unusual problem happened.
1657 Return false if we updated the timestamp. */
1658
1659 static boolean
1660 bsd_update_armap_timestamp (arch)
1661 bfd *arch;
1662 {
1663 struct stat archstat;
1664 struct ar_hdr hdr;
1665 int i;
1666
1667 /* Flush writes, get last-write timestamp from file, and compare it
1668 to the timestamp IN the file. */
1669 bfd_flush (arch);
1670 if (bfd_stat (arch, &archstat) == -1) {
1671 perror ("Reading archive file mod timestamp");
1672 return true; /* Can't read mod time for some reason */
1673 }
1674 if (archstat.st_mtime <= bfd_ardata(arch)->armap_timestamp)
1675 return true; /* OK by the linker's rules */
1676
1677 /* Update the timestamp. */
1678 bfd_ardata(arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET;
1679
1680 /* Prepare an ASCII version suitable for writing. */
1681 memset (hdr.ar_date, 0, sizeof (hdr.ar_date));
1682 sprintf (hdr.ar_date, "%ld", bfd_ardata(arch)->armap_timestamp);
1683 for (i = 0; i < sizeof (hdr.ar_date); i++)
1684 if (hdr.ar_date[i] == '\0')
1685 (hdr.ar_date)[i] = ' ';
1686
1687 /* Write it into the file. */
1688 bfd_seek (arch, bfd_ardata(arch)->armap_datepos, SEEK_SET);
1689 if (bfd_write (hdr.ar_date, sizeof(hdr.ar_date), 1, arch)
1690 != sizeof(hdr.ar_date)) {
1691 perror ("Writing updated armap timestamp");
1692 return true; /* Some error while writing */
1693 }
1694
1695 return false; /* We updated the timestamp successfully. */
1696 }
1697 \f
1698
1699 /* A coff armap looks like :
1700 lARMAG
1701 struct ar_hdr with name = '/'
1702 number of symbols
1703 offset of file for symbol 0
1704 offset of file for symbol 1
1705
1706 offset of file for symbol n-1
1707 symbol name 0
1708 symbol name 1
1709
1710 symbol name n-1
1711
1712 */
1713
1714 boolean
1715 coff_write_armap (arch, elength, map, symbol_count, stridx)
1716 bfd *arch;
1717 unsigned int elength;
1718 struct orl *map;
1719 unsigned int symbol_count;
1720 int stridx;
1721 {
1722 /* The size of the ranlib is the number of exported symbols in the
1723 archive * the number of bytes in a int, + an int for the count */
1724
1725 unsigned int ranlibsize = (symbol_count * 4) + 4;
1726 unsigned int stringsize = stridx;
1727 unsigned int mapsize = stringsize + ranlibsize;
1728 file_ptr archive_member_file_ptr;
1729 bfd *current = arch->archive_head;
1730 int count;
1731 struct ar_hdr hdr;
1732 unsigned int i;
1733 int padit = mapsize & 1;
1734
1735 if (padit) mapsize ++;
1736
1737 /* work out where the first object file will go in the archive */
1738 archive_member_file_ptr = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
1739
1740 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1741 hdr.ar_name[0] = '/';
1742 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
1743 sprintf (hdr.ar_date, "%ld", (long)time (NULL));
1744 /* This, at least, is what Intel coff sets the values to.: */
1745 sprintf ((hdr.ar_uid), "%d", 0);
1746 sprintf ((hdr.ar_gid), "%d", 0);
1747 sprintf ((hdr.ar_mode), "%-7o",(unsigned ) 0);
1748 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1749
1750 for (i = 0; i < sizeof (struct ar_hdr); i++)
1751 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
1752
1753 /* Write the ar header for this item and the number of symbols */
1754
1755
1756 bfd_write ((PTR)&hdr, 1, sizeof (struct ar_hdr), arch);
1757
1758 bfd_write_bigendian_4byte_int(arch, symbol_count);
1759
1760 /* Two passes, first write the file offsets for each symbol -
1761 remembering that each offset is on a two byte boundary. */
1762
1763 /* Write out the file offset for the file associated with each
1764 symbol, and remember to keep the offsets padded out. */
1765
1766 current = arch->archive_head;
1767 count = 0;
1768 while (current != (bfd *)NULL && count < symbol_count) {
1769 /* For each symbol which is used defined in this object, write out
1770 the object file's address in the archive */
1771
1772 while (((bfd *)(map[count]).pos) == current) {
1773 bfd_write_bigendian_4byte_int(arch, archive_member_file_ptr);
1774 count++;
1775 }
1776 /* Add size of this archive entry */
1777 archive_member_file_ptr += arelt_size (current) + sizeof (struct
1778 ar_hdr);
1779 /* remember aboout the even alignment */
1780 archive_member_file_ptr += archive_member_file_ptr % 2;
1781 current = current->next;
1782 }
1783
1784
1785
1786 /* now write the strings themselves */
1787 for (count = 0; count < symbol_count; count++) {
1788 bfd_write ((PTR)*((map[count]).name),
1789 1,
1790 strlen (*((map[count]).name))+1, arch);
1791
1792 }
1793 /* The spec sez this should be a newline. But in order to be
1794 bug-compatible for arc960 we use a null. */
1795 if (padit)
1796 bfd_write("",1,1,arch);
1797
1798 return true;
1799 }