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