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