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