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