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