747745aa6ab090684451af2cec309fe5631b46c3
[binutils-gdb.git] / bfd / archive.c
1 /* BFD back-end for archive files (libraries).
2 Copyright (C) 1990-1991 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 (eg 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
96 #include "bfd.h"
97 #include "sysdep.h"
98 #include "libbfd.h"
99 #include "aout/ar.h"
100 #include "aout/ranlib.h"
101 #include <errno.h>
102
103 #ifndef errno
104 extern int errno;
105 #endif
106
107 #ifdef GNU960
108 #define BFD_GNU960_ARMAG(abfd) (BFD_COFF_FILE_P((abfd)) ? ARMAG : ARMAGB)
109 #endif
110
111 /* We keep a cache of archive filepointers to archive elements to
112 speed up searching the archive by filepos. We only add an entry to
113 the cache when we actually read one. We also don't sort the cache;
114 it's generally short enough to search linearly.
115 Note that the pointers here point to the front of the ar_hdr, not
116 to the front of the contents!
117 */
118 struct ar_cache {
119 file_ptr ptr;
120 bfd* arelt;
121 struct ar_cache *next;
122 };
123
124 #define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)
125 #define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)
126
127 #define arch_hdr(bfd) ((struct ar_hdr *) \
128 (((struct areltdata *)((bfd)->arelt_data))->arch_header))
129 \f
130 boolean
131 _bfd_generic_mkarchive (abfd)
132 bfd *abfd;
133 {
134 abfd->tdata.aout_ar_data = (struct artdata *)bfd_zalloc(abfd, sizeof (struct artdata));
135
136 if (bfd_ardata (abfd) == NULL) {
137 bfd_error = no_memory;
138 return false;
139 }
140 bfd_ardata(abfd)->cache = 0;
141 return true;
142 }
143
144 /*
145 FUNCTION
146 bfd_get_next_mapent
147
148 SYNOPSIS
149 symindex bfd_get_next_mapent(bfd *, symindex previous, carsym ** sym);
150
151 DESCRIPTION
152 This function steps through an archive's symbol table (if it
153 has one). Successively updates <<sym>> with the next symbol's
154 information, returning that symbol's (internal) index into the
155 symbol table.
156
157 Supply BFD_NO_MORE_SYMBOLS as the <<previous>> entry to get
158 the first one; returns BFD_NO_MORE_SYMBOLS when you're already
159 got the last one.
160
161 A <<carsym>> is a canonical archive symbol. The only
162 user-visible element is its name, a null-terminated string.
163 */
164
165 symindex
166 DEFUN(bfd_get_next_mapent,(abfd, prev, entry),
167 bfd *abfd AND
168 symindex prev AND
169 carsym **entry)
170 {
171 if (!bfd_has_map (abfd)) {
172 bfd_error = invalid_operation;
173 return BFD_NO_MORE_SYMBOLS;
174 }
175
176 if (prev == BFD_NO_MORE_SYMBOLS) prev = 0;
177 else if (++prev >= bfd_ardata (abfd)->symdef_count)
178 return BFD_NO_MORE_SYMBOLS;
179
180 *entry = (bfd_ardata (abfd)->symdefs + prev);
181 return prev;
182 }
183
184 /* To be called by backends only */
185 bfd *
186 _bfd_create_empty_archive_element_shell (obfd)
187 bfd *obfd;
188 {
189 bfd *nbfd;
190
191 nbfd = new_bfd_contained_in(obfd);
192 if (nbfd == NULL) {
193 bfd_error = no_memory;
194 return NULL;
195 }
196 return nbfd;
197 }
198
199 /*
200 FUNCTION
201 bfd_set_archive_head
202
203 SYNOPSIS
204 boolean bfd_set_archive_head(bfd *output, bfd *new_head);
205
206 DESCRIPTION
207 Used whilst processing archives. Sets the head of the chain of
208 BFDs contained in an archive to @var{new_head}.
209 */
210
211 boolean
212 DEFUN(bfd_set_archive_head,(output_archive, new_head),
213 bfd *output_archive AND
214 bfd *new_head)
215 {
216
217 output_archive->archive_head = new_head;
218 return true;
219 }
220
221 bfd *
222 look_for_bfd_in_cache (arch_bfd, filepos)
223 bfd *arch_bfd;
224 file_ptr filepos;
225 {
226 struct ar_cache *current;
227
228 for (current = bfd_ardata (arch_bfd)->cache; current != NULL;
229 current = current->next)
230 if (current->ptr == filepos) return current->arelt;
231
232 return NULL;
233 }
234
235 /* Kind of stupid to call cons for each one, but we don't do too many */
236 boolean
237 add_bfd_to_cache (arch_bfd, filepos, new_elt)
238 bfd *arch_bfd, *new_elt;
239 file_ptr filepos;
240 {
241 struct ar_cache *new_cache = (struct ar_cache *)
242 bfd_zalloc(arch_bfd, sizeof (struct ar_cache));
243
244 if (new_cache == NULL) {
245 bfd_error = no_memory;
246 return false;
247 }
248
249 new_cache->ptr = filepos;
250 new_cache->arelt = new_elt;
251 new_cache->next = (struct ar_cache *)NULL;
252 if (bfd_ardata (arch_bfd)->cache == NULL)
253 bfd_ardata (arch_bfd)->cache = new_cache;
254 else {
255 struct ar_cache *current = bfd_ardata (arch_bfd)->cache;
256
257 for (; current->next != NULL; current = current->next);
258 current->next = new_cache;
259 }
260
261 return true;
262 }
263
264 \f
265
266 /* The name begins with space. Hence the rest of the name is an index into
267 the string table. */
268 char *
269 get_extended_arelt_filename (arch, name)
270 bfd *arch;
271 char *name;
272 {
273 unsigned long index = 0;
274
275 /* Should extract string so that I can guarantee not to overflow into
276 the next region, but I"m too lazy. */
277 errno = 0;
278 index = strtol (name, NULL, 10);
279 if (errno != 0) {
280 bfd_error = malformed_archive;
281 return NULL;
282 }
283
284 return bfd_ardata (arch)->extended_names + index;
285 }
286
287 /* This functions reads an arch header and returns an areltdata pointer, or
288 NULL on error.
289
290 Presumes the file pointer is already in the right place (ie pointing
291 to the ar_hdr in the file). Moves the file pointer; on success it
292 should be pointing to the front of the file contents; on failure it
293 could have been moved arbitrarily.
294 */
295
296 struct areltdata *
297 snarf_ar_hdr (abfd)
298 bfd *abfd;
299 {
300 #ifndef errno
301 extern int errno;
302 #endif
303
304 struct ar_hdr hdr;
305 char *hdrp = (char *) &hdr;
306 unsigned int parsed_size;
307 struct areltdata *ared;
308 char *filename = NULL;
309 unsigned int namelen = 0;
310 unsigned int allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);
311 char *allocptr;
312
313 if (bfd_read ((PTR)hdrp, 1, sizeof (struct ar_hdr), abfd)
314 != sizeof (struct ar_hdr)) {
315 bfd_error = no_more_archived_files;
316 return NULL;
317 }
318 if (strncmp ((hdr.ar_fmag), ARFMAG, 2)) {
319 bfd_error = malformed_archive;
320 return NULL;
321 }
322
323 errno = 0;
324 parsed_size = strtol (hdr.ar_size, NULL, 10);
325 if (errno != 0) {
326 bfd_error = malformed_archive;
327 return NULL;
328 }
329
330 /* extract the filename from the archive - there are two ways to
331 specify an extendend name table, either the first char of the
332 name is a space, or it's a slash */
333 if ((hdr.ar_name[0] == '/' || hdr.ar_name[0] == ' ')
334 && bfd_ardata (abfd)->extended_names != NULL) {
335 filename = get_extended_arelt_filename (abfd, hdr.ar_name);
336 if (filename == NULL) {
337 bfd_error = malformed_archive;
338 return NULL;
339 }
340 }
341 else
342 {
343 /* We judge the end of the name by looking for a space or a
344 padchar */
345
346 namelen = 0;
347
348 while (namelen < (unsigned)ar_maxnamelen(abfd) &&
349 ( hdr.ar_name[namelen] != 0 &&
350 hdr.ar_name[namelen] != ' ' &&
351 hdr.ar_name[namelen] != ar_padchar(abfd))) {
352 namelen++;
353 }
354
355 allocsize += namelen + 1;
356 }
357
358 allocptr = bfd_zalloc(abfd, allocsize);
359 if (allocptr == NULL) {
360 bfd_error = no_memory;
361 return NULL;
362 }
363
364 ared = (struct areltdata *) allocptr;
365
366 ared->arch_header = allocptr + sizeof (struct areltdata);
367 memcpy ((char *) ared->arch_header, (char *) &hdr, sizeof (struct ar_hdr));
368 ared->parsed_size = parsed_size;
369
370 if (filename != NULL) ared->filename = filename;
371 else {
372 ared->filename = allocptr + (sizeof (struct areltdata) +
373 sizeof (struct ar_hdr));
374 if (namelen)
375 memcpy (ared->filename, hdr.ar_name, namelen);
376 ared->filename[namelen] = '\0';
377 }
378
379 return ared;
380 }
381 \f
382 /* This is an internal function; it's mainly used when indexing
383 through the archive symbol table, but also used to get the next
384 element, since it handles the bookkeeping so nicely for us.
385 */
386
387 bfd *
388 get_elt_at_filepos (archive, filepos)
389 bfd *archive;
390 file_ptr filepos;
391 {
392 struct areltdata *new_areldata;
393 bfd *n_nfd;
394
395 n_nfd = look_for_bfd_in_cache (archive, filepos);
396 if (n_nfd) return n_nfd;
397
398 if (0 > bfd_seek (archive, filepos, SEEK_SET)) {
399 bfd_error = system_call_error;
400 return NULL;
401 }
402
403 if ((new_areldata = snarf_ar_hdr (archive)) == NULL) return NULL;
404
405 n_nfd = _bfd_create_empty_archive_element_shell (archive);
406 if (n_nfd == NULL) {
407 bfd_release (archive, (PTR)new_areldata);
408 return NULL;
409 }
410 n_nfd->origin = bfd_tell (archive);
411 n_nfd->arelt_data = (PTR) new_areldata;
412 n_nfd->filename = new_areldata->filename;
413
414 if (add_bfd_to_cache (archive, filepos, n_nfd))
415 return n_nfd;
416
417 /* huh? */
418 bfd_release (archive, (PTR)n_nfd);
419 bfd_release (archive, (PTR)new_areldata);
420 return NULL;
421 }
422
423 /*
424 FUNCTION
425 bfd_get_elt_at_index
426
427 SYNOPSIS
428 bfd *bfd_get_elt_at_index(bfd * archive, int index);
429
430 DESCRIPTION
431 Return the bfd which is referenced by the symbol indexed by <<index>>.
432 <<index>> should have been returned by <<bfd_get_next_mapent>> (q.v.).
433
434 */
435 bfd *
436 DEFUN(bfd_get_elt_at_index,(abfd, index),
437 bfd *abfd AND
438 int index)
439 {
440 bfd *result =
441 get_elt_at_filepos
442 (abfd, (bfd_ardata (abfd)->symdefs + index)->file_offset);
443 return result;
444 }
445
446 /*
447 FUNCTION
448 bfd_openr_next_archived_file
449
450 SYNOPSIS
451 bfd* bfd_openr_next_archived_file(bfd *archive, bfd *previous);
452
453 DESCRIPTION
454 Initially provided a BFD containing an archive and NULL, opens
455 an inpout BFD on the first contained element and returns that.
456 Subsequent calls to bfd_openr_next_archived_file should pass
457 the archive and the previous return value to return a created
458 BFD to the next contained element. NULL is returned when there
459 are no more.
460
461 */
462
463 bfd *
464 DEFUN(bfd_openr_next_archived_file,(archive, last_file),
465 bfd *archive AND
466 bfd*last_file)
467 {
468
469 if ((bfd_get_format (archive) != bfd_archive) ||
470 (archive->direction == write_direction)) {
471 bfd_error = invalid_operation;
472 return NULL;
473 }
474
475
476 return BFD_SEND (archive,
477 openr_next_archived_file,
478 (archive,
479 last_file));
480
481 }
482
483 bfd *bfd_generic_openr_next_archived_file(archive, last_file)
484 bfd *archive;
485 bfd *last_file;
486 {
487 file_ptr filestart;
488
489 if (!last_file)
490 filestart = bfd_ardata (archive)->first_file_filepos;
491 else {
492 unsigned int size = arelt_size(last_file);
493 /* Pad to an even boundary... */
494 filestart = last_file->origin + size + size%2;
495 }
496
497 return get_elt_at_filepos (archive, filestart);
498 }
499
500
501 bfd_target *
502 bfd_generic_archive_p (abfd)
503 bfd *abfd;
504 {
505 char armag[SARMAG+1];
506
507 if (bfd_read ((PTR)armag, 1, SARMAG, abfd) != SARMAG) {
508 bfd_error = wrong_format;
509 return 0;
510 }
511
512 #ifdef GNU960
513 if (strncmp (armag, BFD_GNU960_ARMAG(abfd), SARMAG)) return 0;
514 #else
515 if (strncmp (armag, ARMAG, SARMAG) &&
516 strncmp (armag, ARMAGB, SARMAG)) return 0;
517 #endif
518
519
520
521 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
522 involves a cast, we can't do it as the left operand of assignment. */
523 abfd->tdata.aout_ar_data = (struct artdata *) bfd_zalloc(abfd,sizeof (struct artdata));
524
525 if (bfd_ardata (abfd) == NULL) {
526 bfd_error = no_memory;
527 return 0;
528 }
529
530 bfd_ardata (abfd)->first_file_filepos = SARMAG;
531
532 if (!BFD_SEND (abfd, _bfd_slurp_armap, (abfd))) {
533 bfd_release(abfd, bfd_ardata (abfd));
534 abfd->tdata.aout_ar_data = NULL;
535 return 0;
536 }
537
538 if (!BFD_SEND (abfd, _bfd_slurp_extended_name_table, (abfd))) {
539 bfd_release(abfd, bfd_ardata (abfd));
540 abfd->tdata.aout_ar_data = NULL;
541 return 0;
542 }
543
544 return abfd->xvec;
545 }
546
547 /* Returns false on error, true otherwise */
548 boolean
549 bfd_slurp_bsd_armap (abfd)
550 bfd *abfd;
551 {
552
553 struct areltdata *mapdata;
554 char nextname[17];
555 unsigned int counter = 0;
556 int *raw_armap, *rbase;
557 struct artdata *ardata = bfd_ardata (abfd);
558 char *stringbase;
559
560 /* FIXME, if the read fails, this routine quietly returns "true"!!
561 It should probably do that if the read gives 0 bytes (empty archive),
562 but fail for any other size... */
563 if (bfd_read ((PTR)nextname, 1, 16, abfd) == 16) {
564 /* The archive has at least 16 bytes in it */
565 bfd_seek (abfd, -16L, SEEK_CUR);
566
567 /* This should be using RANLIBMAG, but at least it can be grepped for
568 in this comment. */
569 if (strncmp (nextname, "__.SYMDEF ", 16)) {
570 bfd_has_map (abfd) = false;
571 return true;
572 }
573
574 mapdata = snarf_ar_hdr (abfd);
575 if (mapdata == NULL) return false;
576
577 raw_armap = (int *) bfd_zalloc(abfd,mapdata->parsed_size);
578 if (raw_armap == NULL) {
579 bfd_error = no_memory;
580 byebye:
581 bfd_release (abfd, (PTR)mapdata);
582 return false;
583 }
584
585 if (bfd_read ((PTR)raw_armap, 1, mapdata->parsed_size, abfd) !=
586 mapdata->parsed_size) {
587 bfd_error = malformed_archive;
588 byebyebye:
589 bfd_release (abfd, (PTR)raw_armap);
590 goto byebye;
591 }
592
593 ardata->symdef_count = bfd_h_get_32(abfd, (PTR)raw_armap) / sizeof (struct symdef);
594
595 if (ardata->symdef_count * sizeof (struct symdef)
596 > mapdata->parsed_size - sizeof (*raw_armap)) {
597 /* Probably we're using the wrong byte ordering. */
598 bfd_error = wrong_format;
599 goto byebyebye;
600 }
601
602 ardata->cache = 0;
603 rbase = raw_armap+1;
604 ardata->symdefs = (carsym *) rbase;
605 stringbase = ((char *) (ardata->symdefs + ardata->symdef_count)) + 4;
606
607 for (;counter < ardata->symdef_count; counter++) {
608 struct symdef *sym = ((struct symdef *) rbase) + counter;
609 sym->s.name = bfd_h_get_32(abfd, (PTR)(&(sym->s.string_offset))) + stringbase;
610 sym->file_offset = bfd_h_get_32(abfd, (PTR)( &(sym->file_offset)));
611 }
612
613 ardata->first_file_filepos = bfd_tell (abfd);
614 /* Pad to an even boundary if you have to */
615 ardata->first_file_filepos += (ardata-> first_file_filepos) %2;
616 /* FIXME, we should provide some way to free raw_ardata when
617 we are done using the strings from it. For now, it seems
618 to be allocated on an obstack anyway... */
619 bfd_has_map (abfd) = true;
620 }
621 return true;
622 }
623
624 /* Returns false on error, true otherwise */
625 boolean
626 bfd_slurp_coff_armap (abfd)
627 bfd *abfd;
628 {
629 struct areltdata *mapdata;
630 char nextname;
631 int *raw_armap, *rawptr;
632 struct artdata *ardata = bfd_ardata (abfd);
633 char *stringbase;
634 unsigned int stringsize;
635 carsym *carsyms;
636 int result;
637 bfd_vma (*swap)();
638
639 result = bfd_read ((PTR)&nextname, 1, 1, abfd);
640 bfd_seek (abfd, -1L, SEEK_CUR);
641
642 if (result != 1 || nextname != '/') {
643 /* Actually I think this is an error for a COFF archive */
644 bfd_has_map (abfd) = false;
645 return true;
646 }
647
648 mapdata = snarf_ar_hdr (abfd);
649 if (mapdata == NULL) return false;
650
651 raw_armap = (int *) bfd_alloc(abfd,mapdata->parsed_size);
652
653 if (raw_armap == NULL)
654 {
655 bfd_error = no_memory;
656 byebye:
657 bfd_release (abfd, (PTR)mapdata);
658 return false;
659 }
660
661 /* read in the raw map */
662 if (bfd_read ((PTR)raw_armap, 1, mapdata->parsed_size, abfd) !=
663 mapdata->parsed_size) {
664 bfd_error = malformed_archive;
665 oops:
666 bfd_release (abfd, (PTR)raw_armap);
667 goto byebye;
668 }
669
670 /* The coff armap must be read sequentially. So we construct a bsd-style
671 one in core all at once, for simplicity.
672
673 It seems that all numeric information in a coff archive is always
674 in big endian format, nomatter the host or target. */
675
676 stringsize
677 = mapdata->parsed_size - (4 * (_do_getb32((PTR)raw_armap))) - 4;
678 /* Except that some archive formats are broken, and it may be our
679 fault - the i960 little endian coff sometimes has big and sometimes
680 little, because our tools changed. Here's a horrible hack to clean
681 up the crap
682 */
683 swap = _do_getb32;
684
685 if (stringsize > 0xfffff)
686 {
687 /* This looks dangerous, let's do it the other way around */
688 stringsize = mapdata->parsed_size - (4 *
689 (_do_getl32((PTR)raw_armap))) - 4;
690
691 swap = _do_getl32;
692 }
693
694
695 {
696 unsigned int nsymz = swap( (PTR)raw_armap);
697 unsigned int carsym_size = (nsymz * sizeof (carsym));
698 unsigned int ptrsize = (4 * nsymz);
699 unsigned int i;
700 ardata->symdefs = (carsym *) bfd_zalloc(abfd,carsym_size + stringsize + 1);
701 if (ardata->symdefs == NULL) {
702 bfd_error = no_memory;
703 goto oops;
704 }
705 carsyms = ardata->symdefs;
706
707 stringbase = ((char *) ardata->symdefs) + carsym_size;
708 memcpy (stringbase, (char*)raw_armap + ptrsize + 4, stringsize);
709
710
711 /* OK, build the carsyms */
712 for (i = 0; i < nsymz; i++)
713 {
714 rawptr = raw_armap + i + 1;
715 carsyms->file_offset = swap((PTR)rawptr);
716 carsyms->name = stringbase;
717 for (; *(stringbase++););
718 carsyms++;
719 }
720 *stringbase = 0;
721 }
722 ardata->symdef_count = swap((PTR)raw_armap);
723 ardata->first_file_filepos = bfd_tell (abfd);
724 /* Pad to an even boundary if you have to */
725 ardata->first_file_filepos += (ardata->first_file_filepos) %2;
726
727 /* We'd like to release these allocations, but we have allocated stuff
728 since then (using the same obstack, if bfd_release is obstack based).
729 So they will stick around until the BFD is closed. */
730 /* bfd_release (abfd, (PTR)raw_armap);
731 bfd_release (abfd, (PTR)mapdata); */
732 bfd_has_map (abfd) = true;
733 return true;
734 }
735 \f
736 /** Extended name table.
737
738 Normally archives support only 14-character filenames.
739
740 Intel has extended the format: longer names are stored in a special
741 element (the first in the archive, or second if there is an armap);
742 the name in the ar_hdr is replaced by <space><index into filename
743 element>. Index is the P.R. of an int (decimal). Data General have
744 extended the format by using the prefix // for the special element */
745
746 /* Returns false on error, true otherwise */
747 boolean
748 _bfd_slurp_extended_name_table (abfd)
749 bfd *abfd;
750 {
751 char nextname[17];
752 struct areltdata *namedata;
753
754 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
755 we probably don't want to return true. */
756 if (bfd_read ((PTR)nextname, 1, 16, abfd) == 16) {
757
758 bfd_seek (abfd, -16L, SEEK_CUR);
759
760 if (strncmp (nextname, "ARFILENAMES/ ", 16) != 0 &&
761 strncmp (nextname, "// ", 16) != 0)
762 {
763 bfd_ardata (abfd)->extended_names = NULL;
764 return true;
765 }
766
767 namedata = snarf_ar_hdr (abfd);
768 if (namedata == NULL) return false;
769
770 bfd_ardata (abfd)->extended_names = bfd_zalloc(abfd,namedata->parsed_size);
771 if (bfd_ardata (abfd)->extended_names == NULL) {
772 bfd_error = no_memory;
773 byebye:
774 bfd_release (abfd, (PTR)namedata);
775 return false;
776 }
777
778 if (bfd_read ((PTR)bfd_ardata (abfd)->extended_names, 1,
779 namedata->parsed_size, abfd) != namedata->parsed_size) {
780 bfd_error = malformed_archive;
781 bfd_release (abfd, (PTR)(bfd_ardata (abfd)->extended_names));
782 bfd_ardata (abfd)->extended_names = NULL;
783 goto byebye;
784 }
785
786 /* Since the archive is supposed to be printable if it contains
787 text, the entries in the list are newline-padded, not null
788 padded. We'll fix that there.. */
789 {
790 char *temp = bfd_ardata (abfd)->extended_names;
791 for (; *temp != '\0'; ++temp)
792 if (*temp == '\n') *temp = '\0';
793 }
794
795 /* Pad to an even boundary if you have to */
796 bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd);
797 bfd_ardata (abfd)->first_file_filepos +=
798 (bfd_ardata (abfd)->first_file_filepos) %2;
799
800 /* FIXME, we can't release namedata here because it was allocated
801 below extended_names on the obstack... */
802 /* bfd_release (abfd, namedata); */
803 }
804 return true;
805 }
806
807 #ifdef VMS
808
809 /* Return a copy of the stuff in the filename between any :]> and a
810 semicolon */
811 static CONST char *
812 DEFUN(normalize,(file),
813 CONST char *file)
814 {
815 CONST char *first;
816 CONST char *last;
817 char *copy;
818
819 first = file + strlen(file)-1;
820 last = first+1;
821
822 while (first != file)
823 {
824 if (*first == ';')
825 last = first;
826 if (*first == ':' || *first == ']' ||*first == '>')
827 {
828 first++;
829 break;
830 }
831 first --;
832 }
833
834
835 copy = malloc(last - first + 1);
836 memcpy(copy, first, last-first);
837 copy[last-first] = 0;
838
839 return copy;
840 }
841
842 #else
843 static
844 CONST char *normalize(file)
845 CONST char *file;
846 {
847 CONST char * filename = strrchr(file, '/');
848
849 if (filename != (char *)NULL) {
850 filename ++;
851 }
852 else {
853 filename = file;
854 }
855 return filename;
856 }
857 #endif
858 /* Follows archive_head and produces an extended name table if necessary.
859 Returns (in tabloc) a pointer to an extended name table, and in tablen
860 the length of the table. If it makes an entry it clobbers the filename
861 so that the element may be written without further massage.
862 Returns true if it ran successfully, false if something went wrong.
863 A successful return may still involve a zero-length tablen!
864 */
865 boolean
866 bfd_construct_extended_name_table (abfd, tabloc, tablen)
867 bfd *abfd;
868 char **tabloc;
869 unsigned int *tablen;
870 {
871 unsigned int maxname = abfd->xvec->ar_max_namelen;
872 unsigned int total_namelen = 0;
873 bfd *current;
874 char *strptr;
875
876 *tablen = 0;
877
878 /* Figure out how long the table should be */
879 for (current = abfd->archive_head; current != NULL; current = current->next){
880 unsigned int thislen = strlen (normalize(current->filename));
881 if (thislen > maxname) total_namelen += thislen + 1; /* leave room for \n */
882 }
883
884 if (total_namelen == 0) return true;
885
886 *tabloc = bfd_zalloc (abfd,total_namelen);
887 if (*tabloc == NULL) {
888 bfd_error = no_memory;
889 return false;
890 }
891
892 *tablen = total_namelen;
893 strptr = *tabloc;
894
895 for (current = abfd->archive_head; current != NULL; current =
896 current->next) {
897 CONST char *normal =normalize( current->filename);
898 unsigned int thislen = strlen (normal);
899 if (thislen > maxname) {
900 /* Works for now; may need to be re-engineered if we encounter an oddball
901 archive format and want to generalise this hack. */
902 struct ar_hdr *hdr = arch_hdr(current);
903 strcpy (strptr, normal);
904 strptr[thislen] = '\n';
905 hdr->ar_name[0] = ' ';
906 /* We know there will always be enough room (one of the few cases
907 where you may safely use sprintf). */
908 sprintf ((hdr->ar_name) + 1, "%-d", (unsigned) (strptr - *tabloc));
909 /* Kinda Kludgy. We should just use the returned value of sprintf
910 but not all implementations get this right */
911 {
912 char *temp = hdr->ar_name +2;
913 for (; temp < hdr->ar_name + maxname; temp++)
914 if (*temp == '\0') *temp = ' ';
915 }
916 strptr += thislen + 1;
917 }
918 }
919
920 return true;
921 }
922 \f
923 /** A couple of functions for creating ar_hdrs */
924
925 /* Takes a filename, returns an arelt_data for it, or NULL if it can't make one.
926 The filename must refer to a filename in the filesystem.
927 The filename field of the ar_hdr will NOT be initialized
928 */
929
930 struct areltdata *
931 DEFUN(bfd_ar_hdr_from_filesystem, (abfd,filename),
932 bfd* abfd AND
933 CONST char *filename)
934 {
935 struct stat status;
936 struct areltdata *ared;
937 struct ar_hdr *hdr;
938 char *temp, *temp1;
939
940
941 if (stat (filename, &status) != 0) {
942 bfd_error = system_call_error;
943 return NULL;
944 }
945
946 ared = (struct areltdata *) bfd_zalloc(abfd, sizeof (struct ar_hdr) +
947 sizeof (struct areltdata));
948 if (ared == NULL) {
949 bfd_error = no_memory;
950 return NULL;
951 }
952 hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
953
954 /* ar headers are space padded, not null padded! */
955 temp = (char *) hdr;
956 temp1 = temp + sizeof (struct ar_hdr) - 2;
957 for (; temp < temp1; *(temp++) = ' ');
958 strncpy (hdr->ar_fmag, ARFMAG, 2);
959
960 /* Goddamned sprintf doesn't permit MAXIMUM field lengths */
961 sprintf ((hdr->ar_date), "%-12ld", status.st_mtime);
962 sprintf ((hdr->ar_uid), "%d", status.st_uid);
963 sprintf ((hdr->ar_gid), "%d", status.st_gid);
964 sprintf ((hdr->ar_mode), "%-8o", (unsigned) status.st_mode);
965 sprintf ((hdr->ar_size), "%-10ld", status.st_size);
966 /* Correct for a lossage in sprintf whereby it null-terminates. I cannot
967 understand how these C losers could design such a ramshackle bunch of
968 IO operations */
969 temp = (char *) hdr;
970 temp1 = temp + sizeof (struct ar_hdr) - 2;
971 for (; temp < temp1; temp++) {
972 if (*temp == '\0') *temp = ' ';
973 }
974 strncpy (hdr->ar_fmag, ARFMAG, 2);
975 ared->parsed_size = status.st_size;
976 ared->arch_header = (char *) hdr;
977
978 return ared;
979 }
980
981 /* This is magic required by the "ar" program. Since it's
982 undocumented, it's undocumented. You may think that it would
983 take a strong stomach to write this, and it does, but it takes
984 even a stronger stomach to try to code around such a thing!
985 */
986
987 struct ar_hdr *
988 DEFUN(bfd_special_undocumented_glue, (abfd, filename),
989 bfd *abfd AND
990 char *filename)
991 {
992 struct areltdata *ar_elt = bfd_ar_hdr_from_filesystem (abfd, filename);
993 if (ar_elt == NULL)
994 return NULL;
995 return (struct ar_hdr *) ar_elt->arch_header;
996 }
997
998
999 /* Analogous to stat call */
1000 int
1001 bfd_generic_stat_arch_elt (abfd, buf)
1002 bfd *abfd;
1003 struct stat *buf;
1004 {
1005 struct ar_hdr *hdr;
1006 char *aloser;
1007
1008 if (abfd->arelt_data == NULL) {
1009 bfd_error = invalid_operation;
1010 return -1;
1011 }
1012
1013 hdr = arch_hdr (abfd);
1014
1015 #define foo(arelt, stelt, size) \
1016 buf->stelt = strtol (hdr->arelt, &aloser, size); \
1017 if (aloser == hdr->arelt) return -1;
1018
1019 foo (ar_date, st_mtime, 10);
1020 foo (ar_uid, st_uid, 10);
1021 foo (ar_gid, st_gid, 10);
1022 foo (ar_mode, st_mode, 8);
1023 foo (ar_size, st_size, 10);
1024
1025 return 0;
1026 }
1027
1028 void
1029 bfd_dont_truncate_arname (abfd, pathname, arhdr)
1030 bfd *abfd;
1031 CONST char *pathname;
1032 char *arhdr;
1033 {
1034 /* FIXME: This interacts unpleasantly with ar's quick-append option.
1035 Fortunately ic960 users will never use that option. Fixing this
1036 is very hard; fortunately I know how to do it and will do so once
1037 intel's release is out the door. */
1038
1039 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1040 int length;
1041 CONST char *filename = normalize(pathname);
1042 int maxlen = ar_maxnamelen (abfd);
1043
1044 length = strlen (filename);
1045
1046 if (length <= maxlen)
1047 memcpy (hdr->ar_name, filename, length);
1048
1049 if (length < maxlen) (hdr->ar_name)[length] = ar_padchar (abfd);
1050 return;
1051
1052 }
1053
1054 void
1055 bfd_bsd_truncate_arname (abfd, pathname, arhdr)
1056 bfd *abfd;
1057 CONST char *pathname;
1058 char *arhdr;
1059 {
1060 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1061 int length;
1062 CONST char *filename = strrchr (pathname, '/');
1063 int maxlen = ar_maxnamelen (abfd);
1064
1065
1066 if (filename == NULL)
1067 filename = pathname;
1068 else
1069 ++filename;
1070
1071 length = strlen (filename);
1072
1073 if (length <= maxlen)
1074 memcpy (hdr->ar_name, filename, length);
1075 else {
1076 /* pathname: meet procrustes */
1077 memcpy (hdr->ar_name, filename, maxlen);
1078 length = maxlen;
1079 }
1080
1081 if (length < maxlen) (hdr->ar_name)[length] = ar_padchar (abfd);
1082 }
1083
1084 /* Store name into ar header. Truncates the name to fit.
1085 1> strip pathname to be just the basename.
1086 2> if it's short enuf to fit, stuff it in.
1087 3> If it doesn't end with .o, truncate it to fit
1088 4> truncate it before the .o, append .o, stuff THAT in.
1089 */
1090
1091 /* This is what gnu ar does. It's better but incompatible with the bsd ar. */
1092 void
1093 bfd_gnu_truncate_arname (abfd, pathname, arhdr)
1094 bfd *abfd;
1095 CONST char *pathname;
1096 char *arhdr;
1097 {
1098 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1099 int length;
1100 CONST char *filename = strrchr (pathname, '/');
1101 int maxlen = ar_maxnamelen (abfd);
1102
1103 if (filename == NULL)
1104 filename = pathname;
1105 else
1106 ++filename;
1107
1108 length = strlen (filename);
1109
1110 if (length <= maxlen)
1111 memcpy (hdr->ar_name, filename, length);
1112 else { /* pathname: meet procrustes */
1113 memcpy (hdr->ar_name, filename, maxlen);
1114 if ((filename[length - 2] == '.') && (filename[length - 1] == 'o')) {
1115 hdr->ar_name[maxlen - 2] = '.';
1116 hdr->ar_name[maxlen - 1] = 'o';
1117 }
1118 length = maxlen;
1119 }
1120
1121 if (length < 16) (hdr->ar_name)[length] = ar_padchar (abfd);
1122 }
1123 \f
1124
1125 PROTO (boolean, compute_and_write_armap, (bfd *arch, unsigned int elength));
1126
1127 /* The BFD is open for write and has its format set to bfd_archive */
1128 boolean
1129 _bfd_write_archive_contents (arch)
1130 bfd *arch;
1131 {
1132 bfd *current;
1133 char *etable = NULL;
1134 unsigned int elength = 0;
1135 boolean makemap = bfd_has_map (arch);
1136 boolean hasobjects = false; /* if no .o's, don't bother to make a map */
1137 unsigned int i;
1138
1139 /* Verify the viability of all entries; if any of them live in the
1140 filesystem (as opposed to living in an archive open for input)
1141 then construct a fresh ar_hdr for them.
1142 */
1143 for (current = arch->archive_head; current; current = current->next) {
1144 if (bfd_write_p (current)) {
1145 bfd_error = invalid_operation;
1146 return false;
1147 }
1148 if (!current->arelt_data) {
1149 current->arelt_data =
1150 (PTR) bfd_ar_hdr_from_filesystem (arch, current->filename);
1151 if (!current->arelt_data) return false;
1152
1153 /* Put in the file name */
1154
1155 BFD_SEND (arch, _bfd_truncate_arname,(arch,
1156 current->filename,
1157 (char *) arch_hdr(current)));
1158
1159
1160 }
1161
1162 if (makemap) { /* don't bother if we won't make a map! */
1163 if ((bfd_check_format (current, bfd_object))
1164 #if 0 /* FIXME -- these are not set correctly */
1165 && ((bfd_get_file_flags (current) & HAS_SYMS))
1166 #endif
1167 )
1168 hasobjects = true;
1169 }
1170 }
1171
1172 if (!bfd_construct_extended_name_table (arch, &etable, &elength))
1173 return false;
1174
1175 bfd_seek (arch, 0, SEEK_SET);
1176 #ifdef GNU960
1177 bfd_write (BFD_GNU960_ARMAG(arch), 1, SARMAG, arch);
1178 #else
1179 bfd_write (ARMAG, 1, SARMAG, arch);
1180 #endif
1181
1182 if (makemap && hasobjects) {
1183
1184 if (compute_and_write_armap (arch, elength) != true) {
1185 return false;
1186 }
1187 }
1188
1189 if (elength != 0) {
1190 struct ar_hdr hdr;
1191
1192 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1193 sprintf (&(hdr.ar_name[0]), "ARFILENAMES/");
1194 sprintf (&(hdr.ar_size[0]), "%-10d", (int) elength);
1195 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1196 for (i = 0; i < sizeof (struct ar_hdr); i++)
1197 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
1198 bfd_write ((char *)&hdr, 1, sizeof (struct ar_hdr), arch);
1199 bfd_write (etable, 1, elength, arch);
1200 if ((elength % 2) == 1) bfd_write ("\n", 1, 1, arch);
1201
1202 }
1203
1204 for (current = arch->archive_head; current; current = current->next) {
1205 char buffer[DEFAULT_BUFFERSIZE];
1206 unsigned int remaining = arelt_size (current);
1207 struct ar_hdr *hdr = arch_hdr(current);
1208 /* write ar header */
1209
1210 if (bfd_write ((char *)hdr, 1, sizeof(*hdr), arch) != sizeof(*hdr)) {
1211 syserr:
1212 bfd_error = system_call_error;
1213 return false;
1214 }
1215 if (bfd_seek (current, 0L, SEEK_SET) != 0L) goto syserr;
1216 while (remaining)
1217 {
1218 unsigned int amt = DEFAULT_BUFFERSIZE;
1219 if (amt > remaining) {
1220 amt = remaining;
1221 }
1222 errno = 0;
1223 if (bfd_read (buffer, amt, 1, current) != amt) {
1224 if (errno) goto syserr;
1225 /* Looks like a truncated archive. */
1226 bfd_error = malformed_archive;
1227 return false;
1228 }
1229 if (bfd_write (buffer, amt, 1, arch) != amt) goto syserr;
1230 remaining -= amt;
1231 }
1232 if ((arelt_size (current) % 2) == 1) bfd_write ("\n", 1, 1, arch);
1233 }
1234 return true;
1235 }
1236 \f
1237 /* Note that the namidx for the first symbol is 0 */
1238
1239 boolean
1240 compute_and_write_armap (arch, elength)
1241 bfd *arch;
1242 unsigned int elength;
1243 {
1244 bfd *current;
1245 file_ptr elt_no = 0;
1246 struct orl *map;
1247 int orl_max = 15000; /* fine initial default */
1248 int orl_count = 0;
1249 int stridx = 0; /* string index */
1250
1251 /* Dunno if this is the best place for this info... */
1252 if (elength != 0) elength += sizeof (struct ar_hdr);
1253 elength += elength %2 ;
1254
1255 map = (struct orl *) bfd_zalloc (arch,orl_max * sizeof (struct orl));
1256 if (map == NULL) {
1257 bfd_error = no_memory;
1258 return false;
1259 }
1260
1261 /* Drop all the files called __.SYMDEF, we're going to make our
1262 own */
1263 while (arch->archive_head &&
1264 strcmp(arch->archive_head->filename,"__.SYMDEF") == 0)
1265 {
1266 arch->archive_head = arch->archive_head->next;
1267 }
1268 /* Map over each element */
1269 for (current = arch->archive_head;
1270 current != (bfd *)NULL;
1271 current = current->next, elt_no++)
1272 {
1273 if ((bfd_check_format (current, bfd_object) == true)
1274 && ((bfd_get_file_flags (current) & HAS_SYMS))) {
1275 asymbol **syms;
1276 unsigned int storage;
1277 unsigned int symcount;
1278 unsigned int src_count;
1279
1280 storage = get_symtab_upper_bound (current);
1281 if (storage != 0) {
1282
1283 syms = (asymbol **) bfd_zalloc (arch,storage);
1284 if (syms == NULL) {
1285 bfd_error = no_memory; /* FIXME -- memory leak */
1286 return false;
1287 }
1288 symcount = bfd_canonicalize_symtab (current, syms);
1289
1290
1291 /* Now map over all the symbols, picking out the ones we want */
1292 for (src_count = 0; src_count <symcount; src_count++) {
1293 flagword flags =
1294 (syms[src_count])->flags;
1295 asection *sec =
1296 syms[src_count]->section;
1297
1298 if ((flags & BSF_GLOBAL) ||
1299 (flags & BSF_INDIRECT) ||
1300 (sec == &bfd_com_section)) {
1301
1302 /* This symbol will go into the archive header */
1303 if (orl_count == orl_max)
1304 {
1305 orl_max *= 2;
1306 map = (struct orl *) bfd_realloc (arch, (char *) map,
1307 orl_max * sizeof (struct orl));
1308 }
1309
1310 (map[orl_count]).name = (char **) &((syms[src_count])->name);
1311 (map[orl_count]).pos = (file_ptr) current;
1312 (map[orl_count]).namidx = stridx;
1313
1314 stridx += strlen ((syms[src_count])->name) + 1;
1315 ++orl_count;
1316 }
1317 }
1318 }
1319 }
1320 }
1321 /* OK, now we have collected all the data, let's write them out */
1322 if (!BFD_SEND (arch, write_armap,
1323 (arch, elength, map, orl_count, stridx))) {
1324
1325 return false;
1326 }
1327
1328
1329 return true;
1330 }
1331
1332 boolean
1333 bsd_write_armap (arch, elength, map, orl_count, stridx)
1334 bfd *arch;
1335 unsigned int elength;
1336 struct orl *map;
1337 unsigned int orl_count;
1338 int stridx;
1339 {
1340 int padit = stridx & 1;
1341 unsigned int ranlibsize = orl_count * sizeof (struct ranlib);
1342 unsigned int stringsize = stridx + padit;
1343 /* Include 8 bytes to store ranlibsize and stringsize in output. */
1344 unsigned int mapsize = ranlibsize + stringsize + 8;
1345 file_ptr firstreal;
1346 bfd *current = arch->archive_head;
1347 bfd *last_elt = current; /* last element arch seen */
1348 int temp;
1349 int count;
1350 struct ar_hdr hdr;
1351 struct stat statbuf;
1352 unsigned int i;
1353
1354 firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
1355
1356 stat (arch->filename, &statbuf);
1357 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1358 sprintf (hdr.ar_name, RANLIBMAG);
1359
1360 /* write the timestamp of the archive header to be just a little bit
1361 later than the timestamp of the file, otherwise the linker will
1362 complain that the index is out of date.
1363 */
1364
1365 sprintf (hdr.ar_date, "%ld", statbuf.st_mtime + 60);
1366 sprintf (hdr.ar_uid, "%d", getuid());
1367 sprintf (hdr.ar_gid, "%d", getgid());
1368 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
1369 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1370 for (i = 0; i < sizeof (struct ar_hdr); i++)
1371 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
1372 bfd_write ((char *)&hdr, 1, sizeof (struct ar_hdr), arch);
1373 bfd_h_put_32(arch, ranlibsize, (PTR)&temp);
1374 bfd_write (&temp, 1, sizeof (temp), arch);
1375
1376 for (count = 0; count < orl_count; count++) {
1377 struct symdef outs;
1378 struct symdef *outp = &outs;
1379
1380 if (((bfd *)(map[count]).pos) != last_elt) {
1381 do {
1382 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
1383 firstreal += firstreal % 2;
1384 current = current->next;
1385 } while (current != (bfd *)(map[count]).pos);
1386 } /* if new archive element */
1387
1388 last_elt = current;
1389 bfd_h_put_32(arch, ((map[count]).namidx),(PTR) &outs.s.string_offset);
1390 bfd_h_put_32(arch, firstreal,(PTR) &outs.file_offset);
1391 bfd_write ((char *)outp, 1, sizeof (outs), arch);
1392 }
1393
1394 /* now write the strings themselves */
1395 bfd_h_put_32(arch, stringsize, (PTR)&temp);
1396 bfd_write ((PTR)&temp, 1, sizeof (temp), arch);
1397 for (count = 0; count < orl_count; count++)
1398 bfd_write (*((map[count]).name), 1, strlen (*((map[count]).name))+1, arch);
1399
1400 /* The spec sez this should be a newline. But in order to be
1401 bug-compatible for sun's ar we use a null. */
1402 if (padit)
1403 bfd_write("\0",1,1,arch);
1404
1405 return true;
1406 }
1407 \f
1408
1409 /* A coff armap looks like :
1410 lARMAG
1411 struct ar_hdr with name = '/'
1412 number of symbols
1413 offset of file for symbol 0
1414 offset of file for symbol 1
1415
1416 offset of file for symbol n-1
1417 symbol name 0
1418 symbol name 1
1419
1420 symbol name n-1
1421
1422 */
1423
1424 boolean
1425 coff_write_armap (arch, elength, map, symbol_count, stridx)
1426 bfd *arch;
1427 unsigned int elength;
1428 struct orl *map;
1429 unsigned int symbol_count;
1430 int stridx;
1431 {
1432 /* The size of the ranlib is the number of exported symbols in the
1433 archive * the number of bytes in a int, + an int for the count */
1434
1435 unsigned int ranlibsize = (symbol_count * 4) + 4;
1436 unsigned int stringsize = stridx;
1437 unsigned int mapsize = stringsize + ranlibsize;
1438 file_ptr archive_member_file_ptr;
1439 bfd *current = arch->archive_head;
1440 int count;
1441 struct ar_hdr hdr;
1442 unsigned int i;
1443 int padit = mapsize & 1;
1444
1445 if (padit) mapsize ++;
1446
1447 /* work out where the first object file will go in the archive */
1448 archive_member_file_ptr = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
1449
1450 memset ((char *)(&hdr), 0, sizeof (struct ar_hdr));
1451 hdr.ar_name[0] = '/';
1452 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
1453 sprintf (hdr.ar_date, "%ld", (long)time (NULL));
1454 /* This, at least, is what Intel coff sets the values to.: */
1455 sprintf ((hdr.ar_uid), "%d", 0);
1456 sprintf ((hdr.ar_gid), "%d", 0);
1457 sprintf ((hdr.ar_mode), "%-7o",(unsigned ) 0);
1458 hdr.ar_fmag[0] = '`'; hdr.ar_fmag[1] = '\n';
1459
1460 for (i = 0; i < sizeof (struct ar_hdr); i++)
1461 if (((char *)(&hdr))[i] == '\0') (((char *)(&hdr))[i]) = ' ';
1462
1463 /* Write the ar header for this item and the number of symbols */
1464
1465
1466 bfd_write ((PTR)&hdr, 1, sizeof (struct ar_hdr), arch);
1467
1468 bfd_write_bigendian_4byte_int(arch, symbol_count);
1469
1470 /* Two passes, first write the file offsets for each symbol -
1471 remembering that each offset is on a two byte boundary. */
1472
1473 /* Write out the file offset for the file associated with each
1474 symbol, and remember to keep the offsets padded out. */
1475
1476 current = arch->archive_head;
1477 count = 0;
1478 while (current != (bfd *)NULL && count < symbol_count) {
1479 /* For each symbol which is used defined in this object, write out
1480 the object file's address in the archive */
1481
1482 while (((bfd *)(map[count]).pos) == current) {
1483 bfd_write_bigendian_4byte_int(arch, archive_member_file_ptr);
1484 count++;
1485 }
1486 /* Add size of this archive entry */
1487 archive_member_file_ptr += arelt_size (current) + sizeof (struct
1488 ar_hdr);
1489 /* remember aboout the even alignment */
1490 archive_member_file_ptr += archive_member_file_ptr % 2;
1491 current = current->next;
1492 }
1493
1494
1495
1496 /* now write the strings themselves */
1497 for (count = 0; count < symbol_count; count++) {
1498 bfd_write ((PTR)*((map[count]).name),
1499 1,
1500 strlen (*((map[count]).name))+1, arch);
1501
1502 }
1503 /* The spec sez this should be a newline. But in order to be
1504 bug-compatible for arc960 we use a null. */
1505 if (padit)
1506 bfd_write("\0",1,1,arch);
1507
1508 return true;
1509 }