daily update
[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 bfd_boolean fail;
252b5132 670
b34976b6
AM
671 first->target_defaulted = FALSE;
672 fail = FALSE;
252b5132
RH
673 if (bfd_check_format (first, bfd_object)
674 && first->xvec != abfd->xvec)
675 {
3619ad04 676 bfd_set_error (bfd_error_wrong_object_format);
487e54f2 677 bfd_ardata (abfd) = tdata_hold;
252b5132
RH
678 return NULL;
679 }
3619ad04 680 /* And we ought to close `first' here too. */
252b5132
RH
681 }
682 }
683
684 return abfd->xvec;
685}
686
687/* Some constants for a 32 bit BSD archive structure. We do not
688 support 64 bit archives presently; so far as I know, none actually
689 exist. Supporting them would require changing these constants, and
dc810e39 690 changing some H_GET_32 to H_GET_64. */
252b5132
RH
691
692/* The size of an external symdef structure. */
693#define BSD_SYMDEF_SIZE 8
694
695/* The offset from the start of a symdef structure to the file offset. */
696#define BSD_SYMDEF_OFFSET_SIZE 4
697
698/* The size of the symdef count. */
699#define BSD_SYMDEF_COUNT_SIZE 4
700
701/* The size of the string count. */
702#define BSD_STRING_COUNT_SIZE 4
703
06fc8a8c 704/* Returns FALSE on error, TRUE otherwise. */
252b5132 705
b34976b6 706static bfd_boolean
c58b9523 707do_slurp_bsd_armap (bfd *abfd)
252b5132
RH
708{
709 struct areltdata *mapdata;
710 unsigned int counter;
711 bfd_byte *raw_armap, *rbase;
712 struct artdata *ardata = bfd_ardata (abfd);
713 char *stringbase;
dc810e39 714 bfd_size_type parsed_size, amt;
252b5132
RH
715 carsym *set;
716
c58b9523 717 mapdata = _bfd_read_ar_hdr (abfd);
252b5132 718 if (mapdata == NULL)
b34976b6 719 return FALSE;
252b5132 720 parsed_size = mapdata->parsed_size;
c58b9523 721 bfd_release (abfd, mapdata); /* Don't need it any more. */
252b5132 722
c58b9523
AM
723 raw_armap = bfd_zalloc (abfd, parsed_size);
724 if (raw_armap == NULL)
b34976b6 725 return FALSE;
252b5132 726
c58b9523 727 if (bfd_bread (raw_armap, parsed_size, abfd) != parsed_size)
252b5132
RH
728 {
729 if (bfd_get_error () != bfd_error_system_call)
730 bfd_set_error (bfd_error_malformed_archive);
731 byebye:
c58b9523 732 bfd_release (abfd, raw_armap);
b34976b6 733 return FALSE;
252b5132
RH
734 }
735
dc810e39 736 ardata->symdef_count = H_GET_32 (abfd, raw_armap) / BSD_SYMDEF_SIZE;
252b5132
RH
737
738 if (ardata->symdef_count * BSD_SYMDEF_SIZE >
739 parsed_size - BSD_SYMDEF_COUNT_SIZE)
740 {
741 /* Probably we're using the wrong byte ordering. */
742 bfd_set_error (bfd_error_wrong_format);
743 goto byebye;
744 }
745
746 ardata->cache = 0;
747 rbase = raw_armap + BSD_SYMDEF_COUNT_SIZE;
748 stringbase = ((char *) rbase
749 + ardata->symdef_count * BSD_SYMDEF_SIZE
750 + BSD_STRING_COUNT_SIZE);
c58b9523
AM
751 amt = ardata->symdef_count * sizeof (carsym);
752 ardata->symdefs = bfd_alloc (abfd, amt);
252b5132 753 if (!ardata->symdefs)
b34976b6 754 return FALSE;
252b5132
RH
755
756 for (counter = 0, set = ardata->symdefs;
757 counter < ardata->symdef_count;
758 counter++, set++, rbase += BSD_SYMDEF_SIZE)
759 {
dc810e39
AM
760 set->name = H_GET_32 (abfd, rbase) + stringbase;
761 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
252b5132
RH
762 }
763
764 ardata->first_file_filepos = bfd_tell (abfd);
047066e1 765 /* Pad to an even boundary if you have to. */
252b5132
RH
766 ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
767 /* FIXME, we should provide some way to free raw_ardata when
768 we are done using the strings from it. For now, it seems
d70910e8 769 to be allocated on an objalloc anyway... */
b34976b6
AM
770 bfd_has_map (abfd) = TRUE;
771 return TRUE;
252b5132
RH
772}
773
b34976b6 774/* Returns FALSE on error, TRUE otherwise. */
047066e1 775
b34976b6 776static bfd_boolean
c58b9523 777do_slurp_coff_armap (bfd *abfd)
252b5132
RH
778{
779 struct areltdata *mapdata;
780 int *raw_armap, *rawptr;
781 struct artdata *ardata = bfd_ardata (abfd);
782 char *stringbase;
dc810e39 783 bfd_size_type stringsize;
252b5132
RH
784 unsigned int parsed_size;
785 carsym *carsyms;
dc810e39 786 bfd_size_type nsymz; /* Number of symbols in armap. */
edeb6e24 787 bfd_vma (*swap) (const void *);
252b5132 788 char int_buf[sizeof (long)];
dc810e39
AM
789 bfd_size_type carsym_size, ptrsize;
790 unsigned int i;
252b5132 791
c58b9523 792 mapdata = _bfd_read_ar_hdr (abfd);
252b5132 793 if (mapdata == NULL)
b34976b6 794 return FALSE;
252b5132 795 parsed_size = mapdata->parsed_size;
c58b9523 796 bfd_release (abfd, mapdata); /* Don't need it any more. */
252b5132 797
c58b9523 798 if (bfd_bread (int_buf, 4, abfd) != 4)
252b5132
RH
799 {
800 if (bfd_get_error () != bfd_error_system_call)
801 bfd_set_error (bfd_error_malformed_archive);
b34976b6 802 return FALSE;
252b5132
RH
803 }
804 /* It seems that all numeric information in a coff archive is always
d70910e8 805 in big endian format, nomatter the host or target. */
252b5132 806 swap = bfd_getb32;
c58b9523 807 nsymz = bfd_getb32 (int_buf);
252b5132
RH
808 stringsize = parsed_size - (4 * nsymz) - 4;
809
252b5132
RH
810 /* ... except that some archive formats are broken, and it may be our
811 fault - the i960 little endian coff sometimes has big and sometimes
812 little, because our tools changed. Here's a horrible hack to clean
813 up the crap. */
814
815 if (stringsize > 0xfffff
816 && bfd_get_arch (abfd) == bfd_arch_i960
817 && bfd_get_flavour (abfd) == bfd_target_coff_flavour)
818 {
047066e1 819 /* This looks dangerous, let's do it the other way around. */
c58b9523 820 nsymz = bfd_getl32 (int_buf);
252b5132
RH
821 stringsize = parsed_size - (4 * nsymz) - 4;
822 swap = bfd_getl32;
823 }
252b5132
RH
824
825 /* The coff armap must be read sequentially. So we construct a
d70910e8 826 bsd-style one in core all at once, for simplicity. */
252b5132 827
933d961a
JJ
828 if (nsymz > ~ (bfd_size_type) 0 / sizeof (carsym))
829 return FALSE;
830
252b5132
RH
831 carsym_size = (nsymz * sizeof (carsym));
832 ptrsize = (4 * nsymz);
833
933d961a
JJ
834 if (carsym_size + stringsize + 1 <= carsym_size)
835 return FALSE;
836
c58b9523 837 ardata->symdefs = bfd_zalloc (abfd, carsym_size + stringsize + 1);
252b5132 838 if (ardata->symdefs == NULL)
b34976b6 839 return FALSE;
252b5132
RH
840 carsyms = ardata->symdefs;
841 stringbase = ((char *) ardata->symdefs) + carsym_size;
842
d70910e8 843 /* Allocate and read in the raw offsets. */
c58b9523 844 raw_armap = bfd_alloc (abfd, ptrsize);
252b5132
RH
845 if (raw_armap == NULL)
846 goto release_symdefs;
c58b9523
AM
847 if (bfd_bread (raw_armap, ptrsize, abfd) != ptrsize
848 || (bfd_bread (stringbase, stringsize, abfd) != stringsize))
252b5132
RH
849 {
850 if (bfd_get_error () != bfd_error_system_call)
851 bfd_set_error (bfd_error_malformed_archive);
852 goto release_raw_armap;
853 }
854
047066e1 855 /* OK, build the carsyms. */
252b5132
RH
856 for (i = 0; i < nsymz; i++)
857 {
858 rawptr = raw_armap + i;
c58b9523 859 carsyms->file_offset = swap ((bfd_byte *) rawptr);
252b5132
RH
860 carsyms->name = stringbase;
861 stringbase += strlen (stringbase) + 1;
862 carsyms++;
863 }
864 *stringbase = 0;
865
866 ardata->symdef_count = nsymz;
867 ardata->first_file_filepos = bfd_tell (abfd);
047066e1 868 /* Pad to an even boundary if you have to. */
252b5132
RH
869 ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
870
b34976b6 871 bfd_has_map (abfd) = TRUE;
c58b9523 872 bfd_release (abfd, raw_armap);
252b5132 873
047066e1 874 /* Check for a second archive header (as used by PE). */
252b5132
RH
875 {
876 struct areltdata *tmp;
877
dc810e39 878 bfd_seek (abfd, ardata->first_file_filepos, SEEK_SET);
c58b9523 879 tmp = _bfd_read_ar_hdr (abfd);
23afc6f6 880 if (tmp != NULL)
252b5132
RH
881 {
882 if (tmp->arch_header[0] == '/'
23afc6f6 883 && tmp->arch_header[1] == ' ')
252b5132
RH
884 {
885 ardata->first_file_filepos +=
dc810e39 886 (tmp->parsed_size + sizeof (struct ar_hdr) + 1) & ~(unsigned) 1;
252b5132
RH
887 }
888 bfd_release (abfd, tmp);
889 }
890 }
891
b34976b6 892 return TRUE;
252b5132
RH
893
894release_raw_armap:
c58b9523 895 bfd_release (abfd, raw_armap);
252b5132 896release_symdefs:
c58b9523 897 bfd_release (abfd, (ardata)->symdefs);
b34976b6 898 return FALSE;
252b5132
RH
899}
900
901/* This routine can handle either coff-style or bsd-style armaps.
b34976b6 902 Returns FALSE on error, TRUE otherwise */
252b5132 903
b34976b6 904bfd_boolean
c58b9523 905bfd_slurp_armap (bfd *abfd)
252b5132
RH
906{
907 char nextname[17];
c58b9523 908 int i = bfd_bread (nextname, 16, abfd);
252b5132
RH
909
910 if (i == 0)
b34976b6 911 return TRUE;
252b5132 912 if (i != 16)
b34976b6 913 return FALSE;
252b5132 914
dc810e39 915 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
b34976b6 916 return FALSE;
252b5132
RH
917
918 if (!strncmp (nextname, "__.SYMDEF ", 16)
919 || !strncmp (nextname, "__.SYMDEF/ ", 16)) /* old Linux archives */
920 return do_slurp_bsd_armap (abfd);
921 else if (!strncmp (nextname, "/ ", 16))
922 return do_slurp_coff_armap (abfd);
923 else if (!strncmp (nextname, "/SYM64/ ", 16))
924 {
36b45482
TS
925 /* 64bit ELF (Irix 6) archive. */
926#ifdef BFD64
c58b9523 927 extern bfd_boolean bfd_elf64_archive_slurp_armap (bfd *);
36b45482
TS
928 return bfd_elf64_archive_slurp_armap (abfd);
929#else
252b5132 930 bfd_set_error (bfd_error_wrong_format);
b34976b6 931 return FALSE;
36b45482 932#endif
252b5132
RH
933 }
934
b34976b6
AM
935 bfd_has_map (abfd) = FALSE;
936 return TRUE;
252b5132
RH
937}
938\f
06fc8a8c
NC
939/* Returns FALSE on error, TRUE otherwise. */
940/* Flavor 2 of a bsd armap, similar to bfd_slurp_bsd_armap except the
252b5132 941 header is in a slightly different order and the map name is '/'.
d70910e8 942 This flavour is used by hp300hpux. */
252b5132
RH
943
944#define HPUX_SYMDEF_COUNT_SIZE 2
945
b34976b6 946bfd_boolean
c58b9523 947bfd_slurp_bsd_armap_f2 (bfd *abfd)
252b5132
RH
948{
949 struct areltdata *mapdata;
950 char nextname[17];
951 unsigned int counter;
952 bfd_byte *raw_armap, *rbase;
953 struct artdata *ardata = bfd_ardata (abfd);
954 char *stringbase;
955 unsigned int stringsize;
dc810e39 956 bfd_size_type amt;
252b5132 957 carsym *set;
c58b9523 958 int i = bfd_bread (nextname, 16, abfd);
252b5132
RH
959
960 if (i == 0)
b34976b6 961 return TRUE;
252b5132 962 if (i != 16)
b34976b6 963 return FALSE;
252b5132 964
047066e1 965 /* The archive has at least 16 bytes in it. */
dc810e39 966 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
b34976b6 967 return FALSE;
252b5132
RH
968
969 if (!strncmp (nextname, "__.SYMDEF ", 16)
06fc8a8c 970 || !strncmp (nextname, "__.SYMDEF/ ", 16)) /* Old Linux archives. */
252b5132
RH
971 return do_slurp_bsd_armap (abfd);
972
973 if (strncmp (nextname, "/ ", 16))
974 {
b34976b6
AM
975 bfd_has_map (abfd) = FALSE;
976 return TRUE;
252b5132
RH
977 }
978
c58b9523 979 mapdata = _bfd_read_ar_hdr (abfd);
252b5132 980 if (mapdata == NULL)
b34976b6 981 return FALSE;
252b5132 982
dc810e39 983 amt = mapdata->parsed_size;
c58b9523 984 raw_armap = bfd_zalloc (abfd, amt);
252b5132
RH
985 if (raw_armap == NULL)
986 {
987 byebye:
c58b9523 988 bfd_release (abfd, mapdata);
b34976b6 989 return FALSE;
252b5132
RH
990 }
991
c58b9523 992 if (bfd_bread (raw_armap, amt, abfd) != amt)
252b5132
RH
993 {
994 if (bfd_get_error () != bfd_error_system_call)
995 bfd_set_error (bfd_error_malformed_archive);
996 byebyebye:
c58b9523 997 bfd_release (abfd, raw_armap);
252b5132
RH
998 goto byebye;
999 }
1000
c58b9523 1001 ardata->symdef_count = H_GET_16 (abfd, raw_armap);
252b5132
RH
1002
1003 if (ardata->symdef_count * BSD_SYMDEF_SIZE
1004 > mapdata->parsed_size - HPUX_SYMDEF_COUNT_SIZE)
1005 {
1006 /* Probably we're using the wrong byte ordering. */
1007 bfd_set_error (bfd_error_wrong_format);
1008 goto byebyebye;
1009 }
1010
1011 ardata->cache = 0;
1012
dc810e39 1013 stringsize = H_GET_32 (abfd, raw_armap + HPUX_SYMDEF_COUNT_SIZE);
047066e1 1014 /* Skip sym count and string sz. */
252b5132
RH
1015 stringbase = ((char *) raw_armap
1016 + HPUX_SYMDEF_COUNT_SIZE
1017 + BSD_STRING_COUNT_SIZE);
1018 rbase = (bfd_byte *) stringbase + stringsize;
c58b9523
AM
1019 amt = ardata->symdef_count * BSD_SYMDEF_SIZE;
1020 ardata->symdefs = bfd_alloc (abfd, amt);
252b5132 1021 if (!ardata->symdefs)
b34976b6 1022 return FALSE;
252b5132
RH
1023
1024 for (counter = 0, set = ardata->symdefs;
1025 counter < ardata->symdef_count;
1026 counter++, set++, rbase += BSD_SYMDEF_SIZE)
1027 {
dc810e39
AM
1028 set->name = H_GET_32 (abfd, rbase) + stringbase;
1029 set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
252b5132
RH
1030 }
1031
1032 ardata->first_file_filepos = bfd_tell (abfd);
047066e1 1033 /* Pad to an even boundary if you have to. */
252b5132
RH
1034 ardata->first_file_filepos += (ardata->first_file_filepos) % 2;
1035 /* FIXME, we should provide some way to free raw_ardata when
1036 we are done using the strings from it. For now, it seems
d70910e8 1037 to be allocated on an objalloc anyway... */
b34976b6
AM
1038 bfd_has_map (abfd) = TRUE;
1039 return TRUE;
252b5132
RH
1040}
1041\f
1042/** Extended name table.
1043
1044 Normally archives support only 14-character filenames.
1045
1046 Intel has extended the format: longer names are stored in a special
1047 element (the first in the archive, or second if there is an armap);
1048 the name in the ar_hdr is replaced by <space><index into filename
1049 element>. Index is the P.R. of an int (decimal). Data General have
047066e1
KH
1050 extended the format by using the prefix // for the special element. */
1051
b34976b6 1052/* Returns FALSE on error, TRUE otherwise. */
252b5132 1053
b34976b6 1054bfd_boolean
c58b9523 1055_bfd_slurp_extended_name_table (bfd *abfd)
252b5132
RH
1056{
1057 char nextname[17];
1058 struct areltdata *namedata;
dc810e39 1059 bfd_size_type amt;
252b5132
RH
1060
1061 /* FIXME: Formatting sucks here, and in case of failure of BFD_READ,
b34976b6 1062 we probably don't want to return TRUE. */
252b5132 1063 bfd_seek (abfd, bfd_ardata (abfd)->first_file_filepos, SEEK_SET);
c58b9523 1064 if (bfd_bread (nextname, 16, abfd) == 16)
252b5132 1065 {
dc810e39 1066 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
b34976b6 1067 return FALSE;
252b5132
RH
1068
1069 if (strncmp (nextname, "ARFILENAMES/ ", 16) != 0 &&
1070 strncmp (nextname, "// ", 16) != 0)
1071 {
1072 bfd_ardata (abfd)->extended_names = NULL;
9e492e05 1073 bfd_ardata (abfd)->extended_names_size = 0;
b34976b6 1074 return TRUE;
252b5132
RH
1075 }
1076
c58b9523 1077 namedata = _bfd_read_ar_hdr (abfd);
252b5132 1078 if (namedata == NULL)
b34976b6 1079 return FALSE;
252b5132 1080
dc810e39 1081 amt = namedata->parsed_size;
9e492e05
JJ
1082 if (amt + 1 == 0)
1083 goto byebye;
1084
1085 bfd_ardata (abfd)->extended_names_size = amt;
1086 bfd_ardata (abfd)->extended_names = bfd_zalloc (abfd, amt + 1);
252b5132
RH
1087 if (bfd_ardata (abfd)->extended_names == NULL)
1088 {
1089 byebye:
c58b9523 1090 bfd_release (abfd, namedata);
b34976b6 1091 return FALSE;
252b5132
RH
1092 }
1093
c58b9523 1094 if (bfd_bread (bfd_ardata (abfd)->extended_names, amt, abfd) != amt)
252b5132
RH
1095 {
1096 if (bfd_get_error () != bfd_error_system_call)
1097 bfd_set_error (bfd_error_malformed_archive);
c58b9523 1098 bfd_release (abfd, (bfd_ardata (abfd)->extended_names));
252b5132
RH
1099 bfd_ardata (abfd)->extended_names = NULL;
1100 goto byebye;
1101 }
1102
1103 /* Since the archive is supposed to be printable if it contains
1104 text, the entries in the list are newline-padded, not null
1105 padded. In SVR4-style archives, the names also have a
1106 trailing '/'. DOS/NT created archive often have \ in them
1107 We'll fix all problems here.. */
1108 {
9e492e05
JJ
1109 char *ext_names = bfd_ardata (abfd)->extended_names;
1110 char *temp = ext_names;
252b5132 1111 char *limit = temp + namedata->parsed_size;
047066e1
KH
1112 for (; temp < limit; ++temp)
1113 {
1114 if (*temp == '\012')
9e492e05 1115 temp[temp > ext_names && temp[-1] == '/' ? -1 : 0] = '\0';
047066e1
KH
1116 if (*temp == '\\')
1117 *temp = '/';
1118 }
9e492e05 1119 *limit = '\0';
252b5132
RH
1120 }
1121
047066e1 1122 /* Pad to an even boundary if you have to. */
252b5132
RH
1123 bfd_ardata (abfd)->first_file_filepos = bfd_tell (abfd);
1124 bfd_ardata (abfd)->first_file_filepos +=
1125 (bfd_ardata (abfd)->first_file_filepos) % 2;
1126
1127 /* FIXME, we can't release namedata here because it was allocated
d70910e8 1128 below extended_names on the objalloc... */
252b5132 1129 }
b34976b6 1130 return TRUE;
252b5132
RH
1131}
1132
1133#ifdef VMS
1134
1135/* Return a copy of the stuff in the filename between any :]> and a
047066e1
KH
1136 semicolon. */
1137
252b5132 1138static const char *
c58b9523 1139normalize (bfd *abfd, const char *file)
252b5132 1140{
dc810e39
AM
1141 const char *first;
1142 const char *last;
252b5132
RH
1143 char *copy;
1144
1145 first = file + strlen (file) - 1;
1146 last = first + 1;
1147
1148 while (first != file)
1149 {
1150 if (*first == ';')
1151 last = first;
1152 if (*first == ':' || *first == ']' || *first == '>')
1153 {
1154 first++;
1155 break;
1156 }
1157 first--;
1158 }
1159
c58b9523 1160 copy = bfd_alloc (abfd, last - first + 1);
252b5132
RH
1161 if (copy == NULL)
1162 return NULL;
1163
1164 memcpy (copy, first, last - first);
1165 copy[last - first] = 0;
1166
1167 return copy;
1168}
1169
1170#else
1171static const char *
c58b9523 1172normalize (bfd *abfd ATTRIBUTE_UNUSED, const char *file)
252b5132
RH
1173{
1174 const char *filename = strrchr (file, '/');
1175
5af11cab
AM
1176#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1177 {
1178 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1179 char *bslash = strrchr (file, '\\');
2ab47eed 1180 if (filename == NULL || (bslash != NULL && bslash > filename))
5af11cab
AM
1181 filename = bslash;
1182 if (filename == NULL && file[0] != '\0' && file[1] == ':')
1183 filename = file + 1;
1184 }
1185#endif
c58b9523 1186 if (filename != NULL)
252b5132
RH
1187 filename++;
1188 else
1189 filename = file;
1190 return filename;
1191}
1192#endif
1193
1194/* Build a BFD style extended name table. */
1195
b34976b6 1196bfd_boolean
c58b9523
AM
1197_bfd_archive_bsd_construct_extended_name_table (bfd *abfd,
1198 char **tabloc,
1199 bfd_size_type *tablen,
1200 const char **name)
252b5132
RH
1201{
1202 *name = "ARFILENAMES/";
b34976b6 1203 return _bfd_construct_extended_name_table (abfd, FALSE, tabloc, tablen);
252b5132
RH
1204}
1205
1206/* Build an SVR4 style extended name table. */
1207
b34976b6 1208bfd_boolean
c58b9523
AM
1209_bfd_archive_coff_construct_extended_name_table (bfd *abfd,
1210 char **tabloc,
1211 bfd_size_type *tablen,
1212 const char **name)
252b5132
RH
1213{
1214 *name = "//";
b34976b6 1215 return _bfd_construct_extended_name_table (abfd, TRUE, tabloc, tablen);
252b5132
RH
1216}
1217
1218/* Follows archive_head and produces an extended name table if
1219 necessary. Returns (in tabloc) a pointer to an extended name
1220 table, and in tablen the length of the table. If it makes an entry
1221 it clobbers the filename so that the element may be written without
b34976b6 1222 further massage. Returns TRUE if it ran successfully, FALSE if
252b5132
RH
1223 something went wrong. A successful return may still involve a
1224 zero-length tablen! */
1225
b34976b6 1226bfd_boolean
c58b9523
AM
1227_bfd_construct_extended_name_table (bfd *abfd,
1228 bfd_boolean trailing_slash,
1229 char **tabloc,
1230 bfd_size_type *tablen)
252b5132
RH
1231{
1232 unsigned int maxname = abfd->xvec->ar_max_namelen;
dc810e39 1233 bfd_size_type total_namelen = 0;
252b5132
RH
1234 bfd *current;
1235 char *strptr;
1236
1237 *tablen = 0;
1238
047066e1 1239 /* Figure out how long the table should be. */
252b5132
RH
1240 for (current = abfd->archive_head; current != NULL; current = current->next)
1241 {
1242 const char *normal;
1243 unsigned int thislen;
1244
1245 normal = normalize (current, current->filename);
1246 if (normal == NULL)
b34976b6 1247 return FALSE;
252b5132
RH
1248
1249 thislen = strlen (normal);
1250
1251 if (thislen > maxname
1252 && (bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
1253 thislen = maxname;
1254
1255 if (thislen > maxname)
1256 {
1257 /* Add one to leave room for \n. */
1258 total_namelen += thislen + 1;
1259 if (trailing_slash)
1260 {
1261 /* Leave room for trailing slash. */
1262 ++total_namelen;
1263 }
1264 }
1265 else
1266 {
1267 struct ar_hdr *hdr = arch_hdr (current);
1268 if (strncmp (normal, hdr->ar_name, thislen) != 0
1269 || (thislen < sizeof hdr->ar_name
1270 && hdr->ar_name[thislen] != ar_padchar (current)))
1271 {
1272 /* Must have been using extended format even though it
1273 didn't need to. Fix it to use normal format. */
1274 memcpy (hdr->ar_name, normal, thislen);
1275 if (thislen < maxname
1276 || (thislen == maxname && thislen < sizeof hdr->ar_name))
1277 hdr->ar_name[thislen] = ar_padchar (current);
1278 }
1279 }
1280 }
1281
1282 if (total_namelen == 0)
b34976b6 1283 return TRUE;
252b5132
RH
1284
1285 *tabloc = bfd_zalloc (abfd, total_namelen);
1286 if (*tabloc == NULL)
b34976b6 1287 return FALSE;
252b5132
RH
1288
1289 *tablen = total_namelen;
1290 strptr = *tabloc;
1291
1292 for (current = abfd->archive_head; current != NULL; current =
1293 current->next)
1294 {
1295 const char *normal;
1296 unsigned int thislen;
1297
1298 normal = normalize (current, current->filename);
1299 if (normal == NULL)
b34976b6 1300 return FALSE;
252b5132
RH
1301
1302 thislen = strlen (normal);
1303 if (thislen > maxname)
1304 {
1305 /* Works for now; may need to be re-engineered if we
1306 encounter an oddball archive format and want to
d70910e8 1307 generalise this hack. */
252b5132
RH
1308 struct ar_hdr *hdr = arch_hdr (current);
1309 strcpy (strptr, normal);
1310 if (! trailing_slash)
1311 strptr[thislen] = '\012';
1312 else
1313 {
1314 strptr[thislen] = '/';
1315 strptr[thislen + 1] = '\012';
1316 }
1317 hdr->ar_name[0] = ar_padchar (current);
390c0e42
JJ
1318 _bfd_ar_spacepad (hdr->ar_name + 1, maxname - 1, "%-ld",
1319 strptr - *tabloc);
252b5132
RH
1320 strptr += thislen + 1;
1321 if (trailing_slash)
1322 ++strptr;
1323 }
1324 }
1325
b34976b6 1326 return TRUE;
252b5132
RH
1327}
1328\f
06fc8a8c 1329/* A couple of functions for creating ar_hdrs. */
252b5132 1330
23afc6f6
JL
1331#ifdef HPUX_LARGE_AR_IDS
1332/* Function to encode large UID/GID values according to HP. */
047066e1 1333
23afc6f6 1334static void
c58b9523 1335hpux_uid_gid_encode (char str[6], long int id)
23afc6f6
JL
1336{
1337 int cnt;
1338
1339 str[5] = '@' + (id & 3);
1340 id >>= 2;
1341
1342 for (cnt = 4; cnt >= 0; ++cnt, id >>= 6)
1343 str[cnt] = ' ' + (id & 0x3f);
1344}
1345#endif /* HPUX_LARGE_AR_IDS */
1346
633fd09f
ILT
1347#ifndef HAVE_GETUID
1348#define getuid() 0
1349#endif
1350
1351#ifndef HAVE_GETGID
1352#define getgid() 0
1353#endif
1354
252b5132
RH
1355/* Takes a filename, returns an arelt_data for it, or NULL if it can't
1356 make one. The filename must refer to a filename in the filesystem.
1357 The filename field of the ar_hdr will NOT be initialized. If member
d70910e8 1358 is set, and it's an in-memory bfd, we fake it. */
252b5132
RH
1359
1360static struct areltdata *
c58b9523 1361bfd_ar_hdr_from_filesystem (bfd *abfd, const char *filename, bfd *member)
252b5132
RH
1362{
1363 struct stat status;
1364 struct areltdata *ared;
1365 struct ar_hdr *hdr;
dc810e39 1366 bfd_size_type amt;
252b5132
RH
1367
1368 if (member && (member->flags & BFD_IN_MEMORY) != 0)
1369 {
047066e1 1370 /* Assume we just "made" the member, and fake it. */
c58b9523 1371 struct bfd_in_memory *bim = member->iostream;
047066e1
KH
1372 time (&status.st_mtime);
1373 status.st_uid = getuid ();
1374 status.st_gid = getgid ();
252b5132
RH
1375 status.st_mode = 0644;
1376 status.st_size = bim->size;
1377 }
1378 else if (stat (filename, &status) != 0)
1379 {
1380 bfd_set_error (bfd_error_system_call);
1381 return NULL;
1382 }
1383
dc810e39 1384 amt = sizeof (struct ar_hdr) + sizeof (struct areltdata);
c58b9523 1385 ared = bfd_zalloc (abfd, amt);
252b5132
RH
1386 if (ared == NULL)
1387 return NULL;
1388 hdr = (struct ar_hdr *) (((char *) ared) + sizeof (struct areltdata));
1389
047066e1 1390 /* ar headers are space padded, not null padded! */
c58b9523 1391 memset (hdr, ' ', sizeof (struct ar_hdr));
252b5132 1392
390c0e42
JJ
1393 _bfd_ar_spacepad (hdr->ar_date, sizeof (hdr->ar_date), "%-12ld",
1394 status.st_mtime);
23afc6f6
JL
1395#ifdef HPUX_LARGE_AR_IDS
1396 /* HP has a very "special" way to handle UID/GID's with numeric values
1397 > 99999. */
1398 if (status.st_uid > 99999)
390c0e42 1399 hpux_uid_gid_encode (hdr->ar_uid, (long) status.st_uid);
23afc6f6
JL
1400 else
1401#endif
390c0e42
JJ
1402 _bfd_ar_spacepad (hdr->ar_uid, sizeof (hdr->ar_uid), "%ld",
1403 status.st_uid);
23afc6f6
JL
1404#ifdef HPUX_LARGE_AR_IDS
1405 /* HP has a very "special" way to handle UID/GID's with numeric values
1406 > 99999. */
1407 if (status.st_gid > 99999)
390c0e42 1408 hpux_uid_gid_encode (hdr->ar_gid, (long) status.st_gid);
23afc6f6
JL
1409 else
1410#endif
390c0e42
JJ
1411 _bfd_ar_spacepad (hdr->ar_gid, sizeof (hdr->ar_gid), "%ld",
1412 status.st_gid);
1413 _bfd_ar_spacepad (hdr->ar_mode, sizeof (hdr->ar_mode), "%-8lo",
1414 status.st_mode);
1415 _bfd_ar_spacepad (hdr->ar_size, sizeof (hdr->ar_size), "%-10ld",
1416 status.st_size);
1417 memcpy (hdr->ar_fmag, ARFMAG, 2);
252b5132
RH
1418 ared->parsed_size = status.st_size;
1419 ared->arch_header = (char *) hdr;
1420
1421 return ared;
1422}
1423
1424/* This is magic required by the "ar" program. Since it's
047066e1
KH
1425 undocumented, it's undocumented. You may think that it would take
1426 a strong stomach to write this, and it does, but it takes even a
1427 stronger stomach to try to code around such a thing! */
252b5132 1428
c58b9523 1429struct ar_hdr *bfd_special_undocumented_glue (bfd *, const char *);
252b5132
RH
1430
1431struct ar_hdr *
c58b9523 1432bfd_special_undocumented_glue (bfd *abfd, const char *filename)
252b5132
RH
1433{
1434 struct areltdata *ar_elt = bfd_ar_hdr_from_filesystem (abfd, filename, 0);
1435 if (ar_elt == NULL)
1436 return NULL;
1437 return (struct ar_hdr *) ar_elt->arch_header;
1438}
1439
047066e1
KH
1440/* Analogous to stat call. */
1441
252b5132 1442int
c58b9523 1443bfd_generic_stat_arch_elt (bfd *abfd, struct stat *buf)
252b5132
RH
1444{
1445 struct ar_hdr *hdr;
1446 char *aloser;
1447
1448 if (abfd->arelt_data == NULL)
1449 {
1450 bfd_set_error (bfd_error_invalid_operation);
1451 return -1;
1452 }
1453
1454 hdr = arch_hdr (abfd);
1455
047066e1
KH
1456#define foo(arelt, stelt, size) \
1457 buf->stelt = strtol (hdr->arelt, &aloser, size); \
1458 if (aloser == hdr->arelt) \
1459 return -1;
1460
23afc6f6
JL
1461 /* Some platforms support special notations for large IDs. */
1462#ifdef HPUX_LARGE_AR_IDS
047066e1
KH
1463# define foo2(arelt, stelt, size) \
1464 if (hdr->arelt[5] == ' ') \
1465 { \
1466 foo (arelt, stelt, size); \
1467 } \
1468 else \
1469 { \
1470 int cnt; \
1471 for (buf->stelt = cnt = 0; cnt < 5; ++cnt) \
1472 { \
1473 if (hdr->arelt[cnt] < ' ' || hdr->arelt[cnt] > ' ' + 0x3f) \
1474 return -1; \
1475 buf->stelt <<= 6; \
1476 buf->stelt += hdr->arelt[cnt] - ' '; \
1477 } \
1478 if (hdr->arelt[5] < '@' || hdr->arelt[5] > '@' + 3) \
1479 return -1; \
1480 buf->stelt <<= 2; \
1481 buf->stelt += hdr->arelt[5] - '@'; \
1482 }
23afc6f6
JL
1483#else
1484# define foo2(arelt, stelt, size) foo (arelt, stelt, size)
1485#endif
252b5132
RH
1486
1487 foo (ar_date, st_mtime, 10);
23afc6f6
JL
1488 foo2 (ar_uid, st_uid, 10);
1489 foo2 (ar_gid, st_gid, 10);
252b5132
RH
1490 foo (ar_mode, st_mode, 8);
1491
1492 buf->st_size = arch_eltdata (abfd)->parsed_size;
1493
1494 return 0;
1495}
1496
1497void
c58b9523 1498bfd_dont_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
252b5132
RH
1499{
1500 /* FIXME: This interacts unpleasantly with ar's quick-append option.
1501 Fortunately ic960 users will never use that option. Fixing this
1502 is very hard; fortunately I know how to do it and will do so once
d70910e8 1503 intel's release is out the door. */
252b5132
RH
1504
1505 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
1506 size_t length;
1507 const char *filename;
1508 size_t maxlen = ar_maxnamelen (abfd);
1509
1510 if ((bfd_get_file_flags (abfd) & BFD_TRADITIONAL_FORMAT) != 0)
1511 {
1512 bfd_bsd_truncate_arname (abfd, pathname, arhdr);
1513 return;
1514 }
1515
1516 filename = normalize (abfd, pathname);
1517 if (filename == NULL)
1518 {
1519 /* FIXME */
1520 abort ();
1521 }
1522
1523 length = strlen (filename);
1524
1525 if (length <= maxlen)
1526 memcpy (hdr->ar_name, filename, length);
1527
1528 /* Add the padding character if there is room for it. */
1529 if (length < maxlen
1530 || (length == maxlen && length < sizeof hdr->ar_name))
1531 (hdr->ar_name)[length] = ar_padchar (abfd);
1532}
1533
1534void
c58b9523 1535bfd_bsd_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
252b5132
RH
1536{
1537 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
dc810e39
AM
1538 size_t length;
1539 const char *filename = strrchr (pathname, '/');
1540 size_t maxlen = ar_maxnamelen (abfd);
252b5132 1541
5af11cab
AM
1542#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1543 {
1544 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1545 char *bslash = strrchr (pathname, '\\');
2ab47eed 1546 if (filename == NULL || (bslash != NULL && bslash > filename))
5af11cab
AM
1547 filename = bslash;
1548 if (filename == NULL && pathname[0] != '\0' && pathname[1] == ':')
1549 filename = pathname + 1;
1550 }
1551#endif
1552
252b5132
RH
1553 if (filename == NULL)
1554 filename = pathname;
1555 else
1556 ++filename;
1557
1558 length = strlen (filename);
1559
1560 if (length <= maxlen)
1561 memcpy (hdr->ar_name, filename, length);
1562 else
1563 {
1564 /* pathname: meet procrustes */
1565 memcpy (hdr->ar_name, filename, maxlen);
1566 length = maxlen;
1567 }
1568
1569 if (length < maxlen)
1570 (hdr->ar_name)[length] = ar_padchar (abfd);
1571}
1572
1573/* Store name into ar header. Truncates the name to fit.
1574 1> strip pathname to be just the basename.
1575 2> if it's short enuf to fit, stuff it in.
1576 3> If it doesn't end with .o, truncate it to fit
1577 4> truncate it before the .o, append .o, stuff THAT in. */
1578
1579/* This is what gnu ar does. It's better but incompatible with the
d70910e8 1580 bsd ar. */
252b5132
RH
1581
1582void
c58b9523 1583bfd_gnu_truncate_arname (bfd *abfd, const char *pathname, char *arhdr)
252b5132
RH
1584{
1585 struct ar_hdr *hdr = (struct ar_hdr *) arhdr;
dc810e39
AM
1586 size_t length;
1587 const char *filename = strrchr (pathname, '/');
1588 size_t maxlen = ar_maxnamelen (abfd);
252b5132 1589
5af11cab
AM
1590#ifdef HAVE_DOS_BASED_FILE_SYSTEM
1591 {
1592 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
1593 char *bslash = strrchr (pathname, '\\');
2ab47eed 1594 if (filename == NULL || (bslash != NULL && bslash > filename))
5af11cab
AM
1595 filename = bslash;
1596 if (filename == NULL && pathname[0] != '\0' && pathname[1] == ':')
1597 filename = pathname + 1;
1598 }
1599#endif
1600
252b5132
RH
1601 if (filename == NULL)
1602 filename = pathname;
1603 else
1604 ++filename;
1605
1606 length = strlen (filename);
1607
1608 if (length <= maxlen)
1609 memcpy (hdr->ar_name, filename, length);
1610 else
1611 { /* pathname: meet procrustes */
1612 memcpy (hdr->ar_name, filename, maxlen);
1613 if ((filename[length - 2] == '.') && (filename[length - 1] == 'o'))
1614 {
1615 hdr->ar_name[maxlen - 2] = '.';
1616 hdr->ar_name[maxlen - 1] = 'o';
1617 }
1618 length = maxlen;
1619 }
1620
1621 if (length < 16)
1622 (hdr->ar_name)[length] = ar_padchar (abfd);
1623}
1624\f
047066e1 1625/* The BFD is open for write and has its format set to bfd_archive. */
252b5132 1626
b34976b6 1627bfd_boolean
c58b9523 1628_bfd_write_archive_contents (bfd *arch)
252b5132
RH
1629{
1630 bfd *current;
1631 char *etable = NULL;
1632 bfd_size_type elength = 0;
1633 const char *ename = NULL;
b34976b6
AM
1634 bfd_boolean makemap = bfd_has_map (arch);
1635 /* If no .o's, don't bother to make a map. */
1636 bfd_boolean hasobjects = FALSE;
252b5132 1637 bfd_size_type wrote;
252b5132
RH
1638 int tries;
1639
1640 /* Verify the viability of all entries; if any of them live in the
1641 filesystem (as opposed to living in an archive open for input)
1642 then construct a fresh ar_hdr for them. */
1643 for (current = arch->archive_head; current; current = current->next)
1644 {
8000a618
DD
1645 /* This check is checking the bfds for the objects we're reading
1646 from (which are usually either an object file or archive on
1647 disk), not the archive entries we're writing to. We don't
1648 actually create bfds for the archive members, we just copy
1649 them byte-wise when we write out the archive. */
252b5132
RH
1650 if (bfd_write_p (current))
1651 {
1652 bfd_set_error (bfd_error_invalid_operation);
b34976b6 1653 return FALSE;
252b5132
RH
1654 }
1655 if (!current->arelt_data)
1656 {
1657 current->arelt_data =
c58b9523 1658 bfd_ar_hdr_from_filesystem (arch, current->filename, current);
252b5132 1659 if (!current->arelt_data)
b34976b6 1660 return FALSE;
252b5132 1661
047066e1 1662 /* Put in the file name. */
c58b9523
AM
1663 BFD_SEND (arch, _bfd_truncate_arname,
1664 (arch, current->filename, (char *) arch_hdr (current)));
252b5132
RH
1665 }
1666
1667 if (makemap && ! hasobjects)
047066e1 1668 { /* Don't bother if we won't make a map! */
0e71e495 1669 if ((bfd_check_format (current, bfd_object)))
b34976b6 1670 hasobjects = TRUE;
252b5132
RH
1671 }
1672 }
1673
1674 if (!BFD_SEND (arch, _bfd_construct_extended_name_table,
1675 (arch, &etable, &elength, &ename)))
b34976b6 1676 return FALSE;
252b5132
RH
1677
1678 if (bfd_seek (arch, (file_ptr) 0, SEEK_SET) != 0)
b34976b6 1679 return FALSE;
c58b9523 1680 wrote = bfd_bwrite (ARMAG, SARMAG, arch);
252b5132 1681 if (wrote != SARMAG)
b34976b6 1682 return FALSE;
252b5132
RH
1683
1684 if (makemap && hasobjects)
1685 {
82e51918 1686 if (! _bfd_compute_and_write_armap (arch, (unsigned int) elength))
b34976b6 1687 return FALSE;
252b5132
RH
1688 }
1689
1690 if (elength != 0)
1691 {
1692 struct ar_hdr hdr;
1693
390c0e42
JJ
1694 memset (&hdr, ' ', sizeof (struct ar_hdr));
1695 memcpy (hdr.ar_name, ename, strlen (ename));
252b5132 1696 /* Round size up to even number in archive header. */
390c0e42
JJ
1697 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld",
1698 (elength + 1) & ~(bfd_size_type) 1);
1699 memcpy (hdr.ar_fmag, ARFMAG, 2);
c58b9523 1700 if ((bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
252b5132 1701 != sizeof (struct ar_hdr))
dc810e39 1702 || bfd_bwrite (etable, elength, arch) != elength)
b34976b6 1703 return FALSE;
252b5132
RH
1704 if ((elength % 2) == 1)
1705 {
c58b9523 1706 if (bfd_bwrite ("\012", 1, arch) != 1)
b34976b6 1707 return FALSE;
252b5132
RH
1708 }
1709 }
1710
1711 for (current = arch->archive_head; current; current = current->next)
1712 {
1713 char buffer[DEFAULT_BUFFERSIZE];
1714 unsigned int remaining = arelt_size (current);
1715 struct ar_hdr *hdr = arch_hdr (current);
1716
047066e1 1717 /* Write ar header. */
c58b9523 1718 if (bfd_bwrite (hdr, sizeof (*hdr), arch)
dc810e39 1719 != sizeof (*hdr))
b34976b6 1720 return FALSE;
252b5132 1721 if (bfd_seek (current, (file_ptr) 0, SEEK_SET) != 0)
b34976b6 1722 return FALSE;
252b5132
RH
1723 while (remaining)
1724 {
1725 unsigned int amt = DEFAULT_BUFFERSIZE;
1726 if (amt > remaining)
1727 amt = remaining;
1728 errno = 0;
c58b9523 1729 if (bfd_bread (buffer, amt, current) != amt)
252b5132
RH
1730 {
1731 if (bfd_get_error () != bfd_error_system_call)
1732 bfd_set_error (bfd_error_malformed_archive);
b34976b6 1733 return FALSE;
252b5132 1734 }
c58b9523 1735 if (bfd_bwrite (buffer, amt, arch) != amt)
b34976b6 1736 return FALSE;
252b5132
RH
1737 remaining -= amt;
1738 }
1739 if ((arelt_size (current) % 2) == 1)
1740 {
c58b9523 1741 if (bfd_bwrite ("\012", 1, arch) != 1)
b34976b6 1742 return FALSE;
252b5132
RH
1743 }
1744 }
1745
1746 if (makemap && hasobjects)
1747 {
1748 /* Verify the timestamp in the archive file. If it would not be
1749 accepted by the linker, rewrite it until it would be. If
1750 anything odd happens, break out and just return. (The
1751 Berkeley linker checks the timestamp and refuses to read the
1752 table-of-contents if it is >60 seconds less than the file's
1753 modified-time. That painful hack requires this painful hack. */
1754 tries = 1;
1755 do
1756 {
1757 if (bfd_update_armap_timestamp (arch))
1758 break;
1759 (*_bfd_error_handler)
1760 (_("Warning: writing archive was slow: rewriting timestamp\n"));
1761 }
1762 while (++tries < 6);
1763 }
1764
b34976b6 1765 return TRUE;
252b5132
RH
1766}
1767\f
047066e1 1768/* Note that the namidx for the first symbol is 0. */
252b5132 1769
b34976b6 1770bfd_boolean
c58b9523 1771_bfd_compute_and_write_armap (bfd *arch, unsigned int elength)
252b5132
RH
1772{
1773 char *first_name = NULL;
1774 bfd *current;
1775 file_ptr elt_no = 0;
1776 struct orl *map = NULL;
06fc8a8c 1777 unsigned int orl_max = 1024; /* Fine initial default. */
dc810e39 1778 unsigned int orl_count = 0;
06fc8a8c 1779 int stridx = 0;
252b5132
RH
1780 asymbol **syms = NULL;
1781 long syms_max = 0;
b34976b6 1782 bfd_boolean ret;
dc810e39 1783 bfd_size_type amt;
252b5132 1784
d70910e8 1785 /* Dunno if this is the best place for this info... */
252b5132
RH
1786 if (elength != 0)
1787 elength += sizeof (struct ar_hdr);
1788 elength += elength % 2;
1789
c58b9523
AM
1790 amt = orl_max * sizeof (struct orl);
1791 map = bfd_malloc (amt);
252b5132
RH
1792 if (map == NULL)
1793 goto error_return;
1794
1795 /* We put the symbol names on the arch objalloc, and then discard
1796 them when done. */
c58b9523 1797 first_name = bfd_alloc (arch, 1);
252b5132
RH
1798 if (first_name == NULL)
1799 goto error_return;
1800
047066e1 1801 /* Drop all the files called __.SYMDEF, we're going to make our own. */
252b5132
RH
1802 while (arch->archive_head &&
1803 strcmp (arch->archive_head->filename, "__.SYMDEF") == 0)
1804 arch->archive_head = arch->archive_head->next;
1805
047066e1 1806 /* Map over each element. */
252b5132 1807 for (current = arch->archive_head;
c58b9523 1808 current != NULL;
252b5132
RH
1809 current = current->next, elt_no++)
1810 {
82e51918
AM
1811 if (bfd_check_format (current, bfd_object)
1812 && (bfd_get_file_flags (current) & HAS_SYMS) != 0)
252b5132
RH
1813 {
1814 long storage;
1815 long symcount;
1816 long src_count;
1817
1818 storage = bfd_get_symtab_upper_bound (current);
1819 if (storage < 0)
1820 goto error_return;
1821
1822 if (storage != 0)
1823 {
1824 if (storage > syms_max)
1825 {
1826 if (syms_max > 0)
1827 free (syms);
1828 syms_max = storage;
c58b9523 1829 syms = bfd_malloc (syms_max);
252b5132
RH
1830 if (syms == NULL)
1831 goto error_return;
1832 }
1833 symcount = bfd_canonicalize_symtab (current, syms);
1834 if (symcount < 0)
1835 goto error_return;
1836
047066e1
KH
1837 /* Now map over all the symbols, picking out the ones we
1838 want. */
252b5132
RH
1839 for (src_count = 0; src_count < symcount; src_count++)
1840 {
1841 flagword flags = (syms[src_count])->flags;
1842 asection *sec = syms[src_count]->section;
1843
77fb9c28
NC
1844 if ((flags & BSF_GLOBAL ||
1845 flags & BSF_WEAK ||
1846 flags & BSF_INDIRECT ||
1847 bfd_is_com_section (sec))
1848 && ! bfd_is_und_section (sec))
252b5132 1849 {
dc810e39 1850 bfd_size_type namelen;
77fb9c28
NC
1851 struct orl *new_map;
1852
047066e1 1853 /* This symbol will go into the archive header. */
252b5132
RH
1854 if (orl_count == orl_max)
1855 {
1856 orl_max *= 2;
c58b9523
AM
1857 amt = orl_max * sizeof (struct orl);
1858 new_map = bfd_realloc (map, amt);
1859 if (new_map == NULL)
252b5132
RH
1860 goto error_return;
1861
1862 map = new_map;
1863 }
1864
1865 namelen = strlen (syms[src_count]->name);
dc810e39 1866 amt = sizeof (char *);
c58b9523 1867 map[orl_count].name = bfd_alloc (arch, amt);
252b5132
RH
1868 if (map[orl_count].name == NULL)
1869 goto error_return;
1870 *(map[orl_count].name) = bfd_alloc (arch, namelen + 1);
1871 if (*(map[orl_count].name) == NULL)
1872 goto error_return;
1873 strcpy (*(map[orl_count].name), syms[src_count]->name);
dc810e39
AM
1874 map[orl_count].u.abfd = current;
1875 map[orl_count].namidx = stridx;
252b5132
RH
1876
1877 stridx += namelen + 1;
1878 ++orl_count;
77fb9c28 1879 }
252b5132
RH
1880 }
1881 }
1882
1883 /* Now ask the BFD to free up any cached information, so we
1884 don't fill all of memory with symbol tables. */
1885 if (! bfd_free_cached_info (current))
1886 goto error_return;
1887 }
1888 }
1889
047066e1 1890 /* OK, now we have collected all the data, let's write them out. */
252b5132
RH
1891 ret = BFD_SEND (arch, write_armap,
1892 (arch, elength, map, orl_count, stridx));
1893
1894 if (syms_max > 0)
1895 free (syms);
1896 if (map != NULL)
1897 free (map);
1898 if (first_name != NULL)
1899 bfd_release (arch, first_name);
1900
1901 return ret;
1902
1903 error_return:
1904 if (syms_max > 0)
1905 free (syms);
1906 if (map != NULL)
1907 free (map);
1908 if (first_name != NULL)
1909 bfd_release (arch, first_name);
1910
b34976b6 1911 return FALSE;
252b5132
RH
1912}
1913
b34976b6 1914bfd_boolean
c58b9523
AM
1915bsd_write_armap (bfd *arch,
1916 unsigned int elength,
1917 struct orl *map,
1918 unsigned int orl_count,
1919 int stridx)
252b5132
RH
1920{
1921 int padit = stridx & 1;
1922 unsigned int ranlibsize = orl_count * BSD_SYMDEF_SIZE;
1923 unsigned int stringsize = stridx + padit;
d70910e8 1924 /* Include 8 bytes to store ranlibsize and stringsize in output. */
252b5132
RH
1925 unsigned int mapsize = ranlibsize + stringsize + 8;
1926 file_ptr firstreal;
1927 bfd *current = arch->archive_head;
06fc8a8c 1928 bfd *last_elt = current; /* Last element arch seen. */
252b5132
RH
1929 bfd_byte temp[4];
1930 unsigned int count;
1931 struct ar_hdr hdr;
1932 struct stat statbuf;
252b5132
RH
1933
1934 firstreal = mapsize + elength + sizeof (struct ar_hdr) + SARMAG;
1935
1936 stat (arch->filename, &statbuf);
390c0e42
JJ
1937 memset (&hdr, ' ', sizeof (struct ar_hdr));
1938 memcpy (hdr.ar_name, RANLIBMAG, strlen (RANLIBMAG));
252b5132
RH
1939 /* Remember the timestamp, to keep it holy. But fudge it a little. */
1940 bfd_ardata (arch)->armap_timestamp = statbuf.st_mtime + ARMAP_TIME_OFFSET;
1941 bfd_ardata (arch)->armap_datepos = (SARMAG
1942 + offsetof (struct ar_hdr, ar_date[0]));
390c0e42
JJ
1943 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
1944 bfd_ardata (arch)->armap_timestamp);
1945 _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", getuid ());
1946 _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", getgid ());
1947 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld", mapsize);
1948 memcpy (hdr.ar_fmag, ARFMAG, 2);
c58b9523 1949 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
252b5132 1950 != sizeof (struct ar_hdr))
b34976b6 1951 return FALSE;
dc810e39 1952 H_PUT_32 (arch, ranlibsize, temp);
c58b9523 1953 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp))
b34976b6 1954 return FALSE;
252b5132
RH
1955
1956 for (count = 0; count < orl_count; count++)
1957 {
1958 bfd_byte buf[BSD_SYMDEF_SIZE];
1959
dc810e39 1960 if (map[count].u.abfd != last_elt)
252b5132
RH
1961 {
1962 do
1963 {
1964 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
1965 firstreal += firstreal % 2;
1966 current = current->next;
1967 }
dc810e39 1968 while (current != map[count].u.abfd);
06fc8a8c 1969 }
252b5132
RH
1970
1971 last_elt = current;
dc810e39
AM
1972 H_PUT_32 (arch, map[count].namidx, buf);
1973 H_PUT_32 (arch, firstreal, buf + BSD_SYMDEF_OFFSET_SIZE);
c58b9523 1974 if (bfd_bwrite (buf, BSD_SYMDEF_SIZE, arch)
dc810e39 1975 != BSD_SYMDEF_SIZE)
b34976b6 1976 return FALSE;
252b5132
RH
1977 }
1978
047066e1 1979 /* Now write the strings themselves. */
dc810e39 1980 H_PUT_32 (arch, stringsize, temp);
c58b9523 1981 if (bfd_bwrite (temp, sizeof (temp), arch) != sizeof (temp))
b34976b6 1982 return FALSE;
252b5132
RH
1983 for (count = 0; count < orl_count; count++)
1984 {
1985 size_t len = strlen (*map[count].name) + 1;
1986
c58b9523 1987 if (bfd_bwrite (*map[count].name, len, arch) != len)
b34976b6 1988 return FALSE;
252b5132
RH
1989 }
1990
1991 /* The spec sez this should be a newline. But in order to be
d70910e8 1992 bug-compatible for sun's ar we use a null. */
252b5132
RH
1993 if (padit)
1994 {
c58b9523 1995 if (bfd_bwrite ("", 1, arch) != 1)
b34976b6 1996 return FALSE;
252b5132
RH
1997 }
1998
b34976b6 1999 return TRUE;
252b5132
RH
2000}
2001
2002/* At the end of archive file handling, update the timestamp in the
2003 file, so the linker will accept it.
2004
b34976b6
AM
2005 Return TRUE if the timestamp was OK, or an unusual problem happened.
2006 Return FALSE if we updated the timestamp. */
252b5132 2007
b34976b6 2008bfd_boolean
c58b9523 2009_bfd_archive_bsd_update_armap_timestamp (bfd *arch)
252b5132
RH
2010{
2011 struct stat archstat;
2012 struct ar_hdr hdr;
252b5132
RH
2013
2014 /* Flush writes, get last-write timestamp from file, and compare it
2015 to the timestamp IN the file. */
2016 bfd_flush (arch);
2017 if (bfd_stat (arch, &archstat) == -1)
2018 {
5fe39cae 2019 bfd_perror (_("Reading archive file mod timestamp"));
047066e1
KH
2020
2021 /* Can't read mod time for some reason. */
b34976b6 2022 return TRUE;
252b5132
RH
2023 }
2024 if (archstat.st_mtime <= bfd_ardata (arch)->armap_timestamp)
047066e1 2025 /* OK by the linker's rules. */
b34976b6 2026 return TRUE;
252b5132
RH
2027
2028 /* Update the timestamp. */
2029 bfd_ardata (arch)->armap_timestamp = archstat.st_mtime + ARMAP_TIME_OFFSET;
2030
2031 /* Prepare an ASCII version suitable for writing. */
390c0e42
JJ
2032 memset (hdr.ar_date, ' ', sizeof (hdr.ar_date));
2033 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2034 bfd_ardata (arch)->armap_timestamp);
252b5132
RH
2035
2036 /* Write it into the file. */
2037 bfd_ardata (arch)->armap_datepos = (SARMAG
2038 + offsetof (struct ar_hdr, ar_date[0]));
2039 if (bfd_seek (arch, bfd_ardata (arch)->armap_datepos, SEEK_SET) != 0
c58b9523 2040 || (bfd_bwrite (hdr.ar_date, sizeof (hdr.ar_date), arch)
252b5132
RH
2041 != sizeof (hdr.ar_date)))
2042 {
5fe39cae 2043 bfd_perror (_("Writing updated armap timestamp"));
047066e1
KH
2044
2045 /* Some error while writing. */
b34976b6 2046 return TRUE;
252b5132
RH
2047 }
2048
047066e1 2049 /* We updated the timestamp successfully. */
b34976b6 2050 return FALSE;
252b5132
RH
2051}
2052\f
2053/* A coff armap looks like :
2054 lARMAG
2055 struct ar_hdr with name = '/'
2056 number of symbols
2057 offset of file for symbol 0
2058 offset of file for symbol 1
2059
2060 offset of file for symbol n-1
2061 symbol name 0
2062 symbol name 1
2063
06fc8a8c 2064 symbol name n-1 */
252b5132 2065
b34976b6 2066bfd_boolean
c58b9523
AM
2067coff_write_armap (bfd *arch,
2068 unsigned int elength,
2069 struct orl *map,
2070 unsigned int symbol_count,
2071 int stridx)
252b5132
RH
2072{
2073 /* The size of the ranlib is the number of exported symbols in the
08da05b0 2074 archive * the number of bytes in an int, + an int for the count. */
252b5132
RH
2075 unsigned int ranlibsize = (symbol_count * 4) + 4;
2076 unsigned int stringsize = stridx;
2077 unsigned int mapsize = stringsize + ranlibsize;
dc810e39 2078 unsigned int archive_member_file_ptr;
252b5132
RH
2079 bfd *current = arch->archive_head;
2080 unsigned int count;
2081 struct ar_hdr hdr;
252b5132
RH
2082 int padit = mapsize & 1;
2083
2084 if (padit)
2085 mapsize++;
2086
047066e1 2087 /* Work out where the first object file will go in the archive. */
252b5132
RH
2088 archive_member_file_ptr = (mapsize
2089 + elength
2090 + sizeof (struct ar_hdr)
2091 + SARMAG);
2092
390c0e42 2093 memset (&hdr, ' ', sizeof (struct ar_hdr));
252b5132 2094 hdr.ar_name[0] = '/';
390c0e42
JJ
2095 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld",
2096 mapsize);
2097 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
2098 time (NULL));
047066e1 2099 /* This, at least, is what Intel coff sets the values to. */
390c0e42
JJ
2100 _bfd_ar_spacepad (hdr.ar_uid, sizeof (hdr.ar_uid), "%ld", 0);
2101 _bfd_ar_spacepad (hdr.ar_gid, sizeof (hdr.ar_gid), "%ld", 0);
2102 _bfd_ar_spacepad (hdr.ar_mode, sizeof (hdr.ar_mode), "%-7lo", 0);
2103 memcpy (hdr.ar_fmag, ARFMAG, 2);
252b5132 2104
047066e1 2105 /* Write the ar header for this item and the number of symbols. */
c58b9523 2106 if (bfd_bwrite (&hdr, sizeof (struct ar_hdr), arch)
252b5132 2107 != sizeof (struct ar_hdr))
b34976b6 2108 return FALSE;
252b5132 2109
4dae1ae7 2110 if (!bfd_write_bigendian_4byte_int (arch, symbol_count))
b34976b6 2111 return FALSE;
252b5132
RH
2112
2113 /* Two passes, first write the file offsets for each symbol -
2114 remembering that each offset is on a two byte boundary. */
2115
2116 /* Write out the file offset for the file associated with each
2117 symbol, and remember to keep the offsets padded out. */
2118
2119 current = arch->archive_head;
2120 count = 0;
c58b9523 2121 while (current != NULL && count < symbol_count)
252b5132 2122 {
047066e1
KH
2123 /* For each symbol which is used defined in this object, write
2124 out the object file's address in the archive. */
252b5132 2125
dc810e39 2126 while (count < symbol_count && map[count].u.abfd == current)
252b5132 2127 {
4dae1ae7 2128 if (!bfd_write_bigendian_4byte_int (arch, archive_member_file_ptr))
b34976b6 2129 return FALSE;
252b5132
RH
2130 count++;
2131 }
047066e1 2132 /* Add size of this archive entry. */
c58b9523 2133 archive_member_file_ptr += arelt_size (current) + sizeof (struct ar_hdr);
047066e1 2134 /* Remember aboout the even alignment. */
252b5132
RH
2135 archive_member_file_ptr += archive_member_file_ptr % 2;
2136 current = current->next;
2137 }
2138
047066e1 2139 /* Now write the strings themselves. */
252b5132
RH
2140 for (count = 0; count < symbol_count; count++)
2141 {
2142 size_t len = strlen (*map[count].name) + 1;
2143
c58b9523 2144 if (bfd_bwrite (*map[count].name, len, arch) != len)
b34976b6 2145 return FALSE;
252b5132
RH
2146 }
2147
2148 /* The spec sez this should be a newline. But in order to be
d70910e8 2149 bug-compatible for arc960 we use a null. */
252b5132
RH
2150 if (padit)
2151 {
c58b9523 2152 if (bfd_bwrite ("", 1, arch) != 1)
b34976b6 2153 return FALSE;
252b5132
RH
2154 }
2155
b34976b6 2156 return TRUE;
252b5132 2157}
This page took 0.449638 seconds and 4 git commands to generate.